xref: /linux-6.15/include/linux/shrinker.h (revision 9d64fc08)
1 #ifndef _LINUX_SHRINKER_H
2 #define _LINUX_SHRINKER_H
3 
4 /*
5  * This struct is used to pass information from page reclaim to the shrinkers.
6  * We consolidate the values for easier extention later.
7  *
8  * The 'gfpmask' refers to the allocation we are currently trying to
9  * fulfil.
10  */
11 struct shrink_control {
12 	gfp_t gfp_mask;
13 
14 	/*
15 	 * How many objects scan_objects should scan and try to reclaim.
16 	 * This is reset before every call, so it is safe for callees
17 	 * to modify.
18 	 */
19 	unsigned long nr_to_scan;
20 
21 	/*
22 	 * How many objects did scan_objects process?
23 	 * This defaults to nr_to_scan before every call, but the callee
24 	 * should track its actual progress.
25 	 */
26 	unsigned long nr_scanned;
27 
28 	/* current node being shrunk (for NUMA aware shrinkers) */
29 	int nid;
30 
31 	/* current memcg being shrunk (for memcg aware shrinkers) */
32 	struct mem_cgroup *memcg;
33 };
34 
35 #define SHRINK_STOP (~0UL)
36 /*
37  * A callback you can register to apply pressure to ageable caches.
38  *
39  * @count_objects should return the number of freeable items in the cache. If
40  * there are no objects to free or the number of freeable items cannot be
41  * determined, it should return 0. No deadlock checks should be done during the
42  * count callback - the shrinker relies on aggregating scan counts that couldn't
43  * be executed due to potential deadlocks to be run at a later call when the
44  * deadlock condition is no longer pending.
45  *
46  * @scan_objects will only be called if @count_objects returned a non-zero
47  * value for the number of freeable objects. The callout should scan the cache
48  * and attempt to free items from the cache. It should then return the number
49  * of objects freed during the scan, or SHRINK_STOP if progress cannot be made
50  * due to potential deadlocks. If SHRINK_STOP is returned, then no further
51  * attempts to call the @scan_objects will be made from the current reclaim
52  * context.
53  *
54  * @flags determine the shrinker abilities, like numa awareness
55  */
56 struct shrinker {
57 	unsigned long (*count_objects)(struct shrinker *,
58 				       struct shrink_control *sc);
59 	unsigned long (*scan_objects)(struct shrinker *,
60 				      struct shrink_control *sc);
61 
62 	int seeks;	/* seeks to recreate an obj */
63 	long batch;	/* reclaim batch size, 0 = default */
64 	unsigned long flags;
65 
66 	/* These are for internal use */
67 	struct list_head list;
68 	/* objs pending delete, per node */
69 	atomic_long_t *nr_deferred;
70 };
71 #define DEFAULT_SEEKS 2 /* A good number if you don't know better. */
72 
73 /* Flags */
74 #define SHRINKER_NUMA_AWARE	(1 << 0)
75 #define SHRINKER_MEMCG_AWARE	(1 << 1)
76 
77 extern int register_shrinker(struct shrinker *);
78 extern void unregister_shrinker(struct shrinker *);
79 #endif
80