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