Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.

Acked-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

+55
+5
include/linux/vmalloc.h
··· 159 159 int node, const void *caller) __alloc_size(1); 160 160 void *vmalloc_no_huge(unsigned long size) __alloc_size(1); 161 161 162 + extern void *__vmalloc_array(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2); 163 + extern void *vmalloc_array(size_t n, size_t size) __alloc_size(1, 2); 164 + extern void *__vcalloc(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2); 165 + extern void *vcalloc(size_t n, size_t size) __alloc_size(1, 2); 166 + 162 167 extern void vfree(const void *addr); 163 168 extern void vfree_atomic(const void *addr); 164 169
+50
mm/util.c
··· 647 647 } 648 648 EXPORT_SYMBOL(kvrealloc); 649 649 650 + /** 651 + * __vmalloc_array - allocate memory for a virtually contiguous array. 652 + * @n: number of elements. 653 + * @size: element size. 654 + * @flags: the type of memory to allocate (see kmalloc). 655 + */ 656 + void *__vmalloc_array(size_t n, size_t size, gfp_t flags) 657 + { 658 + size_t bytes; 659 + 660 + if (unlikely(check_mul_overflow(n, size, &bytes))) 661 + return NULL; 662 + return __vmalloc(bytes, flags); 663 + } 664 + EXPORT_SYMBOL(__vmalloc_array); 665 + 666 + /** 667 + * vmalloc_array - allocate memory for a virtually contiguous array. 668 + * @n: number of elements. 669 + * @size: element size. 670 + */ 671 + void *vmalloc_array(size_t n, size_t size) 672 + { 673 + return __vmalloc_array(n, size, GFP_KERNEL); 674 + } 675 + EXPORT_SYMBOL(vmalloc_array); 676 + 677 + /** 678 + * __vcalloc - allocate and zero memory for a virtually contiguous array. 679 + * @n: number of elements. 680 + * @size: element size. 681 + * @flags: the type of memory to allocate (see kmalloc). 682 + */ 683 + void *__vcalloc(size_t n, size_t size, gfp_t flags) 684 + { 685 + return __vmalloc_array(n, size, flags | __GFP_ZERO); 686 + } 687 + EXPORT_SYMBOL(__vcalloc); 688 + 689 + /** 690 + * vcalloc - allocate and zero memory for a virtually contiguous array. 691 + * @n: number of elements. 692 + * @size: element size. 693 + */ 694 + void *vcalloc(size_t n, size_t size) 695 + { 696 + return __vmalloc_array(n, size, GFP_KERNEL | __GFP_ZERO); 697 + } 698 + EXPORT_SYMBOL(vcalloc); 699 + 650 700 /* Neutral page->mapping pointer to address_space or anon_vma or other */ 651 701 void *page_rmapping(struct page *page) 652 702 {