Commit 27674ef6 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Matthew Wilcox (Oracle)

mm: remove the extra ZONE_DEVICE struct page refcount

ZONE_DEVICE struct pages have an extra reference count that complicates
the code for put_page() and several places in the kernel that need to
check the reference count to see that a page is not being used (gup,
compaction, migration, etc.). Clean up the code so the reference count
doesn't need to be treated specially for ZONE_DEVICE pages.

Note that this excludes the special idle page wakeup for fsdax pages,
which still happens at refcount 1.  This is a separate issue and will
be sorted out later.  Given that only fsdax pages require the
notifiacation when the refcount hits 1 now, the PAGEMAP_OPS Kconfig
symbol can go away and be replaced with a FS_DAX check for this hook
in the put_page fastpath.

Based on an earlier patch from Ralph Campbell <rcampbell@nvidia.com>.

Link: https://lkml.kernel.org/r/20220210072828.2930359-8-hch@lst.deSigned-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarLogan Gunthorpe <logang@deltatee.com>
Reviewed-by: default avatarRalph Campbell <rcampbell@nvidia.com>
Reviewed-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Reviewed-by: default avatarDan Williams <dan.j.williams@intel.com>
Acked-by: default avatarFelix Kuehling <Felix.Kuehling@amd.com>
Tested-by: default avatar"Sierra Guiza, Alejandro (Alex)" <alex.sierra@amd.com>

Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Ben Skeggs <bskeggs@redhat.com>
Cc: Chaitanya Kulkarni <kch@nvidia.com>
Cc: Christian Knig <christian.koenig@amd.com>
Cc: Karol Herbst <kherbst@redhat.com>
Cc: Lyude Paul <lyude@redhat.com>
Cc: Miaohe Lin <linmiaohe@huawei.com>
Cc: Muchun Song <songmuchun@bytedance.com>
Cc: "Pan, Xinhui" <Xinhui.Pan@amd.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
parent dc90f084
...@@ -713,7 +713,6 @@ static struct page *kvmppc_uvmem_get_page(unsigned long gpa, struct kvm *kvm) ...@@ -713,7 +713,6 @@ static struct page *kvmppc_uvmem_get_page(unsigned long gpa, struct kvm *kvm)
dpage = pfn_to_page(uvmem_pfn); dpage = pfn_to_page(uvmem_pfn);
dpage->zone_device_data = pvt; dpage->zone_device_data = pvt;
get_page(dpage);
lock_page(dpage); lock_page(dpage);
return dpage; return dpage;
out_clear: out_clear:
......
...@@ -225,7 +225,6 @@ svm_migrate_get_vram_page(struct svm_range *prange, unsigned long pfn) ...@@ -225,7 +225,6 @@ svm_migrate_get_vram_page(struct svm_range *prange, unsigned long pfn)
page = pfn_to_page(pfn); page = pfn_to_page(pfn);
svm_range_bo_ref(prange->svm_bo); svm_range_bo_ref(prange->svm_bo);
page->zone_device_data = prange->svm_bo; page->zone_device_data = prange->svm_bo;
get_page(page);
lock_page(page); lock_page(page);
} }
......
...@@ -326,7 +326,6 @@ nouveau_dmem_page_alloc_locked(struct nouveau_drm *drm) ...@@ -326,7 +326,6 @@ nouveau_dmem_page_alloc_locked(struct nouveau_drm *drm)
return NULL; return NULL;
} }
get_page(page);
lock_page(page); lock_page(page);
return page; return page;
} }
......
...@@ -48,7 +48,6 @@ config FS_DAX ...@@ -48,7 +48,6 @@ config FS_DAX
bool "File system based Direct Access (DAX) support" bool "File system based Direct Access (DAX) support"
depends on MMU depends on MMU
depends on !(ARM || MIPS || SPARC) depends on !(ARM || MIPS || SPARC)
select DEV_PAGEMAP_OPS if (ZONE_DEVICE && !FS_DAX_LIMITED)
select FS_IOMAP select FS_IOMAP
select DAX select DAX
help help
......
...@@ -68,9 +68,9 @@ enum memory_type { ...@@ -68,9 +68,9 @@ enum memory_type {
struct dev_pagemap_ops { struct dev_pagemap_ops {
/* /*
* Called once the page refcount reaches 1. (ZONE_DEVICE pages never * Called once the page refcount reaches 0. The reference count will be
* reach 0 refcount unless there is a refcount bug. This allows the * reset to one by the core code after the method is called to prepare
* device driver to implement its own memory management.) * for handing out the page again.
*/ */
void (*page_free)(struct page *page); void (*page_free)(struct page *page);
...@@ -133,16 +133,14 @@ static inline unsigned long pgmap_vmemmap_nr(struct dev_pagemap *pgmap) ...@@ -133,16 +133,14 @@ static inline unsigned long pgmap_vmemmap_nr(struct dev_pagemap *pgmap)
static inline bool is_device_private_page(const struct page *page) static inline bool is_device_private_page(const struct page *page)
{ {
return IS_ENABLED(CONFIG_DEV_PAGEMAP_OPS) && return IS_ENABLED(CONFIG_DEVICE_PRIVATE) &&
IS_ENABLED(CONFIG_DEVICE_PRIVATE) &&
is_zone_device_page(page) && is_zone_device_page(page) &&
page->pgmap->type == MEMORY_DEVICE_PRIVATE; page->pgmap->type == MEMORY_DEVICE_PRIVATE;
} }
static inline bool is_pci_p2pdma_page(const struct page *page) static inline bool is_pci_p2pdma_page(const struct page *page)
{ {
return IS_ENABLED(CONFIG_DEV_PAGEMAP_OPS) && return IS_ENABLED(CONFIG_PCI_P2PDMA) &&
IS_ENABLED(CONFIG_PCI_P2PDMA) &&
is_zone_device_page(page) && is_zone_device_page(page) &&
page->pgmap->type == MEMORY_DEVICE_PCI_P2PDMA; page->pgmap->type == MEMORY_DEVICE_PCI_P2PDMA;
} }
......
...@@ -1090,7 +1090,7 @@ static inline bool is_zone_movable_page(const struct page *page) ...@@ -1090,7 +1090,7 @@ static inline bool is_zone_movable_page(const struct page *page)
return page_zonenum(page) == ZONE_MOVABLE; return page_zonenum(page) == ZONE_MOVABLE;
} }
#ifdef CONFIG_DEV_PAGEMAP_OPS #if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_FS_DAX)
DECLARE_STATIC_KEY_FALSE(devmap_managed_key); DECLARE_STATIC_KEY_FALSE(devmap_managed_key);
bool __put_devmap_managed_page(struct page *page); bool __put_devmap_managed_page(struct page *page);
...@@ -1103,12 +1103,12 @@ static inline bool put_devmap_managed_page(struct page *page) ...@@ -1103,12 +1103,12 @@ static inline bool put_devmap_managed_page(struct page *page)
return __put_devmap_managed_page(page); return __put_devmap_managed_page(page);
} }
#else /* CONFIG_DEV_PAGEMAP_OPS */ #else /* CONFIG_ZONE_DEVICE && CONFIG_FS_DAX */
static inline bool put_devmap_managed_page(struct page *page) static inline bool put_devmap_managed_page(struct page *page)
{ {
return false; return false;
} }
#endif /* CONFIG_DEV_PAGEMAP_OPS */ #endif /* CONFIG_ZONE_DEVICE && CONFIG_FS_DAX */
/* 127: arbitrary random number, small enough to assemble well */ /* 127: arbitrary random number, small enough to assemble well */
#define folio_ref_zero_or_close_to_overflow(folio) \ #define folio_ref_zero_or_close_to_overflow(folio) \
......
...@@ -566,7 +566,6 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice) ...@@ -566,7 +566,6 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice)
} }
dpage->zone_device_data = rpage; dpage->zone_device_data = rpage;
get_page(dpage);
lock_page(dpage); lock_page(dpage);
return dpage; return dpage;
......
...@@ -776,9 +776,6 @@ config ZONE_DEVICE ...@@ -776,9 +776,6 @@ config ZONE_DEVICE
If FS_DAX is enabled, then say Y. If FS_DAX is enabled, then say Y.
config DEV_PAGEMAP_OPS
bool
# #
# Helpers to mirror range of the CPU page tables of a process into device page # Helpers to mirror range of the CPU page tables of a process into device page
# tables. # tables.
...@@ -790,7 +787,6 @@ config HMM_MIRROR ...@@ -790,7 +787,6 @@ config HMM_MIRROR
config DEVICE_PRIVATE config DEVICE_PRIVATE
bool "Unaddressable device memory (GPU memory, ...)" bool "Unaddressable device memory (GPU memory, ...)"
depends on ZONE_DEVICE depends on ZONE_DEVICE
select DEV_PAGEMAP_OPS
help help
Allows creation of struct pages to represent unaddressable device Allows creation of struct pages to represent unaddressable device
......
...@@ -735,4 +735,6 @@ void vunmap_range_noflush(unsigned long start, unsigned long end); ...@@ -735,4 +735,6 @@ void vunmap_range_noflush(unsigned long start, unsigned long end);
int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
unsigned long addr, int page_nid, int *flags); unsigned long addr, int page_nid, int *flags);
void free_zone_device_page(struct page *page);
#endif /* __MM_INTERNAL_H */ #endif /* __MM_INTERNAL_H */
...@@ -5503,17 +5503,12 @@ static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, ...@@ -5503,17 +5503,12 @@ static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
return NULL; return NULL;
/* /*
* Handle MEMORY_DEVICE_PRIVATE which are ZONE_DEVICE page belonging to * Handle device private pages that are not accessible by the CPU, but
* a device and because they are not accessible by CPU they are store * stored as special swap entries in the page table.
* as special swap entry in the CPU page table.
*/ */
if (is_device_private_entry(ent)) { if (is_device_private_entry(ent)) {
page = pfn_swap_entry_to_page(ent); page = pfn_swap_entry_to_page(ent);
/* if (!get_page_unless_zero(page))
* MEMORY_DEVICE_PRIVATE means ZONE_DEVICE page and which have
* a refcount of 1 when free (unlike normal page)
*/
if (!page_ref_add_unless(page, 1, 1))
return NULL; return NULL;
return page; return page;
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <linux/types.h> #include <linux/types.h>
#include <linux/wait_bit.h> #include <linux/wait_bit.h>
#include <linux/xarray.h> #include <linux/xarray.h>
#include "internal.h"
static DEFINE_XARRAY(pgmap_array); static DEFINE_XARRAY(pgmap_array);
...@@ -37,21 +38,19 @@ unsigned long memremap_compat_align(void) ...@@ -37,21 +38,19 @@ unsigned long memremap_compat_align(void)
EXPORT_SYMBOL_GPL(memremap_compat_align); EXPORT_SYMBOL_GPL(memremap_compat_align);
#endif #endif
#ifdef CONFIG_DEV_PAGEMAP_OPS #ifdef CONFIG_FS_DAX
DEFINE_STATIC_KEY_FALSE(devmap_managed_key); DEFINE_STATIC_KEY_FALSE(devmap_managed_key);
EXPORT_SYMBOL(devmap_managed_key); EXPORT_SYMBOL(devmap_managed_key);
static void devmap_managed_enable_put(struct dev_pagemap *pgmap) static void devmap_managed_enable_put(struct dev_pagemap *pgmap)
{ {
if (pgmap->type == MEMORY_DEVICE_PRIVATE || if (pgmap->type == MEMORY_DEVICE_FS_DAX)
pgmap->type == MEMORY_DEVICE_FS_DAX)
static_branch_dec(&devmap_managed_key); static_branch_dec(&devmap_managed_key);
} }
static void devmap_managed_enable_get(struct dev_pagemap *pgmap) static void devmap_managed_enable_get(struct dev_pagemap *pgmap)
{ {
if (pgmap->type == MEMORY_DEVICE_PRIVATE || if (pgmap->type == MEMORY_DEVICE_FS_DAX)
pgmap->type == MEMORY_DEVICE_FS_DAX)
static_branch_inc(&devmap_managed_key); static_branch_inc(&devmap_managed_key);
} }
#else #else
...@@ -61,7 +60,7 @@ static void devmap_managed_enable_get(struct dev_pagemap *pgmap) ...@@ -61,7 +60,7 @@ static void devmap_managed_enable_get(struct dev_pagemap *pgmap)
static void devmap_managed_enable_put(struct dev_pagemap *pgmap) static void devmap_managed_enable_put(struct dev_pagemap *pgmap)
{ {
} }
#endif /* CONFIG_DEV_PAGEMAP_OPS */ #endif /* CONFIG_FS_DAX */
static void pgmap_array_delete(struct range *range) static void pgmap_array_delete(struct range *range)
{ {
...@@ -102,23 +101,12 @@ static unsigned long pfn_end(struct dev_pagemap *pgmap, int range_id) ...@@ -102,23 +101,12 @@ static unsigned long pfn_end(struct dev_pagemap *pgmap, int range_id)
return (range->start + range_len(range)) >> PAGE_SHIFT; return (range->start + range_len(range)) >> PAGE_SHIFT;
} }
static unsigned long pfn_next(struct dev_pagemap *pgmap, unsigned long pfn)
{
if (pfn % (1024 << pgmap->vmemmap_shift))
cond_resched();
return pfn + pgmap_vmemmap_nr(pgmap);
}
static unsigned long pfn_len(struct dev_pagemap *pgmap, unsigned long range_id) static unsigned long pfn_len(struct dev_pagemap *pgmap, unsigned long range_id)
{ {
return (pfn_end(pgmap, range_id) - return (pfn_end(pgmap, range_id) -
pfn_first(pgmap, range_id)) >> pgmap->vmemmap_shift; pfn_first(pgmap, range_id)) >> pgmap->vmemmap_shift;
} }
#define for_each_device_pfn(pfn, map, i) \
for (pfn = pfn_first(map, i); pfn < pfn_end(map, i); \
pfn = pfn_next(map, pfn))
static void pageunmap_range(struct dev_pagemap *pgmap, int range_id) static void pageunmap_range(struct dev_pagemap *pgmap, int range_id)
{ {
struct range *range = &pgmap->ranges[range_id]; struct range *range = &pgmap->ranges[range_id];
...@@ -147,13 +135,11 @@ static void pageunmap_range(struct dev_pagemap *pgmap, int range_id) ...@@ -147,13 +135,11 @@ static void pageunmap_range(struct dev_pagemap *pgmap, int range_id)
void memunmap_pages(struct dev_pagemap *pgmap) void memunmap_pages(struct dev_pagemap *pgmap)
{ {
unsigned long pfn;
int i; int i;
percpu_ref_kill(&pgmap->ref); percpu_ref_kill(&pgmap->ref);
for (i = 0; i < pgmap->nr_range; i++) for (i = 0; i < pgmap->nr_range; i++)
for_each_device_pfn(pfn, pgmap, i) percpu_ref_put_many(&pgmap->ref, pfn_len(pgmap, i));
put_page(pfn_to_page(pfn));
wait_for_completion(&pgmap->done); wait_for_completion(&pgmap->done);
percpu_ref_exit(&pgmap->ref); percpu_ref_exit(&pgmap->ref);
...@@ -464,14 +450,10 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn, ...@@ -464,14 +450,10 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
} }
EXPORT_SYMBOL_GPL(get_dev_pagemap); EXPORT_SYMBOL_GPL(get_dev_pagemap);
#ifdef CONFIG_DEV_PAGEMAP_OPS void free_zone_device_page(struct page *page)
void free_devmap_managed_page(struct page *page)
{ {
/* notify page idle for dax */ if (WARN_ON_ONCE(!is_device_private_page(page)))
if (!is_device_private_page(page)) {
wake_up_var(&page->_refcount);
return; return;
}
__ClearPageWaiters(page); __ClearPageWaiters(page);
...@@ -500,28 +482,27 @@ void free_devmap_managed_page(struct page *page) ...@@ -500,28 +482,27 @@ void free_devmap_managed_page(struct page *page)
*/ */
page->mapping = NULL; page->mapping = NULL;
page->pgmap->ops->page_free(page); page->pgmap->ops->page_free(page);
/*
* Reset the page count to 1 to prepare for handing out the page again.
*/
set_page_count(page, 1);
} }
#ifdef CONFIG_FS_DAX
bool __put_devmap_managed_page(struct page *page) bool __put_devmap_managed_page(struct page *page)
{ {
if (page->pgmap->type != MEMORY_DEVICE_PRIVATE && if (page->pgmap->type != MEMORY_DEVICE_FS_DAX)
page->pgmap->type != MEMORY_DEVICE_FS_DAX)
return false; return false;
/* /*
* devmap page refcounts are 1-based, rather than 0-based: if * fsdax page refcounts are 1-based, rather than 0-based: if
* refcount is 1, then the page is free and the refcount is * refcount is 1, then the page is free and the refcount is
* stable because nobody holds a reference on the page. * stable because nobody holds a reference on the page.
*/ */
switch (page_ref_dec_return(page)) { if (page_ref_dec_return(page) == 1)
case 1: wake_up_var(&page->_refcount);
free_devmap_managed_page(page);
break;
case 0:
__put_page(page);
break;
}
return true; return true;
} }
EXPORT_SYMBOL(__put_devmap_managed_page); EXPORT_SYMBOL(__put_devmap_managed_page);
#endif /* CONFIG_DEV_PAGEMAP_OPS */ #endif /* CONFIG_FS_DAX */
...@@ -338,14 +338,8 @@ static int expected_page_refs(struct address_space *mapping, struct page *page) ...@@ -338,14 +338,8 @@ static int expected_page_refs(struct address_space *mapping, struct page *page)
{ {
int expected_count = 1; int expected_count = 1;
/*
* Device private pages have an extra refcount as they are
* ZONE_DEVICE pages.
*/
expected_count += is_device_private_page(page);
if (mapping) if (mapping)
expected_count += compound_nr(page) + page_has_private(page); expected_count += compound_nr(page) + page_has_private(page);
return expected_count; return expected_count;
} }
......
...@@ -122,17 +122,9 @@ static void __put_compound_page(struct page *page) ...@@ -122,17 +122,9 @@ static void __put_compound_page(struct page *page)
void __put_page(struct page *page) void __put_page(struct page *page)
{ {
if (is_zone_device_page(page)) { if (unlikely(is_zone_device_page(page)))
put_dev_pagemap(page->pgmap); free_zone_device_page(page);
else if (unlikely(PageCompound(page)))
/*
* The page belongs to the device that created pgmap. Do
* not return it to page allocator.
*/
return;
}
if (unlikely(PageCompound(page)))
__put_compound_page(page); __put_compound_page(page);
else else
__put_single_page(page); __put_single_page(page);
...@@ -933,7 +925,7 @@ void release_pages(struct page **pages, int nr) ...@@ -933,7 +925,7 @@ void release_pages(struct page **pages, int nr)
if (put_devmap_managed_page(page)) if (put_devmap_managed_page(page))
continue; continue;
if (put_page_testzero(page)) if (put_page_testzero(page))
put_dev_pagemap(page->pgmap); free_zone_device_page(page);
continue; continue;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment