Skip to contentSkip to conversation

drm/amdgpu: Fix minmax error

PATCH In review Luben Tuikov 4 participants 10 messages Nov 24, 2022 – Nov 26, 2022

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

PATCH 1/1 drm/amdgpu: Fix minmax error

Fix minmax compilation error by using min_t()/max_t(), of the assignment type.

Cc James Zhu <[email protected]>
Cc Felix Kuehling <[email protected]>
Fixes 58170a7a002ad6 ("drm/amdgpu: fix stall on CPU when allocate large system memory")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +7 -3
index 8a2e5716d8dba2..d22d14b0ef0c84 100644
@@ -191,14 +191,18 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
 	hmm_range->dev_private_owner = owner;
 
 	do {
-		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
+		hmm_range->end = min_t(typeof(hmm_range->end),
+				       hmm_range->start + MAX_WALK_BYTE,
+				       end);
 
 		pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
 			hmm_range->start, hmm_range->end);
 
 		/* Assuming 512MB takes maxmium 1 second to fault page address */
-		timeout = max((hmm_range->end - hmm_range->start) >> 29, 1ULL) *
-			HMM_RANGE_DEFAULT_TIMEOUT;
+		timeout = max_t(typeof(timeout),
+				(hmm_range->end - hmm_range->start) >> 29,
+				1ULL);
+		timeout *= HMM_RANGE_DEFAULT_TIMEOUT;
 		timeout = jiffies + msecs_to_jiffies(timeout);
 
 retry:

base-commit: d5e8f4912061ad2e577b4909556e1364e2c2018e
prerequisite-patch-id: 6024d0c36cae3e4a995a8fcf787b91f511a37486

Signature · 1 line
2.39.0.rc0
#1 lore raw
ThispatchisReviewed-by JamesZhu<[email protected]>
Quoted 39 lines · Luben Tuikov

Fix minmax compilation error by using min_t()/max_t(), of the assignment type.

Cc James Zhu<[email protected]>
Cc Felix Kuehling<[email protected]>
Fixes 58170a7a002ad6 ("drm/amdgpu: fix stall on CPU when allocate large system memory")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +7 -3
index 8a2e5716d8dba2..d22d14b0ef0c84 100644
@@ -191,14 +191,18 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
  	hmm_range->dev_private_owner = owner;
 
  	do {
-		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
+		hmm_range->end = min_t(typeof(hmm_range->end),
+				       hmm_range->start + MAX_WALK_BYTE,
+				       end);
 
  		pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
  			hmm_range->start, hmm_range->end);
 
  		/* Assuming 512MB takes maxmium 1 second to fault page address */
-		timeout = max((hmm_range->end - hmm_range->start) >> 29, 1ULL) *
-			HMM_RANGE_DEFAULT_TIMEOUT;
+		timeout = max_t(typeof(timeout),
+				(hmm_range->end - hmm_range->start) >> 29,
+				1ULL);
+		timeout *= HMM_RANGE_DEFAULT_TIMEOUT;
  		timeout = jiffies + msecs_to_jiffies(timeout);
 
  retry:

base-commit: d5e8f4912061ad2e577b4909556e1364e2c2018e
prerequisite-patch-id: 6024d0c36cae3e4a995a8fcf787b91f511a37486

maintainer
#2 lore raw
In reply to Luben Tuikov

Am 24.11.22 um 22:19 schrieb Luben Tuikov:

Quoted 15 lines

Fix minmax compilation error by using min_t()/max_t(), of the assignment type.

Cc James Zhu <[email protected]>
Cc Felix Kuehling <[email protected]>
Fixes 58170a7a002ad6 ("drm/amdgpu: fix stall on CPU when allocate large system memory")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -0
index 8a2e5716d8dba2..d22d14b0ef0c84 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 196

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +3 -1
@@ -191,14 +191,18 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
  	hmm_range->dev_private_owner = owner;
 
  	do {
-		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
+		hmm_range->end = min_t(typeof(hmm_range->end),
+				       hmm_range->start + MAX_WALK_BYTE,
+				       end);

Since end is a local variable I would strongly prefer to just have it use the correct type for it.

Otherwise we might end up using something which doesn't work on all architectures.

Regards,
Christian.

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +4 -2
  		pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
  			hmm_range->start, hmm_range->end);
 
  		/* Assuming 512MB takes maxmium 1 second to fault page address */
-		timeout = max((hmm_range->end - hmm_range->start) >> 29, 1ULL) *
-			HMM_RANGE_DEFAULT_TIMEOUT;
+		timeout = max_t(typeof(timeout),
+				(hmm_range->end - hmm_range->start) >> 29,
+				1ULL);
+		timeout *= HMM_RANGE_DEFAULT_TIMEOUT;
  		timeout = jiffies + msecs_to_jiffies(timeout);
 
  retry:
 

base-commit: d5e8f4912061ad2e577b4909556e1364e2c2018e
prerequisite-patch-id: 6024d0c36cae3e4a995a8fcf787b91f511a37486

#3 lore raw
Quoted 29 lines · Christian König

Am 24.11.22 um 22:19 schrieb Luben Tuikov:

Quoted 15 lines

Fix minmax compilation error by using min_t()/max_t(), of the assignment type.

Cc James Zhu <[email protected]>
Cc Felix Kuehling <[email protected]>
Fixes 58170a7a002ad6 ("drm/amdgpu: fix stall on CPU when allocate large system memory")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -0
index 8a2e5716d8dba2..d22d14b0ef0c84 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 196

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +3 -1
@@ -191,14 +191,18 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
  	hmm_range->dev_private_owner = owner;
 
  	do {
-		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
+		hmm_range->end = min_t(typeof(hmm_range->end),
+				       hmm_range->start + MAX_WALK_BYTE,
+				       end);

Since end is a local variable I would strongly prefer to just have it use the correct type for it.

Quoted 2 lines

Otherwise we might end up using something which doesn't work on all architectures.

They all appear to be "unsigned long". I thought, since we assign to hmm_range->end, we use that type.

Would you prefer at the top of the function to define "timeout" and "end" as,

	typeof(hmm_range->end) end, timeout;

Regards,
Luben

maintainer
#4 lore raw

Am 25.11.22 um 08:56 schrieb Luben Tuikov:

Quoted 28 lines · Christian König
Quoted 27 lines

Am 24.11.22 um 22:19 schrieb Luben Tuikov:

Quoted 15 lines

Fix minmax compilation error by using min_t()/max_t(), of the assignment type.

Cc James Zhu <[email protected]>
Cc Felix Kuehling <[email protected]>
Fixes 58170a7a002ad6 ("drm/amdgpu: fix stall on CPU when allocate large system memory")
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 10 +++++++---
   1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -0
index 8a2e5716d8dba2..d22d14b0ef0c84 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 196

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +3 -1
@@ -191,14 +191,18 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
   	hmm_range->dev_private_owner = owner;
 
   	do {
-		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
+		hmm_range->end = min_t(typeof(hmm_range->end),
+				       hmm_range->start + MAX_WALK_BYTE,
+				       end);

Since end is a local variable I would strongly prefer to just have it use the correct type for it.

Quoted 4 lines
Quoted 2 lines

Otherwise we might end up using something which doesn't work on all architectures.

They all appear to be "unsigned long". I thought, since we assign to hmm_range->end, we use that type.

Mhm, then why does the compiler complain here?

As far as I can see "unsigned long" is correct here, but if we somehow have a typecast then something is not working as expected.

Is MAX_WALK_BYTE maybe of signed type?

Quoted 3 lines

Would you prefer at the top of the function to define "timeout" and "end" as,

	typeof(hmm_range->end) end, timeout;

Well for end that might make sense, but timeout is independent of the hmm range.

Regards,
Christian.

Quoted 4 lines

Regards,
Luben

#5 lore raw
Quoted 29 lines · Christian König

Am 25.11.22 um 08:56 schrieb Luben Tuikov:

Quoted 28 lines · Christian König
Quoted 27 lines

Am 24.11.22 um 22:19 schrieb Luben Tuikov:

Quoted 15 lines

Fix minmax compilation error by using min_t()/max_t(), of the assignment type.

Cc James Zhu <[email protected]>
Cc Felix Kuehling <[email protected]>
Fixes 58170a7a002ad6 ("drm/amdgpu: fix stall on CPU when allocate large system memory")
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 10 +++++++---
   1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -0
index 8a2e5716d8dba2..d22d14b0ef0c84 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 196

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +3 -1
@@ -191,14 +191,18 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
   	hmm_range->dev_private_owner = owner;
 
   	do {
-		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
+		hmm_range->end = min_t(typeof(hmm_range->end),
+				       hmm_range->start + MAX_WALK_BYTE,
+				       end);

Since end is a local variable I would strongly prefer to just have it use the correct type for it.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Otherwise we might end up using something which doesn't work on all architectures.

They all appear to be "unsigned long". I thought, since we assign to hmm_range->end, we use that type.

Mhm, then why does the compiler complain here?

Right... so MAX_WALK_BYTE is 2^36 ULL (diabolically defined as 64ULL<<30 :-) ), and this is why the minmax check complains.

So, since the left-hand expression is unsigned long, i.e.,

	hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
is,
	unsigned long = min(unsigned long long, unsigned long);

The compiler complains.

I'd really prefer MAX_WALK_BYTE be less than or equal to ULONG_MAX, and be defined as <literal>UL. I mean, why is everything in struct hmm_range "unsigned long", but we set a high limit of 10_0000_0000h for an end, and compare it to "end" to find the smaller? If our "end" could potentially be 10_0000_0000h then shouldn't the members in struct hmm_range be unsigned long long as well?

And for the timeout, we have the (now) obvious,

	timeout = max((hmm_range->end - hmm_range->start) >> 29, 1ULL);

and I don't know why we necessarily need a "1ULL", when 1UL would do just fine, and then compilation passes for that statement. I can set this to 1UL, instead of using max_t().

Regards,
Luben

Quoted 21 lines

As far as I can see "unsigned long" is correct here, but if we somehow have a typecast then something is not working as expected.

Is MAX_WALK_BYTE maybe of signed type?

Quoted 3 lines

Would you prefer at the top of the function to define "timeout" and "end" as,

	typeof(hmm_range->end) end, timeout;

Well for end that might make sense, but timeout is independent of the hmm range.

Regards,
Christian.

Quoted 4 lines

Regards,
Luben

#6 lore raw

Am 25.11.22 um 09:33 schrieb Luben Tuikov:

Quoted 37 lines · Christian König
Quoted 28 lines

Am 25.11.22 um 08:56 schrieb Luben Tuikov:

Quoted 27 lines · Christian König
Quoted 26 lines

Am 24.11.22 um 22:19 schrieb Luben Tuikov:

Quoted 15 lines

Fix minmax compilation error by using min_t()/max_t(), of the assignment type.

Cc James Zhu <[email protected]>
Cc Felix Kuehling <[email protected]>
Fixes 58170a7a002ad6 ("drm/amdgpu: fix stall on CPU when allocate large system memory")
---
    drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 10 +++++++---
    1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -0
index 8a2e5716d8dba2..d22d14b0ef0c84 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 196

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +3 -1
@@ -191,14 +191,18 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
    	hmm_range->dev_private_owner = owner;
 
    	do {
-		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
+		hmm_range->end = min_t(typeof(hmm_range->end),
+				       hmm_range->start + MAX_WALK_BYTE,
+				       end);

Since end is a local variable I would strongly prefer to just have it use the correct type for it.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Otherwise we might end up using something which doesn't work on all architectures.

They all appear to be "unsigned long". I thought, since we assign to hmm_range->end, we use that type.

Mhm, then why does the compiler complain here?

Right... so MAX_WALK_BYTE is 2^36 ULL (diabolically defined as 64ULL<<30 :-) ), and this is why the minmax check complains.

Quoted 8 lines

So, since the left-hand expression is unsigned long, i.e.,

	hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
is,
	unsigned long = min(unsigned long long, unsigned long);

The compiler complains.

I'd really prefer MAX_WALK_BYTE be less than or equal to ULONG_MAX,

That's not only a preference, but a must have. Otherwise the code maybe won't work as expected on 32bit architectures.

Quoted 5 lines

and be defined as <literal>UL. I mean, why is everything in struct hmm_range "unsigned long", but we set a high limit of 10_0000_0000h for an end, and compare it to "end" to find the smaller? If our "end" could potentially be 10_0000_0000h then shouldn't the members in struct hmm_range be unsigned long long as well?

No, that the hmm range depends on the address space bits of the CPU is perfectly correct. Essentially this is just an userspace address range.

Our problem here is that this code needs to work on both 32bit and 64bit systems. And on a 32bit system limiting the types wouldn't work correctly as far as I can see.

So the compiler is complaining for rather good reasons and by using "min_t(UL" we just hide that instead of fixing the problem.

I suggest to use "min_t(u64" instead. An intelligent compiler should even be capable of optimizing this away by looking at the input types on 32bit archs.

Quoted 8 lines

And for the timeout, we have the (now) obvious,

	timeout = max((hmm_range->end - hmm_range->start) >> 29, 1ULL);

and I don't know why we necessarily need a "1ULL", when 1UL would do just fine, and then compilation passes for that statement. I can set this to 1UL, instead of using max_t().

I think just changing this to 1UL should be sufficient.

Regards,
Christian.

Quoted 21 lines

Regards,
Luben

Quoted 16 lines

As far as I can see "unsigned long" is correct here, but if we somehow have a typecast then something is not working as expected.

Is MAX_WALK_BYTE maybe of signed type?

Quoted 2 lines

Would you prefer at the top of the function to define "timeout" and "end" as,

	typeof(hmm_range->end) end, timeout;

Well for end that might make sense, but timeout is independent of the hmm range.

Regards,
Christian.

Quoted 3 lines

Regards,
Luben

#7 lore raw
Quoted 47 lines · Christian König

Am 25.11.22 um 09:33 schrieb Luben Tuikov:

Quoted 44 lines · Christian König
Quoted 28 lines

Am 25.11.22 um 08:56 schrieb Luben Tuikov:

Quoted 27 lines · Christian König
Quoted 26 lines

Am 24.11.22 um 22:19 schrieb Luben Tuikov:

Quoted 15 lines

Fix minmax compilation error by using min_t()/max_t(), of the assignment type.

Cc James Zhu <[email protected]>
Cc Felix Kuehling <[email protected]>
Fixes 58170a7a002ad6 ("drm/amdgpu: fix stall on CPU when allocate large system memory")
---
    drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 10 +++++++---
    1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -0
index 8a2e5716d8dba2..d22d14b0ef0c84 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 196

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +3 -1
@@ -191,14 +191,18 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
    	hmm_range->dev_private_owner = owner;
 
    	do {
-		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
+		hmm_range->end = min_t(typeof(hmm_range->end),
+				       hmm_range->start + MAX_WALK_BYTE,
+				       end);

Since end is a local variable I would strongly prefer to just have it use the correct type for it.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Otherwise we might end up using something which doesn't work on all architectures.

They all appear to be "unsigned long". I thought, since we assign to hmm_range->end, we use that type.

Mhm, then why does the compiler complain here?

Right... so MAX_WALK_BYTE is 2^36 ULL (diabolically defined as 64ULL<<30 :-) ), and this is why the minmax check complains.

So, since the left-hand expression is unsigned long, i.e.,

	hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
is,
	unsigned long = min(unsigned long long, unsigned long);

The compiler complains.

Quoted 4 lines
Quoted 1 line

I'd really prefer MAX_WALK_BYTE be less than or equal to ULONG_MAX,

That's not only a preference, but a must have. Otherwise the code maybe won't work as expected on 32bit architectures.

Well, I don't know what to change MAX_WALK_BYTE to, and given the suggestion below to change to min_t(u64, ...), I wonder if messing with MAX_WALK_BYTE even matters--it wouldn't matter so long as the type in min_t() is u64. It's a macro at the moment.

However, the LHS--struct hmm_range's members are all unsigned long and then we're essentially doing (unsigned long) = (u64), which on 32-bit is (u32) = (u64).

Regards,
Luben

Quoted 57 lines
Quoted 6 lines

and be defined as <literal>UL. I mean, why is everything in struct hmm_range "unsigned long", but we set a high limit of 10_0000_0000h for an end, and compare it to "end" to find the smaller? If our "end" could potentially be 10_0000_0000h then shouldn't the members in struct hmm_range be unsigned long long as well?

No, that the hmm range depends on the address space bits of the CPU is perfectly correct. Essentially this is just an userspace address range.

Our problem here is that this code needs to work on both 32bit and 64bit systems. And on a 32bit system limiting the types wouldn't work correctly as far as I can see.

So the compiler is complaining for rather good reasons and by using "min_t(UL" we just hide that instead of fixing the problem.

I suggest to use "min_t(u64" instead. An intelligent compiler should even be capable of optimizing this away by looking at the input types on 32bit archs.

Quoted 8 lines

And for the timeout, we have the (now) obvious,

	timeout = max((hmm_range->end - hmm_range->start) >> 29, 1ULL);

and I don't know why we necessarily need a "1ULL", when 1UL would do just fine, and then compilation passes for that statement. I can set this to 1UL, instead of using max_t().

I think just changing this to 1UL should be sufficient.

Regards,
Christian.

Quoted 21 lines

Regards,
Luben

Quoted 16 lines

As far as I can see "unsigned long" is correct here, but if we somehow have a typecast then something is not working as expected.

Is MAX_WALK_BYTE maybe of signed type?

Quoted 2 lines

Would you prefer at the top of the function to define "timeout" and "end" as,

	typeof(hmm_range->end) end, timeout;

Well for end that might make sense, but timeout is independent of the hmm range.

Regards,
Christian.

Quoted 3 lines

Regards,
Luben

#8 lore raw
Quoted 50 lines · Luben Tuikov
Quoted 46 lines · Christian König

Am 25.11.22 um 09:33 schrieb Luben Tuikov:

Quoted 44 lines · Christian König
Quoted 28 lines

Am 25.11.22 um 08:56 schrieb Luben Tuikov:

Quoted 27 lines · Christian König
Quoted 26 lines

Am 24.11.22 um 22:19 schrieb Luben Tuikov:

Quoted 15 lines

Fix minmax compilation error by using min_t()/max_t(), of the assignment type.

Cc James Zhu <[email protected]>
Cc Felix Kuehling <[email protected]>
Fixes 58170a7a002ad6 ("drm/amdgpu: fix stall on CPU when allocate large system memory")
---
     drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 10 +++++++---
     1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -0
index 8a2e5716d8dba2..d22d14b0ef0c84 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 196

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +3 -1
@@ -191,14 +191,18 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
     	hmm_range->dev_private_owner = owner;
 
     	do {
-		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
+		hmm_range->end = min_t(typeof(hmm_range->end),
+				       hmm_range->start + MAX_WALK_BYTE,
+				       end);

Since end is a local variable I would strongly prefer to just have it use the correct type for it.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Otherwise we might end up using something which doesn't work on all architectures.

They all appear to be "unsigned long". I thought, since we assign to hmm_range->end, we use that type.

Mhm, then why does the compiler complain here?

Right... so MAX_WALK_BYTE is 2^36 ULL (diabolically defined as 64ULL<<30 :-) ), and this is why the minmax check complains.

So, since the left-hand expression is unsigned long, i.e.,

	hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
is,
	unsigned long = min(unsigned long long, unsigned long);

The compiler complains.

Quoted 3 lines
Quoted 1 line

I'd really prefer MAX_WALK_BYTE be less than or equal to ULONG_MAX,

That's not only a preference, but a must have. Otherwise the code maybe won't work as expected on 32bit architectures.

Quoted 8 lines

Well, I don't know what to change MAX_WALK_BYTE to, and given the suggestion below to change to min_t(u64, ...), I wonder if messing with MAX_WALK_BYTE even matters--it wouldn't matter so long as the type in min_t() is u64. It's a macro at the moment.

However, the LHS--struct hmm_range's members are all unsigned long and then we're essentially doing (unsigned long) = (u64), which on 32-bit is (u32) = (u64).

[JZ]MAX_WALK_BYTE can be reduce to #define MAX_WALK_BYTE (2UL<<30)

Quoted 55 lines

Regards,
Luben

Quoted 51 lines
Quoted 5 lines

and be defined as <literal>UL. I mean, why is everything in struct hmm_range "unsigned long", but we set a high limit of 10_0000_0000h for an end, and compare it to "end" to find the smaller? If our "end" could potentially be 10_0000_0000h then shouldn't the members in struct hmm_range be unsigned long long as well?

No, that the hmm range depends on the address space bits of the CPU is perfectly correct. Essentially this is just an userspace address range.

Our problem here is that this code needs to work on both 32bit and 64bit systems. And on a 32bit system limiting the types wouldn't work correctly as far as I can see.

So the compiler is complaining for rather good reasons and by using "min_t(UL" we just hide that instead of fixing the problem.

I suggest to use "min_t(u64" instead. An intelligent compiler should even be capable of optimizing this away by looking at the input types on 32bit archs.

Quoted 7 lines

And for the timeout, we have the (now) obvious,

	timeout = max((hmm_range->end - hmm_range->start) >> 29, 1ULL);

and I don't know why we necessarily need a "1ULL", when 1UL would do just fine, and then compilation passes for that statement. I can set this to 1UL, instead of using max_t().

I think just changing this to 1UL should be sufficient.

Regards,
Christian.

Quoted 20 lines

Regards,
Luben

Quoted 16 lines

As far as I can see "unsigned long" is correct here, but if we somehow have a typecast then something is not working as expected.

Is MAX_WALK_BYTE maybe of signed type?

Quoted 2 lines

Would you prefer at the top of the function to define "timeout" and "end" as,

	typeof(hmm_range->end) end, timeout;

Well for end that might make sense, but timeout is independent of the hmm range.

Regards,
Christian.

Quoted 3 lines

Regards,
Luben

#9 lore raw
Quoted 57 lines · James Zhu
Quoted 55 lines · Luben Tuikov
Quoted 46 lines · Christian König

Am 25.11.22 um 09:33 schrieb Luben Tuikov:

Quoted 44 lines · Christian König
Quoted 28 lines

Am 25.11.22 um 08:56 schrieb Luben Tuikov:

Quoted 27 lines · Christian König
Quoted 26 lines

Am 24.11.22 um 22:19 schrieb Luben Tuikov:

Quoted 15 lines

Fix minmax compilation error by using min_t()/max_t(), of the assignment type.

Cc James Zhu <[email protected]>
Cc Felix Kuehling <[email protected]>
Fixes 58170a7a002ad6 ("drm/amdgpu: fix stall on CPU when allocate large system memory")
---
     drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 10 +++++++---
     1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +0 -0
index 8a2e5716d8dba2..d22d14b0ef0c84 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 196

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +3 -1
@@ -191,14 +191,18 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
     	hmm_range->dev_private_owner = owner;
 
     	do {
-		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
+		hmm_range->end = min_t(typeof(hmm_range->end),
+				       hmm_range->start + MAX_WALK_BYTE,
+				       end);

Since end is a local variable I would strongly prefer to just have it use the correct type for it.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Otherwise we might end up using something which doesn't work on all architectures.

They all appear to be "unsigned long". I thought, since we assign to hmm_range->end, we use that type.

Mhm, then why does the compiler complain here?

Right... so MAX_WALK_BYTE is 2^36 ULL (diabolically defined as 64ULL<<30 :-) ), and this is why the minmax check complains.

So, since the left-hand expression is unsigned long, i.e.,

	hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
is,
	unsigned long = min(unsigned long long, unsigned long);

The compiler complains.

Quoted 3 lines
Quoted 1 line

I'd really prefer MAX_WALK_BYTE be less than or equal to ULONG_MAX,

That's not only a preference, but a must have. Otherwise the code maybe won't work as expected on 32bit architectures.

Well, I don't know what to change MAX_WALK_BYTE to, and given the suggestion below to change to min_t(u64, ...), I wonder if messing with MAX_WALK_BYTE even matters--it wouldn't matter so long as the type in min_t() is u64. It's a macro at the moment.

Quoted 4 lines
Quoted 3 lines

However, the LHS--struct hmm_range's members are all unsigned long and then we're essentially doing (unsigned long) = (u64), which on 32-bit is (u32) = (u64).

[JZ]MAX_WALK_BYTE can be reduce to #define MAX_WALK_BYTE (2UL<<30)

Hi James--okay, I'll prep up a patch.

Regards,
Luben