Skip to contentSkip to conversation

Btrfs: use nofs context when initializing security xattrs to avoid deadlock

PATCH In review [email protected] 3 participants 3 messages Dec 10, 2018 – Dec 12, 2018 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: use nofs context when initializing security xattrs to avoid deadlock

From: Filipe Manana <[email protected]>

When initializing the security xattrs, we are holding a transaction handle therefore we need to use a GFP_NOFS context in order to avoid a deadlock with reclaim in case it's triggered.

Fixes 39a27ec1004e8 ("btrfs: use GFP_KERNEL for xattr and acl allocations")
---
 fs/btrfs/xattr.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/btrfs/xattr.c +8 -0
index ea78c3d6dcfc..f141b45ce349 100644
@@ -11,6 +11,7 @@
 #include <linux/security.h>
 #include <linux/posix_acl_xattr.h>
 #include <linux/iversion.h>
+#include <linux/sched/mm.h>
 #include "ctree.h"
 #include "btrfs_inode.h"
 #include "transaction.h"
@@ -422,9 +423,15 @@ static int btrfs_initxattrs(struct inode *inode,
 {
 	const struct xattr *xattr;
 	struct btrfs_trans_handle *trans = fs_info;
+	unsigned int nofs_flag;
 	char *name;
 	int err = 0;
 
+	/*
+	 * We're holding a transaction handle, so use a NOFS memory allocation
+	 * context to avoid deadlock if reclaim happens.
+	 */
+	nofs_flag = memalloc_nofs_save();
 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
 		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
 			       strlen(xattr->name) + 1, GFP_KERNEL);
@@ -440,6 +447,7 @@ static int btrfs_initxattrs(struct inode *inode,
 		if (err < 0)
 			break;
 	}
+	memalloc_nofs_restore(nofs_flag);
 	return err;
 }
 
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · On 10.12.18 г. 19:53 ч., [email protected] wrote:

From: Filipe Manana <[email protected]>

When initializing the security xattrs, we are holding a transaction handle therefore we need to use a GFP_NOFS context in order to avoid a deadlock with reclaim in case it's triggered.

Fixes 39a27ec1004e8 ("btrfs: use GFP_KERNEL for xattr and acl allocations")
Quoted 41 lines
---
 fs/btrfs/xattr.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/btrfs/xattr.c +8 -0
index ea78c3d6dcfc..f141b45ce349 100644
@@ -11,6 +11,7 @@
 #include <linux/security.h>
 #include <linux/posix_acl_xattr.h>
 #include <linux/iversion.h>
+#include <linux/sched/mm.h>
 #include "ctree.h"
 #include "btrfs_inode.h"
 #include "transaction.h"
@@ -422,9 +423,15 @@ static int btrfs_initxattrs(struct inode *inode,
 {
 	const struct xattr *xattr;
 	struct btrfs_trans_handle *trans = fs_info;
+	unsigned int nofs_flag;
 	char *name;
 	int err = 0;
 
+	/*
+	 * We're holding a transaction handle, so use a NOFS memory allocation
+	 * context to avoid deadlock if reclaim happens.
+	 */
+	nofs_flag = memalloc_nofs_save();
 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
 		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
 			       strlen(xattr->name) + 1, GFP_KERNEL);
@@ -440,6 +447,7 @@ static int btrfs_initxattrs(struct inode *inode,
 		if (err < 0)
 			break;
 	}
+	memalloc_nofs_restore(nofs_flag);
 	return err;
 }
 
Reviewed-by on v1 1/1
#2 lore raw
In reply to [email protected]
Quoted 8 lines · On Mon, Dec 10, 2018 at 05:53:35PM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

When initializing the security xattrs, we are holding a transaction handle therefore we need to use a GFP_NOFS context in order to avoid a deadlock with reclaim in case it's triggered.

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