Skip to contentSkip to conversation

tools/mm/page-types: Fix misc bugs

PATCH Applied Ye Liu 3 participants 10 messages May 13 – May 14 Memory management

lore
Share & tools
lore

Ye Liu

author
#0 lore raw

cover tools/mm/page-types: Fix misc bugs

From: Ye Liu <[email protected]>

This series fixes three issues in tools/mm/page-types.c:

1. Fix two typos in madvise() error messages ("madvice" -> "madvise")
2. Fix operator precedence bug in the sigbus handler where the ternary operator binds looser than addition, producing incorrect offset calculation when sigbus_addr is non-NULL
3. Fix --kpageflags option declaration in getopt_long: has_arg should be 1 (required_argument) since the option requires a file path

Ye Liu (3):
tools/mm/page-types: Fix typo in madvise() error message tools/mm/page-types: Fix ternary operator precedence in sigbus handler tools/mm/page-types: Fix kpageflags option argument in getopt_long

 tools/mm/page-types.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/3 tools/mm/page-types: Fix typo in madvise() error message

From: Ye Liu <[email protected]>

Two error messages incorrectly spelled the madvise() function name as "madvice". Fix the typo in both occurrences.

---
 tools/mm/page-types.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/mm/page-types.c +2 -2
index d7e5e8902af8..6594245217a8 100644
@@ -997,7 +997,7 @@ static void walk_file_range(const char *name, int fd,
 
 		/* turn off readahead */
 		if (madvise(ptr, len, MADV_RANDOM))
-			fatal("madvice failed: %s", name);
+			fatal("madvise failed: %s", name);
 
 		if (sigsetjmp(sigbus_jmp, 1)) {
 			end = off + sigbus_addr ? sigbus_addr - ptr : 0;
@@ -1015,7 +1015,7 @@ static void walk_file_range(const char *name, int fd,
 
 		/* turn off harvesting reference bits */
 		if (madvise(ptr, len, MADV_SEQUENTIAL))
-			fatal("madvice failed: %s", name);
+			fatal("madvise failed: %s", name);
 
 		if (pagemap_read(buf, (unsigned long)ptr / page_size,
 					nr_pages) != nr_pages)
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH 2/3 tools/mm/page-types: Fix ternary operator precedence in sigbus handler

In reply to Ye Liu

From: Ye Liu <[email protected]>

The ternary operator (?:) has lower precedence than addition (+), so the expression off + sigbus_addr ? sigbus_addr - ptr : 0 was parsed as (off + sigbus_addr) ? (sigbus_addr - ptr) : 0 rather than the intended off + (sigbus_addr ? sigbus_addr - ptr : 0). Add explicit parentheses to ensure the correct evaluation order.

---
 tools/mm/page-types.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/mm/page-types.c +1 -1
index 6594245217a8..66f429f2b698 100644
@@ -1000,7 +1000,7 @@ static void walk_file_range(const char *name, int fd,
 			fatal("madvise failed: %s", name);
 
 		if (sigsetjmp(sigbus_jmp, 1)) {
-			end = off + sigbus_addr ? sigbus_addr - ptr : 0;
+			end = off + (sigbus_addr ? sigbus_addr - ptr : 0);
 			fprintf(stderr, "got sigbus at offset %lld: %s\n",
 					(long long)end, name);
 			goto got_sigbus;
Signature · 1 line
2.43.0
author
#7 lore raw

PATCH 3/3 tools/mm/page-types: Fix kpageflags option argument in getopt_long

In reply to Ye Liu

From: Ye Liu <[email protected]>

The --kpageflags option requires an argument to specify the kpageflags file path, but has_arg was set to 0 (no_argument) in the long options table. Change it to 1 (required_argument) so getopt_long correctly parses the argument.

---
 tools/mm/page-types.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/mm/page-types.c +1 -1
index 66f429f2b698..7fc5a8be5997 100644
@@ -1261,7 +1261,7 @@ static const struct option opts[] = {
 	{ "no-summary", 0, NULL, 'N' },
 	{ "hwpoison"  , 0, NULL, 'X' },
 	{ "unpoison"  , 0, NULL, 'x' },
-	{ "kpageflags", 0, NULL, 'F' },
+	{ "kpageflags", 1, NULL, 'F' },
 	{ "help"      , 0, NULL, 'h' },
 	{ NULL        , 0, NULL, 0 }
 };
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/3] tools/mm/page-types: Fix ternary operator precedence in sigbus handler

In reply to Ye Liu
Quoted 18 lines · Ye Liu

From: Ye Liu <[email protected]>

The ternary operator (?:) has lower precedence than addition (+), so the expression off + sigbus_addr ? sigbus_addr - ptr : 0 was parsed as (off + sigbus_addr) ? (sigbus_addr - ptr) : 0 rather than the intended off + (sigbus_addr ? sigbus_addr - ptr : 0). Add explicit parentheses to ensure the correct evaluation order.

---
 tools/mm/page-types.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/mm/page-types.c +0 -0
index 6594245217a8..66f429f2b698 100644

tools/mm/page-types.c static void walk_file_range(const char *name, int fd, · line 1006

Quoted 8 lines
tools/mm/page-types.c +1 -1
@@ -1000,7 +1000,7 @@ static void walk_file_range(const char *name, int fd,
 			fatal("madvise failed: %s", name);
 
 		if (sigsetjmp(sigbus_jmp, 1)) {
-			end = off + sigbus_addr ? sigbus_addr - ptr : 0;
+			end = off + (sigbus_addr ? sigbus_addr - ptr : 0);
 			fprintf(stderr, "got sigbus at offset %lld: %s\n",
 					(long long)end, name);
 			goto got_sigbus;

"sigbus_addr - ptr" will give us the offset into the mmap(). Adding that to off will give us the offset into the file.

No idea what happens if we don't have sigbus_addr ...

Anyhow, your change looks good, I just have no idea whether updating "end" here is the right thing to do.

Signature · 3 lines
Cheers,

David
Acked-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] tools/mm/page-types: Fix kpageflags option argument in getopt_long

In reply to Ye Liu
Quoted 17 lines · Ye Liu

From: Ye Liu <[email protected]>

The --kpageflags option requires an argument to specify the kpageflags file path, but has_arg was set to 0 (no_argument) in the long options table. Change it to 1 (required_argument) so getopt_long correctly parses the argument.

---
 tools/mm/page-types.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/mm/page-types.c +0 -0
index 66f429f2b698..7fc5a8be5997 100644

tools/mm/page-types.c static const struct option opts[] = { · line 1267

Quoted 8 lines
tools/mm/page-types.c +1 -1
@@ -1261,7 +1261,7 @@ static const struct option opts[] = {
 	{ "no-summary", 0, NULL, 'N' },
 	{ "hwpoison"  , 0, NULL, 'X' },
 	{ "unpoison"  , 0, NULL, 'x' },
-	{ "kpageflags", 0, NULL, 'F' },
+	{ "kpageflags", 1, NULL, 'F' },
 	{ "help"      , 0, NULL, 'h' },
 	{ NULL        , 0, NULL, 0 }
 };
Signature · 3 lines
Cheers,

David
Acked-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] tools/mm/page-types: Fix ternary operator precedence in sigbus handler

In reply to Ye Liu
Quoted 9 lines · On Wed, 13 May 2026 10:21:17 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

The ternary operator (?:) has lower precedence than addition (+), so the expression off + sigbus_addr ? sigbus_addr - ptr : 0 was parsed as (off + sigbus_addr) ? (sigbus_addr - ptr) : 0 rather than the intended off + (sigbus_addr ? sigbus_addr - ptr : 0). Add explicit parentheses to ensure the correct evaluation order.

Thanks,
SJ

[...]

Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH 3/3] tools/mm/page-types: Fix kpageflags option argument in getopt_long

In reply to Ye Liu
Quoted 8 lines · On Wed, 13 May 2026 10:21:18 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

The --kpageflags option requires an argument to specify the kpageflags file path, but has_arg was set to 0 (no_argument) in the long options table. Change it to 1 (required_argument) so getopt_long correctly parses the argument.

Thanks,
SJ

[...]