]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-kmem.c
Fix 2.6.35 shrinker callback API change
[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 # ifdef HAVE_3ARGS_SHRINKER_CALLBACK
821 static int spl_kmem_cache_generic_shrinker(struct shrinker *shrinker_cb,
822 int nr_to_scan, unsigned int gfp_mask);
823 # else
824 static int spl_kmem_cache_generic_shrinker(
825 int nr_to_scan, unsigned int gfp_mask);
826 # endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
827 static struct shrinker spl_kmem_cache_shrinker = {
828 .shrink = spl_kmem_cache_generic_shrinker,
829 .seeks = KMC_DEFAULT_SEEKS,
830 };
831 #endif /* HAVE_SET_SHRINKER */
832
833 static void *
834 kv_alloc(spl_kmem_cache_t *skc, int size, int flags)
835 {
836 void *ptr;
837
838 ASSERT(ISP2(size));
839
840 if (skc->skc_flags & KMC_KMEM)
841 ptr = (void *)__get_free_pages(flags, get_order(size));
842 else
843 ptr = __vmalloc(size, flags | __GFP_HIGHMEM, PAGE_KERNEL);
844
845 /* Resulting allocated memory will be page aligned */
846 ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE));
847
848 return ptr;
849 }
850
851 static void
852 kv_free(spl_kmem_cache_t *skc, void *ptr, int size)
853 {
854 ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE));
855 ASSERT(ISP2(size));
856
857 if (skc->skc_flags & KMC_KMEM)
858 free_pages((unsigned long)ptr, get_order(size));
859 else
860 vfree(ptr);
861 }
862
863 /*
864 * Required space for each aligned sks.
865 */
866 static inline uint32_t
867 spl_sks_size(spl_kmem_cache_t *skc)
868 {
869 return P2ROUNDUP_TYPED(sizeof(spl_kmem_slab_t),
870 skc->skc_obj_align, uint32_t);
871 }
872
873 /*
874 * Required space for each aligned object.
875 */
876 static inline uint32_t
877 spl_obj_size(spl_kmem_cache_t *skc)
878 {
879 uint32_t align = skc->skc_obj_align;
880
881 return P2ROUNDUP_TYPED(skc->skc_obj_size, align, uint32_t) +
882 P2ROUNDUP_TYPED(sizeof(spl_kmem_obj_t), align, uint32_t);
883 }
884
885 /*
886 * Lookup the spl_kmem_object_t for an object given that object.
887 */
888 static inline spl_kmem_obj_t *
889 spl_sko_from_obj(spl_kmem_cache_t *skc, void *obj)
890 {
891 return obj + P2ROUNDUP_TYPED(skc->skc_obj_size,
892 skc->skc_obj_align, uint32_t);
893 }
894
895 /*
896 * Required space for each offslab object taking in to account alignment
897 * restrictions and the power-of-two requirement of kv_alloc().
898 */
899 static inline uint32_t
900 spl_offslab_size(spl_kmem_cache_t *skc)
901 {
902 return 1UL << (highbit(spl_obj_size(skc)) + 1);
903 }
904
905 /*
906 * It's important that we pack the spl_kmem_obj_t structure and the
907 * actual objects in to one large address space to minimize the number
908 * of calls to the allocator. It is far better to do a few large
909 * allocations and then subdivide it ourselves. Now which allocator
910 * we use requires balancing a few trade offs.
911 *
912 * For small objects we use kmem_alloc() because as long as you are
913 * only requesting a small number of pages (ideally just one) its cheap.
914 * However, when you start requesting multiple pages with kmem_alloc()
915 * it gets increasingly expensive since it requires contigeous pages.
916 * For this reason we shift to vmem_alloc() for slabs of large objects
917 * which removes the need for contigeous pages. We do not use
918 * vmem_alloc() in all cases because there is significant locking
919 * overhead in __get_vm_area_node(). This function takes a single
920 * global lock when aquiring an available virtual address range which
921 * serializes all vmem_alloc()'s for all slab caches. Using slightly
922 * different allocation functions for small and large objects should
923 * give us the best of both worlds.
924 *
925 * KMC_ONSLAB KMC_OFFSLAB
926 *
927 * +------------------------+ +-----------------+
928 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
929 * | skc_obj_size <-+ | | +-----------------+ | |
930 * | spl_kmem_obj_t | | | |
931 * | skc_obj_size <---+ | +-----------------+ | |
932 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
933 * | ... v | | spl_kmem_obj_t | |
934 * +------------------------+ +-----------------+ v
935 */
936 static spl_kmem_slab_t *
937 spl_slab_alloc(spl_kmem_cache_t *skc, int flags)
938 {
939 spl_kmem_slab_t *sks;
940 spl_kmem_obj_t *sko, *n;
941 void *base, *obj;
942 uint32_t obj_size, offslab_size = 0;
943 int i, rc = 0;
944
945 base = kv_alloc(skc, skc->skc_slab_size, flags);
946 if (base == NULL)
947 SRETURN(NULL);
948
949 sks = (spl_kmem_slab_t *)base;
950 sks->sks_magic = SKS_MAGIC;
951 sks->sks_objs = skc->skc_slab_objs;
952 sks->sks_age = jiffies;
953 sks->sks_cache = skc;
954 INIT_LIST_HEAD(&sks->sks_list);
955 INIT_LIST_HEAD(&sks->sks_free_list);
956 sks->sks_ref = 0;
957 obj_size = spl_obj_size(skc);
958
959 if (skc->skc_flags * KMC_OFFSLAB)
960 offslab_size = spl_offslab_size(skc);
961
962 for (i = 0; i < sks->sks_objs; i++) {
963 if (skc->skc_flags & KMC_OFFSLAB) {
964 obj = kv_alloc(skc, offslab_size, flags);
965 if (!obj)
966 SGOTO(out, rc = -ENOMEM);
967 } else {
968 obj = base + spl_sks_size(skc) + (i * obj_size);
969 }
970
971 ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
972 sko = spl_sko_from_obj(skc, obj);
973 sko->sko_addr = obj;
974 sko->sko_magic = SKO_MAGIC;
975 sko->sko_slab = sks;
976 INIT_LIST_HEAD(&sko->sko_list);
977 list_add_tail(&sko->sko_list, &sks->sks_free_list);
978 }
979
980 list_for_each_entry(sko, &sks->sks_free_list, sko_list)
981 if (skc->skc_ctor)
982 skc->skc_ctor(sko->sko_addr, skc->skc_private, flags);
983 out:
984 if (rc) {
985 if (skc->skc_flags & KMC_OFFSLAB)
986 list_for_each_entry_safe(sko, n, &sks->sks_free_list,
987 sko_list)
988 kv_free(skc, sko->sko_addr, offslab_size);
989
990 kv_free(skc, base, skc->skc_slab_size);
991 sks = NULL;
992 }
993
994 SRETURN(sks);
995 }
996
997 /*
998 * Remove a slab from complete or partial list, it must be called with
999 * the 'skc->skc_lock' held but the actual free must be performed
1000 * outside the lock to prevent deadlocking on vmem addresses.
1001 */
1002 static void
1003 spl_slab_free(spl_kmem_slab_t *sks,
1004 struct list_head *sks_list, struct list_head *sko_list)
1005 {
1006 spl_kmem_cache_t *skc;
1007 SENTRY;
1008
1009 ASSERT(sks->sks_magic == SKS_MAGIC);
1010 ASSERT(sks->sks_ref == 0);
1011
1012 skc = sks->sks_cache;
1013 ASSERT(skc->skc_magic == SKC_MAGIC);
1014 ASSERT(spin_is_locked(&skc->skc_lock));
1015
1016 /*
1017 * Update slab/objects counters in the cache, then remove the
1018 * slab from the skc->skc_partial_list. Finally add the slab
1019 * and all its objects in to the private work lists where the
1020 * destructors will be called and the memory freed to the system.
1021 */
1022 skc->skc_obj_total -= sks->sks_objs;
1023 skc->skc_slab_total--;
1024 list_del(&sks->sks_list);
1025 list_add(&sks->sks_list, sks_list);
1026 list_splice_init(&sks->sks_free_list, sko_list);
1027
1028 SEXIT;
1029 }
1030
1031 /*
1032 * Traverses all the partial slabs attached to a cache and free those
1033 * which which are currently empty, and have not been touched for
1034 * skc_delay seconds to avoid thrashing. The count argument is
1035 * passed to optionally cap the number of slabs reclaimed, a count
1036 * of zero means try and reclaim everything. When flag is set we
1037 * always free an available slab regardless of age.
1038 */
1039 static void
1040 spl_slab_reclaim(spl_kmem_cache_t *skc, int count, int flag)
1041 {
1042 spl_kmem_slab_t *sks, *m;
1043 spl_kmem_obj_t *sko, *n;
1044 LIST_HEAD(sks_list);
1045 LIST_HEAD(sko_list);
1046 uint32_t size = 0;
1047 int i = 0;
1048 SENTRY;
1049
1050 /*
1051 * Move empty slabs and objects which have not been touched in
1052 * skc_delay seconds on to private lists to be freed outside
1053 * the spin lock. This delay time is important to avoid thrashing
1054 * however when flag is set the delay will not be used.
1055 */
1056 spin_lock(&skc->skc_lock);
1057 list_for_each_entry_safe_reverse(sks,m,&skc->skc_partial_list,sks_list){
1058 /*
1059 * All empty slabs are at the end of skc->skc_partial_list,
1060 * therefore once a non-empty slab is found we can stop
1061 * scanning. Additionally, stop when reaching the target
1062 * reclaim 'count' if a non-zero threshhold is given.
1063 */
1064 if ((sks->sks_ref > 0) || (count && i > count))
1065 break;
1066
1067 if (time_after(jiffies,sks->sks_age+skc->skc_delay*HZ)||flag) {
1068 spl_slab_free(sks, &sks_list, &sko_list);
1069 i++;
1070 }
1071 }
1072 spin_unlock(&skc->skc_lock);
1073
1074 /*
1075 * The following two loops ensure all the object destructors are
1076 * run, any offslab objects are freed, and the slabs themselves
1077 * are freed. This is all done outside the skc->skc_lock since
1078 * this allows the destructor to sleep, and allows us to perform
1079 * a conditional reschedule when a freeing a large number of
1080 * objects and slabs back to the system.
1081 */
1082 if (skc->skc_flags & KMC_OFFSLAB)
1083 size = spl_offslab_size(skc);
1084
1085 list_for_each_entry_safe(sko, n, &sko_list, sko_list) {
1086 ASSERT(sko->sko_magic == SKO_MAGIC);
1087
1088 if (skc->skc_dtor)
1089 skc->skc_dtor(sko->sko_addr, skc->skc_private);
1090
1091 if (skc->skc_flags & KMC_OFFSLAB)
1092 kv_free(skc, sko->sko_addr, size);
1093
1094 cond_resched();
1095 }
1096
1097 list_for_each_entry_safe(sks, m, &sks_list, sks_list) {
1098 ASSERT(sks->sks_magic == SKS_MAGIC);
1099 kv_free(skc, sks, skc->skc_slab_size);
1100 cond_resched();
1101 }
1102
1103 SEXIT;
1104 }
1105
1106 /*
1107 * Called regularly on all caches to age objects out of the magazines
1108 * which have not been access in skc->skc_delay seconds. This prevents
1109 * idle magazines from holding memory which might be better used by
1110 * other caches or parts of the system. The delay is present to
1111 * prevent thrashing the magazine.
1112 */
1113 static void
1114 spl_magazine_age(void *data)
1115 {
1116 spl_kmem_magazine_t *skm =
1117 spl_get_work_data(data, spl_kmem_magazine_t, skm_work.work);
1118 spl_kmem_cache_t *skc = skm->skm_cache;
1119 int i = smp_processor_id();
1120
1121 ASSERT(skm->skm_magic == SKM_MAGIC);
1122 ASSERT(skc->skc_magic == SKC_MAGIC);
1123 ASSERT(skc->skc_mag[i] == skm);
1124
1125 if (skm->skm_avail > 0 &&
1126 time_after(jiffies, skm->skm_age + skc->skc_delay * HZ))
1127 (void)spl_cache_flush(skc, skm, skm->skm_refill);
1128
1129 if (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags))
1130 schedule_delayed_work_on(i, &skm->skm_work,
1131 skc->skc_delay / 3 * HZ);
1132 }
1133
1134 /*
1135 * Called regularly to keep a downward pressure on the size of idle
1136 * magazines and to release free slabs from the cache. This function
1137 * never calls the registered reclaim function, that only occures
1138 * under memory pressure or with a direct call to spl_kmem_reap().
1139 */
1140 static void
1141 spl_cache_age(void *data)
1142 {
1143 spl_kmem_cache_t *skc =
1144 spl_get_work_data(data, spl_kmem_cache_t, skc_work.work);
1145
1146 ASSERT(skc->skc_magic == SKC_MAGIC);
1147 spl_slab_reclaim(skc, skc->skc_reap, 0);
1148
1149 if (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags))
1150 schedule_delayed_work(&skc->skc_work, skc->skc_delay / 3 * HZ);
1151 }
1152
1153 /*
1154 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
1155 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
1156 * for very small objects we may end up with more than this so as not
1157 * to waste space in the minimal allocation of a single page. Also for
1158 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
1159 * lower than this and we will fail.
1160 */
1161 static int
1162 spl_slab_size(spl_kmem_cache_t *skc, uint32_t *objs, uint32_t *size)
1163 {
1164 uint32_t sks_size, obj_size, max_size;
1165
1166 if (skc->skc_flags & KMC_OFFSLAB) {
1167 *objs = SPL_KMEM_CACHE_OBJ_PER_SLAB;
1168 *size = sizeof(spl_kmem_slab_t);
1169 } else {
1170 sks_size = spl_sks_size(skc);
1171 obj_size = spl_obj_size(skc);
1172
1173 if (skc->skc_flags & KMC_KMEM)
1174 max_size = ((uint32_t)1 << (MAX_ORDER-3)) * PAGE_SIZE;
1175 else
1176 max_size = (32 * 1024 * 1024);
1177
1178 /* Power of two sized slab */
1179 for (*size = PAGE_SIZE; *size <= max_size; *size *= 2) {
1180 *objs = (*size - sks_size) / obj_size;
1181 if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB)
1182 SRETURN(0);
1183 }
1184
1185 /*
1186 * Unable to satisfy target objects per slab, fall back to
1187 * allocating a maximally sized slab and assuming it can
1188 * contain the minimum objects count use it. If not fail.
1189 */
1190 *size = max_size;
1191 *objs = (*size - sks_size) / obj_size;
1192 if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN)
1193 SRETURN(0);
1194 }
1195
1196 SRETURN(-ENOSPC);
1197 }
1198
1199 /*
1200 * Make a guess at reasonable per-cpu magazine size based on the size of
1201 * each object and the cost of caching N of them in each magazine. Long
1202 * term this should really adapt based on an observed usage heuristic.
1203 */
1204 static int
1205 spl_magazine_size(spl_kmem_cache_t *skc)
1206 {
1207 uint32_t obj_size = spl_obj_size(skc);
1208 int size;
1209 SENTRY;
1210
1211 /* Per-magazine sizes below assume a 4Kib page size */
1212 if (obj_size > (PAGE_SIZE * 256))
1213 size = 4; /* Minimum 4Mib per-magazine */
1214 else if (obj_size > (PAGE_SIZE * 32))
1215 size = 16; /* Minimum 2Mib per-magazine */
1216 else if (obj_size > (PAGE_SIZE))
1217 size = 64; /* Minimum 256Kib per-magazine */
1218 else if (obj_size > (PAGE_SIZE / 4))
1219 size = 128; /* Minimum 128Kib per-magazine */
1220 else
1221 size = 256;
1222
1223 SRETURN(size);
1224 }
1225
1226 /*
1227 * Allocate a per-cpu magazine to assoicate with a specific core.
1228 */
1229 static spl_kmem_magazine_t *
1230 spl_magazine_alloc(spl_kmem_cache_t *skc, int node)
1231 {
1232 spl_kmem_magazine_t *skm;
1233 int size = sizeof(spl_kmem_magazine_t) +
1234 sizeof(void *) * skc->skc_mag_size;
1235 SENTRY;
1236
1237 skm = kmem_alloc_node(size, KM_SLEEP, node);
1238 if (skm) {
1239 skm->skm_magic = SKM_MAGIC;
1240 skm->skm_avail = 0;
1241 skm->skm_size = skc->skc_mag_size;
1242 skm->skm_refill = skc->skc_mag_refill;
1243 skm->skm_cache = skc;
1244 spl_init_delayed_work(&skm->skm_work, spl_magazine_age, skm);
1245 skm->skm_age = jiffies;
1246 }
1247
1248 SRETURN(skm);
1249 }
1250
1251 /*
1252 * Free a per-cpu magazine assoicated with a specific core.
1253 */
1254 static void
1255 spl_magazine_free(spl_kmem_magazine_t *skm)
1256 {
1257 int size = sizeof(spl_kmem_magazine_t) +
1258 sizeof(void *) * skm->skm_size;
1259
1260 SENTRY;
1261 ASSERT(skm->skm_magic == SKM_MAGIC);
1262 ASSERT(skm->skm_avail == 0);
1263
1264 kmem_free(skm, size);
1265 SEXIT;
1266 }
1267
1268 /*
1269 * Create all pre-cpu magazines of reasonable sizes.
1270 */
1271 static int
1272 spl_magazine_create(spl_kmem_cache_t *skc)
1273 {
1274 int i;
1275 SENTRY;
1276
1277 skc->skc_mag_size = spl_magazine_size(skc);
1278 skc->skc_mag_refill = (skc->skc_mag_size + 1) / 2;
1279
1280 for_each_online_cpu(i) {
1281 skc->skc_mag[i] = spl_magazine_alloc(skc, cpu_to_node(i));
1282 if (!skc->skc_mag[i]) {
1283 for (i--; i >= 0; i--)
1284 spl_magazine_free(skc->skc_mag[i]);
1285
1286 SRETURN(-ENOMEM);
1287 }
1288 }
1289
1290 /* Only after everything is allocated schedule magazine work */
1291 for_each_online_cpu(i)
1292 schedule_delayed_work_on(i, &skc->skc_mag[i]->skm_work,
1293 skc->skc_delay / 3 * HZ);
1294
1295 SRETURN(0);
1296 }
1297
1298 /*
1299 * Destroy all pre-cpu magazines.
1300 */
1301 static void
1302 spl_magazine_destroy(spl_kmem_cache_t *skc)
1303 {
1304 spl_kmem_magazine_t *skm;
1305 int i;
1306 SENTRY;
1307
1308 for_each_online_cpu(i) {
1309 skm = skc->skc_mag[i];
1310 (void)spl_cache_flush(skc, skm, skm->skm_avail);
1311 spl_magazine_free(skm);
1312 }
1313
1314 SEXIT;
1315 }
1316
1317 /*
1318 * Create a object cache based on the following arguments:
1319 * name cache name
1320 * size cache object size
1321 * align cache object alignment
1322 * ctor cache object constructor
1323 * dtor cache object destructor
1324 * reclaim cache object reclaim
1325 * priv cache private data for ctor/dtor/reclaim
1326 * vmp unused must be NULL
1327 * flags
1328 * KMC_NOTOUCH Disable cache object aging (unsupported)
1329 * KMC_NODEBUG Disable debugging (unsupported)
1330 * KMC_NOMAGAZINE Disable magazine (unsupported)
1331 * KMC_NOHASH Disable hashing (unsupported)
1332 * KMC_QCACHE Disable qcache (unsupported)
1333 * KMC_KMEM Force kmem backed cache
1334 * KMC_VMEM Force vmem backed cache
1335 * KMC_OFFSLAB Locate objects off the slab
1336 */
1337 spl_kmem_cache_t *
1338 spl_kmem_cache_create(char *name, size_t size, size_t align,
1339 spl_kmem_ctor_t ctor,
1340 spl_kmem_dtor_t dtor,
1341 spl_kmem_reclaim_t reclaim,
1342 void *priv, void *vmp, int flags)
1343 {
1344 spl_kmem_cache_t *skc;
1345 int rc, kmem_flags = KM_SLEEP;
1346 SENTRY;
1347
1348 ASSERTF(!(flags & KMC_NOMAGAZINE), "Bad KMC_NOMAGAZINE (%x)\n", flags);
1349 ASSERTF(!(flags & KMC_NOHASH), "Bad KMC_NOHASH (%x)\n", flags);
1350 ASSERTF(!(flags & KMC_QCACHE), "Bad KMC_QCACHE (%x)\n", flags);
1351 ASSERT(vmp == NULL);
1352
1353 /* We may be called when there is a non-zero preempt_count or
1354 * interrupts are disabled is which case we must not sleep.
1355 */
1356 if (current_thread_info()->preempt_count || irqs_disabled())
1357 kmem_flags = KM_NOSLEEP;
1358
1359 /* Allocate memry for a new cache an initialize it. Unfortunately,
1360 * this usually ends up being a large allocation of ~32k because
1361 * we need to allocate enough memory for the worst case number of
1362 * cpus in the magazine, skc_mag[NR_CPUS]. Because of this we
1363 * explicitly pass KM_NODEBUG to suppress the kmem warning */
1364 skc = (spl_kmem_cache_t *)kmem_zalloc(sizeof(*skc),
1365 kmem_flags | KM_NODEBUG);
1366 if (skc == NULL)
1367 SRETURN(NULL);
1368
1369 skc->skc_magic = SKC_MAGIC;
1370 skc->skc_name_size = strlen(name) + 1;
1371 skc->skc_name = (char *)kmem_alloc(skc->skc_name_size, kmem_flags);
1372 if (skc->skc_name == NULL) {
1373 kmem_free(skc, sizeof(*skc));
1374 SRETURN(NULL);
1375 }
1376 strncpy(skc->skc_name, name, skc->skc_name_size);
1377
1378 skc->skc_ctor = ctor;
1379 skc->skc_dtor = dtor;
1380 skc->skc_reclaim = reclaim;
1381 skc->skc_private = priv;
1382 skc->skc_vmp = vmp;
1383 skc->skc_flags = flags;
1384 skc->skc_obj_size = size;
1385 skc->skc_obj_align = SPL_KMEM_CACHE_ALIGN;
1386 skc->skc_delay = SPL_KMEM_CACHE_DELAY;
1387 skc->skc_reap = SPL_KMEM_CACHE_REAP;
1388 atomic_set(&skc->skc_ref, 0);
1389
1390 INIT_LIST_HEAD(&skc->skc_list);
1391 INIT_LIST_HEAD(&skc->skc_complete_list);
1392 INIT_LIST_HEAD(&skc->skc_partial_list);
1393 spin_lock_init(&skc->skc_lock);
1394 skc->skc_slab_fail = 0;
1395 skc->skc_slab_create = 0;
1396 skc->skc_slab_destroy = 0;
1397 skc->skc_slab_total = 0;
1398 skc->skc_slab_alloc = 0;
1399 skc->skc_slab_max = 0;
1400 skc->skc_obj_total = 0;
1401 skc->skc_obj_alloc = 0;
1402 skc->skc_obj_max = 0;
1403
1404 if (align) {
1405 VERIFY(ISP2(align));
1406 VERIFY3U(align, >=, SPL_KMEM_CACHE_ALIGN); /* Min alignment */
1407 VERIFY3U(align, <=, PAGE_SIZE); /* Max alignment */
1408 skc->skc_obj_align = align;
1409 }
1410
1411 /* If none passed select a cache type based on object size */
1412 if (!(skc->skc_flags & (KMC_KMEM | KMC_VMEM))) {
1413 if (spl_obj_size(skc) < (PAGE_SIZE / 8))
1414 skc->skc_flags |= KMC_KMEM;
1415 else
1416 skc->skc_flags |= KMC_VMEM;
1417 }
1418
1419 rc = spl_slab_size(skc, &skc->skc_slab_objs, &skc->skc_slab_size);
1420 if (rc)
1421 SGOTO(out, rc);
1422
1423 rc = spl_magazine_create(skc);
1424 if (rc)
1425 SGOTO(out, rc);
1426
1427 spl_init_delayed_work(&skc->skc_work, spl_cache_age, skc);
1428 schedule_delayed_work(&skc->skc_work, skc->skc_delay / 3 * HZ);
1429
1430 down_write(&spl_kmem_cache_sem);
1431 list_add_tail(&skc->skc_list, &spl_kmem_cache_list);
1432 up_write(&spl_kmem_cache_sem);
1433
1434 SRETURN(skc);
1435 out:
1436 kmem_free(skc->skc_name, skc->skc_name_size);
1437 kmem_free(skc, sizeof(*skc));
1438 SRETURN(NULL);
1439 }
1440 EXPORT_SYMBOL(spl_kmem_cache_create);
1441
1442 /*
1443 * Register a move callback to for cache defragmentation.
1444 * XXX: Unimplemented but harmless to stub out for now.
1445 */
1446 void
1447 spl_kmem_cache_set_move(kmem_cache_t *skc,
1448 kmem_cbrc_t (move)(void *, void *, size_t, void *))
1449 {
1450 ASSERT(move != NULL);
1451 }
1452 EXPORT_SYMBOL(spl_kmem_cache_set_move);
1453
1454 /*
1455 * Destroy a cache and all objects assoicated with the cache.
1456 */
1457 void
1458 spl_kmem_cache_destroy(spl_kmem_cache_t *skc)
1459 {
1460 DECLARE_WAIT_QUEUE_HEAD(wq);
1461 int i;
1462 SENTRY;
1463
1464 ASSERT(skc->skc_magic == SKC_MAGIC);
1465
1466 down_write(&spl_kmem_cache_sem);
1467 list_del_init(&skc->skc_list);
1468 up_write(&spl_kmem_cache_sem);
1469
1470 /* Cancel any and wait for any pending delayed work */
1471 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1472 cancel_delayed_work(&skc->skc_work);
1473 for_each_online_cpu(i)
1474 cancel_delayed_work(&skc->skc_mag[i]->skm_work);
1475
1476 flush_scheduled_work();
1477
1478 /* Wait until all current callers complete, this is mainly
1479 * to catch the case where a low memory situation triggers a
1480 * cache reaping action which races with this destroy. */
1481 wait_event(wq, atomic_read(&skc->skc_ref) == 0);
1482
1483 spl_magazine_destroy(skc);
1484 spl_slab_reclaim(skc, 0, 1);
1485 spin_lock(&skc->skc_lock);
1486
1487 /* Validate there are no objects in use and free all the
1488 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1489 ASSERT3U(skc->skc_slab_alloc, ==, 0);
1490 ASSERT3U(skc->skc_obj_alloc, ==, 0);
1491 ASSERT3U(skc->skc_slab_total, ==, 0);
1492 ASSERT3U(skc->skc_obj_total, ==, 0);
1493 ASSERT(list_empty(&skc->skc_complete_list));
1494
1495 kmem_free(skc->skc_name, skc->skc_name_size);
1496 spin_unlock(&skc->skc_lock);
1497
1498 kmem_free(skc, sizeof(*skc));
1499
1500 SEXIT;
1501 }
1502 EXPORT_SYMBOL(spl_kmem_cache_destroy);
1503
1504 /*
1505 * Allocate an object from a slab attached to the cache. This is used to
1506 * repopulate the per-cpu magazine caches in batches when they run low.
1507 */
1508 static void *
1509 spl_cache_obj(spl_kmem_cache_t *skc, spl_kmem_slab_t *sks)
1510 {
1511 spl_kmem_obj_t *sko;
1512
1513 ASSERT(skc->skc_magic == SKC_MAGIC);
1514 ASSERT(sks->sks_magic == SKS_MAGIC);
1515 ASSERT(spin_is_locked(&skc->skc_lock));
1516
1517 sko = list_entry(sks->sks_free_list.next, spl_kmem_obj_t, sko_list);
1518 ASSERT(sko->sko_magic == SKO_MAGIC);
1519 ASSERT(sko->sko_addr != NULL);
1520
1521 /* Remove from sks_free_list */
1522 list_del_init(&sko->sko_list);
1523
1524 sks->sks_age = jiffies;
1525 sks->sks_ref++;
1526 skc->skc_obj_alloc++;
1527
1528 /* Track max obj usage statistics */
1529 if (skc->skc_obj_alloc > skc->skc_obj_max)
1530 skc->skc_obj_max = skc->skc_obj_alloc;
1531
1532 /* Track max slab usage statistics */
1533 if (sks->sks_ref == 1) {
1534 skc->skc_slab_alloc++;
1535
1536 if (skc->skc_slab_alloc > skc->skc_slab_max)
1537 skc->skc_slab_max = skc->skc_slab_alloc;
1538 }
1539
1540 return sko->sko_addr;
1541 }
1542
1543 /*
1544 * No available objects on any slabsi, create a new slab. Since this
1545 * is an expensive operation we do it without holding the spinlock and
1546 * only briefly aquire it when we link in the fully allocated and
1547 * constructed slab.
1548 */
1549 static spl_kmem_slab_t *
1550 spl_cache_grow(spl_kmem_cache_t *skc, int flags)
1551 {
1552 spl_kmem_slab_t *sks;
1553 SENTRY;
1554
1555 ASSERT(skc->skc_magic == SKC_MAGIC);
1556 local_irq_enable();
1557 might_sleep();
1558
1559 /*
1560 * Before allocating a new slab check if the slab is being reaped.
1561 * If it is there is a good chance we can wait until it finishes
1562 * and then use one of the newly freed but not aged-out slabs.
1563 */
1564 if (test_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
1565 schedule();
1566 SGOTO(out, sks= NULL);
1567 }
1568
1569 /* Allocate a new slab for the cache */
1570 sks = spl_slab_alloc(skc, flags | __GFP_NORETRY | KM_NODEBUG);
1571 if (sks == NULL)
1572 SGOTO(out, sks = NULL);
1573
1574 /* Link the new empty slab in to the end of skc_partial_list. */
1575 spin_lock(&skc->skc_lock);
1576 skc->skc_slab_total++;
1577 skc->skc_obj_total += sks->sks_objs;
1578 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
1579 spin_unlock(&skc->skc_lock);
1580 out:
1581 local_irq_disable();
1582
1583 SRETURN(sks);
1584 }
1585
1586 /*
1587 * Refill a per-cpu magazine with objects from the slabs for this
1588 * cache. Ideally the magazine can be repopulated using existing
1589 * objects which have been released, however if we are unable to
1590 * locate enough free objects new slabs of objects will be created.
1591 */
1592 static int
1593 spl_cache_refill(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flags)
1594 {
1595 spl_kmem_slab_t *sks;
1596 int rc = 0, refill;
1597 SENTRY;
1598
1599 ASSERT(skc->skc_magic == SKC_MAGIC);
1600 ASSERT(skm->skm_magic == SKM_MAGIC);
1601
1602 refill = MIN(skm->skm_refill, skm->skm_size - skm->skm_avail);
1603 spin_lock(&skc->skc_lock);
1604
1605 while (refill > 0) {
1606 /* No slabs available we may need to grow the cache */
1607 if (list_empty(&skc->skc_partial_list)) {
1608 spin_unlock(&skc->skc_lock);
1609
1610 sks = spl_cache_grow(skc, flags);
1611 if (!sks)
1612 SGOTO(out, rc);
1613
1614 /* Rescheduled to different CPU skm is not local */
1615 if (skm != skc->skc_mag[smp_processor_id()])
1616 SGOTO(out, rc);
1617
1618 /* Potentially rescheduled to the same CPU but
1619 * allocations may have occured from this CPU while
1620 * we were sleeping so recalculate max refill. */
1621 refill = MIN(refill, skm->skm_size - skm->skm_avail);
1622
1623 spin_lock(&skc->skc_lock);
1624 continue;
1625 }
1626
1627 /* Grab the next available slab */
1628 sks = list_entry((&skc->skc_partial_list)->next,
1629 spl_kmem_slab_t, sks_list);
1630 ASSERT(sks->sks_magic == SKS_MAGIC);
1631 ASSERT(sks->sks_ref < sks->sks_objs);
1632 ASSERT(!list_empty(&sks->sks_free_list));
1633
1634 /* Consume as many objects as needed to refill the requested
1635 * cache. We must also be careful not to overfill it. */
1636 while (sks->sks_ref < sks->sks_objs && refill-- > 0 && ++rc) {
1637 ASSERT(skm->skm_avail < skm->skm_size);
1638 ASSERT(rc < skm->skm_size);
1639 skm->skm_objs[skm->skm_avail++]=spl_cache_obj(skc,sks);
1640 }
1641
1642 /* Move slab to skc_complete_list when full */
1643 if (sks->sks_ref == sks->sks_objs) {
1644 list_del(&sks->sks_list);
1645 list_add(&sks->sks_list, &skc->skc_complete_list);
1646 }
1647 }
1648
1649 spin_unlock(&skc->skc_lock);
1650 out:
1651 /* Returns the number of entries added to cache */
1652 SRETURN(rc);
1653 }
1654
1655 /*
1656 * Release an object back to the slab from which it came.
1657 */
1658 static void
1659 spl_cache_shrink(spl_kmem_cache_t *skc, void *obj)
1660 {
1661 spl_kmem_slab_t *sks = NULL;
1662 spl_kmem_obj_t *sko = NULL;
1663 SENTRY;
1664
1665 ASSERT(skc->skc_magic == SKC_MAGIC);
1666 ASSERT(spin_is_locked(&skc->skc_lock));
1667
1668 sko = spl_sko_from_obj(skc, obj);
1669 ASSERT(sko->sko_magic == SKO_MAGIC);
1670 sks = sko->sko_slab;
1671 ASSERT(sks->sks_magic == SKS_MAGIC);
1672 ASSERT(sks->sks_cache == skc);
1673 list_add(&sko->sko_list, &sks->sks_free_list);
1674
1675 sks->sks_age = jiffies;
1676 sks->sks_ref--;
1677 skc->skc_obj_alloc--;
1678
1679 /* Move slab to skc_partial_list when no longer full. Slabs
1680 * are added to the head to keep the partial list is quasi-full
1681 * sorted order. Fuller at the head, emptier at the tail. */
1682 if (sks->sks_ref == (sks->sks_objs - 1)) {
1683 list_del(&sks->sks_list);
1684 list_add(&sks->sks_list, &skc->skc_partial_list);
1685 }
1686
1687 /* Move emply slabs to the end of the partial list so
1688 * they can be easily found and freed during reclamation. */
1689 if (sks->sks_ref == 0) {
1690 list_del(&sks->sks_list);
1691 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
1692 skc->skc_slab_alloc--;
1693 }
1694
1695 SEXIT;
1696 }
1697
1698 /*
1699 * Release a batch of objects from a per-cpu magazine back to their
1700 * respective slabs. This occurs when we exceed the magazine size,
1701 * are under memory pressure, when the cache is idle, or during
1702 * cache cleanup. The flush argument contains the number of entries
1703 * to remove from the magazine.
1704 */
1705 static int
1706 spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush)
1707 {
1708 int i, count = MIN(flush, skm->skm_avail);
1709 SENTRY;
1710
1711 ASSERT(skc->skc_magic == SKC_MAGIC);
1712 ASSERT(skm->skm_magic == SKM_MAGIC);
1713
1714 /*
1715 * XXX: Currently we simply return objects from the magazine to
1716 * the slabs in fifo order. The ideal thing to do from a memory
1717 * fragmentation standpoint is to cheaply determine the set of
1718 * objects in the magazine which will result in the largest
1719 * number of free slabs if released from the magazine.
1720 */
1721 spin_lock(&skc->skc_lock);
1722 for (i = 0; i < count; i++)
1723 spl_cache_shrink(skc, skm->skm_objs[i]);
1724
1725 skm->skm_avail -= count;
1726 memmove(skm->skm_objs, &(skm->skm_objs[count]),
1727 sizeof(void *) * skm->skm_avail);
1728
1729 spin_unlock(&skc->skc_lock);
1730
1731 SRETURN(count);
1732 }
1733
1734 /*
1735 * Allocate an object from the per-cpu magazine, or if the magazine
1736 * is empty directly allocate from a slab and repopulate the magazine.
1737 */
1738 void *
1739 spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags)
1740 {
1741 spl_kmem_magazine_t *skm;
1742 unsigned long irq_flags;
1743 void *obj = NULL;
1744 SENTRY;
1745
1746 ASSERT(skc->skc_magic == SKC_MAGIC);
1747 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1748 ASSERT(flags & KM_SLEEP);
1749 atomic_inc(&skc->skc_ref);
1750 local_irq_save(irq_flags);
1751
1752 restart:
1753 /* Safe to update per-cpu structure without lock, but
1754 * in the restart case we must be careful to reaquire
1755 * the local magazine since this may have changed
1756 * when we need to grow the cache. */
1757 skm = skc->skc_mag[smp_processor_id()];
1758 ASSERTF(skm->skm_magic == SKM_MAGIC, "%x != %x: %s/%p/%p %x/%x/%x\n",
1759 skm->skm_magic, SKM_MAGIC, skc->skc_name, skc, skm,
1760 skm->skm_size, skm->skm_refill, skm->skm_avail);
1761
1762 if (likely(skm->skm_avail)) {
1763 /* Object available in CPU cache, use it */
1764 obj = skm->skm_objs[--skm->skm_avail];
1765 skm->skm_age = jiffies;
1766 } else {
1767 /* Per-CPU cache empty, directly allocate from
1768 * the slab and refill the per-CPU cache. */
1769 (void)spl_cache_refill(skc, skm, flags);
1770 SGOTO(restart, obj = NULL);
1771 }
1772
1773 local_irq_restore(irq_flags);
1774 ASSERT(obj);
1775 ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
1776
1777 /* Pre-emptively migrate object to CPU L1 cache */
1778 prefetchw(obj);
1779 atomic_dec(&skc->skc_ref);
1780
1781 SRETURN(obj);
1782 }
1783 EXPORT_SYMBOL(spl_kmem_cache_alloc);
1784
1785 /*
1786 * Free an object back to the local per-cpu magazine, there is no
1787 * guarantee that this is the same magazine the object was originally
1788 * allocated from. We may need to flush entire from the magazine
1789 * back to the slabs to make space.
1790 */
1791 void
1792 spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj)
1793 {
1794 spl_kmem_magazine_t *skm;
1795 unsigned long flags;
1796 SENTRY;
1797
1798 ASSERT(skc->skc_magic == SKC_MAGIC);
1799 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1800 atomic_inc(&skc->skc_ref);
1801 local_irq_save(flags);
1802
1803 /* Safe to update per-cpu structure without lock, but
1804 * no remote memory allocation tracking is being performed
1805 * it is entirely possible to allocate an object from one
1806 * CPU cache and return it to another. */
1807 skm = skc->skc_mag[smp_processor_id()];
1808 ASSERT(skm->skm_magic == SKM_MAGIC);
1809
1810 /* Per-CPU cache full, flush it to make space */
1811 if (unlikely(skm->skm_avail >= skm->skm_size))
1812 (void)spl_cache_flush(skc, skm, skm->skm_refill);
1813
1814 /* Available space in cache, use it */
1815 skm->skm_objs[skm->skm_avail++] = obj;
1816
1817 local_irq_restore(flags);
1818 atomic_dec(&skc->skc_ref);
1819
1820 SEXIT;
1821 }
1822 EXPORT_SYMBOL(spl_kmem_cache_free);
1823
1824 /*
1825 * The generic shrinker function for all caches. Under linux a shrinker
1826 * may not be tightly coupled with a slab cache. In fact linux always
1827 * systematically trys calling all registered shrinker callbacks which
1828 * report that they contain unused objects. Because of this we only
1829 * register one shrinker function in the shim layer for all slab caches.
1830 * We always attempt to shrink all caches when this generic shrinker
1831 * is called. The shrinker should return the number of free objects
1832 * in the cache when called with nr_to_scan == 0 but not attempt to
1833 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1834 * objects should be freed, because Solaris semantics are to free
1835 * all available objects we may free more objects than requested.
1836 */
1837 #ifdef HAVE_3ARGS_SHRINKER_CALLBACK
1838 static int
1839 spl_kmem_cache_generic_shrinker(struct shrinker *shrinker_cb,
1840 int nr_to_scan, unsigned int gfp_mask)
1841 #else
1842 static int
1843 spl_kmem_cache_generic_shrinker(int nr_to_scan, unsigned int gfp_mask)
1844 #endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
1845 {
1846 spl_kmem_cache_t *skc;
1847 int unused = 0;
1848
1849 down_read(&spl_kmem_cache_sem);
1850 list_for_each_entry(skc, &spl_kmem_cache_list, skc_list) {
1851 if (nr_to_scan)
1852 spl_kmem_cache_reap_now(skc);
1853
1854 /*
1855 * Presume everything alloc'ed in reclaimable, this ensures
1856 * we are called again with nr_to_scan > 0 so can try and
1857 * reclaim. The exact number is not important either so
1858 * we forgo taking this already highly contented lock.
1859 */
1860 unused += skc->skc_obj_alloc;
1861 }
1862 up_read(&spl_kmem_cache_sem);
1863
1864 return (unused * sysctl_vfs_cache_pressure) / 100;
1865 }
1866
1867 /*
1868 * Call the registered reclaim function for a cache. Depending on how
1869 * many and which objects are released it may simply repopulate the
1870 * local magazine which will then need to age-out. Objects which cannot
1871 * fit in the magazine we will be released back to their slabs which will
1872 * also need to age out before being release. This is all just best
1873 * effort and we do not want to thrash creating and destroying slabs.
1874 */
1875 void
1876 spl_kmem_cache_reap_now(spl_kmem_cache_t *skc)
1877 {
1878 SENTRY;
1879
1880 ASSERT(skc->skc_magic == SKC_MAGIC);
1881 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1882
1883 /* Prevent concurrent cache reaping when contended */
1884 if (test_and_set_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
1885 SEXIT;
1886 return;
1887 }
1888
1889 atomic_inc(&skc->skc_ref);
1890
1891 if (skc->skc_reclaim)
1892 skc->skc_reclaim(skc->skc_private);
1893
1894 spl_slab_reclaim(skc, skc->skc_reap, 0);
1895 clear_bit(KMC_BIT_REAPING, &skc->skc_flags);
1896 atomic_dec(&skc->skc_ref);
1897
1898 SEXIT;
1899 }
1900 EXPORT_SYMBOL(spl_kmem_cache_reap_now);
1901
1902 /*
1903 * Reap all free slabs from all registered caches.
1904 */
1905 void
1906 spl_kmem_reap(void)
1907 {
1908 #ifdef HAVE_3ARGS_SHRINKER_CALLBACK
1909 spl_kmem_cache_generic_shrinker(NULL, KMC_REAP_CHUNK, GFP_KERNEL);
1910 #else
1911 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK, GFP_KERNEL);
1912 #endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
1913 }
1914 EXPORT_SYMBOL(spl_kmem_reap);
1915
1916 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1917 static char *
1918 spl_sprintf_addr(kmem_debug_t *kd, char *str, int len, int min)
1919 {
1920 int size = ((len - 1) < kd->kd_size) ? (len - 1) : kd->kd_size;
1921 int i, flag = 1;
1922
1923 ASSERT(str != NULL && len >= 17);
1924 memset(str, 0, len);
1925
1926 /* Check for a fully printable string, and while we are at
1927 * it place the printable characters in the passed buffer. */
1928 for (i = 0; i < size; i++) {
1929 str[i] = ((char *)(kd->kd_addr))[i];
1930 if (isprint(str[i])) {
1931 continue;
1932 } else {
1933 /* Minimum number of printable characters found
1934 * to make it worthwhile to print this as ascii. */
1935 if (i > min)
1936 break;
1937
1938 flag = 0;
1939 break;
1940 }
1941 }
1942
1943 if (!flag) {
1944 sprintf(str, "%02x%02x%02x%02x%02x%02x%02x%02x",
1945 *((uint8_t *)kd->kd_addr),
1946 *((uint8_t *)kd->kd_addr + 2),
1947 *((uint8_t *)kd->kd_addr + 4),
1948 *((uint8_t *)kd->kd_addr + 6),
1949 *((uint8_t *)kd->kd_addr + 8),
1950 *((uint8_t *)kd->kd_addr + 10),
1951 *((uint8_t *)kd->kd_addr + 12),
1952 *((uint8_t *)kd->kd_addr + 14));
1953 }
1954
1955 return str;
1956 }
1957
1958 static int
1959 spl_kmem_init_tracking(struct list_head *list, spinlock_t *lock, int size)
1960 {
1961 int i;
1962 SENTRY;
1963
1964 spin_lock_init(lock);
1965 INIT_LIST_HEAD(list);
1966
1967 for (i = 0; i < size; i++)
1968 INIT_HLIST_HEAD(&kmem_table[i]);
1969
1970 SRETURN(0);
1971 }
1972
1973 static void
1974 spl_kmem_fini_tracking(struct list_head *list, spinlock_t *lock)
1975 {
1976 unsigned long flags;
1977 kmem_debug_t *kd;
1978 char str[17];
1979 SENTRY;
1980
1981 spin_lock_irqsave(lock, flags);
1982 if (!list_empty(list))
1983 printk(KERN_WARNING "%-16s %-5s %-16s %s:%s\n", "address",
1984 "size", "data", "func", "line");
1985
1986 list_for_each_entry(kd, list, kd_list)
1987 printk(KERN_WARNING "%p %-5d %-16s %s:%d\n", kd->kd_addr,
1988 (int)kd->kd_size, spl_sprintf_addr(kd, str, 17, 8),
1989 kd->kd_func, kd->kd_line);
1990
1991 spin_unlock_irqrestore(lock, flags);
1992 SEXIT;
1993 }
1994 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1995 #define spl_kmem_init_tracking(list, lock, size)
1996 #define spl_kmem_fini_tracking(list, lock)
1997 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1998
1999 static void
2000 spl_kmem_init_globals(void)
2001 {
2002 struct zone *zone;
2003
2004 /* For now all zones are includes, it may be wise to restrict
2005 * this to normal and highmem zones if we see problems. */
2006 for_each_zone(zone) {
2007
2008 if (!populated_zone(zone))
2009 continue;
2010
2011 minfree += min_wmark_pages(zone);
2012 desfree += low_wmark_pages(zone);
2013 lotsfree += high_wmark_pages(zone);
2014 }
2015
2016 /* Solaris default values */
2017 swapfs_minfree = MAX(2*1024*1024 >> PAGE_SHIFT, physmem >> 3);
2018 swapfs_reserve = MIN(4*1024*1024 >> PAGE_SHIFT, physmem >> 4);
2019 }
2020
2021 /*
2022 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
2023 */
2024 int
2025 spl_kmem_init_kallsyms_lookup(void)
2026 {
2027 #ifndef HAVE_GET_VMALLOC_INFO
2028 get_vmalloc_info_fn = (get_vmalloc_info_t)
2029 spl_kallsyms_lookup_name("get_vmalloc_info");
2030 if (!get_vmalloc_info_fn) {
2031 printk(KERN_ERR "Error: Unknown symbol get_vmalloc_info\n");
2032 return -EFAULT;
2033 }
2034 #endif /* HAVE_GET_VMALLOC_INFO */
2035
2036 #ifdef HAVE_PGDAT_HELPERS
2037 # ifndef HAVE_FIRST_ONLINE_PGDAT
2038 first_online_pgdat_fn = (first_online_pgdat_t)
2039 spl_kallsyms_lookup_name("first_online_pgdat");
2040 if (!first_online_pgdat_fn) {
2041 printk(KERN_ERR "Error: Unknown symbol first_online_pgdat\n");
2042 return -EFAULT;
2043 }
2044 # endif /* HAVE_FIRST_ONLINE_PGDAT */
2045
2046 # ifndef HAVE_NEXT_ONLINE_PGDAT
2047 next_online_pgdat_fn = (next_online_pgdat_t)
2048 spl_kallsyms_lookup_name("next_online_pgdat");
2049 if (!next_online_pgdat_fn) {
2050 printk(KERN_ERR "Error: Unknown symbol next_online_pgdat\n");
2051 return -EFAULT;
2052 }
2053 # endif /* HAVE_NEXT_ONLINE_PGDAT */
2054
2055 # ifndef HAVE_NEXT_ZONE
2056 next_zone_fn = (next_zone_t)
2057 spl_kallsyms_lookup_name("next_zone");
2058 if (!next_zone_fn) {
2059 printk(KERN_ERR "Error: Unknown symbol next_zone\n");
2060 return -EFAULT;
2061 }
2062 # endif /* HAVE_NEXT_ZONE */
2063
2064 #else /* HAVE_PGDAT_HELPERS */
2065
2066 # ifndef HAVE_PGDAT_LIST
2067 pgdat_list_addr = *(struct pglist_data **)
2068 spl_kallsyms_lookup_name("pgdat_list");
2069 if (!pgdat_list_addr) {
2070 printk(KERN_ERR "Error: Unknown symbol pgdat_list\n");
2071 return -EFAULT;
2072 }
2073 # endif /* HAVE_PGDAT_LIST */
2074 #endif /* HAVE_PGDAT_HELPERS */
2075
2076 #if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS)
2077 get_zone_counts_fn = (get_zone_counts_t)
2078 spl_kallsyms_lookup_name("get_zone_counts");
2079 if (!get_zone_counts_fn) {
2080 printk(KERN_ERR "Error: Unknown symbol get_zone_counts\n");
2081 return -EFAULT;
2082 }
2083 #endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */
2084
2085 /*
2086 * It is now safe to initialize the global tunings which rely on
2087 * the use of the for_each_zone() macro. This macro in turns
2088 * depends on the *_pgdat symbols which are now available.
2089 */
2090 spl_kmem_init_globals();
2091
2092 return 0;
2093 }
2094
2095 int
2096 spl_kmem_init(void)
2097 {
2098 int rc = 0;
2099 SENTRY;
2100
2101 init_rwsem(&spl_kmem_cache_sem);
2102 INIT_LIST_HEAD(&spl_kmem_cache_list);
2103
2104 #ifdef HAVE_SET_SHRINKER
2105 spl_kmem_cache_shrinker = set_shrinker(KMC_DEFAULT_SEEKS,
2106 spl_kmem_cache_generic_shrinker);
2107 if (spl_kmem_cache_shrinker == NULL)
2108 SRETURN(rc = -ENOMEM);
2109 #else
2110 register_shrinker(&spl_kmem_cache_shrinker);
2111 #endif
2112
2113 #ifdef DEBUG_KMEM
2114 kmem_alloc_used_set(0);
2115 vmem_alloc_used_set(0);
2116
2117 spl_kmem_init_tracking(&kmem_list, &kmem_lock, KMEM_TABLE_SIZE);
2118 spl_kmem_init_tracking(&vmem_list, &vmem_lock, VMEM_TABLE_SIZE);
2119 #endif
2120 SRETURN(rc);
2121 }
2122
2123 void
2124 spl_kmem_fini(void)
2125 {
2126 #ifdef DEBUG_KMEM
2127 /* Display all unreclaimed memory addresses, including the
2128 * allocation size and the first few bytes of what's located
2129 * at that address to aid in debugging. Performance is not
2130 * a serious concern here since it is module unload time. */
2131 if (kmem_alloc_used_read() != 0)
2132 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
2133 "kmem leaked %ld/%ld bytes\n",
2134 kmem_alloc_used_read(), kmem_alloc_max);
2135
2136
2137 if (vmem_alloc_used_read() != 0)
2138 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
2139 "vmem leaked %ld/%ld bytes\n",
2140 vmem_alloc_used_read(), vmem_alloc_max);
2141
2142 spl_kmem_fini_tracking(&kmem_list, &kmem_lock);
2143 spl_kmem_fini_tracking(&vmem_list, &vmem_lock);
2144 #endif /* DEBUG_KMEM */
2145 SENTRY;
2146
2147 #ifdef HAVE_SET_SHRINKER
2148 remove_shrinker(spl_kmem_cache_shrinker);
2149 #else
2150 unregister_shrinker(&spl_kmem_cache_shrinker);
2151 #endif
2152
2153 SEXIT;
2154 }