]>
Commit | Line | Data |
---|---|---|
b34b9563 | 1 | /* |
e5b9b344 BB |
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/>. | |
b34b9563 | 23 | */ |
e5b9b344 BB |
24 | |
25 | #include <sys/kmem.h> | |
26 | #include <sys/kmem_cache.h> | |
27 | #include <sys/taskq.h> | |
28 | #include <sys/timer.h> | |
29 | #include <sys/vmem.h> | |
30 | #include <linux/slab.h> | |
31 | #include <linux/swap.h> | |
32 | #include <linux/mm_compat.h> | |
33 | #include <linux/wait_compat.h> | |
34 | ||
35 | /* | |
36 | * Within the scope of spl-kmem.c file the kmem_cache_* definitions | |
37 | * are removed to allow access to the real Linux slab allocator. | |
38 | */ | |
39 | #undef kmem_cache_destroy | |
40 | #undef kmem_cache_create | |
41 | #undef kmem_cache_alloc | |
42 | #undef kmem_cache_free | |
43 | ||
44 | ||
a988a35a RY |
45 | /* |
46 | * Linux 3.16 replaced smp_mb__{before,after}_{atomic,clear}_{dec,inc,bit}() | |
47 | * with smp_mb__{before,after}_atomic() because they were redundant. This is | |
48 | * only used inside our SLAB allocator, so we implement an internal wrapper | |
49 | * here to give us smp_mb__{before,after}_atomic() on older kernels. | |
50 | */ | |
51 | #ifndef smp_mb__before_atomic | |
52 | #define smp_mb__before_atomic(x) smp_mb__before_clear_bit(x) | |
53 | #endif | |
54 | ||
55 | #ifndef smp_mb__after_atomic | |
56 | #define smp_mb__after_atomic(x) smp_mb__after_clear_bit(x) | |
57 | #endif | |
58 | ||
e5b9b344 BB |
59 | /* |
60 | * Cache expiration was implemented because it was part of the default Solaris | |
61 | * kmem_cache behavior. The idea is that per-cpu objects which haven't been | |
62 | * accessed in several seconds should be returned to the cache. On the other | |
63 | * hand Linux slabs never move objects back to the slabs unless there is | |
64 | * memory pressure on the system. By default the Linux method is enabled | |
65 | * because it has been shown to improve responsiveness on low memory systems. | |
66 | * This policy may be changed by setting KMC_EXPIRE_AGE or KMC_EXPIRE_MEM. | |
67 | */ | |
68 | unsigned int spl_kmem_cache_expire = KMC_EXPIRE_MEM; | |
69 | EXPORT_SYMBOL(spl_kmem_cache_expire); | |
70 | module_param(spl_kmem_cache_expire, uint, 0644); | |
71 | MODULE_PARM_DESC(spl_kmem_cache_expire, "By age (0x1) or low memory (0x2)"); | |
72 | ||
1a204968 BB |
73 | /* |
74 | * Cache magazines are an optimization designed to minimize the cost of | |
75 | * allocating memory. They do this by keeping a per-cpu cache of recently | |
76 | * freed objects, which can then be reallocated without taking a lock. This | |
77 | * can improve performance on highly contended caches. However, because | |
78 | * objects in magazines will prevent otherwise empty slabs from being | |
79 | * immediately released this may not be ideal for low memory machines. | |
80 | * | |
81 | * For this reason spl_kmem_cache_magazine_size can be used to set a maximum | |
82 | * magazine size. When this value is set to 0 the magazine size will be | |
83 | * automatically determined based on the object size. Otherwise magazines | |
84 | * will be limited to 2-256 objects per magazine (i.e per cpu). Magazines | |
85 | * may never be entirely disabled in this implementation. | |
86 | */ | |
87 | unsigned int spl_kmem_cache_magazine_size = 0; | |
88 | module_param(spl_kmem_cache_magazine_size, uint, 0444); | |
89 | MODULE_PARM_DESC(spl_kmem_cache_magazine_size, | |
90 | "Default magazine size (2-256), set automatically (0)\n"); | |
91 | ||
e5b9b344 BB |
92 | /* |
93 | * The default behavior is to report the number of objects remaining in the | |
94 | * cache. This allows the Linux VM to repeatedly reclaim objects from the | |
95 | * cache when memory is low satisfy other memory allocations. Alternately, | |
96 | * setting this value to KMC_RECLAIM_ONCE limits how aggressively the cache | |
97 | * is reclaimed. This may increase the likelihood of out of memory events. | |
98 | */ | |
99 | unsigned int spl_kmem_cache_reclaim = 0 /* KMC_RECLAIM_ONCE */; | |
100 | module_param(spl_kmem_cache_reclaim, uint, 0644); | |
101 | MODULE_PARM_DESC(spl_kmem_cache_reclaim, "Single reclaim pass (0x1)"); | |
102 | ||
103 | unsigned int spl_kmem_cache_obj_per_slab = SPL_KMEM_CACHE_OBJ_PER_SLAB; | |
104 | module_param(spl_kmem_cache_obj_per_slab, uint, 0644); | |
105 | MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab, "Number of objects per slab"); | |
106 | ||
107 | unsigned int spl_kmem_cache_obj_per_slab_min = SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN; | |
108 | module_param(spl_kmem_cache_obj_per_slab_min, uint, 0644); | |
109 | MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab_min, | |
b34b9563 | 110 | "Minimal number of objects per slab"); |
e5b9b344 | 111 | |
3018bffa | 112 | unsigned int spl_kmem_cache_max_size = SPL_KMEM_CACHE_MAX_SIZE; |
e5b9b344 BB |
113 | module_param(spl_kmem_cache_max_size, uint, 0644); |
114 | MODULE_PARM_DESC(spl_kmem_cache_max_size, "Maximum size of slab in MB"); | |
115 | ||
116 | /* | |
117 | * For small objects the Linux slab allocator should be used to make the most | |
118 | * efficient use of the memory. However, large objects are not supported by | |
119 | * the Linux slab and therefore the SPL implementation is preferred. A cutoff | |
120 | * of 16K was determined to be optimal for architectures using 4K pages. | |
121 | */ | |
122 | #if PAGE_SIZE == 4096 | |
123 | unsigned int spl_kmem_cache_slab_limit = 16384; | |
124 | #else | |
125 | unsigned int spl_kmem_cache_slab_limit = 0; | |
126 | #endif | |
127 | module_param(spl_kmem_cache_slab_limit, uint, 0644); | |
128 | MODULE_PARM_DESC(spl_kmem_cache_slab_limit, | |
b34b9563 | 129 | "Objects less than N bytes use the Linux slab"); |
e5b9b344 | 130 | |
3018bffa BB |
131 | /* |
132 | * This value defaults to a threshold designed to avoid allocations which | |
133 | * have been deemed costly by the kernel. | |
134 | */ | |
135 | unsigned int spl_kmem_cache_kmem_limit = | |
136 | ((1 << (PAGE_ALLOC_COSTLY_ORDER - 1)) * PAGE_SIZE) / | |
137 | SPL_KMEM_CACHE_OBJ_PER_SLAB; | |
e5b9b344 BB |
138 | module_param(spl_kmem_cache_kmem_limit, uint, 0644); |
139 | MODULE_PARM_DESC(spl_kmem_cache_kmem_limit, | |
b34b9563 | 140 | "Objects less than N bytes use the kmalloc"); |
e5b9b344 | 141 | |
436ad60f BB |
142 | /* |
143 | * The number of threads available to allocate new slabs for caches. This | |
144 | * should not need to be tuned but it is available for performance analysis. | |
145 | */ | |
146 | unsigned int spl_kmem_cache_kmem_threads = 4; | |
147 | module_param(spl_kmem_cache_kmem_threads, uint, 0444); | |
148 | MODULE_PARM_DESC(spl_kmem_cache_kmem_threads, | |
149 | "Number of spl_kmem_cache threads"); | |
150 | ||
e5b9b344 BB |
151 | /* |
152 | * Slab allocation interfaces | |
153 | * | |
154 | * While the Linux slab implementation was inspired by the Solaris | |
155 | * implementation I cannot use it to emulate the Solaris APIs. I | |
156 | * require two features which are not provided by the Linux slab. | |
157 | * | |
158 | * 1) Constructors AND destructors. Recent versions of the Linux | |
159 | * kernel have removed support for destructors. This is a deal | |
160 | * breaker for the SPL which contains particularly expensive | |
161 | * initializers for mutex's, condition variables, etc. We also | |
162 | * require a minimal level of cleanup for these data types unlike | |
b34b9563 | 163 | * many Linux data types which do need to be explicitly destroyed. |
e5b9b344 BB |
164 | * |
165 | * 2) Virtual address space backed slab. Callers of the Solaris slab | |
166 | * expect it to work well for both small are very large allocations. | |
167 | * Because of memory fragmentation the Linux slab which is backed | |
168 | * by kmalloc'ed memory performs very badly when confronted with | |
169 | * large numbers of large allocations. Basing the slab on the | |
170 | * virtual address space removes the need for contiguous pages | |
171 | * and greatly improve performance for large allocations. | |
172 | * | |
173 | * For these reasons, the SPL has its own slab implementation with | |
174 | * the needed features. It is not as highly optimized as either the | |
175 | * Solaris or Linux slabs, but it should get me most of what is | |
176 | * needed until it can be optimized or obsoleted by another approach. | |
177 | * | |
178 | * One serious concern I do have about this method is the relatively | |
179 | * small virtual address space on 32bit arches. This will seriously | |
180 | * constrain the size of the slab caches and their performance. | |
e5b9b344 BB |
181 | */ |
182 | ||
183 | struct list_head spl_kmem_cache_list; /* List of caches */ | |
184 | struct rw_semaphore spl_kmem_cache_sem; /* Cache list lock */ | |
b34b9563 | 185 | taskq_t *spl_kmem_cache_taskq; /* Task queue for ageing / reclaim */ |
e5b9b344 BB |
186 | |
187 | static void spl_cache_shrink(spl_kmem_cache_t *skc, void *obj); | |
188 | ||
189 | SPL_SHRINKER_CALLBACK_FWD_DECLARE(spl_kmem_cache_generic_shrinker); | |
190 | SPL_SHRINKER_DECLARE(spl_kmem_cache_shrinker, | |
191 | spl_kmem_cache_generic_shrinker, KMC_DEFAULT_SEEKS); | |
192 | ||
193 | static void * | |
194 | kv_alloc(spl_kmem_cache_t *skc, int size, int flags) | |
195 | { | |
c3eabc75 | 196 | gfp_t lflags = kmem_flags_convert(flags); |
e5b9b344 BB |
197 | void *ptr; |
198 | ||
3018bffa BB |
199 | if (skc->skc_flags & KMC_KMEM) { |
200 | ASSERT(ISP2(size)); | |
c3eabc75 | 201 | ptr = (void *)__get_free_pages(lflags, get_order(size)); |
3018bffa | 202 | } else { |
c2fa0945 | 203 | ptr = spl_vmalloc(size, lflags | __GFP_HIGHMEM, PAGE_KERNEL); |
3018bffa | 204 | } |
e5b9b344 BB |
205 | |
206 | /* Resulting allocated memory will be page aligned */ | |
207 | ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE)); | |
208 | ||
b34b9563 | 209 | return (ptr); |
e5b9b344 BB |
210 | } |
211 | ||
212 | static void | |
213 | kv_free(spl_kmem_cache_t *skc, void *ptr, int size) | |
214 | { | |
215 | ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE)); | |
e5b9b344 BB |
216 | |
217 | /* | |
218 | * The Linux direct reclaim path uses this out of band value to | |
219 | * determine if forward progress is being made. Normally this is | |
220 | * incremented by kmem_freepages() which is part of the various | |
221 | * Linux slab implementations. However, since we are using none | |
222 | * of that infrastructure we are responsible for incrementing it. | |
223 | */ | |
224 | if (current->reclaim_state) | |
225 | current->reclaim_state->reclaimed_slab += size >> PAGE_SHIFT; | |
226 | ||
3018bffa BB |
227 | if (skc->skc_flags & KMC_KMEM) { |
228 | ASSERT(ISP2(size)); | |
e5b9b344 | 229 | free_pages((unsigned long)ptr, get_order(size)); |
3018bffa | 230 | } else { |
e5b9b344 | 231 | vfree(ptr); |
3018bffa | 232 | } |
e5b9b344 BB |
233 | } |
234 | ||
235 | /* | |
236 | * Required space for each aligned sks. | |
237 | */ | |
238 | static inline uint32_t | |
239 | spl_sks_size(spl_kmem_cache_t *skc) | |
240 | { | |
b34b9563 BB |
241 | return (P2ROUNDUP_TYPED(sizeof (spl_kmem_slab_t), |
242 | skc->skc_obj_align, uint32_t)); | |
e5b9b344 BB |
243 | } |
244 | ||
245 | /* | |
246 | * Required space for each aligned object. | |
247 | */ | |
248 | static inline uint32_t | |
249 | spl_obj_size(spl_kmem_cache_t *skc) | |
250 | { | |
251 | uint32_t align = skc->skc_obj_align; | |
252 | ||
b34b9563 BB |
253 | return (P2ROUNDUP_TYPED(skc->skc_obj_size, align, uint32_t) + |
254 | P2ROUNDUP_TYPED(sizeof (spl_kmem_obj_t), align, uint32_t)); | |
e5b9b344 BB |
255 | } |
256 | ||
257 | /* | |
258 | * Lookup the spl_kmem_object_t for an object given that object. | |
259 | */ | |
260 | static inline spl_kmem_obj_t * | |
261 | spl_sko_from_obj(spl_kmem_cache_t *skc, void *obj) | |
262 | { | |
b34b9563 BB |
263 | return (obj + P2ROUNDUP_TYPED(skc->skc_obj_size, |
264 | skc->skc_obj_align, uint32_t)); | |
e5b9b344 BB |
265 | } |
266 | ||
267 | /* | |
268 | * Required space for each offslab object taking in to account alignment | |
269 | * restrictions and the power-of-two requirement of kv_alloc(). | |
270 | */ | |
271 | static inline uint32_t | |
272 | spl_offslab_size(spl_kmem_cache_t *skc) | |
273 | { | |
b34b9563 | 274 | return (1UL << (fls64(spl_obj_size(skc)) + 1)); |
e5b9b344 BB |
275 | } |
276 | ||
277 | /* | |
278 | * It's important that we pack the spl_kmem_obj_t structure and the | |
279 | * actual objects in to one large address space to minimize the number | |
280 | * of calls to the allocator. It is far better to do a few large | |
281 | * allocations and then subdivide it ourselves. Now which allocator | |
282 | * we use requires balancing a few trade offs. | |
283 | * | |
284 | * For small objects we use kmem_alloc() because as long as you are | |
285 | * only requesting a small number of pages (ideally just one) its cheap. | |
286 | * However, when you start requesting multiple pages with kmem_alloc() | |
287 | * it gets increasingly expensive since it requires contiguous pages. | |
288 | * For this reason we shift to vmem_alloc() for slabs of large objects | |
289 | * which removes the need for contiguous pages. We do not use | |
290 | * vmem_alloc() in all cases because there is significant locking | |
291 | * overhead in __get_vm_area_node(). This function takes a single | |
292 | * global lock when acquiring an available virtual address range which | |
293 | * serializes all vmem_alloc()'s for all slab caches. Using slightly | |
294 | * different allocation functions for small and large objects should | |
295 | * give us the best of both worlds. | |
296 | * | |
297 | * KMC_ONSLAB KMC_OFFSLAB | |
298 | * | |
299 | * +------------------------+ +-----------------+ | |
300 | * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+ | |
301 | * | skc_obj_size <-+ | | +-----------------+ | | | |
302 | * | spl_kmem_obj_t | | | | | |
303 | * | skc_obj_size <---+ | +-----------------+ | | | |
304 | * | spl_kmem_obj_t | | | skc_obj_size | <-+ | | |
305 | * | ... v | | spl_kmem_obj_t | | | |
306 | * +------------------------+ +-----------------+ v | |
307 | */ | |
308 | static spl_kmem_slab_t * | |
309 | spl_slab_alloc(spl_kmem_cache_t *skc, int flags) | |
310 | { | |
311 | spl_kmem_slab_t *sks; | |
312 | spl_kmem_obj_t *sko, *n; | |
313 | void *base, *obj; | |
314 | uint32_t obj_size, offslab_size = 0; | |
315 | int i, rc = 0; | |
316 | ||
317 | base = kv_alloc(skc, skc->skc_slab_size, flags); | |
318 | if (base == NULL) | |
319 | return (NULL); | |
320 | ||
321 | sks = (spl_kmem_slab_t *)base; | |
322 | sks->sks_magic = SKS_MAGIC; | |
323 | sks->sks_objs = skc->skc_slab_objs; | |
324 | sks->sks_age = jiffies; | |
325 | sks->sks_cache = skc; | |
326 | INIT_LIST_HEAD(&sks->sks_list); | |
327 | INIT_LIST_HEAD(&sks->sks_free_list); | |
328 | sks->sks_ref = 0; | |
329 | obj_size = spl_obj_size(skc); | |
330 | ||
331 | if (skc->skc_flags & KMC_OFFSLAB) | |
332 | offslab_size = spl_offslab_size(skc); | |
333 | ||
334 | for (i = 0; i < sks->sks_objs; i++) { | |
335 | if (skc->skc_flags & KMC_OFFSLAB) { | |
336 | obj = kv_alloc(skc, offslab_size, flags); | |
337 | if (!obj) { | |
338 | rc = -ENOMEM; | |
339 | goto out; | |
340 | } | |
341 | } else { | |
342 | obj = base + spl_sks_size(skc) + (i * obj_size); | |
343 | } | |
344 | ||
345 | ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align)); | |
346 | sko = spl_sko_from_obj(skc, obj); | |
347 | sko->sko_addr = obj; | |
348 | sko->sko_magic = SKO_MAGIC; | |
349 | sko->sko_slab = sks; | |
350 | INIT_LIST_HEAD(&sko->sko_list); | |
351 | list_add_tail(&sko->sko_list, &sks->sks_free_list); | |
352 | } | |
353 | ||
354 | out: | |
355 | if (rc) { | |
356 | if (skc->skc_flags & KMC_OFFSLAB) | |
b34b9563 BB |
357 | list_for_each_entry_safe(sko, |
358 | n, &sks->sks_free_list, sko_list) | |
e5b9b344 BB |
359 | kv_free(skc, sko->sko_addr, offslab_size); |
360 | ||
361 | kv_free(skc, base, skc->skc_slab_size); | |
362 | sks = NULL; | |
363 | } | |
364 | ||
365 | return (sks); | |
366 | } | |
367 | ||
368 | /* | |
369 | * Remove a slab from complete or partial list, it must be called with | |
370 | * the 'skc->skc_lock' held but the actual free must be performed | |
371 | * outside the lock to prevent deadlocking on vmem addresses. | |
372 | */ | |
373 | static void | |
374 | spl_slab_free(spl_kmem_slab_t *sks, | |
b34b9563 | 375 | struct list_head *sks_list, struct list_head *sko_list) |
e5b9b344 BB |
376 | { |
377 | spl_kmem_cache_t *skc; | |
378 | ||
379 | ASSERT(sks->sks_magic == SKS_MAGIC); | |
380 | ASSERT(sks->sks_ref == 0); | |
381 | ||
382 | skc = sks->sks_cache; | |
383 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
384 | ASSERT(spin_is_locked(&skc->skc_lock)); | |
385 | ||
386 | /* | |
387 | * Update slab/objects counters in the cache, then remove the | |
388 | * slab from the skc->skc_partial_list. Finally add the slab | |
389 | * and all its objects in to the private work lists where the | |
390 | * destructors will be called and the memory freed to the system. | |
391 | */ | |
392 | skc->skc_obj_total -= sks->sks_objs; | |
393 | skc->skc_slab_total--; | |
394 | list_del(&sks->sks_list); | |
395 | list_add(&sks->sks_list, sks_list); | |
396 | list_splice_init(&sks->sks_free_list, sko_list); | |
397 | } | |
398 | ||
399 | /* | |
1a204968 | 400 | * Reclaim empty slabs at the end of the partial list. |
e5b9b344 BB |
401 | */ |
402 | static void | |
1a204968 | 403 | spl_slab_reclaim(spl_kmem_cache_t *skc) |
e5b9b344 BB |
404 | { |
405 | spl_kmem_slab_t *sks, *m; | |
406 | spl_kmem_obj_t *sko, *n; | |
407 | LIST_HEAD(sks_list); | |
408 | LIST_HEAD(sko_list); | |
409 | uint32_t size = 0; | |
e5b9b344 BB |
410 | |
411 | /* | |
1a204968 BB |
412 | * Empty slabs and objects must be moved to a private list so they |
413 | * can be safely freed outside the spin lock. All empty slabs are | |
414 | * at the end of skc->skc_partial_list, therefore once a non-empty | |
415 | * slab is found we can stop scanning. | |
e5b9b344 BB |
416 | */ |
417 | spin_lock(&skc->skc_lock); | |
b34b9563 BB |
418 | list_for_each_entry_safe_reverse(sks, m, |
419 | &skc->skc_partial_list, sks_list) { | |
1a204968 BB |
420 | |
421 | if (sks->sks_ref > 0) | |
e5b9b344 BB |
422 | break; |
423 | ||
1a204968 | 424 | spl_slab_free(sks, &sks_list, &sko_list); |
e5b9b344 BB |
425 | } |
426 | spin_unlock(&skc->skc_lock); | |
427 | ||
428 | /* | |
429 | * The following two loops ensure all the object destructors are | |
430 | * run, any offslab objects are freed, and the slabs themselves | |
431 | * are freed. This is all done outside the skc->skc_lock since | |
432 | * this allows the destructor to sleep, and allows us to perform | |
433 | * a conditional reschedule when a freeing a large number of | |
434 | * objects and slabs back to the system. | |
435 | */ | |
436 | if (skc->skc_flags & KMC_OFFSLAB) | |
437 | size = spl_offslab_size(skc); | |
438 | ||
439 | list_for_each_entry_safe(sko, n, &sko_list, sko_list) { | |
440 | ASSERT(sko->sko_magic == SKO_MAGIC); | |
441 | ||
442 | if (skc->skc_flags & KMC_OFFSLAB) | |
443 | kv_free(skc, sko->sko_addr, size); | |
444 | } | |
445 | ||
446 | list_for_each_entry_safe(sks, m, &sks_list, sks_list) { | |
447 | ASSERT(sks->sks_magic == SKS_MAGIC); | |
448 | kv_free(skc, sks, skc->skc_slab_size); | |
449 | } | |
450 | } | |
451 | ||
452 | static spl_kmem_emergency_t * | |
453 | spl_emergency_search(struct rb_root *root, void *obj) | |
454 | { | |
455 | struct rb_node *node = root->rb_node; | |
456 | spl_kmem_emergency_t *ske; | |
457 | unsigned long address = (unsigned long)obj; | |
458 | ||
459 | while (node) { | |
460 | ske = container_of(node, spl_kmem_emergency_t, ske_node); | |
461 | ||
462 | if (address < (unsigned long)ske->ske_obj) | |
463 | node = node->rb_left; | |
464 | else if (address > (unsigned long)ske->ske_obj) | |
465 | node = node->rb_right; | |
466 | else | |
b34b9563 | 467 | return (ske); |
e5b9b344 BB |
468 | } |
469 | ||
b34b9563 | 470 | return (NULL); |
e5b9b344 BB |
471 | } |
472 | ||
473 | static int | |
474 | spl_emergency_insert(struct rb_root *root, spl_kmem_emergency_t *ske) | |
475 | { | |
476 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
477 | spl_kmem_emergency_t *ske_tmp; | |
478 | unsigned long address = (unsigned long)ske->ske_obj; | |
479 | ||
480 | while (*new) { | |
481 | ske_tmp = container_of(*new, spl_kmem_emergency_t, ske_node); | |
482 | ||
483 | parent = *new; | |
484 | if (address < (unsigned long)ske_tmp->ske_obj) | |
485 | new = &((*new)->rb_left); | |
486 | else if (address > (unsigned long)ske_tmp->ske_obj) | |
487 | new = &((*new)->rb_right); | |
488 | else | |
b34b9563 | 489 | return (0); |
e5b9b344 BB |
490 | } |
491 | ||
492 | rb_link_node(&ske->ske_node, parent, new); | |
493 | rb_insert_color(&ske->ske_node, root); | |
494 | ||
b34b9563 | 495 | return (1); |
e5b9b344 BB |
496 | } |
497 | ||
498 | /* | |
499 | * Allocate a single emergency object and track it in a red black tree. | |
500 | */ | |
501 | static int | |
502 | spl_emergency_alloc(spl_kmem_cache_t *skc, int flags, void **obj) | |
503 | { | |
c3eabc75 | 504 | gfp_t lflags = kmem_flags_convert(flags); |
e5b9b344 BB |
505 | spl_kmem_emergency_t *ske; |
506 | int empty; | |
507 | ||
508 | /* Last chance use a partial slab if one now exists */ | |
509 | spin_lock(&skc->skc_lock); | |
510 | empty = list_empty(&skc->skc_partial_list); | |
511 | spin_unlock(&skc->skc_lock); | |
512 | if (!empty) | |
513 | return (-EEXIST); | |
514 | ||
c3eabc75 | 515 | ske = kmalloc(sizeof (*ske), lflags); |
e5b9b344 BB |
516 | if (ske == NULL) |
517 | return (-ENOMEM); | |
518 | ||
c3eabc75 | 519 | ske->ske_obj = kmalloc(skc->skc_obj_size, lflags); |
e5b9b344 BB |
520 | if (ske->ske_obj == NULL) { |
521 | kfree(ske); | |
522 | return (-ENOMEM); | |
523 | } | |
524 | ||
525 | spin_lock(&skc->skc_lock); | |
526 | empty = spl_emergency_insert(&skc->skc_emergency_tree, ske); | |
527 | if (likely(empty)) { | |
528 | skc->skc_obj_total++; | |
529 | skc->skc_obj_emergency++; | |
530 | if (skc->skc_obj_emergency > skc->skc_obj_emergency_max) | |
531 | skc->skc_obj_emergency_max = skc->skc_obj_emergency; | |
532 | } | |
533 | spin_unlock(&skc->skc_lock); | |
534 | ||
535 | if (unlikely(!empty)) { | |
536 | kfree(ske->ske_obj); | |
537 | kfree(ske); | |
538 | return (-EINVAL); | |
539 | } | |
540 | ||
541 | *obj = ske->ske_obj; | |
542 | ||
543 | return (0); | |
544 | } | |
545 | ||
546 | /* | |
547 | * Locate the passed object in the red black tree and free it. | |
548 | */ | |
549 | static int | |
550 | spl_emergency_free(spl_kmem_cache_t *skc, void *obj) | |
551 | { | |
552 | spl_kmem_emergency_t *ske; | |
553 | ||
554 | spin_lock(&skc->skc_lock); | |
555 | ske = spl_emergency_search(&skc->skc_emergency_tree, obj); | |
436ad60f | 556 | if (ske) { |
e5b9b344 BB |
557 | rb_erase(&ske->ske_node, &skc->skc_emergency_tree); |
558 | skc->skc_obj_emergency--; | |
559 | skc->skc_obj_total--; | |
560 | } | |
561 | spin_unlock(&skc->skc_lock); | |
562 | ||
436ad60f | 563 | if (ske == NULL) |
e5b9b344 BB |
564 | return (-ENOENT); |
565 | ||
566 | kfree(ske->ske_obj); | |
567 | kfree(ske); | |
568 | ||
569 | return (0); | |
570 | } | |
571 | ||
572 | /* | |
573 | * Release objects from the per-cpu magazine back to their slab. The flush | |
574 | * argument contains the max number of entries to remove from the magazine. | |
575 | */ | |
576 | static void | |
577 | __spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush) | |
578 | { | |
579 | int i, count = MIN(flush, skm->skm_avail); | |
580 | ||
581 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
582 | ASSERT(skm->skm_magic == SKM_MAGIC); | |
583 | ASSERT(spin_is_locked(&skc->skc_lock)); | |
584 | ||
585 | for (i = 0; i < count; i++) | |
586 | spl_cache_shrink(skc, skm->skm_objs[i]); | |
587 | ||
588 | skm->skm_avail -= count; | |
589 | memmove(skm->skm_objs, &(skm->skm_objs[count]), | |
b34b9563 | 590 | sizeof (void *) * skm->skm_avail); |
e5b9b344 BB |
591 | } |
592 | ||
593 | static void | |
594 | spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush) | |
595 | { | |
596 | spin_lock(&skc->skc_lock); | |
597 | __spl_cache_flush(skc, skm, flush); | |
598 | spin_unlock(&skc->skc_lock); | |
599 | } | |
600 | ||
601 | static void | |
602 | spl_magazine_age(void *data) | |
603 | { | |
604 | spl_kmem_cache_t *skc = (spl_kmem_cache_t *)data; | |
605 | spl_kmem_magazine_t *skm = skc->skc_mag[smp_processor_id()]; | |
606 | ||
607 | ASSERT(skm->skm_magic == SKM_MAGIC); | |
608 | ASSERT(skm->skm_cpu == smp_processor_id()); | |
609 | ASSERT(irqs_disabled()); | |
610 | ||
611 | /* There are no available objects or they are too young to age out */ | |
612 | if ((skm->skm_avail == 0) || | |
613 | time_before(jiffies, skm->skm_age + skc->skc_delay * HZ)) | |
614 | return; | |
615 | ||
616 | /* | |
617 | * Because we're executing in interrupt context we may have | |
618 | * interrupted the holder of this lock. To avoid a potential | |
619 | * deadlock return if the lock is contended. | |
620 | */ | |
621 | if (!spin_trylock(&skc->skc_lock)) | |
622 | return; | |
623 | ||
624 | __spl_cache_flush(skc, skm, skm->skm_refill); | |
625 | spin_unlock(&skc->skc_lock); | |
626 | } | |
627 | ||
628 | /* | |
629 | * Called regularly to keep a downward pressure on the cache. | |
630 | * | |
631 | * Objects older than skc->skc_delay seconds in the per-cpu magazines will | |
632 | * be returned to the caches. This is done to prevent idle magazines from | |
633 | * holding memory which could be better used elsewhere. The delay is | |
634 | * present to prevent thrashing the magazine. | |
635 | * | |
636 | * The newly released objects may result in empty partial slabs. Those | |
637 | * slabs should be released to the system. Otherwise moving the objects | |
638 | * out of the magazines is just wasted work. | |
639 | */ | |
640 | static void | |
641 | spl_cache_age(void *data) | |
642 | { | |
643 | spl_kmem_cache_t *skc = (spl_kmem_cache_t *)data; | |
644 | taskqid_t id = 0; | |
645 | ||
646 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
647 | ||
648 | /* Dynamically disabled at run time */ | |
649 | if (!(spl_kmem_cache_expire & KMC_EXPIRE_AGE)) | |
650 | return; | |
651 | ||
652 | atomic_inc(&skc->skc_ref); | |
653 | ||
654 | if (!(skc->skc_flags & KMC_NOMAGAZINE)) | |
655 | on_each_cpu(spl_magazine_age, skc, 1); | |
656 | ||
1a204968 | 657 | spl_slab_reclaim(skc); |
e5b9b344 BB |
658 | |
659 | while (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags) && !id) { | |
660 | id = taskq_dispatch_delay( | |
661 | spl_kmem_cache_taskq, spl_cache_age, skc, TQ_SLEEP, | |
662 | ddi_get_lbolt() + skc->skc_delay / 3 * HZ); | |
663 | ||
664 | /* Destroy issued after dispatch immediately cancel it */ | |
665 | if (test_bit(KMC_BIT_DESTROY, &skc->skc_flags) && id) | |
666 | taskq_cancel_id(spl_kmem_cache_taskq, id); | |
667 | } | |
668 | ||
669 | spin_lock(&skc->skc_lock); | |
670 | skc->skc_taskqid = id; | |
671 | spin_unlock(&skc->skc_lock); | |
672 | ||
673 | atomic_dec(&skc->skc_ref); | |
674 | } | |
675 | ||
676 | /* | |
677 | * Size a slab based on the size of each aligned object plus spl_kmem_obj_t. | |
678 | * When on-slab we want to target spl_kmem_cache_obj_per_slab. However, | |
679 | * for very small objects we may end up with more than this so as not | |
680 | * to waste space in the minimal allocation of a single page. Also for | |
681 | * very large objects we may use as few as spl_kmem_cache_obj_per_slab_min, | |
682 | * lower than this and we will fail. | |
683 | */ | |
684 | static int | |
685 | spl_slab_size(spl_kmem_cache_t *skc, uint32_t *objs, uint32_t *size) | |
686 | { | |
3018bffa | 687 | uint32_t sks_size, obj_size, max_size, tgt_size, tgt_objs; |
e5b9b344 BB |
688 | |
689 | if (skc->skc_flags & KMC_OFFSLAB) { | |
3018bffa BB |
690 | tgt_objs = spl_kmem_cache_obj_per_slab; |
691 | tgt_size = P2ROUNDUP(sizeof (spl_kmem_slab_t), PAGE_SIZE); | |
692 | ||
693 | if ((skc->skc_flags & KMC_KMEM) && | |
694 | (spl_obj_size(skc) > (SPL_MAX_ORDER_NR_PAGES * PAGE_SIZE))) | |
695 | return (-ENOSPC); | |
e5b9b344 BB |
696 | } else { |
697 | sks_size = spl_sks_size(skc); | |
698 | obj_size = spl_obj_size(skc); | |
3018bffa BB |
699 | max_size = (spl_kmem_cache_max_size * 1024 * 1024); |
700 | tgt_size = (spl_kmem_cache_obj_per_slab * obj_size + sks_size); | |
e5b9b344 BB |
701 | |
702 | /* | |
3018bffa BB |
703 | * KMC_KMEM slabs are allocated by __get_free_pages() which |
704 | * rounds up to the nearest order. Knowing this the size | |
705 | * should be rounded up to the next power of two with a hard | |
706 | * maximum defined by the maximum allowed allocation order. | |
e5b9b344 | 707 | */ |
3018bffa BB |
708 | if (skc->skc_flags & KMC_KMEM) { |
709 | max_size = SPL_MAX_ORDER_NR_PAGES * PAGE_SIZE; | |
710 | tgt_size = MIN(max_size, | |
711 | PAGE_SIZE * (1 << MAX(get_order(tgt_size) - 1, 1))); | |
712 | } | |
713 | ||
714 | if (tgt_size <= max_size) { | |
715 | tgt_objs = (tgt_size - sks_size) / obj_size; | |
716 | } else { | |
717 | tgt_objs = (max_size - sks_size) / obj_size; | |
718 | tgt_size = (tgt_objs * obj_size) + sks_size; | |
719 | } | |
e5b9b344 BB |
720 | } |
721 | ||
3018bffa BB |
722 | if (tgt_objs == 0) |
723 | return (-ENOSPC); | |
724 | ||
725 | *objs = tgt_objs; | |
726 | *size = tgt_size; | |
727 | ||
728 | return (0); | |
e5b9b344 BB |
729 | } |
730 | ||
731 | /* | |
732 | * Make a guess at reasonable per-cpu magazine size based on the size of | |
733 | * each object and the cost of caching N of them in each magazine. Long | |
734 | * term this should really adapt based on an observed usage heuristic. | |
735 | */ | |
736 | static int | |
737 | spl_magazine_size(spl_kmem_cache_t *skc) | |
738 | { | |
739 | uint32_t obj_size = spl_obj_size(skc); | |
740 | int size; | |
741 | ||
1a204968 BB |
742 | if (spl_kmem_cache_magazine_size > 0) |
743 | return (MAX(MIN(spl_kmem_cache_magazine_size, 256), 2)); | |
744 | ||
e5b9b344 BB |
745 | /* Per-magazine sizes below assume a 4Kib page size */ |
746 | if (obj_size > (PAGE_SIZE * 256)) | |
747 | size = 4; /* Minimum 4Mib per-magazine */ | |
748 | else if (obj_size > (PAGE_SIZE * 32)) | |
749 | size = 16; /* Minimum 2Mib per-magazine */ | |
750 | else if (obj_size > (PAGE_SIZE)) | |
751 | size = 64; /* Minimum 256Kib per-magazine */ | |
752 | else if (obj_size > (PAGE_SIZE / 4)) | |
753 | size = 128; /* Minimum 128Kib per-magazine */ | |
754 | else | |
755 | size = 256; | |
756 | ||
757 | return (size); | |
758 | } | |
759 | ||
760 | /* | |
761 | * Allocate a per-cpu magazine to associate with a specific core. | |
762 | */ | |
763 | static spl_kmem_magazine_t * | |
764 | spl_magazine_alloc(spl_kmem_cache_t *skc, int cpu) | |
765 | { | |
766 | spl_kmem_magazine_t *skm; | |
b34b9563 BB |
767 | int size = sizeof (spl_kmem_magazine_t) + |
768 | sizeof (void *) * skc->skc_mag_size; | |
e5b9b344 | 769 | |
c3eabc75 | 770 | skm = kmalloc_node(size, GFP_KERNEL, cpu_to_node(cpu)); |
e5b9b344 BB |
771 | if (skm) { |
772 | skm->skm_magic = SKM_MAGIC; | |
773 | skm->skm_avail = 0; | |
774 | skm->skm_size = skc->skc_mag_size; | |
775 | skm->skm_refill = skc->skc_mag_refill; | |
776 | skm->skm_cache = skc; | |
777 | skm->skm_age = jiffies; | |
778 | skm->skm_cpu = cpu; | |
779 | } | |
780 | ||
781 | return (skm); | |
782 | } | |
783 | ||
784 | /* | |
785 | * Free a per-cpu magazine associated with a specific core. | |
786 | */ | |
787 | static void | |
788 | spl_magazine_free(spl_kmem_magazine_t *skm) | |
789 | { | |
e5b9b344 BB |
790 | ASSERT(skm->skm_magic == SKM_MAGIC); |
791 | ASSERT(skm->skm_avail == 0); | |
c3eabc75 | 792 | kfree(skm); |
e5b9b344 BB |
793 | } |
794 | ||
795 | /* | |
796 | * Create all pre-cpu magazines of reasonable sizes. | |
797 | */ | |
798 | static int | |
799 | spl_magazine_create(spl_kmem_cache_t *skc) | |
800 | { | |
801 | int i; | |
802 | ||
803 | if (skc->skc_flags & KMC_NOMAGAZINE) | |
804 | return (0); | |
805 | ||
806 | skc->skc_mag_size = spl_magazine_size(skc); | |
807 | skc->skc_mag_refill = (skc->skc_mag_size + 1) / 2; | |
808 | ||
809 | for_each_online_cpu(i) { | |
810 | skc->skc_mag[i] = spl_magazine_alloc(skc, i); | |
811 | if (!skc->skc_mag[i]) { | |
812 | for (i--; i >= 0; i--) | |
813 | spl_magazine_free(skc->skc_mag[i]); | |
814 | ||
815 | return (-ENOMEM); | |
816 | } | |
817 | } | |
818 | ||
819 | return (0); | |
820 | } | |
821 | ||
822 | /* | |
823 | * Destroy all pre-cpu magazines. | |
824 | */ | |
825 | static void | |
826 | spl_magazine_destroy(spl_kmem_cache_t *skc) | |
827 | { | |
828 | spl_kmem_magazine_t *skm; | |
829 | int i; | |
830 | ||
831 | if (skc->skc_flags & KMC_NOMAGAZINE) | |
832 | return; | |
833 | ||
b34b9563 | 834 | for_each_online_cpu(i) { |
e5b9b344 BB |
835 | skm = skc->skc_mag[i]; |
836 | spl_cache_flush(skc, skm, skm->skm_avail); | |
837 | spl_magazine_free(skm); | |
b34b9563 | 838 | } |
e5b9b344 BB |
839 | } |
840 | ||
841 | /* | |
842 | * Create a object cache based on the following arguments: | |
843 | * name cache name | |
844 | * size cache object size | |
845 | * align cache object alignment | |
846 | * ctor cache object constructor | |
847 | * dtor cache object destructor | |
848 | * reclaim cache object reclaim | |
849 | * priv cache private data for ctor/dtor/reclaim | |
850 | * vmp unused must be NULL | |
851 | * flags | |
852 | * KMC_NOTOUCH Disable cache object aging (unsupported) | |
853 | * KMC_NODEBUG Disable debugging (unsupported) | |
854 | * KMC_NOHASH Disable hashing (unsupported) | |
855 | * KMC_QCACHE Disable qcache (unsupported) | |
856 | * KMC_NOMAGAZINE Enabled for kmem/vmem, Disabled for Linux slab | |
857 | * KMC_KMEM Force kmem backed cache | |
858 | * KMC_VMEM Force vmem backed cache | |
859 | * KMC_SLAB Force Linux slab backed cache | |
860 | * KMC_OFFSLAB Locate objects off the slab | |
861 | */ | |
862 | spl_kmem_cache_t * | |
863 | spl_kmem_cache_create(char *name, size_t size, size_t align, | |
b34b9563 BB |
864 | spl_kmem_ctor_t ctor, spl_kmem_dtor_t dtor, spl_kmem_reclaim_t reclaim, |
865 | void *priv, void *vmp, int flags) | |
e5b9b344 | 866 | { |
c3eabc75 | 867 | gfp_t lflags = kmem_flags_convert(KM_SLEEP); |
b34b9563 | 868 | spl_kmem_cache_t *skc; |
e5b9b344 BB |
869 | int rc; |
870 | ||
871 | /* | |
872 | * Unsupported flags | |
873 | */ | |
874 | ASSERT0(flags & KMC_NOMAGAZINE); | |
875 | ASSERT0(flags & KMC_NOHASH); | |
876 | ASSERT0(flags & KMC_QCACHE); | |
877 | ASSERT(vmp == NULL); | |
878 | ||
879 | might_sleep(); | |
880 | ||
881 | /* | |
b34b9563 | 882 | * Allocate memory for a new cache and initialize it. Unfortunately, |
e5b9b344 BB |
883 | * this usually ends up being a large allocation of ~32k because |
884 | * we need to allocate enough memory for the worst case number of | |
c3eabc75 | 885 | * cpus in the magazine, skc_mag[NR_CPUS]. |
e5b9b344 | 886 | */ |
c3eabc75 | 887 | skc = kzalloc(sizeof (*skc), lflags); |
e5b9b344 BB |
888 | if (skc == NULL) |
889 | return (NULL); | |
890 | ||
891 | skc->skc_magic = SKC_MAGIC; | |
892 | skc->skc_name_size = strlen(name) + 1; | |
c3eabc75 | 893 | skc->skc_name = (char *)kmalloc(skc->skc_name_size, lflags); |
e5b9b344 | 894 | if (skc->skc_name == NULL) { |
c3eabc75 | 895 | kfree(skc); |
e5b9b344 BB |
896 | return (NULL); |
897 | } | |
898 | strncpy(skc->skc_name, name, skc->skc_name_size); | |
899 | ||
900 | skc->skc_ctor = ctor; | |
901 | skc->skc_dtor = dtor; | |
902 | skc->skc_reclaim = reclaim; | |
903 | skc->skc_private = priv; | |
904 | skc->skc_vmp = vmp; | |
905 | skc->skc_linux_cache = NULL; | |
906 | skc->skc_flags = flags; | |
907 | skc->skc_obj_size = size; | |
908 | skc->skc_obj_align = SPL_KMEM_CACHE_ALIGN; | |
909 | skc->skc_delay = SPL_KMEM_CACHE_DELAY; | |
910 | skc->skc_reap = SPL_KMEM_CACHE_REAP; | |
911 | atomic_set(&skc->skc_ref, 0); | |
912 | ||
913 | INIT_LIST_HEAD(&skc->skc_list); | |
914 | INIT_LIST_HEAD(&skc->skc_complete_list); | |
915 | INIT_LIST_HEAD(&skc->skc_partial_list); | |
916 | skc->skc_emergency_tree = RB_ROOT; | |
917 | spin_lock_init(&skc->skc_lock); | |
918 | init_waitqueue_head(&skc->skc_waitq); | |
919 | skc->skc_slab_fail = 0; | |
920 | skc->skc_slab_create = 0; | |
921 | skc->skc_slab_destroy = 0; | |
922 | skc->skc_slab_total = 0; | |
923 | skc->skc_slab_alloc = 0; | |
924 | skc->skc_slab_max = 0; | |
925 | skc->skc_obj_total = 0; | |
926 | skc->skc_obj_alloc = 0; | |
927 | skc->skc_obj_max = 0; | |
928 | skc->skc_obj_deadlock = 0; | |
929 | skc->skc_obj_emergency = 0; | |
930 | skc->skc_obj_emergency_max = 0; | |
931 | ||
932 | /* | |
933 | * Verify the requested alignment restriction is sane. | |
934 | */ | |
935 | if (align) { | |
936 | VERIFY(ISP2(align)); | |
937 | VERIFY3U(align, >=, SPL_KMEM_CACHE_ALIGN); | |
938 | VERIFY3U(align, <=, PAGE_SIZE); | |
939 | skc->skc_obj_align = align; | |
940 | } | |
941 | ||
942 | /* | |
943 | * When no specific type of slab is requested (kmem, vmem, or | |
944 | * linuxslab) then select a cache type based on the object size | |
945 | * and default tunables. | |
946 | */ | |
947 | if (!(skc->skc_flags & (KMC_KMEM | KMC_VMEM | KMC_SLAB))) { | |
948 | ||
949 | /* | |
950 | * Objects smaller than spl_kmem_cache_slab_limit can | |
951 | * use the Linux slab for better space-efficiency. By | |
952 | * default this functionality is disabled until its | |
b34b9563 | 953 | * performance characteristics are fully understood. |
e5b9b344 BB |
954 | */ |
955 | if (spl_kmem_cache_slab_limit && | |
956 | size <= (size_t)spl_kmem_cache_slab_limit) | |
957 | skc->skc_flags |= KMC_SLAB; | |
958 | ||
959 | /* | |
960 | * Small objects, less than spl_kmem_cache_kmem_limit per | |
961 | * object should use kmem because their slabs are small. | |
962 | */ | |
963 | else if (spl_obj_size(skc) <= spl_kmem_cache_kmem_limit) | |
964 | skc->skc_flags |= KMC_KMEM; | |
965 | ||
966 | /* | |
967 | * All other objects are considered large and are placed | |
968 | * on vmem backed slabs. | |
969 | */ | |
970 | else | |
971 | skc->skc_flags |= KMC_VMEM; | |
972 | } | |
973 | ||
974 | /* | |
975 | * Given the type of slab allocate the required resources. | |
976 | */ | |
977 | if (skc->skc_flags & (KMC_KMEM | KMC_VMEM)) { | |
978 | rc = spl_slab_size(skc, | |
979 | &skc->skc_slab_objs, &skc->skc_slab_size); | |
980 | if (rc) | |
981 | goto out; | |
982 | ||
983 | rc = spl_magazine_create(skc); | |
984 | if (rc) | |
985 | goto out; | |
986 | } else { | |
3018bffa BB |
987 | if (size > (SPL_MAX_KMEM_ORDER_NR_PAGES * PAGE_SIZE)) { |
988 | rc = EINVAL; | |
989 | goto out; | |
990 | } | |
991 | ||
e5b9b344 BB |
992 | skc->skc_linux_cache = kmem_cache_create( |
993 | skc->skc_name, size, align, 0, NULL); | |
994 | if (skc->skc_linux_cache == NULL) { | |
995 | rc = ENOMEM; | |
996 | goto out; | |
997 | } | |
998 | ||
c3eabc75 BB |
999 | #if defined(HAVE_KMEM_CACHE_ALLOCFLAGS) |
1000 | skc->skc_linux_cache->allocflags |= __GFP_COMP; | |
1001 | #elif defined(HAVE_KMEM_CACHE_GFPFLAGS) | |
1002 | skc->skc_linux_cache->gfpflags |= __GFP_COMP; | |
1003 | #endif | |
e5b9b344 BB |
1004 | skc->skc_flags |= KMC_NOMAGAZINE; |
1005 | } | |
1006 | ||
1007 | if (spl_kmem_cache_expire & KMC_EXPIRE_AGE) | |
1008 | skc->skc_taskqid = taskq_dispatch_delay(spl_kmem_cache_taskq, | |
1009 | spl_cache_age, skc, TQ_SLEEP, | |
1010 | ddi_get_lbolt() + skc->skc_delay / 3 * HZ); | |
1011 | ||
1012 | down_write(&spl_kmem_cache_sem); | |
1013 | list_add_tail(&skc->skc_list, &spl_kmem_cache_list); | |
1014 | up_write(&spl_kmem_cache_sem); | |
1015 | ||
1016 | return (skc); | |
1017 | out: | |
c3eabc75 BB |
1018 | kfree(skc->skc_name); |
1019 | kfree(skc); | |
e5b9b344 BB |
1020 | return (NULL); |
1021 | } | |
1022 | EXPORT_SYMBOL(spl_kmem_cache_create); | |
1023 | ||
1024 | /* | |
b34b9563 | 1025 | * Register a move callback for cache defragmentation. |
e5b9b344 BB |
1026 | * XXX: Unimplemented but harmless to stub out for now. |
1027 | */ | |
1028 | void | |
1029 | spl_kmem_cache_set_move(spl_kmem_cache_t *skc, | |
1030 | kmem_cbrc_t (move)(void *, void *, size_t, void *)) | |
1031 | { | |
b34b9563 | 1032 | ASSERT(move != NULL); |
e5b9b344 BB |
1033 | } |
1034 | EXPORT_SYMBOL(spl_kmem_cache_set_move); | |
1035 | ||
1036 | /* | |
1037 | * Destroy a cache and all objects associated with the cache. | |
1038 | */ | |
1039 | void | |
1040 | spl_kmem_cache_destroy(spl_kmem_cache_t *skc) | |
1041 | { | |
1042 | DECLARE_WAIT_QUEUE_HEAD(wq); | |
1043 | taskqid_t id; | |
1044 | ||
1045 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
1046 | ASSERT(skc->skc_flags & (KMC_KMEM | KMC_VMEM | KMC_SLAB)); | |
1047 | ||
1048 | down_write(&spl_kmem_cache_sem); | |
1049 | list_del_init(&skc->skc_list); | |
1050 | up_write(&spl_kmem_cache_sem); | |
1051 | ||
1052 | /* Cancel any and wait for any pending delayed tasks */ | |
1053 | VERIFY(!test_and_set_bit(KMC_BIT_DESTROY, &skc->skc_flags)); | |
1054 | ||
1055 | spin_lock(&skc->skc_lock); | |
1056 | id = skc->skc_taskqid; | |
1057 | spin_unlock(&skc->skc_lock); | |
1058 | ||
1059 | taskq_cancel_id(spl_kmem_cache_taskq, id); | |
1060 | ||
b34b9563 BB |
1061 | /* |
1062 | * Wait until all current callers complete, this is mainly | |
e5b9b344 | 1063 | * to catch the case where a low memory situation triggers a |
b34b9563 BB |
1064 | * cache reaping action which races with this destroy. |
1065 | */ | |
e5b9b344 BB |
1066 | wait_event(wq, atomic_read(&skc->skc_ref) == 0); |
1067 | ||
1068 | if (skc->skc_flags & (KMC_KMEM | KMC_VMEM)) { | |
1069 | spl_magazine_destroy(skc); | |
1a204968 | 1070 | spl_slab_reclaim(skc); |
e5b9b344 BB |
1071 | } else { |
1072 | ASSERT(skc->skc_flags & KMC_SLAB); | |
1073 | kmem_cache_destroy(skc->skc_linux_cache); | |
1074 | } | |
1075 | ||
1076 | spin_lock(&skc->skc_lock); | |
1077 | ||
b34b9563 BB |
1078 | /* |
1079 | * Validate there are no objects in use and free all the | |
1080 | * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. | |
1081 | */ | |
e5b9b344 BB |
1082 | ASSERT3U(skc->skc_slab_alloc, ==, 0); |
1083 | ASSERT3U(skc->skc_obj_alloc, ==, 0); | |
1084 | ASSERT3U(skc->skc_slab_total, ==, 0); | |
1085 | ASSERT3U(skc->skc_obj_total, ==, 0); | |
1086 | ASSERT3U(skc->skc_obj_emergency, ==, 0); | |
1087 | ASSERT(list_empty(&skc->skc_complete_list)); | |
1088 | ||
e5b9b344 BB |
1089 | spin_unlock(&skc->skc_lock); |
1090 | ||
c3eabc75 BB |
1091 | kfree(skc->skc_name); |
1092 | kfree(skc); | |
e5b9b344 BB |
1093 | } |
1094 | EXPORT_SYMBOL(spl_kmem_cache_destroy); | |
1095 | ||
1096 | /* | |
1097 | * Allocate an object from a slab attached to the cache. This is used to | |
1098 | * repopulate the per-cpu magazine caches in batches when they run low. | |
1099 | */ | |
1100 | static void * | |
1101 | spl_cache_obj(spl_kmem_cache_t *skc, spl_kmem_slab_t *sks) | |
1102 | { | |
1103 | spl_kmem_obj_t *sko; | |
1104 | ||
1105 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
1106 | ASSERT(sks->sks_magic == SKS_MAGIC); | |
1107 | ASSERT(spin_is_locked(&skc->skc_lock)); | |
1108 | ||
1109 | sko = list_entry(sks->sks_free_list.next, spl_kmem_obj_t, sko_list); | |
1110 | ASSERT(sko->sko_magic == SKO_MAGIC); | |
1111 | ASSERT(sko->sko_addr != NULL); | |
1112 | ||
1113 | /* Remove from sks_free_list */ | |
1114 | list_del_init(&sko->sko_list); | |
1115 | ||
1116 | sks->sks_age = jiffies; | |
1117 | sks->sks_ref++; | |
1118 | skc->skc_obj_alloc++; | |
1119 | ||
1120 | /* Track max obj usage statistics */ | |
1121 | if (skc->skc_obj_alloc > skc->skc_obj_max) | |
1122 | skc->skc_obj_max = skc->skc_obj_alloc; | |
1123 | ||
1124 | /* Track max slab usage statistics */ | |
1125 | if (sks->sks_ref == 1) { | |
1126 | skc->skc_slab_alloc++; | |
1127 | ||
1128 | if (skc->skc_slab_alloc > skc->skc_slab_max) | |
1129 | skc->skc_slab_max = skc->skc_slab_alloc; | |
1130 | } | |
1131 | ||
b34b9563 | 1132 | return (sko->sko_addr); |
e5b9b344 BB |
1133 | } |
1134 | ||
1135 | /* | |
1136 | * Generic slab allocation function to run by the global work queues. | |
1137 | * It is responsible for allocating a new slab, linking it in to the list | |
1138 | * of partial slabs, and then waking any waiters. | |
1139 | */ | |
1140 | static void | |
1141 | spl_cache_grow_work(void *data) | |
1142 | { | |
1143 | spl_kmem_alloc_t *ska = (spl_kmem_alloc_t *)data; | |
1144 | spl_kmem_cache_t *skc = ska->ska_cache; | |
1145 | spl_kmem_slab_t *sks; | |
1146 | ||
c3eabc75 BB |
1147 | #if defined(PF_MEMALLOC_NOIO) |
1148 | unsigned noio_flag = memalloc_noio_save(); | |
1149 | sks = spl_slab_alloc(skc, ska->ska_flags); | |
1150 | memalloc_noio_restore(noio_flag); | |
1151 | #else | |
c2fa0945 | 1152 | fstrans_cookie_t cookie = spl_fstrans_mark(); |
c3eabc75 | 1153 | sks = spl_slab_alloc(skc, ska->ska_flags); |
c2fa0945 | 1154 | spl_fstrans_unmark(cookie); |
c3eabc75 | 1155 | #endif |
e5b9b344 BB |
1156 | spin_lock(&skc->skc_lock); |
1157 | if (sks) { | |
1158 | skc->skc_slab_total++; | |
1159 | skc->skc_obj_total += sks->sks_objs; | |
1160 | list_add_tail(&sks->sks_list, &skc->skc_partial_list); | |
1161 | } | |
1162 | ||
1163 | atomic_dec(&skc->skc_ref); | |
a988a35a | 1164 | smp_mb__before_atomic(); |
e5b9b344 BB |
1165 | clear_bit(KMC_BIT_GROWING, &skc->skc_flags); |
1166 | clear_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags); | |
a988a35a | 1167 | smp_mb__after_atomic(); |
e5b9b344 BB |
1168 | wake_up_all(&skc->skc_waitq); |
1169 | spin_unlock(&skc->skc_lock); | |
1170 | ||
1171 | kfree(ska); | |
1172 | } | |
1173 | ||
1174 | /* | |
1175 | * Returns non-zero when a new slab should be available. | |
1176 | */ | |
1177 | static int | |
1178 | spl_cache_grow_wait(spl_kmem_cache_t *skc) | |
1179 | { | |
b34b9563 | 1180 | return (!test_bit(KMC_BIT_GROWING, &skc->skc_flags)); |
e5b9b344 BB |
1181 | } |
1182 | ||
1183 | /* | |
1184 | * No available objects on any slabs, create a new slab. Note that this | |
1185 | * functionality is disabled for KMC_SLAB caches which are backed by the | |
1186 | * Linux slab. | |
1187 | */ | |
1188 | static int | |
1189 | spl_cache_grow(spl_kmem_cache_t *skc, int flags, void **obj) | |
1190 | { | |
c3eabc75 | 1191 | int remaining, rc = 0; |
e5b9b344 | 1192 | |
c3eabc75 | 1193 | ASSERT0(flags & ~KM_PUBLIC_MASK); |
e5b9b344 BB |
1194 | ASSERT(skc->skc_magic == SKC_MAGIC); |
1195 | ASSERT((skc->skc_flags & KMC_SLAB) == 0); | |
1196 | might_sleep(); | |
1197 | *obj = NULL; | |
1198 | ||
1199 | /* | |
1200 | * Before allocating a new slab wait for any reaping to complete and | |
1201 | * then return so the local magazine can be rechecked for new objects. | |
1202 | */ | |
1203 | if (test_bit(KMC_BIT_REAPING, &skc->skc_flags)) { | |
1204 | rc = spl_wait_on_bit(&skc->skc_flags, KMC_BIT_REAPING, | |
1205 | TASK_UNINTERRUPTIBLE); | |
1206 | return (rc ? rc : -EAGAIN); | |
1207 | } | |
1208 | ||
1209 | /* | |
1210 | * This is handled by dispatching a work request to the global work | |
1211 | * queue. This allows us to asynchronously allocate a new slab while | |
1212 | * retaining the ability to safely fall back to a smaller synchronous | |
1213 | * allocations to ensure forward progress is always maintained. | |
1214 | */ | |
1215 | if (test_and_set_bit(KMC_BIT_GROWING, &skc->skc_flags) == 0) { | |
1216 | spl_kmem_alloc_t *ska; | |
1217 | ||
c3eabc75 | 1218 | ska = kmalloc(sizeof (*ska), kmem_flags_convert(flags)); |
e5b9b344 | 1219 | if (ska == NULL) { |
a988a35a RY |
1220 | clear_bit_unlock(KMC_BIT_GROWING, &skc->skc_flags); |
1221 | smp_mb__after_atomic(); | |
e5b9b344 BB |
1222 | wake_up_all(&skc->skc_waitq); |
1223 | return (-ENOMEM); | |
1224 | } | |
1225 | ||
1226 | atomic_inc(&skc->skc_ref); | |
1227 | ska->ska_cache = skc; | |
c3eabc75 | 1228 | ska->ska_flags = flags; |
e5b9b344 BB |
1229 | taskq_init_ent(&ska->ska_tqe); |
1230 | taskq_dispatch_ent(spl_kmem_cache_taskq, | |
1231 | spl_cache_grow_work, ska, 0, &ska->ska_tqe); | |
1232 | } | |
1233 | ||
1234 | /* | |
1235 | * The goal here is to only detect the rare case where a virtual slab | |
1236 | * allocation has deadlocked. We must be careful to minimize the use | |
1237 | * of emergency objects which are more expensive to track. Therefore, | |
1238 | * we set a very long timeout for the asynchronous allocation and if | |
1239 | * the timeout is reached the cache is flagged as deadlocked. From | |
1240 | * this point only new emergency objects will be allocated until the | |
1241 | * asynchronous allocation completes and clears the deadlocked flag. | |
1242 | */ | |
1243 | if (test_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags)) { | |
1244 | rc = spl_emergency_alloc(skc, flags, obj); | |
1245 | } else { | |
1246 | remaining = wait_event_timeout(skc->skc_waitq, | |
e50e6cc9 | 1247 | spl_cache_grow_wait(skc), HZ / 10); |
e5b9b344 | 1248 | |
436ad60f | 1249 | if (!remaining) { |
e5b9b344 BB |
1250 | spin_lock(&skc->skc_lock); |
1251 | if (test_bit(KMC_BIT_GROWING, &skc->skc_flags)) { | |
1252 | set_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags); | |
1253 | skc->skc_obj_deadlock++; | |
1254 | } | |
1255 | spin_unlock(&skc->skc_lock); | |
1256 | } | |
1257 | ||
1258 | rc = -ENOMEM; | |
1259 | } | |
1260 | ||
1261 | return (rc); | |
1262 | } | |
1263 | ||
1264 | /* | |
1265 | * Refill a per-cpu magazine with objects from the slabs for this cache. | |
1266 | * Ideally the magazine can be repopulated using existing objects which have | |
1267 | * been released, however if we are unable to locate enough free objects new | |
1268 | * slabs of objects will be created. On success NULL is returned, otherwise | |
1269 | * the address of a single emergency object is returned for use by the caller. | |
1270 | */ | |
1271 | static void * | |
1272 | spl_cache_refill(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flags) | |
1273 | { | |
1274 | spl_kmem_slab_t *sks; | |
1275 | int count = 0, rc, refill; | |
1276 | void *obj = NULL; | |
1277 | ||
1278 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
1279 | ASSERT(skm->skm_magic == SKM_MAGIC); | |
1280 | ||
1281 | refill = MIN(skm->skm_refill, skm->skm_size - skm->skm_avail); | |
1282 | spin_lock(&skc->skc_lock); | |
1283 | ||
1284 | while (refill > 0) { | |
1285 | /* No slabs available we may need to grow the cache */ | |
1286 | if (list_empty(&skc->skc_partial_list)) { | |
1287 | spin_unlock(&skc->skc_lock); | |
1288 | ||
1289 | local_irq_enable(); | |
1290 | rc = spl_cache_grow(skc, flags, &obj); | |
1291 | local_irq_disable(); | |
1292 | ||
1293 | /* Emergency object for immediate use by caller */ | |
1294 | if (rc == 0 && obj != NULL) | |
1295 | return (obj); | |
1296 | ||
1297 | if (rc) | |
1298 | goto out; | |
1299 | ||
1300 | /* Rescheduled to different CPU skm is not local */ | |
1301 | if (skm != skc->skc_mag[smp_processor_id()]) | |
1302 | goto out; | |
1303 | ||
b34b9563 BB |
1304 | /* |
1305 | * Potentially rescheduled to the same CPU but | |
e5b9b344 | 1306 | * allocations may have occurred from this CPU while |
b34b9563 BB |
1307 | * we were sleeping so recalculate max refill. |
1308 | */ | |
e5b9b344 BB |
1309 | refill = MIN(refill, skm->skm_size - skm->skm_avail); |
1310 | ||
1311 | spin_lock(&skc->skc_lock); | |
1312 | continue; | |
1313 | } | |
1314 | ||
1315 | /* Grab the next available slab */ | |
1316 | sks = list_entry((&skc->skc_partial_list)->next, | |
b34b9563 | 1317 | spl_kmem_slab_t, sks_list); |
e5b9b344 BB |
1318 | ASSERT(sks->sks_magic == SKS_MAGIC); |
1319 | ASSERT(sks->sks_ref < sks->sks_objs); | |
1320 | ASSERT(!list_empty(&sks->sks_free_list)); | |
1321 | ||
b34b9563 BB |
1322 | /* |
1323 | * Consume as many objects as needed to refill the requested | |
1324 | * cache. We must also be careful not to overfill it. | |
1325 | */ | |
1326 | while (sks->sks_ref < sks->sks_objs && refill-- > 0 && | |
1327 | ++count) { | |
e5b9b344 BB |
1328 | ASSERT(skm->skm_avail < skm->skm_size); |
1329 | ASSERT(count < skm->skm_size); | |
b34b9563 BB |
1330 | skm->skm_objs[skm->skm_avail++] = |
1331 | spl_cache_obj(skc, sks); | |
e5b9b344 BB |
1332 | } |
1333 | ||
1334 | /* Move slab to skc_complete_list when full */ | |
1335 | if (sks->sks_ref == sks->sks_objs) { | |
1336 | list_del(&sks->sks_list); | |
1337 | list_add(&sks->sks_list, &skc->skc_complete_list); | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | spin_unlock(&skc->skc_lock); | |
1342 | out: | |
1343 | return (NULL); | |
1344 | } | |
1345 | ||
1346 | /* | |
1347 | * Release an object back to the slab from which it came. | |
1348 | */ | |
1349 | static void | |
1350 | spl_cache_shrink(spl_kmem_cache_t *skc, void *obj) | |
1351 | { | |
1352 | spl_kmem_slab_t *sks = NULL; | |
1353 | spl_kmem_obj_t *sko = NULL; | |
1354 | ||
1355 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
1356 | ASSERT(spin_is_locked(&skc->skc_lock)); | |
1357 | ||
1358 | sko = spl_sko_from_obj(skc, obj); | |
1359 | ASSERT(sko->sko_magic == SKO_MAGIC); | |
1360 | sks = sko->sko_slab; | |
1361 | ASSERT(sks->sks_magic == SKS_MAGIC); | |
1362 | ASSERT(sks->sks_cache == skc); | |
1363 | list_add(&sko->sko_list, &sks->sks_free_list); | |
1364 | ||
1365 | sks->sks_age = jiffies; | |
1366 | sks->sks_ref--; | |
1367 | skc->skc_obj_alloc--; | |
1368 | ||
b34b9563 BB |
1369 | /* |
1370 | * Move slab to skc_partial_list when no longer full. Slabs | |
e5b9b344 | 1371 | * are added to the head to keep the partial list is quasi-full |
b34b9563 BB |
1372 | * sorted order. Fuller at the head, emptier at the tail. |
1373 | */ | |
e5b9b344 BB |
1374 | if (sks->sks_ref == (sks->sks_objs - 1)) { |
1375 | list_del(&sks->sks_list); | |
1376 | list_add(&sks->sks_list, &skc->skc_partial_list); | |
1377 | } | |
1378 | ||
b34b9563 BB |
1379 | /* |
1380 | * Move empty slabs to the end of the partial list so | |
1381 | * they can be easily found and freed during reclamation. | |
1382 | */ | |
e5b9b344 BB |
1383 | if (sks->sks_ref == 0) { |
1384 | list_del(&sks->sks_list); | |
1385 | list_add_tail(&sks->sks_list, &skc->skc_partial_list); | |
1386 | skc->skc_slab_alloc--; | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | /* | |
1391 | * Allocate an object from the per-cpu magazine, or if the magazine | |
1392 | * is empty directly allocate from a slab and repopulate the magazine. | |
1393 | */ | |
1394 | void * | |
1395 | spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags) | |
1396 | { | |
1397 | spl_kmem_magazine_t *skm; | |
1398 | void *obj = NULL; | |
1399 | ||
c3eabc75 | 1400 | ASSERT0(flags & ~KM_PUBLIC_MASK); |
e5b9b344 BB |
1401 | ASSERT(skc->skc_magic == SKC_MAGIC); |
1402 | ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags)); | |
e5b9b344 BB |
1403 | |
1404 | atomic_inc(&skc->skc_ref); | |
1405 | ||
1406 | /* | |
1407 | * Allocate directly from a Linux slab. All optimizations are left | |
1408 | * to the underlying cache we only need to guarantee that KM_SLEEP | |
1409 | * callers will never fail. | |
1410 | */ | |
1411 | if (skc->skc_flags & KMC_SLAB) { | |
1412 | struct kmem_cache *slc = skc->skc_linux_cache; | |
e5b9b344 | 1413 | do { |
c3eabc75 | 1414 | obj = kmem_cache_alloc(slc, kmem_flags_convert(flags)); |
e5b9b344 BB |
1415 | } while ((obj == NULL) && !(flags & KM_NOSLEEP)); |
1416 | ||
1417 | goto ret; | |
1418 | } | |
1419 | ||
1420 | local_irq_disable(); | |
1421 | ||
1422 | restart: | |
b34b9563 BB |
1423 | /* |
1424 | * Safe to update per-cpu structure without lock, but | |
e5b9b344 BB |
1425 | * in the restart case we must be careful to reacquire |
1426 | * the local magazine since this may have changed | |
b34b9563 BB |
1427 | * when we need to grow the cache. |
1428 | */ | |
e5b9b344 BB |
1429 | skm = skc->skc_mag[smp_processor_id()]; |
1430 | ASSERT(skm->skm_magic == SKM_MAGIC); | |
1431 | ||
1432 | if (likely(skm->skm_avail)) { | |
1433 | /* Object available in CPU cache, use it */ | |
1434 | obj = skm->skm_objs[--skm->skm_avail]; | |
1435 | skm->skm_age = jiffies; | |
1436 | } else { | |
1437 | obj = spl_cache_refill(skc, skm, flags); | |
3018bffa | 1438 | if ((obj == NULL) && !(flags & KM_NOSLEEP)) |
e5b9b344 | 1439 | goto restart; |
3018bffa BB |
1440 | |
1441 | local_irq_enable(); | |
1442 | goto ret; | |
e5b9b344 BB |
1443 | } |
1444 | ||
1445 | local_irq_enable(); | |
1446 | ASSERT(obj); | |
1447 | ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align)); | |
1448 | ||
1449 | ret: | |
1450 | /* Pre-emptively migrate object to CPU L1 cache */ | |
1451 | if (obj) { | |
1452 | if (obj && skc->skc_ctor) | |
1453 | skc->skc_ctor(obj, skc->skc_private, flags); | |
1454 | else | |
1455 | prefetchw(obj); | |
1456 | } | |
1457 | ||
1458 | atomic_dec(&skc->skc_ref); | |
1459 | ||
1460 | return (obj); | |
1461 | } | |
e5b9b344 BB |
1462 | EXPORT_SYMBOL(spl_kmem_cache_alloc); |
1463 | ||
1464 | /* | |
1465 | * Free an object back to the local per-cpu magazine, there is no | |
1466 | * guarantee that this is the same magazine the object was originally | |
1467 | * allocated from. We may need to flush entire from the magazine | |
1468 | * back to the slabs to make space. | |
1469 | */ | |
1470 | void | |
1471 | spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj) | |
1472 | { | |
1473 | spl_kmem_magazine_t *skm; | |
1474 | unsigned long flags; | |
1a204968 | 1475 | int do_reclaim = 0; |
436ad60f | 1476 | int do_emergency = 0; |
e5b9b344 BB |
1477 | |
1478 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
1479 | ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags)); | |
1480 | atomic_inc(&skc->skc_ref); | |
1481 | ||
1482 | /* | |
1483 | * Run the destructor | |
1484 | */ | |
1485 | if (skc->skc_dtor) | |
1486 | skc->skc_dtor(obj, skc->skc_private); | |
1487 | ||
1488 | /* | |
1489 | * Free the object from the Linux underlying Linux slab. | |
1490 | */ | |
1491 | if (skc->skc_flags & KMC_SLAB) { | |
1492 | kmem_cache_free(skc->skc_linux_cache, obj); | |
1493 | goto out; | |
1494 | } | |
1495 | ||
1496 | /* | |
436ad60f BB |
1497 | * While a cache has outstanding emergency objects all freed objects |
1498 | * must be checked. However, since emergency objects will never use | |
1499 | * a virtual address these objects can be safely excluded as an | |
1500 | * optimization. | |
e5b9b344 | 1501 | */ |
436ad60f BB |
1502 | if (!is_vmalloc_addr(obj)) { |
1503 | spin_lock(&skc->skc_lock); | |
1504 | do_emergency = (skc->skc_obj_emergency > 0); | |
1505 | spin_unlock(&skc->skc_lock); | |
1506 | ||
1507 | if (do_emergency && (spl_emergency_free(skc, obj) == 0)) | |
1508 | goto out; | |
e5b9b344 BB |
1509 | } |
1510 | ||
1511 | local_irq_save(flags); | |
1512 | ||
b34b9563 BB |
1513 | /* |
1514 | * Safe to update per-cpu structure without lock, but | |
e5b9b344 BB |
1515 | * no remote memory allocation tracking is being performed |
1516 | * it is entirely possible to allocate an object from one | |
b34b9563 BB |
1517 | * CPU cache and return it to another. |
1518 | */ | |
e5b9b344 BB |
1519 | skm = skc->skc_mag[smp_processor_id()]; |
1520 | ASSERT(skm->skm_magic == SKM_MAGIC); | |
1521 | ||
1a204968 BB |
1522 | /* |
1523 | * Per-CPU cache full, flush it to make space for this object, | |
1524 | * this may result in an empty slab which can be reclaimed once | |
1525 | * interrupts are re-enabled. | |
1526 | */ | |
1527 | if (unlikely(skm->skm_avail >= skm->skm_size)) { | |
e5b9b344 | 1528 | spl_cache_flush(skc, skm, skm->skm_refill); |
1a204968 BB |
1529 | do_reclaim = 1; |
1530 | } | |
e5b9b344 BB |
1531 | |
1532 | /* Available space in cache, use it */ | |
1533 | skm->skm_objs[skm->skm_avail++] = obj; | |
1534 | ||
1535 | local_irq_restore(flags); | |
1a204968 BB |
1536 | |
1537 | if (do_reclaim) | |
1538 | spl_slab_reclaim(skc); | |
e5b9b344 BB |
1539 | out: |
1540 | atomic_dec(&skc->skc_ref); | |
1541 | } | |
1542 | EXPORT_SYMBOL(spl_kmem_cache_free); | |
1543 | ||
1544 | /* | |
1545 | * The generic shrinker function for all caches. Under Linux a shrinker | |
1546 | * may not be tightly coupled with a slab cache. In fact Linux always | |
1547 | * systematically tries calling all registered shrinker callbacks which | |
1548 | * report that they contain unused objects. Because of this we only | |
1549 | * register one shrinker function in the shim layer for all slab caches. | |
1550 | * We always attempt to shrink all caches when this generic shrinker | |
1551 | * is called. | |
1552 | * | |
1553 | * If sc->nr_to_scan is zero, the caller is requesting a query of the | |
1554 | * number of objects which can potentially be freed. If it is nonzero, | |
1555 | * the request is to free that many objects. | |
1556 | * | |
1557 | * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks | |
1558 | * in struct shrinker and also require the shrinker to return the number | |
1559 | * of objects freed. | |
1560 | * | |
1561 | * Older kernels require the shrinker to return the number of freeable | |
1562 | * objects following the freeing of nr_to_free. | |
1563 | * | |
1564 | * Linux semantics differ from those under Solaris, which are to | |
1565 | * free all available objects which may (and probably will) be more | |
1566 | * objects than the requested nr_to_scan. | |
1567 | */ | |
1568 | static spl_shrinker_t | |
1569 | __spl_kmem_cache_generic_shrinker(struct shrinker *shrink, | |
1570 | struct shrink_control *sc) | |
1571 | { | |
1572 | spl_kmem_cache_t *skc; | |
1573 | int alloc = 0; | |
1574 | ||
1575 | down_read(&spl_kmem_cache_sem); | |
1576 | list_for_each_entry(skc, &spl_kmem_cache_list, skc_list) { | |
1577 | if (sc->nr_to_scan) { | |
1578 | #ifdef HAVE_SPLIT_SHRINKER_CALLBACK | |
1579 | uint64_t oldalloc = skc->skc_obj_alloc; | |
1580 | spl_kmem_cache_reap_now(skc, | |
b34b9563 | 1581 | MAX(sc->nr_to_scan>>fls64(skc->skc_slab_objs), 1)); |
e5b9b344 BB |
1582 | if (oldalloc > skc->skc_obj_alloc) |
1583 | alloc += oldalloc - skc->skc_obj_alloc; | |
1584 | #else | |
1585 | spl_kmem_cache_reap_now(skc, | |
b34b9563 | 1586 | MAX(sc->nr_to_scan>>fls64(skc->skc_slab_objs), 1)); |
e5b9b344 BB |
1587 | alloc += skc->skc_obj_alloc; |
1588 | #endif /* HAVE_SPLIT_SHRINKER_CALLBACK */ | |
1589 | } else { | |
1590 | /* Request to query number of freeable objects */ | |
1591 | alloc += skc->skc_obj_alloc; | |
1592 | } | |
1593 | } | |
1594 | up_read(&spl_kmem_cache_sem); | |
1595 | ||
1596 | /* | |
1597 | * When KMC_RECLAIM_ONCE is set allow only a single reclaim pass. | |
1598 | * This functionality only exists to work around a rare issue where | |
1599 | * shrink_slabs() is repeatedly invoked by many cores causing the | |
1600 | * system to thrash. | |
1601 | */ | |
1602 | if ((spl_kmem_cache_reclaim & KMC_RECLAIM_ONCE) && sc->nr_to_scan) | |
1603 | return (SHRINK_STOP); | |
1604 | ||
1605 | return (MAX(alloc, 0)); | |
1606 | } | |
1607 | ||
1608 | SPL_SHRINKER_CALLBACK_WRAPPER(spl_kmem_cache_generic_shrinker); | |
1609 | ||
1610 | /* | |
1611 | * Call the registered reclaim function for a cache. Depending on how | |
1612 | * many and which objects are released it may simply repopulate the | |
1613 | * local magazine which will then need to age-out. Objects which cannot | |
1614 | * fit in the magazine we will be released back to their slabs which will | |
1615 | * also need to age out before being release. This is all just best | |
1616 | * effort and we do not want to thrash creating and destroying slabs. | |
1617 | */ | |
1618 | void | |
1619 | spl_kmem_cache_reap_now(spl_kmem_cache_t *skc, int count) | |
1620 | { | |
1621 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
1622 | ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags)); | |
1623 | ||
1624 | atomic_inc(&skc->skc_ref); | |
1625 | ||
1626 | /* | |
1627 | * Execute the registered reclaim callback if it exists. The | |
1628 | * per-cpu caches will be drained when is set KMC_EXPIRE_MEM. | |
1629 | */ | |
1630 | if (skc->skc_flags & KMC_SLAB) { | |
1631 | if (skc->skc_reclaim) | |
1632 | skc->skc_reclaim(skc->skc_private); | |
1633 | ||
1634 | if (spl_kmem_cache_expire & KMC_EXPIRE_MEM) | |
1635 | kmem_cache_shrink(skc->skc_linux_cache); | |
1636 | ||
1637 | goto out; | |
1638 | } | |
1639 | ||
1640 | /* | |
1641 | * Prevent concurrent cache reaping when contended. | |
1642 | */ | |
1643 | if (test_and_set_bit(KMC_BIT_REAPING, &skc->skc_flags)) | |
1644 | goto out; | |
1645 | ||
1646 | /* | |
1647 | * When a reclaim function is available it may be invoked repeatedly | |
1648 | * until at least a single slab can be freed. This ensures that we | |
1649 | * do free memory back to the system. This helps minimize the chance | |
1650 | * of an OOM event when the bulk of memory is used by the slab. | |
1651 | * | |
1652 | * When free slabs are already available the reclaim callback will be | |
1653 | * skipped. Additionally, if no forward progress is detected despite | |
1654 | * a reclaim function the cache will be skipped to avoid deadlock. | |
1655 | * | |
1656 | * Longer term this would be the correct place to add the code which | |
1657 | * repacks the slabs in order minimize fragmentation. | |
1658 | */ | |
1659 | if (skc->skc_reclaim) { | |
1660 | uint64_t objects = UINT64_MAX; | |
1661 | int do_reclaim; | |
1662 | ||
1663 | do { | |
1664 | spin_lock(&skc->skc_lock); | |
1665 | do_reclaim = | |
1666 | (skc->skc_slab_total > 0) && | |
b34b9563 | 1667 | ((skc->skc_slab_total-skc->skc_slab_alloc) == 0) && |
e5b9b344 BB |
1668 | (skc->skc_obj_alloc < objects); |
1669 | ||
1670 | objects = skc->skc_obj_alloc; | |
1671 | spin_unlock(&skc->skc_lock); | |
1672 | ||
1673 | if (do_reclaim) | |
1674 | skc->skc_reclaim(skc->skc_private); | |
1675 | ||
1676 | } while (do_reclaim); | |
1677 | } | |
1678 | ||
1a204968 | 1679 | /* Reclaim from the magazine and free all now empty slabs. */ |
e5b9b344 BB |
1680 | if (spl_kmem_cache_expire & KMC_EXPIRE_MEM) { |
1681 | spl_kmem_magazine_t *skm; | |
1682 | unsigned long irq_flags; | |
1683 | ||
1684 | local_irq_save(irq_flags); | |
1685 | skm = skc->skc_mag[smp_processor_id()]; | |
1686 | spl_cache_flush(skc, skm, skm->skm_avail); | |
1687 | local_irq_restore(irq_flags); | |
1688 | } | |
1689 | ||
1a204968 | 1690 | spl_slab_reclaim(skc); |
a988a35a RY |
1691 | clear_bit_unlock(KMC_BIT_REAPING, &skc->skc_flags); |
1692 | smp_mb__after_atomic(); | |
e5b9b344 BB |
1693 | wake_up_bit(&skc->skc_flags, KMC_BIT_REAPING); |
1694 | out: | |
1695 | atomic_dec(&skc->skc_ref); | |
1696 | } | |
1697 | EXPORT_SYMBOL(spl_kmem_cache_reap_now); | |
1698 | ||
1699 | /* | |
1700 | * Reap all free slabs from all registered caches. | |
1701 | */ | |
1702 | void | |
1703 | spl_kmem_reap(void) | |
1704 | { | |
1705 | struct shrink_control sc; | |
1706 | ||
1707 | sc.nr_to_scan = KMC_REAP_CHUNK; | |
1708 | sc.gfp_mask = GFP_KERNEL; | |
1709 | ||
1710 | (void) __spl_kmem_cache_generic_shrinker(NULL, &sc); | |
1711 | } | |
1712 | EXPORT_SYMBOL(spl_kmem_reap); | |
1713 | ||
1714 | int | |
1715 | spl_kmem_cache_init(void) | |
1716 | { | |
1717 | init_rwsem(&spl_kmem_cache_sem); | |
1718 | INIT_LIST_HEAD(&spl_kmem_cache_list); | |
1719 | spl_kmem_cache_taskq = taskq_create("spl_kmem_cache", | |
436ad60f | 1720 | spl_kmem_cache_kmem_threads, maxclsyspri, 1, 32, TASKQ_PREPOPULATE); |
e5b9b344 BB |
1721 | spl_register_shrinker(&spl_kmem_cache_shrinker); |
1722 | ||
1723 | return (0); | |
1724 | } | |
1725 | ||
1726 | void | |
1727 | spl_kmem_cache_fini(void) | |
1728 | { | |
1729 | spl_unregister_shrinker(&spl_kmem_cache_shrinker); | |
1730 | taskq_destroy(spl_kmem_cache_taskq); | |
1731 | } |