BPF List
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>,
	bpf@vger.kernel.org, linux-mm@kvack.org,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Lorenzo Stoakes <ljs@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	Federico Di Pierro <nierro92@gmail.com>
Subject: Re: [PATCHv2 bpf-next 1/8] bpf: Honor const on generic kfunc memory arguments
Date: Tue, 3 Nov 2026 22:36:03 +0100	[thread overview]
Message-ID: <aupTwyu_l85H_XJL@krava> (raw)
In-Reply-To: <DMLRV3E28SC8.3FJZXLBLZI4DD@gmail.com>

On Tue, Nov 03, 2026 at 06:03:21PM +0000, Alexei Starovoitov wrote:
> On Mon, Nov 02, 2026 at 11:59 PM Jiri Olsa <jolsa@kernel.org> wrote:
> > +	t = btf_type_skip_modifiers(btf, arg->type, NULL);
> > +	if (!btf_type_is_ptr(t))
> > +		return false;
> > +
> > +	t = btf_type_by_id(btf, t->type);
> > +	while (t) {
> > +		switch (BTF_INFO_KIND(t->info)) {
> > +		case BTF_KIND_CONST:
> > +			return true;
> > +		case BTF_KIND_VOLATILE:
> > +		case BTF_KIND_RESTRICT:
> > +		case BTF_KIND_TYPEDEF:
> > +		case BTF_KIND_TYPE_TAG:
> 
> My earlier feedback must have been lost ?

ah sry, at the time I saw you email I already had my version
and did not realize yours is different

> 
> This switch is a copy of btf_type_is_modifier().
> All three callers already know that the arg is a pointer.
> t is never NULL.
> One helper of half the size is enough.

ok, will change, thanks

jirka

> 
> 
> The patch from the v1 thread, rebased (compile tested only):
> 
> diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
> index 73578a4..54d4ce2 100644
> --- a/Documentation/bpf/kfuncs.rst
> +++ b/Documentation/bpf/kfuncs.rst
> @@ -137,6 +137,10 @@ argument as its size. By default, without __sz annotation, the size of the type
>  of the pointer is used. Without __sz annotation, a kfunc cannot accept a void
>  pointer.
> 
> +The verifier checks such memory for read and write access. When the argument
> +is a pointer to const, like ``const void *mem``, the verifier checks it for
> +read access only, and read-only memory (e.g. ``.rodata``) can be passed.
> +
>  2.3.2 __k Annotation
>  --------------------
> 
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 935b163..21dd273 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -13182,6 +13182,22 @@ static bool is_kfunc_arg_const_mem_size(const struct btf *btf,
>  	return btf_param_match_suffix(btf, arg, "__szk");
>  }
> 
> +/* A kfunc only reads through 'const T *arg'. The compiler enforces it. */
> +static enum bpf_type_flag kfunc_arg_mem_flag(const struct btf *btf,
> +					     const struct btf_param *arg)
> +{
> +	const struct btf_type *t = btf_type_skip_modifiers(btf, arg->type, NULL);
> +	u32 id, ref_id;
> +
> +	btf_type_skip_modifiers(btf, t->type, &ref_id);
> +	for (id = t->type; id != ref_id; id = t->type) {
> +		t = btf_type_by_id(btf, id);
> +		if (btf_kind(t) == BTF_KIND_CONST)
> +			return MEM_RDONLY;
> +	}
> +	return MEM_WRITE;
> +}
> +
>  static bool is_kfunc_arg_constant(const struct btf *btf, const struct btf_param *arg)
>  {
>  	return btf_param_match_suffix(btf, arg, "__k");
> @@ -13550,7 +13566,8 @@ static int resolve_func_arg_type(struct bpf_verifier_env *env,
>  			PTR_ERR(resolve_ret));
>  		return -EINVAL;
>  	}
> -	*arg_type = ARG_PTR_TO_MEM | MEM_FIXED_SIZE | MEM_WRITE |
> +	*arg_type = ARG_PTR_TO_MEM | MEM_FIXED_SIZE |
> +		    kfunc_arg_mem_flag(meta->btf, &args[arg]) |
>  		    (*arg_type & (PTR_MAYBE_NULL | MEM_UNINIT));
> 
>  	return 0;
> @@ -14102,7 +14119,7 @@ get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
>  				reg_arg_name(env, argno), btf_type_str(ref_t), ref_tname);
>  			return -EINVAL;
>  		}
> -		arg_type = ARG_PTR_TO_MEM | MEM_WRITE;
> +		arg_type = ARG_PTR_TO_MEM | kfunc_arg_mem_flag(meta->btf, &args[arg]);
>  	} else if (btf_type_is_struct(ref_t))
>  		/* A pointer to a struct without a size argument is classified as ARG_PTR_TO_BTF_ID */
>  		arg_type = ARG_PTR_TO_BTF_ID;
> @@ -14127,7 +14144,8 @@ get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
>  			return -EINVAL;
>  		}
>  		proto->arg_size[arg] = type_size;
> -		arg_type = ARG_PTR_TO_MEM | MEM_FIXED_SIZE | MEM_WRITE;
> +		arg_type = ARG_PTR_TO_MEM | MEM_FIXED_SIZE |
> +			   kfunc_arg_mem_flag(meta->btf, &args[arg]);
>  	}
> 
>  	if (is_kfunc_arg_uninit(meta->btf, &args[arg]))

  reply	other threads:[~2026-11-03 21:36 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03  7:59 [PATCHv2 bpf-next 0/8] bpf: Add user memory kfuncs Jiri Olsa
2026-11-03  7:59 ` [PATCHv2 bpf-next 1/8] bpf: Honor const on generic kfunc memory arguments Jiri Olsa
2026-11-03  8:52   ` bot+bpf-ci
2026-11-03 18:03   ` Alexei Starovoitov
2026-11-03 21:36     ` Jiri Olsa [this message]
2026-11-03  7:59 ` [PATCHv2 bpf-next 2/8] bpf: Add user memory kfuncs Jiri Olsa
2026-11-03  8:19   ` sashiko-bot
2026-11-03  8:52   ` bot+bpf-ci
2026-11-03 18:05   ` Alexei Starovoitov
2026-11-03 21:36     ` Jiri Olsa
2026-11-03  7:59 ` [PATCHv2 bpf-next 3/8] selftests/bpf: Add get_procmap_info helper Jiri Olsa
2026-11-03  7:59 ` [PATCHv2 bpf-next 4/8] selftests/bpf: Test const kfunc memory arguments Jiri Olsa
2026-11-03  8:10   ` sashiko-bot
2026-11-04 21:12     ` Jiri Olsa
2026-11-03  7:59 ` [PATCHv2 bpf-next 5/8] selftests/bpf: Test user memory mmap kfuncs Jiri Olsa
2026-11-03  8:05   ` sashiko-bot
2026-11-04 21:09     ` Jiri Olsa
2026-11-03  7:59 ` [PATCHv2 bpf-next 6/8] selftests/bpf: Test user memory page exec Jiri Olsa
2026-11-03  8:39   ` bot+bpf-ci
2026-11-04 21:12     ` Jiri Olsa
2026-11-03  7:59 ` [PATCHv2 bpf-next 7/8] selftests/bpf: Test bpf_copy_to_user kfunc Jiri Olsa
2026-11-03  7:59 ` [PATCHv2 bpf-next 8/8] selftests/bpf: Test user memory kfunc errors Jiri Olsa
2026-11-03  8:39   ` bot+bpf-ci
2026-11-04 21:38     ` Jiri Olsa
2026-11-03 18:08 ` [PATCHv2 bpf-next 0/8] bpf: Add user memory kfuncs Alexei Starovoitov
2026-11-03 21:36   ` Jiri Olsa

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=aupTwyu_l85H_XJL@krava \
    --to=olsajiri@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=john.fastabend@gmail.com \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=memxor@gmail.com \
    --cc=nierro92@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox