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