]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/4.19.31/btrfs-setup-a-nofs-context-for-memory-allocation-at-btrfs_create_tree.patch
Linux 4.19.31
[thirdparty/kernel/stable-queue.git] / releases / 4.19.31 / btrfs-setup-a-nofs-context-for-memory-allocation-at-btrfs_create_tree.patch
CommitLineData
df1bfe5f
GKH
1From b89f6d1fcb30a8cbdc18ce00c7d93792076af453 Mon Sep 17 00:00:00 2001
2From: Filipe Manana <fdmanana@suse.com>
3Date: Thu, 13 Dec 2018 21:16:45 +0000
4Subject: Btrfs: setup a nofs context for memory allocation at btrfs_create_tree()
5
6From: Filipe Manana <fdmanana@suse.com>
7
8commit b89f6d1fcb30a8cbdc18ce00c7d93792076af453 upstream.
9
10We are holding a transaction handle when creating a tree, therefore we can
11not allocate the root using GFP_KERNEL, as we could deadlock if reclaim is
12triggered by the allocation, therefore setup a nofs context.
13
14Fixes: 74e4d82757f74 ("btrfs: let callers of btrfs_alloc_root pass gfp flags")
15CC: stable@vger.kernel.org # 4.9+
16Reviewed-by: Nikolay Borisov <nborisov@suse.com>
17Signed-off-by: Filipe Manana <fdmanana@suse.com>
18Reviewed-by: David Sterba <dsterba@suse.com>
19Signed-off-by: David Sterba <dsterba@suse.com>
20Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
21
22---
23 fs/btrfs/disk-io.c | 8 ++++++++
24 1 file changed, 8 insertions(+)
25
26--- a/fs/btrfs/disk-io.c
27+++ b/fs/btrfs/disk-io.c
28@@ -17,6 +17,7 @@
29 #include <linux/semaphore.h>
30 #include <linux/error-injection.h>
31 #include <linux/crc32c.h>
32+#include <linux/sched/mm.h>
33 #include <asm/unaligned.h>
34 #include "ctree.h"
35 #include "disk-io.h"
36@@ -1236,10 +1237,17 @@ struct btrfs_root *btrfs_create_tree(str
37 struct btrfs_root *tree_root = fs_info->tree_root;
38 struct btrfs_root *root;
39 struct btrfs_key key;
40+ unsigned int nofs_flag;
41 int ret = 0;
42 uuid_le uuid = NULL_UUID_LE;
43
44+ /*
45+ * We're holding a transaction handle, so use a NOFS memory allocation
46+ * context to avoid deadlock if reclaim happens.
47+ */
48+ nofs_flag = memalloc_nofs_save();
49 root = btrfs_alloc_root(fs_info, GFP_KERNEL);
50+ memalloc_nofs_restore(nofs_flag);
51 if (!root)
52 return ERR_PTR(-ENOMEM);
53