* [PATCH 0/2] lib/btree: fix two bugs with key 0
@ 2026-12-05 23:07 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 ` [PATCH 2/2] lib/btree: stop btree_get_prev() from wrapping around below key 0 Jyun-An Chen
0 siblings, 2 replies; 3+ messages in thread
From: Jyun-An Chen @ 2026-12-05 23:07 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel, Jyun-An Chen
Unused slots in a lib/btree node hold key 0, and inner node keys can
be lowered to 0, so key 0 is not handled correctly in two places.
Patch 1 fixes btree_remove() of a key 0 that is not in the tree, which
drops another entry from the tree and can write in front of the node.
Patch 2 fixes btree_get_prev() wrapping around below key 0, which makes
a btree_for_each_safe*() loop never terminate.
The only in-tree user of lib/btree is qla2xxx, and I could not find a
path there that hits either bug, so this is not known to fix a problem
that users see today. Key 0 is a valid key, though, and these fixes
keep a future user of the library from running into them.
Jyun-An Chen (2):
lib/btree: fix btree_remove() of a key 0 that is not in the tree
lib/btree: stop btree_get_prev() from wrapping around below key 0
lib/btree.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] lib/btree: fix btree_remove() of a key 0 that is not in the tree
2026-12-05 23:07 [PATCH 0/2] lib/btree: fix two bugs with key 0 Jyun-An Chen
@ 2026-12-05 23:07 ` Jyun-An Chen
2026-12-05 23:07 ` [PATCH 2/2] lib/btree: stop btree_get_prev() from wrapping around below key 0 Jyun-An Chen
1 sibling, 0 replies; 3+ messages in thread
From: Jyun-An Chen @ 2026-12-05 23:07 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel, Jyun-An Chen
Unused slots in a btree node hold key 0 and a NULL value. When
btree_remove() is called for key 0 and the tree does not contain it,
getpos() stops at the first unused slot of the leaf (pos == fill), and
keycmp() reports a match. btree_remove_level() then removes that slot:
nothing is shifted, and clearpair(geo, node, fill - 1) wipes the
smallest real entry of the leaf, which is dropped from the tree without
its value being returned to the caller. If the leaf is the empty root
node left behind after the last entry was removed, fill is 0 and
clearpair() writes in front of the node. If the leaf is full, pos is
geo->no_pairs and keycmp() reads a value slot as if it were a key.
For example, with keys 1..5 in a btree32, btree_remove() of key 0
returns NULL, and afterwards btree_lookup() of key 1 returns NULL too.
Treat pos == fill as a miss.
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 9c80c0c7bba8..3c2560173c62 100644
--- a/lib/btree.c
+++ b/lib/btree.c
@@ -600,7 +600,7 @@ static void *btree_remove_level(struct btree_head *head, struct btree_geo *geo,
node = find_level(head, geo, key, level);
pos = getpos(geo, node, key);
fill = getfill(geo, node, pos);
- if ((level == 1) && (keycmp(geo, node, pos, key) != 0))
+ if (level == 1 && (pos == fill || keycmp(geo, node, pos, key) != 0))
return NULL;
ret = bval(geo, node, pos);
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] lib/btree: stop btree_get_prev() from wrapping around below key 0
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
1 sibling, 0 replies; 3+ messages in thread
From: Jyun-An Chen @ 2026-12-05 23:07 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel, Jyun-An Chen
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;
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-05 23:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/2] lib/btree: stop btree_get_prev() from wrapping around below key 0 Jyun-An Chen
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.