Skip to contentSkip to conversation

mm: vmalloc: introduce array allocation functions

PATCH In review Paolo Bonzini 5 participants 13 messages Mar 8, 2022 – Mar 10, 2022

Subsystems (11)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

cover mm: vmalloc: introduce array allocation functions

The first patch in this series introduces four array allocation functions to replace vmalloc(array_size()) and vzalloc(array_size()), of which Linux has dozens of occurrences. The Functions take care of the multiplication and overflow check, result in simpler code and make it easier for developers to avoid overflow bugs.

The other two patches start to apply the functions in the mm/ and KVM areas. In the case of KVM, it is also important to switch from kvcalloc to __vcalloc; the allocation size is driven by userspace and can be larger than 4GiB, which has been forbidden by the kv*alloc functions since 5.15.

Paolo

Paolo Bonzini (3):
mm: vmalloc: introduce array allocation functions
mm: use vmalloc_array and vcalloc for array allocations
KVM: use __vcalloc for very large allocations

 arch/powerpc/kvm/book3s_hv_uvmem.c |  2 +-
 arch/x86/kvm/mmu/page_track.c      |  7 +++--
 arch/x86/kvm/x86.c                 |  4 +--
 include/linux/vmalloc.h            |  5 +++
 mm/percpu-stats.c                  |  2 +-
 mm/swap_cgroup.c                   |  4 +--
 mm/util.c                          | 50 ++++++++++++++++++++++++++++++
 virt/kvm/kvm_main.c                |  4 +--
 9 files changed, 66 insertions(+), 12 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/3 mm: vmalloc: introduce array allocation functions

Linux has dozens of occurrences of vmalloc(array_size()) and vzalloc(array_size()). Allow to simplify the code by providing vmalloc_array and vcalloc, as well as the underscored variants that let the caller specify the GFP flags.

---
 include/linux/vmalloc.h |  5 +++++
 mm/util.c               | 50 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
include/linux/vmalloc.h +5 -0
index 880227b9f044..d1bbd4fd50c5 100644
@@ -159,6 +159,11 @@ void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
 		int node, const void *caller) __alloc_size(1);
 void *vmalloc_no_huge(unsigned long size) __alloc_size(1);
 
+extern void *__vmalloc_array(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
+extern void *vmalloc_array(size_t n, size_t size) __alloc_size(1, 2);
+extern void *__vcalloc(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
+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);
 
mm/util.c +50 -0

56 lines not on this page · raw message

Signature · 1 line
2.31.1
reviewer Acked-by on v1 1/3
#2 lore raw
Quoted 8 lines · Paolo Bonzini

Linux has dozens of occurrences of vmalloc(array_size()) and vzalloc(array_size()). Allow to simplify the code by providing vmalloc_array and vcalloc, as well as the underscored variants that let the caller specify the GFP flags.

Seems useful

Is there any reason you haven't used __alloc_size(1, 2) annotation?

Thanks!

Quoted 84 lines
---
 include/linux/vmalloc.h |  5 +++++
 mm/util.c               | 50 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
include/linux/vmalloc.h +5 -0
index 880227b9f044..d1bbd4fd50c5 100644
@@ -159,6 +159,11 @@ void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
 		int node, const void *caller) __alloc_size(1);
 void *vmalloc_no_huge(unsigned long size) __alloc_size(1);
 
+extern void *__vmalloc_array(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
+extern void *vmalloc_array(size_t n, size_t size) __alloc_size(1, 2);
+extern void *__vcalloc(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
+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);
 
mm/util.c +50 -0
index 7e43369064c8..94475abe54a0 100644
@@ -647,6 +647,56 @@ void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
 }
 EXPORT_SYMBOL(kvrealloc);
 
+/**
+ * __vmalloc_array - allocate memory for a virtually contiguous array.
+ * @n: number of elements.
+ * @size: element size.
+ * @flags: the type of memory to allocate (see kmalloc).
+ */
+void *__vmalloc_array(size_t n, size_t size, gfp_t flags)
+{
+	size_t bytes;
+
+	if (unlikely(check_mul_overflow(n, size, &bytes)))
+		return NULL;
+	return __vmalloc(bytes, flags);
+}
+EXPORT_SYMBOL(__vmalloc_array);
+
+/**
+ * vmalloc_array - allocate memory for a virtually contiguous array.
+ * @n: number of elements.
+ * @size: element size.
+ */
+void *vmalloc_array(size_t n, size_t size)
+{
+	return __vmalloc_array(n, size, GFP_KERNEL);
+}
+EXPORT_SYMBOL(vmalloc_array);
+
+/**
+ * __vcalloc - allocate and zero memory for a virtually contiguous array.
+ * @n: number of elements.
+ * @size: element size.
+ * @flags: the type of memory to allocate (see kmalloc).
+ */
+void *__vcalloc(size_t n, size_t size, gfp_t flags)
+{
+	return __vmalloc_array(n, size, flags | __GFP_ZERO);
+}
+EXPORT_SYMBOL(__vcalloc);
+
+/**
+ * vcalloc - allocate and zero memory for a virtually contiguous array.
+ * @n: number of elements.
+ * @size: element size.
+ */
+void *vcalloc(size_t n, size_t size)
+{
+	return __vmalloc_array(n, size, GFP_KERNEL | __GFP_ZERO);
+}
+EXPORT_SYMBOL(vcalloc);
+
 /* Neutral page->mapping pointer to address_space or anon_vma or other */
 void *page_rmapping(struct page *page)
 {
Signature · 1 line
2.31.1
Signature · 2 lines
Michal Hocko
SUSE Labs
author maintainer
#3 lore raw
Quoted 4 lines · Michal Hocko

Seems useful

Is there any reason you haven't used __alloc_size(1, 2) annotation?

It's enough to have them in the header:

Quoted 4 lines

include/linux/vmalloc.h void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, · line 165

Quoted 4 lines
include/linux/vmalloc.h +4 -0
+extern void *__vmalloc_array(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
+extern void *vmalloc_array(size_t n, size_t size) __alloc_size(1, 2);
+extern void *__vcalloc(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
+extern void *vcalloc(size_t n, size_t size) __alloc_size(1, 2);

Thanks for the quick review!

Paolo

reviewer
#4 lore raw
Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · Michal Hocko

Seems useful

Quoted 8 lines
Quoted 1 line

Is there any reason you haven't used __alloc_size(1, 2) annotation?

It's enough to have them in the header:

Quoted 4 lines

include/linux/vmalloc.h void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, · line 165

Quoted 4 lines
include/linux/vmalloc.h +4 -0
+extern void *__vmalloc_array(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
+extern void *vmalloc_array(size_t n, size_t size) __alloc_size(1, 2);
+extern void *__vcalloc(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
+extern void *vcalloc(size_t n, size_t size) __alloc_size(1, 2);

My bad, I have expected __alloc_size before the function name and simply haven't noticed it at the end.

Signature · 2 lines
Michal Hocko
SUSE Labs
Reviewed-by on v1 1/3
#5 lore raw
In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

Linux has dozens of occurrences of vmalloc(array_size()) and vzalloc(array_size()). Allow to simplify the code by providing vmalloc_array and vcalloc, as well as the underscored variants that let the caller specify the GFP flags.

Signature · 3 lines
Thanks,

David / dhildenb
author maintainer
#6 lore raw

PATCH 2/3 mm: use vmalloc_array and vcalloc for array allocations

In reply to Paolo Bonzini

Instead of using array_size or just a multiply, use a function that takes care of both the multiplication and the overflow checks.

---
 mm/percpu-stats.c | 2 +-
 mm/swap_cgroup.c  | 4 +---
 2 files changed, 2 insertions(+), 4 deletions(-)
mm/percpu-stats.c +1 -1
index c6bd092ff7a3..e71651cda2de 100644
@@ -144,7 +144,7 @@ static int percpu_stats_show(struct seq_file *m, void *v)
 	spin_unlock_irq(&pcpu_lock);
 
 	/* there can be at most this many free and allocated fragments */
-	buffer = vmalloc(array_size(sizeof(int), (2 * max_nr_alloc + 1)));
+	buffer = vmalloc_array(2 * max_nr_alloc + 1, sizeof(int));
 	if (!buffer)
 		return -ENOMEM;
 
mm/swap_cgroup.c +1 -3
index 7f34343c075a..5a9442979a18 100644
@@ -167,14 +167,12 @@ unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
 int swap_cgroup_swapon(int type, unsigned long max_pages)
 {
 	void *array;
-	unsigned long array_size;
 	unsigned long length;
 	struct swap_cgroup_ctrl *ctrl;
 
 	length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
-	array_size = length * sizeof(void *);
 
-	array = vzalloc(array_size);
+	array = vcalloc(length, sizeof(void *));
 	if (!array)
 		goto nomem;
 
Signature · 1 line
2.31.1
reviewer Acked-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] mm: use vmalloc_array and vcalloc for array allocations

Quoted 5 lines · Paolo Bonzini

Instead of using array_size or just a multiply, use a function that takes care of both the multiplication and the overflow checks.

The resulting code is easier to read indeed.

Quoted 42 lines
---
 mm/percpu-stats.c | 2 +-
 mm/swap_cgroup.c  | 4 +---
 2 files changed, 2 insertions(+), 4 deletions(-)
mm/percpu-stats.c +1 -1
index c6bd092ff7a3..e71651cda2de 100644
@@ -144,7 +144,7 @@ static int percpu_stats_show(struct seq_file *m, void *v)
 	spin_unlock_irq(&pcpu_lock);
 
 	/* there can be at most this many free and allocated fragments */
-	buffer = vmalloc(array_size(sizeof(int), (2 * max_nr_alloc + 1)));
+	buffer = vmalloc_array(2 * max_nr_alloc + 1, sizeof(int));
 	if (!buffer)
 		return -ENOMEM;
 
mm/swap_cgroup.c +1 -3
index 7f34343c075a..5a9442979a18 100644
@@ -167,14 +167,12 @@ unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
 int swap_cgroup_swapon(int type, unsigned long max_pages)
 {
 	void *array;
-	unsigned long array_size;
 	unsigned long length;
 	struct swap_cgroup_ctrl *ctrl;
 
 	length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
-	array_size = length * sizeof(void *);
 
-	array = vzalloc(array_size);
+	array = vcalloc(length, sizeof(void *));
 	if (!array)
 		goto nomem;
 
Signature · 1 line
2.31.1
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Acked-by on v1 2/3
#9 lore raw

Re: [PATCH 2/3] mm: use vmalloc_array and vcalloc for array allocations

In reply to Paolo Bonzini

Hello,

Quoted 46 lines · Paolo Bonzini

Instead of using array_size or just a multiply, use a function that takes care of both the multiplication and the overflow checks.

---
 mm/percpu-stats.c | 2 +-
 mm/swap_cgroup.c  | 4 +---
 2 files changed, 2 insertions(+), 4 deletions(-)
mm/percpu-stats.c +1 -1
index c6bd092ff7a3..e71651cda2de 100644
@@ -144,7 +144,7 @@ static int percpu_stats_show(struct seq_file *m, void *v)
 	spin_unlock_irq(&pcpu_lock);
 
 	/* there can be at most this many free and allocated fragments */
-	buffer = vmalloc(array_size(sizeof(int), (2 * max_nr_alloc + 1)));
+	buffer = vmalloc_array(2 * max_nr_alloc + 1, sizeof(int));
 	if (!buffer)
 		return -ENOMEM;
 
mm/swap_cgroup.c +1 -3
index 7f34343c075a..5a9442979a18 100644
@@ -167,14 +167,12 @@ unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
 int swap_cgroup_swapon(int type, unsigned long max_pages)
 {
 	void *array;
-	unsigned long array_size;
 	unsigned long length;
 	struct swap_cgroup_ctrl *ctrl;
 
 	length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
-	array_size = length * sizeof(void *);
 
-	array = vzalloc(array_size);
+	array = vcalloc(length, sizeof(void *));
 	if (!array)
 		goto nomem;
 
Signature · 1 line
2.31.1

Thanks,
Dennis

author maintainer
#10 lore raw

PATCH 3/3 KVM: use vcalloc/__vcalloc for very large allocations

In reply to Paolo Bonzini

Allocations whose size is related to the memslot size can be arbitrarily large. Do not use kvzalloc/kvcalloc, as those are limited to "not crazy" sizes that fit in 32 bits. Now that it is available, they can use either vcalloc or __vcalloc, the latter if accounting is required.

---
 arch/powerpc/kvm/book3s_hv_uvmem.c | 2 +-
 arch/x86/kvm/mmu/page_track.c      | 7 ++++---
 arch/x86/kvm/x86.c                 | 4 ++--
 virt/kvm/kvm_main.c                | 4 ++--
 4 files changed, 9 insertions(+), 8 deletions(-)
arch/powerpc/kvm/book3s_hv_uvmem.c +1 -1
index e414ca44839f..be441403925b 100644
@@ -251,7 +251,7 @@ int kvmppc_uvmem_slot_init(struct kvm *kvm, const struct kvm_memory_slot *slot)
 	p = kzalloc(sizeof(*p), GFP_KERNEL);
 	if (!p)
 		return -ENOMEM;
-	p->pfns = vzalloc(array_size(slot->npages, sizeof(*p->pfns)));
+	p->pfns = vcalloc(slot->npages, sizeof(*p->pfns));
 	if (!p->pfns) {
 		kfree(p);
 		return -ENOMEM;
arch/x86/kvm/mmu/page_track.c +4 -3
index 68eb1fb548b6..2e09d1b6249f 100644
@@ -47,8 +47,8 @@ int kvm_page_track_create_memslot(struct kvm *kvm,
 			continue;
 
 		slot->arch.gfn_track[i] =
-			kvcalloc(npages, sizeof(*slot->arch.gfn_track[i]),
-				 GFP_KERNEL_ACCOUNT);
+			__vcalloc(npages, sizeof(*slot->arch.gfn_track[i]),
+				  GFP_KERNEL_ACCOUNT);
 		if (!slot->arch.gfn_track[i])
 			goto track_free;
 	}
@@ -75,7 +75,8 @@ int kvm_page_track_write_tracking_alloc(struct kvm_memory_slot *slot)
 	if (slot->arch.gfn_track[KVM_PAGE_TRACK_WRITE])
 		return 0;
 
-	gfn_track = kvcalloc(slot->npages, sizeof(*gfn_track), GFP_KERNEL_ACCOUNT);
+	gfn_track = __vcalloc(slot->npages, sizeof(*gfn_track),
+			      GFP_KERNEL_ACCOUNT);
 	if (gfn_track == NULL)
 		return -ENOMEM;
 
arch/x86/kvm/x86.c +2 -2
index f79bf4552082..4fa4d8269e5b 100644
@@ -11838,7 +11838,7 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages)
 		if (slot->arch.rmap[i])
 			continue;
 
-		slot->arch.rmap[i] = kvcalloc(lpages, sz, GFP_KERNEL_ACCOUNT);
+		slot->arch.rmap[i] = __vcalloc(lpages, sz, GFP_KERNEL_ACCOUNT);
 		if (!slot->arch.rmap[i]) {
 			memslot_rmap_free(slot);
 			return -ENOMEM;
@@ -11875,7 +11875,7 @@ static int kvm_alloc_memslot_metadata(struct kvm *kvm,
 
 		lpages = __kvm_mmu_slot_lpages(slot, npages, level);
 
-		linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
+		linfo = __vcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
 		if (!linfo)
 			goto out_free;
 
virt/kvm/kvm_main.c +2 -2
index c941b97fa133..a5726099df67 100644
@@ -1274,9 +1274,9 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
  */
 static int kvm_alloc_dirty_bitmap(struct kvm_memory_slot *memslot)
 {
-	unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
+	unsigned long dirty_bytes = kvm_dirty_bitmap_bytes(memslot);
 
-	memslot->dirty_bitmap = kvzalloc(dirty_bytes, GFP_KERNEL_ACCOUNT);
+	memslot->dirty_bitmap = __vcalloc(2, dirty_bytes, GFP_KERNEL_ACCOUNT);
 	if (!memslot->dirty_bitmap)
 		return -ENOMEM;
 
Signature · 1 line
2.31.1
Reviewed-by on v1 3/3
#11 lore raw

Re: [PATCH 3/3] KVM: use vcalloc/__vcalloc for very large allocations

Quoted 8 lines · Paolo Bonzini

Allocations whose size is related to the memslot size can be arbitrarily large. Do not use kvzalloc/kvcalloc, as those are limited to "not crazy" sizes that fit in 32 bits. Now that it is available, they can use either vcalloc or __vcalloc, the latter if accounting is required.

Fixes 7661809d493b ("mm: don't allow oversized kvmalloc() calls")

?

Signature · 3 lines
Thanks,

David / dhildenb
maintainer
#12 lore raw

Re: [PATCH 3/3] KVM: use vcalloc/__vcalloc for very large allocations

In reply to Paolo Bonzini
Quoted 6 lines · On Tue, 8 Mar 2022 05:59:18 -0500 Paolo Bonzini <[email protected]> wrote:

Allocations whose size is related to the memslot size can be arbitrarily large. Do not use kvzalloc/kvcalloc, as those are limited to "not crazy" sizes that fit in 32 bits. Now that it is available, they can use either vcalloc or __vcalloc, the latter if accounting is required.

Please fully describe the end user visible runtime effects when proposing a -stable backport. And when not proposing a -stable backport, come to that...