Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp8103281ybi; Tue, 9 Jul 2019 09:14:15 -0700 (PDT) X-Google-Smtp-Source: APXvYqwUT5VLCyMLWZu7bhFgtWpmTnct7g8+wxBngW5g2nfygIy35y4ppor2e7xLivpY6Twmq1t8 X-Received: by 2002:a17:902:e65:: with SMTP id 92mr31910052plw.13.1562688855347; Tue, 09 Jul 2019 09:14:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562688855; cv=none; d=google.com; s=arc-20160816; b=qIJo9VhLSiK14JRu78eH/q4bdDQ06TbwxtD3hFZYOmv1uuu+mx7lKMXDTvRLVDSpaQ 6RJDJODM0CNYwUMk8zqQfhtGD21T5EjjRXQJdu/oCWmQPhDUDNsi8LZooYCl+EJPnO/q /6o+634Hr9ikEszQGqZsNNl/s8KGSQSKLnYnRkAkYGwKUnICOMXxe1GPm9QxfueAto3o 0sbczYHhydJUL7jTAJ3a0OO5Q/4QYkjafPl8z52VDj5UxPKbQXABAFDOPMFxPu1pdiDZ nQeUcsnmqy7n8gwYVf4vRNf3+dds7jTD7cNxkuE7V/dp6SVg078aHKGrTFDfvJbCahrD zkiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=k0VDpNwtwSeIXQsiKRVGWjL0vsEhoHgJk1Y7+2+cwaI=; b=A0kkHyF/GpNU7i7DPv6KM4YOKDeJ96Jrhx91BrMTsP1Wc2V0G+tnqJn785QCmNJ6Ss vlz4JMP7hySy7mvJhE6L8nS4Lrzh1r4okovcMNj8IYCm+o+JLqiV0gHKlmZ1lH5qPI65 BxyI0R6KLVUoi/7azsvEznaHn5SwhN/bgb/0LPnPkvN/S5ANAX+90pfkTSW/bnZpVk7w oTPJKSfV6G0qTJf0kiB+bxkIu8kdpIWCVUoH5EFeUOAMVvVQcyF4z3rRoYrS99Uz8aqu iidp7RMxea9BhnunMgKSYImJl/qlwcXoicWdKax1NBYRJKpEo9XdpvrR9ZODGm6nW5fM Wu/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ar5tnU95; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y5si24693130pfq.181.2019.07.09.09.13.59; Tue, 09 Jul 2019 09:14:15 -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; dkim=pass header.i=@kernel.org header.s=default header.b=Ar5tnU95; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726636AbfGIQKq (ORCPT + 99 others); Tue, 9 Jul 2019 12:10:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:46494 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726060AbfGIQKq (ORCPT ); Tue, 9 Jul 2019 12:10:46 -0400 Received: from linux-8ccs (ip5f5adbef.dynamic.kabel-deutschland.de [95.90.219.239]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 67EB720656; Tue, 9 Jul 2019 16:10:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562688645; bh=sZeMD4oVQwPrdMKxyv7QW1fVkzIcKk8DIBHqtvwcWPs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Ar5tnU95snb3l/W5KYlNQP9YQ1KcXM5zwJ7JQjzNWOjLwiCytDHRWgQyjFt0Oi0GP IF+CW2oXBDXZem/WACI2CXhMNKEaXo3h4WE9cSLKzxIpjD3daJ+RWmC4r7aqgFuYkn T7jI/n2nFLhON2jr2Ygkg+lAcr+VSrOnAKF19Nwc= Date: Tue, 9 Jul 2019 18:10:39 +0200 From: Jessica Yu To: Zhiqiang Liu Cc: rusty@rustcorp.com.au, kay.sievers@vrfy.org, clabbe.montjoie@gmail.com, LKML , wangxiaogang3@huawei.com, zhoukang7@huawei.com, Mingfangsen Subject: Re: [PATCH v2] module: add usage links when calling ref_module func Message-ID: <20190709161039.GA3501@linux-8ccs> References: <4fec6c3b-03b8-dd57-4009-99431105a8a5@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <4fec6c3b-03b8-dd57-4009-99431105a8a5@huawei.com> X-OS: Linux linux-8ccs 4.12.14-lp150.12.28-default x86_64 User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org +++ Zhiqiang Liu [03/07/19 10:09 +0800]: >From: Zhiqiang Liu > >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. > >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 >Suggested-by: Jessica Yu >Reviewed-by: Kang Zhou >--- > 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 >