BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@meta.com>
To: Pu Lehui <pulehui@huaweicloud.com>,
	bpf@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>, Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>, Pu Lehui <pulehui@huawei.com>
Subject: Re: [PATCH bpf-next] bpf, x86: Simplify the parsing logic of structure parameters
Date: Wed, 4 Jan 2023 10:24:07 -0800	[thread overview]
Message-ID: <ba11a68a-6099-0e0e-6531-e70e64429b7e@meta.com> (raw)
In-Reply-To: <20230103013158.1945869-1-pulehui@huaweicloud.com>



On 1/2/23 5:31 PM, Pu Lehui wrote:
> From: Pu Lehui <pulehui@huawei.com>
> 
> Extra_nregs of structure parameters and nr_args can be
> added directly at the beginning, and using a flip flag
> to identifiy structure parameters. Meantime, renaming
> some variables to make them more sense.
> 
> Signed-off-by: Pu Lehui <pulehui@huawei.com>

Thanks for refactoring. Using nr_regs instead of nr_args indeed
making things easier to understand. Ack with a few nits below.

Acked-by: Yonghong Song <yhs@fb.com>

> ---
>   arch/x86/net/bpf_jit_comp.c | 99 +++++++++++++++++--------------------
>   1 file changed, 46 insertions(+), 53 deletions(-)
> 
> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> index e3e2b57e4e13..e7b72299f5a4 100644
> --- a/arch/x86/net/bpf_jit_comp.c
> +++ b/arch/x86/net/bpf_jit_comp.c
> @@ -1839,62 +1839,57 @@ st:			if (is_imm8(insn->off))
>   	return proglen;
>   }
>   
> -static void save_regs(const struct btf_func_model *m, u8 **prog, int nr_args,
> +static void save_regs(const struct btf_func_model *m, u8 **prog, int nr_regs,
>   		      int stack_size)
>   {
> -	int i, j, arg_size, nr_regs;
> +	int i, j, arg_size;
> +	bool is_struct = false;
> +
>   	/* Store function arguments to stack.
>   	 * For a function that accepts two pointers the sequence will be:
>   	 * mov QWORD PTR [rbp-0x10],rdi
>   	 * mov QWORD PTR [rbp-0x8],rsi
>   	 */
> -	for (i = 0, j = 0; i < min(nr_args, 6); i++) {
> -		if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG) {
> -			nr_regs = (m->arg_size[i] + 7) / 8;
> +	for (i = 0, j = 0; i < min(nr_regs, 6); i++) {
> +		arg_size = m->arg_size[j];
> +		if (arg_size > 8) {
>   			arg_size = 8;
> -		} else {
> -			nr_regs = 1;
> -			arg_size = m->arg_size[i];
> +			is_struct ^= 1;
>   		}
>   
> -		while (nr_regs) {
> -			emit_stx(prog, bytes_to_bpf_size(arg_size),
> -				 BPF_REG_FP,
> -				 j == 5 ? X86_REG_R9 : BPF_REG_1 + j,
> -				 -(stack_size - j * 8));
> -			nr_regs--;
> -			j++;
> -		}
> +		emit_stx(prog, bytes_to_bpf_size(arg_size),
> +			 BPF_REG_FP,
> +			 i == 5 ? X86_REG_R9 : BPF_REG_1 + i,
> +			 -(stack_size - i * 8));
> +
> +		j = is_struct ? j : j + 1;
>   	}
>   }
>   
> -static void restore_regs(const struct btf_func_model *m, u8 **prog, int nr_args,
> +static void restore_regs(const struct btf_func_model *m, u8 **prog, int nr_regs,
>   			 int stack_size)
>   {
> -	int i, j, arg_size, nr_regs;
> +	int i, j, arg_size;
> +	bool is_struct = false;

Maybe
	bool next_same_struct = false
to better characterize what it means?

>   
>   	/* Restore function arguments from stack.
>   	 * For a function that accepts two pointers the sequence will be:
>   	 * EMIT4(0x48, 0x8B, 0x7D, 0xF0); mov rdi,QWORD PTR [rbp-0x10]
>   	 * EMIT4(0x48, 0x8B, 0x75, 0xF8); mov rsi,QWORD PTR [rbp-0x8]
>   	 */
> -	for (i = 0, j = 0; i < min(nr_args, 6); i++) {
> -		if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG) {
> -			nr_regs = (m->arg_size[i] + 7) / 8;
> +	for (i = 0, j = 0; i < min(nr_regs, 6); i++) {

Let us put a comment here so the later users can understand the logic
behind 'is_struct ^= 1'.

/* The arg_size is at most 16 bytes, enforced by the verifier. */

> +		arg_size = m->arg_size[j];
> +		if (arg_size > 8) {
>   			arg_size = 8;
> -		} else {
> -			nr_regs = 1;
> -			arg_size = m->arg_size[i];
> +			is_struct ^= 1;

next_same_struct = !next_same_struct;

The same for above save_regs().

>   		}
>   
> -		while (nr_regs) {
> -			emit_ldx(prog, bytes_to_bpf_size(arg_size),
> -				 j == 5 ? X86_REG_R9 : BPF_REG_1 + j,
> -				 BPF_REG_FP,
> -				 -(stack_size - j * 8));
> -			nr_regs--;
> -			j++;
> -		}
> +		emit_ldx(prog, bytes_to_bpf_size(arg_size),
> +			 i == 5 ? X86_REG_R9 : BPF_REG_1 + i,
> +			 BPF_REG_FP,
> +			 -(stack_size - i * 8));
> +
> +		j = is_struct ? j : j + 1;
>   	}
>   }
>   
[...]

  reply	other threads:[~2023-01-04 18:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-03  1:31 [PATCH bpf-next] bpf, x86: Simplify the parsing logic of structure parameters Pu Lehui
2023-01-04 18:24 ` Yonghong Song [this message]
2023-01-05  3:47   ` Pu Lehui

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=ba11a68a-6099-0e0e-6531-e70e64429b7e@meta.com \
    --to=yhs@meta.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=netdev@vger.kernel.org \
    --cc=pulehui@huawei.com \
    --cc=pulehui@huaweicloud.com \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=yhs@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