lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <426fdad5-17a5-41e7-57b9-aa4c1a4f4327@huaweicloud.com>
Date:   Thu, 5 Jan 2023 11:47:27 +0800
From:   Pu Lehui <pulehui@...weicloud.com>
To:     Yonghong Song <yhs@...a.com>, bpf@...r.kernel.org,
        netdev@...r.kernel.org, linux-kernel@...r.kernel.org
Cc:     Alexei Starovoitov <ast@...nel.org>,
        Daniel Borkmann <daniel@...earbox.net>,
        Andrii Nakryiko <andrii@...nel.org>,
        Martin KaFai Lau <martin.lau@...ux.dev>,
        Song Liu <song@...nel.org>, Yonghong Song <yhs@...com>,
        John Fastabend <john.fastabend@...il.com>,
        KP Singh <kpsingh@...nel.org>,
        Stanislav Fomichev <sdf@...gle.com>,
        Hao Luo <haoluo@...gle.com>, Jiri Olsa <jolsa@...nel.org>,
        Pu Lehui <pulehui@...wei.com>
Subject: Re: [PATCH bpf-next] bpf, x86: Simplify the parsing logic of
 structure parameters



On 2023/1/5 2:24, Yonghong Song wrote:
> 
> 
> On 1/2/23 5:31 PM, Pu Lehui wrote:
>> From: Pu Lehui <pulehui@...wei.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@...wei.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@...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?
> 

agree, will do as suggested bellow.

>>       /* 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;
>>       }
>>   }
> [...]

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ