The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Florian Schmaus <florian.schmaus@codasip.com>
To: Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>
Cc: Andrew Jones <ajones@ventanamicro.com>,
	Samuel Holland <samuel.holland@sifive.com>,
	Miaoqian Lin <linmq006@gmail.com>,
	Sean Chang <seanwascoding@gmail.com>,
	linux-riscv@lists.infradead.or, linux-kernel@vger.kernel.org,
	Florian Schmaus <florian.schmaus@codasip.com>
Subject: [PATCH] riscv: module: Use scope-based resource management for scratch buf
Date: Tue, 12 May 2026 08:33:59 +0200	[thread overview]
Message-ID: <20260512063359.713142-1-florian.schmaus@codasip.com> (raw)

The scratch buffer in module_frob_arch_sections() is currently managed
manually, requiring explicit calls to kvfree().

Convert it to use scope-based resource management via the
__free(kvfree) attribute. This automatically guarantees that the
buffer is freed when it goes out of scope, preventing potential memory
leaks. Most notably, it simplifies the kvrealloc() error path by
removing the need to manually free the original buffer before
returning -ENOMEM.

Signed-off-by: Florian Schmaus <florian.schmaus@codasip.com>
---
 arch/riscv/kernel/module-sections.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/arch/riscv/kernel/module-sections.c b/arch/riscv/kernel/module-sections.c
index b3b11b7f7ed9..ed3be8be377e 100644
--- a/arch/riscv/kernel/module-sections.c
+++ b/arch/riscv/kernel/module-sections.c
@@ -116,7 +116,7 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
 	size_t num_scratch_relas = 0;
 	unsigned int num_plts = 0;
 	unsigned int num_gots = 0;
-	Elf_Rela *scratch = NULL;
+	Elf_Rela *scratch __free(kvfree) = NULL;
 	Elf_Rela *new_scratch;
 	size_t scratch_size = 0;
 	int i;
@@ -168,10 +168,9 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
 		if (scratch_size_needed > scratch_size) {
 			scratch_size = scratch_size_needed;
 			new_scratch = kvrealloc(scratch, scratch_size, GFP_KERNEL);
-			if (!new_scratch) {
-				kvfree(scratch);
+			if (!new_scratch)
 				return -ENOMEM;
-			}
+
 			scratch = new_scratch;
 		}
 
@@ -184,7 +183,6 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
 		/* sort the accumulated PLT/GOT relocations so duplicates are adjacent */
 		sort(scratch, num_scratch_relas, sizeof(*scratch), cmp_rela, NULL);
 		count_max_entries(scratch, num_scratch_relas, &num_plts, &num_gots);
-		kvfree(scratch);
 	}
 
 	mod->arch.plt.shdr->sh_type = SHT_NOBITS;
-- 
2.53.0


                 reply	other threads:[~2026-05-12  6:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260512063359.713142-1-florian.schmaus@codasip.com \
    --to=florian.schmaus@codasip.com \
    --cc=ajones@ventanamicro.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=linmq006@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.or \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=samuel.holland@sifive.com \
    --cc=seanwascoding@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox