Commit e1ca7788dec6773b1a2bce51b7141948f2b8bccf

Authored by Dave Young
Committed by Linus Torvalds
1 parent 7bbc0905ea

mm: add vzalloc() and vzalloc_node() helpers

Add vzalloc() and vzalloc_node() to encapsulate the
vmalloc-then-memset-zero operation.

Use __GFP_ZERO to zero fill the allocated memory.

Signed-off-by: Dave Young <hidave.darkstar@gmail.com>
Cc: Christoph Lameter <cl@linux-foundation.org>
Acked-by: Greg Ungerer <gerg@snapgear.com>
Cc: David Howells <dhowells@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 3 changed files with 94 additions and 3 deletions Side-by-side Diff

include/linux/vmalloc.h
... ... @@ -53,8 +53,10 @@
53 53 #endif
54 54  
55 55 extern void *vmalloc(unsigned long size);
  56 +extern void *vzalloc(unsigned long size);
56 57 extern void *vmalloc_user(unsigned long size);
57 58 extern void *vmalloc_node(unsigned long size, int node);
  59 +extern void *vzalloc_node(unsigned long size, int node);
58 60 extern void *vmalloc_exec(unsigned long size);
59 61 extern void *vmalloc_32(unsigned long size);
60 62 extern void *vmalloc_32_user(unsigned long size);
... ... @@ -293,11 +293,58 @@
293 293 }
294 294 EXPORT_SYMBOL(vmalloc);
295 295  
  296 +/*
  297 + * vzalloc - allocate virtually continguos memory with zero fill
  298 + *
  299 + * @size: allocation size
  300 + *
  301 + * Allocate enough pages to cover @size from the page level
  302 + * allocator and map them into continguos kernel virtual space.
  303 + * The memory allocated is set to zero.
  304 + *
  305 + * For tight control over page level allocator and protection flags
  306 + * use __vmalloc() instead.
  307 + */
  308 +void *vzalloc(unsigned long size)
  309 +{
  310 + return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  311 + PAGE_KERNEL);
  312 +}
  313 +EXPORT_SYMBOL(vzalloc);
  314 +
  315 +/**
  316 + * vmalloc_node - allocate memory on a specific node
  317 + * @size: allocation size
  318 + * @node: numa node
  319 + *
  320 + * Allocate enough pages to cover @size from the page level
  321 + * allocator and map them into contiguous kernel virtual space.
  322 + *
  323 + * For tight control over page level allocator and protection flags
  324 + * use __vmalloc() instead.
  325 + */
296 326 void *vmalloc_node(unsigned long size, int node)
297 327 {
298 328 return vmalloc(size);
299 329 }
300   -EXPORT_SYMBOL(vmalloc_node);
  330 +
  331 +/**
  332 + * vzalloc_node - allocate memory on a specific node with zero fill
  333 + * @size: allocation size
  334 + * @node: numa node
  335 + *
  336 + * Allocate enough pages to cover @size from the page level
  337 + * allocator and map them into contiguous kernel virtual space.
  338 + * The memory allocated is set to zero.
  339 + *
  340 + * For tight control over page level allocator and protection flags
  341 + * use __vmalloc() instead.
  342 + */
  343 +void *vzalloc_node(unsigned long size, int node)
  344 +{
  345 + return vzalloc(size);
  346 +}
  347 +EXPORT_SYMBOL(vzalloc_node);
301 348  
302 349 #ifndef PAGE_KERNEL_EXEC
303 350 # define PAGE_KERNEL_EXEC PAGE_KERNEL
... ... @@ -1596,6 +1596,13 @@
1596 1596 }
1597 1597 EXPORT_SYMBOL(__vmalloc);
1598 1598  
  1599 +static inline void *__vmalloc_node_flags(unsigned long size,
  1600 + int node, gfp_t flags)
  1601 +{
  1602 + return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
  1603 + node, __builtin_return_address(0));
  1604 +}
  1605 +
1599 1606 /**
1600 1607 * vmalloc - allocate virtually contiguous memory
1601 1608 * @size: allocation size
1602 1609  
... ... @@ -1607,12 +1614,28 @@
1607 1614 */
1608 1615 void *vmalloc(unsigned long size)
1609 1616 {
1610   - return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1611   - -1, __builtin_return_address(0));
  1617 + return __vmalloc_node_flags(size, -1, GFP_KERNEL | __GFP_HIGHMEM);
1612 1618 }
1613 1619 EXPORT_SYMBOL(vmalloc);
1614 1620  
1615 1621 /**
  1622 + * vzalloc - allocate virtually contiguous memory with zero fill
  1623 + * @size: allocation size
  1624 + * Allocate enough pages to cover @size from the page level
  1625 + * allocator and map them into contiguous kernel virtual space.
  1626 + * The memory allocated is set to zero.
  1627 + *
  1628 + * For tight control over page level allocator and protection flags
  1629 + * use __vmalloc() instead.
  1630 + */
  1631 +void *vzalloc(unsigned long size)
  1632 +{
  1633 + return __vmalloc_node_flags(size, -1,
  1634 + GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
  1635 +}
  1636 +EXPORT_SYMBOL(vzalloc);
  1637 +
  1638 +/**
1616 1639 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1617 1640 * @size: allocation size
1618 1641 *
... ... @@ -1652,6 +1675,25 @@
1652 1675 node, __builtin_return_address(0));
1653 1676 }
1654 1677 EXPORT_SYMBOL(vmalloc_node);
  1678 +
  1679 +/**
  1680 + * vzalloc_node - allocate memory on a specific node with zero fill
  1681 + * @size: allocation size
  1682 + * @node: numa node
  1683 + *
  1684 + * Allocate enough pages to cover @size from the page level
  1685 + * allocator and map them into contiguous kernel virtual space.
  1686 + * The memory allocated is set to zero.
  1687 + *
  1688 + * For tight control over page level allocator and protection flags
  1689 + * use __vmalloc_node() instead.
  1690 + */
  1691 +void *vzalloc_node(unsigned long size, int node)
  1692 +{
  1693 + return __vmalloc_node_flags(size, node,
  1694 + GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
  1695 +}
  1696 +EXPORT_SYMBOL(vzalloc_node);
1655 1697  
1656 1698 #ifndef PAGE_KERNEL_EXEC
1657 1699 # define PAGE_KERNEL_EXEC PAGE_KERNEL