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