diff mbox series

[v2,3/3] ext4: Prevent ext4_kvmalloc_nofs() from re-entering the filesystem and deadlocking

Message ID 20191227080523.31808-4-naoto.kobayashi4c@gmail.com
State Accepted
Headers show
Series ext4: Prevent memory reclaim from re-entering the filesystem and deadlocking | expand

Commit Message

Naoto Kobayashi Dec. 27, 2019, 8:05 a.m. UTC
Even if __vmalloc() receives GFP_NOFS, this function allocates
data pages and auxiliary structures (e.g. pagetables) with __GFP_FS[1].
To prevent memory reclaim from re-entering the filesystem here and
potentially deadlocking, use memalloc_nofs_save() that gets
__vmalloc() to drop __GFP_FS.

[1] linux-tree/Documentation/core-api/gfp-mask-fs-io.rst

Signed-off-by: Naoto Kobayashi <naoto.kobayashi4c@gmail.com>
---
 fs/ext4/super.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

--
2.16.6
diff mbox series

Patch

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index e8965aa6ecce..7f4c9a43a3f3 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -43,6 +43,7 @@ 
 #include <linux/uaccess.h>
 #include <linux/iversion.h>
 #include <linux/unicode.h>
+#include <linux/sched/mm.h>

 #include <linux/kthread.h>
 #include <linux/freezer.h>
@@ -206,11 +207,13 @@  void ext4_superblock_csum_set(struct super_block *sb)

 void *ext4_kvmalloc_nofs(size_t size)
 {
+	unsigned int nofs_flag;
 	void *ret;

-	ret = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
-	if (!ret)
-		ret = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
+	/* kvmalloc() does not support GFP_NOFS */
+	nofs_flag = memalloc_nofs_save();
+	ret = kvmalloc(size, GFP_KERNEL);
+	memalloc_nofs_restore(nofs_flag);
 	return ret;
 }