Module Name:    src
Committed By:   thorpej
Date:           Sat Dec  2 21:02:13 UTC 2023

Modified Files:
        src/distrib/sets/lists/comp: mi
        src/share/man/man9: Makefile vmem.9
        src/sys/kern: subr_vmem.c
        src/sys/sys: vmem.h

Log Message:
Add a vmem_xalloc_addr() function, which allocates a specific address
from an arena.  This is just a convenience wrapper around vmem_xalloc(),
that's just a bit more obvious how to use and performs some additional
sanity checks.


To generate a diff of this commit:
cvs rdiff -u -r1.2450 -r1.2451 src/distrib/sets/lists/comp/mi
cvs rdiff -u -r1.467 -r1.468 src/share/man/man9/Makefile
cvs rdiff -u -r1.21 -r1.22 src/share/man/man9/vmem.9
cvs rdiff -u -r1.110 -r1.111 src/sys/kern/subr_vmem.c
cvs rdiff -u -r1.23 -r1.24 src/sys/sys/vmem.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/comp/mi
diff -u src/distrib/sets/lists/comp/mi:1.2450 src/distrib/sets/lists/comp/mi:1.2451
--- src/distrib/sets/lists/comp/mi:1.2450	Sun Oct 15 13:04:15 2023
+++ src/distrib/sets/lists/comp/mi	Sat Dec  2 21:02:12 2023
@@ -1,4 +1,4 @@
-#	$NetBSD: mi,v 1.2450 2023/10/15 13:04:15 riastradh Exp $
+#	$NetBSD: mi,v 1.2451 2023/12/02 21:02:12 thorpej Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.comp				comp-sys-root
@@ -13097,6 +13097,7 @@
 ./usr/share/man/cat9/vmem_destroy.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/vmem_free.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/vmem_xalloc.0		comp-sys-catman		.cat
+./usr/share/man/cat9/vmem_xalloc_addr.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/vmem_xfree.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/vmem_xfreeall.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/vn_bwrite.0		comp-sys-catman		.cat
@@ -21435,6 +21436,7 @@
 ./usr/share/man/html9/vmem_destroy.html		comp-sys-htmlman	html
 ./usr/share/man/html9/vmem_free.html		comp-sys-htmlman	html
 ./usr/share/man/html9/vmem_xalloc.html		comp-sys-htmlman	html
+./usr/share/man/html9/vmem_xalloc_addr.html	comp-sys-htmlman	html
 ./usr/share/man/html9/vmem_xfree.html		comp-sys-htmlman	html
 ./usr/share/man/html9/vmem_xfreeall.html	comp-sys-htmlman	html
 ./usr/share/man/html9/vn_bwrite.html		comp-sys-htmlman	html
@@ -30012,6 +30014,7 @@
 ./usr/share/man/man9/vmem_destroy.9		comp-sys-man		.man
 ./usr/share/man/man9/vmem_free.9		comp-sys-man		.man
 ./usr/share/man/man9/vmem_xalloc.9		comp-sys-man		.man
+./usr/share/man/man9/vmem_xalloc_addr.9		comp-sys-man		.man
 ./usr/share/man/man9/vmem_xfree.9		comp-sys-man		.man
 ./usr/share/man/man9/vmem_xfreeall.9		comp-sys-man		.man
 ./usr/share/man/man9/vn_bwrite.9		comp-sys-man		.man

Index: src/share/man/man9/Makefile
diff -u src/share/man/man9/Makefile:1.467 src/share/man/man9/Makefile:1.468
--- src/share/man/man9/Makefile:1.467	Sat Jul  8 16:14:11 2023
+++ src/share/man/man9/Makefile	Sat Dec  2 21:02:12 2023
@@ -1,4 +1,4 @@
-#       $NetBSD: Makefile,v 1.467 2023/07/08 16:14:11 christos Exp $
+#       $NetBSD: Makefile,v 1.468 2023/12/02 21:02:12 thorpej Exp $
 
 #	Makefile for section 9 (kernel function and variable) manual pages.
 
@@ -1071,6 +1071,7 @@ MLINKS+=vmem.9 vmem_alloc.9 \
 	vmem.9 vmem_destroy.9 \
 	vmem.9 vmem_free.9 \
 	vmem.9 vmem_xalloc.9 \
+	vmem.9 vmem_xalloc_addr.9 \
 	vmem.9 vmem_xfree.9 \
 	vmem.9 vmem_xfreeall.9
 MLINKS+=vnode.9 vref.9 \

Index: src/share/man/man9/vmem.9
diff -u src/share/man/man9/vmem.9:1.21 src/share/man/man9/vmem.9:1.22
--- src/share/man/man9/vmem.9:1.21	Sat Oct 15 14:54:21 2022
+++ src/share/man/man9/vmem.9	Sat Dec  2 21:02:12 2023
@@ -1,4 +1,4 @@
-.\"	$NetBSD: vmem.9,v 1.21 2022/10/15 14:54:21 riastradh Exp $
+.\"	$NetBSD: vmem.9,v 1.22 2023/12/02 21:02:12 thorpej Exp $
 .\"
 .\" Copyright (c)2006 YAMAMOTO Takashi,
 .\" All rights reserved.
@@ -25,7 +25,7 @@
 .\" SUCH DAMAGE.
 .\"
 .\" ------------------------------------------------------------
-.Dd June 15, 2020
+.Dd December 2, 2023
 .Dt VMEM 9
 .Os
 .\" ------------------------------------------------------------
@@ -60,6 +60,10 @@
 "vmem_size_t phase" "vmem_size_t nocross" "vmem_addr_t minaddr" \
 "vmem_addr_t maxaddr" "vm_flag_t flags" "vmem_addr_t *addrp"
 .\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+.Ft int
+.Fn vmem_xalloc_addr \
+"vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size" "vm_flag_t flags"
+.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 .Ft void
 .Fn vmem_xfree "vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size"
 .\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -387,9 +391,40 @@ overwrites it with the start address of 
 .El
 .Pp
 .\" ------------------------------------------------------------
+.Fn vmem_xalloc_addr
+allocates a specific address from the arena.
+The requested address must be aligned with the arena's quantum.
+.Bl -tag -offset indent -width flags
+.It Fa vm
+The arena which we allocate from.
+.It Fa addr
+The address to allocate.
+.It Fa size
+Specify the size of the allocation.
+.It Fa flags
+A sleep flag.
+Because a specific address is being allocated, any specified allocation
+strategy is ignored.
+.Pp
+The sleep flag must be one of:
+.Bl -tag -width VM_NOSLEEP
+.It Dv VM_SLEEP
+If the allocation cannot be satisfied immediately, sleep until the
+requested range can be allocated.
+.It Dv VM_NOSLEEP
+Don't sleep.
+Immediately return
+.Dv ENOMEM
+if the requested range is not available.
+.El
+.El
+.Pp
+.\" ------------------------------------------------------------
 .Fn vmem_xfree
 frees resource allocated by
 .Fn vmem_xalloc
+or
+.Fn vmem_xalloc_addr
 to the arena.
 .Bl -tag -offset indent -width addr
 .It Fa vm
@@ -397,16 +432,20 @@ The arena which we free to.
 .It Fa addr
 The resource being freed.
 It must have been allocated via
-.Fn vmem_xalloc .
+.Fn vmem_xalloc
+or
+.Fn vmem_xalloc_addr .
 Notably, it must not have been allocated via
-.Fn vmem_alloc .
-Otherwise, the behaviour is undefined.
+.Fn vmem_alloc ,
+otherwise, the behaviour is undefined.
 .It Fa size
 The size of the resource being freed.
 It must be the same as the
 .Fa size
 argument used for
-.Fn vmem_xalloc .
+.Fn vmem_xalloc
+or
+.Fn vmem_xalloc_addr .
 .El
 .Pp
 .\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -479,8 +518,10 @@ The resource being freed.
 It must have been allocated via
 .Fn vmem_alloc .
 Notably, it must not have been allocated via
-.Fn vmem_xalloc .
-Otherwise, the behaviour is undefined.
+.Fn vmem_xalloc
+or
+.Fn vmem_xalloc addr ,
+otherwise, the behaviour is undefined.
 .It Fa size
 The size of the resource being freed.
 It must be the same as the

Index: src/sys/kern/subr_vmem.c
diff -u src/sys/kern/subr_vmem.c:1.110 src/sys/kern/subr_vmem.c:1.111
--- src/sys/kern/subr_vmem.c:1.110	Sat Dec  2 19:06:17 2023
+++ src/sys/kern/subr_vmem.c	Sat Dec  2 21:02:12 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_vmem.c,v 1.110 2023/12/02 19:06:17 thorpej Exp $	*/
+/*	$NetBSD: subr_vmem.c,v 1.111 2023/12/02 21:02:12 thorpej Exp $	*/
 
 /*-
  * Copyright (c)2006,2007,2008,2009 YAMAMOTO Takashi,
@@ -46,7 +46,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_vmem.c,v 1.110 2023/12/02 19:06:17 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_vmem.c,v 1.111 2023/12/02 21:02:12 thorpej Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_ddb.h"
@@ -1112,6 +1112,26 @@ vmem_alloc(vmem_t *vm, vmem_size_t size,
 }
 
 int
+vmem_xalloc_addr(vmem_t *vm, const vmem_addr_t addr, const vmem_size_t size,
+    vm_flag_t flags)
+{
+	vmem_addr_t result;
+	int error;
+
+	KASSERT((addr & vm->vm_quantum_mask) == 0);
+	KASSERT(size != 0);
+
+	flags = (flags & ~VM_INSTANTFIT) | VM_BESTFIT;
+
+	error = vmem_xalloc(vm, size, 0, 0, 0, addr, addr + size - 1,
+	    flags, &result);
+
+	KASSERT(error || result == addr);
+	KASSERT(error == 0 || (flags & VM_SLEEP) == 0);
+	return error;
+}
+
+int
 vmem_xalloc(vmem_t *vm, const vmem_size_t size0, vmem_size_t align,
     const vmem_size_t phase, const vmem_size_t nocross,
     const vmem_addr_t minaddr, const vmem_addr_t maxaddr, const vm_flag_t flags,

Index: src/sys/sys/vmem.h
diff -u src/sys/sys/vmem.h:1.23 src/sys/sys/vmem.h:1.24
--- src/sys/sys/vmem.h:1.23	Sat Dec  2 19:11:52 2023
+++ src/sys/sys/vmem.h	Sat Dec  2 21:02:12 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: vmem.h,v 1.23 2023/12/02 19:11:52 thorpej Exp $	*/
+/*	$NetBSD: vmem.h,v 1.24 2023/12/02 21:02:12 thorpej Exp $	*/
 
 /*-
  * Copyright (c)2006 YAMAMOTO Takashi,
@@ -70,6 +70,7 @@ void		vmem_free(vmem_t *, vmem_addr_t, v
 int		vmem_xalloc(vmem_t *, vmem_size_t, vmem_size_t, vmem_size_t,
 		    vmem_size_t, vmem_addr_t, vmem_addr_t, vm_flag_t,
 		    vmem_addr_t *);
+int		vmem_xalloc_addr(vmem_t *, vmem_addr_t, vmem_size_t, vm_flag_t);
 void		vmem_xfree(vmem_t *, vmem_addr_t, vmem_size_t);
 void		vmem_xfreeall(vmem_t *);
 int		vmem_add(vmem_t *, vmem_addr_t, vmem_size_t, vm_flag_t);

Reply via email to