]> git.proxmox.com Git - mirror_spl.git/blob - include/linux/mm_compat.h
Remove global memory variables
[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 #if !defined(HAVE_SHRINK_CONTROL_STRUCT)
32 struct shrink_control {
33 gfp_t gfp_mask;
34 unsigned long nr_to_scan;
35 };
36 #endif /* HAVE_SHRINK_CONTROL_STRUCT */
37
38 /*
39 * 2.6.xx API compat,
40 * There currently exists no exposed API to partially shrink the dcache.
41 * The expected mechanism to shrink the cache is a registered shrinker
42 * which is called during memory pressure.
43 */
44 #ifndef HAVE_SHRINK_DCACHE_MEMORY
45 # if defined(HAVE_SHRINK_CONTROL_STRUCT)
46 typedef int (*shrink_dcache_memory_t)(struct shrinker *,
47 struct shrink_control *);
48 extern shrink_dcache_memory_t shrink_dcache_memory_fn;
49 # define shrink_dcache_memory(nr, gfp) \
50 ({ \
51 struct shrink_control sc = { .nr_to_scan = nr, .gfp_mask = gfp }; \
52 int __ret__ = 0; \
53 \
54 if (shrink_dcache_memory_fn) \
55 __ret__ = shrink_dcache_memory_fn(NULL, &sc); \
56 \
57 __ret__; \
58 })
59 # elif defined(HAVE_3ARGS_SHRINKER_CALLBACK)
60 typedef int (*shrink_dcache_memory_t)(struct shrinker *, int, gfp_t);
61 extern shrink_dcache_memory_t shrink_dcache_memory_fn;
62 # define shrink_dcache_memory(nr, gfp) \
63 ({ \
64 int __ret__ = 0; \
65 \
66 if (shrink_dcache_memory_fn) \
67 __ret__ = shrink_dcache_memory_fn(NULL, nr, gfp); \
68 \
69 __ret__; \
70 })
71 # else
72 typedef int (*shrink_dcache_memory_t)(int, gfp_t);
73 extern shrink_dcache_memory_t shrink_dcache_memory_fn;
74 # define shrink_dcache_memory(nr, gfp) \
75 ({ \
76 int __ret__ = 0; \
77 \
78 if (shrink_dcache_memory_fn) \
79 __ret__ = shrink_dcache_memory_fn(nr, gfp); \
80 \
81 __ret__; \
82 })
83 # endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
84 #endif /* HAVE_SHRINK_DCACHE_MEMORY */
85
86 /*
87 * 2.6.xx API compat,
88 * There currently exists no exposed API to partially shrink the icache.
89 * The expected mechanism to shrink the cache is a registered shrinker
90 * which is called during memory pressure.
91 */
92 #ifndef HAVE_SHRINK_ICACHE_MEMORY
93 # if defined(HAVE_SHRINK_CONTROL_STRUCT)
94 typedef int (*shrink_icache_memory_t)(struct shrinker *,
95 struct shrink_control *);
96 extern shrink_icache_memory_t shrink_icache_memory_fn;
97 # define shrink_icache_memory(nr, gfp) \
98 ({ \
99 struct shrink_control sc = { .nr_to_scan = nr, .gfp_mask = gfp }; \
100 int __ret__ = 0; \
101 \
102 if (shrink_icache_memory_fn) \
103 __ret__ = shrink_icache_memory_fn(NULL, &sc); \
104 \
105 __ret__; \
106 })
107 # elif defined(HAVE_3ARGS_SHRINKER_CALLBACK)
108 typedef int (*shrink_icache_memory_t)(struct shrinker *, int, gfp_t);
109 extern shrink_icache_memory_t shrink_icache_memory_fn;
110 # define shrink_icache_memory(nr, gfp) \
111 ({ \
112 int __ret__ = 0; \
113 \
114 if (shrink_icache_memory_fn) \
115 __ret__ = shrink_icache_memory_fn(NULL, nr, gfp); \
116 \
117 __ret__; \
118 })
119 # else
120 typedef int (*shrink_icache_memory_t)(int, gfp_t);
121 extern shrink_icache_memory_t shrink_icache_memory_fn;
122 # define shrink_icache_memory(nr, gfp) \
123 ({ \
124 int __ret__ = 0; \
125 \
126 if (shrink_icache_memory_fn) \
127 __ret__ = shrink_icache_memory_fn(nr, gfp); \
128 \
129 __ret__; \
130 })
131 # endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
132 #endif /* HAVE_SHRINK_ICACHE_MEMORY */
133
134 /*
135 * Due to frequent changes in the shrinker API the following
136 * compatibility wrappers should be used. They are as follows:
137 *
138 * SPL_SHRINKER_DECLARE is used to declare the shrinker which is
139 * passed to spl_register_shrinker()/spl_unregister_shrinker(). Use
140 * shrinker_name to set the shrinker variable name, shrinker_callback
141 * to set the callback function, and seek_cost to define the cost of
142 * reclaiming an object.
143 *
144 * SPL_SHRINKER_DECLARE(shrinker_name, shrinker_callback, seek_cost);
145 *
146 * SPL_SHRINKER_CALLBACK_FWD_DECLARE is used when a forward declaration
147 * of the shrinker callback function is required. Only the callback
148 * function needs to be passed.
149 *
150 * SPL_SHRINKER_CALLBACK_FWD_DECLARE(shrinker_callback);
151 *
152 * SPL_SHRINKER_CALLBACK_WRAPPER is used to declare the callback function
153 * which is registered with the shrinker. This function will call your
154 * custom shrinker which must use the following prototype. Notice the
155 * leading __'s, these must be appended to the callback_function name.
156 *
157 * int __shrinker_callback(struct shrinker *, struct shrink_control *)
158 * SPL_SHRINKER_CALLBACK_WRAPPER(shrinker_callback);a
159 *
160 *
161 * Example:
162 *
163 * SPL_SHRINKER_CALLBACK_FWD_DECLARE(my_shrinker_fn);
164 * SPL_SHRINKER_DECLARE(my_shrinker, my_shrinker_fn, 1);
165 *
166 * static int
167 * __my_shrinker_fn(struct shrinker *shrink, struct shrink_control *sc)
168 * {
169 * if (sc->nr_to_scan) {
170 * ...scan objects in the cache and reclaim them...
171 * }
172 *
173 * ...calculate number of objects in the cache...
174 *
175 * return (number of objects in the cache);
176 * }
177 * SPL_SHRINKER_CALLBACK_WRAPPER(my_shrinker_fn);
178 */
179
180 #define spl_register_shrinker(x) register_shrinker(x)
181 #define spl_unregister_shrinker(x) unregister_shrinker(x)
182
183 /*
184 * Linux 2.6.23 - 2.6.34 Shrinker API Compatibility.
185 */
186 #if defined(HAVE_2ARGS_OLD_SHRINKER_CALLBACK)
187 #define SPL_SHRINKER_DECLARE(s, x, y) \
188 static struct shrinker s = { \
189 .shrink = x, \
190 .seeks = y \
191 }
192
193 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
194 static int fn(int nr_to_scan, unsigned int gfp_mask)
195
196 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
197 static int \
198 fn(int nr_to_scan, unsigned int gfp_mask) \
199 { \
200 struct shrink_control sc; \
201 \
202 sc.nr_to_scan = nr_to_scan; \
203 sc.gfp_mask = gfp_mask; \
204 \
205 return (__ ## fn(NULL, &sc)); \
206 }
207
208 /*
209 * Linux 2.6.35 to 2.6.39 Shrinker API Compatibility.
210 */
211 #elif defined(HAVE_3ARGS_SHRINKER_CALLBACK)
212 #define SPL_SHRINKER_DECLARE(s, x, y) \
213 static struct shrinker s = { \
214 .shrink = x, \
215 .seeks = y \
216 }
217
218 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
219 static int fn(struct shrinker *, int, unsigned int)
220
221 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
222 static int \
223 fn(struct shrinker *shrink, int nr_to_scan, unsigned int gfp_mask) \
224 { \
225 struct shrink_control sc; \
226 \
227 sc.nr_to_scan = nr_to_scan; \
228 sc.gfp_mask = gfp_mask; \
229 \
230 return (__ ## fn(shrink, &sc)); \
231 }
232
233 /*
234 * Linux 3.0 to 3.11 Shrinker API Compatibility.
235 */
236 #elif defined(HAVE_2ARGS_NEW_SHRINKER_CALLBACK)
237 #define SPL_SHRINKER_DECLARE(s, x, y) \
238 static struct shrinker s = { \
239 .shrink = x, \
240 .seeks = y \
241 }
242
243 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
244 static int fn(struct shrinker *, struct shrink_control *)
245
246 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
247 static int \
248 fn(struct shrinker *shrink, struct shrink_control *sc) \
249 { \
250 return (__ ## fn(shrink, sc)); \
251 }
252
253 /*
254 * Linux 3.12 and later Shrinker API Compatibility.
255 */
256 #elif defined(HAVE_SPLIT_SHRINKER_CALLBACK)
257 #define SPL_SHRINKER_DECLARE(s, x, y) \
258 static struct shrinker s = { \
259 .count_objects = x ## _count_objects, \
260 .scan_objects = x ## _scan_objects, \
261 .seeks = y \
262 }
263
264 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
265 static unsigned long fn ## _count_objects(struct shrinker *, \
266 struct shrink_control *); \
267 static unsigned long fn ## _scan_objects(struct shrinker *, \
268 struct shrink_control *)
269
270 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
271 static unsigned long \
272 fn ## _count_objects(struct shrinker *shrink, struct shrink_control *sc)\
273 { \
274 int __ret__; \
275 \
276 sc->nr_to_scan = 0; \
277 __ret__ = __ ## fn(NULL, sc); \
278 \
279 /* Errors may not be returned and must be converted to zeros */ \
280 return ((__ret__ < 0) ? 0 : __ret__); \
281 } \
282 \
283 static unsigned long \
284 fn ## _scan_objects(struct shrinker *shrink, struct shrink_control *sc) \
285 { \
286 int __ret__; \
287 \
288 __ret__ = __ ## fn(NULL, sc); \
289 return ((__ret__ < 0) ? SHRINK_STOP : __ret__); \
290 }
291 #else
292 /*
293 * Linux 2.x to 2.6.22, or a newer shrinker API has been introduced.
294 */
295 #error "Unknown shrinker callback"
296 #endif
297
298 #endif /* SPL_MM_COMPAT_H */