• Alistair Popple's avatar
    mm/memremap.c: take a pgmap reference on page allocation · 0dc45ca1
    Alistair Popple authored
    ZONE_DEVICE pages have a struct dev_pagemap which is allocated by a
    driver.  When the struct page is first allocated by the kernel in
    memremap_pages() a reference is taken on the associated pagemap to ensure
    it is not freed prior to the pages being freed.
    
    Prior to 27674ef6 ("mm: remove the extra ZONE_DEVICE struct page
    refcount") pages were considered free and returned to the driver when the
    reference count dropped to one.  However the pagemap reference was not
    dropped until the page reference count hit zero.  This would occur as part
    of the final put_page() in memunmap_pages() which would wait for all pages
    to be freed prior to returning.
    
    When the extra refcount was removed the pagemap reference was no longer
    being dropped in put_page().  Instead memunmap_pages() was changed to
    explicitly drop the pagemap references.  This means that memunmap_pages()
    can complete even though pages are still mapped by the kernel which can
    lead to kernel crashes, particularly if a driver frees the pagemap.
    
    To fix this drivers should take a pagemap reference when allocating the
    page.  This reference can then be returned when the page is freed.
    
    Link: https://lkml.kernel.org/r/12d155ec727935ebfbb4d639a03ab374917ea51b.1664366292.git-series.apopple@nvidia.comSigned-off-by: default avatarAlistair Popple <apopple@nvidia.com>
    Fixes: 27674ef6 ("mm: remove the extra ZONE_DEVICE struct page refcount")
    Cc: Jason Gunthorpe <jgg@nvidia.com>
    Cc: Felix Kuehling <Felix.Kuehling@amd.com>
    Cc: Alex Deucher <alexander.deucher@amd.com>
    Cc: Christian König <christian.koenig@amd.com>
    Cc: Ben Skeggs <bskeggs@redhat.com>
    Cc: Lyude Paul <lyude@redhat.com>
    Cc: Ralph Campbell <rcampbell@nvidia.com>
    Cc: Alex Sierra <alex.sierra@amd.com>
    Cc: John Hubbard <jhubbard@nvidia.com>
    Cc: Dan Williams <dan.j.williams@intel.com>
    
    Cc: David Hildenbrand <david@redhat.com>
    Cc: "Huang, Ying" <ying.huang@intel.com>
    Cc: Matthew Wilcox <willy@infradead.org>
    Cc: Michael Ellerman <mpe@ellerman.id.au>
    Cc: Yang Shi <shy828301@gmail.com>
    Cc: Zi Yan <ziy@nvidia.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    0dc45ca1
memremap.c 15 KB