From patchwork Mon Dec 23 20:13:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13919214 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C8AEC1C5F20 for ; Mon, 23 Dec 2024 20:13:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734984819; cv=none; b=UjeGyF2rvUpaLFfuY3hvbMRwCImkLMC4/xgEuo97gBQt12Gqeq8LRDrRvqnkd7dVSAZmZQxYrnb0o1C4fDocd1kLsFjkFKJxo0xcWSc6TF3ptZ6BV30wwbUOAM+4yX9dUjpeGnN5ZvqBHbmdRafhSUdGaUiq7R3YpIZR4dilCms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734984819; c=relaxed/simple; bh=twF35KAwFtDDW1a937N75qcfQ8htqMpDhrgOzA3ntDM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IXgygTdWTO7vXEReYnI0f+Ly5mAz4KXs741r1QU1XVI28FNKQtTV1gHHnuaCAt5aByFmFxY8vtSWEw9RwjSosyElfDqOZAfzEHZYTmhvS8V3ox/vT//T8lo/zNYChHQKtOpAilf4UxCyS0Hn6JhR73L2Tcy2Oe/v7OlS6RZoqlU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=EruiDTKU; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EruiDTKU" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2eeeb5b7022so4159857a91.0 for ; Mon, 23 Dec 2024 12:13:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734984816; x=1735589616; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=G3AMP19Z6VEHI5UpmOXNK7NurAZp2kc6IdV88EWGS14=; b=EruiDTKU2sKaelp4yfuyip3ZBAWhMVbF502aEnsa/X4WZ2vSZsCy2DpJ2sdxPPrgtD wS2g4BPVj18s341+WO6wpPC99DUbenZlGdaHbeOFBvbpXpO7+viQ3J36Bm6/IMvCoFBV R0R6cdNXCKvQwQGZTFopzxGL5CKaLN10pEyb1IFpIm3ShcMjyF/mmimJAA4zrSPOIiDN 1MsMXvHACE+e+LPunvXgP5XsiAQrISPXeIRZ1rx0LR0Oet2Ol1RPtGDTUH6gwcpn98UJ 5NRTxk+MydlfpZTukroYnsNLvG0O3qMM0QOu5zlSA6dnGUYzzZCqSd7JV5vTWpiKMyCx J11g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734984816; x=1735589616; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G3AMP19Z6VEHI5UpmOXNK7NurAZp2kc6IdV88EWGS14=; b=PXf9ngG0xiPxfNBs6QzJah4h8Xs5OiCm2qztySeDksG587+tU4qNK7pw2BGCfXQCF4 fTeJf/SyD/YZ4vYzvf/1qm0qluHAtHG4x8BVzZHZ8WbGNmj60i05+FIm1f67gAx7XTCs PfKUxakaxWU6T/4Z782scXJs9+HW1dwbmKfwv5+vk3j+yahWKgJUXAQK5SVsrR6fTX2B bWitYnzrSq1IVxbFP5a/Yuh8OlYpc2/w5cZhP2e1N/Y/3Ufn39Jv2z81/K/IPKOfPj+Q UU0x+/3Kc9Tah79gOlY0EmGi61XbP7WvdOgEfk6JUay61Fn4LbHvzLt8hwiTDzE+/NwS TzEQ== X-Forwarded-Encrypted: i=1; AJvYcCV/Q8npohpz0/5+Tq4reOVknpSZDGbMuVGshJTxdLvaYlYB6EJFgarYq2VM1tabKbQOwGA0eZnqChoxVgQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxpHiZDRcRb4fctUdsAEnZACD+kkZK5uk02EOGaae2VVNaxBZhK z25CyR+JCjwfVBS4V6OuEpYhensxoxICIslUyMtJ7eGwv11el5/+7sO1GlNcTqfIRlXI5oYgztc GFoMZBA== X-Google-Smtp-Source: AGHT+IFnqpaX2dmjmPD3Dv/b7lsEYQuw4DtaWgwOM10bmnlmyUi13PN7tGMzr6lR0qdkbplVgiIMCPn03OHf X-Received: from pjbsb11.prod.google.com ([2002:a17:90b:50cb:b0:2ea:61ba:b8f7]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1d4c:b0:2ea:3f34:f194 with SMTP id 98e67ed59e1d1-2f452e1b50emr22222942a91.10.1734984816198; Mon, 23 Dec 2024 12:13:36 -0800 (PST) Date: Mon, 23 Dec 2024 20:13:30 +0000 In-Reply-To: <20241223-extended-modversions-v11-0-221d184ee9a7@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241223-extended-modversions-v11-0-221d184ee9a7@google.com> X-Mailer: b4 0.15-dev Message-ID: <20241223-extended-modversions-v11-1-221d184ee9a7@google.com> Subject: [PATCH v11 1/5] modules: Support extended MODVERSIONS info From: Matthew Maurer To: Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Jonathan Corbet Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Matthew Maurer Adds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer). Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information. Reviewed-by: Sami Tolvanen Signed-off-by: Matthew Maurer --- arch/powerpc/kernel/module_64.c | 24 ++++++++++- kernel/module/internal.h | 11 +++++ kernel/module/main.c | 92 +++++++++++++++++++++++++++++++++++++---- kernel/module/version.c | 45 ++++++++++++++++++++ 4 files changed, 162 insertions(+), 10 deletions(-) diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_64.c index 45dac7b46aa3cdcb2058a2320b88c0d67e5586b3..34a5aec4908fba3b91a02e914264cb525918942a 100644 --- a/arch/powerpc/kernel/module_64.c +++ b/arch/powerpc/kernel/module_64.c @@ -369,6 +369,24 @@ static void dedotify_versions(struct modversion_info *vers, } } +/* Same as normal versions, remove a leading dot if present. */ +static void dedotify_ext_version_names(char *str_seq, unsigned long size) +{ + unsigned long out = 0; + unsigned long in; + char last = '\0'; + + for (in = 0; in < size; in++) { + /* Skip one leading dot */ + if (last == '\0' && str_seq[in] == '.') + in++; + last = str_seq[in]; + str_seq[out++] = last; + } + /* Zero the trailing portion of the names table for robustness */ + memset(&str_seq[out], 0, size - out); +} + /* * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. * seem to be defined (value set later). @@ -438,10 +456,12 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr, me->arch.toc_section = i; if (sechdrs[i].sh_addralign < 8) sechdrs[i].sh_addralign = 8; - } - else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0) + } else if (strcmp(secstrings + sechdrs[i].sh_name, "__versions") == 0) dedotify_versions((void *)hdr + sechdrs[i].sh_offset, sechdrs[i].sh_size); + else if (strcmp(secstrings + sechdrs[i].sh_name, "__version_ext_names") == 0) + dedotify_ext_version_names((void *)hdr + sechdrs[i].sh_offset, + sechdrs[i].sh_size); if (sechdrs[i].sh_type == SHT_SYMTAB) dedotify((void *)hdr + sechdrs[i].sh_offset, diff --git a/kernel/module/internal.h b/kernel/module/internal.h index daef2be8390222c22220e2f168baa8d35ad531b9..59959c21b205bf91c0073260885743098c4022cf 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -86,6 +86,8 @@ struct load_info { unsigned int vers; unsigned int info; unsigned int pcpu; + unsigned int vers_ext_crc; + unsigned int vers_ext_name; } index; }; @@ -389,6 +391,15 @@ void module_layout(struct module *mod, struct modversion_info *ver, struct kerne struct kernel_symbol *ks, struct tracepoint * const *tp); int check_modstruct_version(const struct load_info *info, struct module *mod); int same_magic(const char *amagic, const char *bmagic, bool has_crcs); +struct modversion_info_ext { + size_t remaining; + const s32 *crc; + const char *name; +}; +void modversion_ext_start(const struct load_info *info, struct modversion_info_ext *ver); +void modversion_ext_advance(struct modversion_info_ext *ver); +#define for_each_modversion_info_ext(ver, info) \ + for (modversion_ext_start(info, &ver); ver.remaining > 0; modversion_ext_advance(&ver)) #else /* !CONFIG_MODVERSIONS */ static inline int check_version(const struct load_info *info, const char *symname, diff --git a/kernel/module/main.c b/kernel/module/main.c index 878191c65efc629f14659712cd2b7ee972a6f73d..b5c052cb05715609c1d7a59c1b9efd2dc9b1d668 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2073,6 +2073,82 @@ static int elf_validity_cache_index_str(struct load_info *info) return 0; } +/** + * elf_validity_cache_index_versions() - Validate and cache version indices + * @info: Load info to cache version indices in. + * Must have &load_info->sechdrs and &load_info->secstrings populated. + * @flags: Load flags, relevant to suppress version loading, see + * uapi/linux/module.h + * + * If we're ignoring modversions based on @flags, zero all version indices + * and return validity. Othewrise check: + * + * * If "__version_ext_crcs" is present, "__version_ext_names" is present + * * There is a name present for every crc + * + * Then populate: + * + * * &load_info->index.vers + * * &load_info->index.vers_ext_crc + * * &load_info->index.vers_ext_names + * + * if present. + * + * Return: %0 if valid, %-ENOEXEC on failure. + */ +static int elf_validity_cache_index_versions(struct load_info *info, int flags) +{ + unsigned int vers_ext_crc; + unsigned int vers_ext_name; + size_t crc_count; + size_t remaining_len; + size_t name_size; + char *name; + + /* If modversions were suppressed, pretend we didn't find any */ + if (flags & MODULE_INIT_IGNORE_MODVERSIONS) { + info->index.vers = 0; + info->index.vers_ext_crc = 0; + info->index.vers_ext_name = 0; + return 0; + } + + vers_ext_crc = find_sec(info, "__version_ext_crcs"); + vers_ext_name = find_sec(info, "__version_ext_names"); + + /* If we have one field, we must have the other */ + if (!!vers_ext_crc != !!vers_ext_name) { + pr_err("extended version crc+name presence does not match"); + return -ENOEXEC; + } + + /* + * If we have extended version information, we should have the same + * number of entries in every section. + */ + if (vers_ext_crc) { + crc_count = info->sechdrs[vers_ext_crc].sh_size / sizeof(s32); + name = (void *)info->hdr + + info->sechdrs[vers_ext_name].sh_offset; + remaining_len = info->sechdrs[vers_ext_name].sh_size; + + while (crc_count--) { + name_size = strnlen(name, remaining_len) + 1; + if (name_size > remaining_len) { + pr_err("more extended version crcs than names"); + return -ENOEXEC; + } + remaining_len -= name_size; + name += name_size; + } + } + + info->index.vers = find_sec(info, "__versions"); + info->index.vers_ext_crc = vers_ext_crc; + info->index.vers_ext_name = vers_ext_name; + return 0; +} + /** * elf_validity_cache_index() - Resolve, validate, cache section indices * @info: Load info to read from and update. @@ -2087,9 +2163,7 @@ static int elf_validity_cache_index_str(struct load_info *info) * * elf_validity_cache_index_mod() * * elf_validity_cache_index_sym() * * elf_validity_cache_index_str() - * - * If versioning is not suppressed via flags, load the version index from - * a section called "__versions" with no validation. + * * elf_validity_cache_index_versions() * * If CONFIG_SMP is enabled, load the percpu section by name with no * validation. @@ -2112,11 +2186,9 @@ static int elf_validity_cache_index(struct load_info *info, int flags) err = elf_validity_cache_index_str(info); if (err < 0) return err; - - if (flags & MODULE_INIT_IGNORE_MODVERSIONS) - info->index.vers = 0; /* Pretend no __versions section! */ - else - info->index.vers = find_sec(info, "__versions"); + err = elf_validity_cache_index_versions(info, flags); + if (err < 0) + return err; info->index.pcpu = find_pcpusec(info); @@ -2327,6 +2399,10 @@ static int rewrite_section_headers(struct load_info *info, int flags) /* Track but don't keep modinfo and version sections. */ info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; + info->sechdrs[info->index.vers_ext_crc].sh_flags &= + ~(unsigned long)SHF_ALLOC; + info->sechdrs[info->index.vers_ext_name].sh_flags &= + ~(unsigned long)SHF_ALLOC; info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; return 0; diff --git a/kernel/module/version.c b/kernel/module/version.c index 53f43ac5a73e9d537a9e95ff97728a51fad0e797..c246d40879706d4f413fa7ea9bbe2264ea1b2aa8 100644 --- a/kernel/module/version.c +++ b/kernel/module/version.c @@ -19,11 +19,28 @@ int check_version(const struct load_info *info, unsigned int versindex = info->index.vers; unsigned int i, num_versions; struct modversion_info *versions; + struct modversion_info_ext version_ext; /* Exporting module didn't supply crcs? OK, we're already tainted. */ if (!crc) return 1; + /* If we have extended version info, rely on it */ + if (info->index.vers_ext_crc) { + for_each_modversion_info_ext(version_ext, info) { + if (strcmp(version_ext.name, symname) != 0) + continue; + if (*version_ext.crc == *crc) + return 1; + pr_debug("Found checksum %X vs module %X\n", + *crc, *version_ext.crc); + goto bad_version; + } + pr_warn_once("%s: no extended symbol version for %s\n", + info->name, symname); + return 1; + } + /* No versions at all? modprobe --force does this. */ if (versindex == 0) return try_to_force_load(mod, symname) == 0; @@ -87,6 +104,34 @@ int same_magic(const char *amagic, const char *bmagic, return strcmp(amagic, bmagic) == 0; } +void modversion_ext_start(const struct load_info *info, + struct modversion_info_ext *start) +{ + unsigned int crc_idx = info->index.vers_ext_crc; + unsigned int name_idx = info->index.vers_ext_name; + Elf_Shdr *sechdrs = info->sechdrs; + + /* + * Both of these fields are needed for this to be useful + * Any future fields should be initialized to NULL if absent. + */ + if (crc_idx == 0 || name_idx == 0) { + start->remaining = 0; + return; + } + + start->crc = (const s32 *)sechdrs[crc_idx].sh_addr; + start->name = (const char *)sechdrs[name_idx].sh_addr; + start->remaining = sechdrs[crc_idx].sh_size / sizeof(*start->crc); +} + +void modversion_ext_advance(struct modversion_info_ext *vers) +{ + vers->remaining--; + vers->crc++; + vers->name += strlen(vers->name) + 1; +} + /* * Generate the signature for all relevant module structures here. * If these change, we don't want to try to parse the module. From patchwork Mon Dec 23 20:13:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13919215 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 728061C9DC6 for ; Mon, 23 Dec 2024 20:13:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734984821; cv=none; b=WvBaC+UATjm49Fq3mF2rcAHddqRJTmQbrRHHpbISaasmutRzQzvnxmdpp+SUP9BoqDUcv1Kh+qt23fhias0OEXl55grsBSfXv72ejp4A3bA9EXY2ued5WB8Ui5ACtLD/M/LJDRyw1LoEPjQrNQVHZfDxz9ZN7WVAu7CZAoi4Td4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734984821; c=relaxed/simple; bh=+pGdWw9dCP4TIIYqcXyKB+6q5rytt9oeO6oqK97XBeg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=C1Fc3jQUr9Qe1PglR2DhYXeoMAj4QiS/BEEm7ckDsASnMfgycVpgzCo/Fm7k3JOO6f0NrcZeT0Se1rXdTpGTYK72qBe9aInLM8zlF946YL4pNmFz7o0shUXeyOu+zVzxXZrtR8iEOIAG2FEi0nliradQYk6E5aM3y3gZaHUOLpU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DfHKNlVm; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DfHKNlVm" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2163dc0f689so67281145ad.1 for ; Mon, 23 Dec 2024 12:13:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734984818; x=1735589618; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Wm8B/9CMerVHPJIle0PHCkEtY10ODvfRfmKMnultoak=; b=DfHKNlVmjCDxyMV9276ORrCNSCAHlAhUPzWO5vzXMoU7TEoCBej2xjTWYsAEQX7Noy jr+sLsZ3dKkOgLNr7lfGXX0+wKXM2VPc4npDlOwK1tKStnjBX3FTGF+tR3cFEEzivEgc i4g5os+XH1ms5p0jvkkMyeAd47aE4z/70AJwKy/yNOzFk2Izv5rZdnZYK16K8KnnBm1e BDyX7EDhKFpC0k6TlyKHsp+pWthgSiatnVbha40nZLE1HF3Ko9vaYjU4lAxWe7Wxsmfi 4IaMR8OSRG/G/DYacR0lYmMO7HNiJ/sK4q6egwa4KLAkFqMsK0CpmOLa7HQNTwd47qc0 YWdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734984818; x=1735589618; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Wm8B/9CMerVHPJIle0PHCkEtY10ODvfRfmKMnultoak=; b=cfFCkLma2Xv8XMYGOpRGzov1XRdhE3HaxS7Y9vHIChsl/VaxDjXMWhrg09k1NVXklz 0jzz3I3ljeGV4MBmyFSCpmuAggrhaLq80n49PD2smsGe/XsfABe+sq9BQnKosFJTWUV3 jVJndttpsf2J0z3VFJ3iw4aDUyNGfgZ6OZhcxAHp3zFoEHYTuhdUpE7xEO6fiEnqFAmL NQ8GSx8jnJVL0B0MPjwzEfz2UloMnh3tXeRKI/qhubiVLLG+zsISXxBG8h37JhvrpBUg ndAmgOB8pZiXlhR7MZhcKVjN+E7i6RT89NIR6SRcM9h40/V4Qex9obWZuqUepxr4OxOl AeQg== X-Forwarded-Encrypted: i=1; AJvYcCV0OrDhRQY6yiPuav90Pf+jzHoCQFPBDIusEXXIc7zT1zYVOfyPqBOWAUSWjd3KKuSm8444YUjClD8hEqU=@vger.kernel.org X-Gm-Message-State: AOJu0YxfFsB60TIDpeo0417kjh722mGbAzfxcaoJvmBSeEERk5HQc1jk k5BoMCad5+3Z0SlqlkwRgUkvS1QqLZ3w+ZpVLng7eqAP1WAoXvL6IgIX6ZQpWnae6heconJ7wkM XJdlO5w== X-Google-Smtp-Source: AGHT+IF/CSACGuQIODdBatw3qoH5oZKqR5KjGsMU23n8bvd3AqVBxh4DpdMuET/yVzNegGW+DtQIzSp6IxGh X-Received: from plot20.prod.google.com ([2002:a17:902:8c94:b0:216:7952:a32a]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e746:b0:216:6769:9ee7 with SMTP id d9443c01a7336-219e6f13c5dmr185546905ad.41.1734984817690; Mon, 23 Dec 2024 12:13:37 -0800 (PST) Date: Mon, 23 Dec 2024 20:13:31 +0000 In-Reply-To: <20241223-extended-modversions-v11-0-221d184ee9a7@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241223-extended-modversions-v11-0-221d184ee9a7@google.com> X-Mailer: b4 0.15-dev Message-ID: <20241223-extended-modversions-v11-2-221d184ee9a7@google.com> Subject: [PATCH v11 2/5] modpost: Produce extended MODVERSIONS information From: Matthew Maurer To: Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Jonathan Corbet Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Matthew Maurer Generate both the existing modversions format and the new extended one when running modpost. Presence of this metadata in the final .ko is guarded by CONFIG_EXTENDED_MODVERSIONS. We no longer generate an error on long symbols in modpost if CONFIG_EXTENDED_MODVERSIONS is set, as they can now be appropriately encoded in the extended section. These symbols will be skipped in the previous encoding. An error will still be generated if CONFIG_EXTENDED_MODVERSIONS is not set. Reviewed-by: Sami Tolvanen Signed-off-by: Matthew Maurer --- kernel/module/Kconfig | 10 ++++++++ scripts/Makefile.modpost | 1 + scripts/mod/modpost.c | 65 +++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig index d443fc504ffca0d1001f880ec496ab1f21fe979e..9568b629a03ce8289d3f3597eefc66fc96445720 100644 --- a/kernel/module/Kconfig +++ b/kernel/module/Kconfig @@ -207,6 +207,16 @@ config ASM_MODVERSIONS assembly. This can be enabled only when the target architecture supports it. +config EXTENDED_MODVERSIONS + bool "Extended Module Versioning Support" + depends on MODVERSIONS + help + This enables extended MODVERSIONs support, allowing long symbol + names to be versioned. + + The most likely reason you would enable this is to enable Rust + support. If unsure, say N. + config MODULE_SRCVERSION_ALL bool "Source checksum for all modules" help diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost index ab0e94ea62496e11dbaa3ffc289ce546862795ca..40426fc6350985780c0092beb49c6cc29b9eff62 100644 --- a/scripts/Makefile.modpost +++ b/scripts/Makefile.modpost @@ -43,6 +43,7 @@ MODPOST = $(objtree)/scripts/mod/modpost modpost-args = \ $(if $(CONFIG_MODULES),-M) \ $(if $(CONFIG_MODVERSIONS),-m) \ + $(if $(CONFIG_EXTENDED_MODVERSIONS),-x) \ $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \ $(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \ $(if $(KBUILD_MODPOST_WARN),-w) \ diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index fb787a5715f5e8b37713162979ca45355770dc73..c58f08681fc1e37929333ba3a3e0989c4db5a79d 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -33,6 +33,8 @@ static bool module_enabled; static bool modversions; /* Is CONFIG_MODULE_SRCVERSION_ALL set? */ static bool all_versions; +/* Is CONFIG_EXTENDED_MODVERSIONS set? */ +static bool extended_modversions; /* If we are modposting external module set to 1 */ static bool external_module; /* Only warn about unresolved symbols */ @@ -1804,6 +1806,52 @@ static void add_exported_symbols(struct buffer *buf, struct module *mod) } } +/** + * Record CRCs for unresolved symbols, supporting long names + */ +static void add_extended_versions(struct buffer *b, struct module *mod) +{ + struct symbol *s; + + if (!extended_modversions) + return; + + buf_printf(b, "\n"); + buf_printf(b, "static const s32 ____version_ext_crcs[]\n"); + buf_printf(b, "__used __section(\"__version_ext_crcs\") = {\n"); + list_for_each_entry(s, &mod->unresolved_symbols, list) { + if (!s->module) + continue; + if (!s->crc_valid) { + /* + * We already warned on this when producing the legacy + * modversions table. + */ + continue; + } + buf_printf(b, "\t%#8x,\n", s->crc); + } + buf_printf(b, "};\n"); + + buf_printf(b, "static const char ____version_ext_names[]\n"); + buf_printf(b, "__used __section(\"__version_ext_names\") =\n"); + list_for_each_entry(s, &mod->unresolved_symbols, list) { + if (!s->module) + continue; + if (!s->crc_valid) { + /* + * We already warned on this when producing the legacy + * modversions table. + * We need to skip its name too, as the indexes in + * both tables need to align. + */ + continue; + } + buf_printf(b, "\t\"%s\\0\"\n", s->name); + } + buf_printf(b, ";\n"); +} + /** * Record CRCs for unresolved symbols **/ @@ -1827,9 +1875,14 @@ static void add_versions(struct buffer *b, struct module *mod) continue; } if (strlen(s->name) >= MODULE_NAME_LEN) { - error("too long symbol \"%s\" [%s.ko]\n", - s->name, mod->name); - break; + if (extended_modversions) + /* this symbol will only be in the extended info */ + continue; + else { + error("too long symbol \"%s\" [%s.ko]\n", + s->name, mod->name); + break; + } } buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); @@ -1960,6 +2013,7 @@ static void write_mod_c_file(struct module *mod) add_header(&buf, mod); add_exported_symbols(&buf, mod); add_versions(&buf, mod); + add_extended_versions(&buf, mod); add_depends(&buf, mod); buf_printf(&buf, "\n"); @@ -2125,7 +2179,7 @@ int main(int argc, char **argv) LIST_HEAD(dump_lists); struct dump_list *dl, *dl2; - while ((opt = getopt(argc, argv, "ei:MmnT:to:au:WwENd:")) != -1) { + while ((opt = getopt(argc, argv, "ei:MmnT:to:au:WwENd:x")) != -1) { switch (opt) { case 'e': external_module = true; @@ -2174,6 +2228,9 @@ int main(int argc, char **argv) case 'd': missing_namespace_deps = optarg; break; + case 'x': + extended_modversions = true; + break; default: exit(1); } From patchwork Mon Dec 23 20:13:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13919216 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D9E91CDFDC for ; Mon, 23 Dec 2024 20:13:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734984821; cv=none; b=u5myK5NEYvcx4lrljQ5ykmsJGJZwbMehIfbbTf6pdUfA4gXoDaMtMx6mlda0Bwb8pOkIPBG1LS2ucc4/A/r6xsAdeTFgrA3ENq6WKk30j17J5Z1yb/5wnSm/zUT+Ll1KcCDclWwXl5BCcktx/ukk4/ZCi2QXLYmT1tMCCcMAvMc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734984821; c=relaxed/simple; bh=RjukP2CQnO/TZxDoNcJm3Ea2UnLYWI4ZmTxJOgCi4mU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=auuKAT32rpr1JHYUrU3qF6nsvEBZrHF+zXxeKBAJZstLAwEz9ijlprLboQ02v4UYw/5pbFt206DXUmsAOY5dtNdVrMyPrH/cINXawULCpcqNrGkEtqlztix3RnUD49LeMV0iRmBYIfZ0GZepf0akflcIiezZbATWAnl4A/kEwfk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=yNvOE5BN; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yNvOE5BN" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-218ad674181so47687225ad.1 for ; Mon, 23 Dec 2024 12:13:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734984819; x=1735589619; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=On1WZKdx98ADO/7Heas/21kozOIUfUi4pVgpzWkLyb8=; b=yNvOE5BNlgl+0eWi8IOLu/cDSg5wv2aJq8UzeFNfZmMCggWNYMRHokcjjMMj7c8tjU dUz+iFisZ7jXqc/kreVhQuzzRd/qujiVKpMHboi6eJANtTVY+IwWfq/niwJX4QWwjzIi 0/EF+CfLt9DorJWDZxXhHZFoZa/M8HyEaVd3H8V5FeOqnnYKzZ15mFT/0qw9OM0sKEqg /nd4Pk8updTlEceyyCdzBVNKd4lipdZy4Y0cxe2edjvX1x5ywjJHgJdW4oTjPpQwtMKS AmJhv9QFYcYMB8/B90XjWZIs70k6rkmSKopMfLBMSYpFP1DStOuY5LDOgYxH4xP1pyLK OwIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734984819; x=1735589619; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=On1WZKdx98ADO/7Heas/21kozOIUfUi4pVgpzWkLyb8=; b=BL5ue1emgbP2rXhz7MEXZtG5CJspZlherND+wmXsaimttDYbngEPxFTtoZN3V7JHUO 08hjUgTlX/QehFBKA84JeJIfprULAew1TI06XzVvRGjDGi3TjH6/4NDGt/0leagJp/ZT OrVLkgmehJAzx6wpAs0my9XvIYg+zLjgSEkRno8WoL6M7Fy92al8xLBg39YIUWbN7ZXL uFXOaAsDfFBaCXK7Lm4tNyDVc9bbUAqB5GD9VOPPE1mUygSvg3crN5JPBbtFOtZ6FfSF KTEwWfW2lg47/PnW7lAKpUHEjvVh9BpHV97RG7hu8VMmufruxakgoahsnCdurxJ8YImS 3Vgw== X-Forwarded-Encrypted: i=1; AJvYcCUsnCbe/JQPGLJvM5hMf1KwCE4t7s3k4V9G/pk9Ypqs1OOeRMxLkbGlZkpcKwReb5hUzN1NFthE/IN8ktc=@vger.kernel.org X-Gm-Message-State: AOJu0YwZMZzAp5GvFBOcd/2OC4JSjW7/4bak3v801cgU13S0ICafqNQP Nh1v9BzKC2vc5/PDtJ0vjyUVkn7UAg48v9ObIC++3wC0f3tm9jKeqKEKv+IXQvwAQMBxSoe6IzN DTTyFKw== X-Google-Smtp-Source: AGHT+IGE9QNRZS/47usgacyywk7VqHL13GPydkc9VQX1I2h7hJC6BSGbzv80ZSOChZmA73HolHBhU5N9WmQP X-Received: from pfbay17.prod.google.com ([2002:a05:6a00:3011:b0:725:1ef3:c075]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3382:b0:1e1:9e9f:ae4 with SMTP id adf61e73a8af0-1e5e1e87c5bmr17167226637.13.1734984819276; Mon, 23 Dec 2024 12:13:39 -0800 (PST) Date: Mon, 23 Dec 2024 20:13:32 +0000 In-Reply-To: <20241223-extended-modversions-v11-0-221d184ee9a7@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241223-extended-modversions-v11-0-221d184ee9a7@google.com> X-Mailer: b4 0.15-dev Message-ID: <20241223-extended-modversions-v11-3-221d184ee9a7@google.com> Subject: [PATCH v11 3/5] modules: Allow extended modversions without basic MODVERSIONS From: Matthew Maurer To: Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Jonathan Corbet Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Matthew Maurer If you know that your kernel modules will only ever be loaded by a newer kernel, you can disable BASIC_MODVERSIONS to save space. This also allows easy creation of test modules to seee how tooling will respond to modules that only have the new format. Signed-off-by: Matthew Maurer --- kernel/module/Kconfig | 15 +++++++++++++++ scripts/Makefile.modpost | 1 + scripts/mod/modpost.c | 19 +++++++++++-------- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig index 9568b629a03ce8289d3f3597eefc66fc96445720..5f701391ab12c7d1b3ca496c9b3443c5a76a8745 100644 --- a/kernel/module/Kconfig +++ b/kernel/module/Kconfig @@ -217,6 +217,21 @@ config EXTENDED_MODVERSIONS The most likely reason you would enable this is to enable Rust support. If unsure, say N. +config BASIC_MODVERSIONS + bool "Basic Module Versioning Support" + depends on MODVERSIONS + default MODVERSIONS + help + This enables basic MODVERSIONS support, allowing older tools or + kernels to potentially load modules. + + Disabling this may cause older `modprobe` or `kmod` to be unable + to read MODVERSIONS information from built modules. With this + disabled, older kernels may treat this module as unversioned. + + This is enabled by default when MODVERSIONS are enabled. + If unsure, say Y. + config MODULE_SRCVERSION_ALL bool "Source checksum for all modules" help diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost index 40426fc6350985780c0092beb49c6cc29b9eff62..d7d45067d08b94a82451d66a64eae29b6826e139 100644 --- a/scripts/Makefile.modpost +++ b/scripts/Makefile.modpost @@ -43,6 +43,7 @@ MODPOST = $(objtree)/scripts/mod/modpost modpost-args = \ $(if $(CONFIG_MODULES),-M) \ $(if $(CONFIG_MODVERSIONS),-m) \ + $(if $(CONFIG_BASIC_MODVERSIONS),-b) \ $(if $(CONFIG_EXTENDED_MODVERSIONS),-x) \ $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \ $(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \ diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index c58f08681fc1e37929333ba3a3e0989c4db5a79d..d13d10bf534a4c15fa76fffb2a1dc9832bd0e95a 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -33,6 +33,8 @@ static bool module_enabled; static bool modversions; /* Is CONFIG_MODULE_SRCVERSION_ALL set? */ static bool all_versions; +/* Is CONFIG_BASIC_MODVERSIONS set? */ +static bool basic_modversions; /* Is CONFIG_EXTENDED_MODVERSIONS set? */ static bool extended_modversions; /* If we are modposting external module set to 1 */ @@ -1823,10 +1825,8 @@ static void add_extended_versions(struct buffer *b, struct module *mod) if (!s->module) continue; if (!s->crc_valid) { - /* - * We already warned on this when producing the legacy - * modversions table. - */ + warn("\"%s\" [%s.ko] has no CRC!\n", + s->name, mod->name); continue; } buf_printf(b, "\t%#8x,\n", s->crc); @@ -1840,8 +1840,8 @@ static void add_extended_versions(struct buffer *b, struct module *mod) continue; if (!s->crc_valid) { /* - * We already warned on this when producing the legacy - * modversions table. + * We already warned on this when producing the crc + * table. * We need to skip its name too, as the indexes in * both tables need to align. */ @@ -1859,7 +1859,7 @@ static void add_versions(struct buffer *b, struct module *mod) { struct symbol *s; - if (!modversions) + if (!basic_modversions) return; buf_printf(b, "\n"); @@ -2179,7 +2179,7 @@ int main(int argc, char **argv) LIST_HEAD(dump_lists); struct dump_list *dl, *dl2; - while ((opt = getopt(argc, argv, "ei:MmnT:to:au:WwENd:x")) != -1) { + while ((opt = getopt(argc, argv, "ei:MmnT:to:au:WwENd:xb")) != -1) { switch (opt) { case 'e': external_module = true; @@ -2228,6 +2228,9 @@ int main(int argc, char **argv) case 'd': missing_namespace_deps = optarg; break; + case 'b': + basic_modversions = true; + break; case 'x': extended_modversions = true; break; From patchwork Mon Dec 23 20:13:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13919217 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 435041D0F50 for ; Mon, 23 Dec 2024 20:13:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734984823; cv=none; b=H2g6DH3nEugCsdczn4HljnjTrhC9iAZJlaGrrf9kPAm/o4OMnSZOGJQKiFZ18ViNcnm11KzVrKDJNaEz41BPX7vSUfyFRyCXWjTE99uf3Brq+tp6TjFTnnqrCmZjZXHImMsQZwFb8eRmfyHHQPV88zJLnzPM6vtOZPXPrWoi6j4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734984823; c=relaxed/simple; bh=XKLtg6hasoIGAI7uNbbTgs8kYTxARwPlbSF6Jrbr4s0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jE+gjlG4aquv/ertebJ24KvIkGDm9Bqfl2YNmxRoOhHaOLxTcyMXGWb7nRj4BGvu86POYEPW7U14j9+Ql7MAzZeZtGCc+V1l0wcjABZADkdvvWjTuZoxvDmvGL0iDq+LLishhiFLA2obd0A2s2n5nGo3jxp+kchYKPqyKyZg6GQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AhwVq9dJ; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AhwVq9dJ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2166a1a5cc4so39152555ad.3 for ; Mon, 23 Dec 2024 12:13:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734984821; x=1735589621; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Vj7WTgvEC5PveVDeiSHDuXxxtWMFpPRYSD2aMV1KZG0=; b=AhwVq9dJ0uBoioMFQhYuQSehHM5yZZ4AL9rV6gAR+Hfhe4Ippm69S9/GOh+Hfl/crF A7EFZzWb7ayMdua6DkxOIa9R8lK7/4sRWwXsBuNPjGxOADt/HUtYug5hJSmBBEDCx7iU ZBYgEQ0lqSI1aNDhn1X7n0VG4y9FPPVmpM8L6hsSQCOnn/OEOSZLinsRiklYIYiQExz7 KThuQr54iAGOGZJtUIjpz0bmARG1WeVn6jaYxG+LOYezX2m1+cFNMp494nLxsIasEjJ8 q0IY5fThwQJmllcNO7iOo7K2izhasNbcYovQz9d7sGNhNr1/AMbdlyyOpBGp1IcTPCvz cSrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734984821; x=1735589621; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Vj7WTgvEC5PveVDeiSHDuXxxtWMFpPRYSD2aMV1KZG0=; b=pLAJBUANQxgV2JCAH5Wqd6E0u8CGULbd41ki5msLuxrZecEa7zfLkfKcUrv3BxKg7F 1xsYtWKI6dT5rhMeUy/02Evs0zPaC5bd/wAI/fcPckm2jnHPlsUUt1edUdZ2okfjYVo0 VVFWc7P1iBq2r1oAHmIy7agyYK8oji+fZN92prcGcCBjmbHfFP5z4umkQmX4LHRWpKBD MEGLNGGN3zPLC3zCty9H2ceTArr212Js0otMtKeewjC9NhSc53sBc27sNs2oW3Gqswdc WpJljLEIF7kR0oUONuZDf4F56oUBpY7898aAai42+5lX0tnmNWjyhfTbUndqyniGJD+0 y9gw== X-Forwarded-Encrypted: i=1; AJvYcCW9b6XcQORG78phBI0vdNER87qB9jhXw80AKWESMBrL9DkHB0Uir8xvYW2BDbLyp6lU62oOPxJbP3X+OWg=@vger.kernel.org X-Gm-Message-State: AOJu0YwP4Xn3xuopmm1xffsfLB1CZ84q5ZwAsWy33D0s/Fn8JaNfisCv E74NLu84BUIAdeauDBTw1RrHMe5uDsMh6gpGjnBnJFLmKrk1M/QUn3N2APepjHxj3i+BwLaZsAE deye/nQ== X-Google-Smtp-Source: AGHT+IGkxGexGuNywu/k0KXqccYrQixwE39uyONt1ZNVUe9UCqKHxNa+Ck9RutuRSHwTduJeB9GIxlgPxMZP X-Received: from pgcl19.prod.google.com ([2002:a63:7013:0:b0:7fd:53c0:aac]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:670b:b0:1e0:d32f:24e2 with SMTP id adf61e73a8af0-1e5e081edf3mr24885139637.38.1734984820770; Mon, 23 Dec 2024 12:13:40 -0800 (PST) Date: Mon, 23 Dec 2024 20:13:33 +0000 In-Reply-To: <20241223-extended-modversions-v11-0-221d184ee9a7@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241223-extended-modversions-v11-0-221d184ee9a7@google.com> X-Mailer: b4 0.15-dev Message-ID: <20241223-extended-modversions-v11-4-221d184ee9a7@google.com> Subject: [PATCH v11 4/5] Documentation/kbuild: Document storage of symbol information From: Matthew Maurer To: Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Jonathan Corbet Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Matthew Maurer Document where exported and imported symbols are kept, format options, and limitations. Signed-off-by: Matthew Maurer --- Documentation/kbuild/modules.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst index 101de236cd0c9abe1f5684d80063ff3f9a7fc673..a42f00d8cb90ff6ee44677c1278287ef25a84c89 100644 --- a/Documentation/kbuild/modules.rst +++ b/Documentation/kbuild/modules.rst @@ -423,6 +423,26 @@ Symbols From the Kernel (vmlinux + modules) 1) It lists all exported symbols from vmlinux and all modules. 2) It lists the CRC if CONFIG_MODVERSIONS is enabled. +Version Information Formats +--------------------------- + + Exported symbols have information stored in __ksymtab or __ksymtab_gpl + sections. Symbol names and namespaces are stored in __ksymtab_strings, + using a format similar to the string table used for ELF. If + CONFIG_MODVERSIONS is enabled, the CRCs corresponding to exported + symbols will be added to the __kcrctab or __kcrctab_gpl. + + If CONFIG_BASIC_MODVERSIONS is enabled (default with + CONFIG_MODVERSIONS), imported symbols will have their symbol name and + CRC stored in the __versions section of the importing module. This + mode only supports symbols of length up to 64 bytes. + + If CONFIG_EXTENDED_MODVERSIONS is enabled (required to enable both + CONFIG_MODVERSIONS and CONFIG_RUST at the same time), imported symbols + will have their symbol name recorded in the __version_ext_names + section as a series of concatenated, null-terminated strings. CRCs for + these symbols will be recorded in the __version_ext_crcs section. + Symbols and External Modules ---------------------------- From patchwork Mon Dec 23 20:13:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13919218 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 138D71D6195 for ; Mon, 23 Dec 2024 20:13:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734984824; cv=none; b=pgDIc2fP6SrNl/8euwr1XiRsp49cpgegbma2pRkiLQvyHm5AscYea6wx0rr0RkuY4hTg6ndbufugY550n3NB/Gmd4K5xpW+lmj4phDDdxVx5VGoZQQ3F0SSkb6iMPJqfd5S4bJhohPTzJqDJIgIo4LJGAyopWuvhzA9zg1rJBgs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734984824; c=relaxed/simple; bh=OfARBdrSO25YJUBOotWgeA+0nMfy8zMZa0sJjFrf0fc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a6Mb/QIsN/T2a8Jbymlpc2AqIlcgFv2wtMfiokE1eD+x30dkf3ChLl5rRXPpVeFIyOPzj23jUxl4ZNU4gcEt94iJrI8Xwy55geio+frJWMhivcjFOi+mQcM/qUgJgUUkXt/ySkOct6kum7Jk1IVQF5MtkQxR5cE5UCtmiPrfUeY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RuY2nPko; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RuY2nPko" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21681a2c0d5so35670805ad.2 for ; Mon, 23 Dec 2024 12:13:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734984822; x=1735589622; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NnqTVWJzvIfhKg7Bjweyg4HBEOW+h34t+hptux0JJqo=; b=RuY2nPkolnQ7y6txezKQja6qlNpAZrG1CnfD5He0eqIYFdwojspi1qvlI43P7wznlh Hz9pTd75RYUtyLVqWfG377PO9nyXVgohXe4fOJ7Uq8zXXzvHRBbWo3QP2Zovo0vHFmeO PsNrUSziJnr7gn+edxUh7lc584++TLHf0/r2v89RIX/4ongPK6kKg1Py7syi2DMG1oMw zcbdMMcMB1C/8nJE/+sZqWbHbqSzP/pmeYCsG6+2sTp+oODs3tcunyXm7101GqNTStXz N4RTLHq/y21R5CfyXTYRetd/BUNUTzRuFqY0JB6rX1oIuMEeS477TFunYLNoRvTjsJYp RGJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734984822; x=1735589622; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NnqTVWJzvIfhKg7Bjweyg4HBEOW+h34t+hptux0JJqo=; b=Uoe6bN9lleifCle5MVewFcXW1BU6B5sWjJvFvAO6VjtBPL7rQzK97uRE2wO6rJ3tcM JuEYFg/OEYblXrY6/TV99kO1u2Rh70xo3HejtueGXckCs13QzgCtlfODT32Yx2A1STno l6endWEnlKZHRIkTgX02vfCe83THLEjf1vFSguvUPBSXGNTXdFMPNeJQxvTmw2lWhmdF AXkNBAHi618IPerjVe/CXE7ShSC5PbnFQ0AWAzhYD9nOHSPgSHM9OnMVAlR6CdBGLHpE bbA9ZHmulXunmlEfK8qCLebIXUZG379CoUBJhNAYLAiAVevkKt/2cHI/hGPs/Z3ljzVa L8Gg== X-Forwarded-Encrypted: i=1; AJvYcCW+reUvty8WeCZqsyvRupmNGpZtq0ViV7cB10MJwU6/HkC3QeXByQfW2jd51Z04GAfrhqk7QbS+kOEzLtg=@vger.kernel.org X-Gm-Message-State: AOJu0YwR1X82pif68yUkB/ziF+w0qUvbqL3bx3JS74n/FFx23AJ+cZQ/ 10xX9hc8vFPLsyCFPcbO7Lt0b6JlJw6rbO9Hz80iZJaLZufVjvJOAllBDv011zqtFYhrMYlwhXv 3IDoOzQ== X-Google-Smtp-Source: AGHT+IEgrY2jVNmxoprwdCtHCe+K+IEMEfsclKzGT/kTeJto8OaNi6B6DgjKUyGBg3iYFXCtXPVyl7pSvozj X-Received: from pfwy1.prod.google.com ([2002:a05:6a00:1c81:b0:727:3a40:52d7]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:ad3:b0:71d:f2e3:a878 with SMTP id d2e1a72fcca58-72abdd3c2afmr21164530b3a.5.1734984822301; Mon, 23 Dec 2024 12:13:42 -0800 (PST) Date: Mon, 23 Dec 2024 20:13:34 +0000 In-Reply-To: <20241223-extended-modversions-v11-0-221d184ee9a7@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241223-extended-modversions-v11-0-221d184ee9a7@google.com> X-Mailer: b4 0.15-dev Message-ID: <20241223-extended-modversions-v11-5-221d184ee9a7@google.com> Subject: [PATCH v11 5/5] rust: Use gendwarfksyms + extended modversions for CONFIG_MODVERSIONS From: Matthew Maurer To: Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Jonathan Corbet Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Matthew Maurer From: Sami Tolvanen Previously, two things stopped Rust from using MODVERSIONS: 1. Rust symbols are occasionally too long to be represented in the original versions table 2. Rust types cannot be properly hashed by the existing genksyms approach because: * Looking up type definitions in Rust is more complex than C * Type layout is potentially dependent on the compiler in Rust, not just the source type declaration. CONFIG_EXTENDED_MODVERSIONS addresses the first point, and CONFIG_GENDWARFKSYMS the second. If Rust wants to use MODVERSIONS, allow it to do so by selecting both features. Signed-off-by: Sami Tolvanen Co-developed-by: Matthew Maurer Signed-off-by: Matthew Maurer --- init/Kconfig | 3 ++- rust/Makefile | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/init/Kconfig b/init/Kconfig index c1f9eb3d5f2e892e977ba1425599502dc830f552..b60acfd9431e0ac2bf401ecb6523b5104ad31150 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1959,7 +1959,8 @@ config RUST bool "Rust support" depends on HAVE_RUST depends on RUST_IS_AVAILABLE - depends on !MODVERSIONS + select EXTENDED_MODVERSIONS if MODVERSIONS + depends on !MODVERSIONS || GENDWARFKSYMS depends on !GCC_PLUGIN_RANDSTRUCT depends on !RANDSTRUCT depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE diff --git a/rust/Makefile b/rust/Makefile index a40a3936126d603836e0ec9b42a1285916b60e45..80f970ad81f7989afe5ff2b5f633f50feb7f6006 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -329,10 +329,11 @@ $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; $(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE $(call if_changed_dep,bindgen) +rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__cfi/ { printf $(2),$(3) }' + quiet_cmd_exports = EXPORTS $@ cmd_exports = \ - $(NM) -p --defined-only $< \ - | awk '$$2~/(T|R|D|B)/ && $$3!~/__cfi/ {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@ + $(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3) > $@ $(obj)/exports_core_generated.h: $(obj)/core.o FORCE $(call if_changed,exports) @@ -401,11 +402,36 @@ ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),) __ashlti3 __lshrti3 endif +ifdef CONFIG_MODVERSIONS +cmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \ + $(call rust_exports,$@,"%s\n",$$3) | \ + scripts/gendwarfksyms/gendwarfksyms \ + $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \ + $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \ + $@ >> $(dot-target).cmd) +endif + define rule_rustc_library $(call cmd_and_fixdep,rustc_library) $(call cmd,gen_objtooldep) + $(call cmd,gendwarfksyms) endef +define rule_rust_cc_library + $(call if_changed_rule,cc_o_c) + $(call cmd,force_checksrc) + $(call cmd,gendwarfksyms) +endef + +# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol +# versions are calculated for the helpers too. +$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE + +$(call if_changed_rule,rust_cc_library) + +# Disable symbol versioning for exports.o to avoid conflicts with the actual +# symbol versions generated from Rust objects. +$(obj)/exports.o: private skip_gendwarfksyms = 1 + $(obj)/core.o: private skip_clippy = 1 $(obj)/core.o: private skip_flags = -Wunreachable_pub $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym)) @@ -417,13 +443,16 @@ ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),) $(obj)/core.o: scripts/target.json endif +$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1 $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*' $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE +$(call if_changed_rule,rustc_library) +$(obj)/build_error.o: private skip_gendwarfksyms = 1 $(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE +$(call if_changed_rule,rustc_library) +$(obj)/ffi.o: private skip_gendwarfksyms = 1 $(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE +$(call if_changed_rule,rustc_library) @@ -435,6 +464,7 @@ $(obj)/bindings.o: $(src)/bindings/lib.rs \ +$(call if_changed_rule,rustc_library) $(obj)/uapi.o: private rustc_target_flags = --extern ffi +$(obj)/uapi.o: private skip_gendwarfksyms = 1 $(obj)/uapi.o: $(src)/uapi/lib.rs \ $(obj)/ffi.o \ $(obj)/uapi/uapi_generated.rs FORCE