Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Mar 2025 19:32:02 +0800
From:      Zhenlei Huang <zlei@FreeBSD.org>
To:        Mateusz Guzik <mjg@FreeBSD.org>
Cc:        "src-committers@freebsd.org" <src-committers@FreeBSD.org>, "dev-commits-src-all@freebsd.org" <dev-commits-src-all@FreeBSD.org>, Warner Losh <imp@freebsd.org>, "dev-commits-src-main@freebsd.org" <dev-commits-src-main@FreeBSD.org>
Subject:   Re: git: 234683726708 - main - devclass: make devclass_alloc_unit use M_NOWAIT
Message-ID:  <F1B3652E-0D0C-402A-8509-D510992DAC15@FreeBSD.org>
In-Reply-To: <202503061103.526B32Id022652@gitrepo.freebsd.org>
References:  <202503061103.526B32Id022652@gitrepo.freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--Apple-Mail=_35C7DAD6-2678-417F-87A5-DCFFE513A6FC
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii



> On Mar 6, 2025, at 7:03 PM, Mateusz Guzik <mjg@FreeBSD.org> wrote:
>=20
> The branch main has been updated by mjg:
>=20
> URL: =
https://cgit.FreeBSD.org/src/commit/?id=3D234683726708cf5212d672d676d30056=
d4133859
>=20
> commit 234683726708cf5212d672d676d30056d4133859
> Author:     Mateusz Guzik <mjg@FreeBSD.org>
> AuthorDate: 2025-03-06 11:01:49 +0000
> Commit:     Mateusz Guzik <mjg@FreeBSD.org>
> CommitDate: 2025-03-06 11:01:49 +0000
>=20
>    devclass: make devclass_alloc_unit use M_NOWAIT
>=20
>    The only caller already does this.
>=20
>    The routine can be called with a mutex held making M_WAITOK =
illegal.
>=20
>    Sponsored by:   Rubicon Communications, LLC ("Netgate")
> ---
> sys/kern/subr_bus.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>=20
> diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c
> index 9506e471705c..0422352bba51 100644
> --- a/sys/kern/subr_bus.c
> +++ b/sys/kern/subr_bus.c
> @@ -1208,6 +1208,7 @@ devclass_get_sysctl_tree(devclass_t dc)
> static int
> devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
> {
> +	device_t *devices;
> 	const char *s;
> 	int unit =3D *unitp;
>=20
> @@ -1264,8 +1265,11 @@ devclass_alloc_unit(devclass_t dc, device_t =
dev, int *unitp)
> 		int newsize;
>=20
> 		newsize =3D unit + 1;
> -		dc->devices =3D reallocf(dc->devices,
> -		    newsize * sizeof(*dc->devices), M_BUS, M_WAITOK);
> +		devices =3D reallocf(dc->devices,
> +		    newsize * sizeof(*dc->devices), M_BUS, M_NOWAIT);

I'd recommend against this. =46rom the commit message of f3d3c63442ff, =
Warner said,
> In addition, transition to M_WAITOK since this is a sleepable context
So, the M_WAITOK is intentional.

Rather than reverting this, the caller devclass_add_device() should use =
M_WAITOK.

```
-       dev->nameunit =3D malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
-       if (!dev->nameunit)
-               return (ENOMEM);
+       dev->nameunit =3D malloc(buflen, M_BUS, M_WAITOK | M_ZERO);
```

Best regards,
Zhenlei

> +		if (devices =3D=3D NULL)
> +			return (ENOMEM);
> +		dc->devices =3D devices;
> 		memset(dc->devices + dc->maxunit, 0,
> 		    sizeof(device_t) * (newsize - dc->maxunit));
> 		dc->maxunit =3D newsize;




--Apple-Mail=_35C7DAD6-2678-417F-87A5-DCFFE513A6FC
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=us-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dus-ascii"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" class=3D""><br =
class=3D""><div><br class=3D""><blockquote type=3D"cite" class=3D""><div =
class=3D"">On Mar 6, 2025, at 7:03 PM, Mateusz Guzik &lt;<a =
href=3D"mailto:mjg@FreeBSD.org" class=3D"">mjg@FreeBSD.org</a>&gt; =
wrote:</div><br class=3D"Apple-interchange-newline"><div class=3D""><div =
class=3D"">The branch main has been updated by mjg:<br class=3D""><br =
class=3D"">URL: <a =
href=3D"https://cgit.FreeBSD.org/src/commit/?id=3D234683726708cf5212d672d6=
76d30056d4133859" =
class=3D"">https://cgit.FreeBSD.org/src/commit/?id=3D234683726708cf5212d67=
2d676d30056d4133859</a><br class=3D""><br class=3D"">commit =
234683726708cf5212d672d676d30056d4133859<br class=3D"">Author: =
&nbsp;&nbsp;&nbsp;&nbsp;Mateusz Guzik &lt;<a =
href=3D"mailto:mjg@FreeBSD.org" class=3D"">mjg@FreeBSD.org</a>&gt;<br =
class=3D"">AuthorDate: 2025-03-06 11:01:49 +0000<br class=3D"">Commit: =
&nbsp;&nbsp;&nbsp;&nbsp;Mateusz Guzik &lt;<a =
href=3D"mailto:mjg@FreeBSD.org" class=3D"">mjg@FreeBSD.org</a>&gt;<br =
class=3D"">CommitDate: 2025-03-06 11:01:49 +0000<br class=3D""><br =
class=3D""> &nbsp;&nbsp;&nbsp;devclass: make devclass_alloc_unit use =
M_NOWAIT<br class=3D""><br class=3D""> &nbsp;&nbsp;&nbsp;The only caller =
already does this.<br class=3D""><br class=3D""> &nbsp;&nbsp;&nbsp;The =
routine can be called with a mutex held making M_WAITOK illegal.<br =
class=3D""><br class=3D""> &nbsp;&nbsp;&nbsp;Sponsored by: =
&nbsp;&nbsp;Rubicon Communications, LLC ("Netgate")<br class=3D"">---<br =
class=3D""> sys/kern/subr_bus.c | 8 ++++++--<br class=3D""> 1 file =
changed, 6 insertions(+), 2 deletions(-)<br class=3D""><br class=3D"">diff=
 --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c<br class=3D"">index =
9506e471705c..0422352bba51 100644<br class=3D"">--- =
a/sys/kern/subr_bus.c<br class=3D"">+++ b/sys/kern/subr_bus.c<br =
class=3D"">@@ -1208,6 +1208,7 @@ devclass_get_sysctl_tree(devclass_t =
dc)<br class=3D""> static int<br class=3D""> =
devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)<br =
class=3D""> {<br class=3D"">+<span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span>device_t *devices;<br class=3D""> =
<span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>const char *s;<br class=3D""> <span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span>int unit =3D *unitp;<br =
class=3D""><br class=3D"">@@ -1264,8 +1265,11 @@ =
devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)<br =
class=3D""> <span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span><span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>int newsize;<br class=3D""><br class=3D""> <span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>newsize =3D=
 unit + 1;<br class=3D"">-<span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span>dc-&gt;devices =3D =
reallocf(dc-&gt;devices,<br class=3D"">-<span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span> &nbsp;&nbsp;&nbsp;newsize * =
sizeof(*dc-&gt;devices), M_BUS, M_WAITOK);<br class=3D"">+<span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>devices =3D=
 reallocf(dc-&gt;devices,<br class=3D"">+<span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span> &nbsp;&nbsp;&nbsp;newsize * =
sizeof(*dc-&gt;devices), M_BUS, M_NOWAIT);<br =
class=3D""></div></div></blockquote><div><br class=3D""></div>I'd =
recommend against this. =46rom the commit message of f3d3c63442ff, =
Warner said,</div><div>&gt;&nbsp;In addition, transition to M_WAITOK =
since this is a sleepable context</div><div>So, the&nbsp;M_WAITOK is =
intentional.</div><div><br class=3D""></div><div>Rather than reverting =
this, the caller&nbsp;devclass_add_device() should use <span =
style=3D"caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" =
class=3D"">M_WAITOK.</span></div><div><span style=3D"caret-color: rgb(0, =
0, 0); color: rgb(0, 0, 0);" class=3D""><br =
class=3D""></span></div><div><span style=3D"caret-color: rgb(0, 0, 0); =
color: rgb(0, 0, 0);" class=3D"">```</span></div><div><div><font =
color=3D"#000000" class=3D""><span style=3D"caret-color: rgb(0, 0, 0);" =
class=3D"">- &nbsp; &nbsp; &nbsp; dev-&gt;nameunit =3D malloc(buflen, =
M_BUS, M_NOWAIT|M_ZERO);</span></font></div><div><font color=3D"#000000" =
class=3D""><span style=3D"caret-color: rgb(0, 0, 0);" class=3D"">- =
&nbsp; &nbsp; &nbsp; if =
(!dev-&gt;nameunit)</span></font></div><div><font color=3D"#000000" =
class=3D""><span style=3D"caret-color: rgb(0, 0, 0);" class=3D"">- =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return =
(ENOMEM);</span></font></div><div><font color=3D"#000000" class=3D""><span=
 style=3D"caret-color: rgb(0, 0, 0);" class=3D"">+ &nbsp; &nbsp; &nbsp; =
dev-&gt;nameunit =3D malloc(buflen, M_BUS, M_WAITOK | =
M_ZERO);</span></font></div></div><div><span style=3D"caret-color: =
rgb(0, 0, 0); color: rgb(0, 0, 0);" class=3D"">```</span></div><div><span =
style=3D"caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class=3D""><br =
class=3D""></span></div><div><div style=3D"caret-color: rgb(0, 0, 0); =
color: rgb(0, 0, 0);">Best regards,</div><div style=3D"caret-color: =
rgb(0, 0, 0); color: rgb(0, 0, 0);">Zhenlei</div></div><div><br =
class=3D""><blockquote type=3D"cite" class=3D""><div class=3D""><div =
class=3D"">+<span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span><span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>if (devices =3D=3D NULL)<br class=3D"">+<span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>return =
(ENOMEM);<br class=3D"">+<span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span>dc-&gt;devices =3D devices;<br =
class=3D""> <span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span><span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>memset(dc-&gt;devices + dc-&gt;maxunit, 0,<br class=3D""> <span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span> =
&nbsp;&nbsp;&nbsp;sizeof(device_t) * (newsize - dc-&gt;maxunit));<br =
class=3D""> <span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span><span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>dc-&gt;maxunit =3D newsize;<br =
class=3D""></div></div></blockquote></div><br class=3D""><div class=3D"">
<div><br class=3D""></div>

</div>
<br class=3D""></body></html>=

--Apple-Mail=_35C7DAD6-2678-417F-87A5-DCFFE513A6FC--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?F1B3652E-0D0C-402A-8509-D510992DAC15>