Re: [PATCH 09/16] page-flags: define PG_reserved behavior on compound pages

From: Christoph Hellwig
Date: Mon Feb 03 2020 - 12:29:38 EST


On Mon, Feb 03, 2020 at 06:18:44PM +0300, Kirill A. Shutemov wrote:
> > Much later than you would ever expect, but we just had a user update an
> > ancient device and trip over this.
> > https://gitlab.freedesktop.org/drm/intel/issues/1027
> >
> > In drm_pci_alloc() we allocate a high-order page (for it to be physically
> > contiguous) and mark each page as Reserved.
> >
> > dmah->vaddr = dma_alloc_coherent(&dev->pdev->dev, size,
> > &dmah->busaddr,
> > GFP_KERNEL | __GFP_COMP);
> >
> > /* XXX - Is virt_to_page() legal for consistent mem? */
> > /* Reserve */
> > for (addr = (unsigned long)dmah->vaddr, sz = size;
> > sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
> > SetPageReserved(virt_to_page((void *)addr));
> > }
> >
> > It's been doing that since

This code is completely and utterly broken. Drivers were never allowed
to call virt_to_page() on the memory returned from dma_alloc_coherent
(or pci_alloc_consistent before that), as many implementations return
virtual addresses that are not in the kernel mapping. So this code
needs to go away and not papered over.