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