Remove extra ZONE_DEVICE page refcount
Subsystems (20)
cover Remove extra ZONE_DEVICE page refcount
This patch includes Ralph Campbell’s ZONE_DEVICE refcount cleanup and additionally the changes necessary to avoid breaking the separately submitted MEMORY_DEVICE_COHERENT page migration code. Ralph’s original description: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. Following a suggestion by Christoph, we attempted to combine this cleanup with the device patch migration patch series, however this caused xftests 413 to fail with a warning, the root cause of which has large kernel footprint than just device memory. Fixing this issue properly will require cooperation between multiple development groups working across multiple kernel subsystems, as is apparent from the discussion under the earlier, combined patch submission.
We therefore propose to break this work out separately as its own patch, so it can receive the cooperative development work it needs. The deep problem arises from the get_user_pages API, which has proved troublesome for many years. It is possible that a concerted effort to repair this particular refcount issue properly will be a step in the direction of rationalizing this popular and problematic API.
In the larger picture, this API rationalization work probably deserves an agenda item at the upcoming Filesystem, MM & BPF Summit:
https://events.linuxfoundation.org/lsfmm/
The wide ranging discussion following previous iterations of the migration patchset focused almost exclusively on the refcount cleanup patch. The thread is here:
https://lore.kernel.org/linux-mm/[email protected]/
and links a number of previous threads. It is apparent that there is a lot of work in progress by a number of developer groups in parallel, and that there are issues with the order in which this work should be attempted and merged.
Jason provided his list of “balls in the air”:
- Joao's compound page support for device_dax and more
- Alex's DEVICE_COHERENT
- The refcount normalization
- Removing the pgmap test from GUP
- Removing the need for the PUD/PMD/PTE special bit
- Removing the need for the PUD/PMD/PTE devmap bit
- Remove PUD/PMD vma_is_special
- folios for fsdax
- shootdown for fsdax
It is not clear that the refcount cleanup in this patch should be the first item on the list to be merged, however it has proved to be a good starting point for a cooperative effort to address the underlying issues.
Ralph, if you would prefer to take back “ownership” of this patch, it’s yours, otherwise we will be happy to keep it in play and get it merged when some other pieces of the puzzle fall into place.
Ralph Campbell (2):
ext4/xfs: add page refcount helper
mm: remove extra ZONE_DEVICE struct page refcount
arch/powerpc/kvm/book3s_hv_uvmem.c | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 3 +- drivers/gpu/drm/nouveau/nouveau_dmem.c | 2 +- fs/dax.c | 8 +-- fs/ext4/inode.c | 5 +- fs/fuse/dax.c | 4 +- fs/xfs/xfs_file.c | 4 +- include/linux/dax.h | 10 ++++ include/linux/memremap.h | 7 ++- include/linux/mm.h | 11 ---- lib/test_hmm.c | 2 +- mm/internal.h | 7 +++ mm/memcontrol.c | 6 +- mm/memremap.c | 72 +++++++----------------- mm/migrate.c | 5 -- mm/page_alloc.c | 3 + mm/swap.c | 45 ++------------- 17 files changed, 62 insertions(+), 134 deletions(-)
Signature · 1 line
2.32.0
PATCH 1/2 ext4/xfs: add page refcount helper
From: Ralph Campbell <[email protected]>
There are several places where ZONE_DEVICE struct pages assume a reference count == 1 means the page is idle and free. Instead of open coding this, add a helper function to hide this detail.
---
v3:
[AS]: rename dax_layout_is_idle_page func to dax_page_unused
v4:
[AS]: This ref count functionality was missing on fuse/dax.c.
--- fs/dax.c | 4 ++-- fs/ext4/inode.c | 5 +---- fs/fuse/dax.c | 4 +--- fs/xfs/xfs_file.c | 4 +--- include/linux/dax.h | 10 ++++++++++ 5 files changed, 15 insertions(+), 12 deletions(-)
fs/dax.c +2 -2
@@ -369,7 +369,7 @@ static void dax_disassociate_entry(void *entry, struct address_space *mapping,for_each_mapped_pfn (entry, pfn) {struct page *page =pfn_to_page (pfn); -WARN_ON_ONCE (trunc &&page_ref_count (page) >1 ); +WARN_ON_ONCE (trunc && !dax_page_unused (page));WARN_ON_ONCE (page->mapping && page->mapping != mapping); page->mapping =NULL ; page->index =0 ;
@@ -383,7 +383,7 @@ static struct page *dax_busy_page(void *entry)for_each_mapped_pfn (entry, pfn) {struct page *page =pfn_to_page (pfn); -if (page_ref_count (page) >1 ) +if (!dax_page_unused (page))return page; }return NULL ;
fs/ext4/inode.c +1 -4
@@ -3913,10 +3913,7 @@ int ext4_break_layouts(struct inode *inode)if (!page)return 0 ; - error =___wait_var_event (&page->_refcount, -atomic_read (&page->_refcount) ==1 , -TASK_INTERRUPTIBLE ,0 ,0 , -ext4_wait_dax_page (inode)); + error =dax_wait_page (inode, page, ext4_wait_dax_page); }while (error ==0 );return error;
fs/fuse/dax.c +1 -3
@@ -676,9 +676,7 @@ static int __fuse_dax_break_layouts(struct inode *inode, bool *retry,return 0 ; *retry = true; -return ___wait_var_event (&page->_refcount, -atomic_read (&page->_refcount) ==1 ,TASK_INTERRUPTIBLE , -0 ,0 ,fuse_wait_dax_page (inode)); +return dax_wait_page (inode, page, fuse_wait_dax_page); }/* dmap_end == 0 leads to unmapping of whole file */
fs/xfs/xfs_file.c +1 -3
@@ -860,9 +860,7 @@ xfs_break_dax_layouts(return 0 ; *retry = true; -return ___wait_var_event (&page->_refcount, -atomic_read (&page->_refcount) ==1 ,TASK_INTERRUPTIBLE , -0 ,0 ,xfs_wait_dax_page (inode)); +return dax_wait_page (inode, page, xfs_wait_dax_page); }int
include/linux/dax.h +10 -0
@@ -216,6 +216,16 @@ static inline bool dax_mapping(struct address_space *mapping)return mapping->host &&IS_DAX (mapping->host); } +static inline bool dax_page_unused (struct page *page) +{ +return page_ref_count (page) ==1 ; +} + +#define dax_wait_page (_inode, _page, _wait_cb) \ + ___wait_var_event(&(_page)->_refcount, \ + dax_page_unused(_page), \ + TASK_INTERRUPTIBLE, 0, 0, _wait_cb(_inode)) +#ifdef CONFIG_DEV_DAX_HMEM_DEVICES void hmem_register_device (int target_nid,struct resource *r);#else
Signature · 1 line
2.32.0
PATCH 2/2 mm: remove extra ZONE_DEVICE struct page refcount
In reply to Alex SierraFrom: Ralph Campbell <[email protected]>
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.
--- arch/powerpc/kvm/book3s_hv_uvmem.c | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 3 +- drivers/gpu/drm/nouveau/nouveau_dmem.c | 2 +- fs/dax.c | 4 +- include/linux/dax.h | 2 +- include/linux/memremap.h | 7 ++- include/linux/mm.h | 11 ---- lib/test_hmm.c | 2 +- mm/internal.h | 7 +++ mm/memcontrol.c | 6 +- mm/memremap.c | 72 +++++++----------------- mm/migrate.c | 5 -- mm/page_alloc.c | 3 + mm/swap.c | 45 ++------------- 14 files changed, 48 insertions(+), 123 deletions(-)
arch/powerpc/kvm/book3s_hv_uvmem.c +1 -1
@@ -712,7 +712,7 @@ static struct page *kvmppc_uvmem_get_page(unsigned long gpa, struct kvm *kvm) dpage =pfn_to_page (uvmem_pfn); dpage->zone_device_data = pvt; -get_page (dpage); +init_page_count (dpage);lock_page (dpage);return dpage; out_clear:
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +2 -1
@@ -224,7 +224,8 @@ svm_migrate_get_vram_page(struct svm_range *prange, unsigned long pfn) page =pfn_to_page (pfn);svm_range_bo_ref (prange->svm_bo); page->zone_device_data = prange->svm_bo; -get_page (page); +VM_BUG_ON_PAGE (page_ref_count (page), page); +init_page_count (page);lock_page (page); }
drivers/gpu/drm/nouveau/nouveau_dmem.c +1 -1
@@ -324,7 +324,7 @@ nouveau_dmem_page_alloc_locked(struct nouveau_drm *drm)return NULL ; } -get_page (page); +init_page_count (page);lock_page (page);return page; }
fs/dax.c +2 -2
@@ -572,14 +572,14 @@ static void *grab_mapping_entry(struct xa_state *xas,/** * dax_layout_busy_page_range - find first pinned page in @mapping -* @mapping: address space to scan for a page with ref count > 1 +* @mapping: address space to scan for a page with ref count > 0 * @start: Starting offset. Page containing 'start' is included. * @end: End offset. Page containing 'end' is included. If 'end' is LLONG_MAX, * pages from 'start' till the end of file are included. * * DAX requires ZONE_DEVICE mapped pages. These pages are never * 'onlined' to the page allocator so they are considered idle when -* page->count == 1. A filesystem uses this interface to determine if +* page->count == 0. A filesystem uses this interface to determine if * any page in the mapping is busy, i.e. for DMA, or other * get_user_pages() usages. *
include/linux/dax.h +1 -1
@@ -218,7 +218,7 @@ static inline bool dax_mapping(struct address_space *mapping)static inline bool dax_page_unused (struct page *page) { -return page_ref_count (page) ==1 ; +return page_ref_count (page) ==0 ; }#define dax_wait_page (_inode, _page, _wait_cb) \
include/linux/memremap.h +4 -3
@@ -74,9 +74,10 @@ enum memory_type {struct dev_pagemap_ops {/* -* Called once the page refcount reaches 1. (ZONE_DEVICE pages never -* reach 0 refcount unless there is a refcount bug. This allows the -* device driver to implement its own memory management.) +* Called once the page refcount reaches 0. The reference count +* should be reset to one with init_page_count(page) before reusing +* the page. This allows the device driver to implement its own +* memory management. */ void (*page_free)(struct page *page);
include/linux/mm.h +0 -11
@@ -1241,17 +1241,6 @@ static inline void put_page(struct page *page) { page =compound_head (page); -/* -* For devmap managed pages we need to catch refcount transition from -* 2 to 1, when refcount reach one it means the page is free and we -* need to inform the device driver through callback. See -* include/linux/memremap.h and HMM for details. -*/ -if (page_is_devmap_managed (page)) { -put_devmap_managed_page (page); -return ; - } -if (put_page_testzero (page))__put_page (page); }
lib/test_hmm.c +1 -1
@@ -595,7 +595,7 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice) } dpage->zone_device_data = rpage; -get_page (dpage); +init_page_count (dpage);lock_page (dpage);return dpage;
mm/internal.h +7 -0
@@ -670,5 +670,12 @@ void vunmap_range_noflush(unsigned long start, unsigned long end);int numa_migrate_prep (struct page *page,struct vm_area_struct *vma,unsigned long addr,int page_nid,int *flags); +#ifdef CONFIG_DEV_PAGEMAP_OPS +void free_zone_device_page (struct page *page); +#else +static inline void free_zone_device_page (struct page *page) +{ +} +#endif #endif /* __MM_INTERNAL_H */
mm/memcontrol.c +1 -5
@@ -5515,11 +5515,7 @@ static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,*/ if (is_device_private_entry (ent)) { page =pfn_swap_entry_to_page (ent); -/* -* 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 )) +if (!get_page_unless_zero (page))return NULL ;return page; }
mm/memremap.c +20 -52
124 lines not on this page · raw message
mm/migrate.c +0 -5
@@ -358,11 +358,6 @@ static int expected_page_refs(struct address_space *mapping, struct page *page) {int expected_count =1 ; -/* -* Device private pages have an extra refcount as they are -* ZONE_DEVICE pages. -*/ - expected_count +=is_device_page (page);if (mapping) expected_count +=thp_nr_pages (page) +page_has_private (page);
mm/page_alloc.c +3 -0
@@ -6580,6 +6580,9 @@ void __ref memmap_init_zone_device(struct zone *zone,__init_single_page (page, pfn, zone_idx, nid); +/* ZONE_DEVICE pages start with a zero reference count. */ +set_page_count (page,0 ); +/* * Mark page reserved as it will need to wait for onlining * phase for it to be fully associated with a zone.
mm/swap.c +5 -40
72 lines not on this page · raw message
Signature · 1 line
2.32.0