Re: Blocks runtime in the kernel

From: Warner Losh <imp_at_bsdimp.com>
Date: Thu, 16 Mar 2023 15:41:17 UTC
On Thu, Mar 16, 2023 at 9:22 AM Justin Hibbits <jhibbits@freebsd.org> 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 = 0;
> > >
> > > if_foreach_b(^(if_t ifp) {
> > >   if (if_getlinkstate(ifp) == 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=78352
> >
> > 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 = start;
> >
> >         return Block_copy( ^(void) {
> >                 int ret = i;
> >                 i += increment;
> >                 return ret;
> >         });
> >
> > }
> >
> > int main(void) {
> >         IntBlock mycounter = 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 counting
(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 lot
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 better
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