From mboxrd@z Thu Jan 1 00:00:00 1970 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 A95CF2E8B6B for ; Thu, 4 Jun 2026 02:02:46 +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=1780538568; cv=none; b=oGgAxwJcabKuu45EfE/dCEfq4A6cm5IzAuhPAfFrD0oVGHxWq3dPO3fQg0xFn3lxetVVOOUNr+U6gfsUQbJo9kUX89HTyTvx2Av3Qycl8dj9v/fkioxdFR/YLAX0c3CuivU6Vv4uIrOKMisJqqMx3EODJ50vWJpN/Mr1iqTtEwE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780538568; c=relaxed/simple; bh=3t/DAyGvNL0/LW/JOD2fGCS4PNVrVIn6BhFfqveDG5Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OlvJwSaXoYaq+VXAYabOdSCc8hrVnsr6YzNxR0dWKA3LUSeeW7ceX68QkoCfQVZ7XW0tUNRy57W4EiuGrlNitKcanOURJLXZoxYGT/YuC8J10//aDbQwE3KgQUSbBV++45pQkBzWrPNOJ11D9DToZh2KXjszt898JE+7HplZ4ig= 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=jWKVscqa; 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="jWKVscqa" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2beff6b6e74so2158105ad.1 for ; Wed, 03 Jun 2026 19:02:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780538566; x=1781143366; 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=cusk696wlOPTxoLpjL8sQnvno9DNsartad/zeYEHRE4=; b=jWKVscqarIpG57yRK8ideSo+LT/vaS4ohTXk9mEezWuqcZ3fMhkw7M3alDRqUh8H6A m0uGQe3TAmBPLTV6xFcrH8XssH6NZXyktGPqhCKcll1Cb8GoMFLPWF+DAHp0e+aVNhiL H7CH6k1DiKQ7n5bHaVx3+AbhetQu950/vspcGGql7t/4AXZNXx4n0dn5AyJz//ABEQBN aVUk5jtPYnbrh7tc80cefa3qP+/rwiUTDMo9ik2XKGc7TfAeTBup/35U7KbincJ0fg3N rfNt0pu+1vFQ6E1yusAxe6l3RVypnph2Xvqkuz2xok3I6DCAXW3KcD9QR5AWoQWQSJeF y7TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780538566; x=1781143366; 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=cusk696wlOPTxoLpjL8sQnvno9DNsartad/zeYEHRE4=; b=LgMde9MA8T71geuM4bfKwiyUFCJR4zCsKbLLmdqQ8KpWx5E1LnuFbxSvj98Kuq2zTd m8v9vvzxr4ju47L6X2X+5f5szG+GreUSC+IWUwSypKLO6Jb58R/7Ti/n739uXr87viFZ xbqvTZ69vzbzYGs1fAEV/MKsPwarVdyM69yLRtVoxY/iGCuakRRht+ZcvXfhqWOxK5lM 2gC3w70GQgL5ve14sZOI9PxkhLdomjynKJR4Jzk6zur3q2OOilZKpKDwwYx3l6jU67aa zNQwHT8e6dd+JTTQdCHfqOg+srqX+Jv1N8C+yO1X/RLPEOm/v6DuJUm37h/Km+iYGMUF KNFw== X-Forwarded-Encrypted: i=1; AFNElJ/P6wOhDqAGOg1pB5//9JITg9l8gregUc3BKu5PHT+1xUR+HwtnWMnDUKWRJE55xwyBqtI/Y0huX1Mx4gk=@vger.kernel.org X-Gm-Message-State: AOJu0Ywc2lfO6bYAu4NMMXgod4RcNF0DeqHgNYOkM518t3fVeVuSqmM3 /88Jy8qEhoByk6lpElE6pPtCIECRXDH8ladwp37jxPWfTelaCIH+pYGBqEOoTHpMhhvEOYCA7gD SyZd1b2sp X-Received: from pldb6.prod.google.com ([2002:a17:902:ed06:b0:2bd:52b9:305e]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1b28:b0:2c0:c625:4019 with SMTP id d9443c01a7336-2c163fadcdemr62034665ad.25.1780538565666; Wed, 03 Jun 2026 19:02:45 -0700 (PDT) Date: Thu, 4 Jun 2026 02:01:39 +0000 In-Reply-To: <20260604020143.748245-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: <20260604020143.748245-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.1032.g2f8565e1d1-goog Message-ID: <20260604020143.748245-18-jrhilke@google.com> Subject: [PATCH v5 17/21] KVM: selftests: Add pin_task_to_random_cpu() helper function 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-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 88211d696a11..5968d04a77ea 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 0089d4543f5e..e538e6147ec3 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.1032.g2f8565e1d1-goog