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