Sun Dec 3 10:25:02 2017 UTC ()
Pull up following revision(s) (requested by mlelstv in ticket #1521):
	share/man/man9/kmem.9: revision 1.20 via patch
	share/man/man9/vmem.9: revision 1.16
	sys/kern/subr_kmem.c: revision 1.62
	sys/kern/subr_vmem.c: revision 1.94
fix vmem_alloc() to never return an error for VM_SLEEP requests,
thus fixing kmem_alloc() to never return NULL for KM_SLEEP requests.
instead these operations will retry forever, which was the intent.


(snj)
diff -r1.14 -r1.14.4.1 src/share/man/man9/kmem.9
diff -r1.15 -r1.15.8.1 src/share/man/man9/vmem.9
diff -r1.60 -r1.60.2.1 src/sys/kern/subr_kmem.c
diff -r1.92 -r1.92.4.1 src/sys/kern/subr_vmem.c

cvs diff -r1.14 -r1.14.4.1 src/share/man/man9/kmem.9 (expand / switch to unified diff)

--- src/share/man/man9/kmem.9 2013/11/26 20:47:26 1.14
+++ src/share/man/man9/kmem.9 2017/12/03 10:25:02 1.14.4.1
@@ -1,41 +1,41 @@ @@ -1,41 +1,41 @@
1.\" $NetBSD: kmem.9,v 1.14 2013/11/26 20:47:26 rmind Exp $ 1.\" $NetBSD: kmem.9,v 1.14.4.1 2017/12/03 10:25:02 snj Exp $
2.\" 2.\"
3.\" Copyright (c)2006 YAMAMOTO Takashi, 3.\" Copyright (c)2006 YAMAMOTO Takashi,
4.\" All rights reserved. 4.\" All rights reserved.
5.\" 5.\"
6.\" Redistribution and use in source and binary forms, with or without 6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions 7.\" modification, are permitted provided that the following conditions
8.\" are met: 8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright 9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer. 10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the 12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution. 13.\" documentation and/or other materials provided with the distribution.
14.\" 14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE. 25.\" SUCH DAMAGE.
26.\" 26.\"
27.\" ------------------------------------------------------------ 27.\" ------------------------------------------------------------
28.Dd November 26, 2013 28.Dd October 31, 2017
29.Dt KMEM 9 29.Dt KMEM 9
30.Os 30.Os
31.\" ------------------------------------------------------------ 31.\" ------------------------------------------------------------
32.Sh NAME 32.Sh NAME
33.Nm kmem 33.Nm kmem
34.Nd kernel wired memory allocator 34.Nd kernel wired memory allocator
35.\" ------------------------------------------------------------ 35.\" ------------------------------------------------------------
36.Sh SYNOPSIS 36.Sh SYNOPSIS
37.In sys/kmem.h 37.In sys/kmem.h
38.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 38.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39.Ft void * 39.Ft void *
40.Fn kmem_alloc \ 40.Fn kmem_alloc \
41"size_t size" "km_flag_t kmflags" 41"size_t size" "km_flag_t kmflags"
@@ -65,26 +65,29 @@ @@ -65,26 +65,29 @@
65.Sh DESCRIPTION 65.Sh DESCRIPTION
66.Fn kmem_alloc 66.Fn kmem_alloc
67allocates kernel wired memory. 67allocates kernel wired memory.
68It takes the following arguments. 68It takes the following arguments.
69.Bl -tag -width kmflags 69.Bl -tag -width kmflags
70.It Fa size 70.It Fa size
71Specify the size of allocation in bytes. 71Specify the size of allocation in bytes.
72.It Fa kmflags 72.It Fa kmflags
73Either of the following: 73Either of the following:
74.Bl -tag -width KM_NOSLEEP 74.Bl -tag -width KM_NOSLEEP
75.It KM_SLEEP 75.It KM_SLEEP
76If the allocation cannot be satisfied immediately, sleep until enough 76If the allocation cannot be satisfied immediately, sleep until enough
77memory is available. 77memory is available.
 78If
 79.Dv KM_SLEEP
 80is specified, then the allocation cannot fail.
78.It KM_NOSLEEP 81.It KM_NOSLEEP
79Don't sleep. 82Don't sleep.
80Immediately return 83Immediately return
81.Dv NULL 84.Dv NULL
82if there is not enough memory available. 85if there is not enough memory available.
83It should only be used when failure to allocate will not have harmful, 86It should only be used when failure to allocate will not have harmful,
84user-visible effects. 87user-visible effects.
85.Pp 88.Pp
86.Bf -symbolic 89.Bf -symbolic
87Use of 90Use of
88.Dv KM_NOSLEEP 91.Dv KM_NOSLEEP
89is strongly discouraged as it can create transient, hard to debug failures 92is strongly discouraged as it can create transient, hard to debug failures
90that occur when the system is under memory pressure. 93that occur when the system is under memory pressure.

cvs diff -r1.15 -r1.15.8.1 src/share/man/man9/vmem.9 (expand / switch to unified diff)

--- src/share/man/man9/vmem.9 2013/01/29 22:02:17 1.15
+++ src/share/man/man9/vmem.9 2017/12/03 10:25:02 1.15.8.1
@@ -1,41 +1,41 @@ @@ -1,41 +1,41 @@
1.\" $NetBSD: vmem.9,v 1.15 2013/01/29 22:02:17 wiz Exp $ 1.\" $NetBSD: vmem.9,v 1.15.8.1 2017/12/03 10:25:02 snj Exp $
2.\" 2.\"
3.\" Copyright (c)2006 YAMAMOTO Takashi, 3.\" Copyright (c)2006 YAMAMOTO Takashi,
4.\" All rights reserved. 4.\" All rights reserved.
5.\" 5.\"
6.\" Redistribution and use in source and binary forms, with or without 6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions 7.\" modification, are permitted provided that the following conditions
8.\" are met: 8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright 9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer. 10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the 12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution. 13.\" documentation and/or other materials provided with the distribution.
14.\" 14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE. 25.\" SUCH DAMAGE.
26.\" 26.\"
27.\" ------------------------------------------------------------ 27.\" ------------------------------------------------------------
28.Dd January 29, 2013 28.Dd February 28, 2016
29.Dt VMEM 9 29.Dt VMEM 9
30.Os 30.Os
31.\" ------------------------------------------------------------ 31.\" ------------------------------------------------------------
32.Sh NAME 32.Sh NAME
33.Nm vmem 33.Nm vmem
34.Nd virtual memory allocator 34.Nd virtual memory allocator
35.\" ------------------------------------------------------------ 35.\" ------------------------------------------------------------
36.Sh SYNOPSIS 36.Sh SYNOPSIS
37.In sys/vmem.h 37.In sys/vmem.h
38.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 38.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39.Ft vmem_t * 39.Ft vmem_t *
40.Fn vmem_create \ 40.Fn vmem_create \
41"const char *name" "vmem_addr_t base" "vmem_size_t size" "vmem_size_t quantum" \ 41"const char *name" "vmem_addr_t base" "vmem_size_t size" "vmem_size_t quantum" \
@@ -73,27 +73,27 @@ @@ -73,27 +73,27 @@
73.Fn vmem_destroy "vmem_t *vm" 73.Fn vmem_destroy "vmem_t *vm"
74.\" ------------------------------------------------------------ 74.\" ------------------------------------------------------------
75.Sh DESCRIPTION 75.Sh DESCRIPTION
76The 76The
77.Nm 77.Nm
78is a general purpose resource allocator. 78is a general purpose resource allocator.
79Despite its name, it can be used for arbitrary resources 79Despite its name, it can be used for arbitrary resources
80other than virtual memory. 80other than virtual memory.
81.Pp 81.Pp
82.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 82.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
83.Fn vmem_create 83.Fn vmem_create
84creates a new vmem arena. 84creates a new vmem arena.
85.Pp 85.Pp
86.Bl -tag -width qcache_max 86.Bl -tag -offset indent -width qcache_max
87.It Fa name 87.It Fa name
88The string to describe the vmem. 88The string to describe the vmem.
89.It Fa base 89.It Fa base
90The start address of the initial span. 90The start address of the initial span.
91Pass 91Pass
92.Dv 0 92.Dv 0
93if no initial span is required. 93if no initial span is required.
94.It Fa size 94.It Fa size
95The size of the initial span. 95The size of the initial span.
96Pass 96Pass
97.Dv 0 97.Dv 0
98if no initial span is required. 98if no initial span is required.
99.It Fa quantum 99.It Fa quantum
@@ -108,27 +108,27 @@ to @@ -108,27 +108,27 @@ to
108.Dv NULL 108.Dv NULL
109to disable automatic imports. 109to disable automatic imports.
110.Nm 110.Nm
111calls 111calls
112.Fo "(*allocfn)" 112.Fo "(*allocfn)"
113.Fa arg 113.Fa arg
114.Fa size 114.Fa size
115.Fa flags 115.Fa flags
116.Fa "\*[Am]addrp" 116.Fa "\*[Am]addrp"
117.Fc 117.Fc
118to import a span of size at least 118to import a span of size at least
119.Fa size . 119.Fa size .
120.Fa allocfn 120.Fa allocfn
121should accept the same 121must accept the same
122.Fa flags 122.Fa flags
123as 123as
124.Fn vmem_alloc . 124.Fn vmem_alloc .
125.Fa allocfn 125.Fa allocfn
126must return 126must return
127.Dv ENOMEM 127.Dv ENOMEM
128to indicate failure, or 0 on success. 128to indicate failure, or 0 on success.
129If 129If
130.Fa allocfn 130.Fa allocfn
131succeeds, it must write the starting address of the imported span to 131succeeds, it must write the starting address of the imported span to
132.Fa addrp . 132.Fa addrp .
133.It Fa freefn 133.It Fa freefn
134The callback function used to free spans to the backend arena. 134The callback function used to free spans to the backend arena.
@@ -159,42 +159,43 @@ may be @@ -159,42 +159,43 @@ may be
159passes 159passes
160.Fa arg 160.Fa arg
161as the first argument of 161as the first argument of
162.Fa allocfn 162.Fa allocfn
163and 163and
164.Fa freefn . 164.Fa freefn .
165.It Fa qcache_max 165.It Fa qcache_max
166The largest size of allocations which can be served by quantum cache. 166The largest size of allocations which can be served by quantum cache.
167It is merely a hint and can be ignored. 167It is merely a hint and can be ignored.
168.It Fa flags 168.It Fa flags
169Either of: 169Either of:
170.Bl -tag -width VM_NOSLEEP 170.Bl -tag -width VM_NOSLEEP
171.It Dv VM_SLEEP 171.It Dv VM_SLEEP
172Can sleep until enough resources are available. 172If the allocation cannot be satisfied immediately, sleep until enough
 173resources are available.
173.It Dv VM_NOSLEEP 174.It Dv VM_NOSLEEP
174Don't sleep. 175Don't sleep.
175Immediately return 176Immediately return
176.Dv NULL 177.Dv NULL
177if there are not enough resources available. 178if there are not enough resources available.
178.El 179.El
179.It Fa ipl 180.It Fa ipl
180Interrupt level to be blocked for allocating from vmem. 181Interrupt level to be blocked for allocating from vmem.
181.El 182.El
182.Pp 183.Pp
183.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 184.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
184.Fn vmem_xcreate 185.Fn vmem_xcreate
185creates a new vmem arena. 186creates a new vmem arena.
186.Pp 187.Pp
187.Bl -tag -width qcache_max 188.Bl -tag -offset indent -width qcache_max
188.It Fa name 189.It Fa name
189The string to describe the vmem. 190The string to describe the vmem.
190.It Fa base 191.It Fa base
191The start address of the initial span. 192The start address of the initial span.
192Pass 193Pass
193.Dv 0 194.Dv 0
194if no initial span is required. 195if no initial span is required.
195.It Fa size 196.It Fa size
196The size of the initial span. 197The size of the initial span.
197Pass 198Pass
198.Dv 0 199.Dv 0
199if no initial span is required. 200if no initial span is required.
200.It Fa quantum 201.It Fa quantum
@@ -210,27 +211,27 @@ to @@ -210,27 +211,27 @@ to
210to disable automatic imports. 211to disable automatic imports.
211.Nm 212.Nm
212calls 213calls
213.Fo "(*allocfn)" 214.Fo "(*allocfn)"
214.Fa arg 215.Fa arg
215.Fa size 216.Fa size
216.Fa "\*[Am]actualsize" 217.Fa "\*[Am]actualsize"
217.Fa flags 218.Fa flags
218.Fa "\*[Am]addrp" 219.Fa "\*[Am]addrp"
219.Fc 220.Fc
220to import a span of size at least 221to import a span of size at least
221.Fa size . 222.Fa size .
222.Fa allocfn 223.Fa allocfn
223should accept the same 224must accept the same
224.Fa flags 225.Fa flags
225as 226as
226.Fn vmem_alloc . 227.Fn vmem_alloc .
227.Fa allocfn 228.Fa allocfn
228must return 229must return
229.Dv ENOMEM 230.Dv ENOMEM
230to indicate failure, or 0 on success. 231to indicate failure, or 0 on success.
231If 232If
232.Fa allocfn 233.Fa allocfn
233succeeds, it must write the actual size of the allocation to 234succeeds, it must write the actual size of the allocation to
234.Fa actualsize 235.Fa actualsize
235and the starting address of the imported span to 236and the starting address of the imported span to
236.Fa addrp . 237.Fa addrp .
@@ -264,227 +265,233 @@ may be @@ -264,227 +265,233 @@ may be
264passes 265passes
265.Fa arg 266.Fa arg
266as the first argument of 267as the first argument of
267.Fa allocfn 268.Fa allocfn
268and 269and
269.Fa freefn . 270.Fa freefn .
270.It Fa qcache_max 271.It Fa qcache_max
271The largest size of allocations which can be served by quantum cache. 272The largest size of allocations which can be served by quantum cache.
272It is merely a hint and can be ignored. 273It is merely a hint and can be ignored.
273.It Fa flags 274.It Fa flags
274Either of: 275Either of:
275.Bl -tag -width VM_NOSLEEP 276.Bl -tag -width VM_NOSLEEP
276.It Dv VM_SLEEP 277.It Dv VM_SLEEP
277Can sleep until enough resources are available. 278If the allocation cannot be satisfied immediately, sleep until enough
 279resources are available.
278.It Dv VM_NOSLEEP 280.It Dv VM_NOSLEEP
279Don't sleep. 281Don't sleep.
280Immediately return 282Immediately return
281.Dv NULL 283.Dv NULL
282if there are not enough resources available. 284if there are not enough resources available.
283.El 285.El
284.It Fa ipl 286.It Fa ipl
285Interrupt level to be blocked for allocating from vmem. 287Interrupt level to be blocked for allocating from vmem.
286.El 288.El
287.Pp 289.Pp
288.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 290.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
289.Fn vmem_add 291.Fn vmem_add
290adds a span of size 292adds a span of size
291.Fa size 293.Fa size
292starting at 294starting at
293.Fa addr 295.Fa addr
294to the arena. 296to the arena.
295Returns 297Returns
2960 2980
297on success, 299on success,
298.Dv ENOMEM 300.Dv ENOMEM
299on failure. 301on failure.
300.Fa flags 302.Bl -tag -offset indent -width flags
301should be one of: 303.It Fa flags
 304Either of:
302.Bl -tag -width VM_NOSLEEP 305.Bl -tag -width VM_NOSLEEP
303.It Dv VM_SLEEP 306.It Dv VM_SLEEP
304Can sleep until enough resources are available. 307If the allocation cannot be satisfied immediately, sleep until enough
 308resources are available.
305.It Dv VM_NOSLEEP 309.It Dv VM_NOSLEEP
306Don't sleep. 310Don't sleep.
307Immediately return 311Immediately return
308.Dv ENOMEM 312.Dv ENOMEM
309if there are not enough resources available. 313if there are not enough resources available.
310.El 314.El
 315.El
311.Pp 316.Pp
312.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 317.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
313.Fn vmem_xalloc 318.Fn vmem_xalloc
314allocates a resource from the arena. 319allocates a resource from the arena.
315.Pp 320.Pp
316.Bl -tag -width nocross 321.Bl -tag -offset indent -width nocross
317.It Fa vm 322.It Fa vm
318The arena which we allocate from. 323The arena which we allocate from.
319.It Fa size 324.It Fa size
320Specify the size of the allocation. 325Specify the size of the allocation.
321.It Fa align 326.It Fa align
322If zero, don't care about the alignment of the allocation. 327If zero, don't care about the alignment of the allocation.
323Otherwise, request a resource segment starting at 328Otherwise, request a resource segment starting at
324offset 329offset
325.Fa phase 330.Fa phase
326from an 331from an
327.Fa align 332.Fa align
328aligned boundary. 333aligned boundary.
329.It Fa phase 334.It Fa phase
330See the above description of 335See the above description of
331.Fa align . 336.Fa align .
332If 337If
333.Fa align 338.Fa align
334is zero, 339is zero,
335.Fa phase 340.Fa phase
336should be zero. 341must be zero.
337Otherwise, 342Otherwise,
338.Fa phase 343.Fa phase
339should be smaller than 344must be smaller than
340.Fa align . 345.Fa align .
341.It Fa nocross 346.It Fa nocross
342Request a resource which doesn't cross 347Request a resource which doesn't cross
343.Fa nocross 348.Fa nocross
344aligned boundary. 349aligned boundary.
345.It Fa minaddr 350.It Fa minaddr
346Specify the minimum address which can be allocated, or 351Specify the minimum address which can be allocated, or
347.Dv VMEM_ADDR_MIN 352.Dv VMEM_ADDR_MIN
348if the caller does not care. 353if the caller does not care.
349.It Fa maxaddr 354.It Fa maxaddr
350Specify the maximum address which can be allocated, or 355Specify the maximum address which can be allocated, or
351.Dv VMEM_ADDR_MAX 356.Dv VMEM_ADDR_MAX
352if the caller does not care. 357if the caller does not care.
353.It Fa flags 358.It Fa flags
354A bitwise OR of an allocation strategy and a sleep flag. 359A bitwise OR of an allocation strategy and a sleep flag.
355.Pp 360.Pp
356The allocation strategy is one of: 361The allocation strategy must be one of:
357.Bl -tag -width VM_INSTANTFIT 362.Bl -tag -width VM_INSTANTFIT
358.It Dv VM_BESTFIT 363.It Dv VM_BESTFIT
359Prefer space efficiency. 364Prefer space efficiency.
360.It Dv VM_INSTANTFIT 365.It Dv VM_INSTANTFIT
361Prefer performance. 366Prefer performance.
362.El 367.El
363.Pp 368.Pp
364The sleep flag should be one of: 369The sleep flag must be one of:
365.Bl -tag -width VM_NOSLEEP 370.Bl -tag -width VM_NOSLEEP
366.It Dv VM_SLEEP 371.It Dv VM_SLEEP
367Can sleep until enough resources are available. 372If the allocation cannot be satisfied immediately, sleep until enough
 373resources are available.
368.It Dv VM_NOSLEEP 374.It Dv VM_NOSLEEP
369Don't sleep. 375Don't sleep.
370Immediately return 376Immediately return
371.Dv ENOMEM 377.Dv ENOMEM
372if there are not enough resources available. 378if there are not enough resources available.
373.El 379.El
374.It Fa addrp 380.It Fa addrp
375On success, if 381On success, if
376.Fa addrp 382.Fa addrp
377is not 383is not
378.Dv NULL , 384.Dv NULL ,
379.Fn vmem_xalloc 385.Fn vmem_xalloc
380overwrites it with the start address of the allocated span. 386overwrites it with the start address of the allocated span.
381.El 387.El
382.Pp 388.Pp
383.\" ------------------------------------------------------------ 389.\" ------------------------------------------------------------
384.Fn vmem_xfree 390.Fn vmem_xfree
385frees resource allocated by 391frees resource allocated by
386.Fn vmem_xalloc 392.Fn vmem_xalloc
387to the arena. 393to the arena.
388.Pp 394.Pp
389.Bl -tag -width addr 395.Bl -tag -offset indent -width addr
390.It Fa vm 396.It Fa vm
391The arena which we free to. 397The arena which we free to.
392.It Fa addr 398.It Fa addr
393The resource being freed. 399The resource being freed.
394It must be the one returned by 400It must have been allocated via
395.Fn vmem_xalloc . 401.Fn vmem_xalloc .
396Notably, it must not be the one from 402Notably, it must not have been allocated via
397.Fn vmem_alloc . 403.Fn vmem_alloc .
398Otherwise, the behaviour is undefined. 404Otherwise, the behaviour is undefined.
399.It Fa size 405.It Fa size
400The size of the resource being freed. 406The size of the resource being freed.
401It must be the same as the 407It must be the same as the
402.Fa size 408.Fa size
403argument used for 409argument used for
404.Fn vmem_xalloc . 410.Fn vmem_xalloc .
405.El 411.El
406.Pp 412.Pp
407.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 413.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
408.Fn vmem_alloc 414.Fn vmem_alloc
409allocates a resource from the arena. 415allocates a resource from the arena.
410.Pp 416.Pp
411.Bl -tag -width flags 417.Bl -tag -offset indent -width flags
412.It Fa vm 418.It Fa vm
413The arena which we allocate from. 419The arena which we allocate from.
414.It Fa size 420.It Fa size
415Specify the size of the allocation. 421Specify the size of the allocation.
416.It Fa flags 422.It Fa flags
417A bitwise OR of an allocation strategy and a sleep flag. 423A bitwise OR of an allocation strategy and a sleep flag.
418.Pp 424.Pp
419The allocation strategy is one of: 425The allocation strategy must be one of:
420.Bl -tag -width VM_INSTANTFIT 426.Bl -tag -width VM_INSTANTFIT
421.It Dv VM_BESTFIT 427.It Dv VM_BESTFIT
422Prefer space efficiency. 428Prefer space efficiency.
423.It Dv VM_INSTANTFIT 429.It Dv VM_INSTANTFIT
424Prefer performance. 430Prefer performance.
425.El 431.El
426.Pp 432.Pp
427The sleep flag should be one of: 433The sleep flag must be one of:
428.Bl -tag -width VM_NOSLEEP 434.Bl -tag -width VM_NOSLEEP
429.It Dv VM_SLEEP 435.It Dv VM_SLEEP
430Can sleep until enough resources are available. 436If the allocation cannot be satisfied immediately, sleep until enough
 437resources are available.
431.It Dv VM_NOSLEEP 438.It Dv VM_NOSLEEP
432Don't sleep. 439Don't sleep.
433Immediately return 440Immediately return
434.Dv ENOMEM 441.Dv ENOMEM
435if there are not enough resources available. 442if there are not enough resources available.
436.El 443.El
437.It Fa addrp 444.It Fa addrp
438On success, if 445On success, if
439.Fa addrp 446.Fa addrp
440is not 447is not
441.Dv NULL , 448.Dv NULL ,
442.Fn vmem_alloc 449.Fn vmem_alloc
443overwrites it with the start address of the allocated span. 450overwrites it with the start address of the allocated span.
444.El 451.El
445.Pp 452.Pp
446.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 453.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
447.Fn vmem_free 454.Fn vmem_free
448frees resource allocated by 455frees resource allocated by
449.Fn vmem_alloc 456.Fn vmem_alloc
450to the arena. 457to the arena.
451.Pp 458.Pp
452.Bl -tag -width addr 459.Bl -tag -offset indent -width addr
453.It Fa vm 460.It Fa vm
454The arena which we free to. 461The arena which we free to.
455.It Fa addr 462.It Fa addr
456The resource being freed. 463The resource being freed.
457It must be the one returned by 464It must have been allocated via
458.Fn vmem_alloc . 465.Fn vmem_alloc .
459Notably, it must not be the one from 466Notably, it must not have been allocated via
460.Fn vmem_xalloc . 467.Fn vmem_xalloc .
461Otherwise, the behaviour is undefined. 468Otherwise, the behaviour is undefined.
462.It Fa size 469.It Fa size
463The size of the resource being freed. 470The size of the resource being freed.
464It must be the same as the 471It must be the same as the
465.Fa size 472.Fa size
466argument used for 473argument used for
467.Fn vmem_alloc . 474.Fn vmem_alloc .
468.El 475.El
469.Pp 476.Pp
470.\" ------------------------------------------------------------ 477.\" ------------------------------------------------------------
471.Fn vmem_destroy 478.Fn vmem_destroy
472destroys a vmem arena. 479destroys a vmem arena.
473.Pp 480.Pp
474.Bl -tag -width vm 481.Bl -tag -offset indent -width vm
475.It Fa vm 482.It Fa vm
476The vmem arena being destroyed. 483The vmem arena being destroyed.
477The caller should ensure that no one will use it anymore. 484The caller must ensure that no one will use it anymore.
478.El 485.El
479.\" ------------------------------------------------------------ 486.\" ------------------------------------------------------------
480.Sh RETURN VALUES 487.Sh RETURN VALUES
481.Fn vmem_create 488.Fn vmem_create
482return a pointer to the newly allocated vmem_t. 489return a pointer to the newly allocated vmem_t.
483Otherwise, it returns 490Otherwise, it returns
484.Dv NULL . 491.Dv NULL .
485.Pp 492.Pp
486On success, 493On success,
487.Fn vmem_xalloc 494.Fn vmem_xalloc
488and 495and
489.Fn vmem_alloc 496.Fn vmem_alloc
490return 0. 497return 0.

cvs diff -r1.60 -r1.60.2.1 src/sys/kern/subr_kmem.c (expand / switch to unified diff)

--- src/sys/kern/subr_kmem.c 2014/07/22 07:38:41 1.60
+++ src/sys/kern/subr_kmem.c 2017/12/03 10:25:02 1.60.2.1
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: subr_kmem.c,v 1.60 2014/07/22 07:38:41 maxv Exp $ */ 1/* $NetBSD: subr_kmem.c,v 1.60.2.1 2017/12/03 10:25:02 snj Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc. 4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran. 8 * by Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -90,27 +90,27 @@ @@ -90,27 +90,27 @@
90 * A kernel with "option DEBUG" has "kmguard" debugging feature compiled 90 * A kernel with "option DEBUG" has "kmguard" debugging feature compiled
91 * in. See the comment in uvm/uvm_kmguard.c for what kind of bugs it tries 91 * in. See the comment in uvm/uvm_kmguard.c for what kind of bugs it tries
92 * to detect. Even if compiled in, it's disabled by default because it's 92 * to detect. Even if compiled in, it's disabled by default because it's
93 * very expensive. You can enable it on boot by: 93 * very expensive. You can enable it on boot by:
94 * boot -d 94 * boot -d
95 * db> w kmem_guard_depth 0t30000 95 * db> w kmem_guard_depth 0t30000
96 * db> c 96 * db> c
97 * 97 *
98 * The default value of kmem_guard_depth is 0, which means disabled. 98 * The default value of kmem_guard_depth is 0, which means disabled.
99 * It can be changed by KMEM_GUARD_DEPTH kernel config option. 99 * It can be changed by KMEM_GUARD_DEPTH kernel config option.
100 */ 100 */
101 101
102#include <sys/cdefs.h> 102#include <sys/cdefs.h>
103__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.60 2014/07/22 07:38:41 maxv Exp $"); 103__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.60.2.1 2017/12/03 10:25:02 snj Exp $");
104 104
105#include <sys/param.h> 105#include <sys/param.h>
106#include <sys/callback.h> 106#include <sys/callback.h>
107#include <sys/kmem.h> 107#include <sys/kmem.h>
108#include <sys/pool.h> 108#include <sys/pool.h>
109#include <sys/debug.h> 109#include <sys/debug.h>
110#include <sys/lockdebug.h> 110#include <sys/lockdebug.h>
111#include <sys/cpu.h> 111#include <sys/cpu.h>
112 112
113#include <uvm/uvm_extern.h> 113#include <uvm/uvm_extern.h>
114#include <uvm/uvm_map.h> 114#include <uvm/uvm_map.h>
115#include <uvm/uvm_kmguard.h> 115#include <uvm/uvm_kmguard.h>
116 116
@@ -363,43 +363,51 @@ kmem_intr_free(void *p, size_t requested @@ -363,43 +363,51 @@ kmem_intr_free(void *p, size_t requested
363} 363}
364 364
365/* ---- kmem API */ 365/* ---- kmem API */
366 366
367/* 367/*
368 * kmem_alloc: allocate wired memory. 368 * kmem_alloc: allocate wired memory.
369 * => must not be called from interrupt context. 369 * => must not be called from interrupt context.
370 */ 370 */
371 371
372void * 372void *
373kmem_alloc(size_t size, km_flag_t kmflags) 373kmem_alloc(size_t size, km_flag_t kmflags)
374{ 374{
375 375
 376 void *v;
 377
376 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()), 378 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
377 "kmem(9) should not be used from the interrupt context"); 379 "kmem(9) should not be used from the interrupt context");
378 return kmem_intr_alloc(size, kmflags); 380 v = kmem_intr_alloc(size, kmflags);
 381 KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
 382 return v;
379} 383}
380 384
381/* 385/*
382 * kmem_zalloc: allocate zeroed wired memory. 386 * kmem_zalloc: allocate zeroed wired memory.
383 * => must not be called from interrupt context. 387 * => must not be called from interrupt context.
384 */ 388 */
385 389
386void * 390void *
387kmem_zalloc(size_t size, km_flag_t kmflags) 391kmem_zalloc(size_t size, km_flag_t kmflags)
388{ 392{
389 393
 394 void *v;
 395
390 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()), 396 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
391 "kmem(9) should not be used from the interrupt context"); 397 "kmem(9) should not be used from the interrupt context");
392 return kmem_intr_zalloc(size, kmflags); 398 v = kmem_intr_zalloc(size, kmflags);
 399 KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
 400 return v;
393} 401}
394 402
395/* 403/*
396 * kmem_free: free wired memory allocated by kmem_alloc. 404 * kmem_free: free wired memory allocated by kmem_alloc.
397 * => must not be called from interrupt context. 405 * => must not be called from interrupt context.
398 */ 406 */
399 407
400void 408void
401kmem_free(void *p, size_t size) 409kmem_free(void *p, size_t size)
402{ 410{
403 411
404 KASSERT(!cpu_intr_p()); 412 KASSERT(!cpu_intr_p());
405 KASSERT(!cpu_softintr_p()); 413 KASSERT(!cpu_softintr_p());

cvs diff -r1.92 -r1.92.4.1 src/sys/kern/subr_vmem.c (expand / switch to unified diff)

--- src/sys/kern/subr_vmem.c 2014/04/02 18:09:10 1.92
+++ src/sys/kern/subr_vmem.c 2017/12/03 10:25:02 1.92.4.1
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: subr_vmem.c,v 1.92 2014/04/02 18:09:10 para Exp $ */ 1/* $NetBSD: subr_vmem.c,v 1.92.4.1 2017/12/03 10:25:02 snj Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c)2006,2007,2008,2009 YAMAMOTO Takashi, 4 * Copyright (c)2006,2007,2008,2009 YAMAMOTO Takashi,
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -36,27 +36,27 @@ @@ -36,27 +36,27 @@
36 * - A pool(9) is used for vmem boundary tags 36 * - A pool(9) is used for vmem boundary tags
37 * - During a pool get call the global vmem_btag_refill_lock is taken, 37 * - During a pool get call the global vmem_btag_refill_lock is taken,
38 * to serialize access to the allocation reserve, but no other 38 * to serialize access to the allocation reserve, but no other
39 * vmem arena locks. 39 * vmem arena locks.
40 * - During pool_put calls no vmem mutexes are locked. 40 * - During pool_put calls no vmem mutexes are locked.
41 * - pool_drain doesn't hold the pool's mutex while releasing memory to 41 * - pool_drain doesn't hold the pool's mutex while releasing memory to
42 * its backing therefore no interferance with any vmem mutexes. 42 * its backing therefore no interferance with any vmem mutexes.
43 * - The boundary tag pool is forced to put page headers into pool pages 43 * - The boundary tag pool is forced to put page headers into pool pages
44 * (PR_PHINPAGE) and not off page to avoid pool recursion. 44 * (PR_PHINPAGE) and not off page to avoid pool recursion.
45 * (due to sizeof(bt_t) it should be the case anyway) 45 * (due to sizeof(bt_t) it should be the case anyway)
46 */ 46 */
47 47
48#include <sys/cdefs.h> 48#include <sys/cdefs.h>
49__KERNEL_RCSID(0, "$NetBSD: subr_vmem.c,v 1.92 2014/04/02 18:09:10 para Exp $"); 49__KERNEL_RCSID(0, "$NetBSD: subr_vmem.c,v 1.92.4.1 2017/12/03 10:25:02 snj Exp $");
50 50
51#if defined(_KERNEL) 51#if defined(_KERNEL)
52#include "opt_ddb.h" 52#include "opt_ddb.h"
53#endif /* defined(_KERNEL) */ 53#endif /* defined(_KERNEL) */
54 54
55#include <sys/param.h> 55#include <sys/param.h>
56#include <sys/hash.h> 56#include <sys/hash.h>
57#include <sys/queue.h> 57#include <sys/queue.h>
58#include <sys/bitops.h> 58#include <sys/bitops.h>
59 59
60#if defined(_KERNEL) 60#if defined(_KERNEL)
61#include <sys/systm.h> 61#include <sys/systm.h>
62#include <sys/kernel.h> /* hz */ 62#include <sys/kernel.h> /* hz */
@@ -184,29 +184,39 @@ static struct vmem_btag static_bts[STATI @@ -184,29 +184,39 @@ static struct vmem_btag static_bts[STATI
184static int static_bt_count = STATIC_BT_COUNT; 184static int static_bt_count = STATIC_BT_COUNT;
185 185
186static struct vmem kmem_va_meta_arena_store; 186static struct vmem kmem_va_meta_arena_store;
187vmem_t *kmem_va_meta_arena; 187vmem_t *kmem_va_meta_arena;
188static struct vmem kmem_meta_arena_store; 188static struct vmem kmem_meta_arena_store;
189vmem_t *kmem_meta_arena = NULL; 189vmem_t *kmem_meta_arena = NULL;
190 190
191static kmutex_t vmem_btag_refill_lock; 191static kmutex_t vmem_btag_refill_lock;
192static kmutex_t vmem_btag_lock; 192static kmutex_t vmem_btag_lock;
193static LIST_HEAD(, vmem_btag) vmem_btag_freelist; 193static LIST_HEAD(, vmem_btag) vmem_btag_freelist;
194static size_t vmem_btag_freelist_count = 0; 194static size_t vmem_btag_freelist_count = 0;
195static struct pool vmem_btag_pool; 195static struct pool vmem_btag_pool;
196 196
 197static void
 198vmem_kick_pdaemon(void)
 199{
 200#if defined(_KERNEL)
 201 mutex_spin_enter(&uvm_fpageqlock);
 202 uvm_kick_pdaemon();
 203 mutex_spin_exit(&uvm_fpageqlock);
 204#endif
 205}
 206
197/* ---- boundary tag */ 207/* ---- boundary tag */
198 208
199static int bt_refill(vmem_t *vm, vm_flag_t flags); 209static int bt_refill(vmem_t *vm);
200 210
201static void * 211static void *
202pool_page_alloc_vmem_meta(struct pool *pp, int flags) 212pool_page_alloc_vmem_meta(struct pool *pp, int flags)
203{ 213{
204 const vm_flag_t vflags = (flags & PR_WAITOK) ? VM_SLEEP: VM_NOSLEEP; 214 const vm_flag_t vflags = (flags & PR_WAITOK) ? VM_SLEEP: VM_NOSLEEP;
205 vmem_addr_t va; 215 vmem_addr_t va;
206 int ret; 216 int ret;
207 217
208 ret = vmem_alloc(kmem_meta_arena, pp->pr_alloc->pa_pagesz, 218 ret = vmem_alloc(kmem_meta_arena, pp->pr_alloc->pa_pagesz,
209 (vflags & ~VM_FITMASK) | VM_INSTANTFIT | VM_POPULATING, &va); 219 (vflags & ~VM_FITMASK) | VM_INSTANTFIT | VM_POPULATING, &va);
210 220
211 return ret ? NULL : (void *)va; 221 return ret ? NULL : (void *)va;
212} 222}
@@ -216,32 +226,30 @@ pool_page_free_vmem_meta(struct pool *pp @@ -216,32 +226,30 @@ pool_page_free_vmem_meta(struct pool *pp
216{ 226{
217 227
218 vmem_free(kmem_meta_arena, (vmem_addr_t)v, pp->pr_alloc->pa_pagesz); 228 vmem_free(kmem_meta_arena, (vmem_addr_t)v, pp->pr_alloc->pa_pagesz);
219} 229}
220 230
221/* allocator for vmem-pool metadata */ 231/* allocator for vmem-pool metadata */
222struct pool_allocator pool_allocator_vmem_meta = { 232struct pool_allocator pool_allocator_vmem_meta = {
223 .pa_alloc = pool_page_alloc_vmem_meta, 233 .pa_alloc = pool_page_alloc_vmem_meta,
224 .pa_free = pool_page_free_vmem_meta, 234 .pa_free = pool_page_free_vmem_meta,
225 .pa_pagesz = 0 235 .pa_pagesz = 0
226}; 236};
227 237
228static int 238static int
229bt_refill(vmem_t *vm, vm_flag_t flags) 239bt_refill(vmem_t *vm)
230{ 240{
231 bt_t *bt; 241 bt_t *bt;
232 242
233 KASSERT(flags & VM_NOSLEEP); 
234 
235 VMEM_LOCK(vm); 243 VMEM_LOCK(vm);
236 if (vm->vm_nfreetags > BT_MINRESERVE) { 244 if (vm->vm_nfreetags > BT_MINRESERVE) {
237 VMEM_UNLOCK(vm); 245 VMEM_UNLOCK(vm);
238 return 0; 246 return 0;
239 } 247 }
240 248
241 mutex_enter(&vmem_btag_lock); 249 mutex_enter(&vmem_btag_lock);
242 while (!LIST_EMPTY(&vmem_btag_freelist) && 250 while (!LIST_EMPTY(&vmem_btag_freelist) &&
243 vm->vm_nfreetags <= BT_MINRESERVE) { 251 vm->vm_nfreetags <= BT_MINRESERVE) {
244 bt = LIST_FIRST(&vmem_btag_freelist); 252 bt = LIST_FIRST(&vmem_btag_freelist);
245 LIST_REMOVE(bt, bt_freelist); 253 LIST_REMOVE(bt, bt_freelist);
246 LIST_INSERT_HEAD(&vm->vm_freetags, bt, bt_freelist); 254 LIST_INSERT_HEAD(&vm->vm_freetags, bt, bt_freelist);
247 vm->vm_nfreetags++; 255 vm->vm_nfreetags++;
@@ -260,46 +268,57 @@ bt_refill(vmem_t *vm, vm_flag_t flags) @@ -260,46 +268,57 @@ bt_refill(vmem_t *vm, vm_flag_t flags)
260 break; 268 break;
261 LIST_INSERT_HEAD(&vm->vm_freetags, bt, bt_freelist); 269 LIST_INSERT_HEAD(&vm->vm_freetags, bt, bt_freelist);
262 vm->vm_nfreetags++; 270 vm->vm_nfreetags++;
263 } 271 }
264 272
265 if (vm->vm_nfreetags <= BT_MINRESERVE) { 273 if (vm->vm_nfreetags <= BT_MINRESERVE) {
266 VMEM_UNLOCK(vm); 274 VMEM_UNLOCK(vm);
267 return ENOMEM; 275 return ENOMEM;
268 } 276 }
269 277
270 VMEM_UNLOCK(vm); 278 VMEM_UNLOCK(vm);
271 279
272 if (kmem_meta_arena != NULL) { 280 if (kmem_meta_arena != NULL) {
273 bt_refill(kmem_arena, (flags & ~VM_FITMASK) 281 (void)bt_refill(kmem_arena);
274 | VM_INSTANTFIT | VM_POPULATING); 282 (void)bt_refill(kmem_va_meta_arena);
275 bt_refill(kmem_va_meta_arena, (flags & ~VM_FITMASK) 283 (void)bt_refill(kmem_meta_arena);
276 | VM_INSTANTFIT | VM_POPULATING); 
277 bt_refill(kmem_meta_arena, (flags & ~VM_FITMASK) 
278 | VM_INSTANTFIT | VM_POPULATING); 
279 } 284 }
280 285
281 return 0; 286 return 0;
282} 287}
283 288
284static bt_t * 289static bt_t *
285bt_alloc(vmem_t *vm, vm_flag_t flags) 290bt_alloc(vmem_t *vm, vm_flag_t flags)
286{ 291{
287 bt_t *bt; 292 bt_t *bt;
288 VMEM_LOCK(vm); 293 VMEM_LOCK(vm);
289 while (vm->vm_nfreetags <= BT_MINRESERVE && (flags & VM_POPULATING) == 0) { 294 while (vm->vm_nfreetags <= BT_MINRESERVE && (flags & VM_POPULATING) == 0) {
290 VMEM_UNLOCK(vm); 295 VMEM_UNLOCK(vm);
291 if (bt_refill(vm, VM_NOSLEEP | VM_INSTANTFIT)) { 296 if (bt_refill(vm)) {
292 return NULL; 297 if ((flags & VM_NOSLEEP) != 0) {
 298 return NULL;
 299 }
 300
 301 /*
 302 * It would be nice to wait for something specific here
 303 * but there are multiple ways that a retry could
 304 * succeed and we can't wait for multiple things
 305 * simultaneously. So we'll just sleep for an arbitrary
 306 * short period of time and retry regardless.
 307 * This should be a very rare case.
 308 */
 309
 310 vmem_kick_pdaemon();
 311 kpause("btalloc", false, 1, NULL);
293 } 312 }
294 VMEM_LOCK(vm); 313 VMEM_LOCK(vm);
295 } 314 }
296 bt = LIST_FIRST(&vm->vm_freetags); 315 bt = LIST_FIRST(&vm->vm_freetags);
297 LIST_REMOVE(bt, bt_freelist); 316 LIST_REMOVE(bt, bt_freelist);
298 vm->vm_nfreetags--; 317 vm->vm_nfreetags--;
299 VMEM_UNLOCK(vm); 318 VMEM_UNLOCK(vm);
300 319
301 return bt; 320 return bt;
302} 321}
303 322
304static void 323static void
305bt_free(vmem_t *vm, bt_t *bt) 324bt_free(vmem_t *vm, bt_t *bt)
@@ -930,27 +949,27 @@ vmem_init(vmem_t *vm, const char *name, @@ -930,27 +949,27 @@ vmem_init(vmem_t *vm, const char *name,
930 memset(&vm->vm_hash0, 0, sizeof(struct vmem_hashlist)); 949 memset(&vm->vm_hash0, 0, sizeof(struct vmem_hashlist));
931 vm->vm_hashsize = 1; 950 vm->vm_hashsize = 1;
932 vm->vm_hashlist = &vm->vm_hash0; 951 vm->vm_hashlist = &vm->vm_hash0;
933 952
934 if (size != 0) { 953 if (size != 0) {
935 if (vmem_add(vm, base, size, flags) != 0) { 954 if (vmem_add(vm, base, size, flags) != 0) {
936 vmem_destroy1(vm); 955 vmem_destroy1(vm);
937 return NULL; 956 return NULL;
938 } 957 }
939 } 958 }
940 959
941#if defined(_KERNEL) 960#if defined(_KERNEL)
942 if (flags & VM_BOOTSTRAP) { 961 if (flags & VM_BOOTSTRAP) {
943 bt_refill(vm, VM_NOSLEEP); 962 bt_refill(vm);
944 } 963 }
945 964
946 mutex_enter(&vmem_list_lock); 965 mutex_enter(&vmem_list_lock);
947 LIST_INSERT_HEAD(&vmem_list, vm, vm_alllist); 966 LIST_INSERT_HEAD(&vmem_list, vm, vm_alllist);
948 mutex_exit(&vmem_list_lock); 967 mutex_exit(&vmem_list_lock);
949#endif /* defined(_KERNEL) */ 968#endif /* defined(_KERNEL) */
950 969
951 return vm; 970 return vm;
952} 971}
953 972
954 973
955 974
956/* 975/*
@@ -1167,31 +1186,27 @@ retry: @@ -1167,31 +1186,27 @@ retry:
1167 * XXX should try to import a region large enough to 1186 * XXX should try to import a region large enough to
1168 * satisfy restrictions? 1187 * satisfy restrictions?
1169 */ 1188 */
1170 1189
1171 goto fail; 1190 goto fail;
1172 } 1191 }
1173 /* XXX eeek, minaddr & maxaddr not respected */ 1192 /* XXX eeek, minaddr & maxaddr not respected */
1174 if (vmem_import(vm, size, flags) == 0) { 1193 if (vmem_import(vm, size, flags) == 0) {
1175 goto retry; 1194 goto retry;
1176 } 1195 }
1177 /* XXX */ 1196 /* XXX */
1178 1197
1179 if ((flags & VM_SLEEP) != 0) { 1198 if ((flags & VM_SLEEP) != 0) {
1180#if defined(_KERNEL) 1199 vmem_kick_pdaemon();
1181 mutex_spin_enter(&uvm_fpageqlock); 
1182 uvm_kick_pdaemon(); 
1183 mutex_spin_exit(&uvm_fpageqlock); 
1184#endif 
1185 VMEM_LOCK(vm); 1200 VMEM_LOCK(vm);
1186 VMEM_CONDVAR_WAIT(vm); 1201 VMEM_CONDVAR_WAIT(vm);
1187 VMEM_UNLOCK(vm); 1202 VMEM_UNLOCK(vm);
1188 goto retry; 1203 goto retry;
1189 } 1204 }
1190fail: 1205fail:
1191 bt_free(vm, btnew); 1206 bt_free(vm, btnew);
1192 bt_free(vm, btnew2); 1207 bt_free(vm, btnew2);
1193 return ENOMEM; 1208 return ENOMEM;
1194 1209
1195gotit: 1210gotit:
1196 KASSERT(bt->bt_type == BT_TYPE_FREE); 1211 KASSERT(bt->bt_type == BT_TYPE_FREE);
1197 KASSERT(bt->bt_size >= size); 1212 KASSERT(bt->bt_size >= size);