]> git.baikalelectronics.ru Git - kernel.git/commit
mm/memremap: fix memunmap_pages() race with get_dev_pagemap()
authorMiaohe Lin <linmiaohe@huawei.com>
Thu, 9 Jun 2022 12:13:05 +0000 (20:13 +0800)
committerakpm <akpm@linux-foundation.org>
Fri, 17 Jun 2022 02:48:31 +0000 (19:48 -0700)
commit269b904611f685f5844bd28424159de4e6331f12
tree4b76609ac9ac17d254cf50fb74d9b2d9b8dd8b14
parentdd11260ce39714734bcae504de60529e987155ad
mm/memremap: fix memunmap_pages() race with get_dev_pagemap()

Think about the below scene:

 CPU1 CPU2
 memunmap_pages
   percpu_ref_exit
     __percpu_ref_exit
       free_percpu(percpu_count);
         /* percpu_count is freed here! */
 get_dev_pagemap
   xa_load(&pgmap_array, PHYS_PFN(phys))
     /* pgmap still in the pgmap_array */
   percpu_ref_tryget_live(&pgmap->ref)
     if __ref_is_percpu
       /* __PERCPU_REF_ATOMIC_DEAD not set yet */
       this_cpu_inc(*percpu_count)
         /* access freed percpu_count here! */
      ref->percpu_count_ptr = __PERCPU_REF_ATOMIC_DEAD;
        /* too late... */
   pageunmap_range

To fix the issue, do percpu_ref_exit() after pgmap_array is emptied. So
we won't do percpu_ref_tryget_live() against a being freed percpu_ref.

Link: https://lkml.kernel.org/r/20220609121305.2508-1-linmiaohe@huawei.com
Fixes: ae01e73b9802 ("mm/memremap_pages: support multiple ranges per invocation")
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/memremap.c