Skip to contentSkip to conversation

Fix isocpus's param handling when CPUMASK_OFFSTACK=n.

PATCH In review Rakib Mullick 3 participants 6 messages Oct 15, 2017 – Oct 24, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix isocpus's param handling when CPUMASK_OFFSTACK=n.

cpulist_parse() uses nr_cpumask_bits as limit to parse the
passed buffer from kernel commandline. What nr_cpumask_bits represents varies depends upon CONFIG_CPUMASK_OFFSTACK option. If CONFIG_CPUMASK_OFFSTACK=n, then nr_cpumask_bits is same as NR_CPUS, which might not represent the # of cpus really exist (default 64). So, there's a chance of gap between nr_cpu_ids and NR_CPUS, which ultimately lead towards invalid cpulist_parse() operation. For example, if isolcpus=9 is passed on a 8 cpu system (CONFIG_CPUMASK_OFFSTACK=n) it doesn't show the error that it suppose to.

This patch fixes this issue by effectively find out the last cpu of the passed isolcpus list and checking it with nr_cpu_ids. Also, fixes the error message where the nr_cpu_ids should be nr_cpu_ids-1, since the cpu numbering starts from 0.

---
 include/linux/cpumask.h | 16 ++++++++++++++++
 kernel/sched/topology.c |  8 +++++---
 2 files changed, 21 insertions(+), 3 deletions(-)
include/linux/cpumask.h +16 -0
index 2404ad2..f9a7c9b 100644
@@ -130,6 +130,11 @@ static inline unsigned int cpumask_first(const struct cpumask *srcp)
 	return 0;
 }
 
+static inline unsigned int cpumask_last(const struct cpumask *srcp)
+{
+	return 0;
+}
+
 /* Valid inputs for n are -1 and 0. */
 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
 {
@@ -179,6 +184,17 @@ static inline unsigned int cpumask_first(const struct cpumask *srcp)
 }
 
 /**
+ * cpumask_last - get the last cpu in a cpumask
+ * @srcp:	- the cpumask pointer
+ *
+ * Returns	>= nr_cpumask_bits if no cpus set.
+ */
+static inline unsigned int cpumask_last(const struct cpumask *srcp)
+{
+	return find_last_bit(cpumask_bits(srcp), nr_cpumask_bits);
+}
+
+/**
  * cpumask_next - get the next cpu in a cpumask
  * @n: the cpu prior to the place to search (ie. return will be > @n)
  * @srcp: the cpumask pointer
kernel/sched/topology.c +5 -3
index 1b0b4fb..55b5e09 100644
@@ -452,12 +452,14 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
 /* Setup the mask of CPUs configured for isolated domains */
 static int __init isolated_cpu_setup(char *str)
 {
-	int ret;
+	int ret, lastcpu;
 
 	alloc_bootmem_cpumask_var(&cpu_isolated_map);
 	ret = cpulist_parse(str, cpu_isolated_map);
-	if (ret) {
-		pr_err("sched: Error, all isolcpus= values must be between 0 and %d\n", nr_cpu_ids);
+	lastcpu = cpumask_last(cpu_isolated_map);
+	if (ret || lastcpu >= nr_cpu_ids) {
+		pr_err("sched: Error, all isolcpus= values must be between 0 and %d\n",
+				nr_cpu_ids-1);
 		return 0;
 	}
 	return 1;
Signature · 1 line
2.9.3
#1 lore raw
Quoted 16 lines · Rakib Mullick

cpulist_parse() uses nr_cpumask_bits as limit to parse the
passed buffer from kernel commandline. What nr_cpumask_bits represents varies depends upon CONFIG_CPUMASK_OFFSTACK option. If CONFIG_CPUMASK_OFFSTACK=n, then nr_cpumask_bits is same as NR_CPUS, which might not represent the # of cpus really exist (default 64). So, there's a chance of gap between nr_cpu_ids and NR_CPUS, which ultimately lead towards invalid cpulist_parse() operation. For example, if isolcpus=9 is passed on a 8 cpu system (CONFIG_CPUMASK_OFFSTACK=n) it doesn't show the error that it suppose to.

This patch fixes this issue by effectively find out the last cpu of the passed isolcpus list and checking it with nr_cpu_ids. Also, fixes the error message where the nr_cpu_ids should be nr_cpu_ids-1, since the cpu numbering starts from 0.

Quoted 5 lines
---
 include/linux/cpumask.h | 16 ++++++++++++++++
 kernel/sched/topology.c |  8 +++++---
 2 files changed, 21 insertions(+), 3 deletions(-)

What kernel is this against? It does not apply to the latest kernels.

Thanks,

Ingo

#2 lore raw

PATCH 1/1 Fix isocpus's param handling when CPUMASK_OFFSTACK=n.

Quoted 8 lines
Quoted 4 lines · Ingo Molnar

Rakib Mullick <[email protected]> wrote:

 include/linux/cpumask.h | 16 ++++++++++++++++
 kernel/sched/topology.c |  8 +++++---
 2 files changed, 21 insertions(+), 3 deletions(-)

What kernel is this against? It does not apply to the latest kernels.

It was against 4.14-rc4, prepared before -rc5 release. Please, consider the below one, against -rc5.

cpulist_parse() uses nr_cpumask_bits as limit to parse the
passed buffer from kernel commandline. What nr_cpumask_bits represents varies depends upon CONFIG_CPUMASK_OFFSTACK option. If CONFIG_CPUMASK_OFFSTACK=n, then nr_cpumask_bits is same as NR_CPUS, which might not represent the # of cpus really exist (default 64). So, there's a chance of gap between nr_cpu_ids and NR_CPUS, which ultimately lead towards invalid cpulist_parse() operation. For example, if isolcpus=9 is passed on a 8 cpu system (CONFIG_CPUMASK_OFFSTACK=n) it doesn't show the error that it suppose to.

This patch fixes this issue by effectively find out the last cpu of the passed isolcpus list and checking it with nr_cpu_ids. Also, fixes the error message where the nr_cpu_ids should be nr_cpu_ids-1, since the cpu numbering starts from 0.

---
 include/linux/cpumask.h | 16 ++++++++++++++++
 kernel/sched/topology.c |  8 +++++---
 2 files changed, 21 insertions(+), 3 deletions(-)
include/linux/cpumask.h +16 -0
index cd415b7..5631725 100644
@@ -130,6 +130,11 @@ static inline unsigned int cpumask_first(const struct cpumask *srcp)
 	return 0;
 }
 
+static inline unsigned int cpumask_last(const struct cpumask *srcp)
+{
+	return 0;
+}
+
 /* Valid inputs for n are -1 and 0. */
 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
 {
@@ -178,6 +183,17 @@ static inline unsigned int cpumask_first(const struct cpumask *srcp)
 	return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
 }
 
+/**
+ * cpumask_last - get the last cpu in a cpumask
+ * @srcp:	- the cpumask pointer
+ *
+ * Returns	>= nr_cpumask_bits if no cpus set.
+ */
+static inline unsigned int cpumask_last(const struct cpumask *srcp)
+{
+	return find_last_bit(cpumask_bits(srcp), nr_cpumask_bits);
+}
+
 unsigned int cpumask_next(int n, const struct cpumask *srcp);
 
 /**
kernel/sched/topology.c +5 -3
index f1cf4f3..b9265c8 100644
@@ -466,12 +466,14 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
 /* Setup the mask of CPUs configured for isolated domains */
 static int __init isolated_cpu_setup(char *str)
 {
-	int ret;
+	int ret, lastcpu;
 
 	alloc_bootmem_cpumask_var(&cpu_isolated_map);
 	ret = cpulist_parse(str, cpu_isolated_map);
-	if (ret) {
-		pr_err("sched: Error, all isolcpus= values must be between 0 and %u\n", nr_cpu_ids);
+	lastcpu = cpumask_last(cpu_isolated_map);
+	if (ret || lastcpu >= nr_cpu_ids) {
+		pr_err("sched: Error, all isolcpus= values must be between 0 and %u\n",
+				nr_cpu_ids-1);
 		return 0;
 	}
 	return 1;
Signature · 1 line
2.9.3
#3 lore raw
Quoted 51 lines · Rakib Mullick
Quoted 8 lines
Quoted 4 lines · Ingo Molnar

Rakib Mullick <[email protected]> wrote:

 include/linux/cpumask.h | 16 ++++++++++++++++
 kernel/sched/topology.c |  8 +++++---
 2 files changed, 21 insertions(+), 3 deletions(-)

What kernel is this against? It does not apply to the latest kernels.

It was against 4.14-rc4, prepared before -rc5 release. Please, consider the below one, against -rc5.

cpulist_parse() uses nr_cpumask_bits as limit to parse the
passed buffer from kernel commandline. What nr_cpumask_bits represents varies depends upon CONFIG_CPUMASK_OFFSTACK option. If CONFIG_CPUMASK_OFFSTACK=n, then nr_cpumask_bits is same as NR_CPUS, which might not represent the # of cpus really exist (default 64). So, there's a chance of gap between nr_cpu_ids and NR_CPUS, which ultimately lead towards invalid cpulist_parse() operation. For example, if isolcpus=9 is passed on a 8 cpu system (CONFIG_CPUMASK_OFFSTACK=n) it doesn't show the error that it suppose to.

This patch fixes this issue by effectively find out the last cpu of the passed isolcpus list and checking it with nr_cpu_ids. Also, fixes the error message where the nr_cpu_ids should be nr_cpu_ids-1, since the cpu numbering starts from 0.

---
 include/linux/cpumask.h | 16 ++++++++++++++++
 kernel/sched/topology.c |  8 +++++---
 2 files changed, 21 insertions(+), 3 deletions(-)
include/linux/cpumask.h +5 -0
index cd415b7..5631725 100644
@@ -130,6 +130,11 @@ static inline unsigned int cpumask_first(const struct cpumask *srcp)
 	return 0;
 }
 
+static inline unsigned int cpumask_last(const struct cpumask *srcp)
+{
+	return 0;
+}
+
 /* Valid inputs for n are -1 and 0. */
 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
 {

include/linux/cpumask.h static inline unsigned int cpumask_first(const struct cpumask *srcp) · line 187

Quoted 5 lines
include/linux/cpumask.h +2 -0
@@ -178,6 +183,17 @@ static inline unsigned int cpumask_first(const struct cpumask *srcp)
 	return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
 }
 
+/**
+ * cpumask_last - get the last cpu in a cpumask

Please capitalize 'CPU' properly in documentation.

Quoted 19 lines
include/linux/cpumask.h +9 -0
+ * @srcp:	- the cpumask pointer
+ *
+ * Returns	>= nr_cpumask_bits if no cpus set.
+ */
+static inline unsigned int cpumask_last(const struct cpumask *srcp)
+{
+	return find_last_bit(cpumask_bits(srcp), nr_cpumask_bits);
+}
+
 unsigned int cpumask_next(int n, const struct cpumask *srcp);
 
 /**
kernel/sched/topology.c +0 -0
index f1cf4f3..b9265c8 100644
@@ -466,12 +466,14 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
 /* Setup the mask of CPUs configured for isolated domains */
 static int __init isolated_cpu_setup(char *str)

kernel/sched/topology.c cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) · line 474

Quoted 10 lines
kernel/sched/topology.c +3 -3
 {
-	int ret;
+	int ret, lastcpu;
 
 	alloc_bootmem_cpumask_var(&cpu_isolated_map);
 	ret = cpulist_parse(str, cpu_isolated_map);
-	if (ret) {
-		pr_err("sched: Error, all isolcpus= values must be between 0 and %u\n", nr_cpu_ids);
+	lastcpu = cpumask_last(cpu_isolated_map);
+	if (ret || lastcpu >= nr_cpu_ids) {

Any reason why 'lastcpu' has to be introduced - why not just use cpumask_last() directly in the condition? It looks obvious enough of a pattern.

kernel/sched/topology.c cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) · line 476

Quoted 2 lines
kernel/sched/topology.c +2 -0
+		pr_err("sched: Error, all isolcpus= values must be between 0 and %u\n",
+				nr_cpu_ids-1);

Please don't break the line mindlessly just due to checkpatch complaining - it makes the code less readable.

Thanks,

Ingo

#4 lore raw

PATCH 1/1 Fix isocpus's param handling when CPUMASK_OFFSTACK=n.

Quoted 9 lines · Ingo Molnar
Quoted 4 lines · Rakib Mullick

+/**
+ * cpumask_last - get the last cpu in a cpumask

Please capitalize 'CPU' properly in documentation.

OK.

Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +3 -2
+     int ret, lastcpu;
 
      alloc_bootmem_cpumask_var(&cpu_isolated_map);
      ret = cpulist_parse(str, cpu_isolated_map);
-     if (ret) {
-             pr_err("sched: Error, all isolcpus= values must be between 0 and %u\n", nr_cpu_ids);
+     lastcpu = cpumask_last(cpu_isolated_map);
+     if (ret || lastcpu >= nr_cpu_ids) {

Any reason why 'lastcpu' has to be introduced - why not just use cpumask_last() directly in the condition? It looks obvious enough of a pattern.

Thought it reflects what we're doing here, but yes, actually it's redundant.

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+             pr_err("sched: Error, all isolcpus= values must be between 0 and %u\n",
+                             nr_cpu_ids-1);

Please don't break the line mindlessly just due to checkpatch complaining - it makes the code less readable.

Yes, right, mindlessly followed what checkpatch was complaining.

Please see the following patch, with changes made based on your review and patched up against -rc6.

Thanks,
Rakib
---

[PATCH](v1) Fix isocpus's param handling when CPUMASK_OFFSTACK=n.

cpulist_parse() uses nr_cpumask_bits as limit to parse the
passed buffer from kernel commandline. What nr_cpumask_bits represents varies depends upon CONFIG_CPUMASK_OFFSTACK option. If CONFIG_CPUMASK_OFFSTACK=n, then nr_cpumask_bits is same as NR_CPUS, which might not represent the # of cpus really exist (default 64). So, there's a chance of gap between nr_cpu_ids and NR_CPUS, which ultimately lead towards invalid cpulist_parse() operation. For example, if isolcpus=9 is passed on a 8 cpu system (CONFIG_CPUMASK_OFFSTACK=n) it doesn't show the error that it suppose to.

This patch fixes this issue by effectively find out the last cpu of the passed isolcpus list and checking it with nr_cpu_ids. Also, fixes the error message where the nr_cpu_ids should be nr_cpu_ids-1, since the cpu numbering starts from 0.

Changes since v0 (Ingo):
* use cpumask_last() directly into the condition.
* use CPU rather cpu in documentation
* undo line break

---
 include/linux/cpumask.h | 16 ++++++++++++++++
 kernel/sched/topology.c |  4 ++--
 2 files changed, 18 insertions(+), 2 deletions(-)
include/linux/cpumask.h +16 -0
index cd415b7..63661de 100644
@@ -130,6 +130,11 @@ static inline unsigned int cpumask_first(const struct cpumask *srcp)
 	return 0;
 }
 
+static inline unsigned int cpumask_last(const struct cpumask *srcp)
+{
+	return 0;
+}
+
 /* Valid inputs for n are -1 and 0. */
 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
 {
@@ -178,6 +183,17 @@ static inline unsigned int cpumask_first(const struct cpumask *srcp)
 	return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
 }
 
+/**
+ * cpumask_last - get the last CPU in a cpumask
+ * @srcp:	- the cpumask pointer
+ *
+ * Returns	>= nr_cpumask_bits if no CPUs set.
+ */
+static inline unsigned int cpumask_last(const struct cpumask *srcp)
+{
+	return find_last_bit(cpumask_bits(srcp), nr_cpumask_bits);
+}
+
 unsigned int cpumask_next(int n, const struct cpumask *srcp);
 
 /**
kernel/sched/topology.c +2 -2
index f1cf4f3..060cee5 100644
@@ -470,8 +470,8 @@ static int __init isolated_cpu_setup(char *str)
 
 	alloc_bootmem_cpumask_var(&cpu_isolated_map);
 	ret = cpulist_parse(str, cpu_isolated_map);
-	if (ret) {
-		pr_err("sched: Error, all isolcpus= values must be between 0 and %u\n", nr_cpu_ids);
+	if (ret || cpumask_last(cpu_isolated_map) >= nr_cpu_ids) {
+		pr_err("sched: Error, all isolcpus= values must be between 0 and %u\n", nr_cpu_ids-1);
 		return 0;
 	}
 	return 1;
Signature · 1 line
2.9.3
#5 lore raw

[tip:sched/core] sched/isolcpus: Fix "isolcpus=" boot parameter handling when !CONFIG_CPUMASK_OFFSTACK

sched/isolcpus: Fix "isolcpus=" boot parameter handling when !CONFIG_CPUMASK_OFFSTACK
Commit-ID:  e22cdc3fc5991956146b9856d36b4971fe54dcd6
Gitweb:     https://git.kernel.org/tip/e22cdc3fc5991956146b9856d36b4971fe54dcd6
Author:     Rakib Mullick <[email protected]>
AuthorDate: Mon, 23 Oct 2017 19:01:54 +0600
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 24 Oct 2017 11:47:25 +0200

sched/isolcpus: Fix "isolcpus=" boot parameter handling when !CONFIG_CPUMASK_OFFSTACK

cpulist_parse() uses nr_cpumask_bits as a limit to parse the passed buffer from kernel commandline. What nr_cpumask_bits represents varies depending upon the CONFIG_CPUMASK_OFFSTACK option:

- If CONFIG_CPUMASK_OFFSTACK=n, then nr_cpumask_bits is the same as NR_CPUS, which might not represent the # of CPUs that really exist (default 64). So, there's a chance of a gap between nr_cpu_ids and NR_CPUS, which ultimately lead towards invalid cpulist_parse() operation. For example, if isolcpus=9 is passed on an 8 cpu system (CONFIG_CPUMASK_OFFSTACK=n) it doesn't show the error that it's supposed to.

This patch fixes this bug by finding the last CPU of the passed isolcpus= list and checking it against nr_cpu_ids.

It also fixes the error message where the nr_cpu_ids should be nr_cpu_ids-1, since CPU numbering starts from 0.

 include/linux/cpumask.h |   16 ++++++++++++++++
 kernel/sched/topology.c |    4 ++--
 2 files changed, 18 insertions(+), 2 deletions(-)
---
 include/linux/cpumask.h | 16 ++++++++++++++++
 kernel/sched/topology.c |  4 ++--
 2 files changed, 18 insertions(+), 2 deletions(-)
include/linux/cpumask.h +16 -0
index cd415b7..63661de 100644
@@ -130,6 +130,11 @@ static inline unsigned int cpumask_first(const struct cpumask *srcp)
 	return 0;
 }
 
+static inline unsigned int cpumask_last(const struct cpumask *srcp)
+{
+	return 0;
+}
+
 /* Valid inputs for n are -1 and 0. */
 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
 {
@@ -178,6 +183,17 @@ static inline unsigned int cpumask_first(const struct cpumask *srcp)
 	return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
 }
 
+/**
+ * cpumask_last - get the last CPU in a cpumask
+ * @srcp:	- the cpumask pointer
+ *
+ * Returns	>= nr_cpumask_bits if no CPUs set.
+ */
+static inline unsigned int cpumask_last(const struct cpumask *srcp)
+{
+	return find_last_bit(cpumask_bits(srcp), nr_cpumask_bits);
+}
+
 unsigned int cpumask_next(int n, const struct cpumask *srcp);
 
 /**
kernel/sched/topology.c +2 -2
index e50450c..e3d31b0 100644
@@ -476,8 +476,8 @@ static int __init isolated_cpu_setup(char *str)
 
 	alloc_bootmem_cpumask_var(&cpu_isolated_map);
 	ret = cpulist_parse(str, cpu_isolated_map);
-	if (ret) {
-		pr_err("sched: Error, all isolcpus= values must be between 0 and %u\n", nr_cpu_ids);
+	if (ret || cpumask_last(cpu_isolated_map) >= nr_cpu_ids) {
+		pr_err("sched: Error, all isolcpus= values must be between 0 and %u - ignoring them.\n", nr_cpu_ids-1);
 		return 0;
 	}
 	return 1;