Skip to contentSkip to conversation

drm/amdgpu: Fix __rcu fence pointer accesses

PATCH In review Alex Hung 4 participants 5 messages Jul 9 – Jul 17

Subsystems (3)
lore
Share & tools
lore

Alex Hung

author
#0 lore raw

PATCH 1/2 drm/amdgpu: Fix __rcu fence pointer accesses

Building for ARCH=um with W=1 C=1 makes sparse report "incompatible types in comparison expression (different address spaces)" warnings in the KFD code, exposed after UML builds were enabled:

- amdgpu_amdkfd_fence.c compares the __rcu-annotated dma_fence.ops pointer directly in to_amdgpu_amdkfd_fence().
- amdgpu_amdkfd_gpuvm.c compares the __rcu eviction fence pointer directly in amdgpu_amdkfd_gpuvm_restore_process_bos().

Fixes af3f2f5db265 ("drm/amdgpu: Remove UML build exclusion from Kconfig")
Cc Harry Wentland <[email protected]>
Assisted-by Copilot:Claude-Opus-4.8
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c +1 -1
index 6a364357522b..b0299d861903 100644
@@ -92,7 +92,7 @@ struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f)
 		return NULL;
 
 	fence = container_of(f, struct amdgpu_amdkfd_fence, base);
-	if (f->ops == &amdkfd_fence_ops)
+	if (rcu_access_pointer(f->ops) == &amdkfd_fence_ops)
 		return fence;
 
 	return NULL;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -1
index 35fe2c974699..f0f516a79424 100644
@@ -3096,7 +3096,7 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *
 		process_info->eviction_fence = new_fence;
 		replace_eviction_fence(ef, dma_fence_get(&new_fence->base));
 	} else {
-		WARN_ONCE(*ef != &process_info->eviction_fence->base,
+		WARN_ONCE(rcu_access_pointer(*ef) != &process_info->eviction_fence->base,
 			  "KFD eviction fence doesn't match KGD process_info");
 	}
 

--
2.43.0

author
#1 lore raw

PATCH 2/2 drm/amdgpu: Pack nested ucode_info struct

Building for ARCH=um with W=1 C=1 makes the "amd_sriov_msg_vf2pf_info must be 1 KB" static assertion in amdgv_sriovmsg.h fail under sparse, exposed after UML builds were enabled.

Sparse does not honor #pragma pack(push, 1) for the nested ucode_info struct, so it sizes each element as 8 bytes instead of 5 and computes the surrounding structure as larger than 1 KB. The compilers get this right via the enclosing pragma, but the annotation should be explicit.

Fixes af3f2f5db265 ("drm/amdgpu: Remove UML build exclusion from Kconfig")
Cc Harry Wentland <[email protected]>
Assisted-by Copilot:Claude-Opus-4.8
---
 drivers/gpu/drm/amd/amdgpu/amdgv_sriovmsg.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgv_sriovmsg.h +1 -1
index d80f01c0e754..b02561f41b58 100644
@@ -379,7 +379,7 @@ struct amd_sriov_msg_vf2pf_info {
 	struct {
 		uint8_t id;
 		uint32_t version;
-	} ucode_info[AMD_SRIOV_MSG_RESERVE_UCODE];
+	} __packed ucode_info[AMD_SRIOV_MSG_RESERVE_UCODE];
 	uint64_t dummy_page_addr;
 	/* FB allocated for guest MES to record UQ info */
 	uint64_t mes_info_addr;
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: Pack nested ucode_info struct

Quoted 9 lines · Alex Hung

Building for ARCH=um with W=1 C=1 makes the "amd_sriov_msg_vf2pf_info must be 1 KB" static assertion in amdgv_sriovmsg.h fail under sparse, exposed after UML builds were enabled.

Sparse does not honor #pragma pack(push, 1) for the nested ucode_info struct, so it sizes each element as 8 bytes instead of 5 and computes the surrounding structure as larger than 1 KB. The compilers get this right via the enclosing pragma, but the annotation should be explicit.

Quoted 6 lines
Fixes af3f2f5db265 ("drm/amdgpu: Remove UML build exclusion from Kconfig")
Cc Harry Wentland <[email protected]>
Assisted-by Copilot:Claude-Opus-4.8

Harry

Quoted 17 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgv_sriovmsg.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgv_sriovmsg.h +1 -1
index d80f01c0e754..b02561f41b58 100644
@@ -379,7 +379,7 @@ struct amd_sriov_msg_vf2pf_info {
 	struct {
 		uint8_t id;
 		uint32_t version;
-	} ucode_info[AMD_SRIOV_MSG_RESERVE_UCODE];
+	} __packed ucode_info[AMD_SRIOV_MSG_RESERVE_UCODE];
 	uint64_t dummy_page_addr;
 	/* FB allocated for guest MES to record UQ info */
 	uint64_t mes_info_addr;
#3 lore raw
In reply to Alex Hung
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Misuse of rcu_access_pointer() on a non-RCU annotated pointer (f->ops), which actively triggers a…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Misuse of rcu_access_pointer() on a non-RCU annotated pointer (f->ops), which actively triggers a Sparse warning. --

--- Patch [1]: [PATCH 1/2] drm/amdgpu: Fix __rcu fence pointer accesses --- commit 5448af797b4d872eec1209aac5ec209c3c2b6b4a
Author: Alex Hung <[email protected]>

drm/amdgpu: Fix __rcu fence pointer accesses

This commit uses rcu_access_pointer() to resolve sparse warnings when comparing fence pointers in the KFD code. It wraps f->ops and an eviction fence pointer to address 'incompatible types in comparison expression' warnings.

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f) · line 95

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c +1 -1
index 31b8fa52b42fa..3419bad5c23a7 100644
@@ -92,7 +92,7 @@ struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f)
 		return NULL;
 
 	fence = container_of(f, struct amdgpu_amdkfd_fence, base);
-	if (f->ops == &amdkfd_fence_ops)
+	if (rcu_access_pointer(f->ops) == &amdkfd_fence_ops)

[Severity: Low]
Is f->ops actually an __rcu-annotated pointer?

Looking at struct dma_fence, the ops field is a standard const struct dma_fence_ops * without the __rcu annotation.

Does wrapping a non-RCU pointer in rcu_access_pointer() trigger the exact Sparse warning ("different address spaces") this commit aims to fix, since the macro expects an __rcu pointer?

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c +0 -0
 		return fence;
 
 	return NULL;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/2
#4 lore raw
In reply to Alex Hung
Quoted 9 lines · Alex Hung

Building for ARCH=um with W=1 C=1 makes sparse report "incompatible types in comparison expression (different address spaces)" warnings in the KFD code, exposed after UML builds were enabled:

- amdgpu_amdkfd_fence.c compares the __rcu-annotated dma_fence.ops pointer directly in to_amdgpu_amdkfd_fence().
- amdgpu_amdkfd_gpuvm.c compares the __rcu eviction fence pointer directly in amdgpu_amdkfd_gpuvm_restore_process_bos().

Quoted 6 lines
Fixes af3f2f5db265 ("drm/amdgpu: Remove UML build exclusion from Kconfig")
Cc Harry Wentland <[email protected]>
Assisted-by Copilot:Claude-Opus-4.8
Quoted 34 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c +1 -1
index 6a364357522b..b0299d861903 100644
@@ -92,7 +92,7 @@ struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f)
 		return NULL;
 
 	fence = container_of(f, struct amdgpu_amdkfd_fence, base);
-	if (f->ops == &amdkfd_fence_ops)
+	if (rcu_access_pointer(f->ops) == &amdkfd_fence_ops)
 		return fence;
 
 	return NULL;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -2
index 35fe2c974699..f0f516a79424 100644
@@ -3096,7 +3096,7 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *
 		process_info->eviction_fence = new_fence;
 		replace_eviction_fence(ef, dma_fence_get(&new_fence->base));
 	} else {
-		WARN_ONCE(*ef != &process_info->eviction_fence->base,
+		WARN_ONCE(rcu_access_pointer(*ef) != &process_info->eviction_fence->base,
 			  "KFD eviction fence doesn't match KGD process_info");
 	}
 
--

2.43.0