<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Mar 15, 2017 at 5:21 AM, Chris Wilson <span dir="ltr"><<a href="mailto:chris@chris-wilson.co.uk" target="_blank">chris@chris-wilson.co.uk</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 Wed, Mar 15, 2017 at 09:50:57AM +0000, Chris Wilson wrote:<br>
> On Tue, Mar 14, 2017 at 10:43:07PM -0700, Jason Ekstrand wrote:<br>
> > +void<br>
> > +anv_bo_cache_release(struct anv_device *device,<br>
> > +                     struct anv_bo_cache *cache,<br>
> > +                     struct anv_bo *bo_in,<br>
> > +                     VkAllocationCallbacks *alloc)<br>
> > +{<br>
> > +   assert(anv_bo_cache_lookup(<wbr>cache, bo_in->gem_handle) == bo_in);<br>
> > +   struct anv_cached_bo *bo = (struct anv_cached_bo *)bo_in;<br>
> > +<br>
> > +   uint32_t count = __sync_fetch_and_add(&bo-><wbr>refcount, -1);<br>
> > +   assert(count > 0);<br>
> > +   if (count > 1)<br>
> > +      return;<br>
> > +   assert(count == 1);<br>
> > +   assert(bo->refcount == 0);<br>
><br>
> There's a window here for a second thread to acquire a reference to the<br>
> bo, just before you then go and free it.<br>
><br>
> The trick is the final unref has to be inside the mutex, earlier can be<br>
> outside. See refcount_dec_and_lock() in the kernel.<br>
<br>
</span>The alternative is to check against the dead bo when retrieving from the<br>
cache (under the mutex) -- refcount_add_not_zero. That is probably the<br>
cheaper option.</blockquote><div><br></div><div>How about if I just check the refcount again after I've entered the mutex and release the mutex and bail if it isn't zero?  The only time the reference count is ever imported is in import and it's done inside the mutex.  That's the cheapest option.<br><br></div><div>--Jason<br></div><div> </div></div><br></div></div>