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 struct shrinker *shrinker_alloc(unsigned int flags, const char *fmt, ...)
18 return calloc(sizeof(struct shrinker), 1);
21 int shrinker_register(struct shrinker *shrinker)
23 mutex_lock(&shrinker_lock);
24 list_add_tail(&shrinker->list, &shrinker_list);
25 mutex_unlock(&shrinker_lock);
29 void unregister_shrinker(struct shrinker *shrinker)
31 mutex_lock(&shrinker_lock);
32 list_del(&shrinker->list);
33 mutex_unlock(&shrinker_lock);
41 void si_meminfo(struct sysinfo *val)
43 long page_size = sysconf(_SC_PAGESIZE);
44 memset(val, 0, sizeof(*val));
47 val->totalram = sysconf(_SC_PHYS_PAGES) * page_size;
48 val->freeram = sysconf(_SC_AVPHYS_PAGES) * page_size;
51 static void run_shrinkers_allocation_failed(gfp_t gfp_mask)
53 struct shrinker *shrinker;
55 mutex_lock(&shrinker_lock);
56 list_for_each_entry(shrinker, &shrinker_list, list) {
57 struct shrink_control sc = { .gfp_mask = gfp_mask, };
59 unsigned long have = shrinker->count_objects(shrinker, &sc);
61 sc.nr_to_scan = have / 8;
63 shrinker->scan_objects(shrinker, &sc);
65 mutex_unlock(&shrinker_lock);
68 void run_shrinkers(gfp_t gfp_mask, bool allocation_failed)
70 struct shrinker *shrinker;
72 struct mallinfo malloc_info = mallinfo();
75 if (!(gfp_mask & GFP_KERNEL))
78 /* Fast out if there are no shrinkers to run. */
79 if (list_empty(&shrinker_list))
82 if (allocation_failed) {
83 run_shrinkers_allocation_failed(gfp_mask);
89 if (info.totalram && info.totalram >> 4 < info.freeram) {
90 /* freeram goes up when system swaps, use malloced data instead */
91 want_shrink = -malloc_info.arena + (info.totalram / 10 * 8);
96 /* We want to play nice with other apps keep 6% avaliable, free 3% */
97 want_shrink = (info.totalram >> 5);
100 mutex_lock(&shrinker_lock);
101 list_for_each_entry(shrinker, &shrinker_list, list) {
102 struct shrink_control sc = {
103 .gfp_mask = gfp_mask,
104 .nr_to_scan = want_shrink >> PAGE_SHIFT
107 shrinker->scan_objects(shrinker, &sc);
109 mutex_unlock(&shrinker_lock);
112 static int shrinker_thread(void *arg)
114 while (!kthread_should_stop()) {
118 clock_gettime(CLOCK_MONOTONIC, &to);
120 __set_current_state(TASK_INTERRUPTIBLE);
122 while ((v = READ_ONCE(current->state)) != TASK_RUNNING &&
124 futex(¤t->state, FUTEX_WAIT_BITSET|FUTEX_PRIVATE_FLAG,
125 v, &to, NULL, (uint32_t)~0);
126 if (kthread_should_stop())
128 if (v != TASK_RUNNING)
129 __set_current_state(TASK_RUNNING);
130 run_shrinkers(GFP_KERNEL, false);
136 struct task_struct *shrinker_task;
138 __attribute__((constructor(103)))
139 static void shrinker_thread_init(void)
141 shrinker_task = kthread_run(shrinker_thread, NULL, "shrinkers");
142 BUG_ON(IS_ERR(shrinker_task));
147 * We seem to be hitting a rare segfault when shutting down the shrinker thread.
148 * Disabling this is going to cause some harmless warnings about memory leaks:
150 __attribute__((destructor(103)))
151 static void shrinker_thread_exit(void)
153 int ret = kthread_stop(shrinker_task);
156 shrinker_task = NULL;