Skip to contentSkip to conversation

btrfs: compression: allocate buckets with workspace

PATCH Superseded Rosen Penev 5 participants 5 messages Jun 29 – Jun 30 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: compression: allocate buckets with workspace

Convert 3 allocations into one. Simplifies code slightly.

---
 fs/btrfs/compression.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
fs/btrfs/compression.c +5 -13
index ffb6b52863a7..da6749ff5924 100644
@@ -650,11 +650,11 @@ struct heuristic_ws {
 	/* Partial copy of input data */
 	u8 *sample;
 	u32 sample_size;
-	/* Buckets store counters for each byte value */
-	struct bucket_item *bucket;
 	/* Sorting buffer */
 	struct bucket_item *bucket_b;
 	struct list_head list;
+	/* Buckets store counters for each byte value */
+	struct bucket_item bucket[];
 };
 
 static void free_heuristic_ws(struct list_head *ws)
@@ -664,8 +664,6 @@ static void free_heuristic_ws(struct list_head *ws)
 	workspace = list_entry(ws, struct heuristic_ws, list);
 
 	kvfree(workspace->sample);
-	kfree(workspace->bucket);
-	kfree(workspace->bucket_b);
 	kfree(workspace);
 }
 
@@ -673,22 +671,16 @@ static struct list_head *alloc_heuristic_ws(struct btrfs_fs_info *fs_info)
 {
 	struct heuristic_ws *ws;
 
-	ws = kzalloc_obj(*ws);
+	ws = kzalloc(struct_size(ws, bucket, BUCKET_SIZE * 2), GFP_KERNEL);
 	if (!ws)
 		return ERR_PTR(-ENOMEM);
 
+	ws->bucket_b = ws->bucket + BUCKET_SIZE;
+
 	ws->sample = kvmalloc(MAX_SAMPLE_SIZE, GFP_KERNEL);
 	if (!ws->sample)
 		goto fail;
 
-	ws->bucket = kzalloc_objs(*ws->bucket, BUCKET_SIZE);
-	if (!ws->bucket)
-		goto fail;
-
-	ws->bucket_b = kzalloc_objs(*ws->bucket_b, BUCKET_SIZE);
-	if (!ws->bucket_b)
-		goto fail;
-
 	INIT_LIST_HEAD(&ws->list);
 	return &ws->list;
 fail:
Signature · 1 line
2.54.0
#1 lore raw
Quoted 35 lines · Rosen Penev

Convert 3 allocations into one. Simplifies code slightly.

---
  fs/btrfs/compression.c | 18 +++++-------------
  1 file changed, 5 insertions(+), 13 deletions(-)
fs/btrfs/compression.c +2 -4
index ffb6b52863a7..da6749ff5924 100644
@@ -650,11 +650,11 @@ struct heuristic_ws {
  	/* Partial copy of input data */
  	u8 *sample;
  	u32 sample_size;
-	/* Buckets store counters for each byte value */
-	struct bucket_item *bucket;
  	/* Sorting buffer */
  	struct bucket_item *bucket_b;
  	struct list_head list;
+	/* Buckets store counters for each byte value */
+	struct bucket_item bucket[];
  };
 
  static void free_heuristic_ws(struct list_head *ws)
@@ -664,8 +664,6 @@ static void free_heuristic_ws(struct list_head *ws)
  	workspace = list_entry(ws, struct heuristic_ws, list);
 
  	kvfree(workspace->sample);
-	kfree(workspace->bucket);
-	kfree(workspace->bucket_b);
  	kfree(workspace);
  }
 

fs/btrfs/compression.c static struct list_head *alloc_heuristic_ws(struct btrfs_fs_info *fs_info) · line 674

Quoted 5 lines
fs/btrfs/compression.c +1 -1
@@ -673,22 +671,16 @@ static struct list_head *alloc_heuristic_ws(struct btrfs_fs_info *fs_info)
  {
  	struct heuristic_ws *ws;
 
-	ws = kzalloc_obj(*ws);
+	ws = kzalloc(struct_size(ws, bucket, BUCKET_SIZE * 2), GFP_KERNEL);

It seems that size is fixed and known at compile time, and since we want to inline the buckets in the struct, I think we can have something like this to save one pointer:

struct heuristic_ws {
	...
	struct bucket_item bucket[BUCKET_SIZE];
	struct bucket_item bucket_b[BUCKET_SIZE];
}

However, each bucket array takes exactly 1024B memory. Currently we have a 48B allocation for the struct it self, 2 * 1024B allocation for bucket array, and 8192B allocation for sample. With these 2 arrays inlined, the struct will need 32 + 2048B allocation if my calculation is correct, which will goes into kmalloc-4k. Seems not good.

Thanks,
Sun YangKai

Quoted 20 lines
fs/btrfs/compression.c +2 -8
  	if (!ws)
  		return ERR_PTR(-ENOMEM);
 
+	ws->bucket_b = ws->bucket + BUCKET_SIZE;
+
  	ws->sample = kvmalloc(MAX_SAMPLE_SIZE, GFP_KERNEL);
  	if (!ws->sample)
  		goto fail;
 
-	ws->bucket = kzalloc_objs(*ws->bucket, BUCKET_SIZE);
-	if (!ws->bucket)
-		goto fail;
-
-	ws->bucket_b = kzalloc_objs(*ws->bucket_b, BUCKET_SIZE);
-	if (!ws->bucket_b)
-		goto fail;
-
  	INIT_LIST_HEAD(&ws->list);
  	return &ws->list;
  fail:
#2 lore raw
Quoted 11 lines · Sun YangKai

fs/btrfs/compression.c static struct list_head *alloc_heuristic_ws(struct btrfs_fs_info *fs_info) · line 674

Quoted 2 lines
fs/btrfs/compression.c +1 -1
-	ws = kzalloc_obj(*ws);
+	ws = kzalloc(struct_size(ws, bucket, BUCKET_SIZE * 2), GFP_KERNEL);

It seems that size is fixed and known at compile time, and since we want to inline the buckets in the struct, I think we can have something like this to save one pointer:

struct heuristic_ws {
	...
	struct bucket_item bucket[BUCKET_SIZE];
	struct bucket_item bucket_b[BUCKET_SIZE];

I like this idea, given that we know the size at compile time.

Quoted 7 lines

}

However, each bucket array takes exactly 1024B memory. Currently we have a 48B allocation for the struct it self, 2 * 1024B allocation for bucket array, and 8192B allocation for sample. With these 2 arrays inlined, the struct will need 32 + 2048B allocation if my calculation is correct, which will goes into kmalloc-4k. Seems not good.

SLUB merges same/similar sized structures for the named caches, I'm not sure if this also works for the generic slabs. We can add the kmem cache and so we can utilize the merging.

The sharing can be seen in /sys/kernel/slab, "ls -l | grep <SIZE>". The size is rounded up to hw cacheline, so for the heuristic workspace it'll end up in size 2112. I see at least one existing named slab on my system, 'dmaengine-unmap-256'. This may not matter, but a named cache can fix the slack space in the 4k slab.

#3 lore raw
Quoted 23 lines · David Sterba
Quoted 11 lines · Sun YangKai

fs/btrfs/compression.c static struct list_head *alloc_heuristic_ws(struct btrfs_fs_info *fs_info) · line 674

Quoted 2 lines
fs/btrfs/compression.c +1 -1
-	ws = kzalloc_obj(*ws);
+	ws = kzalloc(struct_size(ws, bucket, BUCKET_SIZE * 2), GFP_KERNEL);

It seems that size is fixed and known at compile time, and since we want to inline the buckets in the struct, I think we can have something like this to save one pointer:

struct heuristic_ws {
	...
	struct bucket_item bucket[BUCKET_SIZE];
	struct bucket_item bucket_b[BUCKET_SIZE];

I like this idea, given that we know the size at compile time.

Quoted 7 lines

}

However, each bucket array takes exactly 1024B memory. Currently we have a 48B allocation for the struct it self, 2 * 1024B allocation for bucket array, and 8192B allocation for sample. With these 2 arrays inlined, the struct will need 32 + 2048B allocation if my calculation is correct, which will goes into kmalloc-4k. Seems not good.

Quoted 3 lines

SLUB merges same/similar sized structures for the named caches, I'm not sure if this also works for the generic slabs. We can add the kmem cache and so we can utilize the merging.

Oh, I've missed that. Then things seems good for me.

Quoted 5 lines

The sharing can be seen in /sys/kernel/slab, "ls -l | grep <SIZE>". The size is rounded up to hw cacheline, so for the heuristic workspace it'll end up in size 2112. I see at least one existing named slab on my system, 'dmaengine-unmap-256'. This may not matter, but a named cache can fix the slack space in the 4k slab.

And I have

❯ sudo grep 2112 /proc/slabinfo
sighand_cache 593 1335 2112 15 8

on my system :)

Thanks,
Sun YangKai

#4 lore raw
In reply to Rosen Penev
Quoted 35 lines · Rosen Penev

Convert 3 allocations into one. Simplifies code slightly.

---
  fs/btrfs/compression.c | 18 +++++-------------
  1 file changed, 5 insertions(+), 13 deletions(-)
fs/btrfs/compression.c +2 -4
index ffb6b52863a7..da6749ff5924 100644
@@ -650,11 +650,11 @@ struct heuristic_ws {
  	/* Partial copy of input data */
  	u8 *sample;
  	u32 sample_size;
-	/* Buckets store counters for each byte value */
-	struct bucket_item *bucket;
  	/* Sorting buffer */
  	struct bucket_item *bucket_b;
  	struct list_head list;
+	/* Buckets store counters for each byte value */
+	struct bucket_item bucket[];
  };
 
  static void free_heuristic_ws(struct list_head *ws)
@@ -664,8 +664,6 @@ static void free_heuristic_ws(struct list_head *ws)
  	workspace = list_entry(ws, struct heuristic_ws, list);
 
  	kvfree(workspace->sample);
-	kfree(workspace->bucket);
-	kfree(workspace->bucket_b);
  	kfree(workspace);
  }
 

fs/btrfs/compression.c static struct list_head *alloc_heuristic_ws(struct btrfs_fs_info *fs_info) · line 679

Quoted 10 lines
fs/btrfs/compression.c +3 -1
@@ -673,22 +671,16 @@ static struct list_head *alloc_heuristic_ws(struct btrfs_fs_info *fs_info)
  {
  	struct heuristic_ws *ws;
 
-	ws = kzalloc_obj(*ws);
+	ws = kzalloc(struct_size(ws, bucket, BUCKET_SIZE * 2), GFP_KERNEL);
  	if (!ws)
  		return ERR_PTR(-ENOMEM);
 
+	ws->bucket_b = ws->bucket + BUCKET_SIZE;
+

This barely improves any readability.

We have two different bucket pointers, one is a variable sized member, one (bucket_b) is a pointer.

Not a fan of this at all.

Quoted 15 lines
fs/btrfs/compression.c +0 -8
  	ws->sample = kvmalloc(MAX_SAMPLE_SIZE, GFP_KERNEL);
  	if (!ws->sample)
  		goto fail;
 
-	ws->bucket = kzalloc_objs(*ws->bucket, BUCKET_SIZE);
-	if (!ws->bucket)
-		goto fail;
-
-	ws->bucket_b = kzalloc_objs(*ws->bucket_b, BUCKET_SIZE);
-	if (!ws->bucket_b)
-		goto fail;
-
  	INIT_LIST_HEAD(&ws->list);
  	return &ws->list;
  fail: