On Tue, Nov 17, 2026 at 8:47 AM Daniel P. Berrangé <[email protected]> wrote:
>
> On Tue, Nov 17, 2026 at 01:46:12PM +0000, Daniel P. Berrangé wrote:
> > On Mon, Nov 16, 2026 at 11:49:59AM -0500, John Snow wrote:
> > > Signed-off-by: John Snow <[email protected]>
> > > ---
> > >  meson.build              |  8 ++++++++
> > >  util/qemu-thread-posix.c | 30 ++++++++++++++++++++++++++++++
> > >  2 files changed, 38 insertions(+)
> > >
> > > diff --git a/meson.build b/meson.build
> > > index d7bcb581c18..ef961df2f64 100644
> > > --- a/meson.build
> > > +++ b/meson.build
> > > @@ -2878,7 +2878,15 @@ config_host_data.set('CONFIG_PTHREAD_GET_NAME_NP', 
> > > cc.links(osdep_prefix + '''
> > >      pthread_get_name_np(pthread_self(), buf, sizeof(buf));
> > >      return 0;
> > >    }''', dependencies: threads))
> > > +config_host_data.set('CONFIG_PTHREAD_GETATTR_NP', cc.links(osdep_prefix 
> > > + '''
> > > +  #include <pthread.h>
> > >
> > > +  int main(void)
> > > +  {
> > > +    pthread_attr_t attr;
> > > +    pthread_getattr_np(pthread_self(), &attr);
> > > +    return 0;
> > > +  }''', dependencies: threads))
> > >  config_host_data.set('CONFIG_PTHREAD_CONDATTR_SETCLOCK', 
> > > cc.links(osdep_prefix + '''
> > >    #include <pthread.h>
> > >
> > > diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
> > > index 089606c93fb..967e0f15f6d 100644
> > > --- a/util/qemu-thread-posix.c
> > > +++ b/util/qemu-thread-posix.c
> > > @@ -17,6 +17,7 @@
> > >  #include "qemu-thread-common.h"
> > >  #include "qemu/tsan.h"
> > >  #include "qemu/bitmap.h"
> > > +#include "qemu/mmap-vma.h"
> > >
> > >  #if defined(CONFIG_PTHREAD_SET_NAME_NP) || 
> > > defined(CONFIG_PTHREAD_GET_NAME_NP)
> > >  #include <pthread_np.h>
> > > @@ -362,6 +363,13 @@ static void qemu_thread_atexit_notify(void *arg)
> > >
> > >  void qemu_thread_set_name(const char *name)
> > >  {
> > > +# if defined(CONFIG_PTHREAD_GETATTR_NP)
> > > +    pthread_attr_t real_attr;
> > > +    void *pthread_addr;
> > > +    size_t stacksize;
> > > +    size_t guardsize;
> > > +# endif
> > > +
> > >      /*
> > >       * Attempt to set the thread name; note that this is for debug, so
> > >       * we're not going to fail if we can't set it.
> > > @@ -373,6 +381,28 @@ void qemu_thread_set_name(const char *name)
> > >  # elif defined(CONFIG_PTHREAD_SET_NAME_NP)
> > >      pthread_set_name_np(pthread_self(), name);
> > >  # endif
> > > +
> > > +# if defined(CONFIG_PTHREAD_GETATTR_NP)
> > > +    if (pthread_getattr_np(pthread_self(), &real_attr)) {
> > > +        return;
> > > +    }
> > > +
> > > +    if (pthread_attr_getguardsize(&real_attr, &guardsize)) {
> > > +        goto cleanup;
> > > +    }
> > > +
> > > +    if (pthread_attr_getstack(&real_attr, &pthread_addr, &stacksize)) {
> > > +        goto cleanup;
> > > +    }
> > > +
> > > +    /* Though there's a guard page, it isn't actually marked RO, so
> > > +     * just label the entire region */
> > > +    qemu_name_vma(pthread_addr - guardsize, stacksize + guardsize,
> > > +                  "pthread (%s)", name);
> >
> > On the one hand this code is pretty sketchy as it is making
> > some assumptions about glibc allocating thread stack. On
> > the other hand qemu_name_vma() is a no-op on non-Linux
> > platforms, and we ignore fails, so we'll degrade nicely if
> > any C libraries (musl) deal with thread stack differently.
> >
> > I feel that glibc itself really ought to set the VMA name
> > in response to pthread_set_name_np but I'm open to us doing
> > this in QEMU though as I think it is safe enough.

libc will label this, but it will give it a very generic name like
"malloc" or something like that. This is a "free upgrade", but you're
right that it is porcelain as it requires us to know a little bit
about the memory layout.

I'll add a better comment in-line here to warn folks that this is not
necessarily *super* reliable, though in testing it works great...

One concern I have is that even though there is a guard page
allocation here, nothing appears to set permissions on said guard
page: it remains fully RW. That's not a particularly effective guard
page, I'd have thought...

>
> Reviewed-by: Daniel P. Berrangé <[email protected]>
>
>
> With regards,
> Daniel
> --
> |: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
> |: https://libvirt.org          ~~          https://entangle-photo.org :|
> |: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
>


Reply via email to