linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] module: add usage links when calling ref_module func
@ 2019-07-03  2:09 Zhiqiang Liu
  2019-07-06  6:17 ` Zhiqiang Liu
  2019-07-09 16:10 ` Jessica Yu
  0 siblings, 2 replies; 5+ messages in thread
From: Zhiqiang Liu @ 2019-07-03  2:09 UTC (permalink / raw)
  To: Jessica Yu, rusty, kay.sievers, clabbe.montjoie
  Cc: LKML, wangxiaogang3, zhoukang7, Mingfangsen

From: Zhiqiang Liu <liuzhiqiang26@huawei.com>

Users can call ref_module func in their modules to construct
relationships with other modules. However, the holders
'/sys/module/<mod-name>/holders' of the target module donot include
the users` module. So lsmod command misses detailed info of 'Used by'.

When load module, the process is given as follows,
load_module()
	-> mod_sysfs_setup()
		-> add_usage_links
	-> do_init_module
		-> mod->init()

add_usage_links func creates holders of target modules linking to
this module. If ref_module is called in mod->init() func, the usage
links cannot be added.

Here, we will add usage link of a to b's holder_dir.

V1->V2:
- remove incorrect Fixes tag
- fix error handling of sysfs_create_link as suggested by Jessica Yu

Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Suggested-by: Jessica Yu <jeyu@kernel.org>
Reviewed-by: Kang Zhou <zhoukang7@huawei.com>
---
 kernel/module.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/kernel/module.c b/kernel/module.c
index 80c7c09584cf..672abce2222c 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -837,25 +837,26 @@ static int already_uses(struct module *a, struct module *b)
  *    'b' can walk the list to see who sourced them), and of 'a'
  *    targets (so 'a' can see what modules it targets).
  */
-static int add_module_usage(struct module *a, struct module *b)
+static struct module_use *add_module_usage(struct module *a, struct module *b)
 {
 	struct module_use *use;

 	pr_debug("Allocating new usage for %s.\n", a->name);
 	use = kmalloc(sizeof(*use), GFP_ATOMIC);
 	if (!use)
-		return -ENOMEM;
+		return NULL;

 	use->source = a;
 	use->target = b;
 	list_add(&use->source_list, &b->source_list);
 	list_add(&use->target_list, &a->target_list);
-	return 0;
+	return use;
 }

 /* Module a uses b: caller needs module_mutex() */
 int ref_module(struct module *a, struct module *b)
 {
+	struct module_use *use;
 	int err;

 	if (b == NULL || already_uses(a, b))
@@ -866,9 +867,18 @@ int ref_module(struct module *a, struct module *b)
 	if (err)
 		return err;

-	err = add_module_usage(a, b);
+	use = add_module_usage(a, b);
+	if (!use) {
+		module_put(b);
+		return -ENOMEM;
+	}
+
+	err = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
 	if (err) {
 		module_put(b);
+		list_del(&use->source_list);
+		list_del(&use->target_list);
+		kfree(use);
 		return err;
 	}
 	return 0;
-- 
2.19.1


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

* Re: [PATCH v2] module: add usage links when calling ref_module func
  2019-07-03  2:09 [PATCH v2] module: add usage links when calling ref_module func Zhiqiang Liu
@ 2019-07-06  6:17 ` Zhiqiang Liu
  2019-07-09 16:10 ` Jessica Yu
  1 sibling, 0 replies; 5+ messages in thread
From: Zhiqiang Liu @ 2019-07-06  6:17 UTC (permalink / raw)
  To: Jessica Yu, rusty, kay.sievers, clabbe.montjoie
  Cc: LKML, wangxiaogang3, zhoukang7, Mingfangsen

Friendly ping ...

On 2019/7/3 10:09, Zhiqiang Liu wrote:
> From: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> 
> Users can call ref_module func in their modules to construct
> relationships with other modules. However, the holders
> '/sys/module/<mod-name>/holders' of the target module donot include
> the users` module. So lsmod command misses detailed info of 'Used by'.
> 
> When load module, the process is given as follows,
> load_module()
> 	-> mod_sysfs_setup()
> 		-> add_usage_links
> 	-> do_init_module
> 		-> mod->init()
> 
> add_usage_links func creates holders of target modules linking to
> this module. If ref_module is called in mod->init() func, the usage
> links cannot be added.
> 
> Here, we will add usage link of a to b's holder_dir.
> 
> V1->V2:
> - remove incorrect Fixes tag
> - fix error handling of sysfs_create_link as suggested by Jessica Yu
> 
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> Suggested-by: Jessica Yu <jeyu@kernel.org>
> Reviewed-by: Kang Zhou <zhoukang7@huawei.com>
> ---
>  kernel/module.c | 18 ++++++++++++++----
>  1 file changed, 14 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/module.c b/kernel/module.c
> index 80c7c09584cf..672abce2222c 100644
> --- a/kernel/module.c
> +++ b/kernel/module.c
> @@ -837,25 +837,26 @@ static int already_uses(struct module *a, struct module *b)
>   *    'b' can walk the list to see who sourced them), and of 'a'
>   *    targets (so 'a' can see what modules it targets).
>   */
> -static int add_module_usage(struct module *a, struct module *b)
> +static struct module_use *add_module_usage(struct module *a, struct module *b)
>  {
>  	struct module_use *use;
> 
>  	pr_debug("Allocating new usage for %s.\n", a->name);
>  	use = kmalloc(sizeof(*use), GFP_ATOMIC);
>  	if (!use)
> -		return -ENOMEM;
> +		return NULL;
> 
>  	use->source = a;
>  	use->target = b;
>  	list_add(&use->source_list, &b->source_list);
>  	list_add(&use->target_list, &a->target_list);
> -	return 0;
> +	return use;
>  }
> 
>  /* Module a uses b: caller needs module_mutex() */
>  int ref_module(struct module *a, struct module *b)
>  {
> +	struct module_use *use;
>  	int err;
> 
>  	if (b == NULL || already_uses(a, b))
> @@ -866,9 +867,18 @@ int ref_module(struct module *a, struct module *b)
>  	if (err)
>  		return err;
> 
> -	err = add_module_usage(a, b);
> +	use = add_module_usage(a, b);
> +	if (!use) {
> +		module_put(b);
> +		return -ENOMEM;
> +	}
> +
> +	err = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
>  	if (err) {
>  		module_put(b);
> +		list_del(&use->source_list);
> +		list_del(&use->target_list);
> +		kfree(use);
>  		return err;
>  	}
>  	return 0;
> 


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

* Re: [PATCH v2] module: add usage links when calling ref_module func
  2019-07-03  2:09 [PATCH v2] module: add usage links when calling ref_module func Zhiqiang Liu
  2019-07-06  6:17 ` Zhiqiang Liu
@ 2019-07-09 16:10 ` Jessica Yu
  2019-07-11  6:03   ` Zhiqiang Liu
  1 sibling, 1 reply; 5+ messages in thread
From: Jessica Yu @ 2019-07-09 16:10 UTC (permalink / raw)
  To: Zhiqiang Liu
  Cc: rusty, kay.sievers, clabbe.montjoie, LKML, wangxiaogang3,
	zhoukang7, Mingfangsen

+++ Zhiqiang Liu [03/07/19 10:09 +0800]:
>From: Zhiqiang Liu <liuzhiqiang26@huawei.com>
>
>Users can call ref_module func in their modules to construct
>relationships with other modules. However, the holders
>'/sys/module/<mod-name>/holders' of the target module donot include
>the users` module. So lsmod command misses detailed info of 'Used by'.
>
>When load module, the process is given as follows,
>load_module()
>	-> mod_sysfs_setup()
>		-> add_usage_links
>	-> do_init_module
>		-> mod->init()
>
>add_usage_links func creates holders of target modules linking to
>this module. If ref_module is called in mod->init() func, the usage
>links cannot be added.
>
>Here, we will add usage link of a to b's holder_dir.
>
>V1->V2:
>- remove incorrect Fixes tag
>- fix error handling of sysfs_create_link as suggested by Jessica Yu
>
>Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
>Suggested-by: Jessica Yu <jeyu@kernel.org>
>Reviewed-by: Kang Zhou <zhoukang7@huawei.com>
>---
> kernel/module.c | 18 ++++++++++++++----
> 1 file changed, 14 insertions(+), 4 deletions(-)
>
>diff --git a/kernel/module.c b/kernel/module.c
>index 80c7c09584cf..672abce2222c 100644
>--- a/kernel/module.c
>+++ b/kernel/module.c
>@@ -837,25 +837,26 @@ static int already_uses(struct module *a, struct module *b)
>  *    'b' can walk the list to see who sourced them), and of 'a'
>  *    targets (so 'a' can see what modules it targets).
>  */
>-static int add_module_usage(struct module *a, struct module *b)
>+static struct module_use *add_module_usage(struct module *a, struct module *b)
> {
> 	struct module_use *use;
>
> 	pr_debug("Allocating new usage for %s.\n", a->name);
> 	use = kmalloc(sizeof(*use), GFP_ATOMIC);
> 	if (!use)
>-		return -ENOMEM;
>+		return NULL;
>
> 	use->source = a;
> 	use->target = b;
> 	list_add(&use->source_list, &b->source_list);
> 	list_add(&use->target_list, &a->target_list);
>-	return 0;
>+	return use;
> }
>
> /* Module a uses b: caller needs module_mutex() */
> int ref_module(struct module *a, struct module *b)
> {
>+	struct module_use *use;
> 	int err;
>
> 	if (b == NULL || already_uses(a, b))
>@@ -866,9 +867,18 @@ int ref_module(struct module *a, struct module *b)
> 	if (err)
> 		return err;
>
>-	err = add_module_usage(a, b);
>+	use = add_module_usage(a, b);
>+	if (!use) {
>+		module_put(b);
>+		return -ENOMEM;
>+	}
>+
>+	err = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);

Sigh. This ultimately doesn't work because in load_module(), we use
ref_module() in resolve_symbol(), and mod->mkobj.kobj doesn't get
initialized until mod_sysfs_init(), which happens much later in
load_module(). So what happens is that the ref_module(mod, owner) call
in resolve_symbol() returns an error because sysfs_create_link() fails here.
We could *maybe* move sysfs initialization earlier in load_module()
but that is an entirely untested idea and I would need to think about
that more.

> 	if (err) {
> 		module_put(b);
>+		list_del(&use->source_list);
>+		list_del(&use->target_list);
>+		kfree(use);
> 		return err;
> 	}
> 	return 0;
>-- 
>2.19.1
>

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

* Re: [PATCH v2] module: add usage links when calling ref_module func
  2019-07-09 16:10 ` Jessica Yu
@ 2019-07-11  6:03   ` Zhiqiang Liu
  2019-07-11  9:17     ` Jessica Yu
  0 siblings, 1 reply; 5+ messages in thread
From: Zhiqiang Liu @ 2019-07-11  6:03 UTC (permalink / raw)
  To: Jessica Yu
  Cc: rusty, kay.sievers, clabbe.montjoie, LKML, wangxiaogang3,
	zhoukang7, Mingfangsen

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="gb18030", Size: 2316 bytes --]



On 2019/7/10 0:10, Jessica Yu wrote:
> +++ Zhiqiang Liu [03/07/19 10:09 +0800]:
>> From: Zhiqiang Liu <liuzhiqiang26@huawei.com
>>
>> V1->V2:
>> - remove incorrect Fixes tag
>> - fix error handling of sysfs_create_link as suggested by Jessica Yu
>>
>> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
>> Suggested-by: Jessica Yu <jeyu@kernel.org>
>> Reviewed-by: Kang Zhou <zhoukang7@huawei.com>
>> ---
>> kernel/module.c | 18 ++++++++++++++----
>> 1 file changed, 14 insertions(+), 4 deletions(-)
>>
>> diff --git a/kernel/module.c b/kernel/module.c
>> index 80c7c09584cf..672abce2222c 100644
>> --- a/kernel/module.c
>> +++ b/kernel/module.c
>> @@ -837,25 +837,26 @@ static int already_uses(struct module *a, struct module *b)
>> 0„2*0„20„20„2 'b' can walk the list to see who sourced them), and of 'a'
>> 0„2*0„20„20„2 targets (so 'a' can see what modules it targets).
>> 0„2*/
>> /* Module a uses b: caller needs module_mutex() */
>> int ref_module(struct module *a, struct module *b)
>> {
>> +0„20„20„2 struct module_use *use;
>> 0„20„20„20„2int err;
>>
>> 0„20„20„20„2if (b == NULL || already_uses(a, b))
>> @@ -866,9 +867,18 @@ int ref_module(struct module *a, struct module *b)
>> 0„20„20„20„2if (err)
>> 0„20„20„20„20„20„20„2 return err;
>>
>> -0„20„20„2 err = add_module_usage(a, b);
>> +0„20„20„2 use = add_module_usage(a, b);
>> +0„20„20„2 if (!use) {
>> +0„20„20„20„20„20„20„2 module_put(b);
>> +0„20„20„20„20„20„20„2 return -ENOMEM;
>> +0„20„20„2 }
>> +
>> +0„20„20„2 err = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
> 
> Sigh. This ultimately doesn't work because in load_module(), we use
> ref_module() in resolve_symbol(), and mod->mkobj.kobj doesn't get
> initialized until mod_sysfs_init(), which happens much later in
> load_module(). So what happens is that the ref_module(mod, owner) call
> in resolve_symbol() returns an error because sysfs_create_link() fails here.
> We could *maybe* move sysfs initialization earlier in load_module()
> but that is an entirely untested idea and I would need to think about
> that more.

Thank you for the reply.
I have tested the patch through livepatch. Maybe I miss somethings.
I will rewrite the patch and test it entirely before sending the v3 patch.

Thanks again.



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

* Re: [PATCH v2] module: add usage links when calling ref_module func
  2019-07-11  6:03   ` Zhiqiang Liu
@ 2019-07-11  9:17     ` Jessica Yu
  0 siblings, 0 replies; 5+ messages in thread
From: Jessica Yu @ 2019-07-11  9:17 UTC (permalink / raw)
  To: Zhiqiang Liu
  Cc: rusty, kay.sievers, clabbe.montjoie, LKML, wangxiaogang3,
	zhoukang7, Mingfangsen

+++ Zhiqiang Liu [11/07/19 14:03 +0800]:
>
>
>On 2019/7/10 0:10, Jessica Yu wrote:
>> +++ Zhiqiang Liu [03/07/19 10:09 +0800]:
>>> From: Zhiqiang Liu <liuzhiqiang26@huawei.com
>>>
>>> V1->V2:
>>> - remove incorrect Fixes tag
>>> - fix error handling of sysfs_create_link as suggested by Jessica Yu
>>>
>>> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
>>> Suggested-by: Jessica Yu <jeyu@kernel.org>
>>> Reviewed-by: Kang Zhou <zhoukang7@huawei.com>
>>> ---
>>> kernel/module.c | 18 ++++++++++++++----
>>> 1 file changed, 14 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/kernel/module.c b/kernel/module.c
>>> index 80c7c09584cf..672abce2222c 100644
>>> --- a/kernel/module.c
>>> +++ b/kernel/module.c
>>> @@ -837,25 +837,26 @@ static int already_uses(struct module *a, struct module *b)
>>>  *    'b' can walk the list to see who sourced them), and of 'a'
>>>  *    targets (so 'a' can see what modules it targets).
>>>  */
>>> /* Module a uses b: caller needs module_mutex() */
>>> int ref_module(struct module *a, struct module *b)
>>> {
>>> +    struct module_use *use;
>>>     int err;
>>>
>>>     if (b == NULL || already_uses(a, b))
>>> @@ -866,9 +867,18 @@ int ref_module(struct module *a, struct module *b)
>>>     if (err)
>>>         return err;
>>>
>>> -    err = add_module_usage(a, b);
>>> +    use = add_module_usage(a, b);
>>> +    if (!use) {
>>> +        module_put(b);
>>> +        return -ENOMEM;
>>> +    }
>>> +
>>> +    err = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
>>
>> Sigh. This ultimately doesn't work because in load_module(), we use
>> ref_module() in resolve_symbol(), and mod->mkobj.kobj doesn't get
>> initialized until mod_sysfs_init(), which happens much later in
>> load_module(). So what happens is that the ref_module(mod, owner) call
>> in resolve_symbol() returns an error because sysfs_create_link() fails here.
>> We could *maybe* move sysfs initialization earlier in load_module()
>> but that is an entirely untested idea and I would need to think about
>> that more.
>
>Thank you for the reply.
>I have tested the patch through livepatch. Maybe I miss somethings.
>I will rewrite the patch and test it entirely before sending the v3 patch.

Thanks Zhiqiang. A boot test and testing loading modules with
dependencies would be really appreciated.

Thanks!

Jessica

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

end of thread, other threads:[~2019-07-11  9:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-03  2:09 [PATCH v2] module: add usage links when calling ref_module func Zhiqiang Liu
2019-07-06  6:17 ` Zhiqiang Liu
2019-07-09 16:10 ` Jessica Yu
2019-07-11  6:03   ` Zhiqiang Liu
2019-07-11  9:17     ` Jessica Yu

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