Skip to content

Commit 5eefe17

Browse files
anakryikoborkmann
authored andcommitted
libbpf: Clean up ringbuf size adjustment implementation
Drop unused iteration variable, move overflow prevention check into the for loop. Fixes: 0087a68 ("libbpf: Automatically fix up BPF_MAP_TYPE_RINGBUF size, if necessary") Reported-by: Nathan Chancellor <nathan@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20220510185159.754299-1-andrii@kernel.org
1 parent 93dafa9 commit 5eefe17

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4951,7 +4951,7 @@ static bool is_pow_of_2(size_t x)
49514951
static size_t adjust_ringbuf_sz(size_t sz)
49524952
{
49534953
__u32 page_sz = sysconf(_SC_PAGE_SIZE);
4954-
__u32 i, mul;
4954+
__u32 mul;
49554955

49564956
/* if user forgot to set any size, make sure they see error */
49574957
if (sz == 0)
@@ -4967,9 +4967,7 @@ static size_t adjust_ringbuf_sz(size_t sz)
49674967
* user-set size to satisfy both user size request and kernel
49684968
* requirements and substitute correct max_entries for map creation.
49694969
*/
4970-
for (i = 0, mul = 1; ; i++, mul <<= 1) {
4971-
if (mul > UINT_MAX / page_sz) /* prevent __u32 overflow */
4972-
break;
4970+
for (mul = 1; mul <= UINT_MAX / page_sz; mul <<= 1) {
49734971
if (mul * page_sz > sz)
49744972
return mul * page_sz;
49754973
}

0 commit comments

Comments
 (0)