From: Hari Bathini <hbathini@linux.ibm.com>
To: Christophe Leroy <christophe.leroy@csgroup.eu>,
linuxppc-dev <linuxppc-dev@lists.ozlabs.org>,
"bpf@vger.kernel.org" <bpf@vger.kernel.org>
Cc: "Naveen N. Rao" <naveen.n.rao@linux.ibm.com>,
Song Liu <songliubraving@fb.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>
Subject: Re: [RFC PATCH 1/3] powerpc/bpf: implement bpf_arch_text_copy
Date: Mon, 14 Nov 2022 20:24:16 +0530 [thread overview]
Message-ID: <e6304866-26f4-51f6-c825-9355a2d15b80@linux.ibm.com> (raw)
In-Reply-To: <cd26aa17-962f-aaab-a7bc-203a0d63f6c9@csgroup.eu>
On 13/11/22 6:47 pm, Christophe Leroy wrote:
> Le 10/11/2022 à 19:43, Hari Bathini a écrit :
>> bpf_arch_text_copy is used to dump JITed binary to RX page, allowing
>> multiple BPF programs to share the same page. Using patch_instruction
>> to implement it.
>
> Using patch_instruction() is nice for a quick implementation, but it is
> probably suboptimal. Due to the amount of data to be copied, it is worth
Yeah.
> a dedicated function that maps a RW copy of the page to be updated then
> does the copy at once with memcpy() then unmaps the page.
I will see if I can come up with such implementation for the respin.
>
>>
>> Signed-off-by: Hari Bathini <hbathini@linux.ibm.com>
>> ---
>> arch/powerpc/net/bpf_jit_comp.c | 39 ++++++++++++++++++++++++++++++++-
>> 1 file changed, 38 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
>> index 43e634126514..7383e0effad2 100644
>> --- a/arch/powerpc/net/bpf_jit_comp.c
>> +++ b/arch/powerpc/net/bpf_jit_comp.c
>> @@ -13,9 +13,12 @@
>> #include <linux/netdevice.h>
>> #include <linux/filter.h>
>> #include <linux/if_vlan.h>
>> -#include <asm/kprobes.h>
>> +#include <linux/memory.h>
>> #include <linux/bpf.h>
>>
>> +#include <asm/kprobes.h>
>> +#include <asm/code-patching.h>
>> +
>> #include "bpf_jit.h"
>>
>> static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
>> @@ -23,6 +26,35 @@ static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
>> memset32(area, BREAKPOINT_INSTRUCTION, size / 4);
>> }
>>
>> +/*
>> + * Patch 'len' bytes of instructions from opcode to addr, one instruction
>> + * at a time. Returns addr on success. ERR_PTR(-EINVAL), otherwise.
>> + */
>> +static void *bpf_patch_instructions(void *addr, void *opcode, size_t len)
>> +{
>> + void *ret = ERR_PTR(-EINVAL);
>> + size_t patched = 0;
>> + u32 *inst = opcode;
>> + u32 *start = addr;
>> +
>> + if (WARN_ON_ONCE(core_kernel_text((unsigned long)addr)))
>> + return ret;
>> +
>> + mutex_lock(&text_mutex);
>> + while (patched < len) {
>> + if (patch_instruction(start++, ppc_inst(*inst)))
>> + goto error;
>> +
>> + inst++;
>> + patched += 4;
>> + }
>> +
>> + ret = addr;
>> +error:
>> + mutex_unlock(&text_mutex);
>> + return ret;
>> +}
>> +
>> /* Fix updated addresses (for subprog calls, ldimm64, et al) during extra pass */
>> static int bpf_jit_fixup_addresses(struct bpf_prog *fp, u32 *image,
>> struct codegen_context *ctx, u32 *addrs)
>> @@ -357,3 +389,8 @@ int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, int pass, struct code
>> ctx->exentry_idx++;
>> return 0;
>> }
>> +
>> +void *bpf_arch_text_copy(void *dst, void *src, size_t len)
>> +{
>> + return bpf_patch_instructions(dst, src, len);
>> +}
>
> I can't see the added value of having two functions when the first one
> just calls the second one and is the only user of it. Why not have
> implemented bpf_patch_instructions() directly inside bpf_arch_text_copy() ?
>
> By the way, it can be nice to have two functions, but split them
> differently, to avoid the goto: etc ....
>
> I also prefer using for loops instead of while loops.
>
> It could have looked like below (untested):
>
> static void *bpf_patch_instructions(void *addr, void *opcode, size_t len)
> {
> u32 *inst = opcode;
> u32 *start = addr;
> u32 *end = addr + len;
>
> for (inst = opcode, start = addr; start < end; inst++, start++) {
> if (patch_instruction(start, ppc_inst(*inst)))
> return ERR_PTR(-EINVAL);
> }
>
> return addr;
> }
>
> void *bpf_arch_text_copy(void *dst, void *src, size_t len)
> {
> if (WARN_ON_ONCE(core_kernel_text((unsigned long)dst)))
> return ret;
>
> mutex_lock(&text_mutex);
>
> ret = bpf_patch_instructions(dst, src, len);
>
> mutex_unlock(&text_mutex);
>
> return ret;
> }
>
>
Sure. Will use this.
Thanks
Hari
next prev parent reply other threads:[~2022-11-14 14:54 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-10 18:43 [RFC PATCH 0/3] enable bpf_prog_pack allocator for powerpc Hari Bathini
2022-11-10 18:43 ` [RFC PATCH 1/3] powerpc/bpf: implement bpf_arch_text_copy Hari Bathini
2022-11-13 13:17 ` Christophe Leroy
2022-11-14 14:54 ` Hari Bathini [this message]
2022-11-10 18:43 ` [RFC PATCH 2/3] powerpc/bpf: implement bpf_arch_text_invalidate for bpf_prog_pack Hari Bathini
2022-11-13 18:00 ` Christophe Leroy
2022-11-10 18:43 ` [RFC PATCH 3/3] powerpc/bpf: use bpf_jit_binary_pack_[alloc|finalize|free] Hari Bathini
2022-11-13 18:36 ` Christophe Leroy
2022-11-11 11:25 ` [RFC PATCH 0/3] enable bpf_prog_pack allocator for powerpc Christophe Leroy
2022-11-14 14:47 ` Hari Bathini
[not found] ` <bf0af91e-861c-1608-7150-d31578be9b02@csgroup.eu>
[not found] ` <e0266414-843f-db48-a56d-1d8a8944726a@csgroup.eu>
2022-11-16 17:01 ` Hari Bathini
2022-11-16 17:16 ` Christophe Leroy
2022-11-17 6:59 ` Christophe Leroy
2022-11-18 8:39 ` Hari Bathini
2022-11-18 8:51 ` Christophe Leroy
2022-11-18 9:39 ` Hari Bathini
2022-11-18 11:46 ` Christophe Leroy
2022-11-18 17:28 ` Song Liu
2022-11-18 18:05 ` Christophe Leroy
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=e6304866-26f4-51f6-c825-9355a2d15b80@linux.ibm.com \
--to=hbathini@linux.ibm.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=christophe.leroy@csgroup.eu \
--cc=daniel@iogearbox.net \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=naveen.n.rao@linux.ibm.com \
--cc=songliubraving@fb.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