From nobody Tue Apr 28 06:24:52 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CBF77C43334 for ; Sat, 4 Jun 2022 17:16:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239148AbiFDRQT (ORCPT ); Sat, 4 Jun 2022 13:16:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239045AbiFDRQO (ORCPT ); Sat, 4 Jun 2022 13:16:14 -0400 Received: from polaris.svanheule.net (polaris.svanheule.net [84.16.241.116]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54348286D4 for ; Sat, 4 Jun 2022 10:16:13 -0700 (PDT) Received: from localhost.localdomain (unknown [IPv6:2a02:a03f:eaf9:8401:aa9f:5d01:1b2a:e3cd]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sander@svanheule.net) by polaris.svanheule.net (Postfix) with ESMTPSA id 76B102E2936; Sat, 4 Jun 2022 19:16:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svanheule.net; s=mail1707; t=1654362969; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uIeD4GYUpGif2uY8WZoeDbu3gkmjTMXFybpVd2A6ewQ=; b=oVxnb3IuWxPCuVYNFbuSOI/4i4NrkgwUjeBrTlnX2wQeT1Tuv0uay0pOz+DlHbGw7L6cR/ 6/VK2DY+fvLBJD92R2R50JbAi8AFaRovJkrSEYmQzquuYFxMtFCSwIBLXf44D0uAHG5DJE PV9TF5dGBc/SlMuF7k4LnDguufA+ubiFzAOSd0yXMRiIGeF6DraThVhlNaT3kHmE75gT/C CGdNYL1aMAdvAPzT+FUYe/5c0/DBi6bnkPd9W/IEJuaQqf3CSvCLMPSJc4FhoapiD8hHWj GJGTVQ53/NT4Hz+AtJ8G9mUHhYSbOS9CiYXd4eVsvcj58OxpreCe5gUF3DNkRw== From: Sander Vanheule To: Peter Zijlstra , Yury Norov , Andrew Morton , Valentin Schneider , Thomas Gleixner , Greg Kroah-Hartman , Marco Elver Cc: linux-kernel@vger.kernel.org, Andy Shevchenko , Sander Vanheule Subject: [PATCH v2 1/4] lib/test: Introduce cpumask KUnit test suite Date: Sat, 4 Jun 2022 19:15:56 +0200 Message-Id: <5a55f41812dc18fccf66f8130682ff8e669e0c80.1654362935.git.sander@svanheule.net> X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a basic suite of tests for cpumask, providing some tests for empty and completely filled cpumasks. Signed-off-by: Sander Vanheule --- lib/Kconfig.debug | 9 ++++ lib/Makefile | 1 + lib/test_cpumask.c | 115 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 lib/test_cpumask.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index b8cc65d22169..85f2eb5c0b07 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2100,6 +2100,15 @@ config LKDTM Documentation on how to use the module can be found in Documentation/fault-injection/provoke-crashes.rst =20 +config TEST_CPUMASK + tristate "cpumask tests" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Enable to turn on cpumask tests, running at boot or module load time. + + If unsure, say N. + config TEST_LIST_SORT tristate "Linked list sorting test" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/lib/Makefile b/lib/Makefile index 89fcae891361..81f944cd74ae 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -100,6 +100,7 @@ obj-$(CONFIG_TEST_HMM) +=3D test_hmm.o obj-$(CONFIG_TEST_FREE_PAGES) +=3D test_free_pages.o obj-$(CONFIG_KPROBES_SANITY_TEST) +=3D test_kprobes.o obj-$(CONFIG_TEST_REF_TRACKER) +=3D test_ref_tracker.o +obj-$(CONFIG_TEST_CPUMASK) +=3D test_cpumask.o CFLAGS_test_fprobe.o +=3D $(CC_FLAGS_FTRACE) obj-$(CONFIG_FPROBE_SANITY_TEST) +=3D test_fprobe.o # diff --git a/lib/test_cpumask.c b/lib/test_cpumask.c new file mode 100644 index 000000000000..3b69fcb6730e --- /dev/null +++ b/lib/test_cpumask.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * KUnit tests for cpumask. + * + * Author: Sander Vanheule + */ + +#include +#include + +#define FOR_EACH_ITER_EQ(_test, _iter, _expect, _loop) \ + do { \ + (_iter) =3D 0; \ + _loop \ + (_iter)++; \ + KUNIT_EXPECT_EQ((_test), (_expect), (_iter)); \ + } while (0) + +static cpumask_t mask_empty; +static cpumask_t mask_all; + +static void test_cpumask_weight(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, cpumask_empty(&mask_empty)); + KUNIT_EXPECT_TRUE(test, cpumask_full(cpu_possible_mask)); + KUNIT_EXPECT_TRUE(test, cpumask_full(&mask_all)); + + KUNIT_EXPECT_EQ(test, 0, cpumask_weight(&mask_empty)); + KUNIT_EXPECT_EQ(test, nr_cpu_ids, cpumask_weight(cpu_possible_mask)); + KUNIT_EXPECT_EQ(test, nr_cpumask_bits, cpumask_weight(&mask_all)); +} + +static void test_cpumask_first(struct kunit *test) +{ + KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_first(&mask_empty)); + KUNIT_EXPECT_EQ(test, 0, cpumask_first(cpu_possible_mask)); + + KUNIT_EXPECT_EQ(test, 0, cpumask_first_zero(&mask_empty)); + KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_first_zero(cpu_possible_mask)); +} + +static void test_cpumask_last(struct kunit *test) +{ + KUNIT_EXPECT_LE(test, nr_cpumask_bits, cpumask_last(&mask_empty)); + KUNIT_EXPECT_EQ(test, nr_cpumask_bits - 1, cpumask_last(cpu_possible_mask= )); +} + +static void test_cpumask_next(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, 0, cpumask_next_zero(-1, &mask_empty)); + KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_next_zero(-1, cpu_possible_mask= )); + + KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_next(-1, &mask_empty)); + KUNIT_EXPECT_EQ(test, 0, cpumask_next(-1, cpu_possible_mask)); +} + +static void test_cpumask_iterators(struct kunit *test) +{ + unsigned int iterations; + int cpu; + + FOR_EACH_ITER_EQ(test, iterations, cpumask_weight(&mask_empty), + for_each_cpu(cpu, &mask_empty)); + FOR_EACH_ITER_EQ(test, iterations, nr_cpu_ids - cpumask_weight(&mask_empt= y), + for_each_cpu_not(cpu, &mask_empty)); + FOR_EACH_ITER_EQ(test, iterations, cpumask_weight(&mask_empty), + for_each_cpu_wrap(cpu, &mask_empty, nr_cpu_ids / 2)); + + FOR_EACH_ITER_EQ(test, iterations, cpumask_weight(cpu_possible_mask), + for_each_cpu(cpu, cpu_possible_mask)); + FOR_EACH_ITER_EQ(test, iterations, nr_cpu_ids - cpumask_weight(cpu_possib= le_mask), + for_each_cpu_not(cpu, cpu_possible_mask)); + FOR_EACH_ITER_EQ(test, iterations, cpumask_weight(cpu_possible_mask), + for_each_cpu_wrap(cpu, cpu_possible_mask, nr_cpu_ids / 2)); +} + +static void test_cpumask_iterators_builtin(struct kunit *test) +{ + unsigned int iterations; + int cpu; + + FOR_EACH_ITER_EQ(test, iterations, nr_cpu_ids, + for_each_possible_cpu(cpu)); + FOR_EACH_ITER_EQ(test, iterations, cpumask_weight(cpu_online_mask), + for_each_online_cpu(cpu)); + FOR_EACH_ITER_EQ(test, iterations, cpumask_weight(cpu_present_mask), + for_each_present_cpu(cpu)); +} + +static int test_cpumask_init(struct kunit *test) +{ + cpumask_clear(&mask_empty); + cpumask_setall(&mask_all); + + return 0; +} + +static struct kunit_case test_cpumask_cases[] =3D { + KUNIT_CASE(test_cpumask_weight), + KUNIT_CASE(test_cpumask_first), + KUNIT_CASE(test_cpumask_last), + KUNIT_CASE(test_cpumask_next), + KUNIT_CASE(test_cpumask_iterators), + KUNIT_CASE(test_cpumask_iterators_builtin), + {} +}; + +static struct kunit_suite test_cpumask_suite =3D { + .name =3D "cpumask", + .init =3D test_cpumask_init, + .test_cases =3D test_cpumask_cases, +}; +kunit_test_suite(test_cpumask_suite); + +MODULE_LICENSE("GPL"); --=20 2.36.1 From nobody Tue Apr 28 06:24:52 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47A94C433EF for ; Sat, 4 Jun 2022 17:16:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239211AbiFDRQc (ORCPT ); Sat, 4 Jun 2022 13:16:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239101AbiFDRQS (ORCPT ); Sat, 4 Jun 2022 13:16:18 -0400 Received: from polaris.svanheule.net (polaris.svanheule.net [IPv6:2a00:c98:2060:a004:1::200]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D805286D1 for ; Sat, 4 Jun 2022 10:16:15 -0700 (PDT) Received: from localhost.localdomain (unknown [IPv6:2a02:a03f:eaf9:8401:aa9f:5d01:1b2a:e3cd]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sander@svanheule.net) by polaris.svanheule.net (Postfix) with ESMTPSA id 68DB72E2937; Sat, 4 Jun 2022 19:16:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svanheule.net; s=mail1707; t=1654362970; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m2edoHbj3wrWKOg2SiGaDrZ7TOXtCWMXUcn2SYbbj2U=; b=blIRla83hu1Vw22ajDQCQ3BDXtcp31wGfrbx8llgAVyd+JWN64wWfbTY32RQZF2IfOkCb+ JFVmudFlFR6xvaE4hqMFWtyLr3OwTeFdt5jO90LmSez+27FqyOr7i9G8PkAykA28VKgPJC 0SSAgnCt4sofVoZ2nefv8dAp15dUCK+2tX57xhPAA0//2rQsiBeB5vk3cC4UMsQ8fzOIKI iY0mUsUpZantU4OQm5Q9Ru0ppy5F14EgPk0H6UGJyho28sio2DSJh71U0ID2Ywbt6shydk 7SA2+KRAXRrj8LU7UPCWO19fqPTMsyV7qMZJiykK/SoyrntKsyXonU5sarDWyw== From: Sander Vanheule To: Peter Zijlstra , Yury Norov , Andrew Morton , Valentin Schneider , Thomas Gleixner , Greg Kroah-Hartman , Marco Elver Cc: linux-kernel@vger.kernel.org, Andy Shevchenko , Sander Vanheule Subject: [PATCH v2 2/4] cpumask: Fix invalid uniprocessor mask assumption Date: Sat, 4 Jun 2022 19:15:57 +0200 Message-Id: <9971a47294f5f414c4f33091f5277307e0099355.1654362935.git.sander@svanheule.net> X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On uniprocessor builds, any CPU mask is assumed to contain exactly one CPU (cpu0). This assumption ignores the existence of empty masks, for which the optimised implementations result in incorrect behaviour, and causes the tests to fail: [...] # Subtest: cpumask [...] 1..6 [...] ok 1 - test_cpumask_weight [...] # test_cpumask_first: ASSERTION FAILED at lib/test_cpumask.c:35 [...] Expected 1U <=3D cpumask_first(&mask_empty), but [...] 1U =3D=3D 1 [...] cpumask_first(&mask_empty) =3D=3D 0 [...] not ok 2 - test_cpumask_first [...] # test_cpumask_last: ASSERTION FAILED at lib/test_cpumask.c:44 [...] Expected ((unsigned int)1) <=3D cpumask_last(&mask_empty), but [...] ((unsigned int)1) =3D=3D 1 [...] cpumask_last(&mask_empty) =3D=3D 0 [...] not ok 3 - test_cpumask_last [...] # test_cpumask_next: ASSERTION FAILED at lib/test_cpumask.c:51 [...] Expected 1U <=3D cpumask_next_zero(-1, ((const struct cpumask *)&= __cpu_possible_mask)), but [...] 1U =3D=3D 1 [...] cpumask_next_zero(-1, ((const struct cpumask *)&__cpu_possibl= e_mask)) =3D=3D 0 [...] not ok 4 - test_cpumask_next [...] # test_cpumask_iterators: EXPECTATION FAILED at lib/test_cpumask.= c:62 [...] Expected (cpumask_weight(&mask_empty)) =3D=3D (iterations), but [...] (cpumask_weight(&mask_empty)) =3D=3D 0 [...] (iterations) =3D=3D 1 [...] # test_cpumask_iterators: EXPECTATION FAILED at lib/test_cpumask.= c:66 [...] Expected (cpumask_weight(&mask_empty)) =3D=3D (iterations), but [...] (cpumask_weight(&mask_empty)) =3D=3D 0 [...] (iterations) =3D=3D 1 [...] # test_cpumask_iterators: EXPECTATION FAILED at lib/test_cpumask.= c:71 [...] Expected (1U - cpumask_weight(((const struct cpumask *)&__cpu_pos= sible_mask))) =3D=3D (iterations), but [...] (1U - cpumask_weight(((const struct cpumask *)&__cpu_possible= _mask))) =3D=3D 0 [...] (iterations) =3D=3D 1 [...] not ok 5 - test_cpumask_iterators [...] ok 6 - test_cpumask_iterators_builtin Drop the incorrectly optimised code and use the generic implementations in all cases. Signed-off-by: Sander Vanheule --- include/linux/cpumask.h | 80 ----------------------------------------- lib/Makefile | 3 +- 2 files changed, 1 insertion(+), 82 deletions(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index fe29ac7cc469..d6add0e29ef4 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -116,85 +116,6 @@ static __always_inline unsigned int cpumask_check(unsi= gned int cpu) return cpu; } =20 -#if NR_CPUS =3D=3D 1 -/* Uniprocessor. Assume all masks are "1". */ -static inline unsigned int cpumask_first(const struct cpumask *srcp) -{ - return 0; -} - -static inline unsigned int cpumask_first_zero(const struct cpumask *srcp) -{ - return 0; -} - -static inline unsigned int cpumask_first_and(const struct cpumask *srcp1, - const struct cpumask *srcp2) -{ - return 0; -} - -static inline unsigned int cpumask_last(const struct cpumask *srcp) -{ - return 0; -} - -/* Valid inputs for n are -1 and 0. */ -static inline unsigned int cpumask_next(int n, const struct cpumask *srcp) -{ - return n+1; -} - -static inline unsigned int cpumask_next_zero(int n, const struct cpumask *= srcp) -{ - return n+1; -} - -static inline unsigned int cpumask_next_and(int n, - const struct cpumask *srcp, - const struct cpumask *andp) -{ - return n+1; -} - -static inline unsigned int cpumask_next_wrap(int n, const struct cpumask *= mask, - int start, bool wrap) -{ - /* cpu0 unless stop condition, wrap and at cpu0, then nr_cpumask_bits */ - return (wrap && n =3D=3D 0); -} - -/* cpu must be a valid cpu, ie 0, so there's no other choice. */ -static inline unsigned int cpumask_any_but(const struct cpumask *mask, - unsigned int cpu) -{ - return 1; -} - -static inline unsigned int cpumask_local_spread(unsigned int i, int node) -{ - return 0; -} - -static inline int cpumask_any_and_distribute(const struct cpumask *src1p, - const struct cpumask *src2p) { - return cpumask_first_and(src1p, src2p); -} - -static inline int cpumask_any_distribute(const struct cpumask *srcp) -{ - return cpumask_first(srcp); -} - -#define for_each_cpu(cpu, mask) \ - for ((cpu) =3D 0; (cpu) < 1; (cpu)++, (void)mask) -#define for_each_cpu_not(cpu, mask) \ - for ((cpu) =3D 0; (cpu) < 1; (cpu)++, (void)mask) -#define for_each_cpu_wrap(cpu, mask, start) \ - for ((cpu) =3D 0; (cpu) < 1; (cpu)++, (void)mask, (void)(start)) -#define for_each_cpu_and(cpu, mask1, mask2) \ - for ((cpu) =3D 0; (cpu) < 1; (cpu)++, (void)mask1, (void)mask2) -#else /** * cpumask_first - get the first cpu in a cpumask * @srcp: the cpumask pointer @@ -324,7 +245,6 @@ extern int cpumask_next_wrap(int n, const struct cpumas= k *mask, int start, bool for ((cpu) =3D -1; \ (cpu) =3D cpumask_next_and((cpu), (mask1), (mask2)), \ (cpu) < nr_cpu_ids;) -#endif /* SMP */ =20 #define CPU_BITS_NONE \ { \ diff --git a/lib/Makefile b/lib/Makefile index 81f944cd74ae..5abd7b2064f1 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -34,10 +34,9 @@ lib-y :=3D ctype.o string.o vsprintf.o cmdline.o \ is_single_threaded.o plist.o decompress.o kobject_uevent.o \ earlycpio.o seq_buf.o siphash.o dec_and_lock.o \ nmi_backtrace.o nodemask.o win_minmax.o memcat_p.o \ - buildid.o + buildid.o cpumask.o =20 lib-$(CONFIG_PRINTK) +=3D dump_stack.o -lib-$(CONFIG_SMP) +=3D cpumask.o =20 lib-y +=3D kobject.o klist.o obj-y +=3D lockref.o --=20 2.36.1 From nobody Tue Apr 28 06:24:52 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD2AACCA477 for ; Sat, 4 Jun 2022 17:16:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239147AbiFDRQ2 (ORCPT ); Sat, 4 Jun 2022 13:16:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239112AbiFDRQS (ORCPT ); Sat, 4 Jun 2022 13:16:18 -0400 Received: from polaris.svanheule.net (polaris.svanheule.net [IPv6:2a00:c98:2060:a004:1::200]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EDDA286D4 for ; Sat, 4 Jun 2022 10:16:15 -0700 (PDT) Received: from localhost.localdomain (unknown [IPv6:2a02:a03f:eaf9:8401:aa9f:5d01:1b2a:e3cd]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sander@svanheule.net) by polaris.svanheule.net (Postfix) with ESMTPSA id 075EA2E2938; Sat, 4 Jun 2022 19:16:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svanheule.net; s=mail1707; t=1654362971; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UmyaFQVp9aEpKb+oKQXkJfXH9xvYY1MiNgw55GvfXww=; b=pUIveiGWzRZte8JuDbyM2ORSh0Kmi3hAwOlQkeetVK6iOYPU2t9vhdoW593X229yyhBlKg to/yhjn49TGAIXI6AFcksajLK1YMlacmpIyK2o2f+JPYW0QZWbJ/OL2ldkDwAoo40fLzG+ 664494gWmYXdGiQxm8VOFqjCVw+p1ssz3oaFCtonw59fJXJ2TZ1cqPHn5Qvn2dVxb7h18s xHmRqL+FY9HVMUXf7FQQqfNtCRev0IvRDWi0jLykUmpI9gc7OJfMKyhtgvNDsCVxDKUMU9 jOOqWvUXyZAeeCviE1l6BCgr6+MS8/nUAvh87Ev80SkcgEXFHZGPul0tY/Xcmg== From: Sander Vanheule To: Peter Zijlstra , Yury Norov , Andrew Morton , Valentin Schneider , Thomas Gleixner , Greg Kroah-Hartman , Marco Elver Cc: linux-kernel@vger.kernel.org, Andy Shevchenko , Sander Vanheule Subject: [PATCH v2 3/4] cpumask: Add UP optimised for_each_*_cpu versions Date: Sat, 4 Jun 2022 19:15:58 +0200 Message-Id: X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On uniprocessor builds, the following loops will always run over a mask that contains one enabled CPU (cpu0): - for_each_possible_cpu - for_each_online_cpu - for_each_present_cpu Provide uniprocessor-specific macros for these loops, that always run exactly once. Signed-off-by: Sander Vanheule --- include/linux/cpumask.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index d6add0e29ef4..7ccddbc27ac3 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -731,9 +731,16 @@ extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS); /* First bits of cpu_bit_bitmap are in fact unset. */ #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0]) =20 +#if NR_CPUS =3D=3D 1 +/* Uniprocessor: the possible/online/present masks are always "1" */ +#define for_each_possible_cpu(cpu) for ((cpu) =3D 0; (cpu) < 1; (cpu)++) +#define for_each_online_cpu(cpu) for ((cpu) =3D 0; (cpu) < 1; (cpu)++) +#define for_each_present_cpu(cpu) for ((cpu) =3D 0; (cpu) < 1; (cpu)++) +#else #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask) #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask) #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask) +#endif =20 /* Wrappers for arch boot code to manipulate normally-constant masks */ void init_cpu_present(const struct cpumask *src); --=20 2.36.1 From nobody Tue Apr 28 06:24:52 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B236FC433EF for ; Sat, 4 Jun 2022 17:16:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239242AbiFDRQf (ORCPT ); Sat, 4 Jun 2022 13:16:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239119AbiFDRQS (ORCPT ); Sat, 4 Jun 2022 13:16:18 -0400 Received: from polaris.svanheule.net (polaris.svanheule.net [84.16.241.116]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BFB2286D6 for ; Sat, 4 Jun 2022 10:16:15 -0700 (PDT) Received: from localhost.localdomain (unknown [IPv6:2a02:a03f:eaf9:8401:aa9f:5d01:1b2a:e3cd]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sander@svanheule.net) by polaris.svanheule.net (Postfix) with ESMTPSA id 8F80E2E2939; Sat, 4 Jun 2022 19:16:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svanheule.net; s=mail1707; t=1654362971; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=43KXIoq1D6bklJo+f5FH1Gs3lknZQq9mQVsSDG8wsAw=; b=sFX2aPP2xERNo6JHr1+Q0XRUXA8EPdR31koEmHzTMj1I15c6TMH/DlcjH6+gbwrk3axLth n7MNOjTpHUVg+320V9mk8A9zc3nPxvx4d8NemPyu1gtC3Qfext1uShxyPFe2MLIphRiwIE UpJHYla+HxRexNDRy7HLs0zaeZJJbH/Oc8nInnM2z8YvIutc0tXcOtABsc6c2SDYzBAGxD 8imeEYnOpJp12VVgOmmq0KzXEPQ6o5jLz3IvgoOPJe3s2FEpYpvZ0indQxNmm8MwI44onk 88udotzaZSdDwVDLVHNMOWezmb5laak1zC0xsJEsR0WDSNxkhOhSPjNTaJw7EQ== From: Sander Vanheule To: Peter Zijlstra , Yury Norov , Andrew Morton , Valentin Schneider , Thomas Gleixner , Greg Kroah-Hartman , Marco Elver Cc: linux-kernel@vger.kernel.org, Andy Shevchenko , Sander Vanheule Subject: [PATCH v2 4/4] cpumask: Update cpumask_next_wrap() signature Date: Sat, 4 Jun 2022 19:15:59 +0200 Message-Id: X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The extern specifier is not needed for this declaration, so drop it. The function also depends only on the input parameters, and has no side effects, so it can be marked __pure like other functions in cpumask.h Signed-off-by: Sander Vanheule --- include/linux/cpumask.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 7ccddbc27ac3..f37ce00741a3 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -210,7 +210,7 @@ int cpumask_any_distribute(const struct cpumask *srcp); (cpu) =3D cpumask_next_zero((cpu), (mask)), \ (cpu) < nr_cpu_ids;) =20 -extern int cpumask_next_wrap(int n, const struct cpumask *mask, int start,= bool wrap); +int __pure cpumask_next_wrap(int n, const struct cpumask *mask, int start,= bool wrap); =20 /** * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a spe= cified location --=20 2.36.1