Message ID | 20221018145538.2046842-1-xukuohai@huaweicloud.com (mailing list archive) |
---|---|
State | Accepted |
Commit | d9740535b857650bd6211a67ac0c0d574cba1dce |
Delegated to: | BPF |
Headers | show |
Series | libbpf: Avoid allocating reg_name with sscanf in parse_usdt_arg() | expand |
Context | Check | Description |
---|---|---|
netdev/tree_selection | success | Not a local patch |
bpf/vmtest-bpf-next-PR | success | PR summary |
bpf/vmtest-bpf-next-VM_Test-1 | success | Logs for build for s390x with gcc |
bpf/vmtest-bpf-next-VM_Test-14 | success | Logs for test_progs_no_alu32 on x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-16 | success | Logs for test_verifier on x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-17 | success | Logs for test_verifier on x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-7 | success | Logs for test_maps on x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-8 | success | Logs for test_maps on x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-10 | success | Logs for test_progs on x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-11 | success | Logs for test_progs on x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-13 | success | Logs for test_progs_no_alu32 on x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-6 | success | Logs for test_maps on s390x with gcc |
bpf/vmtest-bpf-next-VM_Test-12 | success | Logs for test_progs_no_alu32 on s390x with gcc |
bpf/vmtest-bpf-next-VM_Test-15 | success | Logs for test_verifier on s390x with gcc |
bpf/vmtest-bpf-next-VM_Test-9 | success | Logs for test_progs on s390x with gcc |
bpf/vmtest-bpf-next-VM_Test-4 | success | Logs for llvm-toolchain |
bpf/vmtest-bpf-next-VM_Test-5 | success | Logs for set-matrix |
bpf/vmtest-bpf-next-VM_Test-2 | success | Logs for build for x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-3 | success | Logs for build for x86_64 with llvm-16 |
On 10/18, Xu Kuohai wrote: > From: Xu Kuohai <xukuohai@huawei.com> > The reg_name in parse_usdt_arg() is used to hold register name, which > is short enough to be held in a 16-byte array, so we could define > reg_name as char reg_name[16] to avoid dynamically allocating reg_name > with sscanf. > Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com> > Signed-off-by: Xu Kuohai <xukuohai@huawei.com> Addresses Andrii's suggestion from the following: https://lore.kernel.org/bpf/86c88c01-22eb-b7f8-9c65-0faf97b4096b@huawei.com/ Acked-by: Stanislav Fomichev <sdf@google.com> > --- > tools/lib/bpf/usdt.c | 16 ++++++---------- > 1 file changed, 6 insertions(+), 10 deletions(-) > diff --git a/tools/lib/bpf/usdt.c b/tools/lib/bpf/usdt.c > index 49f3c3b7f609..28fa1b2283de 100644 > --- a/tools/lib/bpf/usdt.c > +++ b/tools/lib/bpf/usdt.c > @@ -1225,26 +1225,24 @@ static int calc_pt_regs_off(const char *reg_name) > static int parse_usdt_arg(const char *arg_str, int arg_num, struct > usdt_arg_spec *arg) > { > - char *reg_name = NULL; > + char reg_name[16]; > int arg_sz, len, reg_off; > long off; > - if (sscanf(arg_str, " %d @ %ld ( %%%m[^)] ) %n", &arg_sz, &off, > ®_name, &len) == 3) { > + if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n", &arg_sz, &off, > reg_name, &len) == 3) { > /* Memory dereference case, e.g., -4@-20(%rbp) */ > arg->arg_type = USDT_ARG_REG_DEREF; > arg->val_off = off; > reg_off = calc_pt_regs_off(reg_name); > - free(reg_name); > if (reg_off < 0) > return reg_off; > arg->reg_off = reg_off; > - } else if (sscanf(arg_str, " %d @ %%%ms %n", &arg_sz, ®_name, &len) > == 2) { > + } else if (sscanf(arg_str, " %d @ %%%15s %n", &arg_sz, reg_name, &len) > == 2) { > /* Register read case, e.g., -4@%eax */ > arg->arg_type = USDT_ARG_REG; > arg->val_off = 0; > reg_off = calc_pt_regs_off(reg_name); > - free(reg_name); > if (reg_off < 0) > return reg_off; > arg->reg_off = reg_off; > @@ -1456,16 +1454,15 @@ static int calc_pt_regs_off(const char *reg_name) > static int parse_usdt_arg(const char *arg_str, int arg_num, struct > usdt_arg_spec *arg) > { > - char *reg_name = NULL; > + char reg_name[16]; > int arg_sz, len, reg_off; > long off; > - if (sscanf(arg_str, " %d @ %ld ( %m[a-z0-9] ) %n", &arg_sz, &off, > ®_name, &len) == 3) { > + if (sscanf(arg_str, " %d @ %ld ( %15[a-z0-9] ) %n", &arg_sz, &off, > reg_name, &len) == 3) { > /* Memory dereference case, e.g., -8@-88(s0) */ > arg->arg_type = USDT_ARG_REG_DEREF; > arg->val_off = off; > reg_off = calc_pt_regs_off(reg_name); > - free(reg_name); > if (reg_off < 0) > return reg_off; > arg->reg_off = reg_off; > @@ -1474,12 +1471,11 @@ static int parse_usdt_arg(const char *arg_str, > int arg_num, struct usdt_arg_spec > arg->arg_type = USDT_ARG_CONST; > arg->val_off = off; > arg->reg_off = 0; > - } else if (sscanf(arg_str, " %d @ %m[a-z0-9] %n", &arg_sz, ®_name, > &len) == 2) { > + } else if (sscanf(arg_str, " %d @ %15[a-z0-9] %n", &arg_sz, reg_name, > &len) == 2) { > /* Register read case, e.g., -8@a1 */ > arg->arg_type = USDT_ARG_REG; > arg->val_off = 0; > reg_off = calc_pt_regs_off(reg_name); > - free(reg_name); > if (reg_off < 0) > return reg_off; > arg->reg_off = reg_off; > -- > 2.30.2
Hello: This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <andrii@kernel.org>: On Tue, 18 Oct 2022 10:55:38 -0400 you wrote: > From: Xu Kuohai <xukuohai@huawei.com> > > The reg_name in parse_usdt_arg() is used to hold register name, which > is short enough to be held in a 16-byte array, so we could define > reg_name as char reg_name[16] to avoid dynamically allocating reg_name > with sscanf. > > [...] Here is the summary with links: - libbpf: Avoid allocating reg_name with sscanf in parse_usdt_arg() https://git.kernel.org/bpf/bpf-next/c/d9740535b857 You are awesome, thank you!
diff --git a/tools/lib/bpf/usdt.c b/tools/lib/bpf/usdt.c index 49f3c3b7f609..28fa1b2283de 100644 --- a/tools/lib/bpf/usdt.c +++ b/tools/lib/bpf/usdt.c @@ -1225,26 +1225,24 @@ static int calc_pt_regs_off(const char *reg_name) static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg) { - char *reg_name = NULL; + char reg_name[16]; int arg_sz, len, reg_off; long off; - if (sscanf(arg_str, " %d @ %ld ( %%%m[^)] ) %n", &arg_sz, &off, ®_name, &len) == 3) { + if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n", &arg_sz, &off, reg_name, &len) == 3) { /* Memory dereference case, e.g., -4@-20(%rbp) */ arg->arg_type = USDT_ARG_REG_DEREF; arg->val_off = off; reg_off = calc_pt_regs_off(reg_name); - free(reg_name); if (reg_off < 0) return reg_off; arg->reg_off = reg_off; - } else if (sscanf(arg_str, " %d @ %%%ms %n", &arg_sz, ®_name, &len) == 2) { + } else if (sscanf(arg_str, " %d @ %%%15s %n", &arg_sz, reg_name, &len) == 2) { /* Register read case, e.g., -4@%eax */ arg->arg_type = USDT_ARG_REG; arg->val_off = 0; reg_off = calc_pt_regs_off(reg_name); - free(reg_name); if (reg_off < 0) return reg_off; arg->reg_off = reg_off; @@ -1456,16 +1454,15 @@ static int calc_pt_regs_off(const char *reg_name) static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg) { - char *reg_name = NULL; + char reg_name[16]; int arg_sz, len, reg_off; long off; - if (sscanf(arg_str, " %d @ %ld ( %m[a-z0-9] ) %n", &arg_sz, &off, ®_name, &len) == 3) { + if (sscanf(arg_str, " %d @ %ld ( %15[a-z0-9] ) %n", &arg_sz, &off, reg_name, &len) == 3) { /* Memory dereference case, e.g., -8@-88(s0) */ arg->arg_type = USDT_ARG_REG_DEREF; arg->val_off = off; reg_off = calc_pt_regs_off(reg_name); - free(reg_name); if (reg_off < 0) return reg_off; arg->reg_off = reg_off; @@ -1474,12 +1471,11 @@ static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec arg->arg_type = USDT_ARG_CONST; arg->val_off = off; arg->reg_off = 0; - } else if (sscanf(arg_str, " %d @ %m[a-z0-9] %n", &arg_sz, ®_name, &len) == 2) { + } else if (sscanf(arg_str, " %d @ %15[a-z0-9] %n", &arg_sz, reg_name, &len) == 2) { /* Register read case, e.g., -8@a1 */ arg->arg_type = USDT_ARG_REG; arg->val_off = 0; reg_off = calc_pt_regs_off(reg_name); - free(reg_name); if (reg_off < 0) return reg_off; arg->reg_off = reg_off;