Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src fix vmem_alloc() to never return an error for VM_SLEEP reque...
details: https://anonhg.NetBSD.org/src/rev/d2563916c1e2
branches: trunk
changeset: 343860:d2563916c1e2
user: chs <chs%NetBSD.org@localhost>
date: Mon Feb 29 00:34:17 2016 +0000
description:
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.
diffstat:
share/man/man9/kmem.9 | 19 ++------------
share/man/man9/vmem.9 | 65 ++++++++++++++++++++++++++++----------------------
sys/kern/subr_kmem.c | 16 +++++++++---
sys/kern/subr_vmem.c | 55 +++++++++++++++++++++++++++---------------
4 files changed, 86 insertions(+), 69 deletions(-)
diffs (truncated from 442 to 300 lines):
diff -r b39944709569 -r d2563916c1e2 share/man/man9/kmem.9
--- a/share/man/man9/kmem.9 Mon Feb 29 00:17:54 2016 +0000
+++ b/share/man/man9/kmem.9 Mon Feb 29 00:34:17 2016 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: kmem.9,v 1.19 2015/12/11 10:05:17 wiz Exp $
+.\" $NetBSD: kmem.9,v 1.20 2016/02/29 00:34:17 chs Exp $
.\"
.\" Copyright (c)2006 YAMAMOTO Takashi,
.\" All rights reserved.
@@ -25,7 +25,7 @@
.\" SUCH DAMAGE.
.\"
.\" ------------------------------------------------------------
-.Dd December 10, 2015
+.Dd February 28, 2016
.Dt KMEM 9
.Os
.\" ------------------------------------------------------------
@@ -77,15 +77,9 @@
.It Dv KM_SLEEP
If the allocation cannot be satisfied immediately, sleep until enough
memory is available.
-Note that this does not mean that if
+If
.Dv KM_SLEEP
is specified, then the allocation cannot fail.
-Under resource stress conditions, the allocation can fail and the
-function will return
-.Dv NULL .
-One such scenario is when the allocation size is larger than it can ever
-be allocated; another is when the system memory resources are exhausted
-to even allocate pools of pages.
.It Dv KM_NOSLEEP
Don't sleep.
Immediately return
@@ -143,9 +137,6 @@
.Fn kmem_alloc
or
.Fn kmem_zalloc .
-One such scenario is when the allocation size is larger than it can ever
-be allocated; another is when the system memory resources are exhausted
-to even allocate pools of pages.
.It Fa size
The size of the memory being freed, in bytes.
It must be the same as the
@@ -185,10 +176,6 @@
.Fn kmem_free
may also block.
.Pp
-Always check the return value of the allocators, even when
-.Dv KM_SLEEP
-is specified to avoid kernel crashes during resource stress conditions.
-.Pp
For some locks this is permissible or even unavoidable.
For others, particularly locks that may be taken from soft interrupt context,
it is a serious problem.
diff -r b39944709569 -r d2563916c1e2 share/man/man9/vmem.9
--- a/share/man/man9/vmem.9 Mon Feb 29 00:17:54 2016 +0000
+++ b/share/man/man9/vmem.9 Mon Feb 29 00:34:17 2016 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: vmem.9,v 1.15 2013/01/29 22:02:17 wiz Exp $
+.\" $NetBSD: vmem.9,v 1.16 2016/02/29 00:34:17 chs Exp $
.\"
.\" Copyright (c)2006 YAMAMOTO Takashi,
.\" All rights reserved.
@@ -25,7 +25,7 @@
.\" SUCH DAMAGE.
.\"
.\" ------------------------------------------------------------
-.Dd January 29, 2013
+.Dd February 28, 2016
.Dt VMEM 9
.Os
.\" ------------------------------------------------------------
@@ -83,7 +83,7 @@
.Fn vmem_create
creates a new vmem arena.
.Pp
-.Bl -tag -width qcache_max
+.Bl -tag -offset indent -width qcache_max
.It Fa name
The string to describe the vmem.
.It Fa base
@@ -118,7 +118,7 @@
to import a span of size at least
.Fa size .
.Fa allocfn
-should accept the same
+must accept the same
.Fa flags
as
.Fn vmem_alloc .
@@ -169,7 +169,8 @@
Either of:
.Bl -tag -width VM_NOSLEEP
.It Dv VM_SLEEP
-Can sleep until enough resources are available.
+If the allocation cannot be satisfied immediately, sleep until enough
+resources are available.
.It Dv VM_NOSLEEP
Don't sleep.
Immediately return
@@ -184,7 +185,7 @@
.Fn vmem_xcreate
creates a new vmem arena.
.Pp
-.Bl -tag -width qcache_max
+.Bl -tag -offset indent -width qcache_max
.It Fa name
The string to describe the vmem.
.It Fa base
@@ -220,7 +221,7 @@
to import a span of size at least
.Fa size .
.Fa allocfn
-should accept the same
+must accept the same
.Fa flags
as
.Fn vmem_alloc .
@@ -274,7 +275,8 @@
Either of:
.Bl -tag -width VM_NOSLEEP
.It Dv VM_SLEEP
-Can sleep until enough resources are available.
+If the allocation cannot be satisfied immediately, sleep until enough
+resources are available.
.It Dv VM_NOSLEEP
Don't sleep.
Immediately return
@@ -297,23 +299,26 @@
on success,
.Dv ENOMEM
on failure.
-.Fa flags
-should be one of:
+.Bl -tag -offset indent -width flags
+.It Fa flags
+Either of:
.Bl -tag -width VM_NOSLEEP
.It Dv VM_SLEEP
-Can sleep until enough resources are available.
+If the allocation cannot be satisfied immediately, sleep until enough
+resources are available.
.It Dv VM_NOSLEEP
Don't sleep.
Immediately return
.Dv ENOMEM
if there are not enough resources available.
.El
+.El
.Pp
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Fn vmem_xalloc
allocates a resource from the arena.
.Pp
-.Bl -tag -width nocross
+.Bl -tag -offset indent -width nocross
.It Fa vm
The arena which we allocate from.
.It Fa size
@@ -333,10 +338,10 @@
.Fa align
is zero,
.Fa phase
-should be zero.
+must be zero.
Otherwise,
.Fa phase
-should be smaller than
+must be smaller than
.Fa align .
.It Fa nocross
Request a resource which doesn't cross
@@ -353,7 +358,7 @@
.It Fa flags
A bitwise OR of an allocation strategy and a sleep flag.
.Pp
-The allocation strategy is one of:
+The allocation strategy must be one of:
.Bl -tag -width VM_INSTANTFIT
.It Dv VM_BESTFIT
Prefer space efficiency.
@@ -361,10 +366,11 @@
Prefer performance.
.El
.Pp
-The sleep flag should be one of:
+The sleep flag must be one of:
.Bl -tag -width VM_NOSLEEP
.It Dv VM_SLEEP
-Can sleep until enough resources are available.
+If the allocation cannot be satisfied immediately, sleep until enough
+resources are available.
.It Dv VM_NOSLEEP
Don't sleep.
Immediately return
@@ -386,14 +392,14 @@
.Fn vmem_xalloc
to the arena.
.Pp
-.Bl -tag -width addr
+.Bl -tag -offset indent -width addr
.It Fa vm
The arena which we free to.
.It Fa addr
The resource being freed.
-It must be the one returned by
+It must have been allocated via
.Fn vmem_xalloc .
-Notably, it must not be the one from
+Notably, it must not have been allocated via
.Fn vmem_alloc .
Otherwise, the behaviour is undefined.
.It Fa size
@@ -408,7 +414,7 @@
.Fn vmem_alloc
allocates a resource from the arena.
.Pp
-.Bl -tag -width flags
+.Bl -tag -offset indent -width flags
.It Fa vm
The arena which we allocate from.
.It Fa size
@@ -416,7 +422,7 @@
.It Fa flags
A bitwise OR of an allocation strategy and a sleep flag.
.Pp
-The allocation strategy is one of:
+The allocation strategy must be one of:
.Bl -tag -width VM_INSTANTFIT
.It Dv VM_BESTFIT
Prefer space efficiency.
@@ -424,10 +430,11 @@
Prefer performance.
.El
.Pp
-The sleep flag should be one of:
+The sleep flag must be one of:
.Bl -tag -width VM_NOSLEEP
.It Dv VM_SLEEP
-Can sleep until enough resources are available.
+If the allocation cannot be satisfied immediately, sleep until enough
+resources are available.
.It Dv VM_NOSLEEP
Don't sleep.
Immediately return
@@ -449,14 +456,14 @@
.Fn vmem_alloc
to the arena.
.Pp
-.Bl -tag -width addr
+.Bl -tag -offset indent -width addr
.It Fa vm
The arena which we free to.
.It Fa addr
The resource being freed.
-It must be the one returned by
+It must have been allocated via
.Fn vmem_alloc .
-Notably, it must not be the one from
+Notably, it must not have been allocated via
.Fn vmem_xalloc .
Otherwise, the behaviour is undefined.
.It Fa size
@@ -471,10 +478,10 @@
.Fn vmem_destroy
destroys a vmem arena.
.Pp
-.Bl -tag -width vm
+.Bl -tag -offset indent -width vm
.It Fa vm
The vmem arena being destroyed.
-The caller should ensure that no one will use it anymore.
+The caller must ensure that no one will use it anymore.
.El
.\" ------------------------------------------------------------
.Sh RETURN VALUES
diff -r b39944709569 -r d2563916c1e2 sys/kern/subr_kmem.c
--- a/sys/kern/subr_kmem.c Mon Feb 29 00:17:54 2016 +0000
+++ b/sys/kern/subr_kmem.c Mon Feb 29 00:34:17 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_kmem.c,v 1.61 2015/07/27 09:24:28 maxv Exp $ */
+/* $NetBSD: subr_kmem.c,v 1.62 2016/02/29 00:34:17 chs Exp $ */
/*-
* Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
@@ -100,7 +100,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.61 2015/07/27 09:24:28 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.62 2016/02/29 00:34:17 chs Exp $");
#include <sys/param.h>
#include <sys/callback.h>
@@ -383,9 +383,13 @@
void *
kmem_alloc(size_t size, km_flag_t kmflags)
{
Home |
Main Index |
Thread Index |
Old Index