Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp220228pxb; Thu, 21 Jan 2021 05:41:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJzbQSeYIjFTelVo00gdGsQEFlcx4VQa7CwSryR7TfbhpmK/r+9/0AUdI+4hIzekAxgHonzR X-Received: by 2002:aa7:cf8d:: with SMTP id z13mr576990edx.119.1611236474041; Thu, 21 Jan 2021 05:41:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611236474; cv=none; d=google.com; s=arc-20160816; b=TPknrC0Aefvtiuz1IYcKtBvHsoptf4y/vKymEcIbfND59xPtLQBkFbWUzOqrfgGRO7 aZjDd4gVHv5ipwHP/wTELbGU7iYX03/tohVNRJ9dRg+SMF7APc+vfNLSzpydygMZYcvi kipw3KVVbQk5q0QNh8BqXsHroPVCbtnmKQYp4poe6jSYknkb+tgQmcLclv2KtllHEnXv OBCes3M/winb1DH1c6VQgVy4bFk+OuHgwiSR9/jdnlDC7EpqM1JknuAIzxoYyxKiRePT 7aMJqzzeCs+Vxx7WBQoI+aZKujNFGsH8j5qrYbGLWyul1kGKjUihNPYYoHshV4krlmH1 XeRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=LEqV0YucJGoO/qBUnA782Gstn0eiwdK9KK0CxAlgdFQ=; b=VMQrtK79oGjDoBZwGeGRErwYBffojYpbwiL3xV0lAbhc8rbCU6sy9Ta0qHxJGvUOID hP7CV8rREsxFTnza6vNujwyUYT3GlKfAXOjbKHHTR0tGTX8qV4dXmUEwo3WuTINl7G9m 916szJMpsrDhYcgWZsDbijOUSSEeI7jaY6WpDPQMPqrOGKmPfoX2s9WHlmw8jCEGHEtK +EsPSAb7OO3rixfBPFvIt0e5lc0pldfy4rHxpAFE9P4LTEAzPL8AyGJOz39CHiwCQMtk sSosxWnXgZYrCt07ZSIMHgZ+vBTi47LnTPO62tm7Cbm/ekWiTM+1a19LQFKg7AJDAMAf 95HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=mIzR3Twp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d2si2089421edd.145.2021.01.21.05.40.44; Thu, 21 Jan 2021 05:41:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=mIzR3Twp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727001AbhAUNkH (ORCPT + 99 others); Thu, 21 Jan 2021 08:40:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728685AbhAUNiB (ORCPT ); Thu, 21 Jan 2021 08:38:01 -0500 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [IPv6:2a00:1450:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EC33C0613CF for ; Thu, 21 Jan 2021 05:37:20 -0800 (PST) Received: by mail-lj1-x230.google.com with SMTP id b10so2495183ljp.6 for ; Thu, 21 Jan 2021 05:37:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=LEqV0YucJGoO/qBUnA782Gstn0eiwdK9KK0CxAlgdFQ=; b=mIzR3TwpRPZooXGXgWEuR9yR78ZPR30DNBbIDReYCADSMlmmjuZuogNLufjqNeEFmH io35mw8ym0R9pRMxY2MW9keSCMXyrglcOAQ+O2nsUTxojaiGc2Q6xkYMkwXTKQk2lz5j xG3n7HIaExSXumKW64SYDrRhrHtoAwsrebmVIEZIFQYqMTnTeXQNVb1rMrOXaJR1JmUd QvS4P3o1zYpF8zCOGpF6hL7VTo5KthcTjQs+h+VQxWTV+V4RXv/d3NwnD52MxnHA2d59 0Wkp5u5d+DxsSy61ytCIITtSf1XyiQzoYZOmSKcG0rbwuV+G5ty9mqkAC2UpW5GHgR8T SBqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=LEqV0YucJGoO/qBUnA782Gstn0eiwdK9KK0CxAlgdFQ=; b=mWKkXz/DDu5ned4KSdFccxTpW45crqU8OrjiqCOsoQfmYDdnroH1NlYUS+lHrKnIWU HymnQmzHhQ04lrjjeQBjvr0swvo+NBKaUteX0+cXlXxAuIqsWd0rsAZFc4Y9wUuZ7tLi kGPFFJc2i5Eq0QA/Vhb0v8pG0SWFMkbMSIouL6La0mOdiJrpKYGnohwdk0lX9U0FTAGe m8CkGf2IJfoAkSJ/hRSXH4lBh8P1Q4+n3kRmx1wZMvDxIEZn9O8F37W31aH1hubuAtXy uLyPxEer2TKxSLrrUNFIBNHRI+1AfDklaJEa8fdvRA9H+mPqmpPiWhDnkENFw5sHlNd6 CHKw== X-Gm-Message-State: AOAM533gitTei24FUdUHNHe4yQJjrHZN7Kn8OP4Zbrd21dCh04x66JRi PqGTpNRGIXYeRNzSH9U9ck3AZLSCpmN652pCna7/bQ== X-Received: by 2002:a2e:9dc1:: with SMTP id x1mr6636681ljj.32.1611236238882; Thu, 21 Jan 2021 05:37:18 -0800 (PST) MIME-Version: 1.0 References: <20210119204508.9256-1-john.stultz@linaro.org> <20210119204508.9256-3-john.stultz@linaro.org> In-Reply-To: <20210119204508.9256-3-john.stultz@linaro.org> From: Sumit Semwal Date: Thu, 21 Jan 2021 19:07:06 +0530 Message-ID: Subject: Re: [RESEND][PATCH 3/3] dma-buf: heaps: Rework heep allocation hooks to return struct dma_buf instead of fd To: John Stultz Cc: lkml , 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 , "open list:DMA BUFFER SHARING FRAMEWORK" , DRI mailing list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi John, On Wed, 20 Jan 2021 at 02:15, John Stultz wrote: > > 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(). Thanks for the patch! LGTM, feels a lot neater now. I'll merge into drm-misc-next. > > 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: =C3=98rjan 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(-) > > 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 =3D heap->ops->allocate(heap, len, fd_flags, heap_flags); > + if (IS_ERR(dmabuf)) > + return PTR_ERR(dmabuf); > + > + fd =3D dma_buf_fd(dmabuf, fd_flags); > + if (fd < 0) { > + dma_buf_put(dmabuf); > + /* just return, as put will call release and that will fr= ee */ > + } > + 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 = =3D { > .release =3D 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 =3D dma_heap_get_drvdata(heap); > struct cma_heap_buffer *buffer; > @@ -290,7 +290,7 @@ static int cma_heap_allocate(struct dma_heap *heap, > > buffer =3D 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 =3D PTR_ERR(dmabuf); > goto free_pages; > } > - > - ret =3D dma_buf_fd(dmabuf, fd_flags); > - if (ret < 0) { > - dma_buf_put(dmabuf); > - /* just return, as put will call release and that will fr= ee */ > - 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 =3D { > 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(unsigne= d 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 =3D 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 *hea= p, > ret =3D PTR_ERR(dmabuf); > goto free_pages; > } > - > - ret =3D dma_buf_fd(dmabuf, fd_flags); > - if (ret < 0) { > - dma_buf_put(dmabuf); > - /* just return, as put will call release and that will fr= ee */ > - 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 =3D { > 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); > }; > > /** > -- > 2.17.1 > Best, Sumit.