mm: remove page_maybe_dma_pinned() and page_mkclean()
Subsystems (10)
cover mm: remove page_maybe_dma_pinned() and page_mkclean()
Most page_maybe_dma_pinned() and page_mkclean() callers have been converted to the folio equivalents, after two more convertsions, remove them and update the comment and documention.
Kefeng Wang (4):
fs/proc/task_mmu: use folio API in pte_is_pinned()
mm: remove page_maybe_dma_pinned()
fb_defio: use a folio in fb_deferred_io_work()
mm: remove page_mkclean()
Documentation/core-api/pin_user_pages.rst | 16 ++++++++-------- drivers/video/fbdev/core/fb_defio.c | 13 +++++++------ fs/proc/task_mmu.c | 8 ++++---- include/linux/mm.h | 11 +++-------- include/linux/rmap.h | 4 ---- mm/gup.c | 2 +- mm/mremap.c | 2 +- 7 files changed, 24 insertions(+), 32 deletions(-)
Signature · 1 line
2.27.0
PATCH 1/4 fs/proc/task_mmu: use folio API in pte_is_pinned()
Convert to use vm_normal_folio() and folio_maybe_dma_pinned() API, which helps to remove page_maybe_dma_pinned() in the subsequent change.
--- fs/proc/task_mmu.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/proc/task_mmu.c +4 -4
@@ -1088,7 +1088,7 @@ struct clear_refs_private {static inline bool pte_is_pinned (struct vm_area_struct *vma,unsigned long addr,pte_t pte) { -struct page *page; +struct folio *folio;if (!pte_write (pte))return false;
@@ -1096,10 +1096,10 @@ static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr,return false;if (likely (!test_bit (MMF_HAS_PINNED , &vma->vm_mm->flags)))return false; - page =vm_normal_page (vma, addr, pte); -if (!page) + folio =vm_normal_folio (vma, addr, pte); +if (!folio)return false; -return page_maybe_dma_pinned (page); +return folio_maybe_dma_pinned (folio); } static inline void clear_soft_dirty(struct vm_area_struct *vma,
Signature · 1 line
2.27.0
Re: [PATCH 1/4] fs/proc/task_mmu: use folio API in pte_is_pinned()
Quoted 25 lines · Kefeng Wang
Convert to use
vm_normal_folio()andfolio_maybe_dma_pinned()API, which helps to removepage_maybe_dma_pinned()in the subsequent change.Signed-off-by Kefeng Wang <[email protected]>--- fs/proc/task_mmu.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)fs/proc/task_mmu.c +1
-1@@ -1088,7 +1088,7 @@ struct clear_refs_private {static inline bool pte_is_pinned (struct vm_area_struct *vma,unsigned long addr,pte_t pte) { -struct page *page; +struct folio *folio;if (!pte_write (pte))return false;@@ -1096,10 +1096,10 @@ static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr,return false;if (likely (!test_bit (MMF_HAS_PINNED , &vma->vm_mm->flags)))return false;
fs/proc/task_mmu.c static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, · line 1105
Quoted 10 lines
fs/proc/task_mmu.c +3
-3- page =vm_normal_page (vma, addr, pte); -if (!page) + folio =vm_normal_folio (vma, addr, pte); +if (!folio)return false; -return page_maybe_dma_pinned (page); +return folio_maybe_dma_pinned (folio); } static inline void clear_soft_dirty(struct vm_area_struct *vma,
Likely we should just get rid of the pte_is_pinned() check completely now. We don't perform the same for PMDs, we don't sync against GUP-fast, and the original COW vs. GUP issue was resolved.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH 1/4] fs/proc/task_mmu: use folio API in pte_is_pinned()
Quoted 28 lines · David Hildenbrand
Quoted 27 lines · Kefeng Wang
Convert to use
vm_normal_folio()andfolio_maybe_dma_pinned()API, which helps to removepage_maybe_dma_pinned()in the subsequent change.Signed-off-by Kefeng Wang <[email protected]>---
fs/proc/task_mmu.c| 8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)fs/proc/task_mmu.c +2
-3@@ -1088,7 +1088,7 @@ struct clear_refs_private { static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte) { - struct page *page; + struct folio *folio; if (!pte_write(pte)) return false;@@ -1096,10 +1096,10 @@ static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, return false; if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))) return false; - page = vm_normal_page(vma, addr, pte); - if (!page) + folio = vm_normal_folio(vma, addr, pte);
Quoted 8 lines
fs/proc/task_mmu.c static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, · line 1105
Quoted 6 lines
fs/proc/task_mmu.c +2
-1+ if (!folio) return false; - return page_maybe_dma_pinned(page); + return folio_maybe_dma_pinned(folio); } static inline void clear_soft_dirty(struct vm_area_struct *vma,Likely we should just get rid of the
pte_is_pinned()check completely now. We don't perform the same for PMDs, we don't sync against GUP-fast,
Yes, no handle for clear PMDs.
Quoted 1 line
and the original COW vs. GUP issue was resolved.
Agree, but I'm not sure about removing it, from the commit 9348b73c2e1bf,
"The whole "track page soft dirty" state doesn't work with pinned pages anyway, since the page might be dirtied by the pinning entity without ever being noticed in the page tables."
The issue is between the pin mechanism and "track page soft dirty", if
the page is pinned, the pining entiry(DMA?) could change the page but
the pte dirty won't be set, so maybe we still need it, even add some similar thing for PMD? Correct me if I'm wrong, thanks.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 1/4] fs/proc/task_mmu: use folio API in pte_is_pinned()
Quoted 58 lines · David Hildenbrand
Quoted 48 lines · Kefeng Wang
Quoted 30 lines · David Hildenbrand
Quoted 29 lines · Kefeng Wang
Convert to use
vm_normal_folio()andfolio_maybe_dma_pinned()API, which helps to removepage_maybe_dma_pinned()in the subsequent change.Signed-off-by Kefeng Wang <[email protected]>---
fs/proc/task_mmu.c| 8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)fs/proc/task_mmu.c +3
-3@@ -1088,7 +1088,7 @@ struct clear_refs_private { static inline bool pte_is_pinned(struct vm_area_struct *vma,unsigned long addr,pte_t pte) { - struct page *page; + struct folio *folio; if (!pte_write(pte)) return false;@@ -1096,10 +1096,10 @@ static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, return false; if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))) return false; - page = vm_normal_page(vma, addr, pte); - if (!page) + folio = vm_normal_folio(vma, addr, pte); + if (!folio)Quoted 8 lines
fs/proc/task_mmu.c static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, · line 1105
Quoted 5 lines
fs/proc/task_mmu.c +1
-1return false; - return page_maybe_dma_pinned(page); + return folio_maybe_dma_pinned(folio); } static inline void clear_soft_dirty(struct vm_area_struct *vma,Likely we should just get rid of the
pte_is_pinned()check completely now. We don't perform the same for PMDs, we don't sync against GUP-fast,Yes, no handle for clear PMDs.
Quoted 1 line
and the original COW vs. GUP issue was resolved.
Agree, but I'm not sure about removing it, from the commit
9348b73c2e1bf,Quoted 8 lines
"The whole "track page soft dirty" state doesn't work with pinned pages anyway, since the page might be dirtied by the pinning entity without ever being noticed in the page tables."
The issue is between the pin mechanism and "track page soft dirty", if the page is pinned, the pining entiry(DMA?) could change the page but the pte dirty won't be set, so maybe we still need it, even add some similar thing for PMD? Correct me if I'm wrong, thanks.
Quoted 9 lines
Yes, but it doesn't work with any mechanism that write-protects PTEs, including
mprotect()and uffd-wp.Then, we never synced agaisnt concurrent GUP-fast, concurrent
O_DIRECTthat might still use !FOLL_PIN, never handled PMD ... so it's all not consistent nor really helpful nowdays.... and if you have read-only pinned pages (which we cannot distinguish)
OK, many cases need to be addressed.
Quoted 52 lines
I have a proper patch lying around here for quite a while:
commit
9ef578b7aba8bba626b904fe90e5be0690842fd3
Author: David Hildenbrand <[email protected]>
Date: Wed Feb 16 20:39:43 2022 +0100
fs/proc/task_mmu: allow setting pinned pages R/O for softdirty
tracking
Before we hadPG_anon_exclusive, our new COW logic would end up replacing a pinned page in the page tables, detecting it as possibly shared. Consequently, we'd lost synchronicity between the page mapped into the page table and the page pin.
We tried preventing mapping pinned pages R/O, however, history told us that that is impossible -- and we addedPG_anon_exclusiveto have
it all
working reliably again.
Now that we havePG_anon_exclusivein place, let's get rid of the
check for
pinned pages and revert it to the old state.
Yes, we won't be able to detect the following scenario correctly:
(1) R/W-pin a page.
(2) Clear softdirty.
(3) Modify the page via the R/W-pin.
However, that isn't working reliably right now either way, because
* The current check is racy, because we can race with GUP-fast
taking a
R/W pin while we're clearing the softdirty marker and mapping the
page
R/O.
* The current check cannot handleFOLL_GETR/W references, as used forO_DIRECT. So if there is concurrent I/O the PTE will get marked as !softdirty, yet, direct I/O will modify page content afterwards.
* We check for pins only when handling PTEs, but not for PMDs etc. Also, this handling is in no way different to other mechanisms
(mprotect,
uffd-wp) that map pages R/O to catch successive write access via the page table -- because acceses via the page pin no longer go logically via the page table, the page table is bypassed.
With this change, the interface now works again as expected when we
have
R/O pins, and behaves just like any other mechanism that uses write protection to catch successive writes (mprotect, uffd-wp) -- and
they all
face the same issue regarding R/W access via GUP (FOLL_PINandFOLL_GET).
User space better be aware that using read-protection to catch
writes to
a page can miss writes via GUP. Softdirt tracking cannot reliably
catch
modifications via GUP after clearing softdirty and returning to user space.
Quoted 3 lines
But I understand if you want to be careful :) So I might send that patch out at some point myself ...
Thank for your detail explanation, let's wait it out.
Andrew Morton
maintainerRe: [PATCH 1/4] fs/proc/task_mmu: use folio API in pte_is_pinned()
Quoted 11 lines · On Wed, 5 Jun 2024 09:30:59 +0800 Kefeng Wang <[email protected]>…
Quoted 11 lines
protection to catch successive writes (mprotect, uffd-wp) -- and
they all
face the same issue regarding R/W access via GUP (FOLL_PINandFOLL_GET).
User space better be aware that using read-protection to catch
writes to
a page can miss writes via GUP. Softdirt tracking cannot reliably
catch
modifications via GUP after clearing softdirty and returning to user space.
Quoted 6 lines
Quoted 4 lines
But I understand if you want to be careful :) So I might send that patch out at
some point myself ...Thank for your detail explanation, let's wait it out.
Did we wait long enough? Should we move ahead with this change or should we drop it and have another run at it?
Re: [PATCH 1/4] fs/proc/task_mmu: use folio API in pte_is_pinned()
Quoted 18 lines · Andrew Morton
Quoted 16 lines · On Wed, 5 Jun 2024 09:30:59 +0800 Kefeng Wang <[email protected]>…
Quoted 15 lines
protection to catch successive writes (mprotect, uffd-wp) -- and
they all
face the same issue regarding R/W access via GUP (FOLL_PINandFOLL_GET).
User space better be aware that using read-protection to catch
writes to
a page can miss writes via GUP. Softdirt tracking cannot reliably
catch
modifications via GUP after clearing softdirty and returning to user space.But I understand if you want to be careful :) So I might send that patch out at
some point myself ...
Quoted 4 lines
Quoted 1 line
Thank for your detail explanation, let's wait it out.
Did we wait long enough? Should we move ahead with this change or should we drop it and have another run at it?
The change is fine, we will revisit removing this code (instead of cleaning it up) separately.
Signature · 3 lines
Cheers, David / dhildenb
PATCH 2/4 mm: remove page_maybe_dma_pinned()
In reply to Kefeng WangAfter the last user of page_maybe_dma_pinned() is converted to folio_maybe_dma_pinned(), remove page_maybe_dma_pinned() and update the document and comment.
--- Documentation/core-api/pin_user_pages.rst | 10 +++++----- include/linux/mm.h | 9 ++------- 2 files changed, 7 insertions(+), 12 deletions(-)
Documentation/core-api/pin_user_pages.rst +5 -5
@@ -196,20 +196,20 @@ INCORRECT (uses FOLL_GET calls): write to the data within the pages put_page() -page_maybe_dma_pinned(): the whole point of pinning +folio_maybe_dma_pinned(): the whole point of pinning =================================================== -The whole point of marking pages as "DMA-pinned" or "gup-pinned" is to be able -to query, "is this page DMA-pinned?" That allows code such as page_mkclean() +The whole point of marking folios as "DMA-pinned" or "gup-pinned" is to be able +to query, "is this folio DMA-pinned?" That allows code such as page_mkclean() (and file system writeback code in general) to make informed decisions about -what to do when a page cannot be unmapped due to such pins. +what to do when a folio cannot be unmapped due to such pins. What to do in those cases is the subject of a years-long series of discussions and debates (see the References at the end of this document). It's a TODO item here: fill in the details once that's worked out. Meanwhile, it's safe to say that having this available: :: - static inline bool page_maybe_dma_pinned(struct page *page) + static inline bool folio_maybe_dma_pinned(struct folio *folio) ...is a prerequisite to solving the long-running gup+DMA problem.
include/linux/mm.h +2 -7
@@ -1953,8 +1953,8 @@ static inline struct folio *pfn_folio(unsigned long pfn)* * For more information, please see Documentation/core-api/pin_user_pages.rst. * -* Return: True, if it is likely that the page has been "dma-pinned". -* False, if the page is definitely not dma-pinned. +* Return: True, if it is likely that the folio has been "dma-pinned". +* False, if the folio is definitely not dma-pinned. */ static inline bool folio_maybe_dma_pinned (struct folio *folio) {
@@ -1973,11 +1973,6 @@ static inline bool folio_maybe_dma_pinned(struct folio *folio)GUP_PIN_COUNTING_BIAS ; } -static inline bool page_maybe_dma_pinned (struct page *page) -{ -return folio_maybe_dma_pinned (page_folio (page)); -} -/* * This should most likely only be called during fork() to see whether we * should break the cow immediately for an anon page on the src mm.
Signature · 1 line
2.27.0
PATCH 3/4 fb_defio: use a folio in fb_deferred_io_work()
In reply to Kefeng WangReplaces three calls to compound_head() with one, which removes last caller of page_mkclean().
--- drivers/video/fbdev/core/fb_defio.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/video/fbdev/core/fb_defio.c +5 -4
@@ -244,10 +244,11 @@ static void fb_deferred_io_work(struct work_struct *work)/* here we mkclean the pages, then do all deferred IO */ mutex_lock (&fbdefio->lock);list_for_each_entry (pageref, &fbdefio->pagereflist, list) { -struct page *cur = pageref->page; -lock_page (cur); -page_mkclean (cur); -unlock_page (cur); +struct folio *folio =page_folio (pageref->page); + +folio_lock (folio); +folio_mkclean (folio); +folio_unlock (folio); }/* driver's callback with pagereflist */
Signature · 1 line
2.27.0
Re: [PATCH 3/4] fb_defio: use a folio in fb_deferred_io_work()
Quoted 18 lines · Kefeng Wang
Replaces three calls to
compound_head()with one, which removes last caller ofpage_mkclean().Signed-off-by Kefeng Wang <[email protected]>--- drivers/video/fbdev/core/fb_defio.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/video/fbdev/core/fb_defio.c +0
-2@@ -244,10 +244,11 @@ static void fb_deferred_io_work(struct work_struct *work)/* here we mkclean the pages, then do all deferred IO */ mutex_lock (&fbdefio->lock);list_for_each_entry (pageref, &fbdefio->pagereflist, list) { -struct page *cur = pageref->page; -lock_page (cur);
drivers/video/fbdev/core/fb_defio.c static void fb_deferred_io_work(struct work_struct *work) · line 254
Quoted 10 lines
drivers/video/fbdev/core/fb_defio.c +5
-2-page_mkclean (cur); -unlock_page (cur); +struct folio *folio =page_folio (pageref->page); + +folio_lock (folio); +folio_mkclean (folio); +folio_unlock (folio); }/* driver's callback with pagereflist */
Signature · 3 lines
Cheers, David / dhildenb
PATCH 4/4 mm: remove page_mkclean()
In reply to Kefeng WangThere are no more users of page_mkclean(), remove it and update the document and comment.
--- Documentation/core-api/pin_user_pages.rst | 8 ++++---- drivers/video/fbdev/core/fb_defio.c | 4 ++-- include/linux/mm.h | 2 +- include/linux/rmap.h | 4 ---- mm/gup.c | 2 +- mm/mremap.c | 2 +- 6 files changed, 9 insertions(+), 13 deletions(-)
Documentation/core-api/pin_user_pages.rst +4 -4
@@ -132,7 +132,7 @@ CASE 1: Direct IO (DIO) ----------------------- There are GUP references to pages that are serving as DIO buffers. These buffers are needed for a relatively short time (so they -are not "long term"). No special synchronization with page_mkclean() or +are not "long term"). No special synchronization with folio_mkclean() or munmap() is provided. Therefore, flags to set at the call site are: :: FOLL_PIN
@@ -144,7 +144,7 @@ CASE 2: RDMA ------------ There are GUP references to pages that are serving as DMA buffers. These buffers are needed for a long time ("long term"). No special -synchronization with page_mkclean() or munmap() is provided. Therefore, flags +synchronization with folio_mkclean() or munmap() is provided. Therefore, flags to set at the call site are: :: FOLL_PIN | FOLL_LONGTERM
@@ -170,7 +170,7 @@ callback, simply remove the range from the device's page tables. Either way, as long as the driver unpins the pages upon mmu notifier callback, then there is proper synchronization with both filesystem and mm -(page_mkclean(), munmap(), etc). Therefore, neither flag needs to be set. +(folio_mkclean(), munmap(), etc). Therefore, neither flag needs to be set. CASE 4: Pinning for struct page manipulation only -------------------------------------------------
@@ -200,7 +200,7 @@ folio_maybe_dma_pinned(): the whole point of pinning =================================================== The whole point of marking folios as "DMA-pinned" or "gup-pinned" is to be able -to query, "is this folio DMA-pinned?" That allows code such as page_mkclean() +to query, "is this folio DMA-pinned?" That allows code such as folio_mkclean() (and file system writeback code in general) to make informed decisions about what to do when a folio cannot be unmapped due to such pins.
drivers/video/fbdev/core/fb_defio.c +2 -2
@@ -113,7 +113,7 @@ static vm_fault_t fb_deferred_io_fault(struct vm_fault *vmf)printk (KERN_ERR "no mapping available\n" );BUG_ON (!page->mapping); - page->index = vmf->pgoff;/* for page_mkclean() */ + page->index = vmf->pgoff;/* for folio_mkclean() */ vmf->page = page;return 0 ;
@@ -161,7 +161,7 @@ static vm_fault_t fb_deferred_io_track_page(struct fb_info *info, unsigned long/* * We want the page to remain locked from ->page_mkwrite until -* the PTE is marked dirty to avoid page_mkclean() being called +* the PTE is marked dirty to avoid folio_mkclean() being called * before the PTE is updated, which would leave the page ignored * by defio. * Do this by locking the page here and informing the caller
include/linux/mm.h +1 -1
@@ -1610,7 +1610,7 @@ static inline void put_page(struct page *page)* issue. * * Locking: the lockless algorithm described in folio_try_get_rcu() -* provides safe operation for get_user_pages(), page_mkclean() and +* provides safe operation for get_user_pages(), folio_mkclean() and * other calls that race to set up page table entries. */ #define GUP_PIN_COUNTING_BIAS (1U << 10)
include/linux/rmap.h +0 -4
@@ -793,8 +793,4 @@ static inline int folio_mkclean(struct folio *folio) }#endif /* CONFIG_MMU */ -static inline int page_mkclean (struct page *page) -{ -return folio_mkclean (page_folio (page)); -}#endif /* _LINUX_RMAP_H */
mm/gup.c +1 -1
@@ -393,7 +393,7 @@ void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,* 1) This code sees the page as already dirty, so it * skips the call to set_page_dirty(). That could happen * because clear_page_dirty_for_io() called -* page_mkclean(), followed by set_page_dirty(). +* folio_mkclean(), followed by set_page_dirty(). * However, now the page is going to get written back, * which meets the original intention of setting it * dirty, so all is well: clear_page_dirty_for_io() goes
mm/mremap.c +1 -1
@@ -198,7 +198,7 @@ static int move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,* PTE. * * NOTE! Both old and new PTL matter: the old one -* for racing with page_mkclean(), the new one to +* for racing with folio_mkclean(), the new one to * make sure the physical page stays valid until * the TLB entry for the old mapping has been * flushed.
Signature · 1 line
2.27.0
PATCH 1/1 pin_user_pages: fix underline length
Documentation/core-api/pin_user_pages.rst:200: WARNING: Title underline too short. folio_maybe_dma_pinned(): the whole point of pinning
===================================================
---
Andrew, please help to squash this into "mm: remove page_mkclean()", thansk.
Documentation/core-api/pin_user_pages.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/core-api/pin_user_pages.rst +1 -1
@@ -197,7 +197,7 @@ INCORRECT (uses FOLL_GET calls): put_page() folio_maybe_dma_pinned(): the whole point of pinning -=================================================== +==================================================== The whole point of marking folios as "DMA-pinned" or "gup-pinned" is to be able to query, "is this folio DMA-pinned?" That allows code such as folio_mkclean()
Signature · 1 line
2.27.0