Skip to contentSkip to conversation

drm/amdgpu: Use local64_try_cmpxchg in amdgpu_perf_read

PATCH Applied Uros Bizjak 2 participants 2 messages Jul 3, 2023 – Aug 9, 2023

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

PATCH 1/1 drm/amdgpu: Use local64_try_cmpxchg in amdgpu_perf_read

Use local64_try_cmpxchg instead of local64_cmpxchg (*ptr, old, new) == old in amdgpu_perf_read. x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).

Also, try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop.

No functional change intended.

Cc Alex Deucher <[email protected]>
Cc "Christian König" <[email protected]>
Cc "Pan, Xinhui" <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_pmu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pmu.c +2 -3
index 71ee361d0972..6e91ea1de5aa 100644
@@ -276,9 +276,8 @@ static void amdgpu_perf_read(struct perf_event *event)
 	    (!pe->adev->df.funcs->pmc_get_count))
 		return;
 
+	prev = local64_read(&hwc->prev_count);
 	do {
-		prev = local64_read(&hwc->prev_count);
-
 		switch (hwc->config_base) {
 		case AMDGPU_PMU_EVENT_CONFIG_TYPE_DF:
 		case AMDGPU_PMU_EVENT_CONFIG_TYPE_XGMI:
@@ -289,7 +288,7 @@ static void amdgpu_perf_read(struct perf_event *event)
 			count = 0;
 			break;
 		}
-	} while (local64_cmpxchg(&hwc->prev_count, prev, count) != prev);
+	} while (!local64_try_cmpxchg(&hwc->prev_count, &prev, count));
 
 	local64_add(count - prev, &event->count);
 }
Signature · 1 line
2.41.0
Applied
#1 lore raw

Applied. Thanks!

Alex

Quoted 48 lines · Uros Bizjak

Use local64_try_cmpxchg instead of local64_cmpxchg (*ptr, old, new) == old in amdgpu_perf_read. x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).

Also, try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop.

No functional change intended.

Cc Alex Deucher <[email protected]>
Cc "Christian König" <[email protected]>
Cc "Pan, Xinhui" <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_pmu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pmu.c +2 -4
index 71ee361d0972..6e91ea1de5aa 100644
@@ -276,9 +276,8 @@ static void amdgpu_perf_read(struct perf_event *event)
            (!pe->adev->df.funcs->pmc_get_count))
                return;
 
+       prev = local64_read(&hwc->prev_count);
        do {
-               prev = local64_read(&hwc->prev_count);
-
                switch (hwc->config_base) {
                case AMDGPU_PMU_EVENT_CONFIG_TYPE_DF:
                case AMDGPU_PMU_EVENT_CONFIG_TYPE_XGMI:
@@ -289,7 +288,7 @@ static void amdgpu_perf_read(struct perf_event *event)
                        count = 0;
                        break;
                }
-       } while (local64_cmpxchg(&hwc->prev_count, prev, count) != prev);
+       } while (!local64_try_cmpxchg(&hwc->prev_count, &prev, count));
 
        local64_add(count - prev, &event->count);
 }
--

2.41.0