kthread_cpus: Avoid large stack allocation

Since commit 7b55e47e1d ("Restore CPU affinity patches'
functionality"), the function kthread_setup() has allocated a cpumask
structure on the stack, which uses 1024 bytes on the stack when
CONFIG_MAXSMP is enabled. This was detected after setting
CONFIG_FRAME_WARN to 1024 for testing purposes.

To clarify, with the following configuration:

  $ grep -e MAXSMP= -e NR_CPUS= -e CPUMASK_OFFSTACK= \
    -e FRAME_WARN= .config
  CONFIG_MAXSMP=y
  CONFIG_NR_CPUS=8192
  CONFIG_CPUMASK_OFFSTACK=y
  CONFIG_FRAME_WARN=1024

the following build warning is encountered:

    CC      kernel/cpu.o
  kernel/cpu.c: In function 'kthread_setup':
  kernel/cpu.c:2475:1: warning: the frame size of 1032 bytes is \
      larger than 1024 bytes [-Wframe-larger-than=]

This commit resolves this issue by allocating the cpumask structure
using the boot memory allocator via alloc_bootmem_cpumask_var().
A similar approach is taken for the housekeeping-related cpumask
structures in kernel/sched/isolation.c.

Please note that the issue fixed by this commit (large stack allocation)
only exists when CONFIG_CPUMASK_OFFSTACK is enabled. StarlingX currently
disables CONFIG_CPUMASK_OFFSTACK. Disabling CONFIG_CPUMASK_OFFSTACK sets
CONFIG_NR_CPUS' maximum value to 512 CPUs, which in turn corresponds to
64 bytes on the stack. In contrast, enabling CONFIG_CPUMASK_OFFSTACK
(for example, by setting CONFIG_MAXSMP=y) raises the upper limit for
CONFIG_NR_CPUS to 8192 CPUs, which translates to a 1024-byte bitmap.

No change in behaviour is intended.

While carrying out the code changes, space characters in the affected
function were converted to tab characters as a minor clean-up.

Testing:
* Monolithic ISO image build was successful.
* Installation and bootstrap of the built ISO was successful with a
  virtual machine in All-in-One simplex mode, using the low-latency
  profile.
* kthread_cpus argument's behaviour was confirmed to not have been
  negatively affected by checking that the CPU affinity of kthreadd
  (i.e., pid 2) matches the kthread_cpus= argument's value.
* kthread_cpus argument's error handling was confirmed to work as
  expected by booting up the installed system with an invalid argument
  ('kthread_cpus=abc'). In this scenario, the CPU affinity of kthreadd
  was observed to be correctly set to 0-7 (with an 8-CPU VM).

Change-Id: I31d2175d6084142e63d4b38d7b0c5677046fce4f
Closes-Bug: 1957188
Fixes: 7b55e47e1d ("Restore CPU affinity patches' functionality")
Signed-off-by: M. Vefa Bicakci <vefa.bicakci@windriver.com>
This commit is contained in:
M. Vefa Bicakci
2022-01-11 17:00:27 -05:00
parent 98644316c6
commit 8fde1a8070
2 changed files with 48 additions and 40 deletions

View File

@@ -1,4 +1,4 @@
From c8cf8051b6e4118613fbdfccbca0c4a0c19ec4a5 Mon Sep 17 00:00:00 2001
From 5fba1536bd24bc77ef0d6b2516fefcff69d7cf59 Mon Sep 17 00:00:00 2001
From: Chris Friesen <chris.friesen@windriver.com>
Date: Tue, 24 Nov 2015 16:27:28 -0500
Subject: [PATCH] affine compute kernel threads
@@ -31,19 +31,19 @@ Signed-off-by: Vefa Bicakci <vefa.bicakci@windriver.com>
[jm: Adapted the patch for context changes.]
Signed-off-by: Jiping Ma <jiping.ma2@windriver.com>
---
.../admin-guide/kernel-parameters.txt | 10 ++++++++++
.../admin-guide/kernel-parameters.txt | 10 ++++++++
include/linux/cpumask.h | 3 +++
init/main.c | 2 ++
kernel/cpu.c | 19 +++++++++++++++++++
kernel/cpu.c | 23 +++++++++++++++++++
kernel/kthread.c | 4 ++--
kernel/umh.c | 3 +++
6 files changed, 39 insertions(+), 2 deletions(-)
6 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 5a241afb4728..78a923d087c1 100644
index a19a3005b545..6f79c718ae4f 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -2217,6 +2217,16 @@
@@ -2223,6 +2223,16 @@
See also Documentation/trace/kprobetrace.rst "Kernel
Boot Parameter" section.
@@ -86,10 +86,10 @@ index 383684e30f12..8fcc67ea3d8c 100644
extern atomic_t __num_online_cpus;
diff --git a/init/main.c b/init/main.c
index 0e4cc913695e..df9759f2859b 100644
index db693781a12f..4e7777bdab6e 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1542,6 +1542,8 @@ static noinline void __init kernel_init_freeable(void)
@@ -1536,6 +1536,8 @@ static noinline void __init kernel_init_freeable(void)
do_basic_setup();
@@ -99,10 +99,10 @@ index 0e4cc913695e..df9759f2859b 100644
console_on_rootfs();
diff --git a/kernel/cpu.c b/kernel/cpu.c
index 4e11e91010e1..52aa0ee41352 100644
index 016f2d0686b6..6783db02e9ae 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -2456,6 +2456,25 @@ EXPORT_SYMBOL(__cpu_active_mask);
@@ -2505,6 +2505,29 @@ EXPORT_SYMBOL(__cpu_active_mask);
atomic_t __num_online_cpus __read_mostly;
EXPORT_SYMBOL(__num_online_cpus);
@@ -112,15 +112,19 @@ index 4e11e91010e1..52aa0ee41352 100644
+
+static int __init kthread_setup(char *str)
+{
+ struct cpumask tmp_mask;
+ cpumask_var_t tmp_mask;
+ int err;
+
+ err = cpulist_parse(str, &tmp_mask);
+
+ alloc_bootmem_cpumask_var(&tmp_mask);
+
+ err = cpulist_parse(str, tmp_mask);
+ if (!err)
+ cpumask_copy(&__cpu_kthread_mask, &tmp_mask);
+ cpumask_copy(&__cpu_kthread_mask, tmp_mask);
+ else
+ pr_err("Cannot parse 'kthread_cpus=%s'; error %d\n", str, err);
+
+ pr_err("Cannot parse 'kthread_cpus=%s'; error %d\n", str, err);
+
+ free_bootmem_cpumask_var(tmp_mask);
+
+ return 1;
+}
+__setup("kthread_cpus=", kthread_setup);
@@ -129,10 +133,10 @@ index 4e11e91010e1..52aa0ee41352 100644
{
cpumask_copy(&__cpu_present_mask, src);
diff --git a/kernel/kthread.c b/kernel/kthread.c
index cdfaf64263b3..251a753c852e 100644
index 3ce6a31db7b4..683008e94fd4 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -279,7 +279,7 @@ static int kthread(void *_create)
@@ -300,7 +300,7 @@ static int kthread(void *_create)
* back to default in case they have been changed.
*/
sched_setscheduler_nocheck(current, SCHED_NORMAL, &param);
@@ -141,7 +145,7 @@ index cdfaf64263b3..251a753c852e 100644
/* OK, tell user we're spawned, wait for stop or wakeup */
__set_current_state(TASK_UNINTERRUPTIBLE);
@@ -634,7 +634,7 @@ int kthreadd(void *unused)
@@ -655,7 +655,7 @@ int kthreadd(void *unused)
/* Setup a clean context for our children to inherit. */
set_task_comm(tsk, "kthreadd");
ignore_signals(tsk);
@@ -165,5 +169,5 @@ index 3f646613a9d3..e5027cee43f7 100644
* Our parent (unbound workqueue) runs with elevated scheduling
* priority. Avoid propagating that into the userspace child.
--
2.31.1
2.29.2

View File

@@ -1,4 +1,4 @@
From a04995a064c17b5e5df6d6f184c1cf01bb8bc6a0 Mon Sep 17 00:00:00 2001
From d4c6a64967be6e34fec40ddff5714efe001be7c3 Mon Sep 17 00:00:00 2001
From: Chris Friesen <chris.friesen@windriver.com>
Date: Tue, 24 Nov 2015 16:27:28 -0500
Subject: [PATCH] affine compute kernel threads
@@ -31,19 +31,19 @@ Signed-off-by: Vefa Bicakci <vefa.bicakci@windriver.com>
[jm: Adapted the patch for context changes.]
Signed-off-by: Jiping Ma <jiping.ma2@windriver.com>
---
.../admin-guide/kernel-parameters.txt | 10 ++++++++++
.../admin-guide/kernel-parameters.txt | 10 ++++++++
include/linux/cpumask.h | 3 +++
init/main.c | 2 ++
kernel/cpu.c | 19 +++++++++++++++++++
kernel/kthread.c | 5 ++---
kernel/cpu.c | 23 +++++++++++++++++++
kernel/kthread.c | 5 ++--
kernel/umh.c | 3 +++
6 files changed, 39 insertions(+), 3 deletions(-)
6 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 26bfe7ae711b..91bb37814527 100644
index 19e9e220eaa1..0260789adc94 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -2217,6 +2217,16 @@
@@ -2223,6 +2223,16 @@
See also Documentation/trace/kprobetrace.rst "Kernel
Boot Parameter" section.
@@ -86,10 +86,10 @@ index f0d895d6ac39..45f338cfdd6f 100644
extern atomic_t __num_online_cpus;
diff --git a/init/main.c b/init/main.c
index d9d914111251..951410241ef4 100644
index db693781a12f..4e7777bdab6e 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1527,6 +1527,8 @@ static noinline void __init kernel_init_freeable(void)
@@ -1536,6 +1536,8 @@ static noinline void __init kernel_init_freeable(void)
do_basic_setup();
@@ -99,10 +99,10 @@ index d9d914111251..951410241ef4 100644
console_on_rootfs();
diff --git a/kernel/cpu.c b/kernel/cpu.c
index 2b8d7a5db383..b5dbec330189 100644
index 67c22941b5f2..67b1a67bd8f0 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -2449,6 +2449,25 @@ EXPORT_SYMBOL(__cpu_active_mask);
@@ -2498,6 +2498,29 @@ EXPORT_SYMBOL(__cpu_active_mask);
atomic_t __num_online_cpus __read_mostly;
EXPORT_SYMBOL(__num_online_cpus);
@@ -112,15 +112,19 @@ index 2b8d7a5db383..b5dbec330189 100644
+
+static int __init kthread_setup(char *str)
+{
+ struct cpumask tmp_mask;
+ cpumask_var_t tmp_mask;
+ int err;
+
+ err = cpulist_parse(str, &tmp_mask);
+
+ alloc_bootmem_cpumask_var(&tmp_mask);
+
+ err = cpulist_parse(str, tmp_mask);
+ if (!err)
+ cpumask_copy(&__cpu_kthread_mask, &tmp_mask);
+ cpumask_copy(&__cpu_kthread_mask, tmp_mask);
+ else
+ pr_err("Cannot parse 'kthread_cpus=%s'; error %d\n", str, err);
+
+
+ free_bootmem_cpumask_var(tmp_mask);
+
+ return 1;
+}
+__setup("kthread_cpus=", kthread_setup);
@@ -129,10 +133,10 @@ index 2b8d7a5db383..b5dbec330189 100644
{
cpumask_copy(&__cpu_present_mask, src);
diff --git a/kernel/kthread.c b/kernel/kthread.c
index 5edf7e19ab26..7c3924752999 100644
index 508fe5278285..a7bb87b00cea 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -384,8 +384,7 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
@@ -405,8 +405,7 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
* The kernel thread should not inherit these properties.
*/
sched_setscheduler_nocheck(task, SCHED_NORMAL, &param);
@@ -142,7 +146,7 @@ index 5edf7e19ab26..7c3924752999 100644
}
kfree(create);
return task;
@@ -634,7 +633,7 @@ int kthreadd(void *unused)
@@ -655,7 +654,7 @@ int kthreadd(void *unused)
/* Setup a clean context for our children to inherit. */
set_task_comm(tsk, "kthreadd");
ignore_signals(tsk);
@@ -166,5 +170,5 @@ index 3f646613a9d3..e5027cee43f7 100644
* Our parent (unbound workqueue) runs with elevated scheduling
* priority. Avoid propagating that into the userspace child.
--
2.31.1
2.29.2