Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CFB0D606A322 for ; Fri, 4 Dec 2026 13:20:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=efmmDfau Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xZTCh-0002IF-HR; Fri, 04 Dec 2026 08:19:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xZTCg-0002Gl-5T for qemu-rust@nongnu.org; Fri, 04 Dec 2026 08:19:58 -0500 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xZTCd-0004vE-Gy for qemu-rust@nongnu.org; Fri, 04 Dec 2026 08:19:57 -0500 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-4bb64ef5a14so7596745e9.1 for ; Fri, 04 Dec 2026 05:19:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1796390394; x=1796995194; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Eb3a/XHcGnKoe60vKCWLtT8hZIt59hvyLZsvG9VyEJ8=; b=efmmDfaueobLUr15B0chiioSo0UEFEVbIoyxq3jhMTjMMwpTTsBDfkA/w5gajcCqcQ oS3O/GC2qn4UstKVlzaWcYzljlEaeHZ7V09s00xiBWOAooucKEBIMhU5TwTjJvIojIS1 2c2ydGYNfO9Fc2FEu4S2jLcwTdymiW4IqN4W8fyvWmSjYpZazHiIk2f689R3abHg8Wpb 5BOQFLYgZUvm0GFouYgO4IxOeydUmOsQUc/+1M0tchqgBZv9vgrCzNmLT8WwMA0HvSkG qYRAbLdB+HZcTHIwn7LlIiT6RPigufwwfPn+JGfzjBiEte89TAWoDMWyWPeN/CfpMXdN DHgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796390394; x=1796995194; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Eb3a/XHcGnKoe60vKCWLtT8hZIt59hvyLZsvG9VyEJ8=; b=ZWKaFjZtjPJfaKpHFabqmJPTNF0/Mly7sdheN+0Wketcjpcjnk/x+ZA8mhZTPPSfsf WbgB/GVW5RkFqUpYQhImS77g2yb5fVE3LIkW+zxwwa8oGuTWBGEkvBrqO8OhMFql/ut2 qMvaikeMng95wx39lMwhUYEm0kZD3JP3hM3vcP6UV2pDzEYGi3pzWxsWW5sFpp1iotsR n17CCHaCWZfIjHBSYUcqY3rdHa9UByJ5ParaBzz3b+XA/y9tfSsmTXmTZ4Zhxj2Xe/JO UHYk4n6nN6+8pI2+bnH9iUhWQtruY0akWAhHUV9MTQxgH6iF0ex3sC1v/+XYbU3Ycwcx SDXA== X-Forwarded-Encrypted: i=1; AL+hErE7mWGIt8edCI4vn/zP6jlzOxZNzNM0DK32+6AwX3+YZdkNgZmbkfiIgarobHyaYFtZqR9CabWJPCs=@nongnu.org X-Gm-Message-State: APuU4Z8RPEHwFovEY2NTVbjVttzkW2+nMLBxRJ9Icax+8QJZEVJkvsGG Pk+jkGWdZIuP1OyCSevbk1PS7fMzLacIa59Kkae+Wbed9mG3iP2g18uOZAEIiN3GPbY= X-Gm-Gg: AZFdTZdwEUqhoGONmoyhUdQvSRWRH8vvWX+84cbuGh8Ez4AfWylMzEorif3sfmfcbzA CfJz55B+KhKk6FpETrh72zubNjAHHUJwFMT/DQ3Qd+8NBMHqftMbNf62uPROrBMD2JLJhQLhLKq fArlgEZZg++RROJK39v+c4MBSaaswOf9e0Rs1r2Q96FP9Fph+ow+DVrZ5wd1GGfWVhYLIh4QNyB M4yrwz23FBWhEDLFzOoWbAJnUb8an0/M81IegjCryGbYk/WqRjD2MiPLRF1j+TwW6pOfZuZ+12m nSIPn17u+zidDgdiOpOekQ/hvH/WgePGVLVALitvgDHGW9m2vHPb5f00DsUNfu5kFJdHjBWmCFT 4CndptkOLRyIVCHChXQiJyYlDCNAkoVGENtU1EFt8ceO3bZC/znGGYwi2mUk0fq8inI4N1c/pQ4 Uu9ncvsSLco5xKMi80xYC1MNrAbhZMMZOB5GTb0qRdHcHskUp7rlZk2Pec1jBVEPS/XREoK/GVG U8pC6rBw/1+KZY8MdJIjMu3DrmFIRAwOnVbTn9dxx0moqvo74DbJlivMpFMuJ3Leqzd72DAAxBB 2zzsLX2FMjdOExnr00Jced+Qn0B1d9PXOc/nt50xZXoNqRSRZ5DIirTqNgUXd/MDqQ95gEeM05r x75sy2AjojTs1eyOk96siXwN9ZTSifTFMIA== X-Received: by 2002:a05:600c:34d2:b0:4a1:fae5:f2e2 with SMTP id 5b1f17b1804b1-4bbeb6ef054mr40912535e9.4.1796390389787; Fri, 04 Dec 2026 05:19:49 -0800 (PST) Received: from lanath.. (wildly.archaic.org.uk. [81.2.115.145]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d1738cfb80sm5342097f8f.36.2026.12.04.05.19.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 05:19:48 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Cc: Jason Wang , Stefan Hajnoczi , Fam Zheng , Manos Pitsidianakis , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Samuel Thibault , Paolo Bonzini , Thomas Huth , qemu-rust@nongnu.org Subject: [PATCH 2/6] util/qemu-timer: Add optional reentrancy guard to the timer API Date: Fri, 4 Dec 2026 13:19:42 +0000 Message-ID: <20261204131946.2175006-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261204131946.2175006-1-peter.maydell@linaro.org> References: <20261204131946.2175006-1-peter.maydell@linaro.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2a00:1450:4864:20::330; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x330.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-rust@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: QEMU Rust-related patches and discussion List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-rust-bounces+qemu-rust=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 It's common for devices that use QEMU timers to do things in their timer callbacks that may either directly be a DMA operation, or to call out to other device models (e.g. by setting an interrupt) that might indirectly trigger a DMA operation. If the guest can arrange for that DMA operation to be to an address which is in the device's own memory-mapped registers, the device code will be re-entered in a way it doesn't expect. Our mechanism for avoiding that is the MemReentrancyGuard. We automatically set that when calling into a device's memory operation read and write functions, and if the device directly uses the QEMUBH API they can ask for the BH callbacks to be guarded (via e.g. aio_bh_new_guarded() or qemu_bh_new_guarded()), but for a timer callback the device has to manually enable its guard. We did that for the USB XHCI device in commit f91b0544fe1f ("hw/usb/hcd-xhci: Set reentrancy guard in timer functions"). To make it less awkward for devices to enable a reentrancy guard for their timer callbacks, add support for the guard to the QEMU timer API, in the same way that we did for the BH API in commit 9c86c97f12 ("async: Add an optional reentrancy guard to the BH API"). In this commit, we add the MemReentrancyGuard pointer to the QEMU timer struct, and if it is set we engage it across the callback. This will do nothing yet because we don't expose any API changes to allow it to be set up when the timer is initialized. Note that it is OK to write to reentrancy_guard->engaged_in_io even after the callback has returned and potentially might have e.g. deleted a timer, because we are going to require that the guard structure itself remains valid. The expected guard is the one in the DeviceState struct, and devices shouldn't be deleting themselves in timer callbacks, so this should be OK. (It's also an implicit requirement for other uses of the reentrancy guard mechanism.) Cc: qemu-stable@nongnu.org Signed-off-by: Peter Maydell --- include/qemu/timer.h | 2 ++ util/qemu-timer.c | 31 +++++++++++++++++++++++++++++-- util/trace-events | 3 +++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/include/qemu/timer.h b/include/qemu/timer.h index 8e7c217306b..11bcc3f0d36 100644 --- a/include/qemu/timer.h +++ b/include/qemu/timer.h @@ -4,6 +4,7 @@ #include "qemu/bitops.h" #include "qemu/notify.h" #include "qemu/host-utils.h" +#include "qemu/mem-reentrancy.h" #define NANOSECONDS_PER_SECOND 1000000000LL #define MICROSECONDS_PER_SECOND 1000000LL @@ -88,6 +89,7 @@ struct QEMUTimer { QEMUTimerCB *cb; void *opaque; QEMUTimer *next; + MemReentrancyGuard *reentrancy_guard; int attributes; int scale; }; diff --git a/util/qemu-timer.c b/util/qemu-timer.c index f7920130854..6658e5e623d 100644 --- a/util/qemu-timer.c +++ b/util/qemu-timer.c @@ -31,6 +31,7 @@ #include "system/replay.h" #include "system/cpus.h" #include "hw/core/cpu.h" +#include "trace.h" #ifdef CONFIG_POSIX #include @@ -362,6 +363,7 @@ void timer_init_full(QEMUTimer *ts, ts->scale = scale; ts->attributes = attributes; ts->expire_time = -1; + ts->reentrancy_guard = NULL; } void timer_deinit(QEMUTimer *ts) @@ -388,11 +390,33 @@ static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts) } } +static void timer_run_callback(QEMUTimerCB *cb, void *opaque, + MemReentrancyGuard *reentrancy_guard) +{ + /* Run the timer cb, optionally with a reentrancy guard engaged. */ + bool last_engaged_in_io = false; + + if (reentrancy_guard) { + last_engaged_in_io = reentrancy_guard->engaged_in_io; + if (last_engaged_in_io) { + trace_reentrant_timer(cb, opaque); + } + + reentrancy_guard->engaged_in_io = true; + } + + cb(opaque); + + if (reentrancy_guard) { + reentrancy_guard->engaged_in_io = last_engaged_in_io; + } +} + static void timer_fire(CPUState *cpu, run_on_cpu_data data) { QEMUTimer *t = data.host_ptr; - t->cb(t->opaque); + timer_run_callback(t->cb, t->opaque, t->reentrancy_guard); } static bool timer_mod_ns_locked(QEMUTimerList *timer_list, @@ -571,6 +595,8 @@ static bool timerlist_run_timers(QEMUTimerList *timer_list) current_time = qemu_clock_get_ns(timer_list->clock->type); qemu_mutex_lock(&timer_list->active_timers_lock); while ((ts = timer_list->active_timers)) { + MemReentrancyGuard *reentrancy_guard; + if (!timer_expired_ns(ts, current_time)) { /* No expired timers left. The checkpoint can be skipped * if no timers fired or they were all external. @@ -595,10 +621,11 @@ static bool timerlist_run_timers(QEMUTimerList *timer_list) ts->expire_time = -1; cb = ts->cb; opaque = ts->opaque; + reentrancy_guard = ts->reentrancy_guard; /* run the callback (the timer list can be modified) */ qemu_mutex_unlock(&timer_list->active_timers_lock); - cb(opaque); + timer_run_callback(cb, opaque, reentrancy_guard); qemu_mutex_lock(&timer_list->active_timers_lock); progress = true; diff --git a/util/trace-events b/util/trace-events index df549646d12..45763bca80f 100644 --- a/util/trace-events +++ b/util/trace-events @@ -85,6 +85,9 @@ qemu_mutex_lock(void *mutex, const char *file, const int line) "waiting on mutex qemu_mutex_locked(void *mutex, const char *file, const int line) "taken mutex %p (%s:%d)" qemu_mutex_unlock(void *mutex, const char *file, const int line) "released mutex %p (%s:%d)" +# qemu-timer.c +reentrant_timer(void *cb, void *opaque) "timer callback %p opaque %p" + # vfio-helpers.c qemu_vfio_dma_reset_temporary(void *s) "s %p" qemu_vfio_ram_block_added(void *s, void *p, size_t size) "s %p host %p size 0x%zx" -- 2.43.0