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
31 #if !defined(HAVE_SHRINK_CONTROL_STRUCT)
32 struct shrink_control
{
34 unsigned long nr_to_scan
;
36 #endif /* HAVE_SHRINK_CONTROL_STRUCT */
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.
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) \
51 struct shrink_control sc = { .nr_to_scan = nr, .gfp_mask = gfp }; \
54 if (shrink_dcache_memory_fn) \
55 __ret__ = shrink_dcache_memory_fn(NULL, &sc); \
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) \
66 if (shrink_dcache_memory_fn) \
67 __ret__ = shrink_dcache_memory_fn(NULL, nr, gfp); \
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) \
78 if (shrink_dcache_memory_fn) \
79 __ret__ = shrink_dcache_memory_fn(nr, gfp); \
83 # endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
84 #endif /* HAVE_SHRINK_DCACHE_MEMORY */
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.
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) \
99 struct shrink_control sc = { .nr_to_scan = nr, .gfp_mask = gfp }; \
102 if (shrink_icache_memory_fn) \
103 __ret__ = shrink_icache_memory_fn(NULL, &sc); \
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) \
114 if (shrink_icache_memory_fn) \
115 __ret__ = shrink_icache_memory_fn(NULL, nr, gfp); \
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) \
126 if (shrink_icache_memory_fn) \
127 __ret__ = shrink_icache_memory_fn(nr, gfp); \
131 # endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
132 #endif /* HAVE_SHRINK_ICACHE_MEMORY */
135 * Due to frequent changes in the shrinker API the following
136 * compatibility wrappers should be used. They are as follows:
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.
144 * SPL_SHRINKER_DECLARE(shrinker_name, shrinker_callback, seek_cost);
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.
150 * SPL_SHRINKER_CALLBACK_FWD_DECLARE(shrinker_callback);
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.
157 * int __shrinker_callback(struct shrinker *, struct shrink_control *)
158 * SPL_SHRINKER_CALLBACK_WRAPPER(shrinker_callback);a
163 * SPL_SHRINKER_CALLBACK_FWD_DECLARE(my_shrinker_fn);
164 * SPL_SHRINKER_DECLARE(my_shrinker, my_shrinker_fn, 1);
167 * __my_shrinker_fn(struct shrinker *shrink, struct shrink_control *sc)
169 * if (sc->nr_to_scan) {
170 * ...scan objects in the cache and reclaim them...
173 * ...calculate number of objects in the cache...
175 * return (number of objects in the cache);
177 * SPL_SHRINKER_CALLBACK_WRAPPER(my_shrinker_fn);
180 #define spl_register_shrinker(x) register_shrinker(x)
181 #define spl_unregister_shrinker(x) unregister_shrinker(x)
184 * Linux 2.6.23 - 2.6.34 Shrinker API Compatibility.
186 #if defined(HAVE_2ARGS_OLD_SHRINKER_CALLBACK)
187 #define SPL_SHRINKER_DECLARE(s, x, y) \
188 static struct shrinker s = { \
193 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
194 static int fn(int nr_to_scan, unsigned int gfp_mask)
196 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
198 fn(int nr_to_scan, unsigned int gfp_mask) \
200 struct shrink_control sc; \
202 sc.nr_to_scan = nr_to_scan; \
203 sc.gfp_mask = gfp_mask; \
205 return (__ ## fn(NULL, &sc)); \
209 * Linux 2.6.35 to 2.6.39 Shrinker API Compatibility.
211 #elif defined(HAVE_3ARGS_SHRINKER_CALLBACK)
212 #define SPL_SHRINKER_DECLARE(s, x, y) \
213 static struct shrinker s = { \
218 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
219 static int fn(struct shrinker *, int, unsigned int)
221 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
223 fn(struct shrinker *shrink, int nr_to_scan, unsigned int gfp_mask) \
225 struct shrink_control sc; \
227 sc.nr_to_scan = nr_to_scan; \
228 sc.gfp_mask = gfp_mask; \
230 return (__ ## fn(shrink, &sc)); \
234 * Linux 3.0 to 3.11 Shrinker API Compatibility.
236 #elif defined(HAVE_2ARGS_NEW_SHRINKER_CALLBACK)
237 #define SPL_SHRINKER_DECLARE(s, x, y) \
238 static struct shrinker s = { \
243 #define SPL_SHRINKER_CALLBACK_FWD_DECLARE(fn) \
244 static int fn(struct shrinker *, struct shrink_control *)
246 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
248 fn(struct shrinker *shrink, struct shrink_control *sc) \
250 return (__ ## fn(shrink, sc)); \
254 * Linux 3.12 and later Shrinker API Compatibility.
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, \
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 *)
270 #define SPL_SHRINKER_CALLBACK_WRAPPER(fn) \
271 static unsigned long \
272 fn ## _count_objects(struct shrinker *shrink, struct shrink_control *sc)\
276 sc->nr_to_scan = 0; \
277 __ret__ = __ ## fn(NULL, sc); \
279 /* Errors may not be returned and must be converted to zeros */ \
280 return ((__ret__ < 0) ? 0 : __ret__); \
283 static unsigned long \
284 fn ## _scan_objects(struct shrinker *shrink, struct shrink_control *sc) \
288 __ret__ = __ ## fn(NULL, sc); \
289 return ((__ret__ < 0) ? SHRINK_STOP : __ret__); \
293 * Linux 2.x to 2.6.22, or a newer shrinker API has been introduced.
295 #error "Unknown shrinker callback"
298 #endif /* SPL_MM_COMPAT_H */