From patchwork Fri Apr 7 19:32:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Wilson X-Patchwork-Id: 9670315 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 1A195602B3 for ; Fri, 7 Apr 2017 19:32:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0503A2787C for ; Fri, 7 Apr 2017 19:32:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ED7AF27FBE; Fri, 7 Apr 2017 19:32:17 +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=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7A7E32787C for ; Fri, 7 Apr 2017 19:32:16 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 895BF6ED07; Fri, 7 Apr 2017 19:32:15 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x242.google.com (mail-wm0-x242.google.com [IPv6:2a00:1450:400c:c09::242]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0E7B76ED07 for ; Fri, 7 Apr 2017 19:32:14 +0000 (UTC) Received: by mail-wm0-x242.google.com with SMTP id o81so2924332wmb.0 for ; Fri, 07 Apr 2017 12:32:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id; bh=2NWpTHLjW/8/y5J82fnMDwVQJ/OSpT9U+NqxAbP3kno=; b=fzbKXns/3JeU5fCuyDcTIDqT6TZkOqOLwfWlft+a2ubll0i9ByWFHoCVfmhSTLSTce 23bf2ltdJA0p8oxhS+y0/5JwySHS5oay8hwY4XSGB3bie48u2BlL5r/Y37mplEbREqFk zYqEOnbb1ZXX0Y1GWm0TUwM6JUNh54KMt2aZ5HfrtGzF0ZtjoTcCB4JrxkyZY8ZFnZuN vCMfToWUQlyyKmZwTklRZoZjHgZLNkvpEXMCS08xdgyALTXbAMdIUlvx9Ej3xrjiMIv/ eqZTQMbmhSr3caVH1ZAUdjZCqB4QZY9Y87RS5Ml+g67FUWo4QeF+LS/kaqCYkUfT9MBR pupw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=2NWpTHLjW/8/y5J82fnMDwVQJ/OSpT9U+NqxAbP3kno=; b=AjHPkfeWjhvm32xF90D+BSuNzv+6DmtXR4UsvNdP+lfdXywdqL+LrEHl+6mSrX38AO DErFCAR15sIRPAtJ8kpoQSu4Xr7Ru1r43c/spwvoVK2jiTGV6b1bOoKoRq8PVH4YYuD9 D3IzXW0a/s6Jq0Vq37CDku5wzkcmVfqAsD44nadM3NpowQ30rumH/kwNQxZJlarwZGLx cMC9NnZcOOO3VVmuE3zv3N3suK8qMei8lrxCLgHHTI8Gao234Mhe+9fxGQr9mSg6N0sV jFURqZwA+SZuduuFqEPCE90eHW/VJwLVovRvpfgA0yFlVDQftrW/IYmaJfhWK0q0K55B 9QnQ== X-Gm-Message-State: AN3rC/6abzqKUHmn9agUuXimbWtKns6QxPjIcRMIPYt8ulkKjQaFLiJh ufWTblYOZLvUzF22 X-Received: by 10.28.5.8 with SMTP id 8mr749854wmf.70.1491593532479; Fri, 07 Apr 2017 12:32:12 -0700 (PDT) Received: from haswell.alporthouse.com ([78.156.65.138]) by smtp.gmail.com with ESMTPSA id 94sm7094079wrp.34.2017.04.07.12.32.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Apr 2017 12:32:11 -0700 (PDT) From: Chris Wilson To: dri-devel@lists.freedesktop.org Subject: [PATCH] dma-buf: Implement simple read/write vfs ops Date: Fri, 7 Apr 2017 20:32:08 +0100 Message-Id: <20170407193208.678-1-chris@chris-wilson.co.uk> X-Mailer: git-send-email 2.11.0 Cc: Daniel Vetter X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP It is expected that processes will pass dma-buf fd between drivers, and only using the fd themselves for mmaping and synchronisation ioctls. However, it may be convenient for an application to read/write into the dma-buf, for instance a test case to check the internal dma_buf->ops->kmap interface. There may also be a small advantage to avoiding the mmap() for very simple/small operations. Note in particular, synchronisation with the device is left to the caller with an explicit DMA_BUF_IOCTL_SYNC, rather than done implicitly inside the read/write, so that the user can avoid synchronisation if they so choose. Signed-off-by: Chris Wilson Cc: Laura Abbott Cc: Sumit Semwal Cc: Daniel Vetter Cc: Sean Paul --- drivers/dma-buf/dma-buf.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 1ce7974a28a3..e4388d5258ed 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -124,6 +124,92 @@ static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence) return base + offset; } +static ssize_t dma_buf_read(struct file *file, + char __user *ubuf, size_t remain, + loff_t *offset) +{ + struct dma_buf *dmabuf = file->private_data; + unsigned long idx; + unsigned int start; + size_t written; + + if (!is_dma_buf_file(file)) + return -EBADF; + + written = 0; + idx = *offset >> PAGE_SHIFT; + start = offset_in_page(*offset); + while (remain) { + unsigned int len = min_t(size_t, remain, PAGE_SIZE - start); + unsigned int copied; + void *vaddr; + + vaddr = dma_buf_kmap(dmabuf, idx); + if (!vaddr) + return written ?: -EIO; + + copied = copy_to_user(vaddr, ubuf, len); + dma_buf_kunmap(dmabuf, idx, vaddr); + + written += copied ?: len; + if (copied) { + *offset += copied; + return written ?: -EFAULT; + } + + remain -= len; + *offset += len; + ubuf += len; + start = 0; + idx++; + } + + return written; +} + +static ssize_t dma_buf_write(struct file *file, + const char __user *ubuf, size_t remain, + loff_t *offset) +{ + struct dma_buf *dmabuf = file->private_data; + unsigned long idx; + unsigned int start; + size_t written; + + if (!is_dma_buf_file(file)) + return -EBADF; + + written = 0; + idx = *offset >> PAGE_SHIFT; + start = offset_in_page(*offset); + while (remain) { + unsigned int len = min_t(size_t, remain, PAGE_SIZE - start); + unsigned int copied; + void *vaddr; + + vaddr = dma_buf_kmap(dmabuf, idx); + if (!vaddr) + return written ?: -EIO; + + copied = copy_from_user(vaddr, ubuf, len); + dma_buf_kunmap(dmabuf, idx, vaddr); + + written += copied ?: len; + if (copied) { + *offset += copied; + return written ?: -EFAULT; + } + + remain -= len; + *offset += len; + ubuf += len; + start = 0; + idx++; + } + + return written; +} + /** * DOC: fence polling * @@ -318,6 +404,8 @@ static const struct file_operations dma_buf_fops = { .release = dma_buf_release, .mmap = dma_buf_mmap_internal, .llseek = dma_buf_llseek, + .read = dma_buf_read, + .write = dma_buf_write, .poll = dma_buf_poll, .unlocked_ioctl = dma_buf_ioctl, #ifdef CONFIG_COMPAT