linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] workqueue: Fix possible memory leaks in wq_numa_init()
@ 2021-07-19  6:59 Zhen Lei
  2021-07-19  8:49 ` Lai Jiangshan
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Zhen Lei @ 2021-07-19  6:59 UTC (permalink / raw)
  To: Tejun Heo, Lai Jiangshan, linux-kernel; +Cc: 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.

Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
 kernel/workqueue.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 50142fc08902..6aa0ba582d15 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] workqueue: Fix possible memory leaks in wq_numa_init()
  2021-07-19  6:59 [PATCH] workqueue: Fix possible memory leaks in wq_numa_init() Zhen Lei
@ 2021-07-19  8:49 ` Lai Jiangshan
  2021-07-21 16:23   ` Tejun Heo
  2021-07-22  1:55 ` Lai Jiangshan
  2021-07-22  2:05 ` Lai Jiangshan
  2 siblings, 1 reply; 7+ messages in thread
From: Lai Jiangshan @ 2021-07-19  8:49 UTC (permalink / raw)
  To: Zhen Lei; +Cc: Tejun Heo, linux-kernel

Hello, Tejun

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

https://lore.kernel.org/lkml/1418379595-6281-2-git-send-email-laijs@cn.fujitsu.com/
https://lore.kernel.org/lkml/1442457835-24238-1-git-send-email-xlpang@126.com/
https://lore.kernel.org/lkml/1452170339-26748-3-git-send-email-wanghaibin.wang@huawei.com/
https://lore.kernel.org/lkml/1533518439-3982-1-git-send-email-wang.yi59@zte.com.cn/

And this one.

Thanks
Lai


On Mon, Jul 19, 2021 at 3:00 PM Zhen Lei <thunder.leizhen@huawei.com> wrote:
>
> 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.
>
> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> ---
>  kernel/workqueue.c | 19 ++++++++++---------
>  1 file changed, 10 insertions(+), 9 deletions(-)
>
> diff --git a/kernel/workqueue.c b/kernel/workqueue.c
> index 50142fc08902..6aa0ba582d15 100644
> --- a/kernel/workqueue.c
> +++ b/kernel/workqueue.c
> @@ -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
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] workqueue: Fix possible memory leaks in wq_numa_init()
  2021-07-19  8:49 ` Lai Jiangshan
@ 2021-07-21 16:23   ` Tejun Heo
  0 siblings, 0 replies; 7+ messages in thread
From: Tejun Heo @ 2021-07-21 16:23 UTC (permalink / raw)
  To: Lai Jiangshan; +Cc: Zhen Lei, linux-kernel

On Mon, Jul 19, 2021 at 04:49:05PM +0800, Lai Jiangshan wrote:
> Hello, Tejun
> 
> I think it would be better to fix it.
> There have been several tries, sorted by date:
> 
> https://lore.kernel.org/lkml/1418379595-6281-2-git-send-email-laijs@cn.fujitsu.com/
> https://lore.kernel.org/lkml/1442457835-24238-1-git-send-email-xlpang@126.com/
> https://lore.kernel.org/lkml/1452170339-26748-3-git-send-email-wanghaibin.wang@huawei.com/
> https://lore.kernel.org/lkml/1533518439-3982-1-git-send-email-wang.yi59@zte.com.cn/
> 
> 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.

-- 
tejun

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] workqueue: Fix possible memory leaks in wq_numa_init()
  2021-07-19  6:59 [PATCH] workqueue: Fix possible memory leaks in wq_numa_init() Zhen Lei
  2021-07-19  8:49 ` Lai Jiangshan
@ 2021-07-22  1:55 ` Lai Jiangshan
  2021-07-22  2:37   ` Leizhen (ThunderTown)
  2021-07-22  2:05 ` Lai Jiangshan
  2 siblings, 1 reply; 7+ messages in thread
From: Lai Jiangshan @ 2021-07-22  1:55 UTC (permalink / raw)
  To: Zhen Lei; +Cc: Tejun Heo, linux-kernel

On Mon, Jul 19, 2021 at 3:00 PM Zhen Lei <thunder.leizhen@huawei.com> wrote:
>
> 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.
>
> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> ---
>  kernel/workqueue.c | 19 ++++++++++---------
>  1 file changed, 10 insertions(+), 9 deletions(-)
>
> diff --git a/kernel/workqueue.c b/kernel/workqueue.c
> index 50142fc08902..6aa0ba582d15 100644
> --- a/kernel/workqueue.c
> +++ b/kernel/workqueue.c
> @@ -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));

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.

>         }
>
>         wq_numa_possible_cpumask = tbl;
> --
> 2.25.1
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] workqueue: Fix possible memory leaks in wq_numa_init()
  2021-07-19  6:59 [PATCH] workqueue: Fix possible memory leaks in wq_numa_init() Zhen Lei
  2021-07-19  8:49 ` Lai Jiangshan
  2021-07-22  1:55 ` Lai Jiangshan
@ 2021-07-22  2:05 ` Lai Jiangshan
  2021-07-22  2:44   ` Leizhen (ThunderTown)
  2 siblings, 1 reply; 7+ messages in thread
From: Lai Jiangshan @ 2021-07-22  2:05 UTC (permalink / raw)
  To: Zhen Lei; +Cc: Tejun Heo, linux-kernel

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

On Thu, Jul 22, 2021 at 12:23 AM Tejun Heo <tj@kernel.org> wrote:
> 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.

On Mon, Jul 19, 2021 at 3:00 PM Zhen Lei <thunder.leizhen@huawei.com> wrote:
>
> 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.
>
> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> ---
>  kernel/workqueue.c | 19 ++++++++++---------
>  1 file changed, 10 insertions(+), 9 deletions(-)
>
> diff --git a/kernel/workqueue.c b/kernel/workqueue.c
> index 50142fc08902..6aa0ba582d15 100644
> --- a/kernel/workqueue.c
> +++ b/kernel/workqueue.c
> @@ -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.

> +                       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));

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]);"

>         }
>
>         wq_numa_possible_cpumask = tbl;
> --
> 2.25.1
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] workqueue: Fix possible memory leaks in wq_numa_init()
  2021-07-22  1:55 ` Lai Jiangshan
@ 2021-07-22  2:37   ` Leizhen (ThunderTown)
  0 siblings, 0 replies; 7+ messages in thread
From: Leizhen (ThunderTown) @ 2021-07-22  2:37 UTC (permalink / raw)
  To: Lai Jiangshan; +Cc: Tejun Heo, linux-kernel



On 2021/7/22 9:55, Lai Jiangshan wrote:
> On Mon, Jul 19, 2021 at 3:00 PM Zhen Lei <thunder.leizhen@huawei.com> wrote:
>>
>> 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.
>>
>> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
>> ---
>>  kernel/workqueue.c | 19 ++++++++++---------
>>  1 file changed, 10 insertions(+), 9 deletions(-)
>>
>> diff --git a/kernel/workqueue.c b/kernel/workqueue.c
>> index 50142fc08902..6aa0ba582d15 100644
>> --- a/kernel/workqueue.c
>> +++ b/kernel/workqueue.c
>> @@ -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));
> 
> 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.

OK,thanks.

> 
>>         }
>>
>>         wq_numa_possible_cpumask = tbl;
>> --
>> 2.25.1
>>
> .
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] workqueue: Fix possible memory leaks in wq_numa_init()
  2021-07-22  2:05 ` Lai Jiangshan
@ 2021-07-22  2:44   ` Leizhen (ThunderTown)
  0 siblings, 0 replies; 7+ messages in thread
From: Leizhen (ThunderTown) @ 2021-07-22  2:44 UTC (permalink / raw)
  To: Lai Jiangshan; +Cc: Tejun Heo, linux-kernel



On 2021/7/22 10:05, Lai Jiangshan wrote:
> Please ignore my previous email which included/copied a wrong line of code.
> 
> On Thu, Jul 22, 2021 at 12:23 AM Tejun Heo <tj@kernel.org> wrote:
>> 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.


> 
> On Mon, Jul 19, 2021 at 3:00 PM Zhen Lei <thunder.leizhen@huawei.com> wrote:
>>
>> 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.
>>
>> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
>> ---
>>  kernel/workqueue.c | 19 ++++++++++---------
>>  1 file changed, 10 insertions(+), 9 deletions(-)
>>
>> diff --git a/kernel/workqueue.c b/kernel/workqueue.c
>> index 50142fc08902..6aa0ba582d15 100644
>> --- a/kernel/workqueue.c
>> +++ b/kernel/workqueue.c
>> @@ -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.

OK, I will remove it.

> 
>> +                       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));
> 
> 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]);"

OK.

> 
>>         }
>>
>>         wq_numa_possible_cpumask = tbl;
>> --
>> 2.25.1
>>
> .
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-07-22  2:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-19  6:59 [PATCH] workqueue: Fix possible memory leaks in wq_numa_init() Zhen Lei
2021-07-19  8:49 ` Lai Jiangshan
2021-07-21 16:23   ` Tejun Heo
2021-07-22  1:55 ` Lai Jiangshan
2021-07-22  2:37   ` Leizhen (ThunderTown)
2021-07-22  2:05 ` Lai Jiangshan
2021-07-22  2:44   ` Leizhen (ThunderTown)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).