• Danilo Krummrich's avatar
    mm: vmalloc: implement vrealloc() · 3ddc2fef
    Danilo Krummrich authored
    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/20240704170738.3621-1-dakr@redhat.com/
    [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.
    
    [dakr@kernel.org: fix missing nommu implementation]
      Link: https://lkml.kernel.org/r/20240725141227.13954-1-dakr@kernel.org
    [dakr@kernel.org: document concurrency restrictions]
      Link: https://lkml.kernel.org/r/20240725125442.4957-1-dakr@kernel.org
    [dakr@kernel.org: consider spare memory for __GFP_ZERO]
      Link: https://lkml.kernel.org/r/20240730185049.6244-3-dakr@kernel.org
    [dakr@kernel.org: properly document __GFP_ZERO behavior]
      Link: https://lkml.kernel.org/r/20240730185049.6244-4-dakr@kernel.org
    Link: https://lkml.kernel.org/r/20240722163111.4766-1-dakr@kernel.org
    Link: https://lkml.kernel.org/r/20240722163111.4766-2-dakr@kernel.orgSigned-off-by: default avatarDanilo Krummrich <dakr@kernel.org>
    Acked-by: default avatarMichal Hocko <mhocko@suse.com>
    Acked-by: default avatarVlastimil Babka <vbabka@suse.cz>
    Cc: Chandan Babu R <chandan.babu@oracle.com>
    Cc: Christian König <christian.koenig@amd.com>
    Cc: Christoph Hellwig <hch@infradead.org>
    Cc: Christoph Lameter <cl@linux.com>
    Cc: David Rientjes <rientjes@google.com>
    Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
    Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
    Cc: Kees Cook <kees@kernel.org>
    Cc: Marc Zyngier <maz@kernel.org>
    Cc: Michael Ellerman <mpe@ellerman.id.au>
    Cc: Miguel Ojeda <ojeda@kernel.org>
    Cc: Oliver Upton <oliver.upton@linux.dev>
    Cc: Pekka Enberg <penberg@kernel.org>
    Cc: Roman Gushchin <roman.gushchin@linux.dev>
    Cc: Uladzislau Rezki <urezki@gmail.com>
    Cc: Wedson Almeida Filho <wedsonaf@gmail.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    3ddc2fef
vmalloc.c 134 KB