Skip to contentSkip to conversation

Add generic data patching functions

PATCH Superseded Benjamin Gray 3 participants 12 messages Mar 25, 2024 – May 14, 2024

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add generic data patching functions

Currently patch_instruction() bases the write length on the value being written. If the value looks like a prefixed instruction it writes 8 bytes, otherwise it writes 4 bytes. This makes it potentially buggy to use for writing arbitrary data, as if you want to write 4 bytes but it decides to write 8 bytes it may clobber the following memory or be unaligned and trigger an oops if it tries to cross a page boundary.

To solve this, this series pulls out the size parameter to the 'top' of the memory patching logic, and propagates it through the various functions.

The two sizes supported are int and long; this allows for patching instructions and pointers on both ppc32 and ppc64. On ppc32 these are the same size, so care is taken to only use the size parameter on static functions, so the compiler can optimise it out entirely. Unfortunately GCC trips over its own feet here and won't optimise in a way that is optimal for strict RWX (mpc85xx_smp_defconfig) and no RWX (pmac32_defconfig). More details in the v2 cover letter.

Changes from v2:
* Various changes noted on each patch
* Data patching now enforced to be aligned
* Restore page aligned flushing optimisation

Changes from v1:
* Addressed the v1 review actions
* Removed noinline (for now)

v2: https://patchwork.ozlabs.org/project/linuxppc-dev/cover/[email protected]/
v1: https://patchwork.ozlabs.org/project/linuxppc-dev/cover/[email protected]/

Benjamin Gray (5):
powerpc/code-patching: Add generic memory patching powerpc/code-patching: Add data patch alignment check powerpc/64: Convert patch_instruction() to patch_u32() powerpc/32: Convert patch_instruction() to patch_uint() powerpc/code-patching: Add boot selftest for data patching

 arch/powerpc/include/asm/code-patching.h | 37 +++++++++++++
 arch/powerpc/kernel/module_64.c          |  5 +-
 arch/powerpc/kernel/static_call.c        |  2 +-
 arch/powerpc/lib/code-patching.c         | 70 +++++++++++++++++++-----
 arch/powerpc/lib/test-code-patching.c    | 36 ++++++++++++
 arch/powerpc/platforms/powermac/smp.c    |  2 +-
 6 files changed, 132 insertions(+), 20 deletions(-)

--
2.44.0

#1 lore raw

PATCH v3 1/5 powerpc/code-patching: Add generic memory patching

patch_instruction() is designed for patching instructions in otherwise readonly memory. Other consumers also sometimes need to patch readonly memory, so have abused patch_instruction() for arbitrary data patches.

This is a problem on ppc64 as patch_instruction() decides on the patch width using the 'instruction' opcode to see if it's a prefixed instruction. Data that triggers this can lead to larger writes, possibly crossing a page boundary and failing the write altogether.

Introduce patch_uint(), and patch_ulong(), with aliases patch_u32(), and patch_u64() (on ppc64) designed for aligned data patches. The patch size is now determined by the called function, and is passed as an additional parameter to generic internals.

While the instruction flushing is not required for data patches, it remains unconditional in this patch. A followup series is possible if benchmarking shows fewer flushes gives an improvement in some data-patching workload.

ppc32 does not support prefixed instructions, so is unaffected by the original issue. Care is taken in not exposing the size parameter in the public (non-static) interface, so the compiler can const-propagate it away.

---

v3: * Rename from *_memory to *_mem
* Change type of ppc32 patch_uint() address to void*
* Explain introduction of val32 for big endian
* Some formatting

v2: * Deduplicate patch_32() definition
* Use u32 for val32
* Remove noinline

---
 arch/powerpc/include/asm/code-patching.h | 31 ++++++++++++
 arch/powerpc/lib/code-patching.c         | 64 ++++++++++++++++++------
 2 files changed, 80 insertions(+), 15 deletions(-)
arch/powerpc/include/asm/code-patching.h +31 -0
index 0e29ccf903d0..21a36e2c4e26 100644
@@ -76,6 +76,37 @@ int patch_instruction(u32 *addr, ppc_inst_t instr);
 int raw_patch_instruction(u32 *addr, ppc_inst_t instr);
 int patch_instructions(u32 *addr, u32 *code, size_t len, bool repeat_instr);
 
+/*
+ * The data patching functions patch_uint() and patch_ulong(), etc., must be
+ * called on aligned addresses.
+ *
+ * The instruction patching functions patch_instruction() and similar must be
+ * called on addresses satisfying instruction alignment requirements.
+ */
+
+#ifdef CONFIG_PPC64
+
+int patch_uint(void *addr, unsigned int val);
+int patch_ulong(void *addr, unsigned long val);
+
+#define patch_u64 patch_ulong
+
+#else
+
+static inline int patch_uint(void *addr, unsigned int val)
+{
+	return patch_instruction(addr, ppc_inst(val));
+}
+
+static inline int patch_ulong(void *addr, unsigned long val)
+{
+	return patch_instruction(addr, ppc_inst(val));
+}
+
+#endif
+
+#define patch_u32 patch_uint
+
 static inline unsigned long patch_site_addr(s32 *site)
 {
 	return (unsigned long)site + *site;
arch/powerpc/lib/code-patching.c +49 -15

125 lines not on this page · raw message

Signature · 1 line
2.44.0
#2 lore raw

PATCH v3 2/5 powerpc/code-patching: Add data patch alignment check

In reply to Benjamin Gray

The new data patching still needs to be aligned within a cacheline too for the flushes to work correctly. To simplify this requirement, we just say data patches must be aligned.

Detect when data patching is not aligned, returning an invalid argument error.

---

v3: * New in v3

---
 arch/powerpc/include/asm/code-patching.h | 6 ++++++
 arch/powerpc/lib/code-patching.c         | 6 ++++++
 2 files changed, 12 insertions(+)
arch/powerpc/include/asm/code-patching.h +6 -0
index 21a36e2c4e26..e7f14720f630 100644
@@ -95,11 +95,17 @@ int patch_ulong(void *addr, unsigned long val);
 
 static inline int patch_uint(void *addr, unsigned int val)
 {
+	if (!IS_ALIGNED((unsigned long)addr, sizeof(unsigned int)))
+		return -EINVAL;
+
 	return patch_instruction(addr, ppc_inst(val));
 }
 
 static inline int patch_ulong(void *addr, unsigned long val)
 {
+	if (!IS_ALIGNED((unsigned long)addr, sizeof(unsigned long)))
+		return -EINVAL;
+
 	return patch_instruction(addr, ppc_inst(val));
 }
 
arch/powerpc/lib/code-patching.c +6 -0
index a31e313c6321..35c620dbdfd4 100644
@@ -386,12 +386,18 @@ NOKPROBE_SYMBOL(patch_instruction);
 
 int patch_uint(void *addr, unsigned int val)
 {
+	if (!IS_ALIGNED((unsigned long)addr, sizeof(unsigned int)))
+		return -EINVAL;
+
 	return patch_mem(addr, val, false);
 }
 NOKPROBE_SYMBOL(patch_uint);
 
 int patch_ulong(void *addr, unsigned long val)
 {
+	if (!IS_ALIGNED((unsigned long)addr, sizeof(unsigned long)))
+		return -EINVAL;
+
 	return patch_mem(addr, val, true);
 }
 NOKPROBE_SYMBOL(patch_ulong);
Signature · 1 line
2.44.0
#3 lore raw

PATCH v3 3/5 powerpc/64: Convert patch_instruction() to patch_u32()

In reply to Benjamin Gray

This use of patch_instruction() is working on 32 bit data, and can fail if the data looks like a prefixed instruction and the extra write crosses a page boundary. Use patch_u32() to fix the write size.

Fixes 8734b41b3efe ("powerpc/module_64: Fix livepatching for RO modules")

---

v2: * Added the fixes tag, it seems appropriate even if the subject does
mention a more robust solution being required.

patch_u64() should be more efficient, but judging from the bug report it doesn't seem like the data is doubleword aligned.

---
 arch/powerpc/kernel/module_64.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/powerpc/kernel/module_64.c +2 -3
index 7112adc597a8..e9bab599d0c2 100644
@@ -651,12 +651,11 @@ static inline int create_stub(const Elf64_Shdr *sechdrs,
 	// func_desc_t is 8 bytes if ABIv2, else 16 bytes
 	desc = func_desc(addr);
 	for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) {
-		if (patch_instruction(((u32 *)&entry->funcdata) + i,
-				      ppc_inst(((u32 *)(&desc))[i])))
+		if (patch_u32(((u32 *)&entry->funcdata) + i, ((u32 *)&desc)[i]))
 			return 0;
 	}
 
-	if (patch_instruction(&entry->magic, ppc_inst(STUB_MAGIC)))
+	if (patch_u32(&entry->magic, STUB_MAGIC))
 		return 0;
 
 	return 1;
Signature · 1 line
2.44.0
#4 lore raw

Re: [PATCH v3 3/5] powerpc/64: Convert patch_instruction() to patch_u32()

Quoted 8 lines · Benjamin Gray

This use of patch_instruction() is working on 32 bit data, and can fail if the data looks like a prefixed instruction and the extra write crosses a page boundary. Use patch_u32() to fix the write size.

Fixes 8734b41b3efe ("powerpc/module_64: Fix livepatching for RO modules")
Quoted 7 lines

---

v2: * Added the fixes tag, it seems appropriate even if the subject does
mention a more robust solution being required.

patch_u64() should be more efficient, but judging from the bug report it doesn't seem like the data is doubleword aligned.

Asking again, is that still the case? It looks like at least the first fix below can be converted to patch_u64().

- Naveen

Quoted 26 lines
---
 arch/powerpc/kernel/module_64.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/powerpc/kernel/module_64.c +2 -3
index 7112adc597a8..e9bab599d0c2 100644
@@ -651,12 +651,11 @@ static inline int create_stub(const Elf64_Shdr *sechdrs,
 	// func_desc_t is 8 bytes if ABIv2, else 16 bytes
 	desc = func_desc(addr);
 	for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) {
-		if (patch_instruction(((u32 *)&entry->funcdata) + i,
-				      ppc_inst(((u32 *)(&desc))[i])))
+		if (patch_u32(((u32 *)&entry->funcdata) + i, ((u32 *)&desc)[i]))
 			return 0;
 	}
 
-	if (patch_instruction(&entry->magic, ppc_inst(STUB_MAGIC)))
+	if (patch_u32(&entry->magic, STUB_MAGIC))
 		return 0;
 
 	return 1;
Signature · 1 line
2.44.0
#5 lore raw

Re: [PATCH v3 3/5] powerpc/64: Convert patch_instruction() to patch_u32()

Quoted 21 lines · Naveen N Rao
Quoted 12 lines · Benjamin Gray

This use of patch_instruction() is working on 32 bit data, and can fail
if the data looks like a prefixed instruction and the extra write crosses a page boundary. Use patch_u32() to fix the write size.

Fixes 8734b41b3efe ("powerpc/module_64: Fix livepatching for RO

modules")

---

Quoted 7 lines

v2: * Added the fixes tag, it seems appropriate even if the subject does
      mention a more robust solution being required.

patch_u64() should be more efficient, but judging from the bug report
it doesn't seem like the data is doubleword aligned.

Quoted 5 lines

Asking again, is that still the case? It looks like at least the first
fix below can be converted to patch_u64().

- Naveen

Sorry, I think I forgot this question last time. Reading the commit descriptions you linked, I don't see any mention of "entry->funcdata will always be doubleword aligned because XYZ". If the patch makes it doubleword aligned anyway, I wouldn't be confident asserting all callers will always do this without looking into it a lot more.

Perhaps a separate series could optimise it with appropriate justification/assertions to catch bad alignment. But I think leaving it out of this series is fine because the original works in words, so it's not regressing anything.

Quoted 33 lines
Quoted 33 lines

---
 arch/powerpc/kernel/module_64.c | 5 ++---

 1 file changed, 2 insertions(+), 3 deletions(-)
arch/powerpc/kernel/module_64.c +1 -2
index 7112adc597a8..e9bab599d0c2 100644
@@ -651,12 +651,11 @@ static inline int create_stub(const
 Elf64_Shdr *sechdrs,
  	// func_desc_t is 8 bytes if ABIv2, else 16 bytes
  	desc = func_desc(addr);
  	for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) {
-		if (patch_instruction(((u32 *)&entry->funcdata) +
 i,
-				      ppc_inst(((u32
 *)(&desc))[i])))
+		if (patch_u32(((u32 *)&entry->funcdata) + i, ((u32
 *)&desc)[i]))
  			return 0;
  	}

arch/powerpc/kernel/module_64.c +0 -1
-	if (patch_instruction(&entry->magic,

ppc_inst(STUB_MAGIC)))

arch/powerpc/kernel/module_64.c +1 -0
+	if (patch_u32(&entry->magic, STUB_MAGIC))

  return 0;

  return 1;

Signature · 1 line
2.44.0
#6 lore raw

Re: [PATCH v3 3/5] powerpc/64: Convert patch_instruction() to patch_u32()

Le 14/05/2024 à 04:59, Benjamin Gray a écrit :

Quoted 34 lines · Naveen N Rao
Quoted 21 lines · Benjamin Gray
Quoted 12 lines

This use of patch_instruction() is working on 32 bit data, and can fail
if the data looks like a prefixed instruction and the extra write crosses a page boundary. Use patch_u32() to fix the write size.

Fixes 8734b41b3efe ("powerpc/module_64: Fix livepatching for RO

modules")

---

Quoted 7 lines

v2: * Added the fixes tag, it seems appropriate even if the subject does
      mention a more robust solution being required.

patch_u64() should be more efficient, but judging from the bug report
it doesn't seem like the data is doubleword aligned.

Quoted 5 lines

Asking again, is that still the case? It looks like at least the first
fix below can be converted to patch_u64().

- Naveen

Sorry, I think I forgot this question last time. Reading the commit descriptions you linked, I don't see any mention of "entry->funcdata will always be doubleword aligned because XYZ". If the patch makes it doubleword aligned anyway, I wouldn't be confident asserting all callers will always do this without looking into it a lot more.

Quoted 4 lines

Perhaps a separate series could optimise it with appropriate justification/assertions to catch bad alignment. But I think leaving it out of this series is fine because the original works in words, so it's not regressing anything.

As far as I can see, the struct is 64 bits aligned by definition so funcdata field is aligned too as there are just 8x u32 before it:

struct ppc64_stub_entry {
	/*
	 * 28 byte jump instruction sequence (7 instructions) that can
	 * hold ppc64_stub_insns or stub_insns. Must be 8-byte aligned
	 * with PCREL kernels that use prefix instructions in the stub.
	 */
	u32 jump[7];
	/* Used by ftrace to identify stubs */
	u32 magic;
	/* Data for the above code */
	func_desc_t funcdata;
} __aligned(8);
Quoted 35 lines
Quoted 34 lines
Quoted 34 lines

---
  arch/powerpc/kernel/module_64.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
arch/powerpc/kernel/module_64.c +1 -3
index 7112adc597a8..e9bab599d0c2 100644
@@ -651,12 +651,11 @@ static inline int create_stub(const
 Elf64_Shdr *sechdrs,
  	// func_desc_t is 8 bytes if ABIv2, else 16 bytes
  	desc = func_desc(addr);
  	for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) {
-		if (patch_instruction(((u32 *)&entry->funcdata) +
 i,
-				      ppc_inst(((u32
 *)(&desc))[i])))
+		if (patch_u32(((u32 *)&entry->funcdata) + i, ((u32
 *)&desc)[i]))
  			return 0;
  	}
 
-	if (patch_instruction(&entry->magic,

ppc_inst(STUB_MAGIC)))

arch/powerpc/kernel/module_64.c +1 -0
+	if (patch_u32(&entry->magic, STUB_MAGIC))
  		return 0;
 
  	return 1;
Signature · 1 line
2.44.0
#7 lore raw

Re: [PATCH v3 3/5] powerpc/64: Convert patch_instruction() to patch_u32()

Quoted 36 lines · Christophe Leroy

Le 14/05/2024 à 04:59, Benjamin Gray a écrit :

Quoted 33 lines · Naveen N Rao
Quoted 21 lines · Benjamin Gray
Quoted 12 lines

This use of patch_instruction() is working on 32 bit data, and can fail
if the data looks like a prefixed instruction and the extra write crosses a page boundary. Use patch_u32() to fix the write size.

Fixes 8734b41b3efe ("powerpc/module_64: Fix livepatching for RO

modules")

---

Quoted 7 lines

v2: * Added the fixes tag, it seems appropriate even if the subject does
      mention a more robust solution being required.

patch_u64() should be more efficient, but judging from the bug report
it doesn't seem like the data is doubleword aligned.

Quoted 5 lines

Asking again, is that still the case? It looks like at least the first
fix below can be converted to patch_u64().

- Naveen

Sorry, I think I forgot this question last time. Reading the commit descriptions you linked, I don't see any mention of "entry->funcdata will always be doubleword aligned because XYZ". If the patch makes it doubleword aligned anyway, I wouldn't be confident asserting all callers will always do this without looking into it a lot more.

No worries. I was asking primarily to check if you had noticed a specific issue with alignment.

As Christophe mentions, the structure is aligned. It is primarily allotted in a separate stubs section for modules. Looking at it closer though, I wonder if we need the below:

arch/powerpc/kernel/module_64.c +4 -1
index cccb1f78e058..0226d73a0007 100644
@@ -428,8 +428,11 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr,
 
        /* Find .toc and .stubs sections, symtab and strtab */
        for (i = 1; i < hdr->e_shnum; i++) {
-               if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
+               if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0) {
                        me->arch.stubs_section = i;
+                       if (sechdrs[i].sh_addralign < 8)
+                               sechdrs[i].sh_addralign = 8;
+               }
 #ifdef CONFIG_PPC_KERNEL_PCREL
                else if (strcmp(secstrings + sechdrs[i].sh_name, ".data..percpu") == 0)
                        me->arch.pcpu_section = i;
Quoted 5 lines
Quoted 5 lines

Perhaps a separate series could optimise it with appropriate justification/assertions to catch bad alignment. But I think leaving it out of this series is fine because the original works in words, so it's not regressing anything.

That should be fine.

Quoted 4 lines

As far as I can see, the struct is 64 bits aligned by definition so funcdata field is aligned too as there are just 8x u32 before it:

Quoted 13 lines
struct ppc64_stub_entry {
	/*
	 * 28 byte jump instruction sequence (7 instructions) that can
	 * hold ppc64_stub_insns or stub_insns. Must be 8-byte aligned
	 * with PCREL kernels that use prefix instructions in the stub.
	 */
	u32 jump[7];
	/* Used by ftrace to identify stubs */
	u32 magic;
	/* Data for the above code */
	func_desc_t funcdata;
} __aligned(8);

Thanks,
Naveen

#8 lore raw

PATCH v3 4/5 powerpc/32: Convert patch_instruction() to patch_uint()

In reply to Benjamin Gray

These changes are for patch_instruction() uses on data. Unlike ppc64 these should not be incorrect as-is, but using the patch_uint() alias better reflects what kind of data being patched and allows for benchmarking the effect of different patch_* implementations (e.g., skipping instruction flushing when patching data).

---
 arch/powerpc/kernel/static_call.c     | 2 +-
 arch/powerpc/platforms/powermac/smp.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kernel/static_call.c +1 -1
index 863a7aa24650..1502b7e439ca 100644
@@ -17,7 +17,7 @@ void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
 	mutex_lock(&text_mutex);
 
 	if (func && !is_short) {
-		err = patch_instruction(tramp + PPC_SCT_DATA, ppc_inst(target));
+		err = patch_ulong(tramp + PPC_SCT_DATA, target);
 		if (err)
 			goto out;
 	}
arch/powerpc/platforms/powermac/smp.c +1 -1
index 15644be31990..d21b681f52fb 100644
@@ -827,7 +827,7 @@ static int smp_core99_kick_cpu(int nr)
 	mdelay(1);
 
 	/* Restore our exception vector */
-	patch_instruction(vector, ppc_inst(save_vector));
+	patch_uint(vector, save_vector);
 
 	local_irq_restore(flags);
 	if (ppc_md.progress) ppc_md.progress("smp_core99_kick_cpu done", 0x347);
Signature · 1 line
2.44.0
#9 lore raw

PATCH v3 5/5 powerpc/code-patching: Add boot selftest for data patching

In reply to Benjamin Gray

Extend the code patching selftests with some basic coverage of the new data patching variants too.

---

v3: * New in v3

---
 arch/powerpc/lib/test-code-patching.c | 36 +++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
arch/powerpc/lib/test-code-patching.c +36 -0
index c44823292f73..e96c48fcd4db 100644
@@ -347,6 +347,41 @@ static void __init test_prefixed_patching(void)
 	check(!memcmp(iptr, expected, sizeof(expected)));
 }
 
+static void __init test_data_patching(void)
+{
+	void *buf;
+	u32 *addr32;
+
+	buf = vzalloc(PAGE_SIZE);
+	check(buf);
+	if (!buf)
+		return;
+
+	addr32 = buf + 128;
+
+	addr32[1] = 0xA0A1A2A3;
+	addr32[2] = 0xB0B1B2B3;
+
+	patch_uint(&addr32[1], 0xC0C1C2C3);
+
+	check(addr32[0] == 0);
+	check(addr32[1] == 0xC0C1C2C3);
+	check(addr32[2] == 0xB0B1B2B3);
+	check(addr32[3] == 0);
+
+	patch_ulong(&addr32[1], 0xD0D1D2D3);
+
+	check(addr32[0] == 0);
+	*(unsigned long *)(&addr32[1]) = 0xD0D1D2D3;
+
+	if (!IS_ENABLED(CONFIG_PPC64))
+		check(addr32[2] == 0xB0B1B2B3);
+
+	check(addr32[3] == 0);
+
+	vfree(buf);
+}
+
 static int __init test_code_patching(void)
 {
 	pr_info("Running code patching self-tests ...\n");
@@ -356,6 +391,7 @@ static int __init test_code_patching(void)
 	test_create_function_call();
 	test_translate_branch();
 	test_prefixed_patching();
+	test_data_patching();
 
 	return 0;
 }
Signature · 1 line
2.44.0
#10 lore raw

Re: [PATCH v3 5/5] powerpc/code-patching: Add boot selftest for data patching

Quoted 36 lines · Benjamin Gray

Extend the code patching selftests with some basic coverage of the new data patching variants too.

---

v3: * New in v3

---
 arch/powerpc/lib/test-code-patching.c | 36 +++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
arch/powerpc/lib/test-code-patching.c +16 -0
index c44823292f73..e96c48fcd4db 100644
@@ -347,6 +347,41 @@ static void __init test_prefixed_patching(void)
 	check(!memcmp(iptr, expected, sizeof(expected)));
 }
 
+static void __init test_data_patching(void)
+{
+	void *buf;
+	u32 *addr32;
+
+	buf = vzalloc(PAGE_SIZE);
+	check(buf);
+	if (!buf)
+		return;
+
+	addr32 = buf + 128;
+
+	addr32[1] = 0xA0A1A2A3;
+	addr32[2] = 0xB0B1B2B3;
+
+	patch_uint(&addr32[1], 0xC0C1C2C3);

arch/powerpc/lib/test-code-patching.c static void __init test_prefixed_patching(void) · line 375

Quoted 10 lines
arch/powerpc/lib/test-code-patching.c +10 -0
+
+	check(addr32[0] == 0);
+	check(addr32[1] == 0xC0C1C2C3);
+	check(addr32[2] == 0xB0B1B2B3);
+	check(addr32[3] == 0);
+
+	patch_ulong(&addr32[1], 0xD0D1D2D3);
+
+	check(addr32[0] == 0);
+	*(unsigned long *)(&addr32[1]) = 0xD0D1D2D3;

Should that have been a check() instead?

- Naveen

Quoted 23 lines
arch/powerpc/lib/test-code-patching.c +10 -0
+
+	if (!IS_ENABLED(CONFIG_PPC64))
+		check(addr32[2] == 0xB0B1B2B3);
+
+	check(addr32[3] == 0);
+
+	vfree(buf);
+}
+
 static int __init test_code_patching(void)
 {
 	pr_info("Running code patching self-tests ...\n");
@@ -356,6 +391,7 @@ static int __init test_code_patching(void)
 	test_create_function_call();
 	test_translate_branch();
 	test_prefixed_patching();
+	test_data_patching();
 
 	return 0;
 }
Signature · 1 line
2.44.0
Acked-by on the v3 series
#11 lore raw
In reply to Benjamin Gray
Quoted 37 lines · Benjamin Gray

Currently patch_instruction() bases the write length on the value being written. If the value looks like a prefixed instruction it writes 8 bytes, otherwise it writes 4 bytes. This makes it potentially buggy to use for writing arbitrary data, as if you want to write 4 bytes but it decides to write 8 bytes it may clobber the following memory or be unaligned and trigger an oops if it tries to cross a page boundary.

To solve this, this series pulls out the size parameter to the 'top' of the memory patching logic, and propagates it through the various functions.

The two sizes supported are int and long; this allows for patching instructions and pointers on both ppc32 and ppc64. On ppc32 these are the same size, so care is taken to only use the size parameter on static functions, so the compiler can optimise it out entirely. Unfortunately GCC trips over its own feet here and won't optimise in a way that is optimal for strict RWX (mpc85xx_smp_defconfig) and no RWX (pmac32_defconfig). More details in the v2 cover letter.

Changes from v2:
* Various changes noted on each patch
* Data patching now enforced to be aligned
* Restore page aligned flushing optimisation

Changes from v1:
* Addressed the v1 review actions
* Removed noinline (for now)

v2: https://patchwork.ozlabs.org/project/linuxppc-dev/cover/[email protected]/
v1: https://patchwork.ozlabs.org/project/linuxppc-dev/cover/[email protected]/

Benjamin Gray (5):
powerpc/code-patching: Add generic memory patching powerpc/code-patching: Add data patch alignment check powerpc/64: Convert patch_instruction() to patch_u32() powerpc/32: Convert patch_instruction() to patch_uint() powerpc/code-patching: Add boot selftest for data patching

Quoted 7 lines
 arch/powerpc/include/asm/code-patching.h | 37 +++++++++++++
 arch/powerpc/kernel/module_64.c          |  5 +-
 arch/powerpc/kernel/static_call.c        |  2 +-
 arch/powerpc/lib/code-patching.c         | 70 +++++++++++++++++++-----
 arch/powerpc/lib/test-code-patching.c    | 36 ++++++++++++
 arch/powerpc/platforms/powermac/smp.c    |  2 +-
 6 files changed, 132 insertions(+), 20 deletions(-)

Apart from the minor comments, for this series:

Thanks for working on this.

- Naveen