]> git.proxmox.com Git - mirror_spl.git/blob - include/linux/mm_compat.h
Linux 3.12 compat: New shrinker API
[mirror_spl.git] / include / linux / mm_compat.h
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 \*****************************************************************************/
24
25 #ifndef _SPL_MM_COMPAT_H
26 #define _SPL_MM_COMPAT_H
27
28 #include <linux/mm.h>
29 #include <linux/fs.h>
30
31 /*
32 * Linux 2.6.31 API Change.
33 * Individual pages_{min,low,high} moved in to watermark array.
34 */
35 #ifndef min_wmark_pages
36 #define min_wmark_pages(z) (z->pages_min)
37 #endif
38
39 #ifndef low_wmark_pages
40 #define low_wmark_pages(z) (z->pages_low)
41 #endif
42
43 #ifndef high_wmark_pages
44 #define high_wmark_pages(z) (z->pages_high)
45 #endif
46
47 #if !defined(HAVE_SHRINK_CONTROL_STRUCT)
48 struct shrink_control {
49 gfp_t gfp_mask;
50 unsigned long nr_to_scan;
51 };
52 #endif /* HAVE_SHRINK_CONTROL_STRUCT */
53
54 /*
55 * 2.6.xx API compat,
56 * There currently exists no exposed API to partially shrink the dcache.
57 * The expected mechanism to shrink the cache is a registered shrinker
58 * which is called during memory pressure.
59 */
60 #ifndef HAVE_SHRINK_DCACHE_MEMORY
61 # if defined(HAVE_SHRINK_CONTROL_STRUCT)
62 typedef int (*shrink_dcache_memory_t)(struct shrinker *,
63 struct shrink_control *);
64 extern shrink_dcache_memory_t shrink_dcache_memory_fn;
65 # define shrink_dcache_memory(nr, gfp) \
66 ({ \
67 struct shrink_control sc = { .nr_to_scan = nr, .gfp_mask = gfp }; \
68 int __ret__ = 0; \
69 \
70 if (shrink_dcache_memory_fn) \
71 __ret__ = shrink_dcache_memory_fn(NULL, &sc); \
72 \
73 __ret__; \
74 })
75 # elif defined(HAVE_3ARGS_SHRINKER_CALLBACK)
76 typedef int (*shrink_dcache_memory_t)(struct shrinker *, int, gfp_t);
77 extern shrink_dcache_memory_t shrink_dcache_memory_fn;
78 # define shrink_dcache_memory(nr, gfp) \
79 ({ \
80 int __ret__ = 0; \
81 \
82 if (shrink_dcache_memory_fn) \
83 __ret__ = shrink_dcache_memory_fn(NULL, nr, gfp); \
84 \
85 __ret__; \
86 })
87 # else
88 typedef int (*shrink_dcache_memory_t)(int, gfp_t);
89 extern shrink_dcache_memory_t shrink_dcache_memory_fn;
90 # define shrink_dcache_memory(nr, gfp) \
91 ({ \
92 int __ret__ = 0; \
93 \
94 if (shrink_dcache_memory_fn) \
95 __ret__ = shrink_dcache_memory_fn(nr, gfp); \
96 \
97 __ret__; \
98 })
99 # endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
100 #endif /* HAVE_SHRINK_DCACHE_MEMORY */
101
102 /*
103 * 2.6.xx API compat,
104 * There currently exists no exposed API to partially shrink the icache.
105 * The expected mechanism to shrink the cache is a registered shrinker
106 * which is called during memory pressure.
107 */
108 #ifndef HAVE_SHRINK_ICACHE_MEMORY
109 # if defined(HAVE_SHRINK_CONTROL_STRUCT)
110 typedef int (*shrink_icache_memory_t)(struct shrinker *,
111 struct shrink_control *);
112 extern shrink_icache_memory_t shrink_icache_memory_fn;
113 # define shrink_icache_memory(nr, gfp) \
114 ({ \
115 struct shrink_control sc = { .nr_to_scan = nr, .gfp_mask = gfp }; \
116 int __ret__ = 0; \
117 \
118 if (shrink_icache_memory_fn) \
119 __ret__ = shrink_icache_memory_fn(NULL, &sc); \
120 \
121 __ret__; \
122 })
123 # elif defined(HAVE_3ARGS_SHRINKER_CALLBACK)
124 typedef int (*shrink_icache_memory_t)(struct shrinker *, int, gfp_t);
125 extern shrink_icache_memory_t shrink_icache_memory_fn;
126 # define shrink_icache_memory(nr, gfp) \
127 ({ \
128 int __ret__ = 0; \
129 \
130 if (shrink_icache_memory_fn) \
131 __ret__ = shrink_icache_memory_fn(NULL, nr, gfp); \
132 \
133 __ret__; \
134 })
135 # else
136 typedef int (*shrink_icache_memory_t)(int, gfp_t);
137 extern shrink_icache_memory_t shrink_icache_memory_fn;
138 # define shrink_icache_memory(nr, gfp) \
139 ({ \
140 int __ret__ = 0; \
141 \
142 if (shrink_icache_memory_fn) \
143 __ret__ = shrink_icache_memory_fn(nr, gfp); \
144 \
145 __ret__; \
146 })
147 # endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
148 #endif /* HAVE_SHRINK_ICACHE_MEMORY */
149
150 /*
151 * Due to frequent changes in the shrinker API the following
152 * compatibility wrappers should be used. They are as follows:
153 *
154 * SPL_SHRINKER_DECLARE is used to declare the shrinker which is
155 * passed to spl_register_shrinker()/spl_unregister_shrinker(). Use
156 * shrinker_name to set the shrinker variable name, shrinker_callback
157 * to set the callback function, and seek_cost to define the cost of
158 * reclaiming an object.
159 *
160 * SPL_SHRINKER_DECLARE(shrinker_name, shrinker_callback, seek_cost);
161 *
162 * SPL_SHRINKER_CALLBACK_FWD_DECLARE is used when a forward declaration
163 * of the shrinker callback function is required. Only the callback
164 * function needs to be passed.
165 *
166 * SPL_SHRINKER_CALLBACK_FWD_DECLARE(shrinker_callback);
167 *
168 * SPL_SHRINKER_CALLBACK_WRAPPER is used to declare the callback function
169 * which is registered with the shrinker. This function will call your
170 * custom shrinker which must use the following prototype. Notice the
171 * leading __'s, these must be appended to the callback_function name.
172 *
173 * int __shrinker_callback(struct shrinker *, struct shrink_control *)
174 * SPL_SHRINKER_CALLBACK_WRAPPER(shrinker_callback);a
175 *
176 *
177 * Example:
178 *
179 * SPL_SHRINKER_CALLBACK_FWD_DECLARE(my_shrinker_fn);
180 * SPL_SHRINKER_DECLARE(my_shrinker, my_shrinker_fn, 1);
181 *
182 * static int
183 * __my_shrinker_fn(struct shrinker *shrink, struct shrink_control *sc)
184 * {
185 * if (sc->nr_to_scan) {
186 * ...scan objects in the cache and reclaim them...
187 * }
188 *
189 * ...calculate number of objects in the cache...
190 *
191 * return (number of objects in the cache);
192 * }
193 * SPL_SHRINKER_CALLBACK_WRAPPER(my_shrinker_fn);
194 */
195
196 #define spl_register_shrinker(x) register_shrinker(x)
197 #define spl_unregister_shrinker(x) unregister_shrinker(x)
198
199 /*
200 * Linux 2.6.23 - 2.6.34 Shrinker API Compatibility.
201 */
202 #if defined(HAVE_2ARGS_OLD_SHRINKER_CALLBACK)
203 #define SPL_SHRINKER_DECLARE(s, x, y) \
204 static struct shrinker s = { \
205 .shrink = x, \
206 .seeks = y \
207 }
208
209 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
210 static int fn(int nr_to_scan, unsigned int gfp_mask)
211
212 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
213 static int \
214 fn(int nr_to_scan, unsigned int gfp_mask) \
215 { \
216 struct shrink_control sc; \
217 \
218 sc.nr_to_scan = nr_to_scan; \
219 sc.gfp_mask = gfp_mask; \
220 \
221 return (__ ## fn(NULL, &sc)); \
222 }
223
224 /*
225 * Linux 2.6.35 to 2.6.39 Shrinker API Compatibility.
226 */
227 #elif defined(HAVE_3ARGS_SHRINKER_CALLBACK)
228 #define SPL_SHRINKER_DECLARE(s, x, y) \
229 static struct shrinker s = { \
230 .shrink = x, \
231 .seeks = y \
232 }
233
234 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
235 static int fn(struct shrinker *, int, unsigned int)
236
237 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
238 static int \
239 fn(struct shrinker *shrink, int nr_to_scan, unsigned int gfp_mask) \
240 { \
241 struct shrink_control sc; \
242 \
243 sc.nr_to_scan = nr_to_scan; \
244 sc.gfp_mask = gfp_mask; \
245 \
246 return (__ ## fn(shrink, &sc)); \
247 }
248
249 /*
250 * Linux 3.0 to 3.11 Shrinker API Compatibility.
251 */
252 #elif defined(HAVE_2ARGS_NEW_SHRINKER_CALLBACK)
253 #define SPL_SHRINKER_DECLARE(s, x, y) \
254 static struct shrinker s = { \
255 .shrink = x, \
256 .seeks = y \
257 }
258
259 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
260 static int fn(struct shrinker *, struct shrink_control *)
261
262 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
263 static int \
264 fn(struct shrinker *shrink, struct shrink_control *sc) \
265 { \
266 return (__ ## fn(shrink, sc)); \
267 }
268
269 /*
270 * Linux 3.12 and later Shrinker API Compatibility.
271 */
272 #elif defined(HAVE_SPLIT_SHRINKER_CALLBACK)
273 #define SPL_SHRINKER_DECLARE(s, x, y) \
274 static struct shrinker s = { \
275 .count_objects = x ## _count_objects, \
276 .scan_objects = x ## _scan_objects, \
277 .seeks = y \
278 }
279
280 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
281 static unsigned long fn ## _count_objects(struct shrinker *, \
282 struct shrink_control *); \
283 static unsigned long fn ## _scan_objects(struct shrinker *, \
284 struct shrink_control *)
285
286 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
287 static unsigned long \
288 fn ## _count_objects(struct shrinker *shrink, struct shrink_control *sc)\
289 { \
290 int __ret__; \
291 \
292 sc->nr_to_scan = 0; \
293 __ret__ = __ ## fn(NULL, sc); \
294 \
295 /* Errors may not be returned and must be converted to zeros */ \
296 return ((__ret__ < 0) ? 0 : __ret__); \
297 } \
298 \
299 static unsigned long \
300 fn ## _scan_objects(struct shrinker *shrink, struct shrink_control *sc) \
301 { \
302 int __ret__; \
303 \
304 __ret__ = __ ## fn(NULL, sc); \
305 return ((__ret__ < 0) ? SHRINK_STOP : __ret__); \
306 }
307 #else
308 /*
309 * Linux 2.x to 2.6.22, or a newer shrinker API has been introduced.
310 */
311 #error "Unknown shrinker callback"
312 #endif
313
314 #endif /* SPL_MM_COMPAT_H */