From nobody Tue Dec 16 13:23:22 2025 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 94B731B47D9 for ; Thu, 20 Jun 2024 17:57:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718906245; cv=none; b=kRbz9Gq/Qnw2iFsk5hw70KGrRTi1ZvueNez206c1hPrDM9hb0W13TlMF5ZTJTdtG1IdOm0CrOPqfwvXZ8flib/cnsMBFv/BrCdQziMeGzY7XTc4Fo7B/WwdIHBLj6ipPI3XN6dmyyUc/BLwq8t8OQWT13GQ7BipxLKCUmR/xQVU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718906245; c=relaxed/simple; bh=iFxwlIlYJmwx3d0DAafe4DTo8c0DRvIQvWI6JBsAmH8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VC840l10T2TeO3tKrnLRaOSwKeZiCD1TFivT6lLmPQiwlDlNlz93nxg8hbiHhl+LD40CUdHJYUG5LRkJvTJhqcudEq3Wq4+UVI0CZD67TEGlGzFDGZvS6nWRwl6pxlOQMqdl3Y/TH3aMxsjwQSRxiW/Uh6bkikd7IOt4+gaUag4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DLQPyUnw; arc=none smtp.client-ip=209.85.214.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DLQPyUnw" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1f9d9b57b90so5177655ad.0 for ; Thu, 20 Jun 2024 10:57:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718906243; x=1719511043; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1FPc4+rs8YXE06jsQAfHrhhg6ai+ZxxKg14pnSGboBY=; b=DLQPyUnwRwTs03/sHtZECv4mGHGQqVVbF+tTWRYfFko/WUqHxtC01TdW+XCaMSnAXN f7SWDDFr00C+3P9L3+X7J4caoaNCQtAnjgf7NR/UJdtP44Cq8meNoRr4Cp9trLk/wKsc xidStn5/cU5xHCmirhKi+yxi374GL4hjRi0xK3acmX+USDN3RqJE0GEiVM96GwKlVaES qr2WUQUC91IC7WjJojX0g0dV0Mvclbgb4cEFqY6oVCKtkc/kJ1YJBnkVBnAl1iu6yO/X jsvaJKIn7bMUR8Vo9XwQCuwOIsfP0PypD1ZVvq+ZH9ssu3LkcWrdIm8fRCCAW1FEh9VN va2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718906243; x=1719511043; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1FPc4+rs8YXE06jsQAfHrhhg6ai+ZxxKg14pnSGboBY=; b=RJWdnFqaVCZcCI3BGkysluooF5ShkZ6w83WZ6iJkXLGhoKZTYneZqj4C96xpM3LTpK lhJaQNFSnhLua1lVNN7IFTck1o5ndEuMlC0QvLnv5nf1nhljgDRzkM8BezEC9M/byBkj c+xUEYeVVs35RsEOgvu5E6TMpIQm4KxDXfKvMJnXIYVQIoS0jlVBOZnBB9Taaui5rTuq l/sxCNchvmZo2WCazStwx17myHqooVLjTKxzvMmyiyjHG4IS3hrNP+ztN4vHTyekFwJt Zq89dBdk/bY/5AyHhiXcI3Y1sUyQchViYfvmNzqTd2ztKeG7fMeXV0XC1YB9oVDa5vwp 2Kfw== X-Gm-Message-State: AOJu0Yzeu0Rw8AN+MrYa2uBmY9nxYtkmzV77v0fOOdo6h22vnXd/f4Fb kcIiqWJgrqel/71YvceUBvxm/Y2Cm+tF4n5wVwCeWjCstViaoVBNBKfPmGjBBGI= X-Google-Smtp-Source: AGHT+IEb+NM6rxCaMHA96INVWGzEGUheh9Av2Sh9BOmyvfITs/fmbdQOFOrzoApocqlSRwsejpxs/A== X-Received: by 2002:a17:903:32c8:b0:1f9:9768:ea61 with SMTP id d9443c01a7336-1f9aa3b0a03mr79468045ad.2.1718906242783; Thu, 20 Jun 2024 10:57:22 -0700 (PDT) Received: from localhost ([216.228.127.128]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855eeaaddsm140325445ad.122.2024.06.20.10.57.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jun 2024 10:57:22 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Yury Norov , Andy Shevchenko , Rasmus Villemoes , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider Cc: Alexey Klimov , Bart Van Assche , Jan Kara , Linus Torvalds , Matthew Wilcox , Mirsad Todorovac , Sergey Shtylyov , Mathieu Desnoyers Subject: [PATCH v4 05/40] sched: add cpumask_find_and_set() and use it in __mm_cid_get() Date: Thu, 20 Jun 2024 10:56:28 -0700 Message-ID: <20240620175703.605111-6-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240620175703.605111-1-yury.norov@gmail.com> References: <20240620175703.605111-1-yury.norov@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" __mm_cid_get() uses __mm_cid_try_get() helper to atomically acquire a bit in mm cid mask. Now that we have atomic find_and_set_bit(), we can easily extend it to cpumasks and use in the scheduler code. cpumask_find_and_set() considers cid mask as a volatile region of memory, as it actually is in this case. So, if it's changed while search is in progress, KCSAN wouldn't fire warning on it. CC: Peter Zijlstra Signed-off-by: Yury Norov Reviewed-by: Mathieu Desnoyers --- MAINTAINERS | 1 + include/linux/cpumask_atomic.h | 20 ++++++++++++++++++++ kernel/sched/sched.h | 15 ++++++--------- 3 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 include/linux/cpumask_atomic.h diff --git a/MAINTAINERS b/MAINTAINERS index 54f37d4f33dd..7173c74896d8 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3730,6 +3730,7 @@ F: include/linux/bitmap-str.h F: include/linux/bitmap.h F: include/linux/bits.h F: include/linux/cpumask.h +F: include/linux/cpumask_atomic.h F: include/linux/find_atomic.h F: include/linux/find.h F: include/linux/nodemask.h diff --git a/include/linux/cpumask_atomic.h b/include/linux/cpumask_atomic.h new file mode 100644 index 000000000000..1aaf9a63cbe6 --- /dev/null +++ b/include/linux/cpumask_atomic.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __LINUX_CPUMASK_ATOMIC_H_ +#define __LINUX_CPUMASK_ATOMIC_H_ + +#include +#include + +/* + * cpumask_find_and_set - find the first unset cpu in a cpumask and + * set it atomically + * @srcp: the cpumask pointer + * + * Return: >=3D nr_cpu_ids if nothing is found. + */ +static inline unsigned int cpumask_find_and_set(volatile struct cpumask *s= rcp) +{ + return find_and_set_bit(cpumask_bits(srcp), small_cpumask_bits); +} + +#endif /* __LINUX_CPUMASK_ATOMIC_H_ */ diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index a831af102070..557896f8ccd7 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -3312,23 +3313,19 @@ static inline void mm_cid_put(struct mm_struct *mm) =20 static inline int __mm_cid_try_get(struct mm_struct *mm) { - struct cpumask *cpumask; - int cid; + struct cpumask *cpumask =3D mm_cidmask(mm); + int cid =3D nr_cpu_ids; =20 - cpumask =3D mm_cidmask(mm); /* * Retry finding first zero bit if the mask is temporarily * filled. This only happens during concurrent remote-clear * which owns a cid without holding a rq lock. */ - for (;;) { - cid =3D cpumask_first_zero(cpumask); - if (cid < nr_cpu_ids) - break; + while (cid >=3D nr_cpu_ids) { + cid =3D cpumask_find_and_set(cpumask); cpu_relax(); } - if (cpumask_test_and_set_cpu(cid, cpumask)) - return -1; + return cid; } =20 --=20 2.43.0