linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx
@ 2025-05-13  5:57 Chao Yu
  2025-05-13  5:57 ` [PATCH 2/2] f2fs: introduce FAULT_VMALLOC Chao Yu
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Chao Yu @ 2025-05-13  5:57 UTC (permalink / raw)
  To: jaegeuk; +Cc: linux-f2fs-devel, linux-kernel, Chao Yu

.init_{,de}compress_ctx uses kvmalloc() to alloc memory, it will try
to allocate physically continuous page first, it may cause more memory
allocation pressure, let's use vmalloc instead to mitigate it.

[Test]
cd /data/local/tmp
touch file
f2fs_io setflags compression file
f2fs_io getflags file
for i in $(seq 1 10); do sync; echo 3 > /proc/sys/vm/drop_caches;\
time f2fs_io write 512 0 4096 zero osync file; truncate -s 0 file;\
done

[Result]
Before		After		Delta
21.243		21.694		-2.12%

For compression, we recommend to use ioctl to compress file data in
background for workaround.

For decompression, only zstd will be affected.

Signed-off-by: Chao Yu <chao@kernel.org>
---
 fs/f2fs/compress.c | 23 ++++++++++-------------
 fs/f2fs/f2fs.h     |  5 +++++
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
index 1e62fdffda07..37d18e2a3327 100644
--- a/fs/f2fs/compress.c
+++ b/fs/f2fs/compress.c
@@ -180,8 +180,7 @@ void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct folio *folio)
 #ifdef CONFIG_F2FS_FS_LZO
 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_vmalloc(LZO1X_MEM_COMPRESS);
 	if (!cc->private)
 		return -ENOMEM;
 
@@ -191,7 +190,7 @@ static int lzo_init_compress_ctx(struct compress_ctx *cc)
 
 static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
 {
-	kvfree(cc->private);
+	vfree(cc->private);
 	cc->private = NULL;
 }
 
@@ -248,7 +247,7 @@ 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_vmalloc(size);
 	if (!cc->private)
 		return -ENOMEM;
 
@@ -263,7 +262,7 @@ static int lz4_init_compress_ctx(struct compress_ctx *cc)
 
 static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
 {
-	kvfree(cc->private);
+	vfree(cc->private);
 	cc->private = NULL;
 }
 
@@ -344,8 +343,7 @@ static int zstd_init_compress_ctx(struct compress_ctx *cc)
 	params = zstd_get_params(level, cc->rlen);
 	workspace_size = zstd_cstream_workspace_bound(&params.cParams);
 
-	workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
-					workspace_size, GFP_NOFS);
+	workspace = f2fs_vmalloc(workspace_size);
 	if (!workspace)
 		return -ENOMEM;
 
@@ -353,7 +351,7 @@ static int zstd_init_compress_ctx(struct compress_ctx *cc)
 	if (!stream) {
 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
 				"%s zstd_init_cstream failed", __func__);
-		kvfree(workspace);
+		vfree(workspace);
 		return -EIO;
 	}
 
@@ -366,7 +364,7 @@ static int zstd_init_compress_ctx(struct compress_ctx *cc)
 
 static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
 {
-	kvfree(cc->private);
+	vfree(cc->private);
 	cc->private = NULL;
 	cc->private2 = NULL;
 }
@@ -425,8 +423,7 @@ static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
 
 	workspace_size = zstd_dstream_workspace_bound(max_window_size);
 
-	workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
-					workspace_size, GFP_NOFS);
+	workspace = f2fs_vmalloc(workspace_size);
 	if (!workspace)
 		return -ENOMEM;
 
@@ -434,7 +431,7 @@ static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
 	if (!stream) {
 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
 				"%s zstd_init_dstream failed", __func__);
-		kvfree(workspace);
+		vfree(workspace);
 		return -EIO;
 	}
 
@@ -446,7 +443,7 @@ static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
 
 static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
 {
-	kvfree(dic->private);
+	vfree(dic->private);
 	dic->private = NULL;
 	dic->private2 = NULL;
 }
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 9432fd15766a..6e9615b5cdc3 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -3540,6 +3540,11 @@ static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
 }
 
+static inline void *f2fs_vmalloc(size_t size)
+{
+	return vmalloc(size);
+}
+
 static inline int get_extra_isize(struct inode *inode)
 {
 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] f2fs: introduce FAULT_VMALLOC
  2025-05-13  5:57 [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx Chao Yu
@ 2025-05-13  5:57 ` Chao Yu
  2025-05-13  6:01 ` [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx Christoph Hellwig
  2025-05-14 15:40 ` [f2fs-dev] [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{, de}compress_ctx patchwork-bot+f2fs
  2 siblings, 0 replies; 5+ messages in thread
From: Chao Yu @ 2025-05-13  5:57 UTC (permalink / raw)
  To: jaegeuk; +Cc: linux-f2fs-devel, linux-kernel, Chao Yu

Introduce a new fault type FAULT_VMALLOC to simulate no memory error in
f2fs_vmalloc().

Signed-off-by: Chao Yu <chao@kernel.org>
---
 Documentation/ABI/testing/sysfs-fs-f2fs | 1 +
 Documentation/filesystems/f2fs.rst      | 1 +
 fs/f2fs/compress.c                      | 9 +++++----
 fs/f2fs/f2fs.h                          | 6 +++++-
 fs/f2fs/super.c                         | 1 +
 5 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-fs-f2fs b/Documentation/ABI/testing/sysfs-fs-f2fs
index e060798f9fc1..bf03263b9f46 100644
--- a/Documentation/ABI/testing/sysfs-fs-f2fs
+++ b/Documentation/ABI/testing/sysfs-fs-f2fs
@@ -736,6 +736,7 @@ Description:	Support configuring fault injection type, should be
 		FAULT_NO_SEGMENT                 0x00100000
 		FAULT_INCONSISTENT_FOOTER        0x00200000
 		FAULT_TIMEOUT                    0x00400000 (1000ms)
+		FAULT_VMALLOC                    0x00800000
 		===========================      ==========
 
 What:		/sys/fs/f2fs/<disk>/discard_io_aware_gran
diff --git a/Documentation/filesystems/f2fs.rst b/Documentation/filesystems/f2fs.rst
index 724fc5e2889a..440e4ae74e44 100644
--- a/Documentation/filesystems/f2fs.rst
+++ b/Documentation/filesystems/f2fs.rst
@@ -208,6 +208,7 @@ fault_type=%d		 Support configuring fault injection type, should be
 			 FAULT_NO_SEGMENT                 0x00100000
 			 FAULT_INCONSISTENT_FOOTER        0x00200000
 			 FAULT_TIMEOUT                    0x00400000 (1000ms)
+			 FAULT_VMALLOC                    0x00800000
 			 ===========================      ==========
 mode=%s			 Control block allocation mode which supports "adaptive"
 			 and "lfs". In "lfs" mode, there should be no random
diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
index 37d18e2a3327..2a9f7b68a6c6 100644
--- a/fs/f2fs/compress.c
+++ b/fs/f2fs/compress.c
@@ -180,7 +180,8 @@ void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct folio *folio)
 #ifdef CONFIG_F2FS_FS_LZO
 static int lzo_init_compress_ctx(struct compress_ctx *cc)
 {
-	cc->private = f2fs_vmalloc(LZO1X_MEM_COMPRESS);
+	cc->private = f2fs_vmalloc(F2FS_I_SB(cc->inode),
+					LZO1X_MEM_COMPRESS);
 	if (!cc->private)
 		return -ENOMEM;
 
@@ -247,7 +248,7 @@ static int lz4_init_compress_ctx(struct compress_ctx *cc)
 		size = LZ4HC_MEM_COMPRESS;
 #endif
 
-	cc->private = f2fs_vmalloc(size);
+	cc->private = f2fs_vmalloc(F2FS_I_SB(cc->inode), size);
 	if (!cc->private)
 		return -ENOMEM;
 
@@ -343,7 +344,7 @@ static int zstd_init_compress_ctx(struct compress_ctx *cc)
 	params = zstd_get_params(level, cc->rlen);
 	workspace_size = zstd_cstream_workspace_bound(&params.cParams);
 
-	workspace = f2fs_vmalloc(workspace_size);
+	workspace = f2fs_vmalloc(F2FS_I_SB(cc->inode), workspace_size);
 	if (!workspace)
 		return -ENOMEM;
 
@@ -423,7 +424,7 @@ static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
 
 	workspace_size = zstd_dstream_workspace_bound(max_window_size);
 
-	workspace = f2fs_vmalloc(workspace_size);
+	workspace = f2fs_vmalloc(F2FS_I_SB(dic->inode), workspace_size);
 	if (!workspace)
 		return -ENOMEM;
 
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 6e9615b5cdc3..bf6056aa09d8 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -64,6 +64,7 @@ enum {
 	FAULT_NO_SEGMENT,
 	FAULT_INCONSISTENT_FOOTER,
 	FAULT_TIMEOUT,
+	FAULT_VMALLOC,
 	FAULT_MAX,
 };
 
@@ -3540,8 +3541,11 @@ static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
 }
 
-static inline void *f2fs_vmalloc(size_t size)
+static inline void *f2fs_vmalloc(struct f2fs_sb_info *sbi, size_t size)
 {
+	if (time_to_inject(sbi, FAULT_VMALLOC))
+		return NULL;
+
 	return vmalloc(size);
 }
 
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 1860edc2a8fb..32f2abac19cf 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -68,6 +68,7 @@ const char *f2fs_fault_name[FAULT_MAX] = {
 	[FAULT_NO_SEGMENT]		= "no free segment",
 	[FAULT_INCONSISTENT_FOOTER]	= "inconsistent footer",
 	[FAULT_TIMEOUT]			= "timeout",
+	[FAULT_VMALLOC]			= "vmalloc",
 };
 
 int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx
  2025-05-13  5:57 [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx Chao Yu
  2025-05-13  5:57 ` [PATCH 2/2] f2fs: introduce FAULT_VMALLOC Chao Yu
@ 2025-05-13  6:01 ` Christoph Hellwig
  2025-05-13  6:34   ` Chao Yu
  2025-05-14 15:40 ` [f2fs-dev] [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{, de}compress_ctx patchwork-bot+f2fs
  2 siblings, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2025-05-13  6:01 UTC (permalink / raw)
  To: Chao Yu; +Cc: jaegeuk, linux-f2fs-devel, linux-kernel, linux-mm

On Tue, May 13, 2025 at 01:57:20PM +0800, Chao Yu wrote:
> .init_{,de}compress_ctx uses kvmalloc() to alloc memory, it will try
> to allocate physically continuous page first, it may cause more memory
> allocation pressure, let's use vmalloc instead to mitigate it.

Shouldn't this be handled in kvmalloc instead of working around it in
callers?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx
  2025-05-13  6:01 ` [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx Christoph Hellwig
@ 2025-05-13  6:34   ` Chao Yu
  0 siblings, 0 replies; 5+ messages in thread
From: Chao Yu @ 2025-05-13  6:34 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: chao, jaegeuk, linux-f2fs-devel, linux-kernel, linux-mm

On 5/13/25 14:01, Christoph Hellwig wrote:
> On Tue, May 13, 2025 at 01:57:20PM +0800, Chao Yu wrote:
>> .init_{,de}compress_ctx uses kvmalloc() to alloc memory, it will try
>> to allocate physically continuous page first, it may cause more memory
>> allocation pressure, let's use vmalloc instead to mitigate it.
> 
> Shouldn't this be handled in kvmalloc instead of working around it in
> callers?

kvmalloc() will allocate physically continuous page first, so it may race
w/ other physically continuous page allocator who uses kmalloc(), in
scenario of there are few physically continuous pages.

Thanks,


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [f2fs-dev] [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{, de}compress_ctx
  2025-05-13  5:57 [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx Chao Yu
  2025-05-13  5:57 ` [PATCH 2/2] f2fs: introduce FAULT_VMALLOC Chao Yu
  2025-05-13  6:01 ` [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx Christoph Hellwig
@ 2025-05-14 15:40 ` patchwork-bot+f2fs
  2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+f2fs @ 2025-05-14 15:40 UTC (permalink / raw)
  To: Chao Yu; +Cc: jaegeuk, linux-kernel, linux-f2fs-devel

Hello:

This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <jaegeuk@kernel.org>:

On Tue, 13 May 2025 13:57:20 +0800 you wrote:
> .init_{,de}compress_ctx uses kvmalloc() to alloc memory, it will try
> to allocate physically continuous page first, it may cause more memory
> allocation pressure, let's use vmalloc instead to mitigate it.
> 
> [Test]
> cd /data/local/tmp
> touch file
> f2fs_io setflags compression file
> f2fs_io getflags file
> for i in $(seq 1 10); do sync; echo 3 > /proc/sys/vm/drop_caches;\
> time f2fs_io write 512 0 4096 zero osync file; truncate -s 0 file;\
> done
> 
> [...]

Here is the summary with links:
  - [f2fs-dev,1/2] f2fs: use vmalloc instead of kvmalloc in .init_{, de}compress_ctx
    https://git.kernel.org/jaegeuk/f2fs/c/10b26e772b10
  - [f2fs-dev,2/2] f2fs: introduce FAULT_VMALLOC
    https://git.kernel.org/jaegeuk/f2fs/c/2c19d65bab04

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-05-14 15:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-13  5:57 [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx Chao Yu
2025-05-13  5:57 ` [PATCH 2/2] f2fs: introduce FAULT_VMALLOC Chao Yu
2025-05-13  6:01 ` [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{,de}compress_ctx Christoph Hellwig
2025-05-13  6:34   ` Chao Yu
2025-05-14 15:40 ` [f2fs-dev] [PATCH 1/2] f2fs: use vmalloc instead of kvmalloc in .init_{, de}compress_ctx patchwork-bot+f2fs

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).