46e0aeaf4ce6f6b415f16b82a0b893a1dc8599b2
[openwrt/staging/stintel.git] /
1 From 86099deff5abf5f63643eecaedb4c11ae77474ce Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Ma=C3=ADra=20Canal?= <mcanal@igalia.com>
3 Date: Thu, 24 Oct 2024 08:03:19 -0300
4 Subject: [PATCH 1349/1350] cgroup: Use kernel command line to disable memory
5 cgroup
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 Commit 94a23e978235 ("cgroup: Disable cgroup "memory" by default")
11 disabled the memory cgroup by default when initing the cgroups. However,
12 it's possible to disable the memory cgroup by a kernel command line.
13 Hard-coding such a feature can be problematic as some memory management
14 features depend on the order that things are set.
15
16 For example, it is possible to see a NULL pointer dereference caused by
17 commit 94a23e978235cd35f38075072b34152b2b667e6e. The NULL pointer
18 dereference is triggered by the memory shrinker and ends up in a kernel
19 crash.
20
21 [ 50.028629] ==================================================================
22 [ 50.028645] BUG: KASAN: null-ptr-deref in do_shrink_slab+0x1fc/0x978
23 [ 50.028663] Write of size 8 at addr 0000000000000000 by task gfxrecon-replay/1965
24
25 [ 50.028676] CPU: 3 UID: 1000 PID: 1965 Comm: gfxrecon-replay Tainted: G C 6.12.0-rc4-v8-thp-kasan+ #85
26 [ 50.028685] Tainted: [C]=CRAP
27 [ 50.028689] Hardware name: Raspberry Pi 5 Model B Rev 1.0 (DT)
28 [ 50.028694] Call trace:
29 [ 50.028697] dump_backtrace+0xfc/0x120
30 [ 50.028706] show_stack+0x24/0x38
31 [ 50.028711] dump_stack_lvl+0x40/0x88
32 [ 50.028720] print_report+0xe4/0x708
33 [ 50.028728] kasan_report+0xcc/0x130
34 [ 50.028733] kasan_check_range+0x254/0x298
35 [ 50.028738] __kasan_check_write+0x20/0x30
36 [ 50.028745] do_shrink_slab+0x1fc/0x978
37 [ 50.028751] shrink_slab+0x318/0xc38
38 [ 50.028756] shrink_one+0x254/0x6d8
39 [ 50.028762] shrink_node+0x26b4/0x2848
40 [ 50.028767] do_try_to_free_pages+0x3e4/0x1190
41 [ 50.028773] try_to_free_pages+0x5a4/0xb40
42 [ 50.028778] __alloc_pages_direct_reclaim+0x144/0x298
43 [ 50.028787] __alloc_pages_slowpath+0x5c4/0xc70
44 [ 50.028793] __alloc_pages_noprof+0x4a8/0x6a8
45 [ 50.028800] __folio_alloc_noprof+0x24/0xa8
46 [ 50.028806] shmem_alloc_and_add_folio+0x2ec/0xce0
47 [ 50.028812] shmem_get_folio_gfp+0x380/0xc20
48 [ 50.028818] shmem_read_folio_gfp+0xe0/0x160
49 [ 50.028824] drm_gem_get_pages+0x238/0x620 [drm]
50 [ 50.029039] drm_gem_shmem_get_pages_sgt+0xd8/0x4b8 [drm_shmem_helper]
51 [ 50.029053] v3d_bo_create_finish+0x58/0x1e0 [v3d]
52 [ 50.029083] v3d_create_bo_ioctl+0xac/0x210 [v3d]
53 [ 50.029105] drm_ioctl_kernel+0x1d8/0x2b8 [drm]
54 [ 50.029220] drm_ioctl+0x4b4/0x920 [drm]
55 [ 50.029330] __arm64_sys_ioctl+0x11c/0x160
56 [ 50.029337] invoke_syscall+0x88/0x268
57 [ 50.029345] el0_svc_common+0x160/0x1d8
58 [ 50.029351] do_el0_svc+0x50/0x68
59 [ 50.029358] el0_svc+0x34/0x80
60 [ 50.029364] el0t_64_sync_handler+0x84/0x100
61 [ 50.029371] el0t_64_sync+0x190/0x198
62 [ 50.029376] ==================================================================
63
64 This happens because the memory shrinker is unaware that we are
65 artificially disabling the memory cgroups and therefore it doesn't
66 allocate `nr_deferred` (as it would if we used the kernel command line).
67
68 To avoid such an issue, revert the artificial disablement and disable it
69 through the command line. If a user wants to enable the feature, it can
70 use the `cgroup_enable=` command line.
71
72 Signed-off-by: MaĆ­ra Canal <mcanal@igalia.com>
73 ---
74 arch/arm/boot/dts/broadcom/bcm2708-rpi-bt.dtsi | 2 +-
75 arch/arm/boot/dts/broadcom/bcm270x.dtsi | 2 +-
76 arch/arm/boot/dts/broadcom/bcm2711-rpi-cm4s.dts | 2 +-
77 arch/arm/boot/dts/broadcom/bcm2711-rpi-ds.dtsi | 2 +-
78 arch/arm/boot/dts/broadcom/bcm271x-rpi-bt.dtsi | 2 +-
79 arch/arm64/boot/dts/broadcom/bcm2712-rpi.dtsi | 2 +-
80 kernel/cgroup/cgroup.c | 15 +--------------
81 7 files changed, 7 insertions(+), 20 deletions(-)
82
83 --- a/arch/arm/boot/dts/broadcom/bcm2708-rpi-bt.dtsi
84 +++ b/arch/arm/boot/dts/broadcom/bcm2708-rpi-bt.dtsi
85 @@ -24,7 +24,7 @@
86
87 / {
88 chosen {
89 - bootargs = "coherent_pool=1M 8250.nr_uarts=1 snd_bcm2835.enable_headphones=0";
90 + bootargs = "coherent_pool=1M 8250.nr_uarts=1 snd_bcm2835.enable_headphones=0 cgroup_disable=memory";
91 };
92
93 aliases {
94 --- a/arch/arm/boot/dts/broadcom/bcm270x.dtsi
95 +++ b/arch/arm/boot/dts/broadcom/bcm270x.dtsi
96 @@ -4,7 +4,7 @@
97 / {
98 chosen: chosen {
99 // Disable audio by default
100 - bootargs = "coherent_pool=1M snd_bcm2835.enable_headphones=0";
101 + bootargs = "coherent_pool=1M snd_bcm2835.enable_headphones=0 cgroup_disable=memory";
102 stdout-path = "serial0:115200n8";
103 };
104
105 --- a/arch/arm/boot/dts/broadcom/bcm2711-rpi-cm4s.dts
106 +++ b/arch/arm/boot/dts/broadcom/bcm2711-rpi-cm4s.dts
107 @@ -148,7 +148,7 @@
108
109 / {
110 chosen {
111 - bootargs = "coherent_pool=1M snd_bcm2835.enable_headphones=0 numa_policy=interleave";
112 + bootargs = "coherent_pool=1M snd_bcm2835.enable_headphones=0 cgroup_disable=memory numa_policy=interleave";
113 };
114
115 aliases {
116 --- a/arch/arm/boot/dts/broadcom/bcm2711-rpi-ds.dtsi
117 +++ b/arch/arm/boot/dts/broadcom/bcm2711-rpi-ds.dtsi
118 @@ -3,7 +3,7 @@
119
120 / {
121 chosen {
122 - bootargs = "coherent_pool=1M 8250.nr_uarts=1 snd_bcm2835.enable_headphones=0 numa_policy=interleave";
123 + bootargs = "coherent_pool=1M 8250.nr_uarts=1 snd_bcm2835.enable_headphones=0 cgroup_disable=memory numa_policy=interleave";
124 };
125
126 __overrides__ {
127 --- a/arch/arm/boot/dts/broadcom/bcm271x-rpi-bt.dtsi
128 +++ b/arch/arm/boot/dts/broadcom/bcm271x-rpi-bt.dtsi
129 @@ -24,7 +24,7 @@
130
131 / {
132 chosen {
133 - bootargs = "coherent_pool=1M 8250.nr_uarts=1 snd_bcm2835.enable_headphones=0";
134 + bootargs = "coherent_pool=1M 8250.nr_uarts=1 snd_bcm2835.enable_headphones=0 cgroup_disable=memory";
135 };
136
137 aliases {
138 --- a/arch/arm64/boot/dts/broadcom/bcm2712-rpi.dtsi
139 +++ b/arch/arm64/boot/dts/broadcom/bcm2712-rpi.dtsi
140 @@ -99,7 +99,7 @@
141
142 / {
143 chosen: chosen {
144 - bootargs = "reboot=w coherent_pool=1M 8250.nr_uarts=1 pci=pcie_bus_safe numa_policy=interleave iommu_dma_numa_policy=interleave system_heap.max_order=0";
145 + bootargs = "reboot=w coherent_pool=1M 8250.nr_uarts=1 pci=pcie_bus_safe cgroup_disable=memory numa_policy=interleave iommu_dma_numa_policy=interleave system_heap.max_order=0";
146 stdout-path = "serial10:115200n8";
147 };
148
149 --- a/kernel/cgroup/cgroup.c
150 +++ b/kernel/cgroup/cgroup.c
151 @@ -6060,9 +6060,6 @@ int __init cgroup_init_early(void)
152 return 0;
153 }
154
155 -static u16 cgroup_enable_mask __initdata;
156 -static int __init cgroup_disable(char *str);
157 -
158 /**
159 * cgroup_init - cgroup initialization
160 *
161 @@ -6096,12 +6093,6 @@ int __init cgroup_init(void)
162
163 cgroup_unlock();
164
165 - /*
166 - * Apply an implicit disable, knowing that an explicit enable will
167 - * prevent if from doing anything.
168 - */
169 - cgroup_disable("memory");
170 -
171 for_each_subsys(ss, ssid) {
172 if (ss->early_init) {
173 struct cgroup_subsys_state *css =
174 @@ -6742,10 +6733,6 @@ static int __init cgroup_disable(char *s
175 strcmp(token, ss->legacy_name))
176 continue;
177
178 - /* An explicit cgroup_enable overrides a disable */
179 - if (cgroup_enable_mask & (1 << i))
180 - continue;
181 -
182 static_branch_disable(cgroup_subsys_enabled_key[i]);
183 pr_info("Disabling %s control group subsystem\n",
184 ss->name);
185 @@ -6779,7 +6766,7 @@ static int __init cgroup_enable(char *st
186 strcmp(token, ss->legacy_name))
187 continue;
188
189 - cgroup_enable_mask |= 1 << i;
190 + cgroup_feature_disable_mask &= ~(1 << i);
191 static_branch_enable(cgroup_subsys_enabled_key[i]);
192 pr_info("Enabling %s control group subsystem\n",
193 ss->name);