linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Gray <bgray@linux.ibm.com>
To: linuxppc-dev@lists.ozlabs.org
Cc: christophe.leroy@c-s.fr, Benjamin Gray <bgray@linux.ibm.com>,
	ajd@linux.ibm.com, npiggin@gmail.com
Subject: [RFC PATCH 1/4] powerpc/code-patching: add patch_memory() for writing RO text
Date: Thu,  1 Sep 2022 15:58:20 +1000	[thread overview]
Message-ID: <20220901055823.152983-2-bgray@linux.ibm.com> (raw)
In-Reply-To: <20220901055823.152983-1-bgray@linux.ibm.com>

From: Russell Currey <ruscur@russell.cc>

powerpc allocates a text poke area of one page that is used by
patch_instruction() to modify read-only text when STRICT_KERNEL_RWX
is enabled.

patch_instruction() is only designed for instructions,
so writing data using the text poke area can only happen 4 bytes
at a time - each with a page map/unmap, pte flush and syncs.

This patch introduces patch_memory(), implementing the same
interface as memcpy(), similar to x86's text_poke() and s390's
s390_kernel_write().  patch_memory() only needs to map the text
poke area once, unless the write would cross a page boundary.

Signed-off-by: Russell Currey <ruscur@russell.cc>
Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
---
 arch/powerpc/include/asm/code-patching.h |  1 +
 arch/powerpc/lib/code-patching.c         | 65 ++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/arch/powerpc/include/asm/code-patching.h b/arch/powerpc/include/asm/code-patching.h
index 1c6316ec4b74..3de90748bce7 100644
--- a/arch/powerpc/include/asm/code-patching.h
+++ b/arch/powerpc/include/asm/code-patching.h
@@ -76,6 +76,7 @@ int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
 int patch_branch(u32 *addr, unsigned long target, int flags);
 int patch_instruction(u32 *addr, ppc_inst_t instr);
 int raw_patch_instruction(u32 *addr, ppc_inst_t instr);
+void *patch_memory(void *dest, const void *src, size_t size);
 
 static inline unsigned long patch_site_addr(s32 *site)
 {
diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
index 6edf0697a526..0cca39af44cb 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -14,6 +14,7 @@
 #include <asm/page.h>
 #include <asm/code-patching.h>
 #include <asm/inst.h>
+#include <asm/cacheflush.h>
 
 static int __patch_instruction(u32 *exec_addr, ppc_inst_t instr, u32 *patch_addr)
 {
@@ -183,6 +184,65 @@ static int do_patch_instruction(u32 *addr, ppc_inst_t instr)
 
 	return err;
 }
+
+static int do_patch_memory(void *dest, const void *src, size_t size)
+{
+	int err;
+	unsigned long text_poke_addr, patch_addr;
+
+	text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
+
+	err = map_patch_area(dest, text_poke_addr);
+	if (err)
+		return err;
+
+	patch_addr = text_poke_addr + offset_in_page(dest);
+	copy_to_kernel_nofault((u8 *)patch_addr, src, size);
+
+	flush_icache_range(patch_addr, size);
+	unmap_patch_area(text_poke_addr);
+
+	return 0;
+}
+
+/**
+ * patch_memory - write data using the text poke area
+ *
+ * @dest:	destination address
+ * @src:	source address
+ * @size:	size in bytes
+ *
+ * like memcpy(), but using the text poke area. No atomicity guarantees.
+ * Do not use for instructions, use patch_instruction() instead.
+ * Handles crossing page boundaries, though you shouldn't need to.
+ *
+ * Return value:
+ * 	@dest
+ **/
+void *patch_memory(void *dest, const void *src, size_t size)
+{
+	int err;
+	unsigned long flags;
+	size_t written, write_size;
+
+	// If the poke area isn't set up, it's early boot and we can just memcpy.
+	if (!this_cpu_read(text_poke_area))
+		return memcpy(dest, src, size);
+
+	for (written = 0; written < size; written += write_size) {
+		// Write as much as possible without crossing a page boundary.
+		write_size = min_t(size_t, size - written,
+				   PAGE_SIZE - offset_in_page(dest + written));
+
+		local_irq_save(flags);
+		err = do_patch_memory(dest + written, src + written, write_size);
+		local_irq_restore(flags);
+		if (err)
+			return ERR_PTR(err);
+	}
+
+	return dest;
+}
 #else /* !CONFIG_STRICT_KERNEL_RWX */
 
 static int do_patch_instruction(u32 *addr, ppc_inst_t instr)
@@ -190,6 +250,11 @@ static int do_patch_instruction(u32 *addr, ppc_inst_t instr)
 	return raw_patch_instruction(addr, instr);
 }
 
+void *patch_memory(void *dest, const void *src, size_t size)
+{
+	return memcpy(dest, src, size);
+}
+
 #endif /* CONFIG_STRICT_KERNEL_RWX */
 
 __ro_after_init DEFINE_STATIC_KEY_FALSE(init_mem_is_free);
-- 
2.37.2


  reply	other threads:[~2022-09-01  6:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-01  5:58 [RFC PATCH 0/4] Out-of-line static calls for powerpc64 ELF V2 Benjamin Gray
2022-09-01  5:58 ` Benjamin Gray [this message]
2022-09-01  7:01   ` [RFC PATCH 1/4] powerpc/code-patching: add patch_memory() for writing RO text Christophe Leroy
2022-09-06  1:53     ` Russell Currey
2022-09-01  5:58 ` [RFC PATCH 2/4] static_call: Move static call selftest to static_call.c Benjamin Gray
2022-09-01  6:50   ` Christophe Leroy
2022-09-01  5:58 ` [RFC PATCH 3/4] powerpc/64: Add support for out-of-line static calls Benjamin Gray
2022-09-01  7:33   ` Christophe Leroy
2022-09-01  5:58 ` [RFC PATCH 4/4] powerpc/64: Add tests " Benjamin Gray
2022-09-01  8:07 ` [RFC PATCH 0/4] Out-of-line static calls for powerpc64 ELF V2 Christophe Leroy
2022-09-13  3:31 ` Benjamin Gray

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20220901055823.152983-2-bgray@linux.ibm.com \
    --to=bgray@linux.ibm.com \
    --cc=ajd@linux.ibm.com \
    --cc=christophe.leroy@c-s.fr \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=npiggin@gmail.com \
    /path/to/YOUR_REPLY

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

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