All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jyun-An Chen <jun930436@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, Jyun-An Chen <jun930436@gmail.com>
Subject: [PATCH 2/2] lib/btree: stop btree_get_prev() from wrapping around below key 0
Date: Sun,  6 Dec 2026 07:07:23 +0800	[thread overview]
Message-ID: <20261205230723.96179-3-jun930436@gmail.com> (raw)
In-Reply-To: <20261205230723.96179-1-jun930436@gmail.com>

When btree_get_prev() finds no smaller key in the leaf it descended to,
it retries the search below retry_key, the key of the last inner node
entry it followed.  Inner node keys are not updated when entries are
removed, and find_level() lowers the rightmost inner key to the key it
looks for, so that key can be 0 while the tree has no entry with key 0:
after key 0 was removed, or after btree_remove() was called for a key 0
that was not in the tree.  dec_key() then wraps it around to the
largest possible key, and the search starts over at the end of the
tree.  A btree_for_each_safe*() loop that reaches the bottom of such a
tree never terminates.

For example, with keys 0..51 in a btree32, removing key 0 and then
walking the tree with btree_last() and btree_get_prev() returns 51..1,
then 51..1 again, without end.

There is no key below 0, so stop when retry_key is 0.

Fixes: 5db53f3e80de ("[LogFS] add new flash file system")
Signed-off-by: Jyun-An Chen <jun930436@gmail.com>
---
I used an AI coding assistant (Claude Code) to write simple tests
to check that this change works.

 lib/btree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/btree.c b/lib/btree.c
index 3c2560173c62..c245a14c31bf 100644
--- a/lib/btree.c
+++ b/lib/btree.c
@@ -346,7 +346,7 @@ void *btree_get_prev(struct btree_head *head, struct btree_geo *geo,
 		}
 	}
 miss:
-	if (retry_key) {
+	if (retry_key && !keyzero(geo, retry_key)) {
 		longcpy(key, retry_key, geo->keylen);
 		retry_key = NULL;
 		goto retry;

      parent reply	other threads:[~2026-12-05 23:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 23:07 [PATCH 0/2] lib/btree: fix two bugs with key 0 Jyun-An Chen
2026-12-05 23:07 ` [PATCH 1/2] lib/btree: fix btree_remove() of a key 0 that is not in the tree Jyun-An Chen
2026-12-05 23:07 ` Jyun-An Chen [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=20261205230723.96179-3-jun930436@gmail.com \
    --to=jun930436@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    /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 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.