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 void *private_data; 74 75 /* These are for internal use */ 76 struct list_head list; 77 #ifdef CONFIG_MEMCG 78 /* ID in shrinker_idr */ 79 int id; 80 #endif 81 #ifdef CONFIG_SHRINKER_DEBUG 82 int debugfs_id; 83 const char *name; 84 struct dentry *debugfs_entry; 85 #endif 86 /* objs pending delete, per node */ 87 atomic_long_t *nr_deferred; 88 }; 89 #define DEFAULT_SEEKS 2 /* A good number if you don't know better. */ 90 91 /* Internal flags */ 92 #define SHRINKER_REGISTERED BIT(0) 93 #define SHRINKER_ALLOCATED BIT(1) 94 95 /* Flags for users to use */ 96 #define SHRINKER_NUMA_AWARE BIT(2) 97 #define SHRINKER_MEMCG_AWARE BIT(3) 98 /* 99 * It just makes sense when the shrinker is also MEMCG_AWARE for now, 100 * non-MEMCG_AWARE shrinker should not have this flag set. 101 */ 102 #define SHRINKER_NONSLAB BIT(4) 103 104 struct shrinker *shrinker_alloc(unsigned int flags, const char *fmt, ...); 105 void shrinker_register(struct shrinker *shrinker); 106 void shrinker_free(struct shrinker *shrinker); 107 108 extern int __printf(2, 3) prealloc_shrinker(struct shrinker *shrinker, 109 const char *fmt, ...); 110 extern void register_shrinker_prepared(struct shrinker *shrinker); 111 extern int __printf(2, 3) register_shrinker(struct shrinker *shrinker, 112 const char *fmt, ...); 113 extern void unregister_shrinker(struct shrinker *shrinker); 114 extern void free_prealloced_shrinker(struct shrinker *shrinker); 115 116 #ifdef CONFIG_SHRINKER_DEBUG 117 extern int __printf(2, 3) shrinker_debugfs_rename(struct shrinker *shrinker, 118 const char *fmt, ...); 119 #else /* CONFIG_SHRINKER_DEBUG */ 120 static inline __printf(2, 3) 121 int shrinker_debugfs_rename(struct shrinker *shrinker, const char *fmt, ...) 122 { 123 return 0; 124 } 125 #endif /* CONFIG_SHRINKER_DEBUG */ 126 #endif /* _LINUX_SHRINKER_H */ 127