public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v2] xsk: Elide base_addr comparison in xp_unaligned_validate_desc
@ 2023-04-11 13:00 Kal Conley
  2023-04-12 12:28 ` Magnus Karlsson
  2023-04-13 13:10 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 4+ messages in thread
From: Kal Conley @ 2023-04-11 13:00 UTC (permalink / raw)
  To: Björn Töpel, Magnus Karlsson, Maciej Fijalkowski,
	Jonathan Lemon, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: Kal Conley, netdev, bpf, linux-kernel

Remove redundant (base_addr >= pool->addrs_cnt) comparison from the
conditional.

In particular, addr is computed as:

    addr = base_addr + offset

where base_addr and offset are stored as 48-bit and 16-bit unsigned
integers, respectively. The above sum cannot overflow u64 since
base_addr has a maximum value of 0x0000ffffffffffff and offset has a
maximum value of 0xffff (implying a maximum sum of 0x000100000000fffe).
Since overflow is impossible, it follows that addr >= base_addr.

Now if (base_addr >= pool->addrs_cnt), then clearly:

    addr >= base_addr
         >= pool->addrs_cnt

Thus, (base_addr >= pool->addrs_cnt) implies (addr >= pool->addrs_cnt).
Subsequently, the former comparison is unnecessary in the conditional
since for any boolean expressions A and B, (A || B) && (A -> B) is
equivalent to B.

Signed-off-by: Kal Conley <kal.conley@dectris.com>
---
 net/xdp/xsk_queue.h | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/net/xdp/xsk_queue.h b/net/xdp/xsk_queue.h
index 66c6f57c9c44..dea4f378327d 100644
--- a/net/xdp/xsk_queue.h
+++ b/net/xdp/xsk_queue.h
@@ -153,16 +153,12 @@ static inline bool xp_aligned_validate_desc(struct xsk_buff_pool *pool,
 static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
 					      struct xdp_desc *desc)
 {
-	u64 addr, base_addr;
-
-	base_addr = xp_unaligned_extract_addr(desc->addr);
-	addr = xp_unaligned_add_offset_to_addr(desc->addr);
+	u64 addr = xp_unaligned_add_offset_to_addr(desc->addr);
 
 	if (desc->len > pool->chunk_size)
 		return false;
 
-	if (base_addr >= pool->addrs_cnt || addr >= pool->addrs_cnt ||
-	    addr + desc->len > pool->addrs_cnt ||
+	if (addr >= pool->addrs_cnt || addr + desc->len > pool->addrs_cnt ||
 	    xp_desc_crosses_non_contig_pg(pool, addr, desc->len))
 		return false;
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next v2] xsk: Elide base_addr comparison in xp_unaligned_validate_desc
  2023-04-11 13:00 [PATCH bpf-next v2] xsk: Elide base_addr comparison in xp_unaligned_validate_desc Kal Conley
@ 2023-04-12 12:28 ` Magnus Karlsson
  2023-04-12 12:38   ` Kal Cutter Conley
  2023-04-13 13:10 ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 4+ messages in thread
From: Magnus Karlsson @ 2023-04-12 12:28 UTC (permalink / raw)
  To: Kal Conley
  Cc: Björn Töpel, Magnus Karlsson, Maciej Fijalkowski,
	Jonathan Lemon, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend, netdev, bpf, linux-kernel

On Tue, 11 Apr 2023 at 15:03, Kal Conley <kal.conley@dectris.com> wrote:
>
> Remove redundant (base_addr >= pool->addrs_cnt) comparison from the
> conditional.
>
> In particular, addr is computed as:
>
>     addr = base_addr + offset
>
> where base_addr and offset are stored as 48-bit and 16-bit unsigned
> integers, respectively. The above sum cannot overflow u64 since
> base_addr has a maximum value of 0x0000ffffffffffff and offset has a
> maximum value of 0xffff (implying a maximum sum of 0x000100000000fffe).
> Since overflow is impossible, it follows that addr >= base_addr.
>
> Now if (base_addr >= pool->addrs_cnt), then clearly:
>
>     addr >= base_addr
>          >= pool->addrs_cnt
>
> Thus, (base_addr >= pool->addrs_cnt) implies (addr >= pool->addrs_cnt).
> Subsequently, the former comparison is unnecessary in the conditional
> since for any boolean expressions A and B, (A || B) && (A -> B) is
> equivalent to B.

Thanks Kal! Just checking again that you ran the xsk selftests on your
change and that it passed? If so, here is my ack.

Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>

> Signed-off-by: Kal Conley <kal.conley@dectris.com>
> ---
>  net/xdp/xsk_queue.h | 8 ++------
>  1 file changed, 2 insertions(+), 6 deletions(-)
>
> diff --git a/net/xdp/xsk_queue.h b/net/xdp/xsk_queue.h
> index 66c6f57c9c44..dea4f378327d 100644
> --- a/net/xdp/xsk_queue.h
> +++ b/net/xdp/xsk_queue.h
> @@ -153,16 +153,12 @@ static inline bool xp_aligned_validate_desc(struct xsk_buff_pool *pool,
>  static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
>                                               struct xdp_desc *desc)
>  {
> -       u64 addr, base_addr;
> -
> -       base_addr = xp_unaligned_extract_addr(desc->addr);
> -       addr = xp_unaligned_add_offset_to_addr(desc->addr);
> +       u64 addr = xp_unaligned_add_offset_to_addr(desc->addr);
>
>         if (desc->len > pool->chunk_size)
>                 return false;
>
> -       if (base_addr >= pool->addrs_cnt || addr >= pool->addrs_cnt ||
> -           addr + desc->len > pool->addrs_cnt ||
> +       if (addr >= pool->addrs_cnt || addr + desc->len > pool->addrs_cnt ||
>             xp_desc_crosses_non_contig_pg(pool, addr, desc->len))
>                 return false;
>
> --
> 2.39.2
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next v2] xsk: Elide base_addr comparison in xp_unaligned_validate_desc
  2023-04-12 12:28 ` Magnus Karlsson
@ 2023-04-12 12:38   ` Kal Cutter Conley
  0 siblings, 0 replies; 4+ messages in thread
From: Kal Cutter Conley @ 2023-04-12 12:38 UTC (permalink / raw)
  To: Magnus Karlsson
  Cc: Björn Töpel, Magnus Karlsson, Maciej Fijalkowski,
	Jonathan Lemon, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend, netdev, bpf, linux-kernel

> Thanks Kal! Just checking again that you ran the xsk selftests on your
> change and that it passed? If so, here is my ack.
>
> Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>
>

yep, I ran the tests and they PASSED.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next v2] xsk: Elide base_addr comparison in xp_unaligned_validate_desc
  2023-04-11 13:00 [PATCH bpf-next v2] xsk: Elide base_addr comparison in xp_unaligned_validate_desc Kal Conley
  2023-04-12 12:28 ` Magnus Karlsson
@ 2023-04-13 13:10 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-04-13 13:10 UTC (permalink / raw)
  To: Kal Cutter Conley
  Cc: bjorn, magnus.karlsson, maciej.fijalkowski, jonathan.lemon, davem,
	edumazet, kuba, pabeni, ast, daniel, hawk, john.fastabend, netdev,
	bpf, linux-kernel

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Tue, 11 Apr 2023 15:00:25 +0200 you wrote:
> Remove redundant (base_addr >= pool->addrs_cnt) comparison from the
> conditional.
> 
> In particular, addr is computed as:
> 
>     addr = base_addr + offset
> 
> [...]

Here is the summary with links:
  - [bpf-next,v2] xsk: Elide base_addr comparison in xp_unaligned_validate_desc
    https://git.kernel.org/bpf/bpf-next/c/1ba83f505c53

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-04-13 13:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-11 13:00 [PATCH bpf-next v2] xsk: Elide base_addr comparison in xp_unaligned_validate_desc Kal Conley
2023-04-12 12:28 ` Magnus Karlsson
2023-04-12 12:38   ` Kal Cutter Conley
2023-04-13 13:10 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox