Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3610529ybl; Mon, 3 Feb 2020 03:28:02 -0800 (PST) X-Google-Smtp-Source: APXvYqzeu4klODaRAopYVVgBF5lrGzGEqpDZ5tHZZEoBRepoYJeN8LvBCpZ+L876IpXCDxt1Z91f X-Received: by 2002:a05:6830:99:: with SMTP id a25mr18067651oto.130.1580729282693; Mon, 03 Feb 2020 03:28:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580729282; cv=none; d=google.com; s=arc-20160816; b=TR4LycTq9IFGXGAaFpTZqZYzH3PqYiobDivny65aKyoFnitE9+w0EvuzcK0sXWAzCl 59cus1HGRUHALo7dYlhQQKZTooAsttEnTvzAJYMzLK1vRvDVzftdbzvMJ/NpYRcnnS8n rzXbwJ3EtSMk0RO2PBecB8OyoBslbrsYk/+QW2VF9iUlKyM2un3bvHe2TTiCt4LbhYgH JWfzYp1gJWR6GA1Vu+fCLcS+iQvAU3/swAfTS7u+yz4bK4zeRfVHDhXlDCV/bx47zTbu d9vq9N3D3HhibdFeE4QSMv7Wif56OKgbTZBKZMaT3JIvRB6a3lWf4UxHPd1zQYY6tfZo hSaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=8SHaJ44CIqYv7UPqeEgaIYIZnbMshOxt9MjhG/l3K1U=; b=MMockta3Ma4c7aobPgmr0h61diHqJ6dzdLuc5U5w8G/QgRqueE6bbdzIBxCFawijlM IGkH4IAh5SbOqjKK0llnMleNMo9u7Om0aFY5Vlw9KH1Cc76cQEWUvRspceNZ0OxzVRnB 7pGXQ2JTCOFB5v0zitoYyUSqPvYPge1sOxdXAxLL4ZvPwHmeTLIdyXE+m21TU0S90DmC EUBf22S8PGqD3f1KknGpWcT5qYjBIubsR/xqw7Krz70oDweefeMyiOjclA9hQ9tS+IQm qWrqMksWnHZ2/P47oM992X9AS7HRmQKx5hbG6gv7yC0ghlNurChmRJlLDXUVg74qw7j0 1Wyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OCwDdpZl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id t3si8212979oig.25.2020.02.03.03.27.50; Mon, 03 Feb 2020 03:28:02 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OCwDdpZl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727584AbgBCKE6 (ORCPT + 99 others); Mon, 3 Feb 2020 05:04:58 -0500 Received: from mail-ed1-f66.google.com ([209.85.208.66]:34184 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727437AbgBCKE5 (ORCPT ); Mon, 3 Feb 2020 05:04:57 -0500 Received: by mail-ed1-f66.google.com with SMTP id r18so15378014edl.1 for ; Mon, 03 Feb 2020 02:04:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=8SHaJ44CIqYv7UPqeEgaIYIZnbMshOxt9MjhG/l3K1U=; b=OCwDdpZltO0a/e3YtS+sBfapkLCZqbTqq+scqhmWAT10AD7xcqudPMaLEH2HUvo/sz DLirtJNrt+hkiQpzdppEvbkZIIFkd5PSMoouUjq+bWeOYK+ggUbdFxP7KAIUUQwWk08o M6oIIXsDKOhjisTXO/CV6DIRgFclmbKa8NXwU= 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; bh=8SHaJ44CIqYv7UPqeEgaIYIZnbMshOxt9MjhG/l3K1U=; b=qELPaBvr7NhONF9bbbT5DzKcfC29u9QZy8pRmZnQuWllctyKUaP6zYYXGWeFpf2fyB fYI0hpDo/ld50/MaSVgTtc+mVQjAGjcgCs81sWmpsFsVNrQQwnQDZBCN4oS6MaqhpCDC 4ecug2GmwV3OjfibAYipwKzwskWMCmbLATSFbX3ltGODuAktVq0fLVgNa168erYMyWOI d+CGODBNCzpL5c4QL8AFt66BnF7N26CmihRZ6nq329eD0nOq7FZUv58OJ7sIlif/U9g6 12HF3BxYozoIqufpm0VKgHuCp01Jz6bs/s6HlX7vj97J19Xi0xS12Va3atYA6ShC19fo lEvQ== X-Gm-Message-State: APjAAAV3ZdJUSUDzQVzKNCV3p9GUlb7cRX5Zefe12XNpK9iJ/RsQkXmR DOXRR3mscolDkqI59Z2HHm+fgmdxPtaoLQ== X-Received: by 2002:a05:6402:142b:: with SMTP id c11mr10909994edx.316.1580724294603; Mon, 03 Feb 2020 02:04:54 -0800 (PST) Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com. [209.85.221.46]) by smtp.gmail.com with ESMTPSA id cf5sm995458ejb.60.2020.02.03.02.04.53 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Feb 2020 02:04:54 -0800 (PST) Received: by mail-wr1-f46.google.com with SMTP id c9so17148592wrw.8 for ; Mon, 03 Feb 2020 02:04:53 -0800 (PST) X-Received: by 2002:adf:f103:: with SMTP id r3mr14462383wro.295.1580724292510; Mon, 03 Feb 2020 02:04:52 -0800 (PST) MIME-Version: 1.0 References: <20191217032034.54897-1-senozhatsky@chromium.org> <20191217032034.54897-13-senozhatsky@chromium.org> <1c5198dc-db4e-47d6-0d8b-259fbbb6372f@xs4all.nl> <560ba621-5396-1ea9-625e-a9f83622e052@xs4all.nl> In-Reply-To: From: Tomasz Figa Date: Mon, 3 Feb 2020 19:04:40 +0900 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC][PATCH 12/15] videobuf2: add begin/end cpu_access callbacks to dma-sg To: Hans Verkuil Cc: Sergey Senozhatsky , Hans Verkuil , Mauro Carvalho Chehab , Kyungmin Park , Marek Szyprowski , Sakari Ailus , Laurent Pinchart , Pawel Osciak , Linux Media Mailing List , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jan 30, 2020 at 9:18 PM Hans Verkuil wrote: > > On 1/30/20 12:02 PM, Tomasz Figa wrote: > > On Tue, Jan 28, 2020 at 5:36 PM Hans Verkuil wrote: > >> > >> On 1/28/20 5:38 AM, Tomasz Figa wrote: > >>> On Fri, Jan 10, 2020 at 7:13 PM Hans Verkuil wrote: > >>>> > >>>> On 12/17/19 4:20 AM, Sergey Senozhatsky wrote: > >>>>> Provide begin_cpu_access() and end_cpu_access() dma_buf_ops > >>>>> callbacks for cache synchronisation on exported buffers. > >>>>> > >>>>> Signed-off-by: Sergey Senozhatsky > >>>>> --- > >>>>> .../media/common/videobuf2/videobuf2-dma-sg.c | 22 +++++++++++++++++++ > >>>>> 1 file changed, 22 insertions(+) > >>>>> > >>>>> diff --git a/drivers/media/common/videobuf2/videobuf2-dma-sg.c b/drivers/media/common/videobuf2/videobuf2-dma-sg.c > >>>>> index 6db60e9d5183..bfc99a0cb7b9 100644 > >>>>> --- a/drivers/media/common/videobuf2/videobuf2-dma-sg.c > >>>>> +++ b/drivers/media/common/videobuf2/videobuf2-dma-sg.c > >>>>> @@ -470,6 +470,26 @@ static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf) > >>>>> vb2_dma_sg_put(dbuf->priv); > >>>>> } > >>>>> > >>>> > >>>> There is no corresponding vb2_sg_buffer_consistent function here. > >>>> > >>>> Looking more closely I see that vb2_dma_sg_alloc doesn't pass the dma_attrs > >>>> argument to dma_map_sg_attrs, thus V4L2_FLAG_MEMORY_NON_CONSISTENT has no > >>>> effect on dma-sg buffers. > >>> > >>> videobuf2-dma-sg allocates the memory using the page allocator > >>> directly, which means that there is no memory consistency guarantee. > >>> > >>>> > >>>> Is there a reason why dma_attrs isn't passed on to dma_map_sg_attrs()? > >>>> > >>> > >>> V4L2_FLAG_MEMORY_NON_CONSISTENT is a flag for dma_alloc_attrs(). It > >>> isn't supposed to do anything for dma_map_sg_attrs(), which is only > >>> supposed to create the device (e.g. IOMMU) mapping for already > >>> allocated memory. > >> > >> Ah, right. > >> > >> But could vb2_dma_sg_alloc_compacted() be modified so that is uses > >> dma_alloc_attrs() instead of alloc_pages()? Sorry, that might be a stupid > >> question, I'm not an expert in this area. All I know is that I hate inconsistent > >> APIs where something works for one thing, but not another. > >> > > > > dma_alloc_attrs() would allocate contiguous memory, which kind of goes > > against the vb2_dma_sg allocator idea. Technically we could call it N > > times with size = 1 page to achieve what we want, but is this really > > what we want? > > > > Given that vb2_dma_sg has always been returning non-consistent memory, > > do we have any good reason to add consistent memory to it? If so, > > perhaps we could still do that in an incremental patch, to avoid > > complicating this already complex series? :) > > I very much agree with that. But this should be very clearly documented. > Should V4L2_CAP_MEMORY_NON_CONSISTENT always be set in this case? > Yes, IMHO that would make sense. My understanding is that currently the consistency of allocated memory is unspecified, so it can be either. With V4L2_FLAG_MEMORY_NON_CONSISTENT, the userspace can explicitly ask for inconsistent memory. Moreover, I'd vote for setting V4L2_CAP_MEMORY_NON_CONSISTENT when V4L2_FLAG_MEMORY_NON_CONSISTENT is guaranteed to return inconsistent memory to avoid "optional" features or "hints" without guaranteed behavior. > Regards, > > Hans > > > > > Best regards, > > Tomasz > > > >> Regards, > >> > >> Hans > >> > >>> > >>>> I suspect it was just laziness in the past, and that it should be wired > >>>> up, just as for dma-contig. > >>>> > >>>> Regards, > >>>> > >>>> Hans > >>>> > >>>>> +static int vb2_dma_sg_dmabuf_ops_begin_cpu_access(struct dma_buf *dbuf, > >>>>> + enum dma_data_direction direction) > >>>>> +{ > >>>>> + struct vb2_dma_sg_buf *buf = dbuf->priv; > >>>>> + struct sg_table *sgt = buf->dma_sgt; > >>>>> + > >>>>> + dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir); > >>>>> + return 0; > >>>>> +} > >>>>> + > >>>>> +static int vb2_dma_sg_dmabuf_ops_end_cpu_access(struct dma_buf *dbuf, > >>>>> + enum dma_data_direction direction) > >>>>> +{ > >>>>> + struct vb2_dma_sg_buf *buf = dbuf->priv; > >>>>> + struct sg_table *sgt = buf->dma_sgt; > >>>>> + > >>>>> + dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir); > >>>>> + return 0; > >>>>> +} > >>>>> + > >>>>> static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf) > >>>>> { > >>>>> struct vb2_dma_sg_buf *buf = dbuf->priv; > >>>>> @@ -488,6 +508,8 @@ static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops = { > >>>>> .detach = vb2_dma_sg_dmabuf_ops_detach, > >>>>> .map_dma_buf = vb2_dma_sg_dmabuf_ops_map, > >>>>> .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap, > >>>>> + .begin_cpu_access = vb2_dma_sg_dmabuf_ops_begin_cpu_access, > >>>>> + .end_cpu_access = vb2_dma_sg_dmabuf_ops_end_cpu_access, > >>>>> .vmap = vb2_dma_sg_dmabuf_ops_vmap, > >>>>> .mmap = vb2_dma_sg_dmabuf_ops_mmap, > >>>>> .release = vb2_dma_sg_dmabuf_ops_release, > >>>>> > >>>> > >> >