From nobody Mon Dec 1 22:05:46 2025 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D18131D38C for ; Thu, 27 Nov 2025 23:37:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764286624; cv=none; b=PZ4o7zIrimdcfYa1Gk5MCJ2lWYMlYjsFZ2LdEgDkIJJUMXIFDYg03z5pJKlKYGGIciQyO8/eMosl880TCShYKPXORN+bb2bEcBrHF/v9LXKeIwzOLuSfa+ZObmWLc9/BP3opkDTSDSmBsAiFUuXX6vs8kj0nZ0D7IDj7rOUgDSY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764286624; c=relaxed/simple; bh=6vqEFjqx4qtg11JFsFpgLmKdKFEFnYEv3+5ZQi+BrQs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pEfoypCrpVaVO7qGZj/ZOMoqq5NWj0eQtDO+FsY9VOQVNrCGGhOYqQXTCgiikusbqRoQ+MOqjYeThsfe6j9OV/DeQHNnHjpptIvXhOIUrtfLeFkosdTff9WZpNbSvNYGKi8ThcGQRdjKs/qxg3jpwBM1S8jBZcC+buosKYtWgZ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=Ap4vSDWm; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=pZCS4rKm; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=Qdftk0iS; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=RfFM/vp6; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="Ap4vSDWm"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="pZCS4rKm"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="Qdftk0iS"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="RfFM/vp6" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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 smtp-out2.suse.de (Postfix) with ESMTPS id 2A7AA5BD09; Thu, 27 Nov 2025 23:36:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1764286619; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n0UUaCVzJQj1/iuGKRH/cFH/A07YpZOHP7llM/q/NC4=; b=Ap4vSDWmeM14JLoIya0pM7b9gtnZKJd4TDGsh3BOnyTh+J1Y7xwX0rJ3e/aw0tCqhP0vOE HdJ9yzbIhkFNiTCVTbzMVOcFW8OuNO2eSHufieMWIwI4X2nIOkV+0lK3XUvcvyyXzwjASN YlrKMB83IdkHBvRAwepkhGRnMuchCaQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1764286619; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n0UUaCVzJQj1/iuGKRH/cFH/A07YpZOHP7llM/q/NC4=; b=pZCS4rKmX00/K7qjGPGeGRncIEMz2ZfSI/TAe9YyeOIW7sloLQ/VAOU2EcX0g4VSbcJBfW UlWvNhqTyBeZCyDA== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=Qdftk0iS; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b="RfFM/vp6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1764286618; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n0UUaCVzJQj1/iuGKRH/cFH/A07YpZOHP7llM/q/NC4=; b=Qdftk0iSZmgPWzxli5uiBsXKNZIkrmhwaRuka7bf3JoqtXoRh/Qk6y2mtVASmG6u2V0HVm hIhxIJxY0Rk8KWvMmT6MBvpwQ/LQMaX7x0d56MJ8ZP8+6jomlUGqah1yAgZVpFOm/a58WA frBAff0NWHfn1540T1u4wv7ykcC1JqI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1764286618; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n0UUaCVzJQj1/iuGKRH/cFH/A07YpZOHP7llM/q/NC4=; b=RfFM/vp6sSa9Z/nrGP4ohEtuOJWX49AKgGQPVvl7/g3pGhZxCYRDrjghsrid5emCC+xYLL xwQkw+YaO9oM1KBA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id C09633EA65; Thu, 27 Nov 2025 23:36:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id WoV6I5ngKGlGGQAAD6G6ig (envelope-from ); Thu, 27 Nov 2025 23:36:57 +0000 From: Gabriel Krisman Bertazi To: linux-mm@kvack.org Cc: Gabriel Krisman Bertazi , linux-kernel@vger.kernel.org, jack@suse.cz, Mateusz Guzik , Shakeel Butt , Michal Hocko , Mathieu Desnoyers , Dennis Zhou , Tejun Heo , Christoph Lameter , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan Subject: [RFC PATCH 3/4] mm: Avoid percpu MM counters on single-threaded tasks Date: Thu, 27 Nov 2025 18:36:30 -0500 Message-ID: <20251127233635.4170047-4-krisman@suse.de> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251127233635.4170047-1-krisman@suse.de> References: <20251127233635.4170047-1-krisman@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; URIBL_BLOCKED(0.00)[benchmark.sh:url,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.cz:email,suse.de:mid,suse.de:email,suse.de:dkim]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCPT_COUNT_TWELVE(0.00)[18]; ARC_NA(0.00)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FUZZY_RATELIMITED(0.00)[rspamd.com]; MIME_TRACE(0.00)[0:+]; FREEMAIL_CC(0.00)[suse.de,vger.kernel.org,suse.cz,gmail.com,linux.dev,kernel.org,efficios.com,gentwo.org,linux-foundation.org,redhat.com,oracle.com,google.com]; RCVD_TLS_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.cz:email,benchmark.sh:url,suse.de:mid,suse.de:email,suse.de:dkim,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns]; RCVD_COUNT_TWO(0.00)[2]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_VIA_SMTP_AUTH(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_TRACE(0.00)[suse.de:+]; R_RATELIMIT(0.00)[to_ip_from(RLpqz8f45ibb1mrnbixkpon6m4)]; FREEMAIL_ENVRCPT(0.00)[gmail.com] X-Rspamd-Action: no action X-Spam-Flag: NO X-Spam-Score: -3.01 X-Spam-Level: X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Rspamd-Queue-Id: 2A7AA5BD09 Content-Type: text/plain; charset="utf-8" The cost of the pcpu memory allocation when forking a new task is non-negligible, as reported in a few occasions, such as [1]. But it can also be fully avoided for single-threaded applications, where we know the vast majority of updates happen from the local task context. For the trivial benchmark, bound to cpu 0 to reduce cost of migrations), like below: for (( i =3D 0; i < 20000; i++ )); do /bin/true; done on an 80c machine, this patchset yielded a 6% improvement in system time. On a 256c machine, the system time reduced by 11%. Profiling shows mm_init went from 13.5% of samples to less than 3.33% in the same 256c machine: Before: - 13.50% 3.93% benchmark.sh [kernel.kallsyms] [k] mm_init - 9.57% mm_init + 4.80% pcpu_alloc_noprof + 3.87% __percpu_counter_init_many After: - 3.33% 0.80% benchmark.sh [kernel.kallsyms] [k] mm_init - 2.53% mm_init + 2.05% pcpu_alloc_noprof For kernbench in 256c, the patchset yields a 1.4% improvement on system time. For gitsource, the improvement in system time I'm measuring is around 3.12%. The upgrade adds some overhead to the second fork, in particular an atomic operation, besides the expensive allocation that was moved from the first fork to the second. So a fair question is the impact of this patchset on multi-threaded applications. I wrote a microbenchmark similar to the /bin/true above, but that just spawns a second pthread and waits for it to finish. The second thread just returns immediately. This is executed in a loop, bound to a single NUMA node, with: for (( i =3D 0; i < 20000; i++ )); do /bin/parallel-true; done Profiling shows the lazy upgrade impact is minimal to the performance: - 0.68% 0.04% parallel-true [kernel.kallsyms] [k] __lazy_percpu_c= ounter_upgrade_many - 0.64% __lazy_percpu_counter_upgrade_many 0.62% pcpu_alloc_noprof Which is confirmed by the measured system time. With 20k runs, i'm still getting a slight improvement from baseline for the 2t case (2-4%). [1] https://lore.kernel.org/all/20230608111408.s2minsenlcjow7q3@quack3 Suggested-by: Jan Kara Signed-off-by: Gabriel Krisman Bertazi --- include/linux/mm.h | 24 ++++++++---------------- include/linux/mm_types.h | 4 ++-- include/trace/events/kmem.h | 4 ++-- kernel/fork.c | 14 ++++++-------- 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index d16b33bacc32..29de4c60ac6c 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2679,36 +2679,28 @@ static inline bool get_user_page_fast_only(unsigned= long addr, */ static inline unsigned long get_mm_counter(struct mm_struct *mm, int membe= r) { - return percpu_counter_read_positive(&mm->rss_stat[member]); + return lazy_percpu_counter_read_positive(&mm->rss_stat[member]); } =20 static inline unsigned long get_mm_counter_sum(struct mm_struct *mm, int m= ember) { - return percpu_counter_sum_positive(&mm->rss_stat[member]); + return lazy_percpu_counter_sum_positive(&mm->rss_stat[member]); } =20 void mm_trace_rss_stat(struct mm_struct *mm, int member); =20 static inline void add_mm_counter(struct mm_struct *mm, int member, long v= alue) { - percpu_counter_add(&mm->rss_stat[member], value); - - mm_trace_rss_stat(mm, member); -} - -static inline void inc_mm_counter(struct mm_struct *mm, int member) -{ - percpu_counter_inc(&mm->rss_stat[member]); + if (READ_ONCE(current->mm) =3D=3D mm) + lazy_percpu_counter_add_fast(&mm->rss_stat[member], value); + else + lazy_percpu_counter_add_atomic(&mm->rss_stat[member], value); =20 mm_trace_rss_stat(mm, member); } =20 -static inline void dec_mm_counter(struct mm_struct *mm, int member) -{ - percpu_counter_dec(&mm->rss_stat[member]); - - mm_trace_rss_stat(mm, member); -} +#define inc_mm_counter(mm, member) add_mm_counter(mm, member, 1) +#define dec_mm_counter(mm, member) add_mm_counter(mm, member, -1) =20 /* Optimized variant when folio is already known not to be anon */ static inline int mm_counter_file(struct folio *folio) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 90e5790c318f..5a8d677efa85 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include =20 @@ -1119,7 +1119,7 @@ struct mm_struct { unsigned long saved_e_flags; #endif =20 - struct percpu_counter rss_stat[NR_MM_COUNTERS]; + struct lazy_percpu_counter rss_stat[NR_MM_COUNTERS]; =20 struct linux_binfmt *binfmt; =20 diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h index 7f93e754da5c..e21572f4d8a6 100644 --- a/include/trace/events/kmem.h +++ b/include/trace/events/kmem.h @@ -442,8 +442,8 @@ TRACE_EVENT(rss_stat, __entry->mm_id =3D mm_ptr_to_hash(mm); __entry->curr =3D !!(current->mm =3D=3D mm); __entry->member =3D member; - __entry->size =3D (percpu_counter_sum_positive(&mm->rss_stat[member]) - << PAGE_SHIFT); + __entry->size =3D (lazy_percpu_counter_sum_positive(&mm->rss_stat[member= ]) + << PAGE_SHIFT); ), =20 TP_printk("mm_id=3D%u curr=3D%d type=3D%s size=3D%ldB", diff --git a/kernel/fork.c b/kernel/fork.c index 3da0f08615a9..92698c60922e 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -583,7 +583,7 @@ static void check_mm(struct mm_struct *mm) "Please make sure 'struct resident_page_types[]' is updated as well"); =20 for (i =3D 0; i < NR_MM_COUNTERS; i++) { - long x =3D percpu_counter_sum(&mm->rss_stat[i]); + long x =3D lazy_percpu_counter_sum_local(&mm->rss_stat[i]); =20 if (unlikely(x)) { pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld Comm:%s Pid:= %d\n", @@ -688,7 +688,7 @@ 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); + lazy_percpu_counter_destroy_many(mm->rss_stat, NR_MM_COUNTERS); =20 free_mm(mm); } @@ -1083,16 +1083,11 @@ static struct mm_struct *mm_init(struct mm_struct *= mm, struct task_struct *p, if (mm_alloc_cid(mm, p)) goto fail_cid; =20 - if (percpu_counter_init_many(mm->rss_stat, 0, GFP_KERNEL_ACCOUNT, - NR_MM_COUNTERS)) - goto fail_pcpu; - + lazy_percpu_counter_init_many(mm->rss_stat, 0, NR_MM_COUNTERS); mm->user_ns =3D get_user_ns(user_ns); lru_gen_init_mm(mm); return mm; =20 -fail_pcpu: - mm_destroy_cid(mm); fail_cid: destroy_context(mm); fail_nocontext: @@ -1535,6 +1530,9 @@ static int copy_mm(u64 clone_flags, struct task_struc= t *tsk) return 0; =20 if (clone_flags & CLONE_VM) { + if (lazy_percpu_counter_upgrade_many(oldmm->rss_stat, + NR_MM_COUNTERS, GFP_KERNEL_ACCOUNT)) + return -ENOMEM; mmget(oldmm); mm =3D oldmm; } else { --=20 2.51.0