1 From 3e8589963773a5c23e2f1fe4bcad0e9a90b7f471 Mon Sep 17 00:00:00 2001
2 From: Jiri Slaby <jslaby@suse.cz>
3 Date: Fri, 31 May 2019 22:30:26 -0700
4 Subject: memcg: make it work on sparse non-0-node systems
6 From: Jiri Slaby <jslaby@suse.cz>
8 commit 3e8589963773a5c23e2f1fe4bcad0e9a90b7f471 upstream.
10 We have a single node system with node 0 disabled:
11 Scanning NUMA topology in Northbridge 24
12 Number of physical nodes 2
13 Skipping disabled node 0
14 Node 1 MemBase 0000000000000000 Limit 00000000fbff0000
15 NODE_DATA(1) allocated [mem 0xfbfda000-0xfbfeffff]
17 This causes crashes in memcg when system boots:
18 BUG: unable to handle kernel NULL pointer dereference at 0000000000000008
19 #PF error: [normal kernel read fault]
21 RIP: 0010:list_lru_add+0x94/0x170
25 dput.part.34+0xfc/0x110
27 task_work_run+0x9f/0xc0
28 exit_to_usermode_loop+0xf5/0x100
30 It is reproducible as far as 4.12. I did not try older kernels. You have
31 to have a new enough systemd, e.g. 241 (the reason is unknown -- was not
32 investigated). Cannot be reproduced with systemd 234.
34 The system crashes because the size of lru array is never updated in
35 memcg_update_all_list_lrus and the reads are past the zero-sized array,
36 causing dereferences of random memory.
38 The root cause are list_lru_memcg_aware checks in the list_lru code. The
39 test in list_lru_memcg_aware is broken: it assumes node 0 is always
40 present, but it is not true on some systems as can be seen above.
42 So fix this by avoiding checks on node 0. Remember the memcg-awareness by
43 a bool flag in struct list_lru.
45 Link: http://lkml.kernel.org/r/20190522091940.3615-1-jslaby@suse.cz
46 Fixes: 60d3fd32a7a9 ("list_lru: introduce per-memcg lists")
47 Signed-off-by: Jiri Slaby <jslaby@suse.cz>
48 Acked-by: Michal Hocko <mhocko@suse.com>
49 Suggested-by: Vladimir Davydov <vdavydov.dev@gmail.com>
50 Acked-by: Vladimir Davydov <vdavydov.dev@gmail.com>
51 Reviewed-by: Shakeel Butt <shakeelb@google.com>
52 Cc: Johannes Weiner <hannes@cmpxchg.org>
53 Cc: Raghavendra K T <raghavendra.kt@linux.vnet.ibm.com>
54 Cc: <stable@vger.kernel.org>
55 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
56 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
57 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
60 include/linux/list_lru.h | 1 +
61 mm/list_lru.c | 8 +++-----
62 2 files changed, 4 insertions(+), 5 deletions(-)
64 --- a/include/linux/list_lru.h
65 +++ b/include/linux/list_lru.h
66 @@ -51,6 +51,7 @@ struct list_lru {
67 struct list_lru_node *node;
68 #ifdef CONFIG_MEMCG_KMEM
69 struct list_head list;
76 @@ -42,11 +42,7 @@ static void list_lru_unregister(struct l
77 #ifdef CONFIG_MEMCG_KMEM
78 static inline bool list_lru_memcg_aware(struct list_lru *lru)
81 - * This needs node 0 to be always present, even
82 - * in the systems supporting sparse numa ids.
84 - return !!lru->node[0].memcg_lrus;
85 + return lru->memcg_aware;
88 static inline struct list_lru_one *
89 @@ -389,6 +385,8 @@ static int memcg_init_list_lru(struct li
93 + lru->memcg_aware = memcg_aware;