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