Skip to content

Commit 41332d6

Browse files
ytcoodeborkmann
authored andcommitted
libbpf: Add a check to ensure that page_cnt is non-zero
The page_cnt parameter is used to specify the number of memory pages allocated for each per-CPU buffer, it must be non-zero and a power of 2. Currently, the __perf_buffer__new() function attempts to validate that the page_cnt is a power of 2 but forgets checking for the case where page_cnt is zero, we can fix it by replacing 'page_cnt & (page_cnt - 1)' with 'page_cnt == 0 || (page_cnt & (page_cnt - 1))'. If so, we also don't need to add a check in perf_buffer__new_v0_6_0() to make sure that page_cnt is non-zero and the check for zero in perf_buffer__new_raw_v0_6_0() can also be removed. The code will be cleaner and more readable. Signed-off-by: Yuntao Wang <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8bbe98b commit 41332d6

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10951,7 +10951,7 @@ struct perf_buffer *perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt,
1095110951
{
1095210952
struct perf_buffer_params p = {};
1095310953

10954-
if (page_cnt == 0 || !attr)
10954+
if (!attr)
1095510955
return libbpf_err_ptr(-EINVAL);
1095610956

1095710957
if (!OPTS_VALID(opts, perf_buffer_raw_opts))
@@ -10992,7 +10992,7 @@ static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
1099210992
__u32 map_info_len;
1099310993
int err, i, j, n;
1099410994

10995-
if (page_cnt & (page_cnt - 1)) {
10995+
if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) {
1099610996
pr_warn("page count should be power of two, but is %zu\n",
1099710997
page_cnt);
1099810998
return ERR_PTR(-EINVAL);

0 commit comments

Comments
 (0)