Skip to contentSkip to conversation

workqueue: Fix edge cases for calc of pool's cpumask

PATCH Superseded Michael Bringmann 2 participants 4 messages Jun 6, 2017 Workqueue

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 workqueue: Fix edge cases for calc of pool's cpumask

On NUMA systems with dynamic processors, the content of the cpumask may change over time. As new processors are added via DLPAR operations, workqueues are created for them. Depending upon the order in which CPUs are added/removed, we may run into problems with the content of the cpumask used by the workqueues. This patch deals with situations where the online cpumask for a node is a proper superset of possible cpumask for the node. It also deals with edge cases where the order in which CPUs are removed/added from the online cpumask may leave the set for a node empty, and require execution by CPUs on another node.

In these and other cases, the patch attempts to ensure that a valid, usable cpumask is used to set up newly created pools for workqueues.

---
Changes in V2:
-- Merge in additional logic fixes provided by Tejun Heo. -- Revise safety check added to get_unbound_pool to trigger only
in the event of a dangerously invalid cpumask attribute.

---
 kernel/workqueue.c |   15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
kernel/workqueue.c +8 -7
index c74bf39..460de61 100644
@@ -3366,6 +3366,9 @@ static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
 	copy_workqueue_attrs(pool->attrs, attrs);
 	pool->node = target_node;
 
+	if (!cpumask_weight(pool->attrs->cpumask))
+		cpumask_copy(pool->attrs->cpumask, cpumask_of(smp_processor_id()));
+
 	/*
 	 * no_numa isn't a worker_pool attribute, always clear it.  See
 	 * 'struct workqueue_attrs' comments for detail.
@@ -3559,13 +3562,13 @@ static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
  * stable.
  *
  * Return: %true if the resulting @cpumask is different from @attrs->cpumask,
- * %false if equal.
+ * %false if equal.  On %false return, the content of @cpumask is undefined.
  */
 static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
 				 int cpu_going_down, cpumask_t *cpumask)
 {
 	if (!wq_numa_enabled || attrs->no_numa)
-		goto use_dfl;
+		return false;
 
 	/* does @node have any online CPUs @attrs wants? */
 	cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
@@ -3573,15 +3576,13 @@ static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
 		cpumask_clear_cpu(cpu_going_down, cpumask);
 
 	if (cpumask_empty(cpumask))
-		goto use_dfl;
+		return false;
 
 	/* yeap, return possible CPUs in @node that @attrs wants */
 	cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
-	return !cpumask_equal(cpumask, attrs->cpumask);
 
-use_dfl:
-	cpumask_copy(cpumask, attrs->cpumask);
-	return false;
+	return !cpumask_empty(cpumask) &&
+		!cpumask_equal(cpumask, attrs->cpumask);
 }
 
 /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
maintainer
#1 lore raw

Hello, Michael.

It would have been better to continue debugging in the prev thread. This still seems incorrect for the same reason as before.

Quoted 10 lines · Michael Bringmann

On NUMA systems with dynamic processors, the content of the cpumask may change over time. As new processors are added via DLPAR operations, workqueues are created for them. Depending upon the order in which CPUs are added/removed, we may run into problems with the content of the cpumask used by the workqueues. This patch deals with situations where the online cpumask for a node is a proper superset of possible cpumask for the node. It also deals with edge cases where the order in which CPUs are removed/added from the online cpumask may leave the set for a node empty, and require execution by CPUs on another node.

Quoted 4 lines

In these and other cases, the patch attempts to ensure that a valid, usable cpumask is used to set up newly created pools for workqueues.

Heh, you can't add sob's for other people. For partial attributions, you can just note in the description.

kernel/workqueue.c static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) · line 3370

Quoted 10 lines
kernel/workqueue.c +2 -0
index c74bf39..460de61 100644
@@ -3366,6 +3366,9 @@ static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
 	copy_workqueue_attrs(pool->attrs, attrs);
 	pool->node = target_node;
 
+	if (!cpumask_weight(pool->attrs->cpumask))
+		cpumask_copy(pool->attrs->cpumask, cpumask_of(smp_processor_id()));

So, this is still wrong.

Quoted 25 lines
kernel/workqueue.c +3 -3
 	/*
 	 * no_numa isn't a worker_pool attribute, always clear it.  See
 	 * 'struct workqueue_attrs' comments for detail.
@@ -3559,13 +3562,13 @@ static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
  * stable.
  *
  * Return: %true if the resulting @cpumask is different from @attrs->cpumask,
- * %false if equal.
+ * %false if equal.  On %false return, the content of @cpumask is undefined.
  */
 static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
 				 int cpu_going_down, cpumask_t *cpumask)
 {
 	if (!wq_numa_enabled || attrs->no_numa)
-		goto use_dfl;
+		return false;
 
 	/* does @node have any online CPUs @attrs wants? */
 	cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
@@ -3573,15 +3576,13 @@ static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
 		cpumask_clear_cpu(cpu_going_down, cpumask);
 
 	if (cpumask_empty(cpumask))
-		goto use_dfl;
+		return false;

kernel/workqueue.c static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, · line 3585

Quoted 10 lines
kernel/workqueue.c +2 -4
 
 	/* yeap, return possible CPUs in @node that @attrs wants */
 	cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
-	return !cpumask_equal(cpumask, attrs->cpumask);
 
-use_dfl:
-	cpumask_copy(cpumask, attrs->cpumask);
-	return false;
+	return !cpumask_empty(cpumask) &&
+		!cpumask_equal(cpumask, attrs->cpumask);

And this part doesn't really change that.

CPUs going offline or online shouldn't change their relation to wq_numa_possible_cpumask. I wonder whether the arch code is changing CPU id <-> NUMA node mapping on CPU on/offlining. x86 used to do that too and got recently modified. Can you see whether that's the case?

Thanks.

Signature · 1 line
tejun
#2 lore raw
Quoted 16 lines · Tejun Heo

Hello, Michael.

It would have been better to continue debugging in the prev thread. This still seems incorrect for the same reason as before.

Quoted 10 lines · Michael Bringmann

On NUMA systems with dynamic processors, the content of the cpumask may change over time. As new processors are added via DLPAR operations, workqueues are created for them. Depending upon the order in which CPUs are added/removed, we may run into problems with the content of the cpumask used by the workqueues. This patch deals with situations where the online cpumask for a node is a proper superset of possible cpumask for the node. It also deals with edge cases where the order in which CPUs are removed/added from the online cpumask may leave the set for a node empty, and require execution by CPUs on another node.

Quoted 7 lines
Quoted 4 lines

In these and other cases, the patch attempts to ensure that a valid, usable cpumask is used to set up newly created pools for workqueues.

Heh, you can't add sob's for other people. For partial attributions, you can just note in the description.

Sorry for the error.

Quoted 9 lines
Quoted 9 lines
kernel/workqueue.c +0 -0
index c74bf39..460de61 100644
@@ -3366,6 +3366,9 @@ static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
 	copy_workqueue_attrs(pool->attrs, attrs);
 	pool->node = target_node;
 
Quoted 4 lines

kernel/workqueue.c static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) · line 3370

Quoted 2 lines
kernel/workqueue.c +2 -0
+	if (!cpumask_weight(pool->attrs->cpumask))
+		cpumask_copy(pool->attrs->cpumask, cpumask_of(smp_processor_id()));

So, this is still wrong.

It only catches if something has gone wrong before. The alternative in this case would be,

	BUG(!cpumask_weight(pool->attrs->cpumask));
Quoted 39 lines
Quoted 26 lines
kernel/workqueue.c +3 -3
 	/*
 	 * no_numa isn't a worker_pool attribute, always clear it.  See
 	 * 'struct workqueue_attrs' comments for detail.
@@ -3559,13 +3562,13 @@ static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
  * stable.
  *
  * Return: %true if the resulting @cpumask is different from @attrs->cpumask,
- * %false if equal.
+ * %false if equal.  On %false return, the content of @cpumask is undefined.
  */
 static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
 				 int cpu_going_down, cpumask_t *cpumask)
 {
 	if (!wq_numa_enabled || attrs->no_numa)
-		goto use_dfl;
+		return false;
 
 	/* does @node have any online CPUs @attrs wants? */
 	cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
@@ -3573,15 +3576,13 @@ static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
 		cpumask_clear_cpu(cpu_going_down, cpumask);
 
 	if (cpumask_empty(cpumask))
-		goto use_dfl;
+		return false;

kernel/workqueue.c static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, · line 3585

Quoted 10 lines
kernel/workqueue.c +2 -4
 
 	/* yeap, return possible CPUs in @node that @attrs wants */
 	cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
-	return !cpumask_equal(cpumask, attrs->cpumask);
 
-use_dfl:
-	cpumask_copy(cpumask, attrs->cpumask);
-	return false;
+	return !cpumask_empty(cpumask) &&
+		!cpumask_equal(cpumask, attrs->cpumask);

And this part doesn't really change that.

Quoted 4 lines

CPUs going offline or online shouldn't change their relation to wq_numa_possible_cpumask. I wonder whether the arch code is changing CPU id <-> NUMA node mapping on CPU on/offlining. x86 used to do that too and got recently modified. Can you see whether that's the case?

The but that I see does not appear to be related to changing of CPU/Node mapping -- they are not changing their place when going offline/online. Rather new CPUs are being hot-added to the system (i.e. they were not present at boot), and the node to which they are being added had no CPUs at boot.

Quoted 3 lines

Thanks.

Thanks.

Signature · 7 lines
Michael W. Bringmann
Linux Technology Center
IBM Corporation
Tie-Line  363-5196
External: (512) 286-5196
Cell:       (512) 466-0650
[email protected]
maintainer
#3 lore raw

Hello,

Quoted 13 lines · Michael Bringmann
Quoted 8 lines
Quoted 8 lines
kernel/workqueue.c +0 -0
index c74bf39..460de61 100644
@@ -3366,6 +3366,9 @@ static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
 	copy_workqueue_attrs(pool->attrs, attrs);
 	pool->node = target_node;
 
Quoted 4 lines

kernel/workqueue.c static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) · line 3370

Quoted 2 lines
kernel/workqueue.c +2 -0
+	if (!cpumask_weight(pool->attrs->cpumask))
+		cpumask_copy(pool->attrs->cpumask, cpumask_of(smp_processor_id()));

So, this is still wrong.

Quoted 4 lines

It only catches if something has gone wrong before. The alternative in this case would be,

	BUG(!cpumask_weight(pool->attrs->cpumask));

I'm kinda confused, so the hotplug problems you see across hotplugs go away without the above change and the above is "just in case"?

Quoted 40 lines
Quoted 35 lines
Quoted 22 lines
kernel/workqueue.c +3 -3
@@ -3559,13 +3562,13 @@ static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
  * stable.
  *
  * Return: %true if the resulting @cpumask is different from @attrs->cpumask,
- * %false if equal.
+ * %false if equal.  On %false return, the content of @cpumask is undefined.
  */
 static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
 				 int cpu_going_down, cpumask_t *cpumask)
 {
 	if (!wq_numa_enabled || attrs->no_numa)
-		goto use_dfl;
+		return false;
 
 	/* does @node have any online CPUs @attrs wants? */
 	cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
@@ -3573,15 +3576,13 @@ static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
 		cpumask_clear_cpu(cpu_going_down, cpumask);
 
 	if (cpumask_empty(cpumask))
-		goto use_dfl;
+		return false;

kernel/workqueue.c static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, · line 3585

Quoted 10 lines
kernel/workqueue.c +2 -4
 
 	/* yeap, return possible CPUs in @node that @attrs wants */
 	cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
-	return !cpumask_equal(cpumask, attrs->cpumask);
 
-use_dfl:
-	cpumask_copy(cpumask, attrs->cpumask);
-	return false;
+	return !cpumask_empty(cpumask) &&
+		!cpumask_equal(cpumask, attrs->cpumask);

And this part doesn't really change that.

Quoted 4 lines

CPUs going offline or online shouldn't change their relation to wq_numa_possible_cpumask. I wonder whether the arch code is changing CPU id <-> NUMA node mapping on CPU on/offlining. x86 used to do that too and got recently modified. Can you see whether that's the case?

Quoted 4 lines

The but that I see does not appear to be related to changing of CPU/Node mapping -- they are not changing their place when going offline/online. Rather new CPUs are being hot-added to the system (i.e. they were not present at boot), and the node to which they are being added had no CPUs at boot.

Can you please post the messages with the debug patch from the prev thread? In fact, let's please continue on that thread. I'm having a hard time following what's going wrong with the code.

Thanks.

Signature · 1 line
tejun