From nobody Wed Oct 8 18:23:29 2025 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 2910421348 for ; Thu, 26 Jun 2025 00:12:34 +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=1750896755; cv=none; b=cWWk6Z/BMifYVb+LMC+zI5KA7RzEL+RoWDo1rvcfcR60NNGJjqBPTzb1ViHvhKwvhpffwffkisr+rEdQWFrvn+2Bce5LuFWjHP0rPld+rqrNN+zi4TgyE9V4qZOo7YR7XyI0P6n53cVsUmExFHGTOnoKVVHtg4ajWiq/iKRIQoo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896755; c=relaxed/simple; bh=64rymz2Kz4S/Z4aZF2H6eAnIafN/041V9v/2O/McSuM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oYO/X07WLZKlQGP1c+Z/GDB9GOXX3ECzb0pBOoZ5X5+ahSZUcPtZk+d6Zer8sNTJ39/fzjGiLnBXaTKo9X2c5NvSSc4JpaC0yO8xXGYJY6KrqviH9iJGbtwqVWdMsZDagsk12ZtAllexi81XQ802WWmbmXvUh3SMVIN4SjuAvt4= 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=ufvpqGi2; 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--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="ufvpqGi2" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2349fe994a9so2709325ad.1 for ; Wed, 25 Jun 2025 17:12:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750896753; x=1751501553; 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=7jYV3TYqsZyydz0IjaYFSoLu2pCs+jwctwQKh0JY6aA=; b=ufvpqGi2StfUbRKMylNAPoSlUqIiXyZpmJZCyty7Y4PvfQWjkdihC+6Kj45UE/qyKd txBZwGdiqIJ2XKzRh+Ixnm5b8FHRUSEkJPuA4xe7zSAV/Oa/6fOUD37XdFXOngF9e7sk q8GBhPdAKyRHt7Yle7WQhHnxuaLwojW56uqMyZQO0N/OSoyEJBkTWM2507rQYdVBuGef s7YPRdABW/J+LtmpsauZXBlS4Px5vTg01C6QT3icF72MgbW43iFWgl01l20D+tYU7C4k Iv8QqAYCykehFG2orMA8lW6ArbQMVWqMnZnt+OCMiDGdIq+u4ZLYTvqStQ/c3LO7zNBJ bAPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750896753; x=1751501553; 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=7jYV3TYqsZyydz0IjaYFSoLu2pCs+jwctwQKh0JY6aA=; b=jxb/GUB+6CCrcLKTmnfMqSBRKvubOuh1D9lMX/bborTNJVTpL/Lpv81QfC0GZ47sX8 xf7CLI3/ZiyiFyA3Oei3ovhdTp16pH5+clfU4I01mPkByMWIleuXAOwAt5lOJekYO2gu EqntfHf2cb5KkTBnOx4Vw0Sw08jHYd29QTug3vSd+K+Vop7cGNuF7dKM6Oy74nt8zgs6 RMyuHbiMwJsvt9uDam8LijEm45b8VJcbAgxNAUdTvHxjrQELAzsFTkHu3rHKC0z74zWD hBZLYIP3XsDe1+17lXntFt046Jbn8zIyHx7cA8ME8H5+9nTKiwm9RtJT/FO89DvRvIfM 0UDQ== X-Forwarded-Encrypted: i=1; AJvYcCXn0fc+fsvsxccUJ893K4ZBWScKWECpljWnF0JZ3m7Fr+FmmoDt6Eco/7RX7URHt20SMwQG0KLfh+mZClE=@vger.kernel.org X-Gm-Message-State: AOJu0YxuvI1aAo9E5WfyxSxSw0zcB+3M+VoAU8MHM01P5MhDyPhM3HtZ MEcOGEv2uyfTHleaGgD8ExY/rWqewY+u67ZWI5SUi9fNSevUV7aRBSr2yul8Pu7XosbIsnzr9lN yY1uIzw== X-Google-Smtp-Source: AGHT+IEokWeu3xZE8pF2I2mOTnRiQq8sbkVZWMeUSzDfllmerKjnZZu1jBEaneetis5nr35QOidy+TLEhOA= X-Received: from pjbta6.prod.google.com ([2002:a17:90b:4ec6:b0:312:e5dd:9248]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2f10:b0:223:f9a4:3f99 with SMTP id d9443c01a7336-23824044733mr89766355ad.29.1750896753599; Wed, 25 Jun 2025 17:12:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 25 Jun 2025 17:12:23 -0700 In-Reply-To: <20250626001225.744268-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: <20250626001225.744268-1-seanjc@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250626001225.744268-4-seanjc@google.com> Subject: [PATCH v5 3/5] KVM: selftests: Expand set of APIs for pinning tasks to a single CPU From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Jim Mattson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expand kvm_pin_this_task_to_pcpu() into a set of APIs to allow pinning a task (or self) to a CPU (any or specific). This will allow deduplicating code throughout a variety of selftests. Opportunistically use "self" instead of "this_task" as it is both more concise and less ambiguous. Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/include/kvm_util.h | 31 ++++++++++++++++++- tools/testing/selftests/kvm/lib/kvm_util.c | 15 +++++---- tools/testing/selftests/kvm/lib/memstress.c | 2 +- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing= /selftests/kvm/include/kvm_util.h index 91908d4a6edf..23a506d7eca3 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -21,6 +21,8 @@ #include #include =20 +#include + #include "kvm_util_arch.h" #include "kvm_util_types.h" #include "sparsebit.h" @@ -1054,7 +1056,34 @@ struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kv= m_vm *vm); =20 void kvm_set_files_rlimit(uint32_t nr_vcpus); =20 -void kvm_pin_this_task_to_pcpu(uint32_t pcpu); +int __pin_task_to_cpu(pthread_t task, int cpu); + +static inline void pin_task_to_cpu(pthread_t task, int cpu) +{ + int r; + + r =3D __pin_task_to_cpu(task, cpu); + TEST_ASSERT(!r, "Failed to set thread affinity to pCPU '%u'", cpu); +} + +static inline int pin_task_to_any_cpu(pthread_t task) +{ + int cpu =3D sched_getcpu(); + + pin_task_to_cpu(task, cpu); + return cpu; +} + +static inline void pin_self_to_cpu(int cpu) +{ + pin_task_to_cpu(pthread_self(), cpu); +} + +static inline int pin_self_to_any_cpu(void) +{ + return pin_task_to_any_cpu(pthread_self()); +} + 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); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/sel= ftests/kvm/lib/kvm_util.c index 90f90f1c194f..c3f5142b0a54 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -620,15 +620,14 @@ struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm= _vm *vm) return vm_vcpu_recreate(vm, 0); } =20 -void kvm_pin_this_task_to_pcpu(uint32_t pcpu) +int __pin_task_to_cpu(pthread_t task, int cpu) { - cpu_set_t mask; - int r; + cpu_set_t cpuset; =20 - CPU_ZERO(&mask); - CPU_SET(pcpu, &mask); - r =3D sched_setaffinity(0, sizeof(mask), &mask); - TEST_ASSERT(!r, "sched_setaffinity() failed for pCPU '%u'.", pcpu); + CPU_ZERO(&cpuset); + CPU_SET(cpu, &cpuset); + + return pthread_setaffinity_np(task, sizeof(cpuset), &cpuset); } =20 static uint32_t parse_pcpu(const char *cpu_str, const cpu_set_t *allowed_m= ask) @@ -682,7 +681,7 @@ void kvm_parse_vcpu_pinning(const char *pcpus_string, u= int32_t vcpu_to_pcpu[], =20 /* 2. Check if the main worker needs to be pinned. */ if (cpu) { - kvm_pin_this_task_to_pcpu(parse_pcpu(cpu, &allowed_mask)); + pin_self_to_cpu(parse_pcpu(cpu, &allowed_mask)); cpu =3D strtok(NULL, delim); } =20 diff --git a/tools/testing/selftests/kvm/lib/memstress.c b/tools/testing/se= lftests/kvm/lib/memstress.c index 313277486a1d..557c0a0a5658 100644 --- a/tools/testing/selftests/kvm/lib/memstress.c +++ b/tools/testing/selftests/kvm/lib/memstress.c @@ -265,7 +265,7 @@ static void *vcpu_thread_main(void *data) int vcpu_idx =3D vcpu->vcpu_idx; =20 if (memstress_args.pin_vcpus) - kvm_pin_this_task_to_pcpu(memstress_args.vcpu_to_pcpu[vcpu_idx]); + pin_self_to_cpu(memstress_args.vcpu_to_pcpu[vcpu_idx]); =20 WRITE_ONCE(vcpu->running, true); =20 --=20 2.50.0.727.gbf7dc18ff4-goog