From patchwork Sun Jul 17 12:58:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Wilson X-Patchwork-Id: 9233673 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 75AAE60865 for ; Sun, 17 Jul 2016 13:00:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 67274205AD for ; Sun, 17 Jul 2016 13:00:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5B6592094D; Sun, 17 Jul 2016 13:00:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8B10D205AD for ; Sun, 17 Jul 2016 13:00:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751715AbcGQNA2 (ORCPT ); Sun, 17 Jul 2016 09:00:28 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:33301 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751071AbcGQM7I (ORCPT ); Sun, 17 Jul 2016 08:59:08 -0400 Received: by mail-wm0-f65.google.com with SMTP id o80so8767473wme.0; Sun, 17 Jul 2016 05:59:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=B0zISgTnaA44W8Bcw561fMJKshVvE5Hop5FC4Yzb+Tg=; b=Uy/FpppWj/5VbQg7vnhp++ydrMa+1spRtXte4RTmKhDrlAdP8DpeCAlIRmSANU2TK5 7H9lWOx+3c1c8fQoVu43z6pNSRD0h/q2AeaoxlOKGLSRUUKvA2y5cdqxYTo2bUeWfZwT v38lFCG8//+rWEHZOTKqTa70/FslqSgDan5Wy5MXpqNGLP3bsTQkCWZ7H3nOl1CqjwTD wMzCcyN48hMRM08hMTqSmattkslHMnPdTAPZN5Idi6sBemapyRbNvRh8J20Xld/hmnBz i2WUKoCuAqktAdAucU3bLuqZuYScAknCrAtJ9BY7E/IL6RBnYc3GgCuW67o0Wswck0kj Pqyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=B0zISgTnaA44W8Bcw561fMJKshVvE5Hop5FC4Yzb+Tg=; b=iVpLONeYd+W/S3KK51zPs5EtojEuqvTayMzjs23S0jSe3TBluMvPSld8mkBgXOtqpt /EjQW/YtmogTZ8iKyGw8LLAwEz0bk50l35MEBmyQumuTAJ3v59jMn5lNI9/TqGlWQses 0FzgOQda/sNQsg37fKUg7elW4+fLXGavJ7VOByIxFeX5boFANpvJ4JsiBbK+UKii2u8J c0f8D4sw2qGyAGlbEh7p/sVu5779YE+AMVWq5RrDQqu0clcVDH+NoKFjDATfeLiMzFD5 QXD4wLr7r46AspUgP+k7diixLvKcn3XMw/rJ54/zb4gKw4TVFB8+DxPSRwLPtHrflWNQ 4BCw== X-Gm-Message-State: ALyK8tIu9kM1iCishjRCKGItbvOcPQ4Zm1IabGYysM6yhnXq5zUiOPbfaq0GxRKb5Ygvuw== X-Received: by 10.28.152.66 with SMTP id a63mr20346148wme.66.1468760346442; Sun, 17 Jul 2016 05:59:06 -0700 (PDT) Received: from haswell.alporthouse.com ([78.156.65.138]) by smtp.gmail.com with ESMTPSA id p76sm8125371wmd.10.2016.07.17.05.59.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Jul 2016 05:59:02 -0700 (PDT) From: Chris Wilson To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, Chris Wilson , Sumit Semwal , Shuah Khan , Tejun Heo , Daniel Vetter , Andrew Morton , Ingo Molnar , Kees Cook , Thomas Gleixner , "Paul E. McKenney" , Dan Williams , Andrey Ryabinin , Davidlohr Bueso , Nikolay Aleksandrov , "David S. Miller" , "Peter Zijlstra (Intel)" , Rasmus Villemoes , Andy Shevchenko , Dmitry Vyukov , Alexander Potapenko , linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org Subject: [PATCH v2 3/7] kfence: Extend kfences for listening on DMA fences Date: Sun, 17 Jul 2016 13:58:03 +0100 Message-Id: <1468760287-731-4-git-send-email-chris@chris-wilson.co.uk> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1468760287-731-1-git-send-email-chris@chris-wilson.co.uk> References: <1466759333-4703-1-git-send-email-chris@chris-wilson.co.uk> <1468760287-731-1-git-send-email-chris@chris-wilson.co.uk> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP dma-buf provides an interfaces for receiving notifications from DMA hardware, and for implicitly tracking fences used for rendering into dma-buf. We want to be able to use these event sources along with kfence for easy collection and combining with other events. Signed-off-by: Chris Wilson Cc: Sumit Semwal Cc: Shuah Khan Cc: Tejun Heo Cc: Daniel Vetter Cc: Andrew Morton Cc: Ingo Molnar Cc: Kees Cook Cc: Thomas Gleixner Cc: "Paul E. McKenney" Cc: Dan Williams Cc: Andrey Ryabinin Cc: Davidlohr Bueso Cc: Nikolay Aleksandrov Cc: "David S. Miller" Cc: "Peter Zijlstra (Intel)" Cc: Rasmus Villemoes Cc: Andy Shevchenko Cc: Dmitry Vyukov Cc: Alexander Potapenko Cc: linux-kernel@vger.kernel.org Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Cc: linaro-mm-sig@lists.linaro.org --- drivers/dma-buf/fence.c | 58 +++++++++++++++++++++++++++++++++++++++++++ drivers/dma-buf/reservation.c | 48 +++++++++++++++++++++++++++++++++++ include/linux/fence.h | 6 +++++ include/linux/kfence.h | 2 ++ include/linux/reservation.h | 7 ++++++ kernel/kfence.c | 8 ++++++ 6 files changed, 129 insertions(+) diff --git a/drivers/dma-buf/fence.c b/drivers/dma-buf/fence.c index 7b05dbe9b296..3f06b3b1b4cc 100644 --- a/drivers/dma-buf/fence.c +++ b/drivers/dma-buf/fence.c @@ -22,6 +22,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -530,3 +531,60 @@ fence_init(struct fence *fence, const struct fence_ops *ops, trace_fence_init(fence); } EXPORT_SYMBOL(fence_init); + +struct dma_fence_cb { + struct fence_cb base; + struct kfence *fence; +}; + +static void dma_kfence_wake(struct fence *dma, struct fence_cb *data) +{ + struct dma_fence_cb *cb = container_of(data, typeof(*cb), base); + + kfence_complete(cb->fence); + kfence_put(cb->fence); + kfree(cb); +} + +/** + * kfence_await_dma_fence - set the fence to wait upon a DMA fence + * @fence: this kfence + * @dma: target DMA fence to wait upon + * @gfp: the allowed allocation type + * + * kfence_add_dma() causes the @fence to wait upon completion of a DMA fence. + * + * Returns 1 if the @fence was successfully to the waitqueue of @dma, 0 + * if @dma was already signaled (and so not added), or a negative error code. + */ +int kfence_await_dma_fence(struct kfence *fence, struct fence *dma, gfp_t gfp) +{ + struct dma_fence_cb *cb; + int ret; + + if (fence_is_signaled(dma)) + return 0; + + cb = kmalloc(sizeof(*cb), gfp); + if (!cb) { + if (!gfpflags_allow_blocking(gfp)) + return -ENOMEM; + + return fence_wait(dma, false); + } + + cb->fence = kfence_get(fence); + kfence_await(fence); + + ret = fence_add_callback(dma, &cb->base, dma_kfence_wake); + if (ret == 0) { + ret = 1; + } else { + dma_kfence_wake(dma, &cb->base); + if (ret == -ENOENT) /* fence already signaled */ + ret = 0; + } + + return ret; +} +EXPORT_SYMBOL_GPL(kfence_await_dma_fence); diff --git a/drivers/dma-buf/reservation.c b/drivers/dma-buf/reservation.c index 9566a62ad8e3..138b792af0c3 100644 --- a/drivers/dma-buf/reservation.c +++ b/drivers/dma-buf/reservation.c @@ -543,3 +543,51 @@ unlock_retry: goto retry; } EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu); + +/** + * kfence_add_reservation - set the fence to wait upon a reservation_object + * @fence: this kfence + * @resv: target reservation_object (collection of DMA fences) to wait upon + * @write: Wait for read or read/write access + * @gfp: the allowed allocation type + * + * kfence_add_reservation() causes the @fence to wait upon completion of the + * reservation object (a collection of DMA fences), either for read access + * or for read/write access. + * + * Returns 1 if the @fence was successfully to the waitqueues of @resv, 0 + * if @resev was already signaled (and so not added), or a negative error code. + */ +int kfence_await_reservation(struct kfence *fence, + struct reservation_object *resv, + bool write, + gfp_t gfp) +{ + struct fence *excl, **shared; + unsigned int count, i; + int ret; + + ret = reservation_object_get_fences_rcu(resv, &excl, &count, &shared); + if (ret) + return ret; + + if (write) { + for (i = 0; i < count; i++) { + ret |= kfence_await_dma_fence(fence, shared[i], gfp); + if (ret < 0) + goto out; + } + } + + if (excl) + ret |= kfence_await_dma_fence(fence, excl, gfp); + +out: + fence_put(excl); + for (i = 0; i < count; i++) + fence_put(shared[i]); + kfree(shared); + + return ret; +} +EXPORT_SYMBOL_GPL(kfence_await_reservation); diff --git a/include/linux/fence.h b/include/linux/fence.h index 2056e9fd0138..3c3bc318e826 100644 --- a/include/linux/fence.h +++ b/include/linux/fence.h @@ -34,6 +34,8 @@ struct fence; struct fence_ops; struct fence_cb; +struct kfence; + /** * struct fence - software synchronization primitive * @refcount: refcount for this fence @@ -378,4 +380,8 @@ unsigned fence_context_alloc(unsigned num); ##args); \ } while (0) +int kfence_await_dma_fence(struct kfence *fence, + struct fence *dma, + gfp_t gfp); + #endif /* __LINUX_FENCE_H */ diff --git a/include/linux/kfence.h b/include/linux/kfence.h index 76a2f95dfb70..acbfc2ea7c49 100644 --- a/include/linux/kfence.h +++ b/include/linux/kfence.h @@ -16,6 +16,8 @@ #include struct completion; +struct fence; +struct reservation_object; enum hrtimer_mode; struct kfence { diff --git a/include/linux/reservation.h b/include/linux/reservation.h index b0f305e77b7f..1954bab95db9 100644 --- a/include/linux/reservation.h +++ b/include/linux/reservation.h @@ -49,6 +49,8 @@ extern struct ww_class reservation_ww_class; extern struct lock_class_key reservation_seqcount_class; extern const char reservation_seqcount_string[]; +struct kfence; + /** * struct reservation_object_list - a list of shared fences * @rcu: for internal use @@ -210,4 +212,9 @@ long reservation_object_wait_timeout_rcu(struct reservation_object *obj, bool reservation_object_test_signaled_rcu(struct reservation_object *obj, bool test_all); +int kfence_await_reservation(struct kfence *fence, + struct reservation_object *resv, + bool write, + gfp_t gfp); + #endif /* _LINUX_RESERVATION_H */ diff --git a/kernel/kfence.c b/kernel/kfence.c index 59c27910a749..4605eabc2c1b 100644 --- a/kernel/kfence.c +++ b/kernel/kfence.c @@ -7,7 +7,9 @@ * of the License. */ +#include #include +#include #include /** @@ -51,6 +53,12 @@ * - kfence_await_hrtimer(): the kfence asynchronously wait for an expiration * of a timer * + * - kfence_await_dma_fence(): the kfence asynchronously waits for a DMA + * (hardware signaled) fence + * + * - kfence_await_reservation(): the kfence asynchronously waits for a DMA + * reservation object + * * A kfence is initialised using kfence_init(), and starts off awaiting an * event. Once you have finished setting up the fence, including adding * all of its asynchronous waits, call kfence_complete().