On Mon, Oct 19, 2026 at 08:36 AM Leon Hwang <[email protected]> wrote:
> +     ret = bpf_log_attr_report_size(&attr_log, 0);
> +     if (ret)
> +             goto put_prog;

can we drop bpf_log_attr_report_size() ?
Nothing is logged when the attach succeeds, so there is no size to
report, and libbpf zeroes attr_common, so log_true_size reads 0 anyway.
Finalizing only when ret < 0 already keeps -EFAULT away from a live link.
Then no need for the 2nd write, the log.c refactoring and the put_prog
label.

Something like this (compile tested only):

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 849bc91..61d100e 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -5856,8 +5856,11 @@ err_put:
 }

 #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.path_fd
-static int link_create(union bpf_attr *attr, bpfptr_t uattr)
+static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct 
bpf_common_attr *attr_common,
+                      bpfptr_t uattr_common, u32 size_common)
 {
+       struct bpf_verifier_log *log;
+       struct bpf_log_attr attr_log;
        struct bpf_prog *prog;
        int ret;

@@ -5867,14 +5870,24 @@ static int link_create(union bpf_attr *attr, bpfptr_t 
uattr)
        if (attr->link_create.attach_type == BPF_STRUCT_OPS)
                return bpf_struct_ops_link_create(attr);

+       log = bpf_log_attr_create_vlog(&attr_log, attr_common, uattr_common, 
size_common);
+       if (IS_ERR(log))
+               return PTR_ERR(log);
+
        prog = bpf_prog_get(attr->link_create.prog_fd);
-       if (IS_ERR(prog))
-               return PTR_ERR(prog);
+       if (IS_ERR(prog)) {
+               bpf_log(log, "Invalid prog_fd.\n");
+               ret = PTR_ERR(prog);
+               prog = NULL;
+               goto out;
+       }

        ret = bpf_prog_attach_check_attach_type(prog,
                                                attr->link_create.attach_type);
-       if (ret)
+       if (ret) {
+               bpf_log(log, "Invalid attach_type.\n");
                goto out;
+       }

        switch (prog->type) {
        case BPF_PROG_TYPE_CGROUP_SKB:
@@ -5953,12 +5966,19 @@ static int link_create(union bpf_attr *attr, bpfptr_t 
uattr)
                        ret = bpf_uprobe_multi_link_attach(attr, prog);
                break;
        default:
+               bpf_log(log, "Invalid prog type.\n");
                ret = -EINVAL;
        }

 out:
-       if (ret < 0)
-               bpf_prog_put(prog);
+       if (ret < 0) {
+               int err = bpf_log_attr_finalize(&attr_log, log);
+
+               ret = err ? err : ret;
+               if (prog)
+                       bpf_prog_put(prog);
+       }
+       kfree(log);
        return ret;
 }

@@ -6530,7 +6550,7 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, 
unsigned int size,
                err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
                break;
        case BPF_LINK_CREATE:
-               err = link_create(&attr, uattr);
+               err = link_create(&attr, uattr, &attr_common, uattr_common, 
size_common);
                break;
        case BPF_LINK_UPDATE:
                err = link_update(&attr);

pw-bot: cr

Reply via email to