From nobody Wed Dec 17 07:25:47 2025 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 ED7CFEE49AF for ; Tue, 22 Aug 2023 18:42:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229555AbjHVSmG (ORCPT ); Tue, 22 Aug 2023 14:42:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229485AbjHVSmF (ORCPT ); Tue, 22 Aug 2023 14:42:05 -0400 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 023F0A8 for ; Tue, 22 Aug 2023 11:42:01 -0700 (PDT) Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-4ff9b389677so7355892e87.3 for ; Tue, 22 Aug 2023 11:42:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692729719; x=1693334519; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZcKf8TCfmSL/jTvJtJ1dXOWDhnfVF83USQ3Eyo19kik=; b=IpUdBzg/DdaYRMpbl13X84cDQiYDpmUCgeXaiBqeMbuJoHM1oXSNizZuih3VSwvYMl aZDcfvFa0yOhVkBUIFUaneixc6ZJOUXIfss7UU5ctP6d6wxplb/U69CS6fbvW4JWED1U cGtKnSfUOCEe/NmDiPVOE65upZgDhxf2tK9WwygoOomfip5wkdzE8lSCrdiZWcfowj0d BoM8ULxp4AnpR74B5yg7idNT4bwx7b7DBgaLziZSk3R5i4B8YZHLUjcGja68LIRiEvMf 8HqcuccQTe/izpkHLUmtgmGy7DU4obCcA1WIkptReUG/nQjmH/0FpxoTSIlOZchMrU/D rcCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692729719; x=1693334519; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZcKf8TCfmSL/jTvJtJ1dXOWDhnfVF83USQ3Eyo19kik=; b=BVurvAgiav44D20A4PJ3f3LvAEWy8PwB7KBZ410UyyjM++ZuxsZ/PZOvWHxTSgSZAd 2bKT0/t9UTXB3cVv2nTOOqzLIHkuOc+CenO55iZcd6dGriSMmcBF6GWPCUe47fVCf2xp U9WL9Sbybuqr7xuwYYnmqyUdbPH6qyTFWXIUEN25S3COeLI/ve0ZyEnu3TMvJpyF/Di7 m/hjQAHnFnvDjzFc9BKchi//FQ62AtLNyzgmtemZmH33a9tv4pGQr0hp2QZ1ZA5Dxcyg EInThy1AMB3NotYsZPSorvYVQyw1Vwn9fz+pLsQUX1CGH8hUYg5g8Cgb7hAOsLScIagE 64rA== X-Gm-Message-State: AOJu0Ywm8gVIMmr2JfLjVBFjXCnop4VHj4gRka+261GnkxGHNuBwbOEy RS2ZaFGzi+tVzA220luLz3qKD/Vznh9xCA== X-Google-Smtp-Source: AGHT+IFwNeb+x8+h4MA5Q285B17LTQ1gswDV1LNkxSm1vOHyvMZAe4lJw/QPCraL5VibXY/m5vFGcQ== X-Received: by 2002:a05:6512:1296:b0:4fe:993:2218 with SMTP id u22-20020a056512129600b004fe09932218mr9935537lfs.31.1692729718947; Tue, 22 Aug 2023 11:41:58 -0700 (PDT) Received: from f.. (cst-prg-85-121.cust.vodafone.cz. [46.135.85.121]) by smtp.gmail.com with ESMTPSA id w9-20020a056402070900b00525683f9b2fsm7945317edx.5.2023.08.22.11.41.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 11:41:58 -0700 (PDT) From: Mateusz Guzik To: linux-kernel@vger.kernel.org Cc: dennis@kernel.org, tj@kernel.org, cl@linux.com, akpm@linux-foundation.org, shakeelb@google.com, linux-mm@kvack.org, Mateusz Guzik Subject: [PATCH v2 1/2] pcpcntr: add group allocation/free Date: Tue, 22 Aug 2023 20:41:51 +0200 Message-Id: <20230822184152.2194558-2-mjguzik@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230822184152.2194558-1-mjguzik@gmail.com> References: <20230822184152.2194558-1-mjguzik@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" Allocations and frees are globally serialized on the pcpu lock (and the CPU hotplug lock if enabled, which is the case on Debian). At least one frequent consumer allocates 4 back-to-back counters (and frees them in the same manner), exacerbating the problem. While this does not fully remedy scalability issues, it is a step towards that goal and provides immediate relief. Signed-off-by: Mateusz Guzik --- include/linux/percpu_counter.h | 20 ++++++++--- lib/percpu_counter.c | 61 +++++++++++++++++++++++----------- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/include/linux/percpu_counter.h b/include/linux/percpu_counter.h index 75b73c83bc9d..518a4088b964 100644 --- a/include/linux/percpu_counter.h +++ b/include/linux/percpu_counter.h @@ -30,17 +30,27 @@ struct percpu_counter { =20 extern int percpu_counter_batch; =20 -int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gf= p, - struct lock_class_key *key); +int __percpu_counter_init_many(struct percpu_counter *fbc, s64 amount, gfp= _t gfp, + u32 nr_counters, struct lock_class_key *key); =20 -#define percpu_counter_init(fbc, value, gfp) \ +#define percpu_counter_init_many(fbc, value, gfp, nr_counters) \ ({ \ static struct lock_class_key __key; \ \ - __percpu_counter_init(fbc, value, gfp, &__key); \ + __percpu_counter_init_many(fbc, value, gfp, nr_counters,\ + &__key); \ }) =20 -void percpu_counter_destroy(struct percpu_counter *fbc); + +#define percpu_counter_init(fbc, value, gfp) \ + percpu_counter_init_many(fbc, value, gfp, 1) + +void percpu_counter_destroy_many(struct percpu_counter *fbc, u32 nr_counte= rs); +static inline void percpu_counter_destroy(struct percpu_counter *fbc) +{ + percpu_counter_destroy_many(fbc, 1); +} + void percpu_counter_set(struct percpu_counter *fbc, s64 amount); void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch); diff --git a/lib/percpu_counter.c b/lib/percpu_counter.c index 5004463c4f9f..9338b27f1cdd 100644 --- a/lib/percpu_counter.c +++ b/lib/percpu_counter.c @@ -151,48 +151,71 @@ s64 __percpu_counter_sum(struct percpu_counter *fbc) } EXPORT_SYMBOL(__percpu_counter_sum); =20 -int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gf= p, - struct lock_class_key *key) +int __percpu_counter_init_many(struct percpu_counter *fbc, s64 amount, gfp= _t gfp, + u32 nr_counters, struct lock_class_key *key) { unsigned long flags __maybe_unused; - - raw_spin_lock_init(&fbc->lock); - lockdep_set_class(&fbc->lock, key); - fbc->count =3D amount; - fbc->counters =3D alloc_percpu_gfp(s32, gfp); - if (!fbc->counters) + size_t counter_size; + s32 __percpu *counters; + u32 i; + + counter_size =3D ALIGN(sizeof(*counters), __alignof__(*counters)); + counters =3D __alloc_percpu_gfp(nr_counters * counter_size, + __alignof__(*counters), gfp); + if (!counters) { + fbc[0].counters =3D NULL; return -ENOMEM; + } =20 - debug_percpu_counter_activate(fbc); + for (i =3D 0; i < nr_counters; i++) { + raw_spin_lock_init(&fbc[i].lock); + lockdep_set_class(&fbc[i].lock, key); +#ifdef CONFIG_HOTPLUG_CPU + INIT_LIST_HEAD(&fbc[i].list); +#endif + fbc[i].count =3D amount; + fbc[i].counters =3D (void *)counters + (i * counter_size); + + debug_percpu_counter_activate(&fbc[i]); + } =20 #ifdef CONFIG_HOTPLUG_CPU - INIT_LIST_HEAD(&fbc->list); spin_lock_irqsave(&percpu_counters_lock, flags); - list_add(&fbc->list, &percpu_counters); + for (i =3D 0; i < nr_counters; i++) + list_add(&fbc[i].list, &percpu_counters); spin_unlock_irqrestore(&percpu_counters_lock, flags); #endif return 0; } -EXPORT_SYMBOL(__percpu_counter_init); +EXPORT_SYMBOL(__percpu_counter_init_many); =20 -void percpu_counter_destroy(struct percpu_counter *fbc) +void percpu_counter_destroy_many(struct percpu_counter *fbc, u32 nr_counte= rs) { unsigned long flags __maybe_unused; + u32 i; + + if (WARN_ON_ONCE(!fbc)) + return; =20 - if (!fbc->counters) + if (!fbc[0].counters) return; =20 - debug_percpu_counter_deactivate(fbc); + for (i =3D 0; i < nr_counters; i++) + debug_percpu_counter_deactivate(&fbc[i]); =20 #ifdef CONFIG_HOTPLUG_CPU spin_lock_irqsave(&percpu_counters_lock, flags); - list_del(&fbc->list); + for (i =3D 0; i < nr_counters; i++) + list_del(&fbc[i].list); spin_unlock_irqrestore(&percpu_counters_lock, flags); #endif - free_percpu(fbc->counters); - fbc->counters =3D NULL; + + free_percpu(fbc[0].counters); + + for (i =3D 0; i < nr_counters; i++) + fbc[i].counters =3D NULL; } -EXPORT_SYMBOL(percpu_counter_destroy); +EXPORT_SYMBOL(percpu_counter_destroy_many); =20 int percpu_counter_batch __read_mostly =3D 32; EXPORT_SYMBOL(percpu_counter_batch); --=20 2.39.2 From nobody Wed Dec 17 07:25:47 2025 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 DD6A8EE49AE for ; Tue, 22 Aug 2023 18:42:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229562AbjHVSmH (ORCPT ); Tue, 22 Aug 2023 14:42:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229508AbjHVSmF (ORCPT ); Tue, 22 Aug 2023 14:42:05 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60076CD1 for ; Tue, 22 Aug 2023 11:42:02 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-5008d16cc36so1708128e87.2 for ; Tue, 22 Aug 2023 11:42:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692729720; x=1693334520; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mEOToIeoViTRF8c8rAS7lPuWoT5IGl18T901p8s8lTc=; b=QfVGsVPwABxhEcYrvXbcmGiUuRvcJXymMpVHwTotunc6jGK3pxIitnK0pOSZRFmEuL oXQs8Q+9oKQP1g8HsM+D9ok35AXs4Q8BZUmGNdnBekmT+68LHd+hhZPCs9wHNZ3jhRgI Vt3/bJlJuCKxjpWBm1O+QxCqEFTn1KPle9ZPbnJa0Z+fOom8Xqlq3gvp71cs9gA3QHHB ginjoZv3MmDg3zGF/e+ejtBgdIEqcgUcmM2ZFdFCZ3XMR7dmlwZ6qhx7HaIW6rc0zrX9 CjO/UNJsvzEP05/0XLKfCrEX730ap91i8SeofwitpKE0UWFbkwYzwE72YWNVtvqJbPPB CkbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692729720; x=1693334520; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mEOToIeoViTRF8c8rAS7lPuWoT5IGl18T901p8s8lTc=; b=MCL6XJkCfqErqMiu7rSfMIqwimyPnlCcBzzWhTmjTWlRnbj2O/9M9xcLeUhcLspIaw uS7DfGpyVWyvaOs4l/lOw5D6QOK2Ky6uDBqSNe1VjiU+6fm/buMuyDURkImK6PUVDwib m4DgqYrO/yXun2k9FM64RFfofZSKzwjo7gU+1XxG44Z2ClM2yjK6sPmbEpG4r5xHIta/ CgHHPC4QJT/K2Woh4e/z4Y9/DstrGsFOwiVHUI5Q7UwS2kYzhTAwIAYeVB1hOsoeFn6S igUSeeAWr0o+Oz1i1WdevEUKBnGbnLI2pJ8ggsNcOOltGWSVttAgPI1kOMHWZm6EdeOe Vg3A== X-Gm-Message-State: AOJu0Yx7gOCgEODgrlLhygfIXT0djddXD09zt1dauhqNpGBGXaQ0CSo+ +nW/e97tDi+Yey3GIwyiF5QTQl9ChlNG9Q== X-Google-Smtp-Source: AGHT+IEL+C5O6TMeHs29lajaMbWo+0Cbc3ZEq9kla6k40ShytD8+zBpbctMDQ6f76ngrJSIhW5Btog== X-Received: by 2002:a05:6512:39ca:b0:4fe:82a7:814d with SMTP id k10-20020a05651239ca00b004fe82a7814dmr8200504lfu.48.1692729720458; Tue, 22 Aug 2023 11:42:00 -0700 (PDT) Received: from f.. (cst-prg-85-121.cust.vodafone.cz. [46.135.85.121]) by smtp.gmail.com with ESMTPSA id w9-20020a056402070900b00525683f9b2fsm7945317edx.5.2023.08.22.11.41.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 11:42:00 -0700 (PDT) From: Mateusz Guzik To: linux-kernel@vger.kernel.org Cc: dennis@kernel.org, tj@kernel.org, cl@linux.com, akpm@linux-foundation.org, shakeelb@google.com, linux-mm@kvack.org, Mateusz Guzik Subject: [PATCH v2 2/2] fork: group allocation of per-cpu counters for mm struct Date: Tue, 22 Aug 2023 20:41:52 +0200 Message-Id: <20230822184152.2194558-3-mjguzik@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230822184152.2194558-1-mjguzik@gmail.com> References: <20230822184152.2194558-1-mjguzik@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" A trivial execve scalability test which tries to be very friendly (statically linked binaries, all separate) is predominantly bottlenecked by back-to-back per-cpu counter allocations which serialize on global locks. Ease the pain by allocating and freeing them in one go. Bench can be found here: http://apollo.backplane.com/DFlyMisc/doexec.c $ cc -static -O2 -o static-doexec doexec.c $ ./static-doexec $(nproc) Even at a very modest scale of 26 cores (ops/s): before: 133543.63 after: 186061.81 (+39%) While with the patch these allocations remain a significant problem, the primary bottleneck shifts to: __pv_queued_spin_lock_slowpath+1 _raw_spin_lock_irqsave+57 folio_lruvec_lock_irqsave+91 release_pages+590 tlb_batch_pages_flush+61 tlb_finish_mmu+101 exit_mmap+327 __mmput+61 begin_new_exec+1245 load_elf_binary+712 bprm_execve+644 do_execveat_common.isra.0+429 __x64_sys_execve+50 do_syscall_64+46 entry_SYSCALL_64_after_hwframe+110 Signed-off-by: Mateusz Guzik --- kernel/fork.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/kernel/fork.c b/kernel/fork.c index d2e12b6d2b18..4f0ada33457e 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -909,8 +909,6 @@ static void cleanup_lazy_tlbs(struct mm_struct *mm) */ void __mmdrop(struct mm_struct *mm) { - int i; - BUG_ON(mm =3D=3D &init_mm); WARN_ON_ONCE(mm =3D=3D current->mm); =20 @@ -925,9 +923,8 @@ void __mmdrop(struct mm_struct *mm) put_user_ns(mm->user_ns); mm_pasid_drop(mm); mm_destroy_cid(mm); + percpu_counter_destroy_many(mm->rss_stat, NR_MM_COUNTERS); =20 - for (i =3D 0; i < NR_MM_COUNTERS; i++) - percpu_counter_destroy(&mm->rss_stat[i]); free_mm(mm); } EXPORT_SYMBOL_GPL(__mmdrop); @@ -1252,8 +1249,6 @@ static void mm_init_uprobes_state(struct mm_struct *m= m) static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct = *p, struct user_namespace *user_ns) { - int i; - mt_init_flags(&mm->mm_mt, MM_MT_FLAGS); mt_set_external_lock(&mm->mm_mt, &mm->mmap_lock); atomic_set(&mm->mm_users, 1); @@ -1301,17 +1296,14 @@ static struct mm_struct *mm_init(struct mm_struct *= mm, struct task_struct *p, if (mm_alloc_cid(mm)) goto fail_cid; =20 - for (i =3D 0; i < NR_MM_COUNTERS; i++) - if (percpu_counter_init(&mm->rss_stat[i], 0, GFP_KERNEL_ACCOUNT)) - goto fail_pcpu; + if (percpu_counter_init_many(mm->rss_stat, 0, GFP_KERNEL_ACCOUNT, NR_MM_C= OUNTERS)) + goto fail_pcpu; =20 mm->user_ns =3D get_user_ns(user_ns); lru_gen_init_mm(mm); return mm; =20 fail_pcpu: - while (i > 0) - percpu_counter_destroy(&mm->rss_stat[--i]); mm_destroy_cid(mm); fail_cid: destroy_context(mm); --=20 2.39.2