Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2271942imm; Mon, 16 Jul 2018 05:23:05 -0700 (PDT) X-Google-Smtp-Source: AAOMgpedn2/3Qys8KRpB5kYb6ssrefbeWRe9hfX6J1wTWBTiIAW7kEyO3sziAIHP73VohGg3tl85 X-Received: by 2002:a62:5290:: with SMTP id g138-v6mr17934815pfb.46.1531743785421; Mon, 16 Jul 2018 05:23:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531743785; cv=none; d=google.com; s=arc-20160816; b=y2LqvyO5CQyB0Y6eRoMRL+7zdikIHaBhwlNiJpoy2A87h3QZuNo6SrZdHJWGvLzEpm T+h2Swehfv39DZnkqlVkraDYLlxU1l8LiaJXniBTMqFdh9Heu5fO8mwEcaajc6lpbZPG ETqcMASDxja+g0UQaqsrcJiU5NYkAiYnRX4e/zpnVtCEpWssgWf+xC0sKbE4xP/m6W1x LDtin/t+ewAPhFHyG69JHCV36ZHlwTDkyoMwGKpRGXjjIz1AzSegDrY8wH4wFkREYwCz jGsW12sqvXxYAx/YgH2fmG8eLH6iwYKKG9yeZnFtqe0CmzxHHhxQOgYhQPCJqupzgXCe Lbqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=uMKHrpNNhtX0cvVMIjiPuRB/bNICYt0G5I9g8+sP+nE=; b=nYk1iMSFNXgH9M9Y3THFP9+nfTAh+X4QK1Hmz4nY2h+19NZUlllmQiBumkh4+Rb9F7 7SaFAoyhGu8r0Tr34ESf4LaTSE/FQVfi0/5TVkgoVDpavwA6oayauQQOrn3mfkg8O6Nh As5dR5of7H0wcnsFkagDaZ1U4Al1lZlxiu+YwZis55VNdQLriDMD6XTveveMp133D2XR D5jSDdQrFGRj4PpuGclnhMjhcuZiz7HFgBICIfiW+A/j539pA/32kNFX6Gdv3NJfsPSv oGzxojrMPjMYWAu3tqsaKxdQupIvwVFMZWUIm5bBSqW8N2NNwlmkSlEzuUK8OAhOrpCx 4qYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=aNQFipmi; 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=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o2-v6si29597569pgm.288.2018.07.16.05.22.50; Mon, 16 Jul 2018 05:23:05 -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=@android.com header.s=20161025 header.b=aNQFipmi; 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=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731203AbeGPMsu (ORCPT + 99 others); Mon, 16 Jul 2018 08:48:50 -0400 Received: from mail-ed1-f44.google.com ([209.85.208.44]:41683 "EHLO mail-ed1-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731093AbeGPMsu (ORCPT ); Mon, 16 Jul 2018 08:48:50 -0400 Received: by mail-ed1-f44.google.com with SMTP id s24-v6so13612528edr.8 for ; Mon, 16 Jul 2018 05:21:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=uMKHrpNNhtX0cvVMIjiPuRB/bNICYt0G5I9g8+sP+nE=; b=aNQFipmi4W+YFASqmc2Msq0FamvrT7dN0ntsO7ETyikVz8z/hwEyg7uLk4awX4Kmk5 cSr0QdVT3FZkhlFktid85Hh4sdcjzG5SY0sScJIWYd+8Fquv4LzjpNTZTfJVDLFDC6l0 LI1vIXfrFvQGJ7bhrNcmjRjc09jBGyUsCPQMG8AH3l/g1IABE0ogVxAt+1+PTQvSZnh7 Amkf3ei0RczAsgK7d/nK+qFqnO5R+H2QLeA+sKYlnvr6LRAlEaGBxRXdeDH0If9/j1yA q3jAQkj6GfqidOFDDega9bbRPmfXU5ZCo37EHqd4SEYtSWBx96OiuvAyOqvMnXNJC+qJ cTSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=uMKHrpNNhtX0cvVMIjiPuRB/bNICYt0G5I9g8+sP+nE=; b=dq8igN8r5Go6UrYDOWzJC1EeWTzIM/wpVsMs+4RvMpxE0/IPTn7ISXoLZ+UPDCtx40 BK/NXxjNFdanpJpEJscPLSPIItwQiL2+1IyxzXvSCVpDCzEnS9IwUlhdc9Xz7orMc6x0 GWOQFE3SqO3URzZNUavdQA4mOntxRf5s9FZhrh0FVdCkEEl9UPD01kcrSVFFNQngniqo kz/izS98GP8/6bTZw55TO9YLeY1/udx7EbP80eUCWsJ1WD+VaWQY72/FavIe16nyPcoy oIgccx/3wPvu2OJIJAqhG0VGE/bk/BAMVVHYxXYZoNN8jDF7ZuqwjnvNGlfyKxtZoFn4 Nmzw== X-Gm-Message-State: AOUpUlFA0VJCU4HaVOvb6hQvLixhsNBBkSil2OCAbqoJf6VP+1kkxBjk 5xwLe/jGx2zXhZTmFuCYk995a1/PnNM= X-Received: by 2002:aa7:d7d0:: with SMTP id e16-v6mr16457372eds.82.1531743696632; Mon, 16 Jul 2018 05:21:36 -0700 (PDT) Received: from maco.fcp3.net (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id a9-v6sm4635471edi.26.2018.07.16.05.21.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Jul 2018 05:21:35 -0700 (PDT) From: Martijn Coenen To: linux-kernel@vger.kernel.org Cc: Martijn Coenen , Masahiro Yamada , Michal Marek , Geert Uytterhoeven , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Alan Stern , Greg Kroah-Hartman , Oliver Neukum , Arnd Bergmann , Jessica Yu , Stephen Boyd , Philippe Ombredanne , Kate Stewart , Sam Ravnborg , linux-kbuild@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-usb@vger.kernel.org, usb-storage@lists.one-eyed-alien.net, linux-scsi@vger.kernel.org, linux-arch@vger.kernel.org, maco@google.com, sspatil@google.com, malchev@google.com, joelaf@google.com Subject: [PATCH 3/6] modpost: add support for checking symbol namespaces. Date: Mon, 16 Jul 2018 14:21:22 +0200 Message-Id: <20180716122125.175792-4-maco@android.com> X-Mailer: git-send-email 2.18.0.203.gfac676dfb9-goog In-Reply-To: <20180716122125.175792-1-maco@android.com> References: <20180716122125.175792-1-maco@android.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Emits a warning whenever a module refers to an exported symbol without explicitly importing the namespace that it is defined in. Example: WARNING: module ums-usbat uses symbol usb_stor_resume from namespace USB_STORAGE_NS, but does not import it. Signed-off-by: Martijn Coenen --- scripts/mod/modpost.c | 70 +++++++++++++++++++++++++++++++++++++++---- scripts/mod/modpost.h | 7 +++++ 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 1663fb19343a..a56a8461a96a 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -165,6 +165,7 @@ struct symbol { struct module *module; unsigned int crc; int crc_valid; + const char *ns; /* namespace */ unsigned int weak:1; unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ unsigned int kernel:1; /* 1 if symbol is from kernel @@ -234,6 +235,35 @@ static struct symbol *find_symbol(const char *name) return NULL; } +static bool contains_namespace(struct namespace_list *list, const char *ns) +{ + struct namespace_list *ns_entry; + + for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next) { + if (strcmp(ns_entry->namespace, ns) == 0) + return true; + } + + return false; +} + +static void add_namespace(struct namespace_list **list, const char *ns) +{ + struct namespace_list *ns_entry; + + if (!contains_namespace(*list, ns)) { + ns_entry = NOFAIL(malloc(sizeof(struct namespace_list))); + strcpy(ns_entry->namespace, ns); + ns_entry->next = *list; + *list = ns_entry; + } +} + +static bool module_imports_namespace(struct module *module, const char *ns) +{ + return contains_namespace(module->imported_namespaces, ns); +} + static const struct { const char *str; enum export export; @@ -313,21 +343,40 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec) return export_unknown; } +static const char *sym_extract_ns(const char **symname) +{ + size_t n; + + n = strcspn(*symname, "."); + if (n < strlen(*symname) - 1) { + char *dupsymname = NOFAIL(strdup(*symname)); + + dupsymname[n] = '\0'; + *symname = dupsymname; + return dupsymname + n + 1; + } else { + return NULL; + } +} + /** * Add an exported symbol - it may have already been added without a * CRC, in this case just update the CRC **/ -static struct symbol *sym_add_exported(const char *name, struct module *mod, - enum export export) +static struct symbol *sym_add_exported(const char *name, + struct module *mod, enum export export) { - struct symbol *s = find_symbol(name); + const char *extract_name = name; + const char *ns = sym_extract_ns(&extract_name); + struct symbol *s = find_symbol(extract_name); if (!s) { - s = new_symbol(name, mod, export); + s = new_symbol(extract_name, mod, export); + s->ns = ns; } else { if (!s->preloaded) { warn("%s: '%s' exported twice. Previous export " - "was in %s%s\n", mod->name, name, + "was in %s%s\n", mod->name, extract_name, s->module->name, is_vmlinux(s->module->name) ?"":".ko"); } else { @@ -697,6 +746,10 @@ static void handle_modversions(struct module *mod, struct elf_info *info, } if (strcmp(symname, "init_module") == 0) mod->has_init = 1; + if (strstarts(symname, "__knsimport_")) { + const char *name = symname + strlen("__knsimport_"); + add_namespace(&mod->imported_namespaces, name); + } if (strcmp(symname, "cleanup_module") == 0) mod->has_cleanup = 1; break; @@ -2097,6 +2150,13 @@ static void check_exports(struct module *mod) basename++; else basename = mod->name; + + if (exp->ns && !module_imports_namespace(mod, exp->ns)) { + warn("module %s uses symbol %s from namespace %s, " + "but does not import it.\n", + basename, exp->name, exp->ns); + } + if (!mod->gpl_compatible) check_for_gpl_usage(exp->export, basename, exp->name); check_for_unused(exp->export, basename, exp->name); diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h index 8453d6ac2f77..9626bf3e7424 100644 --- a/scripts/mod/modpost.h +++ b/scripts/mod/modpost.h @@ -109,6 +109,11 @@ buf_printf(struct buffer *buf, const char *fmt, ...); void buf_write(struct buffer *buf, const char *s, int len); +struct namespace_list { + struct namespace_list *next; + char namespace[0]; +}; + struct module { struct module *next; const char *name; @@ -121,6 +126,8 @@ struct module { struct buffer dev_table_buf; char srcversion[25]; int is_dot_o; + // Actual imported namespaces + struct namespace_list *imported_namespaces; }; struct elf_info { -- 2.18.0.203.gfac676dfb9-goog