linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Btrfs: setup a nofs context for memory allocation at btrfs_create_tree()
@ 2018-12-13 21:16 fdmanana
  2018-12-14  7:28 ` Nikolay Borisov
  2019-01-03 16:59 ` David Sterba
  0 siblings, 2 replies; 3+ messages in thread
From: fdmanana @ 2018-12-13 21:16 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

We are holding a transaction handle when creating a tree, therefore we can
not allocate the root using GFP_KERNEL, as we could deadlock if reclaim is
triggered by the allocation, therefore setup a nofs context.

Fixes: 74e4d82757f74 ("btrfs: let callers of btrfs_alloc_root pass gfp flags")
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/disk-io.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 67afeaf28a41..3e1b846156dd 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -17,6 +17,7 @@
 #include <linux/semaphore.h>
 #include <linux/error-injection.h>
 #include <linux/crc32c.h>
+#include <linux/sched/mm.h>
 #include <asm/unaligned.h>
 #include "ctree.h"
 #include "disk-io.h"
@@ -1235,10 +1236,17 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
 	struct btrfs_root *tree_root = fs_info->tree_root;
 	struct btrfs_root *root;
 	struct btrfs_key key;
+	unsigned int nofs_flag;
 	int ret = 0;
 	uuid_le uuid = NULL_UUID_LE;
 
+	/*
+	 * We're holding a transaction handle, so use a NOFS memory allocation
+	 * context to avoid deadlock if reclaim happens.
+	 */
+	nofs_flag = memalloc_nofs_save();
 	root = btrfs_alloc_root(fs_info, GFP_KERNEL);
+	memalloc_nofs_restore(nofs_flag);
 	if (!root)
 		return ERR_PTR(-ENOMEM);
 
-- 
2.11.0


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

* Re: [PATCH] Btrfs: setup a nofs context for memory allocation at btrfs_create_tree()
  2018-12-13 21:16 [PATCH] Btrfs: setup a nofs context for memory allocation at btrfs_create_tree() fdmanana
@ 2018-12-14  7:28 ` Nikolay Borisov
  2019-01-03 16:59 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: Nikolay Borisov @ 2018-12-14  7:28 UTC (permalink / raw)
  To: fdmanana, linux-btrfs



On 13.12.18 г. 23:16 ч., fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> We are holding a transaction handle when creating a tree, therefore we can
> not allocate the root using GFP_KERNEL, as we could deadlock if reclaim is
> triggered by the allocation, therefore setup a nofs context.
> 
> Fixes: 74e4d82757f74 ("btrfs: let callers of btrfs_alloc_root pass gfp flags")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Reviewed-by: Nikolay Borisov <nborisov@suse.com>

> ---
>  fs/btrfs/disk-io.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> index 67afeaf28a41..3e1b846156dd 100644
> --- a/fs/btrfs/disk-io.c
> +++ b/fs/btrfs/disk-io.c
> @@ -17,6 +17,7 @@
>  #include <linux/semaphore.h>
>  #include <linux/error-injection.h>
>  #include <linux/crc32c.h>
> +#include <linux/sched/mm.h>
>  #include <asm/unaligned.h>
>  #include "ctree.h"
>  #include "disk-io.h"
> @@ -1235,10 +1236,17 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
>  	struct btrfs_root *tree_root = fs_info->tree_root;
>  	struct btrfs_root *root;
>  	struct btrfs_key key;
> +	unsigned int nofs_flag;
>  	int ret = 0;
>  	uuid_le uuid = NULL_UUID_LE;
>  
> +	/*
> +	 * We're holding a transaction handle, so use a NOFS memory allocation
> +	 * context to avoid deadlock if reclaim happens.
> +	 */
> +	nofs_flag = memalloc_nofs_save();
>  	root = btrfs_alloc_root(fs_info, GFP_KERNEL);
> +	memalloc_nofs_restore(nofs_flag);
>  	if (!root)
>  		return ERR_PTR(-ENOMEM);
>  
> 

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

* Re: [PATCH] Btrfs: setup a nofs context for memory allocation at btrfs_create_tree()
  2018-12-13 21:16 [PATCH] Btrfs: setup a nofs context for memory allocation at btrfs_create_tree() fdmanana
  2018-12-14  7:28 ` Nikolay Borisov
@ 2019-01-03 16:59 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2019-01-03 16:59 UTC (permalink / raw)
  To: fdmanana; +Cc: linux-btrfs

On Thu, Dec 13, 2018 at 09:16:45PM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> We are holding a transaction handle when creating a tree, therefore we can
> not allocate the root using GFP_KERNEL, as we could deadlock if reclaim is
> triggered by the allocation, therefore setup a nofs context.
> 
> Fixes: 74e4d82757f74 ("btrfs: let callers of btrfs_alloc_root pass gfp flags")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Added to misc-next, thanks.

Regarding this class of bugs, I'm not sure if I mentioned it or not, the
long-term fix is to set NOFS in transaction start and restore at
end/commit. I have a wip patch but not near completion so until then
the individual callsites need to be handled.

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

end of thread, other threads:[~2019-01-03 16:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-13 21:16 [PATCH] Btrfs: setup a nofs context for memory allocation at btrfs_create_tree() fdmanana
2018-12-14  7:28 ` Nikolay Borisov
2019-01-03 16:59 ` David Sterba

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).