From patchwork Sat Dec 18 21:20:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 12686391 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 6A90EC433F5 for ; Sat, 18 Dec 2021 21:22:15 +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=IDi6E7YIdmloXJPF9bYUyJ3D/+zOvbNDz2mchk5SaGk=; b=uXscf4PFo/DS7b FvfYnPRpTWKNcUwzupili0qkIYmq6MZn7DKvLEmhUtAv1mQrdbwTHdqv1RuU4S9SgyNWmlc63c97a 6I6Y++75lfi1nKzB5VAysaaXnbhLXtMxeY0Ki3FZ8mMQ0XzQihADYRg3wSBVMjrNlauudQjOZJqqn 1pZiBFpRwPTrJL/8XUN9hq4f31pCZVPDEhUmczGW4NQART6U8g4Hu0yuvRmI3/4Vouvjmte2q0pm5 rNJYRAUG5WCmpBflXxx6eHIhseuP33Sna0uM7WpkURLHFz2Iu1x/kFUFeog0/hx3+0iaTxCIvjyNT 0UQ83T34zF2yzLQ7bwIQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1myh9b-00Ekr2-3V; Sat, 18 Dec 2021 21:22:07 +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 1myh8Z-00Ek1n-CY; Sat, 18 Dec 2021 21:21:05 +0000 Received: by mail-oi1-x232.google.com with SMTP id t19so9373515oij.1; Sat, 18 Dec 2021 13:21:02 -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=ZG1Hs62u68C5ALHAshDdD3Z6hgReTGi0eu0fnhr5AEs=; b=IIMnrKu1E+8J4ZeqIVFH4MIk+q707s4MTfXZMSHu7EsoSEaHXz6Xdcyp2j22iBGRbw WVHVFtLogRqg/mGYdxkJxTg1IZGqZrXfKAFHvN0SWyjvRKWVp2T/DFgrLVfndTJrfq2u OwC9HVCN/kSFoZV+Kt3qvSFSLRRXYLNLYsAXekXr654BC7f14NeaRIzjiQBBILSD3qyj HBL/MEAh3jwy7tfBsIQxRy8T/fhSamJaDFD5J49R6eZuWJv1xKEfJ4rEwC5POkESFzri oaICpv9ciOKT7yj04uxRt9vA6WWjGWqFCOU3UqGCbRh1xAC0FuFqTq7fyF8WHavhh7BP l2Ww== 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=ZG1Hs62u68C5ALHAshDdD3Z6hgReTGi0eu0fnhr5AEs=; b=QWp6x0Mof0kcd5J0clz5ehId2lMluamcebrdTmA5nWKLMUoq1f8jezDGrQOSUJjVyF g+W4P3CCT3ExPtKogvnljnpzqNh06c9vOuhBRxmwL8cuHOT1Qfn+Mi1DZxNc6YJu3ooS rzusIRSZhFEbuR5tUR/etLnSDSnNFxdEXkDSqNFAYIOlMpule4QKlvVq6+4cY8wAN7sX UToXuZu9QDzxk7XU2N1ken1eD0stYPirqJN/AVDFa7ZOG1iUWbcDpjO+TLXDeHPxbqB/ 10LCS4WD8nzNz7T9/rQSPCRw1h3K+afx3ZIOTrrXNqqSlUU6KZPiF62Ux1Bmn9oVvNTp ogJg== X-Gm-Message-State: AOAM533FgCPO/M+JZTsZP8K9/TrNFX/i10z2bb2Rw6eHC8i7c5+6K1E6 PzfYaQYmO/tSjegTwaz61dM= X-Google-Smtp-Source: ABdhPJzk/ppqcCd39lkFC0tyQzboJ0USG4d79V7IO5XKOSDBVZhakYMUNSWF7GIAyJsgsStLI/YQiw== X-Received: by 2002:a54:4e0c:: with SMTP id a12mr6723576oiy.12.1639862462273; Sat, 18 Dec 2021 13:21:02 -0800 (PST) Received: from localhost (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id i29sm2386537ots.49.2021.12.18.13.21.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Dec 2021 13:21:02 -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 14/17] kernel/cpu: add num_present_cpu counter Date: Sat, 18 Dec 2021 13:20:10 -0800 Message-Id: <20211218212014.1315894-15-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_132103_459220_841ED39B X-CRM114-Status: GOOD ( 20.92 ) 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_present_mask is actively used in the kernel. This patch adds a counter for present cpus, so that users that call num_present_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 | 25 +++++++++++++++---------- kernel/cpu.c | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 0be2504d8e4c..c2a9d15e2cbd 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -100,6 +100,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 cpumask_t cpus_booted_once_mask; @@ -873,15 +874,7 @@ void init_cpu_online(const struct cpumask *src); 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) -{ - if (present) - cpumask_set_cpu(cpu, &__cpu_present_mask); - else - cpumask_clear_cpu(cpu, &__cpu_present_mask); -} +void set_cpu_present(unsigned int cpu, bool present); void set_cpu_online(unsigned int cpu, bool online); @@ -965,7 +958,19 @@ static inline unsigned int num_possible_cpus(void) { return atomic_read(&__num_possible_cpus); } -#define num_present_cpus() cpumask_weight(cpu_present_mask) + +/** + * num_present_cpus() - Read the number of present CPUs + * + * Despite the fact that __num_present_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_present_cpus(void) +{ + return atomic_read(&__num_present_cpus); +} #define num_active_cpus() cpumask_weight(cpu_active_mask) static inline bool cpu_online(unsigned int cpu) diff --git a/kernel/cpu.c b/kernel/cpu.c index a0a815911173..1f7ea1bdde1a 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -2597,6 +2597,9 @@ EXPORT_SYMBOL(__cpu_online_mask); struct cpumask __cpu_present_mask __read_mostly; EXPORT_SYMBOL(__cpu_present_mask); +atomic_t __num_present_cpus __read_mostly; +EXPORT_SYMBOL(__num_present_cpus); + struct cpumask __cpu_active_mask __read_mostly; EXPORT_SYMBOL(__cpu_active_mask); @@ -2609,6 +2612,7 @@ EXPORT_SYMBOL(__num_online_cpus); void init_cpu_present(const struct cpumask *src) { cpumask_copy(&__cpu_present_mask, src); + atomic_set(&__num_present_cpus, cpumask_weight(cpu_present_mask)); } void init_cpu_possible(const struct cpumask *src) @@ -2662,6 +2666,18 @@ void set_cpu_possible(unsigned int cpu, bool possible) } EXPORT_SYMBOL(set_cpu_possible); +void set_cpu_present(unsigned int cpu, bool present) +{ + if (present) { + if (!cpumask_test_and_set_cpu(cpu, &__cpu_present_mask)) + atomic_inc(&__num_present_cpus); + } else { + if (cpumask_test_and_clear_cpu(cpu, &__cpu_present_mask)) + atomic_dec(&__num_present_cpus); + } +} +EXPORT_SYMBOL(set_cpu_present); + /* * Activate the first processor. */