Skip to content

Commit 82241a8

Browse files
Baolin Wangakpm00
authored andcommitted
mm: fix the inaccurate memory statistics issue for users
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 f1a7941 ("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 f1a7941) and latest Linus tree (4c06e63) 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: f1a7941 ("mm: convert mm's rss stats into percpu_counter") Signed-off-by: Baolin Wang <[email protected]> Reviewed-by: Aboorva Devarajan <[email protected]> Tested-by: Aboorva Devarajan <[email protected]> Tested-by Donet Tom <[email protected]> Acked-by: Shakeel Butt <[email protected]> Acked-by: SeongJae Park <[email protected]> Acked-by: Michal Hocko <[email protected]> Reviewed-by: Vlastimil Babka <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Liam Howlett <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Suren Baghdasaryan <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent bd225b9 commit 82241a8

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

fs/proc/task_mmu.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
3636
unsigned long text, lib, swap, anon, file, shmem;
3737
unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
3838

39-
anon = get_mm_counter(mm, MM_ANONPAGES);
40-
file = get_mm_counter(mm, MM_FILEPAGES);
41-
shmem = get_mm_counter(mm, MM_SHMEMPAGES);
39+
anon = get_mm_counter_sum(mm, MM_ANONPAGES);
40+
file = get_mm_counter_sum(mm, MM_FILEPAGES);
41+
shmem = get_mm_counter_sum(mm, MM_SHMEMPAGES);
4242

4343
/*
4444
* Note: to minimize their overhead, mm maintains hiwater_vm and
@@ -59,7 +59,7 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
5959
text = min(text, mm->exec_vm << PAGE_SHIFT);
6060
lib = (mm->exec_vm << PAGE_SHIFT) - text;
6161

62-
swap = get_mm_counter(mm, MM_SWAPENTS);
62+
swap = get_mm_counter_sum(mm, MM_SWAPENTS);
6363
SEQ_PUT_DEC("VmPeak:\t", hiwater_vm);
6464
SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm);
6565
SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm);
@@ -92,12 +92,12 @@ unsigned long task_statm(struct mm_struct *mm,
9292
unsigned long *shared, unsigned long *text,
9393
unsigned long *data, unsigned long *resident)
9494
{
95-
*shared = get_mm_counter(mm, MM_FILEPAGES) +
96-
get_mm_counter(mm, MM_SHMEMPAGES);
95+
*shared = get_mm_counter_sum(mm, MM_FILEPAGES) +
96+
get_mm_counter_sum(mm, MM_SHMEMPAGES);
9797
*text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
9898
>> PAGE_SHIFT;
9999
*data = mm->data_vm + mm->stack_vm;
100-
*resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
100+
*resident = *shared + get_mm_counter_sum(mm, MM_ANONPAGES);
101101
return mm->total_vm;
102102
}
103103

include/linux/mm.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,11 @@ static inline unsigned long get_mm_counter(struct mm_struct *mm, int member)
25682568
return percpu_counter_read_positive(&mm->rss_stat[member]);
25692569
}
25702570

2571+
static inline unsigned long get_mm_counter_sum(struct mm_struct *mm, int member)
2572+
{
2573+
return percpu_counter_sum_positive(&mm->rss_stat[member]);
2574+
}
2575+
25712576
void mm_trace_rss_stat(struct mm_struct *mm, int member);
25722577

25732578
static inline void add_mm_counter(struct mm_struct *mm, int member, long value)

0 commit comments

Comments
 (0)