]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-kmem.c
Reduce max kmem based slab size
[mirror_spl-debian.git] / module / spl / spl-kmem.c
1 /*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
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 int kmem_warning_flag = 1;
232
233 EXPORT_SYMBOL(kmem_alloc_used);
234 EXPORT_SYMBOL(kmem_alloc_max);
235 EXPORT_SYMBOL(vmem_alloc_used);
236 EXPORT_SYMBOL(vmem_alloc_max);
237 EXPORT_SYMBOL(kmem_warning_flag);
238
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.
249 */
250 # ifdef DEBUG_KMEM_TRACKING
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
258 typedef 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
267 spinlock_t kmem_lock;
268 struct hlist_head kmem_table[KMEM_TABLE_SIZE];
269 struct list_head kmem_list;
270
271 spinlock_t vmem_lock;
272 struct hlist_head vmem_table[VMEM_TABLE_SIZE];
273 struct list_head vmem_list;
274
275 EXPORT_SYMBOL(kmem_lock);
276 EXPORT_SYMBOL(kmem_table);
277 EXPORT_SYMBOL(kmem_list);
278
279 EXPORT_SYMBOL(vmem_lock);
280 EXPORT_SYMBOL(vmem_table);
281 EXPORT_SYMBOL(vmem_list);
282 # endif
283
284 int kmem_set_warning(int flag) { return (kmem_warning_flag = !!flag); }
285 #else
286 int kmem_set_warning(int flag) { return 0; }
287 #endif
288 EXPORT_SYMBOL(kmem_set_warning);
289
290 /*
291 * Slab allocation interfaces
292 *
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
301 * require a minimal level of cleanup for these data types unlike
302 * many Linux data type which do need to be explicitly destroyed.
303 *
304 * 2) Virtual address space backed slab. Callers of the Solaris slab
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 *
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.
333 */
334
335 struct list_head spl_kmem_cache_list; /* List of caches */
336 struct rw_semaphore spl_kmem_cache_sem; /* Cache list lock */
337
338 static int spl_cache_flush(spl_kmem_cache_t *skc,
339 spl_kmem_magazine_t *skm, int flush);
340
341 #ifdef HAVE_SET_SHRINKER
342 static struct shrinker *spl_kmem_cache_shrinker;
343 #else
344 static int spl_kmem_cache_generic_shrinker(int nr_to_scan,
345 unsigned int gfp_mask);
346 static struct shrinker spl_kmem_cache_shrinker = {
347 .shrink = spl_kmem_cache_generic_shrinker,
348 .seeks = KMC_DEFAULT_SEEKS,
349 };
350 #endif
351
352 #ifdef DEBUG_KMEM
353 # ifdef DEBUG_KMEM_TRACKING
354
355 static kmem_debug_t *
356 kmem_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
382 void *
383 kmem_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
391 dptr = (kmem_debug_t *) kmalloc_nofail(sizeof(kmem_debug_t),
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,
403 kmem_alloc_used_read(), kmem_alloc_max);
404
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,
413 kmem_alloc_used_read(), kmem_alloc_max);
414 goto out;
415 }
416
417 /* Use the correct allocator */
418 if (node_alloc) {
419 ASSERT(!(flags & __GFP_ZERO));
420 ptr = kmalloc_node_nofail(size, flags, node);
421 } else if (flags & __GFP_ZERO) {
422 ptr = kzalloc_nofail(size, flags & ~__GFP_ZERO);
423 } else {
424 ptr = kmalloc_nofail(size, flags);
425 }
426
427 if (unlikely(ptr == NULL)) {
428 kfree(dptr->kd_func);
429 kfree(dptr);
430 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
431 (unsigned long long) size, flags,
432 kmem_alloc_used_read(), kmem_alloc_max);
433 goto out;
434 }
435
436 kmem_alloc_used_add(size);
437 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max))
438 kmem_alloc_max = kmem_alloc_used_read();
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;
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,
455 ptr, kmem_alloc_used_read(),
456 kmem_alloc_max);
457 }
458 out:
459 RETURN(ptr);
460 }
461 EXPORT_SYMBOL(kmem_alloc_track);
462
463 void
464 kmem_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
481 kmem_alloc_used_sub(size);
482 CDEBUG_LIMIT(D_INFO, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr,
483 (unsigned long long) size, kmem_alloc_used_read(),
484 kmem_alloc_max);
485
486 kfree(dptr->kd_func);
487
488 memset(dptr, 0x5a, sizeof(kmem_debug_t));
489 kfree(dptr);
490
491 memset(ptr, 0x5a, size);
492 kfree(ptr);
493
494 EXIT;
495 }
496 EXPORT_SYMBOL(kmem_free_track);
497
498 void *
499 vmem_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
508 dptr = (kmem_debug_t *) kmalloc_nofail(sizeof(kmem_debug_t),
509 flags & ~__GFP_ZERO);
510 if (dptr == NULL) {
511 CWARN("vmem_alloc(%ld, 0x%x) debug failed\n",
512 sizeof(kmem_debug_t), flags);
513 } else {
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,
522 vmem_alloc_used_read(), vmem_alloc_max);
523 goto out;
524 }
525
526 ptr = __vmalloc(size, (flags | __GFP_HIGHMEM) & ~__GFP_ZERO,
527 PAGE_KERNEL);
528
529 if (unlikely(ptr == NULL)) {
530 kfree(dptr->kd_func);
531 kfree(dptr);
532 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
533 (unsigned long long) size, flags,
534 vmem_alloc_used_read(), vmem_alloc_max);
535 goto out;
536 }
537
538 if (flags & __GFP_ZERO)
539 memset(ptr, 0, size);
540
541 vmem_alloc_used_add(size);
542 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max))
543 vmem_alloc_max = vmem_alloc_used_read();
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;
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,
560 ptr, vmem_alloc_used_read(),
561 vmem_alloc_max);
562 }
563 out:
564 RETURN(ptr);
565 }
566 EXPORT_SYMBOL(vmem_alloc_track);
567
568 void
569 vmem_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
585 vmem_alloc_used_sub(size);
586 CDEBUG_LIMIT(D_INFO, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr,
587 (unsigned long long) size, vmem_alloc_used_read(),
588 vmem_alloc_max);
589
590 kfree(dptr->kd_func);
591
592 memset(dptr, 0x5a, sizeof(kmem_debug_t));
593 kfree(dptr);
594
595 memset(ptr, 0x5a, size);
596 vfree(ptr);
597
598 EXIT;
599 }
600 EXPORT_SYMBOL(vmem_free_track);
601
602 # else /* DEBUG_KMEM_TRACKING */
603
604 void *
605 kmem_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,
616 kmem_alloc_used_read(), kmem_alloc_max);
617
618 /* Use the correct allocator */
619 if (node_alloc) {
620 ASSERT(!(flags & __GFP_ZERO));
621 ptr = kmalloc_node_nofail(size, flags, node);
622 } else if (flags & __GFP_ZERO) {
623 ptr = kzalloc_nofail(size, flags & (~__GFP_ZERO));
624 } else {
625 ptr = kmalloc_nofail(size, flags);
626 }
627
628 if (ptr == NULL) {
629 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
630 (unsigned long long) size, flags,
631 kmem_alloc_used_read(), kmem_alloc_max);
632 } else {
633 kmem_alloc_used_add(size);
634 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max))
635 kmem_alloc_max = kmem_alloc_used_read();
636
637 CDEBUG_LIMIT(D_INFO, "kmem_alloc(%llu, 0x%x) = %p "
638 "(%lld/%llu)\n", (unsigned long long) size, flags, ptr,
639 kmem_alloc_used_read(), kmem_alloc_max);
640 }
641 RETURN(ptr);
642 }
643 EXPORT_SYMBOL(kmem_alloc_debug);
644
645 void
646 kmem_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
653 kmem_alloc_used_sub(size);
654 CDEBUG_LIMIT(D_INFO, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr,
655 (unsigned long long) size, kmem_alloc_used_read(),
656 kmem_alloc_max);
657
658 memset(ptr, 0x5a, size);
659 kfree(ptr);
660
661 EXIT;
662 }
663 EXPORT_SYMBOL(kmem_free_debug);
664
665 void *
666 vmem_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,
678 vmem_alloc_used_read(), vmem_alloc_max);
679 } else {
680 if (flags & __GFP_ZERO)
681 memset(ptr, 0, size);
682
683 vmem_alloc_used_add(size);
684 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max))
685 vmem_alloc_max = vmem_alloc_used_read();
686
687 CDEBUG_LIMIT(D_INFO, "vmem_alloc(%llu, 0x%x) = %p "
688 "(%lld/%llu)\n", (unsigned long long) size, flags, ptr,
689 vmem_alloc_used_read(), vmem_alloc_max);
690 }
691
692 RETURN(ptr);
693 }
694 EXPORT_SYMBOL(vmem_alloc_debug);
695
696 void
697 vmem_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
704 vmem_alloc_used_sub(size);
705 CDEBUG_LIMIT(D_INFO, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr,
706 (unsigned long long) size, vmem_alloc_used_read(),
707 vmem_alloc_max);
708
709 memset(ptr, 0x5a, size);
710 vfree(ptr);
711
712 EXIT;
713 }
714 EXPORT_SYMBOL(vmem_free_debug);
715
716 # endif /* DEBUG_KMEM_TRACKING */
717 #endif /* DEBUG_KMEM */
718
719 static void *
720 kv_alloc(spl_kmem_cache_t *skc, int size, int flags)
721 {
722 void *ptr;
723
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));
733
734 return ptr;
735 }
736
737 static void
738 kv_free(spl_kmem_cache_t *skc, void *ptr, int size)
739 {
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 */
752 static inline uint32_t
753 spl_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 */
762 static inline uint32_t
763 spl_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 */
774 static inline spl_kmem_obj_t *
775 spl_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 */
785 static inline uint32_t
786 spl_offslab_size(spl_kmem_cache_t *skc)
787 {
788 return 1UL << (highbit(spl_obj_size(skc)) + 1);
789 }
790
791 /*
792 * It's important that we pack the spl_kmem_obj_t structure and the
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 */
822 static spl_kmem_slab_t *
823 spl_slab_alloc(spl_kmem_cache_t *skc, int flags)
824 {
825 spl_kmem_slab_t *sks;
826 spl_kmem_obj_t *sko, *n;
827 void *base, *obj;
828 uint32_t obj_size, offslab_size = 0;
829 int i, rc = 0;
830
831 base = kv_alloc(skc, skc->skc_slab_size, flags);
832 if (base == NULL)
833 RETURN(NULL);
834
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;
843 obj_size = spl_obj_size(skc);
844
845 if (skc->skc_flags * KMC_OFFSLAB)
846 offslab_size = spl_offslab_size(skc);
847
848 for (i = 0; i < sks->sks_objs; i++) {
849 if (skc->skc_flags & KMC_OFFSLAB) {
850 obj = kv_alloc(skc, offslab_size, flags);
851 if (!obj)
852 GOTO(out, rc = -ENOMEM);
853 } else {
854 obj = base + spl_sks_size(skc) + (i * obj_size);
855 }
856
857 ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
858 sko = spl_sko_from_obj(skc, obj);
859 sko->sko_addr = obj;
860 sko->sko_magic = SKO_MAGIC;
861 sko->sko_slab = sks;
862 INIT_LIST_HEAD(&sko->sko_list);
863 list_add_tail(&sko->sko_list, &sks->sks_free_list);
864 }
865
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);
869 out:
870 if (rc) {
871 if (skc->skc_flags & KMC_OFFSLAB)
872 list_for_each_entry_safe(sko, n, &sks->sks_free_list,
873 sko_list)
874 kv_free(skc, sko->sko_addr, offslab_size);
875
876 kv_free(skc, base, skc->skc_slab_size);
877 sks = NULL;
878 }
879
880 RETURN(sks);
881 }
882
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.
887 */
888 static void
889 spl_slab_free(spl_kmem_slab_t *sks,
890 struct list_head *sks_list, struct list_head *sko_list)
891 {
892 spl_kmem_cache_t *skc;
893 ENTRY;
894
895 ASSERT(sks->sks_magic == SKS_MAGIC);
896 ASSERT(sks->sks_ref == 0);
897
898 skc = sks->sks_cache;
899 ASSERT(skc->skc_magic == SKC_MAGIC);
900 ASSERT(spin_is_locked(&skc->skc_lock));
901
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 */
908 skc->skc_obj_total -= sks->sks_objs;
909 skc->skc_slab_total--;
910 list_del(&sks->sks_list);
911 list_add(&sks->sks_list, sks_list);
912 list_splice_init(&sks->sks_free_list, sko_list);
913
914 EXIT;
915 }
916
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
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.
924 */
925 static void
926 spl_slab_reclaim(spl_kmem_cache_t *skc, int count, int flag)
927 {
928 spl_kmem_slab_t *sks, *m;
929 spl_kmem_obj_t *sko, *n;
930 LIST_HEAD(sks_list);
931 LIST_HEAD(sko_list);
932 uint32_t size = 0;
933 int i = 0;
934 ENTRY;
935
936 /*
937 * Move empty slabs and objects which have not been touched in
938 * skc_delay seconds on to private lists to be freed outside
939 * the spin lock. This delay time is important to avoid thrashing
940 * however when flag is set the delay will not be used.
941 */
942 spin_lock(&skc->skc_lock);
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))
951 break;
952
953 if (time_after(jiffies,sks->sks_age+skc->skc_delay*HZ)||flag) {
954 spl_slab_free(sks, &sks_list, &sko_list);
955 i++;
956 }
957 }
958 spin_unlock(&skc->skc_lock);
959
960 /*
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.
967 */
968 if (skc->skc_flags & KMC_OFFSLAB)
969 size = spl_offslab_size(skc);
970
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)
978 kv_free(skc, sko->sko_addr, size);
979
980 cond_resched();
981 }
982
983 list_for_each_entry_safe(sks, m, &sks_list, sks_list) {
984 ASSERT(sks->sks_magic == SKS_MAGIC);
985 kv_free(skc, sks, skc->skc_slab_size);
986 cond_resched();
987 }
988
989 EXIT;
990 }
991
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 */
999 static void
1000 spl_magazine_age(void *data)
1001 {
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);
1010
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);
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);
1018 }
1019
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 */
1026 static void
1027 spl_cache_age(void *data)
1028 {
1029 spl_kmem_cache_t *skc =
1030 spl_get_work_data(data, spl_kmem_cache_t, skc_work.work);
1031
1032 ASSERT(skc->skc_magic == SKC_MAGIC);
1033 spl_slab_reclaim(skc, skc->skc_reap, 0);
1034
1035 if (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags))
1036 schedule_delayed_work(&skc->skc_work, skc->skc_delay / 3 * HZ);
1037 }
1038
1039 /*
1040 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
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 */
1047 static int
1048 spl_slab_size(spl_kmem_cache_t *skc, uint32_t *objs, uint32_t *size)
1049 {
1050 uint32_t sks_size, obj_size, max_size;
1051
1052 if (skc->skc_flags & KMC_OFFSLAB) {
1053 *objs = SPL_KMEM_CACHE_OBJ_PER_SLAB;
1054 *size = sizeof(spl_kmem_slab_t);
1055 } else {
1056 sks_size = spl_sks_size(skc);
1057 obj_size = spl_obj_size(skc);
1058
1059 if (skc->skc_flags & KMC_KMEM)
1060 max_size = ((uint32_t)1 << (MAX_ORDER-3)) * PAGE_SIZE;
1061 else
1062 max_size = (32 * 1024 * 1024);
1063
1064 /* Power of two sized slab */
1065 for (*size = PAGE_SIZE; *size <= max_size; *size *= 2) {
1066 *objs = (*size - sks_size) / obj_size;
1067 if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB)
1068 RETURN(0);
1069 }
1070
1071 /*
1072 * Unable to satisfy target objects per slab, fall back to
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);
1080 }
1081
1082 RETURN(-ENOSPC);
1083 }
1084
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 */
1090 static int
1091 spl_magazine_size(spl_kmem_cache_t *skc)
1092 {
1093 uint32_t obj_size = spl_obj_size(skc);
1094 int size;
1095 ENTRY;
1096
1097 /* Per-magazine sizes below assume a 4Kib page size */
1098 if (obj_size > (PAGE_SIZE * 256))
1099 size = 4; /* Minimum 4Mib per-magazine */
1100 else if (obj_size > (PAGE_SIZE * 32))
1101 size = 16; /* Minimum 2Mib per-magazine */
1102 else if (obj_size > (PAGE_SIZE))
1103 size = 64; /* Minimum 256Kib per-magazine */
1104 else if (obj_size > (PAGE_SIZE / 4))
1105 size = 128; /* Minimum 128Kib per-magazine */
1106 else
1107 size = 256;
1108
1109 RETURN(size);
1110 }
1111
1112 /*
1113 * Allocate a per-cpu magazine to assoicate with a specific core.
1114 */
1115 static spl_kmem_magazine_t *
1116 spl_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
1123 skm = kmem_alloc_node(size, KM_SLEEP, node);
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;
1129 skm->skm_cache = skc;
1130 spl_init_delayed_work(&skm->skm_work, spl_magazine_age, skm);
1131 skm->skm_age = jiffies;
1132 }
1133
1134 RETURN(skm);
1135 }
1136
1137 /*
1138 * Free a per-cpu magazine assoicated with a specific core.
1139 */
1140 static void
1141 spl_magazine_free(spl_kmem_magazine_t *skm)
1142 {
1143 int size = sizeof(spl_kmem_magazine_t) +
1144 sizeof(void *) * skm->skm_size;
1145
1146 ENTRY;
1147 ASSERT(skm->skm_magic == SKM_MAGIC);
1148 ASSERT(skm->skm_avail == 0);
1149
1150 kmem_free(skm, size);
1151 EXIT;
1152 }
1153
1154 /*
1155 * Create all pre-cpu magazines of reasonable sizes.
1156 */
1157 static int
1158 spl_magazine_create(spl_kmem_cache_t *skc)
1159 {
1160 int i;
1161 ENTRY;
1162
1163 skc->skc_mag_size = spl_magazine_size(skc);
1164 skc->skc_mag_refill = (skc->skc_mag_size + 1) / 2;
1165
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]);
1171
1172 RETURN(-ENOMEM);
1173 }
1174 }
1175
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
1181 RETURN(0);
1182 }
1183
1184 /*
1185 * Destroy all pre-cpu magazines.
1186 */
1187 static void
1188 spl_magazine_destroy(spl_kmem_cache_t *skc)
1189 {
1190 spl_kmem_magazine_t *skm;
1191 int i;
1192 ENTRY;
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
1200 EXIT;
1201 }
1202
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 */
1223 spl_kmem_cache_t *
1224 spl_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;
1231 int rc, kmem_flags = KM_SLEEP;
1232 ENTRY;
1233
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);
1237 ASSERT(vmp == NULL);
1238
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 */
1242 if (current_thread_info()->preempt_count || irqs_disabled())
1243 kmem_flags = KM_NOSLEEP;
1244
1245 /* Allocate new cache memory and initialize. */
1246 skc = (spl_kmem_cache_t *)kmem_zalloc(sizeof(*skc), kmem_flags);
1247 if (skc == NULL)
1248 RETURN(NULL);
1249
1250 skc->skc_magic = SKC_MAGIC;
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
1259 skc->skc_ctor = ctor;
1260 skc->skc_dtor = dtor;
1261 skc->skc_reclaim = reclaim;
1262 skc->skc_private = priv;
1263 skc->skc_vmp = vmp;
1264 skc->skc_flags = flags;
1265 skc->skc_obj_size = size;
1266 skc->skc_obj_align = SPL_KMEM_CACHE_ALIGN;
1267 skc->skc_delay = SPL_KMEM_CACHE_DELAY;
1268 skc->skc_reap = SPL_KMEM_CACHE_REAP;
1269 atomic_set(&skc->skc_ref, 0);
1270
1271 INIT_LIST_HEAD(&skc->skc_list);
1272 INIT_LIST_HEAD(&skc->skc_complete_list);
1273 INIT_LIST_HEAD(&skc->skc_partial_list);
1274 spin_lock_init(&skc->skc_lock);
1275 skc->skc_slab_fail = 0;
1276 skc->skc_slab_create = 0;
1277 skc->skc_slab_destroy = 0;
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;
1284
1285 if (align) {
1286 VERIFY(ISP2(align));
1287 VERIFY3U(align, >=, SPL_KMEM_CACHE_ALIGN); /* Min alignment */
1288 VERIFY3U(align, <=, PAGE_SIZE); /* Max alignment */
1289 skc->skc_obj_align = align;
1290 }
1291
1292 /* If none passed select a cache type based on object size */
1293 if (!(skc->skc_flags & (KMC_KMEM | KMC_VMEM))) {
1294 if (spl_obj_size(skc) < (PAGE_SIZE / 8))
1295 skc->skc_flags |= KMC_KMEM;
1296 else
1297 skc->skc_flags |= KMC_VMEM;
1298 }
1299
1300 rc = spl_slab_size(skc, &skc->skc_slab_objs, &skc->skc_slab_size);
1301 if (rc)
1302 GOTO(out, rc);
1303
1304 rc = spl_magazine_create(skc);
1305 if (rc)
1306 GOTO(out, rc);
1307
1308 spl_init_delayed_work(&skc->skc_work, spl_cache_age, skc);
1309 schedule_delayed_work(&skc->skc_work, skc->skc_delay / 3 * HZ);
1310
1311 down_write(&spl_kmem_cache_sem);
1312 list_add_tail(&skc->skc_list, &spl_kmem_cache_list);
1313 up_write(&spl_kmem_cache_sem);
1314
1315 RETURN(skc);
1316 out:
1317 kmem_free(skc->skc_name, skc->skc_name_size);
1318 kmem_free(skc, sizeof(*skc));
1319 RETURN(NULL);
1320 }
1321 EXPORT_SYMBOL(spl_kmem_cache_create);
1322
1323 /*
1324 * Destroy a cache and all objects assoicated with the cache.
1325 */
1326 void
1327 spl_kmem_cache_destroy(spl_kmem_cache_t *skc)
1328 {
1329 DECLARE_WAIT_QUEUE_HEAD(wq);
1330 int i;
1331 ENTRY;
1332
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);
1338
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);
1342 for_each_online_cpu(i)
1343 cancel_delayed_work(&skc->skc_mag[i]->skm_work);
1344
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
1352 spl_magazine_destroy(skc);
1353 spl_slab_reclaim(skc, 0, 1);
1354 spin_lock(&skc->skc_lock);
1355
1356 /* Validate there are no objects in use and free all the
1357 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
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);
1362 ASSERT(list_empty(&skc->skc_complete_list));
1363
1364 kmem_free(skc->skc_name, skc->skc_name_size);
1365 spin_unlock(&skc->skc_lock);
1366
1367 kmem_free(skc, sizeof(*skc));
1368
1369 EXIT;
1370 }
1371 EXPORT_SYMBOL(spl_kmem_cache_destroy);
1372
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 */
1377 static void *
1378 spl_cache_obj(spl_kmem_cache_t *skc, spl_kmem_slab_t *sks)
1379 {
1380 spl_kmem_obj_t *sko;
1381
1382 ASSERT(skc->skc_magic == SKC_MAGIC);
1383 ASSERT(sks->sks_magic == SKS_MAGIC);
1384 ASSERT(spin_is_locked(&skc->skc_lock));
1385
1386 sko = list_entry(sks->sks_free_list.next, spl_kmem_obj_t, sko_list);
1387 ASSERT(sko->sko_magic == SKO_MAGIC);
1388 ASSERT(sko->sko_addr != NULL);
1389
1390 /* Remove from sks_free_list */
1391 list_del_init(&sko->sko_list);
1392
1393 sks->sks_age = jiffies;
1394 sks->sks_ref++;
1395 skc->skc_obj_alloc++;
1396
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;
1400
1401 /* Track max slab usage statistics */
1402 if (sks->sks_ref == 1) {
1403 skc->skc_slab_alloc++;
1404
1405 if (skc->skc_slab_alloc > skc->skc_slab_max)
1406 skc->skc_slab_max = skc->skc_slab_alloc;
1407 }
1408
1409 return sko->sko_addr;
1410 }
1411
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.
1417 */
1418 static spl_kmem_slab_t *
1419 spl_cache_grow(spl_kmem_cache_t *skc, int flags)
1420 {
1421 spl_kmem_slab_t *sks;
1422 ENTRY;
1423
1424 ASSERT(skc->skc_magic == SKC_MAGIC);
1425 local_irq_enable();
1426 might_sleep();
1427
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);
1436 }
1437
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);
1442
1443 /* Link the new empty slab in to the end of skc_partial_list. */
1444 spin_lock(&skc->skc_lock);
1445 skc->skc_slab_total++;
1446 skc->skc_obj_total += sks->sks_objs;
1447 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
1448 spin_unlock(&skc->skc_lock);
1449 out:
1450 local_irq_disable();
1451
1452 RETURN(sks);
1453 }
1454
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 */
1461 static int
1462 spl_cache_refill(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flags)
1463 {
1464 spl_kmem_slab_t *sks;
1465 int rc = 0, refill;
1466 ENTRY;
1467
1468 ASSERT(skc->skc_magic == SKC_MAGIC);
1469 ASSERT(skm->skm_magic == SKM_MAGIC);
1470
1471 refill = MIN(skm->skm_refill, skm->skm_size - skm->skm_avail);
1472 spin_lock(&skc->skc_lock);
1473
1474 while (refill > 0) {
1475 /* No slabs available we may need to grow the cache */
1476 if (list_empty(&skc->skc_partial_list)) {
1477 spin_unlock(&skc->skc_lock);
1478
1479 sks = spl_cache_grow(skc, flags);
1480 if (!sks)
1481 GOTO(out, rc);
1482
1483 /* Rescheduled to different CPU skm is not local */
1484 if (skm != skc->skc_mag[smp_processor_id()])
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);
1491
1492 spin_lock(&skc->skc_lock);
1493 continue;
1494 }
1495
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));
1502
1503 /* Consume as many objects as needed to refill the requested
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);
1508 skm->skm_objs[skm->skm_avail++]=spl_cache_obj(skc,sks);
1509 }
1510
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);
1515 }
1516 }
1517
1518 spin_unlock(&skc->skc_lock);
1519 out:
1520 /* Returns the number of entries added to cache */
1521 RETURN(rc);
1522 }
1523
1524 /*
1525 * Release an object back to the slab from which it came.
1526 */
1527 static void
1528 spl_cache_shrink(spl_kmem_cache_t *skc, void *obj)
1529 {
1530 spl_kmem_slab_t *sks = NULL;
1531 spl_kmem_obj_t *sko = NULL;
1532 ENTRY;
1533
1534 ASSERT(skc->skc_magic == SKC_MAGIC);
1535 ASSERT(spin_is_locked(&skc->skc_lock));
1536
1537 sko = spl_sko_from_obj(skc, obj);
1538 ASSERT(sko->sko_magic == SKO_MAGIC);
1539 sks = sko->sko_slab;
1540 ASSERT(sks->sks_magic == SKS_MAGIC);
1541 ASSERT(sks->sks_cache == skc);
1542 list_add(&sko->sko_list, &sks->sks_free_list);
1543
1544 sks->sks_age = jiffies;
1545 sks->sks_ref--;
1546 skc->skc_obj_alloc--;
1547
1548 /* Move slab to skc_partial_list when no longer full. Slabs
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)) {
1552 list_del(&sks->sks_list);
1553 list_add(&sks->sks_list, &skc->skc_partial_list);
1554 }
1555
1556 /* Move emply slabs to the end of the partial list so
1557 * they can be easily found and freed during reclamation. */
1558 if (sks->sks_ref == 0) {
1559 list_del(&sks->sks_list);
1560 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
1561 skc->skc_slab_alloc--;
1562 }
1563
1564 EXIT;
1565 }
1566
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 */
1574 static int
1575 spl_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
1580 ASSERT(skc->skc_magic == SKC_MAGIC);
1581 ASSERT(skm->skm_magic == SKM_MAGIC);
1582
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 */
1590 spin_lock(&skc->skc_lock);
1591 for (i = 0; i < count; i++)
1592 spl_cache_shrink(skc, skm->skm_objs[i]);
1593
1594 skm->skm_avail -= count;
1595 memmove(skm->skm_objs, &(skm->skm_objs[count]),
1596 sizeof(void *) * skm->skm_avail);
1597
1598 spin_unlock(&skc->skc_lock);
1599
1600 RETURN(count);
1601 }
1602
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 */
1607 void *
1608 spl_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
1615 ASSERT(skc->skc_magic == SKC_MAGIC);
1616 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1617 ASSERT(flags & KM_SLEEP);
1618 atomic_inc(&skc->skc_ref);
1619 local_irq_save(irq_flags);
1620
1621 restart:
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()];
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);
1630
1631 if (likely(skm->skm_avail)) {
1632 /* Object available in CPU cache, use it */
1633 obj = skm->skm_objs[--skm->skm_avail];
1634 skm->skm_age = jiffies;
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);
1643 ASSERT(obj);
1644 ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
1645
1646 /* Pre-emptively migrate object to CPU L1 cache */
1647 prefetchw(obj);
1648 atomic_dec(&skc->skc_ref);
1649
1650 RETURN(obj);
1651 }
1652 EXPORT_SYMBOL(spl_kmem_cache_alloc);
1653
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 */
1660 void
1661 spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj)
1662 {
1663 spl_kmem_magazine_t *skm;
1664 unsigned long flags;
1665 ENTRY;
1666
1667 ASSERT(skc->skc_magic == SKC_MAGIC);
1668 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1669 atomic_inc(&skc->skc_ref);
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()];
1677 ASSERT(skm->skm_magic == SKM_MAGIC);
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);
1687 atomic_dec(&skc->skc_ref);
1688
1689 EXIT;
1690 }
1691 EXPORT_SYMBOL(spl_kmem_cache_free);
1692
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 */
1706 static int
1707 spl_kmem_cache_generic_shrinker(int nr_to_scan, unsigned int gfp_mask)
1708 {
1709 spl_kmem_cache_t *skc;
1710 int unused = 0;
1711
1712 down_read(&spl_kmem_cache_sem);
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 }
1725 up_read(&spl_kmem_cache_sem);
1726
1727 return (unused * sysctl_vfs_cache_pressure) / 100;
1728 }
1729
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 */
1738 void
1739 spl_kmem_cache_reap_now(spl_kmem_cache_t *skc)
1740 {
1741 ENTRY;
1742
1743 ASSERT(skc->skc_magic == SKC_MAGIC);
1744 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1745
1746 /* Prevent concurrent cache reaping when contended */
1747 if (test_and_set_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
1748 EXIT;
1749 return;
1750 }
1751
1752 atomic_inc(&skc->skc_ref);
1753
1754 if (skc->skc_reclaim)
1755 skc->skc_reclaim(skc->skc_private);
1756
1757 spl_slab_reclaim(skc, skc->skc_reap, 0);
1758 clear_bit(KMC_BIT_REAPING, &skc->skc_flags);
1759 atomic_dec(&skc->skc_ref);
1760
1761 EXIT;
1762 }
1763 EXPORT_SYMBOL(spl_kmem_cache_reap_now);
1764
1765 /*
1766 * Reap all free slabs from all registered caches.
1767 */
1768 void
1769 spl_kmem_reap(void)
1770 {
1771 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK, GFP_KERNEL);
1772 }
1773 EXPORT_SYMBOL(spl_kmem_reap);
1774
1775 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1776 static char *
1777 spl_sprintf_addr(kmem_debug_t *kd, char *str, int len, int min)
1778 {
1779 int size = ((len - 1) < kd->kd_size) ? (len - 1) : kd->kd_size;
1780 int i, flag = 1;
1781
1782 ASSERT(str != NULL && len >= 17);
1783 memset(str, 0, len);
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++) {
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 }
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
1817 static int
1818 spl_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
1832 static void
1833 spl_kmem_fini_tracking(struct list_head *list, spinlock_t *lock)
1834 {
1835 unsigned long flags;
1836 kmem_debug_t *kd;
1837 char str[17];
1838 ENTRY;
1839
1840 spin_lock_irqsave(lock, flags);
1841 if (!list_empty(list))
1842 printk(KERN_WARNING "%-16s %-5s %-16s %s:%s\n", "address",
1843 "size", "data", "func", "line");
1844
1845 list_for_each_entry(kd, list, kd_list)
1846 printk(KERN_WARNING "%p %-5d %-16s %s:%d\n", kd->kd_addr,
1847 (int)kd->kd_size, spl_sprintf_addr(kd, str, 17, 8),
1848 kd->kd_func, kd->kd_line);
1849
1850 spin_unlock_irqrestore(lock, flags);
1851 EXIT;
1852 }
1853 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1854 #define spl_kmem_init_tracking(list, lock, size)
1855 #define spl_kmem_fini_tracking(list, lock)
1856 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1857
1858 static void
1859 spl_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
1870 minfree += min_wmark_pages(zone);
1871 desfree += low_wmark_pages(zone);
1872 lotsfree += high_wmark_pages(zone);
1873 }
1874
1875 /* Solaris default values */
1876 swapfs_minfree = MAX(2*1024*1024 >> PAGE_SHIFT, physmem >> 3);
1877 swapfs_reserve = MIN(4*1024*1024 >> PAGE_SHIFT, physmem >> 4);
1878 }
1879
1880 /*
1881 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
1882 */
1883 int
1884 spl_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");
1889 if (!get_vmalloc_info_fn) {
1890 printk(KERN_ERR "Error: Unknown symbol get_vmalloc_info\n");
1891 return -EFAULT;
1892 }
1893 #endif /* HAVE_GET_VMALLOC_INFO */
1894
1895 #ifdef HAVE_PGDAT_HELPERS
1896 # ifndef HAVE_FIRST_ONLINE_PGDAT
1897 first_online_pgdat_fn = (first_online_pgdat_t)
1898 spl_kallsyms_lookup_name("first_online_pgdat");
1899 if (!first_online_pgdat_fn) {
1900 printk(KERN_ERR "Error: Unknown symbol first_online_pgdat\n");
1901 return -EFAULT;
1902 }
1903 # endif /* HAVE_FIRST_ONLINE_PGDAT */
1904
1905 # ifndef HAVE_NEXT_ONLINE_PGDAT
1906 next_online_pgdat_fn = (next_online_pgdat_t)
1907 spl_kallsyms_lookup_name("next_online_pgdat");
1908 if (!next_online_pgdat_fn) {
1909 printk(KERN_ERR "Error: Unknown symbol next_online_pgdat\n");
1910 return -EFAULT;
1911 }
1912 # endif /* HAVE_NEXT_ONLINE_PGDAT */
1913
1914 # ifndef HAVE_NEXT_ZONE
1915 next_zone_fn = (next_zone_t)
1916 spl_kallsyms_lookup_name("next_zone");
1917 if (!next_zone_fn) {
1918 printk(KERN_ERR "Error: Unknown symbol next_zone\n");
1919 return -EFAULT;
1920 }
1921 # endif /* HAVE_NEXT_ZONE */
1922
1923 #else /* HAVE_PGDAT_HELPERS */
1924
1925 # ifndef HAVE_PGDAT_LIST
1926 pgdat_list_addr = *(struct pglist_data **)
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 */
1934
1935 #if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS)
1936 get_zone_counts_fn = (get_zone_counts_t)
1937 spl_kallsyms_lookup_name("get_zone_counts");
1938 if (!get_zone_counts_fn) {
1939 printk(KERN_ERR "Error: Unknown symbol get_zone_counts\n");
1940 return -EFAULT;
1941 }
1942 #endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */
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
1954 int
1955 spl_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)
1967 RETURN(rc = -ENOMEM);
1968 #else
1969 register_shrinker(&spl_kmem_cache_shrinker);
1970 #endif
1971
1972 #ifdef DEBUG_KMEM
1973 kmem_alloc_used_set(0);
1974 vmem_alloc_used_set(0);
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
1979 RETURN(rc);
1980 }
1981
1982 void
1983 spl_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. */
1990 if (kmem_alloc_used_read() != 0)
1991 CWARN("kmem leaked %ld/%ld bytes\n",
1992 kmem_alloc_used_read(), kmem_alloc_max);
1993
1994
1995 if (vmem_alloc_used_read() != 0)
1996 CWARN("vmem leaked %ld/%ld bytes\n",
1997 vmem_alloc_used_read(), vmem_alloc_max);
1998
1999 spl_kmem_fini_tracking(&kmem_list, &kmem_lock);
2000 spl_kmem_fini_tracking(&vmem_list, &vmem_lock);
2001 #endif /* DEBUG_KMEM */
2002 ENTRY;
2003
2004 #ifdef HAVE_SET_SHRINKER
2005 remove_shrinker(spl_kmem_cache_shrinker);
2006 #else
2007 unregister_shrinker(&spl_kmem_cache_shrinker);
2008 #endif
2009
2010 EXIT;
2011 }