[v2,1/3] malloc: Add sysdep hooks for request and usable sizes

Message ID 20260930084409.3965-2-muhammad.kamran@arm.com (mailing list archive)
State Superseded
Headers
Series aarch64: Add malloc MTE support |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

Muhammad Kamran Sept. 30, 2026, 8:44 a.m. UTC
  Add sysdep hooks for the malloc request-size and usable-size
calculations.  The default values preserve the existing generic
layout.
---
 malloc/malloc.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
  

Comments

Wilco Dijkstra Sept. 30, 2026, 6:40 p.m. UTC | #1
Hi Muhammad,

+#ifndef SYSDEP_EXTRA_HEADER_SIZE
+#define SYSDEP_EXTRA_HEADER_SIZE 0
+#endif

I would use a single define that is either SIZE_SZ (by default) or CHUNK_HDR_SIZE (for MTE)
and then use that in checked_request2size and chunksize2usable. Not sure what would be a
good name, perhaps CHUNK_OVERHEAD?

Also tidx2usize() will need adjusting. And clear_memory hardcodes the overhead and clears
an odd multiple of SIZE_SZ which is incorrect with MTE. It's worth checking the other uses of
SIZE_SZ too. There are a few incorrect uses of CHUNK_HDR_SIZE (one of which will be fixed by
Sam's mmap fix).

Cheers,
Wilco
  
Muhammad Kamran Oct. 5, 2026, 12:17 p.m. UTC | #2
Hi Wilco,

> +#ifndef SYSDEP_EXTRA_HEADER_SIZE
> +#define SYSDEP_EXTRA_HEADER_SIZE 0
> +#endif
> 
> I would use a single define that is either SIZE_SZ (by default) or CHUNK_HDR_SIZE (for MTE)
> and then use that in checked_request2size and chunksize2usable. Not sure what would be a
> good name, perhaps CHUNK_OVERHEAD?
> 

Thanks, I agree. In v3 [1] I replaced the two size hooks with a single
hidden-header-size hook: it defaults to SIZE_SZ, and AArch64 Linux
overrides it to CHUNK_HDR_SZ for MTE.  I've named it
SYSDEP_MALLOC_HIDDEN_HEADER_SIZE for now but happy to rename it if
needed.

> Also tidx2usize() will need adjusting. And clear_memory hardcodes the overhead and clears
> an odd multiple of SIZE_SZ which is incorrect with MTE. It's worth checking the other uses of
> SIZE_SZ too. There are a few incorrect uses of CHUNK_HDR_SIZE (one of which will be fixed by
> Sam's mmap fix).
>

I have updated checked_request2size, chunksize2usable, and tidx2usize to
use the new hook, and changed the small calloc clear helper to fall back
to memset when the usable size is smaller than the unrolled clear path
supports.

Thanks,
Kamran

[1] https://patchwork.sourceware.org/project/glibc/list/?series=67434
  

Patch

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 011ecd6870..21647e3794 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1005,15 +1005,22 @@  nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    value is less than PTRDIFF_T.  Returns the requested size or
    MINSIZE in case the value is less than MINSIZE, or SIZE_MAX if any
    of the previous checks fail.  */
+
+#ifndef SYSDEP_EXTRA_HEADER_SIZE
+#define SYSDEP_EXTRA_HEADER_SIZE 0
+#endif
+
 static __always_inline size_t
 checked_request2size (size_t req)
 {
   if (__glibc_unlikely (req > PTRDIFF_MAX))
     return SIZE_MAX;
 
-  return (req + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE
-	  ? MINSIZE
-	  : (req + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK);
+  size_t rs = req + SIZE_SZ + SYSDEP_EXTRA_HEADER_SIZE + MALLOC_ALIGN_MASK;
+
+  return (rs < MINSIZE
+          ? MINSIZE
+          : (rs) & ~MALLOC_ALIGN_MASK);
 }
 
 /*
@@ -1115,7 +1122,10 @@  checked_request2size (size_t req)
 
 /* This is the size of the real usable data in the chunk.  Not valid for
    dumped heap chunks.  */
-#define chunksize2usable(n) (n - CHUNK_HDR_SZ + SIZE_SZ)
+#ifndef SYSDEP_MALLOC_USABLE_SIZE_OFFSET
+#define SYSDEP_MALLOC_USABLE_SIZE_OFFSET SIZE_SZ
+#endif
+#define chunksize2usable(n) (n - CHUNK_HDR_SZ + SYSDEP_MALLOC_USABLE_SIZE_OFFSET)
 #define memsize(p) (chunksize2usable (chunksize (p)))
 
 /* Huge page used for an mmap chunk.  */