diff mbox series

[bpf-next,v3,07/18] bpf: ringbuf memory usage

Message ID 20230227152032.12359-8-laoar.shao@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: bpf memory usage | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/series_format fail Series longer than 15 patches (and no cover letter)
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 2 this patch: 2
netdev/cc_maintainers warning 2 maintainers not CCed: song@kernel.org martin.lau@linux.dev
netdev/build_clang success Errors and warnings before: 2 this patch: 2
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 2 this patch: 2
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 37 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-14 fail Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_progs_parallel on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-36 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for test_verifier on x86_64 with llvm-17

Commit Message

Yafang Shao Feb. 27, 2023, 3:20 p.m. UTC
A new helper ringbuf_map_mem_usage() is introduced to calculate ringbuf
memory usage.

The result as follows,
- before
15: ringbuf  name count_map  flags 0x0
        key 0B  value 0B  max_entries 65536  memlock 0B

- after
15: ringbuf  name count_map  flags 0x0
        key 0B  value 0B  max_entries 65536  memlock 78424B

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 kernel/bpf/ringbuf.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Andrii Nakryiko Feb. 27, 2023, 5:21 p.m. UTC | #1
On Mon, Feb 27, 2023 at 7:21 AM Yafang Shao <laoar.shao@gmail.com> wrote:
>
> A new helper ringbuf_map_mem_usage() is introduced to calculate ringbuf
> memory usage.
>
> The result as follows,
> - before
> 15: ringbuf  name count_map  flags 0x0
>         key 0B  value 0B  max_entries 65536  memlock 0B
>
> - after
> 15: ringbuf  name count_map  flags 0x0
>         key 0B  value 0B  max_entries 65536  memlock 78424B
>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> ---
>  kernel/bpf/ringbuf.c | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
>
> diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
> index 80f4b4d..2bbf6e2 100644
> --- a/kernel/bpf/ringbuf.c
> +++ b/kernel/bpf/ringbuf.c
> @@ -336,6 +336,23 @@ static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp,
>         return 0;
>  }
>
> +static u64 ringbuf_map_mem_usage(const struct bpf_map *map)
> +{
> +       struct bpf_ringbuf_map *rb_map;
> +       struct bpf_ringbuf *rb;
> +       int nr_data_pages;
> +       int nr_meta_pages;
> +       u64 usage = sizeof(struct bpf_ringbuf_map);
> +
> +       rb_map = container_of(map, struct bpf_ringbuf_map, map);
> +       rb = rb_map->rb;

nit: rb_map seems unnecessary, I'd just go straight to rb

rb = container_of(map, struct bpf_ringbuf_map, map)->rb;

> +       usage += (u64)rb->nr_pages << PAGE_SHIFT;
> +       nr_meta_pages = RINGBUF_PGOFF + RINGBUF_POS_PAGES;

it would be cleaner to extract this into a constant
RINGBUF_NR_META_PAGES and use it in ringbuf_map_mem_usage and
bpf_ringbuf_area_alloc to keep them in sync

But other than that, looks good:

Acked-by: Andrii Nakryiko <andrii@kernel.org>

> +       nr_data_pages = map->max_entries >> PAGE_SHIFT;
> +       usage += (nr_meta_pages + 2 * nr_data_pages) * sizeof(struct page *);
> +       return usage;
> +}
> +
>  BTF_ID_LIST_SINGLE(ringbuf_map_btf_ids, struct, bpf_ringbuf_map)
>  const struct bpf_map_ops ringbuf_map_ops = {
>         .map_meta_equal = bpf_map_meta_equal,
> @@ -347,6 +364,7 @@ static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp,
>         .map_update_elem = ringbuf_map_update_elem,
>         .map_delete_elem = ringbuf_map_delete_elem,
>         .map_get_next_key = ringbuf_map_get_next_key,
> +       .map_mem_usage = ringbuf_map_mem_usage,
>         .map_btf_id = &ringbuf_map_btf_ids[0],
>  };
>
> @@ -361,6 +379,7 @@ static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp,
>         .map_update_elem = ringbuf_map_update_elem,
>         .map_delete_elem = ringbuf_map_delete_elem,
>         .map_get_next_key = ringbuf_map_get_next_key,
> +       .map_mem_usage = ringbuf_map_mem_usage,
>         .map_btf_id = &user_ringbuf_map_btf_ids[0],
>  };
>
> --
> 1.8.3.1
>
Yafang Shao Feb. 28, 2023, 2:55 a.m. UTC | #2
On Tue, Feb 28, 2023 at 1:22 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Mon, Feb 27, 2023 at 7:21 AM Yafang Shao <laoar.shao@gmail.com> wrote:
> >
> > A new helper ringbuf_map_mem_usage() is introduced to calculate ringbuf
> > memory usage.
> >
> > The result as follows,
> > - before
> > 15: ringbuf  name count_map  flags 0x0
> >         key 0B  value 0B  max_entries 65536  memlock 0B
> >
> > - after
> > 15: ringbuf  name count_map  flags 0x0
> >         key 0B  value 0B  max_entries 65536  memlock 78424B
> >
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > ---
> >  kernel/bpf/ringbuf.c | 19 +++++++++++++++++++
> >  1 file changed, 19 insertions(+)
> >
> > diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
> > index 80f4b4d..2bbf6e2 100644
> > --- a/kernel/bpf/ringbuf.c
> > +++ b/kernel/bpf/ringbuf.c
> > @@ -336,6 +336,23 @@ static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp,
> >         return 0;
> >  }
> >
> > +static u64 ringbuf_map_mem_usage(const struct bpf_map *map)
> > +{
> > +       struct bpf_ringbuf_map *rb_map;
> > +       struct bpf_ringbuf *rb;
> > +       int nr_data_pages;
> > +       int nr_meta_pages;
> > +       u64 usage = sizeof(struct bpf_ringbuf_map);
> > +
> > +       rb_map = container_of(map, struct bpf_ringbuf_map, map);
> > +       rb = rb_map->rb;
>
> nit: rb_map seems unnecessary, I'd just go straight to rb
>
> rb = container_of(map, struct bpf_ringbuf_map, map)->rb;

Thanks for the suggestion. I will do it.

>
> > +       usage += (u64)rb->nr_pages << PAGE_SHIFT;
> > +       nr_meta_pages = RINGBUF_PGOFF + RINGBUF_POS_PAGES;
>
> it would be cleaner to extract this into a constant
> RINGBUF_NR_META_PAGES and use it in ringbuf_map_mem_usage and
> bpf_ringbuf_area_alloc to keep them in sync
>

Will do it.

> But other than that, looks good:
>
> Acked-by: Andrii Nakryiko <andrii@kernel.org>

Thanks for the review.
diff mbox series

Patch

diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
index 80f4b4d..2bbf6e2 100644
--- a/kernel/bpf/ringbuf.c
+++ b/kernel/bpf/ringbuf.c
@@ -336,6 +336,23 @@  static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp,
 	return 0;
 }
 
+static u64 ringbuf_map_mem_usage(const struct bpf_map *map)
+{
+	struct bpf_ringbuf_map *rb_map;
+	struct bpf_ringbuf *rb;
+	int nr_data_pages;
+	int nr_meta_pages;
+	u64 usage = sizeof(struct bpf_ringbuf_map);
+
+	rb_map = container_of(map, struct bpf_ringbuf_map, map);
+	rb = rb_map->rb;
+	usage += (u64)rb->nr_pages << PAGE_SHIFT;
+	nr_meta_pages = RINGBUF_PGOFF + RINGBUF_POS_PAGES;
+	nr_data_pages = map->max_entries >> PAGE_SHIFT;
+	usage += (nr_meta_pages + 2 * nr_data_pages) * sizeof(struct page *);
+	return usage;
+}
+
 BTF_ID_LIST_SINGLE(ringbuf_map_btf_ids, struct, bpf_ringbuf_map)
 const struct bpf_map_ops ringbuf_map_ops = {
 	.map_meta_equal = bpf_map_meta_equal,
@@ -347,6 +364,7 @@  static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp,
 	.map_update_elem = ringbuf_map_update_elem,
 	.map_delete_elem = ringbuf_map_delete_elem,
 	.map_get_next_key = ringbuf_map_get_next_key,
+	.map_mem_usage = ringbuf_map_mem_usage,
 	.map_btf_id = &ringbuf_map_btf_ids[0],
 };
 
@@ -361,6 +379,7 @@  static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp,
 	.map_update_elem = ringbuf_map_update_elem,
 	.map_delete_elem = ringbuf_map_delete_elem,
 	.map_get_next_key = ringbuf_map_get_next_key,
+	.map_mem_usage = ringbuf_map_mem_usage,
 	.map_btf_id = &user_ringbuf_map_btf_ids[0],
 };