BPF List
 help / color / mirror / Atom feed
From: Yafang Shao <laoar.shao@gmail.com>
To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	kafai@fb.com, songliubraving@fb.com, yhs@fb.com,
	john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com,
	haoluo@google.com, jolsa@kernel.org, horenc@vt.edu,
	xiyou.wangcong@gmail.com
Cc: bpf@vger.kernel.org, Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH bpf-next v2 07/18] bpf: ringbuf memory usage
Date: Wed, 22 Feb 2023 01:45:42 +0000	[thread overview]
Message-ID: <20230222014553.47744-8-laoar.shao@gmail.com> (raw)
In-Reply-To: <20230222014553.47744-1-laoar.shao@gmail.com>

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;
+	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],
 };
 
-- 
1.8.3.1


  parent reply	other threads:[~2023-02-22  1:46 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-22  1:45 [PATCH bpf-next v2 00/18] bpf: bpf memory usage Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 01/18] bpf: add new map ops ->map_mem_usage Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 02/18] bpf: lpm_trie memory usage Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 03/18] bpf: hashtab " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 04/18] bpf: arraymap " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 05/18] bpf: stackmap " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 06/18] bpf: reuseport_array " Yafang Shao
2023-02-22  1:45 ` Yafang Shao [this message]
2023-02-22  1:45 ` [PATCH bpf-next v2 08/18] bpf: bloom_filter " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 09/18] bpf: cpumap " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 10/18] bpf: devmap " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 11/18] bpf: queue_stack_maps " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 12/18] bpf: bpf_struct_ops " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 13/18] bpf: local_storage " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 14/18] bpf, net: bpf_local_storage " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 15/18] bpf, net: sock_map " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 16/18] bpf, net: xskmap " Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 17/18] bpf: offload map " Yafang Shao
2023-02-22 11:01   ` kernel test robot
2023-02-23 14:17     ` Yafang Shao
2023-02-22  1:45 ` [PATCH bpf-next v2 18/18] bpf: enforce all maps having memory usage callback Yafang Shao
2023-02-22 19:20   ` Alexei Starovoitov
2023-02-23 14:15     ` Yafang Shao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230222014553.47744-8-laoar.shao@gmail.com \
    --to=laoar.shao@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=horenc@vt.edu \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=sdf@google.com \
    --cc=songliubraving@fb.com \
    --cc=xiyou.wangcong@gmail.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox