All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hui Zhu" <hui.zhu@linux.dev>
Cc: bpf@vger.kernel.org, hannes@cmpxchg.org, tj@kernel.org, mkoutny@suse.com
Subject: Re: [RFC PATCH bpf-next v7 04/11] libbpf: introduce bpf_map__attach_struct_ops_opts()
Date: Tue, 26 May 2026 02:58:24 +0000	[thread overview]
Message-ID: <20260526025824.ACBDA1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20bdaa33cc19364f5f10208c79ef94fe43bd5ac1.1779760876.git.zhuhui@kylinos.cn>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The `expected_revision` field in `struct bpf_struct_ops_opts` is silently ignored.
- [High] Silent fallback to global struct_ops attachment when `target_fd` is provided but the map lacks `BPF_F_LINK`.
--

commit 38a993fa39aa42f01d7ad014d6fc85b69daffad3
Author: Roman Gushchin <roman.gushchin@linux.dev>

libbpf: introduce bpf_map__attach_struct_ops_opts()

This commit introduces an extended version of bpf_map__attach_struct_ops()
which takes an additional struct bpf_struct_ops_opts argument. This enables
passing a target_fd and the BPF_F_CGROUP_FD flag for cgroup attachment.

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 1e8688975d16d..a1b54da1ded25 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
[ ... ]
> @@ -13724,7 +13731,9 @@ struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
>  		return &link->link;
>  	}
>  

[Severity: High]
If a user provides a cgroup FD via opts->target_fd to restrict the attachment
to a specific cgroup, but the map was not created with BPF_F_LINK, does this
silently fall back to global registration?

Looking slightly above this block:

	if (!(map->def.map_flags & BPF_F_LINK)) {
		/* w/o a real link */
		link->link.fd = map->fd;
		link->map_fd = -1;
		return &link->link;
	}

If BPF_F_LINK is missing, it returns early before extracting and applying
opts->target_fd. Should we explicitly reject the operation if opts->target_fd
or opts->flags are set but the map lacks BPF_F_LINK, to prevent subverting
the user's intent to isolate the struct_ops?

> -	fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL);
> +	link_opts.flags = OPTS_GET(opts, flags, 0);
> +	target_fd = OPTS_GET(opts, target_fd, 0);

[Severity: High]
Is the expected_revision field intended to be extracted here?

It is defined in struct bpf_struct_ops_opts but never extracted via
OPTS_GET() or checked. If users attempt to use expected_revision to prevent
TOCTOU races during atomic replacements, will they experience silent
failures because the field is completely ignored?

Should the library either remove this field from the struct, or explicitly
check for OPTS_GET(opts, expected_revision, 0) and return an error?

> +	fd = bpf_link_create(map->fd, target_fd, BPF_STRUCT_OPS, &link_opts);
>  	if (fd < 0) {
>  		free(link);
>  		return libbpf_err_ptr(fd);
[ ... ]
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index bba4e84643967..18af178547ad5 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
[ ... ]
> @@ -945,6 +945,20 @@ bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,
>  struct bpf_map;
>  
>  LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
> +
> +struct bpf_struct_ops_opts {
> +	/* size of this struct, for forward/backward compatibility */
> +	size_t sz;
> +	__u32 flags;
> +	__u32 target_fd;
> +	__u64 expected_revision;
> +	size_t :0;
> +};

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1779760876.git.zhuhui@kylinos.cn?part=4

  reply	other threads:[~2026-05-26  2:58 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-26  2:20 [RFC PATCH bpf-next v7 00/11] mm: BPF struct_ops for dynamic memory protection and async reclaim Hui Zhu
2026-05-26  2:20 ` [RFC PATCH bpf-next v7 01/11] bpf: move bpf_struct_ops_link into bpf.h Hui Zhu
2026-05-26  2:20 ` [RFC PATCH bpf-next v7 02/11] bpf: allow attaching struct_ops to cgroups Hui Zhu
2026-05-26  3:00   ` sashiko-bot
2026-05-26  3:19   ` bot+bpf-ci
2026-05-26  3:19     ` bot+bpf-ci
2026-05-26  2:20 ` [RFC PATCH bpf-next v7 03/11] libbpf: fix return value on memory allocation failure Hui Zhu
2026-05-26  2:41   ` sashiko-bot
2026-05-26  3:06   ` bot+bpf-ci
2026-05-26  3:06     ` bot+bpf-ci
2026-05-26  2:20 ` [RFC PATCH bpf-next v7 04/11] libbpf: introduce bpf_map__attach_struct_ops_opts() Hui Zhu
2026-05-26  2:58   ` sashiko-bot [this message]
2026-05-26  3:06   ` bot+bpf-ci
2026-05-26  3:06     ` bot+bpf-ci
2026-05-27 15:43   ` Yonghong Song
2026-05-28  5:53     ` Leon Hwang
2026-05-26  2:20 ` [RFC PATCH bpf-next v7 05/11] bpf: Pass flags in bpf_link_create for struct_ops Hui Zhu
2026-05-26  3:04   ` sashiko-bot
2026-05-26  2:24 ` [RFC PATCH bpf-next v7 06/11] mm: memcontrol: Add BPF struct_ops for memory controller Hui Zhu
2026-05-26  3:04   ` sashiko-bot
2026-05-26  3:19   ` bot+bpf-ci
2026-05-26  3:19     ` bot+bpf-ci
2026-05-26  2:24 ` [RFC PATCH bpf-next v7 07/11] mm/bpf: Add bpf_try_to_free_mem_cgroup_pages kfunc Hui Zhu
2026-05-26  2:52   ` sashiko-bot
2026-05-26  3:06   ` bot+bpf-ci
2026-05-26  3:06     ` bot+bpf-ci
2026-05-26  2:24 ` [RFC PATCH bpf-next v7 08/11] selftests/bpf: Add tests for memcg_bpf_ops Hui Zhu
2026-05-26  2:50   ` sashiko-bot
2026-05-26  2:27 ` [RFC PATCH bpf-next v7 09/11] selftests/bpf: Add test for memcg_bpf_ops hierarchies Hui Zhu
2026-05-26  2:52   ` sashiko-bot
2026-05-26  2:27 ` [RFC PATCH bpf-next v7 10/11] selftests/bpf: Add selftest for memcg async reclaim via BPF Hui Zhu
2026-05-26  3:06   ` sashiko-bot
2026-05-26  3:06   ` bot+bpf-ci
2026-05-26  3:06     ` bot+bpf-ci
2026-05-26  2:27 ` [RFC PATCH bpf-next v7 11/11] samples/bpf: Add memcg priority control and async reclaim example Hui Zhu
2026-05-26  3:10   ` sashiko-bot
2026-05-26 13:41 ` [RFC PATCH bpf-next v7 00/11] mm: BPF struct_ops for dynamic memory protection and async reclaim Usama Arif
2026-05-27  9:31   ` teawater
2026-05-27  8:47 ` Michal Hocko
2026-05-28  8:27   ` teawater

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=20260526025824.ACBDA1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=hui.zhu@linux.dev \
    --cc=mkoutny@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.