From patchwork Tue Dec 15 22:05:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 344033 Delivered-To: patches@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp4264542jai; Tue, 15 Dec 2020 14:05:29 -0800 (PST) X-Received: by 2002:a63:e402:: with SMTP id a2mr4254342pgi.168.1608069929358; Tue, 15 Dec 2020 14:05:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608069929; cv=none; d=google.com; s=arc-20160816; b=emS/nUV39uu7KwDCfEY09icq8LttFX91b6DLv1DzCR5qkzGaHbkjcaFhvtIp7qTDWe +O1cACgkE0gQyzglYd0cXQyx8dZPdkhxNvOwcSuxZHhAGIDfweVdftZz58MY82747Nlu FrNPzrVwypP7dR9fGVilC7EekeS4CcAZFALBz/kw+aklL18Y4udpCff2sWzcneoXzB0k W820pN1VgzU0qx5BdC5qsmPgS8ex8yu1zsbeqqfN3dGEvmGFHDtuJgTVfQXaM9R8dm3A o5kZZXxAn+ARlqQNNu8DCxnWwrd0t6XhzypS0JR3Zj7TM7jNKeDw+jFpwJBkUnLYyRWL ichA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=kl8PmZ90JFms2rGnBVhby1dPAjXSFRIkuqNGt+DZeDI=; b=Gah0JVtw206laWUNQjyGktiFdDVAK4BLFoVA+xxFyQPmWlQIoT9OORoEjkMAQ2l6me hRwgpTlKQNyY+9EmQ5cdmRf6SfH/m30EXZShFahuafMysGw62CEG9+1dNQCYmHuLLLH/ l1yiUxGIYld0DZ3PlQuifrBXUfmMrPauaEFvh833KRc7yIV2ZlEdrkhhnAQjk3TyDdkV MOqhxj+eDKe/HdPouA+FmJRIWr6BlyMxwAbtuCJzlf2vBpkK5PxpOjYNshsEGNn2iPp7 fMypmewOqZUqfPLKqgL+SG97yigf1slkVaSiTxL+J7wWwGLWzSqvLk2ENYNUP0elKDIt afGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=gtbakvDS; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id u13sor76025pjn.32.2020.12.15.14.05.29 for (Google Transport Security); Tue, 15 Dec 2020 14:05:29 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=gtbakvDS; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kl8PmZ90JFms2rGnBVhby1dPAjXSFRIkuqNGt+DZeDI=; b=gtbakvDS5+hfUPYPisnKHKkpzI2vWtDWjMabBbr36nuJ4SnxstOs4PQOYUvf1rubas UVc6Zd9/Ftn/AKJedeTpl1JX9Mp7tQoHFxWL23lR9Nu+v0DciCOGdKgzjdW8OPgxDWq0 xU+zTjyuHUFkUFK7I3bWOz3PS1qYYCD3N5Zr3007xPpjcCgfSPvN67Wqp/5b6Ba19o0f L/NefZfbRy72TRZVYW/8uTjFQlc6ZnwEviOb5C3p0YeTjceCA8NGB6Ub3pMtd+S3Zr8G UmeZkBEWCcQRxb1om3207cWH5j2y88+4Mvy5bgKq5ARNmW3yFFAW265HlZSGKGP+868t jAGw== 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=kl8PmZ90JFms2rGnBVhby1dPAjXSFRIkuqNGt+DZeDI=; b=mgvGpnstDqkI3NQ7CwHuchaG1X0s76QlcNtIvziKXE0CB4eqmJamvjsOLhkiDH0FDe DVtFuMYIepONoH1o0MiXkl4xZJaB3cFcey+hDr0TshPK9Kt87OVj9rmy7NQ1Ubxcv9yV lyByQvSr/aS8eya7dDOQA8uq4sKkI5feP+BPsMDqPUnYmti/ZhIaBCxslwBgnGpwAPHi siAiWxYVGCFVtUWivFGVeX+JnaHKNJiWWJgeGj3voygqd22tv96JFmHgszSDmZ68kjUg 0oCzZPXN6j/S3Ksnd3/bgs8YmXTEyBFEly2SCA9w8PJr9CLqe73bD+m5XqYVVpq1yA7E lXDw== X-Gm-Message-State: AOAM530DmSZpRq5QPA7vkQeGcmN47JeWsexCttTiSWFk9K7vtvrUHB+x +jTPXh/oaZ+w1enjLGi6MTzNE5qX X-Google-Smtp-Source: ABdhPJz5+eYpmQKEiyL/MUZSD6KzHvL6MBIgXZn3boaKbi6B/hgxQFMNg76pr/UYudpPO0p2PS22oQ== X-Received: by 2002:a17:90a:1b82:: with SMTP id w2mr653081pjc.127.1608069928998; Tue, 15 Dec 2020 14:05:28 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id x15sm84146pfa.80.2020.12.15.14.05.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Dec 2020 14:05:28 -0800 (PST) From: John Stultz To: lkml Cc: John Stultz , Sumit Semwal , Liam Mark , Laura Abbott , Brian Starkey , Hridya Valsaraju , Suren Baghdasaryan , Sandeep Patil , Daniel Mentz , Chris Goldsworthy , =?utf-8?q?=C3=98rjan_Eide?= , Robin Murphy , Ezequiel Garcia , Simon Ser , James Jones , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Subject: [RFC][PATCH 3/3] dma-buf: heaps: Rework heep allocation hooks to return struct dma_buf instead of fd Date: Tue, 15 Dec 2020 22:05:21 +0000 Message-Id: <20201215220521.118318-3-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201215220521.118318-1-john.stultz@linaro.org> References: <20201215220521.118318-1-john.stultz@linaro.org> MIME-Version: 1.0 Every heap needs to create a dmabuf and then export it to a fd via dma_buf_fd(), so to consolidate things a bit, have the heaps just return a struct dmabuf * and let the top level dma_heap_buffer_alloc() call handle creating the fd via dma_buf_fd(). Cc: Sumit Semwal Cc: Liam Mark Cc: Laura Abbott Cc: Brian Starkey Cc: Hridya Valsaraju Cc: Suren Baghdasaryan Cc: Sandeep Patil Cc: Daniel Mentz Cc: Chris Goldsworthy Cc: Ørjan Eide Cc: Robin Murphy Cc: Ezequiel Garcia Cc: Simon Ser Cc: James Jones Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Signed-off-by: John Stultz --- drivers/dma-buf/dma-heap.c | 14 +++++++++++++- drivers/dma-buf/heaps/cma_heap.c | 22 +++++++--------------- drivers/dma-buf/heaps/system_heap.c | 21 +++++++-------------- include/linux/dma-heap.h | 12 ++++++------ 4 files changed, 33 insertions(+), 36 deletions(-) -- 2.17.1 diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index afd22c9dbdcf..6b5db954569f 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -52,6 +52,9 @@ static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, unsigned int fd_flags, unsigned int heap_flags) { + struct dma_buf *dmabuf; + int fd; + /* * Allocations from all heaps have to begin * and end on page boundaries. @@ -60,7 +63,16 @@ static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, if (!len) return -EINVAL; - return heap->ops->allocate(heap, len, fd_flags, heap_flags); + dmabuf = heap->ops->allocate(heap, len, fd_flags, heap_flags); + if (IS_ERR(dmabuf)) + return PTR_ERR(dmabuf); + + fd = dma_buf_fd(dmabuf, fd_flags); + if (fd < 0) { + dma_buf_put(dmabuf); + /* just return, as put will call release and that will free */ + } + return fd; } static int dma_heap_open(struct inode *inode, struct file *file) diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c index 877353e8014f..0c7d6430605f 100644 --- a/drivers/dma-buf/heaps/cma_heap.c +++ b/drivers/dma-buf/heaps/cma_heap.c @@ -268,10 +268,10 @@ static const struct dma_buf_ops cma_heap_buf_ops = { .release = cma_heap_dma_buf_release, }; -static int cma_heap_allocate(struct dma_heap *heap, - unsigned long len, - unsigned long fd_flags, - unsigned long heap_flags) +static struct dma_buf *cma_heap_allocate(struct dma_heap *heap, + unsigned long len, + unsigned long fd_flags, + unsigned long heap_flags) { struct cma_heap *cma_heap = dma_heap_get_drvdata(heap); struct cma_heap_buffer *buffer; @@ -286,7 +286,7 @@ static int cma_heap_allocate(struct dma_heap *heap, buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); if (!buffer) - return -ENOMEM; + return ERR_PTR(-ENOMEM); INIT_LIST_HEAD(&buffer->attachments); mutex_init(&buffer->lock); @@ -345,15 +345,7 @@ static int cma_heap_allocate(struct dma_heap *heap, ret = PTR_ERR(dmabuf); goto free_pages; } - - ret = dma_buf_fd(dmabuf, fd_flags); - if (ret < 0) { - dma_buf_put(dmabuf); - /* just return, as put will call release and that will free */ - return ret; - } - - return ret; + return dmabuf; free_pages: kfree(buffer->pages); @@ -362,7 +354,7 @@ static int cma_heap_allocate(struct dma_heap *heap, free_buffer: kfree(buffer); - return ret; + return ERR_PTR(ret); } static const struct dma_heap_ops cma_heap_ops = { diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index 2321c91891f6..7b154424aeb3 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -332,10 +332,10 @@ static struct page *alloc_largest_available(unsigned long size, return NULL; } -static int system_heap_allocate(struct dma_heap *heap, - unsigned long len, - unsigned long fd_flags, - unsigned long heap_flags) +static struct dma_buf *system_heap_allocate(struct dma_heap *heap, + unsigned long len, + unsigned long fd_flags, + unsigned long heap_flags) { struct system_heap_buffer *buffer; DEFINE_DMA_BUF_EXPORT_INFO(exp_info); @@ -350,7 +350,7 @@ static int system_heap_allocate(struct dma_heap *heap, buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); if (!buffer) - return -ENOMEM; + return ERR_PTR(-ENOMEM); INIT_LIST_HEAD(&buffer->attachments); mutex_init(&buffer->lock); @@ -400,14 +400,7 @@ static int system_heap_allocate(struct dma_heap *heap, ret = PTR_ERR(dmabuf); goto free_pages; } - - ret = dma_buf_fd(dmabuf, fd_flags); - if (ret < 0) { - dma_buf_put(dmabuf); - /* just return, as put will call release and that will free */ - return ret; - } - return ret; + return dmabuf; free_pages: for_each_sgtable_sg(table, sg, i) { @@ -421,7 +414,7 @@ static int system_heap_allocate(struct dma_heap *heap, __free_pages(page, compound_order(page)); kfree(buffer); - return ret; + return ERR_PTR(ret); } static const struct dma_heap_ops system_heap_ops = { diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h index 454e354d1ffb..5bc5c946af58 100644 --- a/include/linux/dma-heap.h +++ b/include/linux/dma-heap.h @@ -16,15 +16,15 @@ struct dma_heap; /** * struct dma_heap_ops - ops to operate on a given heap - * @allocate: allocate dmabuf and return fd + * @allocate: allocate dmabuf and return struct dma_buf ptr * - * allocate returns dmabuf fd on success, -errno on error. + * allocate returns dmabuf on success, ERR_PTR(-errno) on error. */ struct dma_heap_ops { - int (*allocate)(struct dma_heap *heap, - unsigned long len, - unsigned long fd_flags, - unsigned long heap_flags); + struct dma_buf *(*allocate)(struct dma_heap *heap, + unsigned long len, + unsigned long fd_flags, + unsigned long heap_flags); }; /**