Skip to contentSkip to conversation

drm/amdgpu: Update PDEs flush TLB if PTB/PDB moved

PATCH In review Philip Yang 4 participants 8 messages Jun 2, 2022

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

PATCH v2 1/1 drm/amdgpu: Update PDEs flush TLB if PTB/PDB moved

Flush TLBs when existing PDEs are updated because a PTB or PDB moved, but avoids unnecessary TLB flushes when new PDBs or PTBs are added to the page table, which commonly happens when memory is mapped for the first time.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6 -2
index 9596c22fded6..1ea204218903 100644
@@ -737,6 +737,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
 {
 	struct amdgpu_vm_update_params params;
 	struct amdgpu_vm_bo_base *entry;
+	bool flush_tlb_needed = false;
 	int r, idx;
 
 	if (list_empty(&vm->relocated))
@@ -755,6 +756,9 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
 		goto error;
 
 	list_for_each_entry(entry, &vm->relocated, vm_status) {
+		/* vm_flush_needed after updating moved PDEs */
+		flush_tlb_needed |= entry->moved;
+
 		r = amdgpu_vm_pde_update(&params, entry);
 		if (r)
 			goto error;
@@ -764,8 +768,8 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
 	if (r)
 		goto error;
 
-	/* vm_flush_needed after updating PDEs */
-	atomic64_inc(&vm->tlb_seq);
+	if (flush_tlb_needed)
+		atomic64_inc(&vm->tlb_seq);
 
 	while (!list_empty(&vm->relocated)) {
 		entry = list_first_entry(&vm->relocated,
Signature · 1 line
2.35.1
maintainer Reviewed-by on v2 1/1
#1 lore raw

Am 02.06.22 um 15:20 schrieb Philip Yang:

Quoted 7 lines

Flush TLBs when existing PDEs are updated because a PTB or PDB moved, but avoids unnecessary TLB flushes when new PDBs or PTBs are added to the page table, which commonly happens when memory is mapped for the first time.

Quoted 37 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6 -2
index 9596c22fded6..1ea204218903 100644
@@ -737,6 +737,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
  {
  	struct amdgpu_vm_update_params params;
  	struct amdgpu_vm_bo_base *entry;
+	bool flush_tlb_needed = false;
  	int r, idx;
 
  	if (list_empty(&vm->relocated))
@@ -755,6 +756,9 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
  		goto error;
 
  	list_for_each_entry(entry, &vm->relocated, vm_status) {
+		/* vm_flush_needed after updating moved PDEs */
+		flush_tlb_needed |= entry->moved;
+
  		r = amdgpu_vm_pde_update(&params, entry);
  		if (r)
  			goto error;
@@ -764,8 +768,8 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
  	if (r)
  		goto error;
 
-	/* vm_flush_needed after updating PDEs */
-	atomic64_inc(&vm->tlb_seq);
+	if (flush_tlb_needed)
+		atomic64_inc(&vm->tlb_seq);
 
  	while (!list_empty(&vm->relocated)) {
  		entry = list_first_entry(&vm->relocated,
#2 lore raw
In reply to Philip Yang
Quoted 24 lines · Philip Yang

Flush TLBs when existing PDEs are updated because a PTB or PDB moved, but avoids unnecessary TLB flushes when new PDBs or PTBs are added to the page table, which commonly happens when memory is mapped for the first time.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
index 9596c22fded6..1ea204218903 100644
@@ -737,6 +737,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
  {
  	struct amdgpu_vm_update_params params;
  	struct amdgpu_vm_bo_base *entry;
+	bool flush_tlb_needed = false;
  	int r, idx;
 
  	if (list_empty(&vm->relocated))

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_pdes(struct amdgpu_device *adev, · line 760

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
@@ -755,6 +756,9 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
  		goto error;
 
  	list_for_each_entry(entry, &vm->relocated, vm_status) {
+		/* vm_flush_needed after updating moved PDEs */
+		flush_tlb_needed |= entry->moved;

That is a strange thing to do for a bool variable. Why not just assign it?

Thanks,
Lijo

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
+
  		r = amdgpu_vm_pde_update(&params, entry);
  		if (r)
  			goto error;
@@ -764,8 +768,8 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
  	if (r)
  		goto error;
 
-	/* vm_flush_needed after updating PDEs */
-	atomic64_inc(&vm->tlb_seq);
+	if (flush_tlb_needed)
+		atomic64_inc(&vm->tlb_seq);
 
  	while (!list_empty(&vm->relocated)) {
  		entry = list_first_entry(&vm->relocated,
#3 lore raw
Quoted 24 lines · Lijo
Quoted 21 lines · Philip Yang

Flush TLBs when existing PDEs are updated because a PTB or PDB moved, but avoids unnecessary TLB flushes when new PDBs or PTBs are added to the page table, which commonly happens when memory is mapped for the first time.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--

  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
index 9596c22fded6..1ea204218903 100644
@@ -737,6 +737,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
   {
       struct amdgpu_vm_update_params params;
       struct amdgpu_vm_bo_base *entry;
+    bool flush_tlb_needed = false;
       int r, idx;
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_pdes(struct amdgpu_device *adev, · line 759

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
       if (list_empty(&vm->relocated))
@@ -755,6 +756,9 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
           goto error;
       list_for_each_entry(entry, &vm->relocated, vm_status) {
+        /* vm_flush_needed after updating moved PDEs */
+        flush_tlb_needed |= entry->moved;

That is a strange thing to do for a bool variable. Why not just assign it?

Hmm.. In a loop, perhaps you meant logical OR?

Thanks,
Lijo

Quoted 17 lines

Thanks,
Lijo

Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
+
           r = amdgpu_vm_pde_update(&params, entry);
           if (r)
               goto error;
@@ -764,8 +768,8 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
       if (r)
           goto error;
-    /* vm_flush_needed after updating PDEs */
-    atomic64_inc(&vm->tlb_seq);
+    if (flush_tlb_needed)
+        atomic64_inc(&vm->tlb_seq);
       while (!list_empty(&vm->relocated)) {
           entry = list_first_entry(&vm->relocated,
 
maintainer
#4 lore raw

Am 02.06.22 um 15:26 schrieb Lazar, Lijo:

Quoted 39 lines · Lijo
Quoted 26 lines · Philip Yang
Quoted 23 lines

Flush TLBs when existing PDEs are updated because a PTB or PDB moved, but avoids unnecessary TLB flushes when new PDBs or PTBs are added to the page table, which commonly happens when memory is mapped for the first time.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--

  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
index 9596c22fded6..1ea204218903 100644
@@ -737,6 +737,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device 
 *adev,
   {
       struct amdgpu_vm_update_params params;
       struct amdgpu_vm_bo_base *entry;
+    bool flush_tlb_needed = false;
       int r, idx;
       if (list_empty(&vm->relocated))
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_pdes(struct amdgpu_device · line 760

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
@@ -755,6 +756,9 @@ int amdgpu_vm_update_pdes(struct amdgpu_device 
 *adev,
           goto error;
       list_for_each_entry(entry, &vm->relocated, vm_status) {
+        /* vm_flush_needed after updating moved PDEs */
+        flush_tlb_needed |= entry->moved;

That is a strange thing to do for a bool variable. Why not just assign it?

Quoted 1 line

Hmm.. In a loop, perhaps you meant logical OR?

Well IIRC C doesn't have a logical or assignment operator "||=", so "|=" is used instead which also gets the job done.

Christian.

Quoted 22 lines

Thanks,
Lijo

Quoted 18 lines

Thanks,
Lijo

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
+
           r = amdgpu_vm_pde_update(&params, entry);
           if (r)
               goto error;
@@ -764,8 +768,8 @@ int amdgpu_vm_update_pdes(struct amdgpu_device 
 *adev,
       if (r)
           goto error;
-    /* vm_flush_needed after updating PDEs */
-    atomic64_inc(&vm->tlb_seq);
+    if (flush_tlb_needed)
+        atomic64_inc(&vm->tlb_seq);
       while (!list_empty(&vm->relocated)) {
           entry = list_first_entry(&vm->relocated,
 
#5 lore raw
Quoted 40 lines · Christian König

Am 02.06.22 um 15:26 schrieb Lazar, Lijo:

Quoted 39 lines · Lijo
Quoted 35 lines · Philip Yang
Quoted 24 lines

Flush TLBs when existing PDEs are updated because a PTB or PDB moved, but avoids unnecessary TLB flushes when new PDBs or PTBs are added to the page table, which commonly happens when memory is mapped for the first time.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--

  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
index 9596c22fded6..1ea204218903 100644
@@ -737,6 +737,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device 
 *adev,
   {
       struct amdgpu_vm_update_params params;
       struct amdgpu_vm_bo_base *entry;
+    bool flush_tlb_needed = false;
       int r, idx;
       if (list_empty(&vm->relocated))

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_pdes(struct amdgpu_device · line 760

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
@@ -755,6 +756,9 @@ int amdgpu_vm_update_pdes(struct amdgpu_device 
 *adev,
           goto error;
       list_for_each_entry(entry, &vm->relocated, vm_status) {
+        /* vm_flush_needed after updating moved PDEs */
+        flush_tlb_needed |= entry->moved;

That is a strange thing to do for a bool variable. Why not just assign it?

Quoted 4 lines
Quoted 1 line

Hmm.. In a loop, perhaps you meant logical OR?

Well IIRC C doesn't have a logical or assignment operator "||=", so "|=" is used instead which also gets the job done.

var = var || value; also will work.

BTW, v1 of this patch was incrementing vm->tlb_seq for every entry moved. This one increments only once. So is this vm->tlb_seq required only to be a bool?

Thanks,
Lijo

Quoted 25 lines

Christian.

Quoted 22 lines

Thanks,
Lijo

Quoted 18 lines

Thanks,
Lijo

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
+
           r = amdgpu_vm_pde_update(&params, entry);
           if (r)
               goto error;
@@ -764,8 +768,8 @@ int amdgpu_vm_update_pdes(struct amdgpu_device 
 *adev,
       if (r)
           goto error;
-    /* vm_flush_needed after updating PDEs */
-    atomic64_inc(&vm->tlb_seq);
+    if (flush_tlb_needed)
+        atomic64_inc(&vm->tlb_seq);
       while (!list_empty(&vm->relocated)) {
           entry = list_first_entry(&vm->relocated,
 
maintainer
#6 lore raw

Am 02.06.22 um 15:44 schrieb Lazar, Lijo:

Quoted 43 lines · Christian König
Quoted 40 lines

Am 02.06.22 um 15:26 schrieb Lazar, Lijo:

Quoted 39 lines · Lijo
Quoted 35 lines · Philip Yang
Quoted 24 lines

Flush TLBs when existing PDEs are updated because a PTB or PDB moved, but avoids unnecessary TLB flushes when new PDBs or PTBs are added to the page table, which commonly happens when memory is mapped for the first time.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--

  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
index 9596c22fded6..1ea204218903 100644
@@ -737,6 +737,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device 
 *adev,
   {
       struct amdgpu_vm_update_params params;
       struct amdgpu_vm_bo_base *entry;
+    bool flush_tlb_needed = false;
       int r, idx;
       if (list_empty(&vm->relocated))

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_pdes(struct amdgpu_device · line 760

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
@@ -755,6 +756,9 @@ int amdgpu_vm_update_pdes(struct amdgpu_device 
 *adev,
           goto error;
       list_for_each_entry(entry, &vm->relocated, vm_status) {
+        /* vm_flush_needed after updating moved PDEs */
+        flush_tlb_needed |= entry->moved;

That is a strange thing to do for a bool variable. Why not just assign it?

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Hmm.. In a loop, perhaps you meant logical OR?

Well IIRC C doesn't have a logical or assignment operator "||=", so "|=" is used instead which also gets the job done.

var = var || value;  also will work.

BTW, v1 of this patch was incrementing vm->tlb_seq for every entry moved. This one increments only once. So is this vm->tlb_seq required only to be a bool?

No, it is required to only increment once. It's a sequence number and changing it signals that a TLB flush is necessary.

Regards,
Christian.

Quoted 29 lines

Thanks,
Lijo

Quoted 25 lines

Christian.

Quoted 22 lines

Thanks,
Lijo

Quoted 18 lines

Thanks,
Lijo

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
+
           r = amdgpu_vm_pde_update(&params, entry);
           if (r)
               goto error;
@@ -764,8 +768,8 @@ int amdgpu_vm_update_pdes(struct amdgpu_device 
 *adev,
       if (r)
           goto error;
-    /* vm_flush_needed after updating PDEs */
-    atomic64_inc(&vm->tlb_seq);
+    if (flush_tlb_needed)
+        atomic64_inc(&vm->tlb_seq);
       while (!list_empty(&vm->relocated)) {
           entry = list_first_entry(&vm->relocated,
 
Reviewed-by on v2 1/1
#7 lore raw
In reply to Philip Yang

Am 2022-06-02 um 09:20 schrieb Philip Yang:

Quoted 7 lines

Flush TLBs when existing PDEs are updated because a PTB or PDB moved, but avoids unnecessary TLB flushes when new PDBs or PTBs are added to the page table, which commonly happens when memory is mapped for the first time.

Quoted 37 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6 -2
index 9596c22fded6..1ea204218903 100644
@@ -737,6 +737,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
  {
  	struct amdgpu_vm_update_params params;
  	struct amdgpu_vm_bo_base *entry;
+	bool flush_tlb_needed = false;
  	int r, idx;
 
  	if (list_empty(&vm->relocated))
@@ -755,6 +756,9 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
  		goto error;
 
  	list_for_each_entry(entry, &vm->relocated, vm_status) {
+		/* vm_flush_needed after updating moved PDEs */
+		flush_tlb_needed |= entry->moved;
+
  		r = amdgpu_vm_pde_update(&params, entry);
  		if (r)
  			goto error;
@@ -764,8 +768,8 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
  	if (r)
  		goto error;
 
-	/* vm_flush_needed after updating PDEs */
-	atomic64_inc(&vm->tlb_seq);
+	if (flush_tlb_needed)
+		atomic64_inc(&vm->tlb_seq);
 
  	while (!list_empty(&vm->relocated)) {
  		entry = list_first_entry(&vm->relocated,