Skip to contentSkip to conversation

mm: cleanups for device-exclusive entries (hmm)

PATCH Superseded David Hildenbrand 3 participants 20 messages Jan 29, 2025 – Jan 31, 2025

Subsystems (9)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover mm: cleanups for device-exclusive entries (hmm)

This is a follow-up to [1], performing some related cleanups. There are more cleanups to be had, but I'll have to focus on some other stuff next. I might come back to that once I'm stuck on (annoyed by :) ) other things.

Cc Andrew Morton <[email protected]>
Cc "Jérôme Glisse" <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Alex Shi <[email protected]>
Cc Yanteng Si <[email protected]>
Cc Karol Herbst <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
Cc Pasha Tatashin <[email protected]>
Cc Peter Xu <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Jason Gunthorpe <[email protected]>

[1] https://lkml.kernel.org/r/[email protected]

David Hildenbrand (4):
lib/test_hmm: make dmirror_atomic_map() consume a single page mm/mmu_notifier: drop owner from MMU_NOTIFY_EXCLUSIVE mm/memory: pass folio and pte to restore_exclusive_pte() mm/memory: document restore_exclusive_pte()

 drivers/gpu/drm/nouveau/nouveau_svm.c |  6 +--
 include/linux/mmu_notifier.h          |  4 +-
 include/linux/rmap.h                  |  2 +-
 lib/test_hmm.c                        | 35 ++++++-----------
 mm/memory.c                           | 54 +++++++++++++++++++--------
 mm/rmap.c                             |  3 +-
 6 files changed, 54 insertions(+), 50 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/4 lib/test_hmm: make dmirror_atomic_map() consume a single page

The caller now always passes a single page; let's simplify, and return "0" on success.

---
 lib/test_hmm.c | 33 ++++++++++-----------------------
 1 file changed, 10 insertions(+), 23 deletions(-)
lib/test_hmm.c +10 -23
index 9e1b07a227a3..1c0a58279db9 100644
@@ -706,34 +706,23 @@ static int dmirror_check_atomic(struct dmirror *dmirror, unsigned long start,
 	return 0;
 }
 
-static int dmirror_atomic_map(unsigned long start, unsigned long end,
-			      struct page **pages, struct dmirror *dmirror)
+static int dmirror_atomic_map(unsigned long addr, struct page *page,
+		struct dmirror *dmirror)
 {
-	unsigned long pfn, mapped = 0;
-	int i;
+	void *entry;
 
 	/* Map the migrated pages into the device's page tables. */
 	mutex_lock(&dmirror->mutex);
 
-	for (i = 0, pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++, i++) {
-		void *entry;
-
-		if (!pages[i])
-			continue;
-
-		entry = pages[i];
-		entry = xa_tag_pointer(entry, DPT_XA_TAG_ATOMIC);
-		entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC);
-		if (xa_is_err(entry)) {
-			mutex_unlock(&dmirror->mutex);
-			return xa_err(entry);
-		}
-
-		mapped++;
+	entry = xa_tag_pointer(page, DPT_XA_TAG_ATOMIC);
+	entry = xa_store(&dmirror->pt, addr >> PAGE_SHIFT, entry, GFP_ATOMIC);
+	if (xa_is_err(entry)) {
+		mutex_unlock(&dmirror->mutex);
+		return xa_err(entry);
 	}
 
 	mutex_unlock(&dmirror->mutex);
-	return mapped;
+	return 0;
 }
 
 static int dmirror_migrate_finalize_and_map(struct migrate_vma *args,
@@ -803,9 +792,7 @@ static int dmirror_exclusive(struct dmirror *dmirror,
 			break;
 		}
 
-		ret = dmirror_atomic_map(addr, addr + PAGE_SIZE, &page, dmirror);
-		if (!ret)
-			ret = -EBUSY;
+		ret = dmirror_atomic_map(addr, page, dmirror);
 		folio_unlock(folio);
 		folio_put(folio);
 
Signature · 1 line
2.48.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH v1 1/4] lib/test_hmm: make dmirror_atomic_map() consume a single page

Quoted 2 lines · David Hildenbrand

The caller now always passes a single page; let's simplify, and return "0" on success.

Thanks for cleaning that up.

Quoted 67 lines
---
 lib/test_hmm.c | 33 ++++++++++-----------------------
 1 file changed, 10 insertions(+), 23 deletions(-)
lib/test_hmm.c +10 -23
index 9e1b07a227a3..1c0a58279db9 100644
@@ -706,34 +706,23 @@ static int dmirror_check_atomic(struct dmirror *dmirror, unsigned long start,
 	return 0;
 }
 
-static int dmirror_atomic_map(unsigned long start, unsigned long end,
-			      struct page **pages, struct dmirror *dmirror)
+static int dmirror_atomic_map(unsigned long addr, struct page *page,
+		struct dmirror *dmirror)
 {
-	unsigned long pfn, mapped = 0;
-	int i;
+	void *entry;
 
 	/* Map the migrated pages into the device's page tables. */
 	mutex_lock(&dmirror->mutex);
 
-	for (i = 0, pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++, i++) {
-		void *entry;
-
-		if (!pages[i])
-			continue;
-
-		entry = pages[i];
-		entry = xa_tag_pointer(entry, DPT_XA_TAG_ATOMIC);
-		entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC);
-		if (xa_is_err(entry)) {
-			mutex_unlock(&dmirror->mutex);
-			return xa_err(entry);
-		}
-
-		mapped++;
+	entry = xa_tag_pointer(page, DPT_XA_TAG_ATOMIC);
+	entry = xa_store(&dmirror->pt, addr >> PAGE_SHIFT, entry, GFP_ATOMIC);
+	if (xa_is_err(entry)) {
+		mutex_unlock(&dmirror->mutex);
+		return xa_err(entry);
 	}
 
 	mutex_unlock(&dmirror->mutex);
-	return mapped;
+	return 0;
 }
 
 static int dmirror_migrate_finalize_and_map(struct migrate_vma *args,
@@ -803,9 +792,7 @@ static int dmirror_exclusive(struct dmirror *dmirror,
 			break;
 		}
 
-		ret = dmirror_atomic_map(addr, addr + PAGE_SIZE, &page, dmirror);
-		if (!ret)
-			ret = -EBUSY;
+		ret = dmirror_atomic_map(addr, page, dmirror);
 		folio_unlock(folio);
 		folio_put(folio);
 
Signature · 1 line
2.48.1
#3 lore raw

PATCH 2/4 mm/mmu_notifier: drop owner from MMU_NOTIFY_EXCLUSIVE

In reply to David Hildenbrand

We no longer get a MMU_NOTIFY_EXCLUSIVE on conversion with the owner set that one has to filter out: if there already *is* a device-exclusive entry (e.g., other device, we don't have that information), GUP will convert it back to an ordinary PTE and notify via remove_device_exclusive_entry().

---
 drivers/gpu/drm/nouveau/nouveau_svm.c | 6 +-----
 include/linux/mmu_notifier.h          | 4 +---
 include/linux/rmap.h                  | 2 +-
 lib/test_hmm.c                        | 2 +-
 mm/rmap.c                             | 3 +--
 5 files changed, 5 insertions(+), 12 deletions(-)
drivers/gpu/drm/nouveau/nouveau_svm.c +1 -5
index 39e3740980bb..4758fee182b4 100644
@@ -510,10 +510,6 @@ static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni,
 	struct svm_notifier *sn =
 		container_of(mni, struct svm_notifier, notifier);
 
-	if (range->event == MMU_NOTIFY_EXCLUSIVE &&
-	    range->owner == sn->svmm->vmm->cli->drm->dev)
-		return true;
-
 	/*
 	 * serializes the update to mni->invalidate_seq done by caller and
 	 * prevents invalidation of the PTE from progressing while HW is being
@@ -609,7 +605,7 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 
 		notifier_seq = mmu_interval_read_begin(&notifier->notifier);
 		mmap_read_lock(mm);
-		page = make_device_exclusive(mm, start, drm->dev, &folio);
+		page = make_device_exclusive(mm, start, &folio);
 		mmap_read_unlock(mm);
 		if (IS_ERR(page)) {
 			ret = -EINVAL;
include/linux/mmu_notifier.h +1 -3
index d4e714661826..bac2385099dd 100644
@@ -44,9 +44,7 @@ struct mmu_interval_notifier;
  * owner field matches the driver's device private pgmap owner.
  *
  * @MMU_NOTIFY_EXCLUSIVE: to signal a device driver that the device will no
- * longer have exclusive access to the page. When sent during creation of an
- * exclusive range the owner will be initialised to the value provided by the
- * caller of make_device_exclusive(), otherwise the owner will be NULL.
+ * longer have exclusive access to the page.
  */
 enum mmu_notifier_event {
 	MMU_NOTIFY_UNMAP = 0,
include/linux/rmap.h +1 -1
index 86425d42c1a9..3b216b91d2e5 100644
@@ -664,7 +664,7 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags);
 void try_to_unmap(struct folio *, enum ttu_flags flags);
 
 struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr,
-		void *owner, struct folio **foliop);
+		struct folio **foliop);
 
 /* Avoid racy checks */
 #define PVMW_SYNC		(1 << 0)
lib/test_hmm.c +1 -1
index 1c0a58279db9..8520c1d1b21b 100644
@@ -786,7 +786,7 @@ static int dmirror_exclusive(struct dmirror *dmirror,
 		struct folio *folio;
 		struct page *page;
 
-		page = make_device_exclusive(mm, addr, &folio, NULL);
+		page = make_device_exclusive(mm, addr, &folio);
 		if (IS_ERR(page)) {
 			ret = PTR_ERR(page);
 			break;
mm/rmap.c +1 -2
index 4acc9f6d743a..d99dbf59adc6 100644
@@ -2397,7 +2397,6 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags)
  * make_device_exclusive() - Mark an address for exclusive use by a device
  * @mm: mm_struct of associated target process
  * @addr: the virtual address to mark for exclusive device access
- * @owner: passed to MMU_NOTIFY_EXCLUSIVE range notifier to allow filtering
  * @foliop: folio pointer will be stored here on success.
  *
  * This function looks up the page mapped at the given address, grabs a
@@ -2421,7 +2420,7 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags)
  * Returns: pointer to mapped page on success, otherwise a negative error.
  */
 struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr,
-		void *owner, struct folio **foliop)
+		struct folio **foliop)
 {
 	struct folio *folio, *fw_folio;
 	struct vm_area_struct *vma;
Signature · 1 line
2.48.1
#4 lore raw

Re: [PATCH v1 2/4] mm/mmu_notifier: drop owner from MMU_NOTIFY_EXCLUSIVE

Quoted 5 lines · David Hildenbrand

We no longer get a MMU_NOTIFY_EXCLUSIVE on conversion with the owner set that one has to filter out: if there already *is* a device-exclusive entry (e.g., other device, we don't have that information), GUP will convert it back to an ordinary PTE and notify via remove_device_exclusive_entry().

What tree is this against? I tried applying to v6.13 and Linus current master but neither applied cleanly.

Quoted 14 lines
---
 drivers/gpu/drm/nouveau/nouveau_svm.c | 6 +-----
 include/linux/mmu_notifier.h          | 4 +---
 include/linux/rmap.h                  | 2 +-
 lib/test_hmm.c                        | 2 +-
 mm/rmap.c                             | 3 +--
 5 files changed, 5 insertions(+), 12 deletions(-)
drivers/gpu/drm/nouveau/nouveau_svm.c +0 -0
index 39e3740980bb..4758fee182b4 100644

drivers/gpu/drm/nouveau/nouveau_svm.c static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni, · line 515

Quoted 6 lines
drivers/gpu/drm/nouveau/nouveau_svm.c +0 -3
@@ -510,10 +510,6 @@ static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni,
 	struct svm_notifier *sn =
 		container_of(mni, struct svm_notifier, notifier);
 
-	if (range->event == MMU_NOTIFY_EXCLUSIVE &&
-	    range->owner == sn->svmm->vmm->cli->drm->dev)
-		return true;

I think this will cause a live-lock because make_device_exclusive_range() will call the notifier which without the filtering will increment the sequence count and cause endless retries of the loop in nouveau_atomic_range_fault(). The notifier needs to be able to figure out if it was called in response to something this thread did (ie. make_device_exclusive_range) and can therefore ignore the invalidation, or from some other thread.

Looking at hmm_test I see that doesn't use the sequence counter to ensure the PTE remains valid whilst it is mapped. I think that is probably wrong, so apologies if that lead you astray.

Quoted 77 lines
drivers/gpu/drm/nouveau/nouveau_svm.c +1 -1
 	/*
 	 * serializes the update to mni->invalidate_seq done by caller and
 	 * prevents invalidation of the PTE from progressing while HW is being
@@ -609,7 +605,7 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 
 		notifier_seq = mmu_interval_read_begin(&notifier->notifier);
 		mmap_read_lock(mm);
-		page = make_device_exclusive(mm, start, drm->dev, &folio);
+		page = make_device_exclusive(mm, start, &folio);
 		mmap_read_unlock(mm);
 		if (IS_ERR(page)) {
 			ret = -EINVAL;
include/linux/mmu_notifier.h +1 -3
index d4e714661826..bac2385099dd 100644
@@ -44,9 +44,7 @@ struct mmu_interval_notifier;
  * owner field matches the driver's device private pgmap owner.
  *
  * @MMU_NOTIFY_EXCLUSIVE: to signal a device driver that the device will no
- * longer have exclusive access to the page. When sent during creation of an
- * exclusive range the owner will be initialised to the value provided by the
- * caller of make_device_exclusive(), otherwise the owner will be NULL.
+ * longer have exclusive access to the page.
  */
 enum mmu_notifier_event {
 	MMU_NOTIFY_UNMAP = 0,
include/linux/rmap.h +1 -1
index 86425d42c1a9..3b216b91d2e5 100644
@@ -664,7 +664,7 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags);
 void try_to_unmap(struct folio *, enum ttu_flags flags);
 
 struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr,
-		void *owner, struct folio **foliop);
+		struct folio **foliop);
 
 /* Avoid racy checks */
 #define PVMW_SYNC		(1 << 0)
lib/test_hmm.c +1 -1
index 1c0a58279db9..8520c1d1b21b 100644
@@ -786,7 +786,7 @@ static int dmirror_exclusive(struct dmirror *dmirror,
 		struct folio *folio;
 		struct page *page;
 
-		page = make_device_exclusive(mm, addr, &folio, NULL);
+		page = make_device_exclusive(mm, addr, &folio);
 		if (IS_ERR(page)) {
 			ret = PTR_ERR(page);
 			break;
mm/rmap.c +1 -2
index 4acc9f6d743a..d99dbf59adc6 100644
@@ -2397,7 +2397,6 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags)
  * make_device_exclusive() - Mark an address for exclusive use by a device
  * @mm: mm_struct of associated target process
  * @addr: the virtual address to mark for exclusive device access
- * @owner: passed to MMU_NOTIFY_EXCLUSIVE range notifier to allow filtering
  * @foliop: folio pointer will be stored here on success.
  *
  * This function looks up the page mapped at the given address, grabs a
@@ -2421,7 +2420,7 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags)
  * Returns: pointer to mapped page on success, otherwise a negative error.
  */
 struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr,
-		void *owner, struct folio **foliop)
+		struct folio **foliop)
 {
 	struct folio *folio, *fw_folio;
 	struct vm_area_struct *vma;
Signature · 1 line
2.48.1
#5 lore raw

Re: [PATCH v1 2/4] mm/mmu_notifier: drop owner from MMU_NOTIFY_EXCLUSIVE

Quoted 9 lines · Alistair Popple
Quoted 5 lines · David Hildenbrand

We no longer get a MMU_NOTIFY_EXCLUSIVE on conversion with the owner set that one has to filter out: if there already *is* a device-exclusive entry (e.g., other device, we don't have that information), GUP will convert it back to an ordinary PTE and notify via remove_device_exclusive_entry().

What tree is this against? I tried applying to v6.13 and Linus current master but neither applied cleanly.

See the cover letter. This is on top of the fixes series, which is based on mm-unstable from yesterday.

Quoted 18 lines
Quoted 18 lines
---
  drivers/gpu/drm/nouveau/nouveau_svm.c | 6 +-----
  include/linux/mmu_notifier.h          | 4 +---
  include/linux/rmap.h                  | 2 +-
  lib/test_hmm.c                        | 2 +-
  mm/rmap.c                             | 3 +--
  5 files changed, 5 insertions(+), 12 deletions(-)
drivers/gpu/drm/nouveau/nouveau_svm.c +0 -0
index 39e3740980bb..4758fee182b4 100644
@@ -510,10 +510,6 @@ static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni,
  	struct svm_notifier *sn =
  		container_of(mni, struct svm_notifier, notifier);
 
Quoted 10 lines

drivers/gpu/drm/nouveau/nouveau_svm.c static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni, · line 515

Quoted 3 lines
drivers/gpu/drm/nouveau/nouveau_svm.c +0 -3
-	if (range->event == MMU_NOTIFY_EXCLUSIVE &&
-	    range->owner == sn->svmm->vmm->cli->drm->dev)
-		return true;

I think this will cause a live-lock because make_device_exclusive_range() will call the notifier which without the filtering will increment the sequence count and cause endless retries of the loop in nouveau_atomic_range_fault(). The notifier needs to be able to figure out if it was called in response to something this thread did (ie. make_device_exclusive_range) and can therefore ignore the invalidation, or from some other thread.

Yes, as discussed in the other patch, this must stay to inform secondary MMUs about the conversion *to* device exclusive.

Quoted 4 lines

Looking at hmm_test I see that doesn't use the sequence counter to ensure the PTE remains valid whilst it is mapped. I think that is probably wrong, so apologies if that lead you astray.

Yes, the hmm_test does not completely follow the same model the nouveau implementation does; so it might not be completely correct.

Signature · 3 lines
Cheers,

David / dhildenb
#6 lore raw

Re: [PATCH v1 2/4] mm/mmu_notifier: drop owner from MMU_NOTIFY_EXCLUSIVE

Quoted 7 lines · David Hildenbrand
Quoted 3 lines · Alistair Popple

Looking at hmm_test I see that doesn't use the sequence counter to ensure the PTE remains valid whilst it is mapped. I think that is probably wrong, so apologies if that lead you astray.

Yes, the hmm_test does not completely follow the same model the nouveau implementation does; so it might not be completely correct.

But unrelated but just crossed my mind:

I guess another crucial difference is that the hw (probably, not sure) will restart the fault if we don't repair it to its liking. So the hmm-test does need some kind of retry loop too somewhere to match that. But might be good to also still land some of the other improvements discussed in these threads to make make_device_exclusive a bit more reliable instead of relying on busy-looping throug the hw fault handler for everything.
-Sima

Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#7 lore raw

Re: [PATCH v1 2/4] mm/mmu_notifier: drop owner from MMU_NOTIFY_EXCLUSIVE

Quoted 6 lines · Simona Vetter
Quoted 5 lines · David Hildenbrand
Quoted 3 lines · Alistair Popple

Looking at hmm_test I see that doesn't use the sequence counter to ensure the PTE remains valid whilst it is mapped. I think that is probably wrong, so apologies if that lead you astray.

Quoted 8 lines
Quoted 2 lines

Yes, the hmm_test does not completely follow the same model the nouveau implementation does; so it might not be completely correct.

But unrelated but just crossed my mind:

I guess another crucial difference is that the hw (probably, not sure) will restart the fault if we don't repair it to its liking. So the hmm-test does need some kind of retry loop too somewhere to match that.

Yes. Especially for the folio lock spinning is a rather suboptimal approach. So we likely would want the option to just lock it instead of try-locking it. (or getting rid of it entirely :) )

Quoted 4 lines

But might be good to also still land some of the other improvements discussed in these threads to make make_device_exclusive a bit more reliable instead of relying on busy-looping throug the hw fault handler for everything.

Right.

Signature · 3 lines
Cheers,

David / dhildenb
#8 lore raw

PATCH 3/4 mm/memory: pass folio and pte to restore_exclusive_pte()

In reply to David Hildenbrand

Let's pass the folio and the pte to restore_exclusive_pte(), so we can avoid repeated page_folio() and ptep_get(). To do that, pass the pte to try_restore_exclusive_pte() and use a folio in there already.

While at it, just avoid the "swp_entry_t entry" variable in try_restore_exclusive_pte() and add a folio-locked check to restore_exclusive_pte().

---
 mm/memory.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)
mm/memory.c +14 -15
index cd689cd8a7c8..46956994aaff 100644
@@ -719,14 +719,13 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma,
 #endif
 
 static void restore_exclusive_pte(struct vm_area_struct *vma,
-				  struct page *page, unsigned long address,
-				  pte_t *ptep)
+		struct folio *folio, struct page *page, unsigned long address,
+		pte_t *ptep, pte_t orig_pte)
 {
-	struct folio *folio = page_folio(page);
-	pte_t orig_pte;
 	pte_t pte;
 
-	orig_pte = ptep_get(ptep);
+	VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
+
 	pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot)));
 	if (pte_swp_soft_dirty(orig_pte))
 		pte = pte_mksoft_dirty(pte);
@@ -756,16 +755,15 @@ static void restore_exclusive_pte(struct vm_area_struct *vma,
  * Tries to restore an exclusive pte if the page lock can be acquired without
  * sleeping.
  */
-static int
-try_restore_exclusive_pte(pte_t *src_pte, struct vm_area_struct *vma,
-			unsigned long addr)
+static int try_restore_exclusive_pte(struct vm_area_struct *vma,
+		unsigned long addr, pte_t *ptep, pte_t orig_pte)
 {
-	swp_entry_t entry = pte_to_swp_entry(ptep_get(src_pte));
-	struct page *page = pfn_swap_entry_to_page(entry);
+	struct page *page = pfn_swap_entry_to_page(pte_to_swp_entry(orig_pte));
+	struct folio *folio = page_folio(page);
 
-	if (trylock_page(page)) {
-		restore_exclusive_pte(vma, page, addr, src_pte);
-		unlock_page(page);
+	if (folio_trylock(folio)) {
+		restore_exclusive_pte(vma, folio, page, addr, ptep, orig_pte);
+		folio_unlock(folio);
 		return 0;
 	}
 
@@ -871,7 +869,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
 		 * (ie. COW) mappings.
 		 */
 		VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags));
-		if (try_restore_exclusive_pte(src_pte, src_vma, addr))
+		if (try_restore_exclusive_pte(src_vma, addr, src_pte, orig_pte))
 			return -EBUSY;
 		return -ENOENT;
 	} else if (is_pte_marker_entry(entry)) {
@@ -3979,7 +3977,8 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf)
 	vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
 				&vmf->ptl);
 	if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte)))
-		restore_exclusive_pte(vma, vmf->page, vmf->address, vmf->pte);
+		restore_exclusive_pte(vma, folio, vmf->page, vmf->address,
+				      vmf->pte, vmf->orig_pte);
 
 	if (vmf->pte)
 		pte_unmap_unlock(vmf->pte, vmf->ptl);
Signature · 1 line
2.48.1
Reviewed-by on v1 3/4
#9 lore raw

Re: [PATCH v1 3/4] mm/memory: pass folio and pte to restore_exclusive_pte()

Quoted 8 lines · David Hildenbrand

Let's pass the folio and the pte to restore_exclusive_pte(), so we can avoid repeated page_folio() and ptep_get(). To do that, pass the pte to try_restore_exclusive_pte() and use a folio in there already.

While at it, just avoid the "swp_entry_t entry" variable in try_restore_exclusive_pte() and add a folio-locked check to restore_exclusive_pte().

Seems reasonable.

Quoted 74 lines
---
 mm/memory.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)
mm/memory.c +14 -15
index cd689cd8a7c8..46956994aaff 100644
@@ -719,14 +719,13 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma,
 #endif
 
 static void restore_exclusive_pte(struct vm_area_struct *vma,
-				  struct page *page, unsigned long address,
-				  pte_t *ptep)
+		struct folio *folio, struct page *page, unsigned long address,
+		pte_t *ptep, pte_t orig_pte)
 {
-	struct folio *folio = page_folio(page);
-	pte_t orig_pte;
 	pte_t pte;
 
-	orig_pte = ptep_get(ptep);
+	VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
+
 	pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot)));
 	if (pte_swp_soft_dirty(orig_pte))
 		pte = pte_mksoft_dirty(pte);
@@ -756,16 +755,15 @@ static void restore_exclusive_pte(struct vm_area_struct *vma,
  * Tries to restore an exclusive pte if the page lock can be acquired without
  * sleeping.
  */
-static int
-try_restore_exclusive_pte(pte_t *src_pte, struct vm_area_struct *vma,
-			unsigned long addr)
+static int try_restore_exclusive_pte(struct vm_area_struct *vma,
+		unsigned long addr, pte_t *ptep, pte_t orig_pte)
 {
-	swp_entry_t entry = pte_to_swp_entry(ptep_get(src_pte));
-	struct page *page = pfn_swap_entry_to_page(entry);
+	struct page *page = pfn_swap_entry_to_page(pte_to_swp_entry(orig_pte));
+	struct folio *folio = page_folio(page);
 
-	if (trylock_page(page)) {
-		restore_exclusive_pte(vma, page, addr, src_pte);
-		unlock_page(page);
+	if (folio_trylock(folio)) {
+		restore_exclusive_pte(vma, folio, page, addr, ptep, orig_pte);
+		folio_unlock(folio);
 		return 0;
 	}
 
@@ -871,7 +869,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
 		 * (ie. COW) mappings.
 		 */
 		VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags));
-		if (try_restore_exclusive_pte(src_pte, src_vma, addr))
+		if (try_restore_exclusive_pte(src_vma, addr, src_pte, orig_pte))
 			return -EBUSY;
 		return -ENOENT;
 	} else if (is_pte_marker_entry(entry)) {
@@ -3979,7 +3977,8 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf)
 	vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
 				&vmf->ptl);
 	if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte)))
-		restore_exclusive_pte(vma, vmf->page, vmf->address, vmf->pte);
+		restore_exclusive_pte(vma, folio, vmf->page, vmf->address,
+				      vmf->pte, vmf->orig_pte);
 
 	if (vmf->pte)
 		pte_unmap_unlock(vmf->pte, vmf->ptl);
Signature · 1 line
2.48.1
#10 lore raw

PATCH 4/4 mm/memory: document restore_exclusive_pte()

In reply to David Hildenbrand

Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future.

---
 mm/memory.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
mm/memory.c +25 -0
index 46956994aaff..caaae8df11a9 100644
@@ -718,6 +718,31 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma,
 }
 #endif
 
+/**
+ * restore_exclusive_pte - Restore a device-exclusive entry
+ * @vma: VMA covering @address
+ * @folio: the mapped folio
+ * @page: the mapped folio page
+ * @address: the virtual address
+ * @ptep: PTE pointer into the locked page table mapping the folio page
+ * @orig_pte: PTE value at @ptep
+ *
+ * Restore a device-exclusive non-swap entry to an ordinary present PTE.
+ *
+ * The folio and the page table must be locked, and MMU notifiers must have
+ * been called to invalidate any (exclusive) device mappings. In case of
+ * fork(), MMU_NOTIFY_PROTECTION_PAGE is triggered, and in case of a page
+ * fault MMU_NOTIFY_EXCLUSIVE is triggered.
+ *
+ * Locking the folio makes sure that anybody who just converted the PTE to
+ * a device-private entry can map it into the device, before unlocking it; so
+ * the folio lock prevents concurrent conversion to device-exclusive.
+ *
+ * TODO: the folio lock does not protect against all cases of concurrent
+ * page table modifications (e.g., MADV_DONTNEED, mprotect), so device drivers
+ * must already use MMU notifiers to sync against any concurrent changes
+ * Maybe the requirement for the folio lock can be dropped in the future.
+ */
 static void restore_exclusive_pte(struct vm_area_struct *vma,
 		struct folio *folio, struct page *page, unsigned long address,
 		pte_t *ptep, pte_t orig_pte)
Signature · 1 line
2.48.1
#11 lore raw

Re: [PATCH v1 4/4] mm/memory: document restore_exclusive_pte()

Quoted 2 lines · David Hildenbrand

Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future.

Sorry, only just catching up on your other thread. The folio lock was to ensure the GPU got a chance to make forward progress by mapping the page. Without it the CPU could immediately invalidate the entry before the GPU had a chance to retry the fault.

Obviously performance wise having such thrashing is terrible, so should really be avoided by userspace, but the lock at least allowed such programs to complete.

Quoted 22 lines
---
 mm/memory.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
mm/memory.c +9 -0
index 46956994aaff..caaae8df11a9 100644
@@ -718,6 +718,31 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma,
 }
 #endif
 
+/**
+ * restore_exclusive_pte - Restore a device-exclusive entry
+ * @vma: VMA covering @address
+ * @folio: the mapped folio
+ * @page: the mapped folio page
+ * @address: the virtual address
+ * @ptep: PTE pointer into the locked page table mapping the folio page
+ * @orig_pte: PTE value at @ptep
+ *

mm/memory.c struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, · line 739

Quoted 10 lines
mm/memory.c +10 -0
+ * Restore a device-exclusive non-swap entry to an ordinary present PTE.
+ *
+ * The folio and the page table must be locked, and MMU notifiers must have
+ * been called to invalidate any (exclusive) device mappings. In case of
+ * fork(), MMU_NOTIFY_PROTECTION_PAGE is triggered, and in case of a page
+ * fault MMU_NOTIFY_EXCLUSIVE is triggered.
+ *
+ * Locking the folio makes sure that anybody who just converted the PTE to
+ * a device-private entry can map it into the device, before unlocking it; so
+ * the folio lock prevents concurrent conversion to device-exclusive.

I don't quite follow this - a concurrent conversion would already fail because the GUP in make_device_exclusive_range() would most likely cause an unexpected reference during the migration. And if a migration entry has already been installed for the device private PTE conversion then make_device_exclusive_range() will skip it as a non-present entry anyway.

However s/device-private/device-exclusive/ makes sense - the intent was to allow the device to map it before a call to restore_exclusive_pte() (ie. a CPU fault) could convert it back to a normal PTE.

mm/memory.c struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, · line 743

Quoted 3 lines
mm/memory.c +3 -0
+ * TODO: the folio lock does not protect against all cases of concurrent
+ * page table modifications (e.g., MADV_DONTNEED, mprotect), so device drivers
+ * must already use MMU notifiers to sync against any concurrent changes

Right. It's expected drivers are using MMU notifiers to keep page tables in sync, same as for hmm_range_fault().

Quoted 8 lines
mm/memory.c +2 -0
+ * Maybe the requirement for the folio lock can be dropped in the future.
+ */
 static void restore_exclusive_pte(struct vm_area_struct *vma,
 		struct folio *folio, struct page *page, unsigned long address,
 		pte_t *ptep, pte_t orig_pte)
Signature · 1 line
2.48.1
#12 lore raw

Re: [PATCH v1 4/4] mm/memory: document restore_exclusive_pte()

Quoted 11 lines · Alistair Popple
Quoted 2 lines · David Hildenbrand

Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future.

Sorry, only just catching up on your other thread. The folio lock was to ensure the GPU got a chance to make forward progress by mapping the page. Without it the CPU could immediately invalidate the entry before the GPU had a chance to retry the fault.

Quoted 1 line

Obviously performance wise having such thrashing is terrible, so should

really be avoided by userspace, but the lock at least allowed such programs to complete.

Thanks for the clarification. So it's relevant that the MMU notifier in remove_device_exclusive_entry() is sent after taking the folio lock.

However, as soon as we drop the folio lock, remove_device_exclusive_entry() will become active, lock the folio and trigger the MMU notifier.

So the time it is actually mapped into the device is rather

Quoted 31 lines
Quoted 31 lines
---
  mm/memory.c | 25 +++++++++++++++++++++++++
  1 file changed, 25 insertions(+)
mm/memory.c +17 -0
index 46956994aaff..caaae8df11a9 100644
@@ -718,6 +718,31 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma,
  }
  #endif
 
+/**
+ * restore_exclusive_pte - Restore a device-exclusive entry
+ * @vma: VMA covering @address
+ * @folio: the mapped folio
+ * @page: the mapped folio page
+ * @address: the virtual address
+ * @ptep: PTE pointer into the locked page table mapping the folio page
+ * @orig_pte: PTE value at @ptep
+ *
+ * Restore a device-exclusive non-swap entry to an ordinary present PTE.
+ *
+ * The folio and the page table must be locked, and MMU notifiers must have
+ * been called to invalidate any (exclusive) device mappings. In case of
+ * fork(), MMU_NOTIFY_PROTECTION_PAGE is triggered, and in case of a page
+ * fault MMU_NOTIFY_EXCLUSIVE is triggered.
+ *
+ * Locking the folio makes sure that anybody who just converted the PTE to
Quoted 8 lines

mm/memory.c struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, · line 739

Quoted 2 lines
mm/memory.c +2 -0
+ * a device-private entry can map it into the device, before unlocking it; so
+ * the folio lock prevents concurrent conversion to device-exclusive.

I don't quite follow this - a concurrent conversion would already fail because the GUP in make_device_exclusive_range() would most likely cause an unexpected reference during the migration. And if a migration entry has already been installed for the device private PTE conversion then make_device_exclusive_range() will skip it as a non-present entry anyway.

Sorry, I meant "device-exclusive", so migration is not a concern.

Quoted 11 lines

However s/device-private/device-exclusive/ makes sense - the intent was to allow the device to map it before a call to restore_exclusive_pte() (ie. a CPU fault) could convert it back to a normal PTE.

mm/memory.c struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, · line 743

Quoted 3 lines
mm/memory.c +3 -0
+ * TODO: the folio lock does not protect against all cases of concurrent
+ * page table modifications (e.g., MADV_DONTNEED, mprotect), so device drivers
+ * must already use MMU notifiers to sync against any concurrent changes

Right. It's expected drivers are using MMU notifiers to keep page tables in sync, same as for hmm_range_fault().

Let me try to rephrase it given that the folio lock is purely to guarantee forward-progress, not for correctness; that's what MMU notifiers must be used for.

Signature · 3 lines
Cheers,

David / dhildenb
#13 lore raw

Re: [PATCH v1 4/4] mm/memory: document restore_exclusive_pte()

Quoted 13 lines · David Hildenbrand
Quoted 11 lines · Alistair Popple
Quoted 2 lines · David Hildenbrand

Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future.

Sorry, only just catching up on your other thread. The folio lock was to ensure the GPU got a chance to make forward progress by mapping the page. Without it the CPU could immediately invalidate the entry before the GPU had a chance to retry the fault.

Quoted 1 line

Obviously performance wise having such thrashing is terrible, so should

really be avoided by userspace, but the lock at least allowed such programs to complete.

Quoted 7 lines

Thanks for the clarification. So it's relevant that the MMU notifier in remove_device_exclusive_entry() is sent after taking the folio lock.

However, as soon as we drop the folio lock, remove_device_exclusive_entry() will become active, lock the folio and trigger the MMU notifier.

So the time it is actually mapped into the device is rather

Looks like you cut off a bit here (or mail transport did that somewhere), but see my other reply I don't think this is a legit use-case. So we don't have to worry. Well beyond documenting that if userspace concurrently thrashes the same page with both device atomics and cpu access it will stall real bad.
-Sima

Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#14 lore raw

Re: [PATCH v1 4/4] mm/memory: document restore_exclusive_pte()

Quoted 14 lines · Simona Vetter
Quoted 13 lines · David Hildenbrand
Quoted 11 lines · Alistair Popple
Quoted 2 lines · David Hildenbrand

Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future.

Sorry, only just catching up on your other thread. The folio lock was to ensure the GPU got a chance to make forward progress by mapping the page. Without it the CPU could immediately invalidate the entry before the GPU had a chance to retry the fault.

Quoted 1 line

Obviously performance wise having such thrashing is terrible, so should

really be avoided by userspace, but the lock at least allowed such programs to complete.

Quoted 7 lines
Quoted 7 lines

Thanks for the clarification. So it's relevant that the MMU notifier in remove_device_exclusive_entry() is sent after taking the folio lock.

However, as soon as we drop the folio lock, remove_device_exclusive_entry() will become active, lock the folio and trigger the MMU notifier.

So the time it is actually mapped into the device is rather

I meant to say "rather short." :)

Quoted 4 lines

Looks like you cut off a bit here (or mail transport did that somewhere), but see my other reply I don't think this is a legit use-case. So we don't have to worry.

In that case, we would need the folio lock in the future.

Quoted 3 lines

Well beyond documenting that if userspace concurrently thrashes the same page with both device atomics and cpu access it will stall real bad.

I'm curious, is locking between device-cpu or device-device something that can happen frequently? In that case, you would get that trashing naturally?

Signature · 3 lines
Cheers,

David / dhildenb
#15 lore raw

Re: [PATCH v1 4/4] mm/memory: document restore_exclusive_pte()

Quoted 30 lines · David Hildenbrand
Quoted 14 lines · Simona Vetter
Quoted 13 lines · David Hildenbrand
Quoted 11 lines · Alistair Popple
Quoted 2 lines · David Hildenbrand

Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future.

Sorry, only just catching up on your other thread. The folio lock was to ensure the GPU got a chance to make forward progress by mapping the page. Without it the CPU could immediately invalidate the entry before the GPU had a chance to retry the fault.

Quoted 1 line

Obviously performance wise having such thrashing is terrible, so should

really be avoided by userspace, but the lock at least allowed such programs to complete.

Quoted 7 lines
Quoted 7 lines

Thanks for the clarification. So it's relevant that the MMU notifier in remove_device_exclusive_entry() is sent after taking the folio lock.

However, as soon as we drop the folio lock, remove_device_exclusive_entry() will become active, lock the folio and trigger the MMU notifier.

So the time it is actually mapped into the device is rather

I meant to say "rather short." :)

Quoted 4 lines

Looks like you cut off a bit here (or mail transport did that somewhere), but see my other reply I don't think this is a legit use-case. So we don't have to worry.

Quoted 8 lines

In that case, we would need the folio lock in the future.

Quoted 3 lines

Well beyond documenting that if userspace concurrently thrashes the same page with both device atomics and cpu access it will stall real bad.

I'm curious, is locking between device-cpu or device-device something that can happen frequently? In that case, you would get that trashing naturally?

It results in terrible performance so in practice it isn't something that I've seen except when stress testing the driver. Those stress tests were useful for exposing a range of kernel/driver bugs/issues though, and despite the short time it is mapped the lock was sufficient to allow atomic thrashing tests to complete vs. having the device fault endlessly.

So unless it's making things more difficult I'd rather keep the lock.

Quoted 5 lines
Signature · 3 lines
Cheers,

David / dhildenb
#16 lore raw

Re: [PATCH v1 4/4] mm/memory: document restore_exclusive_pte()

Quoted 37 lines · Alistair Popple
Quoted 36 lines · David Hildenbrand
Quoted 14 lines · Simona Vetter
Quoted 13 lines · David Hildenbrand
Quoted 11 lines · Alistair Popple
Quoted 2 lines · David Hildenbrand

Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future.

Sorry, only just catching up on your other thread. The folio lock was to ensure the GPU got a chance to make forward progress by mapping the page. Without it the CPU could immediately invalidate the entry before the GPU had a chance to retry the fault.

Quoted 1 line

Obviously performance wise having such thrashing is terrible, so should

really be avoided by userspace, but the lock at least allowed such programs to complete.

Quoted 7 lines
Quoted 7 lines

Thanks for the clarification. So it's relevant that the MMU notifier in remove_device_exclusive_entry() is sent after taking the folio lock.

However, as soon as we drop the folio lock, remove_device_exclusive_entry() will become active, lock the folio and trigger the MMU notifier.

So the time it is actually mapped into the device is rather

I meant to say "rather short." :)

Quoted 4 lines

Looks like you cut off a bit here (or mail transport did that somewhere), but see my other reply I don't think this is a legit use-case. So we don't have to worry.

In that case, we would need the folio lock in the future.

Quoted 3 lines

Well beyond documenting that if userspace concurrently thrashes the same page with both device atomics and cpu access it will stall real bad.

Quoted 8 lines
Quoted 2 lines

I'm curious, is locking between device-cpu or device-device something that can happen frequently? In that case, you would get that trashing naturally?

It results in terrible performance so in practice it isn't something that I've seen except when stress testing the driver. Those stress tests were useful for exposing a range of kernel/driver bugs/issues though, and despite the short time it is mapped the lock was sufficient to allow atomic thrashing tests to complete vs. having the device fault endlessly.

Yeah the practical use-case of device atomics is that they alternate, as the processing shifts between the cpu and the gpu. Classic one is when you generate variable amounts of data per input block that you fill into a big preallocated array, and the atomic counter is your dumb-as-rock malloc for that. The atomic moves as the generating/consuming of that data moves around the system (and needs a fault each time it moves), but you really never want to have concurrent access going on. Any thrashing concurrent access is just broken userspace (or a driver stress test).

Quoted 1 line

So unless it's making things more difficult I'd rather keep the lock.

But why do these stress-tests need to complete? We have a lot of these in our gpu test suite, and we just nuke them after a short timeout if nothing blows up. Concurrently hammering the same page from both gpu and cpu really isn't something that should have any forward progress gurantees imo. And I feel like too much locking just risks us hiding some much more nasty (design) bugs underneath - I've had that experience reviewing both amdkfd and the in-work xe code. So my preference for gpu interacting with core mm is that we have the least amount of locking to make sure we really don't have a breaking design impendence mismatch going on.

Cheers, Sima

Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#17 lore raw

Re: [PATCH v1 4/4] mm/memory: document restore_exclusive_pte()

In reply to Alistair Popple
Quoted 4 lines · Alistair Popple
Quoted 2 lines · David Hildenbrand

Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future.

Quoted 8 lines

Sorry, only just catching up on your other thread. The folio lock was to ensure the GPU got a chance to make forward progress by mapping the page. Without it the CPU could immediately invalidate the entry before the GPU had a chance to retry the fault.

Obviously performance wise having such thrashing is terrible, so should really be avoided by userspace, but the lock at least allowed such programs to complete.

Imo this is not a legit use-case. If userspace concurrently (instead of clearly alternating) uses the same 4k page for gpu atomics and on the cpu, it just gets to keep the fallout.

Plus there's no guarantee that we hold the folio_lock long enough for the gpu to actually complete the atomic, so this isn't even really helping with forward progress even if this somehow would be a legit usecase.

But this is also why thp is potentially an issue, because if thp constantly creates pmd entries that potentially causes false sharing and we do have thrashing that shouldn't happen.
-Sima

Quoted 58 lines
Quoted 22 lines
---
 mm/memory.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
mm/memory.c +9 -0
index 46956994aaff..caaae8df11a9 100644
@@ -718,6 +718,31 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma,
 }
 #endif
 
+/**
+ * restore_exclusive_pte - Restore a device-exclusive entry
+ * @vma: VMA covering @address
+ * @folio: the mapped folio
+ * @page: the mapped folio page
+ * @address: the virtual address
+ * @ptep: PTE pointer into the locked page table mapping the folio page
+ * @orig_pte: PTE value at @ptep
+ *

mm/memory.c struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, · line 739

Quoted 10 lines
mm/memory.c +10 -0
+ * Restore a device-exclusive non-swap entry to an ordinary present PTE.
+ *
+ * The folio and the page table must be locked, and MMU notifiers must have
+ * been called to invalidate any (exclusive) device mappings. In case of
+ * fork(), MMU_NOTIFY_PROTECTION_PAGE is triggered, and in case of a page
+ * fault MMU_NOTIFY_EXCLUSIVE is triggered.
+ *
+ * Locking the folio makes sure that anybody who just converted the PTE to
+ * a device-private entry can map it into the device, before unlocking it; so
+ * the folio lock prevents concurrent conversion to device-exclusive.

I don't quite follow this - a concurrent conversion would already fail because the GUP in make_device_exclusive_range() would most likely cause an unexpected reference during the migration. And if a migration entry has already been installed for the device private PTE conversion then make_device_exclusive_range() will skip it as a non-present entry anyway.

However s/device-private/device-exclusive/ makes sense - the intent was to allow the device to map it before a call to restore_exclusive_pte() (ie. a CPU fault) could convert it back to a normal PTE.

mm/memory.c struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, · line 743

Quoted 3 lines
mm/memory.c +3 -0
+ * TODO: the folio lock does not protect against all cases of concurrent
+ * page table modifications (e.g., MADV_DONTNEED, mprotect), so device drivers
+ * must already use MMU notifiers to sync against any concurrent changes

Right. It's expected drivers are using MMU notifiers to keep page tables in sync, same as for hmm_range_fault().

Quoted 8 lines
mm/memory.c +2 -0
+ * Maybe the requirement for the folio lock can be dropped in the future.
+ */
 static void restore_exclusive_pte(struct vm_area_struct *vma,
 		struct folio *folio, struct page *page, unsigned long address,
 		pte_t *ptep, pte_t orig_pte)
Signature · 1 line
2.48.1
Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#18 lore raw

Re: [PATCH v1 4/4] mm/memory: document restore_exclusive_pte()

Quoted 14 lines · Simona Vetter
Quoted 4 lines · Alistair Popple
Quoted 2 lines · David Hildenbrand

Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future.

Quoted 8 lines

Sorry, only just catching up on your other thread. The folio lock was to ensure the GPU got a chance to make forward progress by mapping the page. Without it the CPU could immediately invalidate the entry before the GPU had a chance to retry the fault.

Obviously performance wise having such thrashing is terrible, so should really be avoided by userspace, but the lock at least allowed such programs to complete.

Quoted 7 lines

Imo this is not a legit use-case. If userspace concurrently (instead of clearly alternating) uses the same 4k page for gpu atomics and on the cpu, it just gets to keep the fallout.

Plus there's no guarantee that we hold the folio_lock long enough for the gpu to actually complete the atomic, so this isn't even really helping with forward progress even if this somehow would be a legit usecase.

Yes, agree it's not a legit real world use case. In practice though it was useful for testing this and other driver code by thrashing to generate a lot device/cpu faults and invalidations. Obviously "just for testing" is not a great justification though, so if it's causing problems we could get rid of it.

Quoted 3 lines

But this is also why thp is potentially an issue, because if thp constantly creates pmd entries that potentially causes false sharing and we do have thrashing that shouldn't happen.

Yeah, I don't we should extend this to thps.

- Alistair

Quoted 65 lines

-Sima

Quoted 58 lines
Quoted 22 lines
---
 mm/memory.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
mm/memory.c +9 -0
index 46956994aaff..caaae8df11a9 100644
@@ -718,6 +718,31 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma,
 }
 #endif
 
+/**
+ * restore_exclusive_pte - Restore a device-exclusive entry
+ * @vma: VMA covering @address
+ * @folio: the mapped folio
+ * @page: the mapped folio page
+ * @address: the virtual address
+ * @ptep: PTE pointer into the locked page table mapping the folio page
+ * @orig_pte: PTE value at @ptep
+ *

mm/memory.c struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, · line 739

Quoted 10 lines
mm/memory.c +10 -0
+ * Restore a device-exclusive non-swap entry to an ordinary present PTE.
+ *
+ * The folio and the page table must be locked, and MMU notifiers must have
+ * been called to invalidate any (exclusive) device mappings. In case of
+ * fork(), MMU_NOTIFY_PROTECTION_PAGE is triggered, and in case of a page
+ * fault MMU_NOTIFY_EXCLUSIVE is triggered.
+ *
+ * Locking the folio makes sure that anybody who just converted the PTE to
+ * a device-private entry can map it into the device, before unlocking it; so
+ * the folio lock prevents concurrent conversion to device-exclusive.

I don't quite follow this - a concurrent conversion would already fail because the GUP in make_device_exclusive_range() would most likely cause an unexpected reference during the migration. And if a migration entry has already been installed for the device private PTE conversion then make_device_exclusive_range() will skip it as a non-present entry anyway.

However s/device-private/device-exclusive/ makes sense - the intent was to allow the device to map it before a call to restore_exclusive_pte() (ie. a CPU fault) could convert it back to a normal PTE.

mm/memory.c struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, · line 743

Quoted 3 lines
mm/memory.c +3 -0
+ * TODO: the folio lock does not protect against all cases of concurrent
+ * page table modifications (e.g., MADV_DONTNEED, mprotect), so device drivers
+ * must already use MMU notifiers to sync against any concurrent changes

Right. It's expected drivers are using MMU notifiers to keep page tables in sync, same as for hmm_range_fault().

Quoted 8 lines
mm/memory.c +2 -0
+ * Maybe the requirement for the folio lock can be dropped in the future.
+ */
 static void restore_exclusive_pte(struct vm_area_struct *vma,
 		struct folio *folio, struct page *page, unsigned long address,
 		pte_t *ptep, pte_t orig_pte)
Signature · 1 line
2.48.1
Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#19 lore raw

Re: [PATCH v1 4/4] mm/memory: document restore_exclusive_pte()

Quoted 19 lines · Alistair Popple
Quoted 18 lines · Simona Vetter
Quoted 4 lines · Alistair Popple
Quoted 2 lines · David Hildenbrand

Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future.

Quoted 8 lines

Sorry, only just catching up on your other thread. The folio lock was to ensure the GPU got a chance to make forward progress by mapping the page. Without it the CPU could immediately invalidate the entry before the GPU had a chance to retry the fault.

Obviously performance wise having such thrashing is terrible, so should really be avoided by userspace, but the lock at least allowed such programs to complete.

Imo this is not a legit use-case. If userspace concurrently (instead of clearly alternating) uses the same 4k page for gpu atomics and on the cpu, it just gets to keep the fallout.

Quoted 8 lines
Quoted 3 lines

Plus there's no guarantee that we hold the folio_lock long enough for the gpu to actually complete the atomic, so this isn't even really helping with forward progress even if this somehow would be a legit usecase.

Yes, agree it's not a legit real world use case. In practice though it was useful for testing this and other driver code by thrashing to generate a lot device/cpu faults and invalidations. Obviously "just for testing" is not a great justification though, so if it's causing problems we could get rid of it.

Okay, I'll make that clear in the documentation. Getting rid of the folio lock might be really beneficial in some cases.

Signature · 3 lines
Cheers,

David / dhildenb