Merge tag 'xfs-6.4-rc1-fixes' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-block.git] / include / linux / shrinker.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
b0d40c92
DC
2#ifndef _LINUX_SHRINKER_H
3#define _LINUX_SHRINKER_H
4
b7217a0b
M
5#include <linux/atomic.h>
6#include <linux/types.h>
7
b0d40c92
DC
8/*
9 * This struct is used to pass information from page reclaim to the shrinkers.
06c88398 10 * We consolidate the values for easier extension later.
24f7c6b9
DC
11 *
12 * The 'gfpmask' refers to the allocation we are currently trying to
13 * fulfil.
b0d40c92
DC
14 */
15struct shrink_control {
16 gfp_t gfp_mask;
17
92be775a
KT
18 /* current node being shrunk (for NUMA aware shrinkers) */
19 int nid;
20
a0b02131
DC
21 /*
22 * How many objects scan_objects should scan and try to reclaim.
23 * This is reset before every call, so it is safe for callees
24 * to modify.
25 */
b0d40c92 26 unsigned long nr_to_scan;
0ce3d744 27
d460acb5
CW
28 /*
29 * How many objects did scan_objects process?
30 * This defaults to nr_to_scan before every call, but the callee
31 * should track its actual progress.
32 */
33 unsigned long nr_scanned;
34
cb731d6c
VD
35 /* current memcg being shrunk (for memcg aware shrinkers) */
36 struct mem_cgroup *memcg;
b0d40c92
DC
37};
38
24f7c6b9 39#define SHRINK_STOP (~0UL)
9b996468 40#define SHRINK_EMPTY (~0UL - 1)
b0d40c92
DC
41/*
42 * A callback you can register to apply pressure to ageable caches.
43 *
24f7c6b9 44 * @count_objects should return the number of freeable items in the cache. If
9b996468
KT
45 * there are no objects to free, it should return SHRINK_EMPTY, while 0 is
46 * returned in cases of the number of freeable items cannot be determined
47 * or shrinker should skip this cache for this time (e.g., their number
48 * is below shrinkable limit). No deadlock checks should be done during the
24f7c6b9
DC
49 * count callback - the shrinker relies on aggregating scan counts that couldn't
50 * be executed due to potential deadlocks to be run at a later call when the
51 * deadlock condition is no longer pending.
b0d40c92 52 *
24f7c6b9
DC
53 * @scan_objects will only be called if @count_objects returned a non-zero
54 * value for the number of freeable objects. The callout should scan the cache
55 * and attempt to free items from the cache. It should then return the number
56 * of objects freed during the scan, or SHRINK_STOP if progress cannot be made
57 * due to potential deadlocks. If SHRINK_STOP is returned, then no further
58 * attempts to call the @scan_objects will be made from the current reclaim
59 * context.
1d3d4437
GC
60 *
61 * @flags determine the shrinker abilities, like numa awareness
b0d40c92
DC
62 */
63struct shrinker {
24f7c6b9
DC
64 unsigned long (*count_objects)(struct shrinker *,
65 struct shrink_control *sc);
66 unsigned long (*scan_objects)(struct shrinker *,
67 struct shrink_control *sc);
68
b0d40c92 69 long batch; /* reclaim batch size, 0 = default */
e50ef89b
KT
70 int seeks; /* seeks to recreate an obj */
71 unsigned flags;
b0d40c92
DC
72
73 /* These are for internal use */
74 struct list_head list;
0a432dcb 75#ifdef CONFIG_MEMCG
b4c2b231
KT
76 /* ID in shrinker_idr */
77 int id;
5035ebc6
RG
78#endif
79#ifdef CONFIG_SHRINKER_DEBUG
80 int debugfs_id;
e33c267a 81 const char *name;
5035ebc6 82 struct dentry *debugfs_entry;
b4c2b231 83#endif
1d3d4437
GC
84 /* objs pending delete, per node */
85 atomic_long_t *nr_deferred;
b0d40c92
DC
86};
87#define DEFAULT_SEEKS 2 /* A good number if you don't know better. */
1d3d4437
GC
88
89/* Flags */
41ca668a
YS
90#define SHRINKER_REGISTERED (1 << 0)
91#define SHRINKER_NUMA_AWARE (1 << 1)
92#define SHRINKER_MEMCG_AWARE (1 << 2)
0a432dcb
YS
93/*
94 * It just makes sense when the shrinker is also MEMCG_AWARE for now,
95 * non-MEMCG_AWARE shrinker should not have this flag set.
96 */
41ca668a 97#define SHRINKER_NONSLAB (1 << 3)
1d3d4437 98
e33c267a
RG
99extern int __printf(2, 3) prealloc_shrinker(struct shrinker *shrinker,
100 const char *fmt, ...);
8e04944f 101extern void register_shrinker_prepared(struct shrinker *shrinker);
e33c267a
RG
102extern int __printf(2, 3) register_shrinker(struct shrinker *shrinker,
103 const char *fmt, ...);
8e04944f
TH
104extern void unregister_shrinker(struct shrinker *shrinker);
105extern void free_prealloced_shrinker(struct shrinker *shrinker);
880121be 106extern void synchronize_shrinkers(void);
5035ebc6
RG
107
108#ifdef CONFIG_SHRINKER_DEBUG
109extern int shrinker_debugfs_add(struct shrinker *shrinker);
badc28d4 110extern struct dentry *shrinker_debugfs_remove(struct shrinker *shrinker);
e33c267a
RG
111extern int __printf(2, 3) shrinker_debugfs_rename(struct shrinker *shrinker,
112 const char *fmt, ...);
5035ebc6
RG
113#else /* CONFIG_SHRINKER_DEBUG */
114static inline int shrinker_debugfs_add(struct shrinker *shrinker)
115{
116 return 0;
117}
badc28d4 118static inline struct dentry *shrinker_debugfs_remove(struct shrinker *shrinker)
5035ebc6 119{
badc28d4 120 return NULL;
5035ebc6 121}
e33c267a
RG
122static inline __printf(2, 3)
123int shrinker_debugfs_rename(struct shrinker *shrinker, const char *fmt, ...)
124{
125 return 0;
126}
5035ebc6
RG
127#endif /* CONFIG_SHRINKER_DEBUG */
128#endif /* _LINUX_SHRINKER_H */