diff mbox series

[1/2] mm: vmalloc_bytes()

Message ID 20240227072515.3736069-1-kent.overstreet@linux.dev (mailing list archive)
State New
Headers show
Series [1/2] mm: vmalloc_bytes() | expand

Commit Message

Kent Overstreet Feb. 27, 2024, 7:25 a.m. UTC
Add a __ksize() equivalent for vmalloc.

This is being added so we can track amount of memory stranded waiting
for an RCU grace period.

Cc: linux-mm@kvack.org
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 include/linux/vmalloc.h |  1 +
 mm/vmalloc.c            | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)

Comments

Andrew Morton Feb. 27, 2024, 7:55 p.m. UTC | #1
On Tue, 27 Feb 2024 02:25:14 -0500 Kent Overstreet <kent.overstreet@linux.dev> wrote:

> Add a __ksize() equivalent for vmalloc.
> +++ b/mm/vmalloc.c
> @@ -2848,6 +2848,26 @@ void vfree(const void *addr)
>  }
>  EXPORT_SYMBOL(vfree);
>  
> +/**
> + * vmalloc_bytes - Return size of a vmalloc() allocation
> + * @addr:  Memory base address
> + *
> + * Returns the size of the allocation as passed to vmalloc() rounded up to
> + * PAGE_SIZE; does not include extra internal allocations.
> + */
> +size_t vmalloc_bytes(const void *addr)
> +{
> +	struct vm_struct *vm = find_vm_area(addr);
> +	if (unlikely(!vm)) {
> +		WARN(1, KERN_ERR "vmalloc_bytes() called on nonexistent vm area (%p)\n",
> +		     addr);
> +		return 0;

	if (WARN(!vm, "..."))

and I don't think the explicit KERN_ERR facility is needed?
diff mbox series

Patch

diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index c720be70c8dd..3a6e29de8818 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -160,6 +160,7 @@  extern void *vcalloc(size_t n, size_t size) __alloc_size(1, 2);
 
 extern void vfree(const void *addr);
 extern void vfree_atomic(const void *addr);
+extern size_t vmalloc_bytes(const void *addr);
 
 extern void *vmap(struct page **pages, unsigned int count,
 			unsigned long flags, pgprot_t prot);
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index d12a17fc0c17..3642fca84c34 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -2848,6 +2848,26 @@  void vfree(const void *addr)
 }
 EXPORT_SYMBOL(vfree);
 
+/**
+ * vmalloc_bytes - Return size of a vmalloc() allocation
+ * @addr:  Memory base address
+ *
+ * Returns the size of the allocation as passed to vmalloc() rounded up to
+ * PAGE_SIZE; does not include extra internal allocations.
+ */
+size_t vmalloc_bytes(const void *addr)
+{
+	struct vm_struct *vm = find_vm_area(addr);
+	if (unlikely(!vm)) {
+		WARN(1, KERN_ERR "vmalloc_bytes() called on nonexistent vm area (%p)\n",
+		     addr);
+		return 0;
+	}
+
+	return vm->nr_pages * PAGE_SIZE;
+}
+EXPORT_SYMBOL(vmalloc_bytes);
+
 /**
  * vunmap - release virtual mapping obtained by vmap()
  * @addr:   memory base address