Restore CPU affinity patches' functionality

Commit d27f1ee36e ("Linux std kernel upgrade to align with CentOS 8")
and commit c0fee2da8e ("[kernel-rt 4.18] Upgrade kernel-rt to version
4.18.0-147.3.1 based on SRPM") uprevisioned the StarlingX kernel from
v3.10 to v4.18 for migration to CentOS 8 and adapted the kernel patches
accordingly.

However, the CPU affinity patches' uprevisioning was made in a buggy
manner: The connection between the bitmask cpu_kthread_bits and the CPU
mask __cpu_kthread_mask was not kept, which made these two patches
essentially inoperative, as described by bug report 1924799.

This commit updates the two patches to make the kthread_cpus= kernel
command line argument functional again by removing cpu_kthread_bits and
making the argument parser function kthread_setup store the parsed CPU
mask into __cpu_kthread_mask.

This commit introduces error handling for the parsing of the
kthread_cpus= argument's value as well, based on code review feedback.
If a parser error occurs, then the error code is reported and the
effective CPU affinity will correspond to all possible CPUs.

This change was verified with the StarlingX master branch as of last
week. As an example, prior to this commit, the kthreadd kernel thread
(i.e., usually PID 2) had a CPU affinity of 0 on a qemu-based VM with 8
virtual CPUs despite the use of "kthread_cpus=0-1" on the kernel command
line. With this patch, PID 2's CPU affinity is 0-1, as expected.

Change-Id: Ica1dfe00947e4e52f19659b721958f1e1845609b
Closes-Bug: 1924799
Signed-off-by: Vefa Bicakci <vefa.bicakci@windriver.com>
This commit is contained in:
Vefa Bicakci 2021-04-20 18:09:57 -04:00
parent 4013790c6e
commit 7b55e47e1d
2 changed files with 51 additions and 54 deletions

View File

@ -1,7 +1,7 @@
From a0ae6dfee3482b4da4d6f98c7e9f154f269fe5d6 Mon Sep 17 00:00:00 2001
From 41a11b1e49eaad061dca9ab7b642820fa4bd833d 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 03/10] affine compute kernel threads
Subject: [PATCH] affine compute kernel threads
This is a kernel enhancement to configure the cpu affinity of kernel
threads via kernel boot option kthread_cpus=<cpulist>. The compute
@ -27,17 +27,18 @@ Signed-off-by: Vu Tran <vu.tran@windriver.com>
Signed-off-by: Jim Somerville <Jim.Somerville@windriver.com>
Signed-off-by: Zhang Zhiguo <zhangzhg@neusoft.com>
Signed-off-by: Vefa Bicakci <vefa.bicakci@windriver.com>
---
Documentation/admin-guide/kernel-parameters.txt | 9 +++++++++
include/linux/cpumask.h | 3 +++
init/main.c | 2 ++
kernel/cpu.c | 14 ++++++++++++++
kernel/cpu.c | 20 ++++++++++++++++++++
kernel/kthread.c | 4 ++--
kernel/umh.c | 3 +++
6 files changed, 33 insertions(+), 2 deletions(-)
6 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 5d43bff..928b525 100644
index 9707a806ab1f..b864bfff4bce 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1968,6 +1968,15 @@
@ -57,7 +58,7 @@ index 5d43bff..928b525 100644
kpti= [ARM64] Control page table isolation of user
and kernel address spaces.
diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 57f20a0..4526034 100644
index 57f20a0a7794..452603496e53 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -54,6 +54,7 @@ extern unsigned int nr_cpu_ids;
@ -82,10 +83,10 @@ index 57f20a0..4526034 100644
#if NR_CPUS > 1
#define num_online_cpus() cpumask_weight(cpu_online_mask)
diff --git a/init/main.c b/init/main.c
index 42c2f2e..f248b7f 100644
index f50f0b8234d1..92a16ead6b11 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1142,6 +1142,8 @@ static noinline void __init kernel_init_freeable(void)
@@ -1144,6 +1144,8 @@ static noinline void __init kernel_init_freeable(void)
do_basic_setup();
@ -95,29 +96,28 @@ index 42c2f2e..f248b7f 100644
if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
pr_err("Warning: unable to open an initial console.\n");
diff --git a/kernel/cpu.c b/kernel/cpu.c
index d94cf04..0f2e63f 100644
index 23923f77c427..ff74e65b6206 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -2226,6 +2226,9 @@ EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
EXPORT_SYMBOL(cpu_all_bits);
+const DECLARE_BITMAP(cpu_kthread_bits, NR_CPUS) = CPU_BITS_ALL;
+EXPORT_SYMBOL(cpu_kthread_bits);
+
#ifdef CONFIG_INIT_ALL_POSSIBLE
struct cpumask __cpu_possible_mask __read_mostly
= {CPU_BITS_ALL};
@@ -2243,6 +2246,17 @@ EXPORT_SYMBOL(__cpu_present_mask);
@@ -2310,6 +2310,26 @@ EXPORT_SYMBOL(__cpu_present_mask);
struct cpumask __cpu_active_mask __read_mostly;
EXPORT_SYMBOL(__cpu_active_mask);
+struct cpumask __cpu_kthread_mask __read_mostly;
+struct cpumask __cpu_kthread_mask __read_mostly
+ = {CPU_BITS_ALL};
+EXPORT_SYMBOL(__cpu_kthread_mask);
+
+static int __init kthread_setup(char *str)
+{
+ cpulist_parse(str, (struct cpumask *)&cpu_kthread_bits);
+ struct cpumask tmp_mask;
+ int err;
+
+ err = cpulist_parse(str, &tmp_mask);
+ if (!err)
+ cpumask_copy(&__cpu_kthread_mask, &tmp_mask);
+ else
+ pr_err("Cannot parse 'kthread_cpus=%s'; error %d\n", str, err);
+
+ return 1;
+}
+__setup("kthread_cpus=", kthread_setup);
@ -127,10 +127,10 @@ index d94cf04..0f2e63f 100644
{
cpumask_copy(&__cpu_present_mask, src);
diff --git a/kernel/kthread.c b/kernel/kthread.c
index 1f0bf2d..3a5f1af 100644
index e4f3cfd757d2..85f753fc5a3c 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -339,7 +339,7 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
@@ -340,7 +340,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);
@ -139,7 +139,7 @@ index 1f0bf2d..3a5f1af 100644
}
kfree(create);
return task;
@@ -562,7 +562,7 @@ int kthreadd(void *unused)
@@ -563,7 +563,7 @@ int kthreadd(void *unused)
/* Setup a clean context for our children to inherit. */
set_task_comm(tsk, "kthreadd");
ignore_signals(tsk);
@ -149,7 +149,7 @@ index 1f0bf2d..3a5f1af 100644
current->flags |= PF_NOFREEZE;
diff --git a/kernel/umh.c b/kernel/umh.c
index d937cba..94715df 100644
index d937cbad903a..94715dff7d61 100644
--- a/kernel/umh.c
+++ b/kernel/umh.c
@@ -74,6 +74,9 @@ static int call_usermodehelper_exec_async(void *data)
@ -163,5 +163,5 @@ index d937cba..94715df 100644
* Our parent (unbound workqueue) runs with elevated scheduling
* priority. Avoid propagating that into the userspace child.
--
2.7.4
2.29.2

View File

@ -1,10 +1,7 @@
From 6b6edb5a389c03e208e6a123a7807af66283237d Mon Sep 17 00:00:00 2001
Message-Id: <6b6edb5a389c03e208e6a123a7807af66283237d.1527544850.git.Jim.Somerville@windriver.com>
In-Reply-To: <b6ceef1c915827b50ce3f76da4dc47f3eb768b44.1527544850.git.Jim.Somerville@windriver.com>
References: <b6ceef1c915827b50ce3f76da4dc47f3eb768b44.1527544850.git.Jim.Somerville@windriver.com>
From 86a6c5a26e59c6cb938f5e08e33933723249bfe6 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 03/10] affine compute kernel threads
Subject: [PATCH] affine compute kernel threads
This is a kernel enhancement to configure the cpu affinity of kernel
threads via kernel boot option kthread_cpus=<cpulist>. The compute
@ -30,17 +27,18 @@ Signed-off-by: Vu Tran <vu.tran@windriver.com>
Signed-off-by: Jim Somerville <Jim.Somerville@windriver.com>
Signed-off-by: Zhang Zhiguo <zhangzhg@neusoft.com>
Signed-off-by: Vefa Bicakci <vefa.bicakci@windriver.com>
---
Documentation/admin-guide/kernel-parameters.txt | 9 +++++++++
include/linux/cpumask.h | 3 +++
init/main.c | 2 ++
kernel/cpu.c | 14 ++++++++++++++
kernel/cpu.c | 20 ++++++++++++++++++++
kernel/kthread.c | 4 ++--
kernel/umh.c | 3 +++
6 files changed, 33 insertions(+), 2 deletions(-)
6 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 5d43bff..928b525 100644
index 5d43bff81f1d..928b52500e9e 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1968,6 +1968,15 @@
@ -60,7 +58,7 @@ index 5d43bff..928b525 100644
kpti= [ARM64] Control page table isolation of user
and kernel address spaces.
diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 57f20a0..4526034 100644
index 57f20a0a7794..452603496e53 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -54,6 +54,7 @@ extern unsigned int nr_cpu_ids;
@ -85,7 +83,7 @@ index 57f20a0..4526034 100644
#if NR_CPUS > 1
#define num_online_cpus() cpumask_weight(cpu_online_mask)
diff --git a/init/main.c b/init/main.c
index 42c2f2e..f248b7f 100644
index 42c2f2ed74a2..f248b7f07082 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1142,6 +1142,8 @@ static noinline void __init kernel_init_freeable(void)
@ -98,29 +96,28 @@ index 42c2f2e..f248b7f 100644
if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
pr_err("Warning: unable to open an initial console.\n");
diff --git a/kernel/cpu.c b/kernel/cpu.c
index d94cf04..0f2e63f 100644
index d94cf04ef0b8..0f3625c90996 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -2226,6 +2226,9 @@ EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
EXPORT_SYMBOL(cpu_all_bits);
+const DECLARE_BITMAP(cpu_kthread_bits, NR_CPUS) = CPU_BITS_ALL;
+EXPORT_SYMBOL(cpu_kthread_bits);
+
#ifdef CONFIG_INIT_ALL_POSSIBLE
struct cpumask __cpu_possible_mask __read_mostly
= {CPU_BITS_ALL};
@@ -2243,6 +2246,17 @@ EXPORT_SYMBOL(__cpu_present_mask);
@@ -2243,6 +2243,26 @@ EXPORT_SYMBOL(__cpu_present_mask);
struct cpumask __cpu_active_mask __read_mostly;
EXPORT_SYMBOL(__cpu_active_mask);
+struct cpumask __cpu_kthread_mask __read_mostly;
+struct cpumask __cpu_kthread_mask __read_mostly
+ = {CPU_BITS_ALL};
+EXPORT_SYMBOL(__cpu_kthread_mask);
+
+static int __init kthread_setup(char *str)
+{
+ cpulist_parse(str, (struct cpumask *)&cpu_kthread_bits);
+ struct cpumask tmp_mask;
+ int err;
+
+ err = cpulist_parse(str, &tmp_mask);
+ if (!err)
+ cpumask_copy(&__cpu_kthread_mask, &tmp_mask);
+ else
+ pr_err("Cannot parse 'kthread_cpus=%s'; error %d\n", str, err);
+
+ return 1;
+}
+__setup("kthread_cpus=", kthread_setup);
@ -130,7 +127,7 @@ index d94cf04..0f2e63f 100644
{
cpumask_copy(&__cpu_present_mask, src);
diff --git a/kernel/kthread.c b/kernel/kthread.c
index 1f0bf2d..3a5f1af 100644
index 1f0bf2d0f207..3a5f1af5054e 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -339,7 +339,7 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
@ -152,7 +149,7 @@ index 1f0bf2d..3a5f1af 100644
current->flags |= PF_NOFREEZE;
diff --git a/kernel/umh.c b/kernel/umh.c
index d937cba..94715df 100644
index d937cbad903a..94715dff7d61 100644
--- a/kernel/umh.c
+++ b/kernel/umh.c
@@ -74,6 +74,9 @@ static int call_usermodehelper_exec_async(void *data)
@ -166,5 +163,5 @@ index d937cba..94715df 100644
* Our parent (unbound workqueue) runs with elevated scheduling
* priority. Avoid propagating that into the userspace child.
--
2.7.4
2.29.2