Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp348420ybe; Tue, 10 Sep 2019 20:20:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqz1VOIrux5nlPAoJhfUS56ABlB5sb+Lz0XBEL6q/0MjnVu8j9A/8HRfaqTEDg/XUHKNGbMb X-Received: by 2002:a17:906:583:: with SMTP id 3mr27171822ejn.74.1568172034064; Tue, 10 Sep 2019 20:20:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568172034; cv=none; d=google.com; s=arc-20160816; b=sTbGonRVF0r8hT8a09dDwmG46VygJXGwyK+khh8oWQk/ltaSsRQtjqLx+n/liMbPul Nx3POROMehTJTgLiUF4wtm+jA2ejNYH/pfQvX9l/KLVDflXu5vm4A+Dgu5KzCJnq4ZUO wXS+R8gBjBKZ1noXeIDtLYHisdk3MWsglK+wO4kBbV7gjRHkVvO7h1YzGvVvxqHAnvjn DrHoFTDQc5x6GIXik55p8u5nrkq1AG3oqgjPlSYo9etWv3NBrF0HVOif0CQ6HVvRvEz3 ZQXZ1raWFpMzconcsL4EJFdBcznI8VvypnM9/gSkfXvuLcAGEcWOsMRO+KWPSqLeqfCP exeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:references:cc:to:from :subject; bh=ddZD0AqIEokwVI+K3dt6asadg1h8DBOny/uQD9tzFXs=; b=05LKxWsvmz1iinhNPsT50Xz5wkc0vq8g5cDcBavrV6Oaf5TIeJ98PgLbPwF8Etz1i5 vQnlq7lCmReW1aoSzIJYStnbpCucm8wS0O49cmACFbA2+pHw+PiEf8yHRLcfCV1StMgF I3E8i7pmVymULbyTiKtLWAdEQccvATBVwXQVdJTENhWziH5W8r0dLVHSs+ejtUQ+rAcg R/wdUZwJYt5/SOeMZlR10K8SL+kZRZZEGDSoVzAkw2woAJVdq5gjNViPuwuppEJYoWUV w7jTt+oqhl/r53jrpV+954QsklwoWKqD4GJixcQJfWwa7C6IbpGDXczhK8qlNgGOxF8e H/7g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b35si11254541edb.204.2019.09.10.20.20.09; Tue, 10 Sep 2019 20:20:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726598AbfIKDR0 (ORCPT + 99 others); Tue, 10 Sep 2019 23:17:26 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:40516 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726341AbfIKDR0 (ORCPT ); Tue, 10 Sep 2019 23:17:26 -0400 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 751608BE641628B3B9F7; Wed, 11 Sep 2019 11:17:24 +0800 (CST) Received: from [127.0.0.1] (10.184.225.177) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.439.0; Wed, 11 Sep 2019 11:17:16 +0800 Subject: Re: [PATCH v3] module: add link_flag pram in ref_module func to decide whether add usage link From: Zhiqiang Liu To: Jessica Yu , , , CC: LKML , , , Mingfangsen References: <48019b31-3a74-1821-8ad3-72c9ef1219ba@huawei.com> Message-ID: Date: Wed, 11 Sep 2019 11:16:09 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0 MIME-Version: 1.0 In-Reply-To: <48019b31-3a74-1821-8ad3-72c9ef1219ba@huawei.com> Content-Type: text/plain; charset="gb18030" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.184.225.177] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Friendly Ping... On 2019/7/20 22:40, Zhiqiang Liu wrote: > Users can call ref_module func in their modules to construct > relationships with other modules. However, the holders > '/sys/module//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. > > Consider that add_module_usage and add usage_link may separate, the > link_flag pram is added in ref_module func to decide whether add usage > link after add_module_usage. If link_flag is true, it means usage link > of a to b's holder_dir should be created immediately after add_module_usage. > > V2->V3: > - add link_flag pram in ref_module func to decide whether add usage link > > V1->V2: > - remove incorrect Fixes tag > - fix error handling of sysfs_create_link as suggested by Jessica Yu > > Signed-off-by: Zhiqiang Liu > Suggested-by: Jessica Yu > Reviewed-by: Kang Zhou > --- > include/linux/module.h | 2 +- > kernel/module.c | 27 ++++++++++++++++++++------- > 2 files changed, 21 insertions(+), 8 deletions(-) > > diff --git a/include/linux/module.h b/include/linux/module.h > index 188998d3dca9..9ec04b9e93e8 100644 > --- a/include/linux/module.h > +++ b/include/linux/module.h > @@ -632,7 +632,7 @@ static inline void __module_get(struct module *module) > #define symbol_put_addr(p) do { } while (0) > > #endif /* CONFIG_MODULE_UNLOAD */ > -int ref_module(struct module *a, struct module *b); > +int ref_module(struct module *a, struct module *b, bool link_flag); > > /* This is a #define so the string doesn't get put in every .o file */ > #define module_name(mod) \ > diff --git a/kernel/module.c b/kernel/module.c > index 80c7c09584cf..00e4862a8ef7 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) > +int ref_module(struct module *a, struct module *b, bool link_flag) > { > + struct module_use *use; > int err; > > if (b == NULL || already_uses(a, b)) > @@ -866,9 +867,21 @@ 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; > + } > + > + if (!link_flag) > + return 0; > + > + 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; > @@ -1152,7 +1165,7 @@ static inline void module_unload_free(struct module *mod) > { > } > > -int ref_module(struct module *a, struct module *b) > +int ref_module(struct module *a, struct module *b, bool link_flag) > { > return strong_try_module_get(b); > } > @@ -1407,7 +1420,7 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod, > goto getname; > } > > - err = ref_module(mod, owner); > + err = ref_module(mod, owner, false); > if (err) { > sym = ERR_PTR(err); > goto getname; >