[PATCH 1/4] fs/erofs: align the malloc'ed data

Michael Walle mwalle at kernel.org
Tue Mar 24 00:42:17 AEDT 2026


The data buffers are used to transfer from or to hardware peripherals.
Often, there are restrictions on addresses, i.e. they have to be aligned
at a certain size. Thus, allocate the data on the heap instead of the
stack (at a random address alignment). Use malloc_cache_aligned() to get
an aligned buffer.

Signed-off-by: Michael Walle <mwalle at kernel.org>
---
 fs/erofs/data.c     | 11 ++++-------
 fs/erofs/internal.h |  1 +
 2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/fs/erofs/data.c b/fs/erofs/data.c
index b58ec6fcc66..61dbae51a9a 100644
--- a/fs/erofs/data.c
+++ b/fs/erofs/data.c
@@ -319,15 +319,13 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
 		}
 
 		if (map.m_plen > bufsize) {
-			char *tmp;
-
 			bufsize = map.m_plen;
-			tmp = realloc(raw, bufsize);
-			if (!tmp) {
+			free(raw);
+			raw = malloc_cache_aligned(bufsize);
+			if (!raw) {
 				ret = -ENOMEM;
 				break;
 			}
-			raw = tmp;
 		}
 
 		ret = z_erofs_read_one_data(inode, &map, raw,
@@ -336,8 +334,7 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
 		if (ret < 0)
 			break;
 	}
-	if (raw)
-		free(raw);
+	free(raw);
 	return ret < 0 ? ret : 0;
 }
 
diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
index 1875f37fcd2..13c862325a6 100644
--- a/fs/erofs/internal.h
+++ b/fs/erofs/internal.h
@@ -11,6 +11,7 @@
 #include <linux/printk.h>
 #include <linux/log2.h>
 #include <inttypes.h>
+#include <memalign.h>
 #include "erofs_fs.h"
 
 #define erofs_err(fmt, ...)	\
-- 
2.47.3



More information about the Linux-erofs mailing list