[PATCH 1/4] fs/erofs: align the malloc'ed data
Michael Walle
mwalle at kernel.org
Tue Mar 24 02:08:04 AEDT 2026
Hi Gao,
On Mon Mar 23, 2026 at 3:41 PM CET, Gao Xiang wrote:
> Hi Michael,
>
> On 2026/3/23 21:42, Michael Walle wrote:
>> 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.
>
> Many thanks for the patch, I wonder if it's possible to
> submit the patches to erofs-utils first (even make
> malloc_cache_aligned() as another malloc() for example)?
>
> Since I'd like to make u-boot codebase following
> erofs-utils, but I don't think Jianan have the
> bandwidth now, but if you have some use cases,
> you could help syncing up a bit.
Sorry, I don't have the bandwidth neither. But now I see where all
this is coming from. In userspace, you have much less (or none at
all) restrictions. So not sure, if that even makes sense to share. As
you've already pointed out the malloc_cache_aligned() would have to
be changed (back!) to malloc() again.
> Or at least, let's keep these four patches in sync
> between erofs-utils and u-boot.
Not sure what a DMA alignment has to do with userspace, or do you
mean moving all the block data from stack to heap?
This is more or less the result from an internal evaluation. You (or
anybody else) might take it - or leave it.
Do you share code between linux and erofs-utils, too? How does that
work?
Thanks,
-michael
>
> Many thanks!
> Gao Xiang
>
>>
>> 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, ...) \
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 297 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/linux-erofs/attachments/20260323/1571f94a/attachment.sig>
More information about the Linux-erofs
mailing list