]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/shrinker.c
fix build for glibc prior to 2.33
[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 struct meminfo {
37         u64             total;
38         u64             available;
39 };
40
41 void si_meminfo(struct sysinfo *val)
42 {
43         long page_size = sysconf(_SC_PAGESIZE);
44         memset(val, 0, sizeof(*val));
45         val->mem_unit = 1;
46
47         val->totalram = sysconf(_SC_PHYS_PAGES) * page_size;
48         val->freeram  = sysconf(_SC_AVPHYS_PAGES) * page_size;
49 }
50
51 static void run_shrinkers_allocation_failed(gfp_t gfp_mask)
52 {
53         struct shrinker *shrinker;
54
55         mutex_lock(&shrinker_lock);
56         list_for_each_entry(shrinker, &shrinker_list, list) {
57                 struct shrink_control sc = { .gfp_mask  = gfp_mask, };
58
59                 unsigned long have = shrinker->count_objects(shrinker, &sc);
60
61                 sc.nr_to_scan = have / 8;
62
63                 shrinker->scan_objects(shrinker, &sc);
64         }
65         mutex_unlock(&shrinker_lock);
66 }
67
68 void run_shrinkers(gfp_t gfp_mask, bool allocation_failed)
69 {
70         struct shrinker *shrinker;
71         struct sysinfo info;
72         struct mallinfo malloc_info = mallinfo();
73         s64 want_shrink;
74
75         if (!(gfp_mask & GFP_KERNEL))
76                 return;
77
78         /* Fast out if there are no shrinkers to run. */
79         if (list_empty(&shrinker_list))
80                 return;
81
82         if (allocation_failed) {
83                 run_shrinkers_allocation_failed(gfp_mask);
84                 return;
85         }
86
87         si_meminfo(&info);
88
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);
92
93                 if (want_shrink <= 0)
94                         return;
95         } else {
96                 /* We want to play nice with other apps keep 6% avaliable, free 3% */
97                 want_shrink = (info.totalram >> 5);
98         }
99
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
105                 };
106
107                 shrinker->scan_objects(shrinker, &sc);
108         }
109         mutex_unlock(&shrinker_lock);
110 }
111
112 static int shrinker_thread(void *arg)
113 {
114         while (!kthread_should_stop()) {
115                 struct timespec to;
116                 int v;
117
118                 clock_gettime(CLOCK_MONOTONIC, &to);
119                 to.tv_sec += 1;
120                 __set_current_state(TASK_INTERRUPTIBLE);
121                 errno = 0;
122                 while ((v = READ_ONCE(current->state)) != TASK_RUNNING &&
123                        errno != ETIMEDOUT)
124                         futex(&current->state, FUTEX_WAIT_BITSET|FUTEX_PRIVATE_FLAG,
125                               v, &to, NULL, (uint32_t)~0);
126                 if (kthread_should_stop())
127                         break;
128                 if (v != TASK_RUNNING)
129                         __set_current_state(TASK_RUNNING);
130                 run_shrinkers(GFP_KERNEL, false);
131         }
132
133         return 0;
134 }
135
136 struct task_struct *shrinker_task;
137
138 __attribute__((constructor(103)))
139 static void shrinker_thread_init(void)
140 {
141         shrinker_task = kthread_run(shrinker_thread, NULL, "shrinkers");
142         BUG_ON(IS_ERR(shrinker_task));
143 }
144
145 #if 0
146 /*
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:
149  */
150 __attribute__((destructor(103)))
151 static void shrinker_thread_exit(void)
152 {
153         int ret = kthread_stop(shrinker_task);
154         BUG_ON(ret);
155
156         shrinker_task = NULL;
157 }
158 #endif