Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp289197pxf; Wed, 24 Mar 2021 05:20:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwrdDGQWRS2QPS4jXWdxfolAbfs6A8W0mWP7tNyUSTJ0JVeA/ygRtCLTH13ga34fdgaxL43 X-Received: by 2002:a17:906:7150:: with SMTP id z16mr3319866ejj.103.1616588449175; Wed, 24 Mar 2021 05:20:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616588449; cv=none; d=google.com; s=arc-20160816; b=SOIjXnjkS+bx2gEycHWXZrmg075DSoLUFJUff/PIWHunFnA6l4UKAwVGYrv5Lm6vxN G3vZH7xjwlMLyfxuLy5sdOGFlFL2ES8Jvk3h25jgtr2M9bOf+dDJhp4X8O3ecDm0yEjt c6S3PMiIuAlIex31nGHxkGDcwhEvrOr38dqlmSuSFYQsOFtlG672YvPPvBY8MHQflihQ cQQDLBn2e10IJ2T5Pq2u0maID9GRSxWtIyb2ME3TRjPc+Rr8JkJMkpt7GSweSpSKT07+ 8Ovz+vq8sSQIMDi35YKLIK/comZv+IV5FjzpeXdtPB764EtPYtxHS5M1xEMap5C9Ai2b skGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=VuRjzLbq9QnW+8wXxMXWYUJMkL1n+ttgNIblb9wNPMY=; b=dr9MBw45uJVRcKmgQpYN+O333bLQQfDQ9Nu+O7zvZ2BdvY1mXoKeydQaYLRRVG00xK BUEB7hnlCO8LCICMzF5OldgEJZXtlZOF3ntUq/Mo67i93mUizFFdnxgANGED26riRZrs Kg+1LvjKc2INP9Eh+OZzvsNUL5qDczSzYjBkTc1NZb3Dq94Zb6Y1Wkw7m9Jo1/YiIzCV 7C0A6FrGPK1fR9Q4YZ4YB1bcVED+5oguPlWalvJCCI8+f9UF2XWBdJMSluBU2CFbsiQ+ vd7EHdqPnZX1o6F4U3GgPbLjXyzg4cg7EkKuB8cmV42Y2D4KByAioAo8BoL97hsaZUXH AIAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Jmf4GwCa; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ly13si1664518ejb.649.2021.03.24.05.20.21; Wed, 24 Mar 2021 05:20:49 -0700 (PDT) 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=@chromium.org header.s=google header.b=Jmf4GwCa; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232680AbhCXMHp (ORCPT + 99 others); Wed, 24 Mar 2021 08:07:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232160AbhCXMHc (ORCPT ); Wed, 24 Mar 2021 08:07:32 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 307AEC061763 for ; Wed, 24 Mar 2021 05:07:32 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id j25so17160725pfe.2 for ; Wed, 24 Mar 2021 05:07:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=VuRjzLbq9QnW+8wXxMXWYUJMkL1n+ttgNIblb9wNPMY=; b=Jmf4GwCa9kb2V3V3DQGPMOWNdAGbcq1ehTkfWzAE8kTBmYMS1wi3tGPYs60ueI8bKQ SVGgiY1qKloFHgxi4fAUT3ijseg5Gk6phovVoAS4JU3M/Txb4zU7qAL98bTpgsCxBNoW GiA8JaEg9Xfs2o/SkXapZZUDxkLLPj50HQYR8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=VuRjzLbq9QnW+8wXxMXWYUJMkL1n+ttgNIblb9wNPMY=; b=k74gIdr2gGbA+C/5RMRPEPkRYWNGvvYgrgpGeuStGXEIbycsyis6f+otrawfTwbImD weImIi+6Eovp2pIkTqQhU3TLBpaBawNGhy6jspSr/6JV6EQiBHZSM/oRLIpITolrVa3B CBb3+DS/3j/tAcREt2Qqi6pFu7duZfhD77rnBQ99OLKfltsHJ2skGiZcfHBg4+CGoNrv wnuPAKPhUgyB3G7EzqfUj0PXcE5Dkha/1zDfzlwp52xf8649PaWl10P8mSEjILUKpcAt 3vEX6lvvod7llCf5gKFSvep0EyQYANTKnnca6q2t3S+EjnMZYmPWiRr+zpghczG+sEmV 2ReQ== X-Gm-Message-State: AOAM532IoeezLGL9fciFIiENZ4G7VV09+4DqQ9BWIFXvWTr+qCq7jK0d GrGN0LeMjc0oy/n9Y6xALEL/bA== X-Received: by 2002:a63:fe56:: with SMTP id x22mr2728134pgj.329.1616587651535; Wed, 24 Mar 2021 05:07:31 -0700 (PDT) Received: from chromium.org ([2401:fa00:8f:2:9cb3:4047:113:3430]) by smtp.gmail.com with ESMTPSA id y8sm2315523pge.56.2021.03.24.05.07.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 05:07:29 -0700 (PDT) Date: Wed, 24 Mar 2021 21:07:26 +0900 From: Tomasz Figa To: Sergey Senozhatsky Cc: Hans Verkuil , Christoph Hellwig , Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 8/8] videobuf2: handle non-contiguous DMA allocations Message-ID: References: <20210302004624.31294-1-senozhatsky@chromium.org> <20210302004624.31294-9-senozhatsky@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210302004624.31294-9-senozhatsky@chromium.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 02, 2021 at 09:46:24AM +0900, Sergey Senozhatsky wrote: > This adds support for new noncontiguous DMA API, which > requires allocators to have two execution branches: one > for the current API, and one for the new one. > > Signed-off-by: Sergey Senozhatsky > [hch: untested conversion to the ne API] > Signed-off-by: Christoph Hellwig > --- > .../common/videobuf2/videobuf2-dma-contig.c | 141 +++++++++++++++--- > 1 file changed, 117 insertions(+), 24 deletions(-) > > diff --git a/drivers/media/common/videobuf2/videobuf2-dma-contig.c b/drivers/media/common/videobuf2/videobuf2-dma-contig.c > index 1e218bc440c6..d6a9f7b682f3 100644 > --- a/drivers/media/common/videobuf2/videobuf2-dma-contig.c > +++ b/drivers/media/common/videobuf2/videobuf2-dma-contig.c > @@ -17,6 +17,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -42,8 +43,14 @@ struct vb2_dc_buf { > struct dma_buf_attachment *db_attach; > > struct vb2_buffer *vb; > + unsigned int non_coherent_mem:1; > }; > > +static bool vb2_dc_is_coherent(struct vb2_dc_buf *buf) > +{ > + return !buf->non_coherent_mem; > +} nit: Given that this is just a simple negated return, do we need a dedicated helper for it? > + > /*********************************************/ > /* scatterlist table functions */ > /*********************************************/ > @@ -78,12 +85,21 @@ static void *vb2_dc_cookie(struct vb2_buffer *vb, void *buf_priv) > static void *vb2_dc_vaddr(struct vb2_buffer *vb, void *buf_priv) > { > struct vb2_dc_buf *buf = buf_priv; > - struct dma_buf_map map; > - int ret; > > - if (!buf->vaddr && buf->db_attach) { > - ret = dma_buf_vmap(buf->db_attach->dmabuf, &map); > - buf->vaddr = ret ? NULL : map.vaddr; > + if (buf->vaddr) > + return buf->vaddr; > + > + if (buf->db_attach) { > + struct dma_buf_map map; > + > + if (!dma_buf_vmap(buf->db_attach->dmabuf, &map)) > + buf->vaddr = map.vaddr; > + } > + > + if (!vb2_dc_is_coherent(buf)) { I believe it's not possible for both buf->db_attach and !vb2_dc_is_coherent() to be true, but nevertheless the code can be misleading for the reader. Would it make sense to just return early in the if that handles db_attach? > + buf->vaddr = dma_vmap_noncontiguous(buf->dev, > + buf->size, > + buf->dma_sgt); > } > > return buf->vaddr; > @@ -101,13 +117,26 @@ static void vb2_dc_prepare(void *buf_priv) > struct vb2_dc_buf *buf = buf_priv; > struct sg_table *sgt = buf->dma_sgt; > > + /* This takes care of DMABUF and user-enforced cache sync hint */ > if (buf->vb->skip_cache_sync_on_prepare) > return; > > + /* > + * Coherent MMAP buffers do not need to be synced, unlike coherent > + * USERPTR and non-coherent MMAP buffers. USERPTR buffers are always considered non-coherent. > + */ > + if (buf->vb->memory == V4L2_MEMORY_MMAP && vb2_dc_is_coherent(buf)) > + return; > + > if (!sgt) > return; > > + /* For both USERPTR and non-coherent MMAP */ > dma_sync_sgtable_for_device(buf->dev, sgt, buf->dma_dir); > + > + /* Non-coherrent MMAP only */ > + if (!vb2_dc_is_coherent(buf) && buf->vaddr) > + flush_kernel_vmap_range(buf->vaddr, buf->size); > } > > static void vb2_dc_finish(void *buf_priv) > @@ -115,19 +144,46 @@ static void vb2_dc_finish(void *buf_priv) > struct vb2_dc_buf *buf = buf_priv; > struct sg_table *sgt = buf->dma_sgt; > > + /* This takes care of DMABUF and user-enforced cache sync hint */ > if (buf->vb->skip_cache_sync_on_finish) > return; > > + /* > + * Coherent MMAP buffers do not need to be synced, unlike coherent > + * USERPTR and non-coherent MMAP buffers. Ditto. > + */ > + if (buf->vb->memory == V4L2_MEMORY_MMAP && vb2_dc_is_coherent(buf)) > + return; > + > if (!sgt) > return; > > + /* For both USERPTR and non-coherent MMAP */ > dma_sync_sgtable_for_cpu(buf->dev, sgt, buf->dma_dir); > + > + /* Non-coherrent MMAP only */ > + if (!vb2_dc_is_coherent(buf) && buf->vaddr) > + invalidate_kernel_vmap_range(buf->vaddr, buf->size); > } > > /*********************************************/ > /* callbacks for MMAP buffers */ > /*********************************************/ > > +static void __vb2_dc_put(struct vb2_dc_buf *buf) > +{ > + if (vb2_dc_is_coherent(buf)) { > + dma_free_attrs(buf->dev, buf->size, buf->cookie, > + buf->dma_addr, buf->attrs); > + return; > + } > + > + if (buf->vaddr) > + dma_vunmap_noncontiguous(buf->dev, buf->vaddr); > + dma_free_noncontiguous(buf->dev, buf->size, > + buf->dma_sgt, buf->dma_addr); > +} > + > static void vb2_dc_put(void *buf_priv) nit: Unrelated to this patch or series, but could be a follow-up clean-up: Could we rename this and the newly added function to include mmap in the name, since it's only for MMAP buffers? Best regards, Tomasz