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

From: Chen Gang
Date: Thu May 16 2013 - 00:49:43 EST


On 05/16/2013 12:16 PM, Chen Gang wrote:
> On 05/16/2013 11:33 AM, Chen Gang wrote:
>> On 05/16/2013 05:22 AM, Tejun Heo wrote:
>>> On Wed, May 15, 2013 at 02:13:14PM +0800, Chen Gang wrote:
>>>>
>>>> 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.
>>>
>>

__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.

>> 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.
>>
>
> 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().
>
>
>>
>> Even in device_unregister(), it still call device_remove_file() to
>> release the related attributes firstly, then call kobject_del().
>>
>>
>>
>> Thanks.
>>
>
>


--
Chen Gang

Asianux Corporation
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/