From: Martin KaFai Lau <martin.lau@linux•dev>
To: Amery Hung <ameryhung@gmail•com>
Cc: netdev@vger•kernel.org, alexei.starovoitov@gmail•com,
andrii@kernel•org, daniel@iogearbox•net, tj@kernel•org,
martin.lau@kernel•org, bpf@vger•kernel.org, kernel-team@meta•com
Subject: Re: [PATCH v2 bpf-next 2/4] bpf: Support associating BPF program with struct_ops
Date: Thu, 16 Oct 2025 16:51:46 -0700 [thread overview]
Message-ID: <f5bf014d-46d7-44da-8a63-1982cd45d9ba@linux.dev> (raw)
In-Reply-To: <20251016204503.3203690-3-ameryhung@gmail.com>
On 10/16/25 1:45 PM, Amery Hung wrote:
> diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
> index a41e6730edcf..e060d9823e4a 100644
> --- a/kernel/bpf/bpf_struct_ops.c
> +++ b/kernel/bpf/bpf_struct_ops.c
> @@ -528,6 +528,7 @@ static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)
> for (i = 0; i < st_map->funcs_cnt; i++) {
> if (!st_map->links[i])
> break;
> + bpf_prog_disassoc_struct_ops(st_map->links[i]->prog);
It took me some time to understand why it needs to specifically call
bpf_prog_disassoc_struct_ops here for struct_ops programs. bpf_prog_put
has not been done yet. The BPF_PTR_POISON could be set back to NULL. My
understanding is the BPF_PTR_POISON should stay with the prog's lifetime?
> bpf_link_put(st_map->links[i]);
> st_map->links[i] = NULL;
> }
> @@ -801,6 +802,9 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
> goto reset_unlock;
> }
>
> + /* If the program is reused, prog->aux->st_ops_assoc will be poisoned */
> + bpf_prog_assoc_struct_ops(prog, &st_map->map);
> +
> link = kzalloc(sizeof(*link), GFP_USER);
> if (!link) {
> bpf_prog_put(prog);
> @@ -1394,6 +1398,46 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
> return err;
> }
>
> +int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map)
> +{
> + struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
> + void *kdata = &st_map->kvalue.data;
> + int ret = 0;
> +
> + mutex_lock(&prog->aux->st_ops_assoc_mutex);
> +
> + if (prog->aux->st_ops_assoc && prog->aux->st_ops_assoc != kdata) {
> + if (prog->type == BPF_PROG_TYPE_STRUCT_OPS)
> + WRITE_ONCE(prog->aux->st_ops_assoc, BPF_PTR_POISON);
> +
> + ret = -EBUSY;
> + goto out;
> + }
> +
> + WRITE_ONCE(prog->aux->st_ops_assoc, kdata);
> +out:
> + mutex_unlock(&prog->aux->st_ops_assoc_mutex);
> + return ret;
> +}
> +
> +void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog)
> +{
> + mutex_lock(&prog->aux->st_ops_assoc_mutex);
Can it check the prog type here and decide if bpf_struct_ops_put needs
to be called?
> + WRITE_ONCE(prog->aux->st_ops_assoc, NULL);
next prev parent reply other threads:[~2025-10-16 23:52 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-16 20:44 [PATCH v2 bpf-next 0/4] Support associating BPF programs with struct_ops Amery Hung
2025-10-16 20:45 ` [PATCH v2 bpf-next 1/4] bpf: Allow verifier to fixup kernel module kfuncs Amery Hung
2025-10-16 20:45 ` [PATCH v2 bpf-next 2/4] bpf: Support associating BPF program with struct_ops Amery Hung
2025-10-16 23:51 ` Martin KaFai Lau [this message]
2025-10-16 23:58 ` Amery Hung
2025-10-17 0:19 ` Martin KaFai Lau
2025-10-17 16:38 ` Amery Hung
2025-10-17 16:49 ` Amery Hung
2025-10-17 14:18 ` kernel test robot
2025-10-17 16:03 ` kernel test robot
2025-10-17 17:05 ` kernel test robot
2025-10-16 20:45 ` [PATCH v2 bpf-next 3/4] libbpf: Add bpf_prog_assoc_struct_ops() API Amery Hung
2025-10-16 20:45 ` [PATCH v2 bpf-next 4/4] selftests/bpf: Test BPF_PROG_ASSOC_STRUCT_OPS command Amery Hung
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=f5bf014d-46d7-44da-8a63-1982cd45d9ba@linux.dev \
--to=martin.lau@linux$(echo .)dev \
--cc=alexei.starovoitov@gmail$(echo .)com \
--cc=ameryhung@gmail$(echo .)com \
--cc=andrii@kernel$(echo .)org \
--cc=bpf@vger$(echo .)kernel.org \
--cc=daniel@iogearbox$(echo .)net \
--cc=kernel-team@meta$(echo .)com \
--cc=martin.lau@kernel$(echo .)org \
--cc=netdev@vger$(echo .)kernel.org \
--cc=tj@kernel$(echo .)org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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