git: d8394e282f47 - stable/14 - LinuxKPI: Add kvrealloc to linux/slab.h

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Thu, 01 Aug 2024 22:27:57 UTC
The branch stable/14 has been updated by wulf:

URL: https://cgit.FreeBSD.org/src/commit/?id=d8394e282f47f6e0f5670ca37d0d9c1d87b73933

commit d8394e282f47f6e0f5670ca37d0d9c1d87b73933
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2024-07-21 13:08:28 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2024-08-01 21:14:10 +0000

    LinuxKPI: Add kvrealloc to linux/slab.h
    
    Sponsored by:   Serenity Cyber Security, LLC
    MFC after:      1 week
    Reviewed by:    manu
    Differential Revision:  https://reviews.freebsd.org/D45616
    
    (cherry picked from commit ab6e1167909bf1e2792a2ba33000e13d33aaf551)
---
 sys/compat/linuxkpi/common/include/linux/slab.h | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/slab.h b/sys/compat/linuxkpi/common/include/linux/slab.h
index 298306b6ea05..e2d17e0558c2 100644
--- a/sys/compat/linuxkpi/common/include/linux/slab.h
+++ b/sys/compat/linuxkpi/common/include/linux/slab.h
@@ -91,6 +91,8 @@ struct linux_kmem_cache;
 #define	ZERO_SIZE_PTR		((void *)16)
 #define ZERO_OR_NULL_PTR(x)	((x) == NULL || (x) == ZERO_SIZE_PTR)
 
+extern void *lkpi_kmalloc(size_t size, gfp_t flags);
+
 static inline gfp_t
 linux_check_m_flags(gfp_t flags)
 {
@@ -212,13 +214,29 @@ kfree_sensitive(const void *ptr)
 	zfree(__DECONST(void *, ptr), M_KMALLOC);
 }
 
+static inline void *
+kvrealloc(const void *ptr, size_t oldsize, size_t newsize, gfp_t flags)
+{
+	void *newptr;
+
+	if (newsize <= oldsize)
+		return (__DECONST(void *, ptr));
+
+	newptr = kvmalloc(newsize, flags);
+	if (newptr != NULL) {
+		memcpy(newptr, ptr, oldsize);
+		kvfree(ptr);
+	}
+
+	return (newptr);
+}
+
 static inline size_t
 ksize(const void *ptr)
 {
 	return (malloc_usable_size(ptr));
 }
 
-extern void *lkpi_kmalloc(size_t size, gfp_t flags);
 extern struct linux_kmem_cache *linux_kmem_cache_create(const char *name,
     size_t size, size_t align, unsigned flags, linux_kmem_ctor_t *ctor);
 extern void *lkpi_kmem_cache_alloc(struct linux_kmem_cache *, gfp_t);