Re: [PATCH] dm btree: Remove redundant index condition

From: Ming Hung Tsai

Date: Mon Nov 16 2026 - 23:54:26 EST


On Tue, Nov 17, 2026 at 4:07 AM Mikulas Patocka <mpatocka@xxxxxxxxxx> wrote:
>
>
>
> On Fri, 6 Nov 2026, Donut Oreo Computer I wrote:
>
> > The index is unsigned, so checking whether i is non-negative is
> > unnecessary.
>
> Really?
>
> In the caller, there is
>
> "unsigned int level, index = -1, last_level = info->levels - 1;"
>
> In btree_insert_raw, there is "int r, i = *index, top = 1;"
>
> If you cast -1 to unsigned int and then cast it back to int, you get -1.
>
> So, it doesn't seem like an obvious dead code to me.
>
> Mikulas
>
> > Signed-off-by: Donut Oreo Computer <donut.oreo.computer@xxxxxxxxx>
> > ---
> > drivers/md/persistent-data/dm-btree.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/md/persistent-data/dm-btree.c
> > b/drivers/md/persistent-data/dm-btree.c
> > index dd02eee4a23c..b7ac2f6d1d71 100644
> > --- a/drivers/md/persistent-data/dm-btree.c
> > +++ b/drivers/md/persistent-data/dm-btree.c
> > @@ -1108,7 +1108,7 @@ static int btree_insert_raw(struct shadow_spine
> > *s, dm_block_t root,
> > * see a way to do this automatically as part of the spine
> > * op.
> > */
> > - if (shadow_has_parent(s) && i >= 0) { /* FIXME: second
> > clause unness. */
> > + if (shadow_has_parent(s)) {
> > __le64 location =
> > cpu_to_le64(dm_block_location(shadow_current(s)));
> >
> > __dm_bless_for_disk(&location);
> > --
> > 2.56.0
> >
>

The commit message is not accurate. It's not that "the variable i is
always greater than zero", it's "the variable i cannot be negative if
shadow_has_parent() returns true". The reason is how the variable i
relates to the shadow spine. 'i' is the slot in the parent node that
points to the current node. It is initialized to -1 only on the first
btree_insert_raw() call, when the shadow_spine is empty and
shadow_has_parent() returns false (because the top-level root doesn't
have a parent). After shadowing the second node along the path and
shadow_has_parent(s) returns true, the variable i must be
non-negative.

For multi-level btrees, the index passed to remove_raw() means the
slot of the previous level's leaf, hence 'i' is always >= 0 on subtree
roots.

With the invariant "index == -1 only when the shadow_spine is empty",
it should be safe to remove the i >= 0 check. remove_raw() and
remove_nearest() in dm-btree-remove.c alrealy rely on the same rule,
without the extra check.

__btree_get_overwrite_leaf() has the same redundant check. I'll handle
it in a separate patch after testing.


Hank