NetBSD Problem Report #60733

From www@netbsd.org  Wed Sep 16 10:34:27 2026
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256
	 client-signature RSA-PSS (2048 bits) client-digest SHA256)
	(Client CN "mail.netbsd.org", Issuer "YR1" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 5A0801A923B
	for <gnats-bugs@gnats.NetBSD.org>; Wed, 16 Sep 2026 10:34:27 +0000 (UTC)
Message-Id: <20260916103425.C2D7E1A923C@mollari.NetBSD.org>
Date: Wed, 16 Sep 2026 10:34:25 +0000 (UTC)
From: campbell+netbsd@mumble.net
Reply-To: campbell+netbsd@mumble.net
To: gnats-bugs@NetBSD.org
Subject: kmem_intr_alloc(..., KM_SLEEP) mistake isn't detected
X-Send-Pr-Version: www-1.0
X-From4GNATS: "campbell+netbsd@mumble.net via gnats" <gnats-admin@NetBSD.org>

>Number:         60733
>Category:       kern
>Synopsis:       kmem_intr_alloc(..., KM_SLEEP) mistake isn't detected
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kern-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Sep 16 10:35:00 +0000 2026
>Last-Modified:  Wed Sep 16 17:30:01 +0000 2026
>Originator:     Taylor R Campbell
>Release:        current, 11, 10, 9, ...
>Organization:
The Net_intr_BSD Allocation, Asleep
>Environment:
>Description:

	The kmem_intr_alloc function is meant to be used for (mostly
	legacy) code paths that attempt allocation in interrupt
	context.  In such contexts, callers MUST be prepared to handle
	allocation failure gracefully, and MUST NOT sleep.  So it is
	always an error for callers outside subr_kmem.c to attempt
	kmem_intr_alloc(..., KM_SLEEP).

	However, while there is an assertion to forbid kmem_alloc(...)
	in interrupt context, there is no assertion to forbid
	kmem_intr_alloc(..., KM_SLEEP).

	Currently kmem_alloc is (mostly) just a wrapper around
	kmem_intr_alloc with an assertion:

    408 void *
    409 kmem_alloc(size_t size, km_flag_t kmflags)
    410 {
...
    413 	KASSERT(!cpu_intr_p());
    414 	KASSERT(!cpu_softintr_p());
    415 
    416 	v = kmem_intr_alloc(size, kmflags);
...
    421 	KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
    422 	return v;
    423 }

	https://nxr.netbsd.org/xref/src/sys/kern/subr_kmem.c?r=1.89#404

>How-To-Repeat:

	code review

>Fix:

	Factor the bulk of the logic into an internal subroutine, say
	kmem_internal_alloc, so that that instead of

	kmem_alloc
	-> assert not intr context
	-> kmem_intr_alloc
	   -> (allocation logic)

	the call graph will be

	kmem_alloc
	-> assert not intr context
	-> kmem_internal_alloc
	   -> (allocation logic)

	kmem_intr_alloc
	-> assert not KM_SLEEP
	-> kmem_internal_alloc
	   -> (allocation logic)

>Audit-Trail:
From: Jason Thorpe <thorpej@me.com>
To: gnats-bugs@netbsd.org
Cc: kern-bug-people@netbsd.org,
 gnats-admin@netbsd.org,
 netbsd-bugs@netbsd.org
Subject: Re: kern/60733: kmem_intr_alloc(..., KM_SLEEP) mistake isn't detected
Date: Wed, 16 Sep 2026 10:27:58 -0700

 --Apple-Mail=_471B40AD-4A34-4A64-90AA-5A066CD7EE50
 Content-Transfer-Encoding: quoted-printable
 Content-Type: text/plain;
 	charset=utf-8



 > On Sep 16, 2026, at 3:35=E2=80=AFAM, campbell+netbsd@mumble.net via =
 gnats <gnats-admin@netbsd.org> wrote:
 >=20
 > kmem_intr_alloc
 > 	-> assert not KM_SLEEP
 > 	-> kmem_internal_alloc
 > 	   -> (allocation logic)


 kmem_intr_alloc() should simply lose the flag argument?

 -- thorpej


 --Apple-Mail=_471B40AD-4A34-4A64-90AA-5A066CD7EE50
 Content-Transfer-Encoding: quoted-printable
 Content-Type: text/html;
 	charset=utf-8

 <html aria-label=3D"message body"><head><meta http-equiv=3D"content-type" =
 content=3D"text/html; charset=3Dutf-8"></head><body =
 style=3D"overflow-wrap: break-word; -webkit-nbsp-mode: space; =
 line-break: after-white-space;"><br =
 id=3D"lineBreakAtBeginningOfMessage"><div><br><blockquote =
 type=3D"cite"><div>On Sep 16, 2026, at 3:35=E2=80=AFAM, =
 campbell+netbsd@mumble.net via gnats &lt;gnats-admin@netbsd.org&gt; =
 wrote:</div><br class=3D"Apple-interchange-newline"><div><meta =
 charset=3D"UTF-8"><span style=3D"caret-color: rgb(0, 0, 0); font-family: =
 Helvetica; font-size: 12px; font-style: normal; font-variant-caps: =
 normal; font-weight: 400; letter-spacing: normal; orphans: 2; =
 text-align: start; text-indent: 0px; text-transform: none; white-space: =
 normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; =
 text-decoration: none; float: none; display: inline =
 !important;">kmem_intr_alloc</span><br style=3D"caret-color: rgb(0, 0, =
 0); font-family: Helvetica; font-size: 12px; font-style: normal; =
 font-variant-caps: normal; font-weight: 400; letter-spacing: normal; =
 orphans: 2; text-align: start; text-indent: 0px; text-transform: none; =
 white-space: normal; widows: 2; word-spacing: 0px; =
 -webkit-text-stroke-width: 0px; text-decoration-line: none; =
 text-decoration-thickness: auto; text-decoration-style: solid;"><span =
 class=3D"Apple-tab-span" style=3D"caret-color: rgb(0, 0, 0); =
 font-family: Helvetica; font-size: 12px; font-style: normal; =
 font-variant-caps: normal; font-weight: 400; letter-spacing: normal; =
 orphans: 2; text-align: start; text-indent: 0px; text-transform: none; =
 white-space: pre; widows: 2; word-spacing: 0px; =
 -webkit-text-stroke-width: 0px; text-decoration-line: none; =
 text-decoration-thickness: auto; text-decoration-style: solid;">	=
 </span><span style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; =
 font-size: 12px; font-style: normal; font-variant-caps: normal; =
 font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; =
 text-indent: 0px; text-transform: none; white-space: normal; widows: 2; =
 word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: =
 none; float: none; display: inline !important;">-&gt; assert not =
 KM_SLEEP</span><br style=3D"caret-color: rgb(0, 0, 0); font-family: =
 Helvetica; font-size: 12px; font-style: normal; font-variant-caps: =
 normal; font-weight: 400; letter-spacing: normal; orphans: 2; =
 text-align: start; text-indent: 0px; text-transform: none; white-space: =
 normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; =
 text-decoration-line: none; text-decoration-thickness: auto; =
 text-decoration-style: solid;"><span class=3D"Apple-tab-span" =
 style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: =
 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; =
 letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; =
 text-transform: none; white-space: pre; widows: 2; word-spacing: 0px; =
 -webkit-text-stroke-width: 0px; text-decoration-line: none; =
 text-decoration-thickness: auto; text-decoration-style: solid;">	=
 </span><span style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; =
 font-size: 12px; font-style: normal; font-variant-caps: normal; =
 font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; =
 text-indent: 0px; text-transform: none; white-space: normal; widows: 2; =
 word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: =
 none; float: none; display: inline !important;">-&gt; =
 kmem_internal_alloc</span><br style=3D"caret-color: rgb(0, 0, 0); =
 font-family: Helvetica; font-size: 12px; font-style: normal; =
 font-variant-caps: normal; font-weight: 400; letter-spacing: normal; =
 orphans: 2; text-align: start; text-indent: 0px; text-transform: none; =
 white-space: normal; widows: 2; word-spacing: 0px; =
 -webkit-text-stroke-width: 0px; text-decoration-line: none; =
 text-decoration-thickness: auto; text-decoration-style: solid;"><span =
 class=3D"Apple-tab-span" style=3D"caret-color: rgb(0, 0, 0); =
 font-family: Helvetica; font-size: 12px; font-style: normal; =
 font-variant-caps: normal; font-weight: 400; letter-spacing: normal; =
 orphans: 2; text-align: start; text-indent: 0px; text-transform: none; =
 white-space: pre; widows: 2; word-spacing: 0px; =
 -webkit-text-stroke-width: 0px; text-decoration-line: none; =
 text-decoration-thickness: auto; text-decoration-style: solid;">	=
 </span><span style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; =
 font-size: 12px; font-style: normal; font-variant-caps: normal; =
 font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; =
 text-indent: 0px; text-transform: none; white-space: normal; widows: 2; =
 word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: =
 none; float: none; display: inline !important;"><span =
 class=3D"Apple-converted-space">&nbsp;</span>&nbsp;&nbsp;-&gt; =
 (allocation =
 logic)</span></div></blockquote></div><div><br></div><div>kmem_intr_alloc(=
 ) should simply lose the flag argument?</div><br><div>
 <span class=3D"Apple-style-span" style=3D"border-collapse: separate; =
 color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; =
 font-style: normal; font-variant: normal; font-weight: normal; =
 letter-spacing: normal; line-height: normal; orphans: 2; text-align: =
 auto; text-indent: 0px; text-transform: none; white-space: normal; =
 widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; =
 -webkit-border-vertical-spacing: 0px; =
 -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
 auto; -webkit-text-stroke-width: 0px; "><div style=3D"word-wrap: =
 break-word; -webkit-nbsp-mode: space; -webkit-line-break: =
 after-white-space; ">-- thorpej</div></span>

 </div>
 <br></body></html>=

 --Apple-Mail=_471B40AD-4A34-4A64-90AA-5A066CD7EE50--

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.