From patchwork Sat Dec 18 21:20:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 12686393 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 25906C433F5 for ; Sat, 18 Dec 2021 21:22:27 +0000 (UTC) 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=7vY3CG/WZY22PpJW5ejTi6N4tevJgtajgE4g/ZHMmMk=; b=HiOreq2Lclh+Bv ja+AQfDV6NtaVk88GZ23YMElGRTvLKSpx0aFI+yVZhJ0jqKmNnTH0j7ECAjKG7q0o+A0ytHtBex70 OY4xsUQo0stn+yXdjr+Xcv4H/sr3dEOG11XSHP5WVX1v07oLPi0QyOhnbAynGSSPicPqi5w/oPVnk cllYKciaRYM156GUuMylVtTF5DbSLw4bIihL4x8vqXKMDQYJZtxXSEtk4JhZ/Emh7OMAk7lRZCUhx GPk0IWwMr6V7pIHcSXEEbJbg/dxdJZIqAJmeznM0CeInImRwJmZNPwqWfNhcACW1gVomKTjcywxzp 6ZoiHZVSzzmA8eEtpd/w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1myh9k-00EkxW-KA; Sat, 18 Dec 2021 21:22:16 +0000 Received: from mail-oi1-x232.google.com ([2607:f8b0:4864:20::232]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1myh8c-00Ek4I-Bk; Sat, 18 Dec 2021 21:21:08 +0000 Received: by mail-oi1-x232.google.com with SMTP id bk14so9355078oib.7; Sat, 18 Dec 2021 13:21:06 -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=4RN3kEyYZIUbVI8zbTA6/50Tm8Aie4ofxliWXaj5zzI=; b=YQxoC6k8cQ7kYClCwpVsfWWkBeb0lNCqOp+9jiaYb/x+5POwS9ABXWLPczVNlPCEzM iIHk+DzsjobRVCOlcC5XIikxxNxu5X/zaitga1umEIZCudyfpgSlCL7IQ38S7I467MHo pPRd3mGKnvvRiH7Kt5Sy8bEpLpHHxMLS79H6P18S/OWezxc8Qwp2CHDYEydPIFnIv0x5 VcXpAYuGmxjUSgIfiyTeTuXnJrVXxQbs3XD4EhmGr5lZt2cuYbt1zrSxB7wNMUdtcoFY RjhzErjDlzWwlTmVjAk+PBKifp+nLo5LNJz73PCLWdF08B6e3IpAf6fIRMZDIp6xjPQz h1sw== 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=4RN3kEyYZIUbVI8zbTA6/50Tm8Aie4ofxliWXaj5zzI=; b=DtO2pydXBcCT2pf/drvuyp6Qs2GjdyMF8bRg9jmnZXaYs2Fp7UiXDoBYhL1PyekgRI 5LKKovjRiYFg9XobY1ipU6v20g3q0EBd6HUeAO29uLqIUjNWVumcBj0oZby6FzsUk6K1 9FWnZH6rifiLyOtllBzNp1zGM32ZjqxDNr/ZESQr71VC5wz5Py2U7hu6RJ3dijqPIaJL x295rEC6PxaMQEBsQ8b9hFx/tPMmtmIwsAkZH7vZIj10gCp60HyOKFR3gJMI+9EMuR6r 8pJ8cL+au47BPnZ3xrM3+JFvRlS1vRtOhb3zILP8Wm0m8XNRoKEXI4xRqcTdvcc/j82+ 7gFw== X-Gm-Message-State: AOAM532hXBrtNna1PmWqHadw96HYj6R6jqT9q7qNVjtdx1pIUOqTtCgl L1RMtpa6Wwj4t5/9/Fw3f3M= X-Google-Smtp-Source: ABdhPJyJ7o57fml/hco056UTSNVqP6S+b5PlRmb4MutSKjwWJkbs3cpttoLjNt0j+pq04ePN93Dndw== X-Received: by 2002:aca:674a:: with SMTP id b10mr7011112oiy.66.1639862465447; Sat, 18 Dec 2021 13:21:05 -0800 (PST) Received: from localhost (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id n26sm2374672ooc.48.2021.12.18.13.21.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Dec 2021 13:21:05 -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 15/17] kernel/cpu: add num_active_cpu counter Date: Sat, 18 Dec 2021 13:20:11 -0800 Message-Id: <20211218212014.1315894-16-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_132106_511912_546C566E X-CRM114-Status: GOOD ( 20.67 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Similarly to the online cpus, the cpu_active_mask is actively used in the kernel. This patch adds a counter for active cpus, so that users that call num_active_cpus() would know the result immediately, instead of calling the bitmap_weight for the mask. Suggested-by: Nicholas Piggin Signed-off-by: Yury Norov --- include/linux/cpumask.h | 26 +++++++++++++++----------- kernel/cpu.c | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index c2a9d15e2cbd..0add872898f8 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -101,6 +101,7 @@ extern struct cpumask __cpu_dying_mask; extern atomic_t __num_online_cpus; extern atomic_t __num_possible_cpus; extern atomic_t __num_present_cpus; +extern atomic_t __num_active_cpus; extern cpumask_t cpus_booted_once_mask; @@ -875,17 +876,8 @@ void init_cpu_online(const struct cpumask *src); void set_cpu_possible(unsigned int cpu, bool possible); void reset_cpu_possible_mask(void); void set_cpu_present(unsigned int cpu, bool present); - void set_cpu_online(unsigned int cpu, bool online); - -static inline void -set_cpu_active(unsigned int cpu, bool active) -{ - if (active) - cpumask_set_cpu(cpu, &__cpu_active_mask); - else - cpumask_clear_cpu(cpu, &__cpu_active_mask); -} +void set_cpu_active(unsigned int cpu, bool active); static inline void set_cpu_dying(unsigned int cpu, bool dying) @@ -971,7 +963,19 @@ static inline unsigned int num_present_cpus(void) { return atomic_read(&__num_present_cpus); } -#define num_active_cpus() cpumask_weight(cpu_active_mask) + +/** + * num_active_cpus() - Read the number of active CPUs + * + * Despite the fact that __num_active_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_active_cpus(void) +{ + return atomic_read(&__num_active_cpus); +} static inline bool cpu_online(unsigned int cpu) { diff --git a/kernel/cpu.c b/kernel/cpu.c index 1f7ea1bdde1a..62b411d88810 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -2603,6 +2603,9 @@ EXPORT_SYMBOL(__num_present_cpus); struct cpumask __cpu_active_mask __read_mostly; EXPORT_SYMBOL(__cpu_active_mask); +atomic_t __num_active_cpus __read_mostly; +EXPORT_SYMBOL(__num_active_cpus); + struct cpumask __cpu_dying_mask __read_mostly; EXPORT_SYMBOL(__cpu_dying_mask); @@ -2678,6 +2681,18 @@ void set_cpu_present(unsigned int cpu, bool present) } EXPORT_SYMBOL(set_cpu_present); +void set_cpu_active(unsigned int cpu, bool active) +{ + if (active) { + if (!cpumask_test_and_set_cpu(cpu, &__cpu_active_mask)) + atomic_inc(&__num_active_cpus); + } else { + if (cpumask_test_and_clear_cpu(cpu, &__cpu_active_mask)) + atomic_dec(&__num_active_cpus); + } +} +EXPORT_SYMBOL(set_cpu_active); + /* * Activate the first processor. */