1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SHRINKER_H
3 #define _LINUX_SHRINKER_H
4 
5 #include <linux/atomic.h>
6 #include <linux/types.h>
7 
8 /*
9  * This struct is used to pass information from page reclaim to the shrinkers.
10  * We consolidate the values for easier extension later.
11  *
12  * The 'gfpmask' refers to the allocation we are currently trying to
13  * fulfil.
14  */
15 struct shrink_control {
16 	gfp_t gfp_mask;
17 
18 	/* current node being shrunk (for NUMA aware shrinkers) */
19 	int nid;
20 
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 	 */
26 	unsigned long nr_to_scan;
27 
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 
35 	/* current memcg being shrunk (for memcg aware shrinkers) */
36 	struct mem_cgroup *memcg;
37 };
38 
39 #define SHRINK_STOP (~0UL)
40 #define SHRINK_EMPTY (~0UL - 1)
41 /*
42  * A callback you can register to apply pressure to ageable caches.
43  *
44  * @count_objects should return the number of freeable items in the cache. If
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
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.
52  *
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.
60  *
61  * @flags determine the shrinker abilities, like numa awareness
62  */
63 struct shrinker {
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 
69 	long batch;	/* reclaim batch size, 0 = default */
70 	int seeks;	/* seeks to recreate an obj */
71 	unsigned flags;
72 
73 	/* These are for internal use */
74 	struct list_head list;
75 #ifdef CONFIG_MEMCG
76 	/* ID in shrinker_idr */
77 	int id;
78 #endif
79 #ifdef CONFIG_SHRINKER_DEBUG
80 	int debugfs_id;
81 	const char *name;
82 	struct dentry *debugfs_entry;
83 #endif
84 	/* objs pending delete, per node */
85 	atomic_long_t *nr_deferred;
86 };
87 #define DEFAULT_SEEKS 2 /* A good number if you don't know better. */
88 
89 /* Flags */
90 #define SHRINKER_REGISTERED	(1 << 0)
91 #define SHRINKER_NUMA_AWARE	(1 << 1)
92 #define SHRINKER_MEMCG_AWARE	(1 << 2)
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  */
97 #define SHRINKER_NONSLAB	(1 << 3)
98 
99 extern int __printf(2, 3) prealloc_shrinker(struct shrinker *shrinker,
100 					    const char *fmt, ...);
101 extern void register_shrinker_prepared(struct shrinker *shrinker);
102 extern int __printf(2, 3) register_shrinker(struct shrinker *shrinker,
103 					    const char *fmt, ...);
104 extern void unregister_shrinker(struct shrinker *shrinker);
105 extern void free_prealloced_shrinker(struct shrinker *shrinker);
106 extern void synchronize_shrinkers(void);
107 
108 #ifdef CONFIG_SHRINKER_DEBUG
109 extern int shrinker_debugfs_add(struct shrinker *shrinker);
110 extern struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker,
111 					      int *debugfs_id);
112 extern void shrinker_debugfs_remove(struct dentry *debugfs_entry,
113 				    int debugfs_id);
114 extern int __printf(2, 3) shrinker_debugfs_rename(struct shrinker *shrinker,
115 						  const char *fmt, ...);
116 #else /* CONFIG_SHRINKER_DEBUG */
shrinker_debugfs_add(struct shrinker * shrinker)117 static inline int shrinker_debugfs_add(struct shrinker *shrinker)
118 {
119 	return 0;
120 }
shrinker_debugfs_detach(struct shrinker * shrinker,int * debugfs_id)121 static inline struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker,
122 						     int *debugfs_id)
123 {
124 	*debugfs_id = -1;
125 	return NULL;
126 }
shrinker_debugfs_remove(struct dentry * debugfs_entry,int debugfs_id)127 static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry,
128 					   int debugfs_id)
129 {
130 }
131 static inline __printf(2, 3)
shrinker_debugfs_rename(struct shrinker * shrinker,const char * fmt,...)132 int shrinker_debugfs_rename(struct shrinker *shrinker, const char *fmt, ...)
133 {
134 	return 0;
135 }
136 #endif /* CONFIG_SHRINKER_DEBUG */
137 #endif /* _LINUX_SHRINKER_H */
138