public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Andrii Nakryiko <andrii@kernel.org>,
	bpf@vger.kernel.org, ast@kernel.org,  daniel@iogearbox.net,
	martin.lau@kernel.org
Cc: kernel-team@meta.com
Subject: Re: [PATCH v5 bpf-next 17/23] bpf: generalize reg_set_min_max() to handle two sets of two registers
Date: Tue, 31 Oct 2023 20:14:00 +0200	[thread overview]
Message-ID: <1ed9ec950b35d6e53f22e63a21b7d02a8907b4a5.camel@gmail.com> (raw)
In-Reply-To: <20231027181346.4019398-18-andrii@kernel.org>

On Fri, 2023-10-27 at 11:13 -0700, Andrii Nakryiko wrote:
> Change reg_set_min_max() to take FALSE/TRUE sets of two registers each,
> instead of assuming that we are always comparing to a constant. For now
> we still assume that right-hand side registers are constants (and make
> sure that's the case by swapping src/dst regs, if necessary), but
> subsequent patches will remove this limitation.
> 
> Taking two by two registers allows to further unify and simplify
> check_cond_jmp_op() logic. We utilize fake register for BPF_K
> conditional jump case, just like with is_branch_taken() part.
> 
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

> ---
>  kernel/bpf/verifier.c | 112 ++++++++++++++++++------------------------
>  1 file changed, 49 insertions(+), 63 deletions(-)
> 
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index dde04b17c3a3..522566699fbe 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -14387,26 +14387,43 @@ static int is_branch_taken(struct bpf_reg_state *reg1, struct bpf_reg_state *reg
>   * In JEQ/JNE cases we also adjust the var_off values.
>   */
>  static void reg_set_min_max(struct bpf_reg_state *true_reg1,
> +			    struct bpf_reg_state *true_reg2,
>  			    struct bpf_reg_state *false_reg1,
> -			    u64 val, u32 val32,
> +			    struct bpf_reg_state *false_reg2,
>  			    u8 opcode, bool is_jmp32)
>  {
> -	struct tnum false_32off = tnum_subreg(false_reg1->var_off);
> -	struct tnum false_64off = false_reg1->var_off;
> -	struct tnum true_32off = tnum_subreg(true_reg1->var_off);
> -	struct tnum true_64off = true_reg1->var_off;
> -	s64 sval = (s64)val;
> -	s32 sval32 = (s32)val32;
> -
> -	/* If the dst_reg is a pointer, we can't learn anything about its
> -	 * variable offset from the compare (unless src_reg were a pointer into
> -	 * the same object, but we don't bother with that.
> -	 * Since false_reg1 and true_reg1 have the same type by construction, we
> -	 * only need to check one of them for pointerness.
> +	struct tnum false_32off, false_64off;
> +	struct tnum true_32off, true_64off;
> +	u64 val;
> +	u32 val32;
> +	s64 sval;
> +	s32 sval32;
> +
> +	/* If either register is a pointer, we can't learn anything about its
> +	 * variable offset from the compare (unless they were a pointer into
> +	 * the same object, but we don't bother with that).
>  	 */
> -	if (__is_pointer_value(false, false_reg1))
> +	if (false_reg1->type != SCALAR_VALUE || false_reg2->type != SCALAR_VALUE)
> +		return;
> +
> +	/* we expect right-hand registers (src ones) to be constants, for now */
> +	if (!is_reg_const(false_reg2, is_jmp32)) {
> +		opcode = flip_opcode(opcode);
> +		swap(true_reg1, true_reg2);
> +		swap(false_reg1, false_reg2);
> +	}
> +	if (!is_reg_const(false_reg2, is_jmp32))
>  		return;
>  
> +	false_32off = tnum_subreg(false_reg1->var_off);
> +	false_64off = false_reg1->var_off;
> +	true_32off = tnum_subreg(true_reg1->var_off);
> +	true_64off = true_reg1->var_off;
> +	val = false_reg2->var_off.value;
> +	val32 = (u32)tnum_subreg(false_reg2->var_off).value;
> +	sval = (s64)val;
> +	sval32 = (s32)val32;
> +
>  	switch (opcode) {
>  	/* JEQ/JNE comparison doesn't change the register equivalence.
>  	 *
> @@ -14543,22 +14560,6 @@ static void reg_set_min_max(struct bpf_reg_state *true_reg1,
>  	}
>  }
>  
> -/* Same as above, but for the case that dst_reg holds a constant and src_reg is
> - * the variable reg.
> - */
> -static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
> -				struct bpf_reg_state *false_reg,
> -				u64 val, u32 val32,
> -				u8 opcode, bool is_jmp32)
> -{
> -	opcode = flip_opcode(opcode);
> -	/* This uses zero as "not present in table"; luckily the zero opcode,
> -	 * BPF_JA, can't get here.
> -	 */
> -	if (opcode)
> -		reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32);
> -}
> -
>  /* Regs are known to be equal, so intersect their min/max/var_off */
>  static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
>  				  struct bpf_reg_state *dst_reg)
> @@ -14891,45 +14892,30 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
>  	 * comparable.
>  	 */
>  	if (BPF_SRC(insn->code) == BPF_X) {
> -		struct bpf_reg_state *src_reg = &regs[insn->src_reg];
> +		reg_set_min_max(&other_branch_regs[insn->dst_reg],
> +				&other_branch_regs[insn->src_reg],
> +				dst_reg, src_reg, opcode, is_jmp32);
>  
>  		if (dst_reg->type == SCALAR_VALUE &&
> -		    src_reg->type == SCALAR_VALUE) {
> -			if (tnum_is_const(src_reg->var_off) ||
> -			    (is_jmp32 &&
> -			     tnum_is_const(tnum_subreg(src_reg->var_off))))
> -				reg_set_min_max(&other_branch_regs[insn->dst_reg],
> -						dst_reg,
> -						src_reg->var_off.value,
> -						tnum_subreg(src_reg->var_off).value,
> -						opcode, is_jmp32);
> -			else if (tnum_is_const(dst_reg->var_off) ||
> -				 (is_jmp32 &&
> -				  tnum_is_const(tnum_subreg(dst_reg->var_off))))
> -				reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
> -						    src_reg,
> -						    dst_reg->var_off.value,
> -						    tnum_subreg(dst_reg->var_off).value,
> -						    opcode, is_jmp32);
> -			else if (!is_jmp32 &&
> -				 (opcode == BPF_JEQ || opcode == BPF_JNE))
> -				/* Comparing for equality, we can combine knowledge */
> -				reg_combine_min_max(&other_branch_regs[insn->src_reg],
> -						    &other_branch_regs[insn->dst_reg],
> -						    src_reg, dst_reg, opcode);
> -			if (src_reg->id &&
> -			    !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) {
> -				find_equal_scalars(this_branch, src_reg);
> -				find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]);
> -			}
> -
> +		    src_reg->type == SCALAR_VALUE &&
> +		    !is_jmp32 && (opcode == BPF_JEQ || opcode == BPF_JNE)) {
> +			/* Comparing for equality, we can combine knowledge */
> +			reg_combine_min_max(&other_branch_regs[insn->src_reg],
> +					    &other_branch_regs[insn->dst_reg],
> +					    src_reg, dst_reg, opcode);
>  		}
>  	} else if (dst_reg->type == SCALAR_VALUE) {
> -		reg_set_min_max(&other_branch_regs[insn->dst_reg],
> -					dst_reg, insn->imm, (u32)insn->imm,
> -					opcode, is_jmp32);
> +		reg_set_min_max(&other_branch_regs[insn->dst_reg], src_reg, /* fake one */
> +				dst_reg, src_reg /* same fake one */,
> +				opcode, is_jmp32);
>  	}
>  
> +	if (BPF_SRC(insn->code) == BPF_X &&
> +	    src_reg->type == SCALAR_VALUE && src_reg->id &&
> +	    !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) {
> +		find_equal_scalars(this_branch, src_reg);
> +		find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]);
> +	}
>  	if (dst_reg->type == SCALAR_VALUE && dst_reg->id &&
>  	    !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) {
>  		find_equal_scalars(this_branch, dst_reg);


  parent reply	other threads:[~2023-10-31 18:14 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-27 18:13 [PATCH v5 bpf-next 00/23] BPF register bounds logic and testing improvements Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 01/23] selftests/bpf: fix RELEASE=1 build for tc_opts Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 02/23] selftests/bpf: satisfy compiler by having explicit return in btf test Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 03/23] bpf: derive smin/smax from umin/max bounds Andrii Nakryiko
2023-10-31 15:37   ` Eduard Zingerman
2023-10-31 17:30     ` Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 04/23] bpf: derive smin32/smax32 from umin32/umax32 bounds Andrii Nakryiko
2023-10-31 15:37   ` Eduard Zingerman
2023-10-27 18:13 ` [PATCH v5 bpf-next 05/23] bpf: derive subreg bounds from full bounds when upper 32 bits are constant Andrii Nakryiko
2023-10-31 15:37   ` Eduard Zingerman
2023-10-27 18:13 ` [PATCH v5 bpf-next 06/23] bpf: add special smin32/smax32 derivation from 64-bit bounds Andrii Nakryiko
2023-10-31 15:37   ` Eduard Zingerman
2023-10-31 17:39     ` Andrii Nakryiko
2023-10-31 18:41       ` Alexei Starovoitov
2023-10-31 18:49         ` Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 07/23] bpf: improve deduction of 64-bit bounds from 32-bit bounds Andrii Nakryiko
2023-10-31 15:37   ` Eduard Zingerman
2023-10-31 20:26   ` Alexei Starovoitov
2023-10-31 20:33     ` Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 08/23] bpf: try harder to deduce register bounds from different numeric domains Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 09/23] bpf: drop knowledge-losing __reg_combine_{32,64}_into_{64,32} logic Andrii Nakryiko
2023-10-31 15:38   ` Eduard Zingerman
2023-10-27 18:13 ` [PATCH v5 bpf-next 10/23] selftests/bpf: BPF register range bounds tester Andrii Nakryiko
2023-11-08 22:08   ` Eduard Zingerman
2023-11-08 23:23     ` Andrii Nakryiko
2023-11-09  0:30       ` Eduard Zingerman
2023-10-27 18:13 ` [PATCH v5 bpf-next 11/23] bpf: rename is_branch_taken reg arguments to prepare for the second one Andrii Nakryiko
2023-10-30 19:39   ` Alexei Starovoitov
2023-10-31  5:19     ` Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 12/23] bpf: generalize is_branch_taken() to work with two registers Andrii Nakryiko
2023-10-31 15:38   ` Eduard Zingerman
2023-10-31 17:41     ` Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 13/23] bpf: move is_branch_taken() down Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 14/23] bpf: generalize is_branch_taken to handle all conditional jumps in one place Andrii Nakryiko
2023-10-31 15:38   ` Eduard Zingerman
2023-10-27 18:13 ` [PATCH v5 bpf-next 15/23] bpf: unify 32-bit and 64-bit is_branch_taken logic Andrii Nakryiko
2023-10-30 19:52   ` Alexei Starovoitov
2023-10-31  5:28     ` Andrii Nakryiko
2023-10-31 17:35   ` Eduard Zingerman
2023-10-27 18:13 ` [PATCH v5 bpf-next 16/23] bpf: prepare reg_set_min_max for second set of registers Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 17/23] bpf: generalize reg_set_min_max() to handle two sets of two registers Andrii Nakryiko
2023-10-31  2:02   ` Alexei Starovoitov
2023-10-31  6:03     ` Andrii Nakryiko
2023-10-31 16:23       ` Alexei Starovoitov
2023-10-31 17:50         ` Andrii Nakryiko
2023-10-31 17:56           ` Andrii Nakryiko
2023-10-31 18:04             ` Alexei Starovoitov
2023-10-31 18:06               ` Andrii Nakryiko
2023-10-31 18:14   ` Eduard Zingerman [this message]
2023-10-27 18:13 ` [PATCH v5 bpf-next 18/23] bpf: generalize reg_set_min_max() to handle non-const register comparisons Andrii Nakryiko
2023-10-31 23:25   ` Eduard Zingerman
2023-11-01 16:35     ` Andrii Nakryiko
2023-11-01 17:12       ` Eduard Zingerman
2023-10-27 18:13 ` [PATCH v5 bpf-next 19/23] bpf: generalize is_scalar_branch_taken() logic Andrii Nakryiko
2023-10-31  2:12   ` Alexei Starovoitov
2023-10-31  6:12     ` Andrii Nakryiko
2023-10-31 16:34       ` Alexei Starovoitov
2023-10-31 18:01         ` Andrii Nakryiko
2023-10-31 20:53           ` Andrii Nakryiko
2023-10-31 20:55             ` Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 20/23] bpf: enhance BPF_JEQ/BPF_JNE is_branch_taken logic Andrii Nakryiko
2023-10-31  2:20   ` Alexei Starovoitov
2023-10-31  6:16     ` Andrii Nakryiko
2023-10-31 16:36       ` Alexei Starovoitov
2023-10-31 18:04         ` Andrii Nakryiko
2023-10-31 18:06           ` Alexei Starovoitov
2023-10-27 18:13 ` [PATCH v5 bpf-next 21/23] selftests/bpf: adjust OP_EQ/OP_NE handling to use subranges for branch taken Andrii Nakryiko
2023-11-08 18:22   ` Eduard Zingerman
2023-11-08 19:59     ` Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 22/23] selftests/bpf: add range x range test to reg_bounds Andrii Nakryiko
2023-10-27 18:13 ` [PATCH v5 bpf-next 23/23] selftests/bpf: add iter test requiring range x range logic Andrii Nakryiko
2023-10-30 17:55 ` [PATCH v5 bpf-next 00/23] BPF register bounds logic and testing improvements Alexei Starovoitov
2023-10-31  5:19   ` Andrii Nakryiko
2023-11-01 12:37     ` Paul Chaignon
2023-11-01 17:13       ` Andrii Nakryiko
2023-11-07  6:37         ` Harishankar Vishwanathan
2023-11-07 16:38           ` Paul Chaignon

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=1ed9ec950b35d6e53f22e63a21b7d02a8907b4a5.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@meta.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