• Alistair Popple's avatar
    kernel/resource: fix locking in request_free_mem_region · 56fd9491
    Alistair Popple authored
    request_free_mem_region() is used to find an empty range of physical
    addresses for hotplugging ZONE_DEVICE memory.  It does this by iterating
    over the range of possible addresses using region_intersects() to see if
    the range is free before calling request_mem_region() to allocate the
    region.
    
    However the resource_lock is dropped between these two calls meaning by
    the time request_mem_region() is called in request_free_mem_region()
    another thread may have already reserved the requested region.  This
    results in unexpected failures and a message in the kernel log from
    hitting this condition:
    
            /*
             * mm/hmm.c reserves physical addresses which then
             * become unavailable to other users.  Conflicts are
             * not expected.  Warn to aid debugging if encountered.
             */
            if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
                    pr_warn("Unaddressable device %s %pR conflicts with %pR",
                            conflict->name, conflict, res);
    
    These unexpected failures can be corrected by holding resource_lock across
    the two calls.  This also requires memory allocation to be performed prior
    to taking the lock.
    
    Link: https://lkml.kernel.org/r/20210419070109.4780-3-apopple@nvidia.comSigned-off-by: default avatarAlistair Popple <apopple@nvidia.com>
    Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
    Cc: Balbir Singh <bsingharora@gmail.com>
    Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
    Cc: Dan Williams <dan.j.williams@intel.com>
    Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
    Cc: Jerome Glisse <jglisse@redhat.com>
    Cc: John Hubbard <jhubbard@nvidia.com>
    Cc: Muchun Song <smuchun@gmail.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    56fd9491
resource.c 48.1 KB