* [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key
@ 2025-09-29 6:56 Sun YangKai
2025-09-29 7:54 ` Qu Wenruo
2025-09-29 8:30 ` Filipe Manana
0 siblings, 2 replies; 8+ messages in thread
From: Sun YangKai @ 2025-09-29 6:56 UTC (permalink / raw)
To: linux-btrfs; +Cc: Sun YangKai
Restructure the error handling flow in btrfs_tree_mod_log_insert_key
to address memory allocation failures more cleanly.
No functional changes are made - this is purely a code readability
improvement.
Signed-off-by: Sun YangKai <sunk67188@gmail.com>
---
fs/btrfs/tree-mod-log.c | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)
diff --git a/fs/btrfs/tree-mod-log.c b/fs/btrfs/tree-mod-log.c
index 9e8cb3b7c064..4fd7859ad7dc 100644
--- a/fs/btrfs/tree-mod-log.c
+++ b/fs/btrfs/tree-mod-log.c
@@ -267,9 +267,8 @@ int btrfs_tree_mod_log_insert_key(const struct extent_buffer *eb, int slot,
if (!tree_mod_need_log(eb->fs_info, eb))
return 0;
+ /* Allocation error is handled later. */
tm = alloc_tree_mod_elem(eb, slot, op);
- if (!tm)
- ret = -ENOMEM;
if (tree_mod_dont_log(eb->fs_info, eb)) {
kfree(tm);
@@ -278,16 +277,14 @@ int btrfs_tree_mod_log_insert_key(const struct extent_buffer *eb, int slot,
* need to log.
*/
return 0;
- } else if (ret != 0) {
- /*
- * We previously failed to allocate memory and we need to log,
- * so we have to fail.
- */
- goto out_unlock;
}
- ret = tree_mod_log_insert(eb->fs_info, tm);
-out_unlock:
+ /* Deal with allocation error. */
+ if (tm)
+ ret = tree_mod_log_insert(eb->fs_info, tm);
+ else
+ ret = -ENOMEM;
+
write_unlock(&eb->fs_info->tree_mod_log_lock);
if (ret)
kfree(tm);
--
2.51.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key
2025-09-29 6:56 [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key Sun YangKai
@ 2025-09-29 7:54 ` Qu Wenruo
2025-09-29 8:25 ` Sun YangKai
2025-09-29 8:30 ` Filipe Manana
1 sibling, 1 reply; 8+ messages in thread
From: Qu Wenruo @ 2025-09-29 7:54 UTC (permalink / raw)
To: Sun YangKai, linux-btrfs
在 2025/9/29 16:26, Sun YangKai 写道:
> Restructure the error handling flow in btrfs_tree_mod_log_insert_key
> to address memory allocation failures more cleanly.
>
> No functional changes are made - this is purely a code readability
> improvement.
>
> Signed-off-by: Sun YangKai <sunk67188@gmail.com>
> ---
> fs/btrfs/tree-mod-log.c | 17 +++++++----------
> 1 file changed, 7 insertions(+), 10 deletions(-)
>
> diff --git a/fs/btrfs/tree-mod-log.c b/fs/btrfs/tree-mod-log.c
> index 9e8cb3b7c064..4fd7859ad7dc 100644
> --- a/fs/btrfs/tree-mod-log.c
> +++ b/fs/btrfs/tree-mod-log.c
> @@ -267,9 +267,8 @@ int btrfs_tree_mod_log_insert_key(const struct extent_buffer *eb, int slot,
> if (!tree_mod_need_log(eb->fs_info, eb))
> return 0;
>
> + /* Allocation error is handled later. */
> tm = alloc_tree_mod_elem(eb, slot, op);
> - if (!tm)
> - ret = -ENOMEM;
>
> if (tree_mod_dont_log(eb->fs_info, eb)) {
> kfree(tm);
> @@ -278,16 +277,14 @@ int btrfs_tree_mod_log_insert_key(const struct extent_buffer *eb, int slot,
> * need to log.
> */
> return 0;
> - } else if (ret != 0) {
> - /*
> - * We previously failed to allocate memory and we need to log,
> - * so we have to fail.
> - */
> - goto out_unlock;
> }
>
> - ret = tree_mod_log_insert(eb->fs_info, tm);
> -out_unlock:
> + /* Deal with allocation error. */
> + if (tm)
> + ret = tree_mod_log_insert(eb->fs_info, tm);
Sorry, I don't think this is really that better.
In fact I'm wondering why we don't delay the allocation after
tree_mod_dont_log()?
That would be way more straightforward.
Thanks,
Qu
> + else
> + ret = -ENOMEM;
> +
> write_unlock(&eb->fs_info->tree_mod_log_lock);
> if (ret)
> kfree(tm);
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key
2025-09-29 7:54 ` Qu Wenruo
@ 2025-09-29 8:25 ` Sun YangKai
2025-09-29 8:32 ` Filipe Manana
0 siblings, 1 reply; 8+ messages in thread
From: Sun YangKai @ 2025-09-29 8:25 UTC (permalink / raw)
To: wqu; +Cc: linux-btrfs
> > - ret = tree_mod_log_insert(eb->fs_info, tm);
> > -out_unlock:
> > + /* Deal with allocation error. */
> > + if (tm)
> > + ret = tree_mod_log_insert(eb->fs_info, tm);
>
> Sorry, I don't think this is really that better.
>
> In fact I'm wondering why we don't delay the allocation after
> tree_mod_dont_log()?
That's what I want to do at first. However, after looking further into
tree_mod_dont_log(), I found that it trys to get the tree_mod_log_lock. I'm
not sure if it's better or worse to do the allocation things after getting the
lock.
>
> That would be way more straightforward.
>
> Thanks,
> Qu
>
> > + else
> > + ret = -ENOMEM;
> > +
> >
Thanks,
Sun YangKai
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key
2025-09-29 8:25 ` Sun YangKai
@ 2025-09-29 8:32 ` Filipe Manana
2025-09-29 8:43 ` Qu Wenruo
0 siblings, 1 reply; 8+ messages in thread
From: Filipe Manana @ 2025-09-29 8:32 UTC (permalink / raw)
To: Sun YangKai; +Cc: wqu, linux-btrfs
On Mon, Sep 29, 2025 at 9:29 AM Sun YangKai <sunk67188@gmail.com> wrote:
>
> > > - ret = tree_mod_log_insert(eb->fs_info, tm);
> > > -out_unlock:
> > > + /* Deal with allocation error. */
> > > + if (tm)
> > > + ret = tree_mod_log_insert(eb->fs_info, tm);
> >
> > Sorry, I don't think this is really that better.
> >
> > In fact I'm wondering why we don't delay the allocation after
> > tree_mod_dont_log()?
>
> That's what I want to do at first. However, after looking further into
> tree_mod_dont_log(), I found that it trys to get the tree_mod_log_lock. I'm
> not sure if it's better or worse to do the allocation things after getting the
> lock.
You can't do a GFP_NOFS allocation after acquiring a rw lock... The
only allocation that can be done is a GFP_ATOMIC in that context, and
we want to avoid them.
>
> >
> > That would be way more straightforward.
> >
> > Thanks,
> > Qu
> >
> > > + else
> > > + ret = -ENOMEM;
> > > +
> > >
>
> Thanks,
> Sun YangKai
>
>
>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key
2025-09-29 8:32 ` Filipe Manana
@ 2025-09-29 8:43 ` Qu Wenruo
0 siblings, 0 replies; 8+ messages in thread
From: Qu Wenruo @ 2025-09-29 8:43 UTC (permalink / raw)
To: Filipe Manana, Sun YangKai; +Cc: linux-btrfs
在 2025/9/29 18:02, Filipe Manana 写道:
> On Mon, Sep 29, 2025 at 9:29 AM Sun YangKai <sunk67188@gmail.com> wrote:
>>
>>>> - ret = tree_mod_log_insert(eb->fs_info, tm);
>>>> -out_unlock:
>>>> + /* Deal with allocation error. */
>>>> + if (tm)
>>>> + ret = tree_mod_log_insert(eb->fs_info, tm);
>>>
>>> Sorry, I don't think this is really that better.
>>>
>>> In fact I'm wondering why we don't delay the allocation after
>>> tree_mod_dont_log()?
>>
>> That's what I want to do at first. However, after looking further into
>> tree_mod_dont_log(), I found that it trys to get the tree_mod_log_lock. I'm
>> not sure if it's better or worse to do the allocation things after getting the
>> lock.
>
> You can't do a GFP_NOFS allocation after acquiring a rw lock... The
> only allocation that can be done is a GFP_ATOMIC in that context, and
> we want to avoid them.
You're both right. Forgot it's a rwlock.
There is no better solution, I guess it's the best solution for now.
Reviewed-by: Qu Wenruo <wqu@suse.com>
Thanks,
Qu
>
>>
>>>
>>> That would be way more straightforward.
>>>
>>> Thanks,
>>> Qu
>>>
>>>> + else
>>>> + ret = -ENOMEM;
>>>> +
>>>>
>>
>> Thanks,
>> Sun YangKai
>>
>>
>>
>>
>>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key
2025-09-29 6:56 [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key Sun YangKai
2025-09-29 7:54 ` Qu Wenruo
@ 2025-09-29 8:30 ` Filipe Manana
2025-09-29 10:24 ` Sun YangKai
1 sibling, 1 reply; 8+ messages in thread
From: Filipe Manana @ 2025-09-29 8:30 UTC (permalink / raw)
To: Sun YangKai; +Cc: linux-btrfs
On Mon, Sep 29, 2025 at 8:31 AM Sun YangKai <sunk67188@gmail.com> wrote:
>
> Restructure the error handling flow in btrfs_tree_mod_log_insert_key
> to address memory allocation failures more cleanly.
>
> No functional changes are made - this is purely a code readability
> improvement.
>
> Signed-off-by: Sun YangKai <sunk67188@gmail.com>
> ---
> fs/btrfs/tree-mod-log.c | 17 +++++++----------
> 1 file changed, 7 insertions(+), 10 deletions(-)
>
> diff --git a/fs/btrfs/tree-mod-log.c b/fs/btrfs/tree-mod-log.c
> index 9e8cb3b7c064..4fd7859ad7dc 100644
> --- a/fs/btrfs/tree-mod-log.c
> +++ b/fs/btrfs/tree-mod-log.c
> @@ -267,9 +267,8 @@ int btrfs_tree_mod_log_insert_key(const struct extent_buffer *eb, int slot,
> if (!tree_mod_need_log(eb->fs_info, eb))
> return 0;
>
> + /* Allocation error is handled later. */
> tm = alloc_tree_mod_elem(eb, slot, op);
> - if (!tm)
> - ret = -ENOMEM;
I prefer the current way we do things.
First it's less confusing to see some action right after the memory
allocation, rather than much later like this patch proposes.
>
> if (tree_mod_dont_log(eb->fs_info, eb)) {
> kfree(tm);
> @@ -278,16 +277,14 @@ int btrfs_tree_mod_log_insert_key(const struct extent_buffer *eb, int slot,
> * need to log.
> */
> return 0;
> - } else if (ret != 0) {
> - /*
> - * We previously failed to allocate memory and we need to log,
> - * so we have to fail.
> - */
> - goto out_unlock;
Second, having this check and comment here makes it very explicit when
the memory allocation is a problem.
> }
>
> - ret = tree_mod_log_insert(eb->fs_info, tm);
> -out_unlock:
> + /* Deal with allocation error. */
This is a useless comment... The existing comment is much more helpful
and the fact that's in the else statement above, makes it easier to
grok.
I also wonder why you picked only this function, since this pattern is
followed in several other functions...
Not a fan of the proposed change.
> + if (tm)
> + ret = tree_mod_log_insert(eb->fs_info, tm);
> + else
> + ret = -ENOMEM;
> +
> write_unlock(&eb->fs_info->tree_mod_log_lock);
> if (ret)
> kfree(tm);
> --
> 2.51.0
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key
2025-09-29 8:30 ` Filipe Manana
@ 2025-09-29 10:24 ` Sun YangKai
2025-09-29 10:41 ` Filipe Manana
0 siblings, 1 reply; 8+ messages in thread
From: Sun YangKai @ 2025-09-29 10:24 UTC (permalink / raw)
To: fdmanana; +Cc: linux-btrfs
> > - ret = tree_mod_log_insert(eb->fs_info, tm);
> > -out_unlock:
> > + /* Deal with allocation error. */
>
> This is a useless comment... The existing comment is much more helpful
> and the fact that's in the else statement above, makes it easier to
> grok.
I agree that the existing comment is much more helpful, while I personally
don't like the else branch appears after a return/goto/continue/break
statement. So I think it's just about personal code style.
> I also wonder why you picked only this function, since this pattern is
> followed in several other functions...
Because I happened to read this, and it takes me minutes to realise what is
happening and why it was written like this...
And I'm not sure how to make it better. Since this is the most simple one with
this pattern, I just have a try to make it more clear IMO.
> Not a fan of the proposed change.
>
> > + if (tm)
> > + ret = tree_mod_log_insert(eb->fs_info, tm);
> > + else
> > + ret = -ENOMEM;
> > +
> >
> > write_unlock(&eb->fs_info->tree_mod_log_lock);
> > if (ret)
> >
> > kfree(tm);
> >
> > --
> > 2.51.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key
2025-09-29 10:24 ` Sun YangKai
@ 2025-09-29 10:41 ` Filipe Manana
0 siblings, 0 replies; 8+ messages in thread
From: Filipe Manana @ 2025-09-29 10:41 UTC (permalink / raw)
To: Sun YangKai; +Cc: linux-btrfs
On Mon, Sep 29, 2025 at 11:24 AM Sun YangKai <sunk67188@gmail.com> wrote:
>
> > > - ret = tree_mod_log_insert(eb->fs_info, tm);
> > > -out_unlock:
> > > + /* Deal with allocation error. */
> >
> > This is a useless comment... The existing comment is much more helpful
> > and the fact that's in the else statement above, makes it easier to
> > grok.
>
> I agree that the existing comment is much more helpful, while I personally
> don't like the else branch appears after a return/goto/continue/break
> statement. So I think it's just about personal code style.
>
> > I also wonder why you picked only this function, since this pattern is
> > followed in several other functions...
>
> Because I happened to read this, and it takes me minutes to realise what is
> happening and why it was written like this...
Well, having to spend minutes to understand something isn't unusual,
even for more experienced people.
You can't expect to understand all the details of the tree mod log in
just a few minutes, that's unreasonable.
> And I'm not sure how to make it better. Since this is the most simple one with
> this pattern, I just have a try to make it more clear IMO.
This is a short function and I find the existing flow and comments a
lot better to understand (well, I wrote them actually).
>
> > Not a fan of the proposed change.
> >
> > > + if (tm)
> > > + ret = tree_mod_log_insert(eb->fs_info, tm);
> > > + else
> > > + ret = -ENOMEM;
> > > +
> > >
> > > write_unlock(&eb->fs_info->tree_mod_log_lock);
> > > if (ret)
> > >
> > > kfree(tm);
> > >
> > > --
> > > 2.51.0
>
>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2025-09-29 10:42 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-29 6:56 [PATCH] btrfs: reorganize error handling in btrfs_tree_mod_log_insert_key Sun YangKai
2025-09-29 7:54 ` Qu Wenruo
2025-09-29 8:25 ` Sun YangKai
2025-09-29 8:32 ` Filipe Manana
2025-09-29 8:43 ` Qu Wenruo
2025-09-29 8:30 ` Filipe Manana
2025-09-29 10:24 ` Sun YangKai
2025-09-29 10:41 ` Filipe Manana
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.