Skip to content

Commit bcb3007

Browse files
committed
using lookasidelist for abd_chunk_cache
1 parent 143eb1b commit bcb3007

File tree

5 files changed

+130
-12
lines changed

5 files changed

+130
-12
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef _SPL_LOOKASIDELIST_H
2+
#define _SPL_LOOKASIDELIST_H
3+
4+
extern void* osif_malloc(uint64_t);
5+
extern void osif_free(void*, uint64_t);
6+
7+
#define ZFS_LookAsideList_DRV_TAG '!SFZ'
8+
9+
typedef struct lookasidelist_cache {
10+
ULONG cache_active_allocations;
11+
size_t cache_chunksize; // cache object size
12+
LOOKASIDE_LIST_EX lookasideField;
13+
} lookasidelist_cache_t;
14+
15+
lookasidelist_cache_t *lookasidelist_cache_create(size_t size);
16+
void lookasidelist_cache_destroy(lookasidelist_cache_t *pLookasidelist_cache);
17+
void* lookasidelist_cache_alloc(lookasidelist_cache_t *pLookasidelist_cache);
18+
void lookasidelist_cache_free(lookasidelist_cache_t *pLookasidelist_cache,
19+
void *buf);
20+
21+
#endif

module/os/windows/spl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ wdk_add_library(splkern
3737
spl-vnode.c
3838
spl-windows.c
3939
spl-xdr.c
40+
spl-lookasidelist.c
4041
${TMH_FILE_LIST}
4142
)
4243

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <sys/zfs_context.h>
2+
#include <sys/lookasidelist.h>
3+
4+
void *
5+
allocate_func(
6+
__in POOL_TYPE PoolType,
7+
__in SIZE_T NumberOfBytes,
8+
__in ULONG Tag,
9+
__inout PLOOKASIDE_LIST_EX Lookaside)
10+
{
11+
lookasidelist_cache_t *pLookasidelist_cache;
12+
pLookasidelist_cache = CONTAINING_RECORD(Lookaside,
13+
lookasidelist_cache_t, lookasideField);
14+
void *buf = osif_malloc(NumberOfBytes);
15+
ASSERT(buf != NULL);
16+
17+
if (buf != NULL) {
18+
atomic_inc_32(&pLookasidelist_cache->cache_active_allocations);
19+
// can also be calculated from the Lookaside list as below
20+
// Lookaside->L.TotalAllocates - Lookaside->L.TotalFrees
21+
}
22+
23+
return (buf);
24+
}
25+
26+
void free_func(
27+
__in PVOID Buffer,
28+
__inout PLOOKASIDE_LIST_EX Lookaside
29+
) {
30+
lookasidelist_cache_t *pLookasidelist_cache;
31+
pLookasidelist_cache = CONTAINING_RECORD(Lookaside,
32+
lookasidelist_cache_t, lookasideField);
33+
osif_free(Buffer, pLookasidelist_cache->cache_chunksize);
34+
atomic_dec_32(&pLookasidelist_cache->cache_active_allocations);
35+
}
36+
37+
lookasidelist_cache_t *
38+
lookasidelist_cache_create(size_t size)
39+
{
40+
lookasidelist_cache_t *pLookasidelist_cache;
41+
pLookasidelist_cache = ExAllocatePoolWithTag(NonPagedPool,
42+
sizeof (lookasidelist_cache_t), ZFS_LookAsideList_DRV_TAG);
43+
44+
if (pLookasidelist_cache != NULL) {
45+
NTSTATUS retval = ExInitializeLookasideListEx(
46+
&pLookasidelist_cache->lookasideField,
47+
allocate_func,
48+
free_func,
49+
NonPagedPoolNx,
50+
0,
51+
size,
52+
ZFS_LookAsideList_DRV_TAG,
53+
0);
54+
55+
ASSERT(retval == STATUS_SUCCESS);
56+
57+
if (retval == STATUS_SUCCESS) {
58+
pLookasidelist_cache->cache_chunksize = size;
59+
pLookasidelist_cache->cache_active_allocations = 0;
60+
} else {
61+
ExFreePoolWithTag(pLookasidelist_cache,
62+
ZFS_LookAsideList_DRV_TAG);
63+
pLookasidelist_cache = NULL;
64+
}
65+
}
66+
67+
return (pLookasidelist_cache);
68+
}
69+
70+
void
71+
lookasidelist_cache_destroy(lookasidelist_cache_t *pLookasidelist_cache)
72+
{
73+
if (pLookasidelist_cache != NULL) {
74+
ExFlushLookasideListEx(&pLookasidelist_cache->lookasideField);
75+
ExDeleteLookasideListEx(&pLookasidelist_cache->lookasideField);
76+
ExFreePoolWithTag(pLookasidelist_cache,
77+
ZFS_LookAsideList_DRV_TAG);
78+
}
79+
}
80+
81+
void *
82+
lookasidelist_cache_alloc(lookasidelist_cache_t *pLookasidelist_cache)
83+
{
84+
void *buf = ExAllocateFromLookasideListEx(
85+
&pLookasidelist_cache->lookasideField);
86+
ASSERT(buf != NULL);
87+
return (buf);
88+
}
89+
90+
void
91+
lookasidelist_cache_free(lookasidelist_cache_t *pLookasidelist_cache, void *buf)
92+
{
93+
ASSERT(buf != NULL);
94+
if (buf != NULL) {
95+
ExFreeToLookasideListEx(&pLookasidelist_cache->lookasideField,
96+
buf);
97+
}
98+
}

module/os/windows/zfs/abd_os.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <sys/zio.h>
3333
#include <sys/zfs_context.h>
3434
#include <sys/zfs_znode.h>
35+
#include <sys/lookasidelist.h>
3536

3637

3738
typedef struct abd_stats {
@@ -89,7 +90,7 @@ struct {
8990
*/
9091
size_t zfs_abd_chunk_size = 4096;
9192

92-
kmem_cache_t *abd_chunk_cache;
93+
lookasidelist_cache_t *abd_chunk_cache;
9394
static kstat_t *abd_ksp;
9495

9596

@@ -105,7 +106,7 @@ static char *abd_zero_buf = NULL;
105106
static void
106107
abd_free_chunk(void *c)
107108
{
108-
kmem_cache_free(abd_chunk_cache, c);
109+
lookasidelist_cache_free(abd_chunk_cache, c);
109110
}
110111

111112
static size_t
@@ -182,7 +183,7 @@ abd_alloc_chunks(abd_t *abd, size_t size)
182183
{
183184
size_t n = abd_chunkcnt_for_bytes(size);
184185
for (int i = 0; i < n; i++) {
185-
void *c = kmem_cache_alloc(abd_chunk_cache, KM_SLEEP);
186+
void *c = lookasidelist_cache_alloc(abd_chunk_cache);
186187
ABD_SCATTER(abd).abd_chunks[i] = c;
187188
}
188189
ABD_SCATTER(abd).abd_chunk_size = zfs_abd_chunk_size;
@@ -236,7 +237,7 @@ static void
236237
abd_alloc_zero_scatter(void)
237238
{
238239
size_t n = abd_chunkcnt_for_bytes(SPA_MAXBLOCKSIZE);
239-
abd_zero_buf = kmem_cache_alloc(abd_chunk_cache, KM_SLEEP);
240+
abd_zero_buf = lookasidelist_cache_alloc(abd_chunk_cache);
240241
bzero(abd_zero_buf, zfs_abd_chunk_size);
241242
abd_zero_scatter = abd_alloc_struct(SPA_MAXBLOCKSIZE);
242243

@@ -264,15 +265,13 @@ abd_free_zero_scatter(void)
264265

265266
abd_free_struct(abd_zero_scatter);
266267
abd_zero_scatter = NULL;
267-
kmem_cache_free(abd_chunk_cache, abd_zero_buf);
268+
lookasidelist_cache_free(abd_chunk_cache, abd_zero_buf);
268269
}
269270

270271
void
271272
abd_init(void)
272273
{
273-
abd_chunk_cache = kmem_cache_create("abd_chunk", zfs_abd_chunk_size,
274-
MIN(PAGE_SIZE, 4096),
275-
NULL, NULL, NULL, NULL, abd_arena, KMC_NOTOUCH);
274+
abd_chunk_cache = lookasidelist_cache_create(zfs_abd_chunk_size);
276275

277276
abd_ksp = kstat_create("zfs", 0, "abdstats", "misc", KSTAT_TYPE_NAMED,
278277
sizeof (abd_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
@@ -294,7 +293,7 @@ abd_fini(void)
294293
abd_ksp = NULL;
295294
}
296295

297-
kmem_cache_destroy(abd_chunk_cache);
296+
lookasidelist_cache_destroy(abd_chunk_cache);
298297
abd_chunk_cache = NULL;
299298
}
300299

@@ -487,5 +486,5 @@ abd_iter_unmap(struct abd_iter *aiter)
487486
void
488487
abd_cache_reap_now(void)
489488
{
490-
kmem_cache_reap_now(abd_chunk_cache);
489+
// do nothing
491490
}

module/os/windows/zfs/arc_os.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ arc_reclaim_thread(void *unused)
253253
* it is worth reaping the abd_chunk_cache
254254
*/
255255
if (d_adj >= 64LL*1024LL*1024LL) {
256-
extern kmem_cache_t *abd_chunk_cache;
257-
kmem_cache_reap_now(abd_chunk_cache);
256+
abd_cache_reap_now();
258257
}
259258

260259
free_memory = post_adjust_free_memory;

0 commit comments

Comments
 (0)