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