Skip to contentSkip to conversation

f2fs:compress: introduce compress private data slab cache

RFC PATCH In review Fengnan Chang 2 participants 3 messages Oct 28, 2021 – Nov 15, 2021 1 message not in the archive F2FS file system

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 f2fs:compress: introduce compress private data slab cache

Add "f2fs_lzo_compress_private" and "f2fs_lz4_compress_private" slab cache, to speed up memory allocation when init compress ctx. No slab cache is added to zstd as the private data for zstd is related to mount option, and too big.

---
 fs/f2fs/compress.c | 45 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 40 insertions(+), 5 deletions(-)
fs/f2fs/compress.c +40 -5
index 7588e4e817b8..4a8a4858d358 100644
@@ -171,10 +171,11 @@ void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
 }
 
 #ifdef CONFIG_F2FS_FS_LZO
+static struct kmem_cache *lzo_compress_private_slab;
 static int lzo_init_compress_ctx(struct compress_ctx *cc)
 {
-	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
-				LZO1X_MEM_COMPRESS, GFP_NOFS);
+	cc->private = f2fs_kmem_cache_alloc(lzo_compress_private_slab,
+			GFP_F2FS_ZERO, false, F2FS_I_SB(cc->inode));
 	if (!cc->private)
 		return -ENOMEM;
 
@@ -184,7 +185,7 @@ static int lzo_init_compress_ctx(struct compress_ctx *cc)
 
 static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
 {
-	kvfree(cc->private);
+	kmem_cache_free(lzo_compress_private_slab, cc->private);
 	cc->private = NULL;
 }
 
@@ -234,6 +235,7 @@ static const struct f2fs_compress_ops f2fs_lzo_ops = {
 #endif
 
 #ifdef CONFIG_F2FS_FS_LZ4
+static struct kmem_cache *lz4_compress_private_slab;
 static int lz4_init_compress_ctx(struct compress_ctx *cc)
 {
 	unsigned int size = LZ4_MEM_COMPRESS;
@@ -243,7 +245,8 @@ static int lz4_init_compress_ctx(struct compress_ctx *cc)
 		size = LZ4HC_MEM_COMPRESS;
 #endif
 
-	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS);
+	cc->private = f2fs_kmem_cache_alloc(lz4_compress_private_slab,
+			GFP_F2FS_ZERO, false, F2FS_I_SB(cc->inode));
 	if (!cc->private)
 		return -ENOMEM;
 
@@ -258,7 +261,7 @@ static int lz4_init_compress_ctx(struct compress_ctx *cc)
 
 static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
 {
-	kvfree(cc->private);
+	kmem_cache_free(lz4_compress_private_slab, cc->private);
 	cc->private = NULL;
 }
 
@@ -1944,6 +1947,32 @@ void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
 {
 	kmem_cache_destroy(sbi->page_array_slab);
 }
+static int __init f2fs_init_compress_private_cache(void)
+{
+#ifdef CONFIG_F2FS_FS_LZ4
+	lz4_compress_private_slab = f2fs_kmem_cache_create("f2fs_lz4_compress_private",
+					LZ4_MEM_COMPRESS);
+	if (!lz4_compress_private_slab)
+		return -ENOMEM;
+#endif
+#ifdef CONFIG_F2FS_FS_LZO
+	lzo_compress_private_slab = f2fs_kmem_cache_create("f2fs_lzo_compress_private",
+					LZO1X_MEM_COMPRESS);
+	if (!lzo_compress_private_slab)
+		return -ENOMEM;
+#endif
+	return 0;
+}
+
+static void f2fs_destroy_compress_private_cache(void)
+{
+#ifdef CONFIG_F2FS_FS_LZ4
+	kmem_cache_destroy(lz4_compress_private_slab);
+#endif
+#ifdef CONFIG_F2FS_FS_LZO
+	kmem_cache_destroy(lzo_compress_private_slab);
+#endif
+}
 
 static int __init f2fs_init_cic_cache(void)
 {
@@ -1983,7 +2012,12 @@ int __init f2fs_init_compress_cache(void)
 	err = f2fs_init_dic_cache();
 	if (err)
 		goto free_cic;
+	err = f2fs_init_compress_private_cache();
+	if (err)
+		goto free_dic;
 	return 0;
+free_dic:
+	f2fs_destroy_dic_cache();
 free_cic:
 	f2fs_destroy_cic_cache();
 out:
@@ -1994,4 +2028,5 @@ void f2fs_destroy_compress_cache(void)
 {
 	f2fs_destroy_dic_cache();
 	f2fs_destroy_cic_cache();
+	f2fs_destroy_compress_private_cache();
 }
Signature · 8 lines
2.32.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
maintainer
#1 lore raw
Quoted 46 lines · Fengnan Chang

Add "f2fs_lzo_compress_private" and "f2fs_lz4_compress_private" slab cache, to speed up memory allocation when init compress ctx. No slab cache is added to zstd as the private data for zstd is related to mount option, and too big.

---
  fs/f2fs/compress.c | 45 ++++++++++++++++++++++++++++++++++++++++-----
  1 file changed, 40 insertions(+), 5 deletions(-)
fs/f2fs/compress.c +5 -3
index 7588e4e817b8..4a8a4858d358 100644
@@ -171,10 +171,11 @@ void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
  }
 
  #ifdef CONFIG_F2FS_FS_LZO
+static struct kmem_cache *lzo_compress_private_slab;
  static int lzo_init_compress_ctx(struct compress_ctx *cc)
  {
-	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
-				LZO1X_MEM_COMPRESS, GFP_NOFS);
+	cc->private = f2fs_kmem_cache_alloc(lzo_compress_private_slab,
+			GFP_F2FS_ZERO, false, F2FS_I_SB(cc->inode));
  	if (!cc->private)
  		return -ENOMEM;
 
@@ -184,7 +185,7 @@ static int lzo_init_compress_ctx(struct compress_ctx *cc)
 
  static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
  {
-	kvfree(cc->private);
+	kmem_cache_free(lzo_compress_private_slab, cc->private);
  	cc->private = NULL;
  }
 
@@ -234,6 +235,7 @@ static const struct f2fs_compress_ops f2fs_lzo_ops = {
  #endif
 
  #ifdef CONFIG_F2FS_FS_LZ4
+static struct kmem_cache *lz4_compress_private_slab;
  static int lz4_init_compress_ctx(struct compress_ctx *cc)
  {
  	unsigned int size = LZ4_MEM_COMPRESS;

fs/f2fs/compress.c static int lz4_init_compress_ctx(struct compress_ctx *cc) · line 246

Quoted 4 lines
fs/f2fs/compress.c +0 -1
@@ -243,7 +245,8 @@ static int lz4_init_compress_ctx(struct compress_ctx *cc)
  		size = LZ4HC_MEM_COMPRESS;
  #endif
 
-	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS);

The size could be LZ4HC_MEM_COMPRESS rather than LZ4_MEM_COMPRESS if we enable lz4hc algorithm, so they should never share the same lz4_compress_private_slab slab cache.

Other concern is I'm not sure whether there is any side-effect if we introduce slab cache which has such large object size...

Thanks,

Quoted 62 lines
fs/f2fs/compress.c +34 -1
+	cc->private = f2fs_kmem_cache_alloc(lz4_compress_private_slab,
+			GFP_F2FS_ZERO, false, F2FS_I_SB(cc->inode));
  	if (!cc->private)
  		return -ENOMEM;
 
@@ -258,7 +261,7 @@ static int lz4_init_compress_ctx(struct compress_ctx *cc)
 
  static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
  {
-	kvfree(cc->private);
+	kmem_cache_free(lz4_compress_private_slab, cc->private);
  	cc->private = NULL;
  }
 
@@ -1944,6 +1947,32 @@ void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
  {
  	kmem_cache_destroy(sbi->page_array_slab);
  }
+static int __init f2fs_init_compress_private_cache(void)
+{
+#ifdef CONFIG_F2FS_FS_LZ4
+	lz4_compress_private_slab = f2fs_kmem_cache_create("f2fs_lz4_compress_private",
+					LZ4_MEM_COMPRESS);
+	if (!lz4_compress_private_slab)
+		return -ENOMEM;
+#endif
+#ifdef CONFIG_F2FS_FS_LZO
+	lzo_compress_private_slab = f2fs_kmem_cache_create("f2fs_lzo_compress_private",
+					LZO1X_MEM_COMPRESS);
+	if (!lzo_compress_private_slab)
+		return -ENOMEM;
+#endif
+	return 0;
+}
+
+static void f2fs_destroy_compress_private_cache(void)
+{
+#ifdef CONFIG_F2FS_FS_LZ4
+	kmem_cache_destroy(lz4_compress_private_slab);
+#endif
+#ifdef CONFIG_F2FS_FS_LZO
+	kmem_cache_destroy(lzo_compress_private_slab);
+#endif
+}
 
  static int __init f2fs_init_cic_cache(void)
  {
@@ -1983,7 +2012,12 @@ int __init f2fs_init_compress_cache(void)
  	err = f2fs_init_dic_cache();
  	if (err)
  		goto free_cic;
+	err = f2fs_init_compress_private_cache();
+	if (err)
+		goto free_dic;
  	return 0;
+free_dic:
+	f2fs_destroy_dic_cache();
  free_cic:
  	f2fs_destroy_cic_cache();
  out:

fs/f2fs/compress.c void f2fs_destroy_compress_cache(void) · line 2032

Quoted 5 lines
fs/f2fs/compress.c +1 -0
@@ -1994,4 +2028,5 @@ void f2fs_destroy_compress_cache(void)
  {
  	f2fs_destroy_dic_cache();
  	f2fs_destroy_cic_cache();
+	f2fs_destroy_compress_private_cache();
  }

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

A message that is not in the archive (sent off-list or not yet received): AIIAWwDBE7sNmxjtkrk7dqq6.9.1636785352506.Hmail.changfengnan@vivo.com

author
#3 lore raw
Quoted 66 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Chao Yu
Sent: Saturday, November 13, 2021 2:36 PM
To: 常凤楠 <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH] f2fs:compress: introduce compress private data slab cache

Quoted 8 lines · Fengnan Chang

Add "f2fs_lzo_compress_private" and "f2fs_lz4_compress_private" slab cache, to speed up memory allocation when init compress ctx. No slab cache is added to zstd as the private data for zstd is related to mount option, and too big.

---
  fs/f2fs/compress.c | 45

++++++++++++++++++++++++++++++++++++++++-----

fs/f2fs/compress.c void f2fs_compress_ctx_add_page(struct

Quoted 7 lines
  1 file changed, 40 insertions(+), 5 deletions(-)
fs/f2fs/compress.c index renamed from fs/f2fs/compress.c +0 -0

7588e4e817b8..4a8a4858d358 100644

fs/f2fs/compress.c +0 -0
@@ -171,10 +171,11 @@ void f2fs_compress_ctx_add_page(struct

compress_ctx *cc, struct page *page)

fs/f2fs/compress.c static const struct f2fs_compress_ops

Quoted 24 lines
fs/f2fs/compress.c +4 -3
  }
 
  #ifdef CONFIG_F2FS_FS_LZO
+static struct kmem_cache *lzo_compress_private_slab;
  static int lzo_init_compress_ctx(struct compress_ctx *cc)
  {
-	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
-				LZO1X_MEM_COMPRESS, GFP_NOFS);
+	cc->private = f2fs_kmem_cache_alloc(lzo_compress_private_slab,
+			GFP_F2FS_ZERO, false, F2FS_I_SB(cc->inode));
  	if (!cc->private)
  		return -ENOMEM;
 
@@ -184,7 +185,7 @@ static int lzo_init_compress_ctx(struct
 compress_ctx *cc)
 
  static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
  {
-	kvfree(cc->private);
+	kmem_cache_free(lzo_compress_private_slab, cc->private);
  	cc->private = NULL;
  }
@@ -234,6 +235,7 @@ static const struct f2fs_compress_ops

f2fs_lzo_ops = {

fs/f2fs/compress.c static const struct f2fs_compress_ops f2fs_lzo_ops = {

Quoted 7 lines
fs/f2fs/compress.c +1 -0
  #endif
 
  #ifdef CONFIG_F2FS_FS_LZ4
+static struct kmem_cache *lz4_compress_private_slab;
  static int lz4_init_compress_ctx(struct compress_ctx *cc)
  {
  	unsigned int size = LZ4_MEM_COMPRESS; @@ -243,7 +245,8 @@

static

Quoted 4 lines

int lz4_init_compress_ctx(struct compress_ctx *cc)
size = LZ4HC_MEM_COMPRESS;
#endif

Quoted 8 lines

fs/f2fs/compress.c static int lz4_init_compress_ctx(struct compress_ctx *cc) · line 246

Quoted 1 line
fs/f2fs/compress.c +0 -1
-	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS);

The size could be LZ4HC_MEM_COMPRESS rather than LZ4_MEM_COMPRESS if we enable lz4hc algorithm, so they should never share the same lz4_compress_private_slab slab cache.

Other concern is I'm not sure whether there is any side-effect if we introduce slab cache which has such large object size...

Yes, I have this concern too, but in my test, with slab cache indeed be faster...

Quoted 66 lines

Thanks,

fs/f2fs/compress.c void f2fs_destroy_page_array_cache(struct

Quoted 16 lines
fs/f2fs/compress.c +3 -1
+	cc->private = f2fs_kmem_cache_alloc(lz4_compress_private_slab,
+			GFP_F2FS_ZERO, false, F2FS_I_SB(cc->inode));
  	if (!cc->private)
  		return -ENOMEM;
@@ -258,7 +261,7 @@ static int lz4_init_compress_ctx(struct
 compress_ctx *cc)
 
  static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
  {
-	kvfree(cc->private);
+	kmem_cache_free(lz4_compress_private_slab, cc->private);
  	cc->private = NULL;
  }
@@ -1944,6 +1947,32 @@ void f2fs_destroy_page_array_cache(struct

f2fs_sb_info *sbi)

fs/f2fs/compress.c void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)

Quoted 7 lines
fs/f2fs/compress.c +4 -0
  {
  	kmem_cache_destroy(sbi->page_array_slab);
  }
+static int __init f2fs_init_compress_private_cache(void)
+{
+#ifdef CONFIG_F2FS_FS_LZ4
+	lz4_compress_private_slab =

f2fs_kmem_cache_create("f2fs_lz4_compress_private",

fs/f2fs/compress.c void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)

Quoted 6 lines
fs/f2fs/compress.c +6 -0
+					LZ4_MEM_COMPRESS);
+	if (!lz4_compress_private_slab)
+		return -ENOMEM;
+#endif
+#ifdef CONFIG_F2FS_FS_LZO
+	lzo_compress_private_slab =

f2fs_kmem_cache_create("f2fs_lzo_compress_private",

Quoted 31 lines
fs/f2fs/compress.c +21 -0
+					LZO1X_MEM_COMPRESS);
+	if (!lzo_compress_private_slab)
+		return -ENOMEM;
+#endif
+	return 0;
+}
+
+static void f2fs_destroy_compress_private_cache(void)
+{
+#ifdef CONFIG_F2FS_FS_LZ4
+	kmem_cache_destroy(lz4_compress_private_slab);
+#endif
+#ifdef CONFIG_F2FS_FS_LZO
+	kmem_cache_destroy(lzo_compress_private_slab);
+#endif
+}
 
  static int __init f2fs_init_cic_cache(void)
  {
@@ -1983,7 +2012,12 @@ int __init f2fs_init_compress_cache(void)
  	err = f2fs_init_dic_cache();
  	if (err)
  		goto free_cic;
+	err = f2fs_init_compress_private_cache();
+	if (err)
+		goto free_dic;
  	return 0;
+free_dic:
+	f2fs_destroy_dic_cache();
  free_cic:
  	f2fs_destroy_cic_cache();
Quoted 8 lines

fs/f2fs/compress.c void f2fs_destroy_compress_cache(void) · line 2032

Quoted 8 lines
fs/f2fs/compress.c +1 -0
  out:
@@ -1994,4 +2028,5 @@ void f2fs_destroy_compress_cache(void)
  {
  	f2fs_destroy_dic_cache();
  	f2fs_destroy_cic_cache();
+	f2fs_destroy_compress_private_cache();
  }

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel