]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/shrinker.c
Move c_src dirs back to toplevel
[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 void shrinker_free(struct shrinker *s)
17 {
18         if (s->list.next) {
19                 mutex_lock(&shrinker_lock);
20                 list_del(&s->list);
21                 mutex_unlock(&shrinker_lock);
22         }
23         free(s);
24 }
25
26 struct shrinker *shrinker_alloc(unsigned int flags, const char *fmt, ...)
27 {
28         return calloc(sizeof(struct shrinker), 1);
29 }
30
31 int shrinker_register(struct shrinker *shrinker)
32 {
33         mutex_lock(&shrinker_lock);
34         list_add_tail(&shrinker->list, &shrinker_list);
35         mutex_unlock(&shrinker_lock);
36         return 0;
37 }
38
39 static void run_shrinkers_allocation_failed(gfp_t gfp_mask)
40 {
41         struct shrinker *shrinker;
42
43         mutex_lock(&shrinker_lock);
44         list_for_each_entry(shrinker, &shrinker_list, list) {
45                 struct shrink_control sc = { .gfp_mask  = gfp_mask, };
46
47                 unsigned long have = shrinker->count_objects(shrinker, &sc);
48
49                 sc.nr_to_scan = have / 8;
50
51                 shrinker->scan_objects(shrinker, &sc);
52         }
53         mutex_unlock(&shrinker_lock);
54 }
55
56 void run_shrinkers(gfp_t gfp_mask, bool allocation_failed)
57 {
58         struct shrinker *shrinker;
59         struct sysinfo info;
60         s64 want_shrink;
61
62         if (!(gfp_mask & GFP_KERNEL))
63                 return;
64
65         /* Fast out if there are no shrinkers to run. */
66         if (list_empty(&shrinker_list))
67                 return;
68
69         if (allocation_failed) {
70                 run_shrinkers_allocation_failed(gfp_mask);
71                 return;
72         }
73
74         si_meminfo(&info);
75
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;
79         if (want_shrink <= 0)
80                 return;
81
82         mutex_lock(&shrinker_lock);
83         list_for_each_entry(shrinker, &shrinker_list, list) {
84                 struct shrink_control sc = {
85                         .gfp_mask       = gfp_mask,
86                         .nr_to_scan     = want_shrink >> PAGE_SHIFT
87                 };
88
89                 shrinker->scan_objects(shrinker, &sc);
90         }
91         mutex_unlock(&shrinker_lock);
92 }
93
94 static int shrinker_thread(void *arg)
95 {
96         while (!kthread_should_stop()) {
97                 struct timespec to;
98                 int v;
99
100                 clock_gettime(CLOCK_MONOTONIC, &to);
101                 to.tv_sec += 1;
102                 __set_current_state(TASK_INTERRUPTIBLE);
103                 errno = 0;
104                 while ((v = READ_ONCE(current->state)) != TASK_RUNNING &&
105                        errno != ETIMEDOUT)
106                         futex(&current->state, FUTEX_WAIT_BITSET|FUTEX_PRIVATE_FLAG,
107                               v, &to, NULL, (uint32_t)~0);
108                 if (kthread_should_stop())
109                         break;
110                 if (v != TASK_RUNNING)
111                         __set_current_state(TASK_RUNNING);
112                 run_shrinkers(GFP_KERNEL, false);
113         }
114
115         return 0;
116 }
117
118 struct task_struct *shrinker_task;
119
120 __attribute__((constructor(103)))
121 static void shrinker_thread_init(void)
122 {
123         shrinker_task = kthread_run(shrinker_thread, NULL, "shrinkers");
124         BUG_ON(IS_ERR(shrinker_task));
125 }
126
127 #if 0
128 /*
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:
131  */
132 __attribute__((destructor(103)))
133 static void shrinker_thread_exit(void)
134 {
135         int ret = kthread_stop(shrinker_task);
136         BUG_ON(ret);
137
138         shrinker_task = NULL;
139 }
140 #endif