Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 16 Mar 2023 09:41:17 -0600
From:      Warner Losh <imp@bsdimp.com>
To:        Justin Hibbits <jhibbits@freebsd.org>
Cc:        "freebsd-arch@freebsd.org" <freebsd-arch@freebsd.org>
Subject:   Re: Blocks runtime in the kernel
Message-ID:  <CANCZdfqohTcujkDx=rizAXYAKXaDDw09g_hK6WSZ1JGKvvGOgA@mail.gmail.com>
In-Reply-To: <20230316112222.31b1620e@gonegalt.net>
References:  <20230316100611.4892008c@gonegalt.net> <CANCZdfrqSc5cuU=Pbo_fGKmbTeeoTYUaYPqZUahRar1XkbJ6=A@mail.gmail.com> <20230316112222.31b1620e@gonegalt.net>

next in thread | previous in thread | raw e-mail | index | archive | help
--000000000000489b0605f7064b0f
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Thu, Mar 16, 2023 at 9:22=E2=80=AFAM Justin Hibbits <jhibbits@freebsd.or=
g> wrote:

> On Thu, 16 Mar 2023 09:04:29 -0600
> Warner Losh <imp@bsdimp.com> wrote:
>
> > On Thu, Mar 16, 2023, 8:06 AM Justin Hibbits <jhibbits@freebsd.org>
> > wrote:
> >
> > > Most probably know I've been working on the IfAPI conversion of all
> > > network drivers in order to hide the contents of `struct ifnet`.
> > > I'm pretty much done with the development, and it's all in review.
> > > However, there's one bit that I've thought is very clunky since I
> > > added it, the if_foreach() iterator function, which iterates over
> > > all interfaces in the current VNET, and calls a callback to operate
> > > on each interface.  I've noticed that oftentimes I end up with a 2
> > > line callback, which just calls if_foreach_addr_type(), so I end up
> > > with just trivial callback functions, which seems like a waste.
> > >
> > > All that backstory to say, would it be beneficial to anyone else to
> > > add a (very basic) blocks runtime to the kernel for doing things
> > > like this?  The rough change to the IfAPI becomes:
> > >
> > > int if_foreach_b(int (^)(if_t));
> > >
> > > __block int foo =3D 0;
> > >
> > > if_foreach_b(^(if_t ifp) {
> > >   if (if_getlinkstate(ifp) =3D=3D LINK_STATE_UP)
> > >     foo++;
> > > });
> > >
> > > The same could be done for other *_foreach KPIs as well, if this
> > > proves out.  I think I could have something working in the next
> > > several days.
> > >
> > > The only technical snag I see with this would be other compilers.
> > > I'm not sure if GCC still supports blocks, it did at one point.
> > >
> > > What do you think?
> > >
> >
> >
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D78352
> >
> > Suggests that there were issues upstreaming the apple code. So
> > there's that.  The gcc12 port I have can't cope with the sample blocks
> > code I found on Wikipedia:
> > /* blocks-test.c */
> > #include <stdio.h>
> > #include <Block.h>
> > /* Type of block taking nothing returning an int */
> > typedef int (^IntBlock)();
> >
> > IntBlock MakeCounter(int start, int increment) {
> >         __block int i =3D start;
> >
> >         return Block_copy( ^(void) {
> >                 int ret =3D i;
> >                 i +=3D increment;
> >                 return ret;
> >         });
> >
> > }
> >
> > int main(void) {
> >         IntBlock mycounter =3D MakeCounter(5, 2);
> >         printf("First call: %d\n", mycounter());
> >         printf("Second call: %d\n", mycounter());
> >         printf("Third call: %d\n", mycounter());
> >
> >         /* because it was copied, it must also be released */
> >         Block_release(mycounter);
> >
> >         return 0;
> > }
> >
> > Our current clang is OK:
> > % clang -fblocks a.c -o a -lBlocksRuntime
> > %
> >
> > But there's no current users of __block in the kernel. There's no
> > kernel-specific Block.h file,
> > there's no references to BlockRuntime anywhere in the kernel tree and
> > the code in
> > contrib/llvm-project/compiler-rt/lib/BlocksRuntime is completely
> > userland specific. There
> > is no kernel support that I could see, since we don't have a
> > libkern/OSAtomic.h. I'm happy
> > to be corrected on this though: I've never tried to use blocks in the
> > kernel and this is grep
> > level confidence.
> >
> > Clang also doesn't enable blocks unless you pass it -fblock, so you'd
> > need to change a fair
> > portion of the kernel build system to enable that.
> >
> > So I'm thinking regardless of whether or not the project should do
> > this, you'll have a fair amount
> > of choppy waves ahead of you before you could get to the point of
> > starting the ifnet work.
> >
> > Warner
>
> Hi Warner,
>
> I did a very very simple test to see what is required link-wise for
> blocks in kernel.  This was done by changing
> https://reviews.freebsd.org/D38962 to use a block instead of a callback
> for the "bootpc_init_count_if_cb".  I didn't include Block.h or
> anything, and simply added "-fblocks" to kern.mk.  The result is it
> compiles fine, then fails to link (expected) with the following missing
> symbols:
>

As a basic test, that's fine. But I'm not sure we want to globally add
-fblocks
to the kernel. I don't know if that changes anything else. People will want
to know if there's global performance or size impact from doing this and
whether or not the compiler inserts other code because blocks are possible.

_Block_object_dispose
> _Block_object_assign
> _NSConcreteStackBlock
>
> Reading through
> contrib/llvm-project/compiler-rt/lib/BlocksRuntime/runtime.c these
> missing symbols look straightforward to implement for the basic case.
> I'm not thinking of working within the Clang runtime.c, I'm thinking of
> reimplementing the needed functions for this constrained use case (no
> need for GC, etc).
>
> This testing was only marginally more than you did, so I'm probably
> missing some things as well for more complex use cases.
>

I was worried about two things when I looked at the code: reference countin=
g
(which I think is kinda required, even though objc uses it for GC) and
memory
allocation / handling. The former is well understood, and we can adapt
things
(though knowing which subset is required here might be tricky, there's a lo=
t
of flags). The latter, though, would limit the use of these APIs to
situations
where you can call malloc/free M_WAIT, or you'd need to deal with malloc
failures better than runtime.c does.

So while the number of routines is small, I think they are the tip of the
iceburg
and may be more work than you're suggesting.


> I'm guessing from GCC's issues that this is a nonstarter anyway?
>

In the past we've said that it's OK to use clang specific code to get bette=
r
performance, but that depending on it entirely would require a careful
discussion. gcc is produces code that's easier to debug than clang (though
gcc12 build is currently still broken), and that's not nothing and has been
useful for me in the past. jhb can likely speak to other benefits for gcc12
since he did the last round of updates.

So given the difficulties on multiple fronts, I'm not sure it's a great
idea.
But maybe I'm wrong about how difficult things will be and maybe it would
work out in the end. But selecting the network stack to use what will be
an unproven, or at least immature technology is ambitious. We've had a
mixed bag with that stuff (see epoch and smr for examples).

I'm trying hard not to say a flat out "no," because I know that sometimes
things that look hard like this pay off. But no gcc support does make it
really hard to say yes. I've had my say, and I'll let others say from
here.

Warner

--000000000000489b0605f7064b0f
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote">=
<div dir=3D"ltr" class=3D"gmail_attr">On Thu, Mar 16, 2023 at 9:22=E2=80=AF=
AM Justin Hibbits &lt;<a href=3D"mailto:jhibbits@freebsd.org">jhibbits@free=
bsd.org</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"=
margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-lef=
t:1ex">On Thu, 16 Mar 2023 09:04:29 -0600<br>
Warner Losh &lt;<a href=3D"mailto:imp@bsdimp.com" target=3D"_blank">imp@bsd=
imp.com</a>&gt; wrote:<br>
<br>
&gt; On Thu, Mar 16, 2023, 8:06 AM Justin Hibbits &lt;<a href=3D"mailto:jhi=
bbits@freebsd.org" target=3D"_blank">jhibbits@freebsd.org</a>&gt;<br>
&gt; wrote:<br>
&gt; <br>
&gt; &gt; Most probably know I&#39;ve been working on the IfAPI conversion =
of all<br>
&gt; &gt; network drivers in order to hide the contents of `struct ifnet`.<=
br>
&gt; &gt; I&#39;m pretty much done with the development, and it&#39;s all i=
n review.<br>
&gt; &gt; However, there&#39;s one bit that I&#39;ve thought is very clunky=
 since I<br>
&gt; &gt; added it, the if_foreach() iterator function, which iterates over=
<br>
&gt; &gt; all interfaces in the current VNET, and calls a callback to opera=
te<br>
&gt; &gt; on each interface.=C2=A0 I&#39;ve noticed that oftentimes I end u=
p with a 2<br>
&gt; &gt; line callback, which just calls if_foreach_addr_type(), so I end =
up<br>
&gt; &gt; with just trivial callback functions, which seems like a waste.<b=
r>
&gt; &gt;<br>
&gt; &gt; All that backstory to say, would it be beneficial to anyone else =
to<br>
&gt; &gt; add a (very basic) blocks runtime to the kernel for doing things<=
br>
&gt; &gt; like this?=C2=A0 The rough change to the IfAPI becomes:<br>
&gt; &gt;<br>
&gt; &gt; int if_foreach_b(int (^)(if_t));<br>
&gt; &gt;<br>
&gt; &gt; __block int foo =3D 0;<br>
&gt; &gt;<br>
&gt; &gt; if_foreach_b(^(if_t ifp) {<br>
&gt; &gt;=C2=A0 =C2=A0if (if_getlinkstate(ifp) =3D=3D LINK_STATE_UP)<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0foo++;<br>
&gt; &gt; });<br>
&gt; &gt;<br>
&gt; &gt; The same could be done for other *_foreach KPIs as well, if this<=
br>
&gt; &gt; proves out.=C2=A0 I think I could have something working in the n=
ext<br>
&gt; &gt; several days.<br>
&gt; &gt;<br>
&gt; &gt; The only technical snag I see with this would be other compilers.=
<br>
&gt; &gt; I&#39;m not sure if GCC still supports blocks, it did at one poin=
t.<br>
&gt; &gt;<br>
&gt; &gt; What do you think?<br>
&gt; &gt;=C2=A0 <br>
&gt; <br>
&gt; <br>
&gt; <a href=3D"https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D78352" rel=
=3D"noreferrer" target=3D"_blank">https://gcc.gnu.org/bugzilla/show_bug.cgi=
?id=3D78352</a><br>
&gt; <br>
&gt; Suggests that there were issues upstreaming the apple code. So<br>
&gt; there&#39;s that.=C2=A0 The gcc12 port I have can&#39;t cope with the =
sample blocks<br>
&gt; code I found on Wikipedia:<br>
&gt; /* blocks-test.c */<br>
&gt; #include &lt;stdio.h&gt;<br>
&gt; #include &lt;Block.h&gt;<br>
&gt; /* Type of block taking nothing returning an int */<br>
&gt; typedef int (^IntBlock)();<br>
&gt; <br>
&gt; IntBlock MakeCounter(int start, int increment) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0__block int i =3D start;<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return Block_copy( ^(void) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0int ret =
=3D i;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0i +=3D in=
crement;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return re=
t;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0});<br>
&gt; <br>
&gt; }<br>
&gt; <br>
&gt; int main(void) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0IntBlock mycounter =3D MakeCounter(5,=
 2);<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0printf(&quot;First call: %d\n&quot;, =
mycounter());<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0printf(&quot;Second call: %d\n&quot;,=
 mycounter());<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0printf(&quot;Third call: %d\n&quot;, =
mycounter());<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/* because it was copied, it must als=
o be released */<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Block_release(mycounter);<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;<br>
&gt; }<br>
&gt; <br>
&gt; Our current clang is OK:<br>
&gt; % clang -fblocks a.c -o a -lBlocksRuntime<br>
&gt; %<br>
&gt; <br>
&gt; But there&#39;s no current users of __block in the kernel. There&#39;s=
 no<br>
&gt; kernel-specific Block.h file,<br>
&gt; there&#39;s no references to BlockRuntime anywhere in the kernel tree =
and<br>
&gt; the code in<br>
&gt; contrib/llvm-project/compiler-rt/lib/BlocksRuntime is completely<br>
&gt; userland specific. There<br>
&gt; is no kernel support that I could see, since we don&#39;t have a<br>
&gt; libkern/OSAtomic.h. I&#39;m happy<br>
&gt; to be corrected on this though: I&#39;ve never tried to use blocks in =
the<br>
&gt; kernel and this is grep<br>
&gt; level confidence.<br>
&gt; <br>
&gt; Clang also doesn&#39;t enable blocks unless you pass it -fblock, so yo=
u&#39;d<br>
&gt; need to change a fair<br>
&gt; portion of the kernel build system to enable that.<br>
&gt; <br>
&gt; So I&#39;m thinking regardless of whether or not the project should do=
<br>
&gt; this, you&#39;ll have a fair amount<br>
&gt; of choppy waves ahead of you before you could get to the point of<br>
&gt; starting the ifnet work.<br>
&gt; <br>
&gt; Warner<br>
<br>
Hi Warner,<br>
<br>
I did a very very simple test to see what is required link-wise for<br>
blocks in kernel.=C2=A0 This was done by changing<br>
<a href=3D"https://reviews.freebsd.org/D38962" rel=3D"noreferrer" target=3D=
"_blank">https://reviews.freebsd.org/D38962</a>; to use a block instead of a=
 callback<br>
for the &quot;bootpc_init_count_if_cb&quot;.=C2=A0 I didn&#39;t include Blo=
ck.h or<br>
anything, and simply added &quot;-fblocks&quot; to <a href=3D"http://kern.m=
k" rel=3D"noreferrer" target=3D"_blank">kern.mk</a>.=C2=A0 The result is it=
<br>
compiles fine, then fails to link (expected) with the following missing<br>
symbols:<br></blockquote><div><br></div><div>As a basic test, that&#39;s fi=
ne. But I&#39;m not sure we want to globally add -fblocks</div><div>to the =
kernel. I don&#39;t know if that changes anything else. People will want</d=
iv><div>to know if there&#39;s global performance or size impact from doing=
 this and</div><div>whether or not the compiler inserts other code because =
blocks are possible.</div><div><br></div><blockquote class=3D"gmail_quote" =
style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pa=
dding-left:1ex">
_Block_object_dispose<br>
_Block_object_assign<br>
_NSConcreteStackBlock<br>
<br>
Reading through<br>
contrib/llvm-project/compiler-rt/lib/BlocksRuntime/runtime.c these<br>
missing symbols look straightforward to implement for the basic case.<br>
I&#39;m not thinking of working within the Clang runtime.c, I&#39;m thinkin=
g of<br>
reimplementing the needed functions for this constrained use case (no<br>
need for GC, etc).<br>
<br>
This testing was only marginally more than you did, so I&#39;m probably<br>
missing some things as well for more complex use cases.<br></blockquote><di=
v><br></div><div>I was worried about two things when I looked at the code: =
reference counting</div><div>(which I think is kinda required, even though =
objc uses it for GC) and memory</div><div>allocation / handling. The former=
 is well understood, and we can adapt things</div><div>(though knowing whic=
h subset is required here might be tricky, there&#39;s a lot</div><div>of f=
lags). The latter, though, would limit the use of these APIs to situations<=
/div><div>where you can call malloc/free M_WAIT, or you&#39;d need to deal =
with malloc</div><div>failures better than runtime.c does.</div><div><br></=
div><div>So while the number of routines is small, I think they are the tip=
 of the iceburg</div><div>and may be more work than you&#39;re suggesting.<=
/div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0px=
 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I&#39;m guessing from GCC&#39;s issues that this is a nonstarter anyway?<br=
></blockquote><div><br></div><div>In the past we&#39;ve said that it&#39;s =
OK to use clang specific code to get better</div><div>performance, but that=
 depending on it entirely would require a careful</div><div>discussion. gcc=
 is produces code that&#39;s easier to debug than clang (though</div><div>g=
cc12 build is currently still broken), and that&#39;s not nothing and has b=
een</div><div>useful for me in the past. jhb can likely speak to other bene=
fits for gcc12</div><div>since he did the last round of updates.</div><div>=
<br></div><div>So given the difficulties on multiple fronts, I&#39;m not su=
re it&#39;s a great idea.</div><div>But maybe I&#39;m wrong about how diffi=
cult things will be and maybe it would</div><div>work out in the end. But s=
electing the network stack to use what will be</div><div>an unproven, or at=
 least immature technology is ambitious. We&#39;ve had a</div><div>mixed ba=
g with that stuff (see epoch and smr for examples).=C2=A0</div><div><br></d=
iv><div>I&#39;m trying hard not to say a flat out &quot;no,&quot; because I=
 know that sometimes</div><div>things that look hard like this pay off. But=
 no gcc support does make it</div><div>really hard to say yes. I&#39;ve had=
 my say, and I&#39;ll let others say from</div><div>here.</div><div><br></d=
iv><div>Warner</div></div></div>

--000000000000489b0605f7064b0f--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CANCZdfqohTcujkDx=rizAXYAKXaDDw09g_hK6WSZ1JGKvvGOgA>