From patchwork Tue Jan 19 20:45:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 366218 Delivered-To: patches@linaro.org Received: by 2002:a02:a60d:0:0:0:0:0 with SMTP id c13csp1100520jam; Tue, 19 Jan 2021 12:45:16 -0800 (PST) X-Received: by 2002:a17:902:a517:b029:de:79a7:48d9 with SMTP id s23-20020a170902a517b02900de79a748d9mr6550246plq.45.1611089115983; Tue, 19 Jan 2021 12:45:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611089115; cv=none; d=google.com; s=arc-20160816; b=tVpjhcvfvuOjDdHMrs5d2SbZfJTIccW7XpEA4NNnQ8fXbLOT7LsLjoLxG+/QMYqGa0 Qr8uzrJ2Wuz8KF0zjJw5UY2mwaKwpM33zWpOfQw7OM8RSOWu4K362zOjWz14WiZprohf oL9vAa9w48SAKPG1vmxs7KqKOh7wn5jxDt/Zft+IPfK888XRJasx0Kjesi1zXmJulwRN Fx0iOMxFAqqw5FVkCSpFMdzFEgLwxoLXuP5MIzh5eaEp0WcaUw11kJL+DRCyVZ+HJrfo CNZyacYqdmP+4WF/73IIwK8bFJuhdhjw7U/SkAs0eW2Q2CfD6nQ0DjpXsZpSy9MLiyOF +Rgg== 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=njrcSIJAkGY2OvZcMcUOB/aiSRsE8gSP15fU7c5jB6I=; b=h53R8gndQotiUQIHyuCo4eWsO/IFCbMNIO5ap8hHwXCZ9o5dinnNBlwv/IEttIwNF6 lborIHoSewUjKTq/57EYATAEeWwzI0S5Y5ycxOejHpPi34L3pV/MB9X5VUdRqAUNfRGc JiX/P7mEnDaNmQ64OIbg8takEqxBMJRvVXllNr6n7GN46/YX8chuM+ghUq4/aqebgg5K uZx+NAqYTLDxCabASMGCOkdzmXZd6oMR3ZybbeR1EOul6mExmd5890W6A8kkA7Fs6hqq 72Jmhbw8mcKsKWbzo3XQncXJ9TqCKXA++DSA/+ChbH7eVnrALo+SLouZTUfwtZvJ7KZi GNgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=D4Tiif4U; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.41 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-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id nh2sor2260134pjb.20.2021.01.19.12.45.15 for (Google Transport Security); Tue, 19 Jan 2021 12:45:15 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=D4Tiif4U; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.41 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=njrcSIJAkGY2OvZcMcUOB/aiSRsE8gSP15fU7c5jB6I=; b=D4Tiif4UHDgXE0lEl4K0r5he4Lv75bV6HrswkUdJtOBDALOY20/c4H2BSvtnyOb/TI lUvOJdmQt0XsbtCcgTlnkqqpAJQNX09TzBmy5RJFv/agoix5egESsaXy3Nveu5r9N10z fNuTeUbV3nBdadsnb+AbFgi8g+pyo8yGfsg6cOewdwkf78efJVxidklQClROooreN6BY 4Awgdijgf5MJ1bxffI2A7/j/1T07ikg94dEgS8D0bY9No74cF+y6jibT4Ne9hGHwHwRa IX/mVVnT24EmSAISdSkb4YSF2D9+3eN6ND0QDmXLnQZmVdO6m6rILjT+jOvuB8NqiI1K uABQ== 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=njrcSIJAkGY2OvZcMcUOB/aiSRsE8gSP15fU7c5jB6I=; b=EC41qQiYhj0B7x5YKKMclEyj4tyFPRoFiymVyxypUz86D1RSkBV+kQ128aPCp25nSo zVDYu6ZJGwj8apv93n3eCr3wPLeJrLBB6nvpb0J0kH5L+TFlXGNunrEyDds6MzC87t4j DVVuV3r8IQLCU3ALXbJT3OAOaq1fidWd/3UqqACCNm3wchuB5VM4JXQIiJMTxo1DtuSw sTfYmyeUg12Irj++UoaNOU/KPJ3sdefndoMwsK5TFJrl6prlMrf5IC78G9738fFT+9Lh aamREdJR3k8KHmtduNxaHkcs/8ZI5OgY426dYVZNOfWG7eDp1UttlKdHWbc+Fshz96Qc JeLw== X-Gm-Message-State: AOAM5315MraMSHOezjIbpj980P7qKQxSzxZwrAWVrUWOLsI5bnsIqf+U +lFc3NThcZ5cq6ImgNtmb8VFLZgZ X-Google-Smtp-Source: ABdhPJxNNMADX9CbUQovzjYXc9TxCQoBpybpXFayDx9hPArekpnWGmkjCQtFXmhn9Lpe14eQ01u5eg== X-Received: by 2002:a17:90a:7e18:: with SMTP id i24mr1669643pjl.72.1611089115609; Tue, 19 Jan 2021 12:45:15 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id md7sm4129838pjb.52.2021.01.19.12.45.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Jan 2021 12:45:14 -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: [RESEND][PATCH 3/3] dma-buf: heaps: Rework heep allocation hooks to return struct dma_buf instead of fd Date: Tue, 19 Jan 2021 20:45:08 +0000 Message-Id: <20210119204508.9256-3-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210119204508.9256-1-john.stultz@linaro.org> References: <20210119204508.9256-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 0c76cbc3fb11..985c41ffd85b 100644 --- a/drivers/dma-buf/heaps/cma_heap.c +++ b/drivers/dma-buf/heaps/cma_heap.c @@ -272,10 +272,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; @@ -290,7 +290,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); @@ -349,15 +349,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); @@ -366,7 +358,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); }; /**