[PATCH 16/20] headers: add vcalloc() backport for kernels < 5.18

Yi Cong <[email protected]> Wed, 24 Jun 2026 15:38:40 +0800
Newsgroups org.kernel.vger.backports
Message-ID <[email protected]>
From: Yi Cong <[email protected]>

vcalloc() was introduced in v5.18 to allocate and zero virtually
contiguous memory with overflow-checked multiplication. Provide a
wrapper using vzalloc() for older kernels.

Signed-off-by: Yi Cong <[email protected]>
---
 backport/backport-include/linux/vmalloc.h | 25 +++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 backport/backport-include/linux/vmalloc.h

diff --git a/backport/backport-include/linux/vmalloc.h b/backport/backport-include/linux/vmalloc.h
new file mode 100644
index 00000000..233c8608
--- /dev/null
+++ b/backport/backport-include/linux/vmalloc.h
@@ -0,0 +1,25 @@
+#ifndef __BACKPORT_LINUX_VMALLOC_H
+#define __BACKPORT_LINUX_VMALLOC_H
+#include_next <linux/vmalloc.h>
+#include <linux/version.h>
+
+#if LINUX_VERSION_IS_LESS(5,18,0)
+/**
+ * vcalloc - allocate virtually contiguous memory and zero it
+ * @n:		number of elements
+ * @size:	size of each element
+ *
+ * Introduced in v5.18. On older kernels, fall back to vzalloc()
+ * with overflow-checked multiplication.
+ */
+static inline void *vcalloc(size_t n, size_t size)
+{
+	size_t bytes;
+	if (size && n > SIZE_MAX / size)
+		return NULL;
+	bytes = n * size;
+	return vzalloc(bytes);
+}
+#endif
+
+#endif /* __BACKPORT_LINUX_VMALLOC_H */
-- 
2.43.0