* [PATCH bpf] bpf: enforce BPF ringbuf size to be the power of 2
@ 2020-06-29 22:17 Andrii Nakryiko
2020-06-30 3:08 ` Yonghong Song
2020-06-30 4:47 ` Alexei Starovoitov
0 siblings, 2 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2020-06-29 22:17 UTC (permalink / raw)
To: bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko
BPF ringbuf assumes the size to be a multiple of page size and the power of
2 value. The latter is important to avoid division while calculating position
inside the ring buffer and using (N-1) mask instead. This patch fixes omission
to enforce power-of-2 size rule.
Fixes: 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support for it")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
kernel/bpf/ringbuf.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
index 180414bb0d3e..dcc8e8b9df10 100644
--- a/kernel/bpf/ringbuf.c
+++ b/kernel/bpf/ringbuf.c
@@ -132,7 +132,7 @@ static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node)
{
struct bpf_ringbuf *rb;
- if (!data_sz || !PAGE_ALIGNED(data_sz))
+ if (!is_power_of_2(data_sz) || !PAGE_ALIGNED(data_sz))
return ERR_PTR(-EINVAL);
#ifdef CONFIG_64BIT
@@ -166,7 +166,8 @@ static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
return ERR_PTR(-EINVAL);
if (attr->key_size || attr->value_size ||
- attr->max_entries == 0 || !PAGE_ALIGNED(attr->max_entries))
+ !is_power_of_2(attr->max_entries) ||
+ !PAGE_ALIGNED(attr->max_entries))
return ERR_PTR(-EINVAL);
rb_map = kzalloc(sizeof(*rb_map), GFP_USER);
--
2.24.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH bpf] bpf: enforce BPF ringbuf size to be the power of 2
2020-06-29 22:17 [PATCH bpf] bpf: enforce BPF ringbuf size to be the power of 2 Andrii Nakryiko
@ 2020-06-30 3:08 ` Yonghong Song
2020-06-30 4:47 ` Alexei Starovoitov
1 sibling, 0 replies; 4+ messages in thread
From: Yonghong Song @ 2020-06-30 3:08 UTC (permalink / raw)
To: Andrii Nakryiko, bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team
On 6/29/20 3:17 PM, Andrii Nakryiko wrote:
> BPF ringbuf assumes the size to be a multiple of page size and the power of
> 2 value. The latter is important to avoid division while calculating position
> inside the ring buffer and using (N-1) mask instead. This patch fixes omission
> to enforce power-of-2 size rule.
>
> Fixes: 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support for it")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Yonghong Song <yhs@fb.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH bpf] bpf: enforce BPF ringbuf size to be the power of 2
2020-06-29 22:17 [PATCH bpf] bpf: enforce BPF ringbuf size to be the power of 2 Andrii Nakryiko
2020-06-30 3:08 ` Yonghong Song
@ 2020-06-30 4:47 ` Alexei Starovoitov
2020-06-30 5:11 ` Andrii Nakryiko
1 sibling, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2020-06-30 4:47 UTC (permalink / raw)
To: Andrii Nakryiko
Cc: bpf, Network Development, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Kernel Team
On Mon, Jun 29, 2020 at 3:19 PM Andrii Nakryiko <andriin@fb.com> wrote:
>
> BPF ringbuf assumes the size to be a multiple of page size and the power of
> 2 value. The latter is important to avoid division while calculating position
> inside the ring buffer and using (N-1) mask instead. This patch fixes omission
> to enforce power-of-2 size rule.
>
> Fixes: 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support for it")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> ---
> kernel/bpf/ringbuf.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
> index 180414bb0d3e..dcc8e8b9df10 100644
> --- a/kernel/bpf/ringbuf.c
> +++ b/kernel/bpf/ringbuf.c
> @@ -132,7 +132,7 @@ static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node)
> {
> struct bpf_ringbuf *rb;
>
> - if (!data_sz || !PAGE_ALIGNED(data_sz))
> + if (!is_power_of_2(data_sz) || !PAGE_ALIGNED(data_sz))
> return ERR_PTR(-EINVAL);
What's the point checking the same value in two different places?
The check below did that already.
> #ifdef CONFIG_64BIT
> @@ -166,7 +166,8 @@ static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
> return ERR_PTR(-EINVAL);
>
> if (attr->key_size || attr->value_size ||
> - attr->max_entries == 0 || !PAGE_ALIGNED(attr->max_entries))
> + !is_power_of_2(attr->max_entries) ||
> + !PAGE_ALIGNED(attr->max_entries))
> return ERR_PTR(-EINVAL);
>
> rb_map = kzalloc(sizeof(*rb_map), GFP_USER);
> --
> 2.24.1
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH bpf] bpf: enforce BPF ringbuf size to be the power of 2
2020-06-30 4:47 ` Alexei Starovoitov
@ 2020-06-30 5:11 ` Andrii Nakryiko
0 siblings, 0 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2020-06-30 5:11 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Andrii Nakryiko, bpf, Network Development, Alexei Starovoitov,
Daniel Borkmann, Kernel Team
On Mon, Jun 29, 2020 at 9:47 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Mon, Jun 29, 2020 at 3:19 PM Andrii Nakryiko <andriin@fb.com> wrote:
> >
> > BPF ringbuf assumes the size to be a multiple of page size and the power of
> > 2 value. The latter is important to avoid division while calculating position
> > inside the ring buffer and using (N-1) mask instead. This patch fixes omission
> > to enforce power-of-2 size rule.
> >
> > Fixes: 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support for it")
> > Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> > ---
> > kernel/bpf/ringbuf.c | 5 +++--
> > 1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
> > index 180414bb0d3e..dcc8e8b9df10 100644
> > --- a/kernel/bpf/ringbuf.c
> > +++ b/kernel/bpf/ringbuf.c
> > @@ -132,7 +132,7 @@ static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node)
> > {
> > struct bpf_ringbuf *rb;
> >
> > - if (!data_sz || !PAGE_ALIGNED(data_sz))
> > + if (!is_power_of_2(data_sz) || !PAGE_ALIGNED(data_sz))
> > return ERR_PTR(-EINVAL);
>
> What's the point checking the same value in two different places?
> The check below did that already.
I was initially treating bpf_ringbuf_alloc() as a sort of internal API
that some other code (outside of BPF map) might want to use. But I'll
drop for now, it can always be added later.
>
> > #ifdef CONFIG_64BIT
> > @@ -166,7 +166,8 @@ static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
> > return ERR_PTR(-EINVAL);
> >
> > if (attr->key_size || attr->value_size ||
> > - attr->max_entries == 0 || !PAGE_ALIGNED(attr->max_entries))
> > + !is_power_of_2(attr->max_entries) ||
> > + !PAGE_ALIGNED(attr->max_entries))
> > return ERR_PTR(-EINVAL);
> >
> > rb_map = kzalloc(sizeof(*rb_map), GFP_USER);
> > --
> > 2.24.1
> >
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-06-30 5:12 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-29 22:17 [PATCH bpf] bpf: enforce BPF ringbuf size to be the power of 2 Andrii Nakryiko
2020-06-30 3:08 ` Yonghong Song
2020-06-30 4:47 ` Alexei Starovoitov
2020-06-30 5:11 ` Andrii Nakryiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).