public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.cz>
To: Qu Wenruo <quwenruo.btrfs@gmx.com>
Cc: dsterba@suse.cz, Filipe Manana <fdmanana@kernel.org>,
	Qu Wenruo <wqu@suse.com>,
	linux-btrfs@vger.kernel.org
Subject: Re: [PATCH] btrfs: free the allocated memory if btrfs_alloc_page_array() failed
Date: Tue, 28 Nov 2023 17:29:47 +0100	[thread overview]
Message-ID: <20231128162947.GL18929@twin.jikos.cz> (raw)
In-Reply-To: <a3f718be-021e-4711-b934-a0d59d97a054@gmx.com>

On Sat, Nov 25, 2023 at 07:16:07PM +1030, Qu Wenruo wrote:
> On 2023/11/25 00:55, David Sterba wrote:
> > On Fri, Nov 24, 2023 at 12:47:37PM +0000, Filipe Manana wrote:
> >> On Fri, Nov 24, 2023 at 11:50 AM Filipe Manana <fdmanana@kernel.org> wrote:
> >>>
> >>> On Fri, Nov 24, 2023 at 4:30 AM Qu Wenruo <wqu@suse.com> wrote:
> >>>>
> >>>> [BUG]
> >>>> If btrfs_alloc_page_array() failed to allocate all pages but part of the
> >>>> slots, then the partially allocated pages would be leaked in function
> >>>> btrfs_submit_compressed_read().
> >>>>
> >>>> [CAUSE]
> >>>> As explicitly stated, if btrfs_alloc_page_array() returned -ENOMEM,
> >>>> caller is responsible to free the partially allocated pages.
> >>>>
> >>>> For the existing call sites, most of them are fine:
> >>>>
> >>>> - btrfs_raid_bio::stripe_pages
> >>>>    Handled by free_raid_bio().
> >>>>
> >>>> - extent_buffer::pages[]
> >>>>    Handled btrfs_release_extent_buffer_pages().
> >>>>
> >>>> - scrub_stripe::pages[]
> >>>>    Handled by release_scrub_stripe().
> >>>>
> >>>> But there is one exception in btrfs_submit_compressed_read(), if
> >>>> btrfs_alloc_page_array() failed, we didn't cleanup the array and freed
> >>>> the array pointer directly.
> >>>>
> >>>> Initially there is still the error handling in commit dd137dd1f2d7
> >>>> ("btrfs: factor out allocating an array of pages"), but later in commit
> >>>> 544fe4a903ce ("btrfs: embed a btrfs_bio into struct compressed_bio"),
> >>>> the error handling is removed, leading to the possible memory leak.
> >>>>
> >>>> [FIX]
> >>>> This patch would add back the error handling first, then to prevent such
> >>>> situation from happening again, also make btrfs_alloc_page_array() to
> >>>> free the allocated pages as a extra safe net.
> >>>>
> >>>> Fixes: 544fe4a903ce ("btrfs: embed a btrfs_bio into struct compressed_bio")
> >>>> Signed-off-by: Qu Wenruo <wqu@suse.com>
> >>>
> >>> Reviewed-by: Filipe Manana <fdmanana@suse.com>
> >>>
> >>> Looks good, thanks.
> >>
> >> Well, just one comment, see below.
> >>
> >>>
> >>>> ---
> >>>>   fs/btrfs/compression.c |  4 ++++
> >>>>   fs/btrfs/extent_io.c   | 10 +++++++---
> >>>>   2 files changed, 11 insertions(+), 3 deletions(-)
> >>>>
> >>>> diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
> >>>> index 19b22b4653c8..d6120741774b 100644
> >>>> --- a/fs/btrfs/compression.c
> >>>> +++ b/fs/btrfs/compression.c
> >>>> @@ -534,6 +534,10 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio)
> >>>>          return;
> >>>>
> >>>>   out_free_compressed_pages:
> >>>> +       for (int i = 0; i < cb->nr_pages; i++) {
> >>>> +               if (cb->compressed_pages[i])
> >>>> +                       __free_page(cb->compressed_pages[i]);
> >>>> +       }
> >>
> >> So this hunk is not needed, because of the changes you did to
> >> btrfs_alloc_page_array(), as now it always frees any allocated pages
> >> on -ENOMEM.
> >
> > Right, I'll drop the hunk, thanks.
> 
> In that case you may also want to delete the following commit message:
> 
>  > <<This patch would add back the error handling first, then>> to
> prevent such

Updated, thanks.

      reply	other threads:[~2023-11-28 16:37 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-24  4:23 [PATCH] btrfs: free the allocated memory if btrfs_alloc_page_array() failed Qu Wenruo
2023-11-24 11:50 ` Filipe Manana
2023-11-24 12:47   ` Filipe Manana
2023-11-24 14:25     ` David Sterba
2023-11-25  8:46       ` Qu Wenruo
2023-11-28 16:29         ` David Sterba [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231128162947.GL18929@twin.jikos.cz \
    --to=dsterba@suse.cz \
    --cc=fdmanana@kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=quwenruo.btrfs@gmx.com \
    --cc=wqu@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox