1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/slab.h>
4 #include <linux/debugfs.h>
5 #include <linux/seq_file.h>
6 #include <linux/shrinker.h>
7 #include <linux/memcontrol.h>
9 /* defined in vmscan.c */
10 extern struct rw_semaphore shrinker_rwsem;
11 extern struct list_head shrinker_list;
13 static DEFINE_IDA(shrinker_debugfs_ida);
14 static struct dentry *shrinker_debugfs_root;
16 static unsigned long shrinker_count_objects(struct shrinker *shrinker,
17 struct mem_cgroup *memcg,
18 unsigned long *count_per_node)
20 unsigned long nr, total = 0;
24 if (nid == 0 || (shrinker->flags & SHRINKER_NUMA_AWARE)) {
25 struct shrink_control sc = {
26 .gfp_mask = GFP_KERNEL,
31 nr = shrinker->count_objects(shrinker, &sc);
32 if (nr == SHRINK_EMPTY)
38 count_per_node[nid] = nr;
45 static int shrinker_debugfs_count_show(struct seq_file *m, void *v)
47 struct shrinker *shrinker = m->private;
48 unsigned long *count_per_node;
49 struct mem_cgroup *memcg;
54 count_per_node = kcalloc(nr_node_ids, sizeof(unsigned long), GFP_KERNEL);
58 ret = down_read_killable(&shrinker_rwsem);
60 kfree(count_per_node);
65 memcg_aware = shrinker->flags & SHRINKER_MEMCG_AWARE;
67 memcg = mem_cgroup_iter(NULL, NULL, NULL);
69 if (memcg && !mem_cgroup_online(memcg))
72 total = shrinker_count_objects(shrinker,
73 memcg_aware ? memcg : NULL,
76 seq_printf(m, "%lu", mem_cgroup_ino(memcg));
78 seq_printf(m, " %lu", count_per_node[nid]);
83 mem_cgroup_iter_break(NULL, memcg);
87 if (signal_pending(current)) {
88 mem_cgroup_iter_break(NULL, memcg);
92 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
95 up_read(&shrinker_rwsem);
97 kfree(count_per_node);
100 DEFINE_SHOW_ATTRIBUTE(shrinker_debugfs_count);
102 static int shrinker_debugfs_scan_open(struct inode *inode, struct file *file)
104 file->private_data = inode->i_private;
105 return nonseekable_open(inode, file);
108 static ssize_t shrinker_debugfs_scan_write(struct file *file,
109 const char __user *buf,
110 size_t size, loff_t *pos)
112 struct shrinker *shrinker = file->private_data;
113 unsigned long nr_to_scan = 0, ino, read_len;
114 struct shrink_control sc = {
115 .gfp_mask = GFP_KERNEL,
117 struct mem_cgroup *memcg = NULL;
122 read_len = size < (sizeof(kbuf) - 1) ? size : (sizeof(kbuf) - 1);
123 if (copy_from_user(kbuf, buf, read_len))
125 kbuf[read_len] = '\0';
127 if (sscanf(kbuf, "%lu %d %lu", &ino, &nid, &nr_to_scan) != 3)
130 if (nid < 0 || nid >= nr_node_ids)
136 if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
137 memcg = mem_cgroup_get_from_ino(ino);
138 if (!memcg || IS_ERR(memcg))
141 if (!mem_cgroup_online(memcg)) {
142 mem_cgroup_put(memcg);
145 } else if (ino != 0) {
149 ret = down_read_killable(&shrinker_rwsem);
151 mem_cgroup_put(memcg);
157 sc.nr_to_scan = nr_to_scan;
158 sc.nr_scanned = nr_to_scan;
160 shrinker->scan_objects(shrinker, &sc);
162 up_read(&shrinker_rwsem);
163 mem_cgroup_put(memcg);
168 static const struct file_operations shrinker_debugfs_scan_fops = {
169 .owner = THIS_MODULE,
170 .open = shrinker_debugfs_scan_open,
171 .write = shrinker_debugfs_scan_write,
174 int shrinker_debugfs_add(struct shrinker *shrinker)
176 struct dentry *entry;
180 lockdep_assert_held(&shrinker_rwsem);
182 /* debugfs isn't initialized yet, add debugfs entries later. */
183 if (!shrinker_debugfs_root)
186 id = ida_alloc(&shrinker_debugfs_ida, GFP_KERNEL);
189 shrinker->debugfs_id = id;
191 snprintf(buf, sizeof(buf), "%s-%d", shrinker->name, id);
193 /* create debugfs entry */
194 entry = debugfs_create_dir(buf, shrinker_debugfs_root);
196 ida_free(&shrinker_debugfs_ida, id);
197 return PTR_ERR(entry);
199 shrinker->debugfs_entry = entry;
201 debugfs_create_file("count", 0220, entry, shrinker,
202 &shrinker_debugfs_count_fops);
203 debugfs_create_file("scan", 0440, entry, shrinker,
204 &shrinker_debugfs_scan_fops);
208 int shrinker_debugfs_rename(struct shrinker *shrinker, const char *fmt, ...)
210 struct dentry *entry;
212 const char *new, *old;
217 new = kvasprintf_const(GFP_KERNEL, fmt, ap);
223 down_write(&shrinker_rwsem);
225 old = shrinker->name;
226 shrinker->name = new;
228 if (shrinker->debugfs_entry) {
229 snprintf(buf, sizeof(buf), "%s-%d", shrinker->name,
230 shrinker->debugfs_id);
232 entry = debugfs_rename(shrinker_debugfs_root,
233 shrinker->debugfs_entry,
234 shrinker_debugfs_root, buf);
236 ret = PTR_ERR(entry);
238 shrinker->debugfs_entry = entry;
241 up_write(&shrinker_rwsem);
247 EXPORT_SYMBOL(shrinker_debugfs_rename);
249 void shrinker_debugfs_remove(struct shrinker *shrinker)
251 lockdep_assert_held(&shrinker_rwsem);
253 kfree_const(shrinker->name);
254 shrinker->name = NULL;
256 if (!shrinker->debugfs_entry)
259 debugfs_remove_recursive(shrinker->debugfs_entry);
260 ida_free(&shrinker_debugfs_ida, shrinker->debugfs_id);
263 static int __init shrinker_debugfs_init(void)
265 struct shrinker *shrinker;
266 struct dentry *dentry;
269 dentry = debugfs_create_dir("shrinker", NULL);
271 return PTR_ERR(dentry);
272 shrinker_debugfs_root = dentry;
274 /* Create debugfs entries for shrinkers registered at boot */
275 down_write(&shrinker_rwsem);
276 list_for_each_entry(shrinker, &shrinker_list, list)
277 if (!shrinker->debugfs_entry) {
278 ret = shrinker_debugfs_add(shrinker);
282 up_write(&shrinker_rwsem);
286 late_initcall(shrinker_debugfs_init);