Go to file
Baolin Wang 2782358964 mm: fix the inaccurate memory statistics issue for users
commit 82241a83cd15aaaf28200a40ad1a8b480012edaf upstream.

On some large machines with a high number of CPUs running a 64K pagesize
kernel, we found that the 'RES' field is always 0 displayed by the top
command for some processes, which will cause a lot of confusion for users.

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 875525 root      20   0   12480      0      0 R   0.3   0.0   0:00.08 top
      1 root      20   0  172800      0      0 S   0.0   0.0   0:04.52 systemd

The main reason is that the batch size of the percpu counter is quite
large on these machines, caching a significant percpu value, since
converting mm's rss stats into percpu_counter by commit f1a7941243 ("mm:
convert mm's rss stats into percpu_counter").  Intuitively, the batch
number should be optimized, but on some paths, performance may take
precedence over statistical accuracy.  Therefore, introducing a new
interface to add the percpu statistical count and display it to users,
which can remove the confusion.  In addition, this change is not expected
to be on a performance-critical path, so the modification should be
acceptable.

In addition, the 'mm->rss_stat' is updated by using add_mm_counter() and
dec/inc_mm_counter(), which are all wrappers around
percpu_counter_add_batch().  In percpu_counter_add_batch(), there is
percpu batch caching to avoid 'fbc->lock' contention.  This patch changes
task_mem() and task_statm() to get the accurate mm counters under the
'fbc->lock', but this should not exacerbate kernel 'mm->rss_stat' lock
contention due to the percpu batch caching of the mm counters.  The
following test also confirm the theoretical analysis.

I run the stress-ng that stresses anon page faults in 32 threads on my 32
cores machine, while simultaneously running a script that starts 32
threads to busy-loop pread each stress-ng thread's /proc/pid/status
interface.  From the following data, I did not observe any obvious impact
of this patch on the stress-ng tests.

w/o patch:
stress-ng: info:  [6848]          4,399,219,085,152 CPU Cycles          67.327 B/sec
stress-ng: info:  [6848]          1,616,524,844,832 Instructions          24.740 B/sec (0.367 instr. per cycle)
stress-ng: info:  [6848]          39,529,792 Page Faults Total           0.605 M/sec
stress-ng: info:  [6848]          39,529,792 Page Faults Minor           0.605 M/sec

w/patch:
stress-ng: info:  [2485]          4,462,440,381,856 CPU Cycles          68.382 B/sec
stress-ng: info:  [2485]          1,615,101,503,296 Instructions          24.750 B/sec (0.362 instr. per cycle)
stress-ng: info:  [2485]          39,439,232 Page Faults Total           0.604 M/sec
stress-ng: info:  [2485]          39,439,232 Page Faults Minor           0.604 M/sec

On comparing a very simple app which just allocates & touches some
memory against v6.1 (which doesn't have f1a7941243) and latest Linus
tree (4c06e63b9203) I can see that on latest Linus tree the values for
VmRSS, RssAnon and RssFile from /proc/self/status are all zeroes while
they do report values on v6.1 and a Linus tree with this patch.

Link: https://lkml.kernel.org/r/f4586b17f66f97c174f7fd1f8647374fdb53de1c.1749119050.git.baolin.wang@linux.alibaba.com
Fixes: f1a7941243 ("mm: convert mm's rss stats into percpu_counter")
Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Reviewed-by: Aboorva Devarajan <aboorvad@linux.ibm.com>
Tested-by: Aboorva Devarajan <aboorvad@linux.ibm.com>
Tested-by Donet Tom <donettom@linux.ibm.com>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: SeongJae Park <sj@kernel.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Cc: David Hildenbrand <david@redhat.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
(cherry picked from commit 56995226431a37182128d0b0adf39cd003bf94d7)
2025-07-22 16:16:42 +08:00
.github/workflows CI: Add patch check by using build kernel sw8a 2025-07-11 02:39:50 +08:00
Documentation x86/bugs: Add a Transient Scheduler Attacks mitigation 2025-07-14 10:42:19 +08:00
LICENSES LICENSES: add 0BSD license text 2025-01-18 13:07:45 +08:00
arch KVM: SVM: Reject SEV{-ES} intra host migration if vCPU creation is in-flight 2025-07-22 16:16:41 +08:00
block scsi: sd_zbc: block: Respect bio vector limits for REPORT ZONES buffer 2025-05-26 10:36:22 +08:00
certs sign-file,extract-cert: use pkcs11 provider for OPENSSL MAJOR >= 3 2025-05-09 22:54:46 +08:00
crypto crypto: xts - Only add ecb if it is not already there 2025-06-20 14:38:41 +08:00
deepin deepin: OWNERS: Update OWNERS 2025-05-29 18:26:52 +08:00
drivers Revert "ACPI: battery: negate current when discharging" 2025-07-22 16:16:42 +08:00
fs mm: fix the inaccurate memory statistics issue for users 2025-07-22 16:16:42 +08:00
include mm: fix the inaccurate memory statistics issue for users 2025-07-22 16:16:42 +08:00
init sched/isolation: Make CONFIG_CPU_ISOLATION depend on CONFIG_SMP 2025-05-09 23:02:50 +08:00
io_uring io_uring: fix task leak issue in io_wq_create() 2025-07-03 11:32:06 +08:00
ipc ipc: fix to protect IPCS lookups using RCU 2025-07-03 11:31:56 +08:00
kernel perf: Revert to requiring CAP_SYS_ADMIN for uprobes 2025-07-22 16:16:37 +08:00
lib maple_tree: fix mt_destroy_walk() on root leaf node 2025-07-22 16:16:42 +08:00
mm mm/memory-failure: fix crash in split_huge_page_to_list from soft_offline_page 2025-07-22 00:09:43 +08:00
net wifi: prevent A-MSDU attacks in mesh networks 2025-07-22 16:16:41 +08:00
rust rust: module: place cleanup_module() in .exit.text section 2025-07-09 09:49:55 +08:00
samples samples/bpf: Fix compilation failure for samples/bpf on LoongArch Fedora 2025-06-10 10:34:50 +08:00
scripts kbuild: rpm-pkg: simplify installkernel %post 2025-07-09 09:50:04 +08:00
security selinux: fix selinux_xfrm_alloc_user() to set correct ctx_len 2025-07-03 11:32:08 +08:00
sound ASoC: cs35l56: probe() should fail if the device ID is not recognized 2025-07-22 16:16:37 +08:00
tools kallsyms: fix build without execinfo 2025-07-22 16:16:42 +08:00
usr kbuild: hdrcheck: fix cross build with clang 2025-03-18 15:26:16 +08:00
virt KVM: Discard zero mask with function kvm_dirty_ring_reset 2025-06-06 14:45:06 +08:00
.clang-format iommu: Add for_each_group_device() 2023-05-23 08:15:51 +02:00
.cocciconfig
.get_maintainer.ignore get_maintainer: add Alan to .get_maintainer.ignore 2022-08-20 15:17:44 -07:00
.gitattributes .gitattributes: set diff driver for Rust source code files 2023-05-31 17:48:25 +02:00
.gitignore Remove *.orig pattern from .gitignore 2024-12-10 20:42:03 +08:00
.mailmap 20 hotfixes. 12 are cc:stable and the remainder address post-6.5 issues 2023-10-24 09:52:16 -10:00
.rustfmt.toml rust: add `.rustfmt.toml` 2022-09-28 09:02:20 +02:00
COPYING COPYING: state that all contributions really are covered by this file 2020-02-10 13:32:20 -08:00
CREDITS fs: Remove NTFS classic 2024-12-10 20:39:15 +08:00
Kbuild Kbuild updates for v6.1 2022-10-10 12:00:45 -07:00
Kconfig kbuild: ensure full rebuild when the compiler is updated 2020-05-12 13:28:33 +09:00
MAINTAINERS selftests: ifs: verify test interfaces are created by the driver 2025-07-01 11:32:36 +08:00
Makefile Linux 6.6.98 2025-07-16 20:42:49 +08:00
README Drop all 00-INDEX files from Documentation/ 2018-09-09 15:08:58 -06:00

README

Linux kernel
============

There are several guides for kernel developers and users. These guides can
be rendered in a number of formats, like HTML and PDF. Please read
Documentation/admin-guide/README.rst first.

In order to build the documentation, use ``make htmldocs`` or
``make pdfdocs``.  The formatted documentation can also be read online at:

    https://www.kernel.org/doc/html/latest/

There are various text files in the Documentation/ subdirectory,
several of them using the Restructured Text markup notation.

Please read the Documentation/process/changes.rst file, as it contains the
requirements for building and running the kernel, and information about
the problems which may result by upgrading your kernel.