From nobody Sun May 10 10:32:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CCCD0C433EF for ; Thu, 5 May 2022 19:18:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384936AbiEETVo (ORCPT ); Thu, 5 May 2022 15:21:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1384925AbiEETVl (ORCPT ); Thu, 5 May 2022 15:21:41 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8533F4B848; Thu, 5 May 2022 12:18:01 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 46001B82F20; Thu, 5 May 2022 19:18:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F115C385A8; Thu, 5 May 2022 19:17:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651778279; bh=m3BSN+54NVRaC/iJQn28GUNDH+kVUNyA61dRWvK4XN8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mSTw/aIfNEqccH08hbSo/aXsI2VL0YAw4Ieu2Jzd0J2IjVPyWwbQY4di6KlSItYRo 3uP2EpdkeJ5+Ay0N818sifX2HJaxG22Y+ooDdybEMJLMV96SkOryXMShF+UxMxG56q dz8tMy85+vA8fAaEVapMSEDefhhZdCBtWA9Qioy0ACYzRBoIbd5C+LoJKU1mMQUJxh wCUhnPdWECkz85e2qX9L/qwcW8oNyLQ0EZaFSwK9yPg9RmQbW09KwT/TdsyPTyRQv9 bcMXEcL4q0vRL9A+JsROrs//3ijUaacn2qSWZ8TFnvqe+chGqjHBupb8LuYRfYqY0S 1SPjirRnGY5DQ== From: Miguel Ojeda To: Masahiro Yamada , Changbin Du , Nick Desaulniers , Nathan Chancellor , linux-kernel@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Boqun Feng Subject: [PATCH v1 1/3] kallsyms: avoid hardcoding the buffer size Date: Thu, 5 May 2022 21:16:43 +0200 Message-Id: <20220505191704.22812-2-ojeda@kernel.org> In-Reply-To: <20220505191704.22812-1-ojeda@kernel.org> References: <20220505191704.22812-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Boqun Feng This makes it easier to update the size later on. Furthermore, a static assert is added to ensure both are updated when that happens. The relationship used is one that keeps the new size (512+1) close to the original buffer size (500). Signed-off-by: Boqun Feng Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda Reviewed-by: Kees Cook --- scripts/kallsyms.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 8caabddf817c..880c4404731b 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -27,8 +27,18 @@ =20 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) =20 +#define _stringify_1(x) #x +#define _stringify(x) _stringify_1(x) + #define KSYM_NAME_LEN 128 =20 +/* A substantially bigger size than the current maximum. */ +#define KSYM_NAME_LEN_BUFFER 512 +_Static_assert( + KSYM_NAME_LEN_BUFFER =3D=3D KSYM_NAME_LEN * 4, + "Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN" +); + struct sym_entry { unsigned long long addr; unsigned int len; @@ -197,15 +207,15 @@ static void check_symbol_range(const char *sym, unsig= ned long long addr, =20 static struct sym_entry *read_symbol(FILE *in) { - char name[500], type; + char name[KSYM_NAME_LEN_BUFFER+1], type; unsigned long long addr; unsigned int len; struct sym_entry *sym; int rc; =20 - rc =3D fscanf(in, "%llx %c %499s\n", &addr, &type, name); + rc =3D fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &ad= dr, &type, name); if (rc !=3D 3) { - if (rc !=3D EOF && fgets(name, 500, in) =3D=3D NULL) + if (rc !=3D EOF && fgets(name, KSYM_NAME_LEN_BUFFER + 1, in) =3D=3D NULL) fprintf(stderr, "Read error or end of file.\n"); return NULL; } --=20 2.35.3 From nobody Sun May 10 10:32:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90CE8C433F5 for ; Thu, 5 May 2022 19:18:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344734AbiEETV5 (ORCPT ); Thu, 5 May 2022 15:21:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343951AbiEETVy (ORCPT ); Thu, 5 May 2022 15:21:54 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0676E50449; Thu, 5 May 2022 12:18:14 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 89D16B82F20; Thu, 5 May 2022 19:18:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 369B4C385B9; Thu, 5 May 2022 19:18:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651778291; bh=XLwv05HK4cqKvTg/DHDbRijSSxci4Q5RSO07SJNVPxY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=phYQZs1SssV/Z9+rPpcDM6mV2VGi0lgzVa17AlbhV/uhf5N6ioV9N0RmwflxhPAM1 ZUI0G8mEKuANJb09zMh030bcJJ4Ns2ARneVFptBKBX58hOdNLnhWBurFCPD/ZBKtOZ PcXAhQBN8bzGTVpWZCPNPvo04+XNWk8f1hdCReWPqv3mSbwLbQfnct0Toj1QK5Mec7 2v5SazcHgjChdPRFYpsWFMkY0fqs3w81PqNBpXSD0w8JhVguxQHL4UMZGceyCzQ5L3 RwPWESFyL5EkD8vPnp7helkNvkfLvmKH1Qasto4IFZsfMix0drJ/nhNl80lpjgFXgN bqcx0f1AZyjkQ== From: Miguel Ojeda To: Randy Dunlap , Song Liu , Kees Cook , Bixuan Cui , Alexei Starovoitov , Nick Desaulniers , David Vernet , Stephen Boyd , Jiri Olsa , Masahiro Yamada , Changbin Du , Nathan Chancellor , linux-kernel@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Boqun Feng , Matthew Wilcox Subject: [PATCH v1 2/3] kallsyms: support "big" kernel symbols Date: Thu, 5 May 2022 21:16:44 +0200 Message-Id: <20220505191704.22812-3-ojeda@kernel.org> In-Reply-To: <20220505191704.22812-1-ojeda@kernel.org> References: <20220505191704.22812-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Rust symbols can become quite long due to namespacing introduced by modules, types, traits, generics, etc. Increasing to 255 is not enough in some cases, and therefore we need to introduce longer lengths to the symbol table. In order to avoid increasing all lengths to 2 bytes (since most of them are small, including many Rust ones), we use ULEB128 to keep smaller symbols in 1 byte, with the rest in 2 bytes. Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Gary Guo Signed-off-by: Gary Guo Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Co-developed-by: Matthew Wilcox Signed-off-by: Matthew Wilcox Signed-off-by: Miguel Ojeda Reviewed-by: Kees Cook --- kernel/kallsyms.c | 26 ++++++++++++++++++++++---- scripts/kallsyms.c | 29 ++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 79f2eb617a62..e8d2262ef2d2 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -69,12 +69,20 @@ static unsigned int kallsyms_expand_symbol(unsigned int= off, data =3D &kallsyms_names[off]; len =3D *data; data++; + off++; + + /* If MSB is 1, it is a "big" symbol, so needs an additional byte. */ + if ((len & 0x80) !=3D 0) { + len =3D (len & 0x7F) | (*data << 7); + data++; + off++; + } =20 /* * Update the offset to return the offset for the next symbol on * the compressed stream. */ - off +=3D len + 1; + off +=3D len; =20 /* * For every byte on the compressed symbol data, copy the table @@ -127,7 +135,7 @@ static char kallsyms_get_symbol_type(unsigned int off) static unsigned int get_symbol_offset(unsigned long pos) { const u8 *name; - int i; + int i, len; =20 /* * Use the closest marker we have. We have markers every 256 positions, @@ -141,8 +149,18 @@ static unsigned int get_symbol_offset(unsigned long po= s) * so we just need to add the len to the current pointer for every * symbol we wish to skip. */ - for (i =3D 0; i < (pos & 0xFF); i++) - name =3D name + (*name) + 1; + for (i =3D 0; i < (pos & 0xFF); i++) { + len =3D *name; + + /* + * If MSB is 1, it is a "big" symbol, so we need to look into + * the next byte (and skip it, too). + */ + if ((len & 0x80) !=3D 0) + len =3D ((len & 0x7F) | (name[1] << 7)) + 1; + + name =3D name + len + 1; + } =20 return name - kallsyms_names; } diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 880c4404731b..c4e85eec2b4b 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -480,12 +480,35 @@ static void write_src(void) if ((i & 0xFF) =3D=3D 0) markers[i >> 8] =3D off; =20 - printf("\t.byte 0x%02x", table[i]->len); + /* There cannot be any symbol of length zero. */ + if (table[i]->len =3D=3D 0) { + fprintf(stderr, "kallsyms failure: " + "unexpected zero symbol length\n"); + exit(EXIT_FAILURE); + } + + /* Only lengths that fit in up-to-two-byte ULEB128 are supported. */ + if (table[i]->len > 0x3FFF) { + fprintf(stderr, "kallsyms failure: " + "unexpected huge symbol length\n"); + exit(EXIT_FAILURE); + } + + /* Encode length with ULEB128. */ + if (table[i]->len <=3D 0x7F) { + /* Most symbols use a single byte for the length. */ + printf("\t.byte 0x%02x", table[i]->len); + off +=3D table[i]->len + 1; + } else { + /* "Big" symbols use two bytes. */ + printf("\t.byte 0x%02x, 0x%02x", + (table[i]->len & 0x7F) | 0x80, + (table[i]->len >> 7) & 0x7F); + off +=3D table[i]->len + 2; + } for (k =3D 0; k < table[i]->len; k++) printf(", 0x%02x", table[i]->sym[k]); printf("\n"); - - off +=3D table[i]->len + 1; } printf("\n"); =20 --=20 2.35.3 From nobody Sun May 10 10:32:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BFA19C4332F for ; Thu, 5 May 2022 19:18:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384977AbiEETW0 (ORCPT ); Thu, 5 May 2022 15:22:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1384991AbiEETWU (ORCPT ); Thu, 5 May 2022 15:22:20 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2406527FF; Thu, 5 May 2022 12:18:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 84DD7B82F20; Thu, 5 May 2022 19:18:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D26AC385A4; Thu, 5 May 2022 19:18:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651778315; bh=b4na20hsT5ooewQtEeVBDpf7ktvKIX8Bkd9Cwxc5Fdo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ehq7P/Mk2srOHLiAvrZro1oPyRHE8jK83T71M14VMyYyEua18EAo5CBkIVdvCKTTV Tvx4soTEMk6Zj607XuBMs+g/26TAU3zftYNXzmMnyVGD+u8b6B4pKk1/kmBZACEXGs Jyho6kodSWIS5gm5pWTSa7Cl8jP2IYR3EIcu7y6Y9P4oo+Er7kVQKVkHjRhJOYaqxQ HNG+JfcTdviT1iaoaeqewagu7vk8IUvnJQuLK9NLAFoR3IR+3kkKllYwOnMR3KiuGv ir4gbXUrlnC0Mu5mdv7sv/pqd6+5OUlUekeBQfjKX0nQR9tyISUO0i7hVfQe1snrFe 4PVj0+W0aeTSg== From: Miguel Ojeda To: Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Andrew Morton , Sergey Senozhatsky , Kefeng Wang , Helge Deller , Stephen Boyd , Christophe Leroy , Masahiro Yamada , Nick Desaulniers , Changbin Du , Nathan Chancellor , "Gustavo A. R. Silva" , Andi Kleen , Kees Cook , Adrian Hunter , linux-kernel@vger.kernel.org, live-patching@vger.kernel.org, linux-perf-users@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Boqun Feng Subject: [PATCH v1 3/3] kallsyms: increase maximum kernel symbol length to 512 Date: Thu, 5 May 2022 21:16:45 +0200 Message-Id: <20220505191704.22812-4-ojeda@kernel.org> In-Reply-To: <20220505191704.22812-1-ojeda@kernel.org> References: <20220505191704.22812-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Rust symbols can become quite long due to namespacing introduced by modules, types, traits, generics, etc. For instance, the following code: pub mod my_module { pub struct MyType; pub struct MyGenericType(T); pub trait MyTrait { fn my_method() -> u32; } impl MyTrait for MyGenericType { fn my_method() -> u32 { 42 } } } generates a symbol of length 96 when using the upcoming v0 mangling scheme: _RNvXNtCshGpAVYOtgW1_7example9my_moduleINtB2_13MyGenericTypeNtB2_6MyTyp= eENtB2_7MyTrait9my_method At the moment, Rust symbols may reach up to 300 in length. Setting 512 as the maximum seems like a reasonable choice to keep some headroom. Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Gary Guo Signed-off-by: Gary Guo Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Signed-off-by: Miguel Ojeda Reviewed-by: Kees Cook Reviewed-by: Petr Mladek --- include/linux/kallsyms.h | 2 +- kernel/livepatch/core.c | 4 ++-- scripts/kallsyms.c | 4 ++-- tools/include/linux/kallsyms.h | 2 +- tools/lib/perf/include/perf/event.h | 2 +- tools/lib/symbol/kallsyms.h | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h index ce1bd2fbf23e..e5ad6e31697d 100644 --- a/include/linux/kallsyms.h +++ b/include/linux/kallsyms.h @@ -15,7 +15,7 @@ =20 #include =20 -#define KSYM_NAME_LEN 128 +#define KSYM_NAME_LEN 512 #define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s %s]") + \ (KSYM_NAME_LEN - 1) + \ 2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + \ diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index bc475e62279d..ec06ce59d728 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -213,7 +213,7 @@ static int klp_resolve_symbols(Elf_Shdr *sechdrs, const= char *strtab, * we use the smallest/strictest upper bound possible (56, based on * the current definition of MODULE_NAME_LEN) to prevent overflows. */ - BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN !=3D 128); + BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN !=3D 512); =20 relas =3D (Elf_Rela *) relasec->sh_addr; /* For each rela in this klp relocation section */ @@ -227,7 +227,7 @@ static int klp_resolve_symbols(Elf_Shdr *sechdrs, const= char *strtab, =20 /* Format: .klp.sym.sym_objname.sym_name,sympos */ cnt =3D sscanf(strtab + sym->st_name, - ".klp.sym.%55[^.].%127[^,],%lu", + ".klp.sym.%55[^.].%511[^,],%lu", sym_objname, sym_name, &sympos); if (cnt !=3D 3) { pr_err("symbol %s has an incorrectly formatted name\n", diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index c4e85eec2b4b..f9d07f9eb709 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -30,10 +30,10 @@ #define _stringify_1(x) #x #define _stringify(x) _stringify_1(x) =20 -#define KSYM_NAME_LEN 128 +#define KSYM_NAME_LEN 512 =20 /* A substantially bigger size than the current maximum. */ -#define KSYM_NAME_LEN_BUFFER 512 +#define KSYM_NAME_LEN_BUFFER 2048 _Static_assert( KSYM_NAME_LEN_BUFFER =3D=3D KSYM_NAME_LEN * 4, "Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN" diff --git a/tools/include/linux/kallsyms.h b/tools/include/linux/kallsyms.h index efb6c3f5f2a9..5a37ccbec54f 100644 --- a/tools/include/linux/kallsyms.h +++ b/tools/include/linux/kallsyms.h @@ -6,7 +6,7 @@ #include #include =20 -#define KSYM_NAME_LEN 128 +#define KSYM_NAME_LEN 512 =20 struct module; =20 diff --git a/tools/lib/perf/include/perf/event.h b/tools/lib/perf/include/p= erf/event.h index e7758707cadd..116a80c31675 100644 --- a/tools/lib/perf/include/perf/event.h +++ b/tools/lib/perf/include/perf/event.h @@ -95,7 +95,7 @@ struct perf_record_throttle { }; =20 #ifndef KSYM_NAME_LEN -#define KSYM_NAME_LEN 256 +#define KSYM_NAME_LEN 512 #endif =20 struct perf_record_ksymbol { diff --git a/tools/lib/symbol/kallsyms.h b/tools/lib/symbol/kallsyms.h index 72ab9870454b..542f9b059c3b 100644 --- a/tools/lib/symbol/kallsyms.h +++ b/tools/lib/symbol/kallsyms.h @@ -7,7 +7,7 @@ #include =20 #ifndef KSYM_NAME_LEN -#define KSYM_NAME_LEN 256 +#define KSYM_NAME_LEN 512 #endif =20 static inline u8 kallsyms2elf_binding(char type) --=20 2.35.3