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 
26 
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/queue.h>
32 #include <sys/eventhandler.h>
33 #include <sys/sx.h>
34 
35 #include <linux/compat.h>
36 #include <linux/shrinker.h>
37 
38 TAILQ_HEAD(, shrinker) lkpi_shrinkers = TAILQ_HEAD_INITIALIZER(lkpi_shrinkers);
39 static struct sx sx_shrinker;
40 
41 int
42 linuxkpi_register_shrinker(struct shrinker *s)
43 {
44 
45 	KASSERT(s != NULL, ("NULL shrinker"));
46 	KASSERT(s->count_objects != NULL, ("NULL shrinker"));
47 	KASSERT(s->scan_objects != NULL, ("NULL shrinker"));
48 	sx_xlock(&sx_shrinker);
49 	TAILQ_INSERT_TAIL(&lkpi_shrinkers, s, next);
50 	sx_xunlock(&sx_shrinker);
51 	return (0);
52 }
53 
54 void
55 linuxkpi_unregister_shrinker(struct shrinker *s)
56 {
57 
58 	sx_xlock(&sx_shrinker);
59 	TAILQ_REMOVE(&lkpi_shrinkers, s, next);
60 	sx_xunlock(&sx_shrinker);
61 }
62 
63 void
64 linuxkpi_synchronize_shrinkers(void)
65 {
66 
67 	sx_xlock(&sx_shrinker);
68 	sx_xunlock(&sx_shrinker);
69 }
70 
71 #define	SHRINKER_BATCH	512
72 
73 static void
74 shrinker_shrink(struct shrinker *s)
75 {
76 	struct shrink_control sc;
77 	unsigned long can_free;
78 	unsigned long batch;
79 	unsigned long scanned = 0;
80 	unsigned long ret;
81 
82 	can_free = s->count_objects(s, &sc);
83 	if (can_free <= 0)
84 		return;
85 
86 	batch = s->batch ? s->batch : SHRINKER_BATCH;
87 	while (scanned <= can_free) {
88 		sc.nr_to_scan = batch;
89 		ret = s->scan_objects(s, &sc);
90 		if (ret == SHRINK_STOP)
91 			break;
92 		scanned += batch;
93 	}
94 }
95 
96 static void
97 linuxkpi_vm_lowmem(void *arg __unused)
98 {
99 	struct shrinker *s;
100 
101 	sx_xlock(&sx_shrinker);
102 	TAILQ_FOREACH(s, &lkpi_shrinkers, next) {
103 		shrinker_shrink(s);
104 	}
105 	sx_xunlock(&sx_shrinker);
106 }
107 
108 static eventhandler_tag lowmem_tag;
109 
110 static void
111 linuxkpi_sysinit_shrinker(void *arg __unused)
112 {
113 
114 	sx_init(&sx_shrinker, "lkpi-shrinker");
115 	lowmem_tag = EVENTHANDLER_REGISTER(vm_lowmem, linuxkpi_vm_lowmem,
116 	    NULL, EVENTHANDLER_PRI_FIRST);
117 }
118 
119 static void
120 linuxkpi_sysuninit_shrinker(void *arg __unused)
121 {
122 
123 	sx_destroy(&sx_shrinker);
124 	EVENTHANDLER_DEREGISTER(vm_lowmem, lowmem_tag);
125 }
126 
127 SYSINIT(linuxkpi_shrinker, SI_SUB_DRIVERS, SI_ORDER_ANY,
128     linuxkpi_sysinit_shrinker, NULL);
129 SYSUNINIT(linuxkpi_shrinker, SI_SUB_DRIVERS, SI_ORDER_ANY,
130     linuxkpi_sysuninit_shrinker, NULL);
131