Skip to contentSkip to conversation

drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

PATCH In review Philip Yang 4 participants 28 messages Sep 13, 2018 – Oct 4, 2018 27 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Replace our MMU notifier with hmm_mirror_ops.sync_cpu_device_pagetables callback. Enable CONFIG_HMM and CONFIG_HMM_MIRROR as a dependency in DRM_AMDGPU_USERPTR Kconfig.

It supports both KFD userptr and gfx userptr paths.

This depends on several HMM patchset from Jérôme Glisse queued for upstream.

Change-Id Ie62c3c5e3c5b8521ab3b438d1eff2aa2a003835e
---
 drivers/gpu/drm/amd/amdgpu/Kconfig     |   6 +-
 drivers/gpu/drm/amd/amdgpu/Makefile    |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 121 ++++++++++++++-------------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h |   2 +-
 4 files changed, 56 insertions(+), 75 deletions(-)
drivers/gpu/drm/amd/amdgpu/Kconfig +3 -3
index 9221e54..960a633 100644
@@ -26,10 +26,10 @@ config DRM_AMDGPU_CIK
 config DRM_AMDGPU_USERPTR
 	bool "Always enable userptr write support"
 	depends on DRM_AMDGPU
-	select MMU_NOTIFIER
+	select HMM_MIRROR
 	help
-	  This option selects CONFIG_MMU_NOTIFIER if it isn't already
-	  selected to enabled full userptr support.
+	  This option selects CONFIG_HMM and CONFIG_HMM_MIRROR if it
+	  isn't already selected to enabled full userptr support.
 
 config DRM_AMDGPU_GART_DEBUGFS
 	bool "Allow GART access through debugfs"
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 138cb78..c1e5d43 100644
@@ -171,7 +171,7 @@ endif
 amdgpu-$(CONFIG_COMPAT) += amdgpu_ioc32.o
 amdgpu-$(CONFIG_VGA_SWITCHEROO) += amdgpu_atpx_handler.o
 amdgpu-$(CONFIG_ACPI) += amdgpu_acpi.o
-amdgpu-$(CONFIG_MMU_NOTIFIER) += amdgpu_mn.o
+amdgpu-$(CONFIG_HMM) += amdgpu_mn.o
 
 include $(FULL_AMD_PATH)/powerplay/Makefile
 
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +51 -70

250 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +1 -1
index eb0f432..0e27526 100644
@@ -34,7 +34,7 @@ enum amdgpu_mn_type {
 	AMDGPU_MN_TYPE_HSA,
 };
 
-#if defined(CONFIG_MMU_NOTIFIER)
+#if defined(CONFIG_HMM)
 void amdgpu_mn_lock(struct amdgpu_mn *mn);
 void amdgpu_mn_unlock(struct amdgpu_mn *mn);
 struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1536871954-8451-1-git-send-email-Philip.Yang-5C7GfCeVMHo@public.gmane.org

#2 lore raw

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c

Quoted 63 lines · Philip Yang

Replace our MMU notifier with hmm_mirror_ops.sync_cpu_device_pagetables callback. Enable CONFIG_HMM and CONFIG_HMM_MIRROR as a dependency in DRM_AMDGPU_USERPTR Kconfig.

It supports both KFD userptr and gfx userptr paths.

This depends on several HMM patchset from Jérôme Glisse queued for upstream.

Change-Id Ie62c3c5e3c5b8521ab3b438d1eff2aa2a003835e
---
 drivers/gpu/drm/amd/amdgpu/Kconfig     |   6 +-
 drivers/gpu/drm/amd/amdgpu/Makefile    |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 121 ++++++++++++++-------------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h |   2 +-
 4 files changed, 56 insertions(+), 75 deletions(-)
drivers/gpu/drm/amd/amdgpu/Kconfig +3 -3
index 9221e54..960a633 100644
@@ -26,10 +26,10 @@ config DRM_AMDGPU_CIK
 config DRM_AMDGPU_USERPTR
 	bool "Always enable userptr write support"
 	depends on DRM_AMDGPU
-	select MMU_NOTIFIER
+	select HMM_MIRROR
 	help
-	  This option selects CONFIG_MMU_NOTIFIER if it isn't already
-	  selected to enabled full userptr support.
+	  This option selects CONFIG_HMM and CONFIG_HMM_MIRROR if it
+	  isn't already selected to enabled full userptr support.
 
 config DRM_AMDGPU_GART_DEBUGFS
 	bool "Allow GART access through debugfs"
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 138cb78..c1e5d43 100644
@@ -171,7 +171,7 @@ endif
 amdgpu-$(CONFIG_COMPAT) += amdgpu_ioc32.o
 amdgpu-$(CONFIG_VGA_SWITCHEROO) += amdgpu_atpx_handler.o
 amdgpu-$(CONFIG_ACPI) += amdgpu_acpi.o
-amdgpu-$(CONFIG_MMU_NOTIFIER) += amdgpu_mn.o
+amdgpu-$(CONFIG_HMM) += amdgpu_mn.o
 
 include $(FULL_AMD_PATH)/powerplay/Makefile
 
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -1
index e55508b..ad52f34 100644
@@ -45,7 +45,7 @@
 
 #include <linux/firmware.h>
 #include <linux/module.h>
-#include <linux/mmu_notifier.h>
+#include <linux/hmm.h>
 #include <linux/interval_tree.h>
 #include <drm/drmP.h>
 #include <drm/drm.h>
@@ -66,6 +66,7 @@

Need to remove @mn documentation.

Quoted 36 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +5 -2
  * @objects: interval tree containing amdgpu_mn_nodes
  * @read_lock: mutex for recursive locking of @lock
  * @recursion: depth of recursion
+ * @mirror: HMM mirror function support
  *
  * Data for each amdgpu device and process address space.
  */
@@ -73,7 +74,6 @@ struct amdgpu_mn {
 	/* constant after initialisation */
 	struct amdgpu_device	*adev;
 	struct mm_struct	*mm;
-	struct mmu_notifier	mn;
 	enum amdgpu_mn_type	type;
 
 	/* only used on destruction */
@@ -87,6 +87,9 @@ struct amdgpu_mn {
 	struct rb_root_cached	objects;
 	struct mutex		read_lock;
 	atomic_t		recursion;
+
+	/* HMM mirror */
+	struct hmm_mirror	mirror;
 };
 
 /**
@@ -103,7 +106,7 @@ struct amdgpu_mn_node {
 };
 
 /**
- * amdgpu_mn_destroy - destroy the MMU notifier
+ * amdgpu_mn_destroy - destroy the HMM mirror
  *
  * @work: previously sheduled work item
  *
@@ -129,28 +132,26 @@ static void amdgpu_mn_destroy(struct work_struct *work)
 	}

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static void amdgpu_mn_destroy(struct work_struct *work) · line 141

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
 	up_write(&amn->lock);
 	mutex_unlock(&adev->mn_lock);
-	mmu_notifier_unregister_no_release(&amn->mn, amn->mm);
+	hmm_mirror_unregister(&amn->mirror);
+
 	kfree(amn);
 }
 
 /**
  * amdgpu_mn_release - callback to notify about mm destruction

Update the function name in the comment.

Quoted 54 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +12 -17
  *
- * @mn: our notifier
- * @mm: the mm this callback is about
+ * @mirror: the HMM mirror (mm) this callback is about
  *
- * Shedule a work item to lazy destroy our notifier.
+ * Shedule a work item to lazy destroy HMM mirror.
  */
-static void amdgpu_mn_release(struct mmu_notifier *mn,
-			      struct mm_struct *mm)
+static void amdgpu_hmm_mirror_release(struct hmm_mirror *mirror)
 {
-	struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+	struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, mirror);
 
 	INIT_WORK(&amn->work, amdgpu_mn_destroy);
 	schedule_work(&amn->work);
 }
 
-
 /**
  * amdgpu_mn_lock - take the write side lock for this notifier
  *
@@ -237,21 +238,19 @@ static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
 /**
  * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
  *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
+ * @mirror: the hmm_mirror (mm) is about to update
+ * @update: the update start, end address
  *
  * Block for operations on BOs to finish and mark pages as accessed and
  * potentially dirty.
  */
-static int amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
-						 struct mm_struct *mm,
-						 unsigned long start,
-						 unsigned long end,
-						 bool blockable)
+static int amdgpu_mn_invalidate_range_start_gfx(struct hmm_mirror *mirror,
+			const struct hmm_update *update)
 {
-	struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+	struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, mirror);
+	unsigned long start = update->start;
+	unsigned long end = update->end;
+	bool blockable = update->blockable;
 	struct interval_tree_node *it;
 
 	/* notification is exclusive, but interval is inclusive */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static int amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn, · line 281

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -0
@@ -278,28 +277,28 @@ static int amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
 		amdgpu_mn_invalidate_node(node, start, end);
 	}
 
+	amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

Quoted 152 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +29 -49
 	return 0;
 }
 
 /**
  * amdgpu_mn_invalidate_range_start_hsa - callback to notify about mm change
  *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
+ * @mirror: the hmm_mirror (mm) is about to update
+ * @update: the update start, end address
  *
  * We temporarily evict all BOs between start and end. This
  * necessitates evicting all user-mode queues of the process. The BOs
  * are restorted in amdgpu_mn_invalidate_range_end_hsa.
  */
-static int amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
-						 struct mm_struct *mm,
-						 unsigned long start,
-						 unsigned long end,
-						 bool blockable)
+static int amdgpu_mn_invalidate_range_start_hsa(struct hmm_mirror *mirror,
+			const struct hmm_update *update)
 {
-	struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+	struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, mirror);
+	unsigned long start = update->start;
+	unsigned long end = update->end;
+	bool blockable = update->blockable;
 	struct interval_tree_node *it;
 
 	/* notification is exclusive, but interval is inclusive */
@@ -326,59 +325,41 @@ static int amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
 
 			if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
 							 start, end))
-				amdgpu_amdkfd_evict_userptr(mem, mm);
+				amdgpu_amdkfd_evict_userptr(mem, amn->mm);
 		}
 	}
 
+	amdgpu_mn_read_unlock(amn);
+
 	return 0;
 }
 
-/**
- * amdgpu_mn_invalidate_range_end - callback to notify about mm change
- *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
- *
- * Release the lock again to allow new command submissions.
+/* Low bits of any reasonable mm pointer will be unused due to struct
+ * alignment. Use these bits to make a unique key from the mm pointer
+ * and notifier type.
  */
-static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
-					   struct mm_struct *mm,
-					   unsigned long start,
-					   unsigned long end)
-{
-	struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
-
-	amdgpu_mn_read_unlock(amn);
-}
+#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
 
-static const struct mmu_notifier_ops amdgpu_mn_ops[] = {
+static struct hmm_mirror_ops amdgpu_hmm_mirror_ops[] = {
 	[AMDGPU_MN_TYPE_GFX] = {
-		.release = amdgpu_mn_release,
-		.invalidate_range_start = amdgpu_mn_invalidate_range_start_gfx,
-		.invalidate_range_end = amdgpu_mn_invalidate_range_end,
+		.sync_cpu_device_pagetables =
+				amdgpu_mn_invalidate_range_start_gfx,
+		.release = amdgpu_hmm_mirror_release
 	},
 	[AMDGPU_MN_TYPE_HSA] = {
-		.release = amdgpu_mn_release,
-		.invalidate_range_start = amdgpu_mn_invalidate_range_start_hsa,
-		.invalidate_range_end = amdgpu_mn_invalidate_range_end,
+		.sync_cpu_device_pagetables =
+				amdgpu_mn_invalidate_range_start_hsa,
+		.release = amdgpu_hmm_mirror_release
 	},
 };
 
-/* Low bits of any reasonable mm pointer will be unused due to struct
- * alignment. Use these bits to make a unique key from the mm pointer
- * and notifier type.
- */
-#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
-
 /**
- * amdgpu_mn_get - create notifier context
+ * amdgpu_mn_get - create HMM mirror context
  *
  * @adev: amdgpu device pointer
  * @type: type of MMU notifier context
  *
- * Creates a notifier context for current->mm.
+ * Creates a HMM mirror context for current->mm.
  */
 struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
 				enum amdgpu_mn_type type)
@@ -408,12 +389,12 @@ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
 	amn->mm = mm;
 	init_rwsem(&amn->lock);
 	amn->type = type;
-	amn->mn.ops = &amdgpu_mn_ops[type];
 	amn->objects = RB_ROOT_CACHED;
 	mutex_init(&amn->read_lock);
 	atomic_set(&amn->recursion, 0);
 
-	r = __mmu_notifier_register(&amn->mn, mm);
+	amn->mirror.ops = &amdgpu_hmm_mirror_ops[type];
+	r = hmm_mirror_register(&amn->mirror, mm);
 	if (r)
 		goto free_amn;
 
@@ -439,7 +420,7 @@ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
  * @bo: amdgpu buffer object
  * @addr: userptr addr we should monitor
  *
- * Registers an MMU notifier for the given BO at the specified address.
+ * Registers an HMM mirror for the given BO at the specified address.
  * Returns 0 on success, -ERRNO if anything goes wrong.
  */
 int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
@@ -495,11 +476,11 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
 }
 
 /**
- * amdgpu_mn_unregister - unregister a BO for notifier updates
+ * amdgpu_mn_unregister - unregister a BO for HMM mirror updates
  *
  * @bo: amdgpu buffer object
  *
- * Remove any registration of MMU notifier updates from the buffer object.
+ * Remove any registration of HMM mirror updates from the buffer object.
  */
 void amdgpu_mn_unregister(struct amdgpu_bo *bo)
 {
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +0 -0
index eb0f432..0e27526 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h enum amdgpu_mn_type { · line 40

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +1 -1
@@ -34,7 +34,7 @@ enum amdgpu_mn_type {
 	AMDGPU_MN_TYPE_HSA,
 };
 
-#if defined(CONFIG_MMU_NOTIFIER)
+#if defined(CONFIG_HMM)
 void amdgpu_mn_lock(struct amdgpu_mn *mn);
 void amdgpu_mn_unlock(struct amdgpu_mn *mn);
 struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 9d6717ac-23f0-7beb-6e41-58c6e32acdf8-5C7GfCeVMHo@public.gmane.org

#4 lore raw

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 171 lines · Philip Yang

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c

Quoted 63 lines

Replace our MMU notifier with hmm_mirror_ops.sync_cpu_device_pagetables callback. Enable CONFIG_HMM and CONFIG_HMM_MIRROR as a dependency in DRM_AMDGPU_USERPTR Kconfig.

It supports both KFD userptr and gfx userptr paths.

This depends on several HMM patchset from Jérôme Glisse queued for upstream.

Change-Id Ie62c3c5e3c5b8521ab3b438d1eff2aa2a003835e
---
  drivers/gpu/drm/amd/amdgpu/Kconfig     |   6 +-
  drivers/gpu/drm/amd/amdgpu/Makefile    |   2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 121 ++++++++++++++-------------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h |   2 +-
  4 files changed, 56 insertions(+), 75 deletions(-)
drivers/gpu/drm/amd/amdgpu/Kconfig +3 -3
index 9221e54..960a633 100644
@@ -26,10 +26,10 @@ config DRM_AMDGPU_CIK
  config DRM_AMDGPU_USERPTR
  	bool "Always enable userptr write support"
  	depends on DRM_AMDGPU
-	select MMU_NOTIFIER
+	select HMM_MIRROR
  	help
-	  This option selects CONFIG_MMU_NOTIFIER if it isn't already
-	  selected to enabled full userptr support.
+	  This option selects CONFIG_HMM and CONFIG_HMM_MIRROR if it
+	  isn't already selected to enabled full userptr support.
 
  config DRM_AMDGPU_GART_DEBUGFS
  	bool "Allow GART access through debugfs"
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 138cb78..c1e5d43 100644
@@ -171,7 +171,7 @@ endif
  amdgpu-$(CONFIG_COMPAT) += amdgpu_ioc32.o
  amdgpu-$(CONFIG_VGA_SWITCHEROO) += amdgpu_atpx_handler.o
  amdgpu-$(CONFIG_ACPI) += amdgpu_acpi.o
-amdgpu-$(CONFIG_MMU_NOTIFIER) += amdgpu_mn.o
+amdgpu-$(CONFIG_HMM) += amdgpu_mn.o
 
  include $(FULL_AMD_PATH)/powerplay/Makefile
 
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -1
index e55508b..ad52f34 100644
@@ -45,7 +45,7 @@
 
  #include <linux/firmware.h>
  #include <linux/module.h>
-#include <linux/mmu_notifier.h>
+#include <linux/hmm.h>
  #include <linux/interval_tree.h>
  #include <drm/drmP.h>
  #include <drm/drm.h>
@@ -66,6 +66,7 @@

Need to remove @mn documentation.

Quoted 36 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +5 -2
   * @objects: interval tree containing amdgpu_mn_nodes
   * @read_lock: mutex for recursive locking of @lock
   * @recursion: depth of recursion
+ * @mirror: HMM mirror function support
   *
   * Data for each amdgpu device and process address space.
   */
@@ -73,7 +74,6 @@ struct amdgpu_mn {
  	/* constant after initialisation */
  	struct amdgpu_device	*adev;
  	struct mm_struct	*mm;
-	struct mmu_notifier	mn;
  	enum amdgpu_mn_type	type;
 
  	/* only used on destruction */
@@ -87,6 +87,9 @@ struct amdgpu_mn {
  	struct rb_root_cached	objects;
  	struct mutex		read_lock;
  	atomic_t		recursion;
+
+	/* HMM mirror */
+	struct hmm_mirror	mirror;
  };
 
  /**
@@ -103,7 +106,7 @@ struct amdgpu_mn_node {
  };
 
  /**
- * amdgpu_mn_destroy - destroy the MMU notifier
+ * amdgpu_mn_destroy - destroy the HMM mirror
   *
   * @work: previously sheduled work item
   *
@@ -129,28 +132,26 @@ static void amdgpu_mn_destroy(struct work_struct *work)
  	}

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static void amdgpu_mn_destroy(struct work_struct *work) · line 141

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
  	up_write(&amn->lock);
  	mutex_unlock(&adev->mn_lock);
-	mmu_notifier_unregister_no_release(&amn->mn, amn->mm);
+	hmm_mirror_unregister(&amn->mirror);
+
  	kfree(amn);
  }
 
  /**
   * amdgpu_mn_release - callback to notify about mm destruction

Update the function name in the comment.

Quoted 57 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +12 -17
   *
- * @mn: our notifier
- * @mm: the mm this callback is about
+ * @mirror: the HMM mirror (mm) this callback is about
   *
- * Shedule a work item to lazy destroy our notifier.
+ * Shedule a work item to lazy destroy HMM mirror.
   */
-static void amdgpu_mn_release(struct mmu_notifier *mn,
-			      struct mm_struct *mm)
+static void amdgpu_hmm_mirror_release(struct hmm_mirror *mirror)
  {
-	struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+	struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, mirror);
 
  	INIT_WORK(&amn->work, amdgpu_mn_destroy);
  	schedule_work(&amn->work);
  }
 
-
  /**
   * amdgpu_mn_lock - take the write side lock for this notifier
   *
@@ -237,21 +238,19 @@ static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
  /**
   * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
   *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
+ * @mirror: the hmm_mirror (mm) is about to update
+ * @update: the update start, end address
   *
   * Block for operations on BOs to finish and mark pages as accessed and
   * potentially dirty.
   */
-static int amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
-						 struct mm_struct *mm,
-						 unsigned long start,
-						 unsigned long end,
-						 bool blockable)
+static int amdgpu_mn_invalidate_range_start_gfx(struct hmm_mirror *mirror,
+			const struct hmm_update *update)
  {
-	struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+	struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, mirror);
+	unsigned long start = update->start;
+	unsigned long end = update->end;
+	bool blockable = update->blockable;
  	struct interval_tree_node *it;
 
  	/* notification is exclusive, but interval is inclusive */
@@ -278,28 +277,28 @@ static int amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
  		amdgpu_mn_invalidate_node(node, start, end);
  	}
 
Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static int amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn, · line 281

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -0
+	amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

Christian.

Quoted 156 lines
Quoted 156 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +29 -49
  	return 0;
  }
 
  /**
   * amdgpu_mn_invalidate_range_start_hsa - callback to notify about mm change
   *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
+ * @mirror: the hmm_mirror (mm) is about to update
+ * @update: the update start, end address
   *
   * We temporarily evict all BOs between start and end. This
   * necessitates evicting all user-mode queues of the process. The BOs
   * are restorted in amdgpu_mn_invalidate_range_end_hsa.
   */
-static int amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
-						 struct mm_struct *mm,
-						 unsigned long start,
-						 unsigned long end,
-						 bool blockable)
+static int amdgpu_mn_invalidate_range_start_hsa(struct hmm_mirror *mirror,
+			const struct hmm_update *update)
  {
-	struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+	struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, mirror);
+	unsigned long start = update->start;
+	unsigned long end = update->end;
+	bool blockable = update->blockable;
  	struct interval_tree_node *it;
 
  	/* notification is exclusive, but interval is inclusive */
@@ -326,59 +325,41 @@ static int amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
 
  			if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
  							 start, end))
-				amdgpu_amdkfd_evict_userptr(mem, mm);
+				amdgpu_amdkfd_evict_userptr(mem, amn->mm);
  		}
  	}
 
+	amdgpu_mn_read_unlock(amn);
+
  	return 0;
  }
 
-/**
- * amdgpu_mn_invalidate_range_end - callback to notify about mm change
- *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
- *
- * Release the lock again to allow new command submissions.
+/* Low bits of any reasonable mm pointer will be unused due to struct
+ * alignment. Use these bits to make a unique key from the mm pointer
+ * and notifier type.
   */
-static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
-					   struct mm_struct *mm,
-					   unsigned long start,
-					   unsigned long end)
-{
-	struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
-
-	amdgpu_mn_read_unlock(amn);
-}
+#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
 
-static const struct mmu_notifier_ops amdgpu_mn_ops[] = {
+static struct hmm_mirror_ops amdgpu_hmm_mirror_ops[] = {
  	[AMDGPU_MN_TYPE_GFX] = {
-		.release = amdgpu_mn_release,
-		.invalidate_range_start = amdgpu_mn_invalidate_range_start_gfx,
-		.invalidate_range_end = amdgpu_mn_invalidate_range_end,
+		.sync_cpu_device_pagetables =
+				amdgpu_mn_invalidate_range_start_gfx,
+		.release = amdgpu_hmm_mirror_release
  	},
  	[AMDGPU_MN_TYPE_HSA] = {
-		.release = amdgpu_mn_release,
-		.invalidate_range_start = amdgpu_mn_invalidate_range_start_hsa,
-		.invalidate_range_end = amdgpu_mn_invalidate_range_end,
+		.sync_cpu_device_pagetables =
+				amdgpu_mn_invalidate_range_start_hsa,
+		.release = amdgpu_hmm_mirror_release
  	},
  };
 
-/* Low bits of any reasonable mm pointer will be unused due to struct
- * alignment. Use these bits to make a unique key from the mm pointer
- * and notifier type.
- */
-#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
-
  /**
- * amdgpu_mn_get - create notifier context
+ * amdgpu_mn_get - create HMM mirror context
   *
   * @adev: amdgpu device pointer
   * @type: type of MMU notifier context
   *
- * Creates a notifier context for current->mm.
+ * Creates a HMM mirror context for current->mm.
   */
  struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
  				enum amdgpu_mn_type type)
@@ -408,12 +389,12 @@ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
  	amn->mm = mm;
  	init_rwsem(&amn->lock);
  	amn->type = type;
-	amn->mn.ops = &amdgpu_mn_ops[type];
  	amn->objects = RB_ROOT_CACHED;
  	mutex_init(&amn->read_lock);
  	atomic_set(&amn->recursion, 0);
 
-	r = __mmu_notifier_register(&amn->mn, mm);
+	amn->mirror.ops = &amdgpu_hmm_mirror_ops[type];
+	r = hmm_mirror_register(&amn->mirror, mm);
  	if (r)
  		goto free_amn;
 
@@ -439,7 +420,7 @@ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
   * @bo: amdgpu buffer object
   * @addr: userptr addr we should monitor
   *
- * Registers an MMU notifier for the given BO at the specified address.
+ * Registers an HMM mirror for the given BO at the specified address.
   * Returns 0 on success, -ERRNO if anything goes wrong.
   */
  int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
@@ -495,11 +476,11 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
  }
 
  /**
- * amdgpu_mn_unregister - unregister a BO for notifier updates
+ * amdgpu_mn_unregister - unregister a BO for HMM mirror updates
   *
   * @bo: amdgpu buffer object
   *
- * Remove any registration of MMU notifier updates from the buffer object.
+ * Remove any registration of HMM mirror updates from the buffer object.
   */
  void amdgpu_mn_unregister(struct amdgpu_bo *bo)
  {
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +0 -0
index eb0f432..0e27526 100644
@@ -34,7 +34,7 @@ enum amdgpu_mn_type {
  	AMDGPU_MN_TYPE_HSA,
  };
 
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h enum amdgpu_mn_type { · line 40

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +1 -1
-#if defined(CONFIG_MMU_NOTIFIER)
+#if defined(CONFIG_HMM)
  void amdgpu_mn_lock(struct amdgpu_mn *mn);
  void amdgpu_mn_unlock(struct amdgpu_mn *mn);
  struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 58bc3bb9-b7b1-a32f-e355-c78a23d95215-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#6 lore raw
Quoted 174 lines · Christian König

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 172 lines · Philip Yang

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c

Quoted 60 lines

Replace our MMU notifier with hmm_mirror_ops.sync_cpu_device_pagetables callback. Enable CONFIG_HMM and CONFIG_HMM_MIRROR as a dependency in DRM_AMDGPU_USERPTR Kconfig.

It supports both KFD userptr and gfx userptr paths.

This depends on several HMM patchset from Jérôme Glisse queued for upstream.

Change-Id Ie62c3c5e3c5b8521ab3b438d1eff2aa2a003835e

---
  drivers/gpu/drm/amd/amdgpu/Kconfig     |   6 +-
  drivers/gpu/drm/amd/amdgpu/Makefile    |   2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 121
++++++++++++++-------------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h |   2 +-

  4 files changed, 56 insertions(+), 75 deletions(-)
drivers/gpu/drm/amd/amdgpu/Kconfig +0 -0
index 9221e54..960a633 100644
@@ -26,10 +26,10 @@ config DRM_AMDGPU_CIK

  config DRM_AMDGPU_USERPTR
      bool "Always enable userptr write support" depends on DRM_AMDGPU
-    select MMU_NOTIFIER
+    select HMM_MIRROR
      help
-      This option selects CONFIG_MMU_NOTIFIER if it isn't already -      selected to enabled full userptr support.
+      This option selects CONFIG_HMM and CONFIG_HMM_MIRROR if it +      isn't already selected to enabled full userptr support. config DRM_AMDGPU_GART_DEBUGFS
      bool "Allow GART access through debugfs"

drivers/gpu/drm/amd/amdgpu/Makefile +0 -0
index 138cb78..c1e5d43 100644
@@ -171,7 +171,7 @@ endif

  amdgpu-$(CONFIG_COMPAT) += amdgpu_ioc32.o amdgpu-$(CONFIG_VGA_SWITCHEROO) += amdgpu_atpx_handler.o amdgpu-$(CONFIG_ACPI) += amdgpu_acpi.o

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
-amdgpu-$(CONFIG_MMU_NOTIFIER) += amdgpu_mn.o
+amdgpu-$(CONFIG_HMM) += amdgpu_mn.o

    include $(FULL_AMD_PATH)/powerplay/Makefile
  diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c index e55508b..ad52f34 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -1
@@ -45,7 +45,7 @@
     #include <linux/firmware.h>
   #include <linux/module.h>
-#include <linux/mmu_notifier.h>
+#include <linux/hmm.h>
   #include <linux/interval_tree.h>
   #include <drm/drmP.h>
   #include <drm/drm.h>
@@ -66,6 +66,7 @@

Need to remove @mn documentation.

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +5 -2
    * @objects: interval tree containing amdgpu_mn_nodes
    * @read_lock: mutex for recursive locking of @lock
    * @recursion: depth of recursion
+ * @mirror: HMM mirror function support
    *
    * Data for each amdgpu device and process address space.
    */
@@ -73,7 +74,6 @@ struct amdgpu_mn {
       /* constant after initialisation */
       struct amdgpu_device    *adev;
       struct mm_struct    *mm;
-    struct mmu_notifier    mn;
       enum amdgpu_mn_type    type;
         /* only used on destruction */
@@ -87,6 +87,9 @@ struct amdgpu_mn {
       struct rb_root_cached    objects;
       struct mutex        read_lock;
       atomic_t        recursion;
+
+    /* HMM mirror */
+    struct hmm_mirror    mirror;
   };
     /**
@@ -103,7 +106,7 @@ struct amdgpu_mn_node {
   };
     /**
- * amdgpu_mn_destroy - destroy the MMU notifier
+ * amdgpu_mn_destroy - destroy the HMM mirror
    *
    * @work: previously sheduled work item
    *
@@ -129,28 +132,26 @@ static void amdgpu_mn_destroy(struct 
 work_struct *work)

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static void amdgpu_mn_destroy(struct · line 141

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
       }
       up_write(&amn->lock);
       mutex_unlock(&adev->mn_lock);
-    mmu_notifier_unregister_no_release(&amn->mn, amn->mm);
+    hmm_mirror_unregister(&amn->mirror);
+
       kfree(amn);
   }
     /**
    * amdgpu_mn_release - callback to notify about mm destruction

Update the function name in the comment.

Quoted 56 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +4 -6
    *
- * @mn: our notifier
- * @mm: the mm this callback is about
+ * @mirror: the HMM mirror (mm) this callback is about
    *
- * Shedule a work item to lazy destroy our notifier.
+ * Shedule a work item to lazy destroy HMM mirror.
    */
-static void amdgpu_mn_release(struct mmu_notifier *mn,
-                  struct mm_struct *mm)
+static void amdgpu_hmm_mirror_release(struct hmm_mirror *mirror)
   {
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+    struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, 
 mirror);
         INIT_WORK(&amn->work, amdgpu_mn_destroy);
       schedule_work(&amn->work);
   }

  -
  /**
   * amdgpu_mn_lock - take the write side lock for this notifier *

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +5 -10
@@ -237,21 +238,19 @@ static void amdgpu_mn_invalidate_node(struct 
 amdgpu_mn_node *node,
   /**
    * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
    *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
+ * @mirror: the hmm_mirror (mm) is about to update
+ * @update: the update start, end address
    *
    * Block for operations on BOs to finish and mark pages as accessed and
    * potentially dirty.
    */
-static int amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier 
 *mn,
-                         struct mm_struct *mm,
-                         unsigned long start,
-                         unsigned long end,
-                         bool blockable)
+static int amdgpu_mn_invalidate_range_start_gfx(struct hmm_mirror 
 *mirror,
+            const struct hmm_update *update)
   {
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+    struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, 

mirror);
+    unsigned long start = update->start;
+    unsigned long end = update->end;
+    bool blockable = update->blockable;
      struct interval_tree_node *it;
        /* notification is exclusive, but interval is inclusive */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static int · line 281

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -0
@@ -278,28 +277,28 @@ static int 
 amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
           amdgpu_mn_invalidate_node(node, start, end);
       }
   +    amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 4 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime. This is same for old and new way.

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

Old:
   1. down_read_non_owner(&amn->lock)
   2. loop to handle BOs from node->bos through interval tree amn->object nodes
       gfx: wait for pending BOs fence operation done, mark user pages dirty
       kfd: evict user queues of the process, wait for queue unmap/map operation done
   3. update CPU page tables
   4. up_read(&amn->lock)

New, switch step 3 and 4
   1. down_read_non_owner(&amn->lock)
   2. loop to handle BOs from node->bos through interval tree amn->object nodes
       gfx: wait for pending BOs fence operation done, mark user pages dirty
       kfd: evict user queues of the process, wait for queue unmap/map operation done
   3. up_read(&amn->lock)
   4. update CPU page tables

Regards,
Philip

Quoted 168 lines

Christian.

Quoted 165 lines
Quoted 154 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +8 -10
       return 0;
   }
     /**
    * amdgpu_mn_invalidate_range_start_hsa - callback to notify about mm change
    *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
+ * @mirror: the hmm_mirror (mm) is about to update
+ * @update: the update start, end address
    *
    * We temporarily evict all BOs between start and end. This
    * necessitates evicting all user-mode queues of the process. The BOs
    * are restorted in amdgpu_mn_invalidate_range_end_hsa.
    */
-static int amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier 
 *mn,
-                         struct mm_struct *mm,
-                         unsigned long start,
-                         unsigned long end,
-                         bool blockable)
+static int amdgpu_mn_invalidate_range_start_hsa(struct hmm_mirror 
 *mirror,
+            const struct hmm_update *update)
   {
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+    struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, 
 mirror);
+    unsigned long start = update->start;
+    unsigned long end = update->end;
+    bool blockable = update->blockable;

      struct interval_tree_node *it;
        /* notification is exclusive, but interval is inclusive */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
@@ -326,59 +325,41 @@ static int 
 amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
                 if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
                                start, end))
-                amdgpu_amdkfd_evict_userptr(mem, mm);
+                amdgpu_amdkfd_evict_userptr(mem, amn->mm);
           }
       }
   +    amdgpu_mn_read_unlock(amn);
+
       return 0;
   }

  -/**
- * amdgpu_mn_invalidate_range_end - callback to notify about mm change
- *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
- *
- * Release the lock again to allow new command submissions. +/* Low bits of any reasonable mm pointer will be unused due to struct
+ * alignment. Use these bits to make a unique key from the mm pointer
+ * and notifier type.
   */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -9
-static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
-                       struct mm_struct *mm,
-                       unsigned long start,
-                       unsigned long end)
-{
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
-
-    amdgpu_mn_read_unlock(amn);
-}
+#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))

  -static const struct mmu_notifier_ops amdgpu_mn_ops[] = { +static struct hmm_mirror_ops amdgpu_hmm_mirror_ops[] = { [AMDGPU_MN_TYPE_GFX] = {
-        .release = amdgpu_mn_release,
-        .invalidate_range_start = amdgpu_mn_invalidate_range_start_gfx,
-        .invalidate_range_end = amdgpu_mn_invalidate_range_end, +        .sync_cpu_device_pagetables =
+                amdgpu_mn_invalidate_range_start_gfx,
+        .release = amdgpu_hmm_mirror_release
      },
      [AMDGPU_MN_TYPE_HSA] = {
-        .release = amdgpu_mn_release,
-        .invalidate_range_start = amdgpu_mn_invalidate_range_start_hsa,
-        .invalidate_range_end = amdgpu_mn_invalidate_range_end, +        .sync_cpu_device_pagetables =
+                amdgpu_mn_invalidate_range_start_hsa,
+        .release = amdgpu_hmm_mirror_release
      },
  };
  -/* Low bits of any reasonable mm pointer will be unused due to struct
- * alignment. Use these bits to make a unique key from the mm pointer
- * and notifier type.
- */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +0 -2
-#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
-

  /**
- * amdgpu_mn_get - create notifier context
+ * amdgpu_mn_get - create HMM mirror context
   *
   * @adev: amdgpu device pointer
   * @type: type of MMU notifier context
   *
- * Creates a notifier context for current->mm.
+ * Creates a HMM mirror context for current->mm. */
  struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev, enum amdgpu_mn_type type)

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
@@ -408,12 +389,12 @@ struct amdgpu_mn *amdgpu_mn_get(struct 
 amdgpu_device *adev,
       amn->mm = mm;
       init_rwsem(&amn->lock);
       amn->type = type;
-    amn->mn.ops = &amdgpu_mn_ops[type];
       amn->objects = RB_ROOT_CACHED;
       mutex_init(&amn->read_lock);
       atomic_set(&amn->recursion, 0);
   -    r = __mmu_notifier_register(&amn->mn, mm);
+    amn->mirror.ops = &amdgpu_hmm_mirror_ops[type];
+    r = hmm_mirror_register(&amn->mirror, mm);
       if (r)
           goto free_amn;

  @@ -439,7 +420,7 @@ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
   * @bo: amdgpu buffer object
   * @addr: userptr addr we should monitor
   *
- * Registers an MMU notifier for the given BO at the specified address.
+ * Registers an HMM mirror for the given BO at the specified address.
   * Returns 0 on success, -ERRNO if anything goes wrong.
   */
  int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -2
@@ -495,11 +476,11 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, 
 unsigned long addr)
   }
     /**
- * amdgpu_mn_unregister - unregister a BO for notifier updates
+ * amdgpu_mn_unregister - unregister a BO for HMM mirror updates
    *
    * @bo: amdgpu buffer object
    *
- * Remove any registration of MMU notifier updates from the buffer 

object.
+ * Remove any registration of HMM mirror updates from the buffer object.
   */
  void amdgpu_mn_unregister(struct amdgpu_bo *bo)
  {

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +0 -0
index eb0f432..0e27526 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h enum amdgpu_mn_type { · line 40

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +1 -0
@@ -34,7 +34,7 @@ enum amdgpu_mn_type {
       AMDGPU_MN_TYPE_HSA,
   };
   -#if defined(CONFIG_MMU_NOTIFIER)
+#if defined(CONFIG_HMM)
   void amdgpu_mn_lock(struct amdgpu_mn *mn);
   void amdgpu_mn_unlock(struct amdgpu_mn *mn);
   struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 383388c8-1bff-48d9-1044-f16e66bcbfa5-5C7GfCeVMHo@public.gmane.org

#8 lore raw

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 182 lines · Christian König
Quoted 174 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 172 lines · Philip Yang

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c

Quoted 60 lines

Replace our MMU notifier with hmm_mirror_ops.sync_cpu_device_pagetables callback. Enable CONFIG_HMM and CONFIG_HMM_MIRROR as a dependency in DRM_AMDGPU_USERPTR Kconfig.

It supports both KFD userptr and gfx userptr paths.

This depends on several HMM patchset from Jérôme Glisse queued for upstream.

Change-Id Ie62c3c5e3c5b8521ab3b438d1eff2aa2a003835e

---
  drivers/gpu/drm/amd/amdgpu/Kconfig     |   6 +-
  drivers/gpu/drm/amd/amdgpu/Makefile    |   2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 121
++++++++++++++-------------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h |   2 +-

  4 files changed, 56 insertions(+), 75 deletions(-)
drivers/gpu/drm/amd/amdgpu/Kconfig +0 -0
index 9221e54..960a633 100644
@@ -26,10 +26,10 @@ config DRM_AMDGPU_CIK

  config DRM_AMDGPU_USERPTR
      bool "Always enable userptr write support" depends on DRM_AMDGPU
-    select MMU_NOTIFIER
+    select HMM_MIRROR
      help
-      This option selects CONFIG_MMU_NOTIFIER if it isn't already -      selected to enabled full userptr support.
+      This option selects CONFIG_HMM and CONFIG_HMM_MIRROR if it +      isn't already selected to enabled full userptr support. config DRM_AMDGPU_GART_DEBUGFS
      bool "Allow GART access through debugfs"

drivers/gpu/drm/amd/amdgpu/Makefile +0 -0
index 138cb78..c1e5d43 100644
@@ -171,7 +171,7 @@ endif

  amdgpu-$(CONFIG_COMPAT) += amdgpu_ioc32.o amdgpu-$(CONFIG_VGA_SWITCHEROO) += amdgpu_atpx_handler.o amdgpu-$(CONFIG_ACPI) += amdgpu_acpi.o

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
-amdgpu-$(CONFIG_MMU_NOTIFIER) += amdgpu_mn.o
+amdgpu-$(CONFIG_HMM) += amdgpu_mn.o

    include $(FULL_AMD_PATH)/powerplay/Makefile
  diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c index e55508b..ad52f34 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -1
@@ -45,7 +45,7 @@
     #include <linux/firmware.h>
   #include <linux/module.h>
-#include <linux/mmu_notifier.h>
+#include <linux/hmm.h>
   #include <linux/interval_tree.h>
   #include <drm/drmP.h>
   #include <drm/drm.h>
@@ -66,6 +66,7 @@

Need to remove @mn documentation.

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +5 -2
    * @objects: interval tree containing amdgpu_mn_nodes
    * @read_lock: mutex for recursive locking of @lock
    * @recursion: depth of recursion
+ * @mirror: HMM mirror function support
    *
    * Data for each amdgpu device and process address space.
    */
@@ -73,7 +74,6 @@ struct amdgpu_mn {
       /* constant after initialisation */
       struct amdgpu_device    *adev;
       struct mm_struct    *mm;
-    struct mmu_notifier    mn;
       enum amdgpu_mn_type    type;
         /* only used on destruction */
@@ -87,6 +87,9 @@ struct amdgpu_mn {
       struct rb_root_cached    objects;
       struct mutex        read_lock;
       atomic_t        recursion;
+
+    /* HMM mirror */
+    struct hmm_mirror    mirror;
   };
     /**
@@ -103,7 +106,7 @@ struct amdgpu_mn_node {
   };
     /**
- * amdgpu_mn_destroy - destroy the MMU notifier
+ * amdgpu_mn_destroy - destroy the HMM mirror
    *
    * @work: previously sheduled work item
    *
@@ -129,28 +132,26 @@ static void amdgpu_mn_destroy(struct 
 work_struct *work)

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static void amdgpu_mn_destroy(struct · line 141

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
       }
       up_write(&amn->lock);
       mutex_unlock(&adev->mn_lock);
-    mmu_notifier_unregister_no_release(&amn->mn, amn->mm);
+    hmm_mirror_unregister(&amn->mirror);
+
       kfree(amn);
   }
     /**
    * amdgpu_mn_release - callback to notify about mm destruction

Update the function name in the comment.

Quoted 56 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +4 -6
    *
- * @mn: our notifier
- * @mm: the mm this callback is about
+ * @mirror: the HMM mirror (mm) this callback is about
    *
- * Shedule a work item to lazy destroy our notifier.
+ * Shedule a work item to lazy destroy HMM mirror.
    */
-static void amdgpu_mn_release(struct mmu_notifier *mn,
-                  struct mm_struct *mm)
+static void amdgpu_hmm_mirror_release(struct hmm_mirror *mirror)
   {
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+    struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, 
 mirror);
         INIT_WORK(&amn->work, amdgpu_mn_destroy);
       schedule_work(&amn->work);
   }

  -
  /**
   * amdgpu_mn_lock - take the write side lock for this notifier *

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +5 -10
@@ -237,21 +238,19 @@ static void amdgpu_mn_invalidate_node(struct 
 amdgpu_mn_node *node,
   /**
    * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
    *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
+ * @mirror: the hmm_mirror (mm) is about to update
+ * @update: the update start, end address
    *
    * Block for operations on BOs to finish and mark pages as accessed and
    * potentially dirty.
    */
-static int amdgpu_mn_invalidate_range_start_gfx(struct 
 mmu_notifier *mn,
-                         struct mm_struct *mm,
-                         unsigned long start,
-                         unsigned long end,
-                         bool blockable)
+static int amdgpu_mn_invalidate_range_start_gfx(struct hmm_mirror 
 *mirror,
+            const struct hmm_update *update)
   {
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+    struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, 

mirror);
+    unsigned long start = update->start;
+    unsigned long end = update->end;
+    bool blockable = update->blockable;
      struct interval_tree_node *it;
        /* notification is exclusive, but interval is inclusive */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static int · line 281

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -0
@@ -278,28 +277,28 @@ static int 
 amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
           amdgpu_mn_invalidate_node(node, start, end);
       }
   +    amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 4 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Quoted 8 lines

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime. This is same for old and new way.

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 9 lines

Old:
   1. down_read_non_owner(&amn->lock)
   2. loop to handle BOs from node->bos through interval tree amn->object nodes
       gfx: wait for pending BOs fence operation done, mark user pages dirty
       kfd: evict user queues of the process, wait for queue unmap/map operation done
   3. update CPU page tables
   4. up_read(&amn->lock)

Quoted 7 lines

New, switch step 3 and 4
   1. down_read_non_owner(&amn->lock)
   2. loop to handle BOs from node->bos through interval tree amn->object nodes
       gfx: wait for pending BOs fence operation done, mark user pages dirty
       kfd: evict user queues of the process, wait for queue unmap/map operation done
   3. up_read(&amn->lock)
   4. update CPU page tables

The lock is there to make sure that we serialize page table updates with command submission.

If HMM doesn't provide a callback for the end of the invalidating then it can't be used for this.

Adding Jerome as well, since we are certainly missing something here.

Regards,
Christian.

Quoted 173 lines

Regards,
Philip

Quoted 169 lines

Christian.

Quoted 166 lines
Quoted 155 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +8 -10
       return 0;
   }
     /**
    * amdgpu_mn_invalidate_range_start_hsa - callback to notify about mm change
    *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
+ * @mirror: the hmm_mirror (mm) is about to update
+ * @update: the update start, end address
    *
    * We temporarily evict all BOs between start and end. This
    * necessitates evicting all user-mode queues of the process. The BOs
    * are restorted in amdgpu_mn_invalidate_range_end_hsa.
    */
-static int amdgpu_mn_invalidate_range_start_hsa(struct 
 mmu_notifier *mn,
-                         struct mm_struct *mm,
-                         unsigned long start,
-                         unsigned long end,
-                         bool blockable)
+static int amdgpu_mn_invalidate_range_start_hsa(struct hmm_mirror 
 *mirror,
+            const struct hmm_update *update)
   {
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+    struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, 
 mirror);
+    unsigned long start = update->start;
+    unsigned long end = update->end;
+    bool blockable = update->blockable;

      struct interval_tree_node *it;
        /* notification is exclusive, but interval is inclusive */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
@@ -326,59 +325,41 @@ static int 
 amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
                 if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
                                start, end))
-                amdgpu_amdkfd_evict_userptr(mem, mm);
+                amdgpu_amdkfd_evict_userptr(mem, amn->mm);
           }
       }
   +    amdgpu_mn_read_unlock(amn);
+
       return 0;
   }

  -/**
- * amdgpu_mn_invalidate_range_end - callback to notify about mm change
- *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
- *
- * Release the lock again to allow new command submissions. +/* Low bits of any reasonable mm pointer will be unused due to struct
+ * alignment. Use these bits to make a unique key from the mm pointer
+ * and notifier type.
   */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -9
-static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
-                       struct mm_struct *mm,
-                       unsigned long start,
-                       unsigned long end)
-{
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
-
-    amdgpu_mn_read_unlock(amn);
-}
+#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))

  -static const struct mmu_notifier_ops amdgpu_mn_ops[] = { +static struct hmm_mirror_ops amdgpu_hmm_mirror_ops[] = { [AMDGPU_MN_TYPE_GFX] = {
-        .release = amdgpu_mn_release,
-        .invalidate_range_start = amdgpu_mn_invalidate_range_start_gfx,
-        .invalidate_range_end = amdgpu_mn_invalidate_range_end, +        .sync_cpu_device_pagetables =
+                amdgpu_mn_invalidate_range_start_gfx,
+        .release = amdgpu_hmm_mirror_release
      },
      [AMDGPU_MN_TYPE_HSA] = {
-        .release = amdgpu_mn_release,
-        .invalidate_range_start = amdgpu_mn_invalidate_range_start_hsa,
-        .invalidate_range_end = amdgpu_mn_invalidate_range_end, +        .sync_cpu_device_pagetables =
+                amdgpu_mn_invalidate_range_start_hsa,
+        .release = amdgpu_hmm_mirror_release
      },
  };
  -/* Low bits of any reasonable mm pointer will be unused due to struct
- * alignment. Use these bits to make a unique key from the mm pointer
- * and notifier type.
- */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +0 -2
-#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
-

  /**
- * amdgpu_mn_get - create notifier context
+ * amdgpu_mn_get - create HMM mirror context
   *
   * @adev: amdgpu device pointer
   * @type: type of MMU notifier context
   *
- * Creates a notifier context for current->mm.
+ * Creates a HMM mirror context for current->mm. */
  struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev, enum amdgpu_mn_type type)

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
@@ -408,12 +389,12 @@ struct amdgpu_mn *amdgpu_mn_get(struct 
 amdgpu_device *adev,
       amn->mm = mm;
       init_rwsem(&amn->lock);
       amn->type = type;
-    amn->mn.ops = &amdgpu_mn_ops[type];
       amn->objects = RB_ROOT_CACHED;
       mutex_init(&amn->read_lock);
       atomic_set(&amn->recursion, 0);
   -    r = __mmu_notifier_register(&amn->mn, mm);
+    amn->mirror.ops = &amdgpu_hmm_mirror_ops[type];
+    r = hmm_mirror_register(&amn->mirror, mm);
       if (r)
           goto free_amn;

  @@ -439,7 +420,7 @@ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
   * @bo: amdgpu buffer object
   * @addr: userptr addr we should monitor
   *
- * Registers an MMU notifier for the given BO at the specified address.
+ * Registers an HMM mirror for the given BO at the specified address.
   * Returns 0 on success, -ERRNO if anything goes wrong.
   */
  int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -2
@@ -495,11 +476,11 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, 
 unsigned long addr)
   }
     /**
- * amdgpu_mn_unregister - unregister a BO for notifier updates
+ * amdgpu_mn_unregister - unregister a BO for HMM mirror updates
    *
    * @bo: amdgpu buffer object
    *
- * Remove any registration of MMU notifier updates from the buffer 

object.
+ * Remove any registration of HMM mirror updates from the buffer object.
   */
  void amdgpu_mn_unregister(struct amdgpu_bo *bo)
  {

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +0 -0
index eb0f432..0e27526 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h enum amdgpu_mn_type { · line 40

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +1 -0
@@ -34,7 +34,7 @@ enum amdgpu_mn_type {
       AMDGPU_MN_TYPE_HSA,
   };
   -#if defined(CONFIG_MMU_NOTIFIER)
+#if defined(CONFIG_HMM)
   void amdgpu_mn_lock(struct amdgpu_mn *mn);
   void amdgpu_mn_unlock(struct amdgpu_mn *mn);
   struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 3850fbeb-5d91-9c14-43c9-45d5d058e15b-5C7GfCeVMHo@public.gmane.org

#10 lore raw
Quoted 231 lines · Christian König

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 198 lines · Christian König
Quoted 180 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 178 lines · Philip Yang

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c

Quoted 64 lines

Replace our MMU notifier with hmm_mirror_ops.sync_cpu_device_pagetables
callback. Enable CONFIG_HMM and CONFIG_HMM_MIRROR as a dependency in DRM_AMDGPU_USERPTR Kconfig.

It supports both KFD userptr and gfx userptr paths.

This depends on several HMM patchset from Jérôme Glisse queued for upstream.

Change-Id Ie62c3c5e3c5b8521ab3b438d1eff2aa2a003835e

---
  drivers/gpu/drm/amd/amdgpu/Kconfig     |   6 +-
  drivers/gpu/drm/amd/amdgpu/Makefile    |   2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 121
++++++++++++++-------------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h |   2 +-

  4 files changed, 56 insertions(+), 75 deletions(-)
drivers/gpu/drm/amd/amdgpu/Kconfig +0 -0
index 9221e54..960a633 100644
@@ -26,10 +26,10 @@ config DRM_AMDGPU_CIK

  config DRM_AMDGPU_USERPTR
      bool "Always enable userptr write support" depends on DRM_AMDGPU
-    select MMU_NOTIFIER
+    select HMM_MIRROR
      help
-      This option selects CONFIG_MMU_NOTIFIER if it isn't already -      selected to enabled full userptr support.
+      This option selects CONFIG_HMM and CONFIG_HMM_MIRROR if it +      isn't already selected to enabled full userptr support. config DRM_AMDGPU_GART_DEBUGFS
      bool "Allow GART access through debugfs"

drivers/gpu/drm/amd/amdgpu/Makefile +0 -0
index 138cb78..c1e5d43 100644
@@ -171,7 +171,7 @@ endif

  amdgpu-$(CONFIG_COMPAT) += amdgpu_ioc32.o amdgpu-$(CONFIG_VGA_SWITCHEROO) += amdgpu_atpx_handler.o amdgpu-$(CONFIG_ACPI) += amdgpu_acpi.o

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
-amdgpu-$(CONFIG_MMU_NOTIFIER) += amdgpu_mn.o
+amdgpu-$(CONFIG_HMM) += amdgpu_mn.o

    include $(FULL_AMD_PATH)/powerplay/Makefile diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c index e55508b..ad52f34 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -1
@@ -45,7 +45,7 @@
     #include <linux/firmware.h>
   #include <linux/module.h>
-#include <linux/mmu_notifier.h>
+#include <linux/hmm.h>
   #include <linux/interval_tree.h>
   #include <drm/drmP.h>
   #include <drm/drm.h>
@@ -66,6 +66,7 @@

Need to remove @mn documentation.

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +5 -2
    * @objects: interval tree containing amdgpu_mn_nodes
    * @read_lock: mutex for recursive locking of @lock
    * @recursion: depth of recursion
+ * @mirror: HMM mirror function support
    *
    * Data for each amdgpu device and process address space.
    */
@@ -73,7 +74,6 @@ struct amdgpu_mn {
       /* constant after initialisation */
       struct amdgpu_device    *adev;
       struct mm_struct    *mm;
-    struct mmu_notifier    mn;
       enum amdgpu_mn_type    type;
         /* only used on destruction */
@@ -87,6 +87,9 @@ struct amdgpu_mn {
       struct rb_root_cached    objects;
       struct mutex        read_lock;
       atomic_t        recursion;
+
+    /* HMM mirror */
+    struct hmm_mirror    mirror;
   };
     /**
@@ -103,7 +106,7 @@ struct amdgpu_mn_node {
   };
     /**
- * amdgpu_mn_destroy - destroy the MMU notifier
+ * amdgpu_mn_destroy - destroy the HMM mirror
    *
    * @work: previously sheduled work item
    *
@@ -129,28 +132,26 @@ static void amdgpu_mn_destroy(struct
 work_struct *work)

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static void amdgpu_mn_destroy(struct · line 141

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
       }
       up_write(&amn->lock);
       mutex_unlock(&adev->mn_lock);
-    mmu_notifier_unregister_no_release(&amn->mn, amn->mm);
+    hmm_mirror_unregister(&amn->mirror);
+
       kfree(amn);
   }
     /**
    * amdgpu_mn_release - callback to notify about mm destruction

Update the function name in the comment.

Quoted 58 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +4 -6
    *
- * @mn: our notifier
- * @mm: the mm this callback is about
+ * @mirror: the HMM mirror (mm) this callback is about
    *
- * Shedule a work item to lazy destroy our notifier.
+ * Shedule a work item to lazy destroy HMM mirror.
    */
-static void amdgpu_mn_release(struct mmu_notifier *mn,
-                  struct mm_struct *mm)
+static void amdgpu_hmm_mirror_release(struct hmm_mirror *mirror)
   {
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
+    struct amdgpu_mn *amn = container_of(mirror, struct
 amdgpu_mn, mirror);
         INIT_WORK(&amn->work, amdgpu_mn_destroy);
       schedule_work(&amn->work);
   }

  -
  /**
   * amdgpu_mn_lock - take the write side lock for this notifier *

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +0 -0
@@ -237,21 +238,19 @@ static void amdgpu_mn_invalidate_node(struct
 amdgpu_mn_node *node,
   /**
    * amdgpu_mn_invalidate_range_start_gfx - callback to notify

about mm change
   *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
+ * @mirror: the hmm_mirror (mm) is about to update
+ * @update: the update start, end address
   *
   * Block for operations on BOs to finish and mark pages as accessed and
   * potentially dirty.
   */
-static int amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
-                         struct mm_struct *mm,
-                         unsigned long start,
-                         unsigned long end,
-                         bool blockable)
+static int amdgpu_mn_invalidate_range_start_gfx(struct hmm_mirror *mirror,
+            const struct hmm_update *update)
  {
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn); +    struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, mirror);
+    unsigned long start = update->start;
+    unsigned long end = update->end;
+    bool blockable = update->blockable;
      struct interval_tree_node *it;
        /* notification is exclusive, but interval is inclusive */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static int · line 281

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -0
@@ -278,28 +277,28 @@ static int
 amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
           amdgpu_mn_invalidate_node(node, start, end);
       }
   +    amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 12 lines

Old:
   1. down_read_non_owner(&amn->lock)
   2. loop to handle BOs from node->bos through interval tree amn->object nodes
       gfx: wait for pending BOs fence operation done, mark user pages dirty
       kfd: evict user queues of the process, wait for queue unmap/map operation done
   3. update CPU page tables
   4. up_read(&amn->lock)

Quoted 10 lines

New, switch step 3 and 4
   1. down_read_non_owner(&amn->lock)
   2. loop to handle BOs from node->bos through interval tree amn->object nodes
       gfx: wait for pending BOs fence operation done, mark user pages dirty
       kfd: evict user queues of the process, wait for queue unmap/map operation done
   3. up_read(&amn->lock)
   4. update CPU page tables

Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress. But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

Regards,
  Felix

Quoted 192 lines

If HMM doesn't provide a callback for the end of the invalidating then it can't be used for this.

Adding Jerome as well, since we are certainly missing something here.

Regards,
Christian.

Quoted 182 lines

Regards,
Philip

Quoted 178 lines

Christian.

Quoted 175 lines
Quoted 164 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +0 -0
       return 0;
   }
     /**
    * amdgpu_mn_invalidate_range_start_hsa - callback to notify

about mm change
   *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
+ * @mirror: the hmm_mirror (mm) is about to update
+ * @update: the update start, end address
   *
   * We temporarily evict all BOs between start and end. This
   * necessitates evicting all user-mode queues of the process. The BOs
   * are restorted in amdgpu_mn_invalidate_range_end_hsa. */
-static int amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
-                         struct mm_struct *mm,
-                         unsigned long start,
-                         unsigned long end,
-                         bool blockable)
+static int amdgpu_mn_invalidate_range_start_hsa(struct hmm_mirror *mirror,
+            const struct hmm_update *update)
  {
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn); +    struct amdgpu_mn *amn = container_of(mirror, struct amdgpu_mn, mirror);
+    unsigned long start = update->start;
+    unsigned long end = update->end;
+    bool blockable = update->blockable;
      struct interval_tree_node *it;
        /* notification is exclusive, but interval is inclusive */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
@@ -326,59 +325,41 @@ static int
 amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
                 if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
                                start, end))
-                amdgpu_amdkfd_evict_userptr(mem, mm);
+                amdgpu_amdkfd_evict_userptr(mem, amn->mm);
           }
       }
   +    amdgpu_mn_read_unlock(amn);
+
       return 0;
   }

  -/**
- * amdgpu_mn_invalidate_range_end - callback to notify about mm change
- *
- * @mn: our notifier
- * @mm: the mm this callback is about
- * @start: start of updated range
- * @end: end of updated range
- *
- * Release the lock again to allow new command submissions. +/* Low bits of any reasonable mm pointer will be unused due to struct
+ * alignment. Use these bits to make a unique key from the mm pointer
+ * and notifier type.
   */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -9
-static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
-                       struct mm_struct *mm,
-                       unsigned long start,
-                       unsigned long end)
-{
-    struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
-
-    amdgpu_mn_read_unlock(amn);
-}
+#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))

  -static const struct mmu_notifier_ops amdgpu_mn_ops[] = { +static struct hmm_mirror_ops amdgpu_hmm_mirror_ops[] = { [AMDGPU_MN_TYPE_GFX] = {
-        .release = amdgpu_mn_release,
-        .invalidate_range_start = amdgpu_mn_invalidate_range_start_gfx,
-        .invalidate_range_end = amdgpu_mn_invalidate_range_end, +        .sync_cpu_device_pagetables =
+                amdgpu_mn_invalidate_range_start_gfx, +        .release = amdgpu_hmm_mirror_release
      },
      [AMDGPU_MN_TYPE_HSA] = {
-        .release = amdgpu_mn_release,
-        .invalidate_range_start = amdgpu_mn_invalidate_range_start_hsa,
-        .invalidate_range_end = amdgpu_mn_invalidate_range_end, +        .sync_cpu_device_pagetables =
+                amdgpu_mn_invalidate_range_start_hsa, +        .release = amdgpu_hmm_mirror_release
      },
  };
  -/* Low bits of any reasonable mm pointer will be unused due to struct
- * alignment. Use these bits to make a unique key from the mm pointer
- * and notifier type.
- */

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +0 -2
-#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
-

  /**
- * amdgpu_mn_get - create notifier context
+ * amdgpu_mn_get - create HMM mirror context
   *
   * @adev: amdgpu device pointer
   * @type: type of MMU notifier context
   *
- * Creates a notifier context for current->mm.
+ * Creates a HMM mirror context for current->mm. */
  struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev, enum amdgpu_mn_type type)

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -1
@@ -408,12 +389,12 @@ struct amdgpu_mn *amdgpu_mn_get(struct
 amdgpu_device *adev,
       amn->mm = mm;
       init_rwsem(&amn->lock);
       amn->type = type;
-    amn->mn.ops = &amdgpu_mn_ops[type];
       amn->objects = RB_ROOT_CACHED;
       mutex_init(&amn->read_lock);
       atomic_set(&amn->recursion, 0);
   -    r = __mmu_notifier_register(&amn->mn, mm);
+    amn->mirror.ops = &amdgpu_hmm_mirror_ops[type];
+    r = hmm_mirror_register(&amn->mirror, mm);
       if (r)
           goto free_amn;

  @@ -439,7 +420,7 @@ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
   * @bo: amdgpu buffer object
   * @addr: userptr addr we should monitor
   *
- * Registers an MMU notifier for the given BO at the specified address.
+ * Registers an HMM mirror for the given BO at the specified address.
   * Returns 0 on success, -ERRNO if anything goes wrong. */
  int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -2
@@ -495,11 +476,11 @@ int amdgpu_mn_register(struct amdgpu_bo *bo,
 unsigned long addr)
   }
     /**
- * amdgpu_mn_unregister - unregister a BO for notifier updates
+ * amdgpu_mn_unregister - unregister a BO for HMM mirror updates
    *
    * @bo: amdgpu buffer object
    *
- * Remove any registration of MMU notifier updates from the

buffer object.
+ * Remove any registration of HMM mirror updates from the buffer object.
   */
  void amdgpu_mn_unregister(struct amdgpu_bo *bo)
  {

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +0 -0
index eb0f432..0e27526 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h enum amdgpu_mn_type { · line 40

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +1 -0
@@ -34,7 +34,7 @@ enum amdgpu_mn_type {
       AMDGPU_MN_TYPE_HSA,
   };
   -#if defined(CONFIG_MMU_NOTIFIER)
+#if defined(CONFIG_HMM)
   void amdgpu_mn_lock(struct amdgpu_mn *mn);
   void amdgpu_mn_unlock(struct amdgpu_mn *mn);
   struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): de28cee0-3461-4f99-eeae-b793de00ca58-5C7GfCeVMHo@public.gmane.org

#12 lore raw

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

  +    amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
   1. down_read_non_owner(&amn->lock)
   2. loop to handle BOs from node->bos through interval tree
amn->object nodes
       gfx: wait for pending BOs fence operation done, mark user
pages dirty
       kfd: evict user queues of the process, wait for queue
unmap/map operation done
   3. update CPU page tables
   4. up_read(&amn->lock)

New, switch step 3 and 4
   1. down_read_non_owner(&amn->lock)
   2. loop to handle BOs from node->bos through interval tree
amn->object nodes
       gfx: wait for pending BOs fence operation done, mark user
pages dirty
       kfd: evict user queues of the process, wait for queue
unmap/map operation done
   3. up_read(&amn->lock)
   4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): e4cf7212-4340-8639-c8c1-057e4d1083f0-5C7GfCeVMHo@public.gmane.org

#14 lore raw

I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]>; Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

  +    amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
   1. down_read_non_owner(&amn->lock)
   2. loop to handle BOs from node->bos through interval tree
amn->object nodes
       gfx: wait for pending BOs fence operation done, mark user
pages dirty
       kfd: evict user queues of the process, wait for queue
unmap/map operation done
   3. update CPU page tables
   4. up_read(&amn->lock)

New, switch step 3 and 4
   1. down_read_non_owner(&amn->lock)
   2. loop to handle BOs from node->bos through interval tree
amn->object nodes
       gfx: wait for pending BOs fence operation done, mark user
pages dirty
       kfd: evict user queues of the process, wait for queue
unmap/map operation done
   3. up_read(&amn->lock)
   4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): DM5PR12MB17078469EB6D3AF1D53B788992140-2J9CzHegvk9TCtO+SvGBKwdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org

#16 lore raw

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]>; Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): a76b71ac-4b5b-45d7-b48b-6d0e4a7e7524-2ueSQiBKiTY7tOexoI0I+QC/[email protected]

#18 lore raw

So back to my previous question:

Quoted 10 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

Quoted 5 lines

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 3:00 AM
To: Kuehling, Felix <[email protected]>
Cc: Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org<mailto:[email protected]>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>; Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]<mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): DM5PR12MB1707D5E46617B2936F800F1992140-2J9CzHegvk9TCtO+SvGBKwdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org

#20 lore raw

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

Quoted 10 lines

        /* No memory allocation is allowed while holding the mn lock */ amdgpu_mn_lock(p->mn); amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) { struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) { r = -ERESTARTSYS;
                        goto error_abort;
                }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

Quoted 2 lines

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:

Quoted 128 lines

So back to my previous question:

Quoted 11 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command

submission, but I expect that HMM has its own mechanism to prevent that.

Quoted 3 lines

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are

certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,

  Felix

*From:* Koenig, Christian
*Sent:* Thursday, September 27, 2018 3:00 AM
*To:* Kuehling, Felix <[email protected]>
*Cc:* Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called  in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
  Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org <mailto:amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected] <mailto:[email protected]>>; Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

    +    amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
     1. down_read_non_owner(&amn->lock)
     2. loop to handle BOs from node->bos through interval tree amn->object nodes
         gfx: wait for pending BOs fence operation done, mark user pages dirty
         kfd: evict user queues of the process, wait for queue unmap/map operation done
     3. update CPU page tables
     4. up_read(&amn->lock)

New, switch step 3 and 4
     1. down_read_non_owner(&amn->lock)
     2. loop to handle BOs from node->bos through interval tree amn->object nodes
         gfx: wait for pending BOs fence operation done, mark user pages dirty
         kfd: evict user queues of the process, wait for queue unmap/map operation done
     3. up_read(&amn->lock)
     4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Quoted 9 lines

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list
[email protected] <mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): d752c19b-6d2e-c7c1-1cd7-651e25b8f708-5C7GfCeVMHo@public.gmane.org

#22 lore raw
Quoted 8 lines

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

       ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
       amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 5:27 AM
To: Kuehling, Felix <[email protected]>
Cc: Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn);
        amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
                struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
                        r = -ERESTARTSYS;
                        goto error_abort;
                }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

        ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:
So back to my previous question:

Quoted 10 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

Quoted 5 lines

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 3:00 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <[email protected]<mailto:[email protected]>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>; Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]<mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): DM5PR12MB17077A78E0F95BFBA57F2E1A92140-2J9CzHegvk9TCtO+SvGBKwdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org

#24 lore raw

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

Quoted 7 lines
Quoted 2 lines

We double check that there wasn't any page table modification while

we prepared the submission and restart the whole process when there actually was some update.

Quoted 3 lines

The reason why we need to do this is here:

Quoted 9 lines
Quoted 5 lines

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can

release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

Quoted 2 lines

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Quoted 224 lines

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 5:27 AM
*To:* Kuehling, Felix <[email protected]>
*Cc:* Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

            /* No memory allocation is allowed while holding the mn
    lock */
            amdgpu_mn_lock(p->mn);
            amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
                    struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                    if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
                            r = -ERESTARTSYS;
                            goto error_abort;
                    }
            }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

    ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
            amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:

So back to my previous question:

>> But do we really need another lock for this? Wouldn't the

>> re-validation of userptr BOs (currently calling get_user_pages) force

>> synchronization with the ongoing page table invalidation through the

>> mmap_sem or other MM locks?

>

> No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

>

> Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,

  Felix

*From:* Koenig, Christian
*Sent:* Thursday, September 27, 2018 3:00 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called  in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
  Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org <mailto:amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected] <mailto:[email protected]>>; Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:
> On 2018-09-14 01:52 PM, Christian König wrote:
>> Am 14.09.2018 um 19:47 schrieb Philip Yang:
>>> On 2018-09-14 03:51 AM, Christian König wrote:
>>>> Am 13.09.2018 um 23:51 schrieb Felix Kuehling:
>>>>> On 2018-09-13 04:52 PM, Philip Yang wrote:
>>>>> [SNIP]
>>>>>>    + amdgpu_mn_read_unlock(amn);
>>>>>> +
>>>>> amdgpu_mn_read_lock/unlock support recursive locking for multiple
>>>>> overlapping or nested invalidation ranges. But if you'r locking >>>>> and unlocking in the same function. Is that still a concern? >>> I don't understand the possible recursive case, but >>> amdgpu_mn_read_lock() still support recursive locking. >>>> Well the real problem is that unlocking them here won't work. >>>>
>>>> We need to hold the lock until we are sure that the operation which
>>>> updates the page tables is completed.
>>>>
>>> The reason for this change is because hmm mirror has >>> invalidate_start callback, no invalidate_end callback >>>
>>> Check mmu_notifier.c and hmm.c again, below is entire logic to >>> update CPU page tables and callback:
>>>
>>> mn lock amn->lock is used to protect interval tree access because >>> user may submit/register new userptr anytime.
>>> This is same for old and new way.
>>>
>>> step 2 guarantee the GPU operation is done before updating CPU page
>>> table.
>>>
>>> So I think the change is safe. We don't need hold mn lock until the
>>> CPU page tables update is completed.
>> No, that isn't even remotely correct. The lock doesn't protects the
>> interval tree.
>>
>>> Old:
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree >>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user >>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue >>> unmap/map operation done
>>>     3. update CPU page tables
>>>     4. up_read(&amn->lock)
>>>
>>> New, switch step 3 and 4
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree >>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user >>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue >>> unmap/map operation done
>>>     3. up_read(&amn->lock)
>>>     4. update CPU page tables
>> The lock is there to make sure that we serialize page table updates
>> with command submission.
> As I understand it, the idea is to prevent command submission (adding
> new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

> But do we really need another lock for this? Wouldn't the > re-validation of userptr BOs (currently calling get_user_pages) force
> synchronization with the ongoing page table invalidation through the
> mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Quoted 7 lines

Regards,
Christian.
_______________________________________________
amd-gfx mailing list
[email protected] <mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 425fe859-c780-48a5-a2c6-c3bf2b9abb38-5C7GfCeVMHo@public.gmane.org

#26 lore raw
Quoted 8 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 7:24 AM
To: Kuehling, Felix <[email protected]>
Cc: Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

Quoted 8 lines

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

       ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
       amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 5:27 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn);
        amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
                struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
                        r = -ERESTARTSYS;
                        goto error_abort;
                }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

        ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:
So back to my previous question:

Quoted 10 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

Quoted 5 lines

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 3:00 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <[email protected]<mailto:[email protected]>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>; Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]<mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): DM5PR12MB17072AE77EB0DE3AD22B8D7892140-2J9CzHegvk9TCtO+SvGBKwdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org

#28 lore raw

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 9 lines
Quoted 9 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

Quoted 8 lines
Quoted 1 line

We need to hold the lock until the fence is added to the reservation

object.

Quoted 2 lines

Otherwise somebody could have changed the page tables just in the

moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Quoted 302 lines

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 7:24 AM
*To:* Kuehling, Felix <[email protected]>
*Cc:* Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

Quoted 1 line

We double check that there wasn't any page table modification

while we prepared the submission and restart the whole process when there actually was some update.
>
> The reason why we need to do this is here:
>

> ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); >        amdgpu_mn_unlock(p->mn);

>
> Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

    ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

    amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 5:27 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn); amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) { struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) { r = -ERESTARTSYS; goto error_abort; }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:

So back to my previous question:

>> But do we really need another lock for this? Wouldn't the

>> re-validation of userptr BOs (currently calling get_user_pages) force

>> synchronization with the ongoing page table invalidation through the

>> mmap_sem or other MM locks?

>

> No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

>

> Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,

  Felix

*From:* Koenig, Christian
*Sent:* Thursday, September 27, 2018 3:00 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
  Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org <mailto:amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected] <mailto:[email protected]>>; Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:
> On 2018-09-14 01:52 PM, Christian König wrote:
>> Am 14.09.2018 um 19:47 schrieb Philip Yang:
>>> On 2018-09-14 03:51 AM, Christian König wrote:
>>>> Am 13.09.2018 um 23:51 schrieb Felix Kuehling:
>>>>> On 2018-09-13 04:52 PM, Philip Yang wrote:
>>>>> [SNIP]
>>>>>>    + amdgpu_mn_read_unlock(amn);
>>>>>> +
>>>>> amdgpu_mn_read_lock/unlock support recursive locking for multiple
>>>>> overlapping or nested invalidation ranges. But if you'r locking
>>>>> and unlocking in the same function. Is that still a concern? >>> I don't understand the possible recursive case, but >>> amdgpu_mn_read_lock() still support recursive locking. >>>> Well the real problem is that unlocking them here won't work. >>>>
>>>> We need to hold the lock until we are sure that the operation which
>>>> updates the page tables is completed.
>>>>
>>> The reason for this change is because hmm mirror has >>> invalidate_start callback, no invalidate_end callback >>>
>>> Check mmu_notifier.c and hmm.c again, below is entire logic to
>>> update CPU page tables and callback:
>>>
>>> mn lock amn->lock is used to protect interval tree access because
>>> user may submit/register new userptr anytime.
>>> This is same for old and new way.
>>>
>>> step 2 guarantee the GPU operation is done before updating CPU page
>>> table.
>>>
>>> So I think the change is safe. We don't need hold mn lock until the
>>> CPU page tables update is completed.
>> No, that isn't even remotely correct. The lock doesn't protects the
>> interval tree.
>>
>>> Old:
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree >>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue >>> unmap/map operation done
>>>     3. update CPU page tables
>>>     4. up_read(&amn->lock)
>>>
>>> New, switch step 3 and 4
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree >>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue >>> unmap/map operation done
>>>     3. up_read(&amn->lock)
>>>     4. update CPU page tables
>> The lock is there to make sure that we serialize page table updates
>> with command submission.
> As I understand it, the idea is to prevent command submission (adding
> new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

> But do we really need another lock for this? Wouldn't the > re-validation of userptr BOs (currently calling get_user_pages) force
> synchronization with the ongoing page table invalidation through the
> mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Quoted 8 lines

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected] <mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 9b427976-f2ff-8ba1-6ebf-588ca95aef80-5C7GfCeVMHo@public.gmane.org

#30 lore raw
Quoted 6 lines

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:22 AM
To: Kuehling, Felix <[email protected]>
Cc: Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 8 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 7:24 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

Quoted 8 lines

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

       ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
       amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 5:27 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn);
        amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
                struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
                        r = -ERESTARTSYS;
                        goto error_abort;
                }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

        ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:
So back to my previous question:

Quoted 10 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

Quoted 5 lines

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 3:00 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <[email protected]<mailto:[email protected]>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>; Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]<mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): DM5PR12MB17072879EC907150027D6F7892140-2J9CzHegvk9TCtO+SvGBKwdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org

#32 lore raw

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected]>:

Quoted 6 lines

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:22 AM
To: Kuehling, Felix <[email protected]>
Cc: Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 8 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 7:24 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

Quoted 8 lines

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

       ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
       amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 5:27 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn);
        amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
                struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
                        r = -ERESTARTSYS;
                        goto error_abort;
                }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

        ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:
So back to my previous question:

Quoted 10 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

Quoted 5 lines

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 3:00 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org<mailto:[email protected]>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>; Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]<mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 58199419-e20f-4ab0-ac1d-a7eb79f5c6f7-2ueSQiBKiTY7tOexoI0I+QC/[email protected]

#34 lore raw
Quoted 1 line

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

What's the sequence of events you have in mind? Something like this?

  *   Page table is updated and triggers MMU notifier
  *   amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
  *   New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
  *   get_user_pages returns outdated physical addresses

I hope that's not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:59 AM
To: Kuehling, Felix <[email protected]>
Cc: Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:

Quoted 6 lines

I'm not planning to change that. I don't think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

My whole argument is that you don't need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:22 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>
Cc: Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 8 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I'm not planning to change that. I don't think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 7:24 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

Quoted 8 lines

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

       ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
       amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don't see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 5:27 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn);
        amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
                struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
                        r = -ERESTARTSYS;
                        goto error_abort;
                }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

        ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:
So back to my previous question:

Quoted 10 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

Quoted 5 lines

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 3:00 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org<mailto:[email protected]>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>; Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]<mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): DM5PR12MB1707273AC4B0C03A3BEDF73092140-2J9CzHegvk9TCtO+SvGBKwdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org

#36 lore raw

At least with get_user_pages() that is perfectly possible.

For HMM it could be that this is prevented somehow.

Christian.

Am 27.09.2018 16:27 schrieb "Kuehling, Felix" <[email protected]>:

Quoted 1 line

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

What’s the sequence of events you have in mind? Something like this?

  *   Page table is updated and triggers MMU notifier
  *   amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
  *   New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
  *   get_user_pages returns outdated physical addresses

I hope that’s not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:59 AM
To: Kuehling, Felix <[email protected]>
Cc: Yang, Philip <[email protected]>; [email protected]; Jerome Glisse <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:

Quoted 6 lines

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:22 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>
Cc: Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 8 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 7:24 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

Quoted 8 lines

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

       ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
       amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 5:27 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn);
        amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
                struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
                        r = -ERESTARTSYS;
                        goto error_abort;
                }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

        ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:
So back to my previous question:

Quoted 10 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

Quoted 5 lines

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 3:00 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org<mailto:[email protected]>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>; Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]<mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1068c389-56fc-4125-ac40-b1ef2d60eabd-2ueSQiBKiTY7tOexoI0I+QC/[email protected]

#38 lore raw

I think the answer is here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/vm/hmm.rst#n216

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 10:30 AM
To: Kuehling, Felix <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

At least with get_user_pages() that is perfectly possible.

For HMM it could be that this is prevented somehow.

Christian.

Am 27.09.2018 16:27 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:

Quoted 1 line

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

What's the sequence of events you have in mind? Something like this?

  *   Page table is updated and triggers MMU notifier
  *   amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
  *   New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
  *   get_user_pages returns outdated physical addresses

I hope that's not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:59 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>
Cc: Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:

Quoted 6 lines

I'm not planning to change that. I don't think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

My whole argument is that you don't need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:22 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>
Cc: Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 8 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I'm not planning to change that. I don't think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 7:24 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

Quoted 8 lines

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

       ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
       amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don't see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:
ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 5:27 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn);
        amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
                struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
                        r = -ERESTARTSYS;
                        goto error_abort;
                }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

        ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:
So back to my previous question:

Quoted 10 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

Quoted 5 lines

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 3:00 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org<mailto:[email protected]>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>; Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]<mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): DM5PR12MB1707BCD7BFC10EDD594FE90B92140-2J9CzHegvk9TCtO+SvGBKwdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org

#40 lore raw

Yeah, I've read that as well.

My best guess is that we just need to add a call to hmm_vma_range_done() after taking the lock and also replace get_user_pages() with hmm_vma_get_pfns().

But I'm still not 100% sure how all of that is supposed to work together.

Regards,
Christian.

Am 27.09.2018 um 16:50 schrieb Kuehling, Felix:

Quoted 421 lines

I think the answer is here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/vm/hmm.rst#n216

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 10:30 AM
*To:* Kuehling, Felix <[email protected]>
*Cc:* [email protected]; Yang, Philip <[email protected]>; [email protected] *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

At least with get_user_pages() that is perfectly possible.

For HMM it could be that this is prevented somehow.

Christian.

Am 27.09.2018 16:27 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 1 line

In this case you can end up accessing pages which are invalidated while

get_user_pages is in process.

What’s the sequence of events you have in mind? Something like this?

* Page table is updated and triggers MMU notifier
* amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
* New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
* get_user_pages returns outdated physical addresses

I hope that’s not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:59 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 1 line

I’m not planning to change that. I don’t think there is any need to

change it.

Quoted 2 lines

Yeah, but when HMM doesn't provide both the start and the end hock

of the invalidation this way won't work any more.

Quoted 3 lines

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,

Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:22 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 9 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the

reservation object.
>
> Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,

Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 7:24 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

> We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update. >
> The reason why we need to do this is here:
>

> ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
>        amdgpu_mn_unlock(p->mn);

>
> Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,

Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 5:27 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn); amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) { struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) { r = -ERESTARTSYS; goto error_abort; }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:

So back to my previous question:

>> But do we really need another lock for this? Wouldn't the

>> re-validation of userptr BOs (currently calling get_user_pages) force

>> synchronization with the ongoing page table invalidation through the

>> mmap_sem or other MM locks?

>

> No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

>

> Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,

  Felix

*From:* Koenig, Christian
*Sent:* Thursday, September 27, 2018 3:00 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback
to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called  in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
  Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org <mailto:amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org>> On Behalf
Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected] <mailto:[email protected]>>; Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback
to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:
> On 2018-09-14 01:52 PM, Christian König wrote:
>> Am 14.09.2018 um 19:47 schrieb Philip Yang:
>>> On 2018-09-14 03:51 AM, Christian König wrote:
>>>> Am 13.09.2018 um 23:51 schrieb Felix Kuehling:
>>>>> On 2018-09-13 04:52 PM, Philip Yang wrote:
>>>>> [SNIP]
>>>>>>    + amdgpu_mn_read_unlock(amn);
>>>>>> +
>>>>> amdgpu_mn_read_lock/unlock support recursive locking for multiple
>>>>> overlapping or nested invalidation ranges. But if you'r locking
>>>>> and unlocking in the same function. Is that still a concern?
>>> I don't understand the possible recursive case, but >>> amdgpu_mn_read_lock() still support recursive locking. >>>> Well the real problem is that unlocking them here won't work.
>>>>
>>>> We need to hold the lock until we are sure that the operation which
>>>> updates the page tables is completed.
>>>>
>>> The reason for this change is because hmm mirror has >>> invalidate_start callback, no invalidate_end callback >>>
>>> Check mmu_notifier.c and hmm.c again, below is entire logic to
>>> update CPU page tables and callback:
>>>
>>> mn lock amn->lock is used to protect interval tree access because
>>> user may submit/register new userptr anytime. >>> This is same for old and new way.
>>>
>>> step 2 guarantee the GPU operation is done before updating CPU page
>>> table.
>>>
>>> So I think the change is safe. We don't need hold mn lock until the
>>> CPU page tables update is completed.
>> No, that isn't even remotely correct. The lock doesn't protects the
>> interval tree.
>>
>>> Old:
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. update CPU page tables
>>>     4. up_read(&amn->lock)
>>>
>>> New, switch step 3 and 4
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. up_read(&amn->lock)
>>>     4. update CPU page tables
>> The lock is there to make sure that we serialize page table updates
>> with command submission.
> As I understand it, the idea is to prevent command submission (adding
> new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

> But do we really need another lock for this? Wouldn't the > re-validation of userptr BOs (currently calling get_user_pages) force
> synchronization with the ongoing page table invalidation through the
> mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Quoted 8 lines

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected] <mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 11ba3857-9bb0-648e-2806-0533090d9a0a-5C7GfCeVMHo@public.gmane.org

#42 lore raw

I was trying to understand the way how HMM handle this concurrent issue and how we handle it in amdgpu_ttm_tt_userptr_needs_pages() and amdgpu_ttm_tt_affect_userptr(). HMM uses range->valid flag, we use gtt->mmu_invalidations and gtt->last_set_pages. Both use the same lock plus flag idea actually.

Thanks for the information, now I understand fence ttm_eu_fence_buffer_objects() put to BOs will block CPU page table update. This is another side of this concurrent issue I didn't know.

I had same worry that it has issue without invalidate_range_end() callback as the calling sequence Felix lists. Now I think it's fine after taking a look again today because of mm->mmap_sem usage, this is my understanding:

A path:

down_write(&mm->mmap_sem);
mmu_notifier_invalidate_range_start()
    take_lock()
    release_lock()
CPU page table update
mmu_notifier_invalidate_range_end()
up_write(&mm->mmap_sem);

B path:

again:
down_read(&mm->mmap_sem);
hmm_vma_get_pfns()
up_read(&mm->mmap_sem);
....
....
take_lock()
if (!hmm_vma_range_done()) {
   release_lock()
   goto again
}
submit command job...
release_lock()

If you agree, I will submit patch v5 with some minor changes, and submit another patch to replace get_user_page() with HMM.

Regards,
Philip

Quoted 442 lines · Christian König

Yeah, I've read that as well.

My best guess is that we just need to add a call to hmm_vma_range_done() after taking the lock and also replace get_user_pages() with hmm_vma_get_pfns().

But I'm still not 100% sure how all of that is supposed to work together.

Regards,
Christian.

Am 27.09.2018 um 16:50 schrieb Kuehling, Felix:

Quoted 431 lines

I think the answer is here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/vm/hmm.rst#n216

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 10:30 AM
*To:* Kuehling, Felix <[email protected]>
*Cc:* [email protected]; Yang, Philip <[email protected]>; [email protected] *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

At least with get_user_pages() that is perfectly possible.

For HMM it could be that this is prevented somehow.

Christian.

Am 27.09.2018 16:27 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 1 line

In this case you can end up accessing pages which are invalidated

while get_user_pages is in process.

What’s the sequence of events you have in mind? Something like this?

* Page table is updated and triggers MMU notifier
* amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
* New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
* get_user_pages returns outdated physical addresses

I hope that’s not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:59 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 1 line

I’m not planning to change that. I don’t think there is any need to

change it.

Quoted 2 lines

Yeah, but when HMM doesn't provide both the start and the end hock

of the invalidation this way won't work any more.

Quoted 3 lines

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,

Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:22 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 9 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the

reservation object.
>
> Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,

Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 7:24 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

> We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update. >
> The reason why we need to do this is here:
>

> ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
>        amdgpu_mn_unlock(p->mn);

>
> Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 5:27 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn); amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) { struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) { r = -ERESTARTSYS; goto error_abort; }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:

So back to my previous question:

>> But do we really need another lock for this? Wouldn't the

>> re-validation of userptr BOs (currently calling get_user_pages) force

>> synchronization with the ongoing page table invalidation through the

>> mmap_sem or other MM locks?

>

> No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

>

> Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,

  Felix

*From:* Koenig, Christian
*Sent:* Thursday, September 27, 2018 3:00 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror
callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called  in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
  Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org <mailto:amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org>> On Behalf
Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected] <mailto:[email protected]>>; Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback
to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:
> On 2018-09-14 01:52 PM, Christian König wrote:
>> Am 14.09.2018 um 19:47 schrieb Philip Yang:
>>> On 2018-09-14 03:51 AM, Christian König wrote:
>>>> Am 13.09.2018 um 23:51 schrieb Felix Kuehling:
>>>>> On 2018-09-13 04:52 PM, Philip Yang wrote:
>>>>> [SNIP]
>>>>>>    + amdgpu_mn_read_unlock(amn);
>>>>>> +
>>>>> amdgpu_mn_read_lock/unlock support recursive locking for multiple
>>>>> overlapping or nested invalidation ranges. But if you'r locking
>>>>> and unlocking in the same function. Is that still a concern?
>>> I don't understand the possible recursive case, but >>> amdgpu_mn_read_lock() still support recursive locking. >>>> Well the real problem is that unlocking them here won't work.
>>>>
>>>> We need to hold the lock until we are sure that the operation which
>>>> updates the page tables is completed.
>>>>
>>> The reason for this change is because hmm mirror has >>> invalidate_start callback, no invalidate_end callback >>>
>>> Check mmu_notifier.c and hmm.c again, below is entire logic to
>>> update CPU page tables and callback:
>>>
>>> mn lock amn->lock is used to protect interval tree access because
>>> user may submit/register new userptr anytime. >>> This is same for old and new way.
>>>
>>> step 2 guarantee the GPU operation is done before updating CPU page
>>> table.
>>>
>>> So I think the change is safe. We don't need hold mn lock until the
>>> CPU page tables update is completed.
>> No, that isn't even remotely correct. The lock doesn't protects the
>> interval tree.
>>
>>> Old:
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. update CPU page tables
>>>     4. up_read(&amn->lock)
>>>
>>> New, switch step 3 and 4
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. up_read(&amn->lock)
>>>     4. update CPU page tables
>> The lock is there to make sure that we serialize page table updates
>> with command submission.
> As I understand it, the idea is to prevent command submission (adding
> new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

> But do we really need another lock for this? Wouldn't the > re-validation of userptr BOs (currently calling get_user_pages) force
> synchronization with the ongoing page table invalidation through the
> mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected] <mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 16d1faf6-80a4-dc46-bd2a-9cd475808e98-5C7GfCeVMHo@public.gmane.org

#44 lore raw

No, that is incorrect as well :)

The mmap_sem isn't necessary taken during page table updates.

What you could do is replace get_user_pages() directly with HMM. If I'm not completely mistaken that should work as expected.

Christian.

Am 27.09.2018 22:18 schrieb "Yang, Philip" <[email protected]>:
I was trying to understand the way how HMM handle this concurrent issue and how we handle it in amdgpu_ttm_tt_userptr_needs_pages() and amdgpu_ttm_tt_affect_userptr(). HMM uses range->valid flag, we use gtt->mmu_invalidations and gtt->last_set_pages. Both use the same lock plus flag idea actually.

Thanks for the information, now I understand fence ttm_eu_fence_buffer_objects() put to BOs will block CPU page table update. This is another side of this concurrent issue I didn't know.

I had same worry that it has issue without invalidate_range_end() callback as the calling sequence Felix lists. Now I think it's fine after taking a look again today because of mm->mmap_sem usage, this is my understanding:

A path:

down_write(&mm->mmap_sem);
mmu_notifier_invalidate_range_start()

    take_lock()
    release_lock()

CPU page table update
mmu_notifier_invalidate_range_end()
up_write(&mm->mmap_sem);

B path:

again:
down_read(&mm->mmap_sem);
hmm_vma_get_pfns()
up_read(&mm->mmap_sem);
....
....
take_lock()
if (!hmm_vma_range_done()) {
release_lock()
goto again
}
submit command job...
release_lock()

If you agree, I will submit patch v5 with some minor changes, and submit another patch to replace get_user_page() with HMM.

Regards,
Philip

On 2018-09-27 11:36 AM, Christian König wrote:
Yeah, I've read that as well.

My best guess is that we just need to add a call to hmm_vma_range_done() after taking the lock and also replace get_user_pages() with hmm_vma_get_pfns().

But I'm still not 100% sure how all of that is supposed to work together.

Regards,
Christian.

Am 27.09.2018 um 16:50 schrieb Kuehling, Felix:
I think the answer is here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/vm/hmm.rst#n216

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 10:30 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: [email protected]<mailto:[email protected]>; Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

At least with get_user_pages() that is perfectly possible.

For HMM it could be that this is prevented somehow.

Christian.

Am 27.09.2018 16:27 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:

Quoted 1 line

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

What’s the sequence of events you have in mind? Something like this?

  *   Page table is updated and triggers MMU notifier
  *   amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
  *   New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
  *   get_user_pages returns outdated physical addresses

I hope that’s not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:59 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>
Cc: Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:

Quoted 6 lines

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:22 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>
Cc: Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 8 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 7:24 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

Quoted 8 lines

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

       ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
       amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:
ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 5:27 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn);
        amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
                struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
                        r = -ERESTARTSYS;
                        goto error_abort;
                }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

        ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:
So back to my previous question:

Quoted 10 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

Quoted 5 lines

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 3:00 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org<mailto:[email protected]>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>; Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]<mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 8f9f5703-214f-488d-9cfe-ccc64e8cd009-2ueSQiBKiTY7tOexoI0I+QC/[email protected]

#46 lore raw

For B path, we take mm->mmap_sem, then call hmm_vma_get_pfns() or get_user_pages(). This is obvious.

For A path, mmu notifier mmu_notifier_invalidate_range_start()/mmu_notifier_invalidate_range_end() is called in many places, and the calling path is quit complicated inside mm, it's not obvious. I checked many of the them, for example:

do_munmap()
  down_write(&mm->mmap_sem)
  arch_unmap()
    mpx_notify_unmap()...
       zap_bt_entries_mapping()
         zap_page_range()
 up_write(&mm->mmap_sem)

void zap_page_range(struct vm_area_struct *vma, unsigned long start, unsigned long size)
{
    struct mm_struct *mm = vma->vm_mm;
    struct mmu_gather tlb;
    unsigned long end = start + size;

    lru_add_drain();
    tlb_gather_mmu(&tlb, mm, start, end); update_hiwater_rss(mm); mmu_notifier_invalidate_range_start(mm, start, end); for ( ; vma && vma->vm_start < end; vma = vma->vm_next) unmap_single_vma(&tlb, vma, start, end, NULL); mmu_notifier_invalidate_range_end(mm, start, end); tlb_finish_mmu(&tlb, start, end);
}

So AFAIK it's okay without invalidate_range_end() callback.

Regards,
Philip

Quoted 41 lines · Christian

No, that is incorrect as well :)

The mmap_sem isn't necessary taken during page table updates.

What you could do is replace get_user_pages() directly with HMM. If I'm not completely mistaken that should work as expected.

Christian.

Am 27.09.2018 22:18 schrieb "Yang, Philip" <[email protected]>:
I was trying to understand the way how HMM handle this concurrent issue and how we handle it in amdgpu_ttm_tt_userptr_needs_pages() and amdgpu_ttm_tt_affect_userptr(). HMM uses range->valid flag, we use gtt->mmu_invalidations and gtt->last_set_pages. Both use the same lock plus flag idea actually.

Thanks for the information, now I understand fence ttm_eu_fence_buffer_objects() put to BOs will block CPU page table update. This is another side of this concurrent issue I didn't know.

I had same worry that it has issue without invalidate_range_end() callback as the calling sequence Felix lists. Now I think it's fine after taking a look again today because of mm->mmap_sem usage, this is my understanding:

A path:

down_write(&mm->mmap_sem);
mmu_notifier_invalidate_range_start()
    take_lock()
    release_lock()
CPU page table update
mmu_notifier_invalidate_range_end()
up_write(&mm->mmap_sem);

B path:

again:
down_read(&mm->mmap_sem);
hmm_vma_get_pfns()
up_read(&mm->mmap_sem);
....
....
take_lock()
if (!hmm_vma_range_done()) {
   release_lock()
   goto again
}
submit command job...
release_lock()

Quoted 453 lines

If you agree, I will submit patch v5 with some minor changes, and submit another patch to replace get_user_page() with HMM.

Regards,
Philip

Quoted 446 lines · Christian König

Yeah, I've read that as well.

My best guess is that we just need to add a call to hmm_vma_range_done() after taking the lock and also replace get_user_pages() with hmm_vma_get_pfns().

But I'm still not 100% sure how all of that is supposed to work together.

Regards,
Christian.

Am 27.09.2018 um 16:50 schrieb Kuehling, Felix:

Quoted 435 lines

I think the answer is here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/vm/hmm.rst#n216

Regards,

Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 10:30 AM
*To:* Kuehling, Felix <[email protected]>
*Cc:* [email protected]; Yang, Philip <[email protected]>; [email protected] *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

At least with get_user_pages() that is perfectly possible.

For HMM it could be that this is prevented somehow.

Christian.

Am 27.09.2018 16:27 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 1 line

In this case you can end up accessing pages which are invalidated

while get_user_pages is in process.

What’s the sequence of events you have in mind? Something like this?

* Page table is updated and triggers MMU notifier
* amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
* New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
* get_user_pages returns outdated physical addresses

I hope that’s not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:59 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 4 lines

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock

of the invalidation this way won't work any more.

Quoted 3 lines

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,

Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:22 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 9 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the

reservation object.
>
> Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 7:24 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

> We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update. >
> The reason why we need to do this is here:
>

> ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
>        amdgpu_mn_unlock(p->mn);

>
> Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 5:27 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback
to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn); amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) { struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) { r = -ERESTARTSYS; goto error_abort; }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:

So back to my previous question:

>> But do we really need another lock for this? Wouldn't the

>> re-validation of userptr BOs (currently calling get_user_pages) force

>> synchronization with the ongoing page table invalidation through the

>> mmap_sem or other MM locks?

>

> No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

>

> Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,

  Felix

*From:* Koenig, Christian
*Sent:* Thursday, September 27, 2018 3:00 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror
callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called  in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
  Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org <mailto:amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org>> On
Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected] <mailto:[email protected]>>; Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback
to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:
> On 2018-09-14 01:52 PM, Christian König wrote:
>> Am 14.09.2018 um 19:47 schrieb Philip Yang:
>>> On 2018-09-14 03:51 AM, Christian König wrote:
>>>> Am 13.09.2018 um 23:51 schrieb Felix Kuehling:
>>>>> On 2018-09-13 04:52 PM, Philip Yang wrote:
>>>>> [SNIP]
>>>>>>    + amdgpu_mn_read_unlock(amn);
>>>>>> +
>>>>> amdgpu_mn_read_lock/unlock support recursive locking for multiple
>>>>> overlapping or nested invalidation ranges. But if you'r locking
>>>>> and unlocking in the same function. Is that still a concern?
>>> I don't understand the possible recursive case, but >>> amdgpu_mn_read_lock() still support recursive locking. >>>> Well the real problem is that unlocking them here won't work.
>>>>
>>>> We need to hold the lock until we are sure that the operation which
>>>> updates the page tables is completed.
>>>>
>>> The reason for this change is because hmm mirror has >>> invalidate_start callback, no invalidate_end callback >>>
>>> Check mmu_notifier.c and hmm.c again, below is entire logic to
>>> update CPU page tables and callback:
>>>
>>> mn lock amn->lock is used to protect interval tree access because
>>> user may submit/register new userptr anytime. >>> This is same for old and new way.
>>>
>>> step 2 guarantee the GPU operation is done before updating CPU page
>>> table.
>>>
>>> So I think the change is safe. We don't need hold mn lock until the
>>> CPU page tables update is completed.
>> No, that isn't even remotely correct. The lock doesn't protects the
>> interval tree.
>>
>>> Old:
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. update CPU page tables
>>>     4. up_read(&amn->lock)
>>>
>>> New, switch step 3 and 4
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. up_read(&amn->lock)
>>>     4. update CPU page tables
>> The lock is there to make sure that we serialize page table updates
>> with command submission.
> As I understand it, the idea is to prevent command submission (adding
> new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

> But do we really need another lock for this? Wouldn't the > re-validation of userptr BOs (currently calling get_user_pages) force
> synchronization with the ongoing page table invalidation through the
> mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected] <mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): fe0d429b-5038-a297-e02e-423302544477-5C7GfCeVMHo@public.gmane.org

#48 lore raw

No it definitely isn't.

We have literally worked month on this with the core MM developers.

Making sure that we have a consistent page array is absolutely vital for correct operation.

Please also check Jerome's presentation from XDC it also perfectly explains why this approach won't work correctly.

Christian.

Am 28.09.2018 17:07 schrieb "Yang, Philip" <[email protected]>:
For B path, we take mm->mmap_sem, then call hmm_vma_get_pfns() or get_user_pages(). This is obvious.

For A path, mmu notifier mmu_notifier_invalidate_range_start()/mmu_notifier_invalidate_range_end() is called in many places, and the calling path is quit complicated inside mm, it's not obvious. I checked many of the them, for example:

do_munmap()
down_write(&mm->mmap_sem)
arch_unmap()

    mpx_notify_unmap()...
       zap_bt_entries_mapping()
         zap_page_range()

up_write(&mm->mmap_sem)

void zap_page_range(struct vm_area_struct *vma, unsigned long start,
unsigned long size)

{
    struct mm_struct *mm = vma->vm_mm;
    struct mmu_gather tlb;
    unsigned long end = start + size;

    lru_add_drain();
    tlb_gather_mmu(&tlb, mm, start, end);
    update_hiwater_rss(mm);
    mmu_notifier_invalidate_range_start(mm, start, end);
    for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
        unmap_single_vma(&tlb, vma, start, end, NULL);
    mmu_notifier_invalidate_range_end(mm, start, end);
    tlb_finish_mmu(&tlb, start, end);
}

So AFAIK it's okay without invalidate_range_end() callback.

Regards,
Philip

On 2018-09-28 01:25 AM, Koenig, Christian wrote:
No, that is incorrect as well :)

The mmap_sem isn't necessary taken during page table updates.

What you could do is replace get_user_pages() directly with HMM. If I'm not completely mistaken that should work as expected.

Christian.

Am 27.09.2018 22:18 schrieb "Yang, Philip" <[email protected]><mailto:[email protected]>:
I was trying to understand the way how HMM handle this concurrent issue and how we handle it in amdgpu_ttm_tt_userptr_needs_pages() and amdgpu_ttm_tt_affect_userptr(). HMM uses range->valid flag, we use gtt->mmu_invalidations and gtt->last_set_pages. Both use the same lock plus flag idea actually.

Thanks for the information, now I understand fence ttm_eu_fence_buffer_objects() put to BOs will block CPU page table update. This is another side of this concurrent issue I didn't know.

I had same worry that it has issue without invalidate_range_end() callback as the calling sequence Felix lists. Now I think it's fine after taking a look again today because of mm->mmap_sem usage, this is my understanding:

A path:

down_write(&mm->mmap_sem);
mmu_notifier_invalidate_range_start()

    take_lock()
    release_lock()

CPU page table update
mmu_notifier_invalidate_range_end()
up_write(&mm->mmap_sem);

B path:

again:
down_read(&mm->mmap_sem);
hmm_vma_get_pfns()
up_read(&mm->mmap_sem);
....
....
take_lock()
if (!hmm_vma_range_done()) {
release_lock()
goto again
}
submit command job...
release_lock()

If you agree, I will submit patch v5 with some minor changes, and submit another patch to replace get_user_page() with HMM.

Regards,
Philip

On 2018-09-27 11:36 AM, Christian König wrote:
Yeah, I've read that as well.

My best guess is that we just need to add a call to hmm_vma_range_done() after taking the lock and also replace get_user_pages() with hmm_vma_get_pfns().

But I'm still not 100% sure how all of that is supposed to work together.

Regards,
Christian.

Am 27.09.2018 um 16:50 schrieb Kuehling, Felix:
I think the answer is here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/vm/hmm.rst#n216

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 10:30 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: [email protected]<mailto:[email protected]>; Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

At least with get_user_pages() that is perfectly possible.

For HMM it could be that this is prevented somehow.

Christian.

Am 27.09.2018 16:27 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:

Quoted 1 line

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

What’s the sequence of events you have in mind? Something like this?

  *   Page table is updated and triggers MMU notifier
  *   amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
  *   New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
  *   get_user_pages returns outdated physical addresses

I hope that’s not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:59 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>
Cc: Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:

Quoted 6 lines

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 9:22 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>
Cc: Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 8 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 7:24 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

Quoted 8 lines

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

       ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
       amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:
ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 5:27 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */
        amdgpu_mn_lock(p->mn);
        amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
                struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
                        r = -ERESTARTSYS;
                        goto error_abort;
                }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

        ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:
So back to my previous question:

Quoted 10 lines
Quoted 7 lines

But do we really need another lock for this? Wouldn't the

re-validation of userptr BOs (currently calling get_user_pages) force

synchronization with the ongoing page table invalidation through the

mmap_sem or other MM locks?

Quoted 5 lines

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,
Felix

From: Koenig, Christian
Sent: Thursday, September 27, 2018 3:00 AM
To: Kuehling, Felix <[email protected]><mailto:[email protected]>
Cc: Yang, Philip <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]><mailto:[email protected]>
Subject: RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected]<mailto:[email protected]>>:
I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org<mailto:[email protected]>> On Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected]<mailto:[email protected]>>; Yang, Philip <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>; Jerome Glisse <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:

Quoted 57 lines · Christian König
Quoted 56 lines

Am 14.09.2018 um 19:47 schrieb Philip Yang:

Quoted 26 lines · Christian König
Quoted 8 lines

Am 13.09.2018 um 23:51 schrieb Felix Kuehling:

Quoted 7 lines · Philip Yang

[SNIP]

Quoted 2 lines

+ amdgpu_mn_read_unlock(amn);
+

amdgpu_mn_read_lock/unlock support recursive locking for multiple overlapping or nested invalidation ranges. But if you'r locking and unlocking in the same function. Is that still a concern?

I don't understand the possible recursive case, but amdgpu_mn_read_lock() still support recursive locking.

Quoted 5 lines

Well the real problem is that unlocking them here won't work.

We need to hold the lock until we are sure that the operation which updates the page tables is completed.

The reason for this change is because hmm mirror has invalidate_start callback, no invalidate_end callback

Check mmu_notifier.c and hmm.c again, below is entire logic to update CPU page tables and callback:

mn lock amn->lock is used to protect interval tree access because user may submit/register new userptr anytime.
This is same for old and new way.

Quoted 5 lines

step 2 guarantee the GPU operation is done before updating CPU page table.

So I think the change is safe. We don't need hold mn lock until the CPU page tables update is completed.

No, that isn't even remotely correct. The lock doesn't protects the interval tree.

Quoted 21 lines

Old:
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. update CPU page tables
4. up_read(&amn->lock)

New, switch step 3 and 4
1. down_read_non_owner(&amn->lock)
2. loop to handle BOs from node->bos through interval tree
amn->object nodes
gfx: wait for pending BOs fence operation done, mark user
pages dirty
kfd: evict user queues of the process, wait for queue
unmap/map operation done
3. up_read(&amn->lock)
4. update CPU page tables

Quoted 4 lines
Quoted 2 lines

The lock is there to make sure that we serialize page table updates with command submission.

As I understand it, the idea is to prevent command submission (adding new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

Quoted 4 lines

But do we really need another lock for this? Wouldn't the re-validation of userptr BOs (currently calling get_user_pages) force synchronization with the ongoing page table invalidation through the mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected]<mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): b8686e6b-0c3e-4feb-afbd-80397aac31a0-2ueSQiBKiTY7tOexoI0I+QC/[email protected]

#50 lore raw

Checking more code and documentation and thinking about it over my vacation I think I have some new conclusions here.

Currently we are using get_user_pages() together with an MMU notifier to guarantee coherent address space view, because get_user_pages() works by grabbing a reference to the pages and ignoring concurrent page table updates.

But HMM uses a different approach by checking the address space for modifications using hmm_vma_range_done() and re-trying when the address space has changed.

Now what you are trying to do is to change that into get_user_pages() and HMM callback and this is what won't work. We can either use get_user_pages() with MMU notifier or we can use HMM for the work, but we can't mix and match.

So my initial guess was correct that we just need to change both sides of the implementation at the same time.

Regards,
Christian.

Am 28.09.2018 um 17:13 schrieb Koenig, Christian:

Quoted 545 lines

No it definitely isn't.

We have literally worked month on this with the core MM developers.

Making sure that we have a consistent page array is absolutely vital for correct operation.

Please also check Jerome's presentation from XDC it also perfectly explains why this approach won't work correctly.

Christian.

Am 28.09.2018 17:07 schrieb "Yang, Philip" <[email protected]>:
For B path, we take mm->mmap_sem, then call hmm_vma_get_pfns() or get_user_pages(). This is obvious.

For A path, mmu notifier mmu_notifier_invalidate_range_start()/mmu_notifier_invalidate_range_end() is called in many places, and the calling path is quit complicated inside mm, it's not obvious. I checked many of the them, for example:

do_munmap()
  down_write(&mm->mmap_sem)
  arch_unmap()
    mpx_notify_unmap()...
       zap_bt_entries_mapping()
         zap_page_range()
 up_write(&mm->mmap_sem)

void zap_page_range(struct vm_area_struct *vma, unsigned long start, unsigned long size)
{
    struct mm_struct *mm = vma->vm_mm;
    struct mmu_gather tlb;
    unsigned long end = start + size;

    lru_add_drain();
    tlb_gather_mmu(&tlb, mm, start, end); update_hiwater_rss(mm); mmu_notifier_invalidate_range_start(mm, start, end); for ( ; vma && vma->vm_start < end; vma = vma->vm_next) unmap_single_vma(&tlb, vma, start, end, NULL); mmu_notifier_invalidate_range_end(mm, start, end); tlb_finish_mmu(&tlb, start, end);
}

So AFAIK it's okay without invalidate_range_end() callback.

Regards,
Philip

Quoted 41 lines · Christian

No, that is incorrect as well :)

The mmap_sem isn't necessary taken during page table updates.

What you could do is replace get_user_pages() directly with HMM. If I'm not completely mistaken that should work as expected.

Christian.

Am 27.09.2018 22:18 schrieb "Yang, Philip" <[email protected]>:
I was trying to understand the way how HMM handle this concurrent issue and how we handle it in amdgpu_ttm_tt_userptr_needs_pages() and amdgpu_ttm_tt_affect_userptr(). HMM uses range->valid flag, we use gtt->mmu_invalidations and gtt->last_set_pages. Both use the same lock plus flag idea actually.

Thanks for the information, now I understand fence ttm_eu_fence_buffer_objects() put to BOs will block CPU page table update. This is another side of this concurrent issue I didn't know.

I had same worry that it has issue without invalidate_range_end() callback as the calling sequence Felix lists. Now I think it's fine after taking a look again today because of mm->mmap_sem usage, this is my understanding:

A path:

down_write(&mm->mmap_sem);
mmu_notifier_invalidate_range_start()
    take_lock()
    release_lock()
CPU page table update
mmu_notifier_invalidate_range_end()
up_write(&mm->mmap_sem);

B path:

again:
down_read(&mm->mmap_sem);
hmm_vma_get_pfns()
up_read(&mm->mmap_sem);
....
....
take_lock()
if (!hmm_vma_range_done()) {
   release_lock()
   goto again
}
submit command job...
release_lock()

Quoted 455 lines

If you agree, I will submit patch v5 with some minor changes, and submit another patch to replace get_user_page() with HMM.

Regards,
Philip

Quoted 448 lines · Christian König

Yeah, I've read that as well.

My best guess is that we just need to add a call to hmm_vma_range_done() after taking the lock and also replace get_user_pages() with hmm_vma_get_pfns().

But I'm still not 100% sure how all of that is supposed to work together.

Regards,
Christian.

Am 27.09.2018 um 16:50 schrieb Kuehling, Felix:

Quoted 437 lines

I think the answer is here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/vm/hmm.rst#n216

Regards,

Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 10:30 AM
*To:* Kuehling, Felix <[email protected]>
*Cc:* [email protected]; Yang, Philip <[email protected]>; [email protected] *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

At least with get_user_pages() that is perfectly possible.

For HMM it could be that this is prevented somehow.

Christian.

Am 27.09.2018 16:27 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 1 line

In this case you can end up accessing pages which are invalidated while

get_user_pages is in process.

What’s the sequence of events you have in mind? Something like this?

* Page table is updated and triggers MMU notifier
* amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
* New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
* get_user_pages returns outdated physical addresses

I hope that’s not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:59 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 4 lines

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end

hock of the invalidation this way won't work any more.

Quoted 3 lines

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:22 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 9 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the

reservation object.
>
> Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 7:24 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

> We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update. >
> The reason why we need to do this is here:
>

> ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
>        amdgpu_mn_unlock(p->mn);

>
> Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 5:27 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback
to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */ amdgpu_mn_lock(p->mn); amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) { struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) { r = -ERESTARTSYS; goto error_abort; }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:

So back to my previous question:

>> But do we really need another lock for this? Wouldn't the

>> re-validation of userptr BOs (currently calling get_user_pages) force

>> synchronization with the ongoing page table invalidation through the

>> mmap_sem or other MM locks?

>

> No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

>

> Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,

  Felix

*From:* Koenig, Christian
*Sent:* Thursday, September 27, 2018 3:00 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror
callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called  in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
  Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org <mailto:amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org>> On
Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected] <mailto:[email protected]>>; Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror
callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:
> On 2018-09-14 01:52 PM, Christian König wrote:
>> Am 14.09.2018 um 19:47 schrieb Philip Yang:
>>> On 2018-09-14 03:51 AM, Christian König wrote:
>>>> Am 13.09.2018 um 23:51 schrieb Felix Kuehling:
>>>>> On 2018-09-13 04:52 PM, Philip Yang wrote:
>>>>> [SNIP]
>>>>>>    + amdgpu_mn_read_unlock(amn);
>>>>>> +
>>>>> amdgpu_mn_read_lock/unlock support recursive locking for multiple
>>>>> overlapping or nested invalidation ranges. But if you'r locking
>>>>> and unlocking in the same function. Is that still a concern?
>>> I don't understand the possible recursive case, but >>> amdgpu_mn_read_lock() still support recursive locking. >>>> Well the real problem is that unlocking them here won't work.
>>>>
>>>> We need to hold the lock until we are sure that the operation which
>>>> updates the page tables is completed.
>>>>
>>> The reason for this change is because hmm mirror has >>> invalidate_start callback, no invalidate_end callback >>>
>>> Check mmu_notifier.c and hmm.c again, below is entire logic to
>>> update CPU page tables and callback:
>>>
>>> mn lock amn->lock is used to protect interval tree access because
>>> user may submit/register new userptr anytime. >>> This is same for old and new way.
>>>
>>> step 2 guarantee the GPU operation is done before updating CPU page
>>> table.
>>>
>>> So I think the change is safe. We don't need hold mn lock until the
>>> CPU page tables update is completed.
>> No, that isn't even remotely correct. The lock doesn't protects the
>> interval tree.
>>
>>> Old:
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. update CPU page tables
>>>     4. up_read(&amn->lock)
>>>
>>> New, switch step 3 and 4
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. up_read(&amn->lock)
>>>     4. update CPU page tables
>> The lock is there to make sure that we serialize page table updates
>> with command submission.
> As I understand it, the idea is to prevent command submission (adding
> new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

> But do we really need another lock for this? Wouldn't the
> re-validation of userptr BOs (currently calling get_user_pages) force
> synchronization with the ongoing page table invalidation through the
> mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected] <mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Quoted 4 lines

_______________________________________________
amd-gfx mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 09916f9a-3f5f-27ab-01e6-6d77303cf052-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#52 lore raw

Hi Christian,

Yes, I agree. I am working on patch 2 to replace get_user_page with HMM. One problem is in current gfx path, we check if mmu_invalidation multiple times in amdgpu_cs_ioctl() path after get_user_page(), amdgpu_cs_parser_bos(), amdgpu_cs_list_validate(), and amdgpu_cs_submit(). For HMM, hmm_vma_range_done() has to be called once and only once after hmm_vma_get_pfns()/hmm_vma_fault(), so I will call hmm_vma_range_done() inside amdgpu_cs_submit after holding the mn lock. Is my understanding correct?

Philip

Quoted 6 lines · Christian König

Checking more code and documentation and thinking about it over my vacation I think I have some new conclusions here.

Currently we are using get_user_pages() together with an MMU notifier to guarantee coherent address space view, because get_user_pages() works by grabbing a reference to the pages and ignoring concurrent page table updates.

But HMM uses a different approach by checking the address space for modifications using hmm_vma_range_done() and re-trying when the address space has changed.

Quoted 561 lines

Now what you are trying to do is to change that into get_user_pages() and HMM callback and this is what won't work. We can either use get_user_pages() with MMU notifier or we can use HMM for the work, but we can't mix and match.

So my initial guess was correct that we just need to change both sides of the implementation at the same time.

Regards,
Christian.

Am 28.09.2018 um 17:13 schrieb Koenig, Christian:

Quoted 552 lines

No it definitely isn't.

We have literally worked month on this with the core MM developers.

Making sure that we have a consistent page array is absolutely vital for correct operation.

Please also check Jerome's presentation from XDC it also perfectly explains why this approach won't work correctly.

Christian.

Am 28.09.2018 17:07 schrieb "Yang, Philip" <[email protected]>:
For B path, we take mm->mmap_sem, then call hmm_vma_get_pfns() or get_user_pages(). This is obvious.

For A path, mmu notifier mmu_notifier_invalidate_range_start()/mmu_notifier_invalidate_range_end() is called in many places, and the calling path is quit complicated inside mm, it's not obvious. I checked many of the them, for example:

do_munmap()
  down_write(&mm->mmap_sem)
  arch_unmap()
    mpx_notify_unmap()...
       zap_bt_entries_mapping()
         zap_page_range()
 up_write(&mm->mmap_sem)

void zap_page_range(struct vm_area_struct *vma, unsigned long start, unsigned long size)
{
    struct mm_struct *mm = vma->vm_mm;
    struct mmu_gather tlb;
    unsigned long end = start + size;

    lru_add_drain();
    tlb_gather_mmu(&tlb, mm, start, end); update_hiwater_rss(mm); mmu_notifier_invalidate_range_start(mm, start, end); for ( ; vma && vma->vm_start < end; vma = vma->vm_next) unmap_single_vma(&tlb, vma, start, end, NULL); mmu_notifier_invalidate_range_end(mm, start, end); tlb_finish_mmu(&tlb, start, end);
}

So AFAIK it's okay without invalidate_range_end() callback.

Regards,
Philip

Quoted 41 lines · Christian

No, that is incorrect as well :)

The mmap_sem isn't necessary taken during page table updates.

What you could do is replace get_user_pages() directly with HMM. If I'm not completely mistaken that should work as expected.

Christian.

Am 27.09.2018 22:18 schrieb "Yang, Philip" <[email protected]>:
I was trying to understand the way how HMM handle this concurrent issue and how we handle it in amdgpu_ttm_tt_userptr_needs_pages() and amdgpu_ttm_tt_affect_userptr(). HMM uses range->valid flag, we use gtt->mmu_invalidations and gtt->last_set_pages. Both use the same lock plus flag idea actually.

Thanks for the information, now I understand fence ttm_eu_fence_buffer_objects() put to BOs will block CPU page table update. This is another side of this concurrent issue I didn't know.

I had same worry that it has issue without invalidate_range_end() callback as the calling sequence Felix lists. Now I think it's fine after taking a look again today because of mm->mmap_sem usage, this is my understanding:

A path:

down_write(&mm->mmap_sem);
mmu_notifier_invalidate_range_start()
    take_lock()
    release_lock()
CPU page table update
mmu_notifier_invalidate_range_end()
up_write(&mm->mmap_sem);

B path:

again:
down_read(&mm->mmap_sem);
hmm_vma_get_pfns()
up_read(&mm->mmap_sem);
....
....
take_lock()
if (!hmm_vma_range_done()) {
   release_lock()
   goto again
}
submit command job...
release_lock()

Quoted 458 lines

If you agree, I will submit patch v5 with some minor changes, and submit another patch to replace get_user_page() with HMM.

Regards,
Philip

Quoted 451 lines · Christian König

Yeah, I've read that as well.

My best guess is that we just need to add a call to hmm_vma_range_done() after taking the lock and also replace get_user_pages() with hmm_vma_get_pfns().

But I'm still not 100% sure how all of that is supposed to work together.

Regards,
Christian.

Am 27.09.2018 um 16:50 schrieb Kuehling, Felix:

Quoted 440 lines

I think the answer is here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/vm/hmm.rst#n216

Regards,

Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 10:30 AM
*To:* Kuehling, Felix <[email protected]>
*Cc:* [email protected]; Yang, Philip <[email protected]>; [email protected] *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

At least with get_user_pages() that is perfectly possible.

For HMM it could be that this is prevented somehow.

Christian.

Am 27.09.2018 16:27 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 1 line

In this case you can end up accessing pages which are invalidated while

get_user_pages is in process.

What’s the sequence of events you have in mind? Something like this?

* Page table is updated and triggers MMU notifier
* amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
* New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
* get_user_pages returns outdated physical addresses

I hope that’s not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:59 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 1 line

I’m not planning to change that. I don’t think there is any need to

change it.

Quoted 2 lines

Yeah, but when HMM doesn't provide both the start and the end

hock of the invalidation this way won't work any more.

Quoted 3 lines

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:22 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 4 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated,

p->fence);

> amdgpu_mn_unlock(p->mn);

>
> We need to hold the lock until the fence is added to the reservation object.
>
> Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 7:24 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

> We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update. >
> The reason why we need to do this is here:
>

> ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
> amdgpu_mn_unlock(p->mn);

>
> Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 5:27 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback
to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */ amdgpu_mn_lock(p->mn); amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) { struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) { r = -ERESTARTSYS; goto error_abort; }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:

So back to my previous question:

>> But do we really need another lock for this? Wouldn't the

>> re-validation of userptr BOs (currently calling get_user_pages) force

>> synchronization with the ongoing page table invalidation through the

>> mmap_sem or other MM locks?

>

> No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

>

> Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,

  Felix

*From:* Koenig, Christian
*Sent:* Thursday, September 27, 2018 3:00 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror
callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called  in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
  Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org <mailto:amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org>> On
Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected] <mailto:[email protected]>>; Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror
callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:
> On 2018-09-14 01:52 PM, Christian König wrote:
>> Am 14.09.2018 um 19:47 schrieb Philip Yang:
>>> On 2018-09-14 03:51 AM, Christian König wrote:
>>>> Am 13.09.2018 um 23:51 schrieb Felix Kuehling:
>>>>> On 2018-09-13 04:52 PM, Philip Yang wrote:
>>>>> [SNIP]
>>>>>>    + amdgpu_mn_read_unlock(amn);
>>>>>> +
>>>>> amdgpu_mn_read_lock/unlock support recursive locking for multiple
>>>>> overlapping or nested invalidation ranges. But if you'r locking
>>>>> and unlocking in the same function. Is that still a concern?
>>> I don't understand the possible recursive case, but >>> amdgpu_mn_read_lock() still support recursive locking. >>>> Well the real problem is that unlocking them here won't work.
>>>>
>>>> We need to hold the lock until we are sure that the operation which
>>>> updates the page tables is completed.
>>>>
>>> The reason for this change is because hmm mirror has >>> invalidate_start callback, no invalidate_end callback >>>
>>> Check mmu_notifier.c and hmm.c again, below is entire logic to
>>> update CPU page tables and callback:
>>>
>>> mn lock amn->lock is used to protect interval tree access because
>>> user may submit/register new userptr anytime. >>> This is same for old and new way.
>>>
>>> step 2 guarantee the GPU operation is done before updating CPU page
>>> table.
>>>
>>> So I think the change is safe. We don't need hold mn lock until the
>>> CPU page tables update is completed.
>> No, that isn't even remotely correct. The lock doesn't protects the
>> interval tree.
>>
>>> Old:
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. update CPU page tables
>>>     4. up_read(&amn->lock)
>>>
>>> New, switch step 3 and 4
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. up_read(&amn->lock)
>>>     4. update CPU page tables
>> The lock is there to make sure that we serialize page table updates
>> with command submission.
> As I understand it, the idea is to prevent command submission (adding
> new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

> But do we really need another lock for this? Wouldn't the
> re-validation of userptr BOs (currently calling get_user_pages) force
> synchronization with the ongoing page table invalidation through the
> mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected] <mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): bf806477-06b3-61de-fea3-5ad260d92cdd-5C7GfCeVMHo@public.gmane.org

#54 lore raw
Quoted 1 line

Is my understanding correct?

Yes, of hand that sounds correct to me.

The other occasions should just be early bail out to optimize things under memory pressure.

Christian.

Am 03.10.2018 um 22:31 schrieb Philip Yang:

Quoted 578 lines

Hi Christian,

Yes, I agree. I am working on patch 2 to replace get_user_page with HMM. One problem is in current gfx path, we check if mmu_invalidation multiple times in amdgpu_cs_ioctl() path after get_user_page(), amdgpu_cs_parser_bos(), amdgpu_cs_list_validate(), and amdgpu_cs_submit(). For HMM, hmm_vma_range_done() has to be called once and only once after hmm_vma_get_pfns()/hmm_vma_fault(), so I will call hmm_vma_range_done() inside amdgpu_cs_submit after holding the mn lock. Is my understanding correct?

Philip

Quoted 6 lines · Christian König

Checking more code and documentation and thinking about it over my vacation I think I have some new conclusions here.

Currently we are using get_user_pages() together with an MMU notifier to guarantee coherent address space view, because get_user_pages() works by grabbing a reference to the pages and ignoring concurrent page table updates.

But HMM uses a different approach by checking the address space for modifications using hmm_vma_range_done() and re-trying when the address space has changed.

Quoted 562 lines

Now what you are trying to do is to change that into get_user_pages() and HMM callback and this is what won't work. We can either use get_user_pages() with MMU notifier or we can use HMM for the work, but we can't mix and match.

So my initial guess was correct that we just need to change both sides of the implementation at the same time.

Regards,
Christian.

Am 28.09.2018 um 17:13 schrieb Koenig, Christian:

Quoted 553 lines

No it definitely isn't.

We have literally worked month on this with the core MM developers.

Making sure that we have a consistent page array is absolutely vital for correct operation.

Please also check Jerome's presentation from XDC it also perfectly explains why this approach won't work correctly.

Christian.

Am 28.09.2018 17:07 schrieb "Yang, Philip" <[email protected]>:
For B path, we take mm->mmap_sem, then call hmm_vma_get_pfns() or get_user_pages(). This is obvious.

For A path, mmu notifier mmu_notifier_invalidate_range_start()/mmu_notifier_invalidate_range_end() is called in many places, and the calling path is quit complicated inside mm, it's not obvious. I checked many of the them, for example:

do_munmap()
  down_write(&mm->mmap_sem)
  arch_unmap()
    mpx_notify_unmap()...
       zap_bt_entries_mapping()
         zap_page_range()
 up_write(&mm->mmap_sem)

void zap_page_range(struct vm_area_struct *vma, unsigned long start, unsigned long size)
{
    struct mm_struct *mm = vma->vm_mm;
    struct mmu_gather tlb;
    unsigned long end = start + size;

    lru_add_drain();
    tlb_gather_mmu(&tlb, mm, start, end); update_hiwater_rss(mm); mmu_notifier_invalidate_range_start(mm, start, end); for ( ; vma && vma->vm_start < end; vma = vma->vm_next) unmap_single_vma(&tlb, vma, start, end, NULL); mmu_notifier_invalidate_range_end(mm, start, end); tlb_finish_mmu(&tlb, start, end);
}

So AFAIK it's okay without invalidate_range_end() callback.

Regards,
Philip

Quoted 41 lines · Christian

No, that is incorrect as well :)

The mmap_sem isn't necessary taken during page table updates.

What you could do is replace get_user_pages() directly with HMM. If I'm not completely mistaken that should work as expected.

Christian.

Am 27.09.2018 22:18 schrieb "Yang, Philip" <[email protected]>:
I was trying to understand the way how HMM handle this concurrent issue and how we handle it in amdgpu_ttm_tt_userptr_needs_pages() and  amdgpu_ttm_tt_affect_userptr(). HMM uses range->valid flag, we use gtt->mmu_invalidations and gtt->last_set_pages. Both use the same lock plus flag idea actually.

Thanks for the information, now I understand fence ttm_eu_fence_buffer_objects() put to BOs will block CPU page table update. This is another side of this concurrent issue I didn't know.

I had same worry that it has issue without invalidate_range_end() callback as the calling sequence Felix lists. Now I think it's fine after taking a look again today because of mm->mmap_sem usage, this is my understanding:

A path:

down_write(&mm->mmap_sem);
mmu_notifier_invalidate_range_start()
    take_lock()
    release_lock()
CPU page table update
mmu_notifier_invalidate_range_end()
up_write(&mm->mmap_sem);

B path:

again:
down_read(&mm->mmap_sem);
hmm_vma_get_pfns()
up_read(&mm->mmap_sem);
....
....
take_lock()
if (!hmm_vma_range_done()) {
   release_lock()
   goto again
}
submit command job...
release_lock()

Quoted 459 lines

If you agree, I will submit patch v5 with some minor changes, and submit another patch to replace get_user_page() with HMM.

Regards,
Philip

Quoted 452 lines · Christian König

Yeah, I've read that as well.

My best guess is that we just need to add a call to hmm_vma_range_done() after taking the lock and also replace get_user_pages() with hmm_vma_get_pfns().

But I'm still not 100% sure how all of that is supposed to work together.

Regards,
Christian.

Am 27.09.2018 um 16:50 schrieb Kuehling, Felix:

Quoted 441 lines

I think the answer is here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/vm/hmm.rst#n216

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 10:30 AM
*To:* Kuehling, Felix <[email protected]>
*Cc:* [email protected]; Yang, Philip <[email protected]>; [email protected] *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

At least with get_user_pages() that is perfectly possible.

For HMM it could be that this is prevented somehow.

Christian.

Am 27.09.2018 16:27 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 1 line

In this case you can end up accessing pages which are invalidated while

get_user_pages is in process.

What’s the sequence of events you have in mind? Something like this?

* Page table is updated and triggers MMU notifier
* amdgpu MMU notifier runs and waits for pending CS to finish while holding the read lock
* New CS starts just after invalidate_range_start MMU notifier finishes but before the page table update is done
* get_user_pages returns outdated physical addresses

I hope that’s not actually possible and that get_user_pages or hmm_vma_fault would block until the page table update is done. That is, invalidate_range_start marks the start of a page table update, and while that update is in progress, get_user_pages or hmm_vma_fault block. Jerome, can you comment on that?

Thanks,
  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:59 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Yeah I understand that, but again that won't work.

In this case you can end up accessing pages which are invalidated while get_user_pages is in process.

Christian.

Am 27.09.2018 15:41 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

Quoted 1 line

I’m not planning to change that. I don’t think there is any need to

change it.

Quoted 2 lines

Yeah, but when HMM doesn't provide both the start and the end

hock of the invalidation this way won't work any more.

Quoted 3 lines

So we need to find a solution for this,
Christian.

My whole argument is that you don’t need to hold the read lock until the invalidate_range_end. Just read_lock and read_unlock in the invalidate_range_start function.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 9:22 AM
*To:* Kuehling, Felix <[email protected] <mailto:[email protected]>>
*Cc:* Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to replace mmu notifier v4

Am 27.09.2018 um 15:18 schrieb Kuehling, Felix:

Quoted 4 lines

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated,

p->fence);

> amdgpu_mn_unlock(p->mn);

>
> We need to hold the lock until the fence is added to the reservation object.
>
> Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

I’m not planning to change that. I don’t think there is any need to change it.

Yeah, but when HMM doesn't provide both the start and the end hock of the invalidation this way won't work any more.

So we need to find a solution for this,
Christian.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 7:24 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror callback to
replace mmu notifier v4

Am 27.09.2018 um 13:08 schrieb Kuehling, Felix:

> We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update. >
> The reason why we need to do this is here:
>

> ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
> amdgpu_mn_unlock(p->mn);

>
> Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

I don’t see why this requires holding the read-lock until invalidate_range_end. amdgpu_ttm_tt_affect_userptr gets called while the mn read-lock is held in invalidate_range_start notifier.

That's not related to amdgpu_ttm_tt_affect_userptr(), this function could actually be called outside the lock.

The problem is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

amdgpu_mn_unlock(p->mn);

We need to hold the lock until the fence is added to the reservation object.

Otherwise somebody could have changed the page tables just in the moment between the check of amdgpu_ttm_tt_userptr_needs_pages() and adding the fence to the reservation object.

Regards,
Christian.

Regards,

  Felix

*From:*Koenig, Christian
*Sent:* Thursday, September 27, 2018 5:27 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu: use HMM mirror
callback to replace mmu notifier v4

That is correct, but take a look what we do when after calling the amdgpu_mn_read_lock():

        /* No memory allocation is allowed while holding the mn lock */ amdgpu_mn_lock(p->mn); amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) { struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);

                if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) { r = -ERESTARTSYS; goto error_abort; }
        }

We double check that there wasn't any page table modification while we prepared the submission and restart the whole process when there actually was some update.

The reason why we need to do this is here:

ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence); amdgpu_mn_unlock(p->mn);

Only after the new fence is added to the buffer object we can release the lock so that any invalidation will now block on our command submission to finish before it modifies the page table.

The only other option would be to add the fence first and then check if there was any update to the page tables.

The issue with that approach is that adding a fence can't be made undone, so if we find that there actually was an update to the page tables we would need to somehow turn the CS into a dummy (e.g. overwrite all IBs with NOPs or something like that) and still submit it.

Not sure if that is actually possible.

Regards,
Christian.

Am 27.09.2018 um 10:47 schrieb Kuehling, Felix:

So back to my previous question:

>> But do we really need another lock for this? Wouldn't the

>> re-validation of userptr BOs (currently calling get_user_pages) force

>> synchronization with the ongoing page table invalidation through the

>> mmap_sem or other MM locks?

>

> No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

>

> Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

The existing amdgpu_mn_lock/unlock should block the MMU notifier while a command submission is in progress. It should also block command submission while an MMU notifier is in progress.

What we lose with HMM is the ability to hold a read-lock for the entire duration of the invalidate_range_start until invalidate_range_end. As I understand it, that lock is meant to prevent new command submissions while the page tables are being updated by the kernel. But my point is, that get_user_pages or hmm_vma_fault should do the same kind of thing. Before the command submission can go ahead, it needs to update the userptr addresses. If the page tables are still being updated, it will block there even without holding the amdgpu_mn_read_lock.

Regards,

  Felix

*From:* Koenig, Christian
*Sent:* Thursday, September 27, 2018 3:00 AM
*To:* Kuehling, Felix <[email protected]> <mailto:[email protected]>
*Cc:* Yang, Philip <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected]> <mailto:[email protected]> *Subject:* RE: [PATCH] drm/amdgpu: use HMM mirror
callback to replace mmu notifier v4

No, that won't work. We would still run into lock inversion problems.

What we could do with the scheduler is to turn submissions into dummies if we find that the page tables are now outdated.

But that would be really hacky and I'm not sure if that would really work in all cases.

Christian.

Am 27.09.2018 08:53 schrieb "Kuehling, Felix" <[email protected] <mailto:[email protected]>>:

I had a chat with Jerome yesterday. He pointed out that the new blockable parameter can be used to infer whether the MMU notifier is being called  in a reclaim operation. So if blockable==true, it should even be safe to take the BO reservation lock without problems. I think with that we should be able to remove the read-write locking completely and go back to locking (or try-locking for blockable==false) the reservation locks in the MMU notifier?

Regards,
  Felix

-----Original Message-----
From: amd-gfx <amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org <mailto:amd-gfx-bounces-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org>> On
Behalf Of Christian König
Sent: Saturday, September 15, 2018 3:47 AM
To: Kuehling, Felix <[email protected] <mailto:[email protected]>>; Yang, Philip <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>; Jerome Glisse <[email protected] <mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu: use HMM mirror
callback to replace mmu notifier v4

Am 14.09.2018 um 22:21 schrieb Felix Kuehling:
> On 2018-09-14 01:52 PM, Christian König wrote:
>> Am 14.09.2018 um 19:47 schrieb Philip Yang:
>>> On 2018-09-14 03:51 AM, Christian König wrote:
>>>> Am 13.09.2018 um 23:51 schrieb Felix Kuehling:
>>>>> On 2018-09-13 04:52 PM, Philip Yang wrote:
>>>>> [SNIP]
>>>>>>    + amdgpu_mn_read_unlock(amn);
>>>>>> +
>>>>> amdgpu_mn_read_lock/unlock support recursive locking for multiple
>>>>> overlapping or nested invalidation ranges. But if you'r locking
>>>>> and unlocking in the same function. Is that still a concern?
>>> I don't understand the possible recursive case, but >>> amdgpu_mn_read_lock() still support recursive locking.
>>>> Well the real problem is that unlocking them here won't work.
>>>>
>>>> We need to hold the lock until we are sure that the operation which
>>>> updates the page tables is completed. >>>>
>>> The reason for this change is because hmm mirror has >>> invalidate_start callback, no invalidate_end callback >>>
>>> Check mmu_notifier.c and hmm.c again, below is entire logic to
>>> update CPU page tables and callback:
>>>
>>> mn lock amn->lock is used to protect interval tree access because
>>> user may submit/register new userptr anytime. >>> This is same for old and new way.
>>>
>>> step 2 guarantee the GPU operation is done before updating CPU page
>>> table.
>>>
>>> So I think the change is safe. We don't need hold mn lock until the
>>> CPU page tables update is completed.
>> No, that isn't even remotely correct. The lock doesn't protects the
>> interval tree.
>>
>>> Old:
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. update CPU page tables
>>>     4. up_read(&amn->lock)
>>>
>>> New, switch step 3 and 4
>>>     1. down_read_non_owner(&amn->lock)
>>>     2. loop to handle BOs from node->bos through interval tree
>>> amn->object nodes
>>>         gfx: wait for pending BOs fence operation done, mark user
>>> pages dirty
>>>         kfd: evict user queues of the process, wait for queue
>>> unmap/map operation done
>>>     3. up_read(&amn->lock)
>>>     4. update CPU page tables
>> The lock is there to make sure that we serialize page table updates
>> with command submission.
> As I understand it, the idea is to prevent command submission (adding
> new fences to BOs) while a page table invalidation is in progress.

Yes, exactly.

> But do we really need another lock for this? Wouldn't the
> re-validation of userptr BOs (currently calling get_user_pages) force
> synchronization with the ongoing page table invalidation through the
> mmap_sem or other MM locks?

No and yes. We don't hold any other locks while doing command submission, but I expect that HMM has its own mechanism to prevent that.

Since we don't modify amdgpu_mn_lock()/amdgpu_mn_unlock() we are certainly not using this mechanism correctly.

Regards,
Christian.
_______________________________________________
amd-gfx mailing list [email protected] <mailto:[email protected]>
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Quoted 4 lines

_______________________________________________
amd-gfx mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx