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