Subject: linux-next: manual merge of the btrfs-kdave tree with the btrfs-fixes tree

From: Stephen Rothwell <[email-masked]>
Date: Thu, 8 Aug 2019 10:54:54 +1000

Hi all,

Today's linux-next merge of the btrfs-kdave tree got conflicts in:

  fs/btrfs/ctree.h
  fs/btrfs/extent-tree.c

between commits:

  d7cd4dd907c1 ("Btrfs: fix sysfs warning and missing raid sysfs directories")
  07301df7d2fc ("btrfs: trim: Check the range passed into to prevent overflow")

from the btrfs-fixes tree and commit:

  4dfbf33fbfb6 ("btrfs: migrate the block group lookup code")
  f35f767acec1 ("btrfs: move sysfs declarations out of ctree.h")
  76c49f86437b ("btrfs: factor sysfs code out of link_block_group")

from the btrfs-kdave tree.

I fixed it up (see below) and can carry the fix as necessary. This
is now fixed as far as linux-next is concerned, but any non trivial
conflicts should be mentioned to your upstream maintainer when your tree
is submitted for merging.  You may also want to consider cooperating
with the maintainer of the conflicting tree to minimise any particularly
complex conflicts.

I also added this patch:

From: Stephen Rothwell <[email-masked]>
Date: Thu, 8 Aug 2019 10:14:25 +1000
Subject: [PATCH] btrfs: merge fix up for "Btrfs: fix sysfs warning and missing
 raid sysfs directories"

Signed-off-by: Stephen Rothwell <[email-masked]>
---
 fs/btrfs/sysfs.c | 52 +++++++++++++++++++-----------------------------
 1 file changed, 21 insertions(+), 31 deletions(-)

diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index 600db4be740d..ee6f70e8f258 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -4,6 +4,7 @@
  */
 
 #include <linux/sched.h>
+#include <linux/sched/mm.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/completion.h>
@@ -27,7 +28,6 @@ struct btrfs_feature_attr {
 struct raid_kobject {
 	u64 flags;
 	struct kobject kobj;
-	struct list_head list;
 };
 
 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store)			\
@@ -819,33 +819,6 @@ static void init_feature_attrs(void)
 	}
 }
 
-/* link_block_group will queue up kobjects to add when we're reclaim-safe */
-void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info)
-{
-	struct btrfs_space_info *space_info;
-	struct raid_kobject *rkobj;
-	LIST_HEAD(list);
-	int ret = 0;
-
-	spin_lock(&fs_info->pending_raid_kobjs_lock);
-	list_splice_init(&fs_info->pending_raid_kobjs, &list);
-	spin_unlock(&fs_info->pending_raid_kobjs_lock);
-
-	list_for_each_entry(rkobj, &list, list) {
-		space_info = btrfs_find_space_info(fs_info, rkobj->flags);
-
-		ret = kobject_add(&rkobj->kobj, &space_info->kobj,
-				"%s", btrfs_bg_type_to_raid_name(rkobj->flags));
-		if (ret) {
-			kobject_put(&rkobj->kobj);
-			break;
-		}
-	}
-	if (ret)
-		btrfs_warn(fs_info,
-			   "failed to add kobject for block cache, ignoring");
-}
-
 /*
  * Create a sysfs entry for a given block group type at path
  * /sys/fs/btrfs/UUID/allocation/data/TYPE
@@ -855,10 +828,21 @@ void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache)
 	struct btrfs_fs_info *fs_info = cache->fs_info;
 	struct btrfs_space_info *space_info = cache->space_info;
 	struct raid_kobject *rkobj;
+	unsigned int nofs_flag;
+	int ret;
 	const int index = btrfs_bg_flags_to_raid_index(cache->flags);
 
+	/*
+	 * Setup a NOFS context because kobject_add(), deep in its call
+	 * chain, does GFP_KERNEL allocations, and we are often called
+	 * in a context where if reclaim is triggered we can deadlock
+	 * (we are either holding a transaction handle or some lock
+	 * required for a transaction commit).
+	 */
+	nofs_flag = memalloc_nofs_save();
 	rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
 	if (!rkobj) {
+		memalloc_nofs_restore(nofs_flag);
 		btrfs_warn(cache->fs_info,
 				"couldn't alloc memory for raid level kobject");
 		return;
@@ -867,9 +851,15 @@ void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache)
 	rkobj->flags = cache->flags;
 	kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
 
-	spin_lock(&fs_info->pending_raid_kobjs_lock);
-	list_add_tail(&rkobj->list, &fs_info->pending_raid_kobjs);
-	spin_unlock(&fs_info->pending_raid_kobjs_lock);
+	ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
+			  btrfs_bg_type_to_raid_name(rkobj->flags));
+	memalloc_nofs_restore(nofs_flag);
+	if (ret) {
+		kobject_put(&rkobj->kobj);
+		btrfs_warn(fs_info,
+		   "failed to add kobject for block cache, ignoring");
+		return;
+	}
 	space_info->block_group_kobjs[index] = &rkobj->kobj;
 }
 
-- 
2.20.1

-- 
Cheers,
Stephen Rothwell

diff --cc fs/btrfs/ctree.h
index 94660063a162,357316173d84..000000000000
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
diff --cc fs/btrfs/extent-tree.c
index 8b7eb22d508a,ad15d05e256b..000000000000
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@@ -8976,27 -8751,20 +8752,27 @@@ int btrfs_trim_fs(struct btrfs_fs_info 
  	int dev_ret = 0;
  	int ret = 0;
  
 +	/*
 +	 * Check range overflow if range->len is set.
 +	 * The default range->len is U64_MAX.
 +	 */
 +	if (range->len != U64_MAX &&
 +	    check_add_overflow(range->start, range->len, &range_end))
 +		return -EINVAL;
 +
  	cache = btrfs_lookup_first_block_group(fs_info, range->start);
- 	for (; cache; cache = next_block_group(cache)) {
+ 	for (; cache; cache = btrfs_next_block_group(cache)) {
 -		if (cache->key.objectid >= (range->start + range->len)) {
 +		if (cache->key.objectid >= range_end) {
  			btrfs_put_block_group(cache);
  			break;
  		}
  
  		start = max(range->start, cache->key.objectid);
 -		end = min(range->start + range->len,
 -				cache->key.objectid + cache->key.offset);
 +		end = min(range_end, cache->key.objectid + cache->key.offset);
  
  		if (end - start >= range->minlen) {
- 			if (!block_group_cache_done(cache)) {
- 				ret = cache_block_group(cache, 0);
+ 			if (!btrfs_block_group_cache_done(cache)) {
+ 				ret = btrfs_cache_block_group(cache, 0);
  				if (ret) {
  					bg_failed++;
  					bg_ret = ret;