git: 839999e7efdc - main - vm: Add kva_alloc_aligned

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Thu, 30 Nov 2023 11:24:32 UTC
The branch main has been updated by andrew:

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

commit 839999e7efdc980d5ada92ea93719c7e29765809
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2023-11-29 12:54:49 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2023-11-30 10:50:03 +0000

    vm: Add kva_alloc_aligned
    
    Add a function like kva_alloc that allows us to specify the alignment
    of the virtual address space returned.
    
    Reviewed by:    alc, kib, markj
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D42788
---
 sys/vm/vm_extern.h |  1 +
 sys/vm/vm_kern.c   | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/sys/vm/vm_extern.h b/sys/vm/vm_extern.h
index bcef44012472..2e2bc18a0233 100644
--- a/sys/vm/vm_extern.h
+++ b/sys/vm/vm_extern.h
@@ -47,6 +47,7 @@ struct domainset;
 
 /* These operate on kernel virtual addresses only. */
 vm_offset_t kva_alloc(vm_size_t);
+vm_offset_t kva_alloc_aligned(vm_size_t, vm_size_t);
 void kva_free(vm_offset_t, vm_size_t);
 
 /* These operate on pageable virtual addresses. */
diff --git a/sys/vm/vm_kern.c b/sys/vm/vm_kern.c
index 6b22a96bf597..b17b857bd902 100644
--- a/sys/vm/vm_kern.c
+++ b/sys/vm/vm_kern.c
@@ -155,6 +155,27 @@ kva_alloc(vm_size_t size)
 	return (addr);
 }
 
+/*
+ *	kva_alloc_aligned:
+ *
+ *	Allocate a virtual address range as in kva_alloc where the base
+ *	address is aligned to align.
+ */
+vm_offset_t
+kva_alloc_aligned(vm_size_t size, vm_size_t align)
+{
+	vm_offset_t addr;
+
+	TSENTER();
+	size = round_page(size);
+	if (vmem_xalloc(kernel_arena, size, align, 0, 0, VMEM_ADDR_MIN,
+	    VMEM_ADDR_MAX, M_BESTFIT | M_NOWAIT, &addr))
+		return (0);
+	TSEXIT();
+
+	return (addr);
+}
+
 /*
  *	kva_free:
  *