Skip to content

Commit 3ddc2fe

Browse files
Danilo Krummrichakpm00
Danilo Krummrich
authored andcommitted
mm: vmalloc: implement vrealloc()
Patch series "Align kvrealloc() with krealloc()", v2. Besides the obvious (and desired) difference between krealloc() and kvrealloc(), there is some inconsistency in their function signatures and behavior: - krealloc() frees the memory when the requested size is zero, whereas kvrealloc() simply returns a pointer to the existing allocation. - krealloc() behaves like kmalloc() if a NULL pointer is passed, whereas kvrealloc() does not accept a NULL pointer at all and, if passed, would fault instead. - krealloc() is self-contained, whereas kvrealloc() relies on the caller to provide the size of the previous allocation. Inconsistent behavior throughout allocation APIs is error prone, hence make kvrealloc() behave like krealloc(), which seems superior in all mentioned aspects. In order to be able to get rid of kvrealloc()'s oldsize parameter, introduce vrealloc() and make use of it in kvrealloc(). Making use of vrealloc() in kvrealloc() also provides oppertunities to grow (and shrink) allocations more efficiently. For instance, vrealloc() can be optimized to allocate and map additional pages to grow the allocation or unmap and free unused pages to shrink the allocation. Besides the above, those functions are required by Rust's allocator abstractons [1] (rework based on this series in [2]). With `Vec` or `KVec` respectively, potentially growing (and shrinking) data structures are rather common. [1] https://lore.kernel.org/lkml/[email protected]/ [2] https://git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/mm This patch (of 2): Implement vrealloc() analogous to krealloc(). Currently, krealloc() requires the caller to pass the size of the previous memory allocation, which, instead, should be self-contained. We attempt to fix this in a subsequent patch which, in order to do so, requires vrealloc(). Besides that, we need realloc() functions for kernel allocators in Rust too. With `Vec` or `KVec` respectively, potentially growing (and shrinking) data structures are rather common. [[email protected]: fix missing nommu implementation] Link: https://lkml.kernel.org/r/[email protected] [[email protected]: document concurrency restrictions] Link: https://lkml.kernel.org/r/[email protected] [[email protected]: consider spare memory for __GFP_ZERO] Link: https://lkml.kernel.org/r/[email protected] [[email protected]: properly document __GFP_ZERO behavior] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Danilo Krummrich <[email protected]> Acked-by: Michal Hocko <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Cc: Chandan Babu R <[email protected]> Cc: Christian König <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Christoph Lameter <[email protected]> Cc: David Rientjes <[email protected]> Cc: Hyeonggon Yoo <[email protected]> Cc: Joonsoo Kim <[email protected]> Cc: Kees Cook <[email protected]> Cc: Marc Zyngier <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Miguel Ojeda <[email protected]> Cc: Oliver Upton <[email protected]> Cc: Pekka Enberg <[email protected]> Cc: Roman Gushchin <[email protected]> Cc: Uladzislau Rezki <[email protected]> Cc: Wedson Almeida Filho <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 5fe690a commit 3ddc2fe

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

include/linux/vmalloc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ extern void *__vcalloc_noprof(size_t n, size_t size, gfp_t flags) __alloc_size(1
189189
extern void *vcalloc_noprof(size_t n, size_t size) __alloc_size(1, 2);
190190
#define vcalloc(...) alloc_hooks(vcalloc_noprof(__VA_ARGS__))
191191

192+
void * __must_check vrealloc_noprof(const void *p, size_t size, gfp_t flags)
193+
__realloc_size(2);
194+
#define vrealloc(...) alloc_hooks(vrealloc_noprof(__VA_ARGS__))
195+
192196
extern void vfree(const void *addr);
193197
extern void vfree_atomic(const void *addr);
194198

mm/nommu.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ void *__vmalloc_noprof(unsigned long size, gfp_t gfp_mask)
126126
}
127127
EXPORT_SYMBOL(__vmalloc_noprof);
128128

129+
void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
130+
{
131+
return krealloc_noprof(p, size, (flags | __GFP_COMP) & ~__GFP_HIGHMEM);
132+
}
133+
129134
void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,
130135
unsigned long start, unsigned long end, gfp_t gfp_mask,
131136
pgprot_t prot, unsigned long vm_flags, int node,

mm/vmalloc.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4030,6 +4030,76 @@ void *vzalloc_node_noprof(unsigned long size, int node)
40304030
}
40314031
EXPORT_SYMBOL(vzalloc_node_noprof);
40324032

4033+
/**
4034+
* vrealloc - reallocate virtually contiguous memory; contents remain unchanged
4035+
* @p: object to reallocate memory for
4036+
* @size: the size to reallocate
4037+
* @flags: the flags for the page level allocator
4038+
*
4039+
* If @p is %NULL, vrealloc() behaves exactly like vmalloc(). If @size is 0 and
4040+
* @p is not a %NULL pointer, the object pointed to is freed.
4041+
*
4042+
* If __GFP_ZERO logic is requested, callers must ensure that, starting with the
4043+
* initial memory allocation, every subsequent call to this API for the same
4044+
* memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
4045+
* __GFP_ZERO is not fully honored by this API.
4046+
*
4047+
* In any case, the contents of the object pointed to are preserved up to the
4048+
* lesser of the new and old sizes.
4049+
*
4050+
* This function must not be called concurrently with itself or vfree() for the
4051+
* same memory allocation.
4052+
*
4053+
* Return: pointer to the allocated memory; %NULL if @size is zero or in case of
4054+
* failure
4055+
*/
4056+
void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
4057+
{
4058+
size_t old_size = 0;
4059+
void *n;
4060+
4061+
if (!size) {
4062+
vfree(p);
4063+
return NULL;
4064+
}
4065+
4066+
if (p) {
4067+
struct vm_struct *vm;
4068+
4069+
vm = find_vm_area(p);
4070+
if (unlikely(!vm)) {
4071+
WARN(1, "Trying to vrealloc() nonexistent vm area (%p)\n", p);
4072+
return NULL;
4073+
}
4074+
4075+
old_size = get_vm_area_size(vm);
4076+
}
4077+
4078+
/*
4079+
* TODO: Shrink the vm_area, i.e. unmap and free unused pages. What
4080+
* would be a good heuristic for when to shrink the vm_area?
4081+
*/
4082+
if (size <= old_size) {
4083+
/* Zero out spare memory. */
4084+
if (want_init_on_alloc(flags))
4085+
memset((void *)p + size, 0, old_size - size);
4086+
4087+
return (void *)p;
4088+
}
4089+
4090+
/* TODO: Grow the vm_area, i.e. allocate and map additional pages. */
4091+
n = __vmalloc_noprof(size, flags);
4092+
if (!n)
4093+
return NULL;
4094+
4095+
if (p) {
4096+
memcpy(n, p, old_size);
4097+
vfree(p);
4098+
}
4099+
4100+
return n;
4101+
}
4102+
40334103
#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
40344104
#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
40354105
#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)

0 commit comments

Comments
 (0)