Skip to contentSkip to conversation

btrfs: don't bug_on with enomem in __clear_state_bit

PATCH In review Josef Bacik 3 participants 5 messages Apr 13, 2018 – Apr 16, 2018 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: don't bug_on with enomem in __clear_state_bit

From: Josef Bacik <[email protected]>

Since we're allocating under atomic we could every easily enomem, so if that's the case and we can block then loop around and try to allocate the prealloc not under a lock.

We also saw this happen during try_to_release_page in production, in which case it's completely valid to return ENOMEM so we can tell try_to_release_page that we can't release this page.

---
 fs/btrfs/extent_io.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
fs/btrfs/extent_io.c +24 -4
index fb32394fd830..1054dc0158b5 100644
@@ -593,8 +593,9 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
 	struct extent_state *prealloc = NULL;
 	struct rb_node *node;
 	u64 last_end;
-	int err;
+	int err = 0;
 	int clear = 0;
+	bool need_prealloc = false;
 
 	btrfs_debug_check_extent_io_range(tree, start, end);
 
@@ -617,6 +618,9 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
 		 * If we end up needing a new extent state we allocate it later.
 		 */
 		prealloc = alloc_extent_state(mask);
+		if (!prealloc && need_prealloc)
+			return -ENOMEM;
+		need_prealloc = false;
 	}
 
 	spin_lock(&tree->lock);
@@ -676,7 +680,15 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
 
 	if (state->start < start) {
 		prealloc = alloc_extent_state_atomic(prealloc);
-		BUG_ON(!prealloc);
+		if (!prealloc) {
+			if (gfpflags_allow_blocking(mask)) {
+				need_prealloc = true;
+				spin_unlock(&tree->lock);
+				goto again;
+			}
+			err = -ENOMEM;
+			goto out;
+		}
 		err = split_state(tree, state, prealloc, start);
 		if (err)
 			extent_io_tree_panic(tree, err);
@@ -699,7 +711,15 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
 	 */
 	if (state->start <= end && state->end > end) {
 		prealloc = alloc_extent_state_atomic(prealloc);
-		BUG_ON(!prealloc);
+		if (!prealloc) {
+			if (gfpflags_allow_blocking(mask)) {
+				need_prealloc = true;
+				spin_unlock(&tree->lock);
+				goto again;
+			}
+			err = -ENOMEM;
+			goto out;
+		}
 		err = split_state(tree, state, prealloc, end + 1);
 		if (err)
 			extent_io_tree_panic(tree, err);
@@ -734,7 +754,7 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
 	if (prealloc)
 		free_extent_state(prealloc);
 
-	return 0;
+	return err;
 
 }
 
Signature · 1 line
2.14.3
#1 lore raw
Quoted 41 lines · Josef Bacik

From: Josef Bacik <[email protected]>

Since we're allocating under atomic we could every easily enomem, so if that's the case and we can block then loop around and try to allocate the prealloc not under a lock.

We also saw this happen during try_to_release_page in production, in which case it's completely valid to return ENOMEM so we can tell try_to_release_page that we can't release this page.

---
 fs/btrfs/extent_io.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
fs/btrfs/extent_io.c +5 -1
index fb32394fd830..1054dc0158b5 100644
@@ -593,8 +593,9 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
        struct extent_state *prealloc = NULL;
        struct rb_node *node;
        u64 last_end;
-       int err;
+       int err = 0;
        int clear = 0;
+       bool need_prealloc = false;
 
        btrfs_debug_check_extent_io_range(tree, start, end);
 
@@ -617,6 +618,9 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
                 * If we end up needing a new extent state we allocate it later.
                 */
                prealloc = alloc_extent_state(mask);
+               if (!prealloc && need_prealloc)
+                       return -ENOMEM;
+               need_prealloc = false;
        }
 
        spin_lock(&tree->lock);

fs/btrfs/extent_io.c int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, · line 687

Quoted 9 lines
fs/btrfs/extent_io.c +5 -1
@@ -676,7 +680,15 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
 
        if (state->start < start) {
                prealloc = alloc_extent_state_atomic(prealloc);
-               BUG_ON(!prealloc);
+               if (!prealloc) {
+                       if (gfpflags_allow_blocking(mask)) {
+                               need_prealloc = true;
+                               spin_unlock(&tree->lock);
+                               goto again;

Could we simply 'goto search_again;' ?

thanks,
liubo

Quoted 40 lines
fs/btrfs/extent_io.c +14 -3
+                       }
+                       err = -ENOMEM;
+                       goto out;
+               }
                err = split_state(tree, state, prealloc, start);
                if (err)
                        extent_io_tree_panic(tree, err);
@@ -699,7 +711,15 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
         */
        if (state->start <= end && state->end > end) {
                prealloc = alloc_extent_state_atomic(prealloc);
-               BUG_ON(!prealloc);
+               if (!prealloc) {
+                       if (gfpflags_allow_blocking(mask)) {
+                               need_prealloc = true;
+                               spin_unlock(&tree->lock);
+                               goto again;
+                       }
+                       err = -ENOMEM;
+                       goto out;
+               }
                err = split_state(tree, state, prealloc, end + 1);
                if (err)
                        extent_io_tree_panic(tree, err);
@@ -734,7 +754,7 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
        if (prealloc)
                free_extent_state(prealloc);
 
-       return 0;
+       return err;
 
 }
 
--

2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 43 lines · Liu Bo
Quoted 42 lines · Josef Bacik

From: Josef Bacik <[email protected]>

Since we're allocating under atomic we could every easily enomem, so if that's the case and we can block then loop around and try to allocate the prealloc not under a lock.

We also saw this happen during try_to_release_page in production, in which case it's completely valid to return ENOMEM so we can tell try_to_release_page that we can't release this page.

---
 fs/btrfs/extent_io.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
fs/btrfs/extent_io.c +5 -1
index fb32394fd830..1054dc0158b5 100644
@@ -593,8 +593,9 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
        struct extent_state *prealloc = NULL;
        struct rb_node *node;
        u64 last_end;
-       int err;
+       int err = 0;
        int clear = 0;
+       bool need_prealloc = false;
 
        btrfs_debug_check_extent_io_range(tree, start, end);
 
@@ -617,6 +618,9 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
                 * If we end up needing a new extent state we allocate it later.
                 */
                prealloc = alloc_extent_state(mask);
+               if (!prealloc && need_prealloc)
+                       return -ENOMEM;
+               need_prealloc = false;
        }
 
        spin_lock(&tree->lock);
@@ -676,7 +680,15 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
 
Quoted 11 lines

fs/btrfs/extent_io.c int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, · line 687

Quoted 8 lines
fs/btrfs/extent_io.c +5 -1
        if (state->start < start) {
                prealloc = alloc_extent_state_atomic(prealloc);
-               BUG_ON(!prealloc);
+               if (!prealloc) {
+                       if (gfpflags_allow_blocking(mask)) {
+                               need_prealloc = true;
+                               spin_unlock(&tree->lock);
+                               goto again;

Could we simply 'goto search_again;' ?

We could, but I have another patch that's more involved that reworks this logic and kills search_again, so I'm leaving it this way for now for this isolated fix. Thanks,

Josef

#3 lore raw
In reply to Josef Bacik
Quoted 11 lines · Josef Bacik

From: Josef Bacik <[email protected]>

Since we're allocating under atomic we could every easily enomem, so if that's the case and we can block then loop around and try to allocate the prealloc not under a lock.

We also saw this happen during try_to_release_page in production, in which case it's completely valid to return ENOMEM so we can tell try_to_release_page that we can't release this page.

Exactly same patch as

https://patchwork.kernel.org/patch/10053319/

so the same comment applies.

#4 lore raw
Quoted 11 lines · David Sterba
Quoted 10 lines · Josef Bacik

From: Josef Bacik <[email protected]>

Since we're allocating under atomic we could every easily enomem, so if that's the case and we can block then loop around and try to allocate the prealloc not under a lock.

We also saw this happen during try_to_release_page in production, in which case it's completely valid to return ENOMEM so we can tell try_to_release_page that we can't release this page.

Quoted 7 lines
Quoted 1 line

Exactly same patch as

https://patchwork.kernel.org/patch/10053319/

so the same comment applies.

Moving the bugon just makes the same problem happen again. try_to_release_page() will call with whatever arbitrary gfp mask it has, so if it's GFP_ATOMIC we _want_ it to return -ENOMEM. Everybody else that calls that doesn't check the return value calls with a gfp mask that's will allow retrying the allocation. If they fail it's because the box is super out of memory and we'll have larger problems than us not handling the case properly. Thanks,

Josef