<div dir="ltr">Riley (CCed) and I will be at Plumbers in a couple weeks.<div><br></div><div>There is a session on sync planned in the Android track, and of course we'll be available to chat.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Oct 2, 2014 at 1:44 PM, Daniel Vetter <span dir="ltr"><<a href="mailto:daniel@ffwll.ch" target="_blank">daniel@ffwll.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Thu, Oct 02, 2014 at 05:59:51PM +0300, Lauri Peltonen wrote:<br>
> +Rom who seems to be presenting about mainlining android sync at linux plumbers<br>
<br>
</span>Also add Greg KH as fyi that we're working on de-stage one of the android<br>
subsystems.<br>
<span class=""><br>
> On Wed, Oct 01, 2014 at 05:58:52PM +0200, Maarten Lankhorst wrote:<br>
> > You could neuter implicit fences by always attaching the fences as<br>
> > shared when explicit syncing is used. This would work correctly with<br>
> > eviction, and wouldn't cause any unneeded syncing. :)<br>
><br>
> Yes, that will probably work!  So, just to reiterate that I understood you and<br>
> Daniel correctly:<br>
><br>
> - de-stage sync_fence and it's user space API (the tedious part)<br>
> - add dma-buf ioctls for extracting and attaching explicit fences<br>
> - Nouveau specific: allow flagging gem buffers for explicit sync<br>
>   - do not check pre-fences from explicitly synced buffers at submit<br>
>   - continue to attach a shared fence after submit so that pinning and<br>
>     unmapping continue to work<br>
<br>
</span>    - Have explicit in/out fences for the pushbuf ioctl is missing I<br>
      guess in this step?<br>
<br>
I also think we need some kind of demonstration vehicle using nouveau to<br>
satisfy Dave Airlie's open-source userspace requirements for new<br>
interfaces. Might be good to chat with him to make sure we have that<br>
covered (and how much needs to be there really).<br>
<span class=""><br>
> Then working sets and getting rid of locking all buffers individually<br>
> can be dealt with later as an optimization.<br>
<br>
</span>Yeah, sounds like a good plan.<br>
<div><div class="h5"><br>
> On Wed, Oct 01, 2014 at 07:27:21PM +0200, Daniel Vetter wrote:<br>
> > On Wed, Oct 01, 2014 at 06:14:16PM +0300, Lauri Peltonen wrote:<br>
> > > Implicit fences attached to individual buffers are one way for residency<br>
> > > management.  Do you think a working set based model could work in the DRM<br>
> > > framework?  For example, something like this:<br>
> > ><br>
> > > - Allow user space to create "working set objects" and associate buffers with<br>
> > >   them.  If the user space doesn't want to manage working sets explicitly, it<br>
> > >   could also use an implicit default working set that contains all buffers that<br>
> > >   are mapped to the channel vm (on Android we could always use the default<br>
> > >   working set since we don't need to manage residency).  The working sets are<br>
> > >   initially marked as dirty.<br>
> > > - User space tells which working sets are referenced by each work submission.<br>
> > >   Kernel locks these working sets, pins all buffers in dirty working sets, and<br>
> > >   resets the dirty bits.  After kicking off work, kernel stores the fence to<br>
> > >   the _working sets_, and then releases the locks (if an implicit default<br>
> > >   working set is used, then this would be roughly equivalent to storing a fence<br>
> > >   to channel vm that tells "this is the last hw operation that might have<br>
> > >   touched buffers in this address space").<br>
> > > - If swapping doesn't happen, then we just need to check the working set dirty<br>
> > >   bits at each submit.<br>
> > > - When a buffer is swapped out, all working sets that refer to it need to be<br>
> > >   marked as dirty.<br>
> > > - When a buffer is swapped out or unmapped, we need to wait for the fences from<br>
> > >   all working sets that refer to the buffer.<br>
> > ><br>
> > > Initially one might think of working sets as a mere optimization - we now need<br>
> > > to process a few working sets at every submit instead of many individual<br>
> > > buffers.  However, it makes a huge difference because of fences: fences that<br>
> > > are attached to buffers are used for implicitly synchronizing work across<br>
> > > different channels and engines.  They are in the performance critical path, and<br>
> > > we want to carefully manage them (that's the idea of explicit synchronization).<br>
> > > The working set fences, on the other hand, would only be used to guarantee that<br>
> > > we don't swap out or unmap something that the GPU might be accessing.  We never<br>
> > > need to wait for those fences (except when swapping or unmapping), so we can be<br>
> > > conservative without hurting performance.<br>
> ><br>
> > Yeah, within the driver (i.e. for private objects which are never exported<br>
> > to dma_buf) we can recently do stuff like this. And your above idea is<br>
> > roughly one of the things we're tossing around for i915.<br>
> ><br>
> > But the cool stuff with drm is that cmd submission is driver-specific, so<br>
> > you can just go wild with nouveau. Of course you have to coninvce the<br>
> > nouveau guys (and also have open-source users for the new interface).<br>
> ><br>
> > For shared buffers I think we should stick with the implicit fences for a<br>
> > while simply because I'm not sure whether it's really worth the fuzz. And<br>
> > reworking all the drivers and dma-buf for some working sets is a lot of<br>
> > fuzz ;-) Like Maarten said you can mostly short-circuit the implicit<br>
> > fencing by only attaching shared fences.<br>
><br>
> Yes, I'll try to do that.<br>
><br>
><br>
> > In case you're curious: The idea is to have a 1:1 association between<br>
> > ppgtt address spaces and what you call the working set above, to implement<br>
> > the buffer svm model in ocl2. Mostly because we expect that applications<br>
> > won't get the more fine-grained buffer list right anyway. And this kind of<br>
> > gang-scheduling of working set sizes should be more efficient for the<br>
> > usual case where everything fits.<br>
><br>
> If I understood correctly, this would be exactly the same as what I called the<br>
> "default working set" above.  On Android we don't care much about finer grained<br>
> working sets either, because of UMA and no swapping.<br>
<br>
</div></div>Yeah, that's pretty much the idea. Well with ocl the SVM buffer working<br>
set sizes are attached to an ocl context, but I don't think there'll be<br>
more than one of those around really (except maybe when different<br>
libraries all use ocl, but don't work together on the same data).<br>
<span class=""><br>
> > > > Imo de-staging the android syncpt stuff needs to happen first, before drivers<br>
> > > > can use it. Since non-staging stuff really shouldn't depend upon code from<br>
> > > > staging.<br>
> > ><br>
> > > Fully agree.  I thought the best way towards that would be to show some driver<br>
> > > code that _would_ use it. :)<br>
> ><br>
> > Oh, there's the usual chicken&egg where we need a full-blown prototype<br>
> > before we can start merging. Interface work on upstream is super-hard, but<br>
> > given the ridiculous backwards compat guarantees Linus expects us to keep<br>
> > up totally justified. Mistakes are really expensive. So I'm happy to see<br>
> > you charge ahead here.<br>
><br>
> Given that I'm not used to working with upstream, don't expect too much from my<br>
> "charging ahead". :)  I'm still secretly hoping that the Android guys at Google<br>
> would jump in to help, now that we seem to agree that we could de-stage<br>
> sync_fence.<br>
<br>
</span>Forget that, imo the android guys are 100% absorbed with their own stuff,<br>
at least on the gfx side. Occasionally they pipe up with "btw this is what<br>
we're doing now".<br>
<br>
Also, the problem is that to actually push android stuff out of staging<br>
you need a use-case in upstream, which means an open-source gpu driver.<br>
There's not a lot of companies who have both that and ship android, and<br>
definitely not the nexus/android lead platforms.<br>
<br>
Display side would be easier since there's a bunch of kms drivers now<br>
upstream. But given that google decided to go ahead with their own adf<br>
instead of drm-kms that's also a non-starter.<br>
<br>
Heck, I have a hard time draggin our own android folks here at intel into<br>
upstream work ...<br>
<div><div class="h5"><br>
> > > > I'm all for adding explicit syncing. Our plans are roughly.  - Add both an in<br>
> > > > and and out fence to execbuf to sync with other rendering and give userspace<br>
> > > > a fence back. Needs to different flags probably.<br>
> > > ><br>
> > > > - Maybe add an ioctl to dma-bufs to get at the current implicit fences<br>
> > > >   attached to them (both an exclusive and non-exclusive version). This<br>
> > > >   should help with making explicit and implicit sync work together nicely.<br>
> > > ><br>
> > > > - Add fence support to kms. Probably only worth it together with the new<br>
> > > >   atomic stuff. Again we need an in fence to wait for (one for each<br>
> > > >   buffer) and an out fence. The later can easily be implemented by<br>
> > > >   extending struct drm_event, which means not a single driver code line<br>
> > > >   needs to be changed for this.<br>
> > > ><br>
> > > > - For de-staging android syncpts we need to de-clutter the internal<br>
> > > >   interfaces and also review all the ioctls exposed. Like you say it<br>
> > > >   should be just the userspace interface for struct drm_fence. Also, it<br>
> > > >   needs testcases and preferrably manpages.<br>
> > ><br>
> > > This all sounds very similar to what we'd like to do!  Maybe we can move<br>
> > > forward with these parts, and continue to attach fences at submit until we have<br>
> > > a satisfactory solution for the pinning problem?<br>
> ><br>
> > Yeah, that's our plan for i915 too. First add explicit fences, then figure<br>
> > out whether we need to be better at neutering the implicit fences, in case<br>
> > and only where it really gets in the way.<br>
> ><br>
> > > I'd like to understand what are the concrete steps to de-stage struct<br>
> > > sync_fence, since that's the first thing that needs to be done.  For example,<br>
> > > what do you mean by "de-cluttering the internal interfaces"?  Just that we'd<br>
> > > move the sync_fence parts from drivers/staging/android/sync.c to, say,<br>
> > > drivers/dma-buf/sync-fence.c ?  Would we still leave a copy of the existing<br>
> > > full driver to staging/android?<br>
> ><br>
> > Yeah I guess that would be an approach. Personally I think we should also<br>
> > have basic ioctl testcase for all the ioctls exposed by syncpt fds. And<br>
> > reviewing the kerneldoc for the driver-internal interfaces (which includes<br>
> > removing everything that's no made obsolete by struct fence). Bonus points<br>
> > for documenting the ioctls. We could throw the test binary into libdrm<br>
> > maybe, there's a bunch other like it already there.<br>
> ><br>
> > I'm not sure whether/how much google has already for this.<br>
><br>
> The simplest way to add tests is if we allow user space to create and trigger<br>
> fences.  We don't want to enable that from kernel by default, because that<br>
> opens possibilities for deadlocks (e.g. a process could deadlock a compositor<br>
> by passing a fence that it never triggers).  Android sync driver solves this by<br>
> having a separate CONFIG_SW_SYNC_USER that can be used for testing.<br>
><br>
> Here's a simple test by Google:<br>
> <a href="https://android.googlesource.com/platform/system/core/+/master/libsync/sync_test.c" target="_blank">https://android.googlesource.com/platform/system/core/+/master/libsync/sync_test.c</a><br>
<br>
</div></div>Hm, usually we expose such test interfaces through debugfs - that way<br>
production system won't ever ship with it (since there's too many exploits<br>
in there, especially with secure boot). But since you need it for<br>
validation tests (at least for the i915 suite) it should always be there<br>
when you need it.<br>
<br>
Exposing this as a configurable driver in dev is imo a no-go. But we<br>
should be able to easily convert this into a few debugfs files, so not too<br>
much fuzz hopefully.<br>
<span class=""><br>
> And this is their userspace wrapper lib:<br>
> <a href="https://android.googlesource.com/platform/system/core/+/master/libsync/sync.c" target="_blank">https://android.googlesource.com/platform/system/core/+/master/libsync/sync.c</a><br>
> <a href="https://android.googlesource.com/platform/system/core/+/master/libsync/include/sync/sync.h" target="_blank">https://android.googlesource.com/platform/system/core/+/master/libsync/include/sync/sync.h</a><br>
><br>
> Would that go to libdrm now...?<br>
<br>
</span>Imo it would make sense. At least we kinda want to use fences outside of<br>
Android, and a separate library project feels like overkill.<br>
<span class=""><br>
> > Aside: Will you be at XDC or linux plumbers? Either would be a perfect<br>
> > place to discuss plans and ideas - I'll attend both.<br>
><br>
> I wasn't going to, but let's see.  The former is pretty soon and the latter is<br>
> sold out.  At least Andy Ritger from Nvidia is coming to XDC for sure, and he's<br>
> been involved in our internal discussions around these topics. So I suggest you<br>
> have a chat with him at least!  :)<br>
<br>
</span>I'll definitely have a chat (and some beers) with Andy, been a while I've<br>
last seen him ;-)<br>
<div class="HOEnZb"><div class="h5"><br>
Cheers, Daniel<br>
--<br>
Daniel Vetter<br>
Software Engineer, Intel Corporation<br>
<a href="tel:%2B41%20%280%29%2079%20365%2057%2048" value="+41793655748">+41 (0) 79 365 57 48</a> - <a href="http://blog.ffwll.ch" target="_blank">http://blog.ffwll.ch</a><br>
</div></div></blockquote></div><br></div>