put_user_page*(): start converting the call sites
Subsystems (8)
Jan Kara
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to Jerome GlisseQuoted 119 lines · Jerome Glisse
Quoted 108 lines · Jan Kara
Quoted 73 lines · Jerome Glisse
Quoted 62 lines · Jan Kara
Quoted 32 lines · Jerome Glisse
Quoted 14 lines · Jan Kara
Quoted 8 lines · Jan Kara
Agreed. So with page lock it would actually look like:
get_page_pin()lock_page(page); wait_for_stable_page(); atomic_add(&page->_refcount, PAGE_PIN_BIAS); unlock_page(page);Quoted 4 lines
And if we perform
page_pinned()check under page lock, then ifpage_pinned()returned false, we are sure page is not and will not be pinned until we drop the page lock (and also until page writeback is completed if needed).Quoted 8 lines
After some more though, why do we even need
wait_for_stable_page()andlock_page()inget_page_pin()?During writepage
page_mkclean()will write protect all page tables. So there can be no new writeable GUP pins until we unlock the page as all such GUPs will have to first go through fault and ->page_mkwrite() handler. And that will wait on page lock and dowait_for_stable_page()for us anyway. Am I just confused?Yeah with page lock it should synchronize on the pte but you still need to check for writeback iirc the page is unlocked after file system has queue up the write and thus the page can be unlock with write back pending (and
PageWriteback()== trye) and i am not sure that in that states we can safely let anyone write to that page. I am assuming that in some case the block device also expect stable page content (RAID stuff).Quoted 2 lines
So the
PageWriteback()test is not only for racing page_mkclean()/test_set_page_writeback()and GUP but also for pending write back.But this is prevented by
wait_for_stable_page()that is already present in ->page_mkwrite() handlers. Look:->writepage()
/* Page is locked here */
clear_page_dirty_for_io(page)page_mkclean(page) -> page tables get writeprotected /* The following line will be added by our patches */ if (page_pinned(page)) -> bounce TestClearPageDirty(page)
set_page_writeback(page);
unlock_page(page);
...submit_io...IRQ
- IO completion
end_page_writeback()So if GUP happens before
page_mkclean()writeprotects corresponding PTE (and these two actions are synchronized on the PTE lock),page_pinned()will see the increment and report the page as pinned.If GUP happens after
page_mkclean()writeprotects corresponding PTE, it will fault:
handle_mm_fault()Quoted 9 lines
do_wp_page() wp_page_shared() do_page_mkwrite() ->page_mkwrite() - that is block_page_mkwrite() or iomap_page_mkwrite() or whatever filesystem provides lock_page(page) ... prepare page ... wait_for_stable_page(page) -> this blocks until IO completes if someone cares about pages not being modified while under IO.Quoted 8 lines
The case i am worried is GUP see pte with write flag set but has not lock the page yet (GUP is get pte first, then pte to page then lock page), then it locks the page but the lock page can make it wait for a racing page_mkclean()...write back that have not yet write protected the pte the GUP just read. So by the time GUP has the page locked the pte it read might no longer have the write flag set. Hence why you need to also check for write back after taking the page lock. Alternatively you could recheck the pte after a successful
try_lockon the page.This isn't really possible. GUP does:
get_user_pages()
...
follow_page_mask()
...
follow_page_pte()
ptep =pte_offset_map_lock()
check permissions and page sanity
if (flags &FOLL_GET)
get_page(page); -> this would become
atomic_add(&page->_refcount, PAGE_PIN_BIAS);
pte_unmap_unlock(ptep, ptl);
page_mkclean()on the other hand grabs the same pte lock to change the pte to write-protected. So afterpage_mkclean()has modified the PTE we are racing on for access, we are sure to either see increased_refcountor get page fault from GUP.If we see increased
_refcount, we bounce the page and are fine. If GUP faults, we will wait for page lock (so wait until page is prepared for IO and has PageWriteback set) while handling the fault, then enter ->page_mkwrite, which will dowait_for_stable_page()-> wait for outstanding writeback to complete.Quoted 4 lines
So I still conclude - no need for page lock in the GUP path at all AFAICT. In fact we rely on the very same page fault vs page writeback synchronization for normal user faults as well. And normal user mmap access is even nastier than GUP access because the CPU reads page tables without taking PTE lock.
For the "slow" GUP path you are right you do not need a lock as the page table lock give you the ordering. For the GUP fast path you would either need the lock or the memory barrier with the test for page write back.
Quoted 4 lines
Maybe an easier thing is to convert GUP fast to try to take the page table lock if it fails taking the page table lock then we fall back to slow GUP path. Otherwise then we have the same garantee as the slow path.
You're right I was looking at the wrong place for GUP_fast() path. But I still don't think anything special (i.e. page lock or new barrier) is necessary. GUP_fast() takes care already now that it cannot race with page unmapping or write-protection (as there are other places in MM that rely on this). Look, gup_pte_range() has:
if (!page_cache_get_speculative(head))
goto pte_unmap;
if (unlikely(pte_val(pte) != pte_val(*ptep))) {
put_page(head);
goto pte_unmap;
}So that page_cache_get_speculative() will become page_cache_pin_speculative() to increment refcount by PAGE_PIN_BIAS instead of 1. That is atomic ordered operation so it cannot be reordered with the following check that PTE stayed same. So once page_mkclean() write-protects PTE, there can be no new pins from GUP_fast() and we are sure all succeeding pins are visible in page->_refcount after page_mkclean() completes. Again this is nothing new, other mm code already relies on either seeing page->_refcount incremented or GUP fast bailing out (e.g. DAX relies on this). Although strictly speaking I'm not 100% sure what prevents page->_refcount load to be speculatively reordered before PTE update even in current places using this but there's so much stuff inbetween that there's probably something ;). But we could add smp_rmb() after page_mkclean() before changing page_pinned() for the peace of mind I guess.
Quoted 73 lines
The issue is that i am not sure if the page table directory page and it's associated spinlock can go in bad state if the directory is being freed (like a racing munmap). This would need to be check. A scheme that might protect against that is to take the above lock of each level before going down one level. Once you are down one level you can unlock the above level. So at any point in time GUP fast holds the lock to a current and valid directory and thus no one could race to remove it.
GUP_fast() gup_pgd_range() if (p4d_try_map_lock()) { gup_p4d_range() if (pud_try_map_lock()) { p4d_unlock(); gup_pud_range(); if (pmd_try_map_lock()) { pud_unlock(); gup_pmd_range(); if (pte_try_map_lock()) { pmd_unlock(); // Do gup } } } }Maybe this is worse than taking the
mmap_semand checking for vma.Quoted 35 lines
Quoted 35 lines
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines
That actually touches on another question I wanted to get opinions on. GUP can be for read and GUP can be for write (that is one of GUP flags). Filesystems with page cache generally have issues only with GUP for write as it can currently corrupt data, unexpectedly dirty page etc.. DAX & memory hotplug have issues with both (DAX cannot truncate page pinned in any way, memory hotplug will just loop in kernel until the page gets unpinned). So we probably want to track both types of GUP pins and page-cache based filesystems will take the hit even if they don't have to for read-pins?
Yes the distinction between read and write would be nice. With the map count solution you can only increment the mapcount for GUP(write=true).
Quoted 6 lines
Well, but if we track only pins for write, DAX or memory hotplug will not be able to use this mechanism. So at this point I'm more leaning towards tracking all pins. It will cost some performance needlessly for read pins and filesystems using page cache when bouncing such pages but it's not like writeback of pinned pages is some performance critical operation... But I wanted to spell this out so that people are aware of this.
No they would know for regular pin, it is just as page migrate code. If the refcount + (
extra_ref_by_the_code_checking) > mapcount then you know someone has extra reference on your page.Those extra references are either some regular fs event taking place (some code doing
find_get_pagefor instance) or a GUP reference (wether it is a write pin or a read pin).So the only issue is false positive, ie thinking the page is under GUP while it has just elevated refcount because of some other regular fs/mm event. To minimize false positive for a more accurate pin test (write or read) you can enforce few thing:
1 - first page lock
2 - then freeze the page with expected countedQuoted 8 lines
Quoted 2 lines
With that it should minimize false positive. In the end even with the bias case you can also have false positive.
So this is basically what the code is currently doing. And for DAX it works well since the page is being truncated and so essentially nobody is touching it. But for hotplug it doesn't work quite well - hotplug would like to return EBUSY to userspace when the page is pinned but retry if the page reference is just transient.
Quoted 6 lines
I do not think there is anyway around transient refcount other than periodicaly check. Maybe hot unplug (i am assuming we are talking about unplug here) can set the reserved page flag and we can change the page get ref to never inc refcount for page with reserved flag. I see that they have been cleanup on going around reserved page so it might or might not be possible.
Well, there's no problem with transient refcount. The code is fine with retrying in that case. The problem is when the refcount actually is *not* transient and so hot-unplug ends up retrying forever (or for very long time). In that case it should rather bail with EBUSY but currently there's no way to distinguish transient from longer term pin.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 139 lines · Jan Kara
Quoted 119 lines · Jerome Glisse
Quoted 108 lines · Jan Kara
Quoted 73 lines · Jerome Glisse
Quoted 62 lines · Jan Kara
Quoted 32 lines · Jerome Glisse
Quoted 14 lines · Jan Kara
Quoted 8 lines · Jan Kara
Agreed. So with page lock it would actually look like:
get_page_pin()lock_page(page); wait_for_stable_page(); atomic_add(&page->_refcount, PAGE_PIN_BIAS); unlock_page(page);Quoted 4 lines
And if we perform
page_pinned()check under page lock, then ifpage_pinned()returned false, we are sure page is not and will not be pinned until we drop the page lock (and also until page writeback is completed if needed).Quoted 8 lines
After some more though, why do we even need
wait_for_stable_page()andlock_page()inget_page_pin()?During writepage
page_mkclean()will write protect all page tables. So there can be no new writeable GUP pins until we unlock the page as all such GUPs will have to first go through fault and ->page_mkwrite() handler. And that will wait on page lock and dowait_for_stable_page()for us anyway. Am I just confused?Yeah with page lock it should synchronize on the pte but you still need to check for writeback iirc the page is unlocked after file system has queue up the write and thus the page can be unlock with write back pending (and
PageWriteback()== trye) and i am not sure that in that states we can safely let anyone write to that page. I am assuming that in some case the block device also expect stable page content (RAID stuff).Quoted 2 lines
So the
PageWriteback()test is not only for racing page_mkclean()/test_set_page_writeback()and GUP but also for pending write back.But this is prevented by
wait_for_stable_page()that is already present in ->page_mkwrite() handlers. Look:->writepage()
/* Page is locked here */
clear_page_dirty_for_io(page)page_mkclean(page) -> page tables get writeprotected /* The following line will be added by our patches */ if (page_pinned(page)) -> bounce TestClearPageDirty(page)
set_page_writeback(page);
unlock_page(page);
...submit_io...IRQ
- IO completion
end_page_writeback()So if GUP happens before
page_mkclean()writeprotects corresponding PTE (and these two actions are synchronized on the PTE lock),page_pinned()will see the increment and report the page as pinned.If GUP happens after
page_mkclean()writeprotects corresponding PTE, it will fault:
handle_mm_fault()Quoted 9 lines
do_wp_page() wp_page_shared() do_page_mkwrite() ->page_mkwrite() - that is block_page_mkwrite() or iomap_page_mkwrite() or whatever filesystem provides lock_page(page) ... prepare page ... wait_for_stable_page(page) -> this blocks until IO completes if someone cares about pages not being modified while under IO.Quoted 8 lines
The case i am worried is GUP see pte with write flag set but has not lock the page yet (GUP is get pte first, then pte to page then lock page), then it locks the page but the lock page can make it wait for a racing page_mkclean()...write back that have not yet write protected the pte the GUP just read. So by the time GUP has the page locked the pte it read might no longer have the write flag set. Hence why you need to also check for write back after taking the page lock. Alternatively you could recheck the pte after a successful
try_lockon the page.This isn't really possible. GUP does:
get_user_pages()
...
follow_page_mask()
...
follow_page_pte()
ptep =pte_offset_map_lock()
check permissions and page sanity
if (flags &FOLL_GET)
get_page(page); -> this would become
atomic_add(&page->_refcount, PAGE_PIN_BIAS);
pte_unmap_unlock(ptep, ptl);
page_mkclean()on the other hand grabs the same pte lock to change the pte to write-protected. So afterpage_mkclean()has modified the PTE we are racing on for access, we are sure to either see increased_refcountor get page fault from GUP.If we see increased
_refcount, we bounce the page and are fine. If GUP faults, we will wait for page lock (so wait until page is prepared for IO and has PageWriteback set) while handling the fault, then enter ->page_mkwrite, which will dowait_for_stable_page()-> wait for outstanding writeback to complete.Quoted 4 lines
So I still conclude - no need for page lock in the GUP path at all AFAICT. In fact we rely on the very same page fault vs page writeback synchronization for normal user faults as well. And normal user mmap access is even nastier than GUP access because the CPU reads page tables without taking PTE lock.
For the "slow" GUP path you are right you do not need a lock as the page table lock give you the ordering. For the GUP fast path you would either need the lock or the memory barrier with the test for page write back.
Quoted 4 lines
Maybe an easier thing is to convert GUP fast to try to take the page table lock if it fails taking the page table lock then we fall back to slow GUP path. Otherwise then we have the same garantee as the slow path.
You're right I was looking at the wrong place for
GUP_fast()path. But I still don't think anything special (i.e. page lock or new barrier) is necessary.GUP_fast()takes care already now that it cannot race with page unmapping or write-protection (as there are other places in MM that rely on this). Look,gup_pte_range()has:if (!page_cache_get_speculative(head)) goto pte_unmap; if (unlikely(pte_val(pte) != pte_val(*ptep))) { put_page(head); goto pte_unmap; }
Quoted 13 lines
So that
page_cache_get_speculative()will becomepage_cache_pin_speculative()to increment refcount byPAGE_PIN_BIASinstead of 1. That is atomic ordered operation so it cannot be reordered with the following check that PTE stayed same. So oncepage_mkclean()write-protects PTE, there can be no new pins fromGUP_fast()and we are sure all succeeding pins are visible inpage->_refcountafterpage_mkclean()completes. Again this is nothing new, other mm code already relies on either seeingpage->_refcountincremented or GUP fast bailing out (e.g. DAX relies on this). Although strictly speaking I'm not 100% sure what preventspage->_refcountload to be speculatively reordered before PTE update even in current places using this but there's so much stuff inbetween that there's probably something ;). But we could addsmp_rmb()afterpage_mkclean()before changingpage_pinned()for the peace of mind I guess.
Yeah i think you are right, i missed the check on same pte value and the atomic inc in page_cache_get_speculative() is a barrier. I do not think the barrier would be necessary as page_mkclean is taking and dropping locks so those should have enough barriering.
Quoted 81 lines
Quoted 74 lines
The issue is that i am not sure if the page table directory page and it's associated spinlock can go in bad state if the directory is being freed (like a racing munmap). This would need to be check. A scheme that might protect against that is to take the above lock of each level before going down one level. Once you are down one level you can unlock the above level. So at any point in time GUP fast holds the lock to a current and valid directory and thus no one could race to remove it.
GUP_fast() gup_pgd_range() if (p4d_try_map_lock()) { gup_p4d_range() if (pud_try_map_lock()) { p4d_unlock(); gup_pud_range(); if (pmd_try_map_lock()) { pud_unlock(); gup_pmd_range(); if (pte_try_map_lock()) { pmd_unlock(); // Do gup } } } }Maybe this is worse than taking the
mmap_semand checking for vma.Quoted 35 lines
Quoted 35 lines
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines
That actually touches on another question I wanted to get opinions on. GUP can be for read and GUP can be for write (that is one of GUP flags). Filesystems with page cache generally have issues only with GUP for write as it can currently corrupt data, unexpectedly dirty page etc.. DAX & memory hotplug have issues with both (DAX cannot truncate page pinned in any way, memory hotplug will just loop in kernel until the page gets unpinned). So we probably want to track both types of GUP pins and page-cache based filesystems will take the hit even if they don't have to for read-pins?
Yes the distinction between read and write would be nice. With the map count solution you can only increment the mapcount for GUP(write=true).
Quoted 6 lines
Well, but if we track only pins for write, DAX or memory hotplug will not be able to use this mechanism. So at this point I'm more leaning towards tracking all pins. It will cost some performance needlessly for read pins and filesystems using page cache when bouncing such pages but it's not like writeback of pinned pages is some performance critical operation... But I wanted to spell this out so that people are aware of this.
No they would know for regular pin, it is just as page migrate code. If the refcount + (
extra_ref_by_the_code_checking) > mapcount then you know someone has extra reference on your page.Those extra references are either some regular fs event taking place (some code doing
find_get_pagefor instance) or a GUP reference (wether it is a write pin or a read pin).So the only issue is false positive, ie thinking the page is under GUP while it has just elevated refcount because of some other regular fs/mm event. To minimize false positive for a more accurate pin test (write or read) you can enforce few thing:
1 - first page lock
2 - then freeze the page with expected countedQuoted 8 lines
Quoted 2 lines
With that it should minimize false positive. In the end even with the bias case you can also have false positive.
So this is basically what the code is currently doing. And for DAX it works well since the page is being truncated and so essentially nobody is touching it. But for hotplug it doesn't work quite well - hotplug would like to return EBUSY to userspace when the page is pinned but retry if the page reference is just transient.
Quoted 6 lines
I do not think there is anyway around transient refcount other than periodicaly check. Maybe hot unplug (i am assuming we are talking about unplug here) can set the reserved page flag and we can change the page get ref to never inc refcount for page with reserved flag. I see that they have been cleanup on going around reserved page so it might or might not be possible.
Quoted 5 lines
Well, there's no problem with transient refcount. The code is fine with retrying in that case. The problem is when the refcount actually is *not* transient and so hot-unplug ends up retrying forever (or for very long time). In that case it should rather bail with EBUSY but currently there's no way to distinguish transient from longer term pin.
Yes and i was trying to point out that we can try checking for GUP read pin and if we have a doubt about GUP read pin false positive we can do heavier check the second time around if it is still positive then just return -EBUSY or something to user space so that we do not retry forever in the kernel.
So not using bias for read GUP looks ok to me (ie we should still be able to differentiate transient refcount from read GUP). But this can be done as an optimization if it the page bouncing for read GUP ever hurt performance too much.
Cheers,
Jérôme
John Hubbard
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 154 lines · Jerome Glisse
Quoted 139 lines · Jan Kara
Quoted 119 lines · Jerome Glisse
Quoted 108 lines · Jan Kara
Quoted 73 lines · Jerome Glisse
Quoted 62 lines · Jan Kara
Quoted 32 lines · Jerome Glisse
Quoted 14 lines · Jan Kara
Quoted 8 lines · Jan Kara
Agreed. So with page lock it would actually look like:
get_page_pin()lock_page(page); wait_for_stable_page(); atomic_add(&page->_refcount, PAGE_PIN_BIAS); unlock_page(page);Quoted 4 lines
And if we perform
page_pinned()check under page lock, then ifpage_pinned()returned false, we are sure page is not and will not be pinned until we drop the page lock (and also until page writeback is completed if needed).Quoted 8 lines
After some more though, why do we even need
wait_for_stable_page()andlock_page()inget_page_pin()?During writepage
page_mkclean()will write protect all page tables. So there can be no new writeable GUP pins until we unlock the page as all such GUPs will have to first go through fault and ->page_mkwrite() handler. And that will wait on page lock and dowait_for_stable_page()for us anyway. Am I just confused?Yeah with page lock it should synchronize on the pte but you still need to check for writeback iirc the page is unlocked after file system has queue up the write and thus the page can be unlock with write back pending (and
PageWriteback()== trye) and i am not sure that in that states we can safely let anyone write to that page. I am assuming that in some case the block device also expect stable page content (RAID stuff).Quoted 2 lines
So the
PageWriteback()test is not only for racing page_mkclean()/test_set_page_writeback()and GUP but also for pending write back.But this is prevented by
wait_for_stable_page()that is already present in ->page_mkwrite() handlers. Look:->writepage()
/* Page is locked here */
clear_page_dirty_for_io(page)page_mkclean(page) -> page tables get writeprotected /* The following line will be added by our patches */ if (page_pinned(page)) -> bounce TestClearPageDirty(page)
set_page_writeback(page);
unlock_page(page);
...submit_io...IRQ
- IO completion
end_page_writeback()So if GUP happens before
page_mkclean()writeprotects corresponding PTE (and these two actions are synchronized on the PTE lock),page_pinned()will see the increment and report the page as pinned.If GUP happens after
page_mkclean()writeprotects corresponding PTE, it will fault:
handle_mm_fault()Quoted 9 lines
do_wp_page() wp_page_shared() do_page_mkwrite() ->page_mkwrite() - that is block_page_mkwrite() or iomap_page_mkwrite() or whatever filesystem provides lock_page(page) ... prepare page ... wait_for_stable_page(page) -> this blocks until IO completes if someone cares about pages not being modified while under IO.Quoted 8 lines
The case i am worried is GUP see pte with write flag set but has not lock the page yet (GUP is get pte first, then pte to page then lock page), then it locks the page but the lock page can make it wait for a racing page_mkclean()...write back that have not yet write protected the pte the GUP just read. So by the time GUP has the page locked the pte it read might no longer have the write flag set. Hence why you need to also check for write back after taking the page lock. Alternatively you could recheck the pte after a successful
try_lockon the page.This isn't really possible. GUP does:
get_user_pages()
...
follow_page_mask()
...
follow_page_pte()
ptep =pte_offset_map_lock()
check permissions and page sanity
if (flags &FOLL_GET)
get_page(page); -> this would become
atomic_add(&page->_refcount, PAGE_PIN_BIAS);
pte_unmap_unlock(ptep, ptl);
page_mkclean()on the other hand grabs the same pte lock to change the pte to write-protected. So afterpage_mkclean()has modified the PTE we are racing on for access, we are sure to either see increased_refcountor get page fault from GUP.If we see increased
_refcount, we bounce the page and are fine. If GUP faults, we will wait for page lock (so wait until page is prepared for IO and has PageWriteback set) while handling the fault, then enter ->page_mkwrite, which will dowait_for_stable_page()-> wait for outstanding writeback to complete.Quoted 4 lines
So I still conclude - no need for page lock in the GUP path at all AFAICT. In fact we rely on the very same page fault vs page writeback synchronization for normal user faults as well. And normal user mmap access is even nastier than GUP access because the CPU reads page tables without taking PTE lock.
For the "slow" GUP path you are right you do not need a lock as the page table lock give you the ordering. For the GUP fast path you would either need the lock or the memory barrier with the test for page write back.
Quoted 4 lines
Maybe an easier thing is to convert GUP fast to try to take the page table lock if it fails taking the page table lock then we fall back to slow GUP path. Otherwise then we have the same garantee as the slow path.
You're right I was looking at the wrong place for
GUP_fast()path. But I still don't think anything special (i.e. page lock or new barrier) is necessary.GUP_fast()takes care already now that it cannot race with page unmapping or write-protection (as there are other places in MM that rely on this). Look,gup_pte_range()has:if (!page_cache_get_speculative(head)) goto pte_unmap; if (unlikely(pte_val(pte) != pte_val(*ptep))) { put_page(head); goto pte_unmap; }Quoted 13 lines
So that
page_cache_get_speculative()will becomepage_cache_pin_speculative()to increment refcount byPAGE_PIN_BIASinstead of 1. That is atomic ordered operation so it cannot be reordered with the following check that PTE stayed same. So oncepage_mkclean()write-protects PTE, there can be no new pins fromGUP_fast()and we are sure all succeeding pins are visible inpage->_refcountafterpage_mkclean()completes. Again this is nothing new, other mm code already relies on either seeingpage->_refcountincremented or GUP fast bailing out (e.g. DAX relies on this). Although strictly speaking I'm not 100% sure what preventspage->_refcountload to be speculatively reordered before PTE update even in current places using this but there's so much stuff inbetween that there's probably something ;). But we could addsmp_rmb()afterpage_mkclean()before changingpage_pinned()for the peace of mind I guess.
Quoted 5 lines
Yeah i think you are right, i missed the check on same pte value and the atomic inc in
page_cache_get_speculative()is a barrier. I do not think the barrier would be necessary aspage_mkcleanis taking and dropping locks so those should have enough barriering.
Hi Jan, Jerome,
OK, this seems to be up and running locally, but while putting together documentation and polishing up things, I noticed that there is one last piece that I don't quite understand, after all. The page_cache_get_speculative() existing documentation explains how refcount synchronizes these things, but I don't see how that helps with synchronization page_mkclean and gup, in this situation:
gup_fast gets the refcount and rechecks the pte hasn't changed
meanwhile, page_mkclean...wait, how does refcount come into play here? page_mkclean can remove the mapping and insert a write-protected pte, regardless of page refcount, correct? Help? :)
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 170 lines · John Hubbard
Quoted 154 lines · Jerome Glisse
Quoted 139 lines · Jan Kara
Quoted 119 lines · Jerome Glisse
Quoted 108 lines · Jan Kara
Quoted 73 lines · Jerome Glisse
Quoted 62 lines · Jan Kara
Quoted 32 lines · Jerome Glisse
Quoted 14 lines · Jan Kara
Quoted 8 lines · Jan Kara
Agreed. So with page lock it would actually look like:
get_page_pin()lock_page(page); wait_for_stable_page(); atomic_add(&page->_refcount, PAGE_PIN_BIAS); unlock_page(page);Quoted 4 lines
And if we perform
page_pinned()check under page lock, then ifpage_pinned()returned false, we are sure page is not and will not be pinned until we drop the page lock (and also until page writeback is completed if needed).Quoted 8 lines
After some more though, why do we even need
wait_for_stable_page()andlock_page()inget_page_pin()?During writepage
page_mkclean()will write protect all page tables. So there can be no new writeable GUP pins until we unlock the page as all such GUPs will have to first go through fault and ->page_mkwrite() handler. And that will wait on page lock and dowait_for_stable_page()for us anyway. Am I just confused?Yeah with page lock it should synchronize on the pte but you still need to check for writeback iirc the page is unlocked after file system has queue up the write and thus the page can be unlock with write back pending (and
PageWriteback()== trye) and i am not sure that in that states we can safely let anyone write to that page. I am assuming that in some case the block device also expect stable page content (RAID stuff).Quoted 2 lines
So the
PageWriteback()test is not only for racing page_mkclean()/test_set_page_writeback()and GUP but also for pending write back.But this is prevented by
wait_for_stable_page()that is already present in ->page_mkwrite() handlers. Look:->writepage()
/* Page is locked here */
clear_page_dirty_for_io(page)page_mkclean(page) -> page tables get writeprotected /* The following line will be added by our patches */ if (page_pinned(page)) -> bounce TestClearPageDirty(page)
set_page_writeback(page);
unlock_page(page);
...submit_io...IRQ
- IO completion
end_page_writeback()So if GUP happens before
page_mkclean()writeprotects corresponding PTE (and these two actions are synchronized on the PTE lock),page_pinned()will see the increment and report the page as pinned.If GUP happens after
page_mkclean()writeprotects corresponding PTE, it will fault:
handle_mm_fault()Quoted 9 lines
do_wp_page() wp_page_shared() do_page_mkwrite() ->page_mkwrite() - that is block_page_mkwrite() or iomap_page_mkwrite() or whatever filesystem provides lock_page(page) ... prepare page ... wait_for_stable_page(page) -> this blocks until IO completes if someone cares about pages not being modified while under IO.Quoted 8 lines
The case i am worried is GUP see pte with write flag set but has not lock the page yet (GUP is get pte first, then pte to page then lock page), then it locks the page but the lock page can make it wait for a racing page_mkclean()...write back that have not yet write protected the pte the GUP just read. So by the time GUP has the page locked the pte it read might no longer have the write flag set. Hence why you need to also check for write back after taking the page lock. Alternatively you could recheck the pte after a successful
try_lockon the page.This isn't really possible. GUP does:
get_user_pages()
...
follow_page_mask()
...
follow_page_pte()
ptep =pte_offset_map_lock()
check permissions and page sanity
if (flags &FOLL_GET)
get_page(page); -> this would become
atomic_add(&page->_refcount, PAGE_PIN_BIAS);
pte_unmap_unlock(ptep, ptl);
page_mkclean()on the other hand grabs the same pte lock to change the pte to write-protected. So afterpage_mkclean()has modified the PTE we are racing on for access, we are sure to either see increased_refcountor get page fault from GUP.If we see increased
_refcount, we bounce the page and are fine. If GUP faults, we will wait for page lock (so wait until page is prepared for IO and has PageWriteback set) while handling the fault, then enter ->page_mkwrite, which will dowait_for_stable_page()-> wait for outstanding writeback to complete.Quoted 4 lines
So I still conclude - no need for page lock in the GUP path at all AFAICT. In fact we rely on the very same page fault vs page writeback synchronization for normal user faults as well. And normal user mmap access is even nastier than GUP access because the CPU reads page tables without taking PTE lock.
For the "slow" GUP path you are right you do not need a lock as the page table lock give you the ordering. For the GUP fast path you would either need the lock or the memory barrier with the test for page write back.
Quoted 4 lines
Maybe an easier thing is to convert GUP fast to try to take the page table lock if it fails taking the page table lock then we fall back to slow GUP path. Otherwise then we have the same garantee as the slow path.
You're right I was looking at the wrong place for
GUP_fast()path. But I still don't think anything special (i.e. page lock or new barrier) is necessary.GUP_fast()takes care already now that it cannot race with page unmapping or write-protection (as there are other places in MM that rely on this). Look,gup_pte_range()has:if (!page_cache_get_speculative(head)) goto pte_unmap; if (unlikely(pte_val(pte) != pte_val(*ptep))) { put_page(head); goto pte_unmap; }Quoted 13 lines
So that
page_cache_get_speculative()will becomepage_cache_pin_speculative()to increment refcount byPAGE_PIN_BIASinstead of 1. That is atomic ordered operation so it cannot be reordered with the following check that PTE stayed same. So oncepage_mkclean()write-protects PTE, there can be no new pins fromGUP_fast()and we are sure all succeeding pins are visible inpage->_refcountafterpage_mkclean()completes. Again this is nothing new, other mm code already relies on either seeingpage->_refcountincremented or GUP fast bailing out (e.g. DAX relies on this). Although strictly speaking I'm not 100% sure what preventspage->_refcountload to be speculatively reordered before PTE update even in current places using this but there's so much stuff inbetween that there's probably something ;). But we could addsmp_rmb()afterpage_mkclean()before changingpage_pinned()for the peace of mind I guess.Quoted 5 lines
Yeah i think you are right, i missed the check on same pte value and the atomic inc in
page_cache_get_speculative()is a barrier. I do not think the barrier would be necessary aspage_mkcleanis taking and dropping locks so those should have enough barriering.Hi Jan, Jerome,
OK, this seems to be up and running locally, but while putting together documentation and polishing up things, I noticed that there is one last piece that I don't quite understand, after all. The
page_cache_get_speculative()existing documentation explains how refcount synchronizes these things, but I don't see how that helps with synchronizationpage_mkcleanand gup, in this situation:
Quoted 5 lines
gup_fastgets the refcount and rechecks the pte hasn't changedmeanwhile, page_mkclean...wait, how does refcount come into play here?
page_mkcleancan remove the mapping and insert a write-protected pte, regardless of page refcount, correct? Help? :)
Correct, page_mkclean() does not check the refcount and do not need to check it. We need to check for the page pin after the page_mkclean when code is done prepping the page for io (clear_page_dirty_for_io).
The race Jan and I were discussing was about wether we needed to lock the page or not and we do not. For slow path page_mkclean and GUP_slow will synchronize on the page table lock. For GUP_fast the fast code will back off if the pte is not the same and thus either we see the pin after page_mkclean() or GUP_fast back off. You will never have code that miss the pin after page_mkclean() and GUP_fast that did not back off.
Now the page_cache_get_speculative() is for another race when a page is freed concurrently. page_cache_get_speculative() only inc the refcount if the page is not already freed ie refcount != 0. So GUP_fast has 2 exclusions mechanisms, one for racing modification to the page table like page_mkclean (pte the same after incrementing the refcount) and one for racing put_page (only increment refcount if it is not 0). Here for what we want we just modify this second mechanisms to add the bias value not just 1 to the refcount. This keep both mechanisms intacts and give us the page pin test through refcount bias value.
Note that page_mkclean can not race with a put_page() as whoever calls page_mkclean already hold a reference on the page and thus no put_page can free the page.
Does that help ?
Cheers,
Jérôme
John Hubbard
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 181 lines · Jerome Glisse
Quoted 170 lines · John Hubbard
Quoted 154 lines · Jerome Glisse
Quoted 139 lines · Jan Kara
Quoted 119 lines · Jerome Glisse
Quoted 108 lines · Jan Kara
Quoted 73 lines · Jerome Glisse
Quoted 62 lines · Jan Kara
Quoted 32 lines · Jerome Glisse
Quoted 14 lines · Jan Kara
Quoted 8 lines · Jan Kara
Agreed. So with page lock it would actually look like:
get_page_pin()lock_page(page); wait_for_stable_page(); atomic_add(&page->_refcount, PAGE_PIN_BIAS); unlock_page(page);Quoted 4 lines
And if we perform
page_pinned()check under page lock, then ifpage_pinned()returned false, we are sure page is not and will not be pinned until we drop the page lock (and also until page writeback is completed if needed).Quoted 8 lines
After some more though, why do we even need
wait_for_stable_page()andlock_page()inget_page_pin()?During writepage
page_mkclean()will write protect all page tables. So there can be no new writeable GUP pins until we unlock the page as all such GUPs will have to first go through fault and ->page_mkwrite() handler. And that will wait on page lock and dowait_for_stable_page()for us anyway. Am I just confused?Yeah with page lock it should synchronize on the pte but you still need to check for writeback iirc the page is unlocked after file system has queue up the write and thus the page can be unlock with write back pending (and
PageWriteback()== trye) and i am not sure that in that states we can safely let anyone write to that page. I am assuming that in some case the block device also expect stable page content (RAID stuff).Quoted 2 lines
So the
PageWriteback()test is not only for racing page_mkclean()/test_set_page_writeback()and GUP but also for pending write back.But this is prevented by
wait_for_stable_page()that is already present in ->page_mkwrite() handlers. Look:->writepage()
/* Page is locked here */
clear_page_dirty_for_io(page)page_mkclean(page) -> page tables get writeprotected /* The following line will be added by our patches */ if (page_pinned(page)) -> bounce TestClearPageDirty(page) set_page_writeback(page); unlock_page(page); ...submit_io...IRQ
- IO completion
end_page_writeback()So if GUP happens before
page_mkclean()writeprotects corresponding PTE (and these two actions are synchronized on the PTE lock),page_pinned()will see the increment and report the page as pinned.If GUP happens after
page_mkclean()writeprotects corresponding PTE, it will fault:
handle_mm_fault()Quoted 9 lines
do_wp_page() wp_page_shared() do_page_mkwrite() ->page_mkwrite() - that is block_page_mkwrite() or iomap_page_mkwrite() or whatever filesystem provides lock_page(page) ... prepare page ... wait_for_stable_page(page) -> this blocks until IO completes if someone cares about pages not being modified while under IO.Quoted 8 lines
The case i am worried is GUP see pte with write flag set but has not lock the page yet (GUP is get pte first, then pte to page then lock page), then it locks the page but the lock page can make it wait for a racing page_mkclean()...write back that have not yet write protected the pte the GUP just read. So by the time GUP has the page locked the pte it read might no longer have the write flag set. Hence why you need to also check for write back after taking the page lock. Alternatively you could recheck the pte after a successful
try_lockon the page.This isn't really possible. GUP does:
get_user_pages()
...
follow_page_mask()
...
follow_page_pte()
ptep =pte_offset_map_lock()
check permissions and page sanity
if (flags &FOLL_GET)
get_page(page); -> this would become
atomic_add(&page->_refcount, PAGE_PIN_BIAS);
pte_unmap_unlock(ptep, ptl);
page_mkclean()on the other hand grabs the same pte lock to change the pte to write-protected. So afterpage_mkclean()has modified the PTE we are racing on for access, we are sure to either see increased_refcountor get page fault from GUP.If we see increased
_refcount, we bounce the page and are fine. If GUP faults, we will wait for page lock (so wait until page is prepared for IO and has PageWriteback set) while handling the fault, then enter ->page_mkwrite, which will dowait_for_stable_page()-> wait for outstanding writeback to complete.Quoted 4 lines
So I still conclude - no need for page lock in the GUP path at all AFAICT. In fact we rely on the very same page fault vs page writeback synchronization for normal user faults as well. And normal user mmap access is even nastier than GUP access because the CPU reads page tables without taking PTE lock.
For the "slow" GUP path you are right you do not need a lock as the page table lock give you the ordering. For the GUP fast path you would either need the lock or the memory barrier with the test for page write back.
Quoted 4 lines
Maybe an easier thing is to convert GUP fast to try to take the page table lock if it fails taking the page table lock then we fall back to slow GUP path. Otherwise then we have the same garantee as the slow path.
You're right I was looking at the wrong place for
GUP_fast()path. But I still don't think anything special (i.e. page lock or new barrier) is necessary.GUP_fast()takes care already now that it cannot race with page unmapping or write-protection (as there are other places in MM that rely on this). Look,gup_pte_range()has:if (!page_cache_get_speculative(head)) goto pte_unmap; if (unlikely(pte_val(pte) != pte_val(*ptep))) { put_page(head); goto pte_unmap; }Quoted 13 lines
So that
page_cache_get_speculative()will becomepage_cache_pin_speculative()to increment refcount byPAGE_PIN_BIASinstead of 1. That is atomic ordered operation so it cannot be reordered with the following check that PTE stayed same. So oncepage_mkclean()write-protects PTE, there can be no new pins fromGUP_fast()and we are sure all succeeding pins are visible inpage->_refcountafterpage_mkclean()completes. Again this is nothing new, other mm code already relies on either seeingpage->_refcountincremented or GUP fast bailing out (e.g. DAX relies on this). Although strictly speaking I'm not 100% sure what preventspage->_refcountload to be speculatively reordered before PTE update even in current places using this but there's so much stuff inbetween that there's probably something ;). But we could addsmp_rmb()afterpage_mkclean()before changingpage_pinned()for the peace of mind I guess.Quoted 5 lines
Yeah i think you are right, i missed the check on same pte value and the atomic inc in
page_cache_get_speculative()is a barrier. I do not think the barrier would be necessary aspage_mkcleanis taking and dropping locks so those should have enough barriering.Hi Jan, Jerome,
OK, this seems to be up and running locally, but while putting together documentation and polishing up things, I noticed that there is one last piece that I don't quite understand, after all. The
page_cache_get_speculative()existing documentation explains how refcount synchronizes these things, but I don't see how that helps with synchronizationpage_mkcleanand gup, in this situation:Quoted 5 lines
gup_fastgets the refcount and rechecks the pte hasn't changedmeanwhile, page_mkclean...wait, how does refcount come into play here?
page_mkcleancan remove the mapping and insert a write-protected pte, regardless of page refcount, correct? Help? :)Correct,
page_mkclean()does not check the refcount and do not need to check it. We need to check for the page pin after thepage_mkcleanwhen code is done prepping the page for io (clear_page_dirty_for_io).
Quoted 6 lines
The race Jan and I were discussing was about wether we needed to lock the page or not and we do not. For slow path
page_mkcleanandGUP_slowwill synchronize on the page table lock. ForGUP_fastthe fast code will back off if the pte is not the same and thus either we see the pin afterpage_mkclean()orGUP_fastback off. You will never have code that miss the pin afterpage_mkclean()andGUP_fastthat did not back off.
Here is the case I'm wondering about:
thread A thread B
-------- --------
gup_fastpage_mkclean
is page gup-pinned?(no)
page_cache_get_speculative
(gup-pins the page here)
check pte_val unchanged (yes)
set_pte_at()
...and now thread A has created a read-only PTE, after gup_fast walked the page tables and found a writeable entry. And so far, thread A has not seen that the page is pinned.
What am I missing here? The above seems like a problem even before we change anything.
Quoted 11 lines
Now the
page_cache_get_speculative()is for another race when a page is freed concurrently.page_cache_get_speculative()only inc the refcount if the page is not already freed ie refcount != 0. SoGUP_fasthas 2 exclusions mechanisms, one for racing modification to the page table likepage_mkclean(pte the same after incrementing the refcount) and one for racingput_page(only increment refcount if it is not 0). Here for what we want we just modify this second mechanisms to add the bias value not just 1 to the refcount. This keep both mechanisms intacts and give us the page pin test through refcount bias value.
Quoted 5 lines
Note that
page_mkcleancan not race with aput_page()as whoever callspage_mkcleanalready hold a reference on the page and thus noput_pagecan free the page.Does that help ?
Yes...getting close... :)
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Jan Kara
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 201 lines · John Hubbard
Quoted 181 lines · Jerome Glisse
Quoted 170 lines · John Hubbard
Quoted 154 lines · Jerome Glisse
Quoted 139 lines · Jan Kara
Quoted 119 lines · Jerome Glisse
Quoted 108 lines · Jan Kara
Quoted 73 lines · Jerome Glisse
Quoted 62 lines · Jan Kara
Quoted 32 lines · Jerome Glisse
Quoted 14 lines · Jan Kara
Quoted 8 lines · Jan Kara
Agreed. So with page lock it would actually look like:
get_page_pin()lock_page(page); wait_for_stable_page(); atomic_add(&page->_refcount, PAGE_PIN_BIAS); unlock_page(page);Quoted 4 lines
And if we perform
page_pinned()check under page lock, then ifpage_pinned()returned false, we are sure page is not and will not be pinned until we drop the page lock (and also until page writeback is completed if needed).Quoted 8 lines
After some more though, why do we even need
wait_for_stable_page()andlock_page()inget_page_pin()?During writepage
page_mkclean()will write protect all page tables. So there can be no new writeable GUP pins until we unlock the page as all such GUPs will have to first go through fault and ->page_mkwrite() handler. And that will wait on page lock and dowait_for_stable_page()for us anyway. Am I just confused?Yeah with page lock it should synchronize on the pte but you still need to check for writeback iirc the page is unlocked after file system has queue up the write and thus the page can be unlock with write back pending (and
PageWriteback()== trye) and i am not sure that in that states we can safely let anyone write to that page. I am assuming that in some case the block device also expect stable page content (RAID stuff).Quoted 2 lines
So the
PageWriteback()test is not only for racing page_mkclean()/test_set_page_writeback()and GUP but also for pending write back.But this is prevented by
wait_for_stable_page()that is already present in ->page_mkwrite() handlers. Look:->writepage()
/* Page is locked here */
clear_page_dirty_for_io(page)page_mkclean(page) -> page tables get writeprotected /* The following line will be added by our patches */ if (page_pinned(page)) -> bounce TestClearPageDirty(page) set_page_writeback(page); unlock_page(page); ...submit_io...IRQ
- IO completion
end_page_writeback()So if GUP happens before
page_mkclean()writeprotects corresponding PTE (and these two actions are synchronized on the PTE lock),page_pinned()will see the increment and report the page as pinned.If GUP happens after
page_mkclean()writeprotects corresponding PTE, it will fault:
handle_mm_fault()Quoted 9 lines
do_wp_page() wp_page_shared() do_page_mkwrite() ->page_mkwrite() - that is block_page_mkwrite() or iomap_page_mkwrite() or whatever filesystem provides lock_page(page) ... prepare page ... wait_for_stable_page(page) -> this blocks until IO completes if someone cares about pages not being modified while under IO.Quoted 8 lines
The case i am worried is GUP see pte with write flag set but has not lock the page yet (GUP is get pte first, then pte to page then lock page), then it locks the page but the lock page can make it wait for a racing page_mkclean()...write back that have not yet write protected the pte the GUP just read. So by the time GUP has the page locked the pte it read might no longer have the write flag set. Hence why you need to also check for write back after taking the page lock. Alternatively you could recheck the pte after a successful
try_lockon the page.This isn't really possible. GUP does:
get_user_pages()
...
follow_page_mask()
...
follow_page_pte()
ptep =pte_offset_map_lock()
check permissions and page sanity
if (flags &FOLL_GET)
get_page(page); -> this would become
atomic_add(&page->_refcount, PAGE_PIN_BIAS);
pte_unmap_unlock(ptep, ptl);
page_mkclean()on the other hand grabs the same pte lock to change the pte to write-protected. So afterpage_mkclean()has modified the PTE we are racing on for access, we are sure to either see increased_refcountor get page fault from GUP.If we see increased
_refcount, we bounce the page and are fine. If GUP faults, we will wait for page lock (so wait until page is prepared for IO and has PageWriteback set) while handling the fault, then enter ->page_mkwrite, which will dowait_for_stable_page()-> wait for outstanding writeback to complete.Quoted 4 lines
So I still conclude - no need for page lock in the GUP path at all AFAICT. In fact we rely on the very same page fault vs page writeback synchronization for normal user faults as well. And normal user mmap access is even nastier than GUP access because the CPU reads page tables without taking PTE lock.
For the "slow" GUP path you are right you do not need a lock as the page table lock give you the ordering. For the GUP fast path you would either need the lock or the memory barrier with the test for page write back.
Quoted 4 lines
Maybe an easier thing is to convert GUP fast to try to take the page table lock if it fails taking the page table lock then we fall back to slow GUP path. Otherwise then we have the same garantee as the slow path.
You're right I was looking at the wrong place for
GUP_fast()path. But I still don't think anything special (i.e. page lock or new barrier) is necessary.GUP_fast()takes care already now that it cannot race with page unmapping or write-protection (as there are other places in MM that rely on this). Look,gup_pte_range()has:if (!page_cache_get_speculative(head)) goto pte_unmap; if (unlikely(pte_val(pte) != pte_val(*ptep))) { put_page(head); goto pte_unmap; }Quoted 13 lines
So that
page_cache_get_speculative()will becomepage_cache_pin_speculative()to increment refcount byPAGE_PIN_BIASinstead of 1. That is atomic ordered operation so it cannot be reordered with the following check that PTE stayed same. So oncepage_mkclean()write-protects PTE, there can be no new pins fromGUP_fast()and we are sure all succeeding pins are visible inpage->_refcountafterpage_mkclean()completes. Again this is nothing new, other mm code already relies on either seeingpage->_refcountincremented or GUP fast bailing out (e.g. DAX relies on this). Although strictly speaking I'm not 100% sure what preventspage->_refcountload to be speculatively reordered before PTE update even in current places using this but there's so much stuff inbetween that there's probably something ;). But we could addsmp_rmb()afterpage_mkclean()before changingpage_pinned()for the peace of mind I guess.Quoted 5 lines
Yeah i think you are right, i missed the check on same pte value and the atomic inc in
page_cache_get_speculative()is a barrier. I do not think the barrier would be necessary aspage_mkcleanis taking and dropping locks so those should have enough barriering.Hi Jan, Jerome,
OK, this seems to be up and running locally, but while putting together documentation and polishing up things, I noticed that there is one last piece that I don't quite understand, after all. The
page_cache_get_speculative()existing documentation explains how refcount synchronizes these things, but I don't see how that helps with synchronizationpage_mkcleanand gup, in this situation:Quoted 5 lines
gup_fastgets the refcount and rechecks the pte hasn't changedmeanwhile, page_mkclean...wait, how does refcount come into play here?
page_mkcleancan remove the mapping and insert a write-protected pte, regardless of page refcount, correct? Help? :)Correct,
page_mkclean()does not check the refcount and do not need to check it. We need to check for the page pin after thepage_mkcleanwhen code is done prepping the page for io (clear_page_dirty_for_io).Quoted 6 lines
The race Jan and I were discussing was about wether we needed to lock the page or not and we do not. For slow path
page_mkcleanandGUP_slowwill synchronize on the page table lock. ForGUP_fastthe fast code will back off if the pte is not the same and thus either we see the pin afterpage_mkclean()orGUP_fastback off. You will never have code that miss the pin afterpage_mkclean()andGUP_fastthat did not back off.Here is the case I'm wondering about:
thread A thread B
-------- --------
gup_fastpage_mkclean
is page gup-pinned?(no)
page_cache_get_speculative
(gup-pins the page here)
checkpte_valunchanged (yes)
set_pte_at()
Quoted 6 lines
...and now thread A has created a read-only PTE, after
gup_fastwalked the page tables and found a writeable entry. And so far, thread A has not seen that the page is pinned.What am I missing here? The above seems like a problem even before we change anything.
Your implementation of page_mkclean() is wrong :) It needs to first call set_pte_at() and only after that ask "is page gup pinned?". In fact, page_mkclean() probably has no bussiness in checking for page pins whatsoever. It is clear_page_dirty_for_io() that cares, so that should check for page pins after page_mkclean() has returned.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
John Hubbard
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 1 line · Jan Kara
On Mon 28-01-19 22:41:41, John Hubbard wrote:
[...]
Quoted 16 lines
Quoted 16 lines
Here is the case I'm wondering about:
thread A thread B
-------- --------
gup_fastpage_mkclean
is page gup-pinned?(no)
page_cache_get_speculative
(gup-pins the page here)
checkpte_valunchanged (yes)
set_pte_at()...and now thread A has created a read-only PTE, after
gup_fastwalked the page tables and found a writeable entry. And so far, thread A has not seen that the page is pinned.
Quoted 9 lines
Quoted 2 lines
What am I missing here? The above seems like a problem even before we change anything.
Your implementation of
page_mkclean()is wrong :) It needs to first callset_pte_at()and only after that ask "is page gup pinned?". In fact,page_mkclean()probably has no bussiness in checking for page pins whatsoever. It isclear_page_dirty_for_io()that cares, so that should check for page pins afterpage_mkclean()has returned.
Perfect, that was the missing piece for me: page_mkclean() internally doesn't need the consistent view, just the caller does. The whole situation with two distinct lock-free algorithms going on here actually seems clear at last. :)
Thanks (also to Jerome) for explaining this!
thanks,
Signature · 2 lines
John Hubbard NVIDIA
John Hubbard
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to Jan KaraQuoted 14 lines · Jan Kara
Quoted 14 lines
Quoted 14 lines
Quoted 1 line
Quoted 1 line
[...]
Also there is one more idea I had how to record number of pins in the page:
#define
PAGE_PIN_BIAS1024
get_page_pin()atomic_add(&page->_refcount, PAGE_PIN_BIAS);put_page_pin(); atomic_add(&page->_refcount, -PAGE_PIN_BIAS);
page_pinned(page)(atomic_read(&page->_refcount) - page_mapcount(page)) > PAGE_PIN_BIAS
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
This is pretty trivial scheme. It still gives us 22-bits for page pins which should be plenty (but we should check for that and bail with error if it would overflow). Also there will be no false negatives and false positives only if there are more than 1024 non-page-table references to the page which I expect to be rare (we might want to also subtract
hpage_nr_pages()for radix tree references to avoid excessive false positives for huge pages although at this point I don't think they would matter). Thoughts?
Hi Jan,
Some details, sorry I'm not fully grasping your plan without more explanation:
Do I read it correctly that this uses the lower 10 bits for the original page->_refcount, and the upper 22 bits for gup-pinned counts? If so, I'm surprised, because gup-pinned is going to be less than or equal to the normal (get_page-based) pin count. And 1024 seems like it might be reached in a large system with lots of processes and IPC.
Are you just allowing the lower 10 bits to overflow, and that's why the subtraction of mapcount? Wouldn't it be better to allow more than 10 bits, instead?
Another question: do we just allow other kernel code to observe this biased _refcount, or do we attempt to filter it out? In other words, do you expect problems due to some kernel code checking the _refcount and finding a large number there, when it expected, say, 3? I recall some code tries to do that...in fact, ZONE_DEVICE is 1-based, instead of zero-based, with respect to _refcount, right?
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Jan Kara
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 33 lines · John Hubbard
Quoted 14 lines · Jan Kara
Quoted 14 lines
Quoted 14 lines
Quoted 1 line
Quoted 1 line
[...]
Also there is one more idea I had how to record number of pins in the page:
#define
PAGE_PIN_BIAS1024
get_page_pin()atomic_add(&page->_refcount, PAGE_PIN_BIAS);put_page_pin(); atomic_add(&page->_refcount, -PAGE_PIN_BIAS);
page_pinned(page)(atomic_read(&page->_refcount) - page_mapcount(page)) > PAGE_PIN_BIASQuoted 8 lines
Quoted 8 lines
Quoted 8 lines
This is pretty trivial scheme. It still gives us 22-bits for page pins which should be plenty (but we should check for that and bail with error if it would overflow). Also there will be no false negatives and false positives only if there are more than 1024 non-page-table references to the page which I expect to be rare (we might want to also subtract
hpage_nr_pages()for radix tree references to avoid excessive false positives for huge pages although at this point I don't think they would matter). Thoughts?Some details, sorry I'm not fully grasping your plan without more explanation:
Do I read it correctly that this uses the lower 10 bits for the original
page->_refcount, and the upper 22 bits for gup-pinned counts? If so, I'm surprised, because gup-pinned is going to be less than or equal to the normal (get_page-based) pin count. And 1024 seems like it might be reached in a large system with lots of processes and IPC.
Quoted 3 lines
Are you just allowing the lower 10 bits to overflow, and that's why the subtraction of mapcount? Wouldn't it be better to allow more than 10 bits, instead?
I'm not really dividing the page->_refcount counter, that's a wrong way how to think about it I believe. Normal get_page() simply increments the _refcount by 1, get_page_pin() will increment by 1024 (or 999 or whatever - that's PAGE_PIN_BIAS). The choice of value of PAGE_PIN_BIAS is essentially a tradeoff between how many page pins you allow and how likely page_pinned() is to return false positive. Large PAGE_PIN_BIAS means lower amount of false positives but also less page pins allowed for the page before _refcount would overflow.
Now the trick with subtracting of page_mapcount() is following: We know that certain places hold references to the page. Common holders of page references are page table entries. So if we subtract page_mapcount() from _refcount, we'll get more accurate view how many other references (including pins) are there and thus reduce the number of false positives.
Quoted 6 lines
Another question: do we just allow other kernel code to observe this biased
_refcount, or do we attempt to filter it out? In other words, do you expect problems due to some kernel code checking the_refcountand finding a large number there, when it expected, say, 3? I recall some code tries to do that...in fact,ZONE_DEVICEis 1-based, instead of zero-based, with respect to_refcount, right?
I would just allow other places to observe biased refcount. Sure there are places that do comparions on exact refcount value but if such place does not exclude page pins, it cannot really depend on whether there's just one or thousand of them. Generally such places try to detect whether they are the only owner of the page (besides page cache radix tree, LRU, etc.). So they want to bail if any page pin exists and that check remains the same regardless whether we increment _refcount by 1 or by 1024 when pinning the page.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to John HubbardQuoted 42 lines · John Hubbard
Quoted 33 lines · Jerome Glisse
Quoted 16 lines · John Hubbard
Quoted 8 lines · Jerome Glisse
Quoted 6 lines · John Hubbard
Quoted 5 lines · Jerome Glisse
Quoted 4 lines · Jan Kara
Quoted 3 lines · Jerome Glisse
Quoted 2 lines · Jan Kara
Quoted 1 line · Jerome Glisse
On Tue, Dec 18, 2018 at 03:29:34PM -0800, John Hubbard wrote:
[...]
Hi Jerome,
Looks good, in a conceptual sense. Let me do a brain dump of how I see it, in case anyone spots a disastrous conceptual error (such as the
lock_pagepoint), while I'm putting together the revised patchset.Quoted 5 lines
I've studied this carefully, and I agree that using mapcount in this way is viable, *as long* as we use a lock (or a construct that looks just like one: your "memory barrier, check, retry" is really just a lock) in order to hold off
gup()whilepage_mkclean()is in progress. In other words, nothing that increments mapcount may proceed whilepage_mkclean()is running.No, increment to
page->_mapcountare fine whilepage_mkclean()is running. The above solution do work no matter what happens thanks to the memory barrier. By clearing the pin flag first and reading thepage->_mapcountafter (and doing the reverse in GUP) we know that a racing GUP will either have its pin page clear but the incremented mapcount taken into account bypage_mkclean()orpage_mkclean()will miss the incremented mapcount but it will also no clear the pin flag set concurrently by any GUP.Here are all the possible time line:
[T1]:Quoted 7 lines
GUP on CPU0 | page_mkclean() on CPU1 | [G2] atomic_inc(&page->mapcount) | [G3] smp_wmb(); | [G4] SetPagePin(page); | ... | [C1] pined = TestClearPagePin(page);
Quoted 5 lines
It appears that you're using the "page pin is clear" to indicate that
page_mkclean()is running. The problem is, that approach leads to toggling the PagePin flag, and so an observer (other than gup orpage_mkclean) will see intervals during which the PagePin flag is clear, when conceptually it should be set.
Also forgot to stress that i am not using the pin flag to report page_mkclean is running, i am clearing it first because clearing that bit is the thing that is racy. If we clear it first and then read map and pin count and then count number of real mapping we get a proper ordering and we will always detect pined page and properly restore the pin flag at the end of page_mkclean.
In fact GUP or PUP never need to check if the flag is clear. The check in GUP in my pseudo code is an optimization for the write back ordering (no need to do any ordering if the pin flag was already set before the current GUP).
Cheers,
Jérôme
Jan Kara
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to Dave ChinnerQuoted 72 lines · Dave Chinner
Quoted 56 lines · Jan Kara
Hi!
Quoted 25 lines · Dave Chinner
Quoted 10 lines · Jerome Glisse
Quoted 5 lines · Jan Kara
On Fri 07-12-18 21:24:46, Jerome Glisse wrote:
So this approach doesn't look like a win to me over using counter in struct page and I'd rather try looking into squeezing HMM public page usage of struct page so that we can fit that gup counter there as well. I know that it may be easier said than done...So i want back to the drawing board and first i would like to ascertain that we all agree on what the objectives are:
Quoted 7 lines
[O1] Avoid write back from a page still being written by either a
device or some direct I/O or any other existing user of GUP. This would avoid possible file system corruption.[O2] Avoid crash when
set_page_dirty()is call on a page that is
considered clean by core mm (buffer head have been remove and with some file system this turns into an ugly mess).I think that's wrong. This isn't an "avoid a crash" case, this is a "prevent data and/or filesystem corruption" case. The primary goal we have here is removing our exposure to potential corruption, which has the secondary effect of avoiding the crash/panics that currently occur as a result of inconsistent page/filesystem state.
Quoted 5 lines
i.e. The goal is to have ->page_mkwrite() called on the clean page /before/ the file-backed page is marked dirty, and hence we don't expose ourselves to potential corruption or crashes that are a result of inappropriately calling
set_page_dirty()on clean file-backed pages.I agree that [O1] - i.e., avoid corrupting fs data - is more important and [O2] is just one consequence of [O1].
Quoted 15 lines
Quoted 5 lines
For [O1] and [O2] i believe a solution with mapcount would work. So no new struct, no fake vma, nothing like that. In GUP for file back pages we increment both refcount and mapcount (we also need a special
put_user_pageto decrement mapcount when GUP user are done with the page).I don't see how a mapcount can prevent anyone from calling
set_page_dirty()inappropriately.Quoted 5 lines
Now for [O1] the write back have to call
page_mkclean()to go through all reverse mapping of the page and map read only. This means that we can count the number of real mapping and see if the mapcount is bigger than that. If mapcount is bigger than page is pin and we need to use a bounce page to do the writeback.Quoted 3 lines
Doesn't work. Generally filesystems have already mapped the page into bios before they call
clear_page_dirty_for_io(), so it's too late for the filesystem to bounce the page at that point.Quoted 6 lines
Yes, for filesystem it is too late. But the plan we figured back in October was to do the bouncing in the block layer. I.e., mark the bio (or just the particular page) as needing bouncing and then use the existing page bouncing mechanism in the block layer to do the bouncing for us. Ext3 (when it was still a separate fs driver) has been using a mechanism like this to make DIF/DIX work with its metadata.
Sure, that's a possibility, but that doesn't close off any race conditions because there can be DMA into the page in progress while the page is being bounced, right? AFAICT this ext3+DIF/DIX case is different in that there is no 3rd-party access to the page while it is under IO (ext3 arbitrates all access to it's metadata), and so nothing can actually race for modification of the page between submission and bouncing at the block layer.
Quoted 6 lines
In this case, the moment the page is unlocked, anyone else can map it and start (R)DMA on it, and that can happen before the bio is bounced by the block layer. So AFAICT, block layer bouncing doesn't solve the problem of racing writeback and DMA direct to the page we are doing IO on. Yes, it reduces the race window substantially, but it doesn't get rid of it.
The scenario you describe here cannot happen exactly because of the wait_for_stable_page() in ->page_mkwrite() you mention below. If someone will try to GUP a page that is under writeback (has already PageWriteback set), GUP will have to do a write fault because the page is writeprotected in page tables and go into ->page_mkwrite() which will wait.
The problem rather is with someone mapping the page *before* writeback starts, giving the page to HW. Then clear_page_dirty_for_io() writeprotects the page in PTEs but the HW gives a damn about that. Then, after we add the page to the bio but before the page gets bounced by the block layer, the HW can still modify it.
So for anything in the block layer and below, doing the bouncing in the block layer is enough. So DIF/DIX will work, device mapper targets will work etc. But you are right that if the filesystem itself needs stable data, then bouncing in the block layer is not enough. Thanks for catching this hole, I didn't think of that. Ext4 does not need stable data during writeback, btrfs probably does as it needs to compute data checksums during writeout. So we'll probably need something like clear_page_dirty_for_io() returning a bounced page for data integrity writeback of a pinned page. And the filesystem would then need to submit this page for IO.
Quoted 3 lines
/me points to
wait_for_stable_page()in ->page_mkwrite as the mechanism we already have to avoid races between dirtying mapped pages and page writeback....
Yes, the problem is that some RDMA hardware does not (and AFAIU cannot be made to) follow the wait_for_stable_page() rules. And we have a userspace visible API by which application can ask the kernel to give mapped file pages to the HW as a buffer and the HW can fill in the contents at its will until userspace tells the kernel to remove these mapped pages from the HW. The interval how long userspace leaves these pages to the HW is upto userspace and in practice it is often counted in hours.
If you want to tell me this is broken and could always corrupt data and kill the kernel and what not, I agree. The goal is to avoid at least the worst issues without breaking userspace too badly.
Quoted 20 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
For [O2] i believe we can handle that case in the
put_user_page()function to properly dirty the page without causing filesystem freak out.I'm pretty sure you can't call ->page_mkwrite() from
put_user_page(), so I don't think this is workable at all.Quoted 12 lines
Yes, calling ->page_mkwrite() in
put_user_page()is not only technically complicated but also too late - DMA has already modified page contents. What we planned to do (again discussed back in October) was to never allow the pinned page to become clean. I.e.,clear_page_dirty_for_io()would leave pinned pages dirty. Also we would skip pinned pages forWB_SYNC_NONEwriteback as there's no point in that really. That way MM and filesystems would be aware of the real page state - i.e., what's in memory is not in sync (potentially) with what's on disk. I was thinking whether this permanently-dirty state couldn't confuse filesystem in some way but I didn't find anything serious - the worst I could think of are places that dofilemap_write_and_wait()and then invalidate page cache e.g. before hole punching or extent shifting.
Quoted 5 lines
If it's permanently dirty, how do we trigger new COW operations after writeback has "cleaned" the page? i.e. we still need a ->page_mkwrite call to run before we allow the next write to the page to be done, regardless of whether the page is "permanently dirty" or not....
Interaction with COW is certainly an interesting problem. When the page gets pinned, GUP will make sure the page is writeably mapped and trigger a write fault if not. So at the moment the page is pinned, we are sure the page is COWed. Now the question is what should happen when the file A containing this pinned page gets reflinked to file B while the page is still pinned.
Options I can see are:
1) Fail the reflink.
- difficult for sysadmin to discover the source of failure
2) Block reflink until the pin of the page is released.
- can last for a long time, again difficult to discover
3) Don't do anything special.
- can corrupt data as read accesses through file B won't see modifications done to the page (and thus eventually the corresponding disk block) by the HW.
4) Immediately COW the block during reflink when the corresponding page cache page is pinned.
- seems as the best solution at this point, although sadly also requires the most per-filesystem work
Quoted 7 lines
Quoted 4 lines
But these should work fine as is (page cache
invalidation will just happily truncate dirty pages). DIO might get confused by the inability to invalidate dirty pages but then user combining RDMA with DIO on the same file at one moment gets what he deserves...I'm almost certain this will do something that will occur. i.e. permanently mapped RDMA file, filesystem backup program uses DIO....
IMO this falls into the same cathegory like two independent processes doing DIO overwrite of the same file block. Or combining mmap and DIO access. Kernel must not crash, filesystem must stay consistent, but the user is responsible to put together bits of the data that are left...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 80 lines · Jan Kara
Quoted 72 lines · Dave Chinner
Quoted 56 lines · Jan Kara
Hi!
Quoted 25 lines · Dave Chinner
Quoted 10 lines · Jerome Glisse
Quoted 5 lines · Jan Kara
On Fri 07-12-18 21:24:46, Jerome Glisse wrote:
So this approach doesn't look like a win to me over using counter in struct page and I'd rather try looking into squeezing HMM public page usage of struct page so that we can fit that gup counter there as well. I know that it may be easier said than done...So i want back to the drawing board and first i would like to ascertain that we all agree on what the objectives are:
Quoted 7 lines
[O1] Avoid write back from a page still being written by either a
device or some direct I/O or any other existing user of GUP. This would avoid possible file system corruption.[O2] Avoid crash when
set_page_dirty()is call on a page that is
considered clean by core mm (buffer head have been remove and with some file system this turns into an ugly mess).I think that's wrong. This isn't an "avoid a crash" case, this is a "prevent data and/or filesystem corruption" case. The primary goal we have here is removing our exposure to potential corruption, which has the secondary effect of avoiding the crash/panics that currently occur as a result of inconsistent page/filesystem state.
Quoted 5 lines
i.e. The goal is to have ->page_mkwrite() called on the clean page /before/ the file-backed page is marked dirty, and hence we don't expose ourselves to potential corruption or crashes that are a result of inappropriately calling
set_page_dirty()on clean file-backed pages.I agree that [O1] - i.e., avoid corrupting fs data - is more important and [O2] is just one consequence of [O1].
Quoted 15 lines
Quoted 5 lines
For [O1] and [O2] i believe a solution with mapcount would work. So no new struct, no fake vma, nothing like that. In GUP for file back pages we increment both refcount and mapcount (we also need a special
put_user_pageto decrement mapcount when GUP user are done with the page).I don't see how a mapcount can prevent anyone from calling
set_page_dirty()inappropriately.Quoted 5 lines
Now for [O1] the write back have to call
page_mkclean()to go through all reverse mapping of the page and map read only. This means that we can count the number of real mapping and see if the mapcount is bigger than that. If mapcount is bigger than page is pin and we need to use a bounce page to do the writeback.Quoted 3 lines
Doesn't work. Generally filesystems have already mapped the page into bios before they call
clear_page_dirty_for_io(), so it's too late for the filesystem to bounce the page at that point.Quoted 6 lines
Yes, for filesystem it is too late. But the plan we figured back in October was to do the bouncing in the block layer. I.e., mark the bio (or just the particular page) as needing bouncing and then use the existing page bouncing mechanism in the block layer to do the bouncing for us. Ext3 (when it was still a separate fs driver) has been using a mechanism like this to make DIF/DIX work with its metadata.
Sure, that's a possibility, but that doesn't close off any race conditions because there can be DMA into the page in progress while the page is being bounced, right? AFAICT this ext3+DIF/DIX case is different in that there is no 3rd-party access to the page while it is under IO (ext3 arbitrates all access to it's metadata), and so nothing can actually race for modification of the page between submission and bouncing at the block layer.
Quoted 6 lines
In this case, the moment the page is unlocked, anyone else can map it and start (R)DMA on it, and that can happen before the bio is bounced by the block layer. So AFAICT, block layer bouncing doesn't solve the problem of racing writeback and DMA direct to the page we are doing IO on. Yes, it reduces the race window substantially, but it doesn't get rid of it.
Quoted 2 lines
The scenario you describe here cannot happen exactly because of the
wait_for_stable_page()in ->page_mkwrite() you mention below.
In general, no, because stable pages are controlled by block devices.
void wait_for_stable_page(struct page *page)
{
if (bdi_cap_stable_pages_required(inode_to_bdi(page->mapping->host)))
wait_on_page_writeback(page);
}I have previously advocated for the filesystem to be in control of stable pages but, well, too many people shouted "but performance!" and so we still have all these holes I wanted to close in our code...
Quoted 4 lines
If someone
will try to GUP a page that is under writeback (has already PageWriteback set), GUP will have to do a write fault because the page is writeprotected in page tables and go into ->page_mkwrite() which will wait.
Correct, but that doesn't close the problem down because stable pages are something we cannot rely on right now. We need to fix wait_for_stable_page() to always block on page writeback before this specific race condition goes away.
Quoted 5 lines
The problem rather is with someone mapping the page *before* writeback starts, giving the page to HW. Then
clear_page_dirty_for_io()writeprotects the page in PTEs but the HW gives a damn about that. Then, after we add the page to the bio but before the page gets bounced by the block layer, the HW can still modify it.
Sure, that's yet another aspect of the same problem - not getting a write fault when the page is being written to. If we got a write fault, then the wait_for_stable_page() call in ->page_mkwrite would then solve the problem.
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force wait_for_stable_page() to trigger when there are GUP mappings if the underlying storage doesn't already require it.
Quoted 14 lines
So for anything in the block layer and below, doing the bouncing in the block layer is enough. So DIF/DIX will work, device mapper targets will work etc. But you are right that if the filesystem itself needs stable data, then bouncing in the block layer is not enough. Thanks for catching this hole, I didn't think of that. Ext4 does not need stable data during writeback, btrfs probably does as it needs to compute data checksums during writeout. So we'll probably need something like
clear_page_dirty_for_io()returning a bounced page for data integrity writeback of a pinned page. And the filesystem would then need to submit this page for IO.Quoted 3 lines
/me points to
wait_for_stable_page()in ->page_mkwrite as the mechanism we already have to avoid races between dirtying mapped pages and page writeback....
Quoted 7 lines
Yes, the problem is that some RDMA hardware does not (and AFAIU cannot be made to) follow the
wait_for_stable_page()rules. And we have a userspace visible API by which application can ask the kernel to give mapped file pages to the HW as a buffer and the HW can fill in the contents at its will until userspace tells the kernel to remove these mapped pages from the HW. The interval how long userspace leaves these pages to the HW is upto userspace and in practice it is often counted in hours.
Yes, and as we've discussed in previous threads and at LSFMM, that needs to die and be replaced with file leases that allow the kernel to revoke the GUP mappings by breaking the lease.
Quoted 3 lines
If you want to tell me this is broken and could always corrupt data and kill the kernel and what not, I agree. The goal is to avoid at least the worst issues without breaking userspace too badly.
My goal is to create infrastructure that is sane, reliable, verifiable and doesn't have gaping data integrity holes in it that performs as well as the current nasty kernel bypass hacks we have now. i.e. I don't care about the current mess as it's largely unfixable - we're at the point where we need to fix the architecture, not keep trying to slap band-aids over the worst of the symptoms of the current mess.
Quoted 26 lines
Quoted 5 lines
If it's permanently dirty, how do we trigger new COW operations after writeback has "cleaned" the page? i.e. we still need a ->page_mkwrite call to run before we allow the next write to the page to be done, regardless of whether the page is "permanently dirty" or not....
Interaction with COW is certainly an interesting problem. When the page gets pinned, GUP will make sure the page is writeably mapped and trigger a write fault if not. So at the moment the page is pinned, we are sure the page is COWed. Now the question is what should happen when the file A containing this pinned page gets reflinked to file B while the page is still pinned.
Options I can see are:
1) Fail the reflink.
- difficult for sysadmin to discover the source of failure2) Block reflink until the pin of the page is released.
- can last for a long time, again difficult to discover3) Don't do anything special.
- can corrupt data as read accesses through file B won't see modifications done to the page (and thus eventually the corresponding disk block) by the HW.
Quoted 4 lines
4) Immediately COW the block during reflink when the corresponding page cache page is pinned.
- seems as the best solution at this point, although sadly also requires the most per-filesystem work
None of the above are acceptable solutions - they all have nasty corner cases which are going to be difficult to get right, test, etc. IMO, the robust, reliable, testable solution is this:
5) The reflink breaks the file lease, the userspace app releases the pinned pages on the file and drops the lease. The reflink proceeds, does it's work, and then the app gets a new lease on the file. When the app pins the pages again, it triggers new ->page_mkwrite calls to break any sharing that the reflink created. And if the app fails to drop the lease, then we can either fail with a lease related error or kill it....
XFS already has all the file lease breaking stuff in it, we just need userspace interfaces to expose them and userspace apps to start using them.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 5 lines · Dave Chinner
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force
wait_for_stable_page()to trigger when there are GUP mappings if the underlying storage doesn't already require it.
If you want to ban O_DIRECT/etc from writing to file backed pages, then just do it.
Otherwise I'm not sure demanding some unrealistic HW design is reasonable. ie nvme drives are not likely to add page faulting to their IO path any time soon.
A SW architecture that relies on page faulting is just not going to support real world block IO devices.
GPUs and one RDMA are about the only things that can do this today, and they are basically irrelevant to O_DIRECT.
Jason
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 16 lines · Jason Gunthorpe
Quoted 5 lines · Dave Chinner
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force
wait_for_stable_page()to trigger when there are GUP mappings if the underlying storage doesn't already require it.If you want to ban O_DIRECT/etc from writing to file backed pages, then just do it.
Otherwise I'm not sure demanding some unrealistic HW design is reasonable. ie nvme drives are not likely to add page faulting to their IO path any time soon.
Quoted 5 lines
A SW architecture that relies on page faulting is just not going to support real world block IO devices.
GPUs and one RDMA are about the only things that can do this today, and they are basically irrelevant to
O_DIRECT.
Yes.
I'm missing why a bounce buffer is needed. If writeback hits a DMA-writable page why can't that path just turn around and trigger another mkwrite notifcation on behalf of hardware that will never send it? "Nice try writeback, this page is dirty again".
Jan Kara
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 23 lines · Dan Williams
Quoted 16 lines · Jason Gunthorpe
Quoted 5 lines · Dave Chinner
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force
wait_for_stable_page()to trigger when there are GUP mappings if the underlying storage doesn't already require it.If you want to ban O_DIRECT/etc from writing to file backed pages, then just do it.
Otherwise I'm not sure demanding some unrealistic HW design is reasonable. ie nvme drives are not likely to add page faulting to their IO path any time soon.
Quoted 5 lines
A SW architecture that relies on page faulting is just not going to support real world block IO devices.
GPUs and one RDMA are about the only things that can do this today, and they are basically irrelevant to
O_DIRECT.
Quoted 6 lines
Yes.
I'm missing why a bounce buffer is needed. If writeback hits a DMA-writable page why can't that path just turn around and trigger another mkwrite notifcation on behalf of hardware that will never send it? "Nice try writeback, this page is dirty again".
You are conflating two things here. Bounce buffer (or a way to stop DMA from happening) is needed because think what happens when RAID5 computes its stripe checksum while someone modifies the data through DMA. Checksum mismatch and all fun arising from that.
Notifying filesystem about the fact that the page didn't get cleaned by the writeback and still can be modified by the DMA is a different thing.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to Jason GunthorpeQuoted 10 lines · Jason Gunthorpe
Quoted 5 lines · Dave Chinner
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force
wait_for_stable_page()to trigger when there are GUP mappings if the underlying storage doesn't already require it.If you want to ban O_DIRECT/etc from writing to file backed pages, then just do it.
O_DIRECT IO *isn't the problem*.
iO_DIRECT IO uses a short term pin that the existing prefaulting during GUP works just fine for. The problem we have is the long term pins where pages can be cleaned while the pages are pinned. i.e. the use case we current have to disable for DAX because *we can't make it work sanely* without either revokable file leases and/or hardware that is able to trigger page faults when they need write access to a clean page.
Quoted 3 lines
Otherwise I'm not sure demanding some unrealistic HW design is reasonable. ie nvme drives are not likely to add page faulting to their IO path any time soon.
Direct IO on nvme drives are not the problem. It's RDMA pinning pages for hours or days and expecting everyone else to jump through hoops to support their broken page access access model.
Quoted 2 lines
A SW architecture that relies on page faulting is just not going to support real world block IO devices.
The existing software architecture for file backed pages has been based around page faulting for write notifications since ~2005. That horse bolted many, many years ago.
Quoted 2 lines
GPUs and one RDMA are about the only things that can do this today, and they are basically irrelevant to
O_DIRECT.
It's RDMA that we need these changes for, not O_DIRECT.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Jan Kara
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 9 lines · Dave Chinner
Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Dave Chinner
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force
wait_for_stable_page()to trigger when there are GUP mappings if the underlying storage doesn't already require it.
Quoted 4 lines
Quoted 2 lines
If you want to ban O_DIRECT/etc from writing to file backed pages, then just do it.
O_DIRECTIO *isn't the problem*.
That is not true. O_DIRECT IO is a problem. In some aspects it is easier than the problem with RDMA but currently O_DIRECT IO can crash your machine or corrupt data the same way RDMA can. Just the race window is much smaller. So we have to fix the generic GUP infrastructure to make O_DIRECT IO work. I agree that fixing RDMA will likely require even more work like revokable leases or what not.
Quoted 7 lines
iO_DIRECTIO uses a short term pin that the existing prefaulting during GUP works just fine for. The problem we have is the long term pins where pages can be cleaned while the pages are pinned. i.e. the use case we current have to disable for DAX because *we can't make it work sanely* without either revokable file leases and/or hardware that is able to trigger page faults when they need write access to a clean page.
I would like to find a solution to the O_DIRECT IO problem while making the infractructure reusable also for solving the problems with RDMA... Because nobody wants to go through those couple hundred get_user_pages() users in the kernel twice...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 15 lines · Jan Kara
Quoted 9 lines · Dave Chinner
Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Dave Chinner
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force
wait_for_stable_page()to trigger when there are GUP mappings if the underlying storage doesn't already require it.Quoted 4 lines
Quoted 2 lines
If you want to ban O_DIRECT/etc from writing to file backed pages, then just do it.
O_DIRECTIO *isn't the problem*.
Quoted 7 lines
That is not true.
O_DIRECTIO is a problem. In some aspects it is easier than the problem with RDMA but currentlyO_DIRECTIO can crash your machine or corrupt data the same way RDMA can. Just the race window is much smaller. So we have to fix the generic GUP infrastructure to makeO_DIRECTIO work. I agree that fixing RDMA will likely require even more work like revokable leases or what not.
This is what I've understood, talking to all the experts. Dave? Why do you think O_DIRECT is actually OK?
I agree the duration issue with RDMA is different, but don't forget, O_DIRECT goes out to the network too and has potentially very long timeouts as well.
If O_DIRECT works fine then lets use the same approach in RDMA??
Jason
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to Jan KaraQuoted 10 lines · Jan Kara
Quoted 9 lines · Dave Chinner
Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Dave Chinner
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force
wait_for_stable_page()to trigger when there are GUP mappings if the underlying storage doesn't already require it.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
If you want to ban O_DIRECT/etc from writing to file backed pages, then just do it.
O_DIRECTIO *isn't the problem*.That is not true.
O_DIRECTIO is a problem. In some aspects it is easier than the problem with RDMA but currentlyO_DIRECTIO can crash your machine or corrupt data the same way RDMA can.
It's not O_DIRECT - it's a ""transient page pin". Yes, there are problems with that right now, but as we've discussed the issues can be avoided by:
a) stable pages always blocking in ->page_mkwrite;
b) blocking in write_cache_pages() on an elevated map count when WB_SYNC_ALL is set; and
c) blocking in truncate_pagecache() on an elevated map count.
That prevents:
a) gup pinning a page that is currently under writeback and modifying it while IO is in flight;
b) a dirty page being written back while it is pinned by GUP, thereby turning it clean before the gup reference calls set_page_dirty() on DMA completion; and
c) truncate/hole punch for pulling the page out from under the gup operation that is ongoing.
This is an adequate solution for a short term transient pins. It doesn't break fsync(), it doesn't change how truncate works and it fixes the problem where a mapped file is the buffer for an O_DIRECT IO rather than the open fd and that buffer file gets truncated. IOWs, transient pins (and hence O_DIRECT) is not really the problem here.
The problem with this is that blocking on elevated map count does not work for long term pins (i.e. gup_longterm()) which are defined as:
* "longterm" == userspace controlled elevated page count lifetime.
* Contrast this to iov_iter_get_pages() usages which are transient.
It's the "userspace controlled" part of the long term gup pin that is the problem we need to solve. If we treat them the same as a transient pin, then this leads to fsync() and truncate either blocking for a long time waiting for userspace to drop it's gup reference, or having to be failed with something like EBUSY or EAGAIN.
This is the problem revokable file layout leases solve. The NFS server is already using this for revoking delegations from remote clients. Userspace holding long term GUP references is essentially the same thing - it's a delegation of file ownership to userspace that the filesystem must be able to revoke when it needs to run internal and/or 3rd-party requested operations on that delegated file.
If the hardware supports page faults, then we can further optimise the long term pin case to relax stable page requirements and allow page cleaning to occur while there are long term pins. In this case, the hardware will write-fault the clean pages appropriately before DMA is initiated, and hence avoid the need for data integrity operations like fsync() to trigger lease revocation. However, truncate/hole punch still requires lease revocation to work sanely, especially when we consider DAX *must* ensure there are no remaining references to the physical pmem page after the space has been freed.
i.e. conflating the transient and long term gup pins as the same problem doesn't help anyone. If we fix the short term pin problems, then the long term pin problem become tractable by adding a layer over the top (i.e. hardware page fault capability and/or file lease requirements). Existing apps and hardware will continue to work - external operations on the pinned file will simply hang rather than causing corruption or kernel crashes. New (or updated) applications will play nicely with lease revocation and at that point the "long term pin" basically becomes a transient pin where the unpin latency is determined by how quickly the app responds to the lease revocation. And page fault capable hardware will reduce the occurrence of lease revocations due to data writeback/integrity operations and behave almost identically to cpu-based mmap accesses to file backed pages.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Jan Kara
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 30 lines · Dave Chinner
Quoted 10 lines · Jan Kara
Quoted 9 lines · Dave Chinner
Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Dave Chinner
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force
wait_for_stable_page()to trigger when there are GUP mappings if the underlying storage doesn't already require it.Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
If you want to ban O_DIRECT/etc from writing to file backed pages, then just do it.
O_DIRECTIO *isn't the problem*.That is not true.
O_DIRECTIO is a problem. In some aspects it is easier than the problem with RDMA but currentlyO_DIRECTIO can crash your machine or corrupt data the same way RDMA can.It's not
O_DIRECT- it's a ""transient page pin". Yes, there are problems with that right now, but as we've discussed the issues can be avoided by:a) stable pages always blocking in ->page_mkwrite;
b) blocking inwrite_cache_pages()on an elevated map count whenWB_SYNC_ALLis set; and
c) blocking intruncate_pagecache()on an elevated map count.
Quoted 6 lines
That prevents:
a) gup pinning a page that is currently under writeback and modifying it while IO is in flight;
b) a dirty page being written back while it is pinned by GUP, thereby turning it clean before the gup reference callsset_page_dirty()on DMA completion; and
This is not prevented by what you wrote above as currently GUP does not increase page->_mapcount. Currently, there's no way to distinguish GUP page reference from any other page reference - GUP simply does get_page() - and big part of this thread as I see it is exactly about how to introduce this distinction and how to convert all GUP users to the new convention safely (as currently they just pass struct page * pointers around and eventually do put_page() on them). Increasing page->_mapcount in GUP and trying to deduce the pin count from that is one option Jerome suggested. At this point I'm not 100% sure this is going to work but we'll see.
Quoted 9 lines
c) truncate/hole punch for pulling the page out from under the gup operation that is ongoing.
This is an adequate solution for a short term transient pins. It doesn't break
fsync(), it doesn't change how truncate works and it fixes the problem where a mapped file is the buffer for anO_DIRECTIO rather than the open fd and that buffer file gets truncated. IOWs, transient pins (and henceO_DIRECT) is not really the problem here.
For now let's assume that the mechanism how to detect page pinned by GUP is actually somehow solved and we have already page_pinned() implemented. Then what you suggest can actually create a deadlock AFAICS:
Process 1: Process 2:
fsync("file")/* Evil memory buffer with page order reversed */
addr1 = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED, "file", 4096); addr2 = mmap(addr1+4096, 4096, PROT_WRITE, MAP_SHARED, "file", 0);
/* Fault in pages */
*addr1 = 0;
*addr2 = 0;
adds page with index 0
to bio
fd = open("file2", O_RDWR | O_DIRECT);read(fd, addr1, 8192)
-> eventually gets to iov_iter_get_pages() and then to
get_user_pages_fast().
-> pins "file" page with index 1
blocks on pin for
page with index 1
-> blocks in PageWriteback for page with index 0Possibility of deadlocks like this is why I've decided it will be easier to just bounce the page for writeback we cannot avoid rather than block the writeback...
Quoted 7 lines
The problem with this is that blocking on elevated map count does not work for long term pins (i.e.
gup_longterm()) which are defined as:* "longterm" == userspace controlled elevated page count lifetime.
* Contrast this toiov_iter_get_pages()usages which are transient.
Quoted 6 lines
It's the "userspace controlled" part of the long term gup pin that is the problem we need to solve. If we treat them the same as a transient pin, then this leads to
fsync()and truncate either blocking for a long time waiting for userspace to drop it's gup reference, or having to be failed with something like EBUSY or EAGAIN.
I agree. "userspace controlled" pins are another big problem to solve.
Quoted 18 lines
This is the problem revokable file layout leases solve. The NFS server is already using this for revoking delegations from remote clients. Userspace holding long term GUP references is essentially the same thing - it's a delegation of file ownership to userspace that the filesystem must be able to revoke when it needs to run internal and/or 3rd-party requested operations on that delegated file.
If the hardware supports page faults, then we can further optimise the long term pin case to relax stable page requirements and allow page cleaning to occur while there are long term pins. In this case, the hardware will write-fault the clean pages appropriately before DMA is initiated, and hence avoid the need for data integrity operations like
fsync()to trigger lease revocation. However, truncate/hole punch still requires lease revocation to work sanely, especially when we consider DAX *must* ensure there are no remaining references to the physical pmem page after the space has been freed.
Quoted 14 lines
i.e. conflating the transient and long term gup pins as the same problem doesn't help anyone. If we fix the short term pin problems, then the long term pin problem become tractable by adding a layer over the top (i.e. hardware page fault capability and/or file lease requirements). Existing apps and hardware will continue to work - external operations on the pinned file will simply hang rather than causing corruption or kernel crashes. New (or updated) applications will play nicely with lease revocation and at that point the "long term pin" basically becomes a transient pin where the unpin latency is determined by how quickly the app responds to the lease revocation. And page fault capable hardware will reduce the occurrence of lease revocations due to data writeback/integrity operations and behave almost identically to cpu-based mmap accesses to file backed pages.
Agreed. I think we are on the same page wrt this. Just at this point I'm trying to solve the "transient pin" problem...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to Dave ChinnerQuoted 68 lines · Dave Chinner
Quoted 10 lines · Jan Kara
Quoted 9 lines · Dave Chinner
Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Dave Chinner
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force
wait_for_stable_page()to trigger when there are GUP mappings if the underlying storage doesn't already require it.Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
If you want to ban O_DIRECT/etc from writing to file backed pages, then just do it.
O_DIRECTIO *isn't the problem*.That is not true.
O_DIRECTIO is a problem. In some aspects it is easier than the problem with RDMA but currentlyO_DIRECTIO can crash your machine or corrupt data the same way RDMA can.It's not
O_DIRECT- it's a ""transient page pin". Yes, there are problems with that right now, but as we've discussed the issues can be avoided by:a) stable pages always blocking in ->page_mkwrite;
b) blocking inwrite_cache_pages()on an elevated map count whenWB_SYNC_ALLis set; and
c) blocking intruncate_pagecache()on an elevated map count.That prevents:
a) gup pinning a page that is currently under writeback and modifying it while IO is in flight;
b) a dirty page being written back while it is pinned by GUP, thereby turning it clean before the gup reference callsset_page_dirty()on DMA completion; and
c) truncate/hole punch for pulling the page out from under the gup operation that is ongoing.This is an adequate solution for a short term transient pins. It doesn't break
fsync(), it doesn't change how truncate works and it fixes the problem where a mapped file is the buffer for anO_DIRECTIO rather than the open fd and that buffer file gets truncated. IOWs, transient pins (and henceO_DIRECT) is not really the problem here.The problem with this is that blocking on elevated map count does not work for long term pins (i.e.
gup_longterm()) which are defined as:* "longterm" == userspace controlled elevated page count lifetime.
* Contrast this toiov_iter_get_pages()usages which are transient.It's the "userspace controlled" part of the long term gup pin that is the problem we need to solve. If we treat them the same as a transient pin, then this leads to
fsync()and truncate either blocking for a long time waiting for userspace to drop it's gup reference, or having to be failed with something like EBUSY or EAGAIN.This is the problem revokable file layout leases solve. The NFS server is already using this for revoking delegations from remote clients. Userspace holding long term GUP references is essentially the same thing - it's a delegation of file ownership to userspace that the filesystem must be able to revoke when it needs to run internal and/or 3rd-party requested operations on that delegated file.
Quoted 9 lines
If the hardware supports page faults, then we can further optimise the long term pin case to relax stable page requirements and allow page cleaning to occur while there are long term pins. In this case, the hardware will write-fault the clean pages appropriately before DMA is initiated, and hence avoid the need for data integrity operations like
fsync()to trigger lease revocation. However, truncate/hole punch still requires lease revocation to work sanely, especially when we consider DAX *must* ensure there are no remaining references to the physical pmem page after the space has been freed.
truncate does not requires lease recovations for faulting hardware, truncate will trigger a mmu notifier callback which will invalidate the hardware page table. On next access the hardware will fault and this will turn into a regular page fault from kernel point of view.
So truncate/reflink and all fs expectation for faulting hardware do hold. It is exactly as the CPU page table. So if CPU page table is properly updated then so will be the hardware one.
Note that such hardware also abive by munmap() so hardware mapping does not outlive vma.
Cheers,
J�r�me
Jan Kara
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to Dave ChinnerQuoted 48 lines · Dave Chinner
Quoted 24 lines · Jan Kara
Quoted 16 lines · Dave Chinner
Quoted 6 lines · Jan Kara
Yes, for filesystem it is too late. But the plan we figured back in October was to do the bouncing in the block layer. I.e., mark the bio (or just the particular page) as needing bouncing and then use the existing page bouncing mechanism in the block layer to do the bouncing for us. Ext3 (when it was still a separate fs driver) has been using a mechanism like this to make DIF/DIX work with its metadata.
Sure, that's a possibility, but that doesn't close off any race conditions because there can be DMA into the page in progress while the page is being bounced, right? AFAICT this ext3+DIF/DIX case is different in that there is no 3rd-party access to the page while it is under IO (ext3 arbitrates all access to it's metadata), and so nothing can actually race for modification of the page between submission and bouncing at the block layer.
Quoted 6 lines
In this case, the moment the page is unlocked, anyone else can map it and start (R)DMA on it, and that can happen before the bio is bounced by the block layer. So AFAICT, block layer bouncing doesn't solve the problem of racing writeback and DMA direct to the page we are doing IO on. Yes, it reduces the race window substantially, but it doesn't get rid of it.
Quoted 2 lines
The scenario you describe here cannot happen exactly because of the
wait_for_stable_page()in ->page_mkwrite() you mention below.In general, no, because stable pages are controlled by block devices.
void
wait_for_stable_page(struct page *page){ if (bdi_cap_stable_pages_required(inode_to_bdi(page->mapping->host))) wait_on_page_writeback(page); }I have previously advocated for the filesystem to be in control of stable pages but, well, too many people shouted "but performance!" and so we still have all these holes I wanted to close in our code...
Quoted 4 lines
If someone
will try to GUP a page that is under writeback (has already PageWriteback set), GUP will have to do a write fault because the page is writeprotected in page tables and go into ->page_mkwrite() which will wait.
Quoted 4 lines
Correct, but that doesn't close the problem down because stable pages are something we cannot rely on right now. We need to fix
wait_for_stable_page()to always block on page writeback before this specific race condition goes away.
Right, all I said was assuming that someone actually cares about stable pages so bdi_cap_stable_pages_required() is set. I agree with filesystem having ability to control whether stable pages are required or not. But when stable pages get enforced seems like a separate problem to me.
Quoted 11 lines
Quoted 5 lines
The problem rather is with someone mapping the page *before* writeback starts, giving the page to HW. Then
clear_page_dirty_for_io()writeprotects the page in PTEs but the HW gives a damn about that. Then, after we add the page to the bio but before the page gets bounced by the block layer, the HW can still modify it.Sure, that's yet another aspect of the same problem - not getting a write fault when the page is being written to. If we got a write fault, then the
wait_for_stable_page()call in ->page_mkwrite would then solve the problem.
Quoted 5 lines
Essentially, what we are talking about is how to handle broken hardware. I say we should just brun it with napalm and thermite (i.e. taint the kernel with "unsupportable hardware") and force
wait_for_stable_page()to trigger when there are GUP mappings if the underlying storage doesn't already require it.
As I wrote in other email, this is also about direct IO using file mapping as a data buffer. So burn with napalm can hardly be a complete solution... I agree that for the hardware that cannot support revoking of access / fault on access and uses long-term page pins, we may just have to put up with weird behavior in some corner cases.
Quoted 35 lines
Quoted 26 lines
Quoted 5 lines
If it's permanently dirty, how do we trigger new COW operations after writeback has "cleaned" the page? i.e. we still need a ->page_mkwrite call to run before we allow the next write to the page to be done, regardless of whether the page is "permanently dirty" or not....
Interaction with COW is certainly an interesting problem. When the page gets pinned, GUP will make sure the page is writeably mapped and trigger a write fault if not. So at the moment the page is pinned, we are sure the page is COWed. Now the question is what should happen when the file A containing this pinned page gets reflinked to file B while the page is still pinned.
Options I can see are:
1) Fail the reflink.
- difficult for sysadmin to discover the source of failure2) Block reflink until the pin of the page is released.
- can last for a long time, again difficult to discover3) Don't do anything special.
- can corrupt data as read accesses through file B won't see modifications done to the page (and thus eventually the corresponding disk block) by the HW.Quoted 4 lines
4) Immediately COW the block during reflink when the corresponding page cache page is pinned.
- seems as the best solution at this point, although sadly also requires the most per-filesystem workNone of the above are acceptable solutions - they all have nasty corner cases which are going to be difficult to get right, test, etc. IMO, the robust, reliable, testable solution is this:
Quoted 7 lines
5) The reflink breaks the file lease, the userspace app releases the pinned pages on the file and drops the lease. The reflink proceeds, does it's work, and then the app gets a new lease on the file. When the app pins the pages again, it triggers new ->page_mkwrite calls to break any sharing that the reflink created. And if the app fails to drop the lease, then we can either fail with a lease related error or kill it....
This is certainly fine for the GUP users that are going to support leases. But do you want GUP in direct IO to create a lease if the pages are from a file mapping? I belive we need another option at least for GUP references that are short-term in nature and sometimes also performance critical.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to John HubbardQuoted 3 lines · John Hubbard
I see. OK, HMM has done an efficient job of mopping up unused fields, and now we are completely out of space. At this point, after thinking about it carefully, it seems clear that it's time for a single, new field:
Sorry for not replying earlier; I'm travelling and have had trouble keeping on top of my mail.
Adding this field will grow struct page by 4-8 bytes, so it will no longer be 64 bytes. This isn't an acceptable answer.
We have a few options for bits. One is that we have (iirc) two bits available in page->flags on 32-bit. That'll force a few more configurations into using _last_cpupid and/or page_ext. I'm not a huge fan of this approach.
The second is to use page->lru.next bit 1. This requires some care because m68k allows misaligned pointers. If the list_head that it's joined to is misaligned, we'll be in trouble. This can get tricky because some pages are attached to list_heads which are on the stack ... and I don't think gcc guarantees __aligned attributes work for stack variables.
The third is to use page->lru.prev bit 0. We'd want to switch pgmap and hmm_data around to make this work, and we'd want to record this in mm_types.h so nobody tries to use a field which aliases with page->lru.prev and has bit 0 set on a page which can be mapped to userspace (which I currently believe to be true).
The fourth is to use a bit in page->flags for 64-bit and a bit in page_ext->flags for 32-bit. Or we could get rid of page_ext and grow struct page with a ->flags2 on 32-bit.
Fifth, it isn't clear to me how many bits might be left in ->_last_cpupid at this point, and perhaps there's scope for using a bit in there.
Quoted 28 lines
include/linux/mm_types.h +3
-0@@ -182,6 +182,9 @@ struct page {/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */ atomic_t _refcount; +/* DMA usage count. See get_user_pages*(), put_user_page*(). */ +atomic_t _dma_pinned_count; +#ifdef CONFIG_MEMCG struct mem_cgroup *mem_cgroup;#endif ...because after all, the reason this is so difficult is that this fix has to work in pretty much every configuration.
get_user_pages()use is widespread, it's a very general facility, and...it needs fixing. And we're out of space.I'm going to send out an updated RFC that shows the latest, and I think it's going to include the above.
Signature · 3 lines
thanks, John Hubbard NVIDIA
John Hubbard
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · John Hubbard
I see. OK, HMM has done an efficient job of mopping up unused fields, and now we are completely out of space. At this point, after thinking about it carefully, it seems clear that it's time for a single, new field:
Sorry for not replying earlier; I'm travelling and have had trouble keeping on top of my mail.
Hi Matthew,
Quoted 3 lines
Adding this field will grow struct page by 4-8 bytes, so it will no longer be 64 bytes. This isn't an acceptable answer.
I had to ask, though, just in case the historical rules might no longer be ask pressing. But OK.
Quoted 18 lines
We have a few options for bits. One is that we have (iirc) two bits available in
page->flagson 32-bit. That'll force a few more configurations into using_last_cpupidand/orpage_ext. I'm not a huge fan of this approach.The second is to use
page->lru.nextbit 1. This requires some care because m68k allows misaligned pointers. If thelist_headthat it's joined to is misaligned, we'll be in trouble. This can get tricky because some pages are attached tolist_headswhich are on the stack ... and I don't think gcc guarantees__alignedattributes work for stack variables.The third is to use
page->lru.prevbit 0. We'd want to switch pgmap andhmm_dataaround to make this work, and we'd want to record this inmm_types.hso nobody tries to use a field which aliases withpage->lru.prevand has bit 0 set on a page which can be mapped to userspace (which I currently believe to be true).
Quoted 7 lines
The fourth is to use a bit in
page->flagsfor 64-bit and a bit inpage_ext->flagsfor 32-bit. Or we could get rid ofpage_extand grow struct page with a ->flags2 on 32-bit.Fifth, it isn't clear to me how many bits might be left in ->_last_cpupid at this point, and perhaps there's scope for using a bit in there.
Thanks for taking the time to collect and explain all of this, I'm stashing it away as I'm sure it will come up again.
The latest approach to the gup/dma problem here might, or might not, actually need a single page bit. I'll know in a day or two.
Signature · 3 lines
thanks, John Hubbard NVIDIA
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to John HubbardQuoted 3 lines · John Hubbard
Quoted 1 line · Jerome Glisse
On Thu, Dec 06, 2018 at 06:45:49PM -0800, John Hubbard wrote:
[..]
Quoted 20 lines
I see. OK, HMM has done an efficient job of mopping up unused fields, and now we are completely out of space. At this point, after thinking about it carefully, it seems clear that it's time for a single, new field:
include/linux/mm_types.h +3
-0@@ -182,6 +182,9 @@ struct page {/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */ atomic_t _refcount; +/* DMA usage count. See get_user_pages*(), put_user_page*(). */ +atomic_t _dma_pinned_count; +#ifdef CONFIG_MEMCG struct mem_cgroup *mem_cgroup;#endif
Quoted 3 lines
...because after all, the reason this is so difficult is that this fix has to work in pretty much every configuration.
get_user_pages()use is widespread, it's a very general facility, and...it needs fixing. And we're out of space.
HMM seems entirely too greedy in this regard. Especially with zero upstream users. When can we start to delete the pieces of HMM that have no upstream consumers? I would think that would be 4.21 / 5.0 as there needs to be some forcing function. We can always re-add pieces of HMM with it's users when / if they arrive.
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 25 lines · Dan Williams
Quoted 3 lines · John Hubbard
Quoted 1 line · Jerome Glisse
On Thu, Dec 06, 2018 at 06:45:49PM -0800, John Hubbard wrote:
[..]
Quoted 20 lines
I see. OK, HMM has done an efficient job of mopping up unused fields, and now we are completely out of space. At this point, after thinking about it carefully, it seems clear that it's time for a single, new field:
include/linux/mm_types.h +3
-0@@ -182,6 +182,9 @@ struct page {/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */ atomic_t _refcount; +/* DMA usage count. See get_user_pages*(), put_user_page*(). */ +atomic_t _dma_pinned_count; +#ifdef CONFIG_MEMCG struct mem_cgroup *mem_cgroup;#endif
Quoted 9 lines
Quoted 3 lines
...because after all, the reason this is so difficult is that this fix has to work in pretty much every configuration.
get_user_pages()use is widespread, it's a very general facility, and...it needs fixing. And we're out of space.HMM seems entirely too greedy in this regard. Especially with zero upstream users. When can we start to delete the pieces of HMM that have no upstream consumers? I would think that would be 4.21 / 5.0 as there needs to be some forcing function. We can always re-add pieces of HMM with it's users when / if they arrive.
Patchset to use HMM inside nouveau have already been posted, some of the bits have already made upstream and more are line up for next merge window.
Cheers,
J�r�me
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 3 lines · Jerome Glisse
Patchset to use HMM inside nouveau have already been posted, some of the bits have already made upstream and more are line up for next merge window.
Even with that it is a relative fringe feature compared to making something like get_user_pages() that is literally used every to actually work properly.
So I think we need to kick out HMM here and just find another place for it to store data.
And just to make clear that I'm not picking just on this - the same is true to a just a little smaller extent for the pgmap..
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Jerome Glisse
Patchset to use HMM inside nouveau have already been posted, some of the bits have already made upstream and more are line up for next merge window.
Even with that it is a relative fringe feature compared to making something like
get_user_pages()that is literally used every to actually work properly.
Quoted 5 lines
So I think we need to kick out HMM here and just find another place for it to store data.
And just to make clear that I'm not picking just on this - the same is true to a just a little smaller extent for the pgmap..
Most of the user of GUP are well behave (everything under driver/gpu and so is mellanox driver and many other) ie they abide by mmu notifier invalidation call backs. They are a handfull of device driver that thought they could just do GUP and ignore the mmu notifier part and those are the one being problematic. So to me it feels like bystander are be shot for no good reasons.
I proposed an alternative solution to this GUP thing and i don't think it is a crazy one and thinking about it we only need to do that for file back page so we can leave untouch the anonymous page case. This would put a small burden on the user of GUP (by the way i am working on removing GUP from drivers/gpu and other well behave driver, patch posted on dri-devel for some of the GPU already).
So why not explore my idea and see if they are any roadblock on it.
Cheers,
J�r�me
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 6 lines · Jerome Glisse
Most of the user of GUP are well behave (everything under driver/gpu and so is mellanox driver and many other) ie they abide by mmu notifier invalidation call backs. They are a handfull of device driver that thought they could just do GUP and ignore the mmu notifier part and those are the one being problematic. So to me it feels like bystander are be shot for no good reasons.
get_user_pages is used by every single direct I/O, and while the race windows in that case are small they very much exists.
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Jerome Glisse
Most of the user of GUP are well behave (everything under driver/gpu and so is mellanox driver and many other) ie they abide by mmu notifier invalidation call backs. They are a handfull of device driver that thought they could just do GUP and ignore the mmu notifier part and those are the one being problematic. So to me it feels like bystander are be shot for no good reasons.
get_user_pagesis used by every single direct I/O, and while the race windows in that case are small they very much exists.
Yes and my proposal allow to fix that in even a better way than the pin count would ie allowing to provide a callback for write back to wait on direct I/O as you said for direct I/O it is a small window so it would be fine to have write back wait on it.
Cheers,
J�r�me
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to Christoph HellwigQuoted 10 lines · Christoph Hellwig
Quoted 3 lines · Jerome Glisse
Patchset to use HMM inside nouveau have already been posted, some of the bits have already made upstream and more are line up for next merge window.
Even with that it is a relative fringe feature compared to making something like
get_user_pages()that is literally used every to actually work properly.
Quoted 5 lines
So I think we need to kick out HMM here and just find another place for it to store data.
And just to make clear that I'm not picking just on this - the same is true to a just a little smaller extent for the pgmap..
Fair enough, I cringed as I took a full pointer for that use case, I'm happy to look at ways of consolidating or dropping that usage.
Another fix that may put pressure 'struct page' is resolving the untenable situation of dax being incompatible with reflink, i.e. reflink currently requires page-cache pages. Dave has talked about silently establishing page-cache entries when a dax-page is cow'd for reflink, but I wonder if we could go the other way and introduce the mechanism of a page belonging to multiple mappings simultaneously and managed by the filesystem.
Both HMM and ZONE_DEVICE in general are guilty of side-stepping the mm and I'm in favor of undoing that as much as possible,
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
Quoted 7 lines · Dan Williams
Another fix that may put pressure 'struct page' is resolving the untenable situation of dax being incompatible with reflink, i.e. reflink currently requires page-cache pages. Dave has talked about silently establishing page-cache entries when a dax-page is cow'd for reflink, but I wonder if we could go the other way and introduce the mechanism of a page belonging to multiple mappings simultaneously and managed by the filesystem.
FYI, I had a a prototype for DAX + reflink that didn't require the page cache, although it badly reimplemented parts of it. But that was a long time ago, before we started requiring struct page for the DAX memory.
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to Dan WilliamsQuoted 17 lines · Dan Williams
Quoted 10 lines · Christoph Hellwig
Quoted 3 lines · Jerome Glisse
Patchset to use HMM inside nouveau have already been posted, some of the bits have already made upstream and more are line up for next merge window.
Even with that it is a relative fringe feature compared to making something like
get_user_pages()that is literally used every to actually work properly.Quoted 5 lines
So I think we need to kick out HMM here and just find another place for it to store data.
And just to make clear that I'm not picking just on this - the same is true to a just a little smaller extent for the pgmap..
Quoted 8 lines
Fair enough, I cringed as I took a full pointer for that use case, I'm happy to look at ways of consolidating or dropping that usage.
Another fix that may put pressure 'struct page' is resolving the untenable situation of dax being incompatible with reflink, i.e. reflink currently requires page-cache pages. Dave has talked about silently establishing page-cache entries when a dax-page is cow'd for reflink,
I think you've got it the wrong way around there :)
Think of a set of files with the following physical block mappings:
index 0 1 2 3 4 5
inode W A B C D E F inode X B C D E F A inode Y C D E F A B inode Z D E F A B C
Basically, each block has 4 references (one from each file), and each reference to a block is from a diffent file offset. Now, with DAX, each inode wants to put the same struct page into their own address space mapping tree but have different page indexes.
i.e. for block A, inode W wants page->index = 0, X wants 5, Y wants 4 and Z wants 3.
This is not possible with a single struct page and where the problem with DAX, struct pages and physically shared data lies.
This is where the page cache is currently required - each mapping gets it's own copy of the shared block in volatile RAM, but when sharing is broken (by COW) we can toss the volatile copy and go back to using DAX for the newly allocated, single owner {block, struct page} tuple that replaces the shared page.
Quoted 3 lines
but I wonder if we could go the other way and introduce the mechanism of a page belonging to multiple mappings simultaneously and managed by the filesystem.
That's pretty much what I suggested at LSFMM. We do lookups for shared extent mappings through the filesystem buffer cache (which is indexed by physical location) and hold the primary struct page in the filesystem buffer cache. We then hand out dynamically allocated struct pages back to the caller that point to the same physical page and place them in each inode's address space. When a write fault occurs, we allocate a new block, grab the physical struct page, copy the data across, and release the dynamically allocated read-only struct page and reference to the primary struct page held in the filesytem buffer cache.
It's essentially the same model "cached page per inode address space" as using volatile RAM copies via the page cache, except the struct pages point back to the same physical location rather than having their own temporary, volatile copy of the data.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to Matthew WilcoxQuoted 32 lines · Matthew Wilcox
Quoted 10 lines · John Hubbard
Quoted 2 lines · Dan Williams
Except the LRU fields are already in use for
ZONE_DEVICEpages... how does this proposal interact with those?Very badly:
page->pgmapandpage->hmm_databoth get corrupted. Is there an entire use case I'm missing: callingget_user_pages()onZONE_DEVICEpages? Said another
way: is it reasonable to disallow callingget_user_pages()onZONE_DEVICEpages?If we have to support
get_user_pages()onZONE_DEVICEpages, then the whole LRU field approach is unusable.We just need to rearrange
ZONE_DEVICEpages. Please excuse the whitespace damage:+++
b/include/linux/mm_types.h(quoted diff) +3
-1@@ -151,10 +151,12 @@ struct page { #endif }; struct { /* ZONE_DEVICE pages */ + unsigned long _zd_pad_2; /* LRU */ + unsigned long _zd_pad_3; /* LRU */ + unsigned long _zd_pad_1; /* uses mapping */ /** @pgmap: Points to the hosting device page map. */ struct dev_pagemap *pgmap; unsigned long hmm_data; - unsigned long _zd_pad_1; /* uses mapping */ }; /** @rcu_head: You can use this to free a page by RCU. */
Quoted 1 line
You don't use
page->privateorpage->index, do you Dan?
I don't use page->private, but page->index is used by the memory-failure path to do an rmap.
Jan Kara
reviewerRe: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions
In reply to John HubbardQuoted 41 lines · John Hubbard
Quoted 33 lines · Dan Williams
Quoted 23 lines · On Mon, Dec 3, 2018 at 4:17 PM <[email protected]> wrote:
From: John Hubbard <[email protected]>
Introduces
put_user_page(), which simply callsput_page(). This provides a way to update all get_user_pages*() callers, so that they callput_user_page(), instead ofput_page().Also introduces
put_user_pages(), and a few dirty/locked variations, as a replacement forrelease_pages(), and also as a replacement for open-coded loops that release multiple pages.
These may be used for subsequent performance improvements, via batching of pages to be released.This is the first step of fixing the problem described in [1]. The steps are:
1) (This patch): provide put_user_page*() routines, intended to be used for releasing pages that were pinned via get_user_pages*().
2) Convert all of the call sites for get_user_pages*(), to invoke put_user_page*(), instead of
put_page(). This involves dozens of call sites, and will take some time.Quoted 8 lines
3) After (2) is complete, use get_user_pages*() and put_user_page*() to implement tracking of these pages. This tracking will be separate from the existing struct page refcounting.
4) Use the tracking and identification of these pages, to implement special handling (especially in writeback paths) when the pages are backed by a filesystem. Again, [1] provides details as to why that is desirable.
Quoted 6 lines
I thought at Plumbers we talked about using a page bit to tag pages that have had their reference count elevated by
get_user_pages()? That way there is no need to distinguishput_page()fromput_user_page()it just happens internally toput_page(). At the conference Matthew was offering to free up a page bit for this purpose.
Quoted 3 lines
...but then, upon further discussion in that same session, we realized that that doesn't help. You need a reference count. Otherwise a random
put_pagecould affect your dma-pinned pages, etc, etc.
Exactly.
Quoted 3 lines
I was not able to actually find any place where a single additional page bit would help our situation, which is why this still uses LRU fields for both the two bits required (the RFC [1] still applies), and the
dma_pinned_count.
So single page bit could help you with performance. In 99% of cases there's just one reference from GUP. So if you could store that info in page flags, you could safe yourself a relatively expensive removal from LRU and putting it back to make space in struct page for proper refcount. But since you report that the performance isn't that horrible, I'd leave this idea on a backburner. We can always implement it later in case we find in future we need to improve the performance.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH 2/2 infiniband/mm: convert put_page() to put_user_page*()
In reply to [email protected]From: John Hubbard <[email protected]>
For infiniband code that retains pages via get_user_pages*(), release those pages via the new put_user_page(), or put_user_pages*(), instead of put_page()
This is a tiny part of the second step of fixing the problem described in [1]. The steps are:
1) Provide put_user_page*() routines, intended to be used for releasing pages that were pinned via get_user_pages*().
2) Convert all of the call sites for get_user_pages*(), to invoke put_user_page*(), instead of put_page(). This involves dozens of call sites, and will take some time.
3) After (2) is complete, use get_user_pages*() and put_user_page*() to implement tracking of these pages. This tracking will be separate from the existing struct page refcounting.
4) Use the tracking and identification of these pages, to implement special handling (especially in writeback paths) when the pages are backed by a filesystem. Again, [1] provides details as to why that is desirable.
[1] https://lwn.net/Articles/753027/ : "The Trouble with get_user_pages()"
--- drivers/infiniband/core/umem.c | 7 ++++--- drivers/infiniband/core/umem_odp.c | 2 +- drivers/infiniband/hw/hfi1/user_pages.c | 11 ++++------- drivers/infiniband/hw/mthca/mthca_memfree.c | 6 +++--- drivers/infiniband/hw/qib/qib_user_pages.c | 11 ++++------- drivers/infiniband/hw/qib/qib_user_sdma.c | 6 +++--- drivers/infiniband/hw/usnic/usnic_uiom.c | 7 ++++--- 7 files changed, 23 insertions(+), 27 deletions(-)
drivers/infiniband/core/umem.c +4 -3
@@ -58,9 +58,10 @@ static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dfor_each_sg (umem->sg_head.sgl, sg, umem->npages, i) { page =sg_page (sg); -if (!PageDirty (page) && umem->writable && dirty) -set_page_dirty_lock (page); -put_page (page); +if (umem->writable && dirty) +put_user_pages_dirty_lock (&page,1 ); +else +put_user_page (page); }sg_free_table (&umem->sg_head);
drivers/infiniband/core/umem_odp.c +1 -1
@@ -659,7 +659,7 @@ int ib_umem_odp_map_dma_pages(struct ib_umem_odp *umem_odp, u64 user_virt, ret = -EFAULT ;break ; } -put_page (local_page_list[j]); +put_user_page (local_page_list[j]);continue ; }
drivers/infiniband/hw/hfi1/user_pages.c +4 -7
@@ -121,13 +121,10 @@ int hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr, size_t npvoid hfi1_release_user_pages (struct mm_struct *mm,struct page **p,size_t npages,bool dirty) { -size_t i; - -for (i =0 ; i < npages; i++) { -if (dirty) -set_page_dirty_lock (p[i]); -put_page (p[i]); - } +if (dirty) +put_user_pages_dirty_lock (p, npages); +else +put_user_pages (p, npages);if (mm) {/* during close after signal, mm can be NULL */ down_write (&mm->mmap_sem);
drivers/infiniband/hw/mthca/mthca_memfree.c +3 -3
@@ -481,7 +481,7 @@ int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar, ret =pci_map_sg (dev->pdev, &db_tab->page[i].mem,1 ,PCI_DMA_TODEVICE );if (ret <0 ) { -put_page (pages[0 ]); +put_user_page (pages[0 ]); goto out; }
@@ -489,7 +489,7 @@ int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar, mthca_uarc_virt(dev, uar, i));if (ret) {pci_unmap_sg (dev->pdev, &db_tab->page[i].mem,1 ,PCI_DMA_TODEVICE ); -put_page (sg_page (&db_tab->page[i].mem)); +put_user_page (sg_page (&db_tab->page[i].mem)); goto out; }
@@ -555,7 +555,7 @@ void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,if (db_tab->page[i].uvirt) {mthca_UNMAP_ICM (dev,mthca_uarc_virt (dev, uar, i),1 );pci_unmap_sg (dev->pdev, &db_tab->page[i].mem,1 ,PCI_DMA_TODEVICE ); -put_page (sg_page (&db_tab->page[i].mem)); +put_user_page (sg_page (&db_tab->page[i].mem)); } }
drivers/infiniband/hw/qib/qib_user_pages.c +4 -7
@@ -40,13 +40,10 @@static void __qib_release_user_pages (struct page **p,size_t num_pages,int dirty) { -size_t i; - -for (i =0 ; i < num_pages; i++) { -if (dirty) -set_page_dirty_lock (p[i]); -put_page (p[i]); - } +if (dirty) +put_user_pages_dirty_lock (p, num_pages); +else +put_user_pages (p, num_pages); }/*
drivers/infiniband/hw/qib/qib_user_sdma.c +3 -3
@@ -321,7 +321,7 @@ static int qib_user_sdma_page_to_frags(const struct qib_devdata *dd,* the caller can ignore this page. */ if (put) { -put_page (page); +put_user_page (page); }else {/* coalesce case */ kunmap (page);
@@ -635,7 +635,7 @@ static void qib_user_sdma_free_pkt_frag(struct device *dev,kunmap (pkt->addr[i].page);if (pkt->addr[i].put_page) -put_page (pkt->addr[i].page); +put_user_page (pkt->addr[i].page);else __free_page (pkt->addr[i].page); }else if (pkt->addr[i].kvaddr) {
@@ -710,7 +710,7 @@ static int qib_user_sdma_pin_pages(const struct qib_devdata *dd,/* if error, return all pages not managed by pkt */ free_pages:while (i < j) -put_page (pages[i++]); +put_user_page (pages[i++]); done:return ret;
drivers/infiniband/hw/usnic/usnic_uiom.c +4 -3
@@ -77,9 +77,10 @@ static void usnic_uiom_put_pages(struct list_head *chunk_list, int dirty)for_each_sg (chunk->page_list, sg, chunk->nents, i) { page =sg_page (sg); pa =sg_phys (sg); -if (!PageDirty (page) && dirty) -set_page_dirty_lock (page); -put_page (page); +if (dirty) +put_user_pages_dirty_lock (&page,1 ); +else +put_user_page (page);usnic_dbg ("pa: %pa\n" , &pa); }kfree (chunk);
Signature · 1 line
2.19.2
From: [email protected]
Quoted 14 lines
Sent: 04 December 2018 00:17
Summary: I'd like these two patches to go into the next convenient cycle. I *think* that means 4.21.
Details
At the Linux Plumbers Conference, we talked about this approach [1], and the primary lingering concern was over performance. Tom Talpey helped me through a much more accurate run of the fio performance test, and now it's looking like an under 1% performance cost, to add and remove pages from the LRU (this is only paid when dealing with
get_user_pages) [2]. So we should be fine to start converting call sites.
Quoted 2 lines
This patchset gets the conversion started. Both patches already had a fair amount of review.
Shouldn't the commit message contain actual details of the change?
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
John Hubbard
reviewerQuoted 15 lines · David Laight
From: [email protected]
Quoted 14 lines
Sent: 04 December 2018 00:17
Summary: I'd like these two patches to go into the next convenient cycle. I *think* that means 4.21.
Details
At the Linux Plumbers Conference, we talked about this approach [1], and the primary lingering concern was over performance. Tom Talpey helped me through a much more accurate run of the fio performance test, and now it's looking like an under 1% performance cost, to add and remove pages from the LRU (this is only paid when dealing with
get_user_pages) [2]. So we should be fine to start converting call sites.
Quoted 5 lines
Quoted 2 lines
This patchset gets the conversion started. Both patches already had a fair amount of review.
Shouldn't the commit message contain actual details of the change?
Hi David,
This "patch 0000" is not a commit message, as it never shows up in git log. Each of the follow-up patches does have details about the changes it makes.
But maybe you are really asking for more background information, which I should have added in this cover letter. Here's a start:
https://lore.kernel.org/r/[email protected]
...and it looks like this small patch series is not going to work out--I'm going to have to fall back to another RFC spin. So I'll be sure to include you and everyone on that. Hope that helps.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
From: John Hubbard
Quoted 20 lines
Sent: 05 December 2018 01:06
Quoted 18 lines · David Laight
From: [email protected]
Quoted 16 lines
Sent: 04 December 2018 00:17
Summary: I'd like these two patches to go into the next convenient cycle. I *think* that means 4.21.
Details
At the Linux Plumbers Conference, we talked about this approach [1], and the primary lingering concern was over performance. Tom Talpey helped me through a much more accurate run of the fio performance test, and now it's looking like an under 1% performance cost, to add and remove pages from the LRU (this is only paid when dealing with
get_user_pages) [2]. So we should be fine to start converting call sites.This patchset gets the conversion started. Both patches already had a fair amount of review.
Quoted 7 lines
Quoted 2 lines
Shouldn't the commit message contain actual details of the change?
Hi David,
This "patch 0000" is not a commit message, as it never shows up in git log. Each of the follow-up patches does have details about the changes it makes.
I think you should still describe the change - at least in summary.
The patch I looked at didn't really...
IIRC it still referred to external links.
Quoted 4 lines
But maybe you are really asking for more background information, which I should have added in this cover letter. Here's a start:
Yes, but links go stale....
Quoted 3 lines
...and it looks like this small patch series is not going to work out--I'm going to have to fall back to another RFC spin. So I'll be sure to include you and everyone on that. Hope that helps.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Hi!
Quoted 8 lines
Quoted 2 lines
This "patch 0000" is not a commit message, as it never shows up in git log. Each of the follow-up patches does have details about the changes it makes.
I think you should still describe the change - at least in summary.
The patch I looked at didn't really...
IIRC it still referred to external links.
Quoted 6 lines
Quoted 4 lines
But maybe you are really asking for more background information, which I should have added in this cover letter. Here's a start:
Yes, but links go stale....
It should really explain what the end goal is... and not even the [email protected] explains that.
It seems you are introducing small slowdown to simplify something...?
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html