Skip to contentSkip to conversation

kernel/workqueue.c: better to free related resources when failure occurs in wq_numa_init()

PATCH In review Chen Gang 2 participants 10 messages May 14, 2013 – May 17, 2013 Workqueue

lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 kernel/workqueue.c: better to free related resources when failure occurs in wq_numa_init()

When NUMA node mapping not available for one cpu, need disabling NUMA support and proceed.

Also better to release unused resources (the original code can not cause issue, only waste some memory).

---
 kernel/workqueue.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/workqueue.c +3 -0
index 1ae6028..a763079 100644
@@ -4911,6 +4911,9 @@ static void __init wq_numa_init(void)
 		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 */
+			for_each_node(node)
+				free_cpumask_var(tbl[node]);
+			kfree(tbl);
 			return;
 		}
 		cpumask_set_cpu(cpu, tbl[node]);
Signature · 1 line
1.7.11.7
maintainer
#1 lore raw
Quoted 18 lines · Chen Gang

When NUMA node mapping not available for one cpu, need disabling NUMA support and proceed.

Also better to release unused resources (the original code can not cause issue, only waste some memory).

---
 kernel/workqueue.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/workqueue.c +0 -0
index 1ae6028..a763079 100644

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

Quoted 6 lines
kernel/workqueue.c +3 -0
@@ -4911,6 +4911,9 @@ static void __init wq_numa_init(void)
 		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 */
+			for_each_node(node)
+				free_cpumask_var(tbl[node]);
+			kfree(tbl);

Eh... let's just leave it alone. The path is taken iff the whole thing is seriously broken and we're triggering WARN. I don't think we need to worry about small memory wastage there.

Thanks.

Signature · 1 line
tejun
author
#2 lore raw
Quoted 30 lines · Tejun Heo
Quoted 18 lines · Chen Gang

When NUMA node mapping not available for one cpu, need disabling NUMA support and proceed.

Also better to release unused resources (the original code can not cause issue, only waste some memory).

---
 kernel/workqueue.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/workqueue.c +0 -0
index 1ae6028..a763079 100644

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

Quoted 6 lines
kernel/workqueue.c +3 -0
@@ -4911,6 +4911,9 @@ static void __init wq_numa_init(void)
 		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 */
+			for_each_node(node)
+				free_cpumask_var(tbl[node]);
+			kfree(tbl);

Eh... let's just leave it alone. The path is taken iff the whole thing is seriously broken and we're triggering WARN. I don't think we need to worry about small memory wastage there.

Quoted 2 lines

Thanks.

OK, I can understand. I will continue to try to find and make another patches.

Thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#3 lore raw

PATCH 1/1 kernel/workqueue.c: need call device_remove_file() when failure occurs after called device_create_file()

In workqueue_sysfs_register(), when failure occurs after called device_create_file(), need call device_remove_file() to release the related resources, then call device_unregister().

Or it will cause issue.

For individual 'device_attributs' (just like our case), need call device_remove_file() explictly and then call device_unregister(), please reference drivers/base/*.c (e.g node.c or cpu.c).

---
 kernel/workqueue.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/workqueue.c +2 -0
index 1ae6028..de11dae 100644
@@ -3323,6 +3323,8 @@ int workqueue_sysfs_register(struct workqueue_struct *wq)
 		for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
 			ret = device_create_file(&wq_dev->dev, attr);
 			if (ret) {
+				while (--attr >= wq_sysfs_unbound_attrs)
+					device_remove_file(&wq_dev->dev, attr);
 				device_unregister(&wq_dev->dev);
 				wq->wq_dev = NULL;
 				return ret;
Signature · 1 line
1.7.7.6
maintainer
#4 lore raw

Re: [PATCH] kernel/workqueue.c: need call device_remove_file() when failure occurs after called device_create_file()

Quoted 10 lines · Chen Gang

In workqueue_sysfs_register(), when failure occurs after called device_create_file(), need call device_remove_file() to release the related resources, then call device_unregister().

Or it will cause issue.

For individual 'device_attributs' (just like our case), need call device_remove_file() explictly and then call device_unregister(), please reference drivers/base/*.c (e.g node.c or cpu.c).

Hmm... isn't this already taken care of by __sysfs_remove_dir() which device_unregister() calls? That function removes all non-directory files under the directory being removed.

Thanks.

Signature · 1 line
tejun
author
#5 lore raw

Re: [PATCH] kernel/workqueue.c: need call device_remove_file() when failure occurs after called device_create_file()

Quoted 8 lines · Tejun Heo
Quoted 7 lines · Chen Gang

In workqueue_sysfs_register(), when failure occurs after called device_create_file(), need call device_remove_file() to release the related resources, then call device_unregister().

Or it will cause issue.

Quoted 8 lines
Quoted 3 lines

For individual 'device_attributs' (just like our case), need call device_remove_file() explictly and then call device_unregister(), please reference drivers/base/*.c (e.g node.c or cpu.c).

Hmm... isn't this already taken care of by __sysfs_remove_dir() which device_unregister() calls? That function removes all non-directory files under the directory being removed.

It just like what you said:
device_unregister() ->

    kobject_del() ->
      sysfs_remove_dir() ->
        __sysfs_remove_dir() ->
          sysfs_remove_one()
But:
  device_remove_file() ->
    sysfs_remove_file() ->
      ...
      sysfs_attr_ns() ->
        ops->namespace()  (such as device_namespace() in workqueue.c)
      ...
      sysfs_hash_and_remove() ->
        sysfs_remove_one().

So if not call device_remove_file() explicitly, the device_namespace() may be not called.

Even in device_unregister(), it still call device_remove_file() to release the related attributes firstly, then call kobject_del().

Thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#6 lore raw

Re: [PATCH] kernel/workqueue.c: need call device_remove_file() when failure occurs after called device_create_file()

Quoted 34 lines · Chen Gang
Quoted 8 lines · Tejun Heo
Quoted 7 lines · Chen Gang

In workqueue_sysfs_register(), when failure occurs after called device_create_file(), need call device_remove_file() to release the related resources, then call device_unregister().

Or it will cause issue.

Quoted 8 lines
Quoted 3 lines

For individual 'device_attributs' (just like our case), need call device_remove_file() explictly and then call device_unregister(), please reference drivers/base/*.c (e.g node.c or cpu.c).

Hmm... isn't this already taken care of by __sysfs_remove_dir() which device_unregister() calls? That function removes all non-directory files under the directory being removed.

It just like what you said:
device_unregister() ->

    kobject_del() ->
      sysfs_remove_dir() ->
        __sysfs_remove_dir() ->
          sysfs_remove_one()
But:
  device_remove_file() ->
    sysfs_remove_file() ->
      ...
      sysfs_attr_ns() ->
        ops->namespace()  (such as device_namespace() in workqueue.c)
      ...
      sysfs_hash_and_remove() ->
        sysfs_remove_one().
Quoted 3 lines

So if not call device_remove_file() explicitly, the device_namespace() may be not called.

It seems, if not call ops->namespace(), it still is OK. To get a result, it still has much details to continue to read.

But all together, reference the related code of another subsystems, we really need device_remove_file() before call device_unregister().

Quoted 8 lines

Even in device_unregister(), it still call device_remove_file() to release the related attributes firstly, then call kobject_del().

Thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#7 lore raw

Re: [PATCH] kernel/workqueue.c: need call device_remove_file() when failure occurs after called device_create_file()

Quoted 14 lines · Chen Gang
Quoted 13 lines · Chen Gang
Quoted 12 lines · Tejun Heo
Quoted 10 lines · Chen Gang

In workqueue_sysfs_register(), when failure occurs after called device_create_file(), need call device_remove_file() to release the related resources, then call device_unregister().

Or it will cause issue.

For individual 'device_attributs' (just like our case), need call device_remove_file() explictly and then call device_unregister(), please reference drivers/base/*.c (e.g node.c or cpu.c).

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Hmm... isn't this already taken care of by __sysfs_remove_dir() which device_unregister() calls? That function removes all non-directory files under the directory being removed.

__sysfs_remove_dir() remove all related things, but not deref the count.

For __sysfs_remove_dir() ->
...
sysfs_addrm_start()
...
while() {
...
sysfs_remove_one() (not deref the related file)
}
sysfs_addrm_finish() (will deref current dir)

For device_remove_file() ->
sysfs_remove_file() ->

    sysfs_hash_and_remove() ->
      ...
      sysfs_addrm_start()
      ...
      sysfs_remove_one() (not deref the current file)
      sysfs_addrm_finish()  (will deref current file)

So if not call device_remove_file() firstly, the all files under the directory are removed, but the related resources are not released which will cause resource leak.

Thanks.

Quoted 37 lines
Quoted 16 lines

It just like what you said:
device_unregister() ->

    kobject_del() ->
      sysfs_remove_dir() ->
        __sysfs_remove_dir() ->
          sysfs_remove_one()
But:
  device_remove_file() ->
    sysfs_remove_file() ->
      ...
      sysfs_attr_ns() ->
        ops->namespace()  (such as device_namespace() in workqueue.c)
      ...
      sysfs_hash_and_remove() ->
        sysfs_remove_one().
Quoted 3 lines

So if not call device_remove_file() explicitly, the device_namespace() may be not called.

It seems, if not call ops->namespace(), it still is OK. To get a result, it still has much details to continue to read.

But all together, reference the related code of another subsystems, we really need device_remove_file() before call device_unregister().

Quoted 8 lines

Even in device_unregister(), it still call device_remove_file() to release the related attributes firstly, then call kobject_del().

Thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
maintainer
#8 lore raw

Re: [PATCH] kernel/workqueue.c: need call device_remove_file() when failure occurs after called device_create_file()

Hey,

Quoted 22 lines · Chen Gang

__sysfs_remove_dir() remove all related things, but not deref the count.

For __sysfs_remove_dir() ->
...
sysfs_addrm_start()
...
while() {
...
sysfs_remove_one() (not deref the related file)
}
sysfs_addrm_finish() (will deref current dir)

For device_remove_file() ->
sysfs_remove_file() ->

    sysfs_hash_and_remove() ->
      ...
      sysfs_addrm_start()
      ...
      sysfs_remove_one() (not deref the current file)
      sysfs_addrm_finish()  (will deref current file)
Quoted 3 lines

So if not call device_remove_file() firstly, the all files under the directory are removed, but the related resources are not released which will cause resource leak.

Can you please be more specific on what gets leaked and if possible fix it from sysfs side?

Thanks.

Signature · 1 line
tejun
author
#9 lore raw

Re: [PATCH] kernel/workqueue.c: need call device_remove_file() when failure occurs after called device_create_file()

Quoted 25 lines · Tejun Heo

Hey,

Quoted 22 lines · Chen Gang

__sysfs_remove_dir() remove all related things, but not deref the count.

For __sysfs_remove_dir() ->
...
sysfs_addrm_start()
...
while() {
...
sysfs_remove_one() (not deref the related file)
}
sysfs_addrm_finish() (will deref current dir)

For device_remove_file() ->
sysfs_remove_file() ->

    sysfs_hash_and_remove() ->
      ...
      sysfs_addrm_start()
      ...
      sysfs_remove_one() (not deref the current file)
      sysfs_addrm_finish()  (will deref current file)
Quoted 7 lines
Quoted 3 lines

So if not call device_remove_file() firstly, the all files under the directory are removed, but the related resources are not released which will cause resource leak.

Can you please be more specific on what gets leaked and if possible fix it from sysfs side?

Oh, it is my fault. It is not the correct reason.

But I will continue to analyse why it should be add device_remove_file() before device_unregister() in all 'drivers/*' and 'arch/*' source files.

Thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation