diff mbox series

[bpf-next,3/7] mm: vmalloc: introduce vsize()

Message ID 20230202014158.19616-4-laoar.shao@gmail.com (mailing list archive)
State New
Headers show
Series bpf, mm: bpf memory usage | expand

Commit Message

Yafang Shao Feb. 2, 2023, 1:41 a.m. UTC
Introduce a helper to report full size of underlying allocation of a
vmalloc'ed address.

Suggested-by: Uladzislau Rezki <urezki@gmail.com>
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Cc: Uladzislau Rezki <urezki@gmail.com>
Cc: Christoph Hellwig <hch@infradead.org>
---
 include/linux/vmalloc.h |  1 +
 mm/vmalloc.c            | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)

Comments

Christoph Hellwig Feb. 2, 2023, 10:23 a.m. UTC | #1
On Thu, Feb 02, 2023 at 01:41:54AM +0000, Yafang Shao wrote:
> Introduce a helper to report full size of underlying allocation of a
> vmalloc'ed address.

What is the use case for it?
Yafang Shao Feb. 2, 2023, 2:10 p.m. UTC | #2
On Thu, Feb 2, 2023 at 6:23 PM Christoph Hellwig <hch@infradead.org> wrote:
>
> On Thu, Feb 02, 2023 at 01:41:54AM +0000, Yafang Shao wrote:
> > Introduce a helper to report full size of underlying allocation of a
> > vmalloc'ed address.
>
> What is the use case for it?

The use case is in patch #4 and patch #7, to get the bpf memory usage
from the pointers.
#4: https://lore.kernel.org/bpf/20230202014158.19616-5-laoar.shao@gmail.com/T/#u
#7: https://lore.kernel.org/bpf/20230202014158.19616-8-laoar.shao@gmail.com/T/#u

I forgot to Cc you the full patchset. Sorry about that.
The full patchset is at
https://lore.kernel.org/bpf/20230202014158.19616-1-laoar.shao@gmail.com/
diff mbox series

Patch

diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index 096d48a..7fbd390 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -297,4 +297,5 @@  struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
 static inline bool vmalloc_dump_obj(void *object) { return false; }
 #endif
 
+size_t vsize(void *addr);
 #endif /* _LINUX_VMALLOC_H */
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index ca71de7..8499eba 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -4057,6 +4057,23 @@  bool vmalloc_dump_obj(void *object)
 }
 #endif
 
+/* Report full size of underlying allocation of a vmalloc'ed addr */
+size_t vsize(void *addr)
+{
+	struct vmap_area *va;
+	size_t va_size = 0;
+
+	if (!addr)
+		return 0;
+
+	spin_lock(&vmap_area_lock);
+	va = __find_vmap_area((unsigned long)addr, &vmap_area_root);
+	if (va && va->vm)
+		va_size = va->vm->size;
+	spin_unlock(&vmap_area_lock);
+	return va_size;
+}
+
 #ifdef CONFIG_PROC_FS
 static void *s_start(struct seq_file *m, loff_t *pos)
 	__acquires(&vmap_purge_lock)