linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] slub: Fix Off-By-One in the While condition in on_freelist()
@ 2025-02-15 16:57 Lilitha Persefoni Gkini
  2025-02-20  8:20 ` Harry Yoo
  0 siblings, 1 reply; 23+ messages in thread
From: Lilitha Persefoni Gkini @ 2025-02-15 16:57 UTC (permalink / raw)
  To: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Andrew Morton, Vlastimil Babka, Roman Gushchin, Hyeonggon Yoo,
	linux-mm, linux-kernel

The condition `nr <= slab->objects` in the `on_freelist()` serves as
bound while walking through the `freelist` linked list because we can't
have more free objects than the maximum amount of objects in the slab.
But the `=` can result in an extra unnecessary iteration.

The patch changes it to `nr < slab->objects` to ensure it iterates
at most `slab->objects` number of times.

Signed-off-by: Lilitha Persefoni Gkini <lilithpgkini@gmail.com>
---
 mm/slub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/slub.c b/mm/slub.c
index 1f50129dcfb3..ad42450d4b0f 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1435,7 +1435,7 @@ static int on_freelist(struct kmem_cache *s, struct slab *slab, void *search)
 	int max_objects;
 
 	fp = slab->freelist;
-	while (fp && nr <= slab->objects) {
+	while (fp && nr < slab->objects) {
 		if (fp == search)
 			return 1;
 		if (!check_valid_pointer(s, slab, fp)) {
-- 
2.48.1



^ permalink raw reply	[flat|nested] 23+ messages in thread
* [PATCH] slub: Fix Off-By-One in the While condition in on_freelist()
@ 2025-03-02 18:01 Lilith Persefoni Gkini
  2025-03-03 11:06 ` Vlastimil Babka
  0 siblings, 1 reply; 23+ messages in thread
From: Lilith Persefoni Gkini @ 2025-03-02 18:01 UTC (permalink / raw)
  To: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Andrew Morton, Vlastimil Babka, Roman Gushchin, Hyeonggon Yoo,
	linux-mm, linux-kernel, harry.yoo

The on_freelist() uses a while loop to walk through the linked list
freelist of a particular slab until it finds the `search` pattern and
breaks if there is a freepointer in the list that is NULL, or invalid
(fails the check_valid_pointer() check), or the number of objects (nr)
in the freelist is more than `slab->objects + 1`

No valid freelist should have more than slab->objects non NULL pointers,
therefore the while conditional should check until slab->objects amount
of times, not more.

If the `search` pattern is not found in the freelist then the function
should return `fp == search` where fp is the last freepointer from the
while loop.

If the caller of the function was searching for NULL and the freelist is
valid it should return True (1), otherwise False (0).

Signed-off-by: Lilith Persefoni Gkini <lilithgkini@proton.me>
---
 mm/slub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 1f50129dcfb3..0d3dd429b095 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1435,7 +1435,7 @@ static int on_freelist(struct kmem_cache *s, struct slab *slab, void *search)
 	int max_objects;
 
 	fp = slab->freelist;
-	while (fp && nr <= slab->objects) {
+	while (fp && nr < slab->objects) {
 		if (fp == search)
 			return 1;
 		if (!check_valid_pointer(s, slab, fp)) {
@@ -1473,7 +1473,7 @@ static int on_freelist(struct kmem_cache *s, struct slab *slab, void *search)
 		slab->inuse = slab->objects - nr;
 		slab_fix(s, "Object count adjusted");
 	}
-	return search == NULL;
+	return fp == search;
 }
 
 static void trace(struct kmem_cache *s, struct slab *slab, void *object,
-- 
2.48.1



^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2025-03-04 17:14 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-15 16:57 [PATCH] slub: Fix Off-By-One in the While condition in on_freelist() Lilitha Persefoni Gkini
2025-02-20  8:20 ` Harry Yoo
2025-02-20  9:21   ` Harry Yoo
2025-02-21 14:57     ` Lilith Gkini
2025-02-22  3:58       ` Harry Yoo
2025-02-22  9:24         ` Lilith Gkini
2025-02-24  0:00           ` Harry Yoo
2025-02-24 12:12             ` Lilith Gkini
2025-02-25 10:08               ` Harry Yoo
2025-02-27 16:40                 ` Lilith Gkini
2025-03-02 13:11                   ` Harry Yoo
2025-03-02 18:01 Lilith Persefoni Gkini
2025-03-03 11:06 ` Vlastimil Babka
2025-03-03 16:41   ` Lilith Gkini
2025-03-03 17:39     ` Christoph Lameter (Ampere)
2025-03-03 19:06     ` Vlastimil Babka
2025-03-04  8:24       ` Lilith Gkini
2025-03-04  8:41         ` Vlastimil Babka
2025-03-04 11:06           ` Lilith Gkini
2025-03-04 11:20             ` Vlastimil Babka
2025-03-04 12:18               ` Lilith Gkini
2025-03-04 14:25                 ` Vlastimil Babka
2025-03-04 17:14                   ` Lilith Gkini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox