public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: DJ Delorie <dj@redhat.com>
To: Szabolcs Nagy <szabolcs.nagy@arm.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 3/6] malloc: Use chunk2rawmem throughout
Date: Tue, 23 Mar 2021 16:25:25 -0400	[thread overview]
Message-ID: <xn35wlk47e.fsf@rhel8.vm> (raw)
In-Reply-To: <e24c5ca639c47eca08574b51f613762ff9338a1a.1616155129.git.szabolcs.nagy@arm.com> (message from Szabolcs Nagy on Fri, 19 Mar 2021 13:26:49 +0000)

Szabolcs Nagy <szabolcs.nagy@arm.com> writes:
> The difference between chunk2mem and chunk2rawmem is that the latter
> does not get the memory tag for the returned pointer.  It turns out
> chunk2rawmem almost always works:

Given that these two macros are identical on non-aarch64 systems, I'm
going to gloss over the "is it tagged correctly" question since you
folks can just test it, and other targets won't care ;-)

LGTM
Reviewed-by: DJ Delorie <dj@redhat.com>

> sysmalloc: Returns untagged memory.
> _int_malloc: Returns untagged memory.
> _int_free: Takes untagged memory.
> _int_memalign: Returns untagged memory.
> _int_realloc: Takes and returns tagged memory.

We should probably put this information in comments at each function
implementation too, but at least it's in the source files :-)

> -        newmem = chunk2mem (newp);
> +        newmem = tag_at (chunk2rawmem (newp));

Ok.

>     headers have distinct tags.  Converting fully from one to the other
>     involves extracting the tag at the other address and creating a
>     suitable pointer using it.  That can be quite expensive.  There are
> -   many occasions, though when the pointer will not be dereferenced
> -   (for example, because we only want to assert that the pointer is
> -   correctly aligned).  In these cases it is more efficient not
> -   to extract the tag, since the answer will be the same either way.
> -   chunk2rawmem() can be used in these cases.
> - */
> +   cases when the pointers are not dereferenced (for example only used
> +   for alignment check) so the tags are not relevant, and there are
> +   cases when user data is not tagged distinctly from malloc headers
> +   (user data is untagged because tagging is done late in malloc and
> +   early in free).  User memory tagging across internal interfaces:
> +
> +      sysmalloc: Returns untagged memory.
> +      _int_malloc: Returns untagged memory.
> +      _int_free: Takes untagged memory.
> +      _int_memalign: Returns untagged memory.
> +      _int_memalign: Returns untagged memory.
> +      _mid_memalign: Returns tagged memory.
> +      _int_realloc: Takes and returns tagged memory.
> +*/

Ok.

> -/* Convert a user mem pointer to a chunk address without correcting
> +/* Convert a chunk address to a user mem pointer without correcting
>     the tag.  */
>  #define chunk2rawmem(p) ((void*)((char*)(p) + CHUNK_HDR_SZ))

Heh.  Ok.

>  #define misaligned_chunk(p) \
> -  ((uintptr_t)(MALLOC_ALIGNMENT == CHUNK_HDR_SZ ? (p) : chunk2mem (p)) \
> +  ((uintptr_t)(MALLOC_ALIGNMENT == CHUNK_HDR_SZ ? (p) : chunk2rawmem (p)) \
>     & MALLOC_ALIGN_MASK)

Ok.

> -              return chunk2mem (p);
> +              return chunk2rawmem (p);

Ok.

> -      return chunk2mem (p);
> +      return chunk2rawmem (p);

Ok.

> -  uintptr_t mem = (uintptr_t) chunk2mem(p);
> +  uintptr_t mem = (uintptr_t) chunk2rawmem(p);

Ok.

> -  tcache_entry *e = (tcache_entry *) chunk2mem (chunk);
> +  tcache_entry *e = (tcache_entry *) chunk2rawmem (chunk);

Ok.

> -	  void *newmem = chunk2mem (newp);
> +	  void *newmem = tag_at (chunk2rawmem (newp));

Ok.

> -	      void *p = chunk2mem (victim);
> +	      void *p = chunk2rawmem (victim);

Ok.

> -          void *p = chunk2mem (victim);
> +          void *p = chunk2rawmem (victim);

Ok.

> -              void *p = chunk2mem (victim);
> +              void *p = chunk2rawmem (victim);

Ok.

> -              void *p = chunk2mem (victim);
> +              void *p = chunk2rawmem (victim);

Ok.

> -              void *p = chunk2mem (victim);
> +              void *p = chunk2rawmem (victim);

Ok.

> -              void *p = chunk2mem (victim);
> +              void *p = chunk2rawmem (victim);

Ok.

> -          void *p = chunk2mem (victim);
> +          void *p = chunk2rawmem (victim);

Ok.

> -	tcache_entry *e = (tcache_entry *) chunk2mem (p);
> +	tcache_entry *e = (tcache_entry *) chunk2rawmem (p);

Ok.

> -    free_perturb (chunk2mem(p), size - CHUNK_HDR_SZ);
> +    free_perturb (chunk2rawmem(p), size - CHUNK_HDR_SZ);

Ok.

> -    free_perturb (chunk2mem(p), size - CHUNK_HDR_SZ);
> +    free_perturb (chunk2rawmem(p), size - CHUNK_HDR_SZ);

Ok.

> -          return chunk2mem (newp);
> +          return chunk2rawmem (newp);

Ok.

> -  return chunk2mem (p);
> +  return chunk2rawmem (p);

Ok.


  reply	other threads:[~2021-03-23 20:25 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-19 13:25 [PATCH 0/6] malloc: more memory tagging optimizations Szabolcs Nagy
2021-03-19 13:26 ` [PATCH 1/6] malloc: Use memsize instead of CHUNK_AVAILABLE_SIZE Szabolcs Nagy
2021-03-23 20:01   ` DJ Delorie
2021-03-19 13:26 ` [PATCH 2/6] malloc: Use different tag after mremap Szabolcs Nagy
2021-03-23 20:03   ` DJ Delorie
2021-03-19 13:26 ` [PATCH 3/6] malloc: Use chunk2rawmem throughout Szabolcs Nagy
2021-03-23 20:25   ` DJ Delorie [this message]
2021-03-19 13:27 ` [PATCH 4/6] malloc: Rename chunk2rawmem Szabolcs Nagy
2021-03-23 20:43   ` DJ Delorie
2021-03-19 13:27 ` [PATCH 5/6] malloc: Remove unnecessary tagging around _mid_memalign Szabolcs Nagy
2021-03-23 20:44   ` DJ Delorie
2021-03-19 13:27 ` [PATCH 6/6] malloc: Ensure mtag code path in checked_request2size is cold Szabolcs Nagy
2021-03-23 20:46   ` DJ Delorie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xn35wlk47e.fsf@rhel8.vm \
    --to=dj@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=szabolcs.nagy@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).