1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_SHRINKER_H 2 #ifndef _LINUX_SHRINKER_H 3 #define _LINUX_SHRINKER_H 3 #define _LINUX_SHRINKER_H 4 4 5 #include <linux/atomic.h> << 6 #include <linux/types.h> << 7 #include <linux/refcount.h> << 8 #include <linux/completion.h> << 9 << 10 #define SHRINKER_UNIT_BITS BITS_PER_LONG << 11 << 12 /* << 13 * Bitmap and deferred work of shrinker::id co << 14 * shrinkers, which have elements charged to t << 15 */ << 16 struct shrinker_info_unit { << 17 atomic_long_t nr_deferred[SHRINKER_UNI << 18 DECLARE_BITMAP(map, SHRINKER_UNIT_BITS << 19 }; << 20 << 21 struct shrinker_info { << 22 struct rcu_head rcu; << 23 int map_nr_max; << 24 struct shrinker_info_unit *unit[]; << 25 }; << 26 << 27 /* 5 /* 28 * This struct is used to pass information fro 6 * This struct is used to pass information from page reclaim to the shrinkers. 29 * We consolidate the values for easier extens !! 7 * We consolidate the values for easier extention later. 30 * 8 * 31 * The 'gfpmask' refers to the allocation we a 9 * The 'gfpmask' refers to the allocation we are currently trying to 32 * fulfil. 10 * fulfil. 33 */ 11 */ 34 struct shrink_control { 12 struct shrink_control { 35 gfp_t gfp_mask; 13 gfp_t gfp_mask; 36 14 37 /* current node being shrunk (for NUMA << 38 int nid; << 39 << 40 /* 15 /* 41 * How many objects scan_objects shoul 16 * How many objects scan_objects should scan and try to reclaim. 42 * This is reset before every call, so 17 * This is reset before every call, so it is safe for callees 43 * to modify. 18 * to modify. 44 */ 19 */ 45 unsigned long nr_to_scan; 20 unsigned long nr_to_scan; 46 21 47 /* 22 /* 48 * How many objects did scan_objects p 23 * How many objects did scan_objects process? 49 * This defaults to nr_to_scan before 24 * This defaults to nr_to_scan before every call, but the callee 50 * should track its actual progress. 25 * should track its actual progress. 51 */ 26 */ 52 unsigned long nr_scanned; 27 unsigned long nr_scanned; 53 28 >> 29 /* current node being shrunk (for NUMA aware shrinkers) */ >> 30 int nid; >> 31 54 /* current memcg being shrunk (for mem 32 /* current memcg being shrunk (for memcg aware shrinkers) */ 55 struct mem_cgroup *memcg; 33 struct mem_cgroup *memcg; 56 }; 34 }; 57 35 58 #define SHRINK_STOP (~0UL) 36 #define SHRINK_STOP (~0UL) 59 #define SHRINK_EMPTY (~0UL - 1) << 60 /* 37 /* 61 * A callback you can register to apply pressu 38 * A callback you can register to apply pressure to ageable caches. 62 * 39 * 63 * @count_objects should return the number of 40 * @count_objects should return the number of freeable items in the cache. If 64 * there are no objects to free, it should ret !! 41 * there are no objects to free or the number of freeable items cannot be 65 * returned in cases of the number of freeable !! 42 * determined, it should return 0. No deadlock checks should be done during the 66 * or shrinker should skip this cache for this << 67 * is below shrinkable limit). No deadlock che << 68 * count callback - the shrinker relies on agg 43 * count callback - the shrinker relies on aggregating scan counts that couldn't 69 * be executed due to potential deadlocks to b 44 * be executed due to potential deadlocks to be run at a later call when the 70 * deadlock condition is no longer pending. 45 * deadlock condition is no longer pending. 71 * 46 * 72 * @scan_objects will only be called if @count 47 * @scan_objects will only be called if @count_objects returned a non-zero 73 * value for the number of freeable objects. T 48 * value for the number of freeable objects. The callout should scan the cache 74 * and attempt to free items from the cache. I 49 * and attempt to free items from the cache. It should then return the number 75 * of objects freed during the scan, or SHRINK 50 * of objects freed during the scan, or SHRINK_STOP if progress cannot be made 76 * due to potential deadlocks. If SHRINK_STOP 51 * due to potential deadlocks. If SHRINK_STOP is returned, then no further 77 * attempts to call the @scan_objects will be 52 * attempts to call the @scan_objects will be made from the current reclaim 78 * context. 53 * context. 79 * 54 * 80 * @flags determine the shrinker abilities, li 55 * @flags determine the shrinker abilities, like numa awareness 81 */ 56 */ 82 struct shrinker { 57 struct shrinker { 83 unsigned long (*count_objects)(struct 58 unsigned long (*count_objects)(struct shrinker *, 84 struct 59 struct shrink_control *sc); 85 unsigned long (*scan_objects)(struct s 60 unsigned long (*scan_objects)(struct shrinker *, 86 struct s 61 struct shrink_control *sc); 87 62 88 long batch; /* reclaim batch size, << 89 int seeks; /* seeks to recreate a 63 int seeks; /* seeks to recreate an obj */ 90 unsigned flags; !! 64 long batch; /* reclaim batch size, 0 = default */ 91 !! 65 unsigned long flags; 92 /* << 93 * The reference count of this shrinke << 94 * initial refcount of 1, then the loo << 95 * to use it via shrinker_try_get(). L << 96 * the initial refcount will be discar << 97 * asynchronously via RCU after its re << 98 */ << 99 refcount_t refcount; << 100 struct completion done; /* use to wait << 101 struct rcu_head rcu; << 102 << 103 void *private_data; << 104 66 105 /* These are for internal use */ 67 /* These are for internal use */ 106 struct list_head list; 68 struct list_head list; 107 #ifdef CONFIG_MEMCG << 108 /* ID in shrinker_idr */ << 109 int id; << 110 #endif << 111 #ifdef CONFIG_SHRINKER_DEBUG << 112 int debugfs_id; << 113 const char *name; << 114 struct dentry *debugfs_entry; << 115 #endif << 116 /* objs pending delete, per node */ 69 /* objs pending delete, per node */ 117 atomic_long_t *nr_deferred; 70 atomic_long_t *nr_deferred; 118 }; 71 }; 119 #define DEFAULT_SEEKS 2 /* A good number if yo 72 #define DEFAULT_SEEKS 2 /* A good number if you don't know better. */ 120 73 121 /* Internal flags */ !! 74 /* Flags */ 122 #define SHRINKER_REGISTERED BIT(0) !! 75 #define SHRINKER_NUMA_AWARE (1 << 0) 123 #define SHRINKER_ALLOCATED BIT(1) !! 76 #define SHRINKER_MEMCG_AWARE (1 << 1) 124 !! 77 125 /* Flags for users to use */ !! 78 extern int prealloc_shrinker(struct shrinker *shrinker); 126 #define SHRINKER_NUMA_AWARE BIT(2) !! 79 extern void register_shrinker_prepared(struct shrinker *shrinker); 127 #define SHRINKER_MEMCG_AWARE BIT(3) !! 80 extern int register_shrinker(struct shrinker *shrinker); 128 /* !! 81 extern void unregister_shrinker(struct shrinker *shrinker); 129 * It just makes sense when the shrinker is al !! 82 extern void free_prealloced_shrinker(struct shrinker *shrinker); 130 * non-MEMCG_AWARE shrinker should not have th !! 83 #endif 131 */ << 132 #define SHRINKER_NONSLAB BIT(4) << 133 << 134 __printf(2, 3) << 135 struct shrinker *shrinker_alloc(unsigned int f << 136 void shrinker_register(struct shrinker *shrink << 137 void shrinker_free(struct shrinker *shrinker); << 138 << 139 static inline bool shrinker_try_get(struct shr << 140 { << 141 return refcount_inc_not_zero(&shrinker << 142 } << 143 << 144 static inline void shrinker_put(struct shrinke << 145 { << 146 if (refcount_dec_and_test(&shrinker->r << 147 complete(&shrinker->done); << 148 } << 149 << 150 #ifdef CONFIG_SHRINKER_DEBUG << 151 extern int __printf(2, 3) shrinker_debugfs_ren << 152 << 153 #else /* CONFIG_SHRINKER_DEBUG */ << 154 static inline __printf(2, 3) << 155 int shrinker_debugfs_rename(struct shrinker *s << 156 { << 157 return 0; << 158 } << 159 #endif /* CONFIG_SHRINKER_DEBUG */ << 160 #endif /* _LINUX_SHRINKER_H */ << 161 84
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.