From: Fuad Tabba <tabba@google.com>
To: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org
Cc: maz@kernel.org, oliver.upton@linux.dev, will@kernel.org,
mark.rutland@arm.com, joey.gouly@arm.com,
suzuki.poulose@arm.com, yuzenghui@huawei.com,
catalin.marinas@arm.com, broonie@kernel.org,
vdonnefort@google.com, qperret@google.com,
sebastianene@google.com, keirf@google.com, smostafa@google.com,
tabba@google.com
Subject: [PATCH v3 1/9] KVM: arm64: Add build-time check for duplicate DECLARE_REG use
Date: Wed, 27 Aug 2025 11:19:41 +0100 [thread overview]
Message-ID: <20250827101949.4089456-2-tabba@google.com> (raw)
In-Reply-To: <20250827101949.4089456-1-tabba@google.com>
The DECLARE_REG() macro provides a convenient way to create a local
variable initialized from a cpu context in the hyp trap handlers.
However, a common error is to use the macro multiple times in the same
scope with the same register index, but for different logical purposes.
This results in valid C code that compiles without error, but introduces
subtle bugs where a developer expects two different variables to hold
values from two different registers, when in fact they are both sourced
from the same one.
To prevent this entire class of bugs, modify the DECLARE_REG() macro
to declare a dummy variable whose name is derived from the register
index. If the macro is used again with the same index in the same
scope, the compiler will fail with a "redeclaration of variable"
error, turning a subtle runtime bug into an obvious build-time failure.
Signed-off-by: Fuad Tabba <tabba@google.com>
---
Note: I considered a more restrictive compile-time check to also enforce
that the register indexes are sequential and without gaps. However,
achieving this without requiring significant refactoring of all the call
sites results in too much churn. This patch catches most (or all) of the
related issues we have run into in practice.
---
arch/arm64/kvm/hyp/include/nvhe/trap_handler.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/kvm/hyp/include/nvhe/trap_handler.h b/arch/arm64/kvm/hyp/include/nvhe/trap_handler.h
index 1e6d995968a1..ba5382c12787 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/trap_handler.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/trap_handler.h
@@ -12,7 +12,8 @@
#include <asm/kvm_host.h>
#define cpu_reg(ctxt, r) (ctxt)->regs.regs[r]
-#define DECLARE_REG(type, name, ctxt, reg) \
+#define DECLARE_REG(type, name, ctxt, reg) \
+ __always_unused int ___check_reg_ ## reg; \
type name = (type)cpu_reg(ctxt, (reg))
#endif /* __ARM64_KVM_NVHE_TRAP_HANDLER_H__ */
--
2.51.0.261.g7ce5a0a67e-goog
next prev parent reply other threads:[~2025-08-27 10:19 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-27 10:19 [PATCH v3 0/9] KVM: arm64: Reserve pKVM VM handle during initial VM setup Fuad Tabba
2025-08-27 10:19 ` Fuad Tabba [this message]
2025-08-27 10:19 ` [PATCH v3 2/9] KVM: arm64: Rename pkvm.enabled to pkvm.is_protected Fuad Tabba
2025-08-27 10:19 ` [PATCH v3 3/9] KVM: arm64: Rename 'host_kvm' to 'kvm' in pKVM host code Fuad Tabba
2025-08-27 10:19 ` [PATCH v3 4/9] KVM: arm64: Clarify comments to distinguish pKVM mode from protected VMs Fuad Tabba
2025-08-27 10:19 ` [PATCH v3 5/9] KVM: arm64: Decouple hyp VM creation state from its handle Fuad Tabba
2025-08-27 10:19 ` [PATCH v3 6/9] KVM: arm64: Separate allocation and insertion of pKVM VM table entries Fuad Tabba
2025-08-27 10:19 ` [PATCH v3 7/9] KVM: arm64: Consolidate pKVM hypervisor VM initialization logic Fuad Tabba
2025-08-27 10:19 ` [PATCH v3 8/9] KVM: arm64: Introduce separate hypercalls for pKVM VM reservation and initialization Fuad Tabba
2025-08-27 10:19 ` [PATCH v3 9/9] KVM: arm64: Reserve pKVM handle during pkvm_init_host_vm() Fuad Tabba
2025-09-08 16:05 ` Mark Brown
2025-09-08 18:43 ` Fuad Tabba
2025-09-08 18:57 ` Marc Zyngier
2025-09-08 18:52 ` Mark Brown
2025-09-08 18:54 ` Fuad Tabba
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=20250827101949.4089456-2-tabba@google.com \
--to=tabba@google.com \
--cc=broonie@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=joey.gouly@arm.com \
--cc=keirf@google.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=qperret@google.com \
--cc=sebastianene@google.com \
--cc=smostafa@google.com \
--cc=suzuki.poulose@arm.com \
--cc=vdonnefort@google.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.