From mboxrd@z Thu Jan 1 00:00:00 1970 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 8B7FE4A0C for ; Thu, 27 Nov 2025 23:36:47 +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=1764286610; cv=none; b=LJiqFqGbEO8p2lF7I/dcZVeaPgcK8MluaBJxu7fb589YEILbEWUuI93HJUdxRmAsRykAvLagQh9p6YXjRW0nJ/9eUEeQa573dWWCqDXz/kIBTTeRQIEcaMod+5soDvWgNRgyzWJz0V/on4ZwWPnJC45gD9pGXbAkMghG0dmHUws= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764286610; c=relaxed/simple; bh=HC3hMFSjzM2LjMfrpRXy/pb5SIqgpkYfJ567CbKwy04=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=OGG7ukVOkcYY24ywMrNg7yhskrDyS6MLV602fW4ULXUoj+GkVcr7dUlmPxQfbcSsN2JPZNnwkAH9WrtgINqUdBDyf6kr8buZARHgemLB9vI9rDyVe3odEQZ089DnsGq1Rtmy9d1zFP6sDT+O5a8m6iewdbWItxvzu3dpgzi1oHQ= 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=Q3dhaBrY; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=WMf0YdHG; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=J8O60E8/; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=NMupcRbn; 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="Q3dhaBrY"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="WMf0YdHG"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="J8O60E8/"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="NMupcRbn" 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 D5BDF5BD07; Thu, 27 Nov 2025 23:36:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1764286604; 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; bh=wSQDXIqh9OVz4pGakQfvy/4iJYn/YQqDAmAqSfiXVOM=; b=Q3dhaBrY69YQefYdLldfgAKhjkmGZ/6Yl2OUU5UT4OdJ9i4dH71Drz/atF6fG+pJXR7k7b QVcCPHC2nxmBcHgzi8APxFAkeFwDdZbOXuZD836r3enAwso/w9PU3JRWYbLa57yM5C1SF/ EW4sdRWem+GI6+WwsgwfWFNx82JSD6g= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1764286604; 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; bh=wSQDXIqh9OVz4pGakQfvy/4iJYn/YQqDAmAqSfiXVOM=; b=WMf0YdHGmcscI59YS1mHkPvtzW6mT/anJnr8YIb5UvRctUJ/3Uze/OeCQi7Ib/358RqRGe so+dhcU2lvnpK0AA== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="J8O60E8/"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=NMupcRbn DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1764286602; 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; bh=wSQDXIqh9OVz4pGakQfvy/4iJYn/YQqDAmAqSfiXVOM=; b=J8O60E8/jt/AU6ulL8BaoTiEfAgmwWWjpzmWtR99meSLDIZk3C0Txk8J3hfhtpjztKkQ32 /ch4Tz63HbVt7UqL/XtQZ8zo0POVKlaSgQQYeW/aUud9EbDTe+g8v7CQDMsNgex0WzgM4r TyhOaFRQ581FKK8bLY/LODaDWExYS7E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1764286602; 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; bh=wSQDXIqh9OVz4pGakQfvy/4iJYn/YQqDAmAqSfiXVOM=; b=NMupcRbnkA3I49Q3T13JTL68MxJ47UcX7F/Tz6U5LlRcIzQ7I2MUGO1PibULbpABwUYIJm aI/NmV5mWKXNk7CQ== 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 838673EA63; Thu, 27 Nov 2025 23:36:42 +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 qLQ6GYrgKGkIGQAAD6G6ig (envelope-from ); Thu, 27 Nov 2025 23:36:42 +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 0/4] Optimize rss_stat initialization/teardown for single-threaded tasks Date: Thu, 27 Nov 2025 18:36:27 -0500 Message-ID: <20251127233635.4170047-1-krisman@suse.de> X-Mailer: git-send-email 2.51.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCPT_COUNT_TWELVE(0.00)[18]; URIBL_BLOCKED(0.00)[imap1.dmz-prg2.suse.org:rdns,imap1.dmz-prg2.suse.org:helo]; MIME_TRACE(0.00)[0:+]; FUZZY_RATELIMITED(0.00)[rspamd.com]; ARC_NA(0.00)[]; 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)[]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; 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)[]; DWL_DNSWL_BLOCKED(0.00)[suse.de:dkim]; DKIM_TRACE(0.00)[suse.de:+]; FREEMAIL_ENVRCPT(0.00)[gmail.com] X-Spam-Level: X-Spam-Score: -3.01 X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Queue-Id: D5BDF5BD07 X-Rspamd-Action: no action X-Spam-Flag: NO The cost of the pcpu memory allocation is non-negligible for systems with many cpus, and it is quite visible when forking a new task, as reported in a few occasions. In particular, Jan Kara reported the commit introducing per-cpu counters for rss_stat caused a 10% regression of system time for gitsource in his system [1]. In that same occasion, Jan suggested we special-cased the single-threaded case: since we know there won't be frequent remote updates of rss_stats for single-threaded applications, we could special case it with a local counter for most updates, and an atomic counter for the infrequent remote updates. This patchset implements this idea. It exposes a dual-mode counter that starts as a simple counter, cheap to initialize on single-threaded tasks, that can be upgraded inflight to a fully-fledged per cpu counter later. Patch 3 then modifies the rss_stat counters to use that structure, forcing the upgrade as soon as a second task sharing the mm_struct is spawned. By delaying the initialization cost until the MM is shared, we cover single-threaded applications fairly cheaply, while not penalizing applications that spawn multiple threads. On a 256c system, where the pcpu allocation of the rss_stats is quite noticeable, this has reduced the wall-clock time between 6% 15% (depending on the number of cores) of an artificial fork-intensive microbenchmark (calling /bin/true in a loop). In a more realistic benchmark, it showed an improvement of 1.5% on kernbench elapsed time. More performance data, including profilings is available in the patch modifying the rss_stat counters. While this patch exposes a single users of this API, this should be useful in more cases. This is why I made it into a proper API. In addition, considering the recent efforts in this area, such as hierarchical per-cpu counters which are orthogonal to this work because they improve multi-threaded workloads, abstracting this with a new API could help the merging of both works. Finally, this is a RFC because it is an early work. in particular, I'd be interested in more benchmarks suggestions, and I'd like feedback whether this new interface should be implemented inside percpu_counters as lazy counters or as a completely separated interface. Thanks, [1] https://lore.kernel.org/all/20230608111408.s2minsenlcjow7q3@quack3 --- Cc: linux-kernel@vger.kernel.org Cc: jack@suse.cz Cc: Mateusz Guzik Cc: Shakeel Butt Cc: Michal Hocko Cc: Mathieu Desnoyers Cc: Dennis Zhou Cc: Tejun Heo Cc: Christoph Lameter Cc: Andrew Morton Cc: David Hildenbrand Cc: Lorenzo Stoakes Cc: "Liam R. Howlett" Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Suren Baghdasaryan Gabriel Krisman Bertazi (4): lib/percpu_counter: Split out a helper to insert into hotplug list lib: Support lazy initialization of per-cpu counters mm: Avoid percpu MM counters on single-threaded tasks mm: Split a slow path for updating mm counters arch/s390/mm/gmap_helpers.c | 4 +- arch/s390/mm/pgtable.c | 4 +- fs/exec.c | 2 +- include/linux/lazy_percpu_counter.h | 145 ++++++++++++++++++++++++++++ include/linux/mm.h | 26 ++--- include/linux/mm_types.h | 4 +- include/linux/percpu_counter.h | 5 +- include/trace/events/kmem.h | 4 +- kernel/events/uprobes.c | 2 +- kernel/fork.c | 14 ++- lib/percpu_counter.c | 68 ++++++++++--- mm/filemap.c | 2 +- mm/huge_memory.c | 22 ++--- mm/khugepaged.c | 6 +- mm/ksm.c | 2 +- mm/madvise.c | 2 +- mm/memory.c | 20 ++-- mm/migrate.c | 2 +- mm/migrate_device.c | 2 +- mm/rmap.c | 16 +-- mm/swapfile.c | 6 +- mm/userfaultfd.c | 2 +- 22 files changed, 276 insertions(+), 84 deletions(-) create mode 100644 include/linux/lazy_percpu_counter.h -- 2.51.0