Skip to content

Commit 4a3c051

Browse files
hidavemilaq
authored andcommitted
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 <[email protected]> Cc: Christoph Lameter <[email protected]> Acked-by: Greg Ungerer <[email protected]> Cc: David Howells <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent f649f63 commit 4a3c051

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

include/linux/vmalloc.h

+2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ static inline void vmalloc_init(void)
5151
#endif
5252

5353
extern void *vmalloc(unsigned long size);
54+
extern void *vzalloc(unsigned long size);
5455
extern void *vmalloc_user(unsigned long size);
5556
extern void *vmalloc_node(unsigned long size, int node);
57+
extern void *vzalloc_node(unsigned long size, int node);
5658
extern void *vmalloc_exec(unsigned long size);
5759
extern void *vmalloc_32(unsigned long size);
5860
extern void *vmalloc_32_user(unsigned long size);

mm/nommu.c

+48-1
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,58 @@ void *vmalloc(unsigned long size)
298298
}
299299
EXPORT_SYMBOL(vmalloc);
300300

301+
/*
302+
* vzalloc - allocate virtually continguos memory with zero fill
303+
*
304+
* @size: allocation size
305+
*
306+
* Allocate enough pages to cover @size from the page level
307+
* allocator and map them into continguos kernel virtual space.
308+
* The memory allocated is set to zero.
309+
*
310+
* For tight control over page level allocator and protection flags
311+
* use __vmalloc() instead.
312+
*/
313+
void *vzalloc(unsigned long size)
314+
{
315+
return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
316+
PAGE_KERNEL);
317+
}
318+
EXPORT_SYMBOL(vzalloc);
319+
320+
/**
321+
* vmalloc_node - allocate memory on a specific node
322+
* @size: allocation size
323+
* @node: numa node
324+
*
325+
* Allocate enough pages to cover @size from the page level
326+
* allocator and map them into contiguous kernel virtual space.
327+
*
328+
* For tight control over page level allocator and protection flags
329+
* use __vmalloc() instead.
330+
*/
301331
void *vmalloc_node(unsigned long size, int node)
302332
{
303333
return vmalloc(size);
304334
}
305-
EXPORT_SYMBOL(vmalloc_node);
335+
336+
/**
337+
* vzalloc_node - allocate memory on a specific node with zero fill
338+
* @size: allocation size
339+
* @node: numa node
340+
*
341+
* Allocate enough pages to cover @size from the page level
342+
* allocator and map them into contiguous kernel virtual space.
343+
* The memory allocated is set to zero.
344+
*
345+
* For tight control over page level allocator and protection flags
346+
* use __vmalloc() instead.
347+
*/
348+
void *vzalloc_node(unsigned long size, int node)
349+
{
350+
return vzalloc(size);
351+
}
352+
EXPORT_SYMBOL(vzalloc_node);
306353

307354
#ifndef PAGE_KERNEL_EXEC
308355
# define PAGE_KERNEL_EXEC PAGE_KERNEL

mm/vmalloc.c

+44-2
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,13 @@ void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
15831583
}
15841584
EXPORT_SYMBOL(__vmalloc);
15851585

1586+
static inline void *__vmalloc_node_flags(unsigned long size,
1587+
int node, gfp_t flags)
1588+
{
1589+
return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
1590+
node, __builtin_return_address(0));
1591+
}
1592+
15861593
/**
15871594
* vmalloc - allocate virtually contiguous memory
15881595
* @size: allocation size
@@ -1594,11 +1601,27 @@ EXPORT_SYMBOL(__vmalloc);
15941601
*/
15951602
void *vmalloc(unsigned long size)
15961603
{
1597-
return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1598-
-1, __builtin_return_address(0));
1604+
return __vmalloc_node_flags(size, -1, GFP_KERNEL | __GFP_HIGHMEM);
15991605
}
16001606
EXPORT_SYMBOL(vmalloc);
16011607

1608+
/**
1609+
* vzalloc - allocate virtually contiguous memory with zero fill
1610+
* @size: allocation size
1611+
* Allocate enough pages to cover @size from the page level
1612+
* allocator and map them into contiguous kernel virtual space.
1613+
* The memory allocated is set to zero.
1614+
*
1615+
* For tight control over page level allocator and protection flags
1616+
* use __vmalloc() instead.
1617+
*/
1618+
void *vzalloc(unsigned long size)
1619+
{
1620+
return __vmalloc_node_flags(size, -1,
1621+
GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1622+
}
1623+
EXPORT_SYMBOL(vzalloc);
1624+
16021625
/**
16031626
* vmalloc_user - allocate zeroed virtually contiguous memory for userspace
16041627
* @size: allocation size
@@ -1640,6 +1663,25 @@ void *vmalloc_node(unsigned long size, int node)
16401663
}
16411664
EXPORT_SYMBOL(vmalloc_node);
16421665

1666+
/**
1667+
* vzalloc_node - allocate memory on a specific node with zero fill
1668+
* @size: allocation size
1669+
* @node: numa node
1670+
*
1671+
* Allocate enough pages to cover @size from the page level
1672+
* allocator and map them into contiguous kernel virtual space.
1673+
* The memory allocated is set to zero.
1674+
*
1675+
* For tight control over page level allocator and protection flags
1676+
* use __vmalloc_node() instead.
1677+
*/
1678+
void *vzalloc_node(unsigned long size, int node)
1679+
{
1680+
return __vmalloc_node_flags(size, node,
1681+
GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1682+
}
1683+
EXPORT_SYMBOL(vzalloc_node);
1684+
16431685
#ifndef PAGE_KERNEL_EXEC
16441686
# define PAGE_KERNEL_EXEC PAGE_KERNEL
16451687
#endif

0 commit comments

Comments
 (0)