[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAKMK7uH3P-6zs5MVceFD7872owqtcktqsTaQAOKNyaBg4_w=aA@mail.gmail.com>
Date: Wed, 7 Oct 2020 20:01:42 +0200
From: Daniel Vetter <daniel.vetter@...ll.ch>
To: Jason Gunthorpe <jgg@...pe.ca>
Cc: DRI Development <dri-devel@...ts.freedesktop.org>,
LKML <linux-kernel@...r.kernel.org>, kvm@...r.kernel.org,
Linux MM <linux-mm@...ck.org>,
Linux ARM <linux-arm-kernel@...ts.infradead.org>,
linux-samsung-soc <linux-samsung-soc@...r.kernel.org>,
"open list:DMA BUFFER SHARING FRAMEWORK"
<linux-media@...r.kernel.org>, linux-s390@...r.kernel.org,
Dan Williams <dan.j.williams@...el.com>,
Kees Cook <keescook@...omium.org>,
Rik van Riel <riel@...hat.com>,
Benjamin Herrensmidt <benh@...nel.crashing.org>,
Dave Airlie <airlied@...ux.ie>,
Hugh Dickins <hugh@...itas.com>,
Andrew Morton <akpm@...ux-foundation.org>,
John Hubbard <jhubbard@...dia.com>,
Jérôme Glisse <jglisse@...hat.com>,
Jan Kara <jack@...e.cz>,
Daniel Vetter <daniel.vetter@...el.com>
Subject: Re: [PATCH 07/13] mm: close race in generic_access_phys
On Wed, Oct 7, 2020 at 7:27 PM Jason Gunthorpe <jgg@...pe.ca> wrote:
>
> On Wed, Oct 07, 2020 at 06:44:20PM +0200, Daniel Vetter wrote:
> > Way back it was a reasonable assumptions that iomem mappings never
> > change the pfn range they point at. But this has changed:
> >
> > - gpu drivers dynamically manage their memory nowadays, invalidating
> > ptes with unmap_mapping_range when buffers get moved
> >
> > - contiguous dma allocations have moved from dedicated carvetouts to
> > cma regions. This means if we miss the unmap the pfn might contain
> > pagecache or anon memory (well anything allocated with GFP_MOVEABLE)
> >
> > - even /dev/mem now invalidates mappings when the kernel requests that
> > iomem region when CONFIG_IO_STRICT_DEVMEM is set, see 3234ac664a87
> > ("/dev/mem: Revoke mappings when a driver claims the region")
> >
> > Accessing pfns obtained from ptes without holding all the locks is
> > therefore no longer a good idea. Fix this.
> >
> > Since ioremap might need to manipulate pagetables too we need to drop
> > the pt lock and have a retry loop if we raced.
> >
> > While at it, also add kerneldoc and improve the comment for the
> > vma_ops->access function. It's for accessing, not for moving the
> > memory from iomem to system memory, as the old comment seemed to
> > suggest.
> >
> > References: 28b2ee20c7cb ("access_process_vm device memory infrastructure")
> > Cc: Jason Gunthorpe <jgg@...pe.ca>
> > Cc: Dan Williams <dan.j.williams@...el.com>
> > Cc: Kees Cook <keescook@...omium.org>
> > Cc: Rik van Riel <riel@...hat.com>
> > Cc: Benjamin Herrensmidt <benh@...nel.crashing.org>
> > Cc: Dave Airlie <airlied@...ux.ie>
> > Cc: Hugh Dickins <hugh@...itas.com>
> > Cc: Andrew Morton <akpm@...ux-foundation.org>
> > Cc: John Hubbard <jhubbard@...dia.com>
> > Cc: Jérôme Glisse <jglisse@...hat.com>
> > Cc: Jan Kara <jack@...e.cz>
> > Cc: Dan Williams <dan.j.williams@...el.com>
> > Cc: linux-mm@...ck.org
> > Cc: linux-arm-kernel@...ts.infradead.org
> > Cc: linux-samsung-soc@...r.kernel.org
> > Cc: linux-media@...r.kernel.org
> > Signed-off-by: Daniel Vetter <daniel.vetter@...el.com>
> > ---
> > include/linux/mm.h | 3 ++-
> > mm/memory.c | 44 ++++++++++++++++++++++++++++++++++++++++++--
> > 2 files changed, 44 insertions(+), 3 deletions(-)
>
> This does seem to solve the race with revoke_devmem(), but it is really ugly.
>
> It would be much nicer to wrap a rwsem around this access and the unmap.
>
> Any place using it has a nice linear translation from vm_off to pfn,
> so I don't think there is a such a good reason to use follow_pte in
> the first place.
>
> ie why not the helper be this:
>
> int generic_access_phys(unsigned long pfn, unsigned long pgprot,
> void *buf, size_t len, bool write)
>
> Then something like dev/mem would compute pfn and obtain the lock:
>
> dev_access(struct vm_area_struct *vma, unsigned long addr, void *buf, int len, int write)
> {
> cpu_addr = vma->vm_pgoff*PAGE_SIZE + (addr - vma->vm_start));
>
> /* FIXME: Has to be over each page of len */
> if (!devmem_is_allowed_access(PHYS_PFN(cpu_addr/4096)))
> return -EPERM;
>
> down_read(&mem_sem);
> generic_access_phys(cpu_addr/4096, pgprot_val(vma->vm_page_prot),
> buf, len, write);
> up_read(&mem_sem);
> }
>
> The other cases looked simpler because they don't revoke, here the
> mmap_sem alone should be enough protection, they would just need to
> provide the linear translation to pfn.
>
> What do you think?
I think it'd fix the bug, until someone wires ->access up for
drivers/gpu, or the next subsystem. This is also just for ptrace, so
we really don't care when we stall the vm badly and other silly
things. So I figured the somewhat ugly, but full generic solution is
the better one, so that people who want to be able to ptrace
read/write their iomem mmaps can just sprinkle this wherever they feel
like.
But yeah if we go with most minimal fix, i.e. only trying to fix the
current users, then your thing should work and is simpler. But it
leaves the door open for future problems.
-Daniel
--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Powered by blists - more mailing lists