Skip to contentSkip to conversation

workqueue: Fix possible memory leaks in wq_numa_init()

PATCH Superseded Zhen Lei 3 participants 7 messages Jul 19, 2021 – Jul 22, 2021 Workqueue

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Zhen Lei

author
#0 lore raw

PATCH 1/1 workqueue: Fix possible memory leaks in wq_numa_init()

In error handling branch "if (WARN_ON(node == NUMA_NO_NODE))", the previously allocated memories are not released. Doing this before allocating memory eliminates memory leaks.

---
 kernel/workqueue.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
kernel/workqueue.c +10 -9
index 50142fc08902..6aa0ba582d15 100644
@@ -5896,6 +5896,14 @@ static void __init wq_numa_init(void)
 		return;
 	}
 
+	for_each_possible_cpu(cpu) {
+		if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) {
+			pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
+			/* happens iff arch is bonkers, let's just proceed */
+			return;
+		}
+	}
+
 	wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs();
 	BUG_ON(!wq_update_unbound_numa_attrs_buf);
 
@@ -5907,18 +5915,11 @@ static void __init wq_numa_init(void)
 	tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL);
 	BUG_ON(!tbl);
 
-	for_each_node(node)
+	for_each_node(node) {
 		BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
 				node_online(node) ? node : NUMA_NO_NODE));
 
-	for_each_possible_cpu(cpu) {
-		node = cpu_to_node(cpu);
-		if (WARN_ON(node == NUMA_NO_NODE)) {
-			pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
-			/* happens iff arch is bonkers, let's just proceed */
-			return;
-		}
-		cpumask_set_cpu(cpu, tbl[node]);
+		cpumask_copy(tbl[node], cpumask_of_node(node));
 	}
 
 	wq_numa_possible_cpumask = tbl;
Signature · 1 line
2.25.1
reviewer
#1 lore raw

Hello, Tejun

I think it would be better to fix it.
There have been several tries, sorted by date:

https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

And this one.

Thanks
Lai

Quoted 53 lines · Zhen Lei

In error handling branch "if (WARN_ON(node == NUMA_NO_NODE))", the previously allocated memories are not released. Doing this before allocating memory eliminates memory leaks.

---
 kernel/workqueue.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
kernel/workqueue.c +10 -10
index 50142fc08902..6aa0ba582d15 100644
@@ -5896,6 +5896,14 @@ static void __init wq_numa_init(void)
                return;
        }
 
+       for_each_possible_cpu(cpu) {
+               if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) {
+                       pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
+                       /* happens iff arch is bonkers, let's just proceed */
+                       return;
+               }
+       }
+
        wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs();
        BUG_ON(!wq_update_unbound_numa_attrs_buf);
 
@@ -5907,18 +5915,11 @@ static void __init wq_numa_init(void)
        tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL);
        BUG_ON(!tbl);
 
-       for_each_node(node)
+       for_each_node(node) {
                BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
                                node_online(node) ? node : NUMA_NO_NODE));
 
-       for_each_possible_cpu(cpu) {
-               node = cpu_to_node(cpu);
-               if (WARN_ON(node == NUMA_NO_NODE)) {
-                       pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
-                       /* happens iff arch is bonkers, let's just proceed */
-                       return;
-               }
-               cpumask_set_cpu(cpu, tbl[node]);
+               cpumask_copy(tbl[node], cpumask_of_node(node));
        }
 
        wq_numa_possible_cpumask = tbl;
--

2.25.1

maintainer
#2 lore raw
Quoted 11 lines · Lai Jiangshan

Hello, Tejun

I think it would be better to fix it.
There have been several tries, sorted by date:

https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

And this one.

Yeah, idk. It's not an actual bug tho. Maybe we should just add a comment. Which version do you like the best?

Thanks.

Signature · 1 line
tejun
reviewer
#3 lore raw
In reply to Zhen Lei
Quoted 37 lines · Zhen Lei

In error handling branch "if (WARN_ON(node == NUMA_NO_NODE))", the previously allocated memories are not released. Doing this before allocating memory eliminates memory leaks.

---
 kernel/workqueue.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
kernel/workqueue.c +9 -1
index 50142fc08902..6aa0ba582d15 100644
@@ -5896,6 +5896,14 @@ static void __init wq_numa_init(void)
                return;
        }
 
+       for_each_possible_cpu(cpu) {
+               if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) {
+                       pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
+                       /* happens iff arch is bonkers, let's just proceed */
+                       return;
+               }
+       }
+
        wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs();
        BUG_ON(!wq_update_unbound_numa_attrs_buf);
 
@@ -5907,18 +5915,11 @@ static void __init wq_numa_init(void)
        tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL);
        BUG_ON(!tbl);
 
-       for_each_node(node)
+       for_each_node(node) {
                BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
                                node_online(node) ? node : NUMA_NO_NODE));

kernel/workqueue.c static void __init wq_numa_init(void) · line 5922

Quoted 10 lines
kernel/workqueue.c +1 -8
 
-       for_each_possible_cpu(cpu) {
-               node = cpu_to_node(cpu);
-               if (WARN_ON(node == NUMA_NO_NODE)) {
-                       pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
-                       /* happens iff arch is bonkers, let's just proceed */
-                       return;
-               }
-               cpumask_set_cpu(cpu, tbl[node]);
+               cpumask_copy(tbl[node], cpumask_of_node(node));

It is incorrect. cpumask_of_node(node) is the online cpumask of the node, not the possible cpumask of the node that we are interested in.

If the NUMA subsystem provided something like cpumask_possible_of_node(node), we wouldn't need wq_numa_possible_cpumask.

Please keep "cpumask_copy(tbl[node], cpumask_of_node(node));" as before.

Quoted 6 lines
kernel/workqueue.c +0 -1
        }
 
        wq_numa_possible_cpumask = tbl;
--

2.25.1

#4 lore raw
Quoted 52 lines · Lai Jiangshan
Quoted 37 lines · Zhen Lei

In error handling branch "if (WARN_ON(node == NUMA_NO_NODE))", the previously allocated memories are not released. Doing this before allocating memory eliminates memory leaks.

---
 kernel/workqueue.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
kernel/workqueue.c +9 -1
index 50142fc08902..6aa0ba582d15 100644
@@ -5896,6 +5896,14 @@ static void __init wq_numa_init(void)
                return;
        }
 
+       for_each_possible_cpu(cpu) {
+               if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) {
+                       pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
+                       /* happens iff arch is bonkers, let's just proceed */
+                       return;
+               }
+       }
+
        wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs();
        BUG_ON(!wq_update_unbound_numa_attrs_buf);
 
@@ -5907,18 +5915,11 @@ static void __init wq_numa_init(void)
        tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL);
        BUG_ON(!tbl);
 
-       for_each_node(node)
+       for_each_node(node) {
                BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
                                node_online(node) ? node : NUMA_NO_NODE));

kernel/workqueue.c static void __init wq_numa_init(void) · line 5922

Quoted 10 lines
kernel/workqueue.c +1 -8
 
-       for_each_possible_cpu(cpu) {
-               node = cpu_to_node(cpu);
-               if (WARN_ON(node == NUMA_NO_NODE)) {
-                       pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
-                       /* happens iff arch is bonkers, let's just proceed */
-                       return;
-               }
-               cpumask_set_cpu(cpu, tbl[node]);
+               cpumask_copy(tbl[node], cpumask_of_node(node));

It is incorrect. cpumask_of_node(node) is the online cpumask of the node, not the possible cpumask of the node that we are interested in.

Quoted 4 lines

If the NUMA subsystem provided something like cpumask_possible_of_node(node), we wouldn't need wq_numa_possible_cpumask.

Please keep "cpumask_copy(tbl[node], cpumask_of_node(node));" as before.

OK,thanks.

Quoted 9 lines
Quoted 7 lines
kernel/workqueue.c +0 -1
        }
 
        wq_numa_possible_cpumask = tbl;
--

2.25.1

.

reviewer
#5 lore raw
In reply to Zhen Lei

Please ignore my previous email which included/copied a wrong line of code.

Quoted 2 lines · Tejun Heo

Yeah, idk. It's not an actual bug tho. Maybe we should just add a comment. Which version do you like the best?

I hope Zhen Lei continues to refine his version. I think he is still working on it.

Quoted 15 lines · Zhen Lei

In error handling branch "if (WARN_ON(node == NUMA_NO_NODE))", the previously allocated memories are not released. Doing this before allocating memory eliminates memory leaks.

---
 kernel/workqueue.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
kernel/workqueue.c +0 -0
index 50142fc08902..6aa0ba582d15 100644

kernel/workqueue.c static void __init wq_numa_init(void) · line 5904

Quoted 9 lines
kernel/workqueue.c +4 -0
@@ -5896,6 +5896,14 @@ static void __init wq_numa_init(void)
                return;
        }
 
+       for_each_possible_cpu(cpu) {
+               if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) {
+                       pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
 
 
+                       /* happens iff arch is bonkers, let's just proceed */

I don't think this comment is still needed.

Quoted 15 lines
kernel/workqueue.c +4 -0
+                       return;
+               }
+       }
+
        wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs();
        BUG_ON(!wq_update_unbound_numa_attrs_buf);
kernel/workqueue.c +1 -1
@@ -5907,18 +5915,11 @@ static void __init wq_numa_init(void)
        tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL);
        BUG_ON(!tbl);
 
-       for_each_node(node)
+       for_each_node(node) {
                BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
                                node_online(node) ? node : NUMA_NO_NODE));

kernel/workqueue.c static void __init wq_numa_init(void) · line 5922

Quoted 10 lines
kernel/workqueue.c +1 -8
 
-       for_each_possible_cpu(cpu) {
-               node = cpu_to_node(cpu);
-               if (WARN_ON(node == NUMA_NO_NODE)) {
-                       pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
-                       /* happens iff arch is bonkers, let's just proceed */
-                       return;
-               }
-               cpumask_set_cpu(cpu, tbl[node]);
+               cpumask_copy(tbl[node], cpumask_of_node(node));

It is incorrect. cpumask_of_node(node) is the online cpumask of the node, not the possible cpumask of the node that we are interested in.

If the NUMA subsystem provided something like cpumask_possible_of_node(node), we wouldn't need wq_numa_possible_cpumask.

Please keep "for_each_possible_cpu(cpu)" loop and
"cpumask_set_cpu(cpu, tbl[node]);"

Quoted 6 lines
kernel/workqueue.c +0 -1
        }
 
        wq_numa_possible_cpumask = tbl;
--

2.25.1

#6 lore raw
Quoted 8 lines · Lai Jiangshan

Please ignore my previous email which included/copied a wrong line of code.

Quoted 2 lines · Tejun Heo

Yeah, idk. It's not an actual bug tho. Maybe we should just add a comment. Which version do you like the best?

I hope Zhen Lei continues to refine his version. I think he is still working on it.

Yes, I'm inclined to fix it, too. The tools aren't that smart, and handling false positives is an annoyance.

Quoted 20 lines · Zhen Lei
Quoted 18 lines

In error handling branch "if (WARN_ON(node == NUMA_NO_NODE))", the previously allocated memories are not released. Doing this before allocating memory eliminates memory leaks.

---
 kernel/workqueue.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
kernel/workqueue.c +0 -0
index 50142fc08902..6aa0ba582d15 100644
@@ -5896,6 +5896,14 @@ static void __init wq_numa_init(void)
                return;
        }
 
Quoted 8 lines

kernel/workqueue.c static void __init wq_numa_init(void)

Quoted 6 lines
kernel/workqueue.c +4 -0
+       for_each_possible_cpu(cpu) {
+               if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) {
+                       pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
 
 
+                       /* happens iff arch is bonkers, let's just proceed */

I don't think this comment is still needed.

OK, I will remove it.

Quoted 30 lines
Quoted 16 lines
kernel/workqueue.c +4 -0
+                       return;
+               }
+       }
+
        wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs();
        BUG_ON(!wq_update_unbound_numa_attrs_buf);
kernel/workqueue.c +1 -1
@@ -5907,18 +5915,11 @@ static void __init wq_numa_init(void)
        tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL);
        BUG_ON(!tbl);
 
-       for_each_node(node)
+       for_each_node(node) {
                BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
                                node_online(node) ? node : NUMA_NO_NODE));

kernel/workqueue.c static void __init wq_numa_init(void) · line 5922

Quoted 10 lines
kernel/workqueue.c +1 -8
 
-       for_each_possible_cpu(cpu) {
-               node = cpu_to_node(cpu);
-               if (WARN_ON(node == NUMA_NO_NODE)) {
-                       pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
-                       /* happens iff arch is bonkers, let's just proceed */
-                       return;
-               }
-               cpumask_set_cpu(cpu, tbl[node]);
+               cpumask_copy(tbl[node], cpumask_of_node(node));

It is incorrect. cpumask_of_node(node) is the online cpumask of the node, not the possible cpumask of the node that we are interested in.

Quoted 5 lines

If the NUMA subsystem provided something like cpumask_possible_of_node(node), we wouldn't need wq_numa_possible_cpumask.

Please keep "for_each_possible_cpu(cpu)" loop and
"cpumask_set_cpu(cpu, tbl[node]);"

OK.

Quoted 9 lines
Quoted 7 lines
kernel/workqueue.c +0 -1
        }
 
        wq_numa_possible_cpumask = tbl;
--

2.25.1

.