From patchwork Wed Sep 4 03:15:49 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher James Halse Rogers X-Patchwork-Id: 19758 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f198.google.com (mail-wi0-f198.google.com [209.85.212.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 351E824869 for ; Wed, 4 Sep 2013 16:08:41 +0000 (UTC) Received: by mail-wi0-f198.google.com with SMTP id hq15sf434853wib.5 for ; Wed, 04 Sep 2013 09:08:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:cc:subject :precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:mime-version:errors-to:sender :x-original-sender:x-original-authentication-results:mailing-list :content-type:content-transfer-encoding; bh=BaY3K9aC3VxWfiZHBpA1lIDE08v9pTNKXlxoLqrhehM=; b=TN8kLbI31wPD2JYif4XwZL9C+vcW34iuhBQFql0ZEgcrta/VqH8oJlfMR/jNV8eRUq cuqMkvP1BXH5f/glnAfRVbrL93jpcSAThLGilPrQM/Vel+nj9/nln1EZ8uSc8Z8S9UKJ xMqnv68WR1kuX5p7LbYPbV1h7LIdLtYzgqNmgCR7Mw2SewLCg15UflIljTGQP+sb3DcJ KbEVq5BlLulplKI59ks35Kknlnt8dIRrlqmCjoCxEIjq2AWvxxLXmjfwF4oV3q7FP0yZ tbvyDOMSLIKtfqHRJxruYToqX/t9m62qsS3yIP9qb3gm1GU/2/oKlkSj/6FvvmtrjLT/ 8oMg== X-Received: by 10.112.50.3 with SMTP id y3mr672430lbn.5.1378310919894; Wed, 04 Sep 2013 09:08:39 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.36.137 with SMTP id q9ls20186laj.62.gmail; Wed, 04 Sep 2013 09:08:39 -0700 (PDT) X-Received: by 10.112.158.225 with SMTP id wx1mr2308642lbb.37.1378310919318; Wed, 04 Sep 2013 09:08:39 -0700 (PDT) Received: from mail-lb0-f169.google.com (mail-lb0-f169.google.com [209.85.217.169]) by mx.google.com with ESMTPS id l7si11027109lbd.35.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 04 Sep 2013 09:08:39 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.217.169 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.217.169; Received: by mail-lb0-f169.google.com with SMTP id z5so600957lbh.14 for ; Wed, 04 Sep 2013 09:08:39 -0700 (PDT) X-Gm-Message-State: ALoCoQljSy4tXVI155aIEfKedP+VZ/V9RiuuDa/jnOckeDPtPB9JX6DcO42BQfOOcTugFI7LW7Kj X-Received: by 10.112.138.37 with SMTP id qn5mr1016463lbb.52.1378310918973; Wed, 04 Sep 2013 09:08:38 -0700 (PDT) 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.112.11.201 with SMTP id s9csp234181lbb; Wed, 4 Sep 2013 09:08:38 -0700 (PDT) X-Received: by 10.194.174.36 with SMTP id bp4mr3081321wjc.7.1378310918057; Wed, 04 Sep 2013 09:08:38 -0700 (PDT) Received: from ip-10-141-164-156.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id ib3si9363768wjb.48.1969.12.31.16.00.00 (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 04 Sep 2013 09:08:38 -0700 (PDT) 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 1VHFcC-0001Uc-El; Wed, 04 Sep 2013 16:07:32 +0000 Received: from li226-30.members.linode.com ([173.255.216.30] helo=mail.cooperteam.net) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1VH3Ys-0007he-1N for linaro-mm-sig@lists.linaro.org; Wed, 04 Sep 2013 03:15:18 +0000 Received: from RedTail.fritz.box (ppp105-211.static.internode.on.net [150.101.105.211]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: chris) by mail.cooperteam.net (Postfix) with ESMTPSA id D25B3104B29; Wed, 4 Sep 2013 03:16:09 +0000 (UTC) From: Christopher James Halse Rogers To: linux-kernel@vger.kernel.org Date: Wed, 4 Sep 2013 13:15:49 +1000 Message-Id: <1378264549-9185-1-git-send-email-christopher.halse.rogers@canonical.com> X-Mailer: git-send-email 1.8.3.2 X-Mailman-Approved-At: Wed, 04 Sep 2013 16:07:30 +0000 Cc: linux-arch@vger.kernel.org, Christopher James Halse Rogers , dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, robclark@gmail.com, maarten.lankhorst@canonical.com, linux-media@vger.kernel.org Subject: [Linaro-mm-sig] [PATCH] dma-buf: Expose buffer size to userspace (v2) 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: , MIME-Version: 1.0 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: christopher.halse.rogers@canonical.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.217.169 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 Each dma-buf has an associated size and it's reasonable for userspace to want to know what it is. Since userspace already has an fd, expose the size using the size = lseek(fd, SEEK_END, 0); lseek(fd, SEEK_CUR, 0); idiom. v2: Added Daniel's sugeested documentation, with minor fixups Signed-off-by: Christopher James Halse Rogers Reviewed-by: Daniel Vetter Tested-by: Daniel Vetter --- Documentation/dma-buf-sharing.txt | 12 ++++++++++++ drivers/base/dma-buf.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/Documentation/dma-buf-sharing.txt b/Documentation/dma-buf-sharing.txt index 0b23261..849e982 100644 --- a/Documentation/dma-buf-sharing.txt +++ b/Documentation/dma-buf-sharing.txt @@ -407,6 +407,18 @@ Being able to mmap an export dma-buf buffer object has 2 main use-cases: interesting ways depending upong the exporter (if userspace starts depending upon this implicit synchronization). +Other Interfaces Exposed to Userspace on the dma-buf FD +------------------------------------------------------ + +- Since kernel 3.12 the dma-buf FD supports the llseek system call, but only + with offset=0 and whence=SEEK_END|SEEK_SET. SEEK_SET is supported to allow + the usual size discover pattern size = SEEK_END(0); SEEK_SET(0). Every other + llseek operation will report -EINVAL. + + If llseek on dma-buf FDs isn't support the kernel will report -ESPIPE for all + cases. Userspace can use this to detect support for discovering the dma-buf + size using llseek. + Miscellaneous notes ------------------- diff --git a/drivers/base/dma-buf.c b/drivers/base/dma-buf.c index 6687ba7..c33a857 100644 --- a/drivers/base/dma-buf.c +++ b/drivers/base/dma-buf.c @@ -77,9 +77,36 @@ static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma) return dmabuf->ops->mmap(dmabuf, vma); } +static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence) +{ + struct dma_buf *dmabuf; + loff_t base; + + if (!is_dma_buf_file(file)) + return -EBADF; + + dmabuf = file->private_data; + + /* only support discovering the end of the buffer, + but also allow SEEK_SET to maintain the idiomatic + SEEK_END(0), SEEK_CUR(0) pattern */ + if (whence == SEEK_END) + base = dmabuf->size; + else if (whence == SEEK_SET) + base = 0; + else + return -EINVAL; + + if (offset != 0) + return -EINVAL; + + return base + offset; +} + static const struct file_operations dma_buf_fops = { .release = dma_buf_release, .mmap = dma_buf_mmap_internal, + .llseek = dma_buf_llseek, }; /* @@ -133,6 +160,7 @@ struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops, dmabuf->exp_name = exp_name; file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags); + file->f_mode |= FMODE_LSEEK; dmabuf->file = file;