From mboxrd@z Thu Jan 1 00:00:00 1970 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-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, 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 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); } +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, uint32_t vcpu_to_pcpu[], int nr_vcpus) { -- 2.54.0.929.g9b7fa37559-goog