]>
Commit | Line | Data |
---|---|---|
716154c5 BB |
1 | /*****************************************************************************\ |
2 | * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. | |
3 | * Copyright (C) 2007 The Regents of the University of California. | |
4 | * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). | |
5 | * Written by Brian Behlendorf <behlendorf1@llnl.gov>. | |
715f6251 | 6 | * UCRL-CODE-235197 |
7 | * | |
716154c5 | 8 | * This file is part of the SPL, Solaris Porting Layer. |
3d6af2dd | 9 | * For details, see <http://zfsonlinux.org/>. |
715f6251 | 10 | * |
716154c5 BB |
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 | |
715f6251 | 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 | |
716154c5 BB |
22 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. |
23 | ***************************************************************************** | |
24 | * Solaris Porting Layer (SPL) Kmem Implementation. | |
25 | \*****************************************************************************/ | |
715f6251 | 26 | |
f4b37741 | 27 | #include <sys/kmem.h> |
55abb092 | 28 | #include <spl-debug.h> |
f1ca4da6 | 29 | |
b17edc10 BB |
30 | #ifdef SS_DEBUG_SUBSYS |
31 | #undef SS_DEBUG_SUBSYS | |
937879f1 | 32 | #endif |
33 | ||
b17edc10 | 34 | #define SS_DEBUG_SUBSYS SS_KMEM |
937879f1 | 35 | |
0936c344 BB |
36 | /* |
37 | * Cache expiration was implemented because it was part of the default Solaris | |
38 | * kmem_cache behavior. The idea is that per-cpu objects which haven't been | |
39 | * accessed in several seconds should be returned to the cache. On the other | |
40 | * hand Linux slabs never move objects back to the slabs unless there is | |
41 | * memory pressure on the system. By default both methods are disabled, but | |
42 | * may be enabled by setting KMC_EXPIRE_AGE or KMC_EXPIRE_MEM. | |
43 | */ | |
44 | unsigned int spl_kmem_cache_expire = 0; | |
45 | EXPORT_SYMBOL(spl_kmem_cache_expire); | |
46 | module_param(spl_kmem_cache_expire, uint, 0644); | |
47 | MODULE_PARM_DESC(spl_kmem_cache_expire, "By age (0x1) or low memory (0x2)"); | |
48 | ||
36b313da BB |
49 | /* |
50 | * The minimum amount of memory measured in pages to be free at all | |
51 | * times on the system. This is similar to Linux's zone->pages_min | |
ecc39810 | 52 | * multiplied by the number of zones and is sized based on that. |
36b313da BB |
53 | */ |
54 | pgcnt_t minfree = 0; | |
55 | EXPORT_SYMBOL(minfree); | |
56 | ||
57 | /* | |
58 | * The desired amount of memory measured in pages to be free at all | |
59 | * times on the system. This is similar to Linux's zone->pages_low | |
ecc39810 | 60 | * multiplied by the number of zones and is sized based on that. |
36b313da | 61 | * Assuming all zones are being used roughly equally, when we drop |
ecc39810 | 62 | * below this threshold asynchronous page reclamation is triggered. |
36b313da BB |
63 | */ |
64 | pgcnt_t desfree = 0; | |
65 | EXPORT_SYMBOL(desfree); | |
66 | ||
67 | /* | |
68 | * When above this amount of memory measures in pages the system is | |
69 | * determined to have enough free memory. This is similar to Linux's | |
ecc39810 | 70 | * zone->pages_high multiplied by the number of zones and is sized based |
36b313da | 71 | * on that. Assuming all zones are being used roughly equally, when |
ecc39810 | 72 | * asynchronous page reclamation reaches this threshold it stops. |
36b313da BB |
73 | */ |
74 | pgcnt_t lotsfree = 0; | |
75 | EXPORT_SYMBOL(lotsfree); | |
76 | ||
77 | /* Unused always 0 in this implementation */ | |
78 | pgcnt_t needfree = 0; | |
79 | EXPORT_SYMBOL(needfree); | |
80 | ||
36b313da BB |
81 | pgcnt_t swapfs_minfree = 0; |
82 | EXPORT_SYMBOL(swapfs_minfree); | |
83 | ||
84 | pgcnt_t swapfs_reserve = 0; | |
85 | EXPORT_SYMBOL(swapfs_reserve); | |
86 | ||
36b313da BB |
87 | vmem_t *heap_arena = NULL; |
88 | EXPORT_SYMBOL(heap_arena); | |
89 | ||
90 | vmem_t *zio_alloc_arena = NULL; | |
91 | EXPORT_SYMBOL(zio_alloc_arena); | |
92 | ||
93 | vmem_t *zio_arena = NULL; | |
94 | EXPORT_SYMBOL(zio_arena); | |
95 | ||
d1ff2312 | 96 | #ifndef HAVE_GET_VMALLOC_INFO |
96dded38 | 97 | get_vmalloc_info_t get_vmalloc_info_fn = SYMBOL_POISON; |
d1ff2312 BB |
98 | EXPORT_SYMBOL(get_vmalloc_info_fn); |
99 | #endif /* HAVE_GET_VMALLOC_INFO */ | |
100 | ||
5232d256 BB |
101 | #ifdef HAVE_PGDAT_HELPERS |
102 | # ifndef HAVE_FIRST_ONLINE_PGDAT | |
96dded38 | 103 | first_online_pgdat_t first_online_pgdat_fn = SYMBOL_POISON; |
d1ff2312 | 104 | EXPORT_SYMBOL(first_online_pgdat_fn); |
5232d256 | 105 | # endif /* HAVE_FIRST_ONLINE_PGDAT */ |
36b313da | 106 | |
5232d256 | 107 | # ifndef HAVE_NEXT_ONLINE_PGDAT |
96dded38 | 108 | next_online_pgdat_t next_online_pgdat_fn = SYMBOL_POISON; |
d1ff2312 | 109 | EXPORT_SYMBOL(next_online_pgdat_fn); |
5232d256 | 110 | # endif /* HAVE_NEXT_ONLINE_PGDAT */ |
36b313da | 111 | |
5232d256 | 112 | # ifndef HAVE_NEXT_ZONE |
96dded38 | 113 | next_zone_t next_zone_fn = SYMBOL_POISON; |
d1ff2312 | 114 | EXPORT_SYMBOL(next_zone_fn); |
5232d256 BB |
115 | # endif /* HAVE_NEXT_ZONE */ |
116 | ||
117 | #else /* HAVE_PGDAT_HELPERS */ | |
118 | ||
119 | # ifndef HAVE_PGDAT_LIST | |
120 | struct pglist_data *pgdat_list_addr = SYMBOL_POISON; | |
121 | EXPORT_SYMBOL(pgdat_list_addr); | |
122 | # endif /* HAVE_PGDAT_LIST */ | |
123 | ||
124 | #endif /* HAVE_PGDAT_HELPERS */ | |
36b313da | 125 | |
6ae7fef5 | 126 | #ifdef NEED_GET_ZONE_COUNTS |
e11d6c5f | 127 | # ifndef HAVE_GET_ZONE_COUNTS |
96dded38 | 128 | get_zone_counts_t get_zone_counts_fn = SYMBOL_POISON; |
d1ff2312 | 129 | EXPORT_SYMBOL(get_zone_counts_fn); |
96dded38 | 130 | # endif /* HAVE_GET_ZONE_COUNTS */ |
4ab13d3b | 131 | |
e11d6c5f | 132 | unsigned long |
6ae7fef5 | 133 | spl_global_page_state(spl_zone_stat_item_t item) |
4ab13d3b BB |
134 | { |
135 | unsigned long active; | |
136 | unsigned long inactive; | |
137 | unsigned long free; | |
138 | ||
6ae7fef5 BB |
139 | get_zone_counts(&active, &inactive, &free); |
140 | switch (item) { | |
141 | case SPL_NR_FREE_PAGES: return free; | |
142 | case SPL_NR_INACTIVE: return inactive; | |
143 | case SPL_NR_ACTIVE: return active; | |
144 | default: ASSERT(0); /* Unsupported */ | |
e11d6c5f BB |
145 | } |
146 | ||
6ae7fef5 BB |
147 | return 0; |
148 | } | |
149 | #else | |
150 | # ifdef HAVE_GLOBAL_PAGE_STATE | |
151 | unsigned long | |
152 | spl_global_page_state(spl_zone_stat_item_t item) | |
153 | { | |
154 | unsigned long pages = 0; | |
155 | ||
156 | switch (item) { | |
157 | case SPL_NR_FREE_PAGES: | |
158 | # ifdef HAVE_ZONE_STAT_ITEM_NR_FREE_PAGES | |
159 | pages += global_page_state(NR_FREE_PAGES); | |
160 | # endif | |
161 | break; | |
162 | case SPL_NR_INACTIVE: | |
163 | # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE | |
164 | pages += global_page_state(NR_INACTIVE); | |
165 | # endif | |
166 | # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_ANON | |
167 | pages += global_page_state(NR_INACTIVE_ANON); | |
168 | # endif | |
169 | # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_FILE | |
170 | pages += global_page_state(NR_INACTIVE_FILE); | |
171 | # endif | |
172 | break; | |
173 | case SPL_NR_ACTIVE: | |
174 | # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE | |
175 | pages += global_page_state(NR_ACTIVE); | |
176 | # endif | |
177 | # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_ANON | |
178 | pages += global_page_state(NR_ACTIVE_ANON); | |
179 | # endif | |
180 | # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_FILE | |
181 | pages += global_page_state(NR_ACTIVE_FILE); | |
182 | # endif | |
183 | break; | |
184 | default: | |
185 | ASSERT(0); /* Unsupported */ | |
e11d6c5f BB |
186 | } |
187 | ||
6ae7fef5 BB |
188 | return pages; |
189 | } | |
96dded38 | 190 | # else |
6ae7fef5 | 191 | # error "Both global_page_state() and get_zone_counts() unavailable" |
96dded38 | 192 | # endif /* HAVE_GLOBAL_PAGE_STATE */ |
6ae7fef5 | 193 | #endif /* NEED_GET_ZONE_COUNTS */ |
e11d6c5f | 194 | EXPORT_SYMBOL(spl_global_page_state); |
4ab13d3b | 195 | |
e76f4bf1 BB |
196 | #ifndef HAVE_SHRINK_DCACHE_MEMORY |
197 | shrink_dcache_memory_t shrink_dcache_memory_fn = SYMBOL_POISON; | |
198 | EXPORT_SYMBOL(shrink_dcache_memory_fn); | |
199 | #endif /* HAVE_SHRINK_DCACHE_MEMORY */ | |
200 | ||
201 | #ifndef HAVE_SHRINK_ICACHE_MEMORY | |
202 | shrink_icache_memory_t shrink_icache_memory_fn = SYMBOL_POISON; | |
203 | EXPORT_SYMBOL(shrink_icache_memory_fn); | |
204 | #endif /* HAVE_SHRINK_ICACHE_MEMORY */ | |
205 | ||
e11d6c5f BB |
206 | pgcnt_t |
207 | spl_kmem_availrmem(void) | |
208 | { | |
4ab13d3b | 209 | /* The amount of easily available memory */ |
6ae7fef5 BB |
210 | return (spl_global_page_state(SPL_NR_FREE_PAGES) + |
211 | spl_global_page_state(SPL_NR_INACTIVE)); | |
4ab13d3b BB |
212 | } |
213 | EXPORT_SYMBOL(spl_kmem_availrmem); | |
214 | ||
215 | size_t | |
216 | vmem_size(vmem_t *vmp, int typemask) | |
217 | { | |
d1ff2312 BB |
218 | struct vmalloc_info vmi; |
219 | size_t size = 0; | |
220 | ||
4ab13d3b BB |
221 | ASSERT(vmp == NULL); |
222 | ASSERT(typemask & (VMEM_ALLOC | VMEM_FREE)); | |
223 | ||
d1ff2312 BB |
224 | get_vmalloc_info(&vmi); |
225 | if (typemask & VMEM_ALLOC) | |
226 | size += (size_t)vmi.used; | |
227 | ||
228 | if (typemask & VMEM_FREE) | |
229 | size += (size_t)(VMALLOC_TOTAL - vmi.used); | |
230 | ||
231 | return size; | |
4ab13d3b BB |
232 | } |
233 | EXPORT_SYMBOL(vmem_size); | |
4ab13d3b | 234 | |
b868e22f BB |
235 | int |
236 | kmem_debugging(void) | |
237 | { | |
238 | return 0; | |
239 | } | |
240 | EXPORT_SYMBOL(kmem_debugging); | |
241 | ||
242 | #ifndef HAVE_KVASPRINTF | |
243 | /* Simplified asprintf. */ | |
244 | char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap) | |
245 | { | |
246 | unsigned int len; | |
247 | char *p; | |
248 | va_list aq; | |
249 | ||
250 | va_copy(aq, ap); | |
251 | len = vsnprintf(NULL, 0, fmt, aq); | |
252 | va_end(aq); | |
253 | ||
254 | p = kmalloc(len+1, gfp); | |
255 | if (!p) | |
256 | return NULL; | |
257 | ||
258 | vsnprintf(p, len+1, fmt, ap); | |
259 | ||
260 | return p; | |
261 | } | |
262 | EXPORT_SYMBOL(kvasprintf); | |
263 | #endif /* HAVE_KVASPRINTF */ | |
264 | ||
e6de04b7 BB |
265 | char * |
266 | kmem_vasprintf(const char *fmt, va_list ap) | |
267 | { | |
268 | va_list aq; | |
269 | char *ptr; | |
270 | ||
e6de04b7 | 271 | do { |
2c762de8 | 272 | va_copy(aq, ap); |
e6de04b7 | 273 | ptr = kvasprintf(GFP_KERNEL, fmt, aq); |
2c762de8 | 274 | va_end(aq); |
e6de04b7 | 275 | } while (ptr == NULL); |
e6de04b7 BB |
276 | |
277 | return ptr; | |
278 | } | |
279 | EXPORT_SYMBOL(kmem_vasprintf); | |
280 | ||
b868e22f BB |
281 | char * |
282 | kmem_asprintf(const char *fmt, ...) | |
283 | { | |
e6de04b7 | 284 | va_list ap; |
b868e22f BB |
285 | char *ptr; |
286 | ||
b868e22f | 287 | do { |
2c762de8 | 288 | va_start(ap, fmt); |
e6de04b7 | 289 | ptr = kvasprintf(GFP_KERNEL, fmt, ap); |
2c762de8 | 290 | va_end(ap); |
b868e22f | 291 | } while (ptr == NULL); |
b868e22f BB |
292 | |
293 | return ptr; | |
294 | } | |
295 | EXPORT_SYMBOL(kmem_asprintf); | |
296 | ||
10129680 BB |
297 | static char * |
298 | __strdup(const char *str, int flags) | |
299 | { | |
300 | char *ptr; | |
301 | int n; | |
302 | ||
303 | n = strlen(str); | |
304 | ptr = kmalloc_nofail(n + 1, flags); | |
305 | if (ptr) | |
306 | memcpy(ptr, str, n + 1); | |
307 | ||
308 | return ptr; | |
309 | } | |
310 | ||
311 | char * | |
312 | strdup(const char *str) | |
313 | { | |
314 | return __strdup(str, KM_SLEEP); | |
315 | } | |
316 | EXPORT_SYMBOL(strdup); | |
317 | ||
318 | void | |
319 | strfree(char *str) | |
320 | { | |
41f84a8d | 321 | kfree(str); |
10129680 BB |
322 | } |
323 | EXPORT_SYMBOL(strfree); | |
324 | ||
f1ca4da6 | 325 | /* |
2fb9b26a | 326 | * Memory allocation interfaces and debugging for basic kmem_* |
055ffd98 BB |
327 | * and vmem_* style memory allocation. When DEBUG_KMEM is enabled |
328 | * the SPL will keep track of the total memory allocated, and | |
329 | * report any memory leaked when the module is unloaded. | |
f1ca4da6 | 330 | */ |
331 | #ifdef DEBUG_KMEM | |
d04c8a56 | 332 | |
f1ca4da6 | 333 | /* Shim layer memory accounting */ |
d04c8a56 | 334 | # ifdef HAVE_ATOMIC64_T |
550f1705 | 335 | atomic64_t kmem_alloc_used = ATOMIC64_INIT(0); |
a0f6da3d | 336 | unsigned long long kmem_alloc_max = 0; |
550f1705 | 337 | atomic64_t vmem_alloc_used = ATOMIC64_INIT(0); |
a0f6da3d | 338 | unsigned long long vmem_alloc_max = 0; |
10129680 | 339 | # else /* HAVE_ATOMIC64_T */ |
d04c8a56 BB |
340 | atomic_t kmem_alloc_used = ATOMIC_INIT(0); |
341 | unsigned long long kmem_alloc_max = 0; | |
342 | atomic_t vmem_alloc_used = ATOMIC_INIT(0); | |
343 | unsigned long long vmem_alloc_max = 0; | |
10129680 | 344 | # endif /* HAVE_ATOMIC64_T */ |
79b31f36 | 345 | |
ff449ac4 | 346 | EXPORT_SYMBOL(kmem_alloc_used); |
347 | EXPORT_SYMBOL(kmem_alloc_max); | |
348 | EXPORT_SYMBOL(vmem_alloc_used); | |
349 | EXPORT_SYMBOL(vmem_alloc_max); | |
ff449ac4 | 350 | |
055ffd98 BB |
351 | /* When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked |
352 | * but also the location of every alloc and free. When the SPL module is | |
353 | * unloaded a list of all leaked addresses and where they were allocated | |
354 | * will be dumped to the console. Enabling this feature has a significant | |
355 | * impact on performance but it makes finding memory leaks straight forward. | |
356 | * | |
357 | * Not surprisingly with debugging enabled the xmem_locks are very highly | |
358 | * contended particularly on xfree(). If we want to run with this detailed | |
359 | * debugging enabled for anything other than debugging we need to minimize | |
360 | * the contention by moving to a lock per xmem_table entry model. | |
a0f6da3d | 361 | */ |
055ffd98 | 362 | # ifdef DEBUG_KMEM_TRACKING |
a0f6da3d | 363 | |
364 | # define KMEM_HASH_BITS 10 | |
365 | # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS) | |
366 | ||
367 | # define VMEM_HASH_BITS 10 | |
368 | # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS) | |
369 | ||
370 | typedef struct kmem_debug { | |
371 | struct hlist_node kd_hlist; /* Hash node linkage */ | |
372 | struct list_head kd_list; /* List of all allocations */ | |
373 | void *kd_addr; /* Allocation pointer */ | |
374 | size_t kd_size; /* Allocation size */ | |
375 | const char *kd_func; /* Allocation function */ | |
376 | int kd_line; /* Allocation line */ | |
377 | } kmem_debug_t; | |
378 | ||
d6a26c6a | 379 | spinlock_t kmem_lock; |
380 | struct hlist_head kmem_table[KMEM_TABLE_SIZE]; | |
381 | struct list_head kmem_list; | |
382 | ||
13cdca65 | 383 | spinlock_t vmem_lock; |
384 | struct hlist_head vmem_table[VMEM_TABLE_SIZE]; | |
385 | struct list_head vmem_list; | |
386 | ||
d6a26c6a | 387 | EXPORT_SYMBOL(kmem_lock); |
388 | EXPORT_SYMBOL(kmem_table); | |
389 | EXPORT_SYMBOL(kmem_list); | |
390 | ||
13cdca65 | 391 | EXPORT_SYMBOL(vmem_lock); |
392 | EXPORT_SYMBOL(vmem_table); | |
393 | EXPORT_SYMBOL(vmem_list); | |
a0f6da3d | 394 | |
395 | static kmem_debug_t * | |
973e8269 | 396 | kmem_del_init(spinlock_t *lock, struct hlist_head *table, int bits, const void *addr) |
a0f6da3d | 397 | { |
398 | struct hlist_head *head; | |
399 | struct hlist_node *node; | |
400 | struct kmem_debug *p; | |
401 | unsigned long flags; | |
b17edc10 | 402 | SENTRY; |
a0f6da3d | 403 | |
404 | spin_lock_irqsave(lock, flags); | |
405 | ||
b1424add BB |
406 | head = &table[hash_ptr((void *)addr, bits)]; |
407 | hlist_for_each(node, head) { | |
408 | p = list_entry(node, struct kmem_debug, kd_hlist); | |
a0f6da3d | 409 | if (p->kd_addr == addr) { |
410 | hlist_del_init(&p->kd_hlist); | |
411 | list_del_init(&p->kd_list); | |
412 | spin_unlock_irqrestore(lock, flags); | |
413 | return p; | |
414 | } | |
415 | } | |
416 | ||
417 | spin_unlock_irqrestore(lock, flags); | |
418 | ||
b17edc10 | 419 | SRETURN(NULL); |
a0f6da3d | 420 | } |
421 | ||
422 | void * | |
423 | kmem_alloc_track(size_t size, int flags, const char *func, int line, | |
424 | int node_alloc, int node) | |
425 | { | |
426 | void *ptr = NULL; | |
427 | kmem_debug_t *dptr; | |
428 | unsigned long irq_flags; | |
b17edc10 | 429 | SENTRY; |
a0f6da3d | 430 | |
10129680 | 431 | /* Function may be called with KM_NOSLEEP so failure is possible */ |
c89fdee4 | 432 | dptr = (kmem_debug_t *) kmalloc_nofail(sizeof(kmem_debug_t), |
a0f6da3d | 433 | flags & ~__GFP_ZERO); |
434 | ||
10129680 | 435 | if (unlikely(dptr == NULL)) { |
b17edc10 | 436 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "debug " |
3cb77549 BB |
437 | "kmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n", |
438 | sizeof(kmem_debug_t), flags, func, line, | |
439 | kmem_alloc_used_read(), kmem_alloc_max); | |
a0f6da3d | 440 | } else { |
10129680 BB |
441 | /* |
442 | * Marked unlikely because we should never be doing this, | |
443 | * we tolerate to up 2 pages but a single page is best. | |
444 | */ | |
23d91792 | 445 | if (unlikely((size > PAGE_SIZE*2) && !(flags & KM_NODEBUG))) { |
b17edc10 | 446 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "large " |
3cb77549 BB |
447 | "kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n", |
448 | (unsigned long long) size, flags, func, line, | |
d04c8a56 | 449 | kmem_alloc_used_read(), kmem_alloc_max); |
5198ea0e BB |
450 | spl_debug_dumpstack(NULL); |
451 | } | |
a0f6da3d | 452 | |
10129680 BB |
453 | /* |
454 | * We use __strdup() below because the string pointed to by | |
c8e60837 | 455 | * __FUNCTION__ might not be available by the time we want |
10129680 BB |
456 | * to print it since the module might have been unloaded. |
457 | * This can only fail in the KM_NOSLEEP case. | |
458 | */ | |
459 | dptr->kd_func = __strdup(func, flags & ~__GFP_ZERO); | |
c8e60837 | 460 | if (unlikely(dptr->kd_func == NULL)) { |
461 | kfree(dptr); | |
b17edc10 | 462 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, |
10129680 | 463 | "debug __strdup() at %s:%d failed (%lld/%llu)\n", |
3cb77549 | 464 | func, line, kmem_alloc_used_read(), kmem_alloc_max); |
c8e60837 | 465 | goto out; |
466 | } | |
467 | ||
a0f6da3d | 468 | /* Use the correct allocator */ |
469 | if (node_alloc) { | |
470 | ASSERT(!(flags & __GFP_ZERO)); | |
c89fdee4 | 471 | ptr = kmalloc_node_nofail(size, flags, node); |
a0f6da3d | 472 | } else if (flags & __GFP_ZERO) { |
c89fdee4 | 473 | ptr = kzalloc_nofail(size, flags & ~__GFP_ZERO); |
a0f6da3d | 474 | } else { |
c89fdee4 | 475 | ptr = kmalloc_nofail(size, flags); |
a0f6da3d | 476 | } |
477 | ||
478 | if (unlikely(ptr == NULL)) { | |
c8e60837 | 479 | kfree(dptr->kd_func); |
a0f6da3d | 480 | kfree(dptr); |
b17edc10 | 481 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "kmem_alloc" |
3cb77549 BB |
482 | "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n", |
483 | (unsigned long long) size, flags, func, line, | |
d04c8a56 | 484 | kmem_alloc_used_read(), kmem_alloc_max); |
a0f6da3d | 485 | goto out; |
486 | } | |
487 | ||
d04c8a56 BB |
488 | kmem_alloc_used_add(size); |
489 | if (unlikely(kmem_alloc_used_read() > kmem_alloc_max)) | |
490 | kmem_alloc_max = kmem_alloc_used_read(); | |
a0f6da3d | 491 | |
492 | INIT_HLIST_NODE(&dptr->kd_hlist); | |
493 | INIT_LIST_HEAD(&dptr->kd_list); | |
494 | ||
495 | dptr->kd_addr = ptr; | |
496 | dptr->kd_size = size; | |
a0f6da3d | 497 | dptr->kd_line = line; |
498 | ||
499 | spin_lock_irqsave(&kmem_lock, irq_flags); | |
b1424add | 500 | hlist_add_head(&dptr->kd_hlist, |
a0f6da3d | 501 | &kmem_table[hash_ptr(ptr, KMEM_HASH_BITS)]); |
502 | list_add_tail(&dptr->kd_list, &kmem_list); | |
503 | spin_unlock_irqrestore(&kmem_lock, irq_flags); | |
504 | ||
b17edc10 | 505 | SDEBUG_LIMIT(SD_INFO, |
3cb77549 BB |
506 | "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n", |
507 | (unsigned long long) size, flags, func, line, ptr, | |
508 | kmem_alloc_used_read(), kmem_alloc_max); | |
a0f6da3d | 509 | } |
510 | out: | |
b17edc10 | 511 | SRETURN(ptr); |
a0f6da3d | 512 | } |
513 | EXPORT_SYMBOL(kmem_alloc_track); | |
514 | ||
515 | void | |
973e8269 | 516 | kmem_free_track(const void *ptr, size_t size) |
a0f6da3d | 517 | { |
518 | kmem_debug_t *dptr; | |
b17edc10 | 519 | SENTRY; |
a0f6da3d | 520 | |
521 | ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr, | |
522 | (unsigned long long) size); | |
523 | ||
524 | dptr = kmem_del_init(&kmem_lock, kmem_table, KMEM_HASH_BITS, ptr); | |
525 | ||
10129680 BB |
526 | /* Must exist in hash due to kmem_alloc() */ |
527 | ASSERT(dptr); | |
a0f6da3d | 528 | |
529 | /* Size must match */ | |
530 | ASSERTF(dptr->kd_size == size, "kd_size (%llu) != size (%llu), " | |
531 | "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr->kd_size, | |
532 | (unsigned long long) size, dptr->kd_func, dptr->kd_line); | |
533 | ||
d04c8a56 | 534 | kmem_alloc_used_sub(size); |
b17edc10 | 535 | SDEBUG_LIMIT(SD_INFO, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr, |
d04c8a56 | 536 | (unsigned long long) size, kmem_alloc_used_read(), |
a0f6da3d | 537 | kmem_alloc_max); |
538 | ||
c8e60837 | 539 | kfree(dptr->kd_func); |
540 | ||
b1424add | 541 | memset((void *)dptr, 0x5a, sizeof(kmem_debug_t)); |
a0f6da3d | 542 | kfree(dptr); |
543 | ||
b1424add | 544 | memset((void *)ptr, 0x5a, size); |
a0f6da3d | 545 | kfree(ptr); |
546 | ||
b17edc10 | 547 | SEXIT; |
a0f6da3d | 548 | } |
549 | EXPORT_SYMBOL(kmem_free_track); | |
550 | ||
551 | void * | |
552 | vmem_alloc_track(size_t size, int flags, const char *func, int line) | |
553 | { | |
554 | void *ptr = NULL; | |
555 | kmem_debug_t *dptr; | |
556 | unsigned long irq_flags; | |
b17edc10 | 557 | SENTRY; |
a0f6da3d | 558 | |
559 | ASSERT(flags & KM_SLEEP); | |
560 | ||
10129680 | 561 | /* Function may be called with KM_NOSLEEP so failure is possible */ |
ef1c7a06 BB |
562 | dptr = (kmem_debug_t *) kmalloc_nofail(sizeof(kmem_debug_t), |
563 | flags & ~__GFP_ZERO); | |
10129680 | 564 | if (unlikely(dptr == NULL)) { |
b17edc10 | 565 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "debug " |
3cb77549 BB |
566 | "vmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n", |
567 | sizeof(kmem_debug_t), flags, func, line, | |
568 | vmem_alloc_used_read(), vmem_alloc_max); | |
a0f6da3d | 569 | } else { |
10129680 BB |
570 | /* |
571 | * We use __strdup() below because the string pointed to by | |
c8e60837 | 572 | * __FUNCTION__ might not be available by the time we want |
10129680 BB |
573 | * to print it, since the module might have been unloaded. |
574 | * This can never fail because we have already asserted | |
575 | * that flags is KM_SLEEP. | |
576 | */ | |
577 | dptr->kd_func = __strdup(func, flags & ~__GFP_ZERO); | |
c8e60837 | 578 | if (unlikely(dptr->kd_func == NULL)) { |
579 | kfree(dptr); | |
b17edc10 | 580 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, |
10129680 | 581 | "debug __strdup() at %s:%d failed (%lld/%llu)\n", |
3cb77549 | 582 | func, line, vmem_alloc_used_read(), vmem_alloc_max); |
c8e60837 | 583 | goto out; |
584 | } | |
585 | ||
10129680 BB |
586 | /* Use the correct allocator */ |
587 | if (flags & __GFP_ZERO) { | |
588 | ptr = vzalloc_nofail(size, flags & ~__GFP_ZERO); | |
589 | } else { | |
590 | ptr = vmalloc_nofail(size, flags); | |
591 | } | |
a0f6da3d | 592 | |
593 | if (unlikely(ptr == NULL)) { | |
c8e60837 | 594 | kfree(dptr->kd_func); |
a0f6da3d | 595 | kfree(dptr); |
b17edc10 | 596 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "vmem_alloc" |
3cb77549 BB |
597 | "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n", |
598 | (unsigned long long) size, flags, func, line, | |
d04c8a56 | 599 | vmem_alloc_used_read(), vmem_alloc_max); |
a0f6da3d | 600 | goto out; |
601 | } | |
602 | ||
d04c8a56 BB |
603 | vmem_alloc_used_add(size); |
604 | if (unlikely(vmem_alloc_used_read() > vmem_alloc_max)) | |
605 | vmem_alloc_max = vmem_alloc_used_read(); | |
a0f6da3d | 606 | |
607 | INIT_HLIST_NODE(&dptr->kd_hlist); | |
608 | INIT_LIST_HEAD(&dptr->kd_list); | |
609 | ||
610 | dptr->kd_addr = ptr; | |
611 | dptr->kd_size = size; | |
a0f6da3d | 612 | dptr->kd_line = line; |
613 | ||
614 | spin_lock_irqsave(&vmem_lock, irq_flags); | |
b1424add | 615 | hlist_add_head(&dptr->kd_hlist, |
a0f6da3d | 616 | &vmem_table[hash_ptr(ptr, VMEM_HASH_BITS)]); |
617 | list_add_tail(&dptr->kd_list, &vmem_list); | |
618 | spin_unlock_irqrestore(&vmem_lock, irq_flags); | |
619 | ||
b17edc10 | 620 | SDEBUG_LIMIT(SD_INFO, |
3cb77549 BB |
621 | "vmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n", |
622 | (unsigned long long) size, flags, func, line, | |
623 | ptr, vmem_alloc_used_read(), vmem_alloc_max); | |
a0f6da3d | 624 | } |
625 | out: | |
b17edc10 | 626 | SRETURN(ptr); |
a0f6da3d | 627 | } |
628 | EXPORT_SYMBOL(vmem_alloc_track); | |
629 | ||
630 | void | |
973e8269 | 631 | vmem_free_track(const void *ptr, size_t size) |
a0f6da3d | 632 | { |
633 | kmem_debug_t *dptr; | |
b17edc10 | 634 | SENTRY; |
a0f6da3d | 635 | |
636 | ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr, | |
637 | (unsigned long long) size); | |
638 | ||
639 | dptr = kmem_del_init(&vmem_lock, vmem_table, VMEM_HASH_BITS, ptr); | |
10129680 BB |
640 | |
641 | /* Must exist in hash due to vmem_alloc() */ | |
642 | ASSERT(dptr); | |
a0f6da3d | 643 | |
644 | /* Size must match */ | |
645 | ASSERTF(dptr->kd_size == size, "kd_size (%llu) != size (%llu), " | |
646 | "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr->kd_size, | |
647 | (unsigned long long) size, dptr->kd_func, dptr->kd_line); | |
648 | ||
d04c8a56 | 649 | vmem_alloc_used_sub(size); |
b17edc10 | 650 | SDEBUG_LIMIT(SD_INFO, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr, |
d04c8a56 | 651 | (unsigned long long) size, vmem_alloc_used_read(), |
a0f6da3d | 652 | vmem_alloc_max); |
653 | ||
c8e60837 | 654 | kfree(dptr->kd_func); |
655 | ||
b1424add | 656 | memset((void *)dptr, 0x5a, sizeof(kmem_debug_t)); |
a0f6da3d | 657 | kfree(dptr); |
658 | ||
b1424add | 659 | memset((void *)ptr, 0x5a, size); |
a0f6da3d | 660 | vfree(ptr); |
661 | ||
b17edc10 | 662 | SEXIT; |
a0f6da3d | 663 | } |
664 | EXPORT_SYMBOL(vmem_free_track); | |
665 | ||
666 | # else /* DEBUG_KMEM_TRACKING */ | |
667 | ||
668 | void * | |
669 | kmem_alloc_debug(size_t size, int flags, const char *func, int line, | |
670 | int node_alloc, int node) | |
671 | { | |
672 | void *ptr; | |
b17edc10 | 673 | SENTRY; |
a0f6da3d | 674 | |
10129680 BB |
675 | /* |
676 | * Marked unlikely because we should never be doing this, | |
677 | * we tolerate to up 2 pages but a single page is best. | |
678 | */ | |
23d91792 | 679 | if (unlikely((size > PAGE_SIZE * 2) && !(flags & KM_NODEBUG))) { |
b17edc10 | 680 | SDEBUG(SD_CONSOLE | SD_WARNING, |
10129680 | 681 | "large kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n", |
3cb77549 | 682 | (unsigned long long) size, flags, func, line, |
d04c8a56 | 683 | kmem_alloc_used_read(), kmem_alloc_max); |
4b2220f0 | 684 | dump_stack(); |
5198ea0e | 685 | } |
a0f6da3d | 686 | |
687 | /* Use the correct allocator */ | |
688 | if (node_alloc) { | |
689 | ASSERT(!(flags & __GFP_ZERO)); | |
c89fdee4 | 690 | ptr = kmalloc_node_nofail(size, flags, node); |
a0f6da3d | 691 | } else if (flags & __GFP_ZERO) { |
c89fdee4 | 692 | ptr = kzalloc_nofail(size, flags & (~__GFP_ZERO)); |
a0f6da3d | 693 | } else { |
c89fdee4 | 694 | ptr = kmalloc_nofail(size, flags); |
a0f6da3d | 695 | } |
696 | ||
10129680 | 697 | if (unlikely(ptr == NULL)) { |
b17edc10 | 698 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, |
3cb77549 BB |
699 | "kmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n", |
700 | (unsigned long long) size, flags, func, line, | |
d04c8a56 | 701 | kmem_alloc_used_read(), kmem_alloc_max); |
a0f6da3d | 702 | } else { |
d04c8a56 BB |
703 | kmem_alloc_used_add(size); |
704 | if (unlikely(kmem_alloc_used_read() > kmem_alloc_max)) | |
705 | kmem_alloc_max = kmem_alloc_used_read(); | |
a0f6da3d | 706 | |
b17edc10 | 707 | SDEBUG_LIMIT(SD_INFO, |
3cb77549 BB |
708 | "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n", |
709 | (unsigned long long) size, flags, func, line, ptr, | |
10129680 | 710 | kmem_alloc_used_read(), kmem_alloc_max); |
a0f6da3d | 711 | } |
10129680 | 712 | |
b17edc10 | 713 | SRETURN(ptr); |
a0f6da3d | 714 | } |
715 | EXPORT_SYMBOL(kmem_alloc_debug); | |
716 | ||
717 | void | |
973e8269 | 718 | kmem_free_debug(const void *ptr, size_t size) |
a0f6da3d | 719 | { |
b17edc10 | 720 | SENTRY; |
a0f6da3d | 721 | |
722 | ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr, | |
723 | (unsigned long long) size); | |
724 | ||
d04c8a56 | 725 | kmem_alloc_used_sub(size); |
b17edc10 | 726 | SDEBUG_LIMIT(SD_INFO, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr, |
d04c8a56 | 727 | (unsigned long long) size, kmem_alloc_used_read(), |
a0f6da3d | 728 | kmem_alloc_max); |
a0f6da3d | 729 | kfree(ptr); |
730 | ||
b17edc10 | 731 | SEXIT; |
a0f6da3d | 732 | } |
733 | EXPORT_SYMBOL(kmem_free_debug); | |
734 | ||
735 | void * | |
736 | vmem_alloc_debug(size_t size, int flags, const char *func, int line) | |
737 | { | |
738 | void *ptr; | |
b17edc10 | 739 | SENTRY; |
a0f6da3d | 740 | |
741 | ASSERT(flags & KM_SLEEP); | |
742 | ||
10129680 BB |
743 | /* Use the correct allocator */ |
744 | if (flags & __GFP_ZERO) { | |
745 | ptr = vzalloc_nofail(size, flags & (~__GFP_ZERO)); | |
746 | } else { | |
747 | ptr = vmalloc_nofail(size, flags); | |
748 | } | |
749 | ||
750 | if (unlikely(ptr == NULL)) { | |
b17edc10 | 751 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, |
3cb77549 BB |
752 | "vmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n", |
753 | (unsigned long long) size, flags, func, line, | |
d04c8a56 | 754 | vmem_alloc_used_read(), vmem_alloc_max); |
a0f6da3d | 755 | } else { |
d04c8a56 BB |
756 | vmem_alloc_used_add(size); |
757 | if (unlikely(vmem_alloc_used_read() > vmem_alloc_max)) | |
758 | vmem_alloc_max = vmem_alloc_used_read(); | |
a0f6da3d | 759 | |
b17edc10 | 760 | SDEBUG_LIMIT(SD_INFO, "vmem_alloc(%llu, 0x%x) = %p " |
a0f6da3d | 761 | "(%lld/%llu)\n", (unsigned long long) size, flags, ptr, |
d04c8a56 | 762 | vmem_alloc_used_read(), vmem_alloc_max); |
a0f6da3d | 763 | } |
764 | ||
b17edc10 | 765 | SRETURN(ptr); |
a0f6da3d | 766 | } |
767 | EXPORT_SYMBOL(vmem_alloc_debug); | |
768 | ||
769 | void | |
973e8269 | 770 | vmem_free_debug(const void *ptr, size_t size) |
a0f6da3d | 771 | { |
b17edc10 | 772 | SENTRY; |
a0f6da3d | 773 | |
774 | ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr, | |
775 | (unsigned long long) size); | |
776 | ||
d04c8a56 | 777 | vmem_alloc_used_sub(size); |
b17edc10 | 778 | SDEBUG_LIMIT(SD_INFO, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr, |
d04c8a56 | 779 | (unsigned long long) size, vmem_alloc_used_read(), |
a0f6da3d | 780 | vmem_alloc_max); |
a0f6da3d | 781 | vfree(ptr); |
782 | ||
b17edc10 | 783 | SEXIT; |
a0f6da3d | 784 | } |
785 | EXPORT_SYMBOL(vmem_free_debug); | |
786 | ||
787 | # endif /* DEBUG_KMEM_TRACKING */ | |
788 | #endif /* DEBUG_KMEM */ | |
789 | ||
10129680 BB |
790 | /* |
791 | * Slab allocation interfaces | |
792 | * | |
793 | * While the Linux slab implementation was inspired by the Solaris | |
ecc39810 | 794 | * implementation I cannot use it to emulate the Solaris APIs. I |
10129680 BB |
795 | * require two features which are not provided by the Linux slab. |
796 | * | |
797 | * 1) Constructors AND destructors. Recent versions of the Linux | |
798 | * kernel have removed support for destructors. This is a deal | |
799 | * breaker for the SPL which contains particularly expensive | |
800 | * initializers for mutex's, condition variables, etc. We also | |
801 | * require a minimal level of cleanup for these data types unlike | |
802 | * many Linux data type which do need to be explicitly destroyed. | |
803 | * | |
804 | * 2) Virtual address space backed slab. Callers of the Solaris slab | |
805 | * expect it to work well for both small are very large allocations. | |
806 | * Because of memory fragmentation the Linux slab which is backed | |
807 | * by kmalloc'ed memory performs very badly when confronted with | |
808 | * large numbers of large allocations. Basing the slab on the | |
ecc39810 | 809 | * virtual address space removes the need for contiguous pages |
10129680 BB |
810 | * and greatly improve performance for large allocations. |
811 | * | |
812 | * For these reasons, the SPL has its own slab implementation with | |
813 | * the needed features. It is not as highly optimized as either the | |
814 | * Solaris or Linux slabs, but it should get me most of what is | |
815 | * needed until it can be optimized or obsoleted by another approach. | |
816 | * | |
817 | * One serious concern I do have about this method is the relatively | |
818 | * small virtual address space on 32bit arches. This will seriously | |
819 | * constrain the size of the slab caches and their performance. | |
820 | * | |
821 | * XXX: Improve the partial slab list by carefully maintaining a | |
822 | * strict ordering of fullest to emptiest slabs based on | |
ecc39810 | 823 | * the slab reference count. This guarantees the when freeing |
10129680 BB |
824 | * slabs back to the system we need only linearly traverse the |
825 | * last N slabs in the list to discover all the freeable slabs. | |
826 | * | |
827 | * XXX: NUMA awareness for optionally allocating memory close to a | |
ecc39810 | 828 | * particular core. This can be advantageous if you know the slab |
10129680 BB |
829 | * object will be short lived and primarily accessed from one core. |
830 | * | |
831 | * XXX: Slab coloring may also yield performance improvements and would | |
832 | * be desirable to implement. | |
833 | */ | |
834 | ||
835 | struct list_head spl_kmem_cache_list; /* List of caches */ | |
836 | struct rw_semaphore spl_kmem_cache_sem; /* Cache list lock */ | |
a10287e0 | 837 | taskq_t *spl_kmem_cache_taskq; /* Task queue for ageing / reclaim */ |
10129680 | 838 | |
d4899f47 | 839 | static void spl_cache_shrink(spl_kmem_cache_t *skc, void *obj); |
10129680 | 840 | |
a55bcaad | 841 | SPL_SHRINKER_CALLBACK_FWD_DECLARE(spl_kmem_cache_generic_shrinker); |
495bd532 BB |
842 | SPL_SHRINKER_DECLARE(spl_kmem_cache_shrinker, |
843 | spl_kmem_cache_generic_shrinker, KMC_DEFAULT_SEEKS); | |
10129680 | 844 | |
a1502d76 | 845 | static void * |
846 | kv_alloc(spl_kmem_cache_t *skc, int size, int flags) | |
fece7c99 | 847 | { |
a1502d76 | 848 | void *ptr; |
f1ca4da6 | 849 | |
8b45dda2 BB |
850 | ASSERT(ISP2(size)); |
851 | ||
500e95c8 | 852 | if (skc->skc_flags & KMC_KMEM) |
8b45dda2 | 853 | ptr = (void *)__get_free_pages(flags, get_order(size)); |
500e95c8 | 854 | else |
617f79de BB |
855 | ptr = __vmalloc(size, flags | __GFP_HIGHMEM, PAGE_KERNEL); |
856 | ||
8b45dda2 BB |
857 | /* Resulting allocated memory will be page aligned */ |
858 | ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE)); | |
fece7c99 | 859 | |
a1502d76 | 860 | return ptr; |
861 | } | |
fece7c99 | 862 | |
a1502d76 | 863 | static void |
864 | kv_free(spl_kmem_cache_t *skc, void *ptr, int size) | |
865 | { | |
8b45dda2 BB |
866 | ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE)); |
867 | ASSERT(ISP2(size)); | |
868 | ||
06089b9e BB |
869 | /* |
870 | * The Linux direct reclaim path uses this out of band value to | |
871 | * determine if forward progress is being made. Normally this is | |
872 | * incremented by kmem_freepages() which is part of the various | |
873 | * Linux slab implementations. However, since we are using none | |
874 | * of that infrastructure we are responsible for incrementing it. | |
875 | */ | |
876 | if (current->reclaim_state) | |
877 | current->reclaim_state->reclaimed_slab += size >> PAGE_SHIFT; | |
878 | ||
8b45dda2 BB |
879 | if (skc->skc_flags & KMC_KMEM) |
880 | free_pages((unsigned long)ptr, get_order(size)); | |
881 | else | |
882 | vfree(ptr); | |
883 | } | |
884 | ||
885 | /* | |
886 | * Required space for each aligned sks. | |
887 | */ | |
888 | static inline uint32_t | |
889 | spl_sks_size(spl_kmem_cache_t *skc) | |
890 | { | |
891 | return P2ROUNDUP_TYPED(sizeof(spl_kmem_slab_t), | |
892 | skc->skc_obj_align, uint32_t); | |
893 | } | |
894 | ||
895 | /* | |
896 | * Required space for each aligned object. | |
897 | */ | |
898 | static inline uint32_t | |
899 | spl_obj_size(spl_kmem_cache_t *skc) | |
900 | { | |
901 | uint32_t align = skc->skc_obj_align; | |
902 | ||
903 | return P2ROUNDUP_TYPED(skc->skc_obj_size, align, uint32_t) + | |
904 | P2ROUNDUP_TYPED(sizeof(spl_kmem_obj_t), align, uint32_t); | |
905 | } | |
906 | ||
907 | /* | |
908 | * Lookup the spl_kmem_object_t for an object given that object. | |
909 | */ | |
910 | static inline spl_kmem_obj_t * | |
911 | spl_sko_from_obj(spl_kmem_cache_t *skc, void *obj) | |
912 | { | |
913 | return obj + P2ROUNDUP_TYPED(skc->skc_obj_size, | |
914 | skc->skc_obj_align, uint32_t); | |
915 | } | |
916 | ||
917 | /* | |
918 | * Required space for each offslab object taking in to account alignment | |
919 | * restrictions and the power-of-two requirement of kv_alloc(). | |
920 | */ | |
921 | static inline uint32_t | |
922 | spl_offslab_size(spl_kmem_cache_t *skc) | |
923 | { | |
924 | return 1UL << (highbit(spl_obj_size(skc)) + 1); | |
fece7c99 | 925 | } |
926 | ||
ea3e6ca9 BB |
927 | /* |
928 | * It's important that we pack the spl_kmem_obj_t structure and the | |
48e0606a BB |
929 | * actual objects in to one large address space to minimize the number |
930 | * of calls to the allocator. It is far better to do a few large | |
931 | * allocations and then subdivide it ourselves. Now which allocator | |
932 | * we use requires balancing a few trade offs. | |
933 | * | |
934 | * For small objects we use kmem_alloc() because as long as you are | |
935 | * only requesting a small number of pages (ideally just one) its cheap. | |
936 | * However, when you start requesting multiple pages with kmem_alloc() | |
ecc39810 | 937 | * it gets increasingly expensive since it requires contiguous pages. |
48e0606a | 938 | * For this reason we shift to vmem_alloc() for slabs of large objects |
ecc39810 | 939 | * which removes the need for contiguous pages. We do not use |
48e0606a BB |
940 | * vmem_alloc() in all cases because there is significant locking |
941 | * overhead in __get_vm_area_node(). This function takes a single | |
ecc39810 | 942 | * global lock when acquiring an available virtual address range which |
48e0606a BB |
943 | * serializes all vmem_alloc()'s for all slab caches. Using slightly |
944 | * different allocation functions for small and large objects should | |
945 | * give us the best of both worlds. | |
946 | * | |
947 | * KMC_ONSLAB KMC_OFFSLAB | |
948 | * | |
949 | * +------------------------+ +-----------------+ | |
950 | * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+ | |
951 | * | skc_obj_size <-+ | | +-----------------+ | | | |
952 | * | spl_kmem_obj_t | | | | | |
953 | * | skc_obj_size <---+ | +-----------------+ | | | |
954 | * | spl_kmem_obj_t | | | skc_obj_size | <-+ | | |
955 | * | ... v | | spl_kmem_obj_t | | | |
956 | * +------------------------+ +-----------------+ v | |
957 | */ | |
fece7c99 | 958 | static spl_kmem_slab_t * |
a1502d76 | 959 | spl_slab_alloc(spl_kmem_cache_t *skc, int flags) |
fece7c99 | 960 | { |
961 | spl_kmem_slab_t *sks; | |
a1502d76 | 962 | spl_kmem_obj_t *sko, *n; |
963 | void *base, *obj; | |
8b45dda2 BB |
964 | uint32_t obj_size, offslab_size = 0; |
965 | int i, rc = 0; | |
48e0606a | 966 | |
a1502d76 | 967 | base = kv_alloc(skc, skc->skc_slab_size, flags); |
968 | if (base == NULL) | |
b17edc10 | 969 | SRETURN(NULL); |
fece7c99 | 970 | |
a1502d76 | 971 | sks = (spl_kmem_slab_t *)base; |
972 | sks->sks_magic = SKS_MAGIC; | |
973 | sks->sks_objs = skc->skc_slab_objs; | |
974 | sks->sks_age = jiffies; | |
975 | sks->sks_cache = skc; | |
976 | INIT_LIST_HEAD(&sks->sks_list); | |
977 | INIT_LIST_HEAD(&sks->sks_free_list); | |
978 | sks->sks_ref = 0; | |
8b45dda2 | 979 | obj_size = spl_obj_size(skc); |
48e0606a | 980 | |
8d177c18 | 981 | if (skc->skc_flags & KMC_OFFSLAB) |
8b45dda2 | 982 | offslab_size = spl_offslab_size(skc); |
fece7c99 | 983 | |
984 | for (i = 0; i < sks->sks_objs; i++) { | |
a1502d76 | 985 | if (skc->skc_flags & KMC_OFFSLAB) { |
8b45dda2 | 986 | obj = kv_alloc(skc, offslab_size, flags); |
a1502d76 | 987 | if (!obj) |
b17edc10 | 988 | SGOTO(out, rc = -ENOMEM); |
a1502d76 | 989 | } else { |
8b45dda2 | 990 | obj = base + spl_sks_size(skc) + (i * obj_size); |
a1502d76 | 991 | } |
992 | ||
8b45dda2 BB |
993 | ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align)); |
994 | sko = spl_sko_from_obj(skc, obj); | |
fece7c99 | 995 | sko->sko_addr = obj; |
996 | sko->sko_magic = SKO_MAGIC; | |
997 | sko->sko_slab = sks; | |
998 | INIT_LIST_HEAD(&sko->sko_list); | |
fece7c99 | 999 | list_add_tail(&sko->sko_list, &sks->sks_free_list); |
1000 | } | |
1001 | ||
fece7c99 | 1002 | list_for_each_entry(sko, &sks->sks_free_list, sko_list) |
1003 | if (skc->skc_ctor) | |
1004 | skc->skc_ctor(sko->sko_addr, skc->skc_private, flags); | |
2fb9b26a | 1005 | out: |
a1502d76 | 1006 | if (rc) { |
1007 | if (skc->skc_flags & KMC_OFFSLAB) | |
48e0606a BB |
1008 | list_for_each_entry_safe(sko, n, &sks->sks_free_list, |
1009 | sko_list) | |
8b45dda2 | 1010 | kv_free(skc, sko->sko_addr, offslab_size); |
fece7c99 | 1011 | |
a1502d76 | 1012 | kv_free(skc, base, skc->skc_slab_size); |
1013 | sks = NULL; | |
fece7c99 | 1014 | } |
1015 | ||
b17edc10 | 1016 | SRETURN(sks); |
fece7c99 | 1017 | } |
1018 | ||
ea3e6ca9 BB |
1019 | /* |
1020 | * Remove a slab from complete or partial list, it must be called with | |
1021 | * the 'skc->skc_lock' held but the actual free must be performed | |
1022 | * outside the lock to prevent deadlocking on vmem addresses. | |
fece7c99 | 1023 | */ |
f1ca4da6 | 1024 | static void |
ea3e6ca9 BB |
1025 | spl_slab_free(spl_kmem_slab_t *sks, |
1026 | struct list_head *sks_list, struct list_head *sko_list) | |
1027 | { | |
2fb9b26a | 1028 | spl_kmem_cache_t *skc; |
b17edc10 | 1029 | SENTRY; |
57d86234 | 1030 | |
2fb9b26a | 1031 | ASSERT(sks->sks_magic == SKS_MAGIC); |
4afaaefa | 1032 | ASSERT(sks->sks_ref == 0); |
d6a26c6a | 1033 | |
fece7c99 | 1034 | skc = sks->sks_cache; |
1035 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
d46630e0 | 1036 | ASSERT(spin_is_locked(&skc->skc_lock)); |
f1ca4da6 | 1037 | |
1a944a7d BB |
1038 | /* |
1039 | * Update slab/objects counters in the cache, then remove the | |
1040 | * slab from the skc->skc_partial_list. Finally add the slab | |
1041 | * and all its objects in to the private work lists where the | |
1042 | * destructors will be called and the memory freed to the system. | |
1043 | */ | |
fece7c99 | 1044 | skc->skc_obj_total -= sks->sks_objs; |
1045 | skc->skc_slab_total--; | |
1046 | list_del(&sks->sks_list); | |
ea3e6ca9 | 1047 | list_add(&sks->sks_list, sks_list); |
1a944a7d BB |
1048 | list_splice_init(&sks->sks_free_list, sko_list); |
1049 | ||
b17edc10 | 1050 | SEXIT; |
2fb9b26a | 1051 | } |
d6a26c6a | 1052 | |
ea3e6ca9 BB |
1053 | /* |
1054 | * Traverses all the partial slabs attached to a cache and free those | |
1055 | * which which are currently empty, and have not been touched for | |
37db7d8c BB |
1056 | * skc_delay seconds to avoid thrashing. The count argument is |
1057 | * passed to optionally cap the number of slabs reclaimed, a count | |
1058 | * of zero means try and reclaim everything. When flag is set we | |
1059 | * always free an available slab regardless of age. | |
ea3e6ca9 BB |
1060 | */ |
1061 | static void | |
37db7d8c | 1062 | spl_slab_reclaim(spl_kmem_cache_t *skc, int count, int flag) |
2fb9b26a | 1063 | { |
1064 | spl_kmem_slab_t *sks, *m; | |
ea3e6ca9 BB |
1065 | spl_kmem_obj_t *sko, *n; |
1066 | LIST_HEAD(sks_list); | |
1067 | LIST_HEAD(sko_list); | |
8b45dda2 BB |
1068 | uint32_t size = 0; |
1069 | int i = 0; | |
b17edc10 | 1070 | SENTRY; |
2fb9b26a | 1071 | |
2fb9b26a | 1072 | /* |
ea3e6ca9 BB |
1073 | * Move empty slabs and objects which have not been touched in |
1074 | * skc_delay seconds on to private lists to be freed outside | |
1a944a7d BB |
1075 | * the spin lock. This delay time is important to avoid thrashing |
1076 | * however when flag is set the delay will not be used. | |
2fb9b26a | 1077 | */ |
ea3e6ca9 | 1078 | spin_lock(&skc->skc_lock); |
1a944a7d BB |
1079 | list_for_each_entry_safe_reverse(sks,m,&skc->skc_partial_list,sks_list){ |
1080 | /* | |
1081 | * All empty slabs are at the end of skc->skc_partial_list, | |
1082 | * therefore once a non-empty slab is found we can stop | |
1083 | * scanning. Additionally, stop when reaching the target | |
ecc39810 | 1084 | * reclaim 'count' if a non-zero threshold is given. |
1a944a7d | 1085 | */ |
cef7605c | 1086 | if ((sks->sks_ref > 0) || (count && i >= count)) |
37db7d8c BB |
1087 | break; |
1088 | ||
37db7d8c | 1089 | if (time_after(jiffies,sks->sks_age+skc->skc_delay*HZ)||flag) { |
ea3e6ca9 | 1090 | spl_slab_free(sks, &sks_list, &sko_list); |
37db7d8c BB |
1091 | i++; |
1092 | } | |
ea3e6ca9 BB |
1093 | } |
1094 | spin_unlock(&skc->skc_lock); | |
1095 | ||
1096 | /* | |
1a944a7d BB |
1097 | * The following two loops ensure all the object destructors are |
1098 | * run, any offslab objects are freed, and the slabs themselves | |
1099 | * are freed. This is all done outside the skc->skc_lock since | |
1100 | * this allows the destructor to sleep, and allows us to perform | |
1101 | * a conditional reschedule when a freeing a large number of | |
1102 | * objects and slabs back to the system. | |
ea3e6ca9 | 1103 | */ |
1a944a7d | 1104 | if (skc->skc_flags & KMC_OFFSLAB) |
8b45dda2 | 1105 | size = spl_offslab_size(skc); |
ea3e6ca9 | 1106 | |
1a944a7d BB |
1107 | list_for_each_entry_safe(sko, n, &sko_list, sko_list) { |
1108 | ASSERT(sko->sko_magic == SKO_MAGIC); | |
1109 | ||
1110 | if (skc->skc_dtor) | |
1111 | skc->skc_dtor(sko->sko_addr, skc->skc_private); | |
1112 | ||
1113 | if (skc->skc_flags & KMC_OFFSLAB) | |
ea3e6ca9 | 1114 | kv_free(skc, sko->sko_addr, size); |
2fb9b26a | 1115 | } |
1116 | ||
37db7d8c | 1117 | list_for_each_entry_safe(sks, m, &sks_list, sks_list) { |
1a944a7d | 1118 | ASSERT(sks->sks_magic == SKS_MAGIC); |
ea3e6ca9 | 1119 | kv_free(skc, sks, skc->skc_slab_size); |
37db7d8c | 1120 | } |
ea3e6ca9 | 1121 | |
b17edc10 | 1122 | SEXIT; |
f1ca4da6 | 1123 | } |
1124 | ||
ed316348 BB |
1125 | static spl_kmem_emergency_t * |
1126 | spl_emergency_search(struct rb_root *root, void *obj) | |
1127 | { | |
1128 | struct rb_node *node = root->rb_node; | |
1129 | spl_kmem_emergency_t *ske; | |
1130 | unsigned long address = (unsigned long)obj; | |
1131 | ||
1132 | while (node) { | |
1133 | ske = container_of(node, spl_kmem_emergency_t, ske_node); | |
1134 | ||
1135 | if (address < (unsigned long)ske->ske_obj) | |
1136 | node = node->rb_left; | |
1137 | else if (address > (unsigned long)ske->ske_obj) | |
1138 | node = node->rb_right; | |
1139 | else | |
1140 | return ske; | |
1141 | } | |
1142 | ||
1143 | return NULL; | |
1144 | } | |
1145 | ||
1146 | static int | |
1147 | spl_emergency_insert(struct rb_root *root, spl_kmem_emergency_t *ske) | |
1148 | { | |
1149 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
1150 | spl_kmem_emergency_t *ske_tmp; | |
1151 | unsigned long address = (unsigned long)ske->ske_obj; | |
1152 | ||
1153 | while (*new) { | |
1154 | ske_tmp = container_of(*new, spl_kmem_emergency_t, ske_node); | |
1155 | ||
1156 | parent = *new; | |
1157 | if (address < (unsigned long)ske_tmp->ske_obj) | |
1158 | new = &((*new)->rb_left); | |
1159 | else if (address > (unsigned long)ske_tmp->ske_obj) | |
1160 | new = &((*new)->rb_right); | |
1161 | else | |
1162 | return 0; | |
1163 | } | |
1164 | ||
1165 | rb_link_node(&ske->ske_node, parent, new); | |
1166 | rb_insert_color(&ske->ske_node, root); | |
1167 | ||
1168 | return 1; | |
1169 | } | |
1170 | ||
e2dcc6e2 | 1171 | /* |
ed316348 | 1172 | * Allocate a single emergency object and track it in a red black tree. |
e2dcc6e2 BB |
1173 | */ |
1174 | static int | |
1175 | spl_emergency_alloc(spl_kmem_cache_t *skc, int flags, void **obj) | |
1176 | { | |
1177 | spl_kmem_emergency_t *ske; | |
1178 | int empty; | |
1179 | SENTRY; | |
1180 | ||
1181 | /* Last chance use a partial slab if one now exists */ | |
1182 | spin_lock(&skc->skc_lock); | |
1183 | empty = list_empty(&skc->skc_partial_list); | |
1184 | spin_unlock(&skc->skc_lock); | |
1185 | if (!empty) | |
1186 | SRETURN(-EEXIST); | |
1187 | ||
1188 | ske = kmalloc(sizeof(*ske), flags); | |
1189 | if (ske == NULL) | |
1190 | SRETURN(-ENOMEM); | |
1191 | ||
1192 | ske->ske_obj = kmalloc(skc->skc_obj_size, flags); | |
1193 | if (ske->ske_obj == NULL) { | |
1194 | kfree(ske); | |
1195 | SRETURN(-ENOMEM); | |
1196 | } | |
1197 | ||
e2dcc6e2 | 1198 | spin_lock(&skc->skc_lock); |
ed316348 BB |
1199 | empty = spl_emergency_insert(&skc->skc_emergency_tree, ske); |
1200 | if (likely(empty)) { | |
1201 | skc->skc_obj_total++; | |
1202 | skc->skc_obj_emergency++; | |
1203 | if (skc->skc_obj_emergency > skc->skc_obj_emergency_max) | |
1204 | skc->skc_obj_emergency_max = skc->skc_obj_emergency; | |
1205 | } | |
e2dcc6e2 BB |
1206 | spin_unlock(&skc->skc_lock); |
1207 | ||
ed316348 BB |
1208 | if (unlikely(!empty)) { |
1209 | kfree(ske->ske_obj); | |
1210 | kfree(ske); | |
1211 | SRETURN(-EINVAL); | |
1212 | } | |
1213 | ||
1214 | if (skc->skc_ctor) | |
1215 | skc->skc_ctor(ske->ske_obj, skc->skc_private, flags); | |
1216 | ||
e2dcc6e2 BB |
1217 | *obj = ske->ske_obj; |
1218 | ||
1219 | SRETURN(0); | |
1220 | } | |
1221 | ||
1222 | /* | |
ed316348 | 1223 | * Locate the passed object in the red black tree and free it. |
e2dcc6e2 BB |
1224 | */ |
1225 | static int | |
1226 | spl_emergency_free(spl_kmem_cache_t *skc, void *obj) | |
1227 | { | |
ed316348 | 1228 | spl_kmem_emergency_t *ske; |
e2dcc6e2 BB |
1229 | SENTRY; |
1230 | ||
1231 | spin_lock(&skc->skc_lock); | |
ed316348 BB |
1232 | ske = spl_emergency_search(&skc->skc_emergency_tree, obj); |
1233 | if (likely(ske)) { | |
1234 | rb_erase(&ske->ske_node, &skc->skc_emergency_tree); | |
1235 | skc->skc_obj_emergency--; | |
1236 | skc->skc_obj_total--; | |
e2dcc6e2 BB |
1237 | } |
1238 | spin_unlock(&skc->skc_lock); | |
1239 | ||
ed316348 | 1240 | if (unlikely(ske == NULL)) |
e2dcc6e2 BB |
1241 | SRETURN(-ENOENT); |
1242 | ||
1243 | if (skc->skc_dtor) | |
1244 | skc->skc_dtor(ske->ske_obj, skc->skc_private); | |
1245 | ||
1246 | kfree(ske->ske_obj); | |
1247 | kfree(ske); | |
1248 | ||
1249 | SRETURN(0); | |
1250 | } | |
1251 | ||
d4899f47 BB |
1252 | /* |
1253 | * Release objects from the per-cpu magazine back to their slab. The flush | |
1254 | * argument contains the max number of entries to remove from the magazine. | |
1255 | */ | |
1256 | static void | |
1257 | __spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush) | |
1258 | { | |
1259 | int i, count = MIN(flush, skm->skm_avail); | |
1260 | SENTRY; | |
1261 | ||
1262 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
1263 | ASSERT(skm->skm_magic == SKM_MAGIC); | |
1264 | ASSERT(spin_is_locked(&skc->skc_lock)); | |
1265 | ||
1266 | for (i = 0; i < count; i++) | |
1267 | spl_cache_shrink(skc, skm->skm_objs[i]); | |
1268 | ||
1269 | skm->skm_avail -= count; | |
1270 | memmove(skm->skm_objs, &(skm->skm_objs[count]), | |
1271 | sizeof(void *) * skm->skm_avail); | |
1272 | ||
1273 | SEXIT; | |
1274 | } | |
1275 | ||
1276 | static void | |
1277 | spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush) | |
1278 | { | |
1279 | spin_lock(&skc->skc_lock); | |
1280 | __spl_cache_flush(skc, skm, flush); | |
1281 | spin_unlock(&skc->skc_lock); | |
1282 | } | |
1283 | ||
ea3e6ca9 BB |
1284 | static void |
1285 | spl_magazine_age(void *data) | |
f1ca4da6 | 1286 | { |
a10287e0 BB |
1287 | spl_kmem_cache_t *skc = (spl_kmem_cache_t *)data; |
1288 | spl_kmem_magazine_t *skm = skc->skc_mag[smp_processor_id()]; | |
9b1b8e4c BB |
1289 | |
1290 | ASSERT(skm->skm_magic == SKM_MAGIC); | |
a10287e0 | 1291 | ASSERT(skm->skm_cpu == smp_processor_id()); |
d4899f47 BB |
1292 | ASSERT(irqs_disabled()); |
1293 | ||
1294 | /* There are no available objects or they are too young to age out */ | |
1295 | if ((skm->skm_avail == 0) || | |
1296 | time_before(jiffies, skm->skm_age + skc->skc_delay * HZ)) | |
1297 | return; | |
f1ca4da6 | 1298 | |
d4899f47 BB |
1299 | /* |
1300 | * Because we're executing in interrupt context we may have | |
1301 | * interrupted the holder of this lock. To avoid a potential | |
1302 | * deadlock return if the lock is contended. | |
1303 | */ | |
1304 | if (!spin_trylock(&skc->skc_lock)) | |
1305 | return; | |
1306 | ||
1307 | __spl_cache_flush(skc, skm, skm->skm_refill); | |
1308 | spin_unlock(&skc->skc_lock); | |
ea3e6ca9 | 1309 | } |
4efd4118 | 1310 | |
ea3e6ca9 | 1311 | /* |
a10287e0 BB |
1312 | * Called regularly to keep a downward pressure on the cache. |
1313 | * | |
1314 | * Objects older than skc->skc_delay seconds in the per-cpu magazines will | |
1315 | * be returned to the caches. This is done to prevent idle magazines from | |
1316 | * holding memory which could be better used elsewhere. The delay is | |
1317 | * present to prevent thrashing the magazine. | |
1318 | * | |
1319 | * The newly released objects may result in empty partial slabs. Those | |
1320 | * slabs should be released to the system. Otherwise moving the objects | |
1321 | * out of the magazines is just wasted work. | |
ea3e6ca9 BB |
1322 | */ |
1323 | static void | |
1324 | spl_cache_age(void *data) | |
1325 | { | |
a10287e0 BB |
1326 | spl_kmem_cache_t *skc = (spl_kmem_cache_t *)data; |
1327 | taskqid_t id = 0; | |
ea3e6ca9 BB |
1328 | |
1329 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
a10287e0 | 1330 | |
0936c344 BB |
1331 | /* Dynamically disabled at run time */ |
1332 | if (!(spl_kmem_cache_expire & KMC_EXPIRE_AGE)) | |
1333 | return; | |
1334 | ||
a10287e0 BB |
1335 | atomic_inc(&skc->skc_ref); |
1336 | spl_on_each_cpu(spl_magazine_age, skc, 1); | |
37db7d8c | 1337 | spl_slab_reclaim(skc, skc->skc_reap, 0); |
ea3e6ca9 | 1338 | |
a10287e0 BB |
1339 | while (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags) && !id) { |
1340 | id = taskq_dispatch_delay( | |
1341 | spl_kmem_cache_taskq, spl_cache_age, skc, TQ_SLEEP, | |
1342 | ddi_get_lbolt() + skc->skc_delay / 3 * HZ); | |
1343 | ||
1344 | /* Destroy issued after dispatch immediately cancel it */ | |
1345 | if (test_bit(KMC_BIT_DESTROY, &skc->skc_flags) && id) | |
1346 | taskq_cancel_id(spl_kmem_cache_taskq, id); | |
1347 | } | |
1348 | ||
1349 | spin_lock(&skc->skc_lock); | |
1350 | skc->skc_taskqid = id; | |
1351 | spin_unlock(&skc->skc_lock); | |
1352 | ||
1353 | atomic_dec(&skc->skc_ref); | |
2fb9b26a | 1354 | } |
f1ca4da6 | 1355 | |
ea3e6ca9 | 1356 | /* |
8b45dda2 | 1357 | * Size a slab based on the size of each aligned object plus spl_kmem_obj_t. |
ea3e6ca9 BB |
1358 | * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However, |
1359 | * for very small objects we may end up with more than this so as not | |
1360 | * to waste space in the minimal allocation of a single page. Also for | |
1361 | * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN, | |
1362 | * lower than this and we will fail. | |
1363 | */ | |
48e0606a BB |
1364 | static int |
1365 | spl_slab_size(spl_kmem_cache_t *skc, uint32_t *objs, uint32_t *size) | |
1366 | { | |
8b45dda2 | 1367 | uint32_t sks_size, obj_size, max_size; |
48e0606a BB |
1368 | |
1369 | if (skc->skc_flags & KMC_OFFSLAB) { | |
ea3e6ca9 | 1370 | *objs = SPL_KMEM_CACHE_OBJ_PER_SLAB; |
48e0606a BB |
1371 | *size = sizeof(spl_kmem_slab_t); |
1372 | } else { | |
8b45dda2 BB |
1373 | sks_size = spl_sks_size(skc); |
1374 | obj_size = spl_obj_size(skc); | |
ea3e6ca9 BB |
1375 | |
1376 | if (skc->skc_flags & KMC_KMEM) | |
aa600d8a | 1377 | max_size = ((uint32_t)1 << (MAX_ORDER-3)) * PAGE_SIZE; |
ea3e6ca9 BB |
1378 | else |
1379 | max_size = (32 * 1024 * 1024); | |
48e0606a | 1380 | |
8b45dda2 BB |
1381 | /* Power of two sized slab */ |
1382 | for (*size = PAGE_SIZE; *size <= max_size; *size *= 2) { | |
ea3e6ca9 BB |
1383 | *objs = (*size - sks_size) / obj_size; |
1384 | if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB) | |
b17edc10 | 1385 | SRETURN(0); |
ea3e6ca9 | 1386 | } |
48e0606a | 1387 | |
ea3e6ca9 | 1388 | /* |
8b45dda2 | 1389 | * Unable to satisfy target objects per slab, fall back to |
ea3e6ca9 BB |
1390 | * allocating a maximally sized slab and assuming it can |
1391 | * contain the minimum objects count use it. If not fail. | |
1392 | */ | |
1393 | *size = max_size; | |
1394 | *objs = (*size - sks_size) / obj_size; | |
1395 | if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN) | |
b17edc10 | 1396 | SRETURN(0); |
48e0606a BB |
1397 | } |
1398 | ||
b17edc10 | 1399 | SRETURN(-ENOSPC); |
48e0606a BB |
1400 | } |
1401 | ||
ea3e6ca9 BB |
1402 | /* |
1403 | * Make a guess at reasonable per-cpu magazine size based on the size of | |
1404 | * each object and the cost of caching N of them in each magazine. Long | |
1405 | * term this should really adapt based on an observed usage heuristic. | |
1406 | */ | |
4afaaefa | 1407 | static int |
1408 | spl_magazine_size(spl_kmem_cache_t *skc) | |
1409 | { | |
8b45dda2 BB |
1410 | uint32_t obj_size = spl_obj_size(skc); |
1411 | int size; | |
b17edc10 | 1412 | SENTRY; |
4afaaefa | 1413 | |
ea3e6ca9 | 1414 | /* Per-magazine sizes below assume a 4Kib page size */ |
8b45dda2 | 1415 | if (obj_size > (PAGE_SIZE * 256)) |
ea3e6ca9 | 1416 | size = 4; /* Minimum 4Mib per-magazine */ |
8b45dda2 | 1417 | else if (obj_size > (PAGE_SIZE * 32)) |
ea3e6ca9 | 1418 | size = 16; /* Minimum 2Mib per-magazine */ |
8b45dda2 | 1419 | else if (obj_size > (PAGE_SIZE)) |
ea3e6ca9 | 1420 | size = 64; /* Minimum 256Kib per-magazine */ |
8b45dda2 | 1421 | else if (obj_size > (PAGE_SIZE / 4)) |
ea3e6ca9 | 1422 | size = 128; /* Minimum 128Kib per-magazine */ |
4afaaefa | 1423 | else |
ea3e6ca9 | 1424 | size = 256; |
4afaaefa | 1425 | |
b17edc10 | 1426 | SRETURN(size); |
4afaaefa | 1427 | } |
1428 | ||
ea3e6ca9 | 1429 | /* |
ecc39810 | 1430 | * Allocate a per-cpu magazine to associate with a specific core. |
ea3e6ca9 | 1431 | */ |
4afaaefa | 1432 | static spl_kmem_magazine_t * |
08850edd | 1433 | spl_magazine_alloc(spl_kmem_cache_t *skc, int cpu) |
4afaaefa | 1434 | { |
1435 | spl_kmem_magazine_t *skm; | |
1436 | int size = sizeof(spl_kmem_magazine_t) + | |
1437 | sizeof(void *) * skc->skc_mag_size; | |
b17edc10 | 1438 | SENTRY; |
4afaaefa | 1439 | |
08850edd | 1440 | skm = kmem_alloc_node(size, KM_SLEEP, cpu_to_node(cpu)); |
4afaaefa | 1441 | if (skm) { |
1442 | skm->skm_magic = SKM_MAGIC; | |
1443 | skm->skm_avail = 0; | |
1444 | skm->skm_size = skc->skc_mag_size; | |
1445 | skm->skm_refill = skc->skc_mag_refill; | |
9b1b8e4c | 1446 | skm->skm_cache = skc; |
ea3e6ca9 | 1447 | skm->skm_age = jiffies; |
08850edd | 1448 | skm->skm_cpu = cpu; |
4afaaefa | 1449 | } |
1450 | ||
b17edc10 | 1451 | SRETURN(skm); |
4afaaefa | 1452 | } |
1453 | ||
ea3e6ca9 | 1454 | /* |
ecc39810 | 1455 | * Free a per-cpu magazine associated with a specific core. |
ea3e6ca9 | 1456 | */ |
4afaaefa | 1457 | static void |
1458 | spl_magazine_free(spl_kmem_magazine_t *skm) | |
1459 | { | |
a0f6da3d | 1460 | int size = sizeof(spl_kmem_magazine_t) + |
1461 | sizeof(void *) * skm->skm_size; | |
1462 | ||
b17edc10 | 1463 | SENTRY; |
4afaaefa | 1464 | ASSERT(skm->skm_magic == SKM_MAGIC); |
1465 | ASSERT(skm->skm_avail == 0); | |
a0f6da3d | 1466 | |
1467 | kmem_free(skm, size); | |
b17edc10 | 1468 | SEXIT; |
4afaaefa | 1469 | } |
1470 | ||
ea3e6ca9 BB |
1471 | /* |
1472 | * Create all pre-cpu magazines of reasonable sizes. | |
1473 | */ | |
4afaaefa | 1474 | static int |
1475 | spl_magazine_create(spl_kmem_cache_t *skc) | |
1476 | { | |
37db7d8c | 1477 | int i; |
b17edc10 | 1478 | SENTRY; |
4afaaefa | 1479 | |
1480 | skc->skc_mag_size = spl_magazine_size(skc); | |
ea3e6ca9 | 1481 | skc->skc_mag_refill = (skc->skc_mag_size + 1) / 2; |
4afaaefa | 1482 | |
37db7d8c | 1483 | for_each_online_cpu(i) { |
08850edd | 1484 | skc->skc_mag[i] = spl_magazine_alloc(skc, i); |
37db7d8c BB |
1485 | if (!skc->skc_mag[i]) { |
1486 | for (i--; i >= 0; i--) | |
1487 | spl_magazine_free(skc->skc_mag[i]); | |
4afaaefa | 1488 | |
b17edc10 | 1489 | SRETURN(-ENOMEM); |
37db7d8c BB |
1490 | } |
1491 | } | |
4afaaefa | 1492 | |
b17edc10 | 1493 | SRETURN(0); |
4afaaefa | 1494 | } |
1495 | ||
ea3e6ca9 BB |
1496 | /* |
1497 | * Destroy all pre-cpu magazines. | |
1498 | */ | |
4afaaefa | 1499 | static void |
1500 | spl_magazine_destroy(spl_kmem_cache_t *skc) | |
1501 | { | |
37db7d8c BB |
1502 | spl_kmem_magazine_t *skm; |
1503 | int i; | |
b17edc10 | 1504 | SENTRY; |
37db7d8c BB |
1505 | |
1506 | for_each_online_cpu(i) { | |
1507 | skm = skc->skc_mag[i]; | |
d4899f47 | 1508 | spl_cache_flush(skc, skm, skm->skm_avail); |
37db7d8c BB |
1509 | spl_magazine_free(skm); |
1510 | } | |
1511 | ||
b17edc10 | 1512 | SEXIT; |
4afaaefa | 1513 | } |
1514 | ||
ea3e6ca9 BB |
1515 | /* |
1516 | * Create a object cache based on the following arguments: | |
1517 | * name cache name | |
1518 | * size cache object size | |
1519 | * align cache object alignment | |
1520 | * ctor cache object constructor | |
1521 | * dtor cache object destructor | |
1522 | * reclaim cache object reclaim | |
1523 | * priv cache private data for ctor/dtor/reclaim | |
1524 | * vmp unused must be NULL | |
1525 | * flags | |
1526 | * KMC_NOTOUCH Disable cache object aging (unsupported) | |
1527 | * KMC_NODEBUG Disable debugging (unsupported) | |
1528 | * KMC_NOMAGAZINE Disable magazine (unsupported) | |
1529 | * KMC_NOHASH Disable hashing (unsupported) | |
1530 | * KMC_QCACHE Disable qcache (unsupported) | |
1531 | * KMC_KMEM Force kmem backed cache | |
1532 | * KMC_VMEM Force vmem backed cache | |
1533 | * KMC_OFFSLAB Locate objects off the slab | |
1534 | */ | |
2fb9b26a | 1535 | spl_kmem_cache_t * |
1536 | spl_kmem_cache_create(char *name, size_t size, size_t align, | |
1537 | spl_kmem_ctor_t ctor, | |
1538 | spl_kmem_dtor_t dtor, | |
1539 | spl_kmem_reclaim_t reclaim, | |
1540 | void *priv, void *vmp, int flags) | |
1541 | { | |
1542 | spl_kmem_cache_t *skc; | |
296a8e59 | 1543 | int rc; |
b17edc10 | 1544 | SENTRY; |
937879f1 | 1545 | |
a1502d76 | 1546 | ASSERTF(!(flags & KMC_NOMAGAZINE), "Bad KMC_NOMAGAZINE (%x)\n", flags); |
1547 | ASSERTF(!(flags & KMC_NOHASH), "Bad KMC_NOHASH (%x)\n", flags); | |
1548 | ASSERTF(!(flags & KMC_QCACHE), "Bad KMC_QCACHE (%x)\n", flags); | |
48e0606a | 1549 | ASSERT(vmp == NULL); |
a1502d76 | 1550 | |
296a8e59 | 1551 | might_sleep(); |
0a6fd143 | 1552 | |
296a8e59 BB |
1553 | /* |
1554 | * Allocate memory for a new cache an initialize it. Unfortunately, | |
5198ea0e BB |
1555 | * this usually ends up being a large allocation of ~32k because |
1556 | * we need to allocate enough memory for the worst case number of | |
1557 | * cpus in the magazine, skc_mag[NR_CPUS]. Because of this we | |
296a8e59 BB |
1558 | * explicitly pass KM_NODEBUG to suppress the kmem warning |
1559 | */ | |
1560 | skc = kmem_zalloc(sizeof(*skc), KM_SLEEP| KM_NODEBUG); | |
e9d7a2be | 1561 | if (skc == NULL) |
b17edc10 | 1562 | SRETURN(NULL); |
d61e12af | 1563 | |
2fb9b26a | 1564 | skc->skc_magic = SKC_MAGIC; |
2fb9b26a | 1565 | skc->skc_name_size = strlen(name) + 1; |
296a8e59 | 1566 | skc->skc_name = (char *)kmem_alloc(skc->skc_name_size, KM_SLEEP); |
2fb9b26a | 1567 | if (skc->skc_name == NULL) { |
1568 | kmem_free(skc, sizeof(*skc)); | |
b17edc10 | 1569 | SRETURN(NULL); |
2fb9b26a | 1570 | } |
1571 | strncpy(skc->skc_name, name, skc->skc_name_size); | |
1572 | ||
e9d7a2be | 1573 | skc->skc_ctor = ctor; |
1574 | skc->skc_dtor = dtor; | |
1575 | skc->skc_reclaim = reclaim; | |
2fb9b26a | 1576 | skc->skc_private = priv; |
1577 | skc->skc_vmp = vmp; | |
1578 | skc->skc_flags = flags; | |
1579 | skc->skc_obj_size = size; | |
48e0606a | 1580 | skc->skc_obj_align = SPL_KMEM_CACHE_ALIGN; |
2fb9b26a | 1581 | skc->skc_delay = SPL_KMEM_CACHE_DELAY; |
37db7d8c | 1582 | skc->skc_reap = SPL_KMEM_CACHE_REAP; |
ea3e6ca9 | 1583 | atomic_set(&skc->skc_ref, 0); |
2fb9b26a | 1584 | |
2fb9b26a | 1585 | INIT_LIST_HEAD(&skc->skc_list); |
1586 | INIT_LIST_HEAD(&skc->skc_complete_list); | |
1587 | INIT_LIST_HEAD(&skc->skc_partial_list); | |
ed316348 | 1588 | skc->skc_emergency_tree = RB_ROOT; |
d46630e0 | 1589 | spin_lock_init(&skc->skc_lock); |
e2dcc6e2 | 1590 | init_waitqueue_head(&skc->skc_waitq); |
e9d7a2be | 1591 | skc->skc_slab_fail = 0; |
1592 | skc->skc_slab_create = 0; | |
1593 | skc->skc_slab_destroy = 0; | |
2fb9b26a | 1594 | skc->skc_slab_total = 0; |
1595 | skc->skc_slab_alloc = 0; | |
1596 | skc->skc_slab_max = 0; | |
1597 | skc->skc_obj_total = 0; | |
1598 | skc->skc_obj_alloc = 0; | |
1599 | skc->skc_obj_max = 0; | |
165f13c3 | 1600 | skc->skc_obj_deadlock = 0; |
e2dcc6e2 BB |
1601 | skc->skc_obj_emergency = 0; |
1602 | skc->skc_obj_emergency_max = 0; | |
a1502d76 | 1603 | |
48e0606a | 1604 | if (align) { |
8b45dda2 BB |
1605 | VERIFY(ISP2(align)); |
1606 | VERIFY3U(align, >=, SPL_KMEM_CACHE_ALIGN); /* Min alignment */ | |
1607 | VERIFY3U(align, <=, PAGE_SIZE); /* Max alignment */ | |
48e0606a BB |
1608 | skc->skc_obj_align = align; |
1609 | } | |
1610 | ||
a1502d76 | 1611 | /* If none passed select a cache type based on object size */ |
1612 | if (!(skc->skc_flags & (KMC_KMEM | KMC_VMEM))) { | |
8b45dda2 | 1613 | if (spl_obj_size(skc) < (PAGE_SIZE / 8)) |
a1502d76 | 1614 | skc->skc_flags |= KMC_KMEM; |
8b45dda2 | 1615 | else |
a1502d76 | 1616 | skc->skc_flags |= KMC_VMEM; |
a1502d76 | 1617 | } |
1618 | ||
48e0606a BB |
1619 | rc = spl_slab_size(skc, &skc->skc_slab_objs, &skc->skc_slab_size); |
1620 | if (rc) | |
b17edc10 | 1621 | SGOTO(out, rc); |
4afaaefa | 1622 | |
1623 | rc = spl_magazine_create(skc); | |
48e0606a | 1624 | if (rc) |
b17edc10 | 1625 | SGOTO(out, rc); |
2fb9b26a | 1626 | |
0936c344 BB |
1627 | if (spl_kmem_cache_expire & KMC_EXPIRE_AGE) |
1628 | skc->skc_taskqid = taskq_dispatch_delay(spl_kmem_cache_taskq, | |
1629 | spl_cache_age, skc, TQ_SLEEP, | |
1630 | ddi_get_lbolt() + skc->skc_delay / 3 * HZ); | |
ea3e6ca9 | 1631 | |
2fb9b26a | 1632 | down_write(&spl_kmem_cache_sem); |
e9d7a2be | 1633 | list_add_tail(&skc->skc_list, &spl_kmem_cache_list); |
2fb9b26a | 1634 | up_write(&spl_kmem_cache_sem); |
1635 | ||
b17edc10 | 1636 | SRETURN(skc); |
48e0606a BB |
1637 | out: |
1638 | kmem_free(skc->skc_name, skc->skc_name_size); | |
1639 | kmem_free(skc, sizeof(*skc)); | |
b17edc10 | 1640 | SRETURN(NULL); |
f1ca4da6 | 1641 | } |
2fb9b26a | 1642 | EXPORT_SYMBOL(spl_kmem_cache_create); |
f1ca4da6 | 1643 | |
2b354302 BB |
1644 | /* |
1645 | * Register a move callback to for cache defragmentation. | |
1646 | * XXX: Unimplemented but harmless to stub out for now. | |
1647 | */ | |
1648 | void | |
6576a1a7 | 1649 | spl_kmem_cache_set_move(spl_kmem_cache_t *skc, |
2b354302 BB |
1650 | kmem_cbrc_t (move)(void *, void *, size_t, void *)) |
1651 | { | |
1652 | ASSERT(move != NULL); | |
1653 | } | |
1654 | EXPORT_SYMBOL(spl_kmem_cache_set_move); | |
1655 | ||
ea3e6ca9 | 1656 | /* |
ecc39810 | 1657 | * Destroy a cache and all objects associated with the cache. |
ea3e6ca9 | 1658 | */ |
2fb9b26a | 1659 | void |
1660 | spl_kmem_cache_destroy(spl_kmem_cache_t *skc) | |
f1ca4da6 | 1661 | { |
ea3e6ca9 | 1662 | DECLARE_WAIT_QUEUE_HEAD(wq); |
a10287e0 | 1663 | taskqid_t id; |
b17edc10 | 1664 | SENTRY; |
f1ca4da6 | 1665 | |
e9d7a2be | 1666 | ASSERT(skc->skc_magic == SKC_MAGIC); |
1667 | ||
1668 | down_write(&spl_kmem_cache_sem); | |
1669 | list_del_init(&skc->skc_list); | |
1670 | up_write(&spl_kmem_cache_sem); | |
2fb9b26a | 1671 | |
a10287e0 | 1672 | /* Cancel any and wait for any pending delayed tasks */ |
64c075c3 | 1673 | VERIFY(!test_and_set_bit(KMC_BIT_DESTROY, &skc->skc_flags)); |
9b1b8e4c | 1674 | |
a10287e0 BB |
1675 | spin_lock(&skc->skc_lock); |
1676 | id = skc->skc_taskqid; | |
1677 | spin_unlock(&skc->skc_lock); | |
1678 | ||
1679 | taskq_cancel_id(spl_kmem_cache_taskq, id); | |
ea3e6ca9 BB |
1680 | |
1681 | /* Wait until all current callers complete, this is mainly | |
1682 | * to catch the case where a low memory situation triggers a | |
1683 | * cache reaping action which races with this destroy. */ | |
1684 | wait_event(wq, atomic_read(&skc->skc_ref) == 0); | |
1685 | ||
4afaaefa | 1686 | spl_magazine_destroy(skc); |
37db7d8c | 1687 | spl_slab_reclaim(skc, 0, 1); |
d46630e0 | 1688 | spin_lock(&skc->skc_lock); |
d6a26c6a | 1689 | |
2fb9b26a | 1690 | /* Validate there are no objects in use and free all the |
4afaaefa | 1691 | * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */ |
ea3e6ca9 BB |
1692 | ASSERT3U(skc->skc_slab_alloc, ==, 0); |
1693 | ASSERT3U(skc->skc_obj_alloc, ==, 0); | |
1694 | ASSERT3U(skc->skc_slab_total, ==, 0); | |
1695 | ASSERT3U(skc->skc_obj_total, ==, 0); | |
e2dcc6e2 | 1696 | ASSERT3U(skc->skc_obj_emergency, ==, 0); |
2fb9b26a | 1697 | ASSERT(list_empty(&skc->skc_complete_list)); |
a1502d76 | 1698 | |
2fb9b26a | 1699 | kmem_free(skc->skc_name, skc->skc_name_size); |
d46630e0 | 1700 | spin_unlock(&skc->skc_lock); |
ff449ac4 | 1701 | |
4afaaefa | 1702 | kmem_free(skc, sizeof(*skc)); |
2fb9b26a | 1703 | |
b17edc10 | 1704 | SEXIT; |
f1ca4da6 | 1705 | } |
2fb9b26a | 1706 | EXPORT_SYMBOL(spl_kmem_cache_destroy); |
f1ca4da6 | 1707 | |
ea3e6ca9 BB |
1708 | /* |
1709 | * Allocate an object from a slab attached to the cache. This is used to | |
1710 | * repopulate the per-cpu magazine caches in batches when they run low. | |
1711 | */ | |
4afaaefa | 1712 | static void * |
1713 | spl_cache_obj(spl_kmem_cache_t *skc, spl_kmem_slab_t *sks) | |
f1ca4da6 | 1714 | { |
2fb9b26a | 1715 | spl_kmem_obj_t *sko; |
f1ca4da6 | 1716 | |
e9d7a2be | 1717 | ASSERT(skc->skc_magic == SKC_MAGIC); |
1718 | ASSERT(sks->sks_magic == SKS_MAGIC); | |
4afaaefa | 1719 | ASSERT(spin_is_locked(&skc->skc_lock)); |
2fb9b26a | 1720 | |
a1502d76 | 1721 | sko = list_entry(sks->sks_free_list.next, spl_kmem_obj_t, sko_list); |
4afaaefa | 1722 | ASSERT(sko->sko_magic == SKO_MAGIC); |
1723 | ASSERT(sko->sko_addr != NULL); | |
2fb9b26a | 1724 | |
a1502d76 | 1725 | /* Remove from sks_free_list */ |
4afaaefa | 1726 | list_del_init(&sko->sko_list); |
2fb9b26a | 1727 | |
4afaaefa | 1728 | sks->sks_age = jiffies; |
1729 | sks->sks_ref++; | |
1730 | skc->skc_obj_alloc++; | |
2fb9b26a | 1731 | |
4afaaefa | 1732 | /* Track max obj usage statistics */ |
1733 | if (skc->skc_obj_alloc > skc->skc_obj_max) | |
1734 | skc->skc_obj_max = skc->skc_obj_alloc; | |
2fb9b26a | 1735 | |
4afaaefa | 1736 | /* Track max slab usage statistics */ |
1737 | if (sks->sks_ref == 1) { | |
1738 | skc->skc_slab_alloc++; | |
f1ca4da6 | 1739 | |
4afaaefa | 1740 | if (skc->skc_slab_alloc > skc->skc_slab_max) |
1741 | skc->skc_slab_max = skc->skc_slab_alloc; | |
2fb9b26a | 1742 | } |
1743 | ||
4afaaefa | 1744 | return sko->sko_addr; |
1745 | } | |
c30df9c8 | 1746 | |
ea3e6ca9 | 1747 | /* |
e2dcc6e2 BB |
1748 | * Generic slab allocation function to run by the global work queues. |
1749 | * It is responsible for allocating a new slab, linking it in to the list | |
1750 | * of partial slabs, and then waking any waiters. | |
4afaaefa | 1751 | */ |
e2dcc6e2 BB |
1752 | static void |
1753 | spl_cache_grow_work(void *data) | |
4afaaefa | 1754 | { |
33e94ef1 | 1755 | spl_kmem_alloc_t *ska = (spl_kmem_alloc_t *)data; |
e2dcc6e2 | 1756 | spl_kmem_cache_t *skc = ska->ska_cache; |
e9d7a2be | 1757 | spl_kmem_slab_t *sks; |
e2dcc6e2 BB |
1758 | |
1759 | sks = spl_slab_alloc(skc, ska->ska_flags | __GFP_NORETRY | KM_NODEBUG); | |
1760 | spin_lock(&skc->skc_lock); | |
1761 | if (sks) { | |
1762 | skc->skc_slab_total++; | |
1763 | skc->skc_obj_total += sks->sks_objs; | |
1764 | list_add_tail(&sks->sks_list, &skc->skc_partial_list); | |
1765 | } | |
1766 | ||
1767 | atomic_dec(&skc->skc_ref); | |
1768 | clear_bit(KMC_BIT_GROWING, &skc->skc_flags); | |
165f13c3 | 1769 | clear_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags); |
e2dcc6e2 BB |
1770 | wake_up_all(&skc->skc_waitq); |
1771 | spin_unlock(&skc->skc_lock); | |
1772 | ||
1773 | kfree(ska); | |
1774 | } | |
1775 | ||
1776 | /* | |
1777 | * Returns non-zero when a new slab should be available. | |
1778 | */ | |
1779 | static int | |
1780 | spl_cache_grow_wait(spl_kmem_cache_t *skc) | |
1781 | { | |
1782 | return !test_bit(KMC_BIT_GROWING, &skc->skc_flags); | |
1783 | } | |
1784 | ||
dc1b3022 BB |
1785 | static int |
1786 | spl_cache_reclaim_wait(void *word) | |
1787 | { | |
1788 | schedule(); | |
1789 | return 0; | |
1790 | } | |
1791 | ||
e2dcc6e2 BB |
1792 | /* |
1793 | * No available objects on any slabs, create a new slab. | |
1794 | */ | |
1795 | static int | |
1796 | spl_cache_grow(spl_kmem_cache_t *skc, int flags, void **obj) | |
1797 | { | |
165f13c3 | 1798 | int remaining, rc; |
b17edc10 | 1799 | SENTRY; |
f1ca4da6 | 1800 | |
e9d7a2be | 1801 | ASSERT(skc->skc_magic == SKC_MAGIC); |
ea3e6ca9 | 1802 | might_sleep(); |
e2dcc6e2 | 1803 | *obj = NULL; |
e9d7a2be | 1804 | |
ea3e6ca9 | 1805 | /* |
dc1b3022 BB |
1806 | * Before allocating a new slab wait for any reaping to complete and |
1807 | * then return so the local magazine can be rechecked for new objects. | |
ea3e6ca9 | 1808 | */ |
dc1b3022 BB |
1809 | if (test_bit(KMC_BIT_REAPING, &skc->skc_flags)) { |
1810 | rc = wait_on_bit(&skc->skc_flags, KMC_BIT_REAPING, | |
1811 | spl_cache_reclaim_wait, TASK_UNINTERRUPTIBLE); | |
1812 | SRETURN(rc ? rc : -EAGAIN); | |
1813 | } | |
2fb9b26a | 1814 | |
e2dcc6e2 BB |
1815 | /* |
1816 | * This is handled by dispatching a work request to the global work | |
1817 | * queue. This allows us to asynchronously allocate a new slab while | |
1818 | * retaining the ability to safely fall back to a smaller synchronous | |
1819 | * allocations to ensure forward progress is always maintained. | |
1820 | */ | |
1821 | if (test_and_set_bit(KMC_BIT_GROWING, &skc->skc_flags) == 0) { | |
1822 | spl_kmem_alloc_t *ska; | |
4afaaefa | 1823 | |
e2dcc6e2 BB |
1824 | ska = kmalloc(sizeof(*ska), flags); |
1825 | if (ska == NULL) { | |
1826 | clear_bit(KMC_BIT_GROWING, &skc->skc_flags); | |
1827 | wake_up_all(&skc->skc_waitq); | |
1828 | SRETURN(-ENOMEM); | |
1829 | } | |
4afaaefa | 1830 | |
e2dcc6e2 BB |
1831 | atomic_inc(&skc->skc_ref); |
1832 | ska->ska_cache = skc; | |
043f9b57 | 1833 | ska->ska_flags = flags & ~__GFP_FS; |
33e94ef1 BB |
1834 | taskq_init_ent(&ska->ska_tqe); |
1835 | taskq_dispatch_ent(spl_kmem_cache_taskq, | |
1836 | spl_cache_grow_work, ska, 0, &ska->ska_tqe); | |
e2dcc6e2 BB |
1837 | } |
1838 | ||
1839 | /* | |
165f13c3 BB |
1840 | * The goal here is to only detect the rare case where a virtual slab |
1841 | * allocation has deadlocked. We must be careful to minimize the use | |
1842 | * of emergency objects which are more expensive to track. Therefore, | |
1843 | * we set a very long timeout for the asynchronous allocation and if | |
1844 | * the timeout is reached the cache is flagged as deadlocked. From | |
1845 | * this point only new emergency objects will be allocated until the | |
1846 | * asynchronous allocation completes and clears the deadlocked flag. | |
e2dcc6e2 | 1847 | */ |
165f13c3 BB |
1848 | if (test_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags)) { |
1849 | rc = spl_emergency_alloc(skc, flags, obj); | |
1850 | } else { | |
1851 | remaining = wait_event_timeout(skc->skc_waitq, | |
1852 | spl_cache_grow_wait(skc), HZ); | |
1853 | ||
1854 | if (!remaining && test_bit(KMC_BIT_VMEM, &skc->skc_flags)) { | |
1855 | spin_lock(&skc->skc_lock); | |
1856 | if (test_bit(KMC_BIT_GROWING, &skc->skc_flags)) { | |
1857 | set_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags); | |
1858 | skc->skc_obj_deadlock++; | |
1859 | } | |
1860 | spin_unlock(&skc->skc_lock); | |
1861 | } | |
cb5c2ace | 1862 | |
165f13c3 | 1863 | rc = -ENOMEM; |
cb5c2ace | 1864 | } |
e2dcc6e2 BB |
1865 | |
1866 | SRETURN(rc); | |
f1ca4da6 | 1867 | } |
1868 | ||
ea3e6ca9 | 1869 | /* |
e2dcc6e2 BB |
1870 | * Refill a per-cpu magazine with objects from the slabs for this cache. |
1871 | * Ideally the magazine can be repopulated using existing objects which have | |
1872 | * been released, however if we are unable to locate enough free objects new | |
1873 | * slabs of objects will be created. On success NULL is returned, otherwise | |
1874 | * the address of a single emergency object is returned for use by the caller. | |
ea3e6ca9 | 1875 | */ |
e2dcc6e2 | 1876 | static void * |
4afaaefa | 1877 | spl_cache_refill(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flags) |
f1ca4da6 | 1878 | { |
e9d7a2be | 1879 | spl_kmem_slab_t *sks; |
e2dcc6e2 BB |
1880 | int count = 0, rc, refill; |
1881 | void *obj = NULL; | |
b17edc10 | 1882 | SENTRY; |
f1ca4da6 | 1883 | |
e9d7a2be | 1884 | ASSERT(skc->skc_magic == SKC_MAGIC); |
1885 | ASSERT(skm->skm_magic == SKM_MAGIC); | |
1886 | ||
e9d7a2be | 1887 | refill = MIN(skm->skm_refill, skm->skm_size - skm->skm_avail); |
d46630e0 | 1888 | spin_lock(&skc->skc_lock); |
ff449ac4 | 1889 | |
4afaaefa | 1890 | while (refill > 0) { |
ea3e6ca9 | 1891 | /* No slabs available we may need to grow the cache */ |
4afaaefa | 1892 | if (list_empty(&skc->skc_partial_list)) { |
1893 | spin_unlock(&skc->skc_lock); | |
ff449ac4 | 1894 | |
e2dcc6e2 BB |
1895 | local_irq_enable(); |
1896 | rc = spl_cache_grow(skc, flags, &obj); | |
1897 | local_irq_disable(); | |
1898 | ||
1899 | /* Emergency object for immediate use by caller */ | |
1900 | if (rc == 0 && obj != NULL) | |
1901 | SRETURN(obj); | |
1902 | ||
1903 | if (rc) | |
b17edc10 | 1904 | SGOTO(out, rc); |
4afaaefa | 1905 | |
1906 | /* Rescheduled to different CPU skm is not local */ | |
1907 | if (skm != skc->skc_mag[smp_processor_id()]) | |
b17edc10 | 1908 | SGOTO(out, rc); |
e9d7a2be | 1909 | |
1910 | /* Potentially rescheduled to the same CPU but | |
ecc39810 | 1911 | * allocations may have occurred from this CPU while |
e9d7a2be | 1912 | * we were sleeping so recalculate max refill. */ |
1913 | refill = MIN(refill, skm->skm_size - skm->skm_avail); | |
4afaaefa | 1914 | |
1915 | spin_lock(&skc->skc_lock); | |
1916 | continue; | |
1917 | } | |
d46630e0 | 1918 | |
4afaaefa | 1919 | /* Grab the next available slab */ |
1920 | sks = list_entry((&skc->skc_partial_list)->next, | |
1921 | spl_kmem_slab_t, sks_list); | |
1922 | ASSERT(sks->sks_magic == SKS_MAGIC); | |
1923 | ASSERT(sks->sks_ref < sks->sks_objs); | |
1924 | ASSERT(!list_empty(&sks->sks_free_list)); | |
d46630e0 | 1925 | |
4afaaefa | 1926 | /* Consume as many objects as needed to refill the requested |
e9d7a2be | 1927 | * cache. We must also be careful not to overfill it. */ |
e2dcc6e2 | 1928 | while (sks->sks_ref < sks->sks_objs && refill-- > 0 && ++count) { |
e9d7a2be | 1929 | ASSERT(skm->skm_avail < skm->skm_size); |
e2dcc6e2 | 1930 | ASSERT(count < skm->skm_size); |
4afaaefa | 1931 | skm->skm_objs[skm->skm_avail++]=spl_cache_obj(skc,sks); |
e9d7a2be | 1932 | } |
f1ca4da6 | 1933 | |
4afaaefa | 1934 | /* Move slab to skc_complete_list when full */ |
1935 | if (sks->sks_ref == sks->sks_objs) { | |
1936 | list_del(&sks->sks_list); | |
1937 | list_add(&sks->sks_list, &skc->skc_complete_list); | |
2fb9b26a | 1938 | } |
1939 | } | |
57d86234 | 1940 | |
4afaaefa | 1941 | spin_unlock(&skc->skc_lock); |
1942 | out: | |
e2dcc6e2 | 1943 | SRETURN(NULL); |
4afaaefa | 1944 | } |
1945 | ||
ea3e6ca9 BB |
1946 | /* |
1947 | * Release an object back to the slab from which it came. | |
1948 | */ | |
4afaaefa | 1949 | static void |
1950 | spl_cache_shrink(spl_kmem_cache_t *skc, void *obj) | |
1951 | { | |
e9d7a2be | 1952 | spl_kmem_slab_t *sks = NULL; |
4afaaefa | 1953 | spl_kmem_obj_t *sko = NULL; |
b17edc10 | 1954 | SENTRY; |
4afaaefa | 1955 | |
e9d7a2be | 1956 | ASSERT(skc->skc_magic == SKC_MAGIC); |
4afaaefa | 1957 | ASSERT(spin_is_locked(&skc->skc_lock)); |
1958 | ||
8b45dda2 | 1959 | sko = spl_sko_from_obj(skc, obj); |
a1502d76 | 1960 | ASSERT(sko->sko_magic == SKO_MAGIC); |
4afaaefa | 1961 | sks = sko->sko_slab; |
a1502d76 | 1962 | ASSERT(sks->sks_magic == SKS_MAGIC); |
2fb9b26a | 1963 | ASSERT(sks->sks_cache == skc); |
2fb9b26a | 1964 | list_add(&sko->sko_list, &sks->sks_free_list); |
d6a26c6a | 1965 | |
2fb9b26a | 1966 | sks->sks_age = jiffies; |
4afaaefa | 1967 | sks->sks_ref--; |
2fb9b26a | 1968 | skc->skc_obj_alloc--; |
f1ca4da6 | 1969 | |
2fb9b26a | 1970 | /* Move slab to skc_partial_list when no longer full. Slabs |
4afaaefa | 1971 | * are added to the head to keep the partial list is quasi-full |
1972 | * sorted order. Fuller at the head, emptier at the tail. */ | |
1973 | if (sks->sks_ref == (sks->sks_objs - 1)) { | |
2fb9b26a | 1974 | list_del(&sks->sks_list); |
1975 | list_add(&sks->sks_list, &skc->skc_partial_list); | |
1976 | } | |
f1ca4da6 | 1977 | |
ecc39810 | 1978 | /* Move empty slabs to the end of the partial list so |
4afaaefa | 1979 | * they can be easily found and freed during reclamation. */ |
1980 | if (sks->sks_ref == 0) { | |
2fb9b26a | 1981 | list_del(&sks->sks_list); |
1982 | list_add_tail(&sks->sks_list, &skc->skc_partial_list); | |
1983 | skc->skc_slab_alloc--; | |
1984 | } | |
1985 | ||
b17edc10 | 1986 | SEXIT; |
4afaaefa | 1987 | } |
1988 | ||
ea3e6ca9 BB |
1989 | /* |
1990 | * Allocate an object from the per-cpu magazine, or if the magazine | |
1991 | * is empty directly allocate from a slab and repopulate the magazine. | |
1992 | */ | |
4afaaefa | 1993 | void * |
1994 | spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags) | |
1995 | { | |
1996 | spl_kmem_magazine_t *skm; | |
1997 | unsigned long irq_flags; | |
1998 | void *obj = NULL; | |
b17edc10 | 1999 | SENTRY; |
4afaaefa | 2000 | |
e9d7a2be | 2001 | ASSERT(skc->skc_magic == SKC_MAGIC); |
ea3e6ca9 BB |
2002 | ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags)); |
2003 | ASSERT(flags & KM_SLEEP); | |
2004 | atomic_inc(&skc->skc_ref); | |
4afaaefa | 2005 | local_irq_save(irq_flags); |
2006 | ||
2007 | restart: | |
2008 | /* Safe to update per-cpu structure without lock, but | |
ecc39810 | 2009 | * in the restart case we must be careful to reacquire |
4afaaefa | 2010 | * the local magazine since this may have changed |
2011 | * when we need to grow the cache. */ | |
2012 | skm = skc->skc_mag[smp_processor_id()]; | |
e9d7a2be | 2013 | ASSERTF(skm->skm_magic == SKM_MAGIC, "%x != %x: %s/%p/%p %x/%x/%x\n", |
2014 | skm->skm_magic, SKM_MAGIC, skc->skc_name, skc, skm, | |
2015 | skm->skm_size, skm->skm_refill, skm->skm_avail); | |
4afaaefa | 2016 | |
2017 | if (likely(skm->skm_avail)) { | |
2018 | /* Object available in CPU cache, use it */ | |
2019 | obj = skm->skm_objs[--skm->skm_avail]; | |
ea3e6ca9 | 2020 | skm->skm_age = jiffies; |
4afaaefa | 2021 | } else { |
e2dcc6e2 BB |
2022 | obj = spl_cache_refill(skc, skm, flags); |
2023 | if (obj == NULL) | |
2024 | SGOTO(restart, obj = NULL); | |
4afaaefa | 2025 | } |
2026 | ||
2027 | local_irq_restore(irq_flags); | |
fece7c99 | 2028 | ASSERT(obj); |
8b45dda2 | 2029 | ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align)); |
4afaaefa | 2030 | |
2031 | /* Pre-emptively migrate object to CPU L1 cache */ | |
2032 | prefetchw(obj); | |
ea3e6ca9 | 2033 | atomic_dec(&skc->skc_ref); |
4afaaefa | 2034 | |
b17edc10 | 2035 | SRETURN(obj); |
4afaaefa | 2036 | } |
2037 | EXPORT_SYMBOL(spl_kmem_cache_alloc); | |
2038 | ||
ea3e6ca9 BB |
2039 | /* |
2040 | * Free an object back to the local per-cpu magazine, there is no | |
2041 | * guarantee that this is the same magazine the object was originally | |
2042 | * allocated from. We may need to flush entire from the magazine | |
2043 | * back to the slabs to make space. | |
2044 | */ | |
4afaaefa | 2045 | void |
2046 | spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj) | |
2047 | { | |
2048 | spl_kmem_magazine_t *skm; | |
2049 | unsigned long flags; | |
b17edc10 | 2050 | SENTRY; |
4afaaefa | 2051 | |
e9d7a2be | 2052 | ASSERT(skc->skc_magic == SKC_MAGIC); |
ea3e6ca9 BB |
2053 | ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags)); |
2054 | atomic_inc(&skc->skc_ref); | |
e2dcc6e2 BB |
2055 | |
2056 | /* | |
a1af8fb1 BB |
2057 | * Only virtual slabs may have emergency objects and these objects |
2058 | * are guaranteed to have physical addresses. They must be removed | |
2059 | * from the tree of emergency objects and the freed. | |
e2dcc6e2 | 2060 | */ |
a1af8fb1 BB |
2061 | if ((skc->skc_flags & KMC_VMEM) && !kmem_virt(obj)) |
2062 | SGOTO(out, spl_emergency_free(skc, obj)); | |
e2dcc6e2 | 2063 | |
4afaaefa | 2064 | local_irq_save(flags); |
2065 | ||
2066 | /* Safe to update per-cpu structure without lock, but | |
2067 | * no remote memory allocation tracking is being performed | |
2068 | * it is entirely possible to allocate an object from one | |
2069 | * CPU cache and return it to another. */ | |
2070 | skm = skc->skc_mag[smp_processor_id()]; | |
e9d7a2be | 2071 | ASSERT(skm->skm_magic == SKM_MAGIC); |
4afaaefa | 2072 | |
2073 | /* Per-CPU cache full, flush it to make space */ | |
2074 | if (unlikely(skm->skm_avail >= skm->skm_size)) | |
d4899f47 | 2075 | spl_cache_flush(skc, skm, skm->skm_refill); |
4afaaefa | 2076 | |
2077 | /* Available space in cache, use it */ | |
2078 | skm->skm_objs[skm->skm_avail++] = obj; | |
2079 | ||
2080 | local_irq_restore(flags); | |
e2dcc6e2 | 2081 | out: |
ea3e6ca9 | 2082 | atomic_dec(&skc->skc_ref); |
4afaaefa | 2083 | |
b17edc10 | 2084 | SEXIT; |
f1ca4da6 | 2085 | } |
2fb9b26a | 2086 | EXPORT_SYMBOL(spl_kmem_cache_free); |
5c2bb9b2 | 2087 | |
ea3e6ca9 | 2088 | /* |
ecc39810 BB |
2089 | * The generic shrinker function for all caches. Under Linux a shrinker |
2090 | * may not be tightly coupled with a slab cache. In fact Linux always | |
2091 | * systematically tries calling all registered shrinker callbacks which | |
ea3e6ca9 BB |
2092 | * report that they contain unused objects. Because of this we only |
2093 | * register one shrinker function in the shim layer for all slab caches. | |
2094 | * We always attempt to shrink all caches when this generic shrinker | |
2095 | * is called. The shrinker should return the number of free objects | |
2096 | * in the cache when called with nr_to_scan == 0 but not attempt to | |
2097 | * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan | |
cef7605c PS |
2098 | * objects should be freed, which differs from Solaris semantics. |
2099 | * Solaris semantics are to free all available objects which may (and | |
2100 | * probably will) be more objects than the requested nr_to_scan. | |
ea3e6ca9 | 2101 | */ |
a55bcaad BB |
2102 | static int |
2103 | __spl_kmem_cache_generic_shrinker(struct shrinker *shrink, | |
2104 | struct shrink_control *sc) | |
2fb9b26a | 2105 | { |
e9d7a2be | 2106 | spl_kmem_cache_t *skc; |
ea3e6ca9 | 2107 | int unused = 0; |
5c2bb9b2 | 2108 | |
e9d7a2be | 2109 | down_read(&spl_kmem_cache_sem); |
ea3e6ca9 | 2110 | list_for_each_entry(skc, &spl_kmem_cache_list, skc_list) { |
a55bcaad | 2111 | if (sc->nr_to_scan) |
cef7605c PS |
2112 | spl_kmem_cache_reap_now(skc, |
2113 | MAX(sc->nr_to_scan >> fls64(skc->skc_slab_objs), 1)); | |
ea3e6ca9 BB |
2114 | |
2115 | /* | |
2116 | * Presume everything alloc'ed in reclaimable, this ensures | |
2117 | * we are called again with nr_to_scan > 0 so can try and | |
2118 | * reclaim. The exact number is not important either so | |
2119 | * we forgo taking this already highly contented lock. | |
2120 | */ | |
2121 | unused += skc->skc_obj_alloc; | |
2122 | } | |
e9d7a2be | 2123 | up_read(&spl_kmem_cache_sem); |
2fb9b26a | 2124 | |
ea3e6ca9 | 2125 | return (unused * sysctl_vfs_cache_pressure) / 100; |
5c2bb9b2 | 2126 | } |
5c2bb9b2 | 2127 | |
a55bcaad BB |
2128 | SPL_SHRINKER_CALLBACK_WRAPPER(spl_kmem_cache_generic_shrinker); |
2129 | ||
ea3e6ca9 BB |
2130 | /* |
2131 | * Call the registered reclaim function for a cache. Depending on how | |
2132 | * many and which objects are released it may simply repopulate the | |
2133 | * local magazine which will then need to age-out. Objects which cannot | |
2134 | * fit in the magazine we will be released back to their slabs which will | |
2135 | * also need to age out before being release. This is all just best | |
2136 | * effort and we do not want to thrash creating and destroying slabs. | |
2137 | */ | |
57d86234 | 2138 | void |
cef7605c | 2139 | spl_kmem_cache_reap_now(spl_kmem_cache_t *skc, int count) |
57d86234 | 2140 | { |
b17edc10 | 2141 | SENTRY; |
e9d7a2be | 2142 | |
2143 | ASSERT(skc->skc_magic == SKC_MAGIC); | |
ea3e6ca9 | 2144 | ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags)); |
2fb9b26a | 2145 | |
ea3e6ca9 BB |
2146 | /* Prevent concurrent cache reaping when contended */ |
2147 | if (test_and_set_bit(KMC_BIT_REAPING, &skc->skc_flags)) { | |
b17edc10 | 2148 | SEXIT; |
ea3e6ca9 BB |
2149 | return; |
2150 | } | |
2fb9b26a | 2151 | |
ea3e6ca9 | 2152 | atomic_inc(&skc->skc_ref); |
4afaaefa | 2153 | |
b78d4b9d BB |
2154 | /* |
2155 | * When a reclaim function is available it may be invoked repeatedly | |
2156 | * until at least a single slab can be freed. This ensures that we | |
2157 | * do free memory back to the system. This helps minimize the chance | |
2158 | * of an OOM event when the bulk of memory is used by the slab. | |
2159 | * | |
2160 | * When free slabs are already available the reclaim callback will be | |
2161 | * skipped. Additionally, if no forward progress is detected despite | |
2162 | * a reclaim function the cache will be skipped to avoid deadlock. | |
2163 | * | |
2164 | * Longer term this would be the correct place to add the code which | |
2165 | * repacks the slabs in order minimize fragmentation. | |
2166 | */ | |
2167 | if (skc->skc_reclaim) { | |
2168 | uint64_t objects = UINT64_MAX; | |
2169 | int do_reclaim; | |
2170 | ||
2171 | do { | |
2172 | spin_lock(&skc->skc_lock); | |
2173 | do_reclaim = | |
2174 | (skc->skc_slab_total > 0) && | |
2175 | ((skc->skc_slab_total - skc->skc_slab_alloc) == 0) && | |
2176 | (skc->skc_obj_alloc < objects); | |
2177 | ||
2178 | objects = skc->skc_obj_alloc; | |
2179 | spin_unlock(&skc->skc_lock); | |
2180 | ||
2181 | if (do_reclaim) | |
2182 | skc->skc_reclaim(skc->skc_private); | |
2183 | ||
2184 | } while (do_reclaim); | |
2185 | } | |
4afaaefa | 2186 | |
0936c344 BB |
2187 | /* Reclaim from the magazine then the slabs ignoring age and delay. */ |
2188 | if (spl_kmem_cache_expire & KMC_EXPIRE_MEM) { | |
2189 | spl_kmem_magazine_t *skm; | |
2190 | int i; | |
2191 | ||
2192 | for_each_online_cpu(i) { | |
2193 | skm = skc->skc_mag[i]; | |
2194 | spl_cache_flush(skc, skm, skm->skm_avail); | |
2195 | } | |
2196 | } | |
2197 | ||
c0e0fc14 | 2198 | spl_slab_reclaim(skc, count, 1); |
ea3e6ca9 | 2199 | clear_bit(KMC_BIT_REAPING, &skc->skc_flags); |
dc1b3022 BB |
2200 | smp_mb__after_clear_bit(); |
2201 | wake_up_bit(&skc->skc_flags, KMC_BIT_REAPING); | |
2202 | ||
ea3e6ca9 | 2203 | atomic_dec(&skc->skc_ref); |
4afaaefa | 2204 | |
b17edc10 | 2205 | SEXIT; |
57d86234 | 2206 | } |
2fb9b26a | 2207 | EXPORT_SYMBOL(spl_kmem_cache_reap_now); |
57d86234 | 2208 | |
ea3e6ca9 BB |
2209 | /* |
2210 | * Reap all free slabs from all registered caches. | |
2211 | */ | |
f1b59d26 | 2212 | void |
2fb9b26a | 2213 | spl_kmem_reap(void) |
937879f1 | 2214 | { |
a55bcaad BB |
2215 | struct shrink_control sc; |
2216 | ||
2217 | sc.nr_to_scan = KMC_REAP_CHUNK; | |
2218 | sc.gfp_mask = GFP_KERNEL; | |
2219 | ||
2220 | __spl_kmem_cache_generic_shrinker(NULL, &sc); | |
f1ca4da6 | 2221 | } |
2fb9b26a | 2222 | EXPORT_SYMBOL(spl_kmem_reap); |
5d86345d | 2223 | |
ff449ac4 | 2224 | #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING) |
c6dc93d6 | 2225 | static char * |
4afaaefa | 2226 | spl_sprintf_addr(kmem_debug_t *kd, char *str, int len, int min) |
d6a26c6a | 2227 | { |
e9d7a2be | 2228 | int size = ((len - 1) < kd->kd_size) ? (len - 1) : kd->kd_size; |
d6a26c6a | 2229 | int i, flag = 1; |
2230 | ||
2231 | ASSERT(str != NULL && len >= 17); | |
e9d7a2be | 2232 | memset(str, 0, len); |
d6a26c6a | 2233 | |
2234 | /* Check for a fully printable string, and while we are at | |
2235 | * it place the printable characters in the passed buffer. */ | |
2236 | for (i = 0; i < size; i++) { | |
e9d7a2be | 2237 | str[i] = ((char *)(kd->kd_addr))[i]; |
2238 | if (isprint(str[i])) { | |
2239 | continue; | |
2240 | } else { | |
2241 | /* Minimum number of printable characters found | |
2242 | * to make it worthwhile to print this as ascii. */ | |
2243 | if (i > min) | |
2244 | break; | |
2245 | ||
2246 | flag = 0; | |
2247 | break; | |
2248 | } | |
d6a26c6a | 2249 | } |
2250 | ||
2251 | if (!flag) { | |
2252 | sprintf(str, "%02x%02x%02x%02x%02x%02x%02x%02x", | |
2253 | *((uint8_t *)kd->kd_addr), | |
2254 | *((uint8_t *)kd->kd_addr + 2), | |
2255 | *((uint8_t *)kd->kd_addr + 4), | |
2256 | *((uint8_t *)kd->kd_addr + 6), | |
2257 | *((uint8_t *)kd->kd_addr + 8), | |
2258 | *((uint8_t *)kd->kd_addr + 10), | |
2259 | *((uint8_t *)kd->kd_addr + 12), | |
2260 | *((uint8_t *)kd->kd_addr + 14)); | |
2261 | } | |
2262 | ||
2263 | return str; | |
2264 | } | |
2265 | ||
a1502d76 | 2266 | static int |
2267 | spl_kmem_init_tracking(struct list_head *list, spinlock_t *lock, int size) | |
2268 | { | |
2269 | int i; | |
b17edc10 | 2270 | SENTRY; |
a1502d76 | 2271 | |
2272 | spin_lock_init(lock); | |
2273 | INIT_LIST_HEAD(list); | |
2274 | ||
2275 | for (i = 0; i < size; i++) | |
2276 | INIT_HLIST_HEAD(&kmem_table[i]); | |
2277 | ||
b17edc10 | 2278 | SRETURN(0); |
a1502d76 | 2279 | } |
2280 | ||
ff449ac4 | 2281 | static void |
2282 | spl_kmem_fini_tracking(struct list_head *list, spinlock_t *lock) | |
5d86345d | 2283 | { |
2fb9b26a | 2284 | unsigned long flags; |
2285 | kmem_debug_t *kd; | |
2286 | char str[17]; | |
b17edc10 | 2287 | SENTRY; |
2fb9b26a | 2288 | |
ff449ac4 | 2289 | spin_lock_irqsave(lock, flags); |
2290 | if (!list_empty(list)) | |
a0f6da3d | 2291 | printk(KERN_WARNING "%-16s %-5s %-16s %s:%s\n", "address", |
2292 | "size", "data", "func", "line"); | |
2fb9b26a | 2293 | |
ff449ac4 | 2294 | list_for_each_entry(kd, list, kd_list) |
a0f6da3d | 2295 | printk(KERN_WARNING "%p %-5d %-16s %s:%d\n", kd->kd_addr, |
b6b2acc6 | 2296 | (int)kd->kd_size, spl_sprintf_addr(kd, str, 17, 8), |
2fb9b26a | 2297 | kd->kd_func, kd->kd_line); |
2298 | ||
ff449ac4 | 2299 | spin_unlock_irqrestore(lock, flags); |
b17edc10 | 2300 | SEXIT; |
ff449ac4 | 2301 | } |
2302 | #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */ | |
a1502d76 | 2303 | #define spl_kmem_init_tracking(list, lock, size) |
ff449ac4 | 2304 | #define spl_kmem_fini_tracking(list, lock) |
2305 | #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */ | |
2306 | ||
36b313da BB |
2307 | static void |
2308 | spl_kmem_init_globals(void) | |
2309 | { | |
2310 | struct zone *zone; | |
2311 | ||
2312 | /* For now all zones are includes, it may be wise to restrict | |
2313 | * this to normal and highmem zones if we see problems. */ | |
2314 | for_each_zone(zone) { | |
2315 | ||
2316 | if (!populated_zone(zone)) | |
2317 | continue; | |
2318 | ||
baf2979e BB |
2319 | minfree += min_wmark_pages(zone); |
2320 | desfree += low_wmark_pages(zone); | |
2321 | lotsfree += high_wmark_pages(zone); | |
36b313da | 2322 | } |
4ab13d3b BB |
2323 | |
2324 | /* Solaris default values */ | |
96dded38 BB |
2325 | swapfs_minfree = MAX(2*1024*1024 >> PAGE_SHIFT, physmem >> 3); |
2326 | swapfs_reserve = MIN(4*1024*1024 >> PAGE_SHIFT, physmem >> 4); | |
36b313da BB |
2327 | } |
2328 | ||
d1ff2312 BB |
2329 | /* |
2330 | * Called at module init when it is safe to use spl_kallsyms_lookup_name() | |
2331 | */ | |
2332 | int | |
2333 | spl_kmem_init_kallsyms_lookup(void) | |
2334 | { | |
2335 | #ifndef HAVE_GET_VMALLOC_INFO | |
2336 | get_vmalloc_info_fn = (get_vmalloc_info_t) | |
2337 | spl_kallsyms_lookup_name("get_vmalloc_info"); | |
e11d6c5f BB |
2338 | if (!get_vmalloc_info_fn) { |
2339 | printk(KERN_ERR "Error: Unknown symbol get_vmalloc_info\n"); | |
d1ff2312 | 2340 | return -EFAULT; |
e11d6c5f | 2341 | } |
d1ff2312 BB |
2342 | #endif /* HAVE_GET_VMALLOC_INFO */ |
2343 | ||
5232d256 BB |
2344 | #ifdef HAVE_PGDAT_HELPERS |
2345 | # ifndef HAVE_FIRST_ONLINE_PGDAT | |
d1ff2312 BB |
2346 | first_online_pgdat_fn = (first_online_pgdat_t) |
2347 | spl_kallsyms_lookup_name("first_online_pgdat"); | |
e11d6c5f BB |
2348 | if (!first_online_pgdat_fn) { |
2349 | printk(KERN_ERR "Error: Unknown symbol first_online_pgdat\n"); | |
d1ff2312 | 2350 | return -EFAULT; |
e11d6c5f | 2351 | } |
5232d256 | 2352 | # endif /* HAVE_FIRST_ONLINE_PGDAT */ |
d1ff2312 | 2353 | |
5232d256 | 2354 | # ifndef HAVE_NEXT_ONLINE_PGDAT |
d1ff2312 BB |
2355 | next_online_pgdat_fn = (next_online_pgdat_t) |
2356 | spl_kallsyms_lookup_name("next_online_pgdat"); | |
e11d6c5f BB |
2357 | if (!next_online_pgdat_fn) { |
2358 | printk(KERN_ERR "Error: Unknown symbol next_online_pgdat\n"); | |
d1ff2312 | 2359 | return -EFAULT; |
e11d6c5f | 2360 | } |
5232d256 | 2361 | # endif /* HAVE_NEXT_ONLINE_PGDAT */ |
d1ff2312 | 2362 | |
5232d256 | 2363 | # ifndef HAVE_NEXT_ZONE |
d1ff2312 BB |
2364 | next_zone_fn = (next_zone_t) |
2365 | spl_kallsyms_lookup_name("next_zone"); | |
e11d6c5f BB |
2366 | if (!next_zone_fn) { |
2367 | printk(KERN_ERR "Error: Unknown symbol next_zone\n"); | |
d1ff2312 | 2368 | return -EFAULT; |
e11d6c5f | 2369 | } |
5232d256 BB |
2370 | # endif /* HAVE_NEXT_ZONE */ |
2371 | ||
2372 | #else /* HAVE_PGDAT_HELPERS */ | |
2373 | ||
2374 | # ifndef HAVE_PGDAT_LIST | |
124ca8a5 | 2375 | pgdat_list_addr = *(struct pglist_data **) |
5232d256 BB |
2376 | spl_kallsyms_lookup_name("pgdat_list"); |
2377 | if (!pgdat_list_addr) { | |
2378 | printk(KERN_ERR "Error: Unknown symbol pgdat_list\n"); | |
2379 | return -EFAULT; | |
2380 | } | |
2381 | # endif /* HAVE_PGDAT_LIST */ | |
2382 | #endif /* HAVE_PGDAT_HELPERS */ | |
d1ff2312 | 2383 | |
6ae7fef5 | 2384 | #if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS) |
d1ff2312 BB |
2385 | get_zone_counts_fn = (get_zone_counts_t) |
2386 | spl_kallsyms_lookup_name("get_zone_counts"); | |
e11d6c5f BB |
2387 | if (!get_zone_counts_fn) { |
2388 | printk(KERN_ERR "Error: Unknown symbol get_zone_counts\n"); | |
d1ff2312 | 2389 | return -EFAULT; |
e11d6c5f | 2390 | } |
6ae7fef5 | 2391 | #endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */ |
d1ff2312 BB |
2392 | |
2393 | /* | |
2394 | * It is now safe to initialize the global tunings which rely on | |
2395 | * the use of the for_each_zone() macro. This macro in turns | |
2396 | * depends on the *_pgdat symbols which are now available. | |
2397 | */ | |
2398 | spl_kmem_init_globals(); | |
2399 | ||
e76f4bf1 | 2400 | #ifndef HAVE_SHRINK_DCACHE_MEMORY |
fe71c0e5 | 2401 | /* When shrink_dcache_memory_fn == NULL support is disabled */ |
e76f4bf1 | 2402 | shrink_dcache_memory_fn = (shrink_dcache_memory_t) |
fe71c0e5 | 2403 | spl_kallsyms_lookup_name("shrink_dcache_memory"); |
e76f4bf1 BB |
2404 | #endif /* HAVE_SHRINK_DCACHE_MEMORY */ |
2405 | ||
2406 | #ifndef HAVE_SHRINK_ICACHE_MEMORY | |
fe71c0e5 | 2407 | /* When shrink_icache_memory_fn == NULL support is disabled */ |
e76f4bf1 | 2408 | shrink_icache_memory_fn = (shrink_icache_memory_t) |
fe71c0e5 | 2409 | spl_kallsyms_lookup_name("shrink_icache_memory"); |
e76f4bf1 BB |
2410 | #endif /* HAVE_SHRINK_ICACHE_MEMORY */ |
2411 | ||
d1ff2312 BB |
2412 | return 0; |
2413 | } | |
2414 | ||
a1502d76 | 2415 | int |
2416 | spl_kmem_init(void) | |
2417 | { | |
2418 | int rc = 0; | |
b17edc10 | 2419 | SENTRY; |
a1502d76 | 2420 | |
a1502d76 | 2421 | #ifdef DEBUG_KMEM |
d04c8a56 BB |
2422 | kmem_alloc_used_set(0); |
2423 | vmem_alloc_used_set(0); | |
a1502d76 | 2424 | |
2425 | spl_kmem_init_tracking(&kmem_list, &kmem_lock, KMEM_TABLE_SIZE); | |
2426 | spl_kmem_init_tracking(&vmem_list, &vmem_lock, VMEM_TABLE_SIZE); | |
2427 | #endif | |
5c7a0369 TC |
2428 | |
2429 | init_rwsem(&spl_kmem_cache_sem); | |
2430 | INIT_LIST_HEAD(&spl_kmem_cache_list); | |
2431 | spl_kmem_cache_taskq = taskq_create("spl_kmem_cache", | |
2432 | 1, maxclsyspri, 1, 32, TASKQ_PREPOPULATE); | |
2433 | ||
2434 | spl_register_shrinker(&spl_kmem_cache_shrinker); | |
2435 | ||
b17edc10 | 2436 | SRETURN(rc); |
a1502d76 | 2437 | } |
2438 | ||
ff449ac4 | 2439 | void |
2440 | spl_kmem_fini(void) | |
2441 | { | |
ab4e74cc BB |
2442 | SENTRY; |
2443 | ||
2444 | spl_unregister_shrinker(&spl_kmem_cache_shrinker); | |
2445 | taskq_destroy(spl_kmem_cache_taskq); | |
2446 | ||
ff449ac4 | 2447 | #ifdef DEBUG_KMEM |
2448 | /* Display all unreclaimed memory addresses, including the | |
2449 | * allocation size and the first few bytes of what's located | |
2450 | * at that address to aid in debugging. Performance is not | |
2451 | * a serious concern here since it is module unload time. */ | |
d04c8a56 | 2452 | if (kmem_alloc_used_read() != 0) |
b17edc10 | 2453 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, |
3cb77549 BB |
2454 | "kmem leaked %ld/%ld bytes\n", |
2455 | kmem_alloc_used_read(), kmem_alloc_max); | |
ff449ac4 | 2456 | |
2fb9b26a | 2457 | |
d04c8a56 | 2458 | if (vmem_alloc_used_read() != 0) |
b17edc10 | 2459 | SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, |
3cb77549 BB |
2460 | "vmem leaked %ld/%ld bytes\n", |
2461 | vmem_alloc_used_read(), vmem_alloc_max); | |
2fb9b26a | 2462 | |
ff449ac4 | 2463 | spl_kmem_fini_tracking(&kmem_list, &kmem_lock); |
2464 | spl_kmem_fini_tracking(&vmem_list, &vmem_lock); | |
2465 | #endif /* DEBUG_KMEM */ | |
2fb9b26a | 2466 | |
b17edc10 | 2467 | SEXIT; |
5d86345d | 2468 | } |