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