From patchwork Fri Jan 3 07:30:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13925267 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 AC7001C82E2; Fri, 3 Jan 2025 07:30:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889455; cv=none; b=XU8KJLUAJjllhGK8i506ZZaGAIHkTqAsKjXPs2tCmkxOOtSpsbvRFJZNbm3IExHSspFLjtpTFFmFlmYqgSwOv30Vx6NGAx4wzhZ8PpODMo6lsR1fN8bWEvmtDj788m9NQ2DDE7BAu4UAoXSs5lsIs+c52GfUvOHMjHxR/+9vTQE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889455; c=relaxed/simple; bh=8d9RnOeLxBAYfLB453OSPY3ovNxvISADOh5P9Ux7SfE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=pw06yEw3p6SuOyxLPeRXlNy2bu+614BMUQKCltbkl3XRd86w3KTpwhoWMZW+6S1Or/3ff4tjVKNScj3sMWsl6fuwJCqrcoPnjbdYSF+l4eDs51pO7yKj0Wf223qXcerohw4qk6JAU5Ugu08KNRXZP2rmNSfqN4dJeDU6a8dtJlc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=G1FTYZ2x; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="G1FTYZ2x" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7762C4CECE; Fri, 3 Jan 2025 07:30:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735889455; bh=8d9RnOeLxBAYfLB453OSPY3ovNxvISADOh5P9Ux7SfE=; h=From:To:Cc:Subject:Date:From; b=G1FTYZ2xqVXzD/lOUPB4bzpuvMR2PBLPmCoBQGI28j4YUIf7rB4dVFYZ0NyySWXF/ eli4vpBdGb7p/U5duCuiNdhnjJHHriX7lBIp/+m4VlU2k1mriouKnhHHjv9JTkiN58 U7noPwXsR2uLqqeSYBIkVhuhSwhwN6gdKqE9p54z6AnX5luT6WAzoGSl4LbqZSrB+f Q+fTuKk+/bzV6DOeVWtmcTFOoWvO3bwc0ThaWc2SN4gD8SiBfgSxMFtWJqsLWjaD14 ahfIBT94ZQVw+wRoOlNX7/FapPuqS8BVZuugdbaKs8y3WajL7kn3c+6P3bBRbu+rZP Ja3qkU7UzixOQ== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Andreas Gruenbacher , Andrew Morton , Sam Ravnborg Subject: [PATCH 1/6] genksyms: fix memory leak when the same symbol is added from source Date: Fri, 3 Jan 2025 16:30:38 +0900 Message-ID: <20250103073046.2609911-1-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When a symbol that is already registered is added again, __add_symbol() returns without freeing the symbol definition, making it unreachable. The following test cases demonstrate different memory leak points. [Test Case 1] Forward declaration with exactly the same definition $ cat foo.c #include void foo(void); void foo(void) {} EXPORT_SYMBOL(foo); [Test Case 2] Forward declaration with a different definition (e.g. attribute) $ cat foo.c #include void foo(void); __attribute__((__section__(".ref.text"))) void foo(void) {} EXPORT_SYMBOL(foo); [Test Case 3] Preserving an overridden symbol (compile with KBUILD_PRESERVE=1) $ cat foo.c #include void foo(void); void foo(void) { } EXPORT_SYMBOL(foo); $ cat foo.symref override foo void foo ( int ) The memory leaks in Test Case 1 and 2 have existed since the introduction of genksyms into the kernel tree. [1] The memory leak in Test Case 3 was introduced by commit 5dae9a550a74 ("genksyms: allow to ignore symbol checksum changes"). When multiple init_declarators are reduced to an init_declarator_list, the decl_spec must be duplicated. Otherwise, the following Test Case 4 would result in a double-free bug. [Test Case 4] $ cat foo.c #include extern int foo, bar; int foo, bar; EXPORT_SYMBOL(foo); In this case, 'foo' and 'bar' share the same decl_spec, 'int'. It must be unshared before being passed to add_symbol(). [1]: https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git/commit/?id=46bd1da672d66ccd8a639d3c1f8a166048cca608 Fixes: 5dae9a550a74 ("genksyms: allow to ignore symbol checksum changes") Signed-off-by: Masahiro Yamada --- scripts/genksyms/genksyms.c | 3 +++ scripts/genksyms/parse.y | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c index 07f9b8cfb233..8ca46f807b57 100644 --- a/scripts/genksyms/genksyms.c +++ b/scripts/genksyms/genksyms.c @@ -239,6 +239,7 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, "unchanged\n"); } sym->is_declared = 1; + free_list(defn, NULL); return sym; } else if (!sym->is_declared) { if (sym->is_override && flag_preserve) { @@ -247,6 +248,7 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, print_type_name(type, name); fprintf(stderr, " modversion change\n"); sym->is_declared = 1; + free_list(defn, NULL); return sym; } else { status = is_unknown_symbol(sym) ? @@ -254,6 +256,7 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, } } else { error_with_pos("redefinition of %s", name); + free_list(defn, NULL); return sym; } break; diff --git a/scripts/genksyms/parse.y b/scripts/genksyms/parse.y index 8e9b5e69e8f0..840371d01bf4 100644 --- a/scripts/genksyms/parse.y +++ b/scripts/genksyms/parse.y @@ -152,14 +152,19 @@ simple_declaration: ; init_declarator_list_opt: - /* empty */ { $$ = NULL; } - | init_declarator_list + /* empty */ { $$ = NULL; } + | init_declarator_list { free_list(decl_spec, NULL); $$ = $1; } ; init_declarator_list: init_declarator { struct string_list *decl = *$1; *$1 = NULL; + + /* avoid sharing among multiple init_declarators */ + if (decl_spec) + decl_spec = copy_list_range(decl_spec, NULL); + add_symbol(current_name, is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern); current_name = NULL; @@ -170,6 +175,11 @@ init_declarator_list: *$3 = NULL; free_list(*$2, NULL); *$2 = decl_spec; + + /* avoid sharing among multiple init_declarators */ + if (decl_spec) + decl_spec = copy_list_range(decl_spec, NULL); + add_symbol(current_name, is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern); current_name = NULL; From patchwork Fri Jan 3 07:30:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13925268 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 78BD71B0F29; Fri, 3 Jan 2025 07:30:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889457; cv=none; b=DB8MtD2MEC6KvFIYZK4nAO+DOPuBBtYrALyhU6wyroE14dCt1Mi5UHfcWtGZgaoAp/tYIhBGuaIBPGWrn60R84H97b9zuw322j8aEFV20jYyZ29DAbLYKGoLUPHNp2gR5Dh/cMQXfhaL9tNmB33p27PDBDIUgE1znCg7LpotXps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889457; c=relaxed/simple; bh=Vd1RoZapGlgiKLwvIFiTdfFVnrMDlztoRQJAU9ZVH0Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bwx0ilX0NNCBJ7MY+ZTTCimd314trJSwjl8+11NCrOHJ41OtVYI/cZLyLzftmMkLE/SvsjhQHziH/Y9xSsN6RAdMiXgKdfN1VvoqEOlSx066X9KGIqDpumitLiTPsd2Wd2PiiWRF1uW5s/Q37gF0OlxZiaTJ7kevW7MD7+YGQ+c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ntuCP5vy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ntuCP5vy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4C7DC4CED7; Fri, 3 Jan 2025 07:30:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735889457; bh=Vd1RoZapGlgiKLwvIFiTdfFVnrMDlztoRQJAU9ZVH0Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ntuCP5vythvm2ea3DV4pIf3qyfLKJC7aUQpotxCOHZNncwfPfeOlrnStUXGCPJ7QC GoWDGRpPWI/Q3S3rdWCV4QK25OhCfLJZxZpQym3HnLgWB2kQvp7NuECLbVk07xrrm/ Hqqc900nInM3Ivp2cNUtRj3zmfwvr41MMLYpSRiXtg+Iiu4HcYeeoA6pt5VxCy2Y85 CAY05b4JFKWaDsLliSPVf8rkrXkjbf4omsiA0ojESoB20eefGWm7U8YYKM5u9aR05k TRJZ+UtPTvt077MKe4kG8bHpBjxvMOPAOcswoXKkfZD7BFgPyuW6FITgUYsREoxqoq akSniTiohLQcg== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Andreas Gruenbacher , Andrew Morton , Sam Ravnborg Subject: [PATCH 2/6] genksyms: fix memory leak when the same symbol is read from *.symref file Date: Fri, 3 Jan 2025 16:30:39 +0900 Message-ID: <20250103073046.2609911-2-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250103073046.2609911-1-masahiroy@kernel.org> References: <20250103073046.2609911-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When a symbol that is already registered is read again from *.symref file, __add_symbol() removes the previous one from the hash table without freeing it. [Test Case] $ cat foo.c #include void foo(void); void foo(void) {} EXPORT_SYMBOL(foo); $ cat foo.symref foo void foo ( void ) foo void foo ( void ) When a symbol is removed from the hash table, it must be freed along with its ->name and ->defn members. However, sym->name cannot be freed because it is sometimes shared with node->string, but not always. If sym->name and node->string share the same memory, free(sym->name) could lead to a double-free bug. To resolve this issue, always assign a strdup'ed string to sym->name. Fixes: 64e6c1e12372 ("genksyms: track symbol checksum changes") Signed-off-by: Masahiro Yamada --- scripts/genksyms/genksyms.c | 8 ++++++-- scripts/genksyms/genksyms.h | 2 +- scripts/genksyms/parse.y | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c index 8ca46f807b57..c5e8e0e0f949 100644 --- a/scripts/genksyms/genksyms.c +++ b/scripts/genksyms/genksyms.c @@ -272,11 +272,15 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, break; } } + + free_list(sym->defn, NULL); + free(sym->name); + free(sym); --nsyms; } sym = xmalloc(sizeof(*sym)); - sym->name = name; + sym->name = xstrdup(name); sym->type = type; sym->defn = defn; sym->expansion_trail = NULL; @@ -483,7 +487,7 @@ static void read_reference(FILE *f) defn = def; def = read_node(f); } - subsym = add_reference_symbol(xstrdup(sym->string), sym->tag, + subsym = add_reference_symbol(sym->string, sym->tag, defn, is_extern); subsym->is_override = is_override; free_node(sym); diff --git a/scripts/genksyms/genksyms.h b/scripts/genksyms/genksyms.h index 21ed2ec2d98c..5621533dcb8e 100644 --- a/scripts/genksyms/genksyms.h +++ b/scripts/genksyms/genksyms.h @@ -32,7 +32,7 @@ struct string_list { struct symbol { struct symbol *hash_next; - const char *name; + char *name; enum symbol_type type; struct string_list *defn; struct symbol *expansion_trail; diff --git a/scripts/genksyms/parse.y b/scripts/genksyms/parse.y index 840371d01bf4..689cb6bb40b6 100644 --- a/scripts/genksyms/parse.y +++ b/scripts/genksyms/parse.y @@ -482,12 +482,12 @@ enumerator_list: enumerator: IDENT { - const char *name = strdup((*$1)->string); + const char *name = (*$1)->string; add_symbol(name, SYM_ENUM_CONST, NULL, 0); } | IDENT '=' EXPRESSION_PHRASE { - const char *name = strdup((*$1)->string); + const char *name = (*$1)->string; struct string_list *expr = copy_list_range(*$3, *$2); add_symbol(name, SYM_ENUM_CONST, expr, 0); } From patchwork Fri Jan 3 07:30:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13925269 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 AA5041C6F55; Fri, 3 Jan 2025 07:30:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889458; cv=none; b=GQ12V9bBsuT9BdI/JRmIJJy/cSfbRj14nN+AWlTYRmJb47qN9SzTst++T7nbzAI7x08IUa7utqi6Zgu585eCMD4WECpkjAcgpfPQ7gOqpXvP/UlQFbTqTXZ58ArIGgJlQ145JhEeTJ8XMy8HTduuO9XoNMxj+OuHrl3P1r0XflI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889458; c=relaxed/simple; bh=ceAgZPjdW52rBKW/g8fE/outcPlNZN+bCyUt0Ua2z74=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aocDC6IRqxFLZTM7sTeTvzeZg3N4QXy6QOLgfx/JVfDKDD1TMRn5QaR+Ty41BnvYcc/Y5tPz1ntbffPrXRhRYE/qoOy8oK6cJRGO3nwGPqVHPX+JEWbVE4BQkbtNXuzrh14COgSZKP+mniXEDRtn5/yM9hlDKn/O87YPCXrrcQ4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TFYMxrea; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TFYMxrea" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8498BC4CEDC; Fri, 3 Jan 2025 07:30:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735889458; bh=ceAgZPjdW52rBKW/g8fE/outcPlNZN+bCyUt0Ua2z74=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TFYMxrea5qNCatvvNALjRczIoRsI0v2RUAXDgKjNZQbCdLGbLn3bov0Jgbbc2Q5Z9 H+JZ7tU2Z741MQiPvbD7TFHDF9v3y0cp7/Od3JMpmef4pkRcnoVqM4vx7MFMzDSuMC gEVA/h3wOMkq/CTWWFkjfnzrPCWT319lvOe1y0Vp7aTPh7+rLbmM704mdZSeebMRJK 9D3ghTxmudur4KL5VJMpk84FGCTbUtHu3Of5VsftLYqtHi2DIUc+9V1+NKwFpWT+fa v4/jgaytkOakOkEJwPPzYfqdpFxqie9AopqSJHKVE4eqjYClw03OPu8vJizAsZHx3b A/8ZETNkRerUg== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada Subject: [PATCH 3/6] genksyms: reduce the indentation in the for-loop in __add_symbol() Date: Fri, 3 Jan 2025 16:30:40 +0900 Message-ID: <20250103073046.2609911-3-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250103073046.2609911-1-masahiroy@kernel.org> References: <20250103073046.2609911-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To improve readability, reduce the indentation as follows: - Use 'continue' earlier when the symbol does not match - flip !sym->is_declared to flatten the if-else chain No functional changes are intended. Signed-off-by: Masahiro Yamada --- scripts/genksyms/genksyms.c | 63 ++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c index c5e8e0e0f949..5a90acd693f4 100644 --- a/scripts/genksyms/genksyms.c +++ b/scripts/genksyms/genksyms.c @@ -226,41 +226,38 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, h = crc32(name) % HASH_BUCKETS; for (sym = symtab[h]; sym; sym = sym->hash_next) { - if (map_to_ns(sym->type) == map_to_ns(type) && - strcmp(name, sym->name) == 0) { - if (is_reference) - /* fall through */ ; - else if (sym->type == type && - equal_list(sym->defn, defn)) { - if (!sym->is_declared && sym->is_override) { - print_location(); - print_type_name(type, name); - fprintf(stderr, " modversion is " - "unchanged\n"); - } - sym->is_declared = 1; - free_list(defn, NULL); - return sym; - } else if (!sym->is_declared) { - if (sym->is_override && flag_preserve) { - print_location(); - fprintf(stderr, "ignoring "); - print_type_name(type, name); - fprintf(stderr, " modversion change\n"); - sym->is_declared = 1; - free_list(defn, NULL); - return sym; - } else { - status = is_unknown_symbol(sym) ? - STATUS_DEFINED : STATUS_MODIFIED; - } - } else { - error_with_pos("redefinition of %s", name); - free_list(defn, NULL); - return sym; + if (map_to_ns(sym->type) != map_to_ns(type) || + strcmp(name, sym->name)) + continue; + + if (is_reference) { + /* fall through */ ; + } else if (sym->type == type && equal_list(sym->defn, defn)) { + if (!sym->is_declared && sym->is_override) { + print_location(); + print_type_name(type, name); + fprintf(stderr, " modversion is unchanged\n"); } - break; + sym->is_declared = 1; + free_list(defn, NULL); + return sym; + } else if (sym->is_declared) { + error_with_pos("redefinition of %s", name); + free_list(defn, NULL); + return sym; + } else if (sym->is_override && flag_preserve) { + print_location(); + fprintf(stderr, "ignoring "); + print_type_name(type, name); + fprintf(stderr, " modversion change\n"); + sym->is_declared = 1; + free_list(defn, NULL); + return sym; + } else { + status = is_unknown_symbol(sym) ? + STATUS_DEFINED : STATUS_MODIFIED; } + break; } if (sym) { From patchwork Fri Jan 3 07:30:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13925270 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 F08101B87ED; Fri, 3 Jan 2025 07:30:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889460; cv=none; b=Xonah1157nE8GQHkYDRO3JdyR580xZRrcTZgOmlxpdXantQSEqkyymIBwLb8utSu0NzgTAhdeqf0Yj+09QzGIsvKJTBHrZ4ycTQcgFeY+wjafhdW9+fNmecD8ZyUyenUthhBTa55Smh+4aj02n3OyKAoiMuzuMe5iA0gmQ0nc7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889460; c=relaxed/simple; bh=JsaGkekCLjToMuSIlVwobgva6JVGh4RdbrOKVtmuIc8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lNzF+UilzGYL3EOZ0iSOUoU444obKrI8wCliTFDvycU9M72eZ/xOv8Or1Kxt+sOyC2Sl5yYu5jyOAEfH+ZOuS967fxzNMk/nA1rsMJAbETvXzL3YOqwkLhndQ5Sjxpi2FEdIJI5bUbWkGdCAUPHxiSUmdOEC/EVZjpq14WdUh38= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fpg5Lj5r; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fpg5Lj5r" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5E74C4CED7; Fri, 3 Jan 2025 07:30:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735889459; bh=JsaGkekCLjToMuSIlVwobgva6JVGh4RdbrOKVtmuIc8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fpg5Lj5ribWKyGpbXfB6zdra5coYSLYiSF9NeseAN5IXhzJl1aalymjrPxGqr0GUW W30I89GOIRJCeieU9Xe2rmP1kyZeee64PacoDUWlkj7PjD97zyrC58yn1fCvP3vI6k F9g8A+xHoGJeXsIM/Kiz5GRT6yQKWWQdHY7HRbCehEn3MXmmnIa1EGrb+YJZdxnlho D1KLLGdu+NrXUF6mkyORGWuCismadk8lJBfjEkHsPT+fmaJIwlqFnT1CCe2EtV7BOO C9i3gBQBHMVqPzcdm2lHh1e6I0sjVorIPz7QJp5YZybwkQRVT1tzHWk7CczGGdk/Kf oA3PBhqdeVn5A== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada Subject: [PATCH 4/6] genksyms: refactor the return points in the for-loop in __add_symbol() Date: Fri, 3 Jan 2025 16:30:41 +0900 Message-ID: <20250103073046.2609911-4-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250103073046.2609911-1-masahiroy@kernel.org> References: <20250103073046.2609911-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 free_list() must be called before returning from this for-loop. Swap 'break' and the combination of free_list() and 'return'. This reduces the code and minimizes the risk of introducing memory leaks in future changes. Signed-off-by: Masahiro Yamada --- scripts/genksyms/genksyms.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c index 5a90acd693f4..41d6cfce0088 100644 --- a/scripts/genksyms/genksyms.c +++ b/scripts/genksyms/genksyms.c @@ -231,7 +231,7 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, continue; if (is_reference) { - /* fall through */ ; + break; } else if (sym->type == type && equal_list(sym->defn, defn)) { if (!sym->is_declared && sym->is_override) { print_location(); @@ -239,25 +239,21 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, fprintf(stderr, " modversion is unchanged\n"); } sym->is_declared = 1; - free_list(defn, NULL); - return sym; } else if (sym->is_declared) { error_with_pos("redefinition of %s", name); - free_list(defn, NULL); - return sym; } else if (sym->is_override && flag_preserve) { print_location(); fprintf(stderr, "ignoring "); print_type_name(type, name); fprintf(stderr, " modversion change\n"); sym->is_declared = 1; - free_list(defn, NULL); - return sym; } else { status = is_unknown_symbol(sym) ? STATUS_DEFINED : STATUS_MODIFIED; + break; } - break; + free_list(defn, NULL); + return sym; } if (sym) { From patchwork Fri Jan 3 07:30:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13925271 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 157A61CCEF8; Fri, 3 Jan 2025 07:31:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889461; cv=none; b=gqRzzK9LfEGQHg5E82aQM8RptvBqj/9bGIo/hW+LUVuyTizSM0zIpO39eU7fCVtSCTKd4BUu2VKtKpOkmNS9YzZH0MGJaYr18fbaZ1E6cpPGUkzVBN/oy1yERnzqWeJUeF45J8wzvphAbGRrfrHmngOYJoULIev15q/E+Q6efpY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889461; c=relaxed/simple; bh=SFiUna5KUZn58JKpA4mHDy0Sh8iLZ8a7ZHUX599JidU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n3YExQ3fES21Khcxl09enSVkyHs8Pgqj2E1HzLhneRH0wOT7NrT8vdItsR1MDIzG6vne0wuB4svoXF0fQMTh3gnA4Of66JpoTpBr/xT4qzAam739oceXlnwZrReHqQvswIfzcvE0M9nB2MdYAqVeUKIpBN2uoI6f4nlNRkMIFuw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lJmaT0KI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lJmaT0KI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC3B8C4CECE; Fri, 3 Jan 2025 07:30:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735889460; bh=SFiUna5KUZn58JKpA4mHDy0Sh8iLZ8a7ZHUX599JidU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lJmaT0KIkPTYEipuaNbr8gi5hCuQbQSKyXu+G2X8HemXN7dHHr//kP6LrQf8mKBBG 9C/iyGaGYHDmhM3iGb0+z7MCdWA7cwRqYmB8QoFkt2b6wP3k4TZbNTJMFa17h5YEZJ Li1uMyChpuRSP954fV/2H1avBepEUD7LuFZQJVzce9pgte9XStdLD60Hy5eQxJA7ln xP2xvUWnvXY+jezfXDMC2YPiJcE4S3a1y+eqBwc+NWiYBfKyDxSdPZ79G2P7eiykt0 jcuuEhkIvm9nPHkpQ6VGoZRqNkTWyJGIcVtBNR4E9MFW+cczysE/2cMbVSns2/lD1d b+bYH0Ln6l06g== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada Subject: [PATCH 5/6] genksyms: use generic macros for hash table implementation Date: Fri, 3 Jan 2025 16:30:42 +0900 Message-ID: <20250103073046.2609911-5-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250103073046.2609911-1-masahiroy@kernel.org> References: <20250103073046.2609911-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use macros provided by hashtable.h Signed-off-by: Masahiro Yamada --- scripts/genksyms/genksyms.c | 32 ++++++++++++-------------------- scripts/genksyms/genksyms.h | 4 +++- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c index 41d6cfce0088..e2cd3dcb469f 100644 --- a/scripts/genksyms/genksyms.c +++ b/scripts/genksyms/genksyms.c @@ -18,12 +18,12 @@ #include #include +#include + #include "genksyms.h" /*----------------------------------------------------------------------*/ -#define HASH_BUCKETS 4096 - -static struct symbol *symtab[HASH_BUCKETS]; +static HASHTABLE_DEFINE(symbol_hashtable, 1U << 12); static FILE *debugfile; int cur_line = 1; @@ -151,14 +151,14 @@ static enum symbol_type map_to_ns(enum symbol_type t) struct symbol *find_symbol(const char *name, enum symbol_type ns, int exact) { - unsigned long h = crc32(name) % HASH_BUCKETS; struct symbol *sym; - for (sym = symtab[h]; sym; sym = sym->hash_next) + hash_for_each_possible(symbol_hashtable, sym, hnode, crc32(name)) { if (map_to_ns(sym->type) == map_to_ns(ns) && strcmp(name, sym->name) == 0 && sym->is_declared) break; + } if (exact && sym && sym->type != ns) return NULL; @@ -224,8 +224,8 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, return NULL; } - h = crc32(name) % HASH_BUCKETS; - for (sym = symtab[h]; sym; sym = sym->hash_next) { + h = crc32(name); + hash_for_each_possible(symbol_hashtable, sym, hnode, h) { if (map_to_ns(sym->type) != map_to_ns(type) || strcmp(name, sym->name)) continue; @@ -257,14 +257,7 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, } if (sym) { - struct symbol **psym; - - for (psym = &symtab[h]; *psym; psym = &(*psym)->hash_next) { - if (*psym == sym) { - *psym = sym->hash_next; - break; - } - } + hash_del(&sym->hnode); free_list(sym->defn, NULL); free(sym->name); @@ -280,8 +273,7 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, sym->visited = NULL; sym->is_extern = is_extern; - sym->hash_next = symtab[h]; - symtab[h] = sym; + hash_add(symbol_hashtable, &sym->hnode, h); sym->is_declared = !is_reference; sym->status = status; @@ -832,9 +824,9 @@ int main(int argc, char **argv) } if (flag_debug) { - fprintf(debugfile, "Hash table occupancy %d/%d = %g\n", - nsyms, HASH_BUCKETS, - (double)nsyms / (double)HASH_BUCKETS); + fprintf(debugfile, "Hash table occupancy %d/%zd = %g\n", + nsyms, HASH_SIZE(symbol_hashtable), + (double)nsyms / HASH_SIZE(symbol_hashtable)); } if (dumpfile) diff --git a/scripts/genksyms/genksyms.h b/scripts/genksyms/genksyms.h index 5621533dcb8e..8c45ada59ece 100644 --- a/scripts/genksyms/genksyms.h +++ b/scripts/genksyms/genksyms.h @@ -14,6 +14,8 @@ #include +#include + enum symbol_type { SYM_NORMAL, SYM_TYPEDEF, SYM_ENUM, SYM_STRUCT, SYM_UNION, SYM_ENUM_CONST @@ -31,7 +33,7 @@ struct string_list { }; struct symbol { - struct symbol *hash_next; + struct hlist_node hnode; char *name; enum symbol_type type; struct string_list *defn; From patchwork Fri Jan 3 07:30:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13925272 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 7B39B1CD1F6; Fri, 3 Jan 2025 07:31:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889462; cv=none; b=X2X9pFGO88HmN9OEVlTheRGpt3CxLuiDHud4aiPOn2eyHI9FxeSsCi5l2v7lxTBogA/C+3HQM29Qogfr0DB+G8do7cH/uKBDPVryK5RE5EdB1Gd/itTQETl02tNFwoi1jc+kHwe2n64sS+w23Fs5m+fkFO8JPrc2bCz12rj2Pho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889462; c=relaxed/simple; bh=kg7X+7DNKFWqUvd+B+HYfuLNgqbOgSFSu7r41VUpKWU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hhXsFKqz+7NYl7PeaJZXiu51dksrHglCgUR/mabGjdtZnM+aCS6CJEv4ABs5TUDDXuRr5WP8RuirK0vKH4OiPGJjJUwNCJthL2N5HuPmZ4ozfKwzKtX6d8AiTjqVc8bGHHbwdTw4FNdxbp5Q8dy9x/wWDZNldCe8fm+KmdCkdz4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iueCJK3B; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iueCJK3B" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 486EFC4CED7; Fri, 3 Jan 2025 07:31:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735889462; bh=kg7X+7DNKFWqUvd+B+HYfuLNgqbOgSFSu7r41VUpKWU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iueCJK3B2fF/Fokbefa+7TtsbuoXpPTCBLjofILLkE/u8CZsUrCUzSWp+B2cGjJEI QlNekjFK313xyNdCjnseub0/xYyRXXadajMoPIRb6tQw1zY5pz12LEpE02pM6hPJ1s 1en6UvNgS+0AhLMW7K59xW127J8DxyGCOmFM7+mOXBgjW21yBGYaQn4UCwJ/OrqX9M vielj/QnJU/mWBGSi3Wr9410dRI5RulzerAwWoaNtkfsO0AWvZtS07fppvjlDadvrG 1JSwDKmns14cUiymS0jkXLcLS2LON8aTiVlJS2HI9HGyydxUa3iagMbTLdr/M9vB2+ CPCqBvBoAljMw== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada Subject: [PATCH 6/6] genksyms: use uint32_t instead of unsigned long for calculating CRC Date: Fri, 3 Jan 2025 16:30:43 +0900 Message-ID: <20250103073046.2609911-6-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250103073046.2609911-1-masahiroy@kernel.org> References: <20250103073046.2609911-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently, 'unsigned long' is used for intermediate variables when calculating CRCs. The size of 'long' differs depending on the architecture: it is 32 bits on 32-bit architectures and 64 bits on 64-bit architectures. The CRC values generated by genksyms represent the compatibility of exported symbols. Therefore, reproducibility is important. In other words, we need to ensure that the output is the same when the kernel source is identical, regardless of whether genksyms is running on a 32-bit or 64-bit build machine. Fortunately, the output from genksyms is not affected by the build machine's architecture because only the lower 32 bits of the 'unsigned long' variables are used. To make it even clearer that the CRC calculation is independent of the build machine's architecture, this commit explicitly uses the fixed-width type, uint32_t. Signed-off-by: Masahiro Yamada --- scripts/genksyms/genksyms.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c index e2cd3dcb469f..8b0d7ac73dbb 100644 --- a/scripts/genksyms/genksyms.c +++ b/scripts/genksyms/genksyms.c @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -60,7 +61,7 @@ static void print_type_name(enum symbol_type type, const char *name); /*----------------------------------------------------------------------*/ -static const unsigned int crctab32[] = { +static const uint32_t crctab32[] = { 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U, 0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U, 0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U, @@ -115,19 +116,19 @@ static const unsigned int crctab32[] = { 0x2d02ef8dU }; -static unsigned long partial_crc32_one(unsigned char c, unsigned long crc) +static uint32_t partial_crc32_one(uint8_t c, uint32_t crc) { return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8); } -static unsigned long partial_crc32(const char *s, unsigned long crc) +static uint32_t partial_crc32(const char *s, uint32_t crc) { while (*s) crc = partial_crc32_one(*s++, crc); return crc; } -static unsigned long crc32(const char *s) +static uint32_t crc32(const char *s) { return partial_crc32(s, 0xffffffff) ^ 0xffffffff; } @@ -517,7 +518,7 @@ static void print_list(FILE * f, struct string_list *list) } } -static unsigned long expand_and_crc_sym(struct symbol *sym, unsigned long crc) +static uint32_t expand_and_crc_sym(struct symbol *sym, uint32_t crc) { struct string_list *list = sym->defn; struct string_list **e, **b; @@ -624,7 +625,7 @@ static unsigned long expand_and_crc_sym(struct symbol *sym, unsigned long crc) void export_symbol(const char *name) { struct symbol *sym; - unsigned long crc; + uint32_t crc; int has_changed = 0; sym = find_symbol(name, SYM_NORMAL, 0); @@ -672,7 +673,7 @@ void export_symbol(const char *name) if (flag_dump_defs) fputs(">\n", debugfile); - printf("#SYMVER %s 0x%08lx\n", name, crc); + printf("#SYMVER %s 0x%08lx\n", name, (unsigned long)crc); } /*----------------------------------------------------------------------*/