From patchwork Wed Mar 11 03:43:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jason Ekstrand X-Patchwork-Id: 11430611 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C81EE138D for ; Wed, 11 Mar 2020 03:44:10 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A65DE215A4 for ; Wed, 11 Mar 2020 03:44:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=jlekstrand-net.20150623.gappssmtp.com header.i=@jlekstrand-net.20150623.gappssmtp.com header.b="tCu4TDgu" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A65DE215A4 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=jlekstrand.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 92F336E40B; Wed, 11 Mar 2020 03:44:09 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9F9B189FE3 for ; Wed, 11 Mar 2020 03:44:07 +0000 (UTC) Received: by mail-pf1-x443.google.com with SMTP id i13so504082pfe.3 for ; Tue, 10 Mar 2020 20:44:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jlekstrand-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=P16W7ep8HpZEZbf2VCVYHJ7H+KUUPSbN1YkIr+6sSlQ=; b=tCu4TDguooR4tfYyACvCoI7MEVLwPSOiIShWk5/IPARU5TQg0GCHpx5pvxZfZ/2jdb vz0gO9H8hR1jEQgMkpKTiBJoiS7EtJ5+Re21/zT/7+dEqFcH4QhtEq7iklHDQz8aZ4ED h5o+fe9jj8aLNJTdEiFpmQESnjguXQE+DxAy48m+ri2NqUj0ck/EJ53Od44WbappO0u6 M5PMHUWsJ1AUzq32iKUreC8B7vS4OMjdorltjcDguKGnPO99on/KtQaiHpEBS1en5UC6 brO2nCR/2wZiFp6/kxIWfN0gQg4kxueOlrX2kwHyBurpODi7jr2aWxqMP0R/7BccEPV3 ysWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=P16W7ep8HpZEZbf2VCVYHJ7H+KUUPSbN1YkIr+6sSlQ=; b=aZ+edh3+1Yr0ne68jmVjOT7oDDZPfwGrhhOa6JRbiB9FmTUzKU9srGvCoJxoS02Dbf wTOhcwXuopzONN2/YEzJYci12Gv1aDFN7zs/zNjvuMmwgOpcuiv5J+vKI+mIjhQDIt6r ckA3RSaJZyqaB5pNdjeTYbvWgOgffF4uD5OpuoyexTuujrrH5siyoBxeWq2W2ErDk/y2 F7vYt8AiImGlk+zBXagPpbWT7uV7C7G019Zd3ekCozVhJkhyIfU7wAql8gsqGKHP5cab 0bcP6Y6UiUuGG4I+N3dJdCltbPqMffK93M0OWMB3/Ebn1qDSrkyquVMUB+j44QLjC/hP AvrA== X-Gm-Message-State: ANhLgQ29feAQK6AF73dI2BJDR3qDmDm11dPPBfMQSDx8xOZOmwkztF7J vioSueanUp+BPPtkKOg1oi+2ig== X-Google-Smtp-Source: ADFU+vuNjXuDPqhg8zCHX7akIbl+E1cdU/rjV0v8i4t9fu91z6NCzwwYTmCz8DLIFQqRHKFy7riJrw== X-Received: by 2002:a63:7e1a:: with SMTP id z26mr900050pgc.226.1583898247079; Tue, 10 Mar 2020 20:44:07 -0700 (PDT) Received: from omlet.com ([2605:6000:1026:c273::ce4]) by smtp.gmail.com with ESMTPSA id bb13sm3650846pjb.43.2020.03.10.20.44.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2020 20:44:06 -0700 (PDT) From: Jason Ekstrand To: Subject: [PATCH 1/3] dma-buf: add dma_fence_array_for_each (v2) Date: Tue, 10 Mar 2020 22:43:46 -0500 Message-Id: <20200311034351.1275197-1-jason@jlekstrand.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200303190318.522103-1-jason@jlekstrand.net> References: <20200303190318.522103-1-jason@jlekstrand.net> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kate Stewart , daniel.vetter@ffwll.ch, dri-devel@lists.freedesktop.org, Richard Fontana , Jason Ekstrand , jessehall@google.com, jajones@nvidia.com, Greg Hackmann , Gustavo Padovan , airlied@redhat.com, linux-media@vger.kernel.org, daniels@collabora.com, =?utf-8?q?Christian_K?= =?utf-8?q?=C3=B6nig?= , Chenbo Feng , linaro-mm-sig@lists.linaro.org, Thomas Gleixner , Allison Randal , linux-kernel@vger.kernel.org, hoegsberg@google.com, christian.koenig@amd.com Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Christian König Add a helper to iterate over all fences in a dma_fence_array object. v2 (Jason Ekstrand) - Return NULL from dma_fence_array_first if head == NULL. This matches the iterator behavior of dma_fence_chain_for_each in that it iterates zero times if head == NULL. - Return NULL from dma_fence_array_next if index > array->num_fences. Signed-off-by: Jason Ekstrand --- drivers/dma-buf/dma-fence-array.c | 27 +++++++++++++++++++++++++++ include/linux/dma-fence-array.h | 17 +++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/drivers/dma-buf/dma-fence-array.c b/drivers/dma-buf/dma-fence-array.c index d3fbd950be94..2ac1afc697d0 100644 --- a/drivers/dma-buf/dma-fence-array.c +++ b/drivers/dma-buf/dma-fence-array.c @@ -201,3 +201,30 @@ bool dma_fence_match_context(struct dma_fence *fence, u64 context) return true; } EXPORT_SYMBOL(dma_fence_match_context); + +struct dma_fence *dma_fence_array_first(struct dma_fence *head) +{ + struct dma_fence_array *array; + + if (!head) + return NULL; + + array = to_dma_fence_array(head); + if (!array) + return head; + + return array->fences[0]; +} +EXPORT_SYMBOL(dma_fence_array_first); + +struct dma_fence *dma_fence_array_next(struct dma_fence *head, + unsigned int index) +{ + struct dma_fence_array *array = to_dma_fence_array(head); + + if (!array || index >= array->num_fences) + return NULL; + + return array->fences[index]; +} +EXPORT_SYMBOL(dma_fence_array_next); diff --git a/include/linux/dma-fence-array.h b/include/linux/dma-fence-array.h index 303dd712220f..588ac8089dd6 100644 --- a/include/linux/dma-fence-array.h +++ b/include/linux/dma-fence-array.h @@ -74,6 +74,19 @@ to_dma_fence_array(struct dma_fence *fence) return container_of(fence, struct dma_fence_array, base); } +/** + * dma_fence_array_for_each - iterate over all fences in array + * @fence: current fence + * @index: index into the array + * @head: potential dma_fence_array object + * + * Test if @array is a dma_fence_array object and if yes iterate over all fences + * in the array. If not just iterate over the fence in @array itself. + */ +#define dma_fence_array_for_each(fence, index, head) \ + for (index = 0, fence = dma_fence_array_first(head); fence; \ + ++(index), fence = dma_fence_array_next(head, index)) + struct dma_fence_array *dma_fence_array_create(int num_fences, struct dma_fence **fences, u64 context, unsigned seqno, @@ -81,4 +94,8 @@ struct dma_fence_array *dma_fence_array_create(int num_fences, bool dma_fence_match_context(struct dma_fence *fence, u64 context); +struct dma_fence *dma_fence_array_first(struct dma_fence *head); +struct dma_fence *dma_fence_array_next(struct dma_fence *head, + unsigned int index); + #endif /* __LINUX_DMA_FENCE_ARRAY_H */ From patchwork Wed Mar 11 03:43:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jason Ekstrand X-Patchwork-Id: 11430615 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A099492A for ; Wed, 11 Mar 2020 03:44:19 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7EA8921D56 for ; Wed, 11 Mar 2020 03:44:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=jlekstrand-net.20150623.gappssmtp.com header.i=@jlekstrand-net.20150623.gappssmtp.com header.b="s7wtNHBC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7EA8921D56 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=jlekstrand.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 84CE56E8F2; Wed, 11 Mar 2020 03:44:18 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8B96F6E8F2 for ; Wed, 11 Mar 2020 03:44:17 +0000 (UTC) Received: by mail-pj1-x1042.google.com with SMTP id ca13so269620pjb.2 for ; Tue, 10 Mar 2020 20:44:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jlekstrand-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=A3HV2hQYHuH1vQwVenAj0XSB1K+Jb6LvuTN+B44zVl8=; b=s7wtNHBCCDl9SZ3sQ72zz65OKdEzZG/DXWLAEvIOsUSGHo9ezh4eyN0XInDRxwcj6N oW1WZMHBLOthXxv6vDZhfGr43ymZ8ZuPZShetrqutJSbm5GmtpZrgQBrQfTviDMm2I0g F2uOxUjYoqSEykfQV5EoCef+iIRbRdh2+ViAsjW5qpVVzrLkYfrOWxQ1IxAjzd5gmkLD JwCMGkz065KqMUJRmDvmSqnOEgHmdU44C8kfWvW9GJXCzXZqIlcvFQdrJpdSU8+zfodV MM7UElsUnlusu4Mc/bxGKiVAAqy46GyPuTPODzj+TdN22/YDKJ4tRWq4jTH33RJbu4qz 4keg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=A3HV2hQYHuH1vQwVenAj0XSB1K+Jb6LvuTN+B44zVl8=; b=mb8+fuVgUOqnyZ3K/IszIV7LlygRo2ws2Lxj1WQAQSR6pQ1/KD+IBwh7n7rT3lmFZn n4vWhIELUYK1Csc6c05szhx9quw8OJP8HRFKFH/6qTRkmFGBb5SM3FRDUCXfOonPHBzs XPCazCxuz/F8XA1AJHOPMbNIYLDb1wW23fZWp2+sXJ2fbxXHQyrDZIMQyObtwYbwbh6Z ZPOlEcfNQvlROBY44gTPqtMm0y9Ew+36dyB44iKbYBT1VY3g+NmvVLIxhlEGxziEvDP6 bTFE/HaK0TXVL1MjNtnuY+yLNy+s8ruTsIfaJGSltHaZrosGeHbMXHFAjQE7AaDU/lNe EaTw== X-Gm-Message-State: ANhLgQ2jdo+ph+Dysd8yRvNxbjn4yYZT+6gIwMOgHrLNd0iYUzENPFax 05sdOkzwYVYlvp0aW5uwb2YFsA== X-Google-Smtp-Source: ADFU+vtjHVhUoR4M70vM1lhkKff1iZicUhqEePACChVjl3VEImSuyNEPrj5Jz7Zoqz/ADlVZ4k7rcw== X-Received: by 2002:a17:90b:23d1:: with SMTP id md17mr1218411pjb.145.1583898257006; Tue, 10 Mar 2020 20:44:17 -0700 (PDT) Received: from omlet.com ([2605:6000:1026:c273::ce4]) by smtp.gmail.com with ESMTPSA id bb13sm3650846pjb.43.2020.03.10.20.44.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2020 20:44:16 -0700 (PDT) From: Jason Ekstrand To: Subject: [PATCH 2/3] dma-buf: add dma_resv_get_singleton (v2) Date: Tue, 10 Mar 2020 22:43:47 -0500 Message-Id: <20200311034351.1275197-2-jason@jlekstrand.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200311034351.1275197-1-jason@jlekstrand.net> References: <20200303190318.522103-1-jason@jlekstrand.net> <20200311034351.1275197-1-jason@jlekstrand.net> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Christian_K=C3=B6nig?= , Chenbo Feng , daniels@collabora.com, daniel.vetter@ffwll.ch, jajones@nvidia.com, linux-kernel@vger.kernel.org, Greg Hackmann , linaro-mm-sig@lists.linaro.org, hoegsberg@google.com, dri-devel@lists.freedesktop.org, Jason Ekstrand , jessehall@google.com, airlied@redhat.com, christian.koenig@amd.com, linux-media@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Christian König Add a helper function to get a single fence representing all fences in a dma_resv object. This fence is either the only one in the object or all not signaled fences of the object in a flatted out dma_fence_array. v2 (Jason Ekstrand): - Take reference of fences both for creating the dma_fence_array and in the case where we return one fence. - Handle the case where dma_resv_get_list() returns NULL Signed-off-by: Jason Ekstrand --- drivers/dma-buf/dma-resv.c | 118 +++++++++++++++++++++++++++++++++++++ include/linux/dma-resv.h | 3 + 2 files changed, 121 insertions(+) diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c index 4264e64788c4..66591d8ab7ef 100644 --- a/drivers/dma-buf/dma-resv.c +++ b/drivers/dma-buf/dma-resv.c @@ -33,6 +33,8 @@ */ #include +#include +#include #include #include @@ -47,6 +49,19 @@ * write-side updates. */ +/** + * dma_fence_deep_dive_for_each - deep dive into the fence containers + * @fence: resulting fence + * @chain: variable for a dma_fence_chain + * @index: index into a dma_fence_array + * @head: starting point + * + * Helper to deep dive into the fence containers for flattening them. + */ +#define dma_fence_deep_dive_for_each(fence, chain, index, head) \ + dma_fence_chain_for_each(chain, head) \ + dma_fence_array_for_each(fence, index, chain) + DEFINE_WD_CLASS(reservation_ww_class); EXPORT_SYMBOL(reservation_ww_class); @@ -516,6 +531,109 @@ int dma_resv_get_fences_rcu(struct dma_resv *obj, } EXPORT_SYMBOL_GPL(dma_resv_get_fences_rcu); +/** + * dma_resv_get_singleton - get a single fence for the dma_resv object + * @obj: the reservation object + * @extra: extra fence to add to the resulting array + * @result: resulting dma_fence + * + * Get a single fence representing all unsignaled fences in the dma_resv object + * plus the given extra fence. If we got only one fence return a new + * reference to that, otherwise return a dma_fence_array object. + * + * RETURNS + * Returns -NOMEM if allocations fail, zero otherwise. + */ +int dma_resv_get_singleton(struct dma_resv *obj, struct dma_fence *extra, + struct dma_fence **result) +{ + struct dma_resv_list *fobj = dma_resv_get_list(obj); + struct dma_fence *excl = dma_resv_get_excl(obj); + struct dma_fence *fence, *chain, **fences; + struct dma_fence_array *array; + unsigned int num_fences, shared_count; + unsigned int i, j; + + num_fences = 0; + *result = NULL; + + dma_fence_deep_dive_for_each(fence, chain, i, extra) { + if (dma_fence_is_signaled(fence)) + continue; + + *result = fence; + ++num_fences; + } + + dma_fence_deep_dive_for_each(fence, chain, i, excl) { + if (dma_fence_is_signaled(fence)) + continue; + + *result = fence; + ++num_fences; + } + + shared_count = fobj ? fobj->shared_count : 0; + for (i = 0; i < shared_count; ++i) { + struct dma_fence *f; + + f = rcu_dereference_protected(fobj->shared[i], + dma_resv_held(obj)); + dma_fence_deep_dive_for_each(fence, chain, j, f) { + if (dma_fence_is_signaled(fence)) + continue; + + *result = fence; + ++num_fences; + } + } + + if (num_fences <= 1) { + *result = dma_fence_get(*result); + return 0; + } + + fences = kmalloc_array(num_fences, sizeof(struct dma_fence*), + GFP_KERNEL); + if (!fences) + return -ENOMEM; + + num_fences = 0; + + dma_fence_deep_dive_for_each(fence, chain, i, extra) + if (!dma_fence_is_signaled(fence)) + fences[num_fences++] = dma_fence_get(fence); + + dma_fence_deep_dive_for_each(fence, chain, i, excl) + if (!dma_fence_is_signaled(fence)) + fences[num_fences++] = dma_fence_get(fence); + + for (i = 0; i < shared_count; ++i) { + struct dma_fence *f; + + f = rcu_dereference_protected(fobj->shared[i], + dma_resv_held(obj)); + dma_fence_deep_dive_for_each(fence, chain, j, f) + if (!dma_fence_is_signaled(fence)) + fences[num_fences++] = dma_fence_get(fence); + } + + array = dma_fence_array_create(num_fences, fences, + dma_fence_context_alloc(1), + 1, false); + if (!array) + goto error_free; + + *result = &array->base; + return 0; + +error_free: + while (num_fences--) + dma_fence_put(fences[num_fences]); + kfree(fences); + return -ENOMEM; +} + /** * dma_resv_wait_timeout_rcu - Wait on reservation's objects * shared and/or exclusive fences. diff --git a/include/linux/dma-resv.h b/include/linux/dma-resv.h index ee50d10f052b..d50e753e4550 100644 --- a/include/linux/dma-resv.h +++ b/include/linux/dma-resv.h @@ -287,6 +287,9 @@ int dma_resv_get_fences_rcu(struct dma_resv *obj, int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src); +int dma_resv_get_singleton(struct dma_resv *obj, struct dma_fence *extra, + struct dma_fence **result); + long dma_resv_wait_timeout_rcu(struct dma_resv *obj, bool wait_all, bool intr, unsigned long timeout); From patchwork Wed Mar 11 03:43:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Ekstrand X-Patchwork-Id: 11430619 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3E008138D for ; Wed, 11 Mar 2020 03:44:24 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1C80C21D56 for ; Wed, 11 Mar 2020 03:44:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=jlekstrand-net.20150623.gappssmtp.com header.i=@jlekstrand-net.20150623.gappssmtp.com header.b="V5wbqKyJ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1C80C21D56 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=jlekstrand.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3A07D6E8F3; Wed, 11 Mar 2020 03:44:23 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by gabe.freedesktop.org (Postfix) with ESMTPS id 242E96E8F3 for ; Wed, 11 Mar 2020 03:44:22 +0000 (UTC) Received: by mail-pj1-x1044.google.com with SMTP id u10so263102pjy.0 for ; Tue, 10 Mar 2020 20:44:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jlekstrand-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hMsZ5n2u+PGel/PKPO0Vzp2oWzKhB5uQ11WvL+UIaGQ=; b=V5wbqKyJdEhdokczEP+Q5Y6Dyulhe9f0DbqMR2ZIGhbprsU5DH44E5w3d5AO9p5kbu iIK59Mz4eF6hX2NQwg+IyXgDMvDI6El7sRYmq/rNCo9nHUxicwczbY6PtvXjB5pZ+n0o 1gqYd6xP6287IAfRlWETYX8bFynx313rFUmRUb9Qn1giSAvjFU0cV0VpuCqclcUqUSle swVCzsasC1RUT2WJNLArpISSY3A2S/rUi8ehqlm3KXPEIDdmeIi+EAmK/aMt9Inpeyc5 LumXVbgjvQwS5wEJIssoCybhdSwtxVnBOkm1LGUkzO2iaWFmAXeJqHbVZ3a5GORFi+BF p4Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hMsZ5n2u+PGel/PKPO0Vzp2oWzKhB5uQ11WvL+UIaGQ=; b=Ta0eXIhsjiAUs0M3xLilbfHovn6/oZtUW6vszdeqkl9ZpqQzD66+iYdQYLyfrBjlcA 4ejzpizzLGeKNmtOfWX1BuqF0yHRbMjnzT3fwKvJfJNEwRNUHJ1xzTT5aqO9eDD0cQ5h asDZSJOpxRyb9kf/aS1shA+42b+ImR8B7XxioC5hrozqxOGr+fg5HlTwveBTVvpuwCbF Mgp8A/zu2jwTb98dDmTXtHtqrJ5YRb5Sv1h4uL2TLjcILkwdIhWhyVt+JAO3YTKAgCUY ZHyYxxZ0ioY5XvZ5IEXxExWkmDyH/1tKBEkjTs+/JXWCCh9p9f7iqYlx47OUtg1niqSp OxUg== X-Gm-Message-State: ANhLgQ0f+0O5hi3/r0XJxnS6+iuvuuIqImOmAjbvK9NpxuSbzAvo/MK6 av+bkRXJTSAPdbFDrbRd7kmRAQ== X-Google-Smtp-Source: ADFU+vsyc38HlAKduJYbrL0y2cac0FOPPUwBSZxhe9PfTZjeQKeX1PZpmFU0vNIubE/s8pv2abm3Yw== X-Received: by 2002:a17:902:d703:: with SMTP id w3mr1144366ply.264.1583898261589; Tue, 10 Mar 2020 20:44:21 -0700 (PDT) Received: from omlet.com ([2605:6000:1026:c273::ce4]) by smtp.gmail.com with ESMTPSA id bb13sm3650846pjb.43.2020.03.10.20.44.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2020 20:44:20 -0700 (PDT) From: Jason Ekstrand To: Subject: [PATCH 3/3] RFC: dma-buf: Add an API for importing and exporting sync files (v4) Date: Tue, 10 Mar 2020 22:43:48 -0500 Message-Id: <20200311034351.1275197-3-jason@jlekstrand.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200311034351.1275197-1-jason@jlekstrand.net> References: <20200303190318.522103-1-jason@jlekstrand.net> <20200311034351.1275197-1-jason@jlekstrand.net> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Chenbo Feng , daniels@collabora.com, daniel.vetter@ffwll.ch, jajones@nvidia.com, linux-kernel@vger.kernel.org, Greg Hackmann , linaro-mm-sig@lists.linaro.org, hoegsberg@google.com, dri-devel@lists.freedesktop.org, Jason Ekstrand , jessehall@google.com, airlied@redhat.com, christian.koenig@amd.com, linux-media@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Explicit synchronization is the future. At least, that seems to be what most userspace APIs are agreeing on at this point. However, most of our Linux APIs (both userspace and kernel UAPI) are currently built around implicit synchronization with dma-buf. While work is ongoing to change many of the userspace APIs and protocols to an explicit synchronization model, switching over piecemeal is difficult due to the number of potential components involved. On the kernel side, many drivers use dma-buf including GPU (3D/compute), display, v4l, and others. In userspace, we have X11, several Wayland compositors, 3D drivers, compute drivers (OpenCL etc.), media encode/decode, and the list goes on. This patch provides a path forward by allowing userspace to manually manage the fences attached to a dma-buf. Alternatively, one can think of this as making dma-buf's implicit synchronization simply a carrier for an explicit fence. This is accomplished by adding two IOCTLs to dma-buf for importing and exporting a sync file to/from the dma-buf. This way a userspace component which is uses explicit synchronization, such as a Vulkan driver, can manually set the write fence on a buffer before handing it off to an implicitly synchronized component such as a Wayland compositor or video encoder. In this way, each of the different components can be upgraded to an explicit synchronization model one at a time as long as the userspace pieces connecting them are aware of it and import/export fences at the right times. There is a potential race condition with this API if userspace is not careful. A typical use case for implicit synchronization is to wait for the dma-buf to be ready, use it, and then signal it for some other component. Because a sync_file cannot be created until it is guaranteed to complete in finite time, userspace can only signal the dma-buf after it has already submitted the work which uses it to the kernel and has received a sync_file back. There is no way to atomically submit a wait-use-signal operation. This is not, however, really a problem with this API so much as it is a problem with explicit synchronization itself. The way this is typically handled is to have very explicit ownership transfer points in the API or protocol which ensure that only one component is using it at any given time. Both X11 (via the PRESENT extension) and Wayland provide such ownership transfer points via explicit present and idle messages. The decision was intentionally made in this patch to make the import and export operations IOCTLs on the dma-buf itself rather than as a DRM IOCTL. This makes it the import/export operation universal across all components which use dma-buf including GPU, display, v4l, and others. It also means that a userspace component can do the import/export without access to the DRM fd which may be tricky to get in cases where the client communicates with DRM via a userspace API such as OpenGL or Vulkan. At a future date we may choose to add direct import/export APIs to components such as drm_syncobj to avoid allocating a file descriptor and going through two ioctls. However, that seems to be something of a micro-optimization as import/export operations are likely to happen at a rate of a few per frame of rendered or decoded video. v2 (Jason Ekstrand): - Use a wrapper dma_fence_array of all fences including the new one when importing an exclusive fence. v3 (Jason Ekstrand): - Lock around setting shared fences as well as exclusive - Mark SIGNAL_SYNC_FILE as a read-write ioctl. - Initialize ret to 0 in dma_buf_wait_sync_file v4 (Jason Ekstrand): - Use the new dma_resv_get_singleton helper Signed-off-by: Jason Ekstrand --- drivers/dma-buf/dma-buf.c | 96 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/dma-buf.h | 13 ++++- 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index d4097856c86b..09973c689866 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -348,6 +349,95 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf) return ret; } +static long dma_buf_wait_sync_file(struct dma_buf *dmabuf, + const void __user *user_data) +{ + struct dma_buf_sync_file arg; + struct dma_fence *fence; + int ret = 0; + + if (copy_from_user(&arg, user_data, sizeof(arg))) + return -EFAULT; + + if (arg.flags != 0 && arg.flags != DMA_BUF_SYNC_FILE_SYNC_WRITE) + return -EINVAL; + + fence = sync_file_get_fence(arg.fd); + if (!fence) + return -EINVAL; + + dma_resv_lock(dmabuf->resv, NULL); + + if (arg.flags & DMA_BUF_SYNC_FILE_SYNC_WRITE) { + struct dma_fence *singleton = NULL; + ret = dma_resv_get_singleton(dmabuf->resv, fence, &singleton); + if (!ret && singleton) + dma_resv_add_excl_fence(dmabuf->resv, singleton); + } else { + dma_resv_add_shared_fence(dmabuf->resv, fence); + } + + dma_resv_unlock(dmabuf->resv); + + dma_fence_put(fence); + + return ret; +} + +static long dma_buf_signal_sync_file(struct dma_buf *dmabuf, + void __user *user_data) +{ + struct dma_buf_sync_file arg; + struct dma_fence *fence = NULL; + struct sync_file *sync_file; + int fd, ret; + + if (copy_from_user(&arg, user_data, sizeof(arg))) + return -EFAULT; + + if (arg.flags != 0 && arg.flags != DMA_BUF_SYNC_FILE_SYNC_WRITE) + return -EINVAL; + + fd = get_unused_fd_flags(O_CLOEXEC); + if (fd < 0) + return fd; + + if (arg.flags & DMA_BUF_SYNC_FILE_SYNC_WRITE) { + /* We need to include both the exclusive fence and all of + * the shared fences in our fence. + */ + ret = dma_resv_get_singleton(dmabuf->resv, NULL, &fence); + if (ret) + goto err_put_fd; + } else { + fence = dma_resv_get_excl_rcu(dmabuf->resv); + } + + if (!fence) + fence = dma_fence_get_stub(); + + sync_file = sync_file_create(fence); + + dma_fence_put(fence); + + if (!sync_file) { + ret = -EINVAL; + goto err_put_fd; + } + + fd_install(fd, sync_file->file); + + arg.fd = fd; + if (copy_to_user(user_data, &arg, sizeof(arg))) + return -EFAULT; + + return 0; + +err_put_fd: + put_unused_fd(fd); + return ret; +} + static long dma_buf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { @@ -390,6 +480,12 @@ static long dma_buf_ioctl(struct file *file, case DMA_BUF_SET_NAME: return dma_buf_set_name(dmabuf, (const char __user *)arg); + case DMA_BUF_IOCTL_WAIT_SYNC_FILE: + return dma_buf_wait_sync_file(dmabuf, (const void __user *)arg); + + case DMA_BUF_IOCTL_SIGNAL_SYNC_FILE: + return dma_buf_signal_sync_file(dmabuf, (void __user *)arg); + default: return -ENOTTY; } diff --git a/include/uapi/linux/dma-buf.h b/include/uapi/linux/dma-buf.h index dbc7092e04b5..86e07acca90c 100644 --- a/include/uapi/linux/dma-buf.h +++ b/include/uapi/linux/dma-buf.h @@ -37,8 +37,17 @@ struct dma_buf_sync { #define DMA_BUF_NAME_LEN 32 +struct dma_buf_sync_file { + __u32 flags; + __s32 fd; +}; + +#define DMA_BUF_SYNC_FILE_SYNC_WRITE (1 << 0) + #define DMA_BUF_BASE 'b' -#define DMA_BUF_IOCTL_SYNC _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync) -#define DMA_BUF_SET_NAME _IOW(DMA_BUF_BASE, 1, const char *) +#define DMA_BUF_IOCTL_SYNC _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync) +#define DMA_BUF_SET_NAME _IOW(DMA_BUF_BASE, 1, const char *) +#define DMA_BUF_IOCTL_WAIT_SYNC_FILE _IOW(DMA_BUF_BASE, 2, struct dma_buf_sync) +#define DMA_BUF_IOCTL_SIGNAL_SYNC_FILE _IOWR(DMA_BUF_BASE, 3, struct dma_buf_sync) #endif