xsk: Elide base_addr comparison in xp_unaligned_validate_desc
Subsystems (4)
PATCH v2 1/1 xsk: Elide base_addr comparison in xp_unaligned_validate_desc
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.
--- net/xdp/xsk_queue.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
net/xdp/xsk_queue.h +2 -6
@@ -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;
Signature · 1 line
2.39.2
Quoted 16 lines · Kal Conley
Remove redundant (
base_addr>=pool->addrs_cnt) comparison from the conditional.In particular, addr is computed as:
addr =
base_addr+ offsetwhere
base_addrand offset are stored as 48-bit and 16-bit unsigned integers, respectively. The above sum cannot overflow u64 sincebase_addrhas a maximum value of0x0000ffffffffffffand offset has a maximum value of0xffff(implying a maximum sum of0x000100000000fffe). Since overflow is impossible, it follows that addr >=base_addr.Now if (
base_addr>=pool->addrs_cnt), then clearly:
Quoted 7 lines
addr >=
base_addr
>=pool->addrs_cntThus, (
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.
Quoted 31 lines
Signed-off-by Kal Conley <[email protected]>--- net/xdp/xsk_queue.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)net/xdp/xsk_queue.h +2
-7@@ -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
Quoted 5 lines
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 <[email protected]>
yep, I ran the tests and they PASSED.
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Quoted 8 lines · 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!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html