public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Amery Hung <ameryhung@meta.com>,
	Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	"Jose E . Marchesi" <jose.marchesi@oracle.com>,
	Kernel Team <kernel-team@fb.com>,
	Martin KaFai Lau <martin.lau@kernel.org>
Subject: Re: [PATCH bpf-next v4 06/18] bpf: Use argument index instead of register index in kfunc verifier logs
Date: Thu, 16 Apr 2026 07:39:19 -0700	[thread overview]
Message-ID: <b144edf8-ad46-4382-8563-4fd90ac5b2c3@linux.dev> (raw)
In-Reply-To: <CAP17u3PdGDRfE09g7rDqTaMZG_Q_Py1zVb3xKaVQUccUMVN5zw@mail.gmail.com>



On 4/15/26 4:23 PM, Amery Hung wrote:
> On Sun, Apr 12, 2026 at 3:01 PM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
>> On Sat, Apr 11, 2026 at 9:59 PM Yonghong Song <yonghong.song@linux.dev> wrote:
>>> For kfunc argument checking, use the argument index (arg#0, arg#1, ...)
>>> instead of the register index (R1, R2, ...) in verifier log messages.
>>> This is a preparation for future stack-based arguments where kfuncs can
>>> accept more than 5 arguments. Stack arguments won't have a corresponding
>>> register, so using argument index is more appropriate.
>>>
>>> Since some functions like check_mem_access(), check_stack_read_var_off(),
>>> and check_stack_range_initialized() are shared between kfunc argument
>>> checking (check_kfunc_args) and other paths (check_func_arg, do_check_insn, ...),
>>> introduce a `reg_or_arg` encoding: a non-negative value represents a register
>>> index, while a negative value encodes an argument index as -(argno + 1).
>>> The helper reg_arg_name() decodes this to produce either "R%d" or
>>> "arg#%d" for log messages.
>>>
>>> For check_func_arg() callers, in certain cases, the register index is
>>> preserved so existing helper function logs remain unchanged (e.g., "R1", "R2").
>>>
>>> Update selftests to expect the new "arg#N" format in kfunc error
>>> messages.
>>>
>>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>>> ---
>>>   include/linux/bpf_verifier.h                  |   1 +
>>>   kernel/bpf/verifier.c                         | 466 +++++++++---------
>>>   .../selftests/bpf/prog_tests/cb_refs.c        |   2 +-
>>>   .../selftests/bpf/prog_tests/linked_list.c    |   4 +-
>>>   .../selftests/bpf/progs/cpumask_failure.c     |   4 +-
>>>   .../testing/selftests/bpf/progs/dynptr_fail.c |   6 +-
>>>   .../selftests/bpf/progs/iters_testmod.c       |   6 +-
>>>   .../bpf/progs/local_kptr_stash_fail.c         |   2 +-
>>>   .../selftests/bpf/progs/map_kptr_fail.c       |   4 +-
>>>   .../bpf/progs/mem_rdonly_untrusted.c          |   2 +-
>>>   .../bpf/progs/nested_trust_failure.c          |   2 +-
>>>   .../selftests/bpf/progs/res_spin_lock_fail.c  |   2 +-
>>>   .../testing/selftests/bpf/progs/stream_fail.c |   2 +-
>>>   .../selftests/bpf/progs/task_kfunc_failure.c  |   4 +-
>>>   .../bpf/progs/verifier_cgroup_storage.c       |   4 +-
>>>   .../selftests/bpf/progs/verifier_ctx.c        |   2 +-
>>>   .../bpf/progs/verifier_ref_tracking.c         |   2 +-
>>>   .../selftests/bpf/progs/verifier_sock.c       |   6 +-
>>>   .../selftests/bpf/progs/verifier_unpriv.c     |   4 +-
>>>   .../selftests/bpf/progs/verifier_vfs_reject.c |   8 +-
>>>   .../testing/selftests/bpf/progs/wq_failures.c |   4 +-
>>>   tools/testing/selftests/bpf/verifier/calls.c  |   6 +-
>>>   .../testing/selftests/bpf/verifier/map_kptr.c |  10 +-
>>>   23 files changed, 286 insertions(+), 267 deletions(-)
>>>
>>> diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
>>> index 05b9fe98b8f8..291f11ddd176 100644
>>> --- a/include/linux/bpf_verifier.h
>>> +++ b/include/linux/bpf_verifier.h
>>> @@ -910,6 +910,7 @@ struct bpf_verifier_env {
>>>           * e.g., in reg_type_str() to generate reg_type string
>>>           */
>>>          char tmp_str_buf[TMP_STR_BUF_LEN];
>>> +       char tmp_reg_arg_name_buf[16];
>>>          struct bpf_insn insn_buf[INSN_BUF_SIZE];
>>>          struct bpf_insn epilogue_buf[INSN_BUF_SIZE];
>>>          struct bpf_scc_callchain callchain_buf;
>>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>>> index 54296d818d35..01df990f841a 100644
>>> --- a/kernel/bpf/verifier.c
>>> +++ b/kernel/bpf/verifier.c
>>> @@ -2179,6 +2179,18 @@ static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
>>>          return &elem->st;
>>>   }
>>>
>>> +static const char *reg_arg_name(struct bpf_verifier_env *env, int reg_or_arg)
>>> +{
>>> +       char *buf = env->tmp_reg_arg_name_buf;
>>> +       int len = sizeof(env->tmp_reg_arg_name_buf);
>>> +
>>> +       if (reg_or_arg >= 0)
>>> +               snprintf(buf, len, "R%d", reg_or_arg);
>>> +       else
>>> +               snprintf(buf, len, "arg#%d", -(reg_or_arg + 1));
>>> +       return buf;
>>> +}
>> The patches 1-4 make sense, but 5, 6 are too hacky.
>>
>> -       { "incorrect_head_var_off1", "R1 doesn't have constant offset" },
>> +       { "incorrect_head_var_off1", "arg#0 doesn't have constant offset" },
>>
>> This just sucks.
>> It degrades output for no good reason.
>>
>> Instead of inband negative vs positive signalling rename all
>> 'regno' to 'argno' and always pass whatever argno you need 1,2,..5,6, etc
> +1 to avoid using a negative/positive range to signal a stack argument
> or not, and to eliminate passing regno/argno.

This is what I plan to do.

>
>> Pass ptr_reg and size_reg as bpf_reg_state the way patches 1-4 are doing.
>> If argno <= 5 keep 'R%d' output, so all selftest don't change.
>> For argno >= 6 print '*(R12-xx)' where xx is where that arg lives.
>> Printing arg# is too cryptic. Humans/agents need to do mental
>> gymnastics to understand what it means.
>> The output must be easy to consume by agents.
>>
>> I was also thinking whether we can get rid of this 'argno' too.
>> cur_regs - reg is that number for <= 5 and
>> some spilled_ptr - reg for >= 6.
>> Technically we can
>>
>> u32 argno = cur_regs - reg;
>> if (argno <= 5) use it
>> else
>>     argno = spilled_ptr - reg.
>>
>> Feels a bit hacky. Need to sleep on it.
> The else case is actually even less pretty:
>      argno = spilled_ptr - constainer_of(reg, struct
> bpf_stack_arg_state, spilled_ptr);
>
> How about dropping slot_type since it is always STACK_SPILL or
> STACK_MISC and that can be inferred anytime by just calling
> is_spillable_regtype():
> -struct bpf_stack_arg_state *stack_arg_slots;
> +bpf_reg_state *stackarg_regs;

This is what I planned to use
    bpf_reg_state *stack_args;
in bpf_func_state.
But looks like stackarg_regs should be a better name.

>
> Then:
> argno = reg - (reg < stackargs_regs) ? cur_regs :  stackarg_regs;

This is a little bit tricky. In bpf_verifier.h, I see

struct bpf_stack_state {
         struct bpf_reg_state spilled_ptr;   <=== for stack state
         u8 slot_type[BPF_REG_SIZE];
};

struct bpf_verifier_env {
	...
	struct bpf_reg_state fake_reg[1];
	...
	struct bpf_reg_state true_reg1, true_reg2, false_reg1, false_reg2;
}

In kernel/bpf/states.h, we have
	static struct bpf_reg_state unbound_reg;

Maybe others, I didn't check.
This will make things complicated.

I would prefer the previous approach to have a nonnegative number
to represent regno and argno (for stack arguments purpose).

>
>> pw-bot: cr


  reply	other threads:[~2026-04-16 14:39 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-12  4:58 [PATCH bpf-next v4 00/18] bpf: Support stack arguments for BPF functions and kfuncs Yonghong Song
2026-04-12  4:58 ` [PATCH bpf-next v4 01/18] bpf: Remove unused parameter from check_map_kptr_access() Yonghong Song
2026-04-12  4:58 ` [PATCH bpf-next v4 02/18] bpf: Change from "arg #%d" to "arg#%d" in verifier log Yonghong Song
2026-04-12  4:58 ` [PATCH bpf-next v4 03/18] bpf: Refactor to avoid redundant calculation of bpf_reg_state Yonghong Song
2026-04-12  5:31   ` bot+bpf-ci
2026-04-13 14:25     ` Yonghong Song
2026-04-12  4:58 ` [PATCH bpf-next v4 04/18] bpf: Refactor to handle memory and size together Yonghong Song
2026-04-12  5:31   ` bot+bpf-ci
2026-04-13 14:27     ` Yonghong Song
2026-04-12  4:58 ` [PATCH bpf-next v4 05/18] bpf: Change some regno type from u32 to int type Yonghong Song
2026-04-12  4:58 ` [PATCH bpf-next v4 06/18] bpf: Use argument index instead of register index in kfunc verifier logs Yonghong Song
2026-04-12  5:43   ` bot+bpf-ci
2026-04-13 14:37     ` Yonghong Song
2026-04-12 22:01   ` Alexei Starovoitov
2026-04-13 14:45     ` Yonghong Song
2026-04-15 23:23     ` Amery Hung
2026-04-16 14:39       ` Yonghong Song [this message]
2026-04-12  4:59 ` [PATCH bpf-next v4 07/18] bpf: Introduce bpf register BPF_REG_STACK_ARG_BASE Yonghong Song
2026-04-12  4:59 ` [PATCH bpf-next v4 08/18] bpf: Reuse MAX_BPF_FUNC_ARGS for maximum number of arguments Yonghong Song
2026-04-12  4:59 ` [PATCH bpf-next v4 09/18] bpf: Support stack arguments for bpf functions Yonghong Song
2026-04-12  5:43   ` bot+bpf-ci
2026-04-13 15:22     ` Yonghong Song
2026-04-12 22:23   ` Alexei Starovoitov
2026-04-13 16:33     ` Yonghong Song
2026-04-12  5:00 ` [PATCH bpf-next v4 10/18] bpf: Fix interaction between stack argument PTR_TO_STACK and dead slot poisoning Yonghong Song
2026-04-12  5:43   ` bot+bpf-ci
2026-04-13 16:36     ` Yonghong Song
2026-04-15 22:32   ` Amery Hung
2026-04-16 14:21     ` Yonghong Song
2026-04-12  5:00 ` [PATCH bpf-next v4 11/18] bpf: Reject stack arguments in non-JITed programs Yonghong Song
2026-04-12  5:00 ` [PATCH bpf-next v4 12/18] bpf: Reject stack arguments if tail call reachable Yonghong Song
2026-04-12  5:43   ` bot+bpf-ci
2026-04-13 16:37     ` Yonghong Song
2026-04-12  5:00 ` [PATCH bpf-next v4 13/18] bpf: Support stack arguments for kfunc calls Yonghong Song
2026-04-12  5:43   ` bot+bpf-ci
2026-04-13 16:43     ` Yonghong Song
2026-04-12  5:00 ` [PATCH bpf-next v4 14/18] bpf: Enable stack argument support for x86_64 Yonghong Song
2026-04-12  5:00 ` [PATCH bpf-next v4 15/18] bpf,x86: Implement JIT support for stack arguments Yonghong Song
2026-04-12  5:43   ` bot+bpf-ci
2026-04-13 16:49     ` Yonghong Song
2026-04-12 22:36   ` Alexei Starovoitov
2026-04-13 17:26     ` Yonghong Song
2026-04-13 19:59       ` Alexei Starovoitov
2026-04-13 20:32         ` Yonghong Song
2026-04-13 20:38           ` Alexei Starovoitov
2026-04-13 21:10             ` Yonghong Song
2026-04-14 16:45       ` Yonghong Song
2026-04-14 17:51         ` Alexei Starovoitov
2026-04-12  5:00 ` [PATCH bpf-next v4 16/18] selftests/bpf: Add tests for BPF function " Yonghong Song
2026-04-12  5:00 ` [PATCH bpf-next v4 17/18] selftests/bpf: Add negative test for greater-than-8-byte kfunc stack argument Yonghong Song
2026-04-12  5:00 ` [PATCH bpf-next v4 18/18] selftests/bpf: Add verifier tests for stack argument validation Yonghong Song

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=b144edf8-ad46-4382-8563-4fd90ac5b2c3@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=alexei.starovoitov@gmail.com \
    --cc=ameryhung@meta.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jose.marchesi@oracle.com \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox