Live Patching
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@kernel.org>
To: x86@kernel.org
Cc: linux-kernel@vger.kernel.org, Petr Mladek <pmladek@suse.com>,
	Miroslav Benes <mbenes@suse.cz>,
	Joe Lawrence <joe.lawrence@redhat.com>,
	live-patching@vger.kernel.org, Song Liu <song@kernel.org>,
	laokz <laokz@foxmail.com>, Jiri Kosina <jikos@kernel.org>,
	Marcos Paulo de Souza <mpdesouza@suse.com>,
	Weinan Liu <wnliu@google.com>,
	Fazla Mehrab <a.mehrab@bytedance.com>,
	Chen Zhongjin <chenzhongjin@huawei.com>,
	Puranjay Mohan <puranjay@kernel.org>,
	Dylan Hatch <dylanbhatch@google.com>,
	Masahiro Yamada <masahiroy@kernel.org>
Subject: [PATCH v3 42/64] kbuild,x86: Fix special section module permissions
Date: Thu, 26 Jun 2025 16:55:29 -0700	[thread overview]
Message-ID: <cf1cfb9042005be7bf0a1c3f2bdbeebc769e3ee4.1750980517.git.jpoimboe@kernel.org> (raw)
In-Reply-To: <cover.1750980516.git.jpoimboe@kernel.org>

An upcoming patch will add the SHF_MERGE flag to x86 __jump_table and
__bug_table so their entry sizes can be defined in inline asm.

However, those sections have SHF_WRITE, which the Clang linker (lld)
explicitly forbids combining with SHF_MERGE.

Those sections are modified at runtime and must remain writable.  While
SHF_WRITE is ignored by vmlinux, it's still needed for modules.

To work around the linker interference, remove SHF_WRITE during
compilation and restore it after linking the module.

Cc: Masahiro Yamada <masahiroy@kernel.org>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 arch/Kconfig                      |  3 +++
 arch/x86/Kconfig                  |  1 +
 arch/x86/include/asm/bug.h        |  4 ++--
 arch/x86/include/asm/jump_label.h |  2 +-
 scripts/Makefile.modfinal         | 19 ++++++++++++++-----
 5 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/arch/Kconfig b/arch/Kconfig
index a3308a220f86..350ea5df5e8d 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -1314,6 +1314,9 @@ config HAVE_NOINSTR_HACK
 config HAVE_NOINSTR_VALIDATION
 	bool
 
+config NEED_MODULE_PERMISSIONS_FIX
+	bool
+
 config HAVE_UACCESS_VALIDATION
 	bool
 	select OBJTOOL
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 71019b3b54ea..62faa62b5959 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -310,6 +310,7 @@ config X86
 	select HOTPLUG_SPLIT_STARTUP		if SMP && X86_32
 	select IRQ_FORCED_THREADING
 	select LOCK_MM_AND_FIND_VMA
+	select NEED_MODULE_PERMISSIONS_FIX
 	select NEED_PER_CPU_EMBED_FIRST_CHUNK
 	select NEED_PER_CPU_PAGE_FIRST_CHUNK
 	select NEED_SG_DMA_LENGTH
diff --git a/arch/x86/include/asm/bug.h b/arch/x86/include/asm/bug.h
index 8593976b32cb..59e155ee3c76 100644
--- a/arch/x86/include/asm/bug.h
+++ b/arch/x86/include/asm/bug.h
@@ -42,7 +42,7 @@
 #define _BUG_FLAGS(cond_str, ins, flags, extra)				\
 do {									\
 	asm_inline volatile("1:\t" ins "\n"				\
-		     ".pushsection __bug_table,\"aw\"\n"		\
+		     ".pushsection __bug_table,\"a\"\n"			\
 		     "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n"	\
 		     "\t"  __BUG_REL(%c0) "\t# bug_entry::file\n"	\
 		     "\t.word %c1"        "\t# bug_entry::line\n"	\
@@ -60,7 +60,7 @@ do {									\
 #define _BUG_FLAGS(cond_str, ins, flags, extra)				\
 do {									\
 	asm_inline volatile("1:\t" ins "\n"				\
-		     ".pushsection __bug_table,\"aw\"\n"		\
+		     ".pushsection __bug_table,\"a\"\n"			\
 		     "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n"	\
 		     "\t.word %c0"        "\t# bug_entry::flags\n"	\
 		     "\t.org 2b+%c1\n"					\
diff --git a/arch/x86/include/asm/jump_label.h b/arch/x86/include/asm/jump_label.h
index 61dd1dee7812..cd21554b3675 100644
--- a/arch/x86/include/asm/jump_label.h
+++ b/arch/x86/include/asm/jump_label.h
@@ -13,7 +13,7 @@
 #include <linux/types.h>
 
 #define JUMP_TABLE_ENTRY(key, label)			\
-	".pushsection __jump_table,  \"aw\" \n\t"	\
+	".pushsection __jump_table,  \"a\"\n\t"		\
 	_ASM_ALIGN "\n\t"				\
 	".long 1b - . \n\t"				\
 	".long " label " - . \n\t"			\
diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal
index 542ba462ed3e..7a888e1ff70f 100644
--- a/scripts/Makefile.modfinal
+++ b/scripts/Makefile.modfinal
@@ -28,12 +28,24 @@ ccflags-remove-y := $(CC_FLAGS_CFI)
 .module-common.o: $(srctree)/scripts/module-common.c FORCE
 	$(call if_changed_rule,cc_o_c)
 
+ifdef CONFIG_NEED_MODULE_PERMISSIONS_FIX
+cmd_fix_mod_permissions =						\
+	$(OBJCOPY) --set-section-flags __jump_table=alloc,data		\
+		   --set-section-flags __bug_table=alloc,data $@	\
+		   --set-section-flags .static_call_sites=alloc,data $@
+endif
+
 quiet_cmd_ld_ko_o = LD [M]  $@
       cmd_ld_ko_o =							\
 	$(LD) -r $(KBUILD_LDFLAGS)					\
 		$(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE)		\
 		-T $(objtree)/scripts/module.lds -o $@ $(filter %.o, $^)
 
+define rule_ld_ko_o
+	$(call cmd_and_savecmd,ld_ko_o)
+	$(call cmd,fix_mod_permissions)
+endef
+
 quiet_cmd_btf_ko = BTF [M] $@
       cmd_btf_ko = 							\
 	if [ ! -f $(objtree)/vmlinux ]; then				\
@@ -46,14 +58,11 @@ quiet_cmd_btf_ko = BTF [M] $@
 # Same as newer-prereqs, but allows to exclude specified extra dependencies
 newer_prereqs_except = $(filter-out $(PHONY) $(1),$?)
 
-# Same as if_changed, but allows to exclude specified extra dependencies
-if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check),      \
-	$(cmd);                                                              \
-	printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
+if_changed_rule_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check),$(rule_$(1)),@:)
 
 # Re-generate module BTFs if either module's .ko or vmlinux changed
 %.ko: %.o %.mod.o .module-common.o $(objtree)/scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),$(objtree)/vmlinux) FORCE
-	+$(call if_changed_except,ld_ko_o,$(objtree)/vmlinux)
+	+$(call if_changed_rule_except,ld_ko_o,$(objtree)/vmlinux)
 ifdef CONFIG_DEBUG_INFO_BTF_MODULES
 	+$(if $(newer-prereqs),$(call cmd,btf_ko))
 endif
-- 
2.49.0


  parent reply	other threads:[~2025-06-26 23:56 UTC|newest]

Thread overview: 83+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-26 23:54 [PATCH v3 00/64] objtool,livepatch: klp-build livepatch module generation Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 01/64] s390/vmlinux.lds.S: Prevent thunk functions from getting placed with normal text Josh Poimboeuf
2025-06-27  9:34   ` Heiko Carstens
2025-06-26 23:54 ` [PATCH v3 02/64] vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and related macros Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 03/64] x86/module: Improve relocation error messages Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 04/64] x86/kprobes: Remove STACK_FRAME_NON_STANDARD annotation Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 05/64] compiler: Tweak __UNIQUE_ID() naming Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 06/64] compiler.h: Make addressable symbols less of an eyesore Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 07/64] elfnote: Change ELFNOTE() to use __UNIQUE_ID() Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 08/64] kbuild: Remove 'kmod_' prefix from __KBUILD_MODNAME Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 09/64] modpost: Ignore unresolved section bounds symbols Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 10/64] x86/alternative: Refactor INT3 call emulation selftest Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 11/64] objtool: Make find_symbol_containing() less arbitrary Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 12/64] objtool: Fix broken error handling in read_symbols() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 13/64] objtool: Propagate elf_truncate_section() error in elf_write() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 14/64] objtool: Remove error handling boilerplate Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 15/64] objtool: Add empty symbols to the symbol tree again Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 16/64] objtool: Fix interval tree insertion for zero-length symbols Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 17/64] objtool: Fix weak symbol detection Josh Poimboeuf
2025-06-27  9:13   ` Peter Zijlstra
2025-06-27 15:42     ` Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 18/64] objtool: Fix x86 addend calculation Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 19/64] objtool: Fix __pa_symbol() relocation handling Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 20/64] objtool: Fix "unexpected end of section" warning for alternatives Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 21/64] objtool: Check for missing annotation entries in read_annotate() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 22/64] objtool: Const string cleanup Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 23/64] objtool: Clean up compiler flag usage Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 24/64] objtool: Remove .parainstructions reference Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 25/64] objtool: Convert elf iterator macros to use 'struct elf' Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 26/64] objtool: Add section/symbol type helpers Josh Poimboeuf
2025-06-27 10:29   ` Peter Zijlstra
2025-06-27 16:36     ` Josh Poimboeuf
2025-06-30  7:29       ` Peter Zijlstra
2025-07-01 19:05         ` Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 27/64] objtool: Mark .cold subfunctions Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 28/64] objtool: Fix weak symbol hole detection for .cold functions Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 29/64] objtool: Mark prefix functions Josh Poimboeuf
2025-06-27 10:31   ` Peter Zijlstra
2025-06-27 16:53     ` Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 30/64] objtool: Simplify reloc offset calculation in unwind_read_hints() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 31/64] objtool: Avoid emptying lists for duplicate sections Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 32/64] objtool: Rename --Werror to --werror Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 33/64] objtool: Resurrect --backup option Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 34/64] objtool: Reindent check_options[] Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 35/64] objtool: Refactor add_jump_destinations() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 36/64] objtool: Simplify special symbol handling in elf_update_symbol() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 37/64] objtool: Generalize elf_create_symbol() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 38/64] objtool: Generalize elf_create_section() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 39/64] objtool: Add elf_create_data() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 40/64] objtool: Add elf_create_reloc() and elf_init_reloc() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 41/64] objtool: Add elf_create_file() Josh Poimboeuf
2025-06-26 23:55 ` Josh Poimboeuf [this message]
2025-06-27 10:53   ` [PATCH v3 42/64] kbuild,x86: Fix special section module permissions Peter Zijlstra
2025-06-27 17:34     ` Josh Poimboeuf
2025-06-30  7:31       ` Peter Zijlstra
2025-09-16 23:18         ` Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 43/64] x86/alternative: Define ELF section entry size for alternatives Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 44/64] x86/jump_label: Define ELF section entry size for jump labels Josh Poimboeuf
2025-06-27 10:48   ` Peter Zijlstra
2025-06-27 16:55     ` Josh Poimboeuf
2025-06-30  7:35       ` Peter Zijlstra
2025-06-26 23:55 ` [PATCH v3 45/64] x86/static_call: Define ELF section entry size of static calls Josh Poimboeuf
2025-06-27 10:51   ` Peter Zijlstra
2025-06-26 23:55 ` [PATCH v3 46/64] x86/extable: Define ELF section entry size for exception table Josh Poimboeuf
2025-06-27 10:52   ` Peter Zijlstra
2025-06-26 23:55 ` [PATCH v3 47/64] x86/bug: Define ELF section entry size for bug table Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 48/64] x86/orc: Define ELF section entry size for unwind hints Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 49/64] objtool: Unify STACK_FRAME_NON_STANDARD entry sizes Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 50/64] objtool/klp: Add --checksum option to generate per-function checksums Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 51/64] objtool/klp: Add --debug-checksum=<funcs> to show per-instruction checksums Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 52/64] objtool/klp: Introduce klp diff subcommand for diffing object files Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 53/64] objtool/klp: Add --debug option to show cloning decisions Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 54/64] objtool/klp: Add post-link subcommand to finalize livepatch modules Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 55/64] objtool: Disallow duplicate prefix symbols Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 56/64] objtool: Add base objtool support for livepatch modules Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 57/64] livepatch: Add CONFIG_KLP_BUILD Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 58/64] kbuild,objtool: Defer objtool validation step for CONFIG_KLP_BUILD Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 59/64] livepatch/klp-build: Introduce fix-patch-lines script to avoid __LINE__ diff noise Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 60/64] livepatch/klp-build: Add stub init code for livepatch modules Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 61/64] livepatch/klp-build: Introduce klp-build script for generating " Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 62/64] livepatch/klp-build: Add --debug option to show cloning decisions Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 63/64] livepatch/klp-build: Add --show-first-changed option to show function divergence Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 64/64] livepatch: Introduce source code helpers for livepatch modules Josh Poimboeuf

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=cf1cfb9042005be7bf0a1c3f2bdbeebc769e3ee4.1750980517.git.jpoimboe@kernel.org \
    --to=jpoimboe@kernel.org \
    --cc=a.mehrab@bytedance.com \
    --cc=chenzhongjin@huawei.com \
    --cc=dylanbhatch@google.com \
    --cc=jikos@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=laokz@foxmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=mbenes@suse.cz \
    --cc=mpdesouza@suse.com \
    --cc=pmladek@suse.com \
    --cc=puranjay@kernel.org \
    --cc=song@kernel.org \
    --cc=wnliu@google.com \
    --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