From patchwork Sat Dec 18 21:20:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 1570640 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=fZUwllGk; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=JpKKkwIN; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-snps-arc-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JGf1s0yb2z9sR4 for ; Sun, 19 Dec 2021 08:22:09 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jX5z7dG+BVmlm2qJu9eJBBCNBNlOMtYMiwpaNfS1nQw=; b=fZUwllGkS0DZ0e z0QdVXOWTgrfq57te9oT4wawknLtMByGAwq4N+xo8zd7CQ5igNEvwewDsgm1lW/LQdbJYLEH04Ek6 cdR5qZjqoyN7M0Uu7CfjYiA9Vp0/4iW3QOxzfWfZQ4UNGvTBmohyCJjcVXt2KrTCLBQNrvGCTS7/q euBBQ4mD8JmSTEMdYoI3tf/CRW1QxVaMNh5WktMMPcR/su1YITGXJOnOwvBAzyeeyckYaYAT8QpLZ 7Fm1iTZYwbgt3Yjni80k+rd8pB1gBjZiwSxomFbzAFoAn/evPQkIE9rtG3aF5vpP8rjbEX3iH+z7D Xqakj71ZrubD+MMHx+3w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1myh9a-00EkqZ-Co; Sat, 18 Dec 2021 21:22:06 +0000 Received: from mail-ot1-x333.google.com ([2607:f8b0:4864:20::333]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1myh8W-00Ejyu-93; Sat, 18 Dec 2021 21:21:02 +0000 Received: by mail-ot1-x333.google.com with SMTP id a23-20020a9d4717000000b0056c15d6d0caso7434881otf.12; Sat, 18 Dec 2021 13:20:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=TtB4s+TeH5Hsx4aMsA8zovfLkC56GGSxZAM2kKc/k+s=; b=JpKKkwINU8Ydvnyx9TIa0uTEfLIEeORqwMjlt2cBKjHjbDtmjXYoAYcpkHvWOACNZ+ JClsTJjPz351JiBL5YkKHmfgQb6Bs9RYm3XbiLNQpBXdxk6fSPpeP/UdvMr1JZgJmzOO 3zd+x9XrwW0Ta4OedkblF2OTR3HDwbCAnWpcgXnJtqaKYqgdIUsODPw8Wp+KaP/4tQcA 9zIa24wEaUYy2nK+3lrBCplwLyxuKdWlNpxyOXXoiOONLC5fupt6hpsq3NiXG/6pQoSG wtOhHaiB3EGda/k0PB3OUJtDVXKKoy2mUG5TqWrYE76xWrWSeHaSuRq4yt4Mp1WKWa5j aevA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TtB4s+TeH5Hsx4aMsA8zovfLkC56GGSxZAM2kKc/k+s=; b=u8KsG2LWIqfjO9JL6/vE+WgLulCmDht4kwo0945uFt3GV7Bme5SMpuxk8TlgN2TagP p+HCkDfUZo9NsGALElQkC7DTQJgzuCU9Aj2KovvMQgrQ3eZ/RFWne4rPbC23hxGvQrJ1 WK2XQMjVrkqLbX1DLLUtLs1l6ApUamaa7/OGQAgrT1YJvpnKbvZJPvVzCQkp2WmqrMJs o/kt2Ag0368Ogyl/aZWGXtq8jNVPjZpEIYIJWD8i/sCTMNGxKTKWn5AT0ze1rSqWqGEQ x0+sm17QbOu0nOriJu31AZnWCt/8b2f7yixSrCrsl8b49V9R8HkPA3tioa9EfuJnICIy fi9w== X-Gm-Message-State: AOAM530etmhOd02m97ia4c8aB0a193uGUbukCA92ldupMD2kXJOvBiFi zbP39J7IK41uff00SMrpftc= X-Google-Smtp-Source: ABdhPJz65aswwCqCKcm3NSepL1No7Bu916BAt7YX/+A7MjCUNmyZSZaKJNIPfs9JYRO4CobDyeY4pQ== X-Received: by 2002:a05:6830:10:: with SMTP id c16mr6492562otp.79.1639862459100; Sat, 18 Dec 2021 13:20:59 -0800 (PST) Received: from localhost (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id e21sm2358431ote.72.2021.12.18.13.20.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Dec 2021 13:20:58 -0800 (PST) From: Yury Norov To: linux-kernel@vger.kernel.org, Yury Norov , "James E.J. Bottomley" , "Martin K. Petersen" , =?utf-8?b?TWljaGHFgiBN?= =?utf-8?b?aXJvc8WCYXc=?= , "Paul E. McKenney" , "Rafael J. Wysocki" , Alexander Shishkin , Alexey Klimov , Amitkumar Karwar , Andi Kleen , Andrew Lunn , Andrew Morton , Andy Gross , Andy Lutomirski , Andy Shevchenko , Anup Patel , Ard Biesheuvel , Arnaldo Carvalho de Melo , Arnd Bergmann , Borislav Petkov , Catalin Marinas , Christoph Hellwig , Christoph Lameter , Daniel Vetter , Dave Hansen , David Airlie , David Laight , Dennis Zhou , Emil Renner Berthing , Geert Uytterhoeven , Geetha sowjanya , Greg Kroah-Hartman , Guo Ren , Hans de Goede , Heiko Carstens , Ian Rogers , Ingo Molnar , Jakub Kicinski , Jason Wessel , Jens Axboe , Jiri Olsa , Joe Perches , Jonathan Cameron , Juri Lelli , Kees Cook , Krzysztof Kozlowski , Lee Jones , Marc Zyngier , Marcin Wojtas , Mark Gross , Mark Rutland , Matti Vaittinen , Mauro Carvalho Chehab , Mel Gorman , Michael Ellerman , Mike Marciniszyn , Nicholas Piggin , Palmer Dabbelt , Peter Zijlstra , Petr Mladek , Randy Dunlap , Rasmus Villemoes , Russell King , Saeed Mahameed , Sagi Grimberg , Sergey Senozhatsky , Solomon Peachy , Stephen Boyd , Stephen Rothwell , Steven Rostedt , Subbaraya Sundeep , Sudeep Holla , Sunil Goutham , Tariq Toukan , Tejun Heo , Thomas Bogendoerfer , Thomas Gleixner , Ulf Hansson , Vincent Guittot , Vineet Gupta , Viresh Kumar , Vivien Didelot , Vlastimil Babka , Will Deacon , bcm-kernel-feedback-list@broadcom.com, kvm@vger.kernel.org, linux-alpha@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-crypto@vger.kernel.org, linux-csky@vger.kernel.org, linux-ia64@vger.kernel.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-perf-users@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-snps-arc@lists.infradead.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 13/17] kernel/cpu: add num_possible_cpus counter Date: Sat, 18 Dec 2021 13:20:09 -0800 Message-Id: <20211218212014.1315894-14-yury.norov@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211218212014.1315894-1-yury.norov@gmail.com> References: <20211218212014.1315894-1-yury.norov@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211218_132100_388236_F437D5C2 X-CRM114-Status: GOOD ( 21.21 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Similarly to the online cpus, the cpu_possible_mask is actively used in the kernel. This patch adds a counter for possible cpus, so that users that call num_possible_cpus() would know the result immed [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:333 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [yury.norov[at]gmail.com] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: linux-snps-arc@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux on Synopsys ARC Processors List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-snps-arc" Errors-To: linux-snps-arc-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Similarly to the online cpus, the cpu_possible_mask is actively used in the kernel. This patch adds a counter for possible cpus, so that users that call num_possible_cpus() would know the result immediately, instead of calling the bitmap_weight for the mask underlying. Suggested-by: Nicholas Piggin Signed-off-by: Yury Norov --- include/linux/cpumask.h | 30 ++++++++++++++++-------------- kernel/cpu.c | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 1906e3225737..0be2504d8e4c 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -99,6 +99,7 @@ extern struct cpumask __cpu_dying_mask; #define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask) extern atomic_t __num_online_cpus; +extern atomic_t __num_possible_cpus; extern cpumask_t cpus_booted_once_mask; @@ -870,19 +871,8 @@ void init_cpu_present(const struct cpumask *src); void init_cpu_possible(const struct cpumask *src); void init_cpu_online(const struct cpumask *src); -static inline void reset_cpu_possible_mask(void) -{ - bitmap_zero(cpumask_bits(&__cpu_possible_mask), NR_CPUS); -} - -static inline void -set_cpu_possible(unsigned int cpu, bool possible) -{ - if (possible) - cpumask_set_cpu(cpu, &__cpu_possible_mask); - else - cpumask_clear_cpu(cpu, &__cpu_possible_mask); -} +void set_cpu_possible(unsigned int cpu, bool possible); +void reset_cpu_possible_mask(void); static inline void set_cpu_present(unsigned int cpu, bool present) @@ -962,7 +952,19 @@ static inline unsigned int num_online_cpus(void) { return atomic_read(&__num_online_cpus); } -#define num_possible_cpus() cpumask_weight(cpu_possible_mask) + +/** + * num_possible_cpus() - Read the number of possible CPUs + * + * Despite the fact that __num_possible_cpus is of type atomic_t, this + * interface gives only a momentary snapshot and is not protected against + * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held + * region. + */ +static inline unsigned int num_possible_cpus(void) +{ + return atomic_read(&__num_possible_cpus); +} #define num_present_cpus() cpumask_weight(cpu_present_mask) #define num_active_cpus() cpumask_weight(cpu_active_mask) diff --git a/kernel/cpu.c b/kernel/cpu.c index cd7605204d4d..a0a815911173 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -2583,10 +2583,13 @@ EXPORT_SYMBOL(cpu_all_bits); #ifdef CONFIG_INIT_ALL_POSSIBLE struct cpumask __cpu_possible_mask __read_mostly = {CPU_BITS_ALL}; +atomic_t __num_possible_cpus __read_mostly = ATOMIC_INIT(NR_CPUS); #else struct cpumask __cpu_possible_mask __read_mostly; +atomic_t __num_possible_cpus __read_mostly; #endif EXPORT_SYMBOL(__cpu_possible_mask); +EXPORT_SYMBOL(__num_possible_cpus); struct cpumask __cpu_online_mask __read_mostly; EXPORT_SYMBOL(__cpu_online_mask); @@ -2611,6 +2614,7 @@ void init_cpu_present(const struct cpumask *src) void init_cpu_possible(const struct cpumask *src) { cpumask_copy(&__cpu_possible_mask, src); + atomic_set(&__num_possible_cpus, cpumask_weight(cpu_possible_mask)); } void init_cpu_online(const struct cpumask *src) @@ -2640,6 +2644,24 @@ void set_cpu_online(unsigned int cpu, bool online) } } +void reset_cpu_possible_mask(void) +{ + bitmap_zero(cpumask_bits(&__cpu_possible_mask), NR_CPUS); + atomic_set(&__num_possible_cpus, 0); +} + +void set_cpu_possible(unsigned int cpu, bool possible) +{ + if (possible) { + if (!cpumask_test_and_set_cpu(cpu, &__cpu_possible_mask)) + atomic_inc(&__num_possible_cpus); + } else { + if (cpumask_test_and_clear_cpu(cpu, &__cpu_possible_mask)) + atomic_dec(&__num_possible_cpus); + } +} +EXPORT_SYMBOL(set_cpu_possible); + /* * Activate the first processor. */