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 1C86B309F08 for ; Wed, 10 Jun 2026 00:54: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=1781052844; cv=none; b=f3jo4RxSfE46DZQoWPE0v8wyDxbYwLY0190c2ME3ZGoLs+wVzqMfAKaEMZKDgIEJnBE/mH4HxlZazbssAeg+Ibdo2INoyQCR9NdqUGpmjGyzPAL9e+vsx2SEEXZ7Egw2sQXwVNRiJDnzfDFgcgOZf2DRCpFgPvYRN8mOVvwNfN4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781052844; c=relaxed/simple; bh=JlooIplFY0tapZ4KHCVZzqX1ug3glWpGzl8vxECWm74=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Us0J3RD5UoohqPr+JVL/RxSp16rP/s0G0zLOQd2kKT4ep91sh9JRs1Qxjq18aNkhkXeyX0Oamt74BUzyVZPJy/HIEodP4pFU5m/aC4tQmraLq53XQoUzYJ9nU3HSKQBHBFvAmA1Cbqk7Eie/dVP7+eucbwbk4dS7CBt7vog4CLE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UidSsVs5; 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UidSsVs5" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-842211d6e48so6918651b3a.0 for ; Tue, 09 Jun 2026 17:54:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781052842; x=1781657642; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=i+ncGA6Fvns8f5sP+kNiRwZfNV+IfAKagrqzhgLKDtg=; b=UidSsVs5pEagXBkDRhiJZQxl3C2y9BWqt3kDNbsKI6qIOPJHwBDZUuqzGOgbJfRuaa SSW743JlgVA4GoTLqhYw9QxD0Xnfo3PqZCfewSyCfRsDZebiH77kgt152TjRJQUHB9v7 omD4cQtj/kUsBy3Rt/Od6y8G6b7eeu455AGeahKFn48x09dKitFBJXc01XVbj+9u7Q7J APEnZNVRK8EYMAA1ntal794dk66OHyVkF/HSPqQcrI359c09ZVFwpQyE9CjAhAIQEzJX 5A5y417WTDhGPBZH4JjKmf18biL7NC/IqaWRy0rrjFC/3KcNMvxfitCG71sS6tsai6DX tEiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781052842; x=1781657642; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=i+ncGA6Fvns8f5sP+kNiRwZfNV+IfAKagrqzhgLKDtg=; b=aqX0N/dR/tvIfaaZe9bHvCdhefBZJj36xPrHKhB4PPpHNQCI1JUDRpz/8c/QewDIgb WD98xFbKiuRuZLzku0NELnSleVqoAb7mEJboRZKKkMAYXY7Wb6jkRhJViPepEgluuaLL 0YW+Qf4NRNpJzaE/wLm5oDv8fpmB9Kt+yjv8OP73vWxNPAZxX8zvj8DgXPWiaCUubSve uGgAasUjmTRm+RsJPf3n2+KutPPVJToF9dJdKNK3CSacvJJrEte7uLO6uYDjzLYaE7pt fbHnc6EDf6o4sq/SOUGU5m+ROeADfkANAEV2YcMCiTbJc7ljcTZx2nGdwir88cgEx34q 3E1A== X-Forwarded-Encrypted: i=1; AFNElJ8hyuTYklA6StZMrwLF21wqKARhykVgY7t4yv3GjD4yssmRZIruRTiPvAdXj+P/dTlGsqXHSeBpSW5IvEw=@vger.kernel.org X-Gm-Message-State: AOJu0YzqVCC4XdA0SaheWSGhI7UxgQQslKjmcsiz+8E21gPjJUn6CbTo H296eaurzDksOFpksf4eJa0F8N/O8y0Kgm9HDJAIkvnJ4lvzTCJKoHGRnIdop3DZ1kM8ZDnJtG8 e/inHeA== X-Received: from pfad10.prod.google.com ([2002:a05:6a00:28a:b0:842:84fc:f4f3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:b90b:b0:82a:6461:6d15 with SMTP id d2e1a72fcca58-842b0f49acamr22128319b3a.46.1781052842229; Tue, 09 Jun 2026 17:54:02 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 9 Jun 2026 17:53:35 -0700 In-Reply-To: <20260610005338.2967132-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260610005338.2967132-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.1099.g489fc7bff1-goog Message-ID: <20260610005338.2967132-16-seanjc@google.com> Subject: [PATCH v6 15/18] KVM: selftests: Add a utility to pin a task to a random CPU, given a CPU set From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, David Matlack , Josh Hilke Content-Type: text/plain; charset="UTF-8" From: Josh Hilke Add a helper function, pin_task_to_random_cpu(), to pin a task to a random CPU from a given cpu_set_t. This helper will be used eventfd IRQ test to migrate vCPUs to random pCPUs, to stress host-side interrupt routing and delivery. Suggested-by: Sean Christopherson Signed-off-by: Josh Hilke [sean: massage changelog] Signed-off-by: Sean Christopherson --- .../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 c1f588154398..b39e713c30a4 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -1094,6 +1094,8 @@ static inline void pin_task_to_cpu(pthread_t task, int cpu) TEST_ASSERT(!r, "Failed to set thread affinity to pCPU '%u'", cpu); } +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 = sched_getcpu(); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 92013883f35b..72f41b15594c 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); } +void pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus) +{ + int target_idx; + int nr_cpus; + int cpu; + + nr_cpus = CPU_COUNT(possible_cpus); + TEST_ASSERT(nr_cpus > 0, "No CPUs available in possible_cpus"); + + target_idx = kvm_random_u64(&kvm_rng) % nr_cpus; + + for (cpu = 0; cpu < CPU_SETSIZE; cpu++) { + if (CPU_ISSET(cpu, possible_cpus) && target_idx-- == 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, u32 vcpu_to_pcpu[], int nr_vcpus) { -- 2.54.0.1099.g489fc7bff1-goog