Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp4206912ybh; Tue, 17 Mar 2020 14:24:58 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtxgkavkv06tqgCO0eM+DLFEiJxu99OpQbKLJGW5MXXcbnx2+0Zmq4Y6+Q0leSPcXSVo+Cb X-Received: by 2002:a9d:53c4:: with SMTP id i4mr1241622oth.48.1584480298782; Tue, 17 Mar 2020 14:24:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584480298; cv=none; d=google.com; s=arc-20160816; b=rPzK09wfw7cZlTTH6czkoi/MoRxgWBRwx97S3DuOOdcQymhfQuGeQNrukrT9UKDhqs L70NxMjKHqRt76xMXp5mXG/v58Ylkp+3AEGuAfyrYgCbm9a7jKINu3gEZZXaHf874O1b Ukgh5XXb9c6HEz884fMHoh/EP5WuY4VFjVv70FulpmZ29fFB82zU+a4SK2IUc8UqZH8A HGL4c+R9LOKcrHLV8kMERE2MTEmJrjhwcF9k28uiTt1W69p7bmQ+iYn2W6vHxokBVJvN 0RBPQF2pceGw1/i5lPG3qab3/5D2tgqqysNN/7rSd+IAOvn3A9XSBjBu1rKUX+jLGm9O pdWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:from :dkim-signature; bh=jcoY8Zv5ldWOcQg9c9S82nSPUSBzSFSRHAdLvHZUCvo=; b=zXb4mskpYkyhdIoveJJvvxoMe3WKxUC1mcY2XOL0v8ykmUnPZYPwDq5c6gGg0uMjjw WBN9Osp3S8RC18b21qw6+KA9sKlsX+dO0OwdiHdekYmzk/12/NTVyAazaLtQjSrb/j5y ELKYJRbW4AyEpusZQGiZ0NDJKUg00PhZqoTtFM+lb5UgG39lCiiN5xGb6v3DdNXqvE0c RD6BTC4vCeALQQ1Js+sx1SsRgvx7cGov6glSUUfl3Jp31bIrlcUkwXm5zhP0ElH9d8AJ qyn70oG/Q0IeSQZkjdy77XDvd9p8QMbX2U0nRO0Ad+0fi5H9M6ez2dFa5JHJOHtnYok3 593Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@jlekstrand-net.20150623.gappssmtp.com header.s=20150623 header.b=YU13BVIr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l185si2237742oif.274.2020.03.17.14.24.45; Tue, 17 Mar 2020 14:24:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@jlekstrand-net.20150623.gappssmtp.com header.s=20150623 header.b=YU13BVIr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726761AbgCQVVk (ORCPT + 99 others); Tue, 17 Mar 2020 17:21:40 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:42155 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726494AbgCQVVk (ORCPT ); Tue, 17 Mar 2020 17:21:40 -0400 Received: by mail-pg1-f196.google.com with SMTP id h8so12413244pgs.9 for ; Tue, 17 Mar 2020 14:21:39 -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=jcoY8Zv5ldWOcQg9c9S82nSPUSBzSFSRHAdLvHZUCvo=; b=YU13BVIrZ5/2E7dWu6Deuj2TkAksebpqKYWjq03Q2pwGLx5YETjkI/keI2njmjVtb9 6De3yh3kHxDzh59pqgVS89EJSJOQhF9bJqgt9idcLYtInaMyQmDZGJHT1RPNlIxkop5z UhL3NYjCRf2Jd+Rrj/z1iZCG1Fz30todcoluOEGyzpb8PIIdwl0VLL0ujhvtXDnK6xCP eT6ochriC7LKAzwYOB9kTIo79oAK/Xo3wZgZ1J3oNb7exyFiT09QUqIes1PBubRc+HQd W63UWQ4g0NMcGgSL9iScCHBDtEv+mh7FL24/CyraNr/BmxoN9jkDU0+6EvnedPcHPmv4 x/Qw== 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=jcoY8Zv5ldWOcQg9c9S82nSPUSBzSFSRHAdLvHZUCvo=; b=MRRsabqV1wKlzNQav8IZc7g6dM/bVKeW3kVG2/nHUqWhkA1/DMsw0xga8uNkyk2wkE 0h40IVIlu8sEtljw/fjW/JyskGIeGZtGW6iptdoOo6i1Hfcy3dY5A/V21t39xQE7U3QA F6XOObhFr17Rhz350aRyuUgflSsEyeq2d/B1g2DQpPsZoIYa3MWNLs1GD8Wfs3wh9A8N OSwebTEO6ZebA41saBBhMiPKS+ZV82aBt5ga6ufoLvqL9lqOrmkRherJTCP3S91i9Ryo C/TnwYni8+SiBLPU4CMp0QyQ6KFK7ManYCzn8M2G9N61s78gqkAb5xN22fzhDxtfD1z4 eiqg== X-Gm-Message-State: ANhLgQ2h1xkpJxH6Rdv6tSpF5CI29/YpPYSDGXtHzz9Ucpb2ICC4ZQ/5 K4e/O9R9D+BZUda2iaePVKGrsg== X-Received: by 2002:a63:c445:: with SMTP id m5mr1150883pgg.194.1584480098604; Tue, 17 Mar 2020 14:21:38 -0700 (PDT) Received: from omlet.com ([2605:6000:1026:c273::ce4]) by smtp.gmail.com with ESMTPSA id u5sm4185087pfb.153.2020.03.17.14.21.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Mar 2020 14:21:37 -0700 (PDT) From: Jason Ekstrand Cc: airlied@redhat.com, christian.koenig@amd.com, jessehall@google.com, jajones@nvidia.com, daniels@collabora.com, hoegsberg@google.com, daniel.vetter@ffwll.ch, bas@basnieuwenhuizen.nl, Jason Ekstrand , Sumit Semwal , Chenbo Feng , Greg Hackmann , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/3] RFC: dma-buf: Add an API for importing and exporting sync files (v5) Date: Tue, 17 Mar 2020 16:21:14 -0500 Message-Id: <20200317212115.419358-1-jason@jlekstrand.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200311034351.1275197-3-jason@jlekstrand.net> References: <20200311034351.1275197-3-jason@jlekstrand.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 v5 (Jason Ekstrand): - Rename the IOCTLs to import/export rather than wait/signal - Drop the WRITE flag and always get/set the exclusive fence Signed-off-by: Jason Ekstrand --- drivers/dma-buf/dma-buf.c | 84 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/dma-buf.h | 11 ++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index d4097856c86b..d34d27aa3077 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,83 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf) return ret; } +static long dma_buf_import_sync_file(struct dma_buf *dmabuf, + const void __user *user_data) +{ + struct dma_buf_sync_file arg; + struct dma_fence *fence, *singleton = NULL; + int ret = 0; + + if (copy_from_user(&arg, user_data, sizeof(arg))) + return -EFAULT; + + if (arg.flags != 0) + return -EINVAL; + + fence = sync_file_get_fence(arg.fd); + if (!fence) + return -EINVAL; + + dma_resv_lock(dmabuf->resv, NULL); + + ret = dma_resv_get_singleton(dmabuf->resv, fence, &singleton); + if (!ret && singleton) + dma_resv_add_excl_fence(dmabuf->resv, singleton); + + dma_resv_unlock(dmabuf->resv); + + dma_fence_put(fence); + + return ret; +} + +static long dma_buf_export_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) + return -EINVAL; + + fd = get_unused_fd_flags(O_CLOEXEC); + if (fd < 0) + return fd; + + ret = dma_resv_get_singleton(dmabuf->resv, NULL, &fence); + if (ret) + goto err_put_fd; + + 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 +468,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_IMPORT_SYNC_FILE: + return dma_buf_import_sync_file(dmabuf, (const void __user *)arg); + + case DMA_BUF_IOCTL_EXPORT_SYNC_FILE: + return dma_buf_export_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..b746c6459e24 100644 --- a/include/uapi/linux/dma-buf.h +++ b/include/uapi/linux/dma-buf.h @@ -37,8 +37,15 @@ struct dma_buf_sync { #define DMA_BUF_NAME_LEN 32 +struct dma_buf_sync_file { + __u32 flags; + __s32 fd; +}; + #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_IMPORT_SYNC_FILE _IOW(DMA_BUF_BASE, 2, struct dma_buf_sync) +#define DMA_BUF_IOCTL_EXPORT_SYNC_FILE _IOWR(DMA_BUF_BASE, 3, struct dma_buf_sync) #endif -- 2.24.1