mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: Cheng Yongkang <teel4res@gmail.com>
Cc: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
	linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] btrfs: reject zero num_stripes in the system chunk array
Date: Fri, 30 Oct 2026 18:43:54 +1030	[thread overview]
Message-ID: <d2ee0cc1-674e-4447-88da-d346122a43a4@suse.com> (raw)
In-Reply-To: <179334729054.2353588.12398743988940745859@gmail.com>



在 2026/10/30 18:31, Cheng Yongkang 写道:
> Hi Qu,
> 
> Removing the ASSERT alone is not sufficient.  btrfs_chunk_item_size()
> computes
> 
>    sizeof(struct btrfs_chunk) +
>    sizeof(struct btrfs_stripe) * (num_stripes - 1)
> 
> With num_stripes == 0, the (num_stripes - 1) term underflows in the
> unsigned multiplication and the function returns 16 bytes (48 - 32)
> instead of a valid item size.  The system chunk array loop would then
> advance by the wrong size before btrfs_check_chunk_valid() runs.
> 
> So either btrfs_chunk_item_size() has to handle num_stripes == 0 first
> (for example, return sizeof(struct btrfs_chunk) and let
> btrfs_check_chunk_valid() reject it), or the early check has to stay so
> the size calculation is never reached with zero stripes.  I kept the
> early check as the minimal fix; if you prefer centralizing the rejection
> in the tree-checker, I can change btrfs_chunk_item_size() and drop the
> check.

Yes, drop the check and change btrfs_chunk_item_size() to handle zero 
num_stripes is not hard at all, and it's still smaller than your 
original fix:

diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index d5fe5cba0c35..891bc2432870 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -711,9 +711,8 @@ static inline enum btrfs_map_op btrfs_op(const 
struct bio *bio)

  static inline unsigned long btrfs_chunk_item_size(int num_stripes)
  {
-       ASSERT(num_stripes);
-       return sizeof(struct btrfs_chunk) +
-               sizeof(struct btrfs_stripe) * (num_stripes - 1);
+       return offsetof(struct btrfs_chunk, stripe) +
+               sizeof(struct btrfs_stripe) * num_stripes;
  }

  /*


> 
> Thanks,
> Cheng Yongkang


      reply	other threads:[~2026-10-30  8:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-29 14:12 Cheng Yongkang
2026-10-29 17:55 ` Filipe Manana
2026-10-30  8:01   ` Cheng Yongkang
2026-10-29 21:00 ` Qu Wenruo
2026-10-30  8:01   ` Cheng Yongkang
2026-10-30  8:13     ` Qu Wenruo [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=d2ee0cc1-674e-4447-88da-d346122a43a4@suse.com \
    --to=wqu@suse.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=teel4res@gmail.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

all inboxes | Powered by JetHome®