Skip to contentSkip to conversation

Basic system allocator support in xe driver

PATCH In review Oak Zeng 3 participants 72 messages Apr 9, 2024 – Jun 7, 2024 Page 2 of 2

Subsystems (4)
lore
Share & tools
lore
author
#63 lore raw

PATCH v2 29/31 drm/xe/svm: trace svm migration

In reply to Oak Zeng

Add two trace points to trace svm migrations.

---
 drivers/gpu/drm/xe/xe_svm_migrate.c |  5 ++++-
 drivers/gpu/drm/xe/xe_trace.h       | 11 +++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_svm_migrate.c +4 -1
index ab8dd1f58aa4..69096d81bf02 100644
@@ -211,8 +211,10 @@ vm_fault_t xe_svm_migrate_to_sram(struct vm_fault *vmf)
 	xe_svm_for_each_vm(svm, vm) {
 		xe_assert(xe, vm->mm == mm);
 		xe_vma = xe_vm_lookup_vma(vm, vmf->address);
-		if (xe_vma)
+		if (xe_vma) {
+			trace_xe_svm_migrate_to_sram(xe_vma);
 			xe_vm_invalidate_vma(xe_vma);
+		}
 	}
 	migrate_vma_pages(&migrate_vma);
 	migrate_vma_finalize(&migrate_vma);
@@ -328,6 +330,7 @@ int xe_svm_migrate_vma_to_vram(struct xe_vm *vm,
 
 	migrate_vma_pages(&migrate);
 	migrate_vma_finalize(&migrate);
+	trace_xe_svm_migrate_to_vram(vma);
 free_dst_pages:
 	if (ret)
 		xe_devm_free_blocks(&blocks);
drivers/gpu/drm/xe/xe_trace.h +11 -0
index f3fcce9f1434..12e0c9856540 100644
@@ -480,6 +480,17 @@ DEFINE_EVENT(xe_vma, xe_vma_userptr_invalidate_complete,
 	     TP_ARGS(vma)
 );
 
+DEFINE_EVENT(xe_vma, xe_svm_migrate_to_sram,
+		    TP_PROTO(struct xe_vma *vma),
+		    TP_ARGS(vma)
+);
+
+
+DEFINE_EVENT(xe_vma, xe_svm_migrate_to_vram,
+		    TP_PROTO(struct xe_vma *vma),
+		    TP_ARGS(vma)
+);
+
 DECLARE_EVENT_CLASS(xe_vm,
 		    TP_PROTO(struct xe_vm *vm),
 		    TP_ARGS(vm),
Signature · 1 line
2.26.3
author
#64 lore raw

PATCH v2 30/31 drm/xe/svm: Add a helper to determine a vma is fault userptr

In reply to Oak Zeng

xe_vma_is_fault_userptr is added to determine the vma is a fault userptr.

---
 drivers/gpu/drm/xe/xe_vm.h | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/xe/xe_vm.h +5 -0
index d55330988e32..a718f927e362 100644
@@ -166,6 +166,11 @@ static inline bool xe_vma_is_userptr(struct xe_vma *vma)
 		!xe_vma_is_system_allocator(vma);
 }
 
+static inline bool xe_vma_is_fault_userptr(struct xe_vma *vma)
+{
+	return xe_vma_is_userptr(vma) && (vma->gpuva.flags & XE_VMA_FAULT_USERPTR);
+}
+
 /**
  * to_userptr_vma() - Return a pointer to an embedding userptr vma
  * @vma: Pointer to the embedded struct xe_vma
Signature · 1 line
2.26.3
maintainer
#65 lore raw

Re: [v2 30/31] drm/xe/svm: Add a helper to determine a vma is fault userptr

Quoted 13 lines · Oak Zeng

xe_vma_is_fault_userptr is added to determine the vma is a fault userptr.

---
 drivers/gpu/drm/xe/xe_vm.h | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/xe/xe_vm.h +0 -0
index d55330988e32..a718f927e362 100644

drivers/gpu/drm/xe/xe_vm.h static inline bool xe_vma_is_userptr(struct xe_vma *vma) · line 171

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.h +3 -0
@@ -166,6 +166,11 @@ static inline bool xe_vma_is_userptr(struct xe_vma *vma)
 		!xe_vma_is_system_allocator(vma);
 }
 
+static inline bool xe_vma_is_fault_userptr(struct xe_vma *vma)
+{
+	return xe_vma_is_userptr(vma) && (vma->gpuva.flags & XE_VMA_FAULT_USERPTR);

Presumably we never set XE_VMA_FAULT_USERPTR when xe_vma_is_userptr is false probably safe to just check XE_VMA_FAULT_USERPTR here.

Matt

Quoted 8 lines
drivers/gpu/drm/xe/xe_vm.h +2 -0
+}
+
 /**
  * to_userptr_vma() - Return a pointer to an embedding userptr vma
  * @vma: Pointer to the embedded struct xe_vma
Signature · 1 line
2.26.3
author
#66 lore raw

PATCH v2 31/31 drm/xe/svm: Migration from sram to vram for system allocator

In reply to Oak Zeng

If applicable, migrate a vma from sram to vram for system allocator. Traditional userptr is not migrated. Only userptr created during fault (aka userptr splitted from system allocator vma) can be migrated.

FIXME: The migration should be conditional on user memory attributes setting. Add this logic when memory attributes are supported

---
 drivers/gpu/drm/xe/xe_gt_pagefault.c | 9 ++++++++-
 drivers/gpu/drm/xe/xe_vm.c           | 4 ----
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_gt_pagefault.c +8 -1
index 668984f0769e..c6ba00049964 100644
@@ -20,6 +20,7 @@
 #include "xe_guc_ct.h"
 #include "xe_migrate.h"
 #include "xe_trace.h"
+#include "xe_svm.h"
 #include "xe_vm.h"
 
 struct pagefault {
@@ -209,12 +210,18 @@ static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf)
 
 	if (xe_vma_is_userptr(vma) && write_locked) {
 		struct xe_userptr_vma *uvma = to_userptr_vma(vma);
+		struct xe_userptr *userptr = &uvma->userptr;
 
 		spin_lock(&vm->userptr.invalidated_lock);
-		list_del_init(&uvma->userptr.invalidate_link);
+		list_del_init(&userptr->invalidate_link);
 		spin_unlock(&vm->userptr.invalidated_lock);
 
+		mmap_read_lock(userptr->notifier.mm);
+		/**FIXME: Add migration policy here*/
+		if (xe_vma_is_fault_userptr(vma))
+			xe_svm_migrate_vma_to_vram(vm, vma, tile);
 		ret = xe_vma_userptr_pin_pages(uvma);
+		mmap_read_unlock(userptr->notifier.mm);
 		if (ret)
 			goto unlock_vm;
 
drivers/gpu/drm/xe/xe_vm.c +0 -4
index 498b36469d00..8a58fe144a02 100644
@@ -71,16 +71,12 @@ int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma)
 	struct xe_vma *vma = &uvma->vma;
 	struct xe_vm *vm = xe_vma_vm(vma);
 	struct xe_device *xe = vm->xe;
-	struct xe_userptr *userptr;
 	int ret;
 
 	lockdep_assert_held(&vm->lock);
 	xe_assert(xe, xe_vma_is_userptr(vma));
 
-	userptr = &uvma->userptr;
-	mmap_read_lock(userptr->notifier.mm);
 	ret = xe_userptr_populate_range(uvma);
-	mmap_read_unlock(userptr->notifier.mm);
 
 	return ret;
 }
Signature · 1 line
2.26.3
maintainer
#67 lore raw

Re: [v2 31/31] drm/xe/svm: Migration from sram to vram for system allocator

Quoted 31 lines · Oak Zeng

If applicable, migrate a vma from sram to vram for system allocator. Traditional userptr is not migrated. Only userptr created during fault (aka userptr splitted from system allocator vma) can be migrated.

FIXME: The migration should be conditional on user memory attributes setting. Add this logic when memory attributes are supported

---
 drivers/gpu/drm/xe/xe_gt_pagefault.c | 9 ++++++++-
 drivers/gpu/drm/xe/xe_vm.c           | 4 ----
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_gt_pagefault.c +2 -0
index 668984f0769e..c6ba00049964 100644
@@ -20,6 +20,7 @@
 #include "xe_guc_ct.h"
 #include "xe_migrate.h"
 #include "xe_trace.h"
+#include "xe_svm.h"
 #include "xe_vm.h"
 
 struct pagefault {
@@ -209,12 +210,18 @@ static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf)
 
 	if (xe_vma_is_userptr(vma) && write_locked) {
 		struct xe_userptr_vma *uvma = to_userptr_vma(vma);
+		struct xe_userptr *userptr = &uvma->userptr;

drivers/gpu/drm/xe/xe_gt_pagefault.c static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf) · line 222

Quoted 10 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +5 -1
 
 		spin_lock(&vm->userptr.invalidated_lock);
-		list_del_init(&uvma->userptr.invalidate_link);
+		list_del_init(&userptr->invalidate_link);
 		spin_unlock(&vm->userptr.invalidated_lock);
 
+		mmap_read_lock(userptr->notifier.mm);
+		/**FIXME: Add migration policy here*/
+		if (xe_vma_is_fault_userptr(vma))
+			xe_svm_migrate_vma_to_vram(vm, vma, tile);

Agree we need a policy here...

See my comments about locking in [1] thinking if we migrate we likely want to hold the mmap lock until at least the bind being issued to prevent races with the CPU fault handler, at least initially.

[1] https://patchwork.freedesktop.org/patch/588542/?series=132229&rev=1

Quoted 13 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -0
 		ret = xe_vma_userptr_pin_pages(uvma);
+		mmap_read_unlock(userptr->notifier.mm);
 		if (ret)
 			goto unlock_vm;
 
drivers/gpu/drm/xe/xe_vm.c +0 -0
index 498b36469d00..8a58fe144a02 100644
@@ -71,16 +71,12 @@ int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma)
 	struct xe_vma *vma = &uvma->vma;
 	struct xe_vm *vm = xe_vma_vm(vma);
 	struct xe_device *xe = vm->xe;

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma) · line 83

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +0 -4
-	struct xe_userptr *userptr;
 	int ret;
 
 	lockdep_assert_held(&vm->lock);
 	xe_assert(xe, xe_vma_is_userptr(vma));
 
-	userptr = &uvma->userptr;
-	mmap_read_lock(userptr->notifier.mm);
 	ret = xe_userptr_populate_range(uvma);
-	mmap_read_unlock(userptr->notifier.mm);

Now you won't have the lock here other callers of this function... Probably need to have locked / unlocked version or arguments here.

Matt

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	return ret;
 }
Signature · 1 line
2.26.3
author
#68 lore raw

RE: [v2 31/31] drm/xe/svm: Migration from sram to vram for system allocator

Hi Matt,

Quoted 58 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Wednesday, April 10, 2024 10:55 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Ghimiray, Himal Prasad <[email protected]>; Bommu, Krishnaiah <[email protected]>; [email protected]; Welty, Brian <[email protected]>
Subject: Re: [v2 31/31] drm/xe/svm: Migration from sram to vram for system allocator

Quoted 8 lines · Oak Zeng

If applicable, migrate a vma from sram to vram for system allocator. Traditional userptr is not migrated. Only userptr created during fault (aka userptr splitted from system allocator vma) can be migrated.

FIXME: The migration should be conditional on user memory attributes setting. Add this logic when memory attributes are supported

Review

Quoted 7 lines
---
 drivers/gpu/drm/xe/xe_gt_pagefault.c | 9 ++++++++-
 drivers/gpu/drm/xe/xe_vm.c           | 4 ----
 2 files changed, 8 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_gt_pagefault.c

drivers/gpu/drm/xe/xe_gt_pagefault.c static int handle_pagefault(struct xe_gt *gt,

Quoted 12 lines

index 668984f0769e..c6ba00049964 100644

drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -0
@@ -20,6 +20,7 @@
 #include "xe_guc_ct.h"
 #include "xe_migrate.h"
 #include "xe_trace.h"
+#include "xe_svm.h"
 #include "xe_vm.h"
 
 struct pagefault {
@@ -209,12 +210,18 @@ static int handle_pagefault(struct xe_gt *gt,

struct pagefault *pf)

Quoted 4 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -0
 	if (xe_vma_is_userptr(vma) && write_locked) {
 		struct xe_userptr_vma *uvma = to_userptr_vma(vma);
+		struct xe_userptr *userptr = &uvma->userptr;

drivers/gpu/drm/xe/xe_gt_pagefault.c static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf) · line 222

Quoted 10 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +5 -1
 
 		spin_lock(&vm->userptr.invalidated_lock);
-		list_del_init(&uvma->userptr.invalidate_link);
+		list_del_init(&userptr->invalidate_link);
 		spin_unlock(&vm->userptr.invalidated_lock);
 
+		mmap_read_lock(userptr->notifier.mm);
+		/**FIXME: Add migration policy here*/
+		if (xe_vma_is_fault_userptr(vma))
+			xe_svm_migrate_vma_to_vram(vm, vma, tile);

Agree we need a policy here...

Quoted 3 lines

See my comments about locking in [1] thinking if we migrate we likely want to hold the mmap lock until at least the bind being issued to prevent races with the CPU fault handler, at least initially.

As explained in [1], I will continue to use mmap read lock for now. And the read lock only covers migration and userptr-pin-pages but not the bind. The bind correctness is guaranteed by a retry mechanism and a userptr's notifier_lock.

Quoted 23 lines

[1] https://patchwork.freedesktop.org/patch/588542/?series=132229&rev=1

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_pin_pages(struct

Quoted 10 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -0
 		ret = xe_vma_userptr_pin_pages(uvma);
+		mmap_read_unlock(userptr->notifier.mm);
 		if (ret)
 			goto unlock_vm;
 
drivers/gpu/drm/xe/xe_vm.c +0 -0
index 498b36469d00..8a58fe144a02 100644
@@ -71,16 +71,12 @@ int xe_vma_userptr_pin_pages(struct

xe_userptr_vma *uvma)

Quoted 9 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	struct xe_vma *vma = &uvma->vma;
 	struct xe_vm *vm = xe_vma_vm(vma);
 	struct xe_device *xe = vm->xe;
-	struct xe_userptr *userptr;
 	int ret;
 
 	lockdep_assert_held(&vm->lock);
 	xe_assert(xe, xe_vma_is_userptr(vma));
 
Quoted 7 lines

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma) · line 83

Quoted 4 lines
drivers/gpu/drm/xe/xe_vm.c +0 -3
-	userptr = &uvma->userptr;
-	mmap_read_lock(userptr->notifier.mm);
 	ret = xe_userptr_populate_range(uvma);
-	mmap_read_unlock(userptr->notifier.mm);

Now you won't have the lock here other callers of this function... Probably need to have locked / unlocked version or arguments here.

This is addressed in v3.

Oak

Quoted 9 lines

Matt

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	return ret;
 }
--

2.26.3

maintainer
#69 lore raw

Re: [v2 31/31] drm/xe/svm: Migration from sram to vram for system allocator

Quoted 60 lines · Oak

Hi Matt,

Quoted 58 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Wednesday, April 10, 2024 10:55 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Ghimiray, Himal Prasad <[email protected]>; Bommu, Krishnaiah <[email protected]>; [email protected]; Welty, Brian <[email protected]>
Subject: Re: [v2 31/31] drm/xe/svm: Migration from sram to vram for system allocator

Quoted 8 lines · Oak Zeng

If applicable, migrate a vma from sram to vram for system allocator. Traditional userptr is not migrated. Only userptr created during fault (aka userptr splitted from system allocator vma) can be migrated.

FIXME: The migration should be conditional on user memory attributes setting. Add this logic when memory attributes are supported

Review

Quoted 7 lines
---
 drivers/gpu/drm/xe/xe_gt_pagefault.c | 9 ++++++++-
 drivers/gpu/drm/xe/xe_vm.c           | 4 ----
 2 files changed, 8 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_gt_pagefault.c

drivers/gpu/drm/xe/xe_gt_pagefault.c static int handle_pagefault(struct xe_gt *gt,

Quoted 12 lines

index 668984f0769e..c6ba00049964 100644

drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -0
@@ -20,6 +20,7 @@
 #include "xe_guc_ct.h"
 #include "xe_migrate.h"
 #include "xe_trace.h"
+#include "xe_svm.h"
 #include "xe_vm.h"
 
 struct pagefault {
@@ -209,12 +210,18 @@ static int handle_pagefault(struct xe_gt *gt,

struct pagefault *pf)

Quoted 4 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -0
 	if (xe_vma_is_userptr(vma) && write_locked) {
 		struct xe_userptr_vma *uvma = to_userptr_vma(vma);
+		struct xe_userptr *userptr = &uvma->userptr;

drivers/gpu/drm/xe/xe_gt_pagefault.c static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf) · line 222

Quoted 10 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +5 -1
 
 		spin_lock(&vm->userptr.invalidated_lock);
-		list_del_init(&uvma->userptr.invalidate_link);
+		list_del_init(&userptr->invalidate_link);
 		spin_unlock(&vm->userptr.invalidated_lock);
 
+		mmap_read_lock(userptr->notifier.mm);
+		/**FIXME: Add migration policy here*/
+		if (xe_vma_is_fault_userptr(vma))
+			xe_svm_migrate_vma_to_vram(vm, vma, tile);

Agree we need a policy here...

Quoted 6 lines
Quoted 3 lines

See my comments about locking in [1] thinking if we migrate we likely want to hold the mmap lock until at least the bind being issued to prevent races with the CPU fault handler, at least initially.

As explained in [1], I will continue to use mmap read lock for now. And the read lock only covers migration and userptr-pin-pages but not the bind. The bind correctness is guaranteed by a retry mechanism and a userptr's notifier_lock.

See my reply, I think to avoid races VRAM migration and grabbing VRAM pages via hmm_range_fault either need the write lock or a some core refactoring needs to be done to avoid races.

One more possibly wrt to these races, maybe we simply don't care and racing access between the CPU and GPU of shared memory and this is akin to a user fault (i.e. the user must synchronize access or the behavior is undefined possibly resulting in a segfault). If I recall correctly, my tests showed when these races occured either a memory corruption happened or the user got a segfault.

FWIW, Cuda seems to have synchronize calls in their SVM documentation (e.g. pass a malloc address to a shader, execute shader, call synchronize, then access malloc address in user program). I can dig up this documentation if you like.

Agree binds certainly do not need the mmap lock.

Matt

Quoted 44 lines
Quoted 23 lines

[1] https://patchwork.freedesktop.org/patch/588542/?series=132229&rev=1

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_pin_pages(struct

Quoted 10 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -0
 		ret = xe_vma_userptr_pin_pages(uvma);
+		mmap_read_unlock(userptr->notifier.mm);
 		if (ret)
 			goto unlock_vm;
 
drivers/gpu/drm/xe/xe_vm.c +0 -0
index 498b36469d00..8a58fe144a02 100644
@@ -71,16 +71,12 @@ int xe_vma_userptr_pin_pages(struct

xe_userptr_vma *uvma)

Quoted 9 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	struct xe_vma *vma = &uvma->vma;
 	struct xe_vm *vm = xe_vma_vm(vma);
 	struct xe_device *xe = vm->xe;
-	struct xe_userptr *userptr;
 	int ret;
 
 	lockdep_assert_held(&vm->lock);
 	xe_assert(xe, xe_vma_is_userptr(vma));
 
Quoted 7 lines

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma) · line 83

Quoted 4 lines
drivers/gpu/drm/xe/xe_vm.c +0 -3
-	userptr = &uvma->userptr;
-	mmap_read_lock(userptr->notifier.mm);
 	ret = xe_userptr_populate_range(uvma);
-	mmap_read_unlock(userptr->notifier.mm);

Now you won't have the lock here other callers of this function... Probably need to have locked / unlocked version or arguments here.

This is addressed in v3.

Oak

Quoted 9 lines

Matt

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	return ret;
 }
--

2.26.3

maintainer
#70 lore raw

Re: [v2 31/31] drm/xe/svm: Migration from sram to vram for system allocator

Quoted 79 lines · Matthew Brost
Quoted 60 lines · Oak

Hi Matt,

Quoted 58 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Wednesday, April 10, 2024 10:55 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Ghimiray, Himal Prasad <[email protected]>; Bommu, Krishnaiah <[email protected]>; [email protected]; Welty, Brian <[email protected]>
Subject: Re: [v2 31/31] drm/xe/svm: Migration from sram to vram for system allocator

Quoted 8 lines · Oak Zeng

If applicable, migrate a vma from sram to vram for system allocator. Traditional userptr is not migrated. Only userptr created during fault (aka userptr splitted from system allocator vma) can be migrated.

FIXME: The migration should be conditional on user memory attributes setting. Add this logic when memory attributes are supported

Review

Quoted 7 lines
---
 drivers/gpu/drm/xe/xe_gt_pagefault.c | 9 ++++++++-
 drivers/gpu/drm/xe/xe_vm.c           | 4 ----
 2 files changed, 8 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_gt_pagefault.c

drivers/gpu/drm/xe/xe_gt_pagefault.c static int handle_pagefault(struct xe_gt *gt,

Quoted 12 lines

index 668984f0769e..c6ba00049964 100644

drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -0
@@ -20,6 +20,7 @@
 #include "xe_guc_ct.h"
 #include "xe_migrate.h"
 #include "xe_trace.h"
+#include "xe_svm.h"
 #include "xe_vm.h"
 
 struct pagefault {
@@ -209,12 +210,18 @@ static int handle_pagefault(struct xe_gt *gt,

struct pagefault *pf)

Quoted 4 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -0
 	if (xe_vma_is_userptr(vma) && write_locked) {
 		struct xe_userptr_vma *uvma = to_userptr_vma(vma);
+		struct xe_userptr *userptr = &uvma->userptr;

drivers/gpu/drm/xe/xe_gt_pagefault.c static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf) · line 222

Quoted 10 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +5 -1
 
 		spin_lock(&vm->userptr.invalidated_lock);
-		list_del_init(&uvma->userptr.invalidate_link);
+		list_del_init(&userptr->invalidate_link);
 		spin_unlock(&vm->userptr.invalidated_lock);
 
+		mmap_read_lock(userptr->notifier.mm);
+		/**FIXME: Add migration policy here*/
+		if (xe_vma_is_fault_userptr(vma))
+			xe_svm_migrate_vma_to_vram(vm, vma, tile);

Agree we need a policy here...

Quoted 6 lines
Quoted 3 lines

See my comments about locking in [1] thinking if we migrate we likely want to hold the mmap lock until at least the bind being issued to prevent races with the CPU fault handler, at least initially.

As explained in [1], I will continue to use mmap read lock for now. And the read lock only covers migration and userptr-pin-pages but not the bind. The bind correctness is guaranteed by a retry mechanism and a userptr's notifier_lock.

See my reply, I think to avoid races VRAM migration and grabbing VRAM pages via hmm_range_fault either need the write lock or a some core refactoring needs to be done to avoid races.

One more possibly wrt to these races, maybe we simply don't care and racing access between the CPU and GPU of shared memory and this is akin to a user fault (i.e. the user must synchronize access or the behavior is undefined possibly resulting in a segfault). If I recall correctly, my tests showed when these races occured either a memory corruption happened or the user got a segfault.

Quoted 5 lines

FWIW, Cuda seems to have synchronize calls in their SVM documentation (e.g. pass a malloc address to a shader, execute shader, call synchronize, then access malloc address in user program). I can dig up this documentation if you like.

Sorry one more comment again. I haven't looked at the level0 spec for SVM, we should also look at that and perhap spec it out as CPU and GPU access of SVM memory is mutually exclusive and concurent access is undefined.

Also missed a comment below.

Quoted 37 lines

Agree binds certainly do not need the mmap lock.

Matt

Quoted 33 lines
Quoted 23 lines

[1] https://patchwork.freedesktop.org/patch/588542/?series=132229&rev=1

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_pin_pages(struct

Quoted 10 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -0
 		ret = xe_vma_userptr_pin_pages(uvma);
+		mmap_read_unlock(userptr->notifier.mm);
 		if (ret)
 			goto unlock_vm;
 
drivers/gpu/drm/xe/xe_vm.c +0 -0
index 498b36469d00..8a58fe144a02 100644
@@ -71,16 +71,12 @@ int xe_vma_userptr_pin_pages(struct

xe_userptr_vma *uvma)

Quoted 9 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	struct xe_vma *vma = &uvma->vma;
 	struct xe_vm *vm = xe_vma_vm(vma);
 	struct xe_device *xe = vm->xe;
-	struct xe_userptr *userptr;
 	int ret;
 
 	lockdep_assert_held(&vm->lock);
 	xe_assert(xe, xe_vma_is_userptr(vma));
 
Quoted 7 lines

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma) · line 83

Quoted 4 lines
drivers/gpu/drm/xe/xe_vm.c +0 -3
-	userptr = &uvma->userptr;
-	mmap_read_lock(userptr->notifier.mm);
 	ret = xe_userptr_populate_range(uvma);
-	mmap_read_unlock(userptr->notifier.mm);

Now you won't have the lock here other callers of this function... Probably need to have locked / unlocked version or arguments here.

This is addressed in v3.

Misses this. Also to be clear, in you design Xe shouldn't touch the MMAP lock either, that lock should be owned by the DRM layer.

Matt

Quoted 11 lines
Quoted 11 lines

Oak

Quoted 9 lines

Matt

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	return ret;
 }
--

2.26.3

#71 lore raw

✗ CI.Patch_applied: failure for Basic system allocator support in xe driver

In reply to Oak Zeng
== Series Details ==

== Series Details ==

Series: Basic system allocator support in xe driver
URL : https://patchwork.freedesktop.org/series/132229/
State : failure

== Summary ==

=== Applying kernel patches on branch 'drm-tip' with base: ===
Base commit: 7be27f645de2 drm-tip: 2024y-04m-09d-17h-53m-12s UTC integration manifest === git am output follows ===
error: patch failed: drivers/gpu/drm/xe/xe_bo.h:10
error: drivers/gpu/drm/xe/xe_bo.h: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_device.c:226
error: drivers/gpu/drm/xe/xe_device.c: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_exec.c:135
error: drivers/gpu/drm/xe/xe_exec.c: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_exec_queue_types.h:78
error: drivers/gpu/drm/xe/xe_exec_queue_types.h: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_pci.c:375
error: drivers/gpu/drm/xe/xe_pci.c: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_pt.c:1161
error: drivers/gpu/drm/xe/xe_pt.c: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_sched_job_types.h:39
error: drivers/gpu/drm/xe/xe_sched_job_types.h: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_trace.h:264
error: drivers/gpu/drm/xe/xe_trace.h: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_uc_fw.c:105
error: drivers/gpu/drm/xe/xe_uc_fw.c: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_vm.c:515
error: drivers/gpu/drm/xe/xe_vm.c: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_vm.h:207
error: drivers/gpu/drm/xe/xe_vm.h: patch does not apply
error: patch failed: drivers/gpu/drm/xe/xe_vm_types.h:180
error: drivers/gpu/drm/xe/xe_vm_types.h: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Applying: drm/xe: Refactor vm_bind
Patch failed at 0001 drm/xe: Refactor vm_bind
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".