]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/shrinker.c
8a24565ff15c94984db2e437aa37a35aa578c383
[bcachefs-tools-debian] / linux / shrinker.c
1
2 #include <stdio.h>
3 #include <unistd.h>
4
5 #include <linux/kthread.h>
6 #include <linux/list.h>
7 #include <linux/mm.h>
8 #include <linux/mutex.h>
9 #include <linux/shrinker.h>
10
11 #include "tools-util.h"
12
13 static LIST_HEAD(shrinker_list);
14 static DEFINE_MUTEX(shrinker_lock);
15
16 struct shrinker *shrinker_alloc(unsigned int flags, const char *fmt, ...)
17 {
18         return calloc(sizeof(struct shrinker), 1);
19 }
20
21 int shrinker_register(struct shrinker *shrinker)
22 {
23         mutex_lock(&shrinker_lock);
24         list_add_tail(&shrinker->list, &shrinker_list);
25         mutex_unlock(&shrinker_lock);
26         return 0;
27 }
28
29 void unregister_shrinker(struct shrinker *shrinker)
30 {
31         mutex_lock(&shrinker_lock);
32         list_del(&shrinker->list);
33         mutex_unlock(&shrinker_lock);
34 }
35
36 static void run_shrinkers_allocation_failed(gfp_t gfp_mask)
37 {
38         struct shrinker *shrinker;
39
40         mutex_lock(&shrinker_lock);
41         list_for_each_entry(shrinker, &shrinker_list, list) {
42                 struct shrink_control sc = { .gfp_mask  = gfp_mask, };
43
44                 unsigned long have = shrinker->count_objects(shrinker, &sc);
45
46                 sc.nr_to_scan = have / 8;
47
48                 shrinker->scan_objects(shrinker, &sc);
49         }
50         mutex_unlock(&shrinker_lock);
51 }
52
53 void run_shrinkers(gfp_t gfp_mask, bool allocation_failed)
54 {
55         struct shrinker *shrinker;
56         struct sysinfo info;
57         s64 want_shrink;
58
59         if (!(gfp_mask & GFP_KERNEL))
60                 return;
61
62         /* Fast out if there are no shrinkers to run. */
63         if (list_empty(&shrinker_list))
64                 return;
65
66         if (allocation_failed) {
67                 run_shrinkers_allocation_failed(gfp_mask);
68                 return;
69         }
70
71         si_meminfo(&info);
72
73         /* Aim for 6% of physical RAM free without anything in swap */
74         want_shrink = (info.totalram << 4) - info.freeram
75                         + info.totalswap - info.freeswap;
76         if (want_shrink <= 0)
77                 return;
78
79         mutex_lock(&shrinker_lock);
80         list_for_each_entry(shrinker, &shrinker_list, list) {
81                 struct shrink_control sc = {
82                         .gfp_mask       = gfp_mask,
83                         .nr_to_scan     = want_shrink >> PAGE_SHIFT
84                 };
85
86                 shrinker->scan_objects(shrinker, &sc);
87         }
88         mutex_unlock(&shrinker_lock);
89 }
90
91 static int shrinker_thread(void *arg)
92 {
93         while (!kthread_should_stop()) {
94                 struct timespec to;
95                 int v;
96
97                 clock_gettime(CLOCK_MONOTONIC, &to);
98                 to.tv_sec += 1;
99                 __set_current_state(TASK_INTERRUPTIBLE);
100                 errno = 0;
101                 while ((v = READ_ONCE(current->state)) != TASK_RUNNING &&
102                        errno != ETIMEDOUT)
103                         futex(&current->state, FUTEX_WAIT_BITSET|FUTEX_PRIVATE_FLAG,
104                               v, &to, NULL, (uint32_t)~0);
105                 if (kthread_should_stop())
106                         break;
107                 if (v != TASK_RUNNING)
108                         __set_current_state(TASK_RUNNING);
109                 run_shrinkers(GFP_KERNEL, false);
110         }
111
112         return 0;
113 }
114
115 struct task_struct *shrinker_task;
116
117 __attribute__((constructor(103)))
118 static void shrinker_thread_init(void)
119 {
120         shrinker_task = kthread_run(shrinker_thread, NULL, "shrinkers");
121         BUG_ON(IS_ERR(shrinker_task));
122 }
123
124 #if 0
125 /*
126  * We seem to be hitting a rare segfault when shutting down the shrinker thread.
127  * Disabling this is going to cause some harmless warnings about memory leaks:
128  */
129 __attribute__((destructor(103)))
130 static void shrinker_thread_exit(void)
131 {
132         int ret = kthread_stop(shrinker_task);
133         BUG_ON(ret);
134
135         shrinker_task = NULL;
136 }
137 #endif