* [PATCH bpf-next] libbpf: Add a check to ensure that page_cnt is non-zero
@ 2022-03-01 16:57 Yuntao Wang
2022-03-02 23:32 ` Andrii Nakryiko
0 siblings, 1 reply; 4+ messages in thread
From: Yuntao Wang @ 2022-03-01 16:57 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
KP Singh, netdev, linux-kernel, Yuntao Wang
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 '!is_power_of_2(page_cnt)'.
Thus 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 is cleaner and more readable.
Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
---
tools/lib/bpf/libbpf.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index be6480e260c4..4dd1d82cd5b9 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -33,6 +33,7 @@
#include <linux/filter.h>
#include <linux/list.h>
#include <linux/limits.h>
+#include <linux/log2.h>
#include <linux/perf_event.h>
#include <linux/ring_buffer.h>
#include <linux/version.h>
@@ -10951,7 +10952,7 @@ struct perf_buffer *perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt,
{
struct perf_buffer_params p = {};
- if (page_cnt == 0 || !attr)
+ if (!attr)
return libbpf_err_ptr(-EINVAL);
if (!OPTS_VALID(opts, perf_buffer_raw_opts))
@@ -10992,7 +10993,7 @@ static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
__u32 map_info_len;
int err, i, j, n;
- if (page_cnt & (page_cnt - 1)) {
+ if (!is_power_of_2(page_cnt)) {
pr_warn("page count should be power of two, but is %zu\n",
page_cnt);
return ERR_PTR(-EINVAL);
--
2.35.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH bpf-next] libbpf: Add a check to ensure that page_cnt is non-zero
2022-03-01 16:57 [PATCH bpf-next] libbpf: Add a check to ensure that page_cnt is non-zero Yuntao Wang
@ 2022-03-02 23:32 ` Andrii Nakryiko
2022-03-03 0:59 ` [PATCH bpf-next v2] " Yuntao Wang
0 siblings, 1 reply; 4+ messages in thread
From: Andrii Nakryiko @ 2022-03-02 23:32 UTC (permalink / raw)
To: Yuntao Wang
Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
KP Singh, Networking, open list
On Tue, Mar 1, 2022 at 8:57 AM Yuntao Wang <ytcoode@gmail.com> wrote:
>
> 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 '!is_power_of_2(page_cnt)'.
>
> Thus 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 is cleaner and more readable.
>
> Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
> ---
> tools/lib/bpf/libbpf.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index be6480e260c4..4dd1d82cd5b9 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -33,6 +33,7 @@
> #include <linux/filter.h>
> #include <linux/list.h>
> #include <linux/limits.h>
> +#include <linux/log2.h>
we don't have this header implemented in Github repo, so this will be
unnecessary painful
> #include <linux/perf_event.h>
> #include <linux/ring_buffer.h>
> #include <linux/version.h>
> @@ -10951,7 +10952,7 @@ struct perf_buffer *perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt,
> {
> struct perf_buffer_params p = {};
>
> - if (page_cnt == 0 || !attr)
> + if (!attr)
> return libbpf_err_ptr(-EINVAL);
>
> if (!OPTS_VALID(opts, perf_buffer_raw_opts))
> @@ -10992,7 +10993,7 @@ static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
> __u32 map_info_len;
> int err, i, j, n;
>
> - if (page_cnt & (page_cnt - 1)) {
> + if (!is_power_of_2(page_cnt)) {
so let's instead just use `page_cnt == 0 || (page_cnt & (page_cnt -
1))` here explicitly
> pr_warn("page count should be power of two, but is %zu\n",
> page_cnt);
> return ERR_PTR(-EINVAL);
> --
> 2.35.1
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf-next v2] libbpf: Add a check to ensure that page_cnt is non-zero
2022-03-02 23:32 ` Andrii Nakryiko
@ 2022-03-03 0:59 ` Yuntao Wang
2022-03-03 15:30 ` patchwork-bot+netdevbpf
0 siblings, 1 reply; 4+ messages in thread
From: Yuntao Wang @ 2022-03-03 0:59 UTC (permalink / raw)
To: andrii.nakryiko
Cc: andrii, ast, bpf, daniel, john.fastabend, kafai, kpsingh,
linux-kernel, netdev, songliubraving, yhs, ytcoode
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 <ytcoode@gmail.com>
---
v1 -> v2: remove dependency on linux/log2.h header
tools/lib/bpf/libbpf.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index be6480e260c4..81bf01d67671 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -10951,7 +10951,7 @@ struct perf_buffer *perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt,
{
struct perf_buffer_params p = {};
- if (page_cnt == 0 || !attr)
+ if (!attr)
return libbpf_err_ptr(-EINVAL);
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,
__u32 map_info_len;
int err, i, j, n;
- if (page_cnt & (page_cnt - 1)) {
+ if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) {
pr_warn("page count should be power of two, but is %zu\n",
page_cnt);
return ERR_PTR(-EINVAL);
--
2.35.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH bpf-next v2] libbpf: Add a check to ensure that page_cnt is non-zero
2022-03-03 0:59 ` [PATCH bpf-next v2] " Yuntao Wang
@ 2022-03-03 15:30 ` patchwork-bot+netdevbpf
0 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-03-03 15:30 UTC (permalink / raw)
To: Yuntao Wang
Cc: andrii.nakryiko, andrii, ast, bpf, daniel, john.fastabend, kafai,
kpsingh, linux-kernel, netdev, songliubraving, yhs
Hello:
This patch was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:
On Thu, 3 Mar 2022 08:59:21 +0800 you wrote:
> 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))'.
>
> [...]
Here is the summary with links:
- [bpf-next,v2] libbpf: Add a check to ensure that page_cnt is non-zero
https://git.kernel.org/bpf/bpf-next/c/41332d6e3a43
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:[~2022-03-03 15:30 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-01 16:57 [PATCH bpf-next] libbpf: Add a check to ensure that page_cnt is non-zero Yuntao Wang
2022-03-02 23:32 ` Andrii Nakryiko
2022-03-03 0:59 ` [PATCH bpf-next v2] " Yuntao Wang
2022-03-03 15:30 ` patchwork-bot+netdevbpf
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.