Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 29 Apr 2026 21:43:09 +0000
Message-ID:  <69f27b6d.18d9d.6cc8ded5@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch stable/15 has been updated by dumbbell:

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

commit ce3348bf1de9533074226977ac8d8945f1fe40de
Author:     Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
AuthorDate: 2026-04-14 00:01:26 +0000
Commit:     Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
CommitDate: 2026-04-29 21:04:12 +0000

    linuxkpi: Add Linux 6.12 variant of `kvrealloc()`
    
    In Linux 6.12, the API changed to be closer to `krealloc()`:
    * The function does not take the old size anymore
    * The function becomes a wrapper around `krealloc()` with a fallback
      mechanism.
    
    Reviewed by:    bz
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit d74fa49d0c0476353af137d22b5ef8711c67b854)
---
 sys/compat/linuxkpi/common/include/linux/slab.h | 39 +++++++++++++++----------
 sys/compat/linuxkpi/common/src/linux_slab.c     | 38 ++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 16 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/slab.h b/sys/compat/linuxkpi/common/include/linux/slab.h
index 8d023eaddadd..b529a6303085 100644
--- a/sys/compat/linuxkpi/common/include/linux/slab.h
+++ b/sys/compat/linuxkpi/common/include/linux/slab.h
@@ -141,6 +141,13 @@ linux_check_m_flags(gfp_t flags)
 /*
  * Base functions with a native implementation.
  */
+
+static inline size_t
+ksize(const void *ptr)
+{
+	return (malloc_usable_size(ptr));
+}
+
 static inline void *
 kmalloc(size_t size, gfp_t flags)
 {
@@ -264,22 +271,28 @@ kvmalloc_array(size_t n, size_t size, gfp_t flags)
 	return (kvmalloc(size * n, flags));
 }
 
+void * lkpi_kvrealloc(const void *ptr, size_t oldsize, size_t newsize, gfp_t flags);
+
+#if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION < 61200
 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));
+	return (lkpi_kvrealloc(ptr, oldsize, newsize, flags));
+}
+#else
+static inline void *
+kvrealloc(const void *ptr, size_t newsize, gfp_t flags)
+{
+	size_t oldsize;
 
-	newptr = kvmalloc(newsize, flags);
-	if (newptr != NULL) {
-		memcpy(newptr, ptr, oldsize);
-		kvfree(ptr);
-	}
+	if (!ZERO_OR_NULL_PTR(ptr))
+		oldsize = ksize(ptr);
+	else
+		oldsize = 0;
 
-	return (newptr);
+	return (lkpi_kvrealloc(ptr, oldsize, newsize, flags));
 }
+#endif
 
 /*
  * Misc.
@@ -294,12 +307,6 @@ kfree_sensitive(const void *ptr)
 	zfree(__DECONST(void *, ptr), M_KMALLOC);
 }
 
-static inline size_t
-ksize(const void *ptr)
-{
-	return (malloc_usable_size(ptr));
-}
-
 static inline size_t
 kmalloc_size_roundup(size_t size)
 {
diff --git a/sys/compat/linuxkpi/common/src/linux_slab.c b/sys/compat/linuxkpi/common/src/linux_slab.c
index a387e5c23ad3..9fe51f0f4e10 100644
--- a/sys/compat/linuxkpi/common/src/linux_slab.c
+++ b/sys/compat/linuxkpi/common/src/linux_slab.c
@@ -278,6 +278,44 @@ lkpi_krealloc(const void *ptr, size_t size, gfp_t flags)
 	return (nptr);
 }
 
+void *
+lkpi_kvrealloc(const void *ptr, size_t oldsize, size_t newsize, gfp_t flags)
+{
+	void *newptr;
+
+	/*
+	 * We replicate the behaviour of krealloc() instead of calling it
+	 * because we don't need to allocate physically contiguous memory.
+	 */
+
+	if (newsize == 0) {
+		kfree(ptr);
+		return (ZERO_SIZE_PTR);
+	}
+
+	if (ptr == NULL) {
+		newptr = kvmalloc(newsize, flags);
+		return (newptr);
+	}
+
+	newptr = realloc(
+	    __DECONST(void *, ptr), newsize, M_KMALLOC,
+	    linux_check_m_flags(flags));
+
+	if (newptr == NULL) {
+		newptr = kvmalloc(newsize, flags);
+		if (newptr == NULL)
+			return (NULL);
+
+		if (ptr != NULL) {
+			memcpy(newptr, ptr, oldsize);
+			kfree(ptr);
+		}
+	}
+
+	return (newptr);
+}
+
 struct lkpi_kmalloc_ctx {
 	size_t size;
 	gfp_t flags;


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69f27b6d.18d9d.6cc8ded5>