All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Amery Hung" <ameryhung@gmail.com>, <bpf@vger.kernel.org>
Cc: <andrii@kernel.org>, <daniel@iogearbox.net>, <eddyz87@gmail.com>,
	<memxor@gmail.com>, <kernel-team@meta.com>
Subject: Re: [PATCH bpf-next v1 14/22] bpf: Consolidate runtime argument type resolution
Date: Thu, 10 Sep 2026 14:52:01 -0700	[thread overview]
Message-ID: <DLBYWR47GK83.33M4TZ2YKQU5W@gmail.com> (raw)
In-Reply-To: <20260905220117.922028-15-ameryhung@gmail.com>

On Sat Sep 5, 2026 at 3:01 PM PDT, Amery Hung wrote:
> A function argument type can require call-site refinement before
> register admission. Helpers refine map-value arguments according to the
> recorded map type, while kfuncs reinterpret a pointer to a scalar-only
> BTF struct as fixed-size memory when the register does not carry a
> BTF ID.
>
> Add resolve_func_arg_type() as the common entry point. It delegates
> helper map-value refinement to the existing resolve_map_arg_type(),
> then handles the kfunc BTF-to-memory fallback. It updates the effective
> argument type and size consumed by the subsequent checks.
>
> Preserve PTR_MAYBE_NULL when changing the base type so later
> nullability validation observes the BTF contract. Keep OBJ_RELEASE
> arguments in their original pointer class. The latter is not a bug fix:
> existing kfunc checking already validates ownership before this
> resolver. It preserves release metadata for the later shared checking
> order, where ownership validation follows register admission.
>
> Moving the kfunc fallback before admission lets the resulting
> fixed-size memory argument enter the compatibility check directly.
> This is another step toward routing both call types through
> check_func_arg().
>
> Signed-off-by: Amery Hung <ameryhung@gmail.com>
> ---
>  kernel/bpf/verifier.c | 199 +++++++++++++++++++++++-------------------
>  1 file changed, 110 insertions(+), 89 deletions(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 2e068c095436..15ed47b90316 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -8151,8 +8151,8 @@ static bool is_kfunc_call(const struct bpf_call_arg_meta *meta, u32 btf_id)
>  }
>  
>  static int resolve_map_arg_type(struct bpf_verifier_env *env,
> -				 const struct bpf_call_arg_meta *meta,
> -				 enum bpf_arg_type *arg_type)
> +				const struct bpf_call_arg_meta *meta,
> +				enum bpf_arg_type *arg_type)
>  {
>  	if (!meta->map.ptr) {
>  		/* kernel subsystem misconfigured verifier */
> @@ -8180,6 +8180,11 @@ static int resolve_map_arg_type(struct bpf_verifier_env *env,
>  	return 0;
>  }
>  
> +static int resolve_func_arg_type(struct bpf_verifier_env *env,
> +				 struct bpf_reg_state *reg, u32 arg,
> +				 struct bpf_call_arg_meta *meta, int insn_idx,
> +				 enum bpf_arg_type *arg_type, u32 *arg_size);
> +
>  struct bpf_reg_types {
>  	const enum bpf_reg_type types[10];
>  	u32 *btf_id;
> @@ -8794,6 +8799,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
>  	int regno = reg_from_argno(argno);
>  	enum bpf_reg_type type = reg->type;
>  	const u32 *arg_btf_id = NULL;
> +	u32 arg_size = arg_type & MEM_FIXED_SIZE ? fn->arg_size[arg] : 0;
>  	u32 key_size;
>  	int err = 0;
>  
> @@ -8823,11 +8829,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
>  		return -EACCES;
>  	}
>  
> -	if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE) {
> -		err = resolve_map_arg_type(env, meta, &arg_type);
> -		if (err)
> -			return err;
> -	}
> +	err = resolve_func_arg_type(env, reg, arg, meta, insn_idx, &arg_type, &arg_size);
> +	if (err)
> +		return err;
>  
>  	if (bpf_register_is_null(reg) && type_may_be_null(arg_type))
>  		/* A NULL register has a SCALAR_VALUE type, so skip
> @@ -8965,12 +8969,12 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
>  		 * next is_mem_size argument below.
>  		 */
>  		if (arg_type & MEM_FIXED_SIZE) {
> -			err = check_mem_reg(env, reg, argno, fn->arg_size[arg],
> +			err = check_mem_reg(env, reg, argno, arg_size,
>  					    arg_type & MEM_WRITE ? BPF_WRITE : BPF_READ, meta, NULL);
>  			if (err)
>  				return err;
>  			if (arg_type & MEM_ALIGNED)
> -				err = check_ptr_alignment(env, reg, 0, fn->arg_size[arg], true);
> +				err = check_ptr_alignment(env, reg, 0, arg_size, true);
>  		}
>  		break;
>  	case ARG_MEM_SIZE:
> @@ -11814,6 +11818,65 @@ static bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,
>  	return btf_struct_is_composed_of(env, btf, t, BTF_MEMBER_SCALAR);
>  }
>  
> +static int resolve_func_arg_type(struct bpf_verifier_env *env,
> +				 struct bpf_reg_state *reg, u32 arg,
> +				 struct bpf_call_arg_meta *meta, int insn_idx,
> +				 enum bpf_arg_type *arg_type, u32 *arg_size)
> +{
> +	argno_t argno = argno_from_arg(arg + 1);
> +	const struct btf_param *args;
> +	const struct btf_type *ref_t, *resolve_ret;
> +	const struct btf *btf;
> +	const char *ref_tname;
> +	u32 ref_id;
> +
> +	if (base_type(*arg_type) == ARG_PTR_TO_MAP_VALUE)
> +		return resolve_map_arg_type(env, meta, arg_type);
> +
> +	if (base_type(*arg_type) == ARG_PTR_TO_BTF_ID) {

Overall, it all looks great to me.

Here can reduce indent by:
 if (base_type(*arg_type) != ARG_PTR_TO_BTF_ID) 
    return 0;

Pls address AI nits and respin, so we can land asap.


  reply	other threads:[~2026-09-10 21:52 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-05 22:00 [PATCH bpf-next v1 00/22] bpf: Unify helper and kfunc argument checks Amery Hung
2026-09-05 22:00 ` [PATCH bpf-next v1 01/22] bpf: Pass call metadata through shared " Amery Hung
2026-09-05 22:00 ` [PATCH bpf-next v1 02/22] bpf: Address check_func_arg() arguments by argno Amery Hung
2026-09-05 22:44   ` bot+bpf-ci
2026-09-09 17:37     ` Amery Hung
2026-09-05 22:00 ` [PATCH bpf-next v1 03/22] bpf: Only compare func_id against BPF_FUNC_* for helper calls Amery Hung
2026-09-05 22:00 ` [PATCH bpf-next v1 04/22] bpf: Only compare func_id against kfunc BTF IDs for kfunc calls Amery Hung
2026-09-05 22:44   ` bot+bpf-ci
2026-09-09 17:47     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 05/22] bpf: Rename ambiguous function argument types Amery Hung
2026-09-05 23:08   ` bot+bpf-ci
2026-09-09 17:54     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 06/22] bpf: Unify kfunc argument kinds with enum bpf_arg_type Amery Hung
2026-09-05 23:08   ` bot+bpf-ci
2026-09-09 18:04     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 07/22] bpf: Align helper and kfunc ARG_PTR_TO_PROG_AUX handling Amery Hung
2026-09-05 23:08   ` bot+bpf-ci
2026-09-09 18:23     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 08/22] bpf: Classify kfunc arguments the verifier ignores Amery Hung
2026-09-05 22:44   ` bot+bpf-ci
2026-09-09 18:27     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 09/22] bpf: Set OBJ_RELEASE when generating kfunc argument types Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 10/22] bpf: Set MEM_UNINIT and dynptr subtypes when generating kfunc arg types Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 11/22] bpf: Set MEM_RCU when generating kfunc argument types Amery Hung
2026-09-05 22:44   ` bot+bpf-ci
2026-09-09 18:41     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 12/22] bpf: Resolve BTF ID of ARG_PTR_TO_BTF_ID in kfunc bpf_func_proto Amery Hung
2026-09-05 23:08   ` bot+bpf-ci
2026-09-09 20:42     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 13/22] bpf: Resolve ARG_PTR_TO_MEM | MEM_FIXED_SIZE size " Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 14/22] bpf: Consolidate runtime argument type resolution Amery Hung
2026-09-10 21:52   ` Alexei Starovoitov [this message]
2026-09-11 21:01     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 15/22] bpf: Consolidate nullable argument validation Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 16/22] bpf: Drop redundant BTF pointer helper write rejection Amery Hung
2026-09-05 23:08   ` bot+bpf-ci
2026-09-09 20:48     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 17/22] bpf: Consolidate helper and kfunc PTR_TO_BTF_ID argument matching Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 18/22] bpf: Admit kfunc argument registers through check_reg_type() Amery Hung
2026-09-05 23:23   ` bot+bpf-ci
2026-09-10 16:18     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 19/22] selftests/bpf: Test kfunc packet memory direct writes Amery Hung
2026-09-05 22:44   ` bot+bpf-ci
2026-09-11 20:46     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 20/22] bpf: Consolidate function call pkt_access validation Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 21/22] bpf: Consolidate release argument validation Amery Hung
2026-09-05 23:08   ` bot+bpf-ci
2026-09-11 20:47     ` Amery Hung
2026-09-05 22:01 ` [PATCH bpf-next v1 22/22] bpf: Check helper and kfunc arguments in one path Amery Hung
2026-09-05 22:33   ` sashiko-bot
2026-09-11 20:59     ` Amery Hung
2026-09-10 21:53   ` Alexei Starovoitov
2026-09-11 20:55     ` Amery Hung
2026-09-12  3:20 ` [PATCH bpf-next v1 00/22] bpf: Unify helper and kfunc argument checks patchwork-bot+netdevbpf

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=DLBYWR47GK83.33M4TZ2YKQU5W@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=ameryhung@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kernel-team@meta.com \
    --cc=memxor@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 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.