public inbox for bpf@vger.kernel.org
 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, houtao1@huawei.com
Cc: bpf@vger.kernel.org, Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH bpf-next v4 01/18] bpf: add new map ops ->map_mem_usage
Date: Sun,  5 Mar 2023 12:45:58 +0000	[thread overview]
Message-ID: <20230305124615.12358-2-laoar.shao@gmail.com> (raw)
In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com>

Add a new map ops ->map_mem_usage to print the memory usage of a
bpf map.

This is a preparation for the followup change.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 include/linux/bpf.h  |  2 ++
 kernel/bpf/syscall.c | 15 +++++++--------
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index d345680..9059520 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -161,6 +161,8 @@ struct bpf_map_ops {
 				     bpf_callback_t callback_fn,
 				     void *callback_ctx, u64 flags);
 
+	u64 (*map_mem_usage)(const struct bpf_map *map);
+
 	/* BTF id of struct allocated by map_alloc */
 	int *map_btf_id;
 
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index eb50025..073957c 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -771,16 +771,15 @@ static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
 }
 
 #ifdef CONFIG_PROC_FS
-/* Provides an approximation of the map's memory footprint.
- * Used only to provide a backward compatibility and display
- * a reasonable "memlock" info.
- */
-static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
+/* Show the memory usage of a bpf map */
+static u64 bpf_map_memory_usage(const struct bpf_map *map)
 {
 	unsigned long size;
 
-	size = round_up(map->key_size + bpf_map_value_size(map), 8);
+	if (map->ops->map_mem_usage)
+		return map->ops->map_mem_usage(map);
 
+	size = round_up(map->key_size + bpf_map_value_size(map), 8);
 	return round_up(map->max_entries * size, PAGE_SIZE);
 }
 
@@ -803,7 +802,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
 		   "max_entries:\t%u\n"
 		   "map_flags:\t%#x\n"
 		   "map_extra:\t%#llx\n"
-		   "memlock:\t%lu\n"
+		   "memlock:\t%llu\n"
 		   "map_id:\t%u\n"
 		   "frozen:\t%u\n",
 		   map->map_type,
@@ -812,7 +811,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
 		   map->max_entries,
 		   map->map_flags,
 		   (unsigned long long)map->map_extra,
-		   bpf_map_memory_footprint(map),
+		   bpf_map_memory_usage(map),
 		   map->id,
 		   READ_ONCE(map->frozen));
 	if (type) {
-- 
1.8.3.1


  reply	other threads:[~2023-03-05 12:46 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-05 12:45 [PATCH bpf-next v4 00/18] bpf: bpf memory usage Yafang Shao
2023-03-05 12:45 ` Yafang Shao [this message]
2023-03-05 12:45 ` [PATCH bpf-next v4 02/18] bpf: lpm_trie " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 03/18] bpf: hashtab " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 04/18] bpf: arraymap " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 05/18] bpf: stackmap " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 06/18] bpf: reuseport_array " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 07/18] bpf: ringbuf " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 08/18] bpf: bloom_filter " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 09/18] bpf: cpumap " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 10/18] bpf: devmap " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 11/18] bpf: queue_stack_maps " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 12/18] bpf: bpf_struct_ops " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 13/18] bpf: local_storage " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 14/18] bpf, net: bpf_local_storage " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 15/18] bpf, net: sock_map " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 16/18] bpf, net: xskmap " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 17/18] bpf: offload map " Yafang Shao
2023-03-05 12:46 ` [PATCH bpf-next v4 18/18] bpf: enforce all maps having memory usage callback Yafang Shao
2023-03-07 17:40 ` [PATCH bpf-next v4 00/18] bpf: bpf memory usage patchwork-bot+netdevbpf

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=20230305124615.12358-2-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=houtao1@huawei.com \
    --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