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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
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
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 \*****************************************************************************/
25 #ifndef _SPL_MM_COMPAT_H
26 #define _SPL_MM_COMPAT_H
32 * Linux 2.6.31 API Change.
33 * Individual pages_{min,low,high} moved in to watermark array.
35 #ifndef min_wmark_pages
36 #define min_wmark_pages(z) (z->pages_min)
39 #ifndef low_wmark_pages
40 #define low_wmark_pages(z) (z->pages_low)
43 #ifndef high_wmark_pages
44 #define high_wmark_pages(z) (z->pages_high)
47 #if !defined(HAVE_SHRINK_CONTROL_STRUCT)
48 struct shrink_control
{
50 unsigned long nr_to_scan
;
52 #endif /* HAVE_SHRINK_CONTROL_STRUCT */
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.
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) \
67 struct shrink_control sc = { .nr_to_scan = nr, .gfp_mask = gfp }; \
70 if (shrink_dcache_memory_fn) \
71 __ret__ = shrink_dcache_memory_fn(NULL, &sc); \
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) \
82 if (shrink_dcache_memory_fn) \
83 __ret__ = shrink_dcache_memory_fn(NULL, nr, gfp); \
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) \
94 if (shrink_dcache_memory_fn) \
95 __ret__ = shrink_dcache_memory_fn(nr, gfp); \
99 # endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
100 #endif /* HAVE_SHRINK_DCACHE_MEMORY */
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.
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) \
115 struct shrink_control sc = { .nr_to_scan = nr, .gfp_mask = gfp }; \
118 if (shrink_icache_memory_fn) \
119 __ret__ = shrink_icache_memory_fn(NULL, &sc); \
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) \
130 if (shrink_icache_memory_fn) \
131 __ret__ = shrink_icache_memory_fn(NULL, nr, gfp); \
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) \
142 if (shrink_icache_memory_fn) \
143 __ret__ = shrink_icache_memory_fn(nr, gfp); \
147 # endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
148 #endif /* HAVE_SHRINK_ICACHE_MEMORY */
151 * Due to frequent changes in the shrinker API the following
152 * compatibility wrappers should be used. They are as follows:
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.
160 * SPL_SHRINKER_DECLARE(shrinker_name, shrinker_callback, seek_cost);
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.
166 * SPL_SHRINKER_CALLBACK_FWD_DECLARE(shrinker_callback);
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.
173 * int __shrinker_callback(struct shrinker *, struct shrink_control *)
174 * SPL_SHRINKER_CALLBACK_WRAPPER(shrinker_callback);a
179 * SPL_SHRINKER_CALLBACK_FWD_DECLARE(my_shrinker_fn);
180 * SPL_SHRINKER_DECLARE(my_shrinker, my_shrinker_fn, 1);
183 * __my_shrinker_fn(struct shrinker *shrink, struct shrink_control *sc)
185 * if (sc->nr_to_scan) {
186 * ...scan objects in the cache and reclaim them...
189 * ...calculate number of objects in the cache...
191 * return (number of objects in the cache);
193 * SPL_SHRINKER_CALLBACK_WRAPPER(my_shrinker_fn);
196 #define spl_register_shrinker(x) register_shrinker(x)
197 #define spl_unregister_shrinker(x) unregister_shrinker(x)
200 * Linux 2.6.23 - 2.6.34 Shrinker API Compatibility.
202 #if defined(HAVE_2ARGS_OLD_SHRINKER_CALLBACK)
203 #define SPL_SHRINKER_DECLARE(s, x, y) \
204 static struct shrinker s = { \
209 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
210 static int fn(int nr_to_scan, unsigned int gfp_mask)
212 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
214 fn(int nr_to_scan, unsigned int gfp_mask) \
216 struct shrink_control sc; \
218 sc.nr_to_scan = nr_to_scan; \
219 sc.gfp_mask = gfp_mask; \
221 return (__ ## fn(NULL, &sc)); \
225 * Linux 2.6.35 to 2.6.39 Shrinker API Compatibility.
227 #elif defined(HAVE_3ARGS_SHRINKER_CALLBACK)
228 #define SPL_SHRINKER_DECLARE(s, x, y) \
229 static struct shrinker s = { \
234 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
235 static int fn(struct shrinker *, int, unsigned int)
237 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
239 fn(struct shrinker *shrink, int nr_to_scan, unsigned int gfp_mask) \
241 struct shrink_control sc; \
243 sc.nr_to_scan = nr_to_scan; \
244 sc.gfp_mask = gfp_mask; \
246 return (__ ## fn(shrink, &sc)); \
250 * Linux 3.0 to 3.11 Shrinker API Compatibility.
252 #elif defined(HAVE_2ARGS_NEW_SHRINKER_CALLBACK)
253 #define SPL_SHRINKER_DECLARE(s, x, y) \
254 static struct shrinker s = { \
259 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
260 static int fn(struct shrinker *, struct shrink_control *)
262 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
264 fn(struct shrinker *shrink, struct shrink_control *sc) \
266 return (__ ## fn(shrink, sc)); \
270 * Linux 3.12 and later Shrinker API Compatibility.
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, \
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 *)
286 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
287 static unsigned long \
288 fn ## _count_objects(struct shrinker *shrink, struct shrink_control *sc)\
292 sc->nr_to_scan = 0; \
293 __ret__ = __ ## fn(NULL, sc); \
295 /* Errors may not be returned and must be converted to zeros */ \
296 return ((__ret__ < 0) ? 0 : __ret__); \
299 static unsigned long \
300 fn ## _scan_objects(struct shrinker *shrink, struct shrink_control *sc) \
304 __ret__ = __ ## fn(NULL, sc); \
305 return ((__ret__ < 0) ? SHRINK_STOP : __ret__); \
309 * Linux 2.x to 2.6.22, or a newer shrinker API has been introduced.
311 #error "Unknown shrinker callback"
314 #endif /* SPL_MM_COMPAT_H */