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