From mboxrd@z Thu Jan 1 00:00:00 1970 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 922223AD506 for ; Tue, 21 Apr 2026 23:16:55 +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=1776813418; cv=none; b=oamoqR+bP0uV6JQIAApgM8LMZwXlFiSXV2hWlCSD6s633v+dr+uMXbg027di5ZiOeCuDcqbSLpvN0SsFgtYt3s2w0FEEVoGsoU79R15o4+L1cbtcUb1DGDgzH7ncOttu6zAoGNhleCEoK5dvcjHs9+ORQO4u5ZYbJvuSaKSXVcc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776813418; c=relaxed/simple; bh=4cnVa9ZZt3+B2TMTEd7btulyUDHWdkJ6QXPrVFuZKTg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GCiDq5iaG8tD69UP8gg1QQr3XjKAvzXWR28vhd9+2TTBoDd4SloaqqS9I949TVCzHoj/2xZGgkfB6ReNxrvpgW+P33R8Q623qkploJzwSRdFCAuky9Znu3FUfkfL/Z55ApPTbuDOQ+aCTWG5QAqb9/dIAhx8k14Dzz3O950eXUk= 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=eEwrGwPR; 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="eEwrGwPR" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-82f07078ff0so3237314b3a.1 for ; Tue, 21 Apr 2026 16:16:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776813415; x=1777418215; 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=1SWlI+Mb08LTFyPEjXaQ1cgftdkltHpsWIL9YhvjdCs=; b=eEwrGwPRLDeNf2eHvqjJYOrnDgD68BuZFu2/AO0H85uZ1ueAZIHsx7vaPBbL6/zSgf zbwHCqJcE1JcTg8FQF22bDmJiGGn1GgLZbejXIdSg5tlkUdlDNanOh4pJ/rRO0xMRnu8 u/Fi/pjzdiHL/ElXMnI0yY41O2ghBzzEyBLIKfIotFD2JrP+RP/7UWFNWsRzoqEUogrF iXMmmYRMJcSL9ANxfrOOBvuHDh7+YKg+jjJ1hIdJIvcVpiwmIeX1eicPvRxFbzfpx7d0 OE0mqJBxzlh/2eS8yvjEhMhlxQuZ2A7KK2GKUQtAMj1/Gtuzv1L3cYvGEF8ehKtACxn/ 43fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776813415; x=1777418215; 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=1SWlI+Mb08LTFyPEjXaQ1cgftdkltHpsWIL9YhvjdCs=; b=m6AJlFvwP4D/2lHOccFDijq2Q1Kg6Qrd+QpRUin9GCO/8sAUIVYzg9OGc+bh1NYPIY dUUgppcbaQnEDTZ8Dminj8xBElH7YTg3jB/zQ8CssId4pkdQvap9xRUW6YwI9VFNTTNH GmZQ5K6XBymZpajZA4bVES6bt5GOSNx6+z4GwxKW8B+x5ZadX0adCtPjfTxYoM5X8TJv ogsVqVTFO3TMELmcCj/4hobL2sNyl5x+McFdcAdmYXVJyeh3IPCX03KKow+5269IGQNg DwGYN1NNyTE7MbNO10XUz2yK+cBK+GUjcTraxi3dovLeNDgsJXPAwxrRP0AmCIPtGdF/ HgDw== X-Forwarded-Encrypted: i=1; AFNElJ9fqyuWgHyGx6KKpTEgrXgmXcGo4d6acavGnJ9snRNnafMV+VZUlvIm9e49ntzjjYXfzg/YLG7Fvjjd0dw=@vger.kernel.org X-Gm-Message-State: AOJu0YzcD0HO8DH5r727vvUdIHk5FNmPmWOD987ig+zwbXLHwSfVTvgE Oje3/sR8R6RZaD9eqbvlrIxH4FIlTrTjHtyXJBOfAx7tUWfKsX0B39msww2TmcHXNbVr23nIGhY Ma08tSZ7w X-Received: from pfgs10.prod.google.com ([2002:a05:6a00:178a:b0:82f:c180:a882]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1f18:b0:82a:5e9a:922e with SMTP id d2e1a72fcca58-82f8c94279emr23429942b3a.42.1776813414907; Tue, 21 Apr 2026 16:16:54 -0700 (PDT) Date: Tue, 21 Apr 2026 23:15:55 +0000 In-Reply-To: <20260421231557.1254270-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: <20260421231557.1254270-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.rc2.533.g4f5dca5207-goog Message-ID: <20260421231557.1254270-15-jrhilke@google.com> Subject: [PATCH v3 14/16] KVM: selftests: Print vCPU affinity on timeout 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-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_pcpu[], int nr_vcpus); +void kvm_print_vcpu_affinity(struct kvm_vcpu *vcpu, pid_t tid); 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/selftests/kvm/irq_test.c index dd6a08d3bf4f..43338eb3ab41 100644 --- a/tools/testing/selftests/kvm/irq_test.c +++ b/tools/testing/selftests/kvm/irq_test.c @@ -306,6 +306,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]); 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/selftests/kvm/lib/kvm_util.c index 83732c07dfda..f85e061f47fb 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 @@ -662,6 +663,54 @@ void kvm_print_vcpu_pinning_help(void) " (default: no pinning)\n", name, name); } +void kvm_print_vcpu_affinity(struct kvm_vcpu *vcpu, pid_t tid) +{ + int nprocs = get_nprocs(); + bool first_range = true; + int i, start = -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 = 0; i <= 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 == -1) + start = i; + continue; + } + + if (start != -1) { + int end = i - 1; + + if (!first_range) + printf(","); + + if (start == end) + printf("%d", start); + else + printf("%d-%d", start, end); + + start = -1; + first_range = false; + } + } + printf("\n"); +} + void pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus) { int i = 0, nr_cpus = CPU_COUNT(possible_cpus); -- 2.54.0.rc2.533.g4f5dca5207-goog