From patchwork Mon Nov 4 09:36:54 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maarten Lankhorst X-Patchwork-Id: 21322 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ie0-f200.google.com (mail-ie0-f200.google.com [209.85.223.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 6841725B39 for ; Mon, 4 Nov 2013 09:37:11 +0000 (UTC) Received: by mail-ie0-f200.google.com with SMTP id aq17sf20862398iec.11 for ; Mon, 04 Nov 2013 01:37:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:message-id:date:from:user-agent :mime-version:to:references:in-reply-to:cc:subject:precedence :list-id:list-unsubscribe:list-archive:list-post:list-help :list-subscribe:errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list:content-type :content-transfer-encoding; bh=4Gh2lVaLYjClCEA78bhoMgxpaIsLP+U1Jek5wbJW1Qg=; b=cskvxwRPWCdlHgphcan+Wm3gk2iR7iG1uvjmz6niTrMDKqA6wP0ZdJEYf4vocMr+mM du1NreYerZHcqai569i0vd2jIDM8wTO6H8YH/dCyv6nj4UAI4TXXuqvZTO/cyrOfKBvW OruCE+tkD2PFZEpCP0jedWJYr4xSVU5F47rKpM9WVvwJ4IvX/OJaBHvUbpFbP9I5afs0 7eILSugMzIgaTnMh6OVmMjxVAiuUjTuhR4rQ2YXKui/L8vijQZ0HrIOW5VWfIaJwPEUo wECBDq1DJDRLZ9TfE5aaZzvcy7qpJBkT/BvjDs0PVhqiGabEqbLVGVYgXKzFUYYfE9fz bMvQ== X-Gm-Message-State: ALoCoQmTeDhGZIdDDr/yqvRQ9VvdfrorvFMJ44ukEoW5D+cdL2C9pR93W/N0z9BwnLpf0bdzhd2F X-Received: by 10.42.126.72 with SMTP id d8mr4577596ics.4.1383557830678; Mon, 04 Nov 2013 01:37:10 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.105.34 with SMTP id gj2ls2217936qeb.52.gmail; Mon, 04 Nov 2013 01:37:10 -0800 (PST) X-Received: by 10.52.32.37 with SMTP id f5mr8967713vdi.17.1383557830510; Mon, 04 Nov 2013 01:37:10 -0800 (PST) Received: from mail-vb0-f54.google.com (mail-vb0-f54.google.com [209.85.212.54]) by mx.google.com with ESMTPS id j9si4939249vcg.10.2013.11.04.01.37.10 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 04 Nov 2013 01:37:10 -0800 (PST) Received-SPF: neutral (google.com: 209.85.212.54 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.54; Received: by mail-vb0-f54.google.com with SMTP id q12so1229424vbe.13 for ; Mon, 04 Nov 2013 01:37:10 -0800 (PST) X-Received: by 10.58.216.74 with SMTP id oo10mr10770265vec.0.1383557830335; Mon, 04 Nov 2013 01:37:10 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp120215vcz; Mon, 4 Nov 2013 01:37:09 -0800 (PST) X-Received: by 10.180.38.99 with SMTP id f3mr11439729wik.40.1383557828756; Mon, 04 Nov 2013 01:37:08 -0800 (PST) Received: from ip-10-141-164-156.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id v5si299598wiy.15.2013.11.04.01.37.05 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 04 Nov 2013 01:37:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-141-164-156.ec2.internal) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1VdGYQ-0001td-KN; Mon, 04 Nov 2013 09:34:38 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1VdGYL-0001tY-FS for linaro-mm-sig@lists.linaro.org; Mon, 04 Nov 2013 09:34:33 +0000 Received: from 5ed49945.cm-7-5c.dynamic.ziggo.nl ([94.212.153.69] helo=[192.168.1.128]) by youngberry.canonical.com with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1VdGac-0004z6-R0; Mon, 04 Nov 2013 09:36:54 +0000 Message-ID: <52776AB6.3020304@canonical.com> Date: Mon, 04 Nov 2013 10:36:54 +0100 From: Maarten Lankhorst User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-Version: 1.0 To: Colin Cross References: <524BCCD0.90002@canonical.com> <52556ABE.2090201@canonical.com> <52690EEC.5000501@canonical.com> <5270F8D7.4040406@canonical.com> In-Reply-To: X-Enigmail-Version: 1.5.2 Cc: "linaro-mm-sig@lists.linaro.org" , Android Kernel Team , John Stultz , "dri-devel@lists.freedesktop.org" Subject: Re: [Linaro-mm-sig] thoughts of looking at android fences X-BeenThere: linaro-mm-sig@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Errors-To: linaro-mm-sig-bounces@lists.linaro.org Sender: linaro-mm-sig-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: maarten.lankhorst@canonical.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.54 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 op 02-11-13 22:36, Colin Cross schreef: > On Wed, Oct 30, 2013 at 5:17 AM, Maarten Lankhorst > wrote: >> op 24-10-13 14:13, Maarten Lankhorst schreef: >>> So I actually tried to implement it now. I killed all the deprecated members and assumed a linear timeline. >>> This means that syncpoints can only be added at the end, not in between. In particular it means sw_sync >>> might be slightly broken. >>> >>> I only tested it with a simple program I wrote called ufence.c, it's in drivers/staging/android/ufence.c in the following tree: >>> >>> http://cgit.freedesktop.org/~mlankhorst/linux >>> >>> the "rfc: convert android to fence api" has all the changes from my dma-fence proposal to what android would need, >>> it also converts the userspace fence api to use the dma-fence api. >>> >>> sync_pt is implemented as fence too. This meant not having to convert all of android right away, though I did make some changes. >>> I killed the deprecated members and made all the fence calls forward to the sync_timeline_ops. dup and compare are no longer used. >>> >>> I haven't given this a spin on a full android kernel, only with the components that are in mainline kernel under staging and my dumb test program. >>> >>> ~Maarten >>> >>> PS: The nomenclature is very confusing. I want to rename dma-fence to syncpoint, but I want some feedback from the android devs first. :) >>> >> Come on, any feedback? I want to move the discussion forward. >> >> ~Maarten > I experimented with it a little on a device that uses sync and came > across a few bugs: > 1. sync_timeline_signal needs to call __fence_signal on all signaled > points on the timeline, not just the first > 2. fence_add_callback doesn't always initialize cb.node > 3. sync_fence_wait should take ms > 4. sync_print_pt status printing was incorrect > 5. there is a deadlock: > sync_print_obj takes obj->child_list_lock > sync_print_pt > fence_is_signaled > fence_signal takes fence->lock == obj->child_list_lock > 6. freeing a timeline before all the fences holding points on that > timeline have timed out results in a crash > > With the attached patch to fix these issues, our libsync and sync_test > give the same results as with our sync code. I haven't tested against > the full Android framework yet. > > The compare op and timeline ordering is critical to the efficiency of > sync points on Android. The compare op is used when merging fences to > drop all but the latest point on the same timeline. This is necessary > for example when the same buffer is submitted to the display on > multiple frames, like when there is a live wallpaper in the background > updating at 60 fps and a static screen of widgets on top of it. The > static widget buffer is submitted on every frame, returning a new > fence each time. The compositor merges the new fence with the fence > for the previous buffer, and because they are on the same timeline it > merges down to a single point. I experimented with disabling the > merge optimization on a Nexus 10, and found that leaving the screen on > running a live wallpaper eventually resulted in 100k outstanding sync > points. Hey, fence_add_callback will now always initialize cb->node, even on failure. I added __fence_is_signaled, to be used with the lock held. sync_print_pt didn't work when the fence was signaled with an error, I fixed that. So I reworked patch below, no merge optimization yet. It will be done as a separate patch. :) diff --git a/drivers/base/fence.c b/drivers/base/fence.c index 89c89ae19f58..9e7a63c4b07f 100644 --- a/drivers/base/fence.c +++ b/drivers/base/fence.c @@ -185,8 +185,10 @@ int fence_add_callback(struct fence *fence, struct fence_cb *cb, if (WARN_ON(!fence || !func)) return -EINVAL; - if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) + if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { + LIST_INIT_HEAD(&cb->node); return -ENOENT; + } spin_lock_irqsave(fence->lock, flags); @@ -202,7 +204,8 @@ int fence_add_callback(struct fence *fence, struct fence_cb *cb, if (!ret) { cb->func = func; list_add_tail(&cb->node, &fence->cb_list); - } + } else + LIST_INIT_HEAD(&cb->node); spin_unlock_irqrestore(fence->lock, flags); return ret; diff --git a/drivers/staging/android/sync.c b/drivers/staging/android/sync.c index 110a9e99cb71..2c7fd3f2ab23 100644 --- a/drivers/staging/android/sync.c +++ b/drivers/staging/android/sync.c @@ -74,6 +74,16 @@ static void sync_timeline_free(struct kref *kref) kfree(obj); } +static void sync_timeline_get(struct sync_timeline *obj) +{ + kref_get(&obj->kref); +} + +static void sync_timeline_put(struct sync_timeline *obj) +{ + kref_put(&obj->kref, sync_timeline_free); +} + void sync_timeline_destroy(struct sync_timeline *obj) { obj->destroyed = true; @@ -83,8 +93,8 @@ void sync_timeline_destroy(struct sync_timeline *obj) * that their parent is going away. */ - if (!kref_put(&obj->kref, sync_timeline_free)) - sync_timeline_signal(obj); + sync_timeline_signal(obj); + sync_timeline_put(obj); } EXPORT_SYMBOL(sync_timeline_destroy); @@ -98,12 +108,8 @@ void sync_timeline_signal(struct sync_timeline *obj) spin_lock_irqsave(&obj->child_list_lock, flags); list_for_each_entry_safe(pt, next, &obj->active_list_head, active_list) { - if (!pt->base.ops->signaled(&pt->base)) - break; - else { - __fence_signal(&pt->base); + if (__fence_is_signaled(&pt->base)) list_del(&pt->active_list); - } } spin_unlock_irqrestore(&obj->child_list_lock, flags); } @@ -122,6 +128,7 @@ struct sync_pt *sync_pt_create(struct sync_timeline *obj, int size) return NULL; spin_lock_irqsave(&obj->child_list_lock, flags); + sync_timeline_get(obj); __fence_init(&pt->base, &android_fence_ops, &obj->child_list_lock, obj->context, ++obj->value); list_add_tail(&pt->child_list, &obj->child_list_head); INIT_LIST_HEAD(&pt->active_list); @@ -255,7 +262,7 @@ struct sync_fence *sync_fence_merge(const char *name, fence_get(pt); fence->cbs[a->num_fences + i].sync_pt = pt; fence->cbs[a->num_fences + i].fence = fence; - if (fence_add_callback(pt, &fence->cbs[i].cb, fence_check_cb_func)) + if (fence_add_callback(pt, &fence->cbs[a->num_fences + i].cb, fence_check_cb_func)) atomic_dec(&fence->status); } @@ -325,6 +332,8 @@ int sync_fence_wait(struct sync_fence *fence, long timeout) if (timeout < 0) timeout = MAX_SCHEDULE_TIMEOUT; + else + timeout = msecs_to_jiffies(timeout); trace_sync_wait(fence, 1); for (i = 0; i < fence->num_fences; ++i) @@ -383,6 +392,7 @@ static void android_fence_release(struct fence *fence) if (parent->ops->free_pt) parent->ops->free_pt(pt); + sync_timeline_put(parent); kfree(pt); } diff --git a/drivers/staging/android/sync_debug.c b/drivers/staging/android/sync_debug.c index 55ad34085f2f..2ef6496c7cd0 100644 --- a/drivers/staging/android/sync_debug.c +++ b/drivers/staging/android/sync_debug.c @@ -82,18 +82,18 @@ static const char *sync_status_str(int status) static void sync_print_pt(struct seq_file *s, struct sync_pt *pt, bool fence) { - int status = 0; + int status = 1; struct sync_timeline *parent = sync_pt_parent(pt); - if (fence_is_signaled(&pt->base)) { + + if (__fence_is_signaled(&pt->base)) status = pt->base.status; - if (!status) - status = 1; - } + seq_printf(s, " %s%spt %s", fence ? parent->name : "", fence ? "_" : "", sync_status_str(status)); - if (status) { + + if (status <= 0) { struct timeval tv = ktime_to_timeval(pt->base.timestamp); seq_printf(s, "@%ld.%06ld", tv.tv_sec, tv.tv_usec); } diff --git a/include/linux/fence.h b/include/linux/fence.h index 2beb3b0ff2a3..dd1639ff96c7 100644 --- a/include/linux/fence.h +++ b/include/linux/fence.h @@ -237,6 +237,20 @@ int fence_add_callback(struct fence *fence, struct fence_cb *cb, bool fence_remove_callback(struct fence *fence, struct fence_cb *cb); void fence_enable_sw_signaling(struct fence *fence); +static inline bool +__fence_is_signaled(struct fence *fence) +{ + if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) + return true; + + if (fence->ops->signaled && fence->ops->signaled(fence)) { + __fence_signal(fence); + return true; + } + + return false; +} + /** * fence_is_signaled - Return an indication if the fence is signaled yet. * @fence: [in] the fence to check