From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B10E1DDC28 for ; Sat, 30 May 2026 00:23:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100581; cv=none; b=rClSLHTomD69AKGQjPOIlymCSz8As+J5uiyOJxiV3nFVeN2XundUXJjwcDnl4GS0Ejs2Ui1NpSmtpXNojFgoBEGnFlr6nPEl5ZQ1iIsdboBIZB+yjQw/fzqcsQdtKna2Zura/I/w/EmtEs4kt09QlEiQWtqWmOX33FFJAmWh36U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100581; c=relaxed/simple; bh=s2MYJQNj8LYRIAfQocfHgBxQGwQ3ttG+Ot8ZEKdurmw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CIIgJaDYAASc9jRtdZ2bNA68A2KTN0FXyWUKsXzn923wh4Gs1nUwwIa0K8OkZyZwQqub01eGMcustnVMR7cZ8WPAKelJZTGqRNZ2Ii4dSH17saqzyZQzr7NelQp4FDyIgY48rH/gS43LOr10KUf2kcJ8cfV6aOqxF2Sm2If9xBs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fZrriQdB; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fZrriQdB" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3692f395339so14700463a91.1 for ; Fri, 29 May 2026 17:23:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100580; x=1780705380; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9v7UfovR9K9VTf6qWxX2XmbiOmSXdhYyM9BXuDZay90=; b=fZrriQdBCohJVkOtSWvQ0tNxyERpQhv7pH+SjvscGFzU83hBcxh3Hu71okDjhh22sO 05p1SJ8VU5OjmIlcGag6QMTKc5PNysczTsXQ049vgKFMj2h/KmC6ZzKj0XnAzi1bqyPt bJyFtnqXrOPgeM3Nw5iCFSPUpAPnBtld92w5XZA5dqGpL3NerKKX5CJ3tY4pF+aP9N6x 9BNCkWbLO2z2rp5Vpxa4qfkXUlFUG5aW/UvsxNHk74Xfb3DLqWJvSzSOadva227IlPGm UAXjYouWnozcg/L9Rhj6tnvLIvYDYSNb5Iqt/UAuywnQ6lImh/CIt7OsjX/tNgwzHOkB mGkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100580; x=1780705380; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9v7UfovR9K9VTf6qWxX2XmbiOmSXdhYyM9BXuDZay90=; b=JoOLgniQezQxKxFC4c2dvr6vUVe4GMkmqziEy81pNFN/fvQ/LSjl0DOVPYmY7pT/Ba kCFqvkepqnDlnNNk0sFXTl9OSXGGmjRuRCUeIzZYwAd8NSc36Mb6Eb9ypUF4BjDjogKF MCnoaMu7qyVS5QHexYwapfm0d4NcEbL95jIP6yODujuL4BZ0U0rKE2hydSpw0ztNqlSn Uf05VmQDQ0tuE2Ens/6lAR37aIUvtaPfW43YDMLve08lryeyoVZEYas6iG2kb02112Vu bLW3Q38m1eDMK0RTm9TPBZJHciyoM8rSGHAUYzTec5Vy+y0c3Unt17PCpqyYPc9AZznC KKOg== X-Forwarded-Encrypted: i=1; AFNElJ/ImBfEHa4KJX5liQSdBmGxKw78qmKVCw6AgIrieFH4aoCyl9xxSGCqsRvWAIuzbCncd7Lm/LN8/uoedfQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyM4uWcyB8q0fV+e0eSDGfMH5rzAYDQAbpDDeS27LW00BgkhZfM 83ZPYiTMTQci3SyGU/omI20AwJ44Noa1Zt5ThRhy7IYlDWRzWthQAo8uWZV8jmduUZeNVamVvBF ZBBKHzClt X-Received: from pjboh6.prod.google.com ([2002:a17:90b:3a46:b0:36a:fcf5:b57f]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:574e:b0:369:7421:b376 with SMTP id 98e67ed59e1d1-36c501dd387mr1274602a91.19.1780100579735; Fri, 29 May 2026 17:22:59 -0700 (PDT) Date: Sat, 30 May 2026 00:21:16 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-2-jrhilke@google.com> Subject: [PATCH v4 01/19] KVM: selftests: Build and link selftests/vfio/lib into KVM selftests From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Include libvfio.mk into the KVM selftests Makefile and link it into all KVM selftests by adding it to LIBKVM_OBJS. This lays the groundwork for future changes to utilize VFIO devices to verify IRQ bypass in KVM selftests. Note that KVM selftests build their own copy of sefltests/vfio/lib and the resulting object files are placed in $(OUTPUT)/lib. This allows the KVM and VFIO selftests to apply different CFLAGS when building without conflicting with each other. Signed-off-by: David Matlack Signed-off-by: Josh Hilke --- tools/testing/selftests/kvm/Makefile.kvm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index 6471fa214a9f..b3e223f45575 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -252,6 +252,7 @@ OVERRIDE_TARGETS =3D 1 # which causes the environment variable to override the makefile). include ../lib.mk include ../cgroup/lib/libcgroup.mk +include ../vfio/lib/libvfio.mk =20 INSTALL_HDR_PATH =3D $(top_srcdir)/usr LINUX_HDR_PATH =3D $(INSTALL_HDR_PATH)/include/ @@ -306,7 +307,9 @@ LIBKVM_S :=3D $(filter %.S,$(LIBKVM)) LIBKVM_C_OBJ :=3D $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C)) LIBKVM_S_OBJ :=3D $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S)) LIBKVM_STRING_OBJ :=3D $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING)) -LIBKVM_OBJS =3D $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) $(LIB= CGROUP_O) +LIBKVM_OBJS =3D $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) +LIBKVM_OBJS +=3D $(LIBCGROUP_O) +LIBKVM_OBJS +=3D $(LIBVFIO_O) SPLIT_TEST_GEN_PROGS :=3D $(patsubst %, $(OUTPUT)/%, $(SPLIT_TESTS)) SPLIT_TEST_GEN_OBJ :=3D $(patsubst %, $(OUTPUT)/$(ARCH)/%.o, $(SPLIT_TESTS= )) =20 --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B71E11C3BFC for ; Sat, 30 May 2026 00:23:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100586; cv=none; b=WC+8FUCUh4dpvVExikj4dWsjeDm+Ty8/h9FbN/zf1ip1Lfnoyjx4aLqV/RkCd/U0mwow7I94rwHgrOoKKfFarH/z6JMRn+xXf/Ruy9rAj9GmwQ39zgtdLbTuKfQojgfVJ3hWUtJjFRnW5JL4+q8X484E147Z2CzFMrz4GTGEwYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100586; c=relaxed/simple; bh=Za+s+OgPLfvUhN+QI3n1p7epUkpLwBru3OqG8CCuomI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uj+ZRmWW3bHQeIkn+9WcxjI5j3ubvNvB6MF7iLsq0SXA5mlLFBhzUyoPymk3BXNjt1Jt3Psr9djf7jAsRPtqh6bC/sD4JspOzErZWyx/vhsLkCfJrxrwyofikQUtMy0T1yF8zQ3Ipgae0OnyM83U8/Z2EKC0ZOsghdfuqaPXwCA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Fr7F20Rf; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Fr7F20Rf" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-842208d5b0eso425610b3a.3 for ; Fri, 29 May 2026 17:23:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100583; x=1780705383; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9DXbh05mf0SUiaS4tAY6E1/7NEkibUbeq5dHsgLItpk=; b=Fr7F20RfTLfpxAZz8vsawslSD+rCVnmKqyOItVpR6NGQk0qzM5Odw5lnqr3Q2hXAiL 5+ImmS/aG495T4uT5XK1DO7yiyPmlI8cbOaKXU74NXPTKxAz6gXpVFPA+dr4GTpcSl7I o48VsTi1dX9ONpHnt3fRQefJas4tZFiXfcR6DlvM115rjyfctjQAhb/s/cYZeiCWIsI5 IllD7YOwr3OK7J0RG3oBl5PSt8bJ9AjqEIB0xp/nkE7XRzs9+Djh42X5xxRUi9cJ4F50 Bq0aIh9qaC/IcRATXEGx4ULMraZIkVY0bp4S9asZtT6729/ebyV5VcsYXm6YHfhNwTJ1 XXSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100583; x=1780705383; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9DXbh05mf0SUiaS4tAY6E1/7NEkibUbeq5dHsgLItpk=; b=ca6b9r58vhGfn305468+mlcQ/JWElSJ3UNb/vdSvFBbdWF7R2OjsKQvqr4eZppPFOz 67kiU8mAlyERphkQXe50Dj2GIuueS0A05gnP0sVVfFsSRmAu/qVIbb+Gj36TQ3TPSvE2 IkCa3M4IR1ViEXHFvc3aTjSDAmnjXcPcnYr1l1UJqZD9MA+CyHvqWTRqLPjy8cJUaOWy aTKXnjvUFqkW0ZDumjredfPfmYvGfc++CG7dRpA6aqYxAbraI+IMDxHXEX2uGViWPRnQ suY+s96f/GVzVewCxSe6wjSEsdltLAfW5qXxK6WgEW4XdRn3p6fvOZqTWFUkdyE+UJGU posw== X-Forwarded-Encrypted: i=1; AFNElJ96Ton5VsD6wW/sjy/DeufaRhoby+iR/h5+z/LQAYWjmy61qbCDiHHbz+BBPJbezkR0rMvzuygYPLAyrsU=@vger.kernel.org X-Gm-Message-State: AOJu0YyLWVUP1+fOvqclYF2j3hjxRIUTDaKvyf7qnoKZGteAAJLdxIC7 CtwkqB//Jc2Le9Ga67C1pSWVa02REJEMgHV+ecZHPkpp1ckfrlyh08y1PqD4M3szgBSEUQiq2hx Dl/wzdQFB X-Received: from pfbky35.prod.google.com ([2002:a05:6a00:6f63:b0:82f:473b:a2d5]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3a25:b0:82f:9985:d4a1 with SMTP id d2e1a72fcca58-842255cf8edmr1642023b3a.24.1780100582679; Fri, 29 May 2026 17:23:02 -0700 (PDT) Date: Sat, 30 May 2026 00:21:17 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-3-jrhilke@google.com> Subject: [PATCH v4 02/19] KVM: selftests: Add /proc/interrupts parsing helpers for IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Introduce utility functions to parse /proc/interrupts and extract the host IRQ number for a given VFIO device and MSI vector. These helpers lay the groundwork for subsequent patches that will creat a KVM IRQ test and use real VFIO devices to trigger hardware interrupts. Specifically, they are needed to allow the test to log the exact IRQ number being used for better debugging. Suggested-by: Sean Christopherson Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../testing/selftests/kvm/include/proc_util.h | 9 ++++ tools/testing/selftests/kvm/lib/proc_util.c | 42 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/proc_util.h create mode 100644 tools/testing/selftests/kvm/lib/proc_util.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index b3e223f45575..d944b81cad7d 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -11,6 +11,7 @@ LIBKVM +=3D lib/kvm_util.c LIBKVM +=3D lib/lru_gen_util.c LIBKVM +=3D lib/memstress.c LIBKVM +=3D lib/guest_sprintf.c +LIBKVM +=3D lib/proc_util.c LIBKVM +=3D lib/rbtree.c LIBKVM +=3D lib/sparsebit.c LIBKVM +=3D lib/test_util.c diff --git a/tools/testing/selftests/kvm/include/proc_util.h b/tools/testin= g/selftests/kvm/include/proc_util.h new file mode 100644 index 000000000000..7c465e8584e2 --- /dev/null +++ b/tools/testing/selftests/kvm/include/proc_util.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTEST_KVM_PROC_UTIL_H +#define SELFTEST_KVM_PROC_UTIL_H + +#include + +int get_proc_vfio_irq_number(const char *vfio_device_bdf, int msi); + +#endif /* SELFTEST_KVM_PROC_UTIL_H */ diff --git a/tools/testing/selftests/kvm/lib/proc_util.c b/tools/testing/se= lftests/kvm/lib/proc_util.c new file mode 100644 index 000000000000..ad1c54a81869 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/proc_util.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "kvm_util.h" +#include "test_util.h" +#include "proc_util.h" + +#include + +static FILE *open_proc_interrupts(void) +{ + FILE *fp; + + fp =3D fopen("/proc/interrupts", "r"); + TEST_ASSERT(fp, "fopen(/proc/interrupts) failed"); + + return fp; +} + +int get_proc_vfio_irq_number(const char *device_bdf, int msi) +{ + char search_string[64]; + char line[4096]; + int irq =3D -1; + FILE *fp; + + fp =3D open_proc_interrupts(); + + snprintf(search_string, sizeof(search_string), "vfio-msix[%d]", msi); + + while (fgets(line, sizeof(line), fp)) { + if (strstr(line, device_bdf) && strstr(line, search_string)) { + TEST_ASSERT_EQ(1, sscanf(line, "%d:", &irq)); + break; + } + } + + fclose(fp); + + TEST_ASSERT(irq !=3D -1, "Failed to locate IRQ for %s %s", device_bdf, + search_string); + return irq; +} + --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C180B4503B for ; Sat, 30 May 2026 00:23:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100588; cv=none; b=u1Sb6h385OAYvH36HNcPJMejfS+i2fF73sYvnaT/bznEAAj9Z48vsHD00RjlZxdmypC47Y9xKLqq7H680zpG5cVkFyD5Fhh3OBX6rQPiGNp7sFqUaAGkTVgtPsr4XzMHMRh49XYBRj83G1SAZ6rAt8t/Jw8l4cQEXghSxOh/p+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100588; c=relaxed/simple; bh=sXMwmP2UqgFx30vp2DpWcFhwd0AM4JL50dzN5fKYRIQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ezKYryTqrj6yNxATVVZeLIB/rH3T0JnscLdJVFd5BGYCzBrXAZBJa+NPOlihtk3mMYuYdmoqqP0nyCy8eHegu4LDqHJACOH34AHKiXncAFh7lqYCk8WmlKEZ6iw5ly959Gfq+KdPF1vD5WEtalswXhdXpPto0/Z3BUgsjz2OK4I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RwyrjPZv; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RwyrjPZv" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c828f0f5c23so7443464a12.1 for ; Fri, 29 May 2026 17:23:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100586; x=1780705386; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CskMS4d6f2stDOuFXnBJMY4diyRXjtyxYts+dOwiu+M=; b=RwyrjPZvkHVufX1zVNsIG3ko0nADmdd+deS53EkxCX2Wccyikl5M6SPw1CQDTOR0RO fLAuh4ER/a5dncPPX8gj/ht91Eg3qAhCIQNcxtMO4+yTld7/p/xMi0PcBS5fFo/8IAiH rGTAzvxYtvhewy0kOWYvtUBfMcyeWtpGVibf7PjdWEE7YdPb02sZhBLhV5U3vsrqcEUX Nia+J4JJinENZfXEVQmQqRoxmtFD9mVkpc1lQoMqZjtj+anP1sVqjc2167H+89jfBVWt o6ogq4HI5olycYjl79K3YvwvBIYfvfkzj9c06ErpXhpUi5MKIlbBCdpEVzl4Id4hMDJa xkyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100586; x=1780705386; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CskMS4d6f2stDOuFXnBJMY4diyRXjtyxYts+dOwiu+M=; b=KajeiGvCue8nN0dPPQvHR7kYnf5K8B7AF38Pf45Iwl7PQThe+uWot1xFJ8Nu8zayFh 73l9lUuPeCYwn9elJXKs3/5X1vqNwv4RmVBnb1dfWDnI5PE/eO/JKs2/yvChowgMkQvw YskcBONhrI5J6vQWkfu50yx504WSbGkFqcPKEfDtAVg5lGDLSW1TNZ8XjGfpn+AE6wNf asBx0p+lkTEAJTzRuPh3AhvltQFK1pm1CoF1twnkh6p78MmTRwMzmbvTpcY3qt1HFefO ZSQVjzX8frjuvSU0mfZlE+NJJuRAIgA4NbLjgO5zw7ZTu0AobWLDEelY54Lq3//oizNe /KSQ== X-Forwarded-Encrypted: i=1; AFNElJ8CZ3aN/BkOxeT/QFXzUPoU0buX6+G+wLkIe8cPe1foDyVORuT/ZiG0vLxg2KoNchBGT0mTwcDdJaTG+cI=@vger.kernel.org X-Gm-Message-State: AOJu0YwkYIfdIzVTsudlF3ogkcZn6F6OLH+zy2m2BGzfRVlBy6LCGmEQ XcH/VZ0mzGA/h0rNIRiOGi2VugeCTA0l6WzN0sIrQv/rLOcAIehIixG/AGjUEA4ba2PIBR+zXRb XhB+4DO9w X-Received: from pgnr27.prod.google.com ([2002:a63:8f5b:0:b0:c85:7ee2:5d43]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3213:b0:3a2:d79c:414f with SMTP id adf61e73a8af0-3b427f5fca4mr1761269637.48.1780100585901; Fri, 29 May 2026 17:23:05 -0700 (PDT) Date: Sat, 30 May 2026 00:21:18 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-4-jrhilke@google.com> Subject: [PATCH v4 03/19] KVM: selftests: Add guest read/write macros From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add SYNC_FROM_GUEST_AND_READ(vm, variable), to read a variable value from the guest. Add WRITE_AND_SYNC_TO_GUEST(vm, variable) to write a value to a guest variable. These macros improve the readability of code which reads and writes data between host and guest in tests. Also update existing guest reads/writes to use these macros in the following tests: - tools/testing/selftests/kvm/dirty_log_test.c - tools/testing/selftests/kvm/mmu_stress_test.c - tools/testing/selftests/kvm/steal_time.c No functional changes are intended. Suggested-by: Sean Christopherson Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/dirty_log_test.c | 6 ++--- .../testing/selftests/kvm/include/kvm_util.h | 10 +++++++++ tools/testing/selftests/kvm/mmu_stress_test.c | 9 +++----- tools/testing/selftests/kvm/steal_time.c | 22 +++++++------------ 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index d58a641b0e6a..66c3d9554379 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -764,16 +764,14 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) * writing memory during verification, pages that this thread * sees as clean may be written with this iteration's value. */ - WRITE_ONCE(vcpu_stop, true); - sync_global_to_guest(vm, vcpu_stop); + WRITE_AND_SYNC_TO_GUEST(vm, vcpu_stop, true); sem_wait(&sem_vcpu_stop); =20 /* * Clear vcpu_stop after the vCPU thread has acknowledge the * stop request and is waiting, i.e. is definitely not running! */ - WRITE_ONCE(vcpu_stop, false); - sync_global_to_guest(vm, vcpu_stop); + WRITE_AND_SYNC_TO_GUEST(vm, vcpu_stop, false); =20 /* * Sync the number of writes performed before verification, the diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing= /selftests/kvm/include/kvm_util.h index 8b39cb919f4f..499e695f1fc4 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -1144,6 +1144,16 @@ vm_adjust_num_guest_pages(enum vm_guest_mode mode, u= nsigned int num_guest_pages) memcpy(&(g), _p, sizeof(g)); \ }) =20 +#define SYNC_FROM_GUEST_AND_READ(_vm, _variable) ({ \ + sync_global_from_guest(_vm, _variable); \ + READ_ONCE(_variable); \ +}) + +#define WRITE_AND_SYNC_TO_GUEST(_vm, _variable, _value) do { \ + WRITE_ONCE(_variable, _value); \ + sync_global_to_guest(_vm, _variable); \ +} while (0) + /* * Write a global value, but only in the VM's (guest's) domain. Primarily= used * for "globals" that hold per-VM values (VMs always duplicate code and gl= obal diff --git a/tools/testing/selftests/kvm/mmu_stress_test.c b/tools/testing/= selftests/kvm/mmu_stress_test.c index 51c070556f3e..a9fac4bc02c2 100644 --- a/tools/testing/selftests/kvm/mmu_stress_test.c +++ b/tools/testing/selftests/kvm/mmu_stress_test.c @@ -155,10 +155,8 @@ static void *vcpu_worker(void *data) "Expected EFAULT on write to RO memory, got r =3D %d, errno =3D %d",= r, errno); =20 atomic_inc(&nr_ro_faults); - if (atomic_read(&nr_ro_faults) =3D=3D nr_vcpus) { - WRITE_ONCE(all_vcpus_hit_ro_fault, true); - sync_global_to_guest(vm, all_vcpus_hit_ro_fault); - } + if (atomic_read(&nr_ro_faults) =3D=3D nr_vcpus) + WRITE_AND_SYNC_TO_GUEST(vm, all_vcpus_hit_ro_fault, true); =20 #if defined(__x86_64__) || defined(__aarch64__) /* @@ -383,8 +381,7 @@ int main(int argc, char *argv[]) rendezvous_with_vcpus(&time_run2, "run 2"); =20 mprotect(mem, slot_size, PROT_READ); - mprotect_ro_done =3D true; - sync_global_to_guest(vm, mprotect_ro_done); + WRITE_AND_SYNC_TO_GUEST(vm, mprotect_ro_done, true); =20 rendezvous_with_vcpus(&time_ro, "mprotect RO"); mprotect(mem, slot_size, PROT_READ | PROT_WRITE); diff --git a/tools/testing/selftests/kvm/steal_time.c b/tools/testing/selft= ests/kvm/steal_time.c index 7be8adfe5dd3..a866927581fa 100644 --- a/tools/testing/selftests/kvm/steal_time.c +++ b/tools/testing/selftests/kvm/steal_time.c @@ -72,8 +72,8 @@ static void steal_time_init(struct kvm_vcpu *vcpu, uint32= _t i) int ret; =20 /* ST_GPA_BASE is identity mapped */ - st_gva[i] =3D (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE); - sync_global_to_guest(vcpu->vm, st_gva[i]); + WRITE_AND_SYNC_TO_GUEST(vcpu->vm, st_gva[i], + (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE)); =20 ret =3D _vcpu_set_msr(vcpu, MSR_KVM_STEAL_TIME, (ulong)st_gva[i] | KVM_STEAL_RESERVED_MASK); @@ -181,8 +181,7 @@ static void steal_time_init(struct kvm_vcpu *vcpu, uint= 32_t i) vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev); =20 /* ST_GPA_BASE is identity mapped */ - st_gva[i] =3D (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE); - sync_global_to_guest(vm, st_gva[i]); + WRITE_AND_SYNC_TO_GUEST(vm, st_gva[i], (void *)(ST_GPA_BASE + i * STEAL_T= IME_SIZE)); =20 st_ipa =3D (ulong)st_gva[i] | 1; ret =3D __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev); @@ -279,10 +278,8 @@ static bool is_steal_time_supported(struct kvm_vcpu *v= cpu) static void steal_time_init(struct kvm_vcpu *vcpu, uint32_t i) { /* ST_GPA_BASE is identity mapped */ - st_gva[i] =3D (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE); - st_gpa[i] =3D addr_gva2gpa(vcpu->vm, (vm_vaddr_t)st_gva[i]); - sync_global_to_guest(vcpu->vm, st_gva[i]); - sync_global_to_guest(vcpu->vm, st_gpa[i]); + WRITE_AND_SYNC_TO_GUEST(vcpu->vm, st_gva[i], (void *)(ST_GPA_BASE + i * S= TEAL_TIME_SIZE)); + WRITE_AND_SYNC_TO_GUEST(vcpu->vm, st_gpa[i], addr_gva2gpa(vcpu->vm, (vm_v= addr_t)st_gva[i])); } =20 static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpu_idx) @@ -376,8 +373,7 @@ static void steal_time_init(struct kvm_vcpu *vcpu, uint= 32_t i) }; =20 /* ST_GPA_BASE is identity mapped */ - st_gva[i] =3D (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE); - sync_global_to_guest(vm, st_gva[i]); + WRITE_AND_SYNC_TO_GUEST(vm, st_gva[i], (void *)(ST_GPA_BASE + i * STEAL_T= IME_SIZE)); =20 err =3D __vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &attr); TEST_ASSERT(err =3D=3D 0, "No PV stealtime Feature"); @@ -476,8 +472,7 @@ int main(int ac, char **av) =20 /* Second VCPU run, expect guest stolen time to be <=3D run_delay */ run_vcpu(vcpus[i]); - sync_global_from_guest(vm, guest_stolen_time[i]); - stolen_time =3D guest_stolen_time[i]; + stolen_time =3D SYNC_FROM_GUEST_AND_READ(vm, guest_stolen_time[i]); run_delay =3D get_run_delay(); TEST_ASSERT(stolen_time <=3D run_delay, "Expected stolen time <=3D %ld, got %ld", @@ -497,8 +492,7 @@ int main(int ac, char **av) =20 /* Run VCPU again to confirm stolen time is consistent with run_delay */ run_vcpu(vcpus[i]); - sync_global_from_guest(vm, guest_stolen_time[i]); - stolen_time =3D guest_stolen_time[i] - stolen_time; + stolen_time =3D SYNC_FROM_GUEST_AND_READ(vm, guest_stolen_time[i]) - sto= len_time; TEST_ASSERT(stolen_time >=3D run_delay, "Expected stolen time >=3D %ld, got %ld", run_delay, stolen_time); --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B3C6B1F4634 for ; Sat, 30 May 2026 00:23:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100592; cv=none; b=oXNY4GkiYjJzpsP+R+se9+E6xQTPKPpryGkHghm/VoXNiA1Pfj8Qc5UwjBKHUA9NpcTIFdR0GQZFAW3NuWiKH1gSXwErdt3DrX5al2/Sbq+oUdcO641FcBOEfEt+kEHsnuslEUkjj3baqv8NMVdacVDzF+rE0eoOBTwTtxJK1cQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100592; c=relaxed/simple; bh=NUwm4+hig0YVBidWUTEwG8zQL782N1I6qRmGhujqMv0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=crzqqP4NFAXUxVP3++QG8X5lUcsIfSPJGmTqB5hWHWMxDZMgOxSbOi0YZwbDHtWH04cGVEmvlW0e/5YY7CQiYjruDwSXQmXAjmFCYoLaQ4AupggPCboAnrvXg9u7ZscAyrBb08ljIUbKGpbcLHbe1rlGomw55WfBS8LsTfPbyec= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=kbd4dUSn; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kbd4dUSn" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2bd6aeb3637so320420115ad.2 for ; Fri, 29 May 2026 17:23:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100588; x=1780705388; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YjBpmrs5qmlmGxSbD10amIvDODEnkvfGNcjYVL/FcVU=; b=kbd4dUSnTCJ8q82qWOs+2AEQRo0ZTVV8WNgOh8+eBdr+tldx0P/ltS3WCw01VDPUru +BqUVP1gcmkRU/fECmCYwhMlroVybQupy5mDuN2BBw4/rxBUUq+RcQtxKW33yejAc1db kDgEsYXnkMFeI9aC3hfW5KQ6uwU+9wgDPlHgupRpjyZ8NkTVvTsY+E1JPcGDt7L6PqLb NUNJflp6vd+w47hlBCUKc9KxaAqXmAtnXqb0ok1jh8fkogXjA8WUvN6z8ruCm+1VW/SV wqHJg7280Z5klydKevQn6IEFAwWUwpneGdnGtlAI4ruEt6OEvTQ68ArEN0MGdGmylrop /iWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100588; x=1780705388; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YjBpmrs5qmlmGxSbD10amIvDODEnkvfGNcjYVL/FcVU=; b=XEHllkjnrVzUVZ8AdwuC3CqTo5NMn6hUCpRmKy3CATbPn3qTwIp5bEuou1yNgSjL4v JaE1/jh5AwlcCefqIY+tjX97WbzqCNIuC9sxxWGwKHJYT8H+Ftuj5tW6TjsnruBQY2lY GGdIq7RwauPU86wgn6/E3K23zDpsY/NOXqpi2qWjKv98m6PsBxRwOjFmo0uEbrSJQLQ5 ALc+yd9dhnP88sw7U+0uKhl8dj7mEeUWskTWAIinGJpEA/DswcjVHzl3NourzJqTHHyJ 8KAQPPTPgFLW6DHWDDzskea24rMc+NPIrg4u+rJiAkUBWyByZfKYLNdffo+yPogx8BJb hcrg== X-Forwarded-Encrypted: i=1; AFNElJ8/hOgVJUdoyI1RA1+mQNYzNL5hYmRsMbBEqa23bjcQoGLkn7RwmX++idlbSEh8idOa2IDLQIBiqGylzAY=@vger.kernel.org X-Gm-Message-State: AOJu0YzCgTrDGC7wpHxSOUUdb7PZpEHQ21BxhViXWfhkVWAPp9LgvnTN Jt2+Sq02UPfFeClDPL/oa+yi7niU4kGlkS95ZcjQ4XYIcC0k1AHxPHV+n9qYRzHmEp6SsTZEQ3n EDIk530Xl X-Received: from plbmo16.prod.google.com ([2002:a17:903:a90:b0:2b2:5117:a3f]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3585:b0:2b9:6458:1a2c with SMTP id d9443c01a7336-2bf367c3898mr22796995ad.13.1780100587862; Fri, 29 May 2026 17:23:07 -0700 (PDT) Date: Sat, 30 May 2026 00:21:19 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-5-jrhilke@google.com> Subject: [PATCH v4 04/19] KVM: selftests: Rename guest_rng to kvm_rng From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rename functions prefixed with 'guest_random_' to 'kvm_random_' and the global random state variable 'guest_rng' to 'kvm_rng'. This expands their usage and reflects that the random number generator can be used by both the host and the guest. No functional changes are intended. Suggested-by: Sean Christopherson Signed-off-by: Josh Hilke --- .../selftests/kvm/dirty_log_perf_test.c | 4 ++-- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- .../testing/selftests/kvm/include/test_util.h | 22 +++++++++---------- .../selftests/kvm/include/x86/kvm_util_arch.h | 4 ++-- tools/testing/selftests/kvm/lib/kvm_util.c | 20 ++++++++--------- tools/testing/selftests/kvm/lib/memstress.c | 8 +++---- tools/testing/selftests/kvm/lib/test_util.c | 6 ++--- 7 files changed, 33 insertions(+), 33 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/test= ing/selftests/kvm/dirty_log_perf_test.c index 0a1ea1d1e2d8..606abd858a12 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -311,7 +311,7 @@ int main(int argc, char *argv[]) int opt; =20 /* Override the seed to be deterministic by default. */ - guest_random_seed =3D 1; + kvm_random_seed =3D 1; =20 dirty_log_manual_caps =3D kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); @@ -357,7 +357,7 @@ int main(int argc, char *argv[]) p.phys_offset =3D strtoull(optarg, NULL, 0); break; case 'r': - guest_random_seed =3D atoi_positive("Random seed", optarg); + kvm_random_seed =3D atoi_positive("Random seed", optarg); break; case 's': p.backing_src =3D parse_backing_src_type(optarg); diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 66c3d9554379..cddf56400e6c 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -121,7 +121,7 @@ static void guest_code(void) while (true) { while (!READ_ONCE(vcpu_stop)) { addr =3D guest_test_virt_mem; - addr +=3D (guest_random_u64(&guest_rng) % guest_num_pages) + addr +=3D (kvm_random_u64(&kvm_rng) % guest_num_pages) * guest_page_size; addr =3D align_down(addr, host_page_size); =20 diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testin= g/selftests/kvm/include/test_util.h index b4872ba8ed12..ae39c4293b9a 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -106,30 +106,30 @@ struct timespec timespec_sub(struct timespec ts1, str= uct timespec ts2); struct timespec timespec_elapsed(struct timespec start); struct timespec timespec_div(struct timespec ts, int divisor); =20 -struct guest_random_state { +struct kvm_random_state { uint32_t seed; }; =20 -extern uint32_t guest_random_seed; -extern struct guest_random_state guest_rng; +extern uint32_t kvm_random_seed; +extern struct kvm_random_state kvm_rng; =20 -struct guest_random_state new_guest_random_state(uint32_t seed); -uint32_t guest_random_u32(struct guest_random_state *state); +struct kvm_random_state new_kvm_random_state(uint32_t seed); +uint32_t kvm_random_u32(struct kvm_random_state *state); =20 -static inline bool __guest_random_bool(struct guest_random_state *state, +static inline bool __kvm_random_bool(struct kvm_random_state *state, uint8_t percent) { - return (guest_random_u32(state) % 100) < percent; + return (kvm_random_u32(state) % 100) < percent; } =20 -static inline bool guest_random_bool(struct guest_random_state *state) +static inline bool kvm_random_bool(struct kvm_random_state *state) { - return __guest_random_bool(state, 50); + return __kvm_random_bool(state, 50); } =20 -static inline uint64_t guest_random_u64(struct guest_random_state *state) +static inline uint64_t kvm_random_u64(struct kvm_random_state *state) { - return ((uint64_t)guest_random_u32(state) << 32) | guest_random_u32(state= ); + return ((uint64_t)kvm_random_u32(state) << 32) | kvm_random_u32(state); } =20 enum vm_mem_backing_src_type { diff --git a/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h b/tool= s/testing/selftests/kvm/include/x86/kvm_util_arch.h index be35d26bb320..63a12ef9008e 100644 --- a/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h +++ b/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h @@ -55,9 +55,9 @@ static inline bool __vm_arch_has_protected_memory(struct = kvm_vm_arch *arch) do { \ const typeof(mem) val =3D (__val); \ \ - if (!is_forced_emulation_enabled || guest_random_bool(&guest_rng)) { \ + if (!is_forced_emulation_enabled || kvm_random_bool(&kvm_rng)) { \ (mem) =3D val; \ - } else if (guest_random_bool(&guest_rng)) { \ + } else if (kvm_random_bool(&kvm_rng)) { \ __asm__ __volatile__(KVM_FEP "mov %1, %0" \ : "+m" (mem) \ : "r" (val) : "memory"); \ diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/sel= ftests/kvm/lib/kvm_util.c index 1959bf556e88..b247b2015b2a 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -20,9 +20,9 @@ =20 #define KVM_UTIL_MIN_PFN 2 =20 -uint32_t guest_random_seed; -struct guest_random_state guest_rng; -static uint32_t last_guest_seed; +uint32_t kvm_random_seed; +struct kvm_random_state kvm_rng; +static uint32_t last_kvm_seed; =20 static size_t vcpu_mmap_sz(void); =20 @@ -515,12 +515,12 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uin= t32_t nr_runnable_vcpus, slot0 =3D memslot2region(vm, 0); ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size); =20 - if (guest_random_seed !=3D last_guest_seed) { - pr_info("Random seed: 0x%x\n", guest_random_seed); - last_guest_seed =3D guest_random_seed; + if (kvm_random_seed !=3D last_kvm_seed) { + pr_info("Random seed: 0x%x\n", kvm_random_seed); + last_kvm_seed =3D kvm_random_seed; } - guest_rng =3D new_guest_random_state(guest_random_seed); - sync_global_to_guest(vm, guest_rng); + kvm_rng =3D new_kvm_random_state(kvm_random_seed); + sync_global_to_guest(vm, kvm_rng); =20 kvm_arch_vm_post_create(vm, nr_runnable_vcpus); =20 @@ -2358,8 +2358,8 @@ void __attribute((constructor)) kvm_selftest_init(voi= d) sigaction(SIGILL, &sig_sa, NULL); sigaction(SIGFPE, &sig_sa, NULL); =20 - guest_random_seed =3D last_guest_seed =3D random(); - pr_info("Random seed: 0x%x\n", guest_random_seed); + kvm_random_seed =3D last_kvm_seed =3D random(); + pr_info("Random seed: 0x%x\n", kvm_random_seed); =20 kvm_selftest_arch_init(); } diff --git a/tools/testing/selftests/kvm/lib/memstress.c b/tools/testing/se= lftests/kvm/lib/memstress.c index 557c0a0a5658..c5df9c99102a 100644 --- a/tools/testing/selftests/kvm/lib/memstress.c +++ b/tools/testing/selftests/kvm/lib/memstress.c @@ -48,14 +48,14 @@ void memstress_guest_code(uint32_t vcpu_idx) { struct memstress_args *args =3D &memstress_args; struct memstress_vcpu_args *vcpu_args =3D &args->vcpu_args[vcpu_idx]; - struct guest_random_state rand_state; + struct kvm_random_state rand_state; uint64_t gva; uint64_t pages; uint64_t addr; uint64_t page; int i; =20 - rand_state =3D new_guest_random_state(guest_random_seed + vcpu_idx); + rand_state =3D new_kvm_random_state(kvm_random_seed + vcpu_idx); =20 gva =3D vcpu_args->gva; pages =3D vcpu_args->pages; @@ -69,13 +69,13 @@ void memstress_guest_code(uint32_t vcpu_idx) =20 for (i =3D 0; i < pages; i++) { if (args->random_access) - page =3D guest_random_u32(&rand_state) % pages; + page =3D kvm_random_u32(&rand_state) % pages; else page =3D i; =20 addr =3D gva + (page * args->guest_page_size); =20 - if (__guest_random_bool(&rand_state, args->write_percent)) + if (__kvm_random_bool(&rand_state, args->write_percent)) *(uint64_t *)addr =3D 0x0123456789ABCDEF; else READ_ONCE(*(uint64_t *)addr); diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/se= lftests/kvm/lib/test_util.c index 8a1848586a85..e342d9fb4771 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -30,13 +30,13 @@ void __attribute__((used)) expect_sigbus_handler(int si= gnum) * Park-Miller LCG using standard constants. */ =20 -struct guest_random_state new_guest_random_state(uint32_t seed) +struct kvm_random_state new_kvm_random_state(uint32_t seed) { - struct guest_random_state s =3D {.seed =3D seed}; + struct kvm_random_state s =3D {.seed =3D seed}; return s; } =20 -uint32_t guest_random_u32(struct guest_random_state *state) +uint32_t kvm_random_u32(struct kvm_random_state *state) { state->seed =3D (uint64_t)state->seed * 48271 % ((uint32_t)(1 << 31) - 1); return state->seed; --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C6A01A6812 for ; Sat, 30 May 2026 00:23:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100593; cv=none; b=O901RXp+drszkwJj7A8xrYLFQTZuOaz1xH0P5b/oNSK+4c1nLLWUQNwiYwDBX0v7UPqv/sROwlINX/IRZnvZT1/PzRHCgr4Plzz3L3jHb29kcZSdIvD8L4RQ4Qp9OLmiy7yFezMPmZXHF2tVu09AV2Siir7V7M8/Wl8F6xkV+rE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100593; c=relaxed/simple; bh=Wj+UbOSDYhhcXkbNuCEqtUYkT3jEsOdg5YGtZ2onw50=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gkfcEYNyP3tS9FevptcfZeBwbAMLbOhOzzCa5LMLAbpf3fSZqW/JcmoHSTgTF8YcwyWxqYLjrq0jWDPkdXaGAFERjHFuoOgz/QhxFLyyCDH5uZ82l8SBUFHmsB3UOe6+2bVnp5a5kbh40Qk8AOS+ZoEweN/95WIXEcuWvvpRGOY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PccNeIXZ; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PccNeIXZ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2bd6aeb3637so320420555ad.2 for ; Fri, 29 May 2026 17:23:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100590; x=1780705390; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6z2U56XL51gXNFdr4umtF89ubW0Siw/kvIEmINYMcys=; b=PccNeIXZDFHidJ2Y4Wg9eTshol0QBGPJAsUUPGi7ICdSZhalhK0HbcQ0B5ApeJYpo/ SwvJywKaSA5urdaTNGEMPc5A59ILsXQEZZIrPP1WJRFII0UqZKRCbO/6O3nFKVWC1Yhv QieQTLR22qSZJkLHj9/jfEYUfCdiTwPnqY1o5KdKVdFBIPCgPckQAN3LTZT7Kvh+CrXR 5I6e4hUd0z9UkvwlFoP8XjjZGzm9jn4S5NzPWmPvTm6UNcNSAtLjXfXs1ZvpT3rAfQCp CIXkgQLV4Y4Dwzrjr3jtbFbeGXdXUnjemot5930wAlwcB+v37uP6KCq4mb9LgtBGi+Bh /jSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100590; x=1780705390; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6z2U56XL51gXNFdr4umtF89ubW0Siw/kvIEmINYMcys=; b=NVm4b1EEs3f6jhTAFY0LUnuIOLC8+G//FP+ikoeBc5LBq6eCCF7rsbVf8kXjsR9bnW F4Qm2IyoEfSKAaRaTMpwVlAUDyZsq/m7VhkGkTCYlckwviGSxfh57oZ4pb+njwFo087Y 7hfjpVJ3XMgXRdd5rpVSXWaoA1e3IMJArlDWaDJOv+/cXNoBJ0M+RACOnkKl1WW2XOii YQtJ1GK/4GzAItBcT/jIHi6ukTzr+ss+VcqHniFikASKCXrdQWmd5rYxctZ2a7QfCSqq SLV+wDak1FEg2HTNQE9ZvW8I0o2D8IhSbjMDyPYlQnofeoFcjM1dGH+p+ClGa1imP35C d2nQ== X-Forwarded-Encrypted: i=1; AFNElJ+7V/ZWnBV3hIuhp/AkZQkbGQb+eCN9dVUxJSpGXlmfRKN4bVwJQxQt1tUiXpXmA+7XjlfPQmprWGh+a5c=@vger.kernel.org X-Gm-Message-State: AOJu0Ywceu0ehBIa1zJM1FO1KzWhc4HeyMxsieunkBDP4SiSrAg6IT46 5htGqfabqLGp5KiF8/2vgTMsRNONh9Csw0YOdKV2/CJe5nVpm/vV7yzLvypSiC2nRnLw/vn0cj/ IaMr2kClc X-Received: from plaq12.prod.google.com ([2002:a17:903:204c:b0:2b0:aef9:a5a2]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:17c8:b0:2bd:8dbb:293e with SMTP id d9443c01a7336-2bf367c80b3mr24794555ad.14.1780100589696; Fri, 29 May 2026 17:23:09 -0700 (PDT) Date: Sat, 30 May 2026 00:21:20 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-6-jrhilke@google.com> Subject: [PATCH v4 05/19] KVM: selftests: Add helper to generate random u64 in range [min,max] From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce kvm_random_u64_in_range(state, min, max). This function returns a random u64 in the inclusive range of [min, max] using a struct kvm_random_state. Suggested-by: Sean Christopherson Signed-off-by: Josh Hilke --- .../testing/selftests/kvm/include/test_util.h | 3 +++ tools/testing/selftests/kvm/lib/test_util.c | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testin= g/selftests/kvm/include/test_util.h index ae39c4293b9a..d601227dbdba 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -132,6 +132,9 @@ static inline uint64_t kvm_random_u64(struct kvm_random= _state *state) return ((uint64_t)kvm_random_u32(state) << 32) | kvm_random_u32(state); } =20 +uint64_t kvm_random_u64_in_range(struct kvm_random_state *state, uint64_t = min, + uint64_t max); + enum vm_mem_backing_src_type { VM_MEM_SRC_ANONYMOUS, VM_MEM_SRC_ANONYMOUS_THP, diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/se= lftests/kvm/lib/test_util.c index e342d9fb4771..dc47ed9736e9 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -42,6 +42,24 @@ uint32_t kvm_random_u32(struct kvm_random_state *state) return state->seed; } =20 +/* Returns a random u64 in the inclusive range [min, max] */ +uint64_t kvm_random_u64_in_range(struct kvm_random_state *state, uint64_t = min, + uint64_t max) +{ + uint64_t value; + uint64_t range; + + TEST_ASSERT(min <=3D max, "PEBKAC, min =3D 0x%lx, max =3D 0x%lx", min, ma= x); + + value =3D kvm_random_u64(state); + + range =3D max - min; + if (range =3D=3D ULLONG_MAX) + return value; + + return min + (value % (range + 1)); +} + /* * Parses "[0-9]+[kmgt]?". */ --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB2A01A3164 for ; Sat, 30 May 2026 00:23:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100595; cv=none; b=lgXSAVLWQQqRMiM8PqGiZWxk8wFYvpABrMTetzVuMgkjg/LBlZpGuM5dTNd8ynp1mKPAAB6AcZQM4ew1KT54Q9wbVuY8HyP44HyFY5XlfD4kbGH3aQt93apJmIdWEkSh0++BD2O7TBjs7wWLNQvNiHngt072L8fuLX81jZ0DULQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100595; c=relaxed/simple; bh=R+hEcm7b/r397fBUWES/1n0hvwRz7jF4RnQGQaDxRd4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gnhXkYem64YwnRESNM5Y+YUshOT0VBFiHwdjJsvWBI8P6sanP+pYjiACf9vlwD28/qAtFgRHzRWoFCB6f0AsEw9Fn5G3XFheDkiTfxnM+8BmBTehc7KLvXa6XPTQ9fEZYYSR7HnynHrd5njwdM/emt1e3ljJTl/SoYpJAAY8O+k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=BycmC3ow; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="BycmC3ow" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-8422e5b529dso38199b3a.0 for ; Fri, 29 May 2026 17:23:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100592; x=1780705392; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NpzUIf2nz3ba993vjETr2W7fmZYW6oBzRwXR+J9UohE=; b=BycmC3owNgNzR6tk0b7F99W3rptWAOubZmSiQpkcpI3xM1CayFh8vOwzvCSZuS5NLA s+JYX+QjkSrZbIP98rE2G8G6YnrZ2fhFP+fAPM2Eb3Q8ZqpnybI6A7pEkNepgTvK9eIv HIFSPylthFxFnirE+1eSI4/yQGekXlxRJ4a9nzFpPUMN6bvFoyHKnY7Bu8SzCNt++X2J BP2WP10xjAU4h6OMAfpkwEM5ABKvCnGXKUTmZu7KCqB++3ifvDff8Ye5fyosSw+VG6lw ngcuGTrxP0H5FAeLqcQM3TXx6PibHGr+M4neh+3oNMLtPiyOpQCuxP4x6vgXclPf07VT +kNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100592; x=1780705392; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NpzUIf2nz3ba993vjETr2W7fmZYW6oBzRwXR+J9UohE=; b=gC1SZv0gGBDQsDLBlP0i5J/TpaIBmxSqzO78/ri7NR4M2BitHm8DKUMfMJEmKLr9Z6 bo9D5hleWydpqj9BF26w6sx3/97ZUveLmazBlaeMBjKnS66Qwvr20mR8NNCja4T1qGDs lFnHieUZRDe4pIPgGEtN9k/yuUJfLVOeVJE8me2Kj9nm90EM2pXuoi5fGHzzRgsWpKUx 9ngallaFg18ofZifq5c8qCDJMPK6IA4f8Ph7zxM05+40zJowIFdu8K/zAzQmDugIRuag rLXrOZQk13/T3K369usvLRPlJO6mvo2Lc0mmH9gZlqSweELXR0eaHfb21sawTmukODkH SNEA== X-Forwarded-Encrypted: i=1; AFNElJ/oHWTvQcj82JDS5CGqcG1OhK/1FnnD65QaAoLHNpynXRtwV21N/G8B0yL8uCRdadYg3V+n/CArE9hh0j0=@vger.kernel.org X-Gm-Message-State: AOJu0YwHfQaMQOH+Uk6jT1uyH2TfAxtRGYgvgxTuvjgREuGs93hG+spZ oFTACybgAxuMOcmMBJWHDe2AVdKcf+fOFltVv7FoA9vAAVlz/yj8X+kIshOoOA8T4NaSYTZwNJY 7K8jG9NJP X-Received: from pfbfj37.prod.google.com ([2002:a05:6a00:3a25:b0:83e:b5cc:11c3]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3a0e:b0:838:29b3:9ed1 with SMTP id d2e1a72fcca58-842255c8233mr1554904b3a.41.1780100592008; Fri, 29 May 2026 17:23:12 -0700 (PDT) Date: Sat, 30 May 2026 00:21:21 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-7-jrhilke@google.com> Subject: [PATCH v4 06/19] KVM: selftests: Add IRQ injection test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add a new test, irq_test.c, which verifies that KVM correctly injects interrupts into a running guest when triggered via an eventfd bound to a GSI using the irqfd mechanism. This test is intentionally simple. Support for IRQ bypass, receiving interrupts from real devices, and other features will be added to this test in future changes. Suggested-by: Sean Christopherson Link: https://lore.kernel.org/kvm/20250404193923.1413163-68-seanjc@google.c= om/ Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/Makefile.kvm | 2 + tools/testing/selftests/kvm/irq_test.c | 174 +++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 tools/testing/selftests/kvm/irq_test.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index d944b81cad7d..9e5a00699dd0 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -149,6 +149,7 @@ TEST_GEN_PROGS_x86 +=3D coalesced_io_test TEST_GEN_PROGS_x86 +=3D dirty_log_perf_test TEST_GEN_PROGS_x86 +=3D guest_memfd_test TEST_GEN_PROGS_x86 +=3D hardware_disable_test +TEST_GEN_PROGS_x86 +=3D irq_test TEST_GEN_PROGS_x86 +=3D memslot_modification_stress_test TEST_GEN_PROGS_x86 +=3D memslot_perf_test TEST_GEN_PROGS_x86 +=3D mmu_stress_test @@ -157,6 +158,7 @@ TEST_GEN_PROGS_x86 +=3D steal_time TEST_GEN_PROGS_x86 +=3D system_counter_offset_test TEST_GEN_PROGS_x86 +=3D pre_fault_memory_test =20 + # Compiled outputs used by test targets TEST_GEN_PROGS_EXTENDED_x86 +=3D x86/nx_huge_pages_test =20 diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c new file mode 100644 index 000000000000..d3fee51cf451 --- /dev/null +++ b/tools/testing/selftests/kvm/irq_test.c @@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "kvm_util.h" +#include "test_util.h" +#include "apic.h" +#include "processor.h" + +#include +#include +#include +#include +#include + +static u64 timeout_ns =3D 2ULL * 1000 * 1000 * 1000; +static bool guest_ready_for_irqs[KVM_MAX_VCPUS]; +static bool guest_received_irq[KVM_MAX_VCPUS]; +static bool done; + +#define GUEST_RECEIVED_IRQ(__vcpu) \ + SYNC_FROM_GUEST_AND_READ((__vcpu)->vm, guest_received_irq[(__vcpu)->id]) + +static u32 guest_get_vcpu_id(void) +{ + return x2apic_read_reg(APIC_ID); +} + +static void guest_irq_handler(struct ex_regs *regs) +{ + WRITE_ONCE(guest_received_irq[guest_get_vcpu_id()], true); + + x2apic_write_reg(APIC_EOI, 0); +} + +static void guest_code(void) +{ + x2apic_enable(); + + sti_nop(); + + WRITE_ONCE(guest_ready_for_irqs[guest_get_vcpu_id()], true); + + while (!READ_ONCE(done)) + cpu_relax(); + + GUEST_DONE(); +} + +static void *vcpu_thread_main(void *arg) +{ + struct kvm_vcpu *vcpu =3D arg; + struct ucall uc; + + vcpu_run(vcpu); + TEST_ASSERT_EQ(UCALL_DONE, get_ucall(vcpu, &uc)); + + return NULL; +} + +static void kvm_route_msi(struct kvm_vm *vm, u32 gsi, struct kvm_vcpu *vcp= u, + u8 vector) +{ + struct { + struct kvm_irq_routing head; + struct kvm_irq_routing_entry entry; + } routing_data =3D {}; + + struct kvm_irq_routing *routes =3D &routing_data.head; + + routes->nr =3D 1; + routes->entries[0].gsi =3D gsi; + routes->entries[0].type =3D KVM_IRQ_ROUTING_MSI; + routes->entries[0].u.msi.address_lo =3D 0xFEE00000 | (vcpu->id << 12); + routes->entries[0].u.msi.data =3D vector; + + vm_ioctl(vm, KVM_SET_GSI_ROUTING, routes); +} + +static void help(const char *name) +{ + printf("Usage: %s [-h]\n", name); + printf("\n"); + printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n"); + printf("\n"); + exit(KSFT_FAIL); +} + +int main(int argc, char **argv) +{ + /* + * Pick a random vector and a random GSI to use for device IRQ. + * + * Pick an IRQ vector in range [32, UINT8_MAX]. Min value is 32 because + * Linux/x86 reserves vectors 0-31 for exceptions and architecture + * defined NMIs and interrupts. + * + * Pick a GSI in range [24, KVM_MAX_IRQ_ROUTES - 1]. The min value is 24 + * because KVM reserves GSIs 0-15 for legacy ISA IRQs and 16-23 only go + * to the IOAPIC. The max is KVM_MAX_IRQ_ROUTES - 1, because + * KVM_MAX_IRQ_ROUTES is exclusive. + */ + u32 gsi =3D kvm_random_u64_in_range(&kvm_rng, 24, KVM_MAX_IRQ_ROUTES - 1); + u8 vector =3D kvm_random_u64_in_range(&kvm_rng, 32, UINT8_MAX); + + struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; + pthread_t vcpu_threads[KVM_MAX_VCPUS]; + int nr_irqs =3D 1000, nr_vcpus =3D 1; + int i, j, c, eventfd; + struct kvm_vm *vm; + + while ((c =3D getopt(argc, argv, "h")) !=3D -1) { + switch (c) { + case 'h': + default: + help(argv[0]); + } + } + + TEST_REQUIRE(kvm_arch_has_default_irqchip()); + + vm =3D vm_create_with_vcpus(nr_vcpus, guest_code, vcpus); + vm_install_exception_handler(vm, vector, guest_irq_handler); + + eventfd =3D kvm_new_eventfd(); + + printf("Injecting interrupts for GSI %d (Vector 0x%x) %d times\n", + gsi, vector, nr_irqs); + + kvm_assign_irqfd(vm, gsi, eventfd); + + for (i =3D 0; i < nr_vcpus; i++) + pthread_create(&vcpu_threads[i], NULL, vcpu_thread_main, vcpus[i]); + + for (i =3D 0; i < nr_vcpus; i++) { + struct kvm_vcpu *vcpu =3D vcpus[i]; + + while (!SYNC_FROM_GUEST_AND_READ(vm, guest_ready_for_irqs[vcpu->id])) + continue; + } + + for (i =3D 0; i < nr_irqs; i++) { + struct kvm_vcpu *vcpu =3D vcpus[i % nr_vcpus]; + struct timespec start; + + kvm_route_msi(vm, gsi, vcpu, vector); + + for (j =3D 0; j < nr_vcpus; j++) + TEST_ASSERT(!GUEST_RECEIVED_IRQ(vcpus[j]), + "IRQ flag for vCPU %d not clear prior to test", + vcpus[j]->id); + + /* Trigger interrupt */ + eventfd_write(eventfd, 1); + + clock_gettime(CLOCK_MONOTONIC, &start); + for (;;) { + if (GUEST_RECEIVED_IRQ(vcpu)) + break; + + if (timespec_to_ns(timespec_elapsed(start)) > timeout_ns) + TEST_FAIL("vCPU %d timed out waiting for IRQ from GSI %d (Vector 0x%x)= !\n", + vcpu->id, gsi, vector); + } + + WRITE_AND_SYNC_TO_GUEST(vm, guest_received_irq[vcpu->id], false); + } + + WRITE_AND_SYNC_TO_GUEST(vm, done, true); + + for (i =3D 0; i < nr_vcpus; i++) + pthread_join(vcpu_threads[i], NULL); + + printf("Test passed!\n"); + + return 0; +} --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D25ED2248A0 for ; Sat, 30 May 2026 00:23:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100596; cv=none; b=YM3SolhSqjwETe2b+FZrh88zk7kFl84heEgAxF42VewH10pk1LRxuRbU+/iTvunNMJUiJ2tmmb4KtPZYdMa3TnUler+Cg/8yyfkeevZdi1OY1Vbko7hHdbu5ah7CKBb1OBWWam6uIllvaGqfaKSzuGlwSDk11XGvthRNubbixlg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100596; c=relaxed/simple; bh=aF6O9VsSjQ97pF3SrAsvLPtLXN6re5Q7TRYgYqYPOZQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oWOkV/SkwsOd9a8ctpmgKawcHgkdLk9iW/Bs6f6qudv6xlkcc2tcJj99TNgMAXkao+mkCrWaj/R4xbfYZGryuyBu9EWsx/Nm6yJay/0+E/Oneo1cLfkYNP6Jbl02QBMvNuMWycjAl3UYDpSG5pFvdd/AQE7qVwP2MYu1N1tUEsU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=EL3k2J+l; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EL3k2J+l" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2bf08c2a24bso26911025ad.2 for ; Fri, 29 May 2026 17:23:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100594; x=1780705394; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1p04x6ZghZmLiQW4r6y7TuUkEl7HeypX+13BZ14qNiE=; b=EL3k2J+lHvpsnI9YEkzX3rvrUYwExE4gsgH6CGJAuw0BQJTHlB98fMpIdSwzsFEZxN MRoqpDbU3S2gIp9yVERKBQHhBYM4L0YbviAkTssk9/ACFP1capT8fA9Du87zUDQ7Jh4n fkmTW/p6pp5rY+V6D4B1j9E9/dLWkEln7oU2vkIGrAq0Lsk46iSeX018ejridubYNCH3 EsxCJ2kVGmogsWtEqvS1FEQrq+Kmw5yblRoTJhPPbXGq6zo4NzKY9u9aHJMb8IFCV9WG KA1mpBurXbhHyEfNLIivMlCDxVQg6y15PjYWvQdcjKRbe1Fg7DefzvYKonbXGV/zxHDi pQwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100594; x=1780705394; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1p04x6ZghZmLiQW4r6y7TuUkEl7HeypX+13BZ14qNiE=; b=AYEOm/G7rh4VD//bM8ajTkSylEOBfwuYDbHQl24pi55IQRIQ8HY3Qv3g4CVQTCwrfU f6fCvUcDswt7xUKRrVA7oi3NhW0r1f92B3cmXhGw2JmJ9rWj/PhEtTvEvbzFtYMD5eaM 6mXS8YnMtuON4e98s+K5KlZqe59aXiMXwNYXXHSR/Mu/A9+iDtPr3B+agw6/jt8FomvI uwRpyrIsrAm/v+LZtNHZi9RH43TRr05UwcoQI9gEpNhbtOOV4n1/ysFz1jXw3TJnOivv Dvi9mnwFnLCXyu6pCETDcFquQBppp11GcTNMgpdxbLIS8EcjwTayPMLhcHjMYN2Ojbh9 /kOg== X-Forwarded-Encrypted: i=1; AFNElJ+YSTe87ti5dDFhpfaOdEsMxnk4a0h84V7MmAKDMnKx0lsCzmZfuuixfqdEp7DjFCihjgs3YNnh6BuIqTA=@vger.kernel.org X-Gm-Message-State: AOJu0YxKzeBExoaFF8LSCgF2FdBVfYWciCVfgM3BpSjXwIOij6NsPtpY VYEjDPzzD8ZO/jh0hY0jILeIDAw8i+3fQWzReR8BwR2PghB08XEWgntqgKuU64jdLWq1PblEF+/ ODzdtir8k X-Received: from play17.prod.google.com ([2002:a17:902:e191:b0:2bd:16cc:1966]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2409:b0:2bf:211c:4977 with SMTP id d9443c01a7336-2bf3686ccc3mr25408265ad.36.1780100593997; Fri, 29 May 2026 17:23:13 -0700 (PDT) Date: Sat, 30 May 2026 00:21:22 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-8-jrhilke@google.com> Subject: [PATCH v4 07/19] KVM: selftests: Verify IRQ bypass works in IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Trigger interrupts from a VFIO device instead of emulating interrupts using KVM eventfds. This verifies that guests receive interrupts via IRQ bypass. Add a '-d' argument to tools/testing/selftests/kvm/irq_test which takes the segment:bus:device.function number of a PCI device bound to VFIO which will trigger interrupts. The device must have a VFIO selftest driver in order to work with the test. Example: $ ./tools/testing/selftests/kvm/irq_test -d 0000:06:0a.1 Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/irq_test.c | 61 +++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c index d3fee51cf451..616eb38c05b8 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -1,14 +1,17 @@ // SPDX-License-Identifier: GPL-2.0 #include "kvm_util.h" #include "test_util.h" +#include #include "apic.h" #include "processor.h" +#include "proc_util.h" =20 #include #include #include #include #include +#include =20 static u64 timeout_ns =3D 2ULL * 1000 * 1000 * 1000; static bool guest_ready_for_irqs[KVM_MAX_VCPUS]; @@ -55,6 +58,36 @@ static void *vcpu_thread_main(void *arg) return NULL; } =20 +static int vfio_setup_msi(struct vfio_pci_device *device) +{ + const int flags =3D MAP_SHARED | MAP_ANONYMOUS; + const int prot =3D PROT_READ | PROT_WRITE; + struct dma_region *region; + + /* A driver is required to generate an MSI. */ + TEST_REQUIRE(device->driver.ops); + + /* Set up a DMA-able region for the driver to use. */ + region =3D &device->driver.region; + region->iova =3D 0; + region->size =3D SZ_2M; + region->vaddr =3D kvm_mmap(region->size, prot, flags, -1); + TEST_ASSERT(region->vaddr !=3D MAP_FAILED, "mmap() failed\n"); + iommu_map(device->iommu, region); + + vfio_pci_driver_init(device); + return device->driver.msi; +} + +static void trigger_interrupt(struct vfio_pci_device *device, int eventfd) +{ + if (device) + vfio_pci_driver_send_msi(device); + else + eventfd_write(eventfd, 1); +} + + static void kvm_route_msi(struct kvm_vm *vm, u32 gsi, struct kvm_vcpu *vcp= u, u8 vector) { @@ -76,9 +109,10 @@ static void kvm_route_msi(struct kvm_vm *vm, u32 gsi, s= truct kvm_vcpu *vcpu, =20 static void help(const char *name) { - printf("Usage: %s [-h]\n", name); + printf("Usage: %s [-d ] [-h]\n", name); printf("\n"); printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n"); + printf("-d Use a VFIO device to send MSI-X interrupts instead of using an= emulated eventfd\n"); printf("\n"); exit(KSFT_FAIL); } @@ -102,12 +136,18 @@ int main(int argc, char **argv) =20 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; pthread_t vcpu_threads[KVM_MAX_VCPUS]; + struct vfio_pci_device *device =3D NULL; int nr_irqs =3D 1000, nr_vcpus =3D 1; - int i, j, c, eventfd; + int i, j, c, msi, irq, eventfd; + const char *device_bdf =3D NULL; + struct iommu *iommu; struct kvm_vm *vm; =20 - while ((c =3D getopt(argc, argv, "h")) !=3D -1) { + while ((c =3D getopt(argc, argv, "d:h")) !=3D -1) { switch (c) { + case 'd': + device_bdf =3D optarg; + break; case 'h': default: help(argv[0]); @@ -119,7 +159,17 @@ int main(int argc, char **argv) vm =3D vm_create_with_vcpus(nr_vcpus, guest_code, vcpus); vm_install_exception_handler(vm, vector, guest_irq_handler); =20 - eventfd =3D kvm_new_eventfd(); + if (device_bdf) { + iommu =3D iommu_init(default_iommu_mode); + device =3D vfio_pci_device_init(device_bdf, iommu); + msi =3D vfio_setup_msi(device); + irq =3D get_proc_vfio_irq_number(device_bdf, msi); + eventfd =3D device->msi_eventfds[msi]; + printf("Using device %s MSI-X[%d] (IRQ-%d)\n", device_bdf, msi, + irq); + } else { + eventfd =3D kvm_new_eventfd(); + } =20 printf("Injecting interrupts for GSI %d (Vector 0x%x) %d times\n", gsi, vector, nr_irqs); @@ -147,8 +197,7 @@ int main(int argc, char **argv) "IRQ flag for vCPU %d not clear prior to test", vcpus[j]->id); =20 - /* Trigger interrupt */ - eventfd_write(eventfd, 1); + trigger_interrupt(device, eventfd); =20 clock_gettime(CLOCK_MONOTONIC, &start); for (;;) { --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1E1C23D2A4 for ; Sat, 30 May 2026 00:23:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100598; cv=none; b=aDjDCszm67sXC0p1eX1x3iRC7US9BCx1hODCzRR0ll7RK0efSwgJBYDTIVhLdmyCwjyIP7XnHEPn+apS9oAUWRRsoFvWUdiRJDD+RS5RC9e4MchzHtjSkCC7YBzXHHk9w5H69n5kQwnNvarB5YnIlEinGAHk5y53V/pgM7wsrUE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100598; c=relaxed/simple; bh=kuJDGzNlOupw9zrcTsIKl2bXMewkhe+KmRKeRL79LdI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=r1oZfrDGEm0fvTXsdsT8AQryN/0bbZlvp7+QQHi7grqn5es0snoWSMFO/j0OZGsdyOjQjun36zRFCIYm+GOdC7kJRfS4Dbl5y4j2pk3/ddOHGgIEh+yJgfLdfMX5slKGNY47Q5XVOo6UPW7G+wJucwTAfnsim/LaktIsrA0qQ/0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hUxQDWkx; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hUxQDWkx" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2c0a99db8dfso39805ad.3 for ; Fri, 29 May 2026 17:23:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100596; x=1780705396; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tI/496f5tHNM/4UlnuACeSVaDbzvZJZQ1aj5DlmVwnc=; b=hUxQDWkx4vGN683yOM3E9CFejk+Jz1fD1Qc85ciHQwM1gc7k8UYrVzbwevPAGaHwIy GIxcXxl0jauOZvLvWfZhUl3+DAmY8/gHDVwQTbtjHBrUFptC35G/5TSJQqT31ohNBf54 L6vwEtvvYJyHuHjwFkN5UATeOd/025CdbCe8qbcvoeeG38QyTKmyndGM0rj4b7IiveuW EciQ+6d3VqD/H8fBt8LX8A8mZH+pNayCERa2YLEJIT2vB77aQDwbvzK3EHcVpJW8GDww KTmDmln/xWTwOctIA9aCEW8+gQ6VY5TAyUsmlU2LPC7UqJ6URe9KYtgkO8R3rGYpAWpc TPEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100596; x=1780705396; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tI/496f5tHNM/4UlnuACeSVaDbzvZJZQ1aj5DlmVwnc=; b=AAwxICuQmMae4cLgPrvOXJHxeLJgRWAUpoe7Q++dLBkdkcTvlC7n2rN0tdIKPgKQJy FSWfnACBBWQ1c0Wf5CY4vI9vLu+6iXCRvhVUKcVO/NyXtXPblk/9WVp/o0hON1BeZZ9E z/6aFJ9ASTwzIhj/5dU2gJ4W/CcFi+oW1BWGFASUZgW2ch7JGhHesxLuzCMaT7mUGLue MIqP1ykbn4vezkebA+al3/pFIre1g/ZA1T/7F1pmS6Yqjh0+CAWrM/JV5hwBAm9h43+5 DE3mdfTWWtuKvQm68cq6DGjoP/RVJbhqxxk5R7G/tt8kAgC1MGyyPerWaCcinzvpXNwJ LtjQ== X-Forwarded-Encrypted: i=1; AFNElJ8/RUYVFgbhl4YgGkAMe+v9aqpXOcCffGQVb5RUq5q8lei+JX1Bb5AnhSOfhHKkg/71WGy+s88Yb6gdMqY=@vger.kernel.org X-Gm-Message-State: AOJu0YyPDJoKfZDVq4lgFGe9pdINwhHnJYqfb7kRHihmTjeYZ8ml5paz VcXFZmyFT3GyjGKo0w++c2FLbLH8f8rm2VDy0eYaBEpNlv5MFABwstKzYDs70lU7uDltZ8MmCYc 30M7GIUDT X-Received: from plbbj2.prod.google.com ([2002:a17:902:8502:b0:2bf:1b54:1fc4]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e88c:b0:2bf:30c8:bfa1 with SMTP id d9443c01a7336-2bf368bd8aamr23911685ad.41.1780100596044; Fri, 29 May 2026 17:23:16 -0700 (PDT) Date: Sat, 30 May 2026 00:21:23 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-9-jrhilke@google.com> Subject: [PATCH v4 08/19] KVM: selftests: Verify interrupts are received when IRQ affinity changes in IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add the '-a' flag to tools/testing/selftests/kvm/irq_test.c to randomly affinitize the device's host IRQ to different physical CPUs throughout the test. This stresses the kernel's ability to maintain correct interrupt routing and delivery even as the underlying hardware IRQ affinity is changed dynamically via /proc/irq/. Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- .../testing/selftests/kvm/include/proc_util.h | 19 +++++++++++ tools/testing/selftests/kvm/irq_test.c | 34 ++++++++++++++++--- tools/testing/selftests/kvm/lib/proc_util.c | 20 +++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/proc_util.h b/tools/testin= g/selftests/kvm/include/proc_util.h index 7c465e8584e2..2c8086158f68 100644 --- a/tools/testing/selftests/kvm/include/proc_util.h +++ b/tools/testing/selftests/kvm/include/proc_util.h @@ -6,4 +6,23 @@ =20 int get_proc_vfio_irq_number(const char *vfio_device_bdf, int msi); =20 +/* + * open_proc_irq_affinity - Open the smp_affinity_list file for a given IRQ + * @irq: The IRQ number + * + * Opens /proc/irq//smp_affinity_list for writing and returns the FILE + * pointer. + */ +FILE *open_proc_irq_affinity(int irq); + +/* + * write_proc_irq_affinity - Write a CPU number to the smp_affinity_list f= ile + * @fp: The FILE pointer for the smp_affinity_list file + * @irq: The IRQ number (for error reporting) + * @irq_cpu: The CPU number to write + * + * Writes the given CPU number to the provided FILE pointer. + */ +void write_proc_irq_affinity(FILE *fp, int irq, int irq_cpu); + #endif /* SELFTEST_KVM_PROC_UTIL_H */ diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c index 616eb38c05b8..e76784e0a054 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -11,11 +11,13 @@ #include #include #include +#include #include =20 static u64 timeout_ns =3D 2ULL * 1000 * 1000 * 1000; static bool guest_ready_for_irqs[KVM_MAX_VCPUS]; static bool guest_received_irq[KVM_MAX_VCPUS]; +static bool irq_affinity; static bool done; =20 #define GUEST_RECEIVED_IRQ(__vcpu) \ @@ -109,9 +111,10 @@ static void kvm_route_msi(struct kvm_vm *vm, u32 gsi, = struct kvm_vcpu *vcpu, =20 static void help(const char *name) { - printf("Usage: %s [-d ] [-h]\n", name); + printf("Usage: %s [-a] [-d ] [-h]\n", name); printf("\n"); printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n"); + printf("-a Randomly affinitize the device's host IRQ to different physica= l CPUs throughout the test\n"); printf("-d Use a VFIO device to send MSI-X interrupts instead of using an= emulated eventfd\n"); printf("\n"); exit(KSFT_FAIL); @@ -134,17 +137,21 @@ int main(int argc, char **argv) u32 gsi =3D kvm_random_u64_in_range(&kvm_rng, 24, KVM_MAX_IRQ_ROUTES - 1); u8 vector =3D kvm_random_u64_in_range(&kvm_rng, 32, UINT8_MAX); =20 + int i, j, c, msi, irq, eventfd, irq_cpu; struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; pthread_t vcpu_threads[KVM_MAX_VCPUS]; struct vfio_pci_device *device =3D NULL; int nr_irqs =3D 1000, nr_vcpus =3D 1; - int i, j, c, msi, irq, eventfd; const char *device_bdf =3D NULL; + FILE *irq_affinity_fp =3D NULL; struct iommu *iommu; struct kvm_vm *vm; =20 - while ((c =3D getopt(argc, argv, "d:h")) !=3D -1) { + while ((c =3D getopt(argc, argv, "ad:h")) !=3D -1) { switch (c) { + case 'a': + irq_affinity =3D true; + break; case 'd': device_bdf =3D optarg; break; @@ -171,6 +178,11 @@ int main(int argc, char **argv) eventfd =3D kvm_new_eventfd(); } =20 + if (irq_affinity) { + TEST_ASSERT(device_bdf, "-a requires -d"); + irq_affinity_fp =3D open_proc_irq_affinity(irq); + } + printf("Injecting interrupts for GSI %d (Vector 0x%x) %d times\n", gsi, vector, nr_irqs); =20 @@ -192,6 +204,11 @@ int main(int argc, char **argv) =20 kvm_route_msi(vm, gsi, vcpu, vector); =20 + if (irq_affinity && vcpu->id =3D=3D 0) { + irq_cpu =3D kvm_random_u64(&kvm_rng) % get_nprocs(); + write_proc_irq_affinity(irq_affinity_fp, irq, irq_cpu); + } + for (j =3D 0; j < nr_vcpus; j++) TEST_ASSERT(!GUEST_RECEIVED_IRQ(vcpus[j]), "IRQ flag for vCPU %d not clear prior to test", @@ -204,9 +221,15 @@ int main(int argc, char **argv) if (GUEST_RECEIVED_IRQ(vcpu)) break; =20 - if (timespec_to_ns(timespec_elapsed(start)) > timeout_ns) + if (timespec_to_ns(timespec_elapsed(start)) > timeout_ns) { + printf("Timeout waiting for interrupt!\n"); + printf(" vCPU: %d\n", vcpu->id); + if (irq_affinity) + printf(" irq_cpu: %d\n", irq_cpu); + TEST_FAIL("vCPU %d timed out waiting for IRQ from GSI %d (Vector 0x%x)= !\n", vcpu->id, gsi, vector); + } } =20 WRITE_AND_SYNC_TO_GUEST(vm, guest_received_irq[vcpu->id], false); @@ -217,6 +240,9 @@ int main(int argc, char **argv) for (i =3D 0; i < nr_vcpus; i++) pthread_join(vcpu_threads[i], NULL); =20 + if (irq_affinity) + fclose(irq_affinity_fp); + printf("Test passed!\n"); =20 return 0; diff --git a/tools/testing/selftests/kvm/lib/proc_util.c b/tools/testing/se= lftests/kvm/lib/proc_util.c index ad1c54a81869..e5a4ee262f5e 100644 --- a/tools/testing/selftests/kvm/lib/proc_util.c +++ b/tools/testing/selftests/kvm/lib/proc_util.c @@ -40,3 +40,23 @@ int get_proc_vfio_irq_number(const char *device_bdf, int= msi) return irq; } =20 +FILE *open_proc_irq_affinity(int irq) +{ + char path[PATH_MAX]; + FILE *fp; + + snprintf(path, sizeof(path), "/proc/irq/%d/smp_affinity_list", irq); + fp =3D fopen(path, "w"); + TEST_ASSERT(fp, "fopen(%s) failed", path); + + return fp; +} + +void write_proc_irq_affinity(FILE *fp, int irq, int irq_cpu) +{ + int ret; + + ret =3D fprintf(fp, "%d\n", irq_cpu); + TEST_ASSERT(ret > 0, "Failed to affinitize IRQ-%d to CPU %d", irq, irq_cp= u); + fflush(fp); +} --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC56825B0B0 for ; Sat, 30 May 2026 00:23:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100600; cv=none; b=o7N+sHBPdzDrRBC8tsRK3uEvd+meUA8Ayk6qe6Fxz0z66ncF5dzthNeWVvAGjFIQnHKjf3PW++OkFAgGuOIvqDQCXbRQ/UkjGdnT0mjGq7SJCRzF5RwVgAPiBp2NakLv8mTmytoMYmjMvKZYn3tgX1xvV2siezou1f1iHAiGr8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100600; c=relaxed/simple; bh=H7ncH0RzOYzAnvbEgFFJpfICxIyEjpQiv3hqtwy9Qx0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XU2mo4yjZVaIp403g+5EfSZ/I2lrrJKw9QCeE/YKw+d+/H3Day1mPxiLS7dAcEMZ0sssPAHT7+vyNxrEo4qPoE0Bfx6tpK3oyl3FPAkwgsy0gu38kBHVJKIxbrGWrkoYYSNxkVe7LTwPf1ZLwyLFlM7F9LI8cDd5zWyIBsAVfVU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Tb8Umrtk; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Tb8Umrtk" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c8576bc9254so1299843a12.1 for ; Fri, 29 May 2026 17:23:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100598; x=1780705398; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xvgcKpRJpmTCSpBiZzMNprp4unBpkZcFJd82mVpUF10=; b=Tb8UmrtkwH6n0uLy16LrR10k0j2W0t47KQFZ4ep8aZrp9S8P4+9oxu83TtBkYlLf9u 2ep+SZS90W5erUwWNZMkuwCQB8MscSEueWXS+4+sJbIxZSjBP0cbOsiSuNNgCEtNpJlV 9Hgb/w8T3pEUVR+ZFO/Iz8AVP7yErhSr2bsirk51BkG8VFR9zgPKFMSXFqSt+CJvMLKy WJCdX0KA+bzC7BNCOTMfRhqIrkMJqG1MLwuTa8/gm+2HuDOdBCjPHuLILgiydGO83uX5 WpmnI482sVNYzsVRLv7cHwxgdvwX9OwjaTrIt0AxzeMKSyONe+VXpEcCnWtRTs8qcHXv FPsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100598; x=1780705398; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=xvgcKpRJpmTCSpBiZzMNprp4unBpkZcFJd82mVpUF10=; b=JVmHkXDLlwIyFlE/aYLsxH3Fl6qJXfX/qxvrm5d8tGuTXzMqmpjJOOz/l4Xke0MWZC AIUr/Oh8TQgIMmzhWgpa2ZYskOrzcrWhRqrLdAmBu60NGCZG7xfIfM4BVyY9yGOvVxwv DCK42av/Qcra5Z7Qs1xjB+nHOXmG/5wvVgfP5zKZ5H9eKAGFaEGvTJG39srMnAyTdGqh 7GbGYGDX4TVm8/6u3J2ubpb/B9NoXYpGX/jcIEJgHYAENVdlnYjbUuxg3lhZ02+3geHW HhrkcWBNuDofD2RclkoIrecLF5FqvArYLa6bwRVqukMBE4GZ+LiJ1y2rmq6dAzVHEg05 Qz1A== X-Forwarded-Encrypted: i=1; AFNElJ8I80vgSOVIYdhckDVNvpyeSIK9gVyGzieAT2uRs6b3Ry9NE4DYu7OjnYyH/k6hIoCZLVYwlotgNQkju9M=@vger.kernel.org X-Gm-Message-State: AOJu0YzE4CS/5bfa8QV3mcYCV5D6YehlCg6yI6+Txm4Q2O/SZ/7SqQ/S 2VG2/h2Q6UyrGWufjqOMJ3I6IMtJJFtr0093HXy5OvDhmL+ULc6uyEE4B7SMPSBWRtgc9JQZokn ebRtRAZrD X-Received: from pgh1.prod.google.com ([2002:a05:6a02:4e01:b0:c82:7761:9940]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3a46:b0:3a2:f402:50fb with SMTP id adf61e73a8af0-3b4281d8961mr1589744637.34.1780100597979; Fri, 29 May 2026 17:23:17 -0700 (PDT) Date: Sat, 30 May 2026 00:21:24 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-10-jrhilke@google.com> Subject: [PATCH v4 09/19] KVM: selftests: Verify IRQs wake up halted vCPUs in IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add the '-b' flag to tools/testing/selftests/kvm/irq_test.c to verify that interrupts successfully wake up vCPUs that are in a halted state. By default, the guest code spins using cpu_relax() while waiting for interrupts. With the '-b' flag, the guest executes the HLT instruction instead. At the end of the test, the host explicitly routes and triggers a final interrupt for each vCPU. If the interrupt fails to wake the halted vCPU, the guest will never exit its loop, and the host's call to pthread_join() will hang, causing the test to time out and fail. Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/irq_test.c | 32 ++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c index e76784e0a054..562b9ab187a4 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -18,6 +18,7 @@ static u64 timeout_ns =3D 2ULL * 1000 * 1000 * 1000; static bool guest_ready_for_irqs[KVM_MAX_VCPUS]; static bool guest_received_irq[KVM_MAX_VCPUS]; static bool irq_affinity; +static bool block_vcpus; static bool done; =20 #define GUEST_RECEIVED_IRQ(__vcpu) \ @@ -43,8 +44,12 @@ static void guest_code(void) =20 WRITE_ONCE(guest_ready_for_irqs[guest_get_vcpu_id()], true); =20 - while (!READ_ONCE(done)) - cpu_relax(); + while (!READ_ONCE(done)) { + if (block_vcpus) + hlt(); + else + cpu_relax(); + } =20 GUEST_DONE(); } @@ -111,10 +116,11 @@ static void kvm_route_msi(struct kvm_vm *vm, u32 gsi,= struct kvm_vcpu *vcpu, =20 static void help(const char *name) { - printf("Usage: %s [-a] [-d ] [-h]\n", name); + printf("Usage: %s [-a] [-b] [-d ] [-h]\n", n= ame); printf("\n"); printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n"); printf("-a Randomly affinitize the device's host IRQ to different physica= l CPUs throughout the test\n"); + printf("-b Block vCPUs (e.g. HLT) instead of spinning in guest-mode\n"); printf("-d Use a VFIO device to send MSI-X interrupts instead of using an= emulated eventfd\n"); printf("\n"); exit(KSFT_FAIL); @@ -147,11 +153,14 @@ int main(int argc, char **argv) struct iommu *iommu; struct kvm_vm *vm; =20 - while ((c =3D getopt(argc, argv, "ad:h")) !=3D -1) { + while ((c =3D getopt(argc, argv, "abd:h")) !=3D -1) { switch (c) { case 'a': irq_affinity =3D true; break; + case 'b': + block_vcpus =3D true; + break; case 'd': device_bdf =3D optarg; break; @@ -188,6 +197,8 @@ int main(int argc, char **argv) =20 kvm_assign_irqfd(vm, gsi, eventfd); =20 + sync_global_to_guest(vm, block_vcpus); + for (i =3D 0; i < nr_vcpus; i++) pthread_create(&vcpu_threads[i], NULL, vcpu_thread_main, vcpus[i]); =20 @@ -237,8 +248,19 @@ int main(int argc, char **argv) =20 WRITE_AND_SYNC_TO_GUEST(vm, done, true); =20 - for (i =3D 0; i < nr_vcpus; i++) + for (i =3D 0; i < nr_vcpus; i++) { + /* + * Verify that sending an interrupt to a halted vCPU wakes it + * up. If the vCPU does not wake up, the call to pthread_join(), + * below, will hang. + */ + if (block_vcpus) { + kvm_route_msi(vm, gsi, vcpus[i], vector); + trigger_interrupt(device, eventfd); + } + pthread_join(vcpu_threads[i], NULL); + } =20 if (irq_affinity) fclose(irq_affinity_fp); --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F588213E9C for ; Sat, 30 May 2026 00:23:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100601; cv=none; b=jpoN9TxPLG13Ry46NunQiak44feiFO4cqU4cvVbBgX1GAhvn6VO9BqmnKoITBNGIcO2FOmrU+LWog4f7GvjLyo/M0VNQdImy6ASeuXQMN0tvjb3SAy1H/jJldKyjaxzYwHJ/ft4mkXly5oYwJDPkLEYK0oNZA8fj6g5SxRRZKqg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100601; c=relaxed/simple; bh=ab3K8FPCthYQkIyTJarkDDuRL7Mps2jwq7cT7gNX/SM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LO+dvC9ZIdw+fgzTsT8srsduEHjb/gBHxOXYcFqbT+SGgm4yU4NcbGM0QFka8kHEDaJjk1HPAotNK8uDYgqB/p83VPxXkMQ9I1NWtUNQtFEo6wGEJv2k60WooFqmG2knGqpaiNrvQD/6AWwmNcpSgd5H6vAn93pmPmp0mQVp+sQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cmtRaj4C; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cmtRaj4C" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2bf2bc4371bso5732445ad.1 for ; Fri, 29 May 2026 17:23:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100600; x=1780705400; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NMBxpjOSalzemU/ZQuYbqkgWo21WSyDLVg2ebWgM+bg=; b=cmtRaj4CIyFM6pNZ4vkYrMKhGEAKRTpiyJZ3yb8rYBdvxbeZb/rPdn/DdfMd/KxJcd NfnexYzZzLELvJO39WHFEHunP40OrPiSvD1VfRrgjlTbGhVxTriJTnxV3/p0Dotll8g/ cSGrie+PsFPFtUmdWxStW8rhp1Rtvng9ufd055Zf4WVYIfICqg2fy1SClSyUJLNH9x3K nJZ2dj3ChPyljeJ6kDKU9H48BeTt+kEegaoZlJ6/7iqAta1M8MX2pQAuHPrWCtxwk1/q 3GGNPx1gvumay1I8D8jG9gNgMcRsNub6up4DVkw0nbpUrLzq6lmtjb9NK8H2AvZZo6pH IEKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100600; x=1780705400; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NMBxpjOSalzemU/ZQuYbqkgWo21WSyDLVg2ebWgM+bg=; b=fdGeG8rJdPWVGIdRcQJh47OfA/GwseE4jt7ov1NTijijpRt7mtEkNAID2KNH+41v9t R2ZRYbAdKL2hMyxCcSP1pcEMoggF8CfZIv4fvbwvNDi/rmfRS+b/9OIG61HnshqGyZCp W3gobhC0U5L+dopqZeP7nvTvGkuDQiiE4Hb8f/Jrxea+xbCh9FgqhczXhyP6fZYw4mSK c8mbQpngWt5tHovOgn9swbjaQFBCHDmY/Xkon0DwFbnGZpp8rloNtO+B/BIlIvRAER6X KRYYGFNRmxWba5Tw+vKoQ81arE1ydmaGL19suz9uQyxxbe9vcN+nNyyys/Mde0vNFWzU lkaw== X-Forwarded-Encrypted: i=1; AFNElJ9JBXAjX8KbfDlW7m+W7yawSnJ9kONXCaI9UVm7mUbOHSllINvtIAPxyS0fPFp0HZR8Nwg9CjKLPtTGnDg=@vger.kernel.org X-Gm-Message-State: AOJu0Yw28JOGDUJy93BtYGD7hny55k0WVWRbXQ4ZKQ4JhhsEQu8izuNJ EGywWXmzHKG2xln5fdCU6UR00W0iBUQqBXHxptVZZ3kIklv4wPAvXzBhpClDnMNHk5w3//5jdzC 3e+lODpo3 X-Received: from pln12.prod.google.com ([2002:a17:903:360c:b0:2bd:5ced:c1c0]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c402:b0:2b0:6068:4c5f with SMTP id d9443c01a7336-2bf2048b983mr57943065ad.8.1780100599778; Fri, 29 May 2026 17:23:19 -0700 (PDT) Date: Sat, 30 May 2026 00:21:25 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-11-jrhilke@google.com> Subject: [PATCH v4 10/19] KVM: selftests: Verify interrupts are received after modifying IRQ routes in IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add the '-c' flag to tools/testing/selftests/kvm/irq_test.c to destroy and recreate KVM's GSI routing table between interrupts. This ensures that KVM correctly handles dynamic updates to the interrupt routing table while interrupts are actively being signaled by assigned devices. Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/irq_test.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c index 562b9ab187a4..72b9106216ed 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -114,13 +114,21 @@ static void kvm_route_msi(struct kvm_vm *vm, u32 gsi,= struct kvm_vcpu *vcpu, vm_ioctl(vm, KVM_SET_GSI_ROUTING, routes); } =20 +static void kvm_clear_gsi_routes(struct kvm_vm *vm) +{ + struct kvm_irq_routing routes =3D {}; + + vm_ioctl(vm, KVM_SET_GSI_ROUTING, &routes); +} + static void help(const char *name) { - printf("Usage: %s [-a] [-b] [-d ] [-h]\n", n= ame); + printf("Usage: %s [-a] [-b] [-c] [-d ] [-h]\= n", name); printf("\n"); printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n"); printf("-a Randomly affinitize the device's host IRQ to different physica= l CPUs throughout the test\n"); printf("-b Block vCPUs (e.g. HLT) instead of spinning in guest-mode\n"); + printf("-c Destroy and recreate KVM's GSI routing table in between some i= nterrupts\n"); printf("-d Use a VFIO device to send MSI-X interrupts instead of using an= emulated eventfd\n"); printf("\n"); exit(KSFT_FAIL); @@ -150,10 +158,11 @@ int main(int argc, char **argv) int nr_irqs =3D 1000, nr_vcpus =3D 1; const char *device_bdf =3D NULL; FILE *irq_affinity_fp =3D NULL; + bool clear_routes =3D false; struct iommu *iommu; struct kvm_vm *vm; =20 - while ((c =3D getopt(argc, argv, "abd:h")) !=3D -1) { + while ((c =3D getopt(argc, argv, "abcd:h")) !=3D -1) { switch (c) { case 'a': irq_affinity =3D true; @@ -164,6 +173,9 @@ int main(int argc, char **argv) case 'd': device_bdf =3D optarg; break; + case 'c': + clear_routes =3D true; + break; case 'h': default: help(argv[0]); @@ -210,9 +222,13 @@ int main(int argc, char **argv) } =20 for (i =3D 0; i < nr_irqs; i++) { + const bool do_clear_routes =3D clear_routes && (i & BIT(3)); struct kvm_vcpu *vcpu =3D vcpus[i % nr_vcpus]; struct timespec start; =20 + if (do_clear_routes) + kvm_clear_gsi_routes(vm); + kvm_route_msi(vm, gsi, vcpu, vector); =20 if (irq_affinity && vcpu->id =3D=3D 0) { --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B34A6284690 for ; Sat, 30 May 2026 00:23:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100603; cv=none; b=vFXF5Xvn0eZ1ndy9NA8Mb5OnrB/VlmlCKeY0IcsqKiQVqyZjxsCjlCfxz5aVEmZqkqv4vfEusdFQPGC0dQGabVuThGnTTTDqR+pTKfHgfceVC9YYu3ZCzcKw8mXmOvz6gzegt//TImfzOcXEDVfRswGhD8sHciweCIytspsm+D0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100603; c=relaxed/simple; bh=CzntHVhYNcZFHIR2nYlkFWCsxwV0/8XdJ0KvkXkba3k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jsdvpT8wKQmKjZlWVi9zFLRFIcRzNe8LCzeQdXWW0eSDTE/IyjM261uJXoF+9itbnlgFuaL43rk3fz9R+gh7U7EP5HZBxbUfbr1XMOaZCNIJjvtqGR77AYHmyX74LU6tpGCriRI4e+xspu0BB87oix8bX42VDRAPoPGI4/Tqxjk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RnntGQkL; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RnntGQkL" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2bd6aeb3637so320423165ad.2 for ; Fri, 29 May 2026 17:23:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100602; x=1780705402; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lUIxYrcL+WMnprn9yvfqEMt/1FqlOjkjwhMsyvVweJc=; b=RnntGQkLq5eXQpz8IFXzAb6GbgkhenJ4j4cW4/oCnDPbucOXIhOFr+Dj3dQFEScq0M mIYIcqrplQfs6n1i7zRiCPy35UecEnjTTN2VgIxPBBpCK5DaOQSZxdMWu/LQoAvaNXaw pWV4kbnTtFnW54HZbJxRLhx0Gv8xOxycFXgrQyfI/Hp3+U3ZknU9it71OaWZFB3g/MCr 6gNu/xbCnowkKvx3w9Yn4sdmm5/GgDODcITavoS6kK58y33SaiUu8UD8atMUfeyeVmJ3 K6/BluOU6aSQfWpVBaJrDQw2acbS8kkJe00K2t04bJjg0g19EqqRDQUApdp2MbfYt+Te Wt3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100602; x=1780705402; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lUIxYrcL+WMnprn9yvfqEMt/1FqlOjkjwhMsyvVweJc=; b=GxLVSwAW1rfCLMIyeiHkaIOlsJEO6K5i+1eOzVbDmXdN1RwqRdWn7+CPWuxoKJ4P5Z Kj3F6DnZ8zAVI+/eIRF7L4lzmzMkHSX36PtcXKcSHA2WY+gX/Hk1p2Cb8SYbPXXwK8IQ 0pRUqfU077vQ3lbM6XVrJ10DMqXuTM4CAHmE9gGNQdTICZsI4eScuRScT9E9e/K6IctU oPrf8J4sXccWPs4oYix7/xgZ33dt69IvY6d7ll8weBIjbsBe5to0zjSZeFJ0UsqPfmww TDmQzvJWJHybXT0whUAdlPrYvjlEpxjGAWqwRZAYBeiDnlrMv/a2qvoYBjtoQJ9qONd5 R1oQ== X-Forwarded-Encrypted: i=1; AFNElJ+wRVgH+nV71U/lmBCMSt2SuV3R/CFO37d5R1Q4LdVQsRAgHv0xuvn+0PIcqDr8TXMUxHVXg5ZJRud3fYo=@vger.kernel.org X-Gm-Message-State: AOJu0Ywh6gLQ992R3UzOMJjVQSRHiKbAb9vdHcGJez/NbMgT9poThiUS xtOXsV0GQ8lu4uaD64sILtJnuRVmeIOPuNBngz1JoAaNWkvT5QYgJA5BIUSGjHmq6WIeAoP6w29 oZM91wOOk X-Received: from plbks11.prod.google.com ([2002:a17:903:84b:b0:2bf:bf7:6205]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e543:b0:2bd:8395:fed8 with SMTP id d9443c01a7336-2bf36851283mr22803535ad.27.1780100601981; Fri, 29 May 2026 17:23:21 -0700 (PDT) Date: Sat, 30 May 2026 00:21:26 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-12-jrhilke@google.com> Subject: [PATCH v4 11/19] KVM: selftests: Make number of IRQs configurable in IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add a '-i' flag to tools/testing/selftests/kvm/irq_test.c to allow users to specify the number of IRQs generated during a test run. Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/irq_test.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c index 72b9106216ed..e79b66c050ff 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -123,13 +123,14 @@ static void kvm_clear_gsi_routes(struct kvm_vm *vm) =20 static void help(const char *name) { - printf("Usage: %s [-a] [-b] [-c] [-d ] [-h]\= n", name); + printf("Usage: %s [-a] [-b] [-c] [-d ] [-h] = [-i nr_irqs]\n", name); printf("\n"); printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n"); printf("-a Randomly affinitize the device's host IRQ to different physica= l CPUs throughout the test\n"); printf("-b Block vCPUs (e.g. HLT) instead of spinning in guest-mode\n"); printf("-c Destroy and recreate KVM's GSI routing table in between some i= nterrupts\n"); printf("-d Use a VFIO device to send MSI-X interrupts instead of using an= emulated eventfd\n"); + printf("-i The number of IRQs to generate during the test\n"); printf("\n"); exit(KSFT_FAIL); } @@ -162,7 +163,7 @@ int main(int argc, char **argv) struct iommu *iommu; struct kvm_vm *vm; =20 - while ((c =3D getopt(argc, argv, "abcd:h")) !=3D -1) { + while ((c =3D getopt(argc, argv, "abcd:hi:")) !=3D -1) { switch (c) { case 'a': irq_affinity =3D true; @@ -176,6 +177,9 @@ int main(int argc, char **argv) case 'c': clear_routes =3D true; break; + case 'i': + nr_irqs =3D atoi_positive("Number of IRQs", optarg); + break; case 'h': default: help(argv[0]); --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D04AE296BC8 for ; Sat, 30 May 2026 00:23:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100606; cv=none; b=KwIPtx62NTW7hPVFJOuzCOSogDPkkDxBgOnwpaLniObueGCQSbfREukh+T/u8mWeCrRT0yiDQC2YO8CSW4Ohfgy6YmqDyjQ9yDHp5v0tPHWZ2qHQ93irfxC+ZgvVCv3JSolclcCcWQ3WpjPLW9x4YsAZjw/cxPKBVd5UmLmD6jM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100606; c=relaxed/simple; bh=dO/3bjPHYsrbJg51RoK7QIlVFIqnI9KR0CvYXdHoVMw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fw4BgrFBvUVsrDLQulklNHAZOYJgCyzrNYl9BAVIXRcKScbmR9T8Z4iz9hq/icwDIgNeNqtxUaKdtK49+9gTts8EjFG9OG6NzXH89o7I0c9xkp2jlcDkAdBR5X7M2/GG5g5a4qEVhutX1zNUrRMtPyix048onJDWtdGGI0nYN8M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RYTky/6k; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RYTky/6k" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2bf160f7191so9986415ad.3 for ; Fri, 29 May 2026 17:23:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100604; x=1780705404; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=eXDt5fxo2AJ4ekO8u44BGMDqwbXC3Yl2wzUtkTH0I7Y=; b=RYTky/6kkOOV+Cw3GcLAM0hhBKPs2VSRHkuuz+G/sC6moLt5qIj07Luv5SBjPYmKtA kNOVaF2TgxNBw5V36dq11niaB5QPy/epQZmbIxu0csy6Giik0QaFN1Fy/gT3xaMztGDa /KO9TEFO+ff9xqeUvBS0DxAgox/5NpIpRsc11cQNsPwIXgpMGwn6n2C8UDBa2DMwHZiU JsWpLyqgdTdAxmnBuqIdo4Yntxj0UKwcqlNcNmw+a/XWwlMBtisnSdfXZt9iDaMHe/qR bkF4kPlE9ELkog+jFgRi7ZwpPlLC2DbliS/zxG64wtQExeM+CFFoggqpV60PUAVG4HLb 4GdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100604; x=1780705404; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eXDt5fxo2AJ4ekO8u44BGMDqwbXC3Yl2wzUtkTH0I7Y=; b=IJxGBYgJkTGOr/gYDaaha3ujYaYx5N5iEHrUoQxbFwp9bEe95JLwx6P4vy9vrfs1uV C/Nn4aKb+dKwgnjZiLQ2yn7npG5t9ezXcFesrHCKf7w0w2fCgoyWaWEVO8xrOv3+QEjo U5BHbAhHjAp9oooi/sxHdXuYl3ZQorbasnpl9Y9P76Ce9ACdLqT8R28kqzdUefrfJEzE hcWH2v51TpKFMNlyYV+/lMelBuBPKxpL97Un7cmAfriRY6RhFgqpxq/W143z/jMudfKO YhMmpRGTDBnS7aptLxD/0qS96phVLIp3DCxbiScuhL4fgrIF+1nZ82ynEpuDDdzWpUVL CDjw== X-Forwarded-Encrypted: i=1; AFNElJ8R1T3ReQy8hjJm9S6v1vKu/LyneovZCFtijiAPQg2VX/g3tl3GWItv7CLsa/Wi1F+eEfOVPISbWveBArk=@vger.kernel.org X-Gm-Message-State: AOJu0YzhxID9PKDWyXLEGKBXQTXruiG5YXcq2LUeLc67fiRbZCqXYKgj d8eIT8NNIcvNwIkL62t1OoCxrifkxB0ti4Jof3kD4eeNDQjV5aJw4ogC1D7H9ZBq6BkWU63r3LF 2J2Wc41nB X-Received: from plbmf13.prod.google.com ([2002:a17:902:fc8d:b0:2b2:425f:b4f1]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:cf01:b0:2c0:a959:8677 with SMTP id d9443c01a7336-2c0a95987dcmr485875ad.14.1780100604131; Fri, 29 May 2026 17:23:24 -0700 (PDT) Date: Sat, 30 May 2026 00:21:27 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-13-jrhilke@google.com> Subject: [PATCH v4 12/19] KVM: selftests: Verify non-postable IRQ remapping in IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add the -n flag to tools/testing/selftests/kvm/irq_test.c to route a portion of device interrupts as NMIs (Non-Maskable Interrupts) into the guest using an alternating pattern of 4 NMIs followed by 4 regular interrupts. While this adds coverage for NMI injection, the primary goal is to validate KVM's handling of non-postable interrupt delivery. Specifically, the transitions between posted and remapped modes. NMIs are used for this purpose because they are a reliable, architectural way to force these code paths. Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/irq_test.c | 45 ++++++++++++++++++++------ 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c index e79b66c050ff..79b0691c9889 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -17,12 +17,15 @@ static u64 timeout_ns =3D 2ULL * 1000 * 1000 * 1000; static bool guest_ready_for_irqs[KVM_MAX_VCPUS]; static bool guest_received_irq[KVM_MAX_VCPUS]; +static bool guest_received_nmi[KVM_MAX_VCPUS]; static bool irq_affinity; static bool block_vcpus; static bool done; =20 #define GUEST_RECEIVED_IRQ(__vcpu) \ SYNC_FROM_GUEST_AND_READ((__vcpu)->vm, guest_received_irq[(__vcpu)->id]) +#define GUEST_RECEIVED_NMI(__vcpu) \ + SYNC_FROM_GUEST_AND_READ((__vcpu)->vm, guest_received_nmi[(__vcpu)->id]) =20 static u32 guest_get_vcpu_id(void) { @@ -36,6 +39,11 @@ static void guest_irq_handler(struct ex_regs *regs) x2apic_write_reg(APIC_EOI, 0); } =20 +static void guest_nmi_handler(struct ex_regs *regs) +{ + WRITE_ONCE(guest_received_nmi[guest_get_vcpu_id()], true); +} + static void guest_code(void) { x2apic_enable(); @@ -96,7 +104,7 @@ static void trigger_interrupt(struct vfio_pci_device *de= vice, int eventfd) =20 =20 static void kvm_route_msi(struct kvm_vm *vm, u32 gsi, struct kvm_vcpu *vcp= u, - u8 vector) + u8 vector, bool use_nmi) { struct { struct kvm_irq_routing head; @@ -109,7 +117,7 @@ static void kvm_route_msi(struct kvm_vm *vm, u32 gsi, s= truct kvm_vcpu *vcpu, routes->entries[0].gsi =3D gsi; routes->entries[0].type =3D KVM_IRQ_ROUTING_MSI; routes->entries[0].u.msi.address_lo =3D 0xFEE00000 | (vcpu->id << 12); - routes->entries[0].u.msi.data =3D vector; + routes->entries[0].u.msi.data =3D use_nmi ? NMI_VECTOR | (4 << 8) : vecto= r; =20 vm_ioctl(vm, KVM_SET_GSI_ROUTING, routes); } @@ -123,7 +131,7 @@ static void kvm_clear_gsi_routes(struct kvm_vm *vm) =20 static void help(const char *name) { - printf("Usage: %s [-a] [-b] [-c] [-d ] [-h] = [-i nr_irqs]\n", name); + printf("Usage: %s [-a] [-b] [-c] [-d ] [-h] = [-i nr_irqs] [-n]\n", name); printf("\n"); printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n"); printf("-a Randomly affinitize the device's host IRQ to different physica= l CPUs throughout the test\n"); @@ -131,6 +139,7 @@ static void help(const char *name) printf("-c Destroy and recreate KVM's GSI routing table in between some i= nterrupts\n"); printf("-d Use a VFIO device to send MSI-X interrupts instead of using an= emulated eventfd\n"); printf("-i The number of IRQs to generate during the test\n"); + printf("-n Deliver 50 percent of IRQs as non-maskable interrupts\n"); printf("\n"); exit(KSFT_FAIL); } @@ -152,6 +161,7 @@ int main(int argc, char **argv) u32 gsi =3D kvm_random_u64_in_range(&kvm_rng, 24, KVM_MAX_IRQ_ROUTES - 1); u8 vector =3D kvm_random_u64_in_range(&kvm_rng, 32, UINT8_MAX); =20 + bool clear_routes =3D false, use_nmi =3D false; int i, j, c, msi, irq, eventfd, irq_cpu; struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; pthread_t vcpu_threads[KVM_MAX_VCPUS]; @@ -159,11 +169,10 @@ int main(int argc, char **argv) int nr_irqs =3D 1000, nr_vcpus =3D 1; const char *device_bdf =3D NULL; FILE *irq_affinity_fp =3D NULL; - bool clear_routes =3D false; struct iommu *iommu; struct kvm_vm *vm; =20 - while ((c =3D getopt(argc, argv, "abcd:hi:")) !=3D -1) { + while ((c =3D getopt(argc, argv, "abcd:hi:n")) !=3D -1) { switch (c) { case 'a': irq_affinity =3D true; @@ -180,6 +189,9 @@ int main(int argc, char **argv) case 'i': nr_irqs =3D atoi_positive("Number of IRQs", optarg); break; + case 'n': + use_nmi =3D true; + break; case 'h': default: help(argv[0]); @@ -190,6 +202,7 @@ int main(int argc, char **argv) =20 vm =3D vm_create_with_vcpus(nr_vcpus, guest_code, vcpus); vm_install_exception_handler(vm, vector, guest_irq_handler); + vm_install_exception_handler(vm, NMI_VECTOR, guest_nmi_handler); =20 if (device_bdf) { iommu =3D iommu_init(default_iommu_mode); @@ -227,34 +240,43 @@ int main(int argc, char **argv) =20 for (i =3D 0; i < nr_irqs; i++) { const bool do_clear_routes =3D clear_routes && (i & BIT(3)); + const bool do_use_nmi =3D use_nmi && (i & BIT(2)); struct kvm_vcpu *vcpu =3D vcpus[i % nr_vcpus]; struct timespec start; =20 if (do_clear_routes) kvm_clear_gsi_routes(vm); =20 - kvm_route_msi(vm, gsi, vcpu, vector); + kvm_route_msi(vm, gsi, vcpu, vector, do_use_nmi); =20 if (irq_affinity && vcpu->id =3D=3D 0) { irq_cpu =3D kvm_random_u64(&kvm_rng) % get_nprocs(); write_proc_irq_affinity(irq_affinity_fp, irq, irq_cpu); } =20 - for (j =3D 0; j < nr_vcpus; j++) + for (j =3D 0; j < nr_vcpus; j++) { TEST_ASSERT(!GUEST_RECEIVED_IRQ(vcpus[j]), "IRQ flag for vCPU %d not clear prior to test", vcpus[j]->id); + TEST_ASSERT(!GUEST_RECEIVED_NMI(vcpus[j]), + "NMI flag for vCPU %d not clear prior to test", + vcpus[j]->id); + } =20 trigger_interrupt(device, eventfd); =20 clock_gettime(CLOCK_MONOTONIC, &start); for (;;) { - if (GUEST_RECEIVED_IRQ(vcpu)) + if (!do_use_nmi && GUEST_RECEIVED_IRQ(vcpu)) + break; + + if (do_use_nmi && GUEST_RECEIVED_NMI(vcpu)) break; =20 if (timespec_to_ns(timespec_elapsed(start)) > timeout_ns) { printf("Timeout waiting for interrupt!\n"); printf(" vCPU: %d\n", vcpu->id); + printf(" is interrupt NMI: %s\n", do_use_nmi ? "true" : "false"); if (irq_affinity) printf(" irq_cpu: %d\n", irq_cpu); =20 @@ -263,7 +285,10 @@ int main(int argc, char **argv) } } =20 - WRITE_AND_SYNC_TO_GUEST(vm, guest_received_irq[vcpu->id], false); + if (do_use_nmi) + WRITE_AND_SYNC_TO_GUEST(vm, guest_received_nmi[vcpu->id], false); + else + WRITE_AND_SYNC_TO_GUEST(vm, guest_received_irq[vcpu->id], false); } =20 WRITE_AND_SYNC_TO_GUEST(vm, done, true); @@ -275,7 +300,7 @@ int main(int argc, char **argv) * below, will hang. */ if (block_vcpus) { - kvm_route_msi(vm, gsi, vcpus[i], vector); + kvm_route_msi(vm, gsi, vcpus[i], vector, false); trigger_interrupt(device, eventfd); } =20 --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1601E2BDC0E for ; Sat, 30 May 2026 00:23:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100608; cv=none; b=SPME0oSOdGNb00Con3mrjoZMf20El68V/09Hsa1PqzBlT5E+kXbUY05EZ6WOPBJTjE8tJ5/pt+A4zphGXYmC8GUbXMDIYhasTjvwS5Dng7A/FsiCK/UKehykr//TLkSJO8ojX2uExAxgZRNMHOqlAIMGlm/vJpjqESOCsLyrPNc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100608; c=relaxed/simple; bh=e2ukZCOaE1hVDKiG2x+yhZ3ADBfa0ap4M2fqLVx6QTw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eGN2VsFkSkXpAldfm9e7gkhmtYlus8JLJh45glEEznWy6YLMQHZZtYr6uL2oxpVDZyj01CvPe965l2Y7EqsSXyP4/0qyde4Sm6DWeGHO2GerYcpMPvaLmGICtt9CZA8Q5LkGJKtwLeF/SfchAngthIVQI4sbxa97BV3ystI621o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Cq42zjxZ; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Cq42zjxZ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2bf0d79d41eso16253845ad.1 for ; Fri, 29 May 2026 17:23:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100606; x=1780705406; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5EgP5OPxH9Bs2ZhJ3J58MhczjNuI2GKmzjzYwdFlh+w=; b=Cq42zjxZBLrIMHdSPUoDCUXUcXcjHO1jL+E0AoeiFV9shxBrCHwMlSdBd4ZGF9YZFG JGVst1Cvl7507oQuX9q8UZSefNx9bXQt0/FvBEHS/dcmMfpEwlvrHRcVgxS9qYEBLs31 0L5y1eIw9carfiR4BD6tQV9cmYAdXYnHrf2jO3SED6blQ4gifWhIBxs2yQOVVfrNcw7r Bezsn7ClTpEngeHgPuYTbLJ6eTEvFvGHSudP7XG4OB+fFIPHEVCCP56fYsrjHD9vWSWb utUyjuyn2G1zb+hqK5XKefZIS2Eczc+smcrYDp7UUuhhkpQY3PycoosK94SKa6hJICFG nfIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100606; x=1780705406; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5EgP5OPxH9Bs2ZhJ3J58MhczjNuI2GKmzjzYwdFlh+w=; b=QSdkNLnsn2S40+H+diUdSR+JGkx22ZRWqAnjB65Hal0JOj6PvvFWCOcpNe4tmuwxYJ JcRpAfd6q1TkDtt1xBLKWbednTNKIvZ3RtjACEZ27d1lzz4mfsbgoNEikIjAsms0OtJ1 WRRs5XSQc5sFtyaWwforLUCe6E9jt0uqkmOgG+1610SQ950MGFytDzrKIqca1oS5iU8I RV+J/dxl+O1JlNuddvYbpgIh2Rw8r4rs4MTrYX6TdAe5wI6Ily5q4DAYhauMAyZC+0M3 xQbiH6pafsLCnGZ5E0JakCPZQdEVd8MoNkIqCE1QibR672ODpQ+umcotxufynWVALRWd pOBQ== X-Forwarded-Encrypted: i=1; AFNElJ+6gxt+zmk/Jnh7Gj6LtWTKoRFnfyWc1Xs4RYOvmGOL38WuUb2k3nbQSP7UH7T+MvU9YhdO4bbkGSKwCC8=@vger.kernel.org X-Gm-Message-State: AOJu0Yz64QKxwMp1XsI5Uz6IwEvQIFsr3sU9JWNdxkjyiZ40C1cHIumN 3gcCX2ETXz1InYZbVsSanfiV+EE8kk30AYgfTuChDGXOgxNoA3m71ZRUKNEmVTWxNRl7d2w020A V2ZmCr/F3 X-Received: from pgh1.prod.google.com ([2002:a05:6a02:4e01:b0:c82:7761:9940]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2b0c:b0:2bf:800:1a00 with SMTP id d9443c01a7336-2bf3682a4a1mr25445155ad.22.1780100606193; Fri, 29 May 2026 17:23:26 -0700 (PDT) Date: Sat, 30 May 2026 00:21:28 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-14-jrhilke@google.com> Subject: [PATCH v4 13/19] KVM: selftests: Add kvm_gettid() wrapper and convert users From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a KVM wrapper for the gettid() syscall to avoid compilation errors in versions of Glibc that don't contain gettid(). Convert existing users of the syscall to use this new wrapper. This helper will be used in a future change in the KVM IRQ test to migrate vCPU threads. No functional changes intended. Suggested-by: Sean Christopherson Signed-off-by: Josh Hilke --- tools/testing/selftests/kvm/demand_paging_test.c | 2 +- tools/testing/selftests/kvm/include/kvm_syscalls.h | 5 +++++ tools/testing/selftests/kvm/lib/assert.c | 8 ++------ tools/testing/selftests/kvm/lib/test_util.c | 3 ++- tools/testing/selftests/kvm/rseq_test.c | 2 +- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/kvm/demand_paging_test.c b/tools/testi= ng/selftests/kvm/demand_paging_test.c index 0202b78f8680..4ebb2a9c3ba1 100644 --- a/tools/testing/selftests/kvm/demand_paging_test.c +++ b/tools/testing/selftests/kvm/demand_paging_test.c @@ -57,7 +57,7 @@ static void vcpu_worker(struct memstress_vcpu_args *vcpu_= args) static int handle_uffd_page_request(int uffd_mode, int uffd, struct uffd_msg *msg) { - pid_t tid =3D syscall(__NR_gettid); + pid_t tid =3D kvm_gettid(); uint64_t addr =3D msg->arg.pagefault.address; struct timespec start; struct timespec ts_diff; diff --git a/tools/testing/selftests/kvm/include/kvm_syscalls.h b/tools/tes= ting/selftests/kvm/include/kvm_syscalls.h index d4e613162bba..522ffff0462c 100644 --- a/tools/testing/selftests/kvm/include/kvm_syscalls.h +++ b/tools/testing/selftests/kvm/include/kvm_syscalls.h @@ -73,6 +73,11 @@ static inline int kvm_dup(int fd) return new_fd; } =20 +static inline pid_t kvm_gettid(void) +{ + return syscall(__NR_gettid); +} + __KVM_SYSCALL_DEFINE(munmap, 2, void *, mem, size_t, size); __KVM_SYSCALL_DEFINE(close, 1, int, fd); __KVM_SYSCALL_DEFINE(fallocate, 4, int, fd, int, mode, loff_t, offset, lof= f_t, len); diff --git a/tools/testing/selftests/kvm/lib/assert.c b/tools/testing/selft= ests/kvm/lib/assert.c index b49690658c60..7914046d334d 100644 --- a/tools/testing/selftests/kvm/lib/assert.c +++ b/tools/testing/selftests/kvm/lib/assert.c @@ -10,6 +10,7 @@ #include =20 #include "kselftest.h" +#include "kvm_syscalls.h" =20 /* Dumps the current stack trace to stderr. */ static void __attribute__((noinline)) test_dump_stack(void); @@ -58,11 +59,6 @@ static void test_dump_stack(void) #pragma GCC diagnostic pop } =20 -static pid_t _gettid(void) -{ - return syscall(SYS_gettid); -} - void __attribute__((noinline)) test_assert(bool exp, const char *exp_str, const char *file, unsigned int line, const char *fmt, ...) @@ -75,7 +71,7 @@ test_assert(bool exp, const char *exp_str, fprintf(stderr, "=3D=3D=3D=3D Test Assertion Failure =3D=3D=3D=3D\n" " %s:%u: %s\n" " pid=3D%d tid=3D%d errno=3D%d - %s\n", - file, line, exp_str, getpid(), _gettid(), + file, line, exp_str, getpid(), kvm_gettid(), errno, strerror(errno)); test_dump_stack(); if (fmt) { diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/se= lftests/kvm/lib/test_util.c index dc47ed9736e9..2006e46926d3 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -17,6 +17,7 @@ #include "linux/kernel.h" =20 #include "test_util.h" +#include "kvm_syscalls.h" =20 sigjmp_buf expect_sigbus_jmpbuf; =20 @@ -395,7 +396,7 @@ long get_run_delay(void) long val[2]; FILE *fp; =20 - sprintf(path, "/proc/%ld/schedstat", syscall(SYS_gettid)); + sprintf(path, "/proc/%ld/schedstat", (long)kvm_gettid()); fp =3D fopen(path, "r"); /* Return MIN_RUN_DELAY_NS upon failure just to be safe */ if (fscanf(fp, "%ld %ld ", &val[0], &val[1]) < 2) diff --git a/tools/testing/selftests/kvm/rseq_test.c b/tools/testing/selfte= sts/kvm/rseq_test.c index f80ad6b47d16..6510fbfd64f1 100644 --- a/tools/testing/selftests/kvm/rseq_test.c +++ b/tools/testing/selftests/kvm/rseq_test.c @@ -244,7 +244,7 @@ int main(int argc, char *argv[]) vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); =20 pthread_create(&migration_thread, NULL, migration_worker, - (void *)(unsigned long)syscall(SYS_gettid)); + (void *)(unsigned long)kvm_gettid()); =20 if (latency >=3D 0) { /* --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A053226863 for ; Sat, 30 May 2026 00:23:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100610; cv=none; b=pkKoAacBAB1xLHKAmGbM2U2kNzaZcB0X8UsCrZBmqh566c0C+1QLifA5BmTyi8oI4L1pdh78gorJlseHpdDfCSlaIDYhI3TVl8GC9ylWz0uy3ADQo99m5fHV5xdyBG7D3q3sDxxFlwjm8+CV8l+0IzQxzmRU3m/yilvEBinO+eQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100610; c=relaxed/simple; bh=sFiyZUNppJvupkfg1Y319YjkFu4NJ8Q99u8ITtvADpU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nfB7nXyLl4MH+hvNeoUtgd8eA7YmHnQXyzDwmE+AuIuhv309jOu/BBGLPyUFekk7gSkM2rj4IGTPN3Y2wD19xb0HO+womBg8gGKMjALg9ngCqN4Cugb3k18cQnwNu9/8y/KgEpoW1iqRqbSHDvVJvB3U+c0aTFOIcxxphW8S+Sw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nPAvSAEb; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nPAvSAEb" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-36bc380fbf9so1224037a91.3 for ; Fri, 29 May 2026 17:23:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100608; x=1780705408; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mwK+oHR7LpIedlIX8UM2oEn2HzPBvHfoG9vzzz2iSsE=; b=nPAvSAEbvcPpwZLBDejct5C6C86koASgOyei0URdWw8+UG/iG63QYyvsWr67erVLjd To8bctGxZYm3R1oGMDH6ltq6U+Fj3qbxEHtzfpNxlsuIji5HjLAlJsuxQ63bWkzhzATH OUoh+xAz0/SsL79ZVL8Rd6j3zco1kuiTGHNSf6Gp49lAatoMMq1/6Na6/h++O+giSGq8 ilODIqvY8QRy8hBnLgJ4D/ktqY1Hqj+zv95Mr4AmreovO1rJHDSGtBQtfVDj/LkW/QQT djahjRVD4XqQ2gFsNYA06c1r+iE6OW/F4dy+WHdj3zaqk2kBa7yDwc7nqAA46IIpbXGE hNfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100608; x=1780705408; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mwK+oHR7LpIedlIX8UM2oEn2HzPBvHfoG9vzzz2iSsE=; b=suYw9IPe2FCxfkVCW4HrfVgLHWeKsALrDeV+WzZ9ZB6Fe9kHvYmaIhdw9Rhxpd9ghd 60W2JiQ+S1UZ40jp33l6Q/sTfznJAoQ8gWk9Io9xJYCT0AAIHpbRhsJT5gh4BOZVn0TS pzcppCSrsKn5AeLZ2t9g/tYwcU5kZZ03JQTyw/k3TmkcoJ/GiezUqh59fIjA0tFeDAgG I0yxGxP+AcuEgQoMx+pDBJhmYVfN3rO4kV9e8kGoE+wmQHdLS9CT9L28cXAVxMzJ0Z61 bZl91fo9LCPevybzdm/UGkVhmMAzdNKd8AgyzLlgLPdD6/7sHZOUk/tur+82juVj34XV unhA== X-Forwarded-Encrypted: i=1; AFNElJ8m9536nEAyrMjC0k/FYYasImWznNF+pvSzLBIWaK0ypTCEEcdprBODUdS1bc3uz8HB9J7xt9794AI6we0=@vger.kernel.org X-Gm-Message-State: AOJu0YzHfbTW1IRuzYp1pW9dUZeXn21ZqpV1vr70LJVuxxqI8XOHWqXp st/3ShxCBoSbMNxy/my4rCqLr2pWUYToXWM3TfgAIF/0iB+bIPoC8AKvUuas9dr6Bd2CDUh+6o8 V0jjxNfQE X-Received: from pgbdr23.prod.google.com ([2002:a05:6a02:fd7:b0:c82:35ce:8939]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:c6ca:b0:3b2:86c9:baa5 with SMTP id adf61e73a8af0-3b427f59c8fmr1574170637.38.1780100608073; Fri, 29 May 2026 17:23:28 -0700 (PDT) Date: Sat, 30 May 2026 00:21:29 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-15-jrhilke@google.com> Subject: [PATCH v4 14/19] KVM: selftests: Add kvm_sched_getaffinity() wrapper and convert users From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a KVM wrapper for the sched_getaffinity syscall named kvm_sched_getaffinity(). Convert existing users of sched_getaffinity to use the new wrapper. This helper will be used in a future change in the KVM IRQ test to migrate vCPU threads. No functional changes intended. Suggested-by: Sean Christopherson Signed-off-by: Josh Hilke --- tools/testing/selftests/kvm/arch_timer.c | 2 +- tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c | 2 +- tools/testing/selftests/kvm/include/kvm_syscalls.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 5 ++--- tools/testing/selftests/kvm/mmu_stress_test.c | 6 +----- tools/testing/selftests/kvm/rseq_test.c | 4 +--- 6 files changed, 7 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/kvm/arch_timer.c b/tools/testing/selft= ests/kvm/arch_timer.c index cf8fb67104f1..e4289800f432 100644 --- a/tools/testing/selftests/kvm/arch_timer.c +++ b/tools/testing/selftests/kvm/arch_timer.c @@ -85,7 +85,7 @@ static uint32_t test_get_pcpu(void) cpu_set_t online_cpuset; =20 nproc_conf =3D get_nprocs_conf(); - sched_getaffinity(0, sizeof(cpu_set_t), &online_cpuset); + kvm_sched_getaffinity(0, sizeof(cpu_set_t), &online_cpuset); =20 /* Randomly find an available pCPU to place a vCPU on */ do { diff --git a/tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c b/to= ols/testing/selftests/kvm/arm64/arch_timer_edge_cases.c index 993c9e38e729..c7fd601566c1 100644 --- a/tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c +++ b/tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c @@ -1040,7 +1040,7 @@ int main(int argc, char *argv[]) if (!parse_args(argc, argv)) exit(KSFT_SKIP); =20 - sched_getaffinity(0, sizeof(default_cpuset), &default_cpuset); + kvm_sched_getaffinity(0, sizeof(default_cpuset), &default_cpuset); set_counter_defaults(); =20 if (test_args.test_virtual) { diff --git a/tools/testing/selftests/kvm/include/kvm_syscalls.h b/tools/tes= ting/selftests/kvm/include/kvm_syscalls.h index 522ffff0462c..2e362fda50af 100644 --- a/tools/testing/selftests/kvm/include/kvm_syscalls.h +++ b/tools/testing/selftests/kvm/include/kvm_syscalls.h @@ -82,5 +82,6 @@ __KVM_SYSCALL_DEFINE(munmap, 2, void *, mem, size_t, size= ); __KVM_SYSCALL_DEFINE(close, 1, int, fd); __KVM_SYSCALL_DEFINE(fallocate, 4, int, fd, int, mode, loff_t, offset, lof= f_t, len); __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length); +__KVM_SYSCALL_DEFINE(sched_getaffinity, 3, pid_t, pid, size_t, cpusetsize,= cpu_set_t *, mask); =20 #endif /* SELFTEST_KVM_SYSCALLS_H */ diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/sel= ftests/kvm/lib/kvm_util.c index b247b2015b2a..ebd04cf1847b 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -668,13 +668,12 @@ void kvm_parse_vcpu_pinning(const char *pcpus_string,= uint32_t vcpu_to_pcpu[], cpu_set_t allowed_mask; char *cpu, *cpu_list; char delim[2] =3D ","; - int i, r; + int i; =20 cpu_list =3D strdup(pcpus_string); TEST_ASSERT(cpu_list, "strdup() allocation failed."); =20 - r =3D sched_getaffinity(0, sizeof(allowed_mask), &allowed_mask); - TEST_ASSERT(!r, "sched_getaffinity() failed"); + kvm_sched_getaffinity(0, sizeof(allowed_mask), &allowed_mask); =20 cpu =3D strtok(cpu_list, delim); =20 diff --git a/tools/testing/selftests/kvm/mmu_stress_test.c b/tools/testing/= selftests/kvm/mmu_stress_test.c index a9fac4bc02c2..98546edbf11b 100644 --- a/tools/testing/selftests/kvm/mmu_stress_test.c +++ b/tools/testing/selftests/kvm/mmu_stress_test.c @@ -255,11 +255,7 @@ static void rendezvous_with_vcpus(struct timespec *tim= e, const char *name) static void calc_default_nr_vcpus(void) { cpu_set_t possible_mask; - int r; - - r =3D sched_getaffinity(0, sizeof(possible_mask), &possible_mask); - TEST_ASSERT(!r, "sched_getaffinity failed, errno =3D %d (%s)", - errno, strerror(errno)); + kvm_sched_getaffinity(0, sizeof(possible_mask), &possible_mask); =20 nr_vcpus =3D CPU_COUNT(&possible_mask); TEST_ASSERT(nr_vcpus > 0, "Uh, no CPUs?"); diff --git a/tools/testing/selftests/kvm/rseq_test.c b/tools/testing/selfte= sts/kvm/rseq_test.c index 6510fbfd64f1..557e393c223b 100644 --- a/tools/testing/selftests/kvm/rseq_test.c +++ b/tools/testing/selftests/kvm/rseq_test.c @@ -226,9 +226,7 @@ int main(int argc, char *argv[]) } } =20 - r =3D sched_getaffinity(0, sizeof(possible_mask), &possible_mask); - TEST_ASSERT(!r, "sched_getaffinity failed, errno =3D %d (%s)", errno, - strerror(errno)); + kvm_sched_getaffinity(0, sizeof(possible_mask), &possible_mask); =20 calc_min_max_cpu(); =20 --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 611292D7DC8 for ; Sat, 30 May 2026 00:23:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100613; cv=none; b=o1XEdvVlI+0FbZdJ6xgJ2RwcP9vON2jWgS/1B6dQf/EEx5+pDPjpPy+/P5rCnZAvymm4j15Lmzq2hll7HQIBXMhiDsi766tK6WCxYD/AR6TzeO6xt+w+k9zFmLQQXlk33+I7f84LiNb1PyfWhKa68GrgxMPbN831V7Jfqrk7hrQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100613; c=relaxed/simple; bh=TcFrdCIU/eLdRB40OcXqQhVamDIbW7fFaM4iPioZupY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DjzHxsgcAFkOFeL9krWe8PA0cU59iDiWWRBb+RINI+a+YgdyecrvkmQu/cLRr1ym9360Cv+aLWfuEbUdKwCLjuvQ1PMUVu+ytuse/nS2Uh7L9fFWt6TEpTWkYQ+IIwJBncSexDrgYS6o7OVBeAoGv+9MvuSRy+4GjYf++JQs+6A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tIjmMlKN; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tIjmMlKN" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-36bc380fbf9so1224062a91.3 for ; Fri, 29 May 2026 17:23:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100611; x=1780705411; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8AMZSjjAVMXNbasa4YAYk1Hpbm/K3EtJ8vDdHI7VRcM=; b=tIjmMlKNq3NmS77KJytAe9mUuY4VgL7wptH2+U57lBo2lpiYLxm9sE3jAYz1bVr+RM ZqhTcrRGKK0R4rf4Fof5w4kd3dgMM15PSQguNMYfWlptgwg7v+/qNymSFjy4z23dBxUB 1mir1Uzi4tY6aGoBio7ujKiTqi0E6i+hvL1hMQcVg8r/oJnAN7TDjZxPOEiU0dleyRuN 4gqSlUMSCykRf/xpdBW0rAKQ43eroVNIwIx/vyIWeOhCZLsYq158MYfZmxMgMEWsTmqf eSjgobNP5kzL9E4rP3kji32GnfHjVR0aAV6k90+rs2ir8VCcDLTAE5Po5yoHk8C5IC9C FOzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100611; x=1780705411; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8AMZSjjAVMXNbasa4YAYk1Hpbm/K3EtJ8vDdHI7VRcM=; b=o38JO0SUdd8weqNvVae0U7K+2iFgcv8xg9NegONgsaSBk8WD3IzyXQip3wTGDxqw3w LY9WKWSfipEnAWryJVkb8//dCxx64Fh8rF9pONHkV6DQAFo9Q0Z4BX613dWsT1MXhqae /Ljp7PF/uviND49DBbsTakCKLpePRckn7KZ4Ga7nt9hh8q+bJiGLRywd2lHQtcfAd8vd DNupbs37M69og0ATy/py9385rGLWOIyQUJXGXJNa88KyTpp8W9Ykk62k68m9YoVchYxs J/Zt5bt8QWGNljOdBVlWKdpMvUpKHTnVyG8GtFCs/dY5l50yUIIz0c0CIWzObsfX1zj0 O6sA== X-Forwarded-Encrypted: i=1; AFNElJ//wGFkOw7/4jbb/tbkv5h+2hmE25yF6g/UBA5xnDfjbupC4D8VzETuSvVWWYRsMcsTEK1p01EjiYLYIQk=@vger.kernel.org X-Gm-Message-State: AOJu0YyPLMP0dv3W1xHjhHfpdRmP3dpg8lRnzcFXG1++TG/jQHd30pJA mkkdtuUcWdDVCtnlPrNAJuJxvppS7rsa8OQiOsDjfVp22yZeJUeVarfhmiIDdla9CXqX04C4z34 I9h+3pwbx X-Received: from pgu22.prod.google.com ([2002:a63:1456:0:b0:c79:67fe:6d9e]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:cc9a:b0:398:aea8:a9c0 with SMTP id adf61e73a8af0-3b427c690a2mr1614367637.19.1780100611348; Fri, 29 May 2026 17:23:31 -0700 (PDT) Date: Sat, 30 May 2026 00:21:30 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-16-jrhilke@google.com> Subject: [PATCH v4 15/19] KVM: selftests: Add pin_task_to_random_cpu() helper function From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper function pin_task_to_random_cpu() to randomly select a CPU from a given cpu_set_t and pin the specified task to it. This helper will be used in a future change in the KVM IRQ test to migrate vCPU threads. Suggested-by: Sean Christopherson Signed-off-by: Josh Hilke --- .../testing/selftests/kvm/include/kvm_util.h | 2 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing= /selftests/kvm/include/kvm_util.h index 499e695f1fc4..f725ab0ed4f9 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -1096,6 +1096,8 @@ static inline void pin_task_to_cpu(pthread_t task, in= t cpu) TEST_ASSERT(!r, "Failed to set thread affinity to pCPU '%u'", cpu); } =20 +void pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus); + static inline int pin_task_to_any_cpu(pthread_t task) { int cpu =3D sched_getcpu(); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/sel= ftests/kvm/lib/kvm_util.c index ebd04cf1847b..29cd649a54d5 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -662,6 +662,27 @@ void kvm_print_vcpu_pinning_help(void) " (default: no pinning)\n", name, name); } =20 +void pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus) +{ + int target_idx; + int nr_cpus; + int cpu; + + nr_cpus =3D CPU_COUNT(possible_cpus); + TEST_ASSERT(nr_cpus > 0, "No CPUs available in possible_cpus"); + + target_idx =3D kvm_random_u64(&kvm_rng) % nr_cpus; + + for (cpu =3D 0; cpu < CPU_SETSIZE; cpu++) { + if (CPU_ISSET(cpu, possible_cpus) && target_idx-- =3D=3D 0) { + pin_task_to_cpu(task, cpu); + return; + } + } + + TEST_FAIL("Failed to find random CPU in possible_cpus"); +} + void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcp= u[], int nr_vcpus) { --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 305702D97B7 for ; Sat, 30 May 2026 00:23:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100615; cv=none; b=u+kh3KqRPBvsBGHihXadpfaMJgVjeipESzdqaY2WOfbehyDFTpJ94BgGhQ6Oul8UsB5QpahjFEICLwwkIasx0kQmu5f10gPtp1Zfc3r0AU6pxIIneMyqFm72RuvmIPxy/F3QwtvtIeS1KV1Iu3ZYD/KyBcUr6Wy52HuwQZsfsD4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100615; c=relaxed/simple; bh=kE8XjfOxzJs8azoyHN62Q77wqmIgre+C9k/tz5VsTwM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uf8BlskzQV/cbBfOWvHDIVM8E1psF7SnfY8rFy79W+TLchEaNGFary4hGwqtvh6m7CPu0WfmPY4HZyro2RYT/R+XXOs81lZGGlKozTl33RSEx61NgUZslAsL1C6ChdYV4HROhKXP08CxzUShdLasbW+j31VgXpV49cqXz9u0S3Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=f2Zk12P/; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="f2Zk12P/" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c854c4b740cso2505971a12.1 for ; Fri, 29 May 2026 17:23:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100613; x=1780705413; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qm1HsuW4vpwjSUMEY8SE1UJyBnCa5qMSZzhyXJG3FSY=; b=f2Zk12P/XxKZHX4jJlPBVVPvhVPqCEBEG2BgZaS5fQS4t5ZRclCDKufjrCYPJj7C47 wUtw/h3rF61pQTJnr6Oy5Epyh0LeyPVcCYXkIju9ncvKf7rC2OW/l3hnHp/TkCVO+tdp yQckwm2+gdH6sp+rmBloAhEVT/aWjMzU5pypwLasP3O0QKZB/IBxav2PoKkj35JqqBY2 wzerU9bqJSJuhfTS3CENkg/VBwN8gUy+GujjJYlq1+7rpfgHnr/IhMmUXLgK7L94o8Oi sexpxNP1vIYf+otOnMMGNNlEepVLJaLmL317up+0rFS0+fY5cA5jVLrGFu2AZib8LN5q YWig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100613; x=1780705413; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qm1HsuW4vpwjSUMEY8SE1UJyBnCa5qMSZzhyXJG3FSY=; b=QpG61v30mWsbIH+2CEz/6N0ot9/UG20FYJ4FwBKLkNbsztmBIAe41aWHR685+OEaNi gmVGMVJQFuQbO7EtLOIEIGUUDpeaezVGvbgm5kgeRlJnrXgGKbX3FMEwj6MYYklbwN/Y IrZxkRzk4V6ZEpIM8N+NAWqq1wDXyRbvQlei86Dqk2WP+hKtRmE3opL3PxjdG4x1owkx 6cO4j67qIApl3WhgnuhpNUMu1WAy89aDCpfkEcF1WQwLniidaEfTBqkZZMGrRpLK1QHb yrxCrXdm0MRGA9eeLaT0hX+3hlkNDFMI7nsVsCEQmveQvCoicO817Ml43TeEUfFRQ15v jFOQ== X-Forwarded-Encrypted: i=1; AFNElJ+UJblFvBUaaa66HrY/q74wjdK+Y1nhNLWWutHUv9xc65gz2ziSxfWevdZIhbbNqD8t8KRpaOE5eIQKutU=@vger.kernel.org X-Gm-Message-State: AOJu0YxS9YvaC+eW9vA4yLH0i6/LqYpWwoYtvvywHmx4U0jO4/lUBRJw znOm20vDUS8M27HH20vgMS8HM9xdSqSFRWQ7frvKo96BRhITMJ5Yjgocga6S3U2xXXiXiC7qAiW xA5dL1q44 X-Received: from pgbbc23.prod.google.com ([2002:a65:6d97:0:b0:c79:85be:4c5c]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:d80c:b0:39c:4b84:d90f with SMTP id adf61e73a8af0-3b427e7b3d7mr1764882637.8.1780100613386; Fri, 29 May 2026 17:23:33 -0700 (PDT) Date: Sat, 30 May 2026 00:21:31 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-17-jrhilke@google.com> Subject: [PATCH v4 16/19] KVM: selftests: Verify vCPU migration during IRQ delivery in IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add the '-m' flag to tools/testing/selftests/kvm/irq_test.c to migrate vCPU threads across random physical CPUs during the test. This validates KVM's ability to handle vCPUs changing physical CPUs while interrupts are actively being injected. Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/irq_test.c | 34 ++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c index 79b0691c9889..f653317022e3 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -18,6 +18,8 @@ static u64 timeout_ns =3D 2ULL * 1000 * 1000 * 1000; static bool guest_ready_for_irqs[KVM_MAX_VCPUS]; static bool guest_received_irq[KVM_MAX_VCPUS]; static bool guest_received_nmi[KVM_MAX_VCPUS]; +static pid_t vcpu_tids[KVM_MAX_VCPUS]; +static bool migrate_vcpus; static bool irq_affinity; static bool block_vcpus; static bool done; @@ -67,12 +69,23 @@ static void *vcpu_thread_main(void *arg) struct kvm_vcpu *vcpu =3D arg; struct ucall uc; =20 + WRITE_ONCE(vcpu_tids[vcpu->id], kvm_gettid()); + vcpu_run(vcpu); TEST_ASSERT_EQ(UCALL_DONE, get_ucall(vcpu, &uc)); =20 return NULL; } =20 +static void migrate_vcpu_threads(int nr_vcpus, pthread_t *vcpu_threads, + cpu_set_t *available_cpus) +{ + int i; + + for (i =3D 0; i < nr_vcpus; i++) + pin_task_to_random_cpu(vcpu_threads[i], available_cpus); +} + static int vfio_setup_msi(struct vfio_pci_device *device) { const int flags =3D MAP_SHARED | MAP_ANONYMOUS; @@ -131,7 +144,7 @@ static void kvm_clear_gsi_routes(struct kvm_vm *vm) =20 static void help(const char *name) { - printf("Usage: %s [-a] [-b] [-c] [-d ] [-h] = [-i nr_irqs] [-n]\n", name); + printf("Usage: %s [-a] [-b] [-c] [-d ] [-h] = [-i nr_irqs] [-m] [-n]\n", name); printf("\n"); printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n"); printf("-a Randomly affinitize the device's host IRQ to different physica= l CPUs throughout the test\n"); @@ -139,6 +152,7 @@ static void help(const char *name) printf("-c Destroy and recreate KVM's GSI routing table in between some i= nterrupts\n"); printf("-d Use a VFIO device to send MSI-X interrupts instead of using an= emulated eventfd\n"); printf("-i The number of IRQs to generate during the test\n"); + printf("-m Pin vCPU threads to random physical CPUs throughout the test\n= "); printf("-n Deliver 50 percent of IRQs as non-maskable interrupts\n"); printf("\n"); exit(KSFT_FAIL); @@ -171,8 +185,9 @@ int main(int argc, char **argv) FILE *irq_affinity_fp =3D NULL; struct iommu *iommu; struct kvm_vm *vm; + cpu_set_t available_cpus; =20 - while ((c =3D getopt(argc, argv, "abcd:hi:n")) !=3D -1) { + while ((c =3D getopt(argc, argv, "abcd:hi:mn")) !=3D -1) { switch (c) { case 'a': irq_affinity =3D true; @@ -189,6 +204,9 @@ int main(int argc, char **argv) case 'i': nr_irqs =3D atoi_positive("Number of IRQs", optarg); break; + case 'm': + migrate_vcpus =3D true; + break; case 'n': use_nmi =3D true; break; @@ -238,6 +256,15 @@ int main(int argc, char **argv) continue; } =20 + if (migrate_vcpus) { + kvm_sched_getaffinity(vcpu_tids[0], sizeof(available_cpus), &available_c= pus); + + if (nr_vcpus > CPU_COUNT(&available_cpus)) { + printf("There are more vCPUs than pCPUs; refusing to migrate.\n"); + migrate_vcpus =3D false; + } + } + for (i =3D 0; i < nr_irqs; i++) { const bool do_clear_routes =3D clear_routes && (i & BIT(3)); const bool do_use_nmi =3D use_nmi && (i & BIT(2)); @@ -254,6 +281,9 @@ int main(int argc, char **argv) write_proc_irq_affinity(irq_affinity_fp, irq, irq_cpu); } =20 + if (migrate_vcpus && vcpu->id =3D=3D 0) + migrate_vcpu_threads(nr_vcpus, vcpu_threads, &available_cpus); + for (j =3D 0; j < nr_vcpus; j++) { TEST_ASSERT(!GUEST_RECEIVED_IRQ(vcpus[j]), "IRQ flag for vCPU %d not clear prior to test", --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 201272E2286 for ; Sat, 30 May 2026 00:23:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100617; cv=none; b=Xp1FweyaB2jObyT4+CEZ6zA1v3H+0qwcRMxXvi9UivgU+bAEyH5tjFWAFo4+P9ko2jriubhW/ORIZQZc2h2oc7rUehmndZ+MmfcGWw7OVE3eKvYpLEo+c1H2IeFhCdgSbc3sjHt285XupfEIWlmsV3bgFb0HuGpx1liVeUvsGic= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100617; c=relaxed/simple; bh=kHpMtpup6pJQOyEsscJ6R53ahK3JLy7NK5P7+EpKqXY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sfaQ/SVzZCUEudkmlPoDWKalCLbRGI3+juZqNhBGM0wKkooh1dC7cOtEtaFqlEHPng/VTef/MW5TL62XViF4izAzFBX6q0D3GjW5y43HFLbMDJ/61Gr2Y15nERVFpQdnOXAweSm6TdgkyRkK+/IpAJF9/n421d8YEgErpfCQz4s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Y0mzyBsT; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Y0mzyBsT" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-8422a0eee68so256783b3a.0 for ; Fri, 29 May 2026 17:23:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100615; x=1780705415; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8JVFFS3ixiomAORGObVWJ+yUD++/pWXNo86z/Bpd9hk=; b=Y0mzyBsTKCqm2MQaycJ2SCmWY8HZL+GDIPo2i8Kj7EeMZ8FNb5QoC1oo++0fjB5QJc JZyyYqCUSWdlGKh+DfCh/1Fc5AGbWJjKBnGIRkM9G/YGKRVCHnZeNChDyDN/xuWvGCqi 39MDNFauLNhBhQko7EpI++U803dW0iabLyU6bUmE8VCJBeGXUbWGKZFLpD2cfFzb1PA3 79IZH7eWX2OqwGvU/DwziLK4QYpM9MBJOkVVanhfV5D/sbIb13aT1beLsHliDwX+g/n0 LwrUEkkinWuh5uNSIULfu7MwaFK+3qT0GGKllD8+sioxnPdYm6FxGLGcUBDsmRVcC9OF hKGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100615; x=1780705415; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8JVFFS3ixiomAORGObVWJ+yUD++/pWXNo86z/Bpd9hk=; b=eiFOZpTvtnmcHlJAK2edWE83MNSQVDRZSBFvHNLprkfdxIz6SXKdsd59EvQJQpZcP5 3mMUrIuSeORSDwtaEon+B5nsB7uUeTJqKXSjsGUo6iH//TiDyp6KwYsGrjBU971FGH/N ZaB1zK71JjsF0BfFjo5BFV8d2pHGHw52Tfs6JNmQRrm07ua+H8HP3+oMt0GcGH7J/wnF p8XZnjLDDPz3qzXyY6kGgeYjQXn6bnhKO6zzyT6J4TxOIPRVU+xFIApzePeds30CC9cL roFA3ZYC9iMok8lv3xQ1TDy9pb7VbszM94szF36jTVM1/itx6yRS+nT91x8gBKowA5Ya lQ9w== X-Forwarded-Encrypted: i=1; AFNElJ98mNrb0emSHznsVEhFMsKifiD996+UvYXXIUMKWj/hPmG1ioxnkc/F5YkpunsnHhho8NIIah3tgLCRpF4=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1jdxhRlUWTjzl6Wf23z/uGAEnzV4v8kDnFWFRlMaWF+NHf6Sw 3MQR8TmhWhJKZaqHiqcvHCRWjGF9IxwohcEIc9Fj/pn+k9x2LcNFf72NjxCmzwq2quE5viUM0gJ TE5JmTHN3 X-Received: from pgbcs9.prod.google.com ([2002:a05:6a02:4189:b0:c79:3224:837d]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:e58d:b0:3b3:be1b:733e with SMTP id adf61e73a8af0-3b4280a4e36mr1599545637.23.1780100615185; Fri, 29 May 2026 17:23:35 -0700 (PDT) Date: Sat, 30 May 2026 00:21:32 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-18-jrhilke@google.com> Subject: [PATCH v4 17/19] KVM: selftests: Print vCPU affinity on timeout during IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add kvm_print_vcpu_affinity() to lib/kvm_util.c to print the physical CPU affinity of a vCPU thread in a human-readable format. Use this new helper in tools/testing/selftests/kvm/irq_test.c to print the vCPU's affinity if it times out waiting for an interrupt while vCPU migration (-m) is enabled. This provides valuable context for debugging IRQ delivery timeouts when vCPUs are being actively migrated across physical CPUs. Signed-off-by: Josh Hilke --- .../testing/selftests/kvm/include/kvm_util.h | 1 + tools/testing/selftests/kvm/irq_test.c | 2 + tools/testing/selftests/kvm/lib/kvm_util.c | 49 +++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing= /selftests/kvm/include/kvm_util.h index f725ab0ed4f9..0ec82088bc43 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -1119,6 +1119,7 @@ static inline int pin_self_to_any_cpu(void) void kvm_print_vcpu_pinning_help(void); void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcp= u[], int nr_vcpus); +void kvm_print_vcpu_affinity(struct kvm_vcpu *vcpu, pid_t tid); =20 unsigned long vm_compute_max_gfn(struct kvm_vm *vm); unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size); diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c index f653317022e3..0db4ea4eb1ef 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -309,6 +309,8 @@ int main(int argc, char **argv) printf(" is interrupt NMI: %s\n", do_use_nmi ? "true" : "false"); if (irq_affinity) printf(" irq_cpu: %d\n", irq_cpu); + if (migrate_vcpus) + kvm_print_vcpu_affinity(vcpu, vcpu_tids[vcpu->id]); =20 TEST_FAIL("vCPU %d timed out waiting for IRQ from GSI %d (Vector 0x%x)= !\n", vcpu->id, gsi, vector); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/sel= ftests/kvm/lib/kvm_util.c index 29cd649a54d5..b2e534f331e2 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include =20 @@ -662,6 +663,54 @@ void kvm_print_vcpu_pinning_help(void) " (default: no pinning)\n", name, name); } =20 +void kvm_print_vcpu_affinity(struct kvm_vcpu *vcpu, pid_t tid) +{ + int nprocs =3D get_nprocs(); + bool first_range =3D true; + int i, start =3D -1; + cpu_set_t cpus; + + kvm_sched_getaffinity(tid, sizeof(cpus), &cpus); + + /* + * Output format examples: + * - Single CPU: "vCPU 0 (TID 1234) affined to pCPU(s): 2" + * - List: "vCPU 0 (TID 1234) affined to pCPU(s): 0,2,4" + * - Range: "vCPU 0 (TID 1234) affined to pCPU(s): 0-7" + * - Mixed: "vCPU 0 (TID 1234) affined to pCPU(s): 0-3,8,10-12" + */ + printf("vCPU %u (TID %d) affined to pCPU(s): ", vcpu->id, tid); + + for (i =3D 0; i <=3D nprocs; i++) { + /* + * Iterate to nprocs (inclusive) to ensure that if the last pCPU + * is part of a range, the 'else' block triggers one final time + * to flush that range to stdout. + */ + if (i < nprocs && CPU_ISSET(i, &cpus)) { + if (start =3D=3D -1) + start =3D i; + continue; + } + + if (start !=3D -1) { + int end =3D i - 1; + + if (!first_range) + printf(","); + + if (start =3D=3D end) + printf("%d", start); + else + printf("%d-%d", start, end); + + start =3D -1; + first_range =3D false; + } + } + printf("\n"); +} + void pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus) { int target_idx; --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FEF8238C0A for ; Sat, 30 May 2026 00:23:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100619; cv=none; b=QPEKYQ7hneWrIKh2HeA/mASYCC+rGx8oevwFORWT5WILWrfP2CgxbZkwHp97RFh87EEi0liOQxcyNktuYvi0saf/NV/dhw3lKd8GyKJNRos9r+XQX13RtCPUKt/bt4jpb0eYsbNBMUhdIF7H/uWnGEoFq29/uAqwIwO5oZgrcwA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100619; c=relaxed/simple; bh=YHk8ukGNiO4CYahbZWbONFlefLkTB31ABcvUQFu0I2E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QB6zKiGltNZsDwtTVxYlyxdpcmF3WYC22ianehV5zQcfautxjsWwk7uWfMGkKUF/RGAByh2blOP6GFfP4Zi5mMlIRVLX6XZHumEmXBfEMMrrp+uMqAzDCbN+y04avj6fee+J2ir5NSu2GZ+x11Wo+TrCFZB9hB1NyI4XzcFz0GA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GiFhZEUo; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GiFhZEUo" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2c0a81c1738so211955ad.2 for ; Fri, 29 May 2026 17:23:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100617; x=1780705417; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zpKIBxF4TwirmU794bEiVHTfk4L9neuBdaiGjtHF4Ic=; b=GiFhZEUo7vw88iNCUHwR9he7YBkbXqngNtNk8vFV1jro+3qWHMxwk9lqS2ZjTYypXG 2/TW5KzT5/bgZKaNmvlw5ysltEzBFT2YsxyordWA5cWHWdH4HV4blf5Qnjr7p/EusGpo JKMP2BcqCK1ji+LjlZYvdJzKP8ar/N6nmy7fCayYbTxI7y9dCDLeNz6/UtAF6bC/Z+c3 wr6Q8jfEs551qcV+/Ui3LtNUURj7eKe3UFV8+HfkTOWTgHkAIkrAOM4WGaUowhVEV+3e rWaP3rh3NKfN3u3EhsG3++tXHoGVtrP9V6t6wQmSdUbrUnC5A3EDOKuDwPB0pDbu1p8t JWVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100617; x=1780705417; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zpKIBxF4TwirmU794bEiVHTfk4L9neuBdaiGjtHF4Ic=; b=BKH5OaTxsBPIovPY+yXpRqU792SDT80viXf1JoUv4yEhDyIWF93J8MAQ0sAPi0pRRW TiWfovJ7RdUr72SV49yDBO5IVMLcFvayt+qYzhJczrNGNowcUHzifR2JHlpfxOLUrsZB ZWdMDWPFiYh4pmXlJWHeHGRzv/745IPi3aujA+KOYiSby2KaLls1JBQiyc2a2M2V1v19 DjL/kMuuVL1n62D6x0zLaWCV21gmyG3hZW0DhfZ1XuIQtims38jsGcPnEQMliroSwAL4 35JlGbto8sQk7Oegqg9BU/Xhka1DdrnjfGfO3rxO97FH+1tfybcxtoLxgGAP/iZJxMAq /2lg== X-Forwarded-Encrypted: i=1; AFNElJ9dAv4LaCUke++jqMAj8V/l1vMcqxPMniuB5OkPtjEr8xs6MP1uUjU0IaevUoSSRjoDkLJmGjdezsK5juY=@vger.kernel.org X-Gm-Message-State: AOJu0YxoO9fYBsMIgU6pYl6NCvijOW1KwuaVUVlU0Op7LopZ1yW4X850 U7vMEpDFnXzcaZyQ1JKUK5sCmr56DXw+UtoqN+frK9GrgXsXk8lqxw4I/vxFrfHTLEv2NLQszcf w9wkNPH9Z X-Received: from ploe7.prod.google.com ([2002:a17:903:2407:b0:2bf:a0d:a7e8]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:da8d:b0:2bc:ac76:c1cf with SMTP id d9443c01a7336-2bf3683bd81mr22990915ad.24.1780100617198; Fri, 29 May 2026 17:23:37 -0700 (PDT) Date: Sat, 30 May 2026 00:21:33 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-19-jrhilke@google.com> Subject: [PATCH v4 18/19] KVM: selftests: Make number of vCPUs configurable in IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add a '-v' flag to tools/testing/selftests/kvm/irq_test.c to allow users to configure the number of vCPUs to run in the test. Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/irq_test.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c index 0db4ea4eb1ef..967083b8e2ff 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -144,7 +144,7 @@ static void kvm_clear_gsi_routes(struct kvm_vm *vm) =20 static void help(const char *name) { - printf("Usage: %s [-a] [-b] [-c] [-d ] [-h] = [-i nr_irqs] [-m] [-n]\n", name); + printf("Usage: %s [-a] [-b] [-c] [-d ] [-h] = [-i nr_irqs] [-m] [-n] [-v nr_vcpus]\n", name); printf("\n"); printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n"); printf("-a Randomly affinitize the device's host IRQ to different physica= l CPUs throughout the test\n"); @@ -154,6 +154,7 @@ static void help(const char *name) printf("-i The number of IRQs to generate during the test\n"); printf("-m Pin vCPU threads to random physical CPUs throughout the test\n= "); printf("-n Deliver 50 percent of IRQs as non-maskable interrupts\n"); + printf("-v Number of vCPUS to run\n"); printf("\n"); exit(KSFT_FAIL); } @@ -187,7 +188,7 @@ int main(int argc, char **argv) struct kvm_vm *vm; cpu_set_t available_cpus; =20 - while ((c =3D getopt(argc, argv, "abcd:hi:mn")) !=3D -1) { + while ((c =3D getopt(argc, argv, "abcd:hi:mnv:")) !=3D -1) { switch (c) { case 'a': irq_affinity =3D true; @@ -210,6 +211,9 @@ int main(int argc, char **argv) case 'n': use_nmi =3D true; break; + case 'v': + nr_vcpus =3D atoi_positive("Number of vCPUS", optarg); + break; case 'h': default: help(argv[0]); --=20 2.54.0.929.g9b7fa37559-goog From nobody Mon Jun 8 09:48:36 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58F8C3002C8 for ; Sat, 30 May 2026 00:23:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100622; cv=none; b=K6+5xZA97RyYWpErZjFUQRb8dqRVik/1bDycRxCE9/XY+v2dK9PfgT4tMa07abvddCpWi/3F2ZOQiUC5DIo3exqzkPNVKCkx16kuz38grvR4bBgCEc1ySFL10w53ov8vCdNm6rlojgvrBWPuu8S39rHbP2ExrTwhQNxWZEzHd+k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100622; c=relaxed/simple; bh=3yeVP6qjqV8GM2bvDNGyj/WO9RKz1xKmC/mcaxsDUX0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZDpd0lSRncYjyVPmpBH4pHmTISlGEuKKMCM3GFvofS4X+v0xPAGxQoUm2z6KUQPg3vOfKMX9qBymCXK1Qu51FKYmK8rJG9JtgTBfTqjsUmyNfoc0rUCj5YTC5cPSonLlbR1xAdb5mKJcOxIGqHP7PwBpWGRg29J/dODk1NhV2V0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gtcmJBoD; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gtcmJBoD" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-c8581f7723aso317192a12.0 for ; Fri, 29 May 2026 17:23:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100621; x=1780705421; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dNSvW6+2aoUGl/zEwvlnmm46oR9NmJ5Z6dcbh7LJmwQ=; b=gtcmJBoDn1d70iLDrnkld6+5I8r6c/mbFyfNk6zLm69/truUfLheU4KJTy9kPqzuyz plEisHTduqxdRh9vNqs49WyvK/ilhY5tTNnNtmNVu7FFUvSB9OU9i0xWlm/Otn6PWGU1 5gSqqdDW7a7aMEbcWxRG9dZW+COj6kD2SD2kJJtLB53LerWHodJo4e/jtaH75XMXxHBc RM703HvTBtxJ2KaFy4pEQ4ZA2SDBUrvTaKf9JM7uCRa/osyPIoOxOis4331buFj9Paex 5+fhHWnVZa77zHwWwXpNuX33Q1VgSAUsWUq8zANKePpbS6EbC8N+32qa9VbMJt+j9Rxi tdDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100621; x=1780705421; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dNSvW6+2aoUGl/zEwvlnmm46oR9NmJ5Z6dcbh7LJmwQ=; b=lryv13yLawxrkoN1671HtSw3s13olLGVq3I6tQrY0Yvd9ZcSM5yCFu7RGlVk23yBdA 2LaVsUZcQAW4Atboc6E/Yh4jWTNoW+7X59JyXq+cQG3ShYRR/cFfM1wdqDg88JheqynF J1ZXJXzJDZGVNqNt9CB/Cw5uD0ueumOOb2fMxOBtGv49GO3J4d+WBWpoHmc4koWZA1AG i4ndZCof3FanrMINBab79Y22qyCqcdAhmq2JpBN7gFU3jIrgCkWJBVJfzdUYSgxXjIIt XP0Kx+qxNevtT0BnUGIZlXCz7Rqm9IRngKeAPoUWLB+pMDYKBeJ3IWnI+cztSmHGlfPf 5Wng== X-Forwarded-Encrypted: i=1; AFNElJ9BUQDGh5kWpnesHePdeSbhjTLpzfhQ7thQbIEk+/CFVR9QS86xdGnM2i3OqeOZE7FAkYRhCMnWIlJElJw=@vger.kernel.org X-Gm-Message-State: AOJu0YyiDMKtp/pQH/KU2FEwnibS7PBtR2p6ILuEaawo3rktjFk7yhMp csHflYTXpS0toMrfH0JrJQYDLkKEzCcgrG52VNoJettclKLivfQVffMgbPWpdVM0LggWUQ0QLCW 8q5n9+y2c X-Received: from pglc6.prod.google.com ([2002:a63:d06:0:b0:c82:7498:e3b5]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:ce4e:b0:3a2:d79c:416c with SMTP id adf61e73a8af0-3b427f97563mr1614544637.32.1780100620429; Fri, 29 May 2026 17:23:40 -0700 (PDT) Date: Sat, 30 May 2026 00:21:34 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-20-jrhilke@google.com> Subject: [PATCH v4 19/19] KVM: selftests: Add xAPIC support in IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add the '-x' flag to tools/testing/selftests/kvm/irq_test.c to configure the guest to use xAPIC mode instead of the default x2APIC mode. This expands test coverage to ensure that IRQ injection and routing work correctly when the guest uses the older xAPIC interface. Co-developed-by: Josh Hilke Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/kvm/irq_test.c | 28 +++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftes= ts/kvm/irq_test.c index 967083b8e2ff..29f54a435c8e 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -20,6 +20,7 @@ static bool guest_received_irq[KVM_MAX_VCPUS]; static bool guest_received_nmi[KVM_MAX_VCPUS]; static pid_t vcpu_tids[KVM_MAX_VCPUS]; static bool migrate_vcpus; +static bool x2apic =3D true; static bool irq_affinity; static bool block_vcpus; static bool done; @@ -31,14 +32,20 @@ static bool done; =20 static u32 guest_get_vcpu_id(void) { - return x2apic_read_reg(APIC_ID); + if (x2apic) + return x2apic_read_reg(APIC_ID); + else + return xapic_read_reg(APIC_ID) >> 24; } =20 static void guest_irq_handler(struct ex_regs *regs) { WRITE_ONCE(guest_received_irq[guest_get_vcpu_id()], true); =20 - x2apic_write_reg(APIC_EOI, 0); + if (x2apic) + x2apic_write_reg(APIC_EOI, 0); + else + xapic_write_reg(APIC_EOI, 0); } =20 static void guest_nmi_handler(struct ex_regs *regs) @@ -48,7 +55,10 @@ static void guest_nmi_handler(struct ex_regs *regs) =20 static void guest_code(void) { - x2apic_enable(); + if (x2apic) + x2apic_enable(); + else + xapic_enable(); =20 sti_nop(); =20 @@ -144,7 +154,7 @@ static void kvm_clear_gsi_routes(struct kvm_vm *vm) =20 static void help(const char *name) { - printf("Usage: %s [-a] [-b] [-c] [-d ] [-h] = [-i nr_irqs] [-m] [-n] [-v nr_vcpus]\n", name); + printf("Usage: %s [-a] [-b] [-c] [-d ] [-h] = [-i nr_irqs] [-m] [-n] [-v nr_vcpus] [-x]\n", name); printf("\n"); printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n"); printf("-a Randomly affinitize the device's host IRQ to different physica= l CPUs throughout the test\n"); @@ -155,6 +165,7 @@ static void help(const char *name) printf("-m Pin vCPU threads to random physical CPUs throughout the test\n= "); printf("-n Deliver 50 percent of IRQs as non-maskable interrupts\n"); printf("-v Number of vCPUS to run\n"); + printf("-x Use xAPIC mode instead of x2APIC mode in the guest\n"); printf("\n"); exit(KSFT_FAIL); } @@ -188,7 +199,7 @@ int main(int argc, char **argv) struct kvm_vm *vm; cpu_set_t available_cpus; =20 - while ((c =3D getopt(argc, argv, "abcd:hi:mnv:")) !=3D -1) { + while ((c =3D getopt(argc, argv, "abcd:hi:mnv:x")) !=3D -1) { switch (c) { case 'a': irq_affinity =3D true; @@ -214,6 +225,9 @@ int main(int argc, char **argv) case 'v': nr_vcpus =3D atoi_positive("Number of vCPUS", optarg); break; + case 'x': + x2apic =3D false; + break; case 'h': default: help(argv[0]); @@ -226,6 +240,9 @@ int main(int argc, char **argv) vm_install_exception_handler(vm, vector, guest_irq_handler); vm_install_exception_handler(vm, NMI_VECTOR, guest_nmi_handler); =20 + if (!x2apic) + virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA); + if (device_bdf) { iommu =3D iommu_init(default_iommu_mode); device =3D vfio_pci_device_init(device_bdf, iommu); @@ -248,6 +265,7 @@ int main(int argc, char **argv) =20 kvm_assign_irqfd(vm, gsi, eventfd); =20 + sync_global_to_guest(vm, x2apic); sync_global_to_guest(vm, block_vcpus); =20 for (i =3D 0; i < nr_vcpus; i++) --=20 2.54.0.929.g9b7fa37559-goog