Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: John Snow <jsnow@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Peter Xu" <peterx@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"David Gibson" <david@gibson.dropbear.id.au>,
	"Alistair Francis" <alistair.francis@wdc.com>,
	"Stefan Weil" <sw@weilnetz.de>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Fabiano Rosas" <farosas@suse.de>,
	"Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>,
	"Pierrick Bouvier" <pierrick.bouvier@oss.qualcomm.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	kvm@vger.kernel.org,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Dmitry Osipenko" <dmitry.osipenko@collabora.com>,
	"Akihiko Odaki" <odaki@rsg.ci.i.u-tokyo.ac.jp>,
	"John Snow" <jsnow@redhat.com>
Subject: [PATCH 1/7] debug/mem: add utility to name anonymous memory mappings
Date: Mon,  9 Nov 2026 13:59:01 -0500	[thread overview]
Message-ID: <20261109185907.3220584-2-jsnow@redhat.com> (raw)
In-Reply-To: <20261109185907.3220584-1-jsnow@redhat.com>

Add support to name mmap'd memory regions using
PR_SET_VMA_ANON_NAME. On supported platforms, the QEMU utility
functions are no-ops.

This is useful primarily for debugging and profiling QEMU; beind able
to see and correlate each VMA segment with a specific use within QEMU.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 include/qemu/mmap-vma.h | 18 +++++++++++
 util/mmap-vma.c         | 67 +++++++++++++++++++++++++++++++++++++++++
 util/meson.build        |  1 +
 3 files changed, 86 insertions(+)
 create mode 100644 include/qemu/mmap-vma.h
 create mode 100644 util/mmap-vma.c

diff --git a/include/qemu/mmap-vma.h b/include/qemu/mmap-vma.h
new file mode 100644
index 00000000000..763ea5a5cab
--- /dev/null
+++ b/include/qemu/mmap-vma.h
@@ -0,0 +1,18 @@
+/*
+ * Utility for naming VMA allocations in linux
+ *
+ * Copyright (c) Red Hat, Inc 2026
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#ifndef __QEMU_MMAP_VMA_H
+#define __QEMU_MMAP_VMA_H
+
+#include <stddef.h>
+#include "osdep.h"
+
+int qemu_name_vma(void *ptr, size_t sz, const char *fmt, ...)
+    G_GNUC_PRINTF(3, 4);
+
+#endif
diff --git a/util/mmap-vma.c b/util/mmap-vma.c
new file mode 100644
index 00000000000..7dcef3b7455
--- /dev/null
+++ b/util/mmap-vma.c
@@ -0,0 +1,67 @@
+/*
+ * Utility for naming VMA allocations in linux
+ *
+ * Copyright (c) Red Hat, Inc 2026
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+#include "qemu/mmap-vma.h"
+#include "qemu/osdep.h"
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+
+#if defined(__linux__)
+#include <sys/prctl.h>
+#endif
+
+#define VMA_PREFIX " qemu: "
+#define VMA_MAXLEN 80
+
+/*
+ * Give a human-readable name to a previously mmap'd memory segment.
+ * This feature is only available on Linux >= 5.17, and only when the
+ * kernel was configured with CONFIG_ANON_VMA_NAME=y.
+ *
+ * For systems without this option, prctl() will return -1 and set
+ * errno to EINVAL, and can be safely ignored.
+ *
+ * For systems with this option, these human-readable names will be
+ * visible in /proc/$pid/[s]maps, pmap output, etc.
+ */
+G_GNUC_PRINTF(3, 0)
+static int qemu_vname_vma(void *ptr, size_t sz, const char *fmt, va_list args)
+{
+    int rc = 0;
+
+#if defined(PR_SET_VMA_ANON_NAME)
+    char strbuf[VMA_MAXLEN];
+
+    assert(sizeof(VMA_PREFIX) < sizeof(strbuf));
+    strncpy(strbuf, VMA_PREFIX, sizeof(strbuf));
+    rc = vsnprintf(strbuf + sizeof(VMA_PREFIX),
+                   sizeof(strbuf) - sizeof(VMA_PREFIX), fmt, args);
+    if (rc < 0) {
+        return -errno;
+    } else if (rc >= (sizeof(strbuf) - sizeof(VMA_PREFIX))) {
+        sprintf(strbuf + sizeof(strbuf) - 4, "...");
+    }
+
+    rc = prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, sz, strbuf);
+    if (rc) {
+        return -errno;
+    }
+#endif
+
+    return rc;
+}
+
+G_GNUC_PRINTF(3, 4)
+int qemu_name_vma(void *ptr, size_t sz, const char *fmt, ...)
+{
+    va_list args;
+
+    va_start(args, fmt);
+    return qemu_vname_vma(ptr, sz, fmt, args);
+}
diff --git a/util/meson.build b/util/meson.build
index fa174c07a57..861ee228d5b 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -1,5 +1,6 @@
 util_ss.add(files('osdep.c', 'cutils.c', 'unicode.c', 'qemu-timer-common.c'))
 util_ss.add(files('thread-context.c'), numa)
+util_ss.add(files('mmap-vma.c'))
 if host_os != 'windows'
   util_ss.add(files('aio-posix.c'))
   util_ss.add(files('fdmon-poll.c'))
-- 
2.56.0


  reply	other threads:[~2026-11-09 18:59 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-09 18:59 [PATCH 0/7] debug: add PR_SET_VMA_ANON_NAME support John Snow
2026-11-09 18:59 ` John Snow [this message]
2026-11-09 19:29   ` [PATCH 1/7] debug/mem: add utility to name anonymous memory mappings Daniel P. Berrangé
2026-11-10 15:04     ` John Snow
2026-11-09 18:59 ` [PATCH 2/7] debug/mem: add tracing to qemu_vname_vma() John Snow
2026-11-09 19:30   ` Daniel P. Berrangé
2026-11-09 18:59 ` [PATCH 3/7] debug/mem: name <reserved> regions in mmap-alloc John Snow
2026-11-09 19:31   ` Daniel P. Berrangé
2026-11-09 18:59 ` [PATCH 4/7] debug/mem: name pthreads John Snow
2026-11-09 18:59 ` [PATCH 5/7] debug/mem: name coroutine stacks John Snow
2026-11-09 19:34   ` Daniel P. Berrangé
2026-11-09 18:59 ` [PATCH 6/7] debug/mem: name qemu_anon_ram_alloc/qemu_ram_mmap pages John Snow
2026-11-09 19:35   ` Daniel P. Berrangé
2026-11-09 18:59 ` [PATCH 7/7] debug/mem: HACK add qemu_name_buffer John Snow

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=20261109185907.3220584-2-jsnow@redhat.com \
    --to=jsnow@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=alistair.francis@wdc.com \
    --cc=berrange@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=dmitry.osipenko@collabora.com \
    --cc=farosas@suse.de \
    --cc=kvm@vger.kernel.org \
    --cc=marcandre.lureau@redhat.com \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=odaki@rsg.ci.i.u-tokyo.ac.jp \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=philmd@oss.qualcomm.com \
    --cc=pierrick.bouvier@oss.qualcomm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=sw@weilnetz.de \
    /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