From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CBF2FD37E3E for ; Wed, 14 Jan 2026 14:59:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 941766B0099; Wed, 14 Jan 2026 09:59:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 83A1F6B0093; Wed, 14 Jan 2026 09:59:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E7836B0099; Wed, 14 Jan 2026 09:59:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 337866B0093 for ; Wed, 14 Jan 2026 09:59:23 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CD3F088C9A for ; Wed, 14 Jan 2026 14:59:22 +0000 (UTC) X-FDA: 84330877764.21.FE0B549 Received: from smtpout.efficios.com (smtpout.efficios.com [158.69.130.18]) by imf06.hostedemail.com (Postfix) with ESMTP id 40EA8180008 for ; Wed, 14 Jan 2026 14:59:21 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=Wuc7IbOi; spf=pass (imf06.hostedemail.com: domain of mathieu.desnoyers@efficios.com designates 158.69.130.18 as permitted sender) smtp.mailfrom=mathieu.desnoyers@efficios.com; dmarc=pass (policy=none) header.from=efficios.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1768402761; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=v6VxYGqtWM+kAf8wDCficfmE3bkyPUAIXgNLVPI89IU=; b=WlhCCvjSukkO/6IdHdXVya4TsMalQHNmdObzE47UAh3ovzKHBdykd7mz0pLDjaJKjaSCjh gxidRzICX/eYzm9wZyyc1HumCJZeHRMoPtdrss2oNEkx5O/IQvy2quT9KbYNPlvv4LNSzL fZhU1Jz8Cof73BezoqCyGwkb12b8B5A= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=Wuc7IbOi; spf=pass (imf06.hostedemail.com: domain of mathieu.desnoyers@efficios.com designates 158.69.130.18 as permitted sender) smtp.mailfrom=mathieu.desnoyers@efficios.com; dmarc=pass (policy=none) header.from=efficios.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1768402761; a=rsa-sha256; cv=none; b=HKofl8VGh28/Q6IYs7TbhgXrpIroH0asfgmpKnWzo8j2DQWaBsSVokWfTNuL3vWDYCQE4N nXzCiNWVNgwS1XRXE8EDBKwi8TFJx6fj75COhu1/cECbh/9w0w4avJdfMSuoGHHQVMCDz0 IATPaVA96Bkvn4t+g+r1cM/B2uTfcpE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1768402760; bh=v6VxYGqtWM+kAf8wDCficfmE3bkyPUAIXgNLVPI89IU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wuc7IbOihZbh7dLtpdBR+57mh8wT4UvI/c2Qjh1dYk1s7MvpFg68wowiQrzAkuCv0 DskC01oADNYi2hzKPcT29LEPxgG8DLN/kSiHI9yFFGubtb5n4LQp2lE3iTefqeuY89 Oi1wz8HuUmWvrRxaKcHoYrNOpYnKHSWfh4tGNYhHGtiH5HC7deFcU/7lNW3lt8DVUj b222qHH0Vx9LkPxSC7WTtTW162xjSB/AqlFJAmtqc4X3PhUOVx2q0wpthGMWdwRU7i tHfrLiqjVibgJxBtOoUPp0/xno4D8RR+qKGMptfZCZ9jjpG2cSgc+xo35mO5aGTpDe zJtSTvsdVwjzw== Received: from thinkos.internal.efficios.com (mtl.efficios.com [216.120.195.104]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4drq5N3vhXzkrZ; Wed, 14 Jan 2026 09:59:20 -0500 (EST) From: Mathieu Desnoyers To: Andrew Morton Cc: linux-kernel@vger.kernel.org, Mathieu Desnoyers , "Paul E. McKenney" , Steven Rostedt , Masami Hiramatsu , Dennis Zhou , Tejun Heo , Christoph Lameter , Martin Liu , David Rientjes , christian.koenig@amd.com, Shakeel Butt , SeongJae Park , Michal Hocko , Johannes Weiner , Sweet Tea Dorminy , Lorenzo Stoakes , "Liam R . Howlett" , Mike Rapoport , Suren Baghdasaryan , Vlastimil Babka , Christian Brauner , Wei Yang , David Hildenbrand , Miaohe Lin , Al Viro , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, Yu Zhao , Roman Gushchin , Mateusz Guzik , Matthew Wilcox , Baolin Wang , Aboorva Devarajan Subject: [PATCH v16 2/3] mm: Improve RSS counter approximation accuracy for proc interfaces Date: Wed, 14 Jan 2026 09:59:14 -0500 Message-Id: <20260114145915.49926-3-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260114145915.49926-1-mathieu.desnoyers@efficios.com> References: <20260114145915.49926-1-mathieu.desnoyers@efficios.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: kny1nij1wnkja3gmwjqzy76uogx5ufcg X-Rspam-User: X-Rspamd-Queue-Id: 40EA8180008 X-Rspamd-Server: rspam08 X-HE-Tag: 1768402761-843957 X-HE-Meta: U2FsdGVkX1/3Z4w4mfekLGixVQc/xupszguBAVORQwWXhrKDPTKqHGqhMOxuvFGUHeW2Pk3+DBLoeituxrqFXhXEkfDBcFBrat3APlnkst5WBUX1QXj4zZq+ZEbh2efcjnpr5nbhSvR22dMiCiiRXE82FGScR5PNzqN9ZLnpn6XahRAuky6sFHmX1qS1O3VoL2B80hf+9Vw0LBCjn0IkgXNHTx3WK6UGWSBQAI+1zmplQ5wlQcsZZ+ox5K099tLlPSDRucN0HOTZa9wAEXeQ1IyyMkdBpVAoT2Uzy65Cot/0tjr3CD7NfnnOGk3BPo9mXFGlcxofRFa0kTVtqFF4r3tGr9URs+ur1XnNL1w70sLcom3Q+M1P2KLhD0DO08QG+oNRuNjNG2u+WskRAUJCpNFTCdfniApixHtGlWgo0lUctN+v7FOedpyxd0DxOJjBi+mDmslPb6m7SKZweNS6ddFtkwEoZBI7vwrHjFk1sMQ/GnSIFJayPkU+b2DmtDsq4FtN6TT8q9KLnArujh5l48RJEfm0QF2KaQ+xpLXXHpuF/aL4dgi5wr3PZkKoC+ZT4OOTCpggcxah+voJhMp9moI/o/WuUc+7BezTeTF1LL7+OqEfLTv7isZTTxm7dvfnKDx9lE377SPVMto34ATx2g5P9iRDj0R9QCMkf8a7rWaGVy5P4IS+SqeUUGtssJyaQVCM0ZBfv1cAKGilfmxm8+qe/5QFYjYaopMWGfZLG0+kWUmYRta2DTbonQQXtRnQDIPSO6Nh87F9eSAWUc4UeedaseaNVOIfNMT/a1SACaWF88iKoWbMm+qSKj0TI5NM7WyvLjyS+zglPhBzaPwpqdD10UM1Cgh8MTCr3AVMczudEUX7+RL+2S/BRZ8mWYVf/3ZIk5HK0PiU6+0Pl424OHF3xTm46+ueKgTQgBkB7xSRTs0fESk5a6b5nxlQKYM0TD7slU7YN3KblbB97HN fUCl4yDX KgiAqtKId8WTUxX66l5MAOh7tBCjwXL3Y1LGFNSw6BAZIIPJzQAPVpbENdeWaxpvSM3u8IkYH+6YsocLFMWF1B8hMUuFepbZ5FVdbPe4rsk8IZVA+aa26GaYwl8jrBwyr72TKkMzU4nZ3kTpJjfaoHY9vkc7fhsnNVcNtg7Wwepa+fSqX2q51hgw9KPFZN8w17t8QZryQWcUGQi7vJhD7EN0USx/jvnL6icqsmH12ozJaSOKJZ+Lhe3ne3crqfdu3JHfYSYgRp+SBjuYV8EI31gv136esyycJ4FEMikdTAVTX24EyD1EWWOQ2xBct/ZqGDNRAypKoGY5nZwlXkoEo/oF69d5qYIE6bugxtzApM4LppoC/FplbJukjwcvbngvwZBPmsNdSpnihcQsOv63+pmSD24Kgw+QpzD7cYnnmHcaBN2DSNR58KvIYvlrcPtHZdHqPIjfX7j4Tu/45thPoqILZbKgskyk0ewXu0WT/gdhfML1ED4oyEOHVwQLK1myJK+yPkEYrkbxDJtP8uC3IN47EwnRa/Zd0/bYr9AFpUknsBYj3i5Yosqb9GhBqfDlPIMxjgEf/5eFr0+xvLcxF9Dfs7G1ZE3Mue8RWFqvPr2s9WnIfQnAZNkwIPcWNIf2415fHa2aZpJKUCNkHbk6FVJHNLgBsl2G39kUsjNFGPayrIPYg553SSoB6iaxxHr2pkQo6isC81+XsLMd9cYeuY0fIw+uy3w4ZzCX+Yxpy7Jzh3rXXCZX9YMtIAUwGjT4GTbB9VxbK8cTV8vyph0zEn9fcCkCpl8E/Wvff7/LL+Lo3rDD8daD+C8Vf0+SUMRMjobHFCfFci1hrzQKnpNL+hp04cN1ztRkWnoI8OBk30Z3e48GyFiEJL7Rb4SRcIj63LX2xYcPcLf8oyXEo5rd3PqnK+Txzwjv6g7uCGO+BNX4FvkrWFuAUtbaRDgWgNiZSMP2fgsAOmsi8ojCc8hCH2Pe8ZZBo sIxNEIMU BBrZyQXOJCWO+kRGRtdGR1aPWQhxP/FDTWXgCQbSRNohVFq1bQ3+UrfycuHQ46N3fGMcCKEU6OFtVaOyMQ5MBQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Use hierarchical per-cpu counters for RSS tracking to improve the accuracy of per-mm RSS sum approximation on large many-core systems [1]. This improves the accuracy of the RSS values returned by proc interfaces. This is also a preparation step to introduce a 2-pass OOM killer task selection which leverages the approximation and accuracy ranges to quickly eliminate tasks which are outside of the range of the current selection, and thus reduce the latency introduced by execution of the OOM killer. Here is a (possibly incomplete) list of the prior approaches that were used or proposed, along with their downside: 1) Per-thread rss tracking: large error on many-thread processes. 2) Per-CPU counters: up to 12% slower for short-lived processes and 9% increased system time in make test workloads [1]. Moreover, the inaccuracy increases with O(n^2) with the number of CPUs. 3) Per-NUMA-node counters: requires atomics on fast-path (overhead), error is high with systems that have lots of NUMA nodes (32 times the number of NUMA nodes). 4) Use a percise per-cpu counter sum for each counter value query: Requires iteration on each possible CPUs for each sum, which adds overhead (and thus increases OOM killer latency) on large many-core systems running many processes. The approach proposed here is to replace the per-cpu counters by the hierarchical per-cpu counters, which bounds the inaccuracy based on the system topology with O(N*logN). * Testing results: Test hardware: 2 sockets AMD EPYC 9654 96-Core Processor (384 logical CPUs total) Methodology: Comparing the current upstream implementation with the hierarchical counters is done by keeping both implementations wired up in parallel, and running a single-process, single-threaded program which hops randomly across CPUs in the system, calling mmap(2) and munmap(2) on random CPUs, keeping track of an array of allocated mappings, randomly choosing entries to either map or unmap. get_mm_counter() is instrumented to compare the upstream counter approximation to the precise value, and print the delta when going over a given threshold. The delta of the hierarchical counter approximation to the precise value is also printed for comparison. After a few minutes running this test, the upstream implementation counter approximation reaches a 1GB delta from the precise value, compared to 80MB delta with the hierarchical counter. The hierarchical counter provides a guaranteed maximum approximation inaccuracy of 192MB on that hardware topology. * Fast path implementation comparison The new inline percpu_counter_tree_add() uses a this_cpu_add_return() for the fast path (under a certain allocation size threshold). Above that, it calls a slow path which "trickles up" the carry to upper level counters with atomic_add_return. In comparison, the upstream counters implementation calls percpu_counter_add_batch which uses this_cpu_try_cmpxchg() on the fast path, and does a raw_spin_lock_irqsave above a certain threshold. The hierarchical implementation is therefore expected to have less contention on mid-sized allocations than the upstream counters because the atomic counters tracking those bits are only shared across nearby CPUs. In comparison, the upstream counters immediately use a global spinlock when reaching the threshold. * Benchmarks Using will-it-scale page_fault1 benchmarks to compare the upstream counters to the hierarchical counters. This is done with hyperthreading disabled. The speedup is within the standard deviation of the upstream runs, so the overhead is not significant. upstream hierarchical speedup page_fault1_processes -s 100 -t 1 614783 615558 +0.1% page_fault1_threads -s 100 -t 1 612788 612447 -0.1% page_fault1_processes -s 100 -t 96 37994977 37932035 -0.2% page_fault1_threads -s 100 -t 96 2484130 2504860 +0.8% page_fault1_processes -s 100 -t 192 71262917 71118830 -0.2% page_fault1_threads -s 100 -t 192 2446437 2469296 +0.1% This change depends on the following patch: "mm: Fix OOM killer inaccuracy on large many-core systems" [2] Link: https://lore.kernel.org/lkml/20250331223516.7810-2-sweettea-kernel@dorminy.me/ # [1] Link: https://lore.kernel.org/lkml/20260114143642.47333-1-mathieu.desnoyers@efficios.com/ # [2] Signed-off-by: Mathieu Desnoyers Cc: Andrew Morton Cc: "Paul E. McKenney" Cc: Steven Rostedt Cc: Masami Hiramatsu Cc: Mathieu Desnoyers Cc: Dennis Zhou Cc: Tejun Heo Cc: Christoph Lameter Cc: Martin Liu Cc: David Rientjes Cc: christian.koenig@amd.com Cc: Shakeel Butt Cc: SeongJae Park Cc: Michal Hocko Cc: Johannes Weiner Cc: Sweet Tea Dorminy Cc: Lorenzo Stoakes Cc: "Liam R . Howlett" Cc: Mike Rapoport Cc: Suren Baghdasaryan Cc: Vlastimil Babka Cc: Christian Brauner Cc: Wei Yang Cc: David Hildenbrand Cc: Miaohe Lin Cc: Al Viro Cc: linux-mm@kvack.org Cc: linux-trace-kernel@vger.kernel.org Cc: Yu Zhao Cc: Roman Gushchin Cc: Mateusz Guzik Cc: Matthew Wilcox Cc: Baolin Wang Cc: Aboorva Devarajan --- Changes since v15: - Update the commit message to explain that this change is an improvement to the accuracy of proc interfaces approximated RSS values, and a preparation step for reducing OOM killer latency. - Rebase on v2 of "mm: Fix OOM killer and proc stats inaccuracy on large many-core systems". Changes since v14: - This change becomes the preparation for reducing the OOM killer latency. Changes since v13: - Change check_mm print format from %d to %ld. Changes since v10: - Rebase on top of mm_struct static init fixes. - Change the alignment of mm_struct flexible array to the alignment of the rss counters items (which are cacheline aligned on SMP). - Move the rss counters items to first position within the flexible array at the end of the mm_struct to place content in decreasing alignment requirement order. Changes since v8: - Use percpu_counter_tree_init_many and percpu_counter_tree_destroy_many APIs. - Remove percpu tree items allocation. Extend mm_struct size to include rss items. Those are handled through the new helpers get_rss_stat_items() and get_rss_stat_items_size() and passed as parameter to percpu_counter_tree_init_many(). Changes since v7: - Use precise sum positive API to handle a scenario where an unlucky precise sum iteration would observe negative counter values due to concurrent updates. Changes since v6: - Rebased on v6.18-rc3. - Implement get_mm_counter_sum as percpu_counter_tree_precise_sum for /proc virtual files memory state queries. Changes since v5: - Use percpu_counter_tree_approximate_sum_positive. Change since v4: - get_mm_counter needs to return 0 or a positive value. --- include/linux/mm.h | 19 ++++++++++---- include/linux/mm_types.h | 50 +++++++++++++++++++++++++++---------- include/trace/events/kmem.h | 2 +- kernel/fork.c | 22 +++++++++------- 4 files changed, 65 insertions(+), 28 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index bfa1307264df..10d62e8f35e7 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2843,38 +2843,47 @@ static inline bool get_user_page_fast_only(unsigned long addr, { return get_user_pages_fast_only(addr, 1, gup_flags, pagep) == 1; } + +static inline struct percpu_counter_tree_level_item *get_rss_stat_items(struct mm_struct *mm) +{ + unsigned long ptr = (unsigned long)mm; + + ptr += offsetof(struct mm_struct, flexible_array); + return (struct percpu_counter_tree_level_item *)ptr; +} + /* * per-process(per-mm_struct) statistics. */ static inline unsigned long get_mm_counter(struct mm_struct *mm, int member) { - return percpu_counter_read_positive(&mm->rss_stat[member]); + return percpu_counter_tree_approximate_sum_positive(&mm->rss_stat[member]); } static inline unsigned long get_mm_counter_sum(struct mm_struct *mm, int member) { - return percpu_counter_sum_positive(&mm->rss_stat[member]); + return percpu_counter_tree_precise_sum_positive(&mm->rss_stat[member]); } void mm_trace_rss_stat(struct mm_struct *mm, int member); static inline void add_mm_counter(struct mm_struct *mm, int member, long value) { - percpu_counter_add(&mm->rss_stat[member], value); + percpu_counter_tree_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]); + percpu_counter_tree_add(&mm->rss_stat[member], 1); mm_trace_rss_stat(mm, member); } static inline void dec_mm_counter(struct mm_struct *mm, int member) { - percpu_counter_dec(&mm->rss_stat[member]); + percpu_counter_tree_add(&mm->rss_stat[member], -1); mm_trace_rss_stat(mm, member); } diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 9f861ceabe61..c3e8f0ce3112 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include #include @@ -1070,6 +1070,19 @@ typedef struct { DECLARE_BITMAP(__mm_flags, NUM_MM_FLAG_BITS); } __private mm_flags_t; +/* + * The alignment of the mm_struct flexible array is based on the largest + * alignment of its content: + * __alignof__(struct percpu_counter_tree_level_item) provides a + * cacheline aligned alignment on SMP systems, else alignment on + * unsigned long on UP systems. + */ +#ifdef CONFIG_SMP +# define __mm_struct_flexible_array_aligned __aligned(__alignof__(struct percpu_counter_tree_level_item)) +#else +# define __mm_struct_flexible_array_aligned __aligned(__alignof__(unsigned long)) +#endif + struct kioctx_table; struct iommu_mm_data; struct mm_struct { @@ -1215,7 +1228,7 @@ struct mm_struct { unsigned long saved_e_flags; #endif - struct percpu_counter rss_stat[NR_MM_COUNTERS]; + struct percpu_counter_tree rss_stat[NR_MM_COUNTERS]; struct linux_binfmt *binfmt; @@ -1326,10 +1339,13 @@ struct mm_struct { } __randomize_layout; /* - * The mm_cpumask needs to be at the end of mm_struct, because it - * is dynamically sized based on nr_cpu_ids. + * The rss hierarchical counter items, mm_cpumask, and mm_cid + * masks need to be at the end of mm_struct, because they are + * dynamically sized based on nr_cpu_ids. + * The content of the flexible array needs to be placed in + * decreasing alignment requirement order. */ - char flexible_array[] __aligned(__alignof__(unsigned long)); + char flexible_array[] __mm_struct_flexible_array_aligned; }; /* Copy value to the first system word of mm flags, non-atomically. */ @@ -1368,22 +1384,28 @@ extern struct mm_struct init_mm; #define MM_STRUCT_FLEXIBLE_ARRAY_INIT \ { \ - [0 ... sizeof(cpumask_t) + MM_CID_STATIC_SIZE + PERCPU_COUNTER_TREE_ITEMS_STATIC_SIZE - 1] = 0 \ + [0 ... PERCPU_COUNTER_TREE_ITEMS_STATIC_SIZE + sizeof(cpumask_t) + MM_CID_STATIC_SIZE - 1] = 0 \ } -/* Pointer magic because the dynamic array size confuses some compilers. */ -static inline void mm_init_cpumask(struct mm_struct *mm) +static inline size_t get_rss_stat_items_size(void) { - unsigned long cpu_bitmap = (unsigned long)mm; - - cpu_bitmap += offsetof(struct mm_struct, flexible_array); - cpumask_clear((struct cpumask *)cpu_bitmap); + return percpu_counter_tree_items_size() * NR_MM_COUNTERS; } /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */ static inline cpumask_t *mm_cpumask(struct mm_struct *mm) { - return (struct cpumask *)&mm->flexible_array; + unsigned long ptr = (unsigned long)mm; + + ptr += offsetof(struct mm_struct, flexible_array); + /* Skip RSS stats counters. */ + ptr += get_rss_stat_items_size(); + return (struct cpumask *)ptr; +} + +static inline void mm_init_cpumask(struct mm_struct *mm) +{ + cpumask_clear((struct cpumask *)mm_cpumask(mm)); } #ifdef CONFIG_LRU_GEN @@ -1475,6 +1497,8 @@ static inline cpumask_t *mm_cpus_allowed(struct mm_struct *mm) unsigned long bitmap = (unsigned long)mm; bitmap += offsetof(struct mm_struct, flexible_array); + /* Skip RSS stats counters. */ + bitmap += get_rss_stat_items_size(); /* Skip cpu_bitmap */ bitmap += cpumask_size(); return (struct cpumask *)bitmap; diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h index 7f93e754da5c..91c81c44f884 100644 --- a/include/trace/events/kmem.h +++ b/include/trace/events/kmem.h @@ -442,7 +442,7 @@ TRACE_EVENT(rss_stat, __entry->mm_id = mm_ptr_to_hash(mm); __entry->curr = !!(current->mm == mm); __entry->member = member; - __entry->size = (percpu_counter_sum_positive(&mm->rss_stat[member]) + __entry->size = (percpu_counter_tree_approximate_sum_positive(&mm->rss_stat[member]) << PAGE_SHIFT); ), diff --git a/kernel/fork.c b/kernel/fork.c index b1f3915d5f8e..8b56d81af734 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -133,6 +133,11 @@ */ #define MAX_THREADS FUTEX_TID_MASK +/* + * Batch size of rss stat approximation + */ +#define RSS_STAT_BATCH_SIZE 32 + /* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -626,14 +631,12 @@ static void check_mm(struct mm_struct *mm) "Please make sure 'struct resident_page_types[]' is updated as well"); for (i = 0; i < NR_MM_COUNTERS; i++) { - long x = percpu_counter_sum(&mm->rss_stat[i]); - - if (unlikely(x)) { + if (unlikely(percpu_counter_tree_precise_compare_value(&mm->rss_stat[i], 0) != 0)) pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld Comm:%s Pid:%d\n", - mm, resident_page_types[i], x, + mm, resident_page_types[i], + percpu_counter_tree_precise_sum(&mm->rss_stat[i]), current->comm, task_pid_nr(current)); - } } if (mm_pgtables_bytes(mm)) @@ -731,7 +734,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); + percpu_counter_tree_destroy_many(mm->rss_stat, NR_MM_COUNTERS); free_mm(mm); } @@ -1123,8 +1126,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, if (mm_alloc_cid(mm, p)) goto fail_cid; - if (percpu_counter_init_many(mm->rss_stat, 0, GFP_KERNEL_ACCOUNT, - NR_MM_COUNTERS)) + if (percpu_counter_tree_init_many(mm->rss_stat, get_rss_stat_items(mm), + NR_MM_COUNTERS, RSS_STAT_BATCH_SIZE, + GFP_KERNEL_ACCOUNT)) goto fail_pcpu; mm->user_ns = get_user_ns(user_ns); @@ -3006,7 +3010,7 @@ void __init mm_cache_init(void) * dynamically sized based on the maximum CPU number this system * can have, taking hotplug into account (nr_cpu_ids). */ - mm_size = sizeof(struct mm_struct) + cpumask_size() + mm_cid_size(); + mm_size = sizeof(struct mm_struct) + cpumask_size() + mm_cid_size() + get_rss_stat_items_size(); mm_cachep = kmem_cache_create_usercopy("mm_struct", mm_size, ARCH_MIN_MMSTRUCT_ALIGN, -- 2.39.5