Re: [PATCH bpf-next] bpf, x86: Simplify the parsing logic of structure parameters

From: Pu Lehui
Date: Wed Jan 04 2023 - 23:09:29 EST




On 2023/1/5 2:24, Yonghong Song wrote:


On 1/2/23 5:31 PM, Pu Lehui wrote:
From: Pu Lehui <pulehui@xxxxxxxxxx>

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@xxxxxxxxxx>

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@xxxxxx>

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