From patchwork Fri Sep 27 09:35:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 11164209 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3E8BE1747 for ; Fri, 27 Sep 2019 09:37:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1CE63207E0 for ; Fri, 27 Sep 2019 09:37:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nifty.com header.i=@nifty.com header.b="a/t82mTw" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726423AbfI0JhV (ORCPT ); Fri, 27 Sep 2019 05:37:21 -0400 Received: from conuserg-07.nifty.com ([210.131.2.74]:33057 "EHLO conuserg-07.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726438AbfI0JhC (ORCPT ); Fri, 27 Sep 2019 05:37:02 -0400 Received: from localhost.localdomain (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-07.nifty.com with ESMTP id x8R9a5ub001372; Fri, 27 Sep 2019 18:36:07 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-07.nifty.com x8R9a5ub001372 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1569576967; bh=vJzF8WlgUqc1RsR95UB9lwwYV0dr5aBFLgK7a4VpJl0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a/t82mTwbwWZPr0/tQv9F1qEmfxY+FFesX4KbzmGMTG21vQhlXMBP7rrvpG/Gvk6w EC+o8FZKVJbbAVEZoykh5cD2QqCDz/m+73YH1vmQ2Up3QMvK99h0chvD0XxdoYMafC uOsZH+QvFnmPwiHz1IRaSCJXFXASiKeDWhOTxiFDswTY2pRWlD9V3c//TfIpSFUCwr Rz0S84dpoQubEEROWm9wXT/T9ga6jnFTjaA5V4G6/pBsqw1cEZTCcCRkj3XXFk4KLL 2mhnuT7X3lne5Abip9GRwCQrTuntCf8So9MR7YqWsmfYyqDEW/17TOx+STvRxZF6TM Cy6NJ4OYl1HKw== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: Jessica Yu Cc: Matthias Maennich , Greg Kroah-Hartman , Joel Fernandes , Martijn Coenen , Will Deacon , Masahiro Yamada , Michal Marek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/7] modpost: fix broken sym->namespace for external module builds Date: Fri, 27 Sep 2019 18:35:57 +0900 Message-Id: <20190927093603.9140-2-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190927093603.9140-1-yamada.masahiro@socionext.com> References: <20190927093603.9140-1-yamada.masahiro@socionext.com> Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Currently, external module builds produce tons of false-positives: WARNING: module uses symbol from namespace , but does not import it. Here, the part shows a random string. When you build external modules, the symbol info of vmlinux and in-kernel modules are read from $(objtree)/Module.symvers, but read_dump() is buggy in multiple ways: [1] When the modpost is run for vmlinux and in-kernel modules, sym_extract_namespace() correctly allocates memory for the namespace. On the other hand, read_dump() does not, then sym->namespace will point to somewhere in the line buffer of get_next_line(). The data in the buffer will be replaced soon, and sym->namespace will end up with pointing to unrelated data. As a result, check_exports() will show random strings in the warning messages. [2] When there is no namespace, sym_extract_namespace() returns NULL. On the other hand, read_dump() sets namespace to an empty string "". (but, it will be later replaced with unrelated data due to bug [1].) The check_exports() shows a warning unless exp->namespace is NULL, so every symbol read from read_dump() emits the warning, which is mostly false positive. To address [1], I added NOFAIL(strdup(...)) to allocate memory. For [2], I changed the if-conditional in check_exports(). Signed-off-by: Masahiro Yamada Reviewed-by: Matthias Maennich --- scripts/mod/modpost.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 3961941e8e7a..5c628a7d80f7 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -2195,7 +2195,7 @@ static int check_exports(struct module *mod) else basename = mod->name; - if (exp->namespace) { + if (exp->namespace && exp->namespace[0]) { add_namespace(&mod->required_namespaces, exp->namespace); @@ -2453,7 +2453,7 @@ static void read_dump(const char *fname, unsigned int kernel) mod = new_module(modname); mod->skip = 1; } - s = sym_add_exported(symname, namespace, mod, + s = sym_add_exported(symname, NOFAIL(strdup(namespace)), mod, export_no(export)); s->kernel = kernel; s->preloaded = 1; From patchwork Fri Sep 27 09:35:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 11164207 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E4D8D924 for ; Fri, 27 Sep 2019 09:37:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C3781207E0 for ; Fri, 27 Sep 2019 09:37:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nifty.com header.i=@nifty.com header.b="2NU1Uj+G" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727085AbfI0JhN (ORCPT ); Fri, 27 Sep 2019 05:37:13 -0400 Received: from conuserg-07.nifty.com ([210.131.2.74]:33064 "EHLO conuserg-07.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726444AbfI0JhC (ORCPT ); Fri, 27 Sep 2019 05:37:02 -0400 Received: from localhost.localdomain (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-07.nifty.com with ESMTP id x8R9a5uc001372; Fri, 27 Sep 2019 18:36:08 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-07.nifty.com x8R9a5uc001372 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1569576968; bh=k0hBXKWoJkYYxdDyuOyeX7IT6hvCtlLoJTUWwkZpHoo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2NU1Uj+GZgATSlHQ/wpNiFGS13yTeurDYkQ5oWGWobbFSPBjB+S2L6Fw/LgnWkTAq hWIRQQ41fCSLUNCBf+HYJZpbU959wGqrEA72oxHXEOjKJMSWh0zUo3N/sSGmjzhqzA D32uDOKQCjHot8mwF7RjQ3ysW8A3joNSk4qE8q1zcFV/svc+eMf4VFKUt2JxAYJCKw rysk1hb7vz3OotkLSrzSX5ezVfEFv12Sr9ojoceyMo6VmsPBBWJhLrk4vNps7///3U yEBu+i2JGF6ZOMzg2oRMYSRJkvsMvIqDtV92F3sTt+tEvDeBMXe70++/0/ZmSO0rt4 ozmoh+j5CsHlw== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: Jessica Yu Cc: Matthias Maennich , Greg Kroah-Hartman , Joel Fernandes , Martijn Coenen , Will Deacon , Masahiro Yamada , Michal Marek , Will Deacon , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/7] module: swap the order of symbol.namespace Date: Fri, 27 Sep 2019 18:35:58 +0900 Message-Id: <20190927093603.9140-3-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190927093603.9140-1-yamada.masahiro@socionext.com> References: <20190927093603.9140-1-yamada.masahiro@socionext.com> Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Currently, EXPORT_SYMBOL_NS(_GPL) constructs the kernel symbol as follows: __ksymtab_SYMBOL.NAMESPACE The sym_extract_namespace() in modpost allocates memory for the part SYMBOL.NAMESPACE when '.' is contained. One problem is that the pointer returned by strdup() is lost because the symbol name will be copied to malloc'ed memory by alloc_symbol(). No one will keep track of the pointer of strdup'ed memory. sym->namespace still points to the NAMESPACE part. So, if you like, you can free it with complicated code like this: free(sym->namespace - strlen(sym->name) - 1); I would not say it is fatal since we seldom bother to manually free memory in host programs. But, I can fix it in an elegant way. I swapped the order of the symbol and the namespace as follows: __ksymtab_NAMESPACE.SYMBOL then, simplified sym_extract_namespace() so that it allocates memory only for the NAMESPACE part. I prefer this order because it is intuitive and also matches to major languages. For example, NAMESPACE::NAME in C++, MODULE.NAME in Python. Signed-off-by: Masahiro Yamada Reviewed-by: Matthias Maennich --- include/linux/export.h | 4 ++-- scripts/mod/modpost.c | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/include/linux/export.h b/include/linux/export.h index 95f55b7f83a0..0695d4e847d9 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -52,7 +52,7 @@ extern struct module __this_module; __ADDRESSABLE(sym) \ asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \ " .balign 4 \n" \ - "__ksymtab_" #sym NS_SEPARATOR #ns ": \n" \ + "__ksymtab_" #ns NS_SEPARATOR #sym ": \n" \ " .long " #sym "- . \n" \ " .long __kstrtab_" #sym "- . \n" \ " .long __kstrtab_ns_" #sym "- . \n" \ @@ -76,7 +76,7 @@ struct kernel_symbol { #else #define __KSYMTAB_ENTRY_NS(sym, sec, ns) \ static const struct kernel_symbol __ksymtab_##sym##__##ns \ - asm("__ksymtab_" #sym NS_SEPARATOR #ns) \ + asm("__ksymtab_" #ns NS_SEPARATOR #sym) \ __attribute__((section("___ksymtab" sec "+" #sym), used)) \ __aligned(sizeof(void *)) \ = { (unsigned long)&sym, __kstrtab_##sym, __kstrtab_ns_##sym } diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 5c628a7d80f7..d171b0cffb05 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -350,18 +350,16 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec) static const char *sym_extract_namespace(const char **symname) { - size_t n; - char *dupsymname; + char *namespace = NULL; + char *ns_separator; - n = strcspn(*symname, "."); - if (n < strlen(*symname) - 1) { - dupsymname = NOFAIL(strdup(*symname)); - dupsymname[n] = '\0'; - *symname = dupsymname; - return dupsymname + n + 1; + ns_separator = strchr(*symname, '.'); + if (ns_separator) { + namespace = NOFAIL(strndup(*symname, ns_separator - *symname)); + *symname = ns_separator + 1; } - return NULL; + return namespace; } /** From patchwork Fri Sep 27 09:36:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 11164205 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 33D95924 for ; Fri, 27 Sep 2019 09:37:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 07A5C21848 for ; Fri, 27 Sep 2019 09:37:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nifty.com header.i=@nifty.com header.b="EM8WpQlH" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726752AbfI0JhC (ORCPT ); Fri, 27 Sep 2019 05:37:02 -0400 Received: from conuserg-07.nifty.com ([210.131.2.74]:33065 "EHLO conuserg-07.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726473AbfI0JhC (ORCPT ); Fri, 27 Sep 2019 05:37:02 -0400 Received: from localhost.localdomain (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-07.nifty.com with ESMTP id x8R9a5uf001372; Fri, 27 Sep 2019 18:36:10 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-07.nifty.com x8R9a5uf001372 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1569576971; bh=PdteufK64PUrtistqDZxQfPkOhODuQM4/dZUBLfPgTA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EM8WpQlHl/RRg5lRspAhRnXuVP7NMRwN3vND8I4as8aaLPekCtI2sURqIXOQUzDqJ TJHa0dr1aSxHTVohjmVKh7oax8Y6aHX6vl0frGNzHY2Kajqt1EupmVDFm5uVTT36nI WTqmMnV47W4d6qBAKQZwm29A4ypO1YqbCZQRbrrvANW8PLu1Z4gsMkTGzAkFJ/v6aw HY/TlIH66PvW6R/IEv69y/UWP17UXOHEpE7VWJKN4aqBCdnNEQwkFNCXzd0B4zz7VG d5miMuXHwDAUjJBe6WWgZaWnkP07YEIXzgMDUvwQxkliu+OIVfQBRM26Bg370XpJOe 0pyYPUk423uwg== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: Jessica Yu Cc: Matthias Maennich , Greg Kroah-Hartman , Joel Fernandes , Martijn Coenen , Will Deacon , Masahiro Yamada , Michal Marek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/7] kbuild: fix build error of 'make nsdeps' in clean tree Date: Fri, 27 Sep 2019 18:36:01 +0900 Message-Id: <20190927093603.9140-6-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190927093603.9140-1-yamada.masahiro@socionext.com> References: <20190927093603.9140-1-yamada.masahiro@socionext.com> Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Running 'make nsdeps' in a clean source tree fails as follows: $ make -s clean; make -s defconfig; make nsdeps [ snip ] awk: fatal: cannot open file `init/modules.order' for reading (No such file or directory) make: *** [Makefile;1307: modules.order] Error 2 make: *** Deleting file 'modules.order' make: *** Waiting for unfinished jobs.... The cause of the error is 'make nsdeps' does not build modules at all. Set KBUILD_MODULES to fix it. Signed-off-by: Masahiro Yamada Reviewed-by: Matthias Maennich --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d456746da347..80ba8efd56bb 100644 --- a/Makefile +++ b/Makefile @@ -616,7 +616,7 @@ endif # in addition to whatever we do anyway. # Just "make" or "make all" shall build modules as well -ifneq ($(filter all _all modules,$(MAKECMDGOALS)),) +ifneq ($(filter all _all modules nsdeps,$(MAKECMDGOALS)),) KBUILD_MODULES := 1 endif