Skip to contentSkip to conversation

hugeshmat04: Restore the correct shmmax value

PATCH In review Leo Yu-Chi Liang 2 participants 3 messages Aug 3, 2023 – Aug 5, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hugeshmat04: Restore the correct shmmax value

The type of shmmax is unsinged long in Linux kernel and the default value is ((~0UL) - (1UL << 24)). If we try to access this value with %ld format string in scanf-like function, we end up getting different incorrect values depends on the implementation of different libc.

Fix this by accessing shmmax with the correct %lu format string.

---
 testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c +5 -5
index 50efa8a52..b3e7c272a 100644
@@ -83,12 +83,12 @@ static void setup(void)
 	long hpage_size, orig_hugepages;
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
-	SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &orig_shmmax);
-	SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", (long)SIZE);
-	SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &new_shmmax);
+	SAFE_FILE_SCANF(PATH_SHMMAX, "%lu", &orig_shmmax);
+	SAFE_FILE_PRINTF(PATH_SHMMAX, "%lu", (long)SIZE);
+	SAFE_FILE_SCANF(PATH_SHMMAX, "%lu", &new_shmmax);
 
 	if (new_shmmax < SIZE)
-		tst_brk(TCONF,	"shmmax too low, have: %ld", new_shmmax);
+		tst_brk(TCONF,	"shmmax too low, have: %lu", new_shmmax);
 
 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
@@ -99,7 +99,7 @@ static void setup(void)
 static void cleanup(void)
 {
 	if (orig_shmmax != -1)
-		SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", orig_shmmax);
+		SAFE_FILE_PRINTF(PATH_SHMMAX, "%lu", orig_shmmax);
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi!

Quoted 27 lines
---
 testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c +4 -4
index 50efa8a52..b3e7c272a 100644
@@ -83,12 +83,12 @@ static void setup(void)
 	long hpage_size, orig_hugepages;
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
-	SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &orig_shmmax);
-	SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", (long)SIZE);
-	SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &new_shmmax);
+	SAFE_FILE_SCANF(PATH_SHMMAX, "%lu", &orig_shmmax);
+	SAFE_FILE_PRINTF(PATH_SHMMAX, "%lu", (long)SIZE);
+	SAFE_FILE_SCANF(PATH_SHMMAX, "%lu", &new_shmmax);
 
 	if (new_shmmax < SIZE)
-		tst_brk(TCONF,	"shmmax too low, have: %ld", new_shmmax);
+		tst_brk(TCONF,	"shmmax too low, have: %lu", new_shmmax);
 
 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 

testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c static void setup(void) · line 103

Quoted 6 lines
testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c +1 -1
@@ -99,7 +99,7 @@ static void setup(void)
 static void cleanup(void)
 {
 	if (orig_shmmax != -1)
-		SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", orig_shmmax);
+		SAFE_FILE_PRINTF(PATH_SHMMAX, "%lu", orig_shmmax);
 }

Can we please move the PATH_SHMMAX restoration to the .save_restore part of the tst_test structure?

https://github.com/linux-test-project/ltp/wiki/C-Test-API#127-saving--restoring-procsys-values

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#2 lore raw

Hi Cyril,

Quoted 38 lines · Cyril Hrubis

Hi!

Quoted 27 lines
---
 testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c +4 -4
index 50efa8a52..b3e7c272a 100644
@@ -83,12 +83,12 @@ static void setup(void)
 	long hpage_size, orig_hugepages;
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
-	SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &orig_shmmax);
-	SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", (long)SIZE);
-	SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &new_shmmax);
+	SAFE_FILE_SCANF(PATH_SHMMAX, "%lu", &orig_shmmax);
+	SAFE_FILE_PRINTF(PATH_SHMMAX, "%lu", (long)SIZE);
+	SAFE_FILE_SCANF(PATH_SHMMAX, "%lu", &new_shmmax);
 
 	if (new_shmmax < SIZE)
-		tst_brk(TCONF,	"shmmax too low, have: %ld", new_shmmax);
+		tst_brk(TCONF,	"shmmax too low, have: %lu", new_shmmax);
 
 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 

testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c static void setup(void) · line 103

Quoted 6 lines
testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c +1 -1
@@ -99,7 +99,7 @@ static void setup(void)
 static void cleanup(void)
 {
 	if (orig_shmmax != -1)
-		SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", orig_shmmax);
+		SAFE_FILE_PRINTF(PATH_SHMMAX, "%lu", orig_shmmax);
 }

Can we please move the PATH_SHMMAX restoration to the .save_restore part of the tst_test structure?

Quoted 1 line

https://github.com/linux-test-project/ltp/wiki/C-Test-API#127-saving--restoring-procsys-values

Thanks for the pointer.
I will send a v2 patch soon!

Best regards,
Leo

Quoted 4 lines
Signature · 3 lines
-- 
Cyril Hrubis
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp