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