Skip to contentSkip to conversation

LoongArch: Enable STRICT_MODULE_RWX for stricter modules memory permissions

PATCH In review [email protected] 2 participants 10 messages Jun 13 – Jun 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 LoongArch: Enable STRICT_MODULE_RWX for stricter modules memory permissions

From: Haoran Jiang <[email protected]>

Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.

---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.

v3:
Modify commit description.

v4:
Add text_mutex lock in the larch_insn_write call path and CONFIG_STRICT_MODULE_RWX is enabled by default.

UB test on the 3C6000 server shows no significant performance impact.

Before patch:

========================================================================
BYTE UNIX Benchmarks (Version 5.1.6)

System: localhost.localdomain: GNU/Linux
OS: GNU/Linux -- 7.1.0-rc6 -- #1 SMP PREEMPT Wed Jun 10 21:07:41 CST 2026
Machine: loongarch64 (loongarch64)
Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
21:19:51 up 1 min, 2 users, load average: 0.71, 0.38, 0.14; runlevel 2026-06-10

------------------------------------------------------------------------
128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35205725.4 lps (10.0 s, 7 samples)
Double-Precision Whetstone 4244.9 MWIPS (10.0 s, 7 samples)

Execl Throughput                               6717.7 lps   (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks       1213873.8 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks          350740.5 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks       3275103.0 KBps  (30.0 s, 2 samples)
Pipe Throughput                             1981993.9 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                  55287.7 lps   (10.0 s, 7 samples)
Process Creation                               9056.8 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                   6736.5 lpm   (60.0 s, 2 samples)
Shell Scripts (8 concurrent)                   2109.8 lpm   (60.0 s, 2 samples)
System Call Overhead                        1549110.9 lps   (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 35205725.4 3016.8

Double-Precision Whetstone                       55.0       4244.9    771.8
Execl Throughput                                 43.0       6717.7   1562.3
File Copy 1024 bufsize 2000 maxblocks          3960.0    1213873.8   3065.3
File Copy 256 bufsize 500 maxblocks            1655.0     350740.5   2119.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    3275103.0   5646.7
Pipe Throughput                               12440.0    1981993.9   1593.2
Pipe-based Context Switching                   4000.0      55287.7    138.2
Process Creation                                126.0       9056.8    718.8
Shell Scripts (1 concurrent)                     42.4       6736.5   1588.8
Shell Scripts (8 concurrent)                      6.0       2109.8   3516.4
System Call Overhead                          15000.0    1549110.9   1032.7
                                                                   ========

System Benchmarks Index Score 1492.2

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2901925470.7 lps (10.0 s, 7 samples)
Double-Precision Whetstone 503614.9 MWIPS (10.1 s, 7 samples)

Execl Throughput                              34080.1 lps   (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks        309291.4 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks           75115.3 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks       1018101.3 KBps  (30.0 s, 2 samples)
Pipe Throughput                           180702003.1 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                2426596.4 lps   (10.0 s, 7 samples)
Process Creation                              37282.5 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                  50813.7 lpm   (60.1 s, 2 samples)
Shell Scripts (8 concurrent)                   5835.8 lpm   (60.4 s, 2 samples)
System Call Overhead                        9039181.1 lps   (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2901925470.7 248665.4
Double-Precision Whetstone 55.0 503614.9 91566.3
Execl Throughput 43.0 34080.1 7925.6

File Copy 1024 bufsize 2000 maxblocks          3960.0     309291.4    781.0
File Copy 256 bufsize 500 maxblocks            1655.0      75115.3    453.9
File Copy 4096 bufsize 8000 maxblocks          5800.0    1018101.3   1755.3

Pipe Throughput 12440.0 180702003.1 145258.8
Pipe-based Context Switching 4000.0 2426596.4 6066.5
Process Creation 126.0 37282.5 2958.9
Shell Scripts (1 concurrent) 42.4 50813.7 11984.4
Shell Scripts (8 concurrent) 6.0 5835.8 9726.4
System Call Overhead 15000.0 9039181.1 6026.1

                                                                   ========

System Benchmarks Index Score 8765.2

After patch:

128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35438193.7 lps (10.0 s, 7 samples)
Double-Precision Whetstone 4245.7 MWIPS (10.0 s, 7 samples)

Execl Throughput                               5293.7 lps   (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks       1233323.4 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks          355264.5 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks       3333631.6 KBps  (30.0 s, 2 samples)
Pipe Throughput                             1979613.2 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                  55675.2 lps   (10.0 s, 7 samples)
Process Creation                               8528.1 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                   6870.0 lpm   (60.0 s, 2 samples)
Shell Scripts (8 concurrent)                   2115.5 lpm   (60.0 s, 2 samples)
System Call Overhead                        1546959.4 lps   (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 35438193.7 3036.7

Double-Precision Whetstone                       55.0       4245.7    772.0
Execl Throughput                                 43.0       5293.7   1231.1
File Copy 1024 bufsize 2000 maxblocks          3960.0    1233323.4   3114.5
File Copy 256 bufsize 500 maxblocks            1655.0     355264.5   2146.6
File Copy 4096 bufsize 8000 maxblocks          5800.0    3333631.6   5747.6
Pipe Throughput                               12440.0    1979613.2   1591.3
Pipe-based Context Switching                   4000.0      55675.2    139.2
Process Creation                                126.0       8528.1    676.8
Shell Scripts (1 concurrent)                     42.4       6870.0   1620.3
Shell Scripts (8 concurrent)                      6.0       2115.5   3525.8
System Call Overhead                          15000.0    1546959.4   1031.3
                                                                   ========

System Benchmarks Index Score 1465.3

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2903340286.5 lps (10.0 s, 7 samples)
Double-Precision Whetstone 504137.7 MWIPS (10.1 s, 7 samples)

Execl Throughput                              34332.8 lps   (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks        311391.2 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks           72503.3 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks       1000861.7 KBps  (30.0 s, 2 samples)
Pipe Throughput                           179382076.6 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                2415716.6 lps   (10.0 s, 7 samples)
Process Creation                              36873.1 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                  51464.1 lpm   (60.1 s, 2 samples)
Shell Scripts (8 concurrent)                   5976.3 lpm   (60.4 s, 2 samples)
System Call Overhead                        9182389.5 lps   (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2903340286.5 248786.7
Double-Precision Whetstone 55.0 504137.7 91661.4
Execl Throughput 43.0 34332.8 7984.4

File Copy 1024 bufsize 2000 maxblocks          3960.0     311391.2    786.3
File Copy 256 bufsize 500 maxblocks            1655.0      72503.3    438.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1000861.7   1725.6

Pipe Throughput 12440.0 179382076.6 144197.8
Pipe-based Context Switching 4000.0 2415716.6 6039.3
Process Creation 126.0 36873.1 2926.4
Shell Scripts (1 concurrent) 42.4 51464.1 12137.8
Shell Scripts (8 concurrent) 6.0 5976.3 9960.6
System Call Overhead 15000.0 9182389.5 6121.6

                                                                   ========

System Benchmarks Index Score 8759.8

---
 arch/loongarch/Kconfig             |  1 +
 arch/loongarch/kernel/ftrace_dyn.c |  7 ++++++-
 arch/loongarch/kernel/inst.c       | 25 +++++++++++++++++++++----
 arch/loongarch/kernel/jump_label.c |  3 +++
 4 files changed, 31 insertions(+), 5 deletions(-)
arch/loongarch/Kconfig +1 -0
index 606597da46b8..c751d714c287 100644
@@ -27,6 +27,7 @@ config LOONGARCH
 	select ARCH_HAS_PTE_SPECIAL if 64BIT
 	select ARCH_HAS_SET_MEMORY
 	select ARCH_HAS_SET_DIRECT_MAP
+	select ARCH_HAS_STRICT_MODULE_RWX
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
 	select ARCH_HAS_UBSAN
 	select ARCH_HAS_VDSO_ARCH_DATA
arch/loongarch/kernel/ftrace_dyn.c +6 -1
index d5d81d74034c..598dc6434cc4 100644
@@ -8,6 +8,7 @@
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
+#include <linux/memory.h>
 
 #include <asm/inst.h>
 #include <asm/module.h>
@@ -24,8 +25,12 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate)
 			return -EINVAL;
 	}
 
-	if (larch_insn_patch_text((void *)pc, new))
+	mutex_lock(&text_mutex);
+	if (larch_insn_patch_text((void *)pc, new)) {
+		mutex_unlock(&text_mutex);
 		return -EPERM;
+	}
+	mutex_unlock(&text_mutex);
 
 	return 0;
 }
arch/loongarch/kernel/inst.c +21 -4
index 0b9228b7c13a..3de94d465c3c 100644
@@ -6,12 +6,11 @@
 #include <linux/uaccess.h>
 #include <linux/set_memory.h>
 #include <linux/stop_machine.h>
+#include <linux/memory.h>
 
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
-static DEFINE_RAW_SPINLOCK(patch_lock);
-
 void simu_pc(struct pt_regs *regs, union loongarch_instruction insn)
 {
 	unsigned long pc = regs->csr_era;
@@ -207,14 +206,32 @@ int larch_insn_read(void *addr, u32 *insnp)
 int larch_insn_write(void *addr, u32 insn)
 {
 	int ret;
+	int err = 0;
+	size_t start;
 	unsigned long flags = 0;
 
 	if ((unsigned long)addr & 3)
 		return -EINVAL;
 
-	raw_spin_lock_irqsave(&patch_lock, flags);
+	start = round_down((size_t)addr, PAGE_SIZE);
+
+	lockdep_assert_held(&text_mutex);
+
+	err = set_memory_rw(start, 1);
+	if (err) {
+		pr_info("%s: set_memory_rw() failed\n", __func__);
+		return err;
+	}
+
+	local_irq_save(flags);
 	ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
-	raw_spin_unlock_irqrestore(&patch_lock, flags);
+	local_irq_restore(flags);
+
+	err = set_memory_rox(start, 1);
+	if (err) {
+		pr_info("%s: set_memory_rox() failed\n", __func__);
+		return err;
+	}
 
 	return ret;
 }
arch/loongarch/kernel/jump_label.c +3 -0
index 24a3f4d8540c..e6bb040fe4c5 100644
@@ -6,6 +6,7 @@
  */
 #include <linux/kernel.h>
 #include <linux/jump_label.h>
+#include <linux/memory.h>
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
@@ -19,7 +20,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry, enum jump_label_t
 	else
 		insn = larch_insn_gen_nop();
 
+	mutex_lock(&text_mutex);
 	larch_insn_write(addr, insn);
+	mutex_unlock(&text_mutex);
 
 	return true;
 }
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi, Haoran,

Quoted 155 lines · On Sat, Jun 13, 2026 at 4:42 PM <[email protected]> wrote:

From: Haoran Jiang <[email protected]>

Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.

---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.

v3:
Modify commit description.

v4:
Add text_mutex lock in the larch_insn_write call path and CONFIG_STRICT_MODULE_RWX is enabled by default.

UB test on the 3C6000 server shows no significant performance impact.

Before patch:

========================================================================
BYTE UNIX Benchmarks (Version 5.1.6)

   System: localhost.localdomain: GNU/Linux
   OS: GNU/Linux -- 7.1.0-rc6 -- #1 SMP PREEMPT Wed Jun 10 21:07:41 CST 2026
   Machine: loongarch64 (loongarch64)
   Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
   21:19:51 up 1 min,  2 users,  load average: 0.71, 0.38, 0.14; runlevel 2026-06-10

------------------------------------------------------------------------
128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35205725.4 lps (10.0 s, 7 samples)
Double-Precision Whetstone 4244.9 MWIPS (10.0 s, 7 samples)

Execl Throughput                               6717.7 lps   (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks       1213873.8 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks          350740.5 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks       3275103.0 KBps  (30.0 s, 2 samples)
Pipe Throughput                             1981993.9 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                  55287.7 lps   (10.0 s, 7 samples)
Process Creation                               9056.8 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                   6736.5 lpm   (60.0 s, 2 samples)
Shell Scripts (8 concurrent)                   2109.8 lpm   (60.0 s, 2 samples)
System Call Overhead                        1549110.9 lps   (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 35205725.4 3016.8

Double-Precision Whetstone                       55.0       4244.9    771.8
Execl Throughput                                 43.0       6717.7   1562.3
File Copy 1024 bufsize 2000 maxblocks          3960.0    1213873.8   3065.3
File Copy 256 bufsize 500 maxblocks            1655.0     350740.5   2119.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    3275103.0   5646.7
Pipe Throughput                               12440.0    1981993.9   1593.2
Pipe-based Context Switching                   4000.0      55287.7    138.2
Process Creation                                126.0       9056.8    718.8
Shell Scripts (1 concurrent)                     42.4       6736.5   1588.8
Shell Scripts (8 concurrent)                      6.0       2109.8   3516.4
System Call Overhead                          15000.0    1549110.9   1032.7
                                                                   ========

System Benchmarks Index Score 1492.2

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2901925470.7 lps (10.0 s, 7 samples)
Double-Precision Whetstone 503614.9 MWIPS (10.1 s, 7 samples)

Execl Throughput                              34080.1 lps   (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks        309291.4 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks           75115.3 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks       1018101.3 KBps  (30.0 s, 2 samples)
Pipe Throughput                           180702003.1 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                2426596.4 lps   (10.0 s, 7 samples)
Process Creation                              37282.5 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                  50813.7 lpm   (60.1 s, 2 samples)
Shell Scripts (8 concurrent)                   5835.8 lpm   (60.4 s, 2 samples)
System Call Overhead                        9039181.1 lps   (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2901925470.7 248665.4
Double-Precision Whetstone 55.0 503614.9 91566.3
Execl Throughput 43.0 34080.1 7925.6

File Copy 1024 bufsize 2000 maxblocks          3960.0     309291.4    781.0
File Copy 256 bufsize 500 maxblocks            1655.0      75115.3    453.9
File Copy 4096 bufsize 8000 maxblocks          5800.0    1018101.3   1755.3

Pipe Throughput 12440.0 180702003.1 145258.8
Pipe-based Context Switching 4000.0 2426596.4 6066.5
Process Creation 126.0 37282.5 2958.9
Shell Scripts (1 concurrent) 42.4 50813.7 11984.4
Shell Scripts (8 concurrent) 6.0 5835.8 9726.4
System Call Overhead 15000.0 9039181.1 6026.1

                                                                   ========

System Benchmarks Index Score 8765.2

After patch:

128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35438193.7 lps (10.0 s, 7 samples)
Double-Precision Whetstone 4245.7 MWIPS (10.0 s, 7 samples)

Execl Throughput                               5293.7 lps   (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks       1233323.4 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks          355264.5 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks       3333631.6 KBps  (30.0 s, 2 samples)
Pipe Throughput                             1979613.2 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                  55675.2 lps   (10.0 s, 7 samples)
Process Creation                               8528.1 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                   6870.0 lpm   (60.0 s, 2 samples)
Shell Scripts (8 concurrent)                   2115.5 lpm   (60.0 s, 2 samples)
System Call Overhead                        1546959.4 lps   (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 35438193.7 3036.7

Double-Precision Whetstone                       55.0       4245.7    772.0
Execl Throughput                                 43.0       5293.7   1231.1
File Copy 1024 bufsize 2000 maxblocks          3960.0    1233323.4   3114.5
File Copy 256 bufsize 500 maxblocks            1655.0     355264.5   2146.6
File Copy 4096 bufsize 8000 maxblocks          5800.0    3333631.6   5747.6
Pipe Throughput                               12440.0    1979613.2   1591.3
Pipe-based Context Switching                   4000.0      55675.2    139.2
Process Creation                                126.0       8528.1    676.8
Shell Scripts (1 concurrent)                     42.4       6870.0   1620.3
Shell Scripts (8 concurrent)                      6.0       2115.5   3525.8
System Call Overhead                          15000.0    1546959.4   1031.3
                                                                   ========

System Benchmarks Index Score 1465.3

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2903340286.5 lps (10.0 s, 7 samples)
Double-Precision Whetstone 504137.7 MWIPS (10.1 s, 7 samples)

Execl Throughput                              34332.8 lps   (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks        311391.2 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks           72503.3 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks       1000861.7 KBps  (30.0 s, 2 samples)
Pipe Throughput                           179382076.6 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                2415716.6 lps   (10.0 s, 7 samples)
Process Creation                              36873.1 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                  51464.1 lpm   (60.1 s, 2 samples)
Shell Scripts (8 concurrent)                   5976.3 lpm   (60.4 s, 2 samples)
System Call Overhead                        9182389.5 lps   (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2903340286.5 248786.7
Double-Precision Whetstone 55.0 504137.7 91661.4
Execl Throughput 43.0 34332.8 7984.4

File Copy 1024 bufsize 2000 maxblocks          3960.0     311391.2    786.3
File Copy 256 bufsize 500 maxblocks            1655.0      72503.3    438.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1000861.7   1725.6
Quoted 8 lines

Pipe Throughput 12440.0 179382076.6 144197.8
Pipe-based Context Switching 4000.0 2415716.6 6039.3
Process Creation 126.0 36873.1 2926.4
Shell Scripts (1 concurrent) 42.4 51464.1 12137.8
Shell Scripts (8 concurrent) 6.0 5976.3 9960.6
System Call Overhead 15000.0 9182389.5 6121.6

                                                                   ========

System Benchmarks Index Score 8759.8

I think your tests are incomplete. Performance test is just a very basic test, you should make sure all dynamic code modification mechanisms are correct.

At least these should be verified one by one: jump label, kgdb, bpf, ftrace, kprobes, uprobes....

You have modified ftrace, but not the best method that Ihae suggested in V3.

You completely ignore my suggestion about kprobes in V3.

For KGDB, you should use text_mutex to protect copy_to_kernel_nofault().

For uprobes, I have no idea, maybe @Tiezhu can give some suggestions.

Huacai

Quoted 125 lines
---
 arch/loongarch/Kconfig             |  1 +
 arch/loongarch/kernel/ftrace_dyn.c |  7 ++++++-
 arch/loongarch/kernel/inst.c       | 25 +++++++++++++++++++++----
 arch/loongarch/kernel/jump_label.c |  3 +++
 4 files changed, 31 insertions(+), 5 deletions(-)
arch/loongarch/Kconfig +1 -0
index 606597da46b8..c751d714c287 100644
@@ -27,6 +27,7 @@ config LOONGARCH
        select ARCH_HAS_PTE_SPECIAL if 64BIT
        select ARCH_HAS_SET_MEMORY
        select ARCH_HAS_SET_DIRECT_MAP
+       select ARCH_HAS_STRICT_MODULE_RWX
        select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
        select ARCH_HAS_UBSAN
        select ARCH_HAS_VDSO_ARCH_DATA
arch/loongarch/kernel/ftrace_dyn.c +6 -1
index d5d81d74034c..598dc6434cc4 100644
@@ -8,6 +8,7 @@
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
+#include <linux/memory.h>
 
 #include <asm/inst.h>
 #include <asm/module.h>
@@ -24,8 +25,12 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate)
                        return -EINVAL;
        }
 
-       if (larch_insn_patch_text((void *)pc, new))
+       mutex_lock(&text_mutex);
+       if (larch_insn_patch_text((void *)pc, new)) {
+               mutex_unlock(&text_mutex);
                return -EPERM;
+       }
+       mutex_unlock(&text_mutex);
 
        return 0;
 }
arch/loongarch/kernel/inst.c +21 -4
index 0b9228b7c13a..3de94d465c3c 100644
@@ -6,12 +6,11 @@
 #include <linux/uaccess.h>
 #include <linux/set_memory.h>
 #include <linux/stop_machine.h>
+#include <linux/memory.h>
 
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
-static DEFINE_RAW_SPINLOCK(patch_lock);
-
 void simu_pc(struct pt_regs *regs, union loongarch_instruction insn)
 {
        unsigned long pc = regs->csr_era;
@@ -207,14 +206,32 @@ int larch_insn_read(void *addr, u32 *insnp)
 int larch_insn_write(void *addr, u32 insn)
 {
        int ret;
+       int err = 0;
+       size_t start;
        unsigned long flags = 0;
 
        if ((unsigned long)addr & 3)
                return -EINVAL;
 
-       raw_spin_lock_irqsave(&patch_lock, flags);
+       start = round_down((size_t)addr, PAGE_SIZE);
+
+       lockdep_assert_held(&text_mutex);
+
+       err = set_memory_rw(start, 1);
+       if (err) {
+               pr_info("%s: set_memory_rw() failed\n", __func__);
+               return err;
+       }
+
+       local_irq_save(flags);
        ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
-       raw_spin_unlock_irqrestore(&patch_lock, flags);
+       local_irq_restore(flags);
+
+       err = set_memory_rox(start, 1);
+       if (err) {
+               pr_info("%s: set_memory_rox() failed\n", __func__);
+               return err;
+       }
 
        return ret;
 }
arch/loongarch/kernel/jump_label.c +3 -1
index 24a3f4d8540c..e6bb040fe4c5 100644
@@ -6,6 +6,7 @@
  */
 #include <linux/kernel.h>
 #include <linux/jump_label.h>
+#include <linux/memory.h>
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
@@ -19,7 +20,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry, enum jump_label_t
        else
                insn = larch_insn_gen_nop();
 
+       mutex_lock(&text_mutex);
        larch_insn_write(addr, insn);
+       mutex_unlock(&text_mutex);
 
        return true;
 }
--

2.43.0

#2 lore raw

2026年6月13日 18:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 177 lines

Hi, Haoran,

Quoted 147 lines · On Sat, Jun 13, 2026 at 4:42 PM <[email protected]> wrote:

From: Haoran Jiang <[email protected]>

Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.

---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.

v3:
Modify commit description.

v4:
Add text_mutex lock in the larch_insn_write call path and CONFIG_STRICT_MODULE_RWX is enabled by default.

UB test on the 3C6000 server shows no significant performance impact.

Before patch:

========================================================================
BYTE UNIX Benchmarks (Version 5.1.6)

System: localhost.localdomain: GNU/Linux
OS: GNU/Linux -- 7.1.0-rc6 -- #1 SMP PREEMPT Wed Jun 10 21:07:41 CST 2026
Machine: loongarch64 (loongarch64)
Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
21:19:51 up 1 min, 2 users, load average: 0.71, 0.38, 0.14; runlevel 2026-06-10

------------------------------------------------------------------------
128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35205725.4 lps (10.0 s, 7 samples) Double-Precision Whetstone 4244.9 MWIPS (10.0 s, 7 samples) Execl Throughput 6717.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1213873.8 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 350740.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3275103.0 KBps (30.0 s, 2 samples) Pipe Throughput 1981993.9 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55287.7 lps (10.0 s, 7 samples) Process Creation 9056.8 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6736.5 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2109.8 lpm (60.0 s, 2 samples) System Call Overhead 1549110.9 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35205725.4 3016.8 Double-Precision Whetstone 55.0 4244.9 771.8
Execl Throughput 43.0 6717.7 1562.3
File Copy 1024 bufsize 2000 maxblocks 3960.0 1213873.8 3065.3 File Copy 256 bufsize 500 maxblocks 1655.0 350740.5 2119.3 File Copy 4096 bufsize 8000 maxblocks 5800.0 3275103.0 5646.7 Pipe Throughput 12440.0 1981993.9 1593.2
Pipe-based Context Switching 4000.0 55287.7 138.2 Process Creation 126.0 9056.8 718.8
Shell Scripts (1 concurrent) 42.4 6736.5 1588.8
Shell Scripts (8 concurrent) 6.0 2109.8 3516.4
System Call Overhead 15000.0 1549110.9 1032.7
========
System Benchmarks Index Score 1492.2

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2901925470.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 503614.9 MWIPS (10.1 s, 7 samples) Execl Throughput 34080.1 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 309291.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 75115.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1018101.3 KBps (30.0 s, 2 samples) Pipe Throughput 180702003.1 lps (10.0 s, 7 samples) Pipe-based Context Switching 2426596.4 lps (10.0 s, 7 samples) Process Creation 37282.5 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 50813.7 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5835.8 lpm (60.4 s, 2 samples) System Call Overhead 9039181.1 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2901925470.7 248665.4 Double-Precision Whetstone 55.0 503614.9 91566.3
Execl Throughput 43.0 34080.1 7925.6
File Copy 1024 bufsize 2000 maxblocks 3960.0 309291.4 781.0 File Copy 256 bufsize 500 maxblocks 1655.0 75115.3 453.9 File Copy 4096 bufsize 8000 maxblocks 5800.0 1018101.3 1755.3 Pipe Throughput 12440.0 180702003.1 145258.8
Pipe-based Context Switching 4000.0 2426596.4 6066.5 Process Creation 126.0 37282.5 2958.9
Shell Scripts (1 concurrent) 42.4 50813.7 11984.4
Shell Scripts (8 concurrent) 6.0 5835.8 9726.4
System Call Overhead 15000.0 9039181.1 6026.1
========
System Benchmarks Index Score 8765.2

After patch:

128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35438193.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 4245.7 MWIPS (10.0 s, 7 samples) Execl Throughput 5293.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1233323.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 355264.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3333631.6 KBps (30.0 s, 2 samples) Pipe Throughput 1979613.2 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55675.2 lps (10.0 s, 7 samples) Process Creation 8528.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6870.0 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2115.5 lpm (60.0 s, 2 samples) System Call Overhead 1546959.4 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35438193.7 3036.7 Double-Precision Whetstone 55.0 4245.7 772.0
Execl Throughput 43.0 5293.7 1231.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 1233323.4 3114.5 File Copy 256 bufsize 500 maxblocks 1655.0 355264.5 2146.6 File Copy 4096 bufsize 8000 maxblocks 5800.0 3333631.6 5747.6 Pipe Throughput 12440.0 1979613.2 1591.3
Pipe-based Context Switching 4000.0 55675.2 139.2 Process Creation 126.0 8528.1 676.8
Shell Scripts (1 concurrent) 42.4 6870.0 1620.3
Shell Scripts (8 concurrent) 6.0 2115.5 3525.8
System Call Overhead 15000.0 1546959.4 1031.3
========
System Benchmarks Index Score 1465.3

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2903340286.5 lps (10.0 s, 7 samples) Double-Precision Whetstone 504137.7 MWIPS (10.1 s, 7 samples) Execl Throughput 34332.8 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 311391.2 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 72503.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1000861.7 KBps (30.0 s, 2 samples) Pipe Throughput 179382076.6 lps (10.0 s, 7 samples) Pipe-based Context Switching 2415716.6 lps (10.0 s, 7 samples) Process Creation 36873.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 51464.1 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5976.3 lpm (60.4 s, 2 samples) System Call Overhead 9182389.5 lps (10.0 s, 7 samples)

Quoted 16 lines

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2903340286.5 248786.7 Double-Precision Whetstone 55.0 504137.7 91661.4
Execl Throughput 43.0 34332.8 7984.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 311391.2 786.3 File Copy 256 bufsize 500 maxblocks 1655.0 72503.3 438.1 File Copy 4096 bufsize 8000 maxblocks 5800.0 1000861.7 1725.6 Pipe Throughput 12440.0 179382076.6 144197.8
Pipe-based Context Switching 4000.0 2415716.6 6039.3 Process Creation 126.0 36873.1 2926.4
Shell Scripts (1 concurrent) 42.4 51464.1 12137.8
Shell Scripts (8 concurrent) 6.0 5976.3 9960.6
System Call Overhead 15000.0 9182389.5 6121.6
========
System Benchmarks Index Score 8759.8

I think your tests are incomplete. Performance test is just a very basic test, you should make sure all dynamic code modification mechanisms are correct.

At least these should be verified one by one: jump label, kgdb, bpf, ftrace, kprobes, uprobes....

You have modified ftrace, but not the best method that Ihae suggested in V3.

Quoted 8 lines

You completely ignore my suggestion about kprobes in V3.

For KGDB, you should use text_mutex to protect copy_to_kernel_nofault().

For uprobes, I have no idea, maybe @Tiezhu can give some suggestions.

Huacai

For fentry:

ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() are not in the call path of ftrace_init_nop(),

so the text_mutex lock was added in ftrace_modify_code(). However, I overlooked that ftrace_init_nop() also holds the text_mutex lock (e.g., on RISC-V). I am fixing this.

For kprobes:

text_mutex is already held in the caller arm_kprobe(), which then calls arch_arm_kprobe->larch_insn_text_copy. So the kprobes path is properly protected.

For kgdb:

When KGDB enters do_single_step(), it has already stopped execution on all other CPUs. Only the current CPU is running KGDB logic. Do we need to worry about concurrency issues in this case? Also, Sleepable lock is also disallowed in exception context.

For uprobes:

I will check the call path again.

For test:

I have already done some tests using BPF tools that utilize kprobes and fentry. I will run more tests using the selftests suite.

Thanks !

Quoted 125 lines
Quoted 124 lines
---
 arch/loongarch/Kconfig | 1 +
 arch/loongarch/kernel/ftrace_dyn.c | 7 ++++++-
 arch/loongarch/kernel/inst.c | 25 +++++++++++++++++++++----
 arch/loongarch/kernel/jump_label.c | 3 +++
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 606597da46b8..c751d714c287 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -27,6 +27,7 @@ config LOONGARCH
select ARCH_HAS_PTE_SPECIAL if 64BIT
select ARCH_HAS_SET_MEMORY
select ARCH_HAS_SET_DIRECT_MAP
+ select ARCH_HAS_STRICT_MODULE_RWX
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
select ARCH_HAS_UBSAN
select ARCH_HAS_VDSO_ARCH_DATA
diff --git a/arch/loongarch/kernel/ftrace_dyn.c b/arch/loongarch/kernel/ftrace_dyn.c index d5d81d74034c..598dc6434cc4 100644
--- a/arch/loongarch/kernel/ftrace_dyn.c
+++ b/arch/loongarch/kernel/ftrace_dyn.c
@@ -8,6 +8,7 @@
#include <linux/ftrace.h>
#include <linux/kprobes.h>
#include <linux/uaccess.h>
+#include <linux/memory.h>

#include <asm/inst.h>
#include <asm/module.h>
@@ -24,8 +25,12 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate) return -EINVAL;
}

- if (larch_insn_patch_text((void *)pc, new))
+ mutex_lock(&text_mutex);
+ if (larch_insn_patch_text((void *)pc, new)) {
+ mutex_unlock(&text_mutex);
return -EPERM;
+ }
+ mutex_unlock(&text_mutex);

return 0;
}
diff --git a/arch/loongarch/kernel/inst.c b/arch/loongarch/kernel/inst.c index 0b9228b7c13a..3de94d465c3c 100644
--- a/arch/loongarch/kernel/inst.c
+++ b/arch/loongarch/kernel/inst.c
@@ -6,12 +6,11 @@
#include <linux/uaccess.h>
#include <linux/set_memory.h>
#include <linux/stop_machine.h>
+#include <linux/memory.h>

#include <asm/cacheflush.h>
#include <asm/inst.h>

-static DEFINE_RAW_SPINLOCK(patch_lock);
-
void simu_pc(struct pt_regs *regs, union loongarch_instruction insn) {
unsigned long pc = regs->csr_era;
@@ -207,14 +206,32 @@ int larch_insn_read(void *addr, u32 *insnp) int larch_insn_write(void *addr, u32 insn)
{
int ret;
+ int err = 0;
+ size_t start;
unsigned long flags = 0;

if ((unsigned long)addr & 3)
return -EINVAL;

- raw_spin_lock_irqsave(&patch_lock, flags);
+ start = round_down((size_t)addr, PAGE_SIZE);
+
+ lockdep_assert_held(&text_mutex);
+
+ err = set_memory_rw(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rw() failed\n", __func__);
+ return err;
+ }
+
+ local_irq_save(flags);
ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
- raw_spin_unlock_irqrestore(&patch_lock, flags);
+ local_irq_restore(flags);
+
+ err = set_memory_rox(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rox() failed\n", __func__);
+ return err;
+ }

return ret;
}
diff --git a/arch/loongarch/kernel/jump_label.c b/arch/loongarch/kernel/jump_label.c index 24a3f4d8540c..e6bb040fe4c5 100644
--- a/arch/loongarch/kernel/jump_label.c
+++ b/arch/loongarch/kernel/jump_label.c
@@ -6,6 +6,7 @@
*/
#include <linux/kernel.h>
#include <linux/jump_label.h>
+#include <linux/memory.h>
#include <asm/cacheflush.h>
#include <asm/inst.h>

@@ -19,7 +20,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry, enum jump_label_t else
insn = larch_insn_gen_nop();

+ mutex_lock(&text_mutex);
larch_insn_write(addr, insn);
+ mutex_unlock(&text_mutex);

return true;
}
--
2.43.0

maintainer
#3 lore raw
Quoted 187 lines · On Sun, Jun 14, 2026 at 6:24 PM <[email protected]> wrote:

2026年6月13日 18:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 183 lines

Hi, Haoran,

Quoted 147 lines · On Sat, Jun 13, 2026 at 4:42 PM <[email protected]> wrote:

From: Haoran Jiang <[email protected]>

Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.

---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.

v3:
Modify commit description.

v4:
Add text_mutex lock in the larch_insn_write call path and CONFIG_STRICT_MODULE_RWX is enabled by default.

UB test on the 3C6000 server shows no significant performance impact.

Before patch:

========================================================================
BYTE UNIX Benchmarks (Version 5.1.6)

System: localhost.localdomain: GNU/Linux
OS: GNU/Linux -- 7.1.0-rc6 -- #1 SMP PREEMPT Wed Jun 10 21:07:41 CST 2026
Machine: loongarch64 (loongarch64)
Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
21:19:51 up 1 min, 2 users, load average: 0.71, 0.38, 0.14; runlevel 2026-06-10

------------------------------------------------------------------------
128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35205725.4 lps (10.0 s, 7 samples) Double-Precision Whetstone 4244.9 MWIPS (10.0 s, 7 samples) Execl Throughput 6717.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1213873.8 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 350740.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3275103.0 KBps (30.0 s, 2 samples) Pipe Throughput 1981993.9 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55287.7 lps (10.0 s, 7 samples) Process Creation 9056.8 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6736.5 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2109.8 lpm (60.0 s, 2 samples) System Call Overhead 1549110.9 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35205725.4 3016.8 Double-Precision Whetstone 55.0 4244.9 771.8
Execl Throughput 43.0 6717.7 1562.3
File Copy 1024 bufsize 2000 maxblocks 3960.0 1213873.8 3065.3 File Copy 256 bufsize 500 maxblocks 1655.0 350740.5 2119.3 File Copy 4096 bufsize 8000 maxblocks 5800.0 3275103.0 5646.7 Pipe Throughput 12440.0 1981993.9 1593.2
Pipe-based Context Switching 4000.0 55287.7 138.2 Process Creation 126.0 9056.8 718.8
Shell Scripts (1 concurrent) 42.4 6736.5 1588.8
Shell Scripts (8 concurrent) 6.0 2109.8 3516.4
System Call Overhead 15000.0 1549110.9 1032.7
========
System Benchmarks Index Score 1492.2

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2901925470.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 503614.9 MWIPS (10.1 s, 7 samples) Execl Throughput 34080.1 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 309291.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 75115.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1018101.3 KBps (30.0 s, 2 samples) Pipe Throughput 180702003.1 lps (10.0 s, 7 samples) Pipe-based Context Switching 2426596.4 lps (10.0 s, 7 samples) Process Creation 37282.5 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 50813.7 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5835.8 lpm (60.4 s, 2 samples) System Call Overhead 9039181.1 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2901925470.7 248665.4 Double-Precision Whetstone 55.0 503614.9 91566.3
Execl Throughput 43.0 34080.1 7925.6
File Copy 1024 bufsize 2000 maxblocks 3960.0 309291.4 781.0 File Copy 256 bufsize 500 maxblocks 1655.0 75115.3 453.9 File Copy 4096 bufsize 8000 maxblocks 5800.0 1018101.3 1755.3 Pipe Throughput 12440.0 180702003.1 145258.8
Pipe-based Context Switching 4000.0 2426596.4 6066.5 Process Creation 126.0 37282.5 2958.9
Shell Scripts (1 concurrent) 42.4 50813.7 11984.4
Shell Scripts (8 concurrent) 6.0 5835.8 9726.4
System Call Overhead 15000.0 9039181.1 6026.1
========
System Benchmarks Index Score 8765.2

After patch:

128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35438193.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 4245.7 MWIPS (10.0 s, 7 samples) Execl Throughput 5293.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1233323.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 355264.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3333631.6 KBps (30.0 s, 2 samples) Pipe Throughput 1979613.2 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55675.2 lps (10.0 s, 7 samples) Process Creation 8528.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6870.0 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2115.5 lpm (60.0 s, 2 samples) System Call Overhead 1546959.4 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35438193.7 3036.7 Double-Precision Whetstone 55.0 4245.7 772.0
Execl Throughput 43.0 5293.7 1231.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 1233323.4 3114.5 File Copy 256 bufsize 500 maxblocks 1655.0 355264.5 2146.6 File Copy 4096 bufsize 8000 maxblocks 5800.0 3333631.6 5747.6 Pipe Throughput 12440.0 1979613.2 1591.3
Pipe-based Context Switching 4000.0 55675.2 139.2 Process Creation 126.0 8528.1 676.8
Shell Scripts (1 concurrent) 42.4 6870.0 1620.3
Shell Scripts (8 concurrent) 6.0 2115.5 3525.8
System Call Overhead 15000.0 1546959.4 1031.3
========
System Benchmarks Index Score 1465.3

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2903340286.5 lps (10.0 s, 7 samples) Double-Precision Whetstone 504137.7 MWIPS (10.1 s, 7 samples) Execl Throughput 34332.8 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 311391.2 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 72503.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1000861.7 KBps (30.0 s, 2 samples) Pipe Throughput 179382076.6 lps (10.0 s, 7 samples) Pipe-based Context Switching 2415716.6 lps (10.0 s, 7 samples) Process Creation 36873.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 51464.1 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5976.3 lpm (60.4 s, 2 samples) System Call Overhead 9182389.5 lps (10.0 s, 7 samples)

Quoted 16 lines

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2903340286.5 248786.7 Double-Precision Whetstone 55.0 504137.7 91661.4
Execl Throughput 43.0 34332.8 7984.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 311391.2 786.3 File Copy 256 bufsize 500 maxblocks 1655.0 72503.3 438.1 File Copy 4096 bufsize 8000 maxblocks 5800.0 1000861.7 1725.6 Pipe Throughput 12440.0 179382076.6 144197.8
Pipe-based Context Switching 4000.0 2415716.6 6039.3 Process Creation 126.0 36873.1 2926.4
Shell Scripts (1 concurrent) 42.4 51464.1 12137.8
Shell Scripts (8 concurrent) 6.0 5976.3 9960.6
System Call Overhead 15000.0 9182389.5 6121.6
========
System Benchmarks Index Score 8759.8

I think your tests are incomplete. Performance test is just a very basic test, you should make sure all dynamic code modification mechanisms are correct.

At least these should be verified one by one: jump label, kgdb, bpf, ftrace, kprobes, uprobes....

You have modified ftrace, but not the best method that Ihae suggested in V3.

You completely ignore my suggestion about kprobes in V3.

For KGDB, you should use text_mutex to protect copy_to_kernel_nofault().

For uprobes, I have no idea, maybe @Tiezhu can give some suggestions.

Quoted 8 lines
Quoted 2 lines

Huacai

For fentry:

ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() are not in the call path of ftrace_init_nop(),

so the text_mutex lock was added in ftrace_modify_code(). However, I overlooked that ftrace_init_nop() also holds the text_mutex lock (e.g., on RISC-V). I am fixing this.

No, RISC-V is right, use ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() to protect all common cases, and use text_mutex to protect the only exception, ftrace_init_nop().

Quoted 4 lines

For kprobes:

text_mutex is already held in the caller arm_kprobe(), which then calls arch_arm_kprobe->larch_insn_text_copy. So the kprobes path is properly protected.

No, I doubt you didn't see my link, we are talking about arch_prepare_ss_slot(). Though it seems the framework has hold text_mutex, but you don't use the larch_insn_patch_text() API, who handle the RWX?

Quoted 4 lines

For kgdb:

When KGDB enters do_single_step(), it has already stopped execution on all other CPUs. Only the current CPU is running KGDB logic. Do we need to worry about concurrency issues in this case? Also, Sleepable lock is also disallowed in exception context.

The same as above, if you use copy_to_kernel_nofault() directly, who handle the RWX?

Huacai

Quoted 136 lines

For uprobes:

I will check the call path again.

For test:

I have already done some tests using BPF tools that utilize kprobes and fentry. I will run more tests using the selftests suite.

Thanks !

Quoted 125 lines
Quoted 124 lines
---
 arch/loongarch/Kconfig | 1 +
 arch/loongarch/kernel/ftrace_dyn.c | 7 ++++++-
 arch/loongarch/kernel/inst.c | 25 +++++++++++++++++++++----
 arch/loongarch/kernel/jump_label.c | 3 +++
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 606597da46b8..c751d714c287 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -27,6 +27,7 @@ config LOONGARCH
select ARCH_HAS_PTE_SPECIAL if 64BIT
select ARCH_HAS_SET_MEMORY
select ARCH_HAS_SET_DIRECT_MAP
+ select ARCH_HAS_STRICT_MODULE_RWX
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
select ARCH_HAS_UBSAN
select ARCH_HAS_VDSO_ARCH_DATA
diff --git a/arch/loongarch/kernel/ftrace_dyn.c b/arch/loongarch/kernel/ftrace_dyn.c index d5d81d74034c..598dc6434cc4 100644
--- a/arch/loongarch/kernel/ftrace_dyn.c
+++ b/arch/loongarch/kernel/ftrace_dyn.c
@@ -8,6 +8,7 @@
#include <linux/ftrace.h>
#include <linux/kprobes.h>
#include <linux/uaccess.h>
+#include <linux/memory.h>

#include <asm/inst.h>
#include <asm/module.h>
@@ -24,8 +25,12 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate) return -EINVAL;
}

- if (larch_insn_patch_text((void *)pc, new))
+ mutex_lock(&text_mutex);
+ if (larch_insn_patch_text((void *)pc, new)) {
+ mutex_unlock(&text_mutex);
return -EPERM;
+ }
+ mutex_unlock(&text_mutex);

return 0;
}
diff --git a/arch/loongarch/kernel/inst.c b/arch/loongarch/kernel/inst.c index 0b9228b7c13a..3de94d465c3c 100644
--- a/arch/loongarch/kernel/inst.c
+++ b/arch/loongarch/kernel/inst.c
@@ -6,12 +6,11 @@
#include <linux/uaccess.h>
#include <linux/set_memory.h>
#include <linux/stop_machine.h>
+#include <linux/memory.h>

#include <asm/cacheflush.h>
#include <asm/inst.h>

-static DEFINE_RAW_SPINLOCK(patch_lock);
-
void simu_pc(struct pt_regs *regs, union loongarch_instruction insn) {
unsigned long pc = regs->csr_era;
@@ -207,14 +206,32 @@ int larch_insn_read(void *addr, u32 *insnp) int larch_insn_write(void *addr, u32 insn)
{
int ret;
+ int err = 0;
+ size_t start;
unsigned long flags = 0;

if ((unsigned long)addr & 3)
return -EINVAL;

- raw_spin_lock_irqsave(&patch_lock, flags);
+ start = round_down((size_t)addr, PAGE_SIZE);
+
+ lockdep_assert_held(&text_mutex);
+
+ err = set_memory_rw(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rw() failed\n", __func__);
+ return err;
+ }
+
+ local_irq_save(flags);
ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
- raw_spin_unlock_irqrestore(&patch_lock, flags);
+ local_irq_restore(flags);
+
+ err = set_memory_rox(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rox() failed\n", __func__);
+ return err;
+ }

return ret;
}
diff --git a/arch/loongarch/kernel/jump_label.c b/arch/loongarch/kernel/jump_label.c index 24a3f4d8540c..e6bb040fe4c5 100644
--- a/arch/loongarch/kernel/jump_label.c
+++ b/arch/loongarch/kernel/jump_label.c
@@ -6,6 +6,7 @@
*/
#include <linux/kernel.h>
#include <linux/jump_label.h>
+#include <linux/memory.h>
#include <asm/cacheflush.h>
#include <asm/inst.h>

@@ -19,7 +20,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry, enum jump_label_t else
insn = larch_insn_gen_nop();

+ mutex_lock(&text_mutex);
larch_insn_write(addr, insn);
+ mutex_unlock(&text_mutex);

return true;
}
--
2.43.0

#4 lore raw

2026年6月14日 20:10, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 212 lines · On Sun, Jun 14, 2026 at 6:24 PM <[email protected]> wrote:
Quoted 186 lines

2026年6月13日 18:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Hi, Haoran,

Quoted 147 lines · On Sat, Jun 13, 2026 at 4:42 PM <[email protected]> wrote:

From: Haoran Jiang <[email protected]>

Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.

---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.

v3:
Modify commit description.

v4:
Add text_mutex lock in the larch_insn_write call path and CONFIG_STRICT_MODULE_RWX is enabled by default.

UB test on the 3C6000 server shows no significant performance impact.

Before patch:

========================================================================
BYTE UNIX Benchmarks (Version 5.1.6)

System: localhost.localdomain: GNU/Linux
OS: GNU/Linux -- 7.1.0-rc6 -- #1 SMP PREEMPT Wed Jun 10 21:07:41 CST 2026
Machine: loongarch64 (loongarch64)
Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
21:19:51 up 1 min, 2 users, load average: 0.71, 0.38, 0.14; runlevel 2026-06-10

------------------------------------------------------------------------
128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35205725.4 lps (10.0 s, 7 samples) Double-Precision Whetstone 4244.9 MWIPS (10.0 s, 7 samples) Execl Throughput 6717.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1213873.8 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 350740.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3275103.0 KBps (30.0 s, 2 samples) Pipe Throughput 1981993.9 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55287.7 lps (10.0 s, 7 samples) Process Creation 9056.8 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6736.5 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2109.8 lpm (60.0 s, 2 samples) System Call Overhead 1549110.9 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35205725.4 3016.8 Double-Precision Whetstone 55.0 4244.9 771.8
Execl Throughput 43.0 6717.7 1562.3
File Copy 1024 bufsize 2000 maxblocks 3960.0 1213873.8 3065.3 File Copy 256 bufsize 500 maxblocks 1655.0 350740.5 2119.3 File Copy 4096 bufsize 8000 maxblocks 5800.0 3275103.0 5646.7 Pipe Throughput 12440.0 1981993.9 1593.2
Pipe-based Context Switching 4000.0 55287.7 138.2 Process Creation 126.0 9056.8 718.8
Shell Scripts (1 concurrent) 42.4 6736.5 1588.8
Shell Scripts (8 concurrent) 6.0 2109.8 3516.4
System Call Overhead 15000.0 1549110.9 1032.7
========
System Benchmarks Index Score 1492.2

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2901925470.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 503614.9 MWIPS (10.1 s, 7 samples) Execl Throughput 34080.1 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 309291.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 75115.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1018101.3 KBps (30.0 s, 2 samples) Pipe Throughput 180702003.1 lps (10.0 s, 7 samples) Pipe-based Context Switching 2426596.4 lps (10.0 s, 7 samples) Process Creation 37282.5 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 50813.7 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5835.8 lpm (60.4 s, 2 samples) System Call Overhead 9039181.1 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2901925470.7 248665.4 Double-Precision Whetstone 55.0 503614.9 91566.3
Execl Throughput 43.0 34080.1 7925.6
File Copy 1024 bufsize 2000 maxblocks 3960.0 309291.4 781.0 File Copy 256 bufsize 500 maxblocks 1655.0 75115.3 453.9 File Copy 4096 bufsize 8000 maxblocks 5800.0 1018101.3 1755.3 Pipe Throughput 12440.0 180702003.1 145258.8
Pipe-based Context Switching 4000.0 2426596.4 6066.5 Process Creation 126.0 37282.5 2958.9
Shell Scripts (1 concurrent) 42.4 50813.7 11984.4
Shell Scripts (8 concurrent) 6.0 5835.8 9726.4
System Call Overhead 15000.0 9039181.1 6026.1
========
System Benchmarks Index Score 8765.2

After patch:

128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35438193.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 4245.7 MWIPS (10.0 s, 7 samples) Execl Throughput 5293.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1233323.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 355264.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3333631.6 KBps (30.0 s, 2 samples) Pipe Throughput 1979613.2 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55675.2 lps (10.0 s, 7 samples) Process Creation 8528.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6870.0 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2115.5 lpm (60.0 s, 2 samples) System Call Overhead 1546959.4 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35438193.7 3036.7 Double-Precision Whetstone 55.0 4245.7 772.0
Execl Throughput 43.0 5293.7 1231.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 1233323.4 3114.5 File Copy 256 bufsize 500 maxblocks 1655.0 355264.5 2146.6 File Copy 4096 bufsize 8000 maxblocks 5800.0 3333631.6 5747.6 Pipe Throughput 12440.0 1979613.2 1591.3
Pipe-based Context Switching 4000.0 55675.2 139.2 Process Creation 126.0 8528.1 676.8
Shell Scripts (1 concurrent) 42.4 6870.0 1620.3
Shell Scripts (8 concurrent) 6.0 2115.5 3525.8
System Call Overhead 15000.0 1546959.4 1031.3
========
System Benchmarks Index Score 1465.3

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2903340286.5 lps (10.0 s, 7 samples) Double-Precision Whetstone 504137.7 MWIPS (10.1 s, 7 samples) Execl Throughput 34332.8 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 311391.2 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 72503.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1000861.7 KBps (30.0 s, 2 samples) Pipe Throughput 179382076.6 lps (10.0 s, 7 samples) Pipe-based Context Switching 2415716.6 lps (10.0 s, 7 samples) Process Creation 36873.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 51464.1 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5976.3 lpm (60.4 s, 2 samples) System Call Overhead 9182389.5 lps (10.0 s, 7 samples)

Quoted 16 lines

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2903340286.5 248786.7 Double-Precision Whetstone 55.0 504137.7 91661.4
Execl Throughput 43.0 34332.8 7984.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 311391.2 786.3 File Copy 256 bufsize 500 maxblocks 1655.0 72503.3 438.1 File Copy 4096 bufsize 8000 maxblocks 5800.0 1000861.7 1725.6 Pipe Throughput 12440.0 179382076.6 144197.8
Pipe-based Context Switching 4000.0 2415716.6 6039.3 Process Creation 126.0 36873.1 2926.4
Shell Scripts (1 concurrent) 42.4 51464.1 12137.8
Shell Scripts (8 concurrent) 6.0 5976.3 9960.6
System Call Overhead 15000.0 9182389.5 6121.6
========
System Benchmarks Index Score 8759.8

I think your tests are incomplete. Performance test is just a very basic test, you should make sure all dynamic code modification mechanisms are correct.

At least these should be verified one by one: jump label, kgdb, bpf, ftrace, kprobes, uprobes....

You have modified ftrace, but not the best method that Ihae suggested in V3.

You completely ignore my suggestion about kprobes in V3.

For KGDB, you should use text_mutex to protect copy_to_kernel_nofault().

For uprobes, I have no idea, maybe @Tiezhu can give some suggestions.

Quoted 9 lines

Huacai

For fentry:

ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() are not in the call path of ftrace_init_nop(),

so the text_mutex lock was added in ftrace_modify_code(). However, I overlooked that ftrace_init_nop() also holds the text_mutex lock (e.g., on RISC-V). I am fixing this.

No, RISC-V is right, use ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() to protect all common cases, and use text_mutex to protect the only exception, ftrace_init_nop().

Quoted 5 lines

For kprobes:

text_mutex is already held in the caller arm_kprobe(), which then calls arch_arm_kprobe->larch_insn_text_copy. So the kprobes path is properly protected.

No, I doubt you didn't see my link, we are talking about arch_prepare_ss_slot(). Though it seems the framework has hold text_mutex, but you don't use the larch_insn_patch_text() API, who handle the RWX?

Quoted 1 line
Quoted 9 lines
Quoted 4 lines

For kgdb:

When KGDB enters do_single_step(), it has already stopped execution on all other CPUs. Only the current CPU is running KGDB logic. Do we need to worry about concurrency issues in this case? Also, Sleepable lock is also disallowed in exception context.

The same as above, if you use copy_to_kernel_nofault() directly, who handle the RWX?

Huacai

I plan to make the following modifications. Is this approach feasible?

1, For kprobes:

Add modifications for arch_prepare_ss_slot(struct kprobe *p), referencing the patch at:
https://lore.kernel.org/loongarch/CAHirt9gSXZvw0wTv9zupsVRG0_ekza08efW0hwq_yvnU1WbUng@mail.gmail.com/T/#m6a75415f468e671748d6ec8c6b502ca36788c546

static void arch_prepare_ss_slot(struct kprobe *p)

 {
    larch_insn_patch_text(p->ainsn.insn, *p->addr);
    larch_insn_patch_text(p->ainsn.insn + 1, KPROBE_SSTEPBP_INSN);
    p->ainsn.restore = (unsigned long)p->addr + LOONGARCH_INSN_SIZE;

}

2,For kgdb:

Modify page table permissions for the kernel code section by adding the following lockless method. Use apply_to_existing_page_range() to iterate over the virtual address range and perform permission modification operations on existing page table entries (PTEs).

This approach references the RISC-V commit:
fb1cf0878328 ("riscv: rewrite __kernel_map_pages() to fix sleeping in invalid context")

static int change_page_attr(pte_t *ptep, unsigned long addr, void *data) {
handle the RWX
}
static int kgdb_set_page_rw(unsigned long addr, int numpages, long action)

{
        ...
       apply_to_existing_page_range(&init_mm, start, size,
                                            change_page_attr, (void *)action);

      flush_tlb_kernel_range(addr, addr + size);
      ...

}

int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)

{
        ...
       kgdb_set_page_rw(bpt->bpt_addr,1,0);
       err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
                                arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
       kgdb_set_page_rw_(bpt->bpt_addr,1,1);
       ...
}

static int do_single_step(struct pt_regs *regs)

{
        ...
        kgdb_set_page_rw(bpt->bpt_addr,1,0);
        error = copy_to_kernel_nofault((void *)stepped_address,
                                       arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
        kgdb_set_page_rw_(bpt->bpt_addr,1,1);
        ...
}

3,uprobes write to userspace process code segments, which does not conflict with kernel code segment modifications. Therefore, changes for STRICT_MODULE_RWX do not affect uprobes.

Thanks!

Quoted 136 lines
Quoted 135 lines

For uprobes:

I will check the call path again.

For test:

I have already done some tests using BPF tools that utilize kprobes and fentry. I will run more tests using the selftests suite.

Thanks !

Quoted 124 lines

---
arch/loongarch/Kconfig | 1 + arch/loongarch/kernel/ftrace_dyn.c | 7 ++++++- arch/loongarch/kernel/inst.c | 25 +++++++++++++++++++++---- arch/loongarch/kernel/jump_label.c | 3 +++

4 files changed, 31 insertions(+), 5 deletions(-)
arch/loongarch/Kconfig +0 -0
index 606597da46b8..c751d714c287 100644
@@ -27,6 +27,7 @@ config LOONGARCH

select ARCH_HAS_PTE_SPECIAL if 64BIT
select ARCH_HAS_SET_MEMORY
select ARCH_HAS_SET_DIRECT_MAP
+ select ARCH_HAS_STRICT_MODULE_RWX
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN
select ARCH_HAS_VDSO_ARCH_DATA

arch/loongarch/kernel/ftrace_dyn.c +6 -1
index d5d81d74034c..598dc6434cc4 100644
@@ -8,6 +8,7 @@
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
+#include <linux/memory.h>
 
 #include <asm/inst.h>
 #include <asm/module.h>
@@ -24,8 +25,12 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate)
 return -EINVAL;
 }
 
- if (larch_insn_patch_text((void *)pc, new))
+ mutex_lock(&text_mutex);
+ if (larch_insn_patch_text((void *)pc, new)) {
+ mutex_unlock(&text_mutex);
 return -EPERM;
+ }
+ mutex_unlock(&text_mutex);
 
 return 0;
 }
arch/loongarch/kernel/inst.c +21 -4
index 0b9228b7c13a..3de94d465c3c 100644
@@ -6,12 +6,11 @@
 #include <linux/uaccess.h>
 #include <linux/set_memory.h>
 #include <linux/stop_machine.h>
+#include <linux/memory.h>
 
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
-static DEFINE_RAW_SPINLOCK(patch_lock);
-
 void simu_pc(struct pt_regs *regs, union loongarch_instruction insn)
 {
 unsigned long pc = regs->csr_era;
@@ -207,14 +206,32 @@ int larch_insn_read(void *addr, u32 *insnp)
 int larch_insn_write(void *addr, u32 insn)
 {
 int ret;
+ int err = 0;
+ size_t start;
 unsigned long flags = 0;
 
 if ((unsigned long)addr & 3)
 return -EINVAL;
 
- raw_spin_lock_irqsave(&patch_lock, flags);
+ start = round_down((size_t)addr, PAGE_SIZE);
+
+ lockdep_assert_held(&text_mutex);
+
+ err = set_memory_rw(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rw() failed\n", __func__);
+ return err;
+ }
+
+ local_irq_save(flags);
 ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
- raw_spin_unlock_irqrestore(&patch_lock, flags);
+ local_irq_restore(flags);
+
+ err = set_memory_rox(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rox() failed\n", __func__);
+ return err;
+ }
 
 return ret;
 }
arch/loongarch/kernel/jump_label.c +1 -0
index 24a3f4d8540c..e6bb040fe4c5 100644
@@ -6,6 +6,7 @@
 */
 #include <linux/kernel.h>
 #include <linux/jump_label.h>
+#include <linux/memory.h>
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
@@ -19,7 +20,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry, enum jump_label_t

else
insn = larch_insn_gen_nop();

+ mutex_lock(&text_mutex);
larch_insn_write(addr, insn);
+ mutex_unlock(&text_mutex);

return true;
}
--
2.43.0

maintainer
#5 lore raw
Quoted 277 lines · On Sun, Jun 21, 2026 at 8:12 AM <[email protected]> wrote:

2026年6月14日 20:10, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 212 lines · On Sun, Jun 14, 2026 at 6:24 PM <[email protected]> wrote:
Quoted 186 lines

2026年6月13日 18:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Hi, Haoran,

Quoted 147 lines · On Sat, Jun 13, 2026 at 4:42 PM <[email protected]> wrote:

From: Haoran Jiang <[email protected]>

Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.

---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.

v3:
Modify commit description.

v4:
Add text_mutex lock in the larch_insn_write call path and CONFIG_STRICT_MODULE_RWX is enabled by default.

UB test on the 3C6000 server shows no significant performance impact.

Before patch:

========================================================================
BYTE UNIX Benchmarks (Version 5.1.6)

System: localhost.localdomain: GNU/Linux
OS: GNU/Linux -- 7.1.0-rc6 -- #1 SMP PREEMPT Wed Jun 10 21:07:41 CST 2026
Machine: loongarch64 (loongarch64)
Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
21:19:51 up 1 min, 2 users, load average: 0.71, 0.38, 0.14; runlevel 2026-06-10

------------------------------------------------------------------------
128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35205725.4 lps (10.0 s, 7 samples) Double-Precision Whetstone 4244.9 MWIPS (10.0 s, 7 samples) Execl Throughput 6717.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1213873.8 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 350740.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3275103.0 KBps (30.0 s, 2 samples) Pipe Throughput 1981993.9 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55287.7 lps (10.0 s, 7 samples) Process Creation 9056.8 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6736.5 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2109.8 lpm (60.0 s, 2 samples) System Call Overhead 1549110.9 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35205725.4 3016.8 Double-Precision Whetstone 55.0 4244.9 771.8
Execl Throughput 43.0 6717.7 1562.3
File Copy 1024 bufsize 2000 maxblocks 3960.0 1213873.8 3065.3 File Copy 256 bufsize 500 maxblocks 1655.0 350740.5 2119.3 File Copy 4096 bufsize 8000 maxblocks 5800.0 3275103.0 5646.7 Pipe Throughput 12440.0 1981993.9 1593.2
Pipe-based Context Switching 4000.0 55287.7 138.2 Process Creation 126.0 9056.8 718.8
Shell Scripts (1 concurrent) 42.4 6736.5 1588.8
Shell Scripts (8 concurrent) 6.0 2109.8 3516.4
System Call Overhead 15000.0 1549110.9 1032.7
========
System Benchmarks Index Score 1492.2

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2901925470.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 503614.9 MWIPS (10.1 s, 7 samples) Execl Throughput 34080.1 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 309291.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 75115.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1018101.3 KBps (30.0 s, 2 samples) Pipe Throughput 180702003.1 lps (10.0 s, 7 samples) Pipe-based Context Switching 2426596.4 lps (10.0 s, 7 samples) Process Creation 37282.5 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 50813.7 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5835.8 lpm (60.4 s, 2 samples) System Call Overhead 9039181.1 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2901925470.7 248665.4 Double-Precision Whetstone 55.0 503614.9 91566.3
Execl Throughput 43.0 34080.1 7925.6
File Copy 1024 bufsize 2000 maxblocks 3960.0 309291.4 781.0 File Copy 256 bufsize 500 maxblocks 1655.0 75115.3 453.9 File Copy 4096 bufsize 8000 maxblocks 5800.0 1018101.3 1755.3 Pipe Throughput 12440.0 180702003.1 145258.8
Pipe-based Context Switching 4000.0 2426596.4 6066.5 Process Creation 126.0 37282.5 2958.9
Shell Scripts (1 concurrent) 42.4 50813.7 11984.4
Shell Scripts (8 concurrent) 6.0 5835.8 9726.4
System Call Overhead 15000.0 9039181.1 6026.1
========
System Benchmarks Index Score 8765.2

After patch:

128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35438193.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 4245.7 MWIPS (10.0 s, 7 samples) Execl Throughput 5293.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1233323.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 355264.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3333631.6 KBps (30.0 s, 2 samples) Pipe Throughput 1979613.2 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55675.2 lps (10.0 s, 7 samples) Process Creation 8528.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6870.0 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2115.5 lpm (60.0 s, 2 samples) System Call Overhead 1546959.4 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35438193.7 3036.7 Double-Precision Whetstone 55.0 4245.7 772.0
Execl Throughput 43.0 5293.7 1231.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 1233323.4 3114.5 File Copy 256 bufsize 500 maxblocks 1655.0 355264.5 2146.6 File Copy 4096 bufsize 8000 maxblocks 5800.0 3333631.6 5747.6 Pipe Throughput 12440.0 1979613.2 1591.3
Pipe-based Context Switching 4000.0 55675.2 139.2 Process Creation 126.0 8528.1 676.8
Shell Scripts (1 concurrent) 42.4 6870.0 1620.3
Shell Scripts (8 concurrent) 6.0 2115.5 3525.8
System Call Overhead 15000.0 1546959.4 1031.3
========
System Benchmarks Index Score 1465.3

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2903340286.5 lps (10.0 s, 7 samples) Double-Precision Whetstone 504137.7 MWIPS (10.1 s, 7 samples) Execl Throughput 34332.8 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 311391.2 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 72503.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1000861.7 KBps (30.0 s, 2 samples) Pipe Throughput 179382076.6 lps (10.0 s, 7 samples) Pipe-based Context Switching 2415716.6 lps (10.0 s, 7 samples) Process Creation 36873.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 51464.1 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5976.3 lpm (60.4 s, 2 samples) System Call Overhead 9182389.5 lps (10.0 s, 7 samples)

Quoted 16 lines

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2903340286.5 248786.7 Double-Precision Whetstone 55.0 504137.7 91661.4
Execl Throughput 43.0 34332.8 7984.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 311391.2 786.3 File Copy 256 bufsize 500 maxblocks 1655.0 72503.3 438.1 File Copy 4096 bufsize 8000 maxblocks 5800.0 1000861.7 1725.6 Pipe Throughput 12440.0 179382076.6 144197.8
Pipe-based Context Switching 4000.0 2415716.6 6039.3 Process Creation 126.0 36873.1 2926.4
Shell Scripts (1 concurrent) 42.4 51464.1 12137.8
Shell Scripts (8 concurrent) 6.0 5976.3 9960.6
System Call Overhead 15000.0 9182389.5 6121.6
========
System Benchmarks Index Score 8759.8

I think your tests are incomplete. Performance test is just a very basic test, you should make sure all dynamic code modification mechanisms are correct.

At least these should be verified one by one: jump label, kgdb, bpf, ftrace, kprobes, uprobes....

You have modified ftrace, but not the best method that Ihae suggested in V3.

You completely ignore my suggestion about kprobes in V3.

For KGDB, you should use text_mutex to protect copy_to_kernel_nofault().

For uprobes, I have no idea, maybe @Tiezhu can give some suggestions.

Quoted 9 lines

Huacai

For fentry:

ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() are not in the call path of ftrace_init_nop(),

so the text_mutex lock was added in ftrace_modify_code(). However, I overlooked that ftrace_init_nop() also holds the text_mutex lock (e.g., on RISC-V). I am fixing this.

No, RISC-V is right, use ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() to protect all common cases, and use text_mutex to protect the only exception, ftrace_init_nop().

Quoted 5 lines

For kprobes:

text_mutex is already held in the caller arm_kprobe(), which then calls arch_arm_kprobe->larch_insn_text_copy. So the kprobes path is properly protected.

No, I doubt you didn't see my link, we are talking about arch_prepare_ss_slot(). Though it seems the framework has hold text_mutex, but you don't use the larch_insn_patch_text() API, who handle the RWX?

Quoted 1 line
Quoted 9 lines
Quoted 4 lines

For kgdb:

When KGDB enters do_single_step(), it has already stopped execution on all other CPUs. Only the current CPU is running KGDB logic. Do we need to worry about concurrency issues in this case? Also, Sleepable lock is also disallowed in exception context.

The same as above, if you use copy_to_kernel_nofault() directly, who handle the RWX?

Huacai

I plan to make the following modifications. Is this approach feasible?

1, For kprobes:

Add modifications for arch_prepare_ss_slot(struct kprobe *p), referencing the patch at:
https://lore.kernel.org/loongarch/CAHirt9gSXZvw0wTv9zupsVRG0_ekza08efW0hwq_yvnU1WbUng@mail.gmail.com/T/#m6a75415f468e671748d6ec8c6b502ca36788c546

static void arch_prepare_ss_slot(struct kprobe *p)

 {
    larch_insn_patch_text(p->ainsn.insn, *p->addr);
    larch_insn_patch_text(p->ainsn.insn + 1, KPROBE_SSTEPBP_INSN);
    p->ainsn.restore = (unsigned long)p->addr + LOONGARCH_INSN_SIZE;

}

2,For kgdb:

Modify page table permissions for the kernel code section by adding the following lockless method. Use apply_to_existing_page_range() to iterate over the virtual address range and perform permission modification operations on existing page table entries (PTEs).

This approach references the RISC-V commit:
fb1cf0878328 ("riscv: rewrite __kernel_map_pages() to fix sleeping in invalid context")

static int change_page_attr(pte_t *ptep, unsigned long addr, void *data) {
handle the RWX
}
static int kgdb_set_page_rw(unsigned long addr, int numpages, long action)

{
        ...
       apply_to_existing_page_range(&init_mm, start, size,
                                            change_page_attr, (void *)action);

      flush_tlb_kernel_range(addr, addr + size);
      ...

}

int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)

{
        ...
       kgdb_set_page_rw(bpt->bpt_addr,1,0);
       err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
                                arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
       kgdb_set_page_rw_(bpt->bpt_addr,1,1);
       ...
}

static int do_single_step(struct pt_regs *regs)

Quoted 8 lines
{
        ...
        kgdb_set_page_rw(bpt->bpt_addr,1,0);
        error = copy_to_kernel_nofault((void *)stepped_address,
                                       arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
        kgdb_set_page_rw_(bpt->bpt_addr,1,1);
        ...
}

So complicated? Why not replace copy_to_kernel_nofault() with larch_insn_write() directly?

Quoted 2 lines

3,uprobes write to userspace process code segments, which does not conflict with kernel code segment modifications. Therefore, changes for STRICT_MODULE_RWX do not affect uprobes.

@Tiezhu, what's your opinion?

Huacai

Quoted 143 lines

Thanks!

Quoted 136 lines
Quoted 135 lines

For uprobes:

I will check the call path again.

For test:

I have already done some tests using BPF tools that utilize kprobes and fentry. I will run more tests using the selftests suite.

Thanks !

Quoted 124 lines

---
arch/loongarch/Kconfig | 1 + arch/loongarch/kernel/ftrace_dyn.c | 7 ++++++- arch/loongarch/kernel/inst.c | 25 +++++++++++++++++++++---- arch/loongarch/kernel/jump_label.c | 3 +++

4 files changed, 31 insertions(+), 5 deletions(-)
arch/loongarch/Kconfig +0 -0
index 606597da46b8..c751d714c287 100644
@@ -27,6 +27,7 @@ config LOONGARCH

select ARCH_HAS_PTE_SPECIAL if 64BIT
select ARCH_HAS_SET_MEMORY
select ARCH_HAS_SET_DIRECT_MAP
+ select ARCH_HAS_STRICT_MODULE_RWX
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN
select ARCH_HAS_VDSO_ARCH_DATA

arch/loongarch/kernel/ftrace_dyn.c +6 -1
index d5d81d74034c..598dc6434cc4 100644
@@ -8,6 +8,7 @@
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
+#include <linux/memory.h>
 
 #include <asm/inst.h>
 #include <asm/module.h>
@@ -24,8 +25,12 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate)
 return -EINVAL;
 }
 
- if (larch_insn_patch_text((void *)pc, new))
+ mutex_lock(&text_mutex);
+ if (larch_insn_patch_text((void *)pc, new)) {
+ mutex_unlock(&text_mutex);
 return -EPERM;
+ }
+ mutex_unlock(&text_mutex);
 
 return 0;
 }
arch/loongarch/kernel/inst.c +21 -4
index 0b9228b7c13a..3de94d465c3c 100644
@@ -6,12 +6,11 @@
 #include <linux/uaccess.h>
 #include <linux/set_memory.h>
 #include <linux/stop_machine.h>
+#include <linux/memory.h>
 
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
-static DEFINE_RAW_SPINLOCK(patch_lock);
-
 void simu_pc(struct pt_regs *regs, union loongarch_instruction insn)
 {
 unsigned long pc = regs->csr_era;
@@ -207,14 +206,32 @@ int larch_insn_read(void *addr, u32 *insnp)
 int larch_insn_write(void *addr, u32 insn)
 {
 int ret;
+ int err = 0;
+ size_t start;
 unsigned long flags = 0;
 
 if ((unsigned long)addr & 3)
 return -EINVAL;
 
- raw_spin_lock_irqsave(&patch_lock, flags);
+ start = round_down((size_t)addr, PAGE_SIZE);
+
+ lockdep_assert_held(&text_mutex);
+
+ err = set_memory_rw(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rw() failed\n", __func__);
+ return err;
+ }
+
+ local_irq_save(flags);
 ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
- raw_spin_unlock_irqrestore(&patch_lock, flags);
+ local_irq_restore(flags);
+
+ err = set_memory_rox(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rox() failed\n", __func__);
+ return err;
+ }
 
 return ret;
 }
arch/loongarch/kernel/jump_label.c +1 -0
index 24a3f4d8540c..e6bb040fe4c5 100644
@@ -6,6 +6,7 @@
 */
 #include <linux/kernel.h>
 #include <linux/jump_label.h>
+#include <linux/memory.h>
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
@@ -19,7 +20,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry, enum jump_label_t

else
insn = larch_insn_gen_nop();

+ mutex_lock(&text_mutex);
larch_insn_write(addr, insn);
+ mutex_unlock(&text_mutex);

return true;
}
--
2.43.0

#6 lore raw

2026年6月22日 10:29, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 283 lines · On Sun, Jun 21, 2026 at 8:12 AM <[email protected]> wrote:
Quoted 271 lines

2026年6月14日 20:10, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 185 lines · On Sun, Jun 14, 2026 at 6:24 PM <[email protected]> wrote:

2026年6月13日 18:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Hi, Haoran,

Quoted 147 lines · On Sat, Jun 13, 2026 at 4:42 PM <[email protected]> wrote:

From: Haoran Jiang <[email protected]>

Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.

---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.

v3:
Modify commit description.

v4:
Add text_mutex lock in the larch_insn_write call path and CONFIG_STRICT_MODULE_RWX is enabled by default.

UB test on the 3C6000 server shows no significant performance impact.

Before patch:

========================================================================
BYTE UNIX Benchmarks (Version 5.1.6)

System: localhost.localdomain: GNU/Linux
OS: GNU/Linux -- 7.1.0-rc6 -- #1 SMP PREEMPT Wed Jun 10 21:07:41 CST 2026
Machine: loongarch64 (loongarch64)
Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
21:19:51 up 1 min, 2 users, load average: 0.71, 0.38, 0.14; runlevel 2026-06-10

------------------------------------------------------------------------
128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35205725.4 lps (10.0 s, 7 samples) Double-Precision Whetstone 4244.9 MWIPS (10.0 s, 7 samples) Execl Throughput 6717.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1213873.8 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 350740.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3275103.0 KBps (30.0 s, 2 samples) Pipe Throughput 1981993.9 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55287.7 lps (10.0 s, 7 samples) Process Creation 9056.8 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6736.5 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2109.8 lpm (60.0 s, 2 samples) System Call Overhead 1549110.9 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35205725.4 3016.8 Double-Precision Whetstone 55.0 4244.9 771.8
Execl Throughput 43.0 6717.7 1562.3
File Copy 1024 bufsize 2000 maxblocks 3960.0 1213873.8 3065.3 File Copy 256 bufsize 500 maxblocks 1655.0 350740.5 2119.3 File Copy 4096 bufsize 8000 maxblocks 5800.0 3275103.0 5646.7 Pipe Throughput 12440.0 1981993.9 1593.2
Pipe-based Context Switching 4000.0 55287.7 138.2 Process Creation 126.0 9056.8 718.8
Shell Scripts (1 concurrent) 42.4 6736.5 1588.8
Shell Scripts (8 concurrent) 6.0 2109.8 3516.4
System Call Overhead 15000.0 1549110.9 1032.7
========
System Benchmarks Index Score 1492.2

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2901925470.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 503614.9 MWIPS (10.1 s, 7 samples) Execl Throughput 34080.1 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 309291.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 75115.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1018101.3 KBps (30.0 s, 2 samples) Pipe Throughput 180702003.1 lps (10.0 s, 7 samples) Pipe-based Context Switching 2426596.4 lps (10.0 s, 7 samples) Process Creation 37282.5 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 50813.7 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5835.8 lpm (60.4 s, 2 samples) System Call Overhead 9039181.1 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2901925470.7 248665.4 Double-Precision Whetstone 55.0 503614.9 91566.3
Execl Throughput 43.0 34080.1 7925.6
File Copy 1024 bufsize 2000 maxblocks 3960.0 309291.4 781.0 File Copy 256 bufsize 500 maxblocks 1655.0 75115.3 453.9 File Copy 4096 bufsize 8000 maxblocks 5800.0 1018101.3 1755.3 Pipe Throughput 12440.0 180702003.1 145258.8
Pipe-based Context Switching 4000.0 2426596.4 6066.5 Process Creation 126.0 37282.5 2958.9
Shell Scripts (1 concurrent) 42.4 50813.7 11984.4
Shell Scripts (8 concurrent) 6.0 5835.8 9726.4
System Call Overhead 15000.0 9039181.1 6026.1
========
System Benchmarks Index Score 8765.2

After patch:

128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35438193.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 4245.7 MWIPS (10.0 s, 7 samples) Execl Throughput 5293.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1233323.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 355264.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3333631.6 KBps (30.0 s, 2 samples) Pipe Throughput 1979613.2 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55675.2 lps (10.0 s, 7 samples) Process Creation 8528.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6870.0 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2115.5 lpm (60.0 s, 2 samples) System Call Overhead 1546959.4 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35438193.7 3036.7 Double-Precision Whetstone 55.0 4245.7 772.0
Execl Throughput 43.0 5293.7 1231.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 1233323.4 3114.5 File Copy 256 bufsize 500 maxblocks 1655.0 355264.5 2146.6 File Copy 4096 bufsize 8000 maxblocks 5800.0 3333631.6 5747.6 Pipe Throughput 12440.0 1979613.2 1591.3
Pipe-based Context Switching 4000.0 55675.2 139.2 Process Creation 126.0 8528.1 676.8
Shell Scripts (1 concurrent) 42.4 6870.0 1620.3
Shell Scripts (8 concurrent) 6.0 2115.5 3525.8
System Call Overhead 15000.0 1546959.4 1031.3
========
System Benchmarks Index Score 1465.3

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2903340286.5 lps (10.0 s, 7 samples) Double-Precision Whetstone 504137.7 MWIPS (10.1 s, 7 samples) Execl Throughput 34332.8 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 311391.2 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 72503.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1000861.7 KBps (30.0 s, 2 samples) Pipe Throughput 179382076.6 lps (10.0 s, 7 samples) Pipe-based Context Switching 2415716.6 lps (10.0 s, 7 samples) Process Creation 36873.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 51464.1 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5976.3 lpm (60.4 s, 2 samples) System Call Overhead 9182389.5 lps (10.0 s, 7 samples)

Quoted 16 lines

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2903340286.5 248786.7 Double-Precision Whetstone 55.0 504137.7 91661.4
Execl Throughput 43.0 34332.8 7984.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 311391.2 786.3 File Copy 256 bufsize 500 maxblocks 1655.0 72503.3 438.1 File Copy 4096 bufsize 8000 maxblocks 5800.0 1000861.7 1725.6 Pipe Throughput 12440.0 179382076.6 144197.8
Pipe-based Context Switching 4000.0 2415716.6 6039.3 Process Creation 126.0 36873.1 2926.4
Shell Scripts (1 concurrent) 42.4 51464.1 12137.8
Shell Scripts (8 concurrent) 6.0 5976.3 9960.6
System Call Overhead 15000.0 9182389.5 6121.6
========
System Benchmarks Index Score 8759.8

I think your tests are incomplete. Performance test is just a very basic test, you should make sure all dynamic code modification mechanisms are correct.

At least these should be verified one by one: jump label, kgdb, bpf, ftrace, kprobes, uprobes....

You have modified ftrace, but not the best method that Ihae suggested in V3.

You completely ignore my suggestion about kprobes in V3.

For KGDB, you should use text_mutex to protect copy_to_kernel_nofault().

For uprobes, I have no idea, maybe @Tiezhu can give some suggestions.

Quoted 9 lines

Huacai

For fentry:

ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() are not in the call path of ftrace_init_nop(),

so the text_mutex lock was added in ftrace_modify_code(). However, I overlooked that ftrace_init_nop() also holds the text_mutex lock (e.g., on RISC-V). I am fixing this.

No, RISC-V is right, use ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() to protect all common cases, and use text_mutex to protect the only exception, ftrace_init_nop().

Quoted 5 lines

For kprobes:

text_mutex is already held in the caller arm_kprobe(), which then calls arch_arm_kprobe->larch_insn_text_copy. So the kprobes path is properly protected.

No, I doubt you didn't see my link, we are talking about arch_prepare_ss_slot(). Though it seems the framework has hold text_mutex, but you don't use the larch_insn_patch_text() API, who handle the RWX?

Quoted 5 lines

For kgdb:

When KGDB enters do_single_step(), it has already stopped execution on all other CPUs. Only the current CPU is running KGDB logic. Do we need to worry about concurrency issues in this case? Also, Sleepable lock is also disallowed in exception context.

The same as above, if you use copy_to_kernel_nofault() directly, who handle the RWX?

Huacai

I plan to make the following modifications. Is this approach feasible?

1, For kprobes:

Add modifications for arch_prepare_ss_slot(struct kprobe *p), referencing the patch at:
https://lore.kernel.org/loongarch/CAHirt9gSXZvw0wTv9zupsVRG0_ekza08efW0hwq_yvnU1WbUng@mail.gmail.com/T/#m6a75415f468e671748d6ec8c6b502ca36788c546

static void arch_prepare_ss_slot(struct kprobe *p)
{
larch_insn_patch_text(p->ainsn.insn, *p->addr); larch_insn_patch_text(p->ainsn.insn + 1, KPROBE_SSTEPBP_INSN); p->ainsn.restore = (unsigned long)p->addr + LOONGARCH_INSN_SIZE; }

2,For kgdb:

Modify page table permissions for the kernel code section by adding the following lockless method. Use apply_to_existing_page_range() to iterate over the virtual address range and perform permission modification operations on existing page table entries (PTEs).

This approach references the RISC-V commit:
fb1cf0878328 ("riscv: rewrite __kernel_map_pages() to fix sleeping in invalid context")

static int change_page_attr(pte_t *ptep, unsigned long addr, void *data) {
handle the RWX
}
static int kgdb_set_page_rw(unsigned long addr, int numpages, long action) {
...
apply_to_existing_page_range(&init_mm, start, size, change_page_attr, (void *)action);

flush_tlb_kernel_range(addr, addr + size);
...

}

int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) {
...
kgdb_set_page_rw(bpt->bpt_addr,1,0);
err = copy_to_kernel_nofault((char *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); kgdb_set_page_rw_(bpt->bpt_addr,1,1);
...
}

Quoted 10 lines

static int do_single_step(struct pt_regs *regs) {
...
kgdb_set_page_rw(bpt->bpt_addr,1,0);
error = copy_to_kernel_nofault((void *)stepped_address, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); kgdb_set_page_rw_(bpt->bpt_addr,1,1);
...
}

Quoted 3 lines

So complicated? Why not replace copy_to_kernel_nofault() with larch_insn_write() directly?

larch_insn_write->set_memory_rw->__set_memory->mmap_write_lock(&init_mm)

The call path holds a lock that may sleep. Invoking larch_insn_write() within the KGDB exception context is risky, as the context does not permit sleeping.

Quoted 147 lines
Quoted 3 lines

3,uprobes write to userspace process code segments, which does not conflict with kernel code segment modifications. Therefore, changes for STRICT_MODULE_RWX do not affect uprobes.

@Tiezhu, what's your opinion?

Huacai

Quoted 139 lines

Thanks!

Quoted 135 lines

For uprobes:

I will check the call path again.

For test:

I have already done some tests using BPF tools that utilize kprobes and fentry. I will run more tests using the selftests suite.

Thanks !

Quoted 124 lines

---
arch/loongarch/Kconfig | 1 + arch/loongarch/kernel/ftrace_dyn.c | 7 ++++++- arch/loongarch/kernel/inst.c | 25 +++++++++++++++++++++---- arch/loongarch/kernel/jump_label.c | 3 +++

4 files changed, 31 insertions(+), 5 deletions(-)
arch/loongarch/Kconfig +0 -0
index 606597da46b8..c751d714c287 100644
@@ -27,6 +27,7 @@ config LOONGARCH

select ARCH_HAS_PTE_SPECIAL if 64BIT
select ARCH_HAS_SET_MEMORY
select ARCH_HAS_SET_DIRECT_MAP
+ select ARCH_HAS_STRICT_MODULE_RWX
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN
select ARCH_HAS_VDSO_ARCH_DATA

arch/loongarch/kernel/ftrace_dyn.c +6 -1
index d5d81d74034c..598dc6434cc4 100644
@@ -8,6 +8,7 @@
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
+#include <linux/memory.h>
 
 #include <asm/inst.h>
 #include <asm/module.h>
@@ -24,8 +25,12 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate)
 return -EINVAL;
 }
 
- if (larch_insn_patch_text((void *)pc, new))
+ mutex_lock(&text_mutex);
+ if (larch_insn_patch_text((void *)pc, new)) {
+ mutex_unlock(&text_mutex);
 return -EPERM;
+ }
+ mutex_unlock(&text_mutex);
 
 return 0;
 }
arch/loongarch/kernel/inst.c +21 -4
index 0b9228b7c13a..3de94d465c3c 100644
@@ -6,12 +6,11 @@
 #include <linux/uaccess.h>
 #include <linux/set_memory.h>
 #include <linux/stop_machine.h>
+#include <linux/memory.h>
 
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
-static DEFINE_RAW_SPINLOCK(patch_lock);
-
 void simu_pc(struct pt_regs *regs, union loongarch_instruction insn)
 {
 unsigned long pc = regs->csr_era;
@@ -207,14 +206,32 @@ int larch_insn_read(void *addr, u32 *insnp)
 int larch_insn_write(void *addr, u32 insn)
 {
 int ret;
+ int err = 0;
+ size_t start;
 unsigned long flags = 0;
 
 if ((unsigned long)addr & 3)
 return -EINVAL;
 
- raw_spin_lock_irqsave(&patch_lock, flags);
+ start = round_down((size_t)addr, PAGE_SIZE);
+
+ lockdep_assert_held(&text_mutex);
+
+ err = set_memory_rw(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rw() failed\n", __func__);
+ return err;
+ }
+
+ local_irq_save(flags);
 ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
- raw_spin_unlock_irqrestore(&patch_lock, flags);
+ local_irq_restore(flags);
+
+ err = set_memory_rox(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rox() failed\n", __func__);
+ return err;
+ }
 
 return ret;
 }
arch/loongarch/kernel/jump_label.c +1 -0
index 24a3f4d8540c..e6bb040fe4c5 100644
@@ -6,6 +6,7 @@
 */
 #include <linux/kernel.h>
 #include <linux/jump_label.h>
+#include <linux/memory.h>
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
@@ -19,7 +20,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry, enum jump_label_t

else
insn = larch_insn_gen_nop();

+ mutex_lock(&text_mutex);
larch_insn_write(addr, insn);
+ mutex_unlock(&text_mutex);

return true;
}
--
2.43.0

maintainer
#7 lore raw
Quoted 287 lines · On Tue, Jun 23, 2026 at 5:33 PM <[email protected]> wrote:

2026年6月22日 10:29, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 283 lines · On Sun, Jun 21, 2026 at 8:12 AM <[email protected]> wrote:
Quoted 281 lines

2026年6月14日 20:10, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 185 lines · On Sun, Jun 14, 2026 at 6:24 PM <[email protected]> wrote:

2026年6月13日 18:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Hi, Haoran,

Quoted 147 lines · On Sat, Jun 13, 2026 at 4:42 PM <[email protected]> wrote:

From: Haoran Jiang <[email protected]>

Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.

---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.

v3:
Modify commit description.

v4:
Add text_mutex lock in the larch_insn_write call path and CONFIG_STRICT_MODULE_RWX is enabled by default.

UB test on the 3C6000 server shows no significant performance impact.

Before patch:

========================================================================
BYTE UNIX Benchmarks (Version 5.1.6)

System: localhost.localdomain: GNU/Linux
OS: GNU/Linux -- 7.1.0-rc6 -- #1 SMP PREEMPT Wed Jun 10 21:07:41 CST 2026
Machine: loongarch64 (loongarch64)
Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
21:19:51 up 1 min, 2 users, load average: 0.71, 0.38, 0.14; runlevel 2026-06-10

------------------------------------------------------------------------
128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35205725.4 lps (10.0 s, 7 samples) Double-Precision Whetstone 4244.9 MWIPS (10.0 s, 7 samples) Execl Throughput 6717.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1213873.8 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 350740.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3275103.0 KBps (30.0 s, 2 samples) Pipe Throughput 1981993.9 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55287.7 lps (10.0 s, 7 samples) Process Creation 9056.8 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6736.5 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2109.8 lpm (60.0 s, 2 samples) System Call Overhead 1549110.9 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35205725.4 3016.8 Double-Precision Whetstone 55.0 4244.9 771.8
Execl Throughput 43.0 6717.7 1562.3
File Copy 1024 bufsize 2000 maxblocks 3960.0 1213873.8 3065.3 File Copy 256 bufsize 500 maxblocks 1655.0 350740.5 2119.3 File Copy 4096 bufsize 8000 maxblocks 5800.0 3275103.0 5646.7 Pipe Throughput 12440.0 1981993.9 1593.2
Pipe-based Context Switching 4000.0 55287.7 138.2 Process Creation 126.0 9056.8 718.8
Shell Scripts (1 concurrent) 42.4 6736.5 1588.8
Shell Scripts (8 concurrent) 6.0 2109.8 3516.4
System Call Overhead 15000.0 1549110.9 1032.7
========
System Benchmarks Index Score 1492.2

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2901925470.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 503614.9 MWIPS (10.1 s, 7 samples) Execl Throughput 34080.1 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 309291.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 75115.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1018101.3 KBps (30.0 s, 2 samples) Pipe Throughput 180702003.1 lps (10.0 s, 7 samples) Pipe-based Context Switching 2426596.4 lps (10.0 s, 7 samples) Process Creation 37282.5 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 50813.7 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5835.8 lpm (60.4 s, 2 samples) System Call Overhead 9039181.1 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2901925470.7 248665.4 Double-Precision Whetstone 55.0 503614.9 91566.3
Execl Throughput 43.0 34080.1 7925.6
File Copy 1024 bufsize 2000 maxblocks 3960.0 309291.4 781.0 File Copy 256 bufsize 500 maxblocks 1655.0 75115.3 453.9 File Copy 4096 bufsize 8000 maxblocks 5800.0 1018101.3 1755.3 Pipe Throughput 12440.0 180702003.1 145258.8
Pipe-based Context Switching 4000.0 2426596.4 6066.5 Process Creation 126.0 37282.5 2958.9
Shell Scripts (1 concurrent) 42.4 50813.7 11984.4
Shell Scripts (8 concurrent) 6.0 5835.8 9726.4
System Call Overhead 15000.0 9039181.1 6026.1
========
System Benchmarks Index Score 8765.2

After patch:

128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35438193.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 4245.7 MWIPS (10.0 s, 7 samples) Execl Throughput 5293.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1233323.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 355264.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3333631.6 KBps (30.0 s, 2 samples) Pipe Throughput 1979613.2 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55675.2 lps (10.0 s, 7 samples) Process Creation 8528.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6870.0 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2115.5 lpm (60.0 s, 2 samples) System Call Overhead 1546959.4 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35438193.7 3036.7 Double-Precision Whetstone 55.0 4245.7 772.0
Execl Throughput 43.0 5293.7 1231.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 1233323.4 3114.5 File Copy 256 bufsize 500 maxblocks 1655.0 355264.5 2146.6 File Copy 4096 bufsize 8000 maxblocks 5800.0 3333631.6 5747.6 Pipe Throughput 12440.0 1979613.2 1591.3
Pipe-based Context Switching 4000.0 55675.2 139.2 Process Creation 126.0 8528.1 676.8
Shell Scripts (1 concurrent) 42.4 6870.0 1620.3
Shell Scripts (8 concurrent) 6.0 2115.5 3525.8
System Call Overhead 15000.0 1546959.4 1031.3
========
System Benchmarks Index Score 1465.3

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2903340286.5 lps (10.0 s, 7 samples) Double-Precision Whetstone 504137.7 MWIPS (10.1 s, 7 samples) Execl Throughput 34332.8 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 311391.2 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 72503.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1000861.7 KBps (30.0 s, 2 samples) Pipe Throughput 179382076.6 lps (10.0 s, 7 samples) Pipe-based Context Switching 2415716.6 lps (10.0 s, 7 samples) Process Creation 36873.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 51464.1 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5976.3 lpm (60.4 s, 2 samples) System Call Overhead 9182389.5 lps (10.0 s, 7 samples)

Quoted 16 lines

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2903340286.5 248786.7 Double-Precision Whetstone 55.0 504137.7 91661.4
Execl Throughput 43.0 34332.8 7984.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 311391.2 786.3 File Copy 256 bufsize 500 maxblocks 1655.0 72503.3 438.1 File Copy 4096 bufsize 8000 maxblocks 5800.0 1000861.7 1725.6 Pipe Throughput 12440.0 179382076.6 144197.8
Pipe-based Context Switching 4000.0 2415716.6 6039.3 Process Creation 126.0 36873.1 2926.4
Shell Scripts (1 concurrent) 42.4 51464.1 12137.8
Shell Scripts (8 concurrent) 6.0 5976.3 9960.6
System Call Overhead 15000.0 9182389.5 6121.6
========
System Benchmarks Index Score 8759.8

I think your tests are incomplete. Performance test is just a very basic test, you should make sure all dynamic code modification mechanisms are correct.

At least these should be verified one by one: jump label, kgdb, bpf, ftrace, kprobes, uprobes....

You have modified ftrace, but not the best method that Ihae suggested in V3.

You completely ignore my suggestion about kprobes in V3.

For KGDB, you should use text_mutex to protect copy_to_kernel_nofault().

For uprobes, I have no idea, maybe @Tiezhu can give some suggestions.

Quoted 9 lines

Huacai

For fentry:

ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() are not in the call path of ftrace_init_nop(),

so the text_mutex lock was added in ftrace_modify_code(). However, I overlooked that ftrace_init_nop() also holds the text_mutex lock (e.g., on RISC-V). I am fixing this.

No, RISC-V is right, use ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() to protect all common cases, and use text_mutex to protect the only exception, ftrace_init_nop().

Quoted 5 lines

For kprobes:

text_mutex is already held in the caller arm_kprobe(), which then calls arch_arm_kprobe->larch_insn_text_copy. So the kprobes path is properly protected.

No, I doubt you didn't see my link, we are talking about arch_prepare_ss_slot(). Though it seems the framework has hold text_mutex, but you don't use the larch_insn_patch_text() API, who handle the RWX?

Quoted 5 lines

For kgdb:

When KGDB enters do_single_step(), it has already stopped execution on all other CPUs. Only the current CPU is running KGDB logic. Do we need to worry about concurrency issues in this case? Also, Sleepable lock is also disallowed in exception context.

The same as above, if you use copy_to_kernel_nofault() directly, who handle the RWX?

Huacai

I plan to make the following modifications. Is this approach feasible?

1, For kprobes:

Add modifications for arch_prepare_ss_slot(struct kprobe *p), referencing the patch at:
https://lore.kernel.org/loongarch/CAHirt9gSXZvw0wTv9zupsVRG0_ekza08efW0hwq_yvnU1WbUng@mail.gmail.com/T/#m6a75415f468e671748d6ec8c6b502ca36788c546

static void arch_prepare_ss_slot(struct kprobe *p)
{
larch_insn_patch_text(p->ainsn.insn, *p->addr); larch_insn_patch_text(p->ainsn.insn + 1, KPROBE_SSTEPBP_INSN); p->ainsn.restore = (unsigned long)p->addr + LOONGARCH_INSN_SIZE; }

2,For kgdb:

Modify page table permissions for the kernel code section by adding the following lockless method. Use apply_to_existing_page_range() to iterate over the virtual address range and perform permission modification operations on existing page table entries (PTEs).

This approach references the RISC-V commit:
fb1cf0878328 ("riscv: rewrite __kernel_map_pages() to fix sleeping in invalid context")

static int change_page_attr(pte_t *ptep, unsigned long addr, void *data) {
handle the RWX
}
static int kgdb_set_page_rw(unsigned long addr, int numpages, long action) {
...
apply_to_existing_page_range(&init_mm, start, size, change_page_attr, (void *)action);

flush_tlb_kernel_range(addr, addr + size);
...

}

int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) {
...
kgdb_set_page_rw(bpt->bpt_addr,1,0);
err = copy_to_kernel_nofault((char *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); kgdb_set_page_rw_(bpt->bpt_addr,1,1);
...
}

static int do_single_step(struct pt_regs *regs) {
...
kgdb_set_page_rw(bpt->bpt_addr,1,0);
error = copy_to_kernel_nofault((void *)stepped_address, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); kgdb_set_page_rw_(bpt->bpt_addr,1,1);
...
}

Quoted 8 lines
Quoted 3 lines

So complicated? Why not replace copy_to_kernel_nofault() with larch_insn_write() directly?

larch_insn_write->set_memory_rw->__set_memory->mmap_write_lock(&init_mm)

The call path holds a lock that may sleep. Invoking larch_insn_write() within the KGDB exception context is risky, as the context does not permit sleeping.

Why risky? __do_page_fault() is also in the exception context, but it holds sleepable locks.

Huacai

Quoted 148 lines
Quoted 148 lines
Quoted 4 lines

3,uprobes write to userspace process code segments, which does not conflict with kernel code segment modifications. Therefore, changes for STRICT_MODULE_RWX do not affect uprobes.

@Tiezhu, what's your opinion?

Huacai

Quoted 139 lines

Thanks!

Quoted 135 lines

For uprobes:

I will check the call path again.

For test:

I have already done some tests using BPF tools that utilize kprobes and fentry. I will run more tests using the selftests suite.

Thanks !

Quoted 124 lines

---
arch/loongarch/Kconfig | 1 + arch/loongarch/kernel/ftrace_dyn.c | 7 ++++++- arch/loongarch/kernel/inst.c | 25 +++++++++++++++++++++---- arch/loongarch/kernel/jump_label.c | 3 +++

4 files changed, 31 insertions(+), 5 deletions(-)
arch/loongarch/Kconfig +0 -0
index 606597da46b8..c751d714c287 100644
@@ -27,6 +27,7 @@ config LOONGARCH

select ARCH_HAS_PTE_SPECIAL if 64BIT
select ARCH_HAS_SET_MEMORY
select ARCH_HAS_SET_DIRECT_MAP
+ select ARCH_HAS_STRICT_MODULE_RWX
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN
select ARCH_HAS_VDSO_ARCH_DATA

arch/loongarch/kernel/ftrace_dyn.c +6 -1
index d5d81d74034c..598dc6434cc4 100644
@@ -8,6 +8,7 @@
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
+#include <linux/memory.h>
 
 #include <asm/inst.h>
 #include <asm/module.h>
@@ -24,8 +25,12 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate)
 return -EINVAL;
 }
 
- if (larch_insn_patch_text((void *)pc, new))
+ mutex_lock(&text_mutex);
+ if (larch_insn_patch_text((void *)pc, new)) {
+ mutex_unlock(&text_mutex);
 return -EPERM;
+ }
+ mutex_unlock(&text_mutex);
 
 return 0;
 }
arch/loongarch/kernel/inst.c +21 -4
index 0b9228b7c13a..3de94d465c3c 100644
@@ -6,12 +6,11 @@
 #include <linux/uaccess.h>
 #include <linux/set_memory.h>
 #include <linux/stop_machine.h>
+#include <linux/memory.h>
 
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
-static DEFINE_RAW_SPINLOCK(patch_lock);
-
 void simu_pc(struct pt_regs *regs, union loongarch_instruction insn)
 {
 unsigned long pc = regs->csr_era;
@@ -207,14 +206,32 @@ int larch_insn_read(void *addr, u32 *insnp)
 int larch_insn_write(void *addr, u32 insn)
 {
 int ret;
+ int err = 0;
+ size_t start;
 unsigned long flags = 0;
 
 if ((unsigned long)addr & 3)
 return -EINVAL;
 
- raw_spin_lock_irqsave(&patch_lock, flags);
+ start = round_down((size_t)addr, PAGE_SIZE);
+
+ lockdep_assert_held(&text_mutex);
+
+ err = set_memory_rw(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rw() failed\n", __func__);
+ return err;
+ }
+
+ local_irq_save(flags);
 ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
- raw_spin_unlock_irqrestore(&patch_lock, flags);
+ local_irq_restore(flags);
+
+ err = set_memory_rox(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rox() failed\n", __func__);
+ return err;
+ }
 
 return ret;
 }
arch/loongarch/kernel/jump_label.c +1 -0
index 24a3f4d8540c..e6bb040fe4c5 100644
@@ -6,6 +6,7 @@
 */
 #include <linux/kernel.h>
 #include <linux/jump_label.h>
+#include <linux/memory.h>
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
@@ -19,7 +20,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry, enum jump_label_t

else
insn = larch_insn_gen_nop();

+ mutex_lock(&text_mutex);
larch_insn_write(addr, insn);
+ mutex_unlock(&text_mutex);

return true;
}
--
2.43.0

#8 lore raw

2026年6月24日 22:33, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 293 lines · On Tue, Jun 23, 2026 at 5:33 PM <[email protected]> wrote:
Quoted 291 lines

2026年6月22日 10:29, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 270 lines · On Sun, Jun 21, 2026 at 8:12 AM <[email protected]> wrote:

2026年6月14日 20:10, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 185 lines · On Sun, Jun 14, 2026 at 6:24 PM <[email protected]> wrote:

2026年6月13日 18:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Hi, Haoran,

Quoted 147 lines · On Sat, Jun 13, 2026 at 4:42 PM <[email protected]> wrote:

From: Haoran Jiang <[email protected]>

Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.

---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.

v3:
Modify commit description.

v4:
Add text_mutex lock in the larch_insn_write call path and CONFIG_STRICT_MODULE_RWX is enabled by default.

UB test on the 3C6000 server shows no significant performance impact.

Before patch:

========================================================================
BYTE UNIX Benchmarks (Version 5.1.6)

System: localhost.localdomain: GNU/Linux
OS: GNU/Linux -- 7.1.0-rc6 -- #1 SMP PREEMPT Wed Jun 10 21:07:41 CST 2026
Machine: loongarch64 (loongarch64)
Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
21:19:51 up 1 min, 2 users, load average: 0.71, 0.38, 0.14; runlevel 2026-06-10

------------------------------------------------------------------------
128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35205725.4 lps (10.0 s, 7 samples) Double-Precision Whetstone 4244.9 MWIPS (10.0 s, 7 samples) Execl Throughput 6717.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1213873.8 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 350740.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3275103.0 KBps (30.0 s, 2 samples) Pipe Throughput 1981993.9 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55287.7 lps (10.0 s, 7 samples) Process Creation 9056.8 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6736.5 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2109.8 lpm (60.0 s, 2 samples) System Call Overhead 1549110.9 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35205725.4 3016.8 Double-Precision Whetstone 55.0 4244.9 771.8
Execl Throughput 43.0 6717.7 1562.3
File Copy 1024 bufsize 2000 maxblocks 3960.0 1213873.8 3065.3 File Copy 256 bufsize 500 maxblocks 1655.0 350740.5 2119.3 File Copy 4096 bufsize 8000 maxblocks 5800.0 3275103.0 5646.7 Pipe Throughput 12440.0 1981993.9 1593.2
Pipe-based Context Switching 4000.0 55287.7 138.2 Process Creation 126.0 9056.8 718.8
Shell Scripts (1 concurrent) 42.4 6736.5 1588.8
Shell Scripts (8 concurrent) 6.0 2109.8 3516.4
System Call Overhead 15000.0 1549110.9 1032.7
========
System Benchmarks Index Score 1492.2

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2901925470.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 503614.9 MWIPS (10.1 s, 7 samples) Execl Throughput 34080.1 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 309291.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 75115.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1018101.3 KBps (30.0 s, 2 samples) Pipe Throughput 180702003.1 lps (10.0 s, 7 samples) Pipe-based Context Switching 2426596.4 lps (10.0 s, 7 samples) Process Creation 37282.5 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 50813.7 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5835.8 lpm (60.4 s, 2 samples) System Call Overhead 9039181.1 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2901925470.7 248665.4 Double-Precision Whetstone 55.0 503614.9 91566.3
Execl Throughput 43.0 34080.1 7925.6
File Copy 1024 bufsize 2000 maxblocks 3960.0 309291.4 781.0 File Copy 256 bufsize 500 maxblocks 1655.0 75115.3 453.9 File Copy 4096 bufsize 8000 maxblocks 5800.0 1018101.3 1755.3 Pipe Throughput 12440.0 180702003.1 145258.8
Pipe-based Context Switching 4000.0 2426596.4 6066.5 Process Creation 126.0 37282.5 2958.9
Shell Scripts (1 concurrent) 42.4 50813.7 11984.4
Shell Scripts (8 concurrent) 6.0 5835.8 9726.4
System Call Overhead 15000.0 9039181.1 6026.1
========
System Benchmarks Index Score 8765.2

After patch:

128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35438193.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 4245.7 MWIPS (10.0 s, 7 samples) Execl Throughput 5293.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1233323.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 355264.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3333631.6 KBps (30.0 s, 2 samples) Pipe Throughput 1979613.2 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55675.2 lps (10.0 s, 7 samples) Process Creation 8528.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6870.0 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2115.5 lpm (60.0 s, 2 samples) System Call Overhead 1546959.4 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35438193.7 3036.7 Double-Precision Whetstone 55.0 4245.7 772.0
Execl Throughput 43.0 5293.7 1231.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 1233323.4 3114.5 File Copy 256 bufsize 500 maxblocks 1655.0 355264.5 2146.6 File Copy 4096 bufsize 8000 maxblocks 5800.0 3333631.6 5747.6 Pipe Throughput 12440.0 1979613.2 1591.3
Pipe-based Context Switching 4000.0 55675.2 139.2 Process Creation 126.0 8528.1 676.8
Shell Scripts (1 concurrent) 42.4 6870.0 1620.3
Shell Scripts (8 concurrent) 6.0 2115.5 3525.8
System Call Overhead 15000.0 1546959.4 1031.3
========
System Benchmarks Index Score 1465.3

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2903340286.5 lps (10.0 s, 7 samples) Double-Precision Whetstone 504137.7 MWIPS (10.1 s, 7 samples) Execl Throughput 34332.8 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 311391.2 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 72503.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1000861.7 KBps (30.0 s, 2 samples) Pipe Throughput 179382076.6 lps (10.0 s, 7 samples) Pipe-based Context Switching 2415716.6 lps (10.0 s, 7 samples) Process Creation 36873.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 51464.1 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5976.3 lpm (60.4 s, 2 samples) System Call Overhead 9182389.5 lps (10.0 s, 7 samples)

Quoted 16 lines

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2903340286.5 248786.7 Double-Precision Whetstone 55.0 504137.7 91661.4
Execl Throughput 43.0 34332.8 7984.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 311391.2 786.3 File Copy 256 bufsize 500 maxblocks 1655.0 72503.3 438.1 File Copy 4096 bufsize 8000 maxblocks 5800.0 1000861.7 1725.6 Pipe Throughput 12440.0 179382076.6 144197.8
Pipe-based Context Switching 4000.0 2415716.6 6039.3 Process Creation 126.0 36873.1 2926.4
Shell Scripts (1 concurrent) 42.4 51464.1 12137.8
Shell Scripts (8 concurrent) 6.0 5976.3 9960.6
System Call Overhead 15000.0 9182389.5 6121.6
========
System Benchmarks Index Score 8759.8

I think your tests are incomplete. Performance test is just a very basic test, you should make sure all dynamic code modification mechanisms are correct.

At least these should be verified one by one: jump label, kgdb, bpf, ftrace, kprobes, uprobes....

You have modified ftrace, but not the best method that Ihae suggested in V3.

You completely ignore my suggestion about kprobes in V3.

For KGDB, you should use text_mutex to protect copy_to_kernel_nofault().

For uprobes, I have no idea, maybe @Tiezhu can give some suggestions.

Quoted 9 lines

Huacai

For fentry:

ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() are not in the call path of ftrace_init_nop(),

so the text_mutex lock was added in ftrace_modify_code(). However, I overlooked that ftrace_init_nop() also holds the text_mutex lock (e.g., on RISC-V). I am fixing this.

No, RISC-V is right, use ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() to protect all common cases, and use text_mutex to protect the only exception, ftrace_init_nop().

Quoted 5 lines

For kprobes:

text_mutex is already held in the caller arm_kprobe(), which then calls arch_arm_kprobe->larch_insn_text_copy. So the kprobes path is properly protected.

No, I doubt you didn't see my link, we are talking about arch_prepare_ss_slot(). Though it seems the framework has hold text_mutex, but you don't use the larch_insn_patch_text() API, who handle the RWX?

Quoted 5 lines

For kgdb:

When KGDB enters do_single_step(), it has already stopped execution on all other CPUs. Only the current CPU is running KGDB logic. Do we need to worry about concurrency issues in this case? Also, Sleepable lock is also disallowed in exception context.

The same as above, if you use copy_to_kernel_nofault() directly, who handle the RWX?

Huacai

I plan to make the following modifications. Is this approach feasible?

1, For kprobes:

Add modifications for arch_prepare_ss_slot(struct kprobe *p), referencing the patch at:
https://lore.kernel.org/loongarch/CAHirt9gSXZvw0wTv9zupsVRG0_ekza08efW0hwq_yvnU1WbUng@mail.gmail.com/T/#m6a75415f468e671748d6ec8c6b502ca36788c546

static void arch_prepare_ss_slot(struct kprobe *p)
{
larch_insn_patch_text(p->ainsn.insn, *p->addr); larch_insn_patch_text(p->ainsn.insn + 1, KPROBE_SSTEPBP_INSN); p->ainsn.restore = (unsigned long)p->addr + LOONGARCH_INSN_SIZE; }

2,For kgdb:

Modify page table permissions for the kernel code section by adding the following lockless method. Use apply_to_existing_page_range() to iterate over the virtual address range and perform permission modification operations on existing page table entries (PTEs).

This approach references the RISC-V commit:
fb1cf0878328 ("riscv: rewrite __kernel_map_pages() to fix sleeping in invalid context")

static int change_page_attr(pte_t *ptep, unsigned long addr, void *data) {
handle the RWX
}
static int kgdb_set_page_rw(unsigned long addr, int numpages, long action) {
...
apply_to_existing_page_range(&init_mm, start, size, change_page_attr, (void *)action);

flush_tlb_kernel_range(addr, addr + size);
...

}

int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) {
...
kgdb_set_page_rw(bpt->bpt_addr,1,0);
err = copy_to_kernel_nofault((char *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); kgdb_set_page_rw_(bpt->bpt_addr,1,1);
...
}

Quoted 10 lines

static int do_single_step(struct pt_regs *regs) {
...
kgdb_set_page_rw(bpt->bpt_addr,1,0);
error = copy_to_kernel_nofault((void *)stepped_address, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); kgdb_set_page_rw_(bpt->bpt_addr,1,1);
...
}

So complicated? Why not replace copy_to_kernel_nofault() with larch_insn_write() directly?

larch_insn_write->set_memory_rw->__set_memory->mmap_write_lock(&init_mm)

Quoted 8 lines
Quoted 3 lines

The call path holds a lock that may sleep. Invoking larch_insn_write() within the KGDB exception context is risky, as the context does not permit sleeping.

Why risky? __do_page_fault() is also in the exception context, but it holds sleepable locks.

Huacai

If a process holds the mmap_write_lock(&init_mm) lock and then triggers entry into KDB (e.g., via a breakpoint or sysrq),In the KDB session, the user attempts to set a breakpoint using the 'bp' command. The breakpoint setup path (kgdb_arch_set_breakpoint) tries to acquire mmap_write_lock(&init_mm) again. However, all other CPUs are already frozen by KDB and are spinning in a waiting state,No CPU is available to release the lock. As a result, the system enters a deadlock and becomes completely unresponsive.

Quoted 149 lines
Quoted 148 lines
Quoted 4 lines

3,uprobes write to userspace process code segments, which does not conflict with kernel code segment modifications. Therefore, changes for STRICT_MODULE_RWX do not affect uprobes.

@Tiezhu, what's your opinion?

Huacai

Quoted 139 lines

Thanks!

Quoted 135 lines

For uprobes:

I will check the call path again.

For test:

I have already done some tests using BPF tools that utilize kprobes and fentry. I will run more tests using the selftests suite.

Thanks !

Quoted 124 lines

---
arch/loongarch/Kconfig | 1 + arch/loongarch/kernel/ftrace_dyn.c | 7 ++++++- arch/loongarch/kernel/inst.c | 25 +++++++++++++++++++++---- arch/loongarch/kernel/jump_label.c | 3 +++

4 files changed, 31 insertions(+), 5 deletions(-)
arch/loongarch/Kconfig +0 -0
index 606597da46b8..c751d714c287 100644
@@ -27,6 +27,7 @@ config LOONGARCH

select ARCH_HAS_PTE_SPECIAL if 64BIT
select ARCH_HAS_SET_MEMORY
select ARCH_HAS_SET_DIRECT_MAP
+ select ARCH_HAS_STRICT_MODULE_RWX
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN
select ARCH_HAS_VDSO_ARCH_DATA

arch/loongarch/kernel/ftrace_dyn.c +6 -1
index d5d81d74034c..598dc6434cc4 100644
@@ -8,6 +8,7 @@
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
+#include <linux/memory.h>
 
 #include <asm/inst.h>
 #include <asm/module.h>
@@ -24,8 +25,12 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate)
 return -EINVAL;
 }
 
- if (larch_insn_patch_text((void *)pc, new))
+ mutex_lock(&text_mutex);
+ if (larch_insn_patch_text((void *)pc, new)) {
+ mutex_unlock(&text_mutex);
 return -EPERM;
+ }
+ mutex_unlock(&text_mutex);
 
 return 0;
 }
arch/loongarch/kernel/inst.c +21 -4
index 0b9228b7c13a..3de94d465c3c 100644
@@ -6,12 +6,11 @@
 #include <linux/uaccess.h>
 #include <linux/set_memory.h>
 #include <linux/stop_machine.h>
+#include <linux/memory.h>
 
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
-static DEFINE_RAW_SPINLOCK(patch_lock);
-
 void simu_pc(struct pt_regs *regs, union loongarch_instruction insn)
 {
 unsigned long pc = regs->csr_era;
@@ -207,14 +206,32 @@ int larch_insn_read(void *addr, u32 *insnp)
 int larch_insn_write(void *addr, u32 insn)
 {
 int ret;
+ int err = 0;
+ size_t start;
 unsigned long flags = 0;
 
 if ((unsigned long)addr & 3)
 return -EINVAL;
 
- raw_spin_lock_irqsave(&patch_lock, flags);
+ start = round_down((size_t)addr, PAGE_SIZE);
+
+ lockdep_assert_held(&text_mutex);
+
+ err = set_memory_rw(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rw() failed\n", __func__);
+ return err;
+ }
+
+ local_irq_save(flags);
 ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
- raw_spin_unlock_irqrestore(&patch_lock, flags);
+ local_irq_restore(flags);
+
+ err = set_memory_rox(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rox() failed\n", __func__);
+ return err;
+ }
 
 return ret;
 }
arch/loongarch/kernel/jump_label.c +1 -0
index 24a3f4d8540c..e6bb040fe4c5 100644
@@ -6,6 +6,7 @@
 */
 #include <linux/kernel.h>
 #include <linux/jump_label.h>
+#include <linux/memory.h>
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
@@ -19,7 +20,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry, enum jump_label_t

else
insn = larch_insn_gen_nop();

+ mutex_lock(&text_mutex);
larch_insn_write(addr, insn);
+ mutex_unlock(&text_mutex);

return true;
}
--
2.43.0

maintainer
#9 lore raw
Quoted 300 lines · On Thu, Jun 25, 2026 at 3:59 PM <[email protected]> wrote:

2026年6月24日 22:33, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 296 lines · On Tue, Jun 23, 2026 at 5:33 PM <[email protected]> wrote:
Quoted 294 lines

2026年6月22日 10:29, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 270 lines · On Sun, Jun 21, 2026 at 8:12 AM <[email protected]> wrote:

2026年6月14日 20:10, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Quoted 185 lines · On Sun, Jun 14, 2026 at 6:24 PM <[email protected]> wrote:

2026年6月13日 18:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Hi, Haoran,

Quoted 147 lines · On Sat, Jun 13, 2026 at 4:42 PM <[email protected]> wrote:

From: Haoran Jiang <[email protected]>

Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.

---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.

v3:
Modify commit description.

v4:
Add text_mutex lock in the larch_insn_write call path and CONFIG_STRICT_MODULE_RWX is enabled by default.

UB test on the 3C6000 server shows no significant performance impact.

Before patch:

========================================================================
BYTE UNIX Benchmarks (Version 5.1.6)

System: localhost.localdomain: GNU/Linux
OS: GNU/Linux -- 7.1.0-rc6 -- #1 SMP PREEMPT Wed Jun 10 21:07:41 CST 2026
Machine: loongarch64 (loongarch64)
Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
21:19:51 up 1 min, 2 users, load average: 0.71, 0.38, 0.14; runlevel 2026-06-10

------------------------------------------------------------------------
128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35205725.4 lps (10.0 s, 7 samples) Double-Precision Whetstone 4244.9 MWIPS (10.0 s, 7 samples) Execl Throughput 6717.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1213873.8 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 350740.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3275103.0 KBps (30.0 s, 2 samples) Pipe Throughput 1981993.9 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55287.7 lps (10.0 s, 7 samples) Process Creation 9056.8 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6736.5 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2109.8 lpm (60.0 s, 2 samples) System Call Overhead 1549110.9 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35205725.4 3016.8 Double-Precision Whetstone 55.0 4244.9 771.8
Execl Throughput 43.0 6717.7 1562.3
File Copy 1024 bufsize 2000 maxblocks 3960.0 1213873.8 3065.3 File Copy 256 bufsize 500 maxblocks 1655.0 350740.5 2119.3 File Copy 4096 bufsize 8000 maxblocks 5800.0 3275103.0 5646.7 Pipe Throughput 12440.0 1981993.9 1593.2
Pipe-based Context Switching 4000.0 55287.7 138.2 Process Creation 126.0 9056.8 718.8
Shell Scripts (1 concurrent) 42.4 6736.5 1588.8
Shell Scripts (8 concurrent) 6.0 2109.8 3516.4
System Call Overhead 15000.0 1549110.9 1032.7
========
System Benchmarks Index Score 1492.2

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2901925470.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 503614.9 MWIPS (10.1 s, 7 samples) Execl Throughput 34080.1 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 309291.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 75115.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1018101.3 KBps (30.0 s, 2 samples) Pipe Throughput 180702003.1 lps (10.0 s, 7 samples) Pipe-based Context Switching 2426596.4 lps (10.0 s, 7 samples) Process Creation 37282.5 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 50813.7 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5835.8 lpm (60.4 s, 2 samples) System Call Overhead 9039181.1 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2901925470.7 248665.4 Double-Precision Whetstone 55.0 503614.9 91566.3
Execl Throughput 43.0 34080.1 7925.6
File Copy 1024 bufsize 2000 maxblocks 3960.0 309291.4 781.0 File Copy 256 bufsize 500 maxblocks 1655.0 75115.3 453.9 File Copy 4096 bufsize 8000 maxblocks 5800.0 1018101.3 1755.3 Pipe Throughput 12440.0 180702003.1 145258.8
Pipe-based Context Switching 4000.0 2426596.4 6066.5 Process Creation 126.0 37282.5 2958.9
Shell Scripts (1 concurrent) 42.4 50813.7 11984.4
Shell Scripts (8 concurrent) 6.0 5835.8 9726.4
System Call Overhead 15000.0 9039181.1 6026.1
========
System Benchmarks Index Score 8765.2

After patch:

128 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables 35438193.7 lps (10.0 s, 7 samples) Double-Precision Whetstone 4245.7 MWIPS (10.0 s, 7 samples) Execl Throughput 5293.7 lps (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 1233323.4 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 355264.5 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 3333631.6 KBps (30.0 s, 2 samples) Pipe Throughput 1979613.2 lps (10.0 s, 7 samples)
Pipe-based Context Switching 55675.2 lps (10.0 s, 7 samples) Process Creation 8528.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 6870.0 lpm (60.0 s, 2 samples) Shell Scripts (8 concurrent) 2115.5 lpm (60.0 s, 2 samples) System Call Overhead 1546959.4 lps (10.0 s, 7 samples)

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 35438193.7 3036.7 Double-Precision Whetstone 55.0 4245.7 772.0
Execl Throughput 43.0 5293.7 1231.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 1233323.4 3114.5 File Copy 256 bufsize 500 maxblocks 1655.0 355264.5 2146.6 File Copy 4096 bufsize 8000 maxblocks 5800.0 3333631.6 5747.6 Pipe Throughput 12440.0 1979613.2 1591.3
Pipe-based Context Switching 4000.0 55675.2 139.2 Process Creation 126.0 8528.1 676.8
Shell Scripts (1 concurrent) 42.4 6870.0 1620.3
Shell Scripts (8 concurrent) 6.0 2115.5 3525.8
System Call Overhead 15000.0 1546959.4 1031.3
========
System Benchmarks Index Score 1465.3

------------------------------------------------------------------------
128 CPUs in system; running 128 parallel copies of tests

Dhrystone 2 using register variables 2903340286.5 lps (10.0 s, 7 samples) Double-Precision Whetstone 504137.7 MWIPS (10.1 s, 7 samples) Execl Throughput 34332.8 lps (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks 311391.2 KBps (30.0 s, 2 samples) File Copy 256 bufsize 500 maxblocks 72503.3 KBps (30.0 s, 2 samples) File Copy 4096 bufsize 8000 maxblocks 1000861.7 KBps (30.0 s, 2 samples) Pipe Throughput 179382076.6 lps (10.0 s, 7 samples) Pipe-based Context Switching 2415716.6 lps (10.0 s, 7 samples) Process Creation 36873.1 lps (30.0 s, 2 samples)
Shell Scripts (1 concurrent) 51464.1 lpm (60.1 s, 2 samples) Shell Scripts (8 concurrent) 5976.3 lpm (60.4 s, 2 samples) System Call Overhead 9182389.5 lps (10.0 s, 7 samples)

Quoted 16 lines

System Benchmarks Index Values BASELINE RESULT INDEX Dhrystone 2 using register variables 116700.0 2903340286.5 248786.7 Double-Precision Whetstone 55.0 504137.7 91661.4
Execl Throughput 43.0 34332.8 7984.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 311391.2 786.3 File Copy 256 bufsize 500 maxblocks 1655.0 72503.3 438.1 File Copy 4096 bufsize 8000 maxblocks 5800.0 1000861.7 1725.6 Pipe Throughput 12440.0 179382076.6 144197.8
Pipe-based Context Switching 4000.0 2415716.6 6039.3 Process Creation 126.0 36873.1 2926.4
Shell Scripts (1 concurrent) 42.4 51464.1 12137.8
Shell Scripts (8 concurrent) 6.0 5976.3 9960.6
System Call Overhead 15000.0 9182389.5 6121.6
========
System Benchmarks Index Score 8759.8

I think your tests are incomplete. Performance test is just a very basic test, you should make sure all dynamic code modification mechanisms are correct.

At least these should be verified one by one: jump label, kgdb, bpf, ftrace, kprobes, uprobes....

You have modified ftrace, but not the best method that Ihae suggested in V3.

You completely ignore my suggestion about kprobes in V3.

For KGDB, you should use text_mutex to protect copy_to_kernel_nofault().

For uprobes, I have no idea, maybe @Tiezhu can give some suggestions.

Quoted 9 lines

Huacai

For fentry:

ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() are not in the call path of ftrace_init_nop(),

so the text_mutex lock was added in ftrace_modify_code(). However, I overlooked that ftrace_init_nop() also holds the text_mutex lock (e.g., on RISC-V). I am fixing this.

No, RISC-V is right, use ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() to protect all common cases, and use text_mutex to protect the only exception, ftrace_init_nop().

Quoted 5 lines

For kprobes:

text_mutex is already held in the caller arm_kprobe(), which then calls arch_arm_kprobe->larch_insn_text_copy. So the kprobes path is properly protected.

No, I doubt you didn't see my link, we are talking about arch_prepare_ss_slot(). Though it seems the framework has hold text_mutex, but you don't use the larch_insn_patch_text() API, who handle the RWX?

Quoted 5 lines

For kgdb:

When KGDB enters do_single_step(), it has already stopped execution on all other CPUs. Only the current CPU is running KGDB logic. Do we need to worry about concurrency issues in this case? Also, Sleepable lock is also disallowed in exception context.

The same as above, if you use copy_to_kernel_nofault() directly, who handle the RWX?

Huacai

I plan to make the following modifications. Is this approach feasible?

1, For kprobes:

Add modifications for arch_prepare_ss_slot(struct kprobe *p), referencing the patch at:
https://lore.kernel.org/loongarch/CAHirt9gSXZvw0wTv9zupsVRG0_ekza08efW0hwq_yvnU1WbUng@mail.gmail.com/T/#m6a75415f468e671748d6ec8c6b502ca36788c546

static void arch_prepare_ss_slot(struct kprobe *p)
{
larch_insn_patch_text(p->ainsn.insn, *p->addr); larch_insn_patch_text(p->ainsn.insn + 1, KPROBE_SSTEPBP_INSN); p->ainsn.restore = (unsigned long)p->addr + LOONGARCH_INSN_SIZE; }

2,For kgdb:

Modify page table permissions for the kernel code section by adding the following lockless method. Use apply_to_existing_page_range() to iterate over the virtual address range and perform permission modification operations on existing page table entries (PTEs).

This approach references the RISC-V commit:
fb1cf0878328 ("riscv: rewrite __kernel_map_pages() to fix sleeping in invalid context")

static int change_page_attr(pte_t *ptep, unsigned long addr, void *data) {
handle the RWX
}
static int kgdb_set_page_rw(unsigned long addr, int numpages, long action) {
...
apply_to_existing_page_range(&init_mm, start, size, change_page_attr, (void *)action);

flush_tlb_kernel_range(addr, addr + size);
...

}

int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) {
...
kgdb_set_page_rw(bpt->bpt_addr,1,0);
err = copy_to_kernel_nofault((char *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); kgdb_set_page_rw_(bpt->bpt_addr,1,1);
...
}

Quoted 10 lines

static int do_single_step(struct pt_regs *regs) {
...
kgdb_set_page_rw(bpt->bpt_addr,1,0);
error = copy_to_kernel_nofault((void *)stepped_address, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); kgdb_set_page_rw_(bpt->bpt_addr,1,1);
...
}

So complicated? Why not replace copy_to_kernel_nofault() with larch_insn_write() directly?

larch_insn_write->set_memory_rw->__set_memory->mmap_write_lock(&init_mm)

The call path holds a lock that may sleep. Invoking larch_insn_write() within the KGDB exception context is risky, as the context does not permit sleeping.

Quoted 7 lines
Quoted 5 lines

Why risky? __do_page_fault() is also in the exception context, but it holds sleepable locks.

Huacai

If a process holds the mmap_write_lock(&init_mm) lock and then triggers entry into KDB (e.g., via a breakpoint or sysrq),In the KDB session, the user attempts to set a breakpoint using the 'bp' command. The breakpoint setup path (kgdb_arch_set_breakpoint) tries to acquire mmap_write_lock(&init_mm) again. However, all other CPUs are already frozen by KDB and are spinning in a waiting state,No CPU is available to release the lock. As a result, the system enters a deadlock and becomes completely unresponsive.

OK, I know. But there is still a simpler solution for KGDB. Since it is sure that the page is exist for modification, we can use virt_to_kpte() to get the pte, then your kgdb_set_page_rw() can be a simple set_pte() on top of virt_to_kpte().

Huacai

Quoted 151 lines
Quoted 150 lines
Quoted 149 lines
Quoted 5 lines

3,uprobes write to userspace process code segments, which does not conflict with kernel code segment modifications. Therefore, changes for STRICT_MODULE_RWX do not affect uprobes.

@Tiezhu, what's your opinion?

Huacai

Quoted 139 lines

Thanks!

Quoted 135 lines

For uprobes:

I will check the call path again.

For test:

I have already done some tests using BPF tools that utilize kprobes and fentry. I will run more tests using the selftests suite.

Thanks !

Quoted 124 lines

---
arch/loongarch/Kconfig | 1 + arch/loongarch/kernel/ftrace_dyn.c | 7 ++++++- arch/loongarch/kernel/inst.c | 25 +++++++++++++++++++++---- arch/loongarch/kernel/jump_label.c | 3 +++

4 files changed, 31 insertions(+), 5 deletions(-)
arch/loongarch/Kconfig +0 -0
index 606597da46b8..c751d714c287 100644
@@ -27,6 +27,7 @@ config LOONGARCH

select ARCH_HAS_PTE_SPECIAL if 64BIT
select ARCH_HAS_SET_MEMORY
select ARCH_HAS_SET_DIRECT_MAP
+ select ARCH_HAS_STRICT_MODULE_RWX
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN
select ARCH_HAS_VDSO_ARCH_DATA

arch/loongarch/kernel/ftrace_dyn.c +6 -1
index d5d81d74034c..598dc6434cc4 100644
@@ -8,6 +8,7 @@
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
+#include <linux/memory.h>
 
 #include <asm/inst.h>
 #include <asm/module.h>
@@ -24,8 +25,12 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate)
 return -EINVAL;
 }
 
- if (larch_insn_patch_text((void *)pc, new))
+ mutex_lock(&text_mutex);
+ if (larch_insn_patch_text((void *)pc, new)) {
+ mutex_unlock(&text_mutex);
 return -EPERM;
+ }
+ mutex_unlock(&text_mutex);
 
 return 0;
 }
arch/loongarch/kernel/inst.c +21 -4
index 0b9228b7c13a..3de94d465c3c 100644
@@ -6,12 +6,11 @@
 #include <linux/uaccess.h>
 #include <linux/set_memory.h>
 #include <linux/stop_machine.h>
+#include <linux/memory.h>
 
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
-static DEFINE_RAW_SPINLOCK(patch_lock);
-
 void simu_pc(struct pt_regs *regs, union loongarch_instruction insn)
 {
 unsigned long pc = regs->csr_era;
@@ -207,14 +206,32 @@ int larch_insn_read(void *addr, u32 *insnp)
 int larch_insn_write(void *addr, u32 insn)
 {
 int ret;
+ int err = 0;
+ size_t start;
 unsigned long flags = 0;
 
 if ((unsigned long)addr & 3)
 return -EINVAL;
 
- raw_spin_lock_irqsave(&patch_lock, flags);
+ start = round_down((size_t)addr, PAGE_SIZE);
+
+ lockdep_assert_held(&text_mutex);
+
+ err = set_memory_rw(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rw() failed\n", __func__);
+ return err;
+ }
+
+ local_irq_save(flags);
 ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
- raw_spin_unlock_irqrestore(&patch_lock, flags);
+ local_irq_restore(flags);
+
+ err = set_memory_rox(start, 1);
+ if (err) {
+ pr_info("%s: set_memory_rox() failed\n", __func__);
+ return err;
+ }
 
 return ret;
 }
arch/loongarch/kernel/jump_label.c +1 -0
index 24a3f4d8540c..e6bb040fe4c5 100644
@@ -6,6 +6,7 @@
 */
 #include <linux/kernel.h>
 #include <linux/jump_label.h>
+#include <linux/memory.h>
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
 
@@ -19,7 +20,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry, enum jump_label_t

else
insn = larch_insn_gen_nop();

+ mutex_lock(&text_mutex);
larch_insn_write(addr, insn);
+ mutex_unlock(&text_mutex);

return true;
}
--
2.43.0