linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Christophe Leroy <christophe.leroy@csgroup.eu>
To: Benjamin Gray <bgray@linux.ibm.com>,
	"linuxppc-dev@lists.ozlabs.org" <linuxppc-dev@lists.ozlabs.org>
Cc: "ajd@linux.ibm.com" <ajd@linux.ibm.com>,
	Peter Zijlstra <peterz@infradead.org>,
	"npiggin@gmail.com" <npiggin@gmail.com>,
	Ard Biesheuvel <ardb@kernel.org>, Jason Baron <jbaron@akamai.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Josh Poimboeuf <jpoimboe@kernel.org>
Subject: Re: [RFC PATCH 1/4] powerpc/code-patching: add patch_memory() for writing RO text
Date: Thu, 1 Sep 2022 07:01:17 +0000	[thread overview]
Message-ID: <d0ac912f-e665-42a1-c7bf-e62294e17b66@csgroup.eu> (raw)
In-Reply-To: <20220901055823.152983-2-bgray@linux.ibm.com>



Le 01/09/2022 à 07:58, Benjamin Gray a écrit :
> 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);

This is not in line with the optimisation done by 
https://patchwork.ozlabs.org/project/linuxppc-dev/patch/20220815114840.1468656-1-mpe@ellerman.id.au/




> +       if (err)
> +               return err;
> +
> +       patch_addr = text_poke_addr + offset_in_page(dest);
> +       copy_to_kernel_nofault((u8 *)patch_addr, src, size);

copy_to_kernel_nofault() has a performance cost.

> +
> +       flush_icache_range(patch_addr, size);

Is that needed ? We are patching data, not text.

> +       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);

In do_patch_memory() you have flush_icache_range(patch_addr, size);

If that's really needed there, why don't we need it here as well ?

> +}
> +
>   #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  7:02 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 ` [RFC PATCH 1/4] powerpc/code-patching: add patch_memory() for writing RO text Benjamin Gray
2022-09-01  7:01   ` Christophe Leroy [this message]
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=d0ac912f-e665-42a1-c7bf-e62294e17b66@csgroup.eu \
    --to=christophe.leroy@csgroup.eu \
    --cc=ajd@linux.ibm.com \
    --cc=ardb@kernel.org \
    --cc=bgray@linux.ibm.com \
    --cc=jbaron@akamai.com \
    --cc=jpoimboe@kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=npiggin@gmail.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

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

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