Skip to contentSkip to conversation

selftests/mm: fix size truncation in pagemap_ioctl test

PATCH Superseded Zenghui Yu 3 participants 5 messages Sep 7 – Sep 8

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Zenghui Yu

author
#0 lore raw

PATCH 1/1 selftests/mm: fix size truncation in pagemap_ioctl test

From: "Zenghui Yu (Huawei)" <[email protected]>

On arm64 with 64K base pages, the huge page size is 512 MiB, and hpage_unit_tests() builds a 5 GiB range (10 * 512 MiB) for its tests. This exceeds the range of the int size parameters of gethugepage(), wp_addr_range() and pagemap_ioctl(). The implicit truncation to 1 GiB makes gethugepage() allocate a too small buffer, while the callers keep operating on the original 5 GiB range, resulting in spurious failures or SIGSEGV.

Change those size parameters to size_t.

Fixes 46fd75d4a3c9 ("selftests: mm: add pagemap ioctl tests")
Assisted-by GLM-5.3 OpenCode
---
 tools/testing/selftests/mm/pagemap_ioctl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/pagemap_ioctl.c +3 -3
index eadc7159ca5b..48cbea3a33c9 100644
@@ -44,7 +44,7 @@ const char *progname;
 
 #define LEN(region)	((region.end - region.start)/page_size)
 
-static long pagemap_ioctl(void *start, int len, void *vec, int vec_len, int flag,
+static long pagemap_ioctl(void *start, size_t len, void *vec, int vec_len, int flag,
 			  int max_pages, long required_mask, long anyof_mask, long excluded_mask,
 			  long return_mask)
 {
@@ -152,7 +152,7 @@ int wp_free(void *addr, long size)
 	return 0;
 }
 
-int wp_addr_range(void *addr, int size)
+int wp_addr_range(void *addr, size_t size)
 {
 	if (pagemap_ioctl(addr, size, NULL, 0,
 			  PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC,
@@ -787,7 +787,7 @@ int base_tests(char *prefix, char *mem, unsigned long long mem_size, int skip)
 	return 0;
 }
 
-void *gethugepage(int map_size)
+void *gethugepage(size_t map_size)
 {
 	int ret;
 	char *map;
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Zenghui Yu

From: "Zenghui Yu (Huawei)" <[email protected]>

On arm64 with 64K base pages, the huge page size is 512 MiB, and hpage_unit_tests() builds a 5 GiB range (10 * 512 MiB) for its tests. This exceeds the range of the int size parameters of gethugepage(), wp_addr_range() and pagemap_ioctl(). The implicit truncation to 1 GiB makes gethugepage() allocate a too small buffer, while the callers keep operating on the original 5 GiB range, resulting in spurious failures or SIGSEGV.

Quoted 5 lines

Change those size parameters to size_t.

Fixes 46fd75d4a3c9 ("selftests: mm: add pagemap ioctl tests")
Assisted-by GLM-5.3 OpenCode
Quoted 38 lines
---
 tools/testing/selftests/mm/pagemap_ioctl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/pagemap_ioctl.c +3 -3
index eadc7159ca5b..48cbea3a33c9 100644
@@ -44,7 +44,7 @@ const char *progname;
 
 #define LEN(region)	((region.end - region.start)/page_size)
 
-static long pagemap_ioctl(void *start, int len, void *vec, int vec_len, int flag,
+static long pagemap_ioctl(void *start, size_t len, void *vec, int vec_len, int flag,
 			  int max_pages, long required_mask, long anyof_mask, long excluded_mask,
 			  long return_mask)
 {
@@ -152,7 +152,7 @@ int wp_free(void *addr, long size)
 	return 0;
 }
 
-int wp_addr_range(void *addr, int size)
+int wp_addr_range(void *addr, size_t size)
 {
 	if (pagemap_ioctl(addr, size, NULL, 0,
 			  PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC,
@@ -787,7 +787,7 @@ int base_tests(char *prefix, char *mem, unsigned long long mem_size, int skip)
 	return 0;
 }
 
-void *gethugepage(int map_size)
+void *gethugepage(size_t map_size)
 {
 	int ret;
 	char *map;
Signature · 1 line
2.53.0
#2 lore raw
In reply to Zenghui Yu
Quoted 10 lines · Zenghui Yu

From: "Zenghui Yu (Huawei)" <[email protected]>

On arm64 with 64K base pages, the huge page size is 512 MiB, and hpage_unit_tests() builds a 5 GiB range (10 * 512 MiB) for its tests. This exceeds the range of the int size parameters of gethugepage(), wp_addr_range() and pagemap_ioctl(). The implicit truncation to 1 GiB makes gethugepage() allocate a too small buffer, while the callers keep operating on the original 5 GiB range, resulting in spurious failures or SIGSEGV.

Quoted 6 lines

Change those size parameters to size_t.

Fixes 46fd75d4a3c9 ("selftests: mm: add pagemap ioctl tests")
Assisted-by GLM-5.3 OpenCode

---

Looking at pagemap_ioctl.c ... I think most functions should actually be "static".

Using size_t is ok. But then we should also change "unsigned long long mem_size" to size_t, no?

Also, in wp_free() we are suddenly using "long size". In pagemap_ioc() we're also still using "int len".

Can we consistently use one type please?

Signature · 3 lines
Cheers,

David
author
#3 lore raw
Quoted 11 lines · David Hildenbrand (Arm)
Quoted 10 lines · Zenghui Yu

From: "Zenghui Yu (Huawei)" <[email protected]>

On arm64 with 64K base pages, the huge page size is 512 MiB, and hpage_unit_tests() builds a 5 GiB range (10 * 512 MiB) for its tests. This exceeds the range of the int size parameters of gethugepage(), wp_addr_range() and pagemap_ioctl(). The implicit truncation to 1 GiB makes gethugepage() allocate a too small buffer, while the callers keep operating on the original 5 GiB range, resulting in spurious failures or SIGSEGV.

Quoted 8 lines
Quoted 6 lines

Change those size parameters to size_t.

Fixes 46fd75d4a3c9 ("selftests: mm: add pagemap ioctl tests")
Assisted-by GLM-5.3 OpenCode

---

Looking at pagemap_ioctl.c ... I think most functions should actually be "static".

Yup.

Quoted 7 lines

Using size_t is ok. But then we should also change "unsigned long long mem_size" to size_t, no?

Also, in wp_free() we are suddenly using "long size". In pagemap_ioc() we're also still using "int len".

Can we consistently use one type please?

Yes we can. This patch adopts a minimal fix for this specific issue. I'll unify all size-related parameters and variables to size_t in v2.

Thanks,
Zenghui

#4 lore raw
Quoted 26 lines · Zenghui Yu
Quoted 11 lines · David Hildenbrand (Arm)
Quoted 10 lines · Zenghui Yu

From: "Zenghui Yu (Huawei)" <[email protected]>

On arm64 with 64K base pages, the huge page size is 512 MiB, and hpage_unit_tests() builds a 5 GiB range (10 * 512 MiB) for its tests. This exceeds the range of the int size parameters of gethugepage(), wp_addr_range() and pagemap_ioctl(). The implicit truncation to 1 GiB makes gethugepage() allocate a too small buffer, while the callers keep operating on the original 5 GiB range, resulting in spurious failures or SIGSEGV.

Quoted 8 lines
Quoted 6 lines

Change those size parameters to size_t.

Fixes 46fd75d4a3c9 ("selftests: mm: add pagemap ioctl tests")
Assisted-by GLM-5.3 OpenCode

---

Looking at pagemap_ioctl.c ... I think most functions should actually be "static".

Yup.

Quoted 3 lines

Using size_t is ok. But then we should also change "unsigned long long mem_size" to size_t, no?

Quoted 6 lines
Quoted 4 lines

Also, in wp_free() we are suddenly using "long size". In pagemap_ioc() we're also still using "int len".

Can we consistently use one type please?

Yes we can. This patch adopts a minimal fix for this specific issue.

Right, but for this selftest in particular we don't need a minimal fix. :)

Signature · 3 lines
Cheers,

David