Skip to contentSkip to conversation

f2fs_io: adapt w/ page_size in aligned_xalloc()

PATCH Superseded Chao Yu via Linux-f2fs-devel 2 participants 3 messages Jun 16 – Jun 17

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs_io: adapt w/ page_size in aligned_xalloc()

When allocating memory in aligned_xalloc(), if the requested alignment (e.g., F2FS_DEFAULT_BLKSIZE) is smaller than the system's page size, aligned_alloc() will allocate memory that is not page-aligned on systems with 16KB or 64KB page sizes.

As a result, subsequent calls to madvise(..., MADV_HUGEPAGE) will fail because madvise() requires the memory address and length to be page-aligned.

Fix this by dynamically adjusting the alignment and rounding up the requested allocation size (via roundup()) to the system's page size, ensuring that memory is correctly page-aligned for madvise(). Also update madvise() failure to call die() for better error reporting.

---
 tools/f2fs_io/f2fs_io.c | 17 +++++++++++++++++
 tools/f2fs_io/f2fs_io.h |  3 +++
 2 files changed, 20 insertions(+)
tools/f2fs_io/f2fs_io.c +17 -0
index 94e61b8..9a866cc 100644
@@ -97,6 +97,23 @@ static void *xmalloc(size_t size)
 
 static void *aligned_xalloc(size_t alignment, size_t size)
 {
+	long page_size = F2FS_DEFAULT_BLKSIZE;
+
+#ifdef _SC_PAGESIZE
+	page_size = sysconf(_SC_PAGESIZE);
+	if (page_size < 0)
+		page_size = F2FS_DEFAULT_BLKSIZE;
+#endif
+
+	/*
+	 * On systems with large page sizes (e.g., 16KB/64KB), alignment and
+	 * allocation size must be page-aligned to satisfy madvise().
+	 */
+	if (alignment < (size_t)page_size)
+		alignment = page_size;
+
+	size = roundup(size, alignment);
+
 	void *p = aligned_alloc(alignment, size);
 
 	if (!p)
tools/f2fs_io/f2fs_io.h +3 -0
index 539964f..cf1c334 100644
@@ -49,6 +49,9 @@ typedef u32	__be32;
 #endif
 
 #define F2FS_DEFAULT_BLKSIZE	4096
+#ifndef roundup
+#define roundup(x, y)		((((x) + ((y) - 1)) / (y)) * (y))
+#endif
 #define NEW_ADDR	0xFFFFFFFF
 
 #ifndef FS_IOC_GETFLAGS
Signature · 8 lines
2.49.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 6 lines · 于2026年6月16日周二 20:39写道:

When allocating memory in aligned_xalloc(), if the requested alignment (e.g., F2FS_DEFAULT_BLKSIZE) is smaller than the system's page size, aligned_alloc() will allocate memory that is not page-aligned on systems with 16KB or 64KB page sizes.

Quoted 7 lines

As a result, subsequent calls to madvise(..., MADV_HUGEPAGE) will fail because madvise() requires the memory address and length to be page-aligned.

Fix this by dynamically adjusting the alignment and rounding up the requested allocation size (via roundup()) to the system's page size, ensuring that memory is correctly page-aligned for madvise(). Also update madvise() failure to call die() for better error reporting.

Hi Chao,
don't see this part in current patch? "update madvise() failure to call die() for better error reporting"
thanks!

Quoted 54 lines
---
 tools/f2fs_io/f2fs_io.c | 17 +++++++++++++++++
 tools/f2fs_io/f2fs_io.h |  3 +++
 2 files changed, 20 insertions(+)
tools/f2fs_io/f2fs_io.c +17 -0
index 94e61b8..9a866cc 100644
@@ -97,6 +97,23 @@ static void *xmalloc(size_t size)
 
 static void *aligned_xalloc(size_t alignment, size_t size)
 {
+       long page_size = F2FS_DEFAULT_BLKSIZE;
+
+#ifdef _SC_PAGESIZE
+       page_size = sysconf(_SC_PAGESIZE);
+       if (page_size < 0)
+               page_size = F2FS_DEFAULT_BLKSIZE;
+#endif
+
+       /*
+        * On systems with large page sizes (e.g., 16KB/64KB), alignment and
+        * allocation size must be page-aligned to satisfy madvise().
+        */
+       if (alignment < (size_t)page_size)
+               alignment = page_size;
+
+       size = roundup(size, alignment);
+
        void *p = aligned_alloc(alignment, size);
 
        if (!p)
tools/f2fs_io/f2fs_io.h +3 -1
index 539964f..cf1c334 100644
@@ -49,6 +49,9 @@ typedef u32   __be32;
 #endif
 
 #define F2FS_DEFAULT_BLKSIZE   4096
+#ifndef roundup
+#define roundup(x, y)          ((((x) + ((y) - 1)) / (y)) * (y))
+#endif
 #define NEW_ADDR       0xFFFFFFFF
 
 #ifndef FS_IOC_GETFLAGS
--

2.49.0

Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#2 lore raw
Quoted 11 lines · Zhiguo Niu

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 9 lines · 于2026年6月16日周二 20:39写道:

When allocating memory in aligned_xalloc(), if the requested alignment (e.g., F2FS_DEFAULT_BLKSIZE) is smaller than the system's page size, aligned_alloc() will allocate memory that is not page-aligned on systems with 16KB or 64KB page sizes.

As a result, subsequent calls to madvise(..., MADV_HUGEPAGE) will fail because madvise() requires the memory address and length to be page-aligned.

Quoted 7 lines
Quoted 4 lines

Fix this by dynamically adjusting the alignment and rounding up the requested allocation size (via roundup()) to the system's page size, ensuring that memory is correctly page-aligned for madvise(). Also update madvise() failure to call die() for better error reporting.

Hi Chao,
don't see this part in current patch? "update madvise() failure to call die() for better error reporting"

Oh, I missed to update the commit message when squashing two separated patches... please ignore this comment, sorry.

Thanks,

Quoted 56 lines

thanks!

Quoted 54 lines
---
 tools/f2fs_io/f2fs_io.c | 17 +++++++++++++++++
 tools/f2fs_io/f2fs_io.h |  3 +++
 2 files changed, 20 insertions(+)
tools/f2fs_io/f2fs_io.c +17 -0
index 94e61b8..9a866cc 100644
@@ -97,6 +97,23 @@ static void *xmalloc(size_t size)
 
 static void *aligned_xalloc(size_t alignment, size_t size)
 {
+       long page_size = F2FS_DEFAULT_BLKSIZE;
+
+#ifdef _SC_PAGESIZE
+       page_size = sysconf(_SC_PAGESIZE);
+       if (page_size < 0)
+               page_size = F2FS_DEFAULT_BLKSIZE;
+#endif
+
+       /*
+        * On systems with large page sizes (e.g., 16KB/64KB), alignment and
+        * allocation size must be page-aligned to satisfy madvise().
+        */
+       if (alignment < (size_t)page_size)
+               alignment = page_size;
+
+       size = roundup(size, alignment);
+
        void *p = aligned_alloc(alignment, size);
 
        if (!p)
tools/f2fs_io/f2fs_io.h +3 -1
index 539964f..cf1c334 100644
@@ -49,6 +49,9 @@ typedef u32   __be32;
 #endif
 
 #define F2FS_DEFAULT_BLKSIZE   4096
+#ifndef roundup
+#define roundup(x, y)          ((((x) + ((y) - 1)) / (y)) * (y))
+#endif
 #define NEW_ADDR       0xFFFFFFFF
 
 #ifndef FS_IOC_GETFLAGS
--

2.49.0

Quoted 4 lines
Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel