dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Jerome Glisse <j.glisse@gmail.com>
Cc: Tomasz Stanislawski <t.stanislaws@samsung.com>,
	kyungmin.park@samsung.com, dri-devel@lists.freedesktop.org,
	linaro-mm-sig@lists.linaro.org, sakari.ailus@iki.fi,
	Andrzej Pietrasiewicz <andrzej.p@samsung.com>,
	airlied@redhat.com
Subject: Re: [Linaro-mm-sig] [RFCv2 PATCH 2/9 - 4/4] v4l: vb2-dma-contig: update and code refactoring
Date: Tue, 27 Mar 2012 18:58:59 +0200	[thread overview]
Message-ID: <9802988.GJ5c7GM4Nh@avalon> (raw)
In-Reply-To: <CAH3drwZMuNopW7eymBmXR0gy5dw4wfvmG9+-SEyudDEkwkEW5Q@mail.gmail.com>

Hi Jerome,

On Tuesday 27 March 2012 12:45:23 Jerome Glisse wrote:
> On Tue, Mar 27, 2012 at 11:01 AM, Laurent Pinchart wrote:
> > On Thursday 22 March 2012 16:58:27 Tomasz Stanislawski wrote:
> >> On 03/22/2012 03:42 PM, Laurent Pinchart wrote:
> >> > On Thursday 22 March 2012 14:36:33 Tomasz Stanislawski wrote:
> >> >> On 03/22/2012 11:50 AM, Laurent Pinchart wrote:
> >> >>> On Thursday 22 March 2012 11:02:23 Laurent Pinchart wrote:

[snip]

> >> >>>> +        *copy_vma = vb2_get_vma(vma);
> >> >>>> +        if (!*copy_vma) {
> >> >>>> +                printk(KERN_ERR "failed to copy vma\n");
> >> >>>> +                ret = -ENOMEM;
> >> >>>> +                goto cleanup;
> >> >>>> +        }
> >> >>> 
> >> >>> Do we really need to make a copy of the VMA ? The only reason why we
> >> >>> store a pointer to it is to check the flags in vb2_dc_put_userptr().
> >> >>> We could store the flags instead and avoid
> >> >>> vb2_get_dma()/vb2_put_dma() calls altogether.
> >> >> 
> >> >> I remember that there was a very good reason of copying this vma
> >> >> structure.
> >> >> You caught me on 'cargo-cult' programming.
> >> >> I will do some reverse engineering and try to answer it soon.
> >> > 
> >> > OK :-) I'm not copying the VMA in the OMAP3 ISP driver, which is why
> >> > this caught my eyes. If you find the reason why copying it is needed,
> >> > please add a comment to the code.
> >> 
> >> The reason of copying vma was that 'struct vma' has no reference
> >> counters.
> >> Therefore it could be deleted after mm lock is freed, ending with freeing
> >> its all pages belonging to vma. To prevent it, a copy of vma is created.
> >> Notice that inside vb2_get_vma the callback open is called for original
> >> vma, preventing memory from being released. On vb2_put_vma the
> >> complementary close is called.
> > 
> > Feel free to prove me wrong, but I think get_user_pages() is enough to
> > prevent the pages from being freed, even if the VMA is deleted.
> > 
> > However, there's one subtle issue that we will need to deal with when we
> > will implement cache management. It took me a lot of time to debug and
> > fix it when I was working on the OMAP3 ISP driver, so I'll explain it in
> > the hope that someone will find it later when dealing with the same
> > problems :-)
> > 
> > When a buffer is queued, the OMAP3 ISP driver cleans up the cache using
> > the userspace mapping addresses (for USERPTR buffers). This might be a bad
> > thing, but that's the way we currently implement that.
> > 
> > A prior get_user_pages() call will make sure pages are not freed, but due
> > to optimization in the lockless memory management algorithms the
> > userspace mapping can disappear: the kernel might consider that a page
> > can be freed, remove its userspace mapping, and then find out that the
> > page is locked. It will then move on without restoring the userspace
> > mapping, which will be restored when the next page fault occurs.
> > 
> > When cleaning the cache using the userspace mapping addresses, any page
> > for which the userspace mapping has been removed will trigger a page
> > fault. The page fault handler (do_page_fault() in arm/arch/mm/fault.c)
> > will try to read_lock mmap_sem. If it fails, it will check if the page
> > fault occured in userspace context, or from a known exception location. As
> > neither condition is true, it will panic.
> > 
> > The solution I found to this issue was to lock the VMA. This ensured that
> > the userspace mapping would stay in place. See
> > isp_video_buffer_lock_vma() in drivers/media/video/omap3isp/ispqueue.c.
> > You could use a similar approach here if you want to ensure that
> > userspace mappings are not removed, but once again I don't think that's
> > needed (until we get to cache handling) as
> > get_user_pages() will ensure that the pages are not freed.
> 
> I think the proper solution is to not use any user allocated memory and
> always use dma-buf. Some evil process thread might unlock the vma behind
> your back and you back to the original issue.
> 
> The linux memory management is not designed to easily allow use of user
> allocated memory by a device to do dma to/from it, at least not for the
> usecase where dma operation might happen over long period of time.
> 
> I guess some VMA change might help but this might also be hurt full and i am
> not familiar enough with the whole memory management to venture a guess on
> what kind if implication there is.

I agree with you, we should move away from using user-allocated memory. I just 
wanted to explain what I did and why for reference purpose, as it took me lots 
of time to debug the issue. Until every driver moves to dma-buf (and for some 
time after as well) we will still need to support user-allocated memory in 
V4L2, even if the solution isn't completely bullet-proof.

-- 
Regards,

Laurent Pinchart

  reply	other threads:[~2012-03-27 16:59 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-13 10:16 [RFCv2 PATCH 0/9] Integration of videobuf2 with dmabuf Tomasz Stanislawski
2012-03-13 10:16 ` [RFCv2 PATCH 1/9] v4l: vb2: fixes for DMABUF support Tomasz Stanislawski
2012-03-22  7:56   ` Laurent Pinchart
2012-03-13 10:17 ` [RFCv2 PATCH 2/9] v4l: vb2-dma-contig: update and code refactoring Tomasz Stanislawski
2012-03-22  9:27   ` Laurent Pinchart
2012-03-22 10:02     ` [RFCv2 PATCH 2/9 - 1/4] v4l: vb2-dma-contig: Shorten vb2_dma_contig prefix to vb2_dc Laurent Pinchart
2012-03-22 10:02     ` [RFCv2 PATCH 2/9 - 2/4] v4l: vb2-dma-contig: Reorder functions Laurent Pinchart
2012-03-22 10:02     ` [RFCv2 PATCH 2/9 - 3/4] v4l: vb2-dma-contig: Remove unneeded allocation context structure Laurent Pinchart
2012-03-22 10:02     ` [RFCv2 PATCH 2/9 - 4/4] v4l: vb2-dma-contig: update and code refactoring Laurent Pinchart
2012-03-22 10:50       ` [Linaro-mm-sig] " Laurent Pinchart
2012-03-22 13:36         ` Tomasz Stanislawski
2012-03-22 14:42           ` Laurent Pinchart
2012-03-22 14:52             ` Subash Patel
2012-03-22 16:18               ` Tomasz Stanislawski
2012-03-22 15:58             ` Tomasz Stanislawski
2012-03-27 15:01               ` Laurent Pinchart
2012-03-27 16:45                 ` Jerome Glisse
2012-03-27 16:58                   ` Laurent Pinchart [this message]
2012-03-22 13:42   ` [Linaro-mm-sig] [RFCv2 PATCH 2/9] " Subash Patel
2012-03-13 10:17 ` [RFCv2 PATCH 3/9] v4l: vb2: Add dma-contig allocator as dma_buf user Tomasz Stanislawski
2012-03-22 11:04   ` Laurent Pinchart
2012-03-26 15:53     ` Tomasz Stanislawski
2012-03-28 15:50       ` Laurent Pinchart
2012-03-13 10:17 ` [RFCv2 PATCH 4/9] v4l: add buffer exporting via dmabuf Tomasz Stanislawski
2012-03-18 21:47   ` Daniel Vetter
2012-03-22 11:16   ` Laurent Pinchart
2012-03-22 13:57     ` [Linaro-mm-sig] " Subash Patel
2012-03-22 14:07       ` Laurent Pinchart
2012-03-22 14:26         ` Daniel Vetter
2012-03-22 14:43           ` Laurent Pinchart
2012-03-22 14:59         ` Subash Patel
2012-03-22 15:09           ` Laurent Pinchart
2012-03-23 11:33     ` Tomasz Stanislawski
2012-03-27 10:21       ` Laurent Pinchart
2012-03-13 10:17 ` [RFCv2 PATCH 5/9] v4l: vb2: " Tomasz Stanislawski
2012-03-22 11:24   ` Laurent Pinchart
2012-03-23 11:50     ` Tomasz Stanislawski
2012-03-13 10:17 ` [RFCv2 PATCH 6/9] v4l: vb2-dma-contig: add support for DMABUF exporting Tomasz Stanislawski
2012-03-13 10:17 ` [RFCv2 PATCH 7/9] v4l: vb2-dma-contig: change map/unmap behaviour Tomasz Stanislawski
2012-03-22 12:15   ` Laurent Pinchart
2012-03-22 12:25     ` Daniel Vetter
2012-03-27 10:31       ` Laurent Pinchart
2012-03-13 10:17 ` [RFCv2 PATCH 8/9] v4l: fimc: integrate capture i-face with dmabuf Tomasz Stanislawski
2012-03-13 10:17 ` [RFCv2 PATCH 9/9] v4l: s5p-tv: mixer: integrate " Tomasz Stanislawski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9802988.GJ5c7GM4Nh@avalon \
    --to=laurent.pinchart@ideasonboard.com \
    --cc=airlied@redhat.com \
    --cc=andrzej.p@samsung.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=j.glisse@gmail.com \
    --cc=kyungmin.park@samsung.com \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=sakari.ailus@iki.fi \
    --cc=t.stanislaws@samsung.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox