Skip to contentSkip to conversation

drm/amdgpu: increase hmm range get pages timeout

PATCH In review James Zhu 4 participants 10 messages Dec 8, 2023 – Dec 13, 2023

Subsystems (2)
lore
Share & tools
lore

James Zhu

author
#0 lore raw

PATCH 1/2 drm/amdgpu: increase hmm range get pages timeout

When application tries to allocate all system memory and cause memory to swap out. Needs more time for hmm_range_fault to validate the remaining page for allocation. To be safe, increase timeout value to 1 second for 64MB range.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +2 -2
index 081267161d40..b24eb5821fd1 100644
@@ -190,8 +190,8 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
 		pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
 			hmm_range->start, hmm_range->end);
 
-		/* Assuming 128MB takes maximum 1 second to fault page address */
-		timeout = max((hmm_range->end - hmm_range->start) >> 27, 1UL);
+		/* Assuming 64MB takes maximum 1 second to fault page address */
+		timeout = max((hmm_range->end - hmm_range->start) >> 26, 1UL);
 		timeout *= HMM_RANGE_DEFAULT_TIMEOUT;
 		timeout = jiffies + msecs_to_jiffies(timeout);
 
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/2 drm/amdgpu: make an improvement on amdgpu_hmm_range_get_pages

Needn't do schedule for each hmm_range_fault, and use cond_resched to replace schedule.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1 -1
index b24eb5821fd1..c77c4eceea46 100644
@@ -199,6 +199,7 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
 		hmm_range->notifier_seq = mmu_interval_read_begin(notifier);
 		r = hmm_range_fault(hmm_range);
 		if (unlikely(r)) {
+			cond_resched();
 			/*
 			 * FIXME: This timeout should encompass the retry from
 			 * mmu_interval_read_retry() as well.
@@ -212,7 +213,6 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
 			break;
 		hmm_range->hmm_pfns += MAX_WALK_BYTE >> PAGE_SHIFT;
 		hmm_range->start = hmm_range->end;
-		schedule();
 	} while (hmm_range->end < end);
 
 	hmm_range->start = start;
Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: make an improvement on amdgpu_hmm_range_get_pages

Am 09.12.23 um 00:01 schrieb James Zhu:

Quoted 2 lines

Needn't do schedule for each hmm_range_fault, and use cond_resched to replace schedule.

cond_resched() is usually NAKed upstream since it is a NO-OP in most situations.

IIRC there was even a patch set to completely remove it.

Christian.

Quoted 26 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1 -1
index b24eb5821fd1..c77c4eceea46 100644
@@ -199,6 +199,7 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
  		hmm_range->notifier_seq = mmu_interval_read_begin(notifier);
  		r = hmm_range_fault(hmm_range);
  		if (unlikely(r)) {
+			cond_resched();
  			/*
  			 * FIXME: This timeout should encompass the retry from
  			 * mmu_interval_read_retry() as well.
@@ -212,7 +213,6 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
  			break;
  		hmm_range->hmm_pfns += MAX_WALK_BYTE >> PAGE_SHIFT;
  		hmm_range->start = hmm_range->end;
-		schedule();
  	} while (hmm_range->end < end);
 
  	hmm_range->start = start;
#3 lore raw

Re: [PATCH 2/2] drm/amdgpu: make an improvement on amdgpu_hmm_range_get_pages

Quoted 5 lines · Christian König

Am 09.12.23 um 00:01 schrieb James Zhu:

Quoted 2 lines

Needn't do schedule for each hmm_range_fault, and use cond_resched to replace schedule.

cond_resched() is usually NAKed upstream since it is a NO-OP in most situations.

[JZ] then let me change back to schedule(); Thanks!

Quoted 33 lines

IIRC there was even a patch set to completely remove it.

Christian.

Quoted 27 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1 -0
index b24eb5821fd1..c77c4eceea46 100644
@@ -199,6 +199,7 @@ int amdgpu_hmm_range_get_pages(struct 
 mmu_interval_notifier *notifier,
           hmm_range->notifier_seq = mmu_interval_read_begin(notifier);
           r = hmm_range_fault(hmm_range);
           if (unlikely(r)) {
+            cond_resched();
               /*
                * FIXME: This timeout should encompass the retry from

               * mmu_interval_read_retry() as well.

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -1
@@ -212,7 +213,6 @@ int amdgpu_hmm_range_get_pages(struct 
 mmu_interval_notifier *notifier,
               break;
           hmm_range->hmm_pfns += MAX_WALK_BYTE >> PAGE_SHIFT;
           hmm_range->start = hmm_range->end;
-        schedule();
       } while (hmm_range->end < end);
         hmm_range->start = start;
#4 lore raw

Re: [PATCH 2/2] drm/amdgpu: make an improvement on amdgpu_hmm_range_get_pages

In reply to Christian König
Quoted 5 lines · Christian König

Am 09.12.23 um 00:01 schrieb James Zhu:

Quoted 2 lines

Needn't do schedule for each hmm_range_fault, and use cond_resched to replace schedule.

cond_resched() is usually NAKed upstream since it is a NO-OP in most situations.

That's weird, because https://docs.kernel.org/RCU/stallwarn.html specifically recommends it to resolve RCU stall warnings. I previously told James to use that instead of schedule().

Regards,
  Felix

Quoted 33 lines

IIRC there was even a patch set to completely remove it.

Christian.

Quoted 27 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1 -0
index b24eb5821fd1..c77c4eceea46 100644
@@ -199,6 +199,7 @@ int amdgpu_hmm_range_get_pages(struct 
 mmu_interval_notifier *notifier,
           hmm_range->notifier_seq = mmu_interval_read_begin(notifier);
           r = hmm_range_fault(hmm_range);
           if (unlikely(r)) {
+            cond_resched();
               /*
                * FIXME: This timeout should encompass the retry from

               * mmu_interval_read_retry() as well.

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -1
@@ -212,7 +213,6 @@ int amdgpu_hmm_range_get_pages(struct 
 mmu_interval_notifier *notifier,
               break;
           hmm_range->hmm_pfns += MAX_WALK_BYTE >> PAGE_SHIFT;
           hmm_range->start = hmm_range->end;
-        schedule();
       } while (hmm_range->end < end);
         hmm_range->start = start;
maintainer
#5 lore raw

Re: [PATCH 2/2] drm/amdgpu: make an improvement on amdgpu_hmm_range_get_pages

Am 12.12.23 um 00:43 schrieb Felix Kuehling:

Quoted 9 lines · Christian König
Quoted 5 lines

Am 09.12.23 um 00:01 schrieb James Zhu:

Quoted 2 lines

Needn't do schedule for each hmm_range_fault, and use cond_resched to replace schedule.

cond_resched() is usually NAKed upstream since it is a NO-OP in most situations.

That's weird, because https://docs.kernel.org/RCU/stallwarn.html specifically recommends it to resolve RCU stall warnings. I previously told James to use that instead of schedule().

I haven't followed the full discussion and rational about that either, could be that the documentation is outdated.

The latest RFC to remove cond_resched() and explaining the background can be found here: https://lwn.net/Articles/950581/

Regards,
Christian.

Quoted 38 lines

Regards,
  Felix

Quoted 33 lines

IIRC there was even a patch set to completely remove it.

Christian.

Quoted 27 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1 -0
index b24eb5821fd1..c77c4eceea46 100644
@@ -199,6 +199,7 @@ int amdgpu_hmm_range_get_pages(struct 
 mmu_interval_notifier *notifier,
           hmm_range->notifier_seq = mmu_interval_read_begin(notifier);
           r = hmm_range_fault(hmm_range);
           if (unlikely(r)) {
+            cond_resched();
               /*
                * FIXME: This timeout should encompass the retry from

               * mmu_interval_read_retry() as well.

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -1
@@ -212,7 +213,6 @@ int amdgpu_hmm_range_get_pages(struct 
 mmu_interval_notifier *notifier,
               break;
           hmm_range->hmm_pfns += MAX_WALK_BYTE >> PAGE_SHIFT;
           hmm_range->start = hmm_range->end;
-        schedule();
       } while (hmm_range->end < end);
         hmm_range->start = start;
author
#6 lore raw

PATCH v2 2/2 drm/amdgpu: make an improvement on amdgpu_hmm_range_get_pages

In reply to James Zhu

Only schedule when hmm_range_fault returns error.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1 -1
index b24eb5821fd1..55b65fc04b65 100644
@@ -199,6 +199,7 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
 		hmm_range->notifier_seq = mmu_interval_read_begin(notifier);
 		r = hmm_range_fault(hmm_range);
 		if (unlikely(r)) {
+			schedule();
 			/*
 			 * FIXME: This timeout should encompass the retry from
 			 * mmu_interval_read_retry() as well.
@@ -212,7 +213,6 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
 			break;
 		hmm_range->hmm_pfns += MAX_WALK_BYTE >> PAGE_SHIFT;
 		hmm_range->start = hmm_range->end;
-		schedule();
 	} while (hmm_range->end < end);
 
 	hmm_range->start = start;
Signature · 1 line
2.25.1
#7 lore raw
In reply to James Zhu

Ping ...

Quoted 25 lines · James Zhu

When application tries to allocate all system memory and cause memory to swap out. Needs more time for hmm_range_fault to validate the remaining page for allocation. To be safe, increase timeout value to 1 second for 64MB range.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +2 -2
index 081267161d40..b24eb5821fd1 100644
@@ -190,8 +190,8 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
  		pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
  			hmm_range->start, hmm_range->end);
 
-		/* Assuming 128MB takes maximum 1 second to fault page address */
-		timeout = max((hmm_range->end - hmm_range->start) >> 27, 1UL);
+		/* Assuming 64MB takes maximum 1 second to fault page address */
+		timeout = max((hmm_range->end - hmm_range->start) >> 26, 1UL);
  		timeout *= HMM_RANGE_DEFAULT_TIMEOUT;
  		timeout = jiffies + msecs_to_jiffies(timeout);
 
#8 lore raw
Quoted 9 lines · James Zhu

Ping ...

Quoted 6 lines · James Zhu

When application tries to allocate all system memory and cause memory to swap out. Needs more time for hmm_range_fault to validate the remaining page for allocation. To be safe, increase timeout value to 1 second for 64MB range.

This is not the first time we're incrementing this timeout. Eventually we should get rid of that and find a way to make this work reliably without a timeout. There can always be situations where faults take longer, and we should not fail randomly in those cases.

There are also some FIXMEs in this code that should be addressed at the same time.

That said, as a short-term fix, this patch is

Quoted 19 lines
Quoted 19 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -0
index 081267161d40..b24eb5821fd1 100644
@@ -190,8 +190,8 @@ int amdgpu_hmm_range_get_pages(struct 
 mmu_interval_notifier *notifier,
           pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
               hmm_range->start, hmm_range->end);

  -        /* Assuming 128MB takes maximum 1 second to fault page address */ -        timeout = max((hmm_range->end - hmm_range->start) >> 27, 1UL); +        /* Assuming 64MB takes maximum 1 second to fault page address */
+        timeout = max((hmm_range->end - hmm_range->start) >> 26, 1UL); timeout *= HMM_RANGE_DEFAULT_TIMEOUT;
          timeout = jiffies + msecs_to_jiffies(timeout);

#9 lore raw
Quoted 10 lines · Felix Kuehling
Quoted 8 lines · James Zhu

Ping ...

Quoted 5 lines · James Zhu

When application tries to allocate all system memory and cause memory to swap out. Needs more time for hmm_range_fault to validate the remaining page for allocation. To be safe, increase timeout value to 1 second for 64MB range.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

This is not the first time we're incrementing this timeout. Eventually we should get rid of that and find a way to make this work reliably without a timeout. There can always be situations where faults take longer, and we should not fail randomly in those cases.

There are also some FIXMEs in this code that should be addressed at the same time.

That said, as a short-term fix, this patch is

[JZ] Yes, it is just a short-term fix. the root cause is still under study,

Quoted 23 lines
Quoted 19 lines
Quoted 19 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -0
index 081267161d40..b24eb5821fd1 100644
@@ -190,8 +190,8 @@ int amdgpu_hmm_range_get_pages(struct 
 mmu_interval_notifier *notifier,
           pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
               hmm_range->start, hmm_range->end);

  -        /* Assuming 128MB takes maximum 1 second to fault page address */ -        timeout = max((hmm_range->end - hmm_range->start) >> 27, 1UL); +        /* Assuming 64MB takes maximum 1 second to fault page address */
+        timeout = max((hmm_range->end - hmm_range->start) >> 26, 1UL); timeout *= HMM_RANGE_DEFAULT_TIMEOUT;
          timeout = jiffies + msecs_to_jiffies(timeout);