xref: /freebsd/sys/compat/linuxkpi/common/src/linux_shrinker.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/queue.h>
36 #include <sys/eventhandler.h>
37 #include <sys/sx.h>
38 
39 #include <linux/compat.h>
40 #include <linux/shrinker.h>
41 
42 TAILQ_HEAD(, shrinker) lkpi_shrinkers = TAILQ_HEAD_INITIALIZER(lkpi_shrinkers);
43 static struct sx sx_shrinker;
44 
45 int
46 linuxkpi_register_shrinker(struct shrinker *s)
47 {
48 
49 	KASSERT(s != NULL, ("NULL shrinker"));
50 	KASSERT(s->count_objects != NULL, ("NULL shrinker"));
51 	KASSERT(s->scan_objects != NULL, ("NULL shrinker"));
52 	sx_xlock(&sx_shrinker);
53 	TAILQ_INSERT_TAIL(&lkpi_shrinkers, s, next);
54 	sx_xunlock(&sx_shrinker);
55 	return (0);
56 }
57 
58 void
59 linuxkpi_unregister_shrinker(struct shrinker *s)
60 {
61 
62 	sx_xlock(&sx_shrinker);
63 	TAILQ_REMOVE(&lkpi_shrinkers, s, next);
64 	sx_xunlock(&sx_shrinker);
65 }
66 
67 void
68 linuxkpi_synchronize_shrinkers(void)
69 {
70 
71 	sx_xlock(&sx_shrinker);
72 	sx_xunlock(&sx_shrinker);
73 }
74 
75 #define	SHRINKER_BATCH	512
76 
77 static void
78 shrinker_shrink(struct shrinker *s)
79 {
80 	struct shrink_control sc;
81 	unsigned long can_free;
82 	unsigned long batch;
83 	unsigned long scanned = 0;
84 	unsigned long ret;
85 
86 	can_free = s->count_objects(s, &sc);
87 	if (can_free <= 0)
88 		return;
89 
90 	batch = s->batch ? s->batch : SHRINKER_BATCH;
91 	while (scanned <= can_free) {
92 		sc.nr_to_scan = batch;
93 		ret = s->scan_objects(s, &sc);
94 		if (ret == SHRINK_STOP)
95 			break;
96 		scanned += batch;
97 	}
98 }
99 
100 static void
101 linuxkpi_vm_lowmem(void *arg __unused)
102 {
103 	struct shrinker *s;
104 
105 	sx_xlock(&sx_shrinker);
106 	TAILQ_FOREACH(s, &lkpi_shrinkers, next) {
107 		shrinker_shrink(s);
108 	}
109 	sx_xunlock(&sx_shrinker);
110 }
111 
112 static eventhandler_tag lowmem_tag;
113 
114 static void
115 linuxkpi_sysinit_shrinker(void *arg __unused)
116 {
117 
118 	sx_init(&sx_shrinker, "lkpi-shrinker");
119 	lowmem_tag = EVENTHANDLER_REGISTER(vm_lowmem, linuxkpi_vm_lowmem,
120 	    NULL, EVENTHANDLER_PRI_FIRST);
121 }
122 
123 static void
124 linuxkpi_sysuninit_shrinker(void *arg __unused)
125 {
126 
127 	sx_destroy(&sx_shrinker);
128 	EVENTHANDLER_DEREGISTER(vm_lowmem, lowmem_tag);
129 }
130 
131 SYSINIT(linuxkpi_shrinker, SI_SUB_DRIVERS, SI_ORDER_ANY,
132     linuxkpi_sysinit_shrinker, NULL);
133 SYSUNINIT(linuxkpi_shrinker, SI_SUB_DRIVERS, SI_ORDER_ANY,
134     linuxkpi_sysuninit_shrinker, NULL);
135