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