mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@kernel.org>
To: Dylan Hatch <dylanbhatch@google.com>
Cc: x86@kernel.org, linux-kernel@vger.kernel.org,
	 live-patching@vger.kernel.org,
	Peter Zijlstra <peterz@infradead.org>,
	 Joe Lawrence <joe.lawrence@redhat.com>,
	Miroslav Benes <mbenes@suse.cz>, Petr Mladek <pmladek@suse.com>,
	 Song Liu <song@kernel.org>
Subject: Re: [PATCH v3 8/9] objtool/klp: Fix relocations for EXPORT_SYMBOL_FOR_MODULES() symbols
Date: Fri, 14 Aug 2026 18:25:33 -0700	[thread overview]
Message-ID: <an-_y1AuR2DUkcSu@jpoimboe> (raw)
In-Reply-To: <an-wHwTNX1xDxlRC@jpoimboe>

On Fri, Aug 14, 2026 at 05:18:19PM -0700, Josh Poimboeuf wrote:
> On Fri, Aug 14, 2026 at 02:05:48PM -0700, Dylan Hatch wrote:
> > Following up on the other thread [1], I noticed that when a patch is
> > touching a module function with a reference to one of these
> > module-exported symbols, the patch/module is rejected because KLP
> > relocs referencing vmlinux symbols are not allowed from
> > module-specific livepatch relocation sections. I was able to reproduce
> > this with a simple module/livepatch combo that depends on one of these
> > symbols [2] (see samples/livepatch/testmod.c and test.patch):
> > 
> > root@debian-vm:~$ insmod livepatch-test.ko
> > root@debian-vm:~$ insmod testmod.ko
> > insmod: ERROR: could not insert module testmod.ko: Invalid parameters
> > 
> > With dmesg:
> > [  655.596876] livepatch_test: loading out-of-tree module taints kernel.
> > [  655.600961] livepatch_test: tainting kernel with TAINT_LIVEPATCH
> > [  655.605436] livepatch: enabling patch 'livepatch_test'
> > [  655.609119] livepatch: 'livepatch_test': starting patching transition
> > [  656.653454] livepatch: 'livepatch_test': patching complete
> > [  738.777872] livepatch: invalid access to vmlinux symbol
> > 'get_task_policy' from module-specific livepatch relocation section
> > [  738.784899] livepatch: failed to initialize patch 'livepatch_test'
> > for module 'testmod' (-22)
> > [  738.790371] livepatch: patch 'livepatch_test' failed for module
> > 'testmod', refusing to load module 'testmod'
> > 
> > Do you recommend a strategy for working around this, or is this
> > something that would have to be fixed in the kernel?
> 
> Ah, this is another tooling issue, let me work up a patch.

Here is an untested diff, I'll post a proper patch once I get a chance
to test it.

diff --git a/tools/objtool/include/objtool/klp.h b/tools/objtool/include/objtool/klp.h
index 646d8e1f12eff..c57775d78c71e 100644
--- a/tools/objtool/include/objtool/klp.h
+++ b/tools/objtool/include/objtool/klp.h
@@ -20,8 +20,9 @@
  * SHF_RELA_LIVEPATCH, nor does it support having two RELA sections for a
  * single PROGBITS section.
  *
- * "objname" is the name of the object being patched ("vmlinux" or a module
- * name).  post-link uses it to name the resulting
+ * "objname" is the object whose loading gates the relocation: "vmlinux" for
+ * references to vmlinux symbols, otherwise the name of the module being
+ * patched.  post-link uses it to name the resulting
  * .klp.rela.objname.section_name sections.
  */
 #define KLP_RELOCS_SEC	"__klp_relocs"
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index a66049e0726a6..16681a76f13d0 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1344,13 +1344,14 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
 			   struct section *sec, unsigned long offset,
 			   struct export *export)
 {
+	const char *sym_modname, *sym_orig_name, *sec_objname;
 	struct symbol *patched_sym = patched_reloc->sym;
 	s64 addend = reloc_addend(patched_reloc);
-	const char *sym_modname, *sym_orig_name;
-	static struct section *klp_relocs;
 	char tombstone_name[SYM_NAME_LEN];
 	struct symbol *sym, *klp_sym;
 	unsigned long klp_reloc_off;
+	struct section *klp_relocs;
+	char sec_name[SEC_NAME_LEN];
 	char sym_name[SYM_NAME_LEN];
 	struct klp_reloc klp_reloc;
 	unsigned long sympos;
@@ -1441,20 +1442,28 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
 	 * This intermediate step is necessary to prevent corruption by the
 	 * linker, which doesn't know how to properly handle two rela sections
 	 * applying to the same base section.
+	 *
+	 * The objname decides when the reloc gets applied.  A reference to a
+	 * vmlinux symbol goes in the vmlinux section so it gets applied when
+	 * the patch module loads.  Everything else goes in the patched
+	 * object's section, applied when the patched module is loaded.
 	 */
 
+	if (!strcmp(sym_modname, "vmlinux")) {
+		sec_objname = "vmlinux";
+	} else {
+		sec_objname = find_modname(e);
+		if (!sec_objname)
+			return -1;
+	}
+
+	/* section format: __klp_relocs.objname */
+	if (snprintf_check(sec_name, SEC_NAME_LEN,
+			   KLP_RELOCS_SEC ".%s", sec_objname))
+		return -1;
+
+	klp_relocs = find_section_by_name(e->out, sec_name);
 	if (!klp_relocs) {
-		const char *objname = find_modname(e);
-		char sec_name[SEC_NAME_LEN];
-
-		if (!objname)
-			return -1;
-
-		/* section format: __klp_relocs.objname */
-		if (snprintf_check(sec_name, SEC_NAME_LEN,
-				   KLP_RELOCS_SEC ".%s", objname))
-			return -1;
-
 		klp_relocs = elf_create_section(e->out, sec_name, 0,
 						0, SHT_PROGBITS, 8, SHF_ALLOC);
 		if (!klp_relocs)

  reply	other threads:[~2026-08-15  1:25 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-07 21:37 [PATCH v3 0/9] objtool/klp: sympos/module/alternative/etc fixes Josh Poimboeuf
2026-08-07 21:37 ` [PATCH v3 1/9] objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol symbols Josh Poimboeuf
2026-08-13 11:16   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2026-08-07 21:37 ` [PATCH v3 2/9] objtool/klp: Fix size of empty special section entries Josh Poimboeuf
2026-08-13 11:16   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2026-08-07 21:37 ` [PATCH v3 3/9] objtool/klp: Ignore replacement offset of empty x86 alternatives Josh Poimboeuf
2026-08-13 11:16   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2026-08-07 21:37 ` [PATCH v3 4/9] objtool/klp: Explicitly disallow patching or referencing init code/data Josh Poimboeuf
2026-08-11 22:32   ` Song Liu
2026-08-13 11:16   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2026-08-07 21:37 ` [PATCH v3 5/9] objtool/klp: Fix cross-module klp relocation section naming Josh Poimboeuf
2026-08-13 11:16   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2026-08-07 21:37 ` [PATCH v3 6/9] objtool/klp: Don't match local symbols against exports Josh Poimboeuf
2026-08-07 21:46   ` sashiko-bot
2026-08-08 22:27     ` Josh Poimboeuf
2026-08-13 11:16   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2026-08-07 21:37 ` [PATCH v3 7/9] objtool/klp: Allow new references to module exports Josh Poimboeuf
2026-08-13 11:16   ` [tip: objtool/core] " tip-bot2 for Joe Lawrence
2026-08-07 21:37 ` [PATCH v3 8/9] objtool/klp: Fix relocations for EXPORT_SYMBOL_FOR_MODULES() symbols Josh Poimboeuf
2026-08-11 22:36   ` Song Liu
2026-08-13 11:16   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2026-08-14 21:05   ` [PATCH v3 8/9] " Dylan Hatch
2026-08-15  0:18     ` Josh Poimboeuf
2026-08-15  1:25       ` Josh Poimboeuf [this message]
2026-08-07 21:37 ` [PATCH v3 9/9] objtool/klp: Fix line numbers in Module.symvers parse errors Josh Poimboeuf
2026-08-11 22:36   ` Song Liu
2026-08-13 11:16   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2026-08-11 20:36 ` [PATCH v3 0/9] objtool/klp: sympos/module/alternative/etc fixes Joe Lawrence

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=an-_y1AuR2DUkcSu@jpoimboe \
    --to=jpoimboe@kernel.org \
    --cc=dylanbhatch@google.com \
    --cc=joe.lawrence@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=song@kernel.org \
    --cc=x86@kernel.org \
    /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

all inboxes | Powered by JetHome®