deepin-kernel/fs/proc
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
..
Kconfig proc: make config PROC_CHILDREN depend on PROC_FS 2022-10-03 14:21:43 -07:00
Makefile
array.c fs/proc: do_task_stat: Fix ESP not readable during coredump 2025-02-18 11:53:09 +08:00
base.c fs/procfs: fix the comment above proc_pid_wchan() 2025-04-14 15:49:26 +08:00
bootconfig.c
cmdline.c proc: mark /proc/cmdline as permanent 2023-02-02 22:50:02 -08:00
consoles.c proc: consoles: use console_list_lock for list iteration 2022-12-02 11:25:02 +01:00
cpuinfo.c
devices.c proc: mark more files as permanent 2022-10-03 14:21:45 -07:00
fd.c proc: Move fdinfo PTRACE_MODE_READ check into the inode .permission operation 2024-12-10 20:26:25 +08:00
fd.h fs: port ->permission() to pass mnt_idmap 2023-01-19 09:24:28 +01:00
generic.c proc: fix UAF in proc_get_inode() 2025-04-14 13:52:14 +08:00
inode.c fix proc_sys_compare() handling of in-lookup dentries 2025-07-22 16:16:37 +08:00
internal.h proc: fix UAF in proc_get_inode() 2025-04-14 13:52:14 +08:00
interrupts.c
kcore.c fs/proc/kcore.c: Clear ret value in read_kcore_iter after successful iov_iter_zero 2024-12-10 20:45:15 +08:00
kmsg.c printk changes for 6.1 2022-10-10 11:24:19 -07:00
loadavg.c proc: mark more files as permanent 2022-10-03 14:21:45 -07:00
meminfo.c mm: memtest: convert to memtest_report_meminfo() 2023-08-21 13:37:47 -07:00
namespaces.c
nommu.c proc: delete unused <linux/uaccess.h> includes 2022-07-17 17:31:39 -07:00
page.c mm: support page_mapcount() on page_has_type() pages 2024-05-05 17:12:09 +08:00
proc_net.c v6.6-fs.proc.uapi 2023-08-28 11:43:19 -07:00
proc_sysctl.c fix proc_sys_compare() handling of in-lookup dentries 2025-07-22 16:16:37 +08:00
proc_tty.c proc: delete unused <linux/uaccess.h> includes 2022-07-17 17:31:39 -07:00
root.c fs: pass the request_mask to generic_fillattr 2023-08-09 08:56:36 +02:00
self.c procfs: convert to ctime accessor functions 2023-07-24 10:30:03 +02:00
softirqs.c proc/softirqs: replace seq_printf with seq_put_decimal_ull_width 2024-12-10 20:44:22 +08:00
stat.c proc/stat: remove arch_idle_time() 2023-04-18 16:39:33 -07:00
task_mmu.c mm: fix the inaccurate memory statistics issue for users 2025-07-22 16:16:42 +08:00
task_nommu.c proc: nommu: fix empty /proc/<pid>/maps 2023-09-19 13:21:34 -07:00
thread_self.c procfs: convert to ctime accessor functions 2023-07-24 10:30:03 +02:00
uptime.c proc: mark more files as permanent 2022-10-03 14:21:45 -07:00
util.c
version.c proc: mark more files as permanent 2022-10-03 14:21:45 -07:00
vmcore.c fs/proc: fix softlockup in __read_vmcore (part 2) 2025-01-24 10:13:49 +08:00