From: John Snow <jsnow@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Daniel P. Berrangé" <berrange@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
kvm@vger.kernel.org,
"Dmitry Osipenko" <dmitry.osipenko@collabora.com>,
"Fabiano Rosas" <farosas@suse.de>,
"Alistair Francis" <alistair.francis@wdc.com>,
"Pierrick Bouvier" <pierrick.bouvier@oss.qualcomm.com>,
"Stefan Weil" <sw@weilnetz.de>, "Peter Xu" <peterx@redhat.com>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>,
"David Gibson" <david@gibson.dropbear.id.au>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Akihiko Odaki" <odaki@rsg.ci.i.u-tokyo.ac.jp>,
"Marcelo Tosatti" <mtosatti@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"John Snow" <jsnow@redhat.com>
Subject: [PATCH v2 7/7] debug/mem: HACK add qemu_name_buffer
Date: Mon, 16 Nov 2026 11:50:02 -0500 [thread overview]
Message-ID: <20261116165002.30939-8-jsnow@redhat.com> (raw)
In-Reply-To: <20261116165002.30939-1-jsnow@redhat.com>
This is a hack to allow us to name malloc'd buffers that are backed by
anonymous mmap allocations.
Quite useful in my debugging, probably not quite appropriate for
upstream unless we can figure out a more robust method for identifying
when we get an mmap'd page from malloc and adjusting the pointer
offset accordingly - I am currently subtracting a fixed 16 bytes
because that "works on my system", and I have no clue if that's
appropriate everywhere or not.
At least this way, my trick is published to the list for posterity.
Signed-off-by: John Snow <jsnow@redhat.com>
---
include/qemu/bitmap.h | 6 ++++++
include/qemu/mmap-vma.h | 3 +++
hw/core/loader.c | 3 +++
hw/i386/x86-common.c | 3 +++
system/device_tree.c | 2 ++
system/physmem.c | 8 ++++++--
util/mmap-vma.c | 23 +++++++++++++++++++++++
7 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h
index 0044333cb51..6681c87737f 100644
--- a/include/qemu/bitmap.h
+++ b/include/qemu/bitmap.h
@@ -104,6 +104,12 @@ static inline unsigned long *bitmap_try_new(long nbits)
return g_try_new0(unsigned long, nelem);
}
+static inline size_t bitmap_size(long nbits)
+{
+ long nelem = BITS_TO_LONGS(nbits);
+ return sizeof(unsigned long) * nelem;
+}
+
static inline unsigned long *bitmap_new(long nbits)
{
long nelem = BITS_TO_LONGS(nbits);
diff --git a/include/qemu/mmap-vma.h b/include/qemu/mmap-vma.h
index 763ea5a5cab..4646657022c 100644
--- a/include/qemu/mmap-vma.h
+++ b/include/qemu/mmap-vma.h
@@ -15,4 +15,7 @@
int qemu_name_vma(void *ptr, size_t sz, const char *fmt, ...)
G_GNUC_PRINTF(3, 4);
+int qemu_name_buffer(void *ptr, size_t sz, const char *fmt, ...)
+ G_GNUC_PRINTF(3, 4);
+
#endif
diff --git a/hw/core/loader.c b/hw/core/loader.c
index 5cbfba0a86d..cc0e0d6979a 100644
--- a/hw/core/loader.c
+++ b/hw/core/loader.c
@@ -45,6 +45,7 @@
#include "qemu/osdep.h"
#include "qemu/datadir.h"
#include "qemu/error-report.h"
+#include "qemu/mmap-vma.h"
#include "qapi/error.h"
#include "qapi/qapi-commands-machine.h"
#include "qapi/type-helpers.h"
@@ -1111,6 +1112,8 @@ ssize_t rom_add_file(const char *file, const char *fw_dir,
goto err;
}
+ qemu_name_buffer(rom->data, size, "%s", file);
+
if (fw_dir) {
rom->fw_dir = g_strdup(fw_dir);
rom->fw_file = g_strdup(file);
diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c
index dcd4fa8eb79..608c4cf4202 100644
--- a/hw/i386/x86-common.c
+++ b/hw/i386/x86-common.c
@@ -25,6 +25,7 @@
#include "qemu/cutils.h"
#include "qemu/units.h"
#include "qemu/datadir.h"
+#include "qemu/mmap-vma.h"
#include "qapi/error.h"
#include "system/numa.h"
#include "system/system.h"
@@ -914,6 +915,8 @@ void x86_load_linux(X86MachineState *x86ms,
setup = g_malloc(setup_size);
kernel = g_malloc(kernel_size);
+ qemu_name_buffer(kernel, kernel_size, "%s", kernel_filename);
+
fseek(f, 0, SEEK_SET);
if (fread(setup, 1, setup_size, f) != setup_size) {
fprintf(stderr, "fread() failed\n");
diff --git a/system/device_tree.c b/system/device_tree.c
index 48365435ea0..816874fb299 100644
--- a/system/device_tree.c
+++ b/system/device_tree.c
@@ -27,6 +27,7 @@
#include "hw/core/loader.h"
#include "hw/core/boards.h"
#include "qemu/config-file.h"
+#include "qemu/mmap-vma.h"
#include "qapi/qapi-commands-machine.h"
#include "qobject/qdict.h"
@@ -41,6 +42,7 @@ void *create_device_tree(int *sizep)
*sizep = FDT_MAX_SIZE;
fdt = g_malloc0(FDT_MAX_SIZE);
+ qemu_name_buffer(fdt, FDT_MAX_SIZE, "device_tree");
ret = fdt_create(fdt, FDT_MAX_SIZE);
if (ret < 0) {
goto fail;
diff --git a/system/physmem.c b/system/physmem.c
index 202e3a5cd00..dc0e9540919 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -26,6 +26,7 @@
#include "qemu/hbitmap.h"
#include "qemu/madvise.h"
#include "qemu/lockable.h"
+#include "qemu/mmap-vma.h"
#ifdef CONFIG_TCG
#include "accel/tcg/cpu-ops.h"
@@ -2105,7 +2106,7 @@ void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length)
}
/* Called with ram_list.mutex held */
-static void dirty_memory_extend(ram_addr_t new_ram_size)
+static void dirty_memory_extend(ram_addr_t new_ram_size, const char *nickname)
{
unsigned int old_num_blocks = ram_list.num_dirty_blocks;
unsigned int new_num_blocks = DIV_ROUND_UP(new_ram_size,
@@ -2133,6 +2134,9 @@ static void dirty_memory_extend(ram_addr_t new_ram_size)
for (j = old_num_blocks; j < new_num_blocks; j++) {
new_blocks->blocks[j] = bitmap_new(DIRTY_MEMORY_BLOCK_SIZE);
+ qemu_name_buffer(new_blocks->blocks[j],
+ bitmap_size(DIRTY_MEMORY_BLOCK_SIZE),
+ "bitmap<%s>", nickname);
}
qatomic_rcu_set(&ram_list.dirty_memory[i], new_blocks);
@@ -2254,7 +2258,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
}
ram_size = (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS;
- dirty_memory_extend(ram_size);
+ dirty_memory_extend(ram_size, new_block->mr->name);
/* Keep the list sorted from biggest to smallest block. Unlike QTAILQ,
* QLIST (which has an RCU-friendly variant) does not have insertion at
* tail, so save the last element in last_block.
diff --git a/util/mmap-vma.c b/util/mmap-vma.c
index 46ea98f1134..083400a9c2e 100644
--- a/util/mmap-vma.c
+++ b/util/mmap-vma.c
@@ -73,3 +73,26 @@ int qemu_name_vma(void *ptr, size_t sz, const char *fmt, ...)
return rc;
}
+
+G_GNUC_PRINTF(3, 4)
+int qemu_name_buffer(void *ptr, size_t sz, const char *fmt, ...)
+{
+ /*
+ * this is hacky as hell, but ptmalloc will occasionally use
+ * mmap() for large buffers - when it does so, you get a pointer
+ * to 16 bytes after the actual mmap. This function compensates
+ * for that to guess at what the real mmap allocation was.
+ *
+ * This can be used unconditionally whether or not the allocation
+ * was actually mmap'd - if it wasn't, this just fails. Yes, it's
+ * still very ugly.
+ */
+ va_list args;
+ size_t pagesz = qemu_real_host_page_size();
+
+ ptr = ptr - 0x10;
+ sz = ROUND_UP(sz + 0x10, pagesz);
+
+ va_start(args, fmt);
+ return qemu_vname_vma(ptr, sz, fmt, args);
+}
--
2.56.1
prev parent reply other threads:[~2026-11-16 16:50 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 16:49 [PATCH v2 0/7] debug: add PR_SET_VMA_ANON_NAME support John Snow
2026-11-16 16:49 ` [PATCH v2 1/7] debug/mem: add utility to name anonymous memory mappings John Snow
2026-11-17 13:39 ` Daniel P. Berrangé
2026-11-16 16:49 ` [PATCH v2 2/7] debug/mem: add tracing to qemu_vname_vma() John Snow
2026-11-16 16:49 ` [PATCH v2 3/7] debug/mem: name <reserved> regions in mmap-alloc John Snow
2026-11-16 16:49 ` [PATCH v2 4/7] debug/mem: name pthreads John Snow
2026-11-17 13:46 ` Daniel P. Berrangé
2026-11-17 13:47 ` Daniel P. Berrangé
2026-11-16 16:50 ` [PATCH v2 5/7] debug/mem: name coroutine stacks John Snow
2026-11-17 13:47 ` Daniel P. Berrangé
2026-11-16 16:50 ` [PATCH v2 6/7] debug/mem: name qemu_anon_ram_alloc/qemu_ram_mmap pages John Snow
2026-11-16 16:50 ` John Snow [this message]
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=20261116165002.30939-8-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