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 #define SHRINKER_UNIT_BITS BITS_PER_LONG 9 10 /* 11 * Bitmap and deferred work of shrinker::id corresponding to memcg-aware 12 * shrinkers, which have elements charged to the memcg. 13 */ 14 struct shrinker_info_unit { 15 atomic_long_t nr_deferred[SHRINKER_UNIT_BITS]; 16 DECLARE_BITMAP(map, SHRINKER_UNIT_BITS); 17 }; 18 19 struct shrinker_info { 20 struct rcu_head rcu; 21 int map_nr_max; 22 struct shrinker_info_unit *unit[]; 23 }; 24 25 /* 26 * This struct is used to pass information from page reclaim to the shrinkers. 27 * We consolidate the values for easier extension later. 28 * 29 * The 'gfpmask' refers to the allocation we are currently trying to 30 * fulfil. 31 */ 32 struct shrink_control { 33 gfp_t gfp_mask; 34 35 /* current node being shrunk (for NUMA aware shrinkers) */ 36 int nid; 37 38 /* 39 * How many objects scan_objects should scan and try to reclaim. 40 * This is reset before every call, so it is safe for callees 41 * to modify. 42 */ 43 unsigned long nr_to_scan; 44 45 /* 46 * How many objects did scan_objects process? 47 * This defaults to nr_to_scan before every call, but the callee 48 * should track its actual progress. 49 */ 50 unsigned long nr_scanned; 51 52 /* current memcg being shrunk (for memcg aware shrinkers) */ 53 struct mem_cgroup *memcg; 54 }; 55 56 #define SHRINK_STOP (~0UL) 57 #define SHRINK_EMPTY (~0UL - 1) 58 /* 59 * A callback you can register to apply pressure to ageable caches. 60 * 61 * @count_objects should return the number of freeable items in the cache. If 62 * there are no objects to free, it should return SHRINK_EMPTY, while 0 is 63 * returned in cases of the number of freeable items cannot be determined 64 * or shrinker should skip this cache for this time (e.g., their number 65 * is below shrinkable limit). No deadlock checks should be done during the 66 * count callback - the shrinker relies on aggregating scan counts that couldn't 67 * be executed due to potential deadlocks to be run at a later call when the 68 * deadlock condition is no longer pending. 69 * 70 * @scan_objects will only be called if @count_objects returned a non-zero 71 * value for the number of freeable objects. The callout should scan the cache 72 * and attempt to free items from the cache. It should then return the number 73 * of objects freed during the scan, or SHRINK_STOP if progress cannot be made 74 * due to potential deadlocks. If SHRINK_STOP is returned, then no further 75 * attempts to call the @scan_objects will be made from the current reclaim 76 * context. 77 * 78 * @flags determine the shrinker abilities, like numa awareness 79 */ 80 struct shrinker { 81 unsigned long (*count_objects)(struct shrinker *, 82 struct shrink_control *sc); 83 unsigned long (*scan_objects)(struct shrinker *, 84 struct shrink_control *sc); 85 86 long batch; /* reclaim batch size, 0 = default */ 87 int seeks; /* seeks to recreate an obj */ 88 unsigned flags; 89 90 void *private_data; 91 92 /* These are for internal use */ 93 struct list_head list; 94 #ifdef CONFIG_MEMCG 95 /* ID in shrinker_idr */ 96 int id; 97 #endif 98 #ifdef CONFIG_SHRINKER_DEBUG 99 int debugfs_id; 100 const char *name; 101 struct dentry *debugfs_entry; 102 #endif 103 /* objs pending delete, per node */ 104 atomic_long_t *nr_deferred; 105 }; 106 #define DEFAULT_SEEKS 2 /* A good number if you don't know better. */ 107 108 /* Internal flags */ 109 #define SHRINKER_REGISTERED BIT(0) 110 #define SHRINKER_ALLOCATED BIT(1) 111 112 /* Flags for users to use */ 113 #define SHRINKER_NUMA_AWARE BIT(2) 114 #define SHRINKER_MEMCG_AWARE BIT(3) 115 /* 116 * It just makes sense when the shrinker is also MEMCG_AWARE for now, 117 * non-MEMCG_AWARE shrinker should not have this flag set. 118 */ 119 #define SHRINKER_NONSLAB BIT(4) 120 121 struct shrinker *shrinker_alloc(unsigned int flags, const char *fmt, ...); 122 void shrinker_register(struct shrinker *shrinker); 123 void shrinker_free(struct shrinker *shrinker); 124 125 #ifdef CONFIG_SHRINKER_DEBUG 126 extern int __printf(2, 3) shrinker_debugfs_rename(struct shrinker *shrinker, 127 const char *fmt, ...); 128 #else /* CONFIG_SHRINKER_DEBUG */ 129 static inline __printf(2, 3) 130 int shrinker_debugfs_rename(struct shrinker *shrinker, const char *fmt, ...) 131 { 132 return 0; 133 } 134 #endif /* CONFIG_SHRINKER_DEBUG */ 135 #endif /* _LINUX_SHRINKER_H */ 136