]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-kmem.c
Stub out kmem cache defrag API
[mirror_spl-debian.git] / module / spl / spl-kmem.c
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>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Kmem Implementation.
25 \*****************************************************************************/
26
27 #include <sys/kmem.h>
28 #include <spl-debug.h>
29
30 #ifdef SS_DEBUG_SUBSYS
31 #undef SS_DEBUG_SUBSYS
32 #endif
33
34 #define SS_DEBUG_SUBSYS SS_KMEM
35
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
39 * multipled by the number of zones and is sized based on that.
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
47 * multipled by the number of zones and is sized based on that.
48 * Assuming all zones are being used roughly equally, when we drop
49 * below this threshold async page reclamation is triggered.
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
57 * zone->pages_high multipled by the number of zones and is sized based
58 * on that. Assuming all zones are being used roughly equally, when
59 * async page reclamation reaches this threshold it stops.
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
68 pgcnt_t swapfs_minfree = 0;
69 EXPORT_SYMBOL(swapfs_minfree);
70
71 pgcnt_t swapfs_reserve = 0;
72 EXPORT_SYMBOL(swapfs_reserve);
73
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
83 #ifndef HAVE_GET_VMALLOC_INFO
84 get_vmalloc_info_t get_vmalloc_info_fn = SYMBOL_POISON;
85 EXPORT_SYMBOL(get_vmalloc_info_fn);
86 #endif /* HAVE_GET_VMALLOC_INFO */
87
88 #ifdef HAVE_PGDAT_HELPERS
89 # ifndef HAVE_FIRST_ONLINE_PGDAT
90 first_online_pgdat_t first_online_pgdat_fn = SYMBOL_POISON;
91 EXPORT_SYMBOL(first_online_pgdat_fn);
92 # endif /* HAVE_FIRST_ONLINE_PGDAT */
93
94 # ifndef HAVE_NEXT_ONLINE_PGDAT
95 next_online_pgdat_t next_online_pgdat_fn = SYMBOL_POISON;
96 EXPORT_SYMBOL(next_online_pgdat_fn);
97 # endif /* HAVE_NEXT_ONLINE_PGDAT */
98
99 # ifndef HAVE_NEXT_ZONE
100 next_zone_t next_zone_fn = SYMBOL_POISON;
101 EXPORT_SYMBOL(next_zone_fn);
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 */
112
113 #ifdef NEED_GET_ZONE_COUNTS
114 # ifndef HAVE_GET_ZONE_COUNTS
115 get_zone_counts_t get_zone_counts_fn = SYMBOL_POISON;
116 EXPORT_SYMBOL(get_zone_counts_fn);
117 # endif /* HAVE_GET_ZONE_COUNTS */
118
119 unsigned long
120 spl_global_page_state(spl_zone_stat_item_t item)
121 {
122 unsigned long active;
123 unsigned long inactive;
124 unsigned long free;
125
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 */
132 }
133
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 */
173 }
174
175 return pages;
176 }
177 # else
178 # error "Both global_page_state() and get_zone_counts() unavailable"
179 # endif /* HAVE_GLOBAL_PAGE_STATE */
180 #endif /* NEED_GET_ZONE_COUNTS */
181 EXPORT_SYMBOL(spl_global_page_state);
182
183 pgcnt_t
184 spl_kmem_availrmem(void)
185 {
186 /* The amount of easily available memory */
187 return (spl_global_page_state(SPL_NR_FREE_PAGES) +
188 spl_global_page_state(SPL_NR_INACTIVE));
189 }
190 EXPORT_SYMBOL(spl_kmem_availrmem);
191
192 size_t
193 vmem_size(vmem_t *vmp, int typemask)
194 {
195 struct vmalloc_info vmi;
196 size_t size = 0;
197
198 ASSERT(vmp == NULL);
199 ASSERT(typemask & (VMEM_ALLOC | VMEM_FREE));
200
201 get_vmalloc_info(&vmi);
202 if (typemask & VMEM_ALLOC)
203 size += (size_t)vmi.used;
204
205 if (typemask & VMEM_FREE)
206 size += (size_t)(VMALLOC_TOTAL - vmi.used);
207
208 return size;
209 }
210 EXPORT_SYMBOL(vmem_size);
211
212 int
213 kmem_debugging(void)
214 {
215 return 0;
216 }
217 EXPORT_SYMBOL(kmem_debugging);
218
219 #ifndef HAVE_KVASPRINTF
220 /* Simplified asprintf. */
221 char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
222 {
223 unsigned int len;
224 char *p;
225 va_list aq;
226
227 va_copy(aq, ap);
228 len = vsnprintf(NULL, 0, fmt, aq);
229 va_end(aq);
230
231 p = kmalloc(len+1, gfp);
232 if (!p)
233 return NULL;
234
235 vsnprintf(p, len+1, fmt, ap);
236
237 return p;
238 }
239 EXPORT_SYMBOL(kvasprintf);
240 #endif /* HAVE_KVASPRINTF */
241
242 char *
243 kmem_vasprintf(const char *fmt, va_list ap)
244 {
245 va_list aq;
246 char *ptr;
247
248 do {
249 va_copy(aq, ap);
250 ptr = kvasprintf(GFP_KERNEL, fmt, aq);
251 va_end(aq);
252 } while (ptr == NULL);
253
254 return ptr;
255 }
256 EXPORT_SYMBOL(kmem_vasprintf);
257
258 char *
259 kmem_asprintf(const char *fmt, ...)
260 {
261 va_list ap;
262 char *ptr;
263
264 do {
265 va_start(ap, fmt);
266 ptr = kvasprintf(GFP_KERNEL, fmt, ap);
267 va_end(ap);
268 } while (ptr == NULL);
269
270 return ptr;
271 }
272 EXPORT_SYMBOL(kmem_asprintf);
273
274 static char *
275 __strdup(const char *str, int flags)
276 {
277 char *ptr;
278 int n;
279
280 n = strlen(str);
281 ptr = kmalloc_nofail(n + 1, flags);
282 if (ptr)
283 memcpy(ptr, str, n + 1);
284
285 return ptr;
286 }
287
288 char *
289 strdup(const char *str)
290 {
291 return __strdup(str, KM_SLEEP);
292 }
293 EXPORT_SYMBOL(strdup);
294
295 void
296 strfree(char *str)
297 {
298 kfree(str);
299 }
300 EXPORT_SYMBOL(strfree);
301
302 /*
303 * Memory allocation interfaces and debugging for basic kmem_*
304 * and vmem_* style memory allocation. When DEBUG_KMEM is enabled
305 * the SPL will keep track of the total memory allocated, and
306 * report any memory leaked when the module is unloaded.
307 */
308 #ifdef DEBUG_KMEM
309
310 /* Shim layer memory accounting */
311 # ifdef HAVE_ATOMIC64_T
312 atomic64_t kmem_alloc_used = ATOMIC64_INIT(0);
313 unsigned long long kmem_alloc_max = 0;
314 atomic64_t vmem_alloc_used = ATOMIC64_INIT(0);
315 unsigned long long vmem_alloc_max = 0;
316 # else /* HAVE_ATOMIC64_T */
317 atomic_t kmem_alloc_used = ATOMIC_INIT(0);
318 unsigned long long kmem_alloc_max = 0;
319 atomic_t vmem_alloc_used = ATOMIC_INIT(0);
320 unsigned long long vmem_alloc_max = 0;
321 # endif /* HAVE_ATOMIC64_T */
322
323 EXPORT_SYMBOL(kmem_alloc_used);
324 EXPORT_SYMBOL(kmem_alloc_max);
325 EXPORT_SYMBOL(vmem_alloc_used);
326 EXPORT_SYMBOL(vmem_alloc_max);
327
328 /* When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked
329 * but also the location of every alloc and free. When the SPL module is
330 * unloaded a list of all leaked addresses and where they were allocated
331 * will be dumped to the console. Enabling this feature has a significant
332 * impact on performance but it makes finding memory leaks straight forward.
333 *
334 * Not surprisingly with debugging enabled the xmem_locks are very highly
335 * contended particularly on xfree(). If we want to run with this detailed
336 * debugging enabled for anything other than debugging we need to minimize
337 * the contention by moving to a lock per xmem_table entry model.
338 */
339 # ifdef DEBUG_KMEM_TRACKING
340
341 # define KMEM_HASH_BITS 10
342 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
343
344 # define VMEM_HASH_BITS 10
345 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
346
347 typedef struct kmem_debug {
348 struct hlist_node kd_hlist; /* Hash node linkage */
349 struct list_head kd_list; /* List of all allocations */
350 void *kd_addr; /* Allocation pointer */
351 size_t kd_size; /* Allocation size */
352 const char *kd_func; /* Allocation function */
353 int kd_line; /* Allocation line */
354 } kmem_debug_t;
355
356 spinlock_t kmem_lock;
357 struct hlist_head kmem_table[KMEM_TABLE_SIZE];
358 struct list_head kmem_list;
359
360 spinlock_t vmem_lock;
361 struct hlist_head vmem_table[VMEM_TABLE_SIZE];
362 struct list_head vmem_list;
363
364 EXPORT_SYMBOL(kmem_lock);
365 EXPORT_SYMBOL(kmem_table);
366 EXPORT_SYMBOL(kmem_list);
367
368 EXPORT_SYMBOL(vmem_lock);
369 EXPORT_SYMBOL(vmem_table);
370 EXPORT_SYMBOL(vmem_list);
371
372 static kmem_debug_t *
373 kmem_del_init(spinlock_t *lock, struct hlist_head *table, int bits, void *addr)
374 {
375 struct hlist_head *head;
376 struct hlist_node *node;
377 struct kmem_debug *p;
378 unsigned long flags;
379 SENTRY;
380
381 spin_lock_irqsave(lock, flags);
382
383 head = &table[hash_ptr(addr, bits)];
384 hlist_for_each_entry_rcu(p, node, head, kd_hlist) {
385 if (p->kd_addr == addr) {
386 hlist_del_init(&p->kd_hlist);
387 list_del_init(&p->kd_list);
388 spin_unlock_irqrestore(lock, flags);
389 return p;
390 }
391 }
392
393 spin_unlock_irqrestore(lock, flags);
394
395 SRETURN(NULL);
396 }
397
398 void *
399 kmem_alloc_track(size_t size, int flags, const char *func, int line,
400 int node_alloc, int node)
401 {
402 void *ptr = NULL;
403 kmem_debug_t *dptr;
404 unsigned long irq_flags;
405 SENTRY;
406
407 /* Function may be called with KM_NOSLEEP so failure is possible */
408 dptr = (kmem_debug_t *) kmalloc_nofail(sizeof(kmem_debug_t),
409 flags & ~__GFP_ZERO);
410
411 if (unlikely(dptr == NULL)) {
412 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "debug "
413 "kmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n",
414 sizeof(kmem_debug_t), flags, func, line,
415 kmem_alloc_used_read(), kmem_alloc_max);
416 } else {
417 /*
418 * Marked unlikely because we should never be doing this,
419 * we tolerate to up 2 pages but a single page is best.
420 */
421 if (unlikely((size > PAGE_SIZE*2) && !(flags & KM_NODEBUG))) {
422 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "large "
423 "kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
424 (unsigned long long) size, flags, func, line,
425 kmem_alloc_used_read(), kmem_alloc_max);
426 spl_debug_dumpstack(NULL);
427 }
428
429 /*
430 * We use __strdup() below because the string pointed to by
431 * __FUNCTION__ might not be available by the time we want
432 * to print it since the module might have been unloaded.
433 * This can only fail in the KM_NOSLEEP case.
434 */
435 dptr->kd_func = __strdup(func, flags & ~__GFP_ZERO);
436 if (unlikely(dptr->kd_func == NULL)) {
437 kfree(dptr);
438 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
439 "debug __strdup() at %s:%d failed (%lld/%llu)\n",
440 func, line, kmem_alloc_used_read(), kmem_alloc_max);
441 goto out;
442 }
443
444 /* Use the correct allocator */
445 if (node_alloc) {
446 ASSERT(!(flags & __GFP_ZERO));
447 ptr = kmalloc_node_nofail(size, flags, node);
448 } else if (flags & __GFP_ZERO) {
449 ptr = kzalloc_nofail(size, flags & ~__GFP_ZERO);
450 } else {
451 ptr = kmalloc_nofail(size, flags);
452 }
453
454 if (unlikely(ptr == NULL)) {
455 kfree(dptr->kd_func);
456 kfree(dptr);
457 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "kmem_alloc"
458 "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
459 (unsigned long long) size, flags, func, line,
460 kmem_alloc_used_read(), kmem_alloc_max);
461 goto out;
462 }
463
464 kmem_alloc_used_add(size);
465 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max))
466 kmem_alloc_max = kmem_alloc_used_read();
467
468 INIT_HLIST_NODE(&dptr->kd_hlist);
469 INIT_LIST_HEAD(&dptr->kd_list);
470
471 dptr->kd_addr = ptr;
472 dptr->kd_size = size;
473 dptr->kd_line = line;
474
475 spin_lock_irqsave(&kmem_lock, irq_flags);
476 hlist_add_head_rcu(&dptr->kd_hlist,
477 &kmem_table[hash_ptr(ptr, KMEM_HASH_BITS)]);
478 list_add_tail(&dptr->kd_list, &kmem_list);
479 spin_unlock_irqrestore(&kmem_lock, irq_flags);
480
481 SDEBUG_LIMIT(SD_INFO,
482 "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
483 (unsigned long long) size, flags, func, line, ptr,
484 kmem_alloc_used_read(), kmem_alloc_max);
485 }
486 out:
487 SRETURN(ptr);
488 }
489 EXPORT_SYMBOL(kmem_alloc_track);
490
491 void
492 kmem_free_track(void *ptr, size_t size)
493 {
494 kmem_debug_t *dptr;
495 SENTRY;
496
497 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
498 (unsigned long long) size);
499
500 dptr = kmem_del_init(&kmem_lock, kmem_table, KMEM_HASH_BITS, ptr);
501
502 /* Must exist in hash due to kmem_alloc() */
503 ASSERT(dptr);
504
505 /* Size must match */
506 ASSERTF(dptr->kd_size == size, "kd_size (%llu) != size (%llu), "
507 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr->kd_size,
508 (unsigned long long) size, dptr->kd_func, dptr->kd_line);
509
510 kmem_alloc_used_sub(size);
511 SDEBUG_LIMIT(SD_INFO, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr,
512 (unsigned long long) size, kmem_alloc_used_read(),
513 kmem_alloc_max);
514
515 kfree(dptr->kd_func);
516
517 memset(dptr, 0x5a, sizeof(kmem_debug_t));
518 kfree(dptr);
519
520 memset(ptr, 0x5a, size);
521 kfree(ptr);
522
523 SEXIT;
524 }
525 EXPORT_SYMBOL(kmem_free_track);
526
527 void *
528 vmem_alloc_track(size_t size, int flags, const char *func, int line)
529 {
530 void *ptr = NULL;
531 kmem_debug_t *dptr;
532 unsigned long irq_flags;
533 SENTRY;
534
535 ASSERT(flags & KM_SLEEP);
536
537 /* Function may be called with KM_NOSLEEP so failure is possible */
538 dptr = (kmem_debug_t *) kmalloc_nofail(sizeof(kmem_debug_t),
539 flags & ~__GFP_ZERO);
540 if (unlikely(dptr == NULL)) {
541 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "debug "
542 "vmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n",
543 sizeof(kmem_debug_t), flags, func, line,
544 vmem_alloc_used_read(), vmem_alloc_max);
545 } else {
546 /*
547 * We use __strdup() below because the string pointed to by
548 * __FUNCTION__ might not be available by the time we want
549 * to print it, since the module might have been unloaded.
550 * This can never fail because we have already asserted
551 * that flags is KM_SLEEP.
552 */
553 dptr->kd_func = __strdup(func, flags & ~__GFP_ZERO);
554 if (unlikely(dptr->kd_func == NULL)) {
555 kfree(dptr);
556 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
557 "debug __strdup() at %s:%d failed (%lld/%llu)\n",
558 func, line, vmem_alloc_used_read(), vmem_alloc_max);
559 goto out;
560 }
561
562 /* Use the correct allocator */
563 if (flags & __GFP_ZERO) {
564 ptr = vzalloc_nofail(size, flags & ~__GFP_ZERO);
565 } else {
566 ptr = vmalloc_nofail(size, flags);
567 }
568
569 if (unlikely(ptr == NULL)) {
570 kfree(dptr->kd_func);
571 kfree(dptr);
572 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "vmem_alloc"
573 "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
574 (unsigned long long) size, flags, func, line,
575 vmem_alloc_used_read(), vmem_alloc_max);
576 goto out;
577 }
578
579 vmem_alloc_used_add(size);
580 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max))
581 vmem_alloc_max = vmem_alloc_used_read();
582
583 INIT_HLIST_NODE(&dptr->kd_hlist);
584 INIT_LIST_HEAD(&dptr->kd_list);
585
586 dptr->kd_addr = ptr;
587 dptr->kd_size = size;
588 dptr->kd_line = line;
589
590 spin_lock_irqsave(&vmem_lock, irq_flags);
591 hlist_add_head_rcu(&dptr->kd_hlist,
592 &vmem_table[hash_ptr(ptr, VMEM_HASH_BITS)]);
593 list_add_tail(&dptr->kd_list, &vmem_list);
594 spin_unlock_irqrestore(&vmem_lock, irq_flags);
595
596 SDEBUG_LIMIT(SD_INFO,
597 "vmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
598 (unsigned long long) size, flags, func, line,
599 ptr, vmem_alloc_used_read(), vmem_alloc_max);
600 }
601 out:
602 SRETURN(ptr);
603 }
604 EXPORT_SYMBOL(vmem_alloc_track);
605
606 void
607 vmem_free_track(void *ptr, size_t size)
608 {
609 kmem_debug_t *dptr;
610 SENTRY;
611
612 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
613 (unsigned long long) size);
614
615 dptr = kmem_del_init(&vmem_lock, vmem_table, VMEM_HASH_BITS, ptr);
616
617 /* Must exist in hash due to vmem_alloc() */
618 ASSERT(dptr);
619
620 /* Size must match */
621 ASSERTF(dptr->kd_size == size, "kd_size (%llu) != size (%llu), "
622 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr->kd_size,
623 (unsigned long long) size, dptr->kd_func, dptr->kd_line);
624
625 vmem_alloc_used_sub(size);
626 SDEBUG_LIMIT(SD_INFO, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr,
627 (unsigned long long) size, vmem_alloc_used_read(),
628 vmem_alloc_max);
629
630 kfree(dptr->kd_func);
631
632 memset(dptr, 0x5a, sizeof(kmem_debug_t));
633 kfree(dptr);
634
635 memset(ptr, 0x5a, size);
636 vfree(ptr);
637
638 SEXIT;
639 }
640 EXPORT_SYMBOL(vmem_free_track);
641
642 # else /* DEBUG_KMEM_TRACKING */
643
644 void *
645 kmem_alloc_debug(size_t size, int flags, const char *func, int line,
646 int node_alloc, int node)
647 {
648 void *ptr;
649 SENTRY;
650
651 /*
652 * Marked unlikely because we should never be doing this,
653 * we tolerate to up 2 pages but a single page is best.
654 */
655 if (unlikely((size > PAGE_SIZE * 2) && !(flags & KM_NODEBUG))) {
656 SDEBUG(SD_CONSOLE | SD_WARNING,
657 "large kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
658 (unsigned long long) size, flags, func, line,
659 kmem_alloc_used_read(), kmem_alloc_max);
660 spl_debug_dumpstack(NULL);
661 }
662
663 /* Use the correct allocator */
664 if (node_alloc) {
665 ASSERT(!(flags & __GFP_ZERO));
666 ptr = kmalloc_node_nofail(size, flags, node);
667 } else if (flags & __GFP_ZERO) {
668 ptr = kzalloc_nofail(size, flags & (~__GFP_ZERO));
669 } else {
670 ptr = kmalloc_nofail(size, flags);
671 }
672
673 if (unlikely(ptr == NULL)) {
674 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
675 "kmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
676 (unsigned long long) size, flags, func, line,
677 kmem_alloc_used_read(), kmem_alloc_max);
678 } else {
679 kmem_alloc_used_add(size);
680 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max))
681 kmem_alloc_max = kmem_alloc_used_read();
682
683 SDEBUG_LIMIT(SD_INFO,
684 "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
685 (unsigned long long) size, flags, func, line, ptr,
686 kmem_alloc_used_read(), kmem_alloc_max);
687 }
688
689 SRETURN(ptr);
690 }
691 EXPORT_SYMBOL(kmem_alloc_debug);
692
693 void
694 kmem_free_debug(void *ptr, size_t size)
695 {
696 SENTRY;
697
698 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
699 (unsigned long long) size);
700
701 kmem_alloc_used_sub(size);
702 SDEBUG_LIMIT(SD_INFO, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr,
703 (unsigned long long) size, kmem_alloc_used_read(),
704 kmem_alloc_max);
705 kfree(ptr);
706
707 SEXIT;
708 }
709 EXPORT_SYMBOL(kmem_free_debug);
710
711 void *
712 vmem_alloc_debug(size_t size, int flags, const char *func, int line)
713 {
714 void *ptr;
715 SENTRY;
716
717 ASSERT(flags & KM_SLEEP);
718
719 /* Use the correct allocator */
720 if (flags & __GFP_ZERO) {
721 ptr = vzalloc_nofail(size, flags & (~__GFP_ZERO));
722 } else {
723 ptr = vmalloc_nofail(size, flags);
724 }
725
726 if (unlikely(ptr == NULL)) {
727 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
728 "vmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
729 (unsigned long long) size, flags, func, line,
730 vmem_alloc_used_read(), vmem_alloc_max);
731 } else {
732 vmem_alloc_used_add(size);
733 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max))
734 vmem_alloc_max = vmem_alloc_used_read();
735
736 SDEBUG_LIMIT(SD_INFO, "vmem_alloc(%llu, 0x%x) = %p "
737 "(%lld/%llu)\n", (unsigned long long) size, flags, ptr,
738 vmem_alloc_used_read(), vmem_alloc_max);
739 }
740
741 SRETURN(ptr);
742 }
743 EXPORT_SYMBOL(vmem_alloc_debug);
744
745 void
746 vmem_free_debug(void *ptr, size_t size)
747 {
748 SENTRY;
749
750 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
751 (unsigned long long) size);
752
753 vmem_alloc_used_sub(size);
754 SDEBUG_LIMIT(SD_INFO, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr,
755 (unsigned long long) size, vmem_alloc_used_read(),
756 vmem_alloc_max);
757 vfree(ptr);
758
759 SEXIT;
760 }
761 EXPORT_SYMBOL(vmem_free_debug);
762
763 # endif /* DEBUG_KMEM_TRACKING */
764 #endif /* DEBUG_KMEM */
765
766 /*
767 * Slab allocation interfaces
768 *
769 * While the Linux slab implementation was inspired by the Solaris
770 * implemenation I cannot use it to emulate the Solaris APIs. I
771 * require two features which are not provided by the Linux slab.
772 *
773 * 1) Constructors AND destructors. Recent versions of the Linux
774 * kernel have removed support for destructors. This is a deal
775 * breaker for the SPL which contains particularly expensive
776 * initializers for mutex's, condition variables, etc. We also
777 * require a minimal level of cleanup for these data types unlike
778 * many Linux data type which do need to be explicitly destroyed.
779 *
780 * 2) Virtual address space backed slab. Callers of the Solaris slab
781 * expect it to work well for both small are very large allocations.
782 * Because of memory fragmentation the Linux slab which is backed
783 * by kmalloc'ed memory performs very badly when confronted with
784 * large numbers of large allocations. Basing the slab on the
785 * virtual address space removes the need for contigeous pages
786 * and greatly improve performance for large allocations.
787 *
788 * For these reasons, the SPL has its own slab implementation with
789 * the needed features. It is not as highly optimized as either the
790 * Solaris or Linux slabs, but it should get me most of what is
791 * needed until it can be optimized or obsoleted by another approach.
792 *
793 * One serious concern I do have about this method is the relatively
794 * small virtual address space on 32bit arches. This will seriously
795 * constrain the size of the slab caches and their performance.
796 *
797 * XXX: Improve the partial slab list by carefully maintaining a
798 * strict ordering of fullest to emptiest slabs based on
799 * the slab reference count. This gaurentees the when freeing
800 * slabs back to the system we need only linearly traverse the
801 * last N slabs in the list to discover all the freeable slabs.
802 *
803 * XXX: NUMA awareness for optionally allocating memory close to a
804 * particular core. This can be adventageous if you know the slab
805 * object will be short lived and primarily accessed from one core.
806 *
807 * XXX: Slab coloring may also yield performance improvements and would
808 * be desirable to implement.
809 */
810
811 struct list_head spl_kmem_cache_list; /* List of caches */
812 struct rw_semaphore spl_kmem_cache_sem; /* Cache list lock */
813
814 static int spl_cache_flush(spl_kmem_cache_t *skc,
815 spl_kmem_magazine_t *skm, int flush);
816
817 #ifdef HAVE_SET_SHRINKER
818 static struct shrinker *spl_kmem_cache_shrinker;
819 #else
820 static int spl_kmem_cache_generic_shrinker(int nr_to_scan,
821 unsigned int gfp_mask);
822 static struct shrinker spl_kmem_cache_shrinker = {
823 .shrink = spl_kmem_cache_generic_shrinker,
824 .seeks = KMC_DEFAULT_SEEKS,
825 };
826 #endif
827
828 static void *
829 kv_alloc(spl_kmem_cache_t *skc, int size, int flags)
830 {
831 void *ptr;
832
833 ASSERT(ISP2(size));
834
835 if (skc->skc_flags & KMC_KMEM)
836 ptr = (void *)__get_free_pages(flags, get_order(size));
837 else
838 ptr = __vmalloc(size, flags | __GFP_HIGHMEM, PAGE_KERNEL);
839
840 /* Resulting allocated memory will be page aligned */
841 ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE));
842
843 return ptr;
844 }
845
846 static void
847 kv_free(spl_kmem_cache_t *skc, void *ptr, int size)
848 {
849 ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE));
850 ASSERT(ISP2(size));
851
852 if (skc->skc_flags & KMC_KMEM)
853 free_pages((unsigned long)ptr, get_order(size));
854 else
855 vfree(ptr);
856 }
857
858 /*
859 * Required space for each aligned sks.
860 */
861 static inline uint32_t
862 spl_sks_size(spl_kmem_cache_t *skc)
863 {
864 return P2ROUNDUP_TYPED(sizeof(spl_kmem_slab_t),
865 skc->skc_obj_align, uint32_t);
866 }
867
868 /*
869 * Required space for each aligned object.
870 */
871 static inline uint32_t
872 spl_obj_size(spl_kmem_cache_t *skc)
873 {
874 uint32_t align = skc->skc_obj_align;
875
876 return P2ROUNDUP_TYPED(skc->skc_obj_size, align, uint32_t) +
877 P2ROUNDUP_TYPED(sizeof(spl_kmem_obj_t), align, uint32_t);
878 }
879
880 /*
881 * Lookup the spl_kmem_object_t for an object given that object.
882 */
883 static inline spl_kmem_obj_t *
884 spl_sko_from_obj(spl_kmem_cache_t *skc, void *obj)
885 {
886 return obj + P2ROUNDUP_TYPED(skc->skc_obj_size,
887 skc->skc_obj_align, uint32_t);
888 }
889
890 /*
891 * Required space for each offslab object taking in to account alignment
892 * restrictions and the power-of-two requirement of kv_alloc().
893 */
894 static inline uint32_t
895 spl_offslab_size(spl_kmem_cache_t *skc)
896 {
897 return 1UL << (highbit(spl_obj_size(skc)) + 1);
898 }
899
900 /*
901 * It's important that we pack the spl_kmem_obj_t structure and the
902 * actual objects in to one large address space to minimize the number
903 * of calls to the allocator. It is far better to do a few large
904 * allocations and then subdivide it ourselves. Now which allocator
905 * we use requires balancing a few trade offs.
906 *
907 * For small objects we use kmem_alloc() because as long as you are
908 * only requesting a small number of pages (ideally just one) its cheap.
909 * However, when you start requesting multiple pages with kmem_alloc()
910 * it gets increasingly expensive since it requires contigeous pages.
911 * For this reason we shift to vmem_alloc() for slabs of large objects
912 * which removes the need for contigeous pages. We do not use
913 * vmem_alloc() in all cases because there is significant locking
914 * overhead in __get_vm_area_node(). This function takes a single
915 * global lock when aquiring an available virtual address range which
916 * serializes all vmem_alloc()'s for all slab caches. Using slightly
917 * different allocation functions for small and large objects should
918 * give us the best of both worlds.
919 *
920 * KMC_ONSLAB KMC_OFFSLAB
921 *
922 * +------------------------+ +-----------------+
923 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
924 * | skc_obj_size <-+ | | +-----------------+ | |
925 * | spl_kmem_obj_t | | | |
926 * | skc_obj_size <---+ | +-----------------+ | |
927 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
928 * | ... v | | spl_kmem_obj_t | |
929 * +------------------------+ +-----------------+ v
930 */
931 static spl_kmem_slab_t *
932 spl_slab_alloc(spl_kmem_cache_t *skc, int flags)
933 {
934 spl_kmem_slab_t *sks;
935 spl_kmem_obj_t *sko, *n;
936 void *base, *obj;
937 uint32_t obj_size, offslab_size = 0;
938 int i, rc = 0;
939
940 base = kv_alloc(skc, skc->skc_slab_size, flags);
941 if (base == NULL)
942 SRETURN(NULL);
943
944 sks = (spl_kmem_slab_t *)base;
945 sks->sks_magic = SKS_MAGIC;
946 sks->sks_objs = skc->skc_slab_objs;
947 sks->sks_age = jiffies;
948 sks->sks_cache = skc;
949 INIT_LIST_HEAD(&sks->sks_list);
950 INIT_LIST_HEAD(&sks->sks_free_list);
951 sks->sks_ref = 0;
952 obj_size = spl_obj_size(skc);
953
954 if (skc->skc_flags * KMC_OFFSLAB)
955 offslab_size = spl_offslab_size(skc);
956
957 for (i = 0; i < sks->sks_objs; i++) {
958 if (skc->skc_flags & KMC_OFFSLAB) {
959 obj = kv_alloc(skc, offslab_size, flags);
960 if (!obj)
961 SGOTO(out, rc = -ENOMEM);
962 } else {
963 obj = base + spl_sks_size(skc) + (i * obj_size);
964 }
965
966 ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
967 sko = spl_sko_from_obj(skc, obj);
968 sko->sko_addr = obj;
969 sko->sko_magic = SKO_MAGIC;
970 sko->sko_slab = sks;
971 INIT_LIST_HEAD(&sko->sko_list);
972 list_add_tail(&sko->sko_list, &sks->sks_free_list);
973 }
974
975 list_for_each_entry(sko, &sks->sks_free_list, sko_list)
976 if (skc->skc_ctor)
977 skc->skc_ctor(sko->sko_addr, skc->skc_private, flags);
978 out:
979 if (rc) {
980 if (skc->skc_flags & KMC_OFFSLAB)
981 list_for_each_entry_safe(sko, n, &sks->sks_free_list,
982 sko_list)
983 kv_free(skc, sko->sko_addr, offslab_size);
984
985 kv_free(skc, base, skc->skc_slab_size);
986 sks = NULL;
987 }
988
989 SRETURN(sks);
990 }
991
992 /*
993 * Remove a slab from complete or partial list, it must be called with
994 * the 'skc->skc_lock' held but the actual free must be performed
995 * outside the lock to prevent deadlocking on vmem addresses.
996 */
997 static void
998 spl_slab_free(spl_kmem_slab_t *sks,
999 struct list_head *sks_list, struct list_head *sko_list)
1000 {
1001 spl_kmem_cache_t *skc;
1002 SENTRY;
1003
1004 ASSERT(sks->sks_magic == SKS_MAGIC);
1005 ASSERT(sks->sks_ref == 0);
1006
1007 skc = sks->sks_cache;
1008 ASSERT(skc->skc_magic == SKC_MAGIC);
1009 ASSERT(spin_is_locked(&skc->skc_lock));
1010
1011 /*
1012 * Update slab/objects counters in the cache, then remove the
1013 * slab from the skc->skc_partial_list. Finally add the slab
1014 * and all its objects in to the private work lists where the
1015 * destructors will be called and the memory freed to the system.
1016 */
1017 skc->skc_obj_total -= sks->sks_objs;
1018 skc->skc_slab_total--;
1019 list_del(&sks->sks_list);
1020 list_add(&sks->sks_list, sks_list);
1021 list_splice_init(&sks->sks_free_list, sko_list);
1022
1023 SEXIT;
1024 }
1025
1026 /*
1027 * Traverses all the partial slabs attached to a cache and free those
1028 * which which are currently empty, and have not been touched for
1029 * skc_delay seconds to avoid thrashing. The count argument is
1030 * passed to optionally cap the number of slabs reclaimed, a count
1031 * of zero means try and reclaim everything. When flag is set we
1032 * always free an available slab regardless of age.
1033 */
1034 static void
1035 spl_slab_reclaim(spl_kmem_cache_t *skc, int count, int flag)
1036 {
1037 spl_kmem_slab_t *sks, *m;
1038 spl_kmem_obj_t *sko, *n;
1039 LIST_HEAD(sks_list);
1040 LIST_HEAD(sko_list);
1041 uint32_t size = 0;
1042 int i = 0;
1043 SENTRY;
1044
1045 /*
1046 * Move empty slabs and objects which have not been touched in
1047 * skc_delay seconds on to private lists to be freed outside
1048 * the spin lock. This delay time is important to avoid thrashing
1049 * however when flag is set the delay will not be used.
1050 */
1051 spin_lock(&skc->skc_lock);
1052 list_for_each_entry_safe_reverse(sks,m,&skc->skc_partial_list,sks_list){
1053 /*
1054 * All empty slabs are at the end of skc->skc_partial_list,
1055 * therefore once a non-empty slab is found we can stop
1056 * scanning. Additionally, stop when reaching the target
1057 * reclaim 'count' if a non-zero threshhold is given.
1058 */
1059 if ((sks->sks_ref > 0) || (count && i > count))
1060 break;
1061
1062 if (time_after(jiffies,sks->sks_age+skc->skc_delay*HZ)||flag) {
1063 spl_slab_free(sks, &sks_list, &sko_list);
1064 i++;
1065 }
1066 }
1067 spin_unlock(&skc->skc_lock);
1068
1069 /*
1070 * The following two loops ensure all the object destructors are
1071 * run, any offslab objects are freed, and the slabs themselves
1072 * are freed. This is all done outside the skc->skc_lock since
1073 * this allows the destructor to sleep, and allows us to perform
1074 * a conditional reschedule when a freeing a large number of
1075 * objects and slabs back to the system.
1076 */
1077 if (skc->skc_flags & KMC_OFFSLAB)
1078 size = spl_offslab_size(skc);
1079
1080 list_for_each_entry_safe(sko, n, &sko_list, sko_list) {
1081 ASSERT(sko->sko_magic == SKO_MAGIC);
1082
1083 if (skc->skc_dtor)
1084 skc->skc_dtor(sko->sko_addr, skc->skc_private);
1085
1086 if (skc->skc_flags & KMC_OFFSLAB)
1087 kv_free(skc, sko->sko_addr, size);
1088
1089 cond_resched();
1090 }
1091
1092 list_for_each_entry_safe(sks, m, &sks_list, sks_list) {
1093 ASSERT(sks->sks_magic == SKS_MAGIC);
1094 kv_free(skc, sks, skc->skc_slab_size);
1095 cond_resched();
1096 }
1097
1098 SEXIT;
1099 }
1100
1101 /*
1102 * Called regularly on all caches to age objects out of the magazines
1103 * which have not been access in skc->skc_delay seconds. This prevents
1104 * idle magazines from holding memory which might be better used by
1105 * other caches or parts of the system. The delay is present to
1106 * prevent thrashing the magazine.
1107 */
1108 static void
1109 spl_magazine_age(void *data)
1110 {
1111 spl_kmem_magazine_t *skm =
1112 spl_get_work_data(data, spl_kmem_magazine_t, skm_work.work);
1113 spl_kmem_cache_t *skc = skm->skm_cache;
1114 int i = smp_processor_id();
1115
1116 ASSERT(skm->skm_magic == SKM_MAGIC);
1117 ASSERT(skc->skc_magic == SKC_MAGIC);
1118 ASSERT(skc->skc_mag[i] == skm);
1119
1120 if (skm->skm_avail > 0 &&
1121 time_after(jiffies, skm->skm_age + skc->skc_delay * HZ))
1122 (void)spl_cache_flush(skc, skm, skm->skm_refill);
1123
1124 if (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags))
1125 schedule_delayed_work_on(i, &skm->skm_work,
1126 skc->skc_delay / 3 * HZ);
1127 }
1128
1129 /*
1130 * Called regularly to keep a downward pressure on the size of idle
1131 * magazines and to release free slabs from the cache. This function
1132 * never calls the registered reclaim function, that only occures
1133 * under memory pressure or with a direct call to spl_kmem_reap().
1134 */
1135 static void
1136 spl_cache_age(void *data)
1137 {
1138 spl_kmem_cache_t *skc =
1139 spl_get_work_data(data, spl_kmem_cache_t, skc_work.work);
1140
1141 ASSERT(skc->skc_magic == SKC_MAGIC);
1142 spl_slab_reclaim(skc, skc->skc_reap, 0);
1143
1144 if (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags))
1145 schedule_delayed_work(&skc->skc_work, skc->skc_delay / 3 * HZ);
1146 }
1147
1148 /*
1149 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
1150 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
1151 * for very small objects we may end up with more than this so as not
1152 * to waste space in the minimal allocation of a single page. Also for
1153 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
1154 * lower than this and we will fail.
1155 */
1156 static int
1157 spl_slab_size(spl_kmem_cache_t *skc, uint32_t *objs, uint32_t *size)
1158 {
1159 uint32_t sks_size, obj_size, max_size;
1160
1161 if (skc->skc_flags & KMC_OFFSLAB) {
1162 *objs = SPL_KMEM_CACHE_OBJ_PER_SLAB;
1163 *size = sizeof(spl_kmem_slab_t);
1164 } else {
1165 sks_size = spl_sks_size(skc);
1166 obj_size = spl_obj_size(skc);
1167
1168 if (skc->skc_flags & KMC_KMEM)
1169 max_size = ((uint32_t)1 << (MAX_ORDER-3)) * PAGE_SIZE;
1170 else
1171 max_size = (32 * 1024 * 1024);
1172
1173 /* Power of two sized slab */
1174 for (*size = PAGE_SIZE; *size <= max_size; *size *= 2) {
1175 *objs = (*size - sks_size) / obj_size;
1176 if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB)
1177 SRETURN(0);
1178 }
1179
1180 /*
1181 * Unable to satisfy target objects per slab, fall back to
1182 * allocating a maximally sized slab and assuming it can
1183 * contain the minimum objects count use it. If not fail.
1184 */
1185 *size = max_size;
1186 *objs = (*size - sks_size) / obj_size;
1187 if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN)
1188 SRETURN(0);
1189 }
1190
1191 SRETURN(-ENOSPC);
1192 }
1193
1194 /*
1195 * Make a guess at reasonable per-cpu magazine size based on the size of
1196 * each object and the cost of caching N of them in each magazine. Long
1197 * term this should really adapt based on an observed usage heuristic.
1198 */
1199 static int
1200 spl_magazine_size(spl_kmem_cache_t *skc)
1201 {
1202 uint32_t obj_size = spl_obj_size(skc);
1203 int size;
1204 SENTRY;
1205
1206 /* Per-magazine sizes below assume a 4Kib page size */
1207 if (obj_size > (PAGE_SIZE * 256))
1208 size = 4; /* Minimum 4Mib per-magazine */
1209 else if (obj_size > (PAGE_SIZE * 32))
1210 size = 16; /* Minimum 2Mib per-magazine */
1211 else if (obj_size > (PAGE_SIZE))
1212 size = 64; /* Minimum 256Kib per-magazine */
1213 else if (obj_size > (PAGE_SIZE / 4))
1214 size = 128; /* Minimum 128Kib per-magazine */
1215 else
1216 size = 256;
1217
1218 SRETURN(size);
1219 }
1220
1221 /*
1222 * Allocate a per-cpu magazine to assoicate with a specific core.
1223 */
1224 static spl_kmem_magazine_t *
1225 spl_magazine_alloc(spl_kmem_cache_t *skc, int node)
1226 {
1227 spl_kmem_magazine_t *skm;
1228 int size = sizeof(spl_kmem_magazine_t) +
1229 sizeof(void *) * skc->skc_mag_size;
1230 SENTRY;
1231
1232 skm = kmem_alloc_node(size, KM_SLEEP, node);
1233 if (skm) {
1234 skm->skm_magic = SKM_MAGIC;
1235 skm->skm_avail = 0;
1236 skm->skm_size = skc->skc_mag_size;
1237 skm->skm_refill = skc->skc_mag_refill;
1238 skm->skm_cache = skc;
1239 spl_init_delayed_work(&skm->skm_work, spl_magazine_age, skm);
1240 skm->skm_age = jiffies;
1241 }
1242
1243 SRETURN(skm);
1244 }
1245
1246 /*
1247 * Free a per-cpu magazine assoicated with a specific core.
1248 */
1249 static void
1250 spl_magazine_free(spl_kmem_magazine_t *skm)
1251 {
1252 int size = sizeof(spl_kmem_magazine_t) +
1253 sizeof(void *) * skm->skm_size;
1254
1255 SENTRY;
1256 ASSERT(skm->skm_magic == SKM_MAGIC);
1257 ASSERT(skm->skm_avail == 0);
1258
1259 kmem_free(skm, size);
1260 SEXIT;
1261 }
1262
1263 /*
1264 * Create all pre-cpu magazines of reasonable sizes.
1265 */
1266 static int
1267 spl_magazine_create(spl_kmem_cache_t *skc)
1268 {
1269 int i;
1270 SENTRY;
1271
1272 skc->skc_mag_size = spl_magazine_size(skc);
1273 skc->skc_mag_refill = (skc->skc_mag_size + 1) / 2;
1274
1275 for_each_online_cpu(i) {
1276 skc->skc_mag[i] = spl_magazine_alloc(skc, cpu_to_node(i));
1277 if (!skc->skc_mag[i]) {
1278 for (i--; i >= 0; i--)
1279 spl_magazine_free(skc->skc_mag[i]);
1280
1281 SRETURN(-ENOMEM);
1282 }
1283 }
1284
1285 /* Only after everything is allocated schedule magazine work */
1286 for_each_online_cpu(i)
1287 schedule_delayed_work_on(i, &skc->skc_mag[i]->skm_work,
1288 skc->skc_delay / 3 * HZ);
1289
1290 SRETURN(0);
1291 }
1292
1293 /*
1294 * Destroy all pre-cpu magazines.
1295 */
1296 static void
1297 spl_magazine_destroy(spl_kmem_cache_t *skc)
1298 {
1299 spl_kmem_magazine_t *skm;
1300 int i;
1301 SENTRY;
1302
1303 for_each_online_cpu(i) {
1304 skm = skc->skc_mag[i];
1305 (void)spl_cache_flush(skc, skm, skm->skm_avail);
1306 spl_magazine_free(skm);
1307 }
1308
1309 SEXIT;
1310 }
1311
1312 /*
1313 * Create a object cache based on the following arguments:
1314 * name cache name
1315 * size cache object size
1316 * align cache object alignment
1317 * ctor cache object constructor
1318 * dtor cache object destructor
1319 * reclaim cache object reclaim
1320 * priv cache private data for ctor/dtor/reclaim
1321 * vmp unused must be NULL
1322 * flags
1323 * KMC_NOTOUCH Disable cache object aging (unsupported)
1324 * KMC_NODEBUG Disable debugging (unsupported)
1325 * KMC_NOMAGAZINE Disable magazine (unsupported)
1326 * KMC_NOHASH Disable hashing (unsupported)
1327 * KMC_QCACHE Disable qcache (unsupported)
1328 * KMC_KMEM Force kmem backed cache
1329 * KMC_VMEM Force vmem backed cache
1330 * KMC_OFFSLAB Locate objects off the slab
1331 */
1332 spl_kmem_cache_t *
1333 spl_kmem_cache_create(char *name, size_t size, size_t align,
1334 spl_kmem_ctor_t ctor,
1335 spl_kmem_dtor_t dtor,
1336 spl_kmem_reclaim_t reclaim,
1337 void *priv, void *vmp, int flags)
1338 {
1339 spl_kmem_cache_t *skc;
1340 int rc, kmem_flags = KM_SLEEP;
1341 SENTRY;
1342
1343 ASSERTF(!(flags & KMC_NOMAGAZINE), "Bad KMC_NOMAGAZINE (%x)\n", flags);
1344 ASSERTF(!(flags & KMC_NOHASH), "Bad KMC_NOHASH (%x)\n", flags);
1345 ASSERTF(!(flags & KMC_QCACHE), "Bad KMC_QCACHE (%x)\n", flags);
1346 ASSERT(vmp == NULL);
1347
1348 /* We may be called when there is a non-zero preempt_count or
1349 * interrupts are disabled is which case we must not sleep.
1350 */
1351 if (current_thread_info()->preempt_count || irqs_disabled())
1352 kmem_flags = KM_NOSLEEP;
1353
1354 /* Allocate memry for a new cache an initialize it. Unfortunately,
1355 * this usually ends up being a large allocation of ~32k because
1356 * we need to allocate enough memory for the worst case number of
1357 * cpus in the magazine, skc_mag[NR_CPUS]. Because of this we
1358 * explicitly pass KM_NODEBUG to suppress the kmem warning */
1359 skc = (spl_kmem_cache_t *)kmem_zalloc(sizeof(*skc),
1360 kmem_flags | KM_NODEBUG);
1361 if (skc == NULL)
1362 SRETURN(NULL);
1363
1364 skc->skc_magic = SKC_MAGIC;
1365 skc->skc_name_size = strlen(name) + 1;
1366 skc->skc_name = (char *)kmem_alloc(skc->skc_name_size, kmem_flags);
1367 if (skc->skc_name == NULL) {
1368 kmem_free(skc, sizeof(*skc));
1369 SRETURN(NULL);
1370 }
1371 strncpy(skc->skc_name, name, skc->skc_name_size);
1372
1373 skc->skc_ctor = ctor;
1374 skc->skc_dtor = dtor;
1375 skc->skc_reclaim = reclaim;
1376 skc->skc_private = priv;
1377 skc->skc_vmp = vmp;
1378 skc->skc_flags = flags;
1379 skc->skc_obj_size = size;
1380 skc->skc_obj_align = SPL_KMEM_CACHE_ALIGN;
1381 skc->skc_delay = SPL_KMEM_CACHE_DELAY;
1382 skc->skc_reap = SPL_KMEM_CACHE_REAP;
1383 atomic_set(&skc->skc_ref, 0);
1384
1385 INIT_LIST_HEAD(&skc->skc_list);
1386 INIT_LIST_HEAD(&skc->skc_complete_list);
1387 INIT_LIST_HEAD(&skc->skc_partial_list);
1388 spin_lock_init(&skc->skc_lock);
1389 skc->skc_slab_fail = 0;
1390 skc->skc_slab_create = 0;
1391 skc->skc_slab_destroy = 0;
1392 skc->skc_slab_total = 0;
1393 skc->skc_slab_alloc = 0;
1394 skc->skc_slab_max = 0;
1395 skc->skc_obj_total = 0;
1396 skc->skc_obj_alloc = 0;
1397 skc->skc_obj_max = 0;
1398
1399 if (align) {
1400 VERIFY(ISP2(align));
1401 VERIFY3U(align, >=, SPL_KMEM_CACHE_ALIGN); /* Min alignment */
1402 VERIFY3U(align, <=, PAGE_SIZE); /* Max alignment */
1403 skc->skc_obj_align = align;
1404 }
1405
1406 /* If none passed select a cache type based on object size */
1407 if (!(skc->skc_flags & (KMC_KMEM | KMC_VMEM))) {
1408 if (spl_obj_size(skc) < (PAGE_SIZE / 8))
1409 skc->skc_flags |= KMC_KMEM;
1410 else
1411 skc->skc_flags |= KMC_VMEM;
1412 }
1413
1414 rc = spl_slab_size(skc, &skc->skc_slab_objs, &skc->skc_slab_size);
1415 if (rc)
1416 SGOTO(out, rc);
1417
1418 rc = spl_magazine_create(skc);
1419 if (rc)
1420 SGOTO(out, rc);
1421
1422 spl_init_delayed_work(&skc->skc_work, spl_cache_age, skc);
1423 schedule_delayed_work(&skc->skc_work, skc->skc_delay / 3 * HZ);
1424
1425 down_write(&spl_kmem_cache_sem);
1426 list_add_tail(&skc->skc_list, &spl_kmem_cache_list);
1427 up_write(&spl_kmem_cache_sem);
1428
1429 SRETURN(skc);
1430 out:
1431 kmem_free(skc->skc_name, skc->skc_name_size);
1432 kmem_free(skc, sizeof(*skc));
1433 SRETURN(NULL);
1434 }
1435 EXPORT_SYMBOL(spl_kmem_cache_create);
1436
1437 /*
1438 * Register a move callback to for cache defragmentation.
1439 * XXX: Unimplemented but harmless to stub out for now.
1440 */
1441 void
1442 spl_kmem_cache_set_move(kmem_cache_t *skc,
1443 kmem_cbrc_t (move)(void *, void *, size_t, void *))
1444 {
1445 ASSERT(move != NULL);
1446 }
1447 EXPORT_SYMBOL(spl_kmem_cache_set_move);
1448
1449 /*
1450 * Destroy a cache and all objects assoicated with the cache.
1451 */
1452 void
1453 spl_kmem_cache_destroy(spl_kmem_cache_t *skc)
1454 {
1455 DECLARE_WAIT_QUEUE_HEAD(wq);
1456 int i;
1457 SENTRY;
1458
1459 ASSERT(skc->skc_magic == SKC_MAGIC);
1460
1461 down_write(&spl_kmem_cache_sem);
1462 list_del_init(&skc->skc_list);
1463 up_write(&spl_kmem_cache_sem);
1464
1465 /* Cancel any and wait for any pending delayed work */
1466 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1467 cancel_delayed_work(&skc->skc_work);
1468 for_each_online_cpu(i)
1469 cancel_delayed_work(&skc->skc_mag[i]->skm_work);
1470
1471 flush_scheduled_work();
1472
1473 /* Wait until all current callers complete, this is mainly
1474 * to catch the case where a low memory situation triggers a
1475 * cache reaping action which races with this destroy. */
1476 wait_event(wq, atomic_read(&skc->skc_ref) == 0);
1477
1478 spl_magazine_destroy(skc);
1479 spl_slab_reclaim(skc, 0, 1);
1480 spin_lock(&skc->skc_lock);
1481
1482 /* Validate there are no objects in use and free all the
1483 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1484 ASSERT3U(skc->skc_slab_alloc, ==, 0);
1485 ASSERT3U(skc->skc_obj_alloc, ==, 0);
1486 ASSERT3U(skc->skc_slab_total, ==, 0);
1487 ASSERT3U(skc->skc_obj_total, ==, 0);
1488 ASSERT(list_empty(&skc->skc_complete_list));
1489
1490 kmem_free(skc->skc_name, skc->skc_name_size);
1491 spin_unlock(&skc->skc_lock);
1492
1493 kmem_free(skc, sizeof(*skc));
1494
1495 SEXIT;
1496 }
1497 EXPORT_SYMBOL(spl_kmem_cache_destroy);
1498
1499 /*
1500 * Allocate an object from a slab attached to the cache. This is used to
1501 * repopulate the per-cpu magazine caches in batches when they run low.
1502 */
1503 static void *
1504 spl_cache_obj(spl_kmem_cache_t *skc, spl_kmem_slab_t *sks)
1505 {
1506 spl_kmem_obj_t *sko;
1507
1508 ASSERT(skc->skc_magic == SKC_MAGIC);
1509 ASSERT(sks->sks_magic == SKS_MAGIC);
1510 ASSERT(spin_is_locked(&skc->skc_lock));
1511
1512 sko = list_entry(sks->sks_free_list.next, spl_kmem_obj_t, sko_list);
1513 ASSERT(sko->sko_magic == SKO_MAGIC);
1514 ASSERT(sko->sko_addr != NULL);
1515
1516 /* Remove from sks_free_list */
1517 list_del_init(&sko->sko_list);
1518
1519 sks->sks_age = jiffies;
1520 sks->sks_ref++;
1521 skc->skc_obj_alloc++;
1522
1523 /* Track max obj usage statistics */
1524 if (skc->skc_obj_alloc > skc->skc_obj_max)
1525 skc->skc_obj_max = skc->skc_obj_alloc;
1526
1527 /* Track max slab usage statistics */
1528 if (sks->sks_ref == 1) {
1529 skc->skc_slab_alloc++;
1530
1531 if (skc->skc_slab_alloc > skc->skc_slab_max)
1532 skc->skc_slab_max = skc->skc_slab_alloc;
1533 }
1534
1535 return sko->sko_addr;
1536 }
1537
1538 /*
1539 * No available objects on any slabsi, create a new slab. Since this
1540 * is an expensive operation we do it without holding the spinlock and
1541 * only briefly aquire it when we link in the fully allocated and
1542 * constructed slab.
1543 */
1544 static spl_kmem_slab_t *
1545 spl_cache_grow(spl_kmem_cache_t *skc, int flags)
1546 {
1547 spl_kmem_slab_t *sks;
1548 SENTRY;
1549
1550 ASSERT(skc->skc_magic == SKC_MAGIC);
1551 local_irq_enable();
1552 might_sleep();
1553
1554 /*
1555 * Before allocating a new slab check if the slab is being reaped.
1556 * If it is there is a good chance we can wait until it finishes
1557 * and then use one of the newly freed but not aged-out slabs.
1558 */
1559 if (test_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
1560 schedule();
1561 SGOTO(out, sks= NULL);
1562 }
1563
1564 /* Allocate a new slab for the cache */
1565 sks = spl_slab_alloc(skc, flags | __GFP_NORETRY | KM_NODEBUG);
1566 if (sks == NULL)
1567 SGOTO(out, sks = NULL);
1568
1569 /* Link the new empty slab in to the end of skc_partial_list. */
1570 spin_lock(&skc->skc_lock);
1571 skc->skc_slab_total++;
1572 skc->skc_obj_total += sks->sks_objs;
1573 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
1574 spin_unlock(&skc->skc_lock);
1575 out:
1576 local_irq_disable();
1577
1578 SRETURN(sks);
1579 }
1580
1581 /*
1582 * Refill a per-cpu magazine with objects from the slabs for this
1583 * cache. Ideally the magazine can be repopulated using existing
1584 * objects which have been released, however if we are unable to
1585 * locate enough free objects new slabs of objects will be created.
1586 */
1587 static int
1588 spl_cache_refill(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flags)
1589 {
1590 spl_kmem_slab_t *sks;
1591 int rc = 0, refill;
1592 SENTRY;
1593
1594 ASSERT(skc->skc_magic == SKC_MAGIC);
1595 ASSERT(skm->skm_magic == SKM_MAGIC);
1596
1597 refill = MIN(skm->skm_refill, skm->skm_size - skm->skm_avail);
1598 spin_lock(&skc->skc_lock);
1599
1600 while (refill > 0) {
1601 /* No slabs available we may need to grow the cache */
1602 if (list_empty(&skc->skc_partial_list)) {
1603 spin_unlock(&skc->skc_lock);
1604
1605 sks = spl_cache_grow(skc, flags);
1606 if (!sks)
1607 SGOTO(out, rc);
1608
1609 /* Rescheduled to different CPU skm is not local */
1610 if (skm != skc->skc_mag[smp_processor_id()])
1611 SGOTO(out, rc);
1612
1613 /* Potentially rescheduled to the same CPU but
1614 * allocations may have occured from this CPU while
1615 * we were sleeping so recalculate max refill. */
1616 refill = MIN(refill, skm->skm_size - skm->skm_avail);
1617
1618 spin_lock(&skc->skc_lock);
1619 continue;
1620 }
1621
1622 /* Grab the next available slab */
1623 sks = list_entry((&skc->skc_partial_list)->next,
1624 spl_kmem_slab_t, sks_list);
1625 ASSERT(sks->sks_magic == SKS_MAGIC);
1626 ASSERT(sks->sks_ref < sks->sks_objs);
1627 ASSERT(!list_empty(&sks->sks_free_list));
1628
1629 /* Consume as many objects as needed to refill the requested
1630 * cache. We must also be careful not to overfill it. */
1631 while (sks->sks_ref < sks->sks_objs && refill-- > 0 && ++rc) {
1632 ASSERT(skm->skm_avail < skm->skm_size);
1633 ASSERT(rc < skm->skm_size);
1634 skm->skm_objs[skm->skm_avail++]=spl_cache_obj(skc,sks);
1635 }
1636
1637 /* Move slab to skc_complete_list when full */
1638 if (sks->sks_ref == sks->sks_objs) {
1639 list_del(&sks->sks_list);
1640 list_add(&sks->sks_list, &skc->skc_complete_list);
1641 }
1642 }
1643
1644 spin_unlock(&skc->skc_lock);
1645 out:
1646 /* Returns the number of entries added to cache */
1647 SRETURN(rc);
1648 }
1649
1650 /*
1651 * Release an object back to the slab from which it came.
1652 */
1653 static void
1654 spl_cache_shrink(spl_kmem_cache_t *skc, void *obj)
1655 {
1656 spl_kmem_slab_t *sks = NULL;
1657 spl_kmem_obj_t *sko = NULL;
1658 SENTRY;
1659
1660 ASSERT(skc->skc_magic == SKC_MAGIC);
1661 ASSERT(spin_is_locked(&skc->skc_lock));
1662
1663 sko = spl_sko_from_obj(skc, obj);
1664 ASSERT(sko->sko_magic == SKO_MAGIC);
1665 sks = sko->sko_slab;
1666 ASSERT(sks->sks_magic == SKS_MAGIC);
1667 ASSERT(sks->sks_cache == skc);
1668 list_add(&sko->sko_list, &sks->sks_free_list);
1669
1670 sks->sks_age = jiffies;
1671 sks->sks_ref--;
1672 skc->skc_obj_alloc--;
1673
1674 /* Move slab to skc_partial_list when no longer full. Slabs
1675 * are added to the head to keep the partial list is quasi-full
1676 * sorted order. Fuller at the head, emptier at the tail. */
1677 if (sks->sks_ref == (sks->sks_objs - 1)) {
1678 list_del(&sks->sks_list);
1679 list_add(&sks->sks_list, &skc->skc_partial_list);
1680 }
1681
1682 /* Move emply slabs to the end of the partial list so
1683 * they can be easily found and freed during reclamation. */
1684 if (sks->sks_ref == 0) {
1685 list_del(&sks->sks_list);
1686 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
1687 skc->skc_slab_alloc--;
1688 }
1689
1690 SEXIT;
1691 }
1692
1693 /*
1694 * Release a batch of objects from a per-cpu magazine back to their
1695 * respective slabs. This occurs when we exceed the magazine size,
1696 * are under memory pressure, when the cache is idle, or during
1697 * cache cleanup. The flush argument contains the number of entries
1698 * to remove from the magazine.
1699 */
1700 static int
1701 spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush)
1702 {
1703 int i, count = MIN(flush, skm->skm_avail);
1704 SENTRY;
1705
1706 ASSERT(skc->skc_magic == SKC_MAGIC);
1707 ASSERT(skm->skm_magic == SKM_MAGIC);
1708
1709 /*
1710 * XXX: Currently we simply return objects from the magazine to
1711 * the slabs in fifo order. The ideal thing to do from a memory
1712 * fragmentation standpoint is to cheaply determine the set of
1713 * objects in the magazine which will result in the largest
1714 * number of free slabs if released from the magazine.
1715 */
1716 spin_lock(&skc->skc_lock);
1717 for (i = 0; i < count; i++)
1718 spl_cache_shrink(skc, skm->skm_objs[i]);
1719
1720 skm->skm_avail -= count;
1721 memmove(skm->skm_objs, &(skm->skm_objs[count]),
1722 sizeof(void *) * skm->skm_avail);
1723
1724 spin_unlock(&skc->skc_lock);
1725
1726 SRETURN(count);
1727 }
1728
1729 /*
1730 * Allocate an object from the per-cpu magazine, or if the magazine
1731 * is empty directly allocate from a slab and repopulate the magazine.
1732 */
1733 void *
1734 spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags)
1735 {
1736 spl_kmem_magazine_t *skm;
1737 unsigned long irq_flags;
1738 void *obj = NULL;
1739 SENTRY;
1740
1741 ASSERT(skc->skc_magic == SKC_MAGIC);
1742 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1743 ASSERT(flags & KM_SLEEP);
1744 atomic_inc(&skc->skc_ref);
1745 local_irq_save(irq_flags);
1746
1747 restart:
1748 /* Safe to update per-cpu structure without lock, but
1749 * in the restart case we must be careful to reaquire
1750 * the local magazine since this may have changed
1751 * when we need to grow the cache. */
1752 skm = skc->skc_mag[smp_processor_id()];
1753 ASSERTF(skm->skm_magic == SKM_MAGIC, "%x != %x: %s/%p/%p %x/%x/%x\n",
1754 skm->skm_magic, SKM_MAGIC, skc->skc_name, skc, skm,
1755 skm->skm_size, skm->skm_refill, skm->skm_avail);
1756
1757 if (likely(skm->skm_avail)) {
1758 /* Object available in CPU cache, use it */
1759 obj = skm->skm_objs[--skm->skm_avail];
1760 skm->skm_age = jiffies;
1761 } else {
1762 /* Per-CPU cache empty, directly allocate from
1763 * the slab and refill the per-CPU cache. */
1764 (void)spl_cache_refill(skc, skm, flags);
1765 SGOTO(restart, obj = NULL);
1766 }
1767
1768 local_irq_restore(irq_flags);
1769 ASSERT(obj);
1770 ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
1771
1772 /* Pre-emptively migrate object to CPU L1 cache */
1773 prefetchw(obj);
1774 atomic_dec(&skc->skc_ref);
1775
1776 SRETURN(obj);
1777 }
1778 EXPORT_SYMBOL(spl_kmem_cache_alloc);
1779
1780 /*
1781 * Free an object back to the local per-cpu magazine, there is no
1782 * guarantee that this is the same magazine the object was originally
1783 * allocated from. We may need to flush entire from the magazine
1784 * back to the slabs to make space.
1785 */
1786 void
1787 spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj)
1788 {
1789 spl_kmem_magazine_t *skm;
1790 unsigned long flags;
1791 SENTRY;
1792
1793 ASSERT(skc->skc_magic == SKC_MAGIC);
1794 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1795 atomic_inc(&skc->skc_ref);
1796 local_irq_save(flags);
1797
1798 /* Safe to update per-cpu structure without lock, but
1799 * no remote memory allocation tracking is being performed
1800 * it is entirely possible to allocate an object from one
1801 * CPU cache and return it to another. */
1802 skm = skc->skc_mag[smp_processor_id()];
1803 ASSERT(skm->skm_magic == SKM_MAGIC);
1804
1805 /* Per-CPU cache full, flush it to make space */
1806 if (unlikely(skm->skm_avail >= skm->skm_size))
1807 (void)spl_cache_flush(skc, skm, skm->skm_refill);
1808
1809 /* Available space in cache, use it */
1810 skm->skm_objs[skm->skm_avail++] = obj;
1811
1812 local_irq_restore(flags);
1813 atomic_dec(&skc->skc_ref);
1814
1815 SEXIT;
1816 }
1817 EXPORT_SYMBOL(spl_kmem_cache_free);
1818
1819 /*
1820 * The generic shrinker function for all caches. Under linux a shrinker
1821 * may not be tightly coupled with a slab cache. In fact linux always
1822 * systematically trys calling all registered shrinker callbacks which
1823 * report that they contain unused objects. Because of this we only
1824 * register one shrinker function in the shim layer for all slab caches.
1825 * We always attempt to shrink all caches when this generic shrinker
1826 * is called. The shrinker should return the number of free objects
1827 * in the cache when called with nr_to_scan == 0 but not attempt to
1828 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1829 * objects should be freed, because Solaris semantics are to free
1830 * all available objects we may free more objects than requested.
1831 */
1832 static int
1833 spl_kmem_cache_generic_shrinker(int nr_to_scan, unsigned int gfp_mask)
1834 {
1835 spl_kmem_cache_t *skc;
1836 int unused = 0;
1837
1838 down_read(&spl_kmem_cache_sem);
1839 list_for_each_entry(skc, &spl_kmem_cache_list, skc_list) {
1840 if (nr_to_scan)
1841 spl_kmem_cache_reap_now(skc);
1842
1843 /*
1844 * Presume everything alloc'ed in reclaimable, this ensures
1845 * we are called again with nr_to_scan > 0 so can try and
1846 * reclaim. The exact number is not important either so
1847 * we forgo taking this already highly contented lock.
1848 */
1849 unused += skc->skc_obj_alloc;
1850 }
1851 up_read(&spl_kmem_cache_sem);
1852
1853 return (unused * sysctl_vfs_cache_pressure) / 100;
1854 }
1855
1856 /*
1857 * Call the registered reclaim function for a cache. Depending on how
1858 * many and which objects are released it may simply repopulate the
1859 * local magazine which will then need to age-out. Objects which cannot
1860 * fit in the magazine we will be released back to their slabs which will
1861 * also need to age out before being release. This is all just best
1862 * effort and we do not want to thrash creating and destroying slabs.
1863 */
1864 void
1865 spl_kmem_cache_reap_now(spl_kmem_cache_t *skc)
1866 {
1867 SENTRY;
1868
1869 ASSERT(skc->skc_magic == SKC_MAGIC);
1870 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1871
1872 /* Prevent concurrent cache reaping when contended */
1873 if (test_and_set_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
1874 SEXIT;
1875 return;
1876 }
1877
1878 atomic_inc(&skc->skc_ref);
1879
1880 if (skc->skc_reclaim)
1881 skc->skc_reclaim(skc->skc_private);
1882
1883 spl_slab_reclaim(skc, skc->skc_reap, 0);
1884 clear_bit(KMC_BIT_REAPING, &skc->skc_flags);
1885 atomic_dec(&skc->skc_ref);
1886
1887 SEXIT;
1888 }
1889 EXPORT_SYMBOL(spl_kmem_cache_reap_now);
1890
1891 /*
1892 * Reap all free slabs from all registered caches.
1893 */
1894 void
1895 spl_kmem_reap(void)
1896 {
1897 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK, GFP_KERNEL);
1898 }
1899 EXPORT_SYMBOL(spl_kmem_reap);
1900
1901 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1902 static char *
1903 spl_sprintf_addr(kmem_debug_t *kd, char *str, int len, int min)
1904 {
1905 int size = ((len - 1) < kd->kd_size) ? (len - 1) : kd->kd_size;
1906 int i, flag = 1;
1907
1908 ASSERT(str != NULL && len >= 17);
1909 memset(str, 0, len);
1910
1911 /* Check for a fully printable string, and while we are at
1912 * it place the printable characters in the passed buffer. */
1913 for (i = 0; i < size; i++) {
1914 str[i] = ((char *)(kd->kd_addr))[i];
1915 if (isprint(str[i])) {
1916 continue;
1917 } else {
1918 /* Minimum number of printable characters found
1919 * to make it worthwhile to print this as ascii. */
1920 if (i > min)
1921 break;
1922
1923 flag = 0;
1924 break;
1925 }
1926 }
1927
1928 if (!flag) {
1929 sprintf(str, "%02x%02x%02x%02x%02x%02x%02x%02x",
1930 *((uint8_t *)kd->kd_addr),
1931 *((uint8_t *)kd->kd_addr + 2),
1932 *((uint8_t *)kd->kd_addr + 4),
1933 *((uint8_t *)kd->kd_addr + 6),
1934 *((uint8_t *)kd->kd_addr + 8),
1935 *((uint8_t *)kd->kd_addr + 10),
1936 *((uint8_t *)kd->kd_addr + 12),
1937 *((uint8_t *)kd->kd_addr + 14));
1938 }
1939
1940 return str;
1941 }
1942
1943 static int
1944 spl_kmem_init_tracking(struct list_head *list, spinlock_t *lock, int size)
1945 {
1946 int i;
1947 SENTRY;
1948
1949 spin_lock_init(lock);
1950 INIT_LIST_HEAD(list);
1951
1952 for (i = 0; i < size; i++)
1953 INIT_HLIST_HEAD(&kmem_table[i]);
1954
1955 SRETURN(0);
1956 }
1957
1958 static void
1959 spl_kmem_fini_tracking(struct list_head *list, spinlock_t *lock)
1960 {
1961 unsigned long flags;
1962 kmem_debug_t *kd;
1963 char str[17];
1964 SENTRY;
1965
1966 spin_lock_irqsave(lock, flags);
1967 if (!list_empty(list))
1968 printk(KERN_WARNING "%-16s %-5s %-16s %s:%s\n", "address",
1969 "size", "data", "func", "line");
1970
1971 list_for_each_entry(kd, list, kd_list)
1972 printk(KERN_WARNING "%p %-5d %-16s %s:%d\n", kd->kd_addr,
1973 (int)kd->kd_size, spl_sprintf_addr(kd, str, 17, 8),
1974 kd->kd_func, kd->kd_line);
1975
1976 spin_unlock_irqrestore(lock, flags);
1977 SEXIT;
1978 }
1979 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1980 #define spl_kmem_init_tracking(list, lock, size)
1981 #define spl_kmem_fini_tracking(list, lock)
1982 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1983
1984 static void
1985 spl_kmem_init_globals(void)
1986 {
1987 struct zone *zone;
1988
1989 /* For now all zones are includes, it may be wise to restrict
1990 * this to normal and highmem zones if we see problems. */
1991 for_each_zone(zone) {
1992
1993 if (!populated_zone(zone))
1994 continue;
1995
1996 minfree += min_wmark_pages(zone);
1997 desfree += low_wmark_pages(zone);
1998 lotsfree += high_wmark_pages(zone);
1999 }
2000
2001 /* Solaris default values */
2002 swapfs_minfree = MAX(2*1024*1024 >> PAGE_SHIFT, physmem >> 3);
2003 swapfs_reserve = MIN(4*1024*1024 >> PAGE_SHIFT, physmem >> 4);
2004 }
2005
2006 /*
2007 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
2008 */
2009 int
2010 spl_kmem_init_kallsyms_lookup(void)
2011 {
2012 #ifndef HAVE_GET_VMALLOC_INFO
2013 get_vmalloc_info_fn = (get_vmalloc_info_t)
2014 spl_kallsyms_lookup_name("get_vmalloc_info");
2015 if (!get_vmalloc_info_fn) {
2016 printk(KERN_ERR "Error: Unknown symbol get_vmalloc_info\n");
2017 return -EFAULT;
2018 }
2019 #endif /* HAVE_GET_VMALLOC_INFO */
2020
2021 #ifdef HAVE_PGDAT_HELPERS
2022 # ifndef HAVE_FIRST_ONLINE_PGDAT
2023 first_online_pgdat_fn = (first_online_pgdat_t)
2024 spl_kallsyms_lookup_name("first_online_pgdat");
2025 if (!first_online_pgdat_fn) {
2026 printk(KERN_ERR "Error: Unknown symbol first_online_pgdat\n");
2027 return -EFAULT;
2028 }
2029 # endif /* HAVE_FIRST_ONLINE_PGDAT */
2030
2031 # ifndef HAVE_NEXT_ONLINE_PGDAT
2032 next_online_pgdat_fn = (next_online_pgdat_t)
2033 spl_kallsyms_lookup_name("next_online_pgdat");
2034 if (!next_online_pgdat_fn) {
2035 printk(KERN_ERR "Error: Unknown symbol next_online_pgdat\n");
2036 return -EFAULT;
2037 }
2038 # endif /* HAVE_NEXT_ONLINE_PGDAT */
2039
2040 # ifndef HAVE_NEXT_ZONE
2041 next_zone_fn = (next_zone_t)
2042 spl_kallsyms_lookup_name("next_zone");
2043 if (!next_zone_fn) {
2044 printk(KERN_ERR "Error: Unknown symbol next_zone\n");
2045 return -EFAULT;
2046 }
2047 # endif /* HAVE_NEXT_ZONE */
2048
2049 #else /* HAVE_PGDAT_HELPERS */
2050
2051 # ifndef HAVE_PGDAT_LIST
2052 pgdat_list_addr = *(struct pglist_data **)
2053 spl_kallsyms_lookup_name("pgdat_list");
2054 if (!pgdat_list_addr) {
2055 printk(KERN_ERR "Error: Unknown symbol pgdat_list\n");
2056 return -EFAULT;
2057 }
2058 # endif /* HAVE_PGDAT_LIST */
2059 #endif /* HAVE_PGDAT_HELPERS */
2060
2061 #if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS)
2062 get_zone_counts_fn = (get_zone_counts_t)
2063 spl_kallsyms_lookup_name("get_zone_counts");
2064 if (!get_zone_counts_fn) {
2065 printk(KERN_ERR "Error: Unknown symbol get_zone_counts\n");
2066 return -EFAULT;
2067 }
2068 #endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */
2069
2070 /*
2071 * It is now safe to initialize the global tunings which rely on
2072 * the use of the for_each_zone() macro. This macro in turns
2073 * depends on the *_pgdat symbols which are now available.
2074 */
2075 spl_kmem_init_globals();
2076
2077 return 0;
2078 }
2079
2080 int
2081 spl_kmem_init(void)
2082 {
2083 int rc = 0;
2084 SENTRY;
2085
2086 init_rwsem(&spl_kmem_cache_sem);
2087 INIT_LIST_HEAD(&spl_kmem_cache_list);
2088
2089 #ifdef HAVE_SET_SHRINKER
2090 spl_kmem_cache_shrinker = set_shrinker(KMC_DEFAULT_SEEKS,
2091 spl_kmem_cache_generic_shrinker);
2092 if (spl_kmem_cache_shrinker == NULL)
2093 SRETURN(rc = -ENOMEM);
2094 #else
2095 register_shrinker(&spl_kmem_cache_shrinker);
2096 #endif
2097
2098 #ifdef DEBUG_KMEM
2099 kmem_alloc_used_set(0);
2100 vmem_alloc_used_set(0);
2101
2102 spl_kmem_init_tracking(&kmem_list, &kmem_lock, KMEM_TABLE_SIZE);
2103 spl_kmem_init_tracking(&vmem_list, &vmem_lock, VMEM_TABLE_SIZE);
2104 #endif
2105 SRETURN(rc);
2106 }
2107
2108 void
2109 spl_kmem_fini(void)
2110 {
2111 #ifdef DEBUG_KMEM
2112 /* Display all unreclaimed memory addresses, including the
2113 * allocation size and the first few bytes of what's located
2114 * at that address to aid in debugging. Performance is not
2115 * a serious concern here since it is module unload time. */
2116 if (kmem_alloc_used_read() != 0)
2117 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
2118 "kmem leaked %ld/%ld bytes\n",
2119 kmem_alloc_used_read(), kmem_alloc_max);
2120
2121
2122 if (vmem_alloc_used_read() != 0)
2123 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
2124 "vmem leaked %ld/%ld bytes\n",
2125 vmem_alloc_used_read(), vmem_alloc_max);
2126
2127 spl_kmem_fini_tracking(&kmem_list, &kmem_lock);
2128 spl_kmem_fini_tracking(&vmem_list, &vmem_lock);
2129 #endif /* DEBUG_KMEM */
2130 SENTRY;
2131
2132 #ifdef HAVE_SET_SHRINKER
2133 remove_shrinker(spl_kmem_cache_shrinker);
2134 #else
2135 unregister_shrinker(&spl_kmem_cache_shrinker);
2136 #endif
2137
2138 SEXIT;
2139 }