]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob
39419c9128dd639f72b8c622a6f1b42f49a6c3b7
[thirdparty/kernel/stable-queue.git] /
1 From foo@baz Sat Apr 3 10:50:40 AM CEST 2021
2 From: Frank van der Linden <fllinden@amazon.com>
3 Date: Tue, 30 Mar 2021 18:19:08 +0000
4 Subject: mem_cgroup: make sure moving_account, move_lock_task and stat_cpu in the same cacheline
5 To: <stable@vger.kernel.org>
6 Cc: <fllinden@amazon.com>
7 Message-ID: <20210330181910.15378-4-fllinden@amazon.com>
8
9 From: Frank van der Linden <fllinden@amazon.com>
10
11 From: Aaron Lu <aaron.lu@intel.com>
12
13 commit e81bf9793b1861d74953ef041b4f6c7faecc2dbd upstream.
14
15 The LKP robot found a 27% will-it-scale/page_fault3 performance
16 regression regarding commit e27be240df53("mm: memcg: make sure
17 memory.events is uptodate when waking pollers").
18
19 What the test does is:
20 1 mkstemp() a 128M file on a tmpfs;
21 2 start $nr_cpu processes, each to loop the following:
22 2.1 mmap() this file in shared write mode;
23 2.2 write 0 to this file in a PAGE_SIZE step till the end of the file;
24 2.3 unmap() this file and repeat this process.
25 3 After 5 minutes, check how many loops they managed to complete, the
26 higher the better.
27
28 The commit itself looks innocent enough as it merely changed some event
29 counting mechanism and this test didn't trigger those events at all.
30 Perf shows increased cycles spent on accessing root_mem_cgroup->stat_cpu
31 in count_memcg_event_mm()(called by handle_mm_fault()) and in
32 __mod_memcg_state() called by page_add_file_rmap(). So it's likely due
33 to the changed layout of 'struct mem_cgroup' that either make stat_cpu
34 falling into a constantly modifying cacheline or some hot fields stop
35 being in the same cacheline.
36
37 I verified this by moving memory_events[] back to where it was:
38
39 : --- a/include/linux/memcontrol.h
40 : +++ b/include/linux/memcontrol.h
41 : @@ -205,7 +205,6 @@ struct mem_cgroup {
42 : int oom_kill_disable;
43 :
44 : /* memory.events */
45 : - atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
46 : struct cgroup_file events_file;
47 :
48 : /* protect arrays of thresholds */
49 : @@ -238,6 +237,7 @@ struct mem_cgroup {
50 : struct mem_cgroup_stat_cpu __percpu *stat_cpu;
51 : atomic_long_t stat[MEMCG_NR_STAT];
52 : atomic_long_t events[NR_VM_EVENT_ITEMS];
53 : + atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
54 :
55 : unsigned long socket_pressure;
56
57 And performance restored.
58
59 Later investigation found that as long as the following 3 fields
60 moving_account, move_lock_task and stat_cpu are in the same cacheline,
61 performance will be good. To avoid future performance surprise by other
62 commits changing the layout of 'struct mem_cgroup', this patch makes
63 sure the 3 fields stay in the same cacheline.
64
65 One concern of this approach is, moving_account and move_lock_task could
66 be modified when a process changes memory cgroup while stat_cpu is a
67 always read field, it might hurt to place them in the same cacheline. I
68 assume it is rare for a process to change memory cgroup so this should
69 be OK.
70
71 Link: https://lkml.kernel.org/r/20180528114019.GF9904@yexl-desktop
72 Link: http://lkml.kernel.org/r/20180601071115.GA27302@intel.com
73 Signed-off-by: Aaron Lu <aaron.lu@intel.com>
74 Reported-by: kernel test robot <xiaolong.ye@intel.com>
75 Cc: Johannes Weiner <hannes@cmpxchg.org>
76 Cc: Michal Hocko <mhocko@kernel.org>
77 Cc: Tejun Heo <tj@kernel.org>
78 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
79 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
80 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
81 ---
82 include/linux/memcontrol.h | 23 +++++++++++++++++++----
83 1 file changed, 19 insertions(+), 4 deletions(-)
84
85 --- a/include/linux/memcontrol.h
86 +++ b/include/linux/memcontrol.h
87 @@ -155,6 +155,15 @@ enum memcg_kmem_state {
88 KMEM_ONLINE,
89 };
90
91 +#if defined(CONFIG_SMP)
92 +struct memcg_padding {
93 + char x[0];
94 +} ____cacheline_internodealigned_in_smp;
95 +#define MEMCG_PADDING(name) struct memcg_padding name;
96 +#else
97 +#define MEMCG_PADDING(name)
98 +#endif
99 +
100 /*
101 * The memory controller data structure. The memory controller controls both
102 * page cache and RSS per cgroup. We would eventually like to provide
103 @@ -202,7 +211,6 @@ struct mem_cgroup {
104 int oom_kill_disable;
105
106 /* memory.events */
107 - atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
108 struct cgroup_file events_file;
109
110 /* protect arrays of thresholds */
111 @@ -222,19 +230,26 @@ struct mem_cgroup {
112 * mem_cgroup ? And what type of charges should we move ?
113 */
114 unsigned long move_charge_at_immigrate;
115 + /* taken only while moving_account > 0 */
116 + spinlock_t move_lock;
117 + unsigned long move_lock_flags;
118 +
119 + MEMCG_PADDING(_pad1_);
120 +
121 /*
122 * set > 0 if pages under this cgroup are moving to other cgroup.
123 */
124 atomic_t moving_account;
125 - /* taken only while moving_account > 0 */
126 - spinlock_t move_lock;
127 struct task_struct *move_lock_task;
128 - unsigned long move_lock_flags;
129
130 /* memory.stat */
131 struct mem_cgroup_stat_cpu __percpu *stat_cpu;
132 +
133 + MEMCG_PADDING(_pad2_);
134 +
135 atomic_long_t stat[MEMCG_NR_STAT];
136 atomic_long_t events[NR_VM_EVENT_ITEMS];
137 + atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
138
139 unsigned long socket_pressure;
140