Skip to contentSkip to conversation

Btrfs: setup a nofs context for memory allocation at __btrfs_set_acl

PATCH In review [email protected] 3 participants 3 messages Dec 13, 2018 – Jan 3, 2019 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: setup a nofs context for memory allocation at __btrfs_set_acl

From: Filipe Manana <[email protected]>

We are holding a transaction handle when setting an acl, therefore we can not allocate the xattr value buffer using GFP_KERNEL, as we could deadlock if reclaim is triggered by the allocation, therefore setup a nofs context.

Fixes 39a27ec1004e8 ("btrfs: use GFP_KERNEL for xattr and acl allocations")
---
 fs/btrfs/acl.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/btrfs/acl.c +9 -0
index 3b66c957ea6f..5810463dc6d2 100644
@@ -9,6 +9,7 @@
 #include <linux/posix_acl_xattr.h>
 #include <linux/posix_acl.h>
 #include <linux/sched.h>
+#include <linux/sched/mm.h>
 #include <linux/slab.h>
 
 #include "ctree.h"
@@ -72,8 +73,16 @@ static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
 	}
 
 	if (acl) {
+		unsigned int nofs_flag;
+
 		size = posix_acl_xattr_size(acl->a_count);
+		/*
+		 * We're holding a transaction handle, so use a NOFS memory
+		 * allocation context to avoid deadlock if reclaim happens.
+		 */
+		nofs_flag = memalloc_nofs_save();
 		value = kmalloc(size, GFP_KERNEL);
+		memalloc_nofs_restore(nofs_flag);
 		if (!value) {
 			ret = -ENOMEM;
 			goto out;
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · On 13.12.18 г. 23:16 ч., [email protected] wrote:

From: Filipe Manana <[email protected]>

We are holding a transaction handle when setting an acl, therefore we can not allocate the xattr value buffer using GFP_KERNEL, as we could deadlock if reclaim is triggered by the allocation, therefore setup a nofs context.

Fixes 39a27ec1004e8 ("btrfs: use GFP_KERNEL for xattr and acl allocations")
Quoted 34 lines
---
 fs/btrfs/acl.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/btrfs/acl.c +9 -0
index 3b66c957ea6f..5810463dc6d2 100644
@@ -9,6 +9,7 @@
 #include <linux/posix_acl_xattr.h>
 #include <linux/posix_acl.h>
 #include <linux/sched.h>
+#include <linux/sched/mm.h>
 #include <linux/slab.h>
 
 #include "ctree.h"
@@ -72,8 +73,16 @@ static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
 	}
 
 	if (acl) {
+		unsigned int nofs_flag;
+
 		size = posix_acl_xattr_size(acl->a_count);
+		/*
+		 * We're holding a transaction handle, so use a NOFS memory
+		 * allocation context to avoid deadlock if reclaim happens.
+		 */
+		nofs_flag = memalloc_nofs_save();
 		value = kmalloc(size, GFP_KERNEL);
+		memalloc_nofs_restore(nofs_flag);
 		if (!value) {
 			ret = -ENOMEM;
 			goto out;
#2 lore raw
In reply to [email protected]
Quoted 8 lines · On Thu, Dec 13, 2018 at 09:16:56PM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

We are holding a transaction handle when setting an acl, therefore we can not allocate the xattr value buffer using GFP_KERNEL, as we could deadlock if reclaim is triggered by the allocation, therefore setup a nofs context.

Fixes 39a27ec1004e8 ("btrfs: use GFP_KERNEL for xattr and acl allocations")

Added to misc-next, thanks.