5 #include <linux/kthread.h>
6 #include <linux/list.h>
8 #include <linux/mutex.h>
9 #include <linux/shrinker.h>
11 #include "tools-util.h"
13 static LIST_HEAD(shrinker_list);
14 static DEFINE_MUTEX(shrinker_lock);
16 void shrinker_free(struct shrinker *s)
19 mutex_lock(&shrinker_lock);
21 mutex_unlock(&shrinker_lock);
26 struct shrinker *shrinker_alloc(unsigned int flags, const char *fmt, ...)
28 return calloc(sizeof(struct shrinker), 1);
31 int shrinker_register(struct shrinker *shrinker)
33 mutex_lock(&shrinker_lock);
34 list_add_tail(&shrinker->list, &shrinker_list);
35 mutex_unlock(&shrinker_lock);
39 static void run_shrinkers_allocation_failed(gfp_t gfp_mask)
41 struct shrinker *shrinker;
43 mutex_lock(&shrinker_lock);
44 list_for_each_entry(shrinker, &shrinker_list, list) {
45 struct shrink_control sc = { .gfp_mask = gfp_mask, };
47 unsigned long have = shrinker->count_objects(shrinker, &sc);
49 sc.nr_to_scan = have / 8;
51 shrinker->scan_objects(shrinker, &sc);
53 mutex_unlock(&shrinker_lock);
56 void run_shrinkers(gfp_t gfp_mask, bool allocation_failed)
58 struct shrinker *shrinker;
62 if (!(gfp_mask & GFP_KERNEL))
65 /* Fast out if there are no shrinkers to run. */
66 if (list_empty(&shrinker_list))
69 if (allocation_failed) {
70 run_shrinkers_allocation_failed(gfp_mask);
76 /* Aim for 6% of physical RAM free without anything in swap */
77 want_shrink = (info.totalram >> 4) - info.freeram
78 + info.totalswap - info.freeswap;
82 mutex_lock(&shrinker_lock);
83 list_for_each_entry(shrinker, &shrinker_list, list) {
84 struct shrink_control sc = {
86 .nr_to_scan = want_shrink >> PAGE_SHIFT
89 shrinker->scan_objects(shrinker, &sc);
91 mutex_unlock(&shrinker_lock);
94 static int shrinker_thread(void *arg)
96 while (!kthread_should_stop()) {
100 clock_gettime(CLOCK_MONOTONIC, &to);
102 __set_current_state(TASK_INTERRUPTIBLE);
104 while ((v = READ_ONCE(current->state)) != TASK_RUNNING &&
106 futex(¤t->state, FUTEX_WAIT_BITSET|FUTEX_PRIVATE_FLAG,
107 v, &to, NULL, (uint32_t)~0);
108 if (kthread_should_stop())
110 if (v != TASK_RUNNING)
111 __set_current_state(TASK_RUNNING);
112 run_shrinkers(GFP_KERNEL, false);
118 struct task_struct *shrinker_task;
120 __attribute__((constructor(103)))
121 static void shrinker_thread_init(void)
123 shrinker_task = kthread_run(shrinker_thread, NULL, "shrinkers");
124 BUG_ON(IS_ERR(shrinker_task));
129 * We seem to be hitting a rare segfault when shutting down the shrinker thread.
130 * Disabling this is going to cause some harmless warnings about memory leaks:
132 __attribute__((destructor(103)))
133 static void shrinker_thread_exit(void)
135 int ret = kthread_stop(shrinker_task);
138 shrinker_task = NULL;