Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3632521pxu; Tue, 15 Dec 2020 11:26:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJzZ3c3bt7ZFDbHmojIY4ZJzO26XvxI/cAlucZ3wGsGh6F5v9J9mMglUE6mtDQDGxAIIb46i X-Received: by 2002:a50:e68a:: with SMTP id z10mr3876950edm.28.1608060412151; Tue, 15 Dec 2020 11:26:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608060412; cv=none; d=google.com; s=arc-20160816; b=gRq2pCYWbLrggEo1tEJvlrna7XzT3OOja4LaNIVcBNWPFungWnSWutLkjJX/+mLSMB Op5ntAFjGT5bqphDUSmgVUnMB5y0fXXPHN1Rm8QzlPlOP3sIQt9bt4E+5jgjcpLRVgD0 1TJhz9IPbyL2bK8iZ++K4G38JBB2zUOlZ23T1Lu+cq2Xim+fl4iH5LvvCQOFb0TVugnf JCRl0cLbop0QXhks4ZudyxAuEKCxb57q/ZmpD3ybzMdxMLXU0SJ2WzpThSUC69+zZCA7 0oLFxpBRoaRt57IFYkpW1a0MXixhpptacTF5yhJH9+mvXWzopKZzJDOL1H7UIt5ngipb wTUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=jVFwMMQ6DKs7XfDfHBL54ryc0drSH1XuLSwFgllEzFk=; b=1LA+855Oqd21ydM1kdH0v8BOtmxs8I12hR20fucDtXirHBzSbItZAG0XGOTVzLprdG zkmUWanRoKTQ5+9sL2vK/BPZ04QOX9zS6FiOvwI4TGSQ3l3LQtVui7SYK0dNJQZ5jdYT 6zTHKsoRpdCmBXsHs5U8Nhnloe/mIkjBHfKTW2UHn9IBsbiNh4w0Ac7Zp28KiZhuHWtR abJiN2VGChZBxIQScZfzO+qFY0Noi/k4BSNXN/EV27Ge+vIvqaFU2fGOLmRnO8HfiXuP natnK2uk5CNHgSL2dNY5WQzG7zH/GnixodkSFhYQR16CmBJnDTzuRH6Q5xWZ3MV3bIlq sjug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ndufresne-ca.20150623.gappssmtp.com header.s=20150623 header.b="gwGflH/b"; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p12si1347162eji.735.2020.12.15.11.26.27; Tue, 15 Dec 2020 11:26:52 -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=@ndufresne-ca.20150623.gappssmtp.com header.s=20150623 header.b="gwGflH/b"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731920AbgLOTWn (ORCPT + 99 others); Tue, 15 Dec 2020 14:22:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731814AbgLOTWc (ORCPT ); Tue, 15 Dec 2020 14:22:32 -0500 Received: from mail-qk1-x744.google.com (mail-qk1-x744.google.com [IPv6:2607:f8b0:4864:20::744]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A463C0617B0 for ; Tue, 15 Dec 2020 11:21:52 -0800 (PST) Received: by mail-qk1-x744.google.com with SMTP id h4so15414752qkk.4 for ; Tue, 15 Dec 2020 11:21:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ndufresne-ca.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=jVFwMMQ6DKs7XfDfHBL54ryc0drSH1XuLSwFgllEzFk=; b=gwGflH/bRbNg9bfRJPwgIerK4m2SnJXQN6M9Rt9hgfg5E2Tc5Sl3tb/jAHmLeO6Vws md2nJXWa8HjTp88IvMCY+CDp1frGOB15dwZjGkiLeAqkCnT5/2LgtdV+ktApgHfQp4jX LoplF7W9Sp9ZDAc4oR64TZ7ApRAYGmbK5tmHdEu2sX+c9806EfFpIS6Q5rgMhp3BtyQq X6eXZjKf5z9HOraU43WXGNkTC08I7228YqmofgOMcxLT+qAUAr6majb4bg/8t98lMvhs 8eMz5zC64SMrIWbFa+tSqAmysMprOeot8Tovbxx0eOCLVnXvBBWUh6olVLDB91f7PsSg xFYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=jVFwMMQ6DKs7XfDfHBL54ryc0drSH1XuLSwFgllEzFk=; b=kFaPLuWRRYrxKsSQVsP1fE9FoV9dIzABR5QpnmKMgdlBH1cVG354KTP/xX9z0vXgzA eU2ftbLrBQCywu/gTbsSQ1s6xnn38pdQp1ENqSF++MRf2ewwCuTbns+QDw7dsBhuL3xI prHg1My7VyNPo53lngWnhek9Yuccdpp0xFEefM24X9PSbyflncJpvYeosprlg4W0Pbq3 1IMp3s0qB+fZyPaxyBziI2qxEEJHO+JQ4nPAmVEuk5pnhN7KE7DRBFjKfdz9aOruYK0W HntYBbHcMQD1imOm0uL9sYrO9wpe+aMNmSlFttmFAACO81Ukhwzj0N3H6eW1iifFKP93 58nw== X-Gm-Message-State: AOAM531h5SSgsYrN+V79+Fwv3Yv08L1NbrEz+HGmftKD8I3/X7rp2XjH Uz87YcbT9Uoc2DZbumm7knWzhw== X-Received: by 2002:a05:620a:69c:: with SMTP id f28mr10622571qkh.127.1608060111025; Tue, 15 Dec 2020 11:21:51 -0800 (PST) Received: from nicolas-tpx395.lan (173-246-12-168.qc.cable.ebox.net. [173.246.12.168]) by smtp.gmail.com with ESMTPSA id l20sm19454994qtu.25.2020.12.15.11.21.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Dec 2020 11:21:50 -0800 (PST) Message-ID: Subject: Re: [PATCH] media: venus: use contig vb2 ops From: Nicolas Dufresne To: Stanimir Varbanov , Tomasz Figa Cc: Alexandre Courbot , Fritz Koenig , Linux Media Mailing List , Linux Kernel Mailing List , linux-arm-msm , Robin Murphy Date: Tue, 15 Dec 2020 14:21:48 -0500 In-Reply-To: References: <20201214125703.866998-1-acourbot@chromium.org> <5319c101-f4a4-9c99-b15d-4999366f7a63@linaro.org> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.38.1 (3.38.1-1.fc33) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Le mardi 15 décembre 2020 à 15:54 +0200, Stanimir Varbanov a écrit : > Hi Tomasz, > > On 12/15/20 1:47 PM, Tomasz Figa wrote: > > On Tue, Dec 15, 2020 at 8:16 PM Stanimir Varbanov > > wrote: > > > > > > Hi, > > > > > > Cc: Robin > > > > > > On 12/14/20 2:57 PM, Alexandre Courbot wrote: > > > > This driver uses the SG vb2 ops, but effectively only ever accesses the > > > > first entry of the SG table, indicating that it expects a flat layout. > > > > Switch it to use the contiguous ops to make sure this expected invariant > > > > > > Under what circumstances the sg table will has nents > 1? I came down to > > > [1] but not sure I got it right. > > > > > > I'm afraid that for systems with low amount of system memory and when > > > the memory become fragmented, the driver will not work. That's why I > > > started with sg allocator. > > > > It is exactly the opposite. The vb2-dma-contig allocator is "contig" > > in terms of the DMA (aka IOVA) address space. In other words, it > > guarantees that having one DMA address and length fully describes the > > Ahh, I missed that part. Looks like I misunderstood videobu2 contig > allocator. I'm learning everyday too, but I'm surprised I don't see a call to vb2_dma_contig_set_max_seg_size() in this driver (I could also just have missed a patch when overlooking this thread) ? The reason I'm asking, doc says it should be called by driver supporting IOMMU, which seems to be the case for such drivers (MFC, exynos4-is, exynos-gsc, mtk- mdp, s5p-g2d, hantro, rkvdec, zoran, ti-vpe, ..). I posting it, worst case it's all covered and we are good, otherwise perhaps a downstream patch didn't make it ? /** * vb2_dma_contig_set_max_seg_size() - configure DMA max segment size * @dev: device for configuring DMA parameters * @size: size of DMA max segment size to set * * To allow mapping the scatter-list into a single chunk in the DMA * address space, the device is required to have the DMA max segment * size parameter set to a value larger than the buffer size. Otherwise, * the DMA-mapping subsystem will split the mapping into max segment * size chunks. This function sets the DMA max segment size * parameter to let DMA-mapping map a buffer as a single chunk in DMA * address space. * This code assumes that the DMA-mapping subsystem will merge all * scatterlist segments if this is really possible (for example when * an IOMMU is available and enabled). * Ideally, this parameter should be set by the generic bus code, but it * is left with the default 64KiB value due to historical litmiations in * other subsystems (like limited USB host drivers) and there no good * place to set it to the proper value. * This function should be called from the drivers, which are known to * operate on platforms with IOMMU and provide access to shared buffers * (either USERPTR or DMABUF). This should be done before initializing * videobuf2 queue. */ regards, Nicolas > > > buffer. This seems to be the requirement of the hardware/firmware > > handled by the venus driver. If the device is behind an IOMMU, which > > is the case for the SoCs in question, the underlying DMA ops will > > actually allocate a discontiguous set of pages, so it has nothing to > > do to system memory amount or fragmentation. If for some reason the > > IOMMU can't be used, there is no way around, the memory needs to be > > contiguous because of the hardware/firmware/driver expectation. > > > > On the other hand, the vb2-dma-sg allocator doesn't have any > > continuity guarantees for the DMA, or any other, address space. The > > current code works fine, because it calls dma_map_sg() on the whole > > set of pages and that ends up mapping it contiguously in the IOVA > > space, but that's just an implementation detail, not an API guarantee. > > It was good to know. Thanks for the explanation. > > > > > Best regards, > > Tomasz > > > > > > > > [1] > > > https://elixir.bootlin.com/linux/v5.10.1/source/drivers/iommu/dma-iommu.c#L782 > > > > > > > is always enforced. Since the device is supposed to be behind an IOMMU > > > > this should have little to none practical consequences beyond making the > > > > driver not rely on a particular behavior of the SG implementation. > > > > > > > > Reported-by: Tomasz Figa > > > > Signed-off-by: Alexandre Courbot > > > > --- > > > > Hi everyone, > > > > > > > > It probably doesn't hurt to fix this issue before some actual issue > > > > happens. > > > > I have tested this patch on Chrome OS and playback was just as fine as > > > > with > > > > the SG ops. > > > > > > > >  drivers/media/platform/Kconfig              | 2 +- > > > >  drivers/media/platform/qcom/venus/helpers.c | 9 ++------- > > > >  drivers/media/platform/qcom/venus/vdec.c    | 6 +++--- > > > >  drivers/media/platform/qcom/venus/venc.c    | 6 +++--- > > > >  4 files changed, 9 insertions(+), 14 deletions(-) > > > > > > > > diff --git a/drivers/media/platform/Kconfig > > > > b/drivers/media/platform/Kconfig > > > > index 35a18d388f3f..d9d7954111f2 100644 > > > > --- a/drivers/media/platform/Kconfig > > > > +++ b/drivers/media/platform/Kconfig > > > > @@ -533,7 +533,7 @@ config VIDEO_QCOM_VENUS > > > >       depends on INTERCONNECT || !INTERCONNECT > > > >       select QCOM_MDT_LOADER if ARCH_QCOM > > > >       select QCOM_SCM if ARCH_QCOM > > > > -     select VIDEOBUF2_DMA_SG > > > > +     select VIDEOBUF2_DMA_CONTIG > > > >       select V4L2_MEM2MEM_DEV > > > >       help > > > >         This is a V4L2 driver for Qualcomm Venus video accelerator > > > > diff --git a/drivers/media/platform/qcom/venus/helpers.c > > > > b/drivers/media/platform/qcom/venus/helpers.c > > > > index 50439eb1ffea..859d260f002b 100644 > > > > --- a/drivers/media/platform/qcom/venus/helpers.c > > > > +++ b/drivers/media/platform/qcom/venus/helpers.c > > > > @@ -7,7 +7,7 @@ > > > >  #include > > > >  #include > > > >  #include > > > > -#include > > > > +#include > > > >  #include > > > >  #include > > > > > > > > @@ -1284,14 +1284,9 @@ int venus_helper_vb2_buf_init(struct vb2_buffer > > > > *vb) > > > >       struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); > > > >       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); > > > >       struct venus_buffer *buf = to_venus_buffer(vbuf); > > > > -     struct sg_table *sgt; > > > > - > > > > -     sgt = vb2_dma_sg_plane_desc(vb, 0); > > > > -     if (!sgt) > > > > -             return -EFAULT; > > > > > > > >       buf->size = vb2_plane_size(vb, 0); > > > > -     buf->dma_addr = sg_dma_address(sgt->sgl); > > > > > > Can we do it: > > > > > >         if (WARN_ON(sgt->nents > 1)) > > >                 return -EFAULT; > > > > > > I understand that logically using dma-sg when the flat layout is > > > expected by the hardware is wrong, but I haven't seen issues until now. > > > > > > > +     buf->dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0); > > > > > > > >       if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) > > > >               list_add_tail(&buf->reg_list, &inst->registeredbufs); > > > > diff --git a/drivers/media/platform/qcom/venus/vdec.c > > > > b/drivers/media/platform/qcom/venus/vdec.c > > > > index 8488411204c3..3fb277c81aca 100644 > > > > --- a/drivers/media/platform/qcom/venus/vdec.c > > > > +++ b/drivers/media/platform/qcom/venus/vdec.c > > > > @@ -13,7 +13,7 @@ > > > >  #include > > > >  #include > > > >  #include > > > > -#include > > > > +#include > > > > > > > >  #include "hfi_venus_io.h" > > > >  #include "hfi_parser.h" > > > > @@ -1461,7 +1461,7 @@ static int m2m_queue_init(void *priv, struct > > > > vb2_queue *src_vq, > > > >       src_vq->io_modes = VB2_MMAP | VB2_DMABUF; > > > >       src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; > > > >       src_vq->ops = &vdec_vb2_ops; > > > > -     src_vq->mem_ops = &vb2_dma_sg_memops; > > > > +     src_vq->mem_ops = &vb2_dma_contig_memops; > > > >       src_vq->drv_priv = inst; > > > >       src_vq->buf_struct_size = sizeof(struct venus_buffer); > > > >       src_vq->allow_zero_bytesused = 1; > > > > @@ -1475,7 +1475,7 @@ static int m2m_queue_init(void *priv, struct > > > > vb2_queue *src_vq, > > > >       dst_vq->io_modes = VB2_MMAP | VB2_DMABUF; > > > >       dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; > > > >       dst_vq->ops = &vdec_vb2_ops; > > > > -     dst_vq->mem_ops = &vb2_dma_sg_memops; > > > > +     dst_vq->mem_ops = &vb2_dma_contig_memops; > > > >       dst_vq->drv_priv = inst; > > > >       dst_vq->buf_struct_size = sizeof(struct venus_buffer); > > > >       dst_vq->allow_zero_bytesused = 1; > > > > diff --git a/drivers/media/platform/qcom/venus/venc.c > > > > b/drivers/media/platform/qcom/venus/venc.c > > > > index 1c61602c5de1..a09550cd1dba 100644 > > > > --- a/drivers/media/platform/qcom/venus/venc.c > > > > +++ b/drivers/media/platform/qcom/venus/venc.c > > > > @@ -10,7 +10,7 @@ > > > >  #include > > > >  #include > > > >  #include > > > > -#include > > > > +#include > > > >  #include > > > >  #include > > > >  #include > > > > @@ -1001,7 +1001,7 @@ static int m2m_queue_init(void *priv, struct > > > > vb2_queue *src_vq, > > > >       src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; > > > >       src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; > > > >       src_vq->ops = &venc_vb2_ops; > > > > -     src_vq->mem_ops = &vb2_dma_sg_memops; > > > > +     src_vq->mem_ops = &vb2_dma_contig_memops; > > > >       src_vq->drv_priv = inst; > > > >       src_vq->buf_struct_size = sizeof(struct venus_buffer); > > > >       src_vq->allow_zero_bytesused = 1; > > > > @@ -1017,7 +1017,7 @@ static int m2m_queue_init(void *priv, struct > > > > vb2_queue *src_vq, > > > >       dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; > > > >       dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; > > > >       dst_vq->ops = &venc_vb2_ops; > > > > -     dst_vq->mem_ops = &vb2_dma_sg_memops; > > > > +     dst_vq->mem_ops = &vb2_dma_contig_memops; > > > >       dst_vq->drv_priv = inst; > > > >       dst_vq->buf_struct_size = sizeof(struct venus_buffer); > > > >       dst_vq->allow_zero_bytesused = 1; > > > > > > > > > > -- > > > regards, > > > Stan >