Skip to contentSkip to conversation

selftests/mm: Replace obsolete memalign() with posix_memalign()

PATCH In review Deming Wang 2 participants 2 messages Apr 12, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/mm: Replace obsolete memalign() with posix_memalign()

memalign() is obsolete according to its manpage.

Replace memalign() with posix_memalign() and remove malloc.h include that was there for memalign().

As a pointer is passed into posix_memalign(), initialize *p to NULL to silence a warning about the function's return value being used as uninitialized (which is not valid anyway because the error is properly checked before p is returned).

---
 tools/testing/selftests/mm/soft-dirty.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/mm/soft-dirty.c +2 -2
index 21d8830c5f24..4bb7421141a2 100644
@@ -80,8 +80,8 @@ static void test_hugepage(int pagemap_fd, int pagesize)
 	int i, ret;
 	size_t hpage_len = read_pmd_pagesize();
 
-	map = memalign(hpage_len, hpage_len);
-	if (!map)
+	ret = posix_memalign((void *)(&map), hpage_len, hpage_len);
+	if (ret < 0)
 		ksft_exit_fail_msg("memalign failed\n");
 
 	ret = madvise(map, hpage_len, MADV_HUGEPAGE);
Signature · 1 line
2.27.0
#1 lore raw
Quoted 9 lines · Deming Wang

memalign() is obsolete according to its manpage.

Replace memalign() with posix_memalign() and remove malloc.h include that was there for memalign().

As a pointer is passed into posix_memalign(), initialize *p to NULL to silence a warning about the function's return value being used as uninitialized (which is not valid anyway because the error is properly checked before p is returned).

I don't follow how that comment here applies to the patch. What is p? Where is it initialized to NULL?

Quoted 11 lines
---
  tools/testing/selftests/mm/soft-dirty.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/mm/soft-dirty.c +0 -0
index 21d8830c5f24..4bb7421141a2 100644

tools/testing/selftests/mm/soft-dirty.c static void test_hugepage(int pagemap_fd, int pagesize) · line 83

Quoted 6 lines
tools/testing/selftests/mm/soft-dirty.c +1 -2
@@ -80,8 +80,8 @@ static void test_hugepage(int pagemap_fd, int pagesize)
  	int i, ret;
  	size_t hpage_len = read_pmd_pagesize();
 
-	map = memalign(hpage_len, hpage_len);
-	if (!map)
+	ret = posix_memalign((void *)(&map), hpage_len, hpage_len);

posix_memalign expects an "void **memptr", casting to "void *" looks weird. Further, you can drop the parentheses around &map.

ret = posix_memalign((void **)&map, hpage_len, hpage_len);

tools/testing/selftests/mm/soft-dirty.c static void test_hugepage(int pagemap_fd, int pagesize) · line 85

Quoted 2 lines
tools/testing/selftests/mm/soft-dirty.c +1 -0
+	if (ret < 0)
  		ksft_exit_fail_msg("memalign failed\n");

Better adjust the comment to "posix_memalign() failed\n"

Signature · 3 lines
Thanks,

David / dhildenb