From nobody Thu Apr 9 12:32:06 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 29D7BC0502C for ; Mon, 29 Aug 2022 16:58:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231144AbiH2Q6N (ORCPT ); Mon, 29 Aug 2022 12:58:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229798AbiH2Q5x (ORCPT ); Mon, 29 Aug 2022 12:57:53 -0400 Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA8189A994 for ; Mon, 29 Aug 2022 09:57:52 -0700 (PDT) Received: by mail-qk1-x72b.google.com with SMTP id m5so6501955qkk.1 for ; Mon, 29 Aug 2022 09:57:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=hvI2niVAJoh8fUEWHOj2VZhpKK/Cae+ERjbEGQgj2F0=; b=gVN4m3vzK5CCR0qPbV8PX246fCclVzWSB3Ccu4giC6PqzaJkt8gmrEMqT9q5/RoAGR TeTCJG3JX5vrchahPvBTzFTuKmJQ4tNLJsQF8sHCCKUGLtreGy7mYeJPyBF8EVDpFmZq jEHvzwOmCwKW21eJdBHA+XxcIXOsiDpK1NvxtmU9kvPI+Jy9s02M4pGeSateSLOkA7kn Fdj6/0tjhmI1RSskJNjmqjyeT8ApSoy+THP7ysi41fCMRqAe99Ci/n2UNDSOqDQeqWlX WrH6g2wwNvAKOTGlCAKQUTJcQ4//9WlV9pJ2nv1Qrf7QPHGRSwgnf6kGCztG9v8v7pJY h2YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=hvI2niVAJoh8fUEWHOj2VZhpKK/Cae+ERjbEGQgj2F0=; b=qN/mb4Mye93DLLq+VjsQca7CQzfhBImAkE104j3cONjPp6BWTfwJ1iFzY0VbNfNL7y zkxf0/BABmyaF3L6fzdEj7J8joHARX65mTaTplKIPPbxqKzHVN1r5sLeh0Kt0g+ace4p /NLm8w4P6lSPe4wbd/G/MznEC2GLHSWUu32pffc1ObXlzu4G5wZgXBCwg1bvJdaWvWK3 mbUO8QE5H7JbLf0MGvHj1xhIsIrm95k0+F4NQcdPjpNhFse45EiWiWiocf7GYF/4g0xl 5TGUeEBShaBI1udE3DR4HIxCxBZaPkyu9D2gWfHDAZ030ta8Pby+RNlTPdsoYIkcKJK9 QSUg== X-Gm-Message-State: ACgBeo38J2HAQjM0O/Airpv839l61HA7FCEG16NveI5ON2I8SMKHKF9J EZ2jv1fNp8i9i1QKiv8TO+PNqwW4/Xs= X-Google-Smtp-Source: AA6agR7D5NX+4JNVsTlQTiGX3lGmlai40N4SbIGYPtOQ8DkSFYmt/Abs5KTNL2OrN8ta9XEvelUnTQ== X-Received: by 2002:a05:620a:170c:b0:6bb:208c:d276 with SMTP id az12-20020a05620a170c00b006bb208cd276mr9037223qkb.539.1661792271700; Mon, 29 Aug 2022 09:57:51 -0700 (PDT) Received: from localhost ([2601:4c1:c100:2270:5d44:81a:c7bc:27d1]) by smtp.gmail.com with ESMTPSA id bj4-20020a05620a190400b006b5c061844fsm6305404qkb.49.2022.08.29.09.57.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 09:57:51 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Rasmus Villemoes , Andrew Morton , Stephen Rothwell , Peter Zijlstra , Thomas Gleixner , "Paul E . McKenney" , Vlastimil Babka , Dmitry Vyukov , Valentin Schneider , Sander Vanheule , Alexey Klimov , Eric Biggers Subject: [PATCH 1/4] smp: add set_nr_cpu_ids() Date: Mon, 29 Aug 2022 09:57:45 -0700 Message-Id: <20220829165748.1008477-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220829165748.1008477-1-yury.norov@gmail.com> References: <20220829165748.1008477-1-yury.norov@gmail.com> 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" In preparation for support of compile-time nr_cpu_ids, add a setter for the variable. This is a no-op for all arches. Signed-off-by: Yury Norov --- arch/loongarch/kernel/setup.c | 2 +- arch/mips/kernel/setup.c | 2 +- arch/x86/kernel/smpboot.c | 4 ++-- arch/x86/xen/smp_pv.c | 2 +- include/linux/smp.h | 5 +++++ kernel/smp.c | 4 ++-- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c index 8f5c2f9a1a83..18a81edd3ac5 100644 --- a/arch/loongarch/kernel/setup.c +++ b/arch/loongarch/kernel/setup.c @@ -346,7 +346,7 @@ static void __init prefill_possible_map(void) for (; i < NR_CPUS; i++) set_cpu_possible(i, false); =20 - nr_cpu_ids =3D possible; + set_nr_cpu_ids(possible); } #endif =20 diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c index 2ca156a5b231..e8a0759cb4d0 100644 --- a/arch/mips/kernel/setup.c +++ b/arch/mips/kernel/setup.c @@ -750,7 +750,7 @@ static void __init prefill_possible_map(void) for (; i < NR_CPUS; i++) set_cpu_possible(i, false); =20 - nr_cpu_ids =3D possible; + set_nr_cpu_ids(possible); } #else static inline void prefill_possible_map(void) {} diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c index f24227bc3220..3f3ea0287f69 100644 --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -1316,7 +1316,7 @@ static void __init smp_sanity_check(void) nr++; } =20 - nr_cpu_ids =3D 8; + set_nr_cpu_ids(8); } #endif =20 @@ -1569,7 +1569,7 @@ __init void prefill_possible_map(void) possible =3D i; } =20 - nr_cpu_ids =3D possible; + set_nr_cpu_ids(possible); =20 pr_info("Allowing %d CPUs, %d hotplug CPUs\n", possible, max_t(int, possible - num_processors, 0)); diff --git a/arch/x86/xen/smp_pv.c b/arch/x86/xen/smp_pv.c index ba7af2eca755..480be82e9b7b 100644 --- a/arch/x86/xen/smp_pv.c +++ b/arch/x86/xen/smp_pv.c @@ -179,7 +179,7 @@ static void __init _get_smp_config(unsigned int early) * hypercall to expand the max number of VCPUs an already * running guest has. So cap it up to X. */ if (subtract) - nr_cpu_ids =3D nr_cpu_ids - subtract; + set_nr_cpu_ids(nr_cpu_ids - subtract); #endif =20 } diff --git a/include/linux/smp.h b/include/linux/smp.h index a80ab58ae3f1..c9e7a9abef2f 100644 --- a/include/linux/smp.h +++ b/include/linux/smp.h @@ -181,6 +181,11 @@ static inline int get_boot_cpu_id(void) return __boot_cpu_id; } =20 +static inline void set_nr_cpu_ids(unsigned int nr) +{ + nr_cpu_ids =3D nr; +} + #else /* !SMP */ =20 static inline void smp_send_stop(void) { } diff --git a/kernel/smp.c b/kernel/smp.c index 650810a6f29b..27d4ed0aee55 100644 --- a/kernel/smp.c +++ b/kernel/smp.c @@ -1070,7 +1070,7 @@ static int __init nrcpus(char *str) int nr_cpus; =20 if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids) - nr_cpu_ids =3D nr_cpus; + set_nr_cpu_ids(nr_cpus); =20 return 0; } @@ -1095,7 +1095,7 @@ EXPORT_SYMBOL(nr_cpu_ids); /* An arch may set nr_cpu_ids earlier if needed, so this would be redundan= t */ void __init setup_nr_cpu_ids(void) { - nr_cpu_ids =3D find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1; + set_nr_cpu_ids(find_last_bit(cpumask_bits(cpu_possible_mask), NR_CPUS) + = 1); } =20 /* Called by boot processor to activate the rest. */ --=20 2.34.1 From nobody Thu Apr 9 12:32:06 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 4C301ECAAD5 for ; Mon, 29 Aug 2022 16:58:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231192AbiH2Q6Z (ORCPT ); Mon, 29 Aug 2022 12:58:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231150AbiH2Q5y (ORCPT ); Mon, 29 Aug 2022 12:57:54 -0400 Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D08F39A9A8 for ; Mon, 29 Aug 2022 09:57:53 -0700 (PDT) Received: by mail-qv1-xf32.google.com with SMTP id d1so6785091qvs.0 for ; Mon, 29 Aug 2022 09:57:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=2yr+D2Z29fm9kNcmJTj1HTUfj6bYsT7aNdzDoDu4Xao=; b=aSJhhvppoLOB6ONuv9L4JT71wLnlDbFq4KocMuPpcLUo4xQKpg13BQKZXekAECmTCE rS4MdiDt+MzI2Rnt7WhpFZ2IJFw/FEurV0dhPBUR9UTHGXwYoFvliQg0eypzjaeLK53g D4s6kzlK8rduE56YR2+jhOPKJlVrGX/WKfb3mj9qCqn1zDm9nsEESfmb319Q+fUdRBsO PcAV2NUxBnigcKfJV3vCw5YLFgLuwujZHl47BONWFlDmYrAd1JqwnmSOY1ZovfWJ/jvc qQedichT1M940tLrhnVKf9bm+4TIeR9NqQ6xl5g+E18eoxUJThGdqarbvdwl6P+q5+0q 3LGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=2yr+D2Z29fm9kNcmJTj1HTUfj6bYsT7aNdzDoDu4Xao=; b=V7IG3TTsTe3hrmUoBs62fAnv/ja7gNOL+kvfroHbbSipAd9/UuHcyTnCKSTtrA0eQt KncYMtc0bJUR5968RNzn01+y8yt4m+O86EdK3ZDTx2HUouQQrmq1dTJZYyl/dJjNT1yn qkHJ6kqP9k2NYXmSw4u8fXzXSiyhbBY0Y++YMGHkYo3QYN4+qLPGAMmw2r6J7Qu6zbBa Zy3r8IaPDGEUUCaqNad5lZ3a9Qn5AMkzD+UzbFu39sC+LjOT5HC8FAkYZTX9l/UeY5OU LGg3tN5ZlczhQKXxuUFQOxg/t7n7dvNsrrEuDiPkStVL5CWpdpcebfNtGS4jYiLKDWO8 bV6w== X-Gm-Message-State: ACgBeo0sqdcv3WURYdAYN/L8I8tyEoV3x3isUXcfQB/HbzRyDucdmjb+ P801GhcJx7w5psmoNULSUa3THOXayao= X-Google-Smtp-Source: AA6agR70/L2QQF1ATcyW9dJWSu3GjTEPOXrFklOHn8IearbWOsTPA6LLLD5k3ft+hnipvZIO3McLVw== X-Received: by 2002:a05:6214:2409:b0:499:606:153f with SMTP id fv9-20020a056214240900b004990606153fmr3076912qvb.20.1661792272804; Mon, 29 Aug 2022 09:57:52 -0700 (PDT) Received: from localhost ([2601:4c1:c100:2270:5d44:81a:c7bc:27d1]) by smtp.gmail.com with ESMTPSA id he7-20020a05622a600700b00344f7cf05b4sm5488592qtb.14.2022.08.29.09.57.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 09:57:52 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Rasmus Villemoes , Andrew Morton , Stephen Rothwell , Peter Zijlstra , Thomas Gleixner , "Paul E . McKenney" , Vlastimil Babka , Dmitry Vyukov , Valentin Schneider , Sander Vanheule , Alexey Klimov , Eric Biggers Subject: [PATCH 2/4] lib/cpumask: delete misleading comment Date: Mon, 29 Aug 2022 09:57:46 -0700 Message-Id: <20220829165748.1008477-3-yury.norov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220829165748.1008477-1-yury.norov@gmail.com> References: <20220829165748.1008477-1-yury.norov@gmail.com> 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 comment says that HOTPLUG config option enables all cpus in cpu_possible_mask up to NR_CPUs. This is wrong. Even if HOTPLUG is enabled, the mask is populated on boot with respect to ACPI/DT records. Signed-off-by: Yury Norov --- include/linux/cpumask.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index bd047864c7ac..f70bf9ffa9d3 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -67,10 +67,6 @@ extern unsigned int nr_cpu_ids; * cpu_online_mask is the dynamic subset of cpu_present_mask, * indicating those CPUs available for scheduling. * - * If HOTPLUG is enabled, then cpu_possible_mask is forced to have - * all NR_CPUS bits set, otherwise it is just the set of CPUs that - * ACPI reports present at boot. - * * If HOTPLUG is enabled, then cpu_present_mask varies dynamically, * depending on what ACPI reports as currently plugged in, otherwise * cpu_present_mask is just a copy of cpu_possible_mask. --=20 2.34.1 From nobody Thu Apr 9 12:32:06 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 0454BECAAD5 for ; Mon, 29 Aug 2022 16:58:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231177AbiH2Q6Q (ORCPT ); Mon, 29 Aug 2022 12:58:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231151AbiH2Q55 (ORCPT ); Mon, 29 Aug 2022 12:57:57 -0400 Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4E4D9A9AD for ; Mon, 29 Aug 2022 09:57:54 -0700 (PDT) Received: by mail-qk1-x72a.google.com with SMTP id s22so4003430qkj.3 for ; Mon, 29 Aug 2022 09:57:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=nxyi/lkNkq6gnwZzVfM6b9wH0jEP3m9DYKXN3rYV0f4=; b=hdErUyBmH3jAdvJY7oq9Oyjz3AeiA97KYXXVCmmfFF+0AxX7XKd5Q4GLp5gOYHsoIr ux3qjuCpV/IowDld0/tbVMLWrjTBpZs7JMNa6lyAO+1l/uWppuPwFruzgFuOu5v1Uubm DI/t5ZBW5qm2CiyniShfBJ0FM1iZxTa4MGoKFoHRfEKJexK+IyA2GuWAP8hMqpqHM9nl Z0UQ2A88beTZ8OIdoaoEudFKUmOybJnwQTH1LSzyXzKITAQYwdotGn3EvWtIS2Bf6UA2 H9E6nnR83P4RymJ75b+HxuDoKePRIxNxFyG7k2ZFrUGgDLX5w8XDHRfZLwyFRkGDQ4N3 09mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=nxyi/lkNkq6gnwZzVfM6b9wH0jEP3m9DYKXN3rYV0f4=; b=j5cENjq0VX7GTGIh7f4q6F0v9ktdX0zBZeOqEncEvF7EBrA+kIEO6IZXlN6K5095dd wy7KwhIh4aKoOP6lIcU7CxiWVP4XLvHOLkBnmJKuUEXE1ZZzmcDRdVvXxl5uj3odmbH3 hEPqjL6O9TLj7ydbWjVID4d2FocE6qX/EOeoAEmgNmyh/Vl8m4Zh4PuTJGW4XVT6E+4I 2qRClcP6g8XPyPeb/5C/2TLYRdABwcKaHm6cBE/rgvFE0IaEVCdrT81xteSL0HQFZom6 mnWPbaeWHJwT1kZg2D5BzGYJjw8gkMGP9w5okuQlueQ/Fkqpf9EPlGLjjPKAzgmAD77R XoaA== X-Gm-Message-State: ACgBeo0EaUekEh/CbCDNANK+uVnPKFnOy5liORjRb0xBXeOnmu/z7lsL xzzHROzcsp8J0X77VZnAV60OmbpDKu8= X-Google-Smtp-Source: AA6agR5/m5z8JvXmhzWj58EUn4m+KfVf5R5M4QqVKIdiELD2vpOyCPXyTQV1xeTxsvPGJ4AbNN6pHQ== X-Received: by 2002:a05:620a:842:b0:6bb:b876:35d0 with SMTP id u2-20020a05620a084200b006bbb87635d0mr8872940qku.163.1661792273735; Mon, 29 Aug 2022 09:57:53 -0700 (PDT) Received: from localhost ([2601:4c1:c100:2270:5d44:81a:c7bc:27d1]) by smtp.gmail.com with ESMTPSA id 14-20020ac84e8e000000b003436103df40sm5597027qtp.8.2022.08.29.09.57.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 09:57:53 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Rasmus Villemoes , Andrew Morton , Stephen Rothwell , Peter Zijlstra , Thomas Gleixner , "Paul E . McKenney" , Vlastimil Babka , Dmitry Vyukov , Valentin Schneider , Sander Vanheule , Alexey Klimov , Eric Biggers Subject: [PATCH 3/4] lib/cpumask: deprecate nr_cpumask_bits Date: Mon, 29 Aug 2022 09:57:47 -0700 Message-Id: <20220829165748.1008477-4-yury.norov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220829165748.1008477-1-yury.norov@gmail.com> References: <20220829165748.1008477-1-yury.norov@gmail.com> 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" Cpumask code is written in assumption that when CONFIG_CPUMASK_OFFSTACK is enabled, all cpumasks have boot-time defined size, otherwise the size is always NR_CPUS. The latter is wrong because the number of possible cpus is always calculated on boot, and it may be less than NR_CPUS. On my 4-cpu arm64 VM the nr_cpu_ids is 4, as expected, and nr_cpumask_bits is 256, which corresponds to NR_CPUS. This not only leads to useless traversing of cpumask bits greater than 4, this also makes some cpumask routines fail. For example, cpumask_full(0b1111000..000) would erroneously return false in the example above because tail bits in the mask are all unset. This patch deprecates nr_cpumask_bits and wires it to nr_cpu_ids unconditionally, so that cpumask routines will not waste time traversing unused part of cpu masks. It also fixes cpumask_full() and similar routines. As a side effect, because now a length of cpumasks is defined at run-time even if CPUMASK_OFFSTACK is disabled, compiler can't optimize corresponding functions. It increases kernel size by ~2.5KB if OFFSTACK is off. This is addressed in the following patch. Signed-off-by: Yury Norov --- include/linux/cpumask.h | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index f70bf9ffa9d3..b7b863cf8831 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -41,13 +41,8 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); = } cpumask_t; extern unsigned int nr_cpu_ids; #endif =20 -#ifdef CONFIG_CPUMASK_OFFSTACK -/* Assuming NR_CPUS is huge, a runtime limit is more efficient. Also, - * not all bits may be allocated. */ +/* Deprecated. Always use nr_cpu_ids. */ #define nr_cpumask_bits nr_cpu_ids -#else -#define nr_cpumask_bits ((unsigned int)NR_CPUS) -#endif =20 /* * The following particular system cpumasks and operations manage --=20 2.34.1 From nobody Thu Apr 9 12:32:06 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 4A1E9ECAAD2 for ; Mon, 29 Aug 2022 16:58:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231183AbiH2Q6W (ORCPT ); Mon, 29 Aug 2022 12:58:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231156AbiH2Q6B (ORCPT ); Mon, 29 Aug 2022 12:58:01 -0400 Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFA109A9AE for ; Mon, 29 Aug 2022 09:57:55 -0700 (PDT) Received: by mail-qk1-x72d.google.com with SMTP id s22so4003468qkj.3 for ; Mon, 29 Aug 2022 09:57:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=phNzhxWfsmUQ2u7NLzuDZu8A19if4dC4wAlhFRDjgx4=; b=pNuX9maN4/h701mSr5cZiAKMg+Y4R+518jxFEZ4jK4aoEPcgsuA6dS++6Grn3kF54E no/uVarUCSOjLouaLxW2XWKCTgmRcD7UKehC6mu96BXhsbhP7wcCM3IYEUmyoN7Zyiqx 3EKE92GPYujfZGVu3GqCypXcofzIXNOJ1XIDxi5tecVDWmNDICrgjCfZVPRvK4LMoYeY LPbzmjdY8EqQUykP7iyotqfGeTThhAIVPJLCrJD8Nq/BPfJTDsZMCk6EMeSsCgOQzpfN SEQPDDu6vBwnZs4SMLcjWz3bv206+4//w7uX9cTVsuMqSrs4/9hkm61+4hfOVriARlJE UO3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=phNzhxWfsmUQ2u7NLzuDZu8A19if4dC4wAlhFRDjgx4=; b=cyQNXwa9ccqJKfdjGyMOCRFHE921KQSJ+8dzg/nfoYjhyj13P7hN2wCuoN6LT6XHjO +iM1HSd0caaKLLmsQAgRWw85mDFkLnzeDzvCBWm/xPAPeuii9BWk+5nRKAxG6AQ6IkRA PhicTjvefShqNv/RYYGwdpTfm+Hm8fe1YYuQTW3liahTTz57KuBAhYDS6Ay5NKPkZBEU XVmn4vjve6n7ia9A4IN+i6b3hwEFeZrG7xUbsaP9xOUtYrNnVgbdmUNHAEyKun5rFyzd JA9y+QZwixVnv5wEoq2erxDorT099tMKdoFKLKWxLrka1rTDiHl2y8eV08azXaKLcEji Gdmw== X-Gm-Message-State: ACgBeo0k9iZQTG3f/c+baEME16Ej7Fga0/aeOenNMpjD7VKOC6lUrYM3 5mT2gwHIt3xVS/WA8/czGrA6jJy603E= X-Google-Smtp-Source: AA6agR4wZ0WnSV9MSswRQWWLaVE/5Zq5kAuCMk60jvZ6+eIlb5ARgxFruBm0Y870qguCzIrKfYgufA== X-Received: by 2002:a05:620a:29c3:b0:6bb:33ac:9130 with SMTP id s3-20020a05620a29c300b006bb33ac9130mr9458644qkp.701.1661792274783; Mon, 29 Aug 2022 09:57:54 -0700 (PDT) Received: from localhost ([2601:4c1:c100:2270:5d44:81a:c7bc:27d1]) by smtp.gmail.com with ESMTPSA id l7-20020a05620a28c700b006a793bde241sm6119716qkp.63.2022.08.29.09.57.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 09:57:54 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Rasmus Villemoes , Andrew Morton , Stephen Rothwell , Peter Zijlstra , Thomas Gleixner , "Paul E . McKenney" , Vlastimil Babka , Dmitry Vyukov , Valentin Schneider , Sander Vanheule , Alexey Klimov , Eric Biggers Subject: [PATCH 4/4] lib/cpumask: add FORCE_NR_CPUS config option Date: Mon, 29 Aug 2022 09:57:48 -0700 Message-Id: <20220829165748.1008477-5-yury.norov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220829165748.1008477-1-yury.norov@gmail.com> References: <20220829165748.1008477-1-yury.norov@gmail.com> 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 size of cpumasks is hard-limited by compile-time parameter NR_CPUS, but defined at boot-time when kernel parses ACPI/DT tables, and stored in nr_cpu_ids. In many practical cases, number of CPUs for a target is known at compile time, and can be provided with NR_CPUS. In that case, compiler may be instructed to rely on NR_CPUS as on actual number of CPUs, not an upper limit. It allows to optimize many cpumask routines and significantly shrink size of the kernel image. This patch adds FORCE_NR_CPUS option to teach the compiler to rely on NR_CPUS and enable corresponding optimizations. If FORCE_NR_CPUS=3Dy, kernel will not set nr_cpu_ids at boot, but only check that the actual number of possible CPUs is equal to NR_CPUS, and WARN if that doesn't hold. The new option is especially useful in embedded applications because kernel configurations are unique for each SoC, the number of CPUs is constant and known well, and memory limitations are typically harder. For my 4-CPU ARM64 build with NR_CPUS=3D4, FORCE_NR_CPUS=3Dy saves 46KB: add/remove: 3/4 grow/shrink: 46/729 up/down: 652/-46952 (-46300) Signed-off-by: Yury Norov --- include/linux/cpumask.h | 4 ++-- include/linux/smp.h | 4 ++++ kernel/smp.c | 2 ++ lib/Kconfig | 10 ++++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index b7b863cf8831..702c05329853 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -35,8 +35,8 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); }= cpumask_t; */ #define cpumask_pr_args(maskp) nr_cpu_ids, cpumask_bits(maskp) =20 -#if NR_CPUS =3D=3D 1 -#define nr_cpu_ids 1U +#if (NR_CPUS =3D=3D 1) || defined(CONFIG_FORCE_NR_CPUS) +#define nr_cpu_ids ((unsigned int)NR_CPUS) #else extern unsigned int nr_cpu_ids; #endif diff --git a/include/linux/smp.h b/include/linux/smp.h index c9e7a9abef2f..e9e74e08c886 100644 --- a/include/linux/smp.h +++ b/include/linux/smp.h @@ -183,7 +183,11 @@ static inline int get_boot_cpu_id(void) =20 static inline void set_nr_cpu_ids(unsigned int nr) { +#ifdef CONFIG_FORCE_NR_CPUS + WARN_ON(nr_cpu_ids !=3D nr); +#else nr_cpu_ids =3D nr; +#endif } =20 #else /* !SMP */ diff --git a/kernel/smp.c b/kernel/smp.c index 27d4ed0aee55..9cd1d02221ba 100644 --- a/kernel/smp.c +++ b/kernel/smp.c @@ -1088,9 +1088,11 @@ static int __init maxcpus(char *str) =20 early_param("maxcpus", maxcpus); =20 +#ifndef CONFIG_FORCE_NR_CPUS /* Setup number of possible processor ids */ unsigned int nr_cpu_ids __read_mostly =3D NR_CPUS; EXPORT_SYMBOL(nr_cpu_ids); +#endif =20 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundan= t */ void __init setup_nr_cpu_ids(void) diff --git a/lib/Kconfig b/lib/Kconfig index dc1ab2ed1dc6..5de23382e5f3 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -527,6 +527,16 @@ config CPUMASK_OFFSTACK them on the stack. This is a bit more expensive, but avoids stack overflow. =20 +config FORCE_NR_CPUS + bool "NR_CPUS is set to an actual number of CPUs" + depends on SMP + default n + help + Say Yes if you have NR_CPUS set to an actual number of possible + CPUs in your system, not to a default value. This forces the core + code to rely on compile-time value and optimize kernel routines + better. + config CPU_RMAP bool depends on SMP --=20 2.34.1