From patchwork Mon Dec 2 14:59:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 13890931 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54426205AD0; Mon, 2 Dec 2024 15:11:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152298; cv=none; b=knBedDh+GbC+zOCsq9ch7tX6iMqXj17SSeY41iIuMF8fBZuFR3+xaWaFCTTfgCqmL6kuxxTFH8OWf5RHQJ62Y4V4YgCadF/j5En2ETF1GoTl43oQIGXX8zJ+Tg9c2N5N+60eQvY3mr3FKNYDopCKGdIqXAtDXxdpELrLPFBnmis= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152298; c=relaxed/simple; bh=ICOg1urReFlrbqKpNdpCrFXGtC8QcWkJbyQIfD2HIMs=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=uPFpDtul9GQF94uWFMnvJojp8vsFwGlQHdMdRczRlrRP8pUa2i/DY467RCr5MQxO707KC9S26JWe+3AWDQ97xG3xVXoxnMOxp+p129WB4wwx3Q3fR8xHn4pml+uWhR/8qM8si2gaMif4YJ9MPiFV9HJkxl4mu8SFt1Lm7nd1JKo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=T78QLsDK; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="T78QLsDK" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=GKPMvBRLR7vZ9BJ6H1VKy/+n8A02WSymmS041p20eEQ=; b=T78QLsDKTNaT6kqrSjSnU/FuAQ TJIXiXgOHX3AzgSl6S/dP1Eh5NI2fMG8dFbgOcypnjXai/+ZL/90CzQzvksEl3hCeXhH++4+svoMV DPXYE4Di4SEscn9LWEHfyA7K+m63qEFtpVSRGGBLtK0O/EjRfE/UD/d+eafidt7Ki4aOswR6vIgtB rt0fBxZc+ZsmILC8vSwWF634WUiCm63N8TxCXqkP9THPyqqkY/KMo02HmujrcCsdqw9VUOJmIAbAO lr/PZy2HIT9E7LMfn/SwJRSEyGRYt/AThRQ4bgT837QlGVpHuacJqJ22sFz7N6CuaA55xA8tg8Ngn RU6RIAWQ==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tI85A-00000002Bkk-2ZJV; Mon, 02 Dec 2024 15:11:28 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id C2B6F3006F1; Mon, 2 Dec 2024 16:11:27 +0100 (CET) Message-ID: <20241202150810.160972089@infradead.org> User-Agent: quilt/0.66 Date: Mon, 02 Dec 2024 15:59:48 +0100 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, "Peter Zijlstra (Intel)" Subject: [PATCH -v2 2/7] module/modpost: Use for() loop References: <20241202145946.108093528@infradead.org> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Peter Zijlstra (Intel) --- scripts/mod/modpost.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1586,12 +1586,9 @@ static void read_symbols(const char *mod license = get_next_modinfo(&info, "license", license); } - namespace = get_modinfo(&info, "import_ns"); - while (namespace) { + for (namespace = get_modinfo(&info, "import_ns"); namespace; + namespace = get_next_modinfo(&info, "import_ns", namespace)) add_namespace(&mod->imported_namespaces, namespace); - namespace = get_next_modinfo(&info, "import_ns", - namespace); - } if (extra_warn && !get_modinfo(&info, "description")) warn("missing MODULE_DESCRIPTION() in %s\n", modname); From patchwork Mon Dec 2 14:59:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 13890928 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 55789207A3D; Mon, 2 Dec 2024 15:11:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152297; cv=none; b=VI7sKmrqC0CgrsZ1tS4mEzpZFpmgOdyXCvQ/q7BLtHaMm8dBZ1Evy2d4Qqy73sYvjxW44mhmqG/SS0mJhdXHVTDOH3vTR9R+GG7Rspw+ee4pPXzIY/CTFGxRwFZG688li7uTAczTcDlmPNcw5wXH+5l/ZSYoUCA7bgOKsXyManA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152297; c=relaxed/simple; bh=epoKNUw20KjRIxmQqCsJhCOrh6ujz3BwXBFxQJARgCM=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=KryoujPWfvC6vFSwTShR6Q8Mtc09W3nG4C1qaYka+KYJoEdvM2LrveN1jpheOv6PI//RYFjpafWEOv3OLN+EkBDMM55nTRb2MXTWFiu+e6ibBL+Wz9LKc1JxjTlq872c3f0r91fBZFRX4BfdDrSlWAHh/zCBj+0WGD+RTdOb1kw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=d11Eevcr; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="d11Eevcr" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=pv+FVEk471RGcZ+dK0zuN25EA0xObldSGsurRFVCsFs=; b=d11EevcrA7dY47rD9JgaL/AGXG xQNiWbD7MHd7OVoOxPZvUJtSMYbtNKFH0maK7XjAHpKhXIKRJkd7wMlaSzP6yngDLB23T+xQ5dr6I CFTnNZ0kTwWj7uxhB+ImlrEPIiOTUkWar8xm0kJwcpmBMT8W/ni68i0W5Ku8300NRrldF/RPCzDL5 hwUW8pggG8vVA9ibCAarZGrBVPBopJBe6FQv1+0SOzZaB4/9IntbylCSIYJuaKibocH27FnfhEsqE ZBrlcIXIK3Gg+LOhX84c3W4xGU32CAIWMY98joOY+sr0ymCgMfRGMKFkytHwrIMmudm9qwtOIfiG3 GH2wsoqA==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tI859-00000008CPn-18lb; Mon, 02 Dec 2024 15:11:28 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id C64EC300729; Mon, 2 Dec 2024 16:11:27 +0100 (CET) Message-ID: <20241202150810.270511329@infradead.org> User-Agent: quilt/0.66 Date: Mon, 02 Dec 2024 15:59:49 +0100 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, "Peter Zijlstra (Intel)" Subject: [PATCH -v2 3/7] module/modpost: Add basename helper References: <20241202145946.108093528@infradead.org> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Peter Zijlstra (Intel) --- scripts/mod/modpost.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1553,6 +1553,16 @@ static void mod_set_crcs(struct module * free(buf); } +static const char *mod_basename(const char *modname) +{ + const char *basename = strrchr(modname, '/'); + if (basename) + basename++; + else + basename = modname; + return basename; +} + static void read_symbols(const char *modname) { const char *symname; @@ -1697,11 +1707,7 @@ static void check_exports(struct module s->crc_valid = exp->crc_valid; s->crc = exp->crc; - basename = strrchr(mod->name, '/'); - if (basename) - basename++; - else - basename = mod->name; + basename = mod_basename(mod->name); if (!contains_namespace(&mod->imported_namespaces, exp->namespace)) { modpost_log(!allow_missing_ns_imports, From patchwork Mon Dec 2 14:59:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 13890932 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB71B1FECDE; Mon, 2 Dec 2024 15:11:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152299; cv=none; b=ZTPHsh29Gd2CmBpkXQe42ETFzionB8zG7ImwGDa2SEweWn9yjfTouEOsjKMkxNJfaAnEqF/IFzvSMGMUj/Acu0aPZHqsdGJ9jik02srnwBlGEpMaKp/ovgcEf7HkrT6d6eU2Rl6Ug1LnvhyhV4Hm+GM6F09D4VxFV+WsYHn4Ch8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152299; c=relaxed/simple; bh=XHnTJHOPYtZIDpwCX8Qj4Gyauad1xJ05yQqNtHbIPkY=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=HFJGjNFVaK+2ggPdh3ILHNG9Oa3KHGX6RL7LEB4kjsMIpLibhdCvLdTOkLS9am9r4yHvlmtmi641W9lyjWUpxL9hLL8ZCOLwtwncFLAlTNuCN/2JOClBXEVx82+z52frGdygzdLqHT5ZuI08QJwsq77UObeDEV0lp3ZarbS+6vg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=TeVV6PeR; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="TeVV6PeR" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=aGjvucfOz39yLyUBScLYaZkWFoL5jwssjx5+IvdVnTE=; b=TeVV6PeRrbodIeRA0Peu2iSy1W 35Kog7wgqiIar7hDxW/FcHmMlEeOx/TMzcoCNTwv4Jc1/RpD5JyNAx/QIftdgHIHBJvuh6Gl1ltjp TRL5nPu8FulMe/y6i2ZLQjRHyrKl8SNLegDXZmDABrmRURXepAdFk2bRknGvTzJfzifVaTt/WnyWz JpKPzUXZ0Ek1dzHkr/elr/roLRZahPI8KEWCyN9lz8LPzsCGHYmSvIbUN8guKFaL2q167p5rA7GNp Nfh4a7OGTXA4VRZpg3wiXt5mCvcZ/NhHgi6KNOC+NryG5mhL6i4xXYj8yIxxSdj96CyX7BIH7q+1l QTfoSYxQ==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tI859-00000008CPm-17Pt; Mon, 02 Dec 2024 15:11:28 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id C9DFE300731; Mon, 2 Dec 2024 16:11:27 +0100 (CET) Message-ID: <20241202150810.382545529@infradead.org> User-Agent: quilt/0.66 Date: Mon, 02 Dec 2024 15:59:50 +0100 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, "Peter Zijlstra (Intel)" Subject: [PATCH -v2 4/7] module: Add module specific symbol namespace support References: <20241202145946.108093528@infradead.org> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Designate the "MODULE_${modname}" symbol namespace to mean: 'only export to the named module'. Notably, explicit imports of anything in the "MODULE_" space is forbidden. Modules implicitly get the "MODULE_${modname}" namespace added. Signed-off-by: Peter Zijlstra (Intel) --- kernel/module/main.c | 28 ++++++++++++++++++++++++++-- scripts/mod/modpost.c | 5 +++++ 2 files changed, 31 insertions(+), 2 deletions(-) --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1070,6 +1070,13 @@ static int verify_namespace_is_imported( namespace = kernel_symbol_namespace(sym); if (namespace && namespace[0]) { + /* + * Implicitly import MODULE_${mod->name} namespace. + */ + if (strncmp(namespace, "MODULE_", 7) == 0 && + strcmp(namespace+7, mod->name) == 0) + return 0; + for_each_modinfo_entry(imported_namespace, info, "import_ns") { if (strcmp(namespace, imported_namespace) == 0) return 0; @@ -1613,15 +1620,30 @@ static void module_license_taint_check(s } } -static void setup_modinfo(struct module *mod, struct load_info *info) +static int setup_modinfo(struct module *mod, struct load_info *info) { struct module_attribute *attr; + char *imported_namespace; int i; for (i = 0; (attr = modinfo_attrs[i]); i++) { if (attr->setup) attr->setup(mod, get_modinfo(info, attr->attr.name)); } + + for_each_modinfo_entry(imported_namespace, info, "import_ns") { + /* + * 'MODULE_' prefixed namespaces are implicit, disallow + * explicit imports. + */ + if (strstarts(imported_namespace, "MODULE_")) { + pr_err("%s: module tries to import module namespace: %s\n", + mod->name, imported_namespace); + return -EPERM; + } + } + + return 0; } static void free_modinfo(struct module *mod) @@ -2935,7 +2957,9 @@ static int load_module(struct load_info goto free_unload; /* Set up MODINFO_ATTR fields */ - setup_modinfo(mod, info); + err = setup_modinfo(mod, info); + if (err) + goto free_modinfo; /* Fix up syms, so that st_value is a pointer to location. */ err = simplify_symbols(mod, info); --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1565,6 +1565,7 @@ static const char *mod_basename(const ch static void read_symbols(const char *modname) { + char module_namespace[MODULE_NAME_LEN + 8]; const char *symname; char *version; char *license; @@ -1600,6 +1601,10 @@ static void read_symbols(const char *mod namespace = get_next_modinfo(&info, "import_ns", namespace)) add_namespace(&mod->imported_namespaces, namespace); + snprintf(module_namespace, sizeof(module_namespace), "MODULE_%s", + mod_basename(mod->name)); + add_namespace(&mod->imported_namespaces, module_namespace); + if (extra_warn && !get_modinfo(&info, "description")) warn("missing MODULE_DESCRIPTION() in %s\n", modname); } From patchwork Mon Dec 2 14:59:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 13890930 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4E824205ACC; Mon, 2 Dec 2024 15:11:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152297; cv=none; b=ehsONdijU0vnjOYM5tET+juKlocWTd9SHSqT3qVOz6dR2pcDwLfZo1uTwgx6Y2pEq6e4NSxbMl7flKh/cUecOwBiTXWDCXcnfqfOKv/kYd3DBpFQhMKBwPujQaoWOU1PzM1zKTsXBT+cYxu61YWJn8P3aiMbmmq2m+woUiQYtdw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152297; c=relaxed/simple; bh=Gh1yFaFkFcr2xJc52xK1w4jF0RWt4DU76CiOwQOb1Hk=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=eXf+ohg0jFNiKpLxF9Xcv1rarJfq4hhY/yMr1JYxBJg9f0/C9UePIFqIHvb9oQBLXizsa4M9Xhfd98+ndkUArlu5ii/h368+Npvo4NJs3sojNoKtK++b9l6u8vqaI99nTxM+58fddf4D0U6MW8Z7ASTKtGlGnnO3CmiDDUuF1Rw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=ZffuDRwq; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="ZffuDRwq" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=jDtXY2bUyMjOXT8/HogIZaen7IxgRS/M1Ecyy8kWGnA=; b=ZffuDRwqBI2/58aAedJbHfQ+je 9593YANjszF3dgTgQ+9O4oBIzcI4ofkMzZjLSyU40vrOQb5qlKeLFw/KAXoZI1a7oIjfDFC2TMGz1 e52es2buivve00KSoF7oi62hAEM4sc1LBJ3WYwJe4qK4ZQFAxwtyd+5G/5mIkujmBWjupxmWZCTV2 rh++1PyAUUWFCOIB+VDgdFIUj6g0ExKHmx/+l4TEf8/TzFNFVCyOmmS20qJezeRSGRALadqyywRdY nxcvO88qcN3l3/0DAJwW4wZw0SdUSRTIZRbR707EfRNeMFf0jztQVZIb75UNuwlUaf4yeOmLbRHKm G/QxGgQQ==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tI859-00000008CQ3-3re7; Mon, 02 Dec 2024 15:11:29 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id CDA44300780; Mon, 2 Dec 2024 16:11:27 +0100 (CET) Message-ID: <20241202150810.496784892@infradead.org> User-Agent: quilt/0.66 Date: Mon, 02 Dec 2024 15:59:51 +0100 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, "Peter Zijlstra (Intel)" Subject: [PATCH -v2 5/7] module: Extend the MODULE_ namespace parsing References: <20241202145946.108093528@infradead.org> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Instead of only accepting "MODULE_${name}", extend it with a comma separated list of module names and add tail glob support. That is, something like: "MODULE_foo-*,bar" is now possible. Signed-off-by: Peter Zijlstra (Intel) --- kernel/module/main.c | 39 ++++++++++++++++++++++++++++++++++----- scripts/mod/modpost.c | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 11 deletions(-) --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1061,6 +1061,38 @@ static char *get_modinfo(const struct lo return get_next_modinfo(info, tag, NULL); } +/* + * @namespace ~= "MODULE_foo-*,bar", match @modname to 'foo-*' or 'bar' + */ +static bool verify_module_namespace(const char *namespace, const char *modname) +{ + size_t len, modlen = strlen(modname); + const char *sep; + bool glob; + + if (strncmp(namespace, "MODULE_", 7) != 0) + return false; + + for (namespace += 7; *namespace; namespace = sep) { + sep = strchrnul(namespace, ','); + len = sep - namespace; + + glob = false; + if (sep[-1] == '*') { + len--; + glob = true; + } + + if (*sep) + sep++; + + if (strncmp(namespace, modname, len) == 0 && (glob || len == modlen)) + return true; + } + + return false; +} + static int verify_namespace_is_imported(const struct load_info *info, const struct kernel_symbol *sym, struct module *mod) @@ -1070,11 +1102,8 @@ static int verify_namespace_is_imported( namespace = kernel_symbol_namespace(sym); if (namespace && namespace[0]) { - /* - * Implicitly import MODULE_${mod->name} namespace. - */ - if (strncmp(namespace, "MODULE_", 7) == 0 && - strcmp(namespace+7, mod->name) == 0) + + if (verify_module_namespace(namespace, mod->name)) return 0; for_each_modinfo_entry(imported_namespace, info, "import_ns") { --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1565,7 +1565,6 @@ static const char *mod_basename(const ch static void read_symbols(const char *modname) { - char module_namespace[MODULE_NAME_LEN + 8]; const char *symname; char *version; char *license; @@ -1601,10 +1600,6 @@ static void read_symbols(const char *mod namespace = get_next_modinfo(&info, "import_ns", namespace)) add_namespace(&mod->imported_namespaces, namespace); - snprintf(module_namespace, sizeof(module_namespace), "MODULE_%s", - mod_basename(mod->name)); - add_namespace(&mod->imported_namespaces, module_namespace); - if (extra_warn && !get_modinfo(&info, "description")) warn("missing MODULE_DESCRIPTION() in %s\n", modname); } @@ -1687,6 +1682,38 @@ void buf_write(struct buffer *buf, const buf->pos += len; } +/* + * @namespace ~= "MODULE_foo-*,bar", match @modname to 'foo-*' or 'bar' + */ +static bool module_namespace(const char *namespace, const char *modname) +{ + size_t len, modlen = strlen(modname); + const char *sep; + bool glob; + + if (strncmp(namespace, "MODULE_", 7) != 0) + return false; + + for (namespace += 7; *namespace; namespace = sep) { + sep = strchrnul(namespace, ','); + len = sep - namespace; + + glob = false; + if (sep[-1] == '*') { + len--; + glob = true; + } + + if (*sep) + sep++; + + if (strncmp(namespace, modname, len) == 0 && (glob || len == modlen)) + return true; + } + + return false; +} + static void check_exports(struct module *mod) { struct symbol *s, *exp; @@ -1714,7 +1741,8 @@ static void check_exports(struct module basename = mod_basename(mod->name); - if (!contains_namespace(&mod->imported_namespaces, exp->namespace)) { + if (!module_namespace(exp->namespace, basename) && + !contains_namespace(&mod->imported_namespaces, exp->namespace)) { modpost_log(!allow_missing_ns_imports, "module %s uses symbol %s from namespace %s, but does not import it.\n", basename, exp->name, exp->namespace); From patchwork Mon Dec 2 14:59:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 13890927 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7110C205ACD; Mon, 2 Dec 2024 15:11:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152296; cv=none; b=BPdP+j0RlPt/ku4kgy81D8dFnZbU7V8AUV4du/Od4YY+MTn1ZjwvOSXy0ehPZOyqQpDeVp2SczCjcmuIAJgCqJy42KHg0xP0drTT7FiYS64iAA8pgZfwjEvzrxMQcpBlXjgl2witB5rhBt6Qr41Sqtl371SAbFWTRnrDPFAlFSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152296; c=relaxed/simple; bh=a10zqHBADACqh+lSa+gIc0SQC3HXeDKZ/DGTcNuXhoA=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=HydttQUEOy1xBqBsKSNvQgYL+2RBjZAJfYmHoPYfVZ7omNlhCOJrO3a15lLhi2kJgDstugYsuWsquNcv6JI2Uj6QsMl0psjSksAPQ6XhCFD/9r9bsA9Gfbpfk1YS8NAV/NPOOwklvYiWHibWYigR2SWulq6Io/msBMtv0vCposs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=kB6lrSEm; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="kB6lrSEm" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=xFrdyuMgRUUJGyOInxj7bUrT/h7Ni0x8G1WlCO144yg=; b=kB6lrSEm821XI+oiRPpmwx5XvQ pSx3wbtdR4rP3pyHRBatJF4BUIeOW6W5YcrW2OhpGwhNyHxP93sCN9snCldKcvIf+wP58c6tVQRpG GY+BYcVHkLpZzlQLo672VbsIvkbLCGsLicrIXsBDqqDCVwuxTUWZ41rbml82XGwknEqWNA94iuvXV D/RTH3nB2EcEaPtJhYdO97qHmf4RNLac2c3BlIjhS1GWqK3CW1VT74sN+NoW+/oeRzjFC5bZMoGnI qT4l+dgQgSx4z3KcJlb2ugt9OpXKiU26OjxED6YEzd4xPOpDJtr4s2xMlF8o7RXhK8n2uOryOLiNH FFCu0pWQ==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tI859-00000008CQ4-3iat; Mon, 02 Dec 2024 15:11:29 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id D109F300848; Mon, 2 Dec 2024 16:11:27 +0100 (CET) Message-ID: <20241202150810.606849101@infradead.org> User-Agent: quilt/0.66 Date: Mon, 02 Dec 2024 15:59:52 +0100 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, Sean Christopherson , "Peter Zijlstra (Intel)" Subject: [PATCH -v2 6/7] module: Account for the build time module name mangling References: <20241202145946.108093528@infradead.org> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Sean noted that scripts/Makefile.lib:name-fix-token rule will mangle the module name with s/-/_/g. Since this happens late in the build, only the kernel needs to bother with this, the modpost tool still sees the original name. Reported-by: Sean Christopherson Signed-off-by: Peter Zijlstra (Intel) Tested-by: Sean Christopherson --- kernel/module/main.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1062,6 +1062,30 @@ static char *get_modinfo(const struct lo } /* + * Like strncmp(), except s/-/_/g as per scripts/Makefile.lib:name-fix-token rule. + */ +static int mod_strncmp(const char *str_a, const char *str_b, size_t n) +{ + for (int i = 0; i < n; i++) { + char a = str_a[i]; + char b = str_b[i]; + int d; + + if (a == '-') a = '_'; + if (b == '-') b = '_'; + + d = a - b; + if (d) + return d; + + if (!a) + break; + } + + return 0; +} + +/* * @namespace ~= "MODULE_foo-*,bar", match @modname to 'foo-*' or 'bar' */ static bool verify_module_namespace(const char *namespace, const char *modname) @@ -1086,7 +1110,7 @@ static bool verify_module_namespace(cons if (*sep) sep++; - if (strncmp(namespace, modname, len) == 0 && (glob || len == modlen)) + if (mod_strncmp(namespace, modname, len) == 0 && (glob || len == modlen)) return true; } From patchwork Mon Dec 2 14:59:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 13890929 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 414F52040A7; Mon, 2 Dec 2024 15:11:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152297; cv=none; b=lVVTWCVrsihHo1VP3X4L0TLRK0izVbN3V0UL8UkZfDUX8Jhr8CwNlRclm8ifh2j3k97Ad7P2VHT4z46645WaQMiK7rpDQcYPjsU8RSlMbRC/LAS/OzoQJpne3RXtgph7IX6fVm5Fc/O5o99ssW76iH3+zmiLOEQOHmviH62N0pw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733152297; c=relaxed/simple; bh=XhHU543T1F3w14Rm03rZzs+C84JVQ8YZfAkynEfLBx8=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=Fum0A0ujlSPMDS0ve6ocgVZAgayFuKpizowsl93750mY1pYcAx942OA7WoF4pjq8wTNoCQGdR6Vamncz+vSjDo+1Ych5syKS961DUNBkJFrrGeevBz5FhsoshLgdqlSb59lwGFAmile1Pb1K0pmQudC22hxJRK8y4sygF1qcS3A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=sJnm9gen; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="sJnm9gen" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=6N0qDSelpM+yvXc7hsTj7XyqDv+BJKuq3m0opDDR0WI=; b=sJnm9genTuMS+qdJXmTcHhqpMk 0QCvDJz71FSGYaavs78RRfugwdz/0jzKFEY7oHfIDpjOAmnyeRZIXOERbNlAD5hj/Wc1a3OzZq8fH vkFh8KpePxNfFP18TsMJm7KOj7MJ5g6u3LcGiZ2S8ftGl6tKNXki5jl6zE6Oaqh959E26K/frjOeN idRCOoiJvi/Y7cKLh/0Xu9VlCoFLkPZmHu8ImOrU/q/vLZO1EztapC4RMpyVrmWILtkvfQK6ZKp5L A59hr6aM07FaCMVpHG2CU1G0RodyIo2JKhD78kEKeJ4id9fENm4+4w4X66Sie7IliSE/VXCSTNMAa 7GkwlEUA==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tI859-00000008CQ5-3q67; Mon, 02 Dec 2024 15:11:29 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id D4AFF300ABE; Mon, 2 Dec 2024 16:11:27 +0100 (CET) Message-ID: <20241202150810.713959190@infradead.org> User-Agent: quilt/0.66 Date: Mon, 02 Dec 2024 15:59:53 +0100 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, "Peter Zijlstra (Intel)" Subject: [PATCH -v2 7/7] module: Provide EXPORT_SYMBOL_GPL_FOR() helper References: <20241202145946.108093528@infradead.org> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Requested-by: Masahiro Yamada Requested-by: Christoph Hellwig Signed-off-by: Peter Zijlstra (Intel) --- include/linux/export.h | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) --- a/include/linux/export.h +++ b/include/linux/export.h @@ -24,11 +24,23 @@ .long sym #endif -#define ___EXPORT_SYMBOL(sym, license, ns) \ +/* + * LLVM intregrated assembler refuses to merge adjacent string literals (like + * C and GNU-as) and chokes on: + * + * .asciz "MODULE_" "kvm" ; + * + * As would be generated when using EXPORT_SYMBOL_GPL_FOR(foo, "kvm"), use + * varargs to assemble it like so: + * + * .ascii "MODULE_", "kvm", "\0" ; + * + */ +#define ___EXPORT_SYMBOL(sym, license, ns...) \ .section ".export_symbol","a" ASM_NL \ __export_symbol_##sym: ASM_NL \ .asciz license ASM_NL \ - .asciz ns ASM_NL \ + .ascii ns, "\0" ASM_NL \ __EXPORT_SYMBOL_REF(sym) ASM_NL \ .previous @@ -39,20 +51,20 @@ * be reused in other execution contexts such as the UEFI stub or the * decompressor. */ -#define __EXPORT_SYMBOL(sym, license, ns) +#define __EXPORT_SYMBOL(sym, license, ns...) #elif defined(__GENKSYMS__) -#define __EXPORT_SYMBOL(sym, license, ns) __GENKSYMS_EXPORT_SYMBOL(sym) +#define __EXPORT_SYMBOL(sym, license, ns...) __GENKSYMS_EXPORT_SYMBOL(sym) #elif defined(__ASSEMBLY__) -#define __EXPORT_SYMBOL(sym, license, ns) \ +#define __EXPORT_SYMBOL(sym, license, ns...) \ ___EXPORT_SYMBOL(sym, license, ns) #else -#define __EXPORT_SYMBOL(sym, license, ns) \ +#define __EXPORT_SYMBOL(sym, license, ns...) \ extern typeof(sym) sym; \ __ADDRESSABLE(sym) \ asm(__stringify(___EXPORT_SYMBOL(sym, license, ns))) @@ -70,4 +82,6 @@ #define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL(sym, "", ns) #define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym, "GPL", ns) +#define EXPORT_SYMBOL_GPL_FOR(sym, mods) __EXPORT_SYMBOL(sym, "GPL", "MODULE_", mods) + #endif /* _LINUX_EXPORT_H */