workqueue: Fix possible memory leaks in wq_numa_init()
authorZhen Lei <thunder.leizhen@huawei.com>
Thu, 22 Jul 2021 03:03:52 +0000 (11:03 +0800)
committerTejun Heo <tj@kernel.org>
Thu, 29 Jul 2021 17:16:00 +0000 (07:16 -1000)
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.

tj: Note that the condition only occurs when the arch code is pretty broken
and the WARN_ON might as well be BUG_ON().

Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
Reviewed-by: Lai Jiangshan <jiangshanlai@gmail.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
kernel/workqueue.c

index f148eacda55a937645e43bca1189e74cfd43d5bc..542c2d03dab655c1895fc57dd35cd9a02aea1540 100644 (file)
@@ -5902,6 +5902,13 @@ 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);
+                       return;
+               }
+       }
+
        wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs();
        BUG_ON(!wq_update_unbound_numa_attrs_buf);
 
@@ -5919,11 +5926,6 @@ static void __init wq_numa_init(void)
 
        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]);
        }