drm/amdgpu: Fix kfd device lock during partition
Subsystems (3)
PATCH 1/2 drm/amdgpu: Fix kfd device lock during partition
The compute partition switch locked the kfd device, freed it during the pre-switch teardown, and then unlocked at the end - by which point adev->kfd.dev referenced the newly reinitialized device, so the unlock landed on the wrong object and the per-device lock was never balanced.
Add a dedicated prepare function to unwind and clean up kfd device. Don't use the global lock during teardown. A new device is created after partition switch; hence there is no need to do 'unlock' on a different object after the switch.
User is not supposed to run any process on the device during partition switch. Keep acquire/release semantics for init_complete so that the device remains unavailable for a process till its reinitialization is complete.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 19 +++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 6 ++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 6 ++++-- drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c | 11 +---------- drivers/gpu/drm/amd/amdgpu/soc_v1_0.c | 11 +---------- drivers/gpu/drm/amd/amdkfd/kfd_device.c | 10 ++++++++-- drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c | 6 ++++++ 7 files changed, 45 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +19 -0
@@ -245,6 +245,25 @@ void amdgpu_amdkfd_device_fini_sw(struct amdgpu_device *adev) } } +int amdgpu_amdkfd_prepare_partition_switch (struct amdgpu_device *adev) +{ +struct kfd_dev *kfd = adev->kfd.dev; +int r; + +if (!kfd) +return 0 ; + r =kgd2kfd_check_and_lock_kfd (kfd); +if (r) +return r; + + adev->kfd.init_complete = false; +kgd2kfd_device_fini (kfd); + adev->kfd.dev =NULL ; + amdgpu_amdkfd_total_mem_size -= adev->gmc.real_vram_size; + +return 0 ; +} +void amdgpu_amdkfd_interrupt (struct amdgpu_device *adev,const void *ih_ring_entry) {
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +6 -0
@@ -170,6 +170,7 @@ void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,void amdgpu_amdkfd_device_probe (struct amdgpu_device *adev);void amdgpu_amdkfd_device_init (struct amdgpu_device *adev);void amdgpu_amdkfd_device_fini_sw (struct amdgpu_device *adev); +int amdgpu_amdkfd_prepare_partition_switch (struct amdgpu_device *adev);int amdgpu_amdkfd_check_and_lock_kfd (struct amdgpu_device *adev);void amdgpu_amdkfd_unlock_kfd (struct amdgpu_device *adev); int amdgpu_amdkfd_submit_ib(struct amdgpu_device *adev,
@@ -431,6 +432,7 @@ struct kfd_dev *kgd2kfd_probe(struct amdgpu_device *adev, bool vf);bool kgd2kfd_device_init (struct kfd_dev *kfd,const struct kgd2kfd_shared_resources *gpu_resources);void kgd2kfd_device_exit (struct kfd_dev *kfd); +void kgd2kfd_device_fini (struct kfd_dev *kfd);void kgd2kfd_suspend (struct kfd_dev *kfd,bool suspend_proc);int kgd2kfd_resume (struct kfd_dev *kfd,bool resume_proc);void kgd2kfd_suspend_process (struct kfd_dev *kfd);
@@ -485,6 +487,10 @@ static inline void kgd2kfd_device_exit(struct kfd_dev *kfd) { } +static inline void kgd2kfd_device_fini (struct kfd_dev *kfd) +{ +} +static inline void kgd2kfd_suspend (struct kfd_dev *kfd,bool suspend_proc) { }
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +4 -2
@@ -694,14 +694,16 @@ void amdgpu_xcp_update_supported_modes(struct amdgpu_xcp_mgr *xcp_mgr)int amdgpu_xcp_pre_partition_switch (struct amdgpu_xcp_mgr *xcp_mgr,u32 flags) { +int ret =0 ; +/* TODO: * Stop user queues and threads, and make sure GPU is empty of work. */ if (flags &AMDGPU_XCP_OPS_KFD ) -amdgpu_amdkfd_device_fini_sw (xcp_mgr->adev); + ret =amdgpu_amdkfd_prepare_partition_switch (xcp_mgr->adev); -return 0 ; +return ret; }int amdgpu_xcp_post_partition_switch (struct amdgpu_xcp_mgr *xcp_mgr,u32 flags)
drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c +1 -10
@@ -391,15 +391,9 @@ static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, !adev->in_suspend) flags |=AMDGPU_XCP_OPS_KFD ; -if (flags &AMDGPU_XCP_OPS_KFD ) { - ret =amdgpu_amdkfd_check_and_lock_kfd (adev); -if (ret) - goto out; - } - ret =amdgpu_xcp_pre_partition_switch (xcp_mgr, flags);if (ret) - goto unlock; + goto out; num_xcc_per_xcp =__aqua_vanjaram_get_xcc_per_xcp (xcp_mgr, mode);if (adev->gfx.funcs->switch_partition_mode)
@@ -413,9 +407,6 @@ static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, ret =amdgpu_xcp_post_partition_switch (xcp_mgr, flags);if (!ret)__aqua_vanjaram_update_available_partition_mode (xcp_mgr); -unlock: -if (flags &AMDGPU_XCP_OPS_KFD ) -amdgpu_amdkfd_unlock_kfd (adev); out:return ret; }
drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +1 -10
@@ -1097,15 +1097,9 @@ static int soc_v1_0_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,if (adev->kfd.init_complete && !amdgpu_in_reset (adev)) flags |=AMDGPU_XCP_OPS_KFD ; -if (flags &AMDGPU_XCP_OPS_KFD ) { - ret =amdgpu_amdkfd_check_and_lock_kfd (adev); -if (ret) - goto out; - } - ret =amdgpu_xcp_pre_partition_switch (xcp_mgr, flags);if (ret) - goto unlock; + goto out; num_xcc_per_xcp =__soc_v1_0_get_xcc_per_xcp (xcp_mgr, mode); if (adev->gfx.imu.funcs &&
@@ -1126,9 +1120,6 @@ static int soc_v1_0_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, ret =amdgpu_xcp_post_partition_switch (xcp_mgr, flags);if (!ret)__soc_v1_0_update_available_partition_mode (xcp_mgr); -unlock: -if (flags &AMDGPU_XCP_OPS_KFD ) -amdgpu_amdkfd_unlock_kfd (adev); out:return ret; }
drivers/gpu/drm/amd/amdkfd/kfd_device.c +8 -2
@@ -952,7 +952,8 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,svm_range_set_max_pages (kfd->adev); - kfd->init_complete = true; +/* Release pairs with the acquire in kfd_init_apertures(). */ +smp_store_release (&kfd->init_complete, true);dev_info (kfd_device,"added device %x:%x\n" , kfd->adev->pdev->vendor, kfd->adev->pdev->device);
@@ -977,7 +978,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,return kfd->init_complete; } -void kgd2kfd_device_exit (struct kfd_dev *kfd) +void kgd2kfd_device_fini (struct kfd_dev *kfd) {if (kfd->init_complete) {/* Cleanup KFD nodes */
@@ -991,6 +992,11 @@ void kgd2kfd_device_exit(struct kfd_dev *kfd) }kfree (kfd); +} + +void kgd2kfd_device_exit (struct kfd_dev *kfd) +{ +kgd2kfd_device_fini (kfd);/* after remove a kfd device unlock kfd driver */ kgd2kfd_unlock_kfd (NULL );
drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c +6 -0
@@ -396,6 +396,12 @@ int kfd_init_apertures(struct kfd_process *process)continue ; } +/* acquire pairs with the release in kgd2kfd_device_init() */ +if (!smp_load_acquire (&dev->kfd->init_complete)) { + id++; +continue ; + } + pdd =kfd_create_process_device_data (dev, process);if (!pdd) { dev_err(dev->adev->dev,
Signature · 1 line
2.49.0
PATCH 2/2 drm/amdgpu: remove unused kfd lock wrappers
amdgpu_amdkfd_check_and_lock_kfd() and amdgpu_amdkfd_unlock_kfd() no longer have any callers since the compute partition switch was reworked.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 10 ---------- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 -- 2 files changed, 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +0 -10
@@ -836,16 +836,6 @@ int amdgpu_amdkfd_send_close_event_drain_irq(struct amdgpu_device *adev,return 0 ; } -int amdgpu_amdkfd_check_and_lock_kfd (struct amdgpu_device *adev) -{ -return kgd2kfd_check_and_lock_kfd (adev->kfd.dev); -} - -void amdgpu_amdkfd_unlock_kfd (struct amdgpu_device *adev) -{ -kgd2kfd_unlock_kfd (adev->kfd.dev); -} -u64 amdgpu_amdkfd_xcp_memory_size (struct amdgpu_device *adev,int xcp_id) {
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -2
@@ -171,8 +171,6 @@ void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev);void amdgpu_amdkfd_device_init (struct amdgpu_device *adev);void amdgpu_amdkfd_device_fini_sw (struct amdgpu_device *adev);int amdgpu_amdkfd_prepare_partition_switch (struct amdgpu_device *adev); -int amdgpu_amdkfd_check_and_lock_kfd (struct amdgpu_device *adev); -void amdgpu_amdkfd_unlock_kfd (struct amdgpu_device *adev); int amdgpu_amdkfd_submit_ib(struct amdgpu_device *adev, enum kgd_engine_type engine, uint32_t vmid, uint64_t gpu_addr,
Signature · 1 line
2.49.0
AMD General
Quoted 94 lines
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Wednesday, September 9, 2026 3:51 AM
To: [email protected]
Cc: Zhang, Hawking <[email protected]>; Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Joshi, Mukul <[email protected]>; Liu, Xiang(Dean) <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: Fix kfd device lock during partitionThe compute partition switch locked the kfd device, freed it during the pre- switch teardown, and then unlocked at the end - by which point
adev->kfd.devreferenced the newly reinitialized device, so the unlock landed on the wrong object and the per-device lock was never balanced.Add a dedicated prepare function to unwind and clean up kfd device. Don't use the global lock during teardown. A new device is created after partition switch; hence there is no need to do 'unlock' on a different object after the switch.
User is not supposed to run any process on the device during partition switch. Keep acquire/release semantics for
init_completeso that the device remains unavailable for a process till its reinitialization is complete.Signed-off-by Lijo Lazar <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 19 +++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 6 ++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 6 ++++-- drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c | 11 +---------- drivers/gpu/drm/amd/amdgpu/soc_v1_0.c | 11 +---------- drivers/gpu/drm/amd/amdkfd/kfd_device.c | 10 ++++++++-- drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c | 6 ++++++ 7 files changed, 45 insertions(+), 24 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +19
-0@@ -245,6 +245,25 @@ void amdgpu_amdkfd_device_fini_sw(struct amdgpu_device *adev) } } +int amdgpu_amdkfd_prepare_partition_switch (struct amdgpu_device *adev) +{ +struct kfd_dev *kfd = adev->kfd.dev; +int r; + +if (!kfd) +return 0 ; + r =kgd2kfd_check_and_lock_kfd (kfd); +if (r) +return r; + + adev->kfd.init_complete = false; +kgd2kfd_device_fini (kfd); + adev->kfd.dev =NULL ; + amdgpu_amdkfd_total_mem_size -= adev->gmc.real_vram_size; + +return 0 ; +} + void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,const void
*ih_ring_entry)
{drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0
-0@@ -170,6 +170,7 @@ void amdgpu_amdkfd_interrupt(struct
amdgpu_device*adev, voidamdgpu_amdkfd_device_probe(struct amdgpu_device *adev); voidamdgpu_amdkfd_device_init(struct amdgpu_device *adev); voidamdgpu_amdkfd_device_fini_sw(struct amdgpu_device *adev);
+intamdgpu_amdkfd_prepare_partition_switch(struct amdgpu_device *adev);
intamdgpu_amdkfd_check_and_lock_kfd(struct amdgpu_device *adev);
voidamdgpu_amdkfd_unlock_kfd(struct amdgpu_device *adev); int amdgpu_amdkfd_submit_ib(structamdgpu_device*adev, @@ -431,6 +432,7 @@ structkfd_dev*kgd2kfd_probe(structamdgpu_device*adev, bool vf); bool kgd2kfd_device_init(structkfd_dev*kfd,
const structkgd2kfd_shared_resources*gpu_resources); voidkgd2kfd_device_exit(struct kfd_dev *kfd); +voidkgd2kfd_device_fini(struct kfd_dev *kfd);
voidkgd2kfd_suspend(struct kfd_dev *kfd, bool suspend_proc); intkgd2kfd_resume(struct kfd_dev *kfd, bool resume_proc); voidkgd2kfd_suspend_process(struct kfd_dev *kfd); @@ -485,6 +487,10 @@ static inline voidkgd2kfd_device_exit(struct kfd_dev *kfd){ }
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2
-0+static inline void kgd2kfd_device_fini (struct kfd_dev *kfd) { } +
Maybe follow the pattern of having open and close branch on new lines as done for the other functions.
Quoted 124 lines
static inline void
kgd2kfd_suspend(struct kfd_dev *kfd, bool suspend_proc){
} diff --gita/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.cb/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
indexa8c79a66df1d..92fa15ace438100644drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +2
-0@@ -694,14 +694,16 @@ void amdgpu_xcp_update_supported_modes(struct amdgpu_xcp_mgr *xcp_mgr) int amdgpu_xcp_pre_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr,u32 flags) { +int ret =0 ; +/* TODO: * Stop user queues and threads, and make sure GPU is empty of work.
*/if (flags &
AMDGPU_XCP_OPS_KFD)drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +1
-1-amdgpu_amdkfd_device_fini_sw (xcp_mgr->adev); + ret = amdgpu_amdkfd_prepare_partition_switch(xcp_mgr-Quoted 1 line
adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +1
-1-return 0 ; +return ret; } int amdgpu_xcp_post_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr,u32 flags) diff --git
a/drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.cb/drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c
indexcddfe4015f53..699e797679ec100644drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c +1
-7@@ -391,15 +391,9 @@ static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, !adev->in_suspend) flags |=AMDGPU_XCP_OPS_KFD ; -if (flags &AMDGPU_XCP_OPS_KFD ) { - ret =amdgpu_amdkfd_check_and_lock_kfd (adev); -if (ret) - goto out; - } - ret =amdgpu_xcp_pre_partition_switch (xcp_mgr, flags);if (ret) - goto unlock; + goto out; num_xcc_per_xcp = __aqua_vanjaram_get_xcc_per_xcp(xcp_mgr,mode); if (adev->gfx.funcs->switch_partition_mode)drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c +0
-3@@ -413,9 +407,6 @@ static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, ret =amdgpu_xcp_post_partition_switch (xcp_mgr, flags);if (!ret)__aqua_vanjaram_update_available_partition_mode (xcp_mgr); -unlock: -if (flags &AMDGPU_XCP_OPS_KFD ) -amdgpu_amdkfd_unlock_kfd (adev); out:return ret;}
drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +1
-7@@ -1097,15 +1097,9 @@ static int soc_v1_0_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, if (adev->kfd.init_complete && !amdgpu_in_reset(adev)) flags |= AMDGPU_XCP_OPS_KFD; -if (flags &AMDGPU_XCP_OPS_KFD ) { - ret =amdgpu_amdkfd_check_and_lock_kfd (adev); -if (ret) - goto out; - } - ret =amdgpu_xcp_pre_partition_switch (xcp_mgr, flags);if (ret) - goto unlock; + goto out; num_xcc_per_xcp =__soc_v1_0_get_xcc_per_xcp (xcp_mgr, mode);if (adev->gfx.imu.funcs &&drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +0
-3@@ -1126,9 +1120,6 @@ static int soc_v1_0_switch_partition_mode(structamdgpu_xcp_mgr *xcp_mgr, ret =amdgpu_xcp_post_partition_switch (xcp_mgr, flags);if (!ret)__soc_v1_0_update_available_partition_mode (xcp_mgr); -unlock: -if (flags &AMDGPU_XCP_OPS_KFD ) -amdgpu_amdkfd_unlock_kfd (adev); out:return ret;}
drivers/gpu/drm/amd/amdkfd/kfd_device.c +3
-2@@ -952,7 +952,8 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,svm_range_set_max_pages (kfd->adev); - kfd->init_complete = true; +/* Release pairs with the acquire in kfd_init_apertures(). */ +smp_store_release (&kfd->init_complete, true);dev_info (kfd_device,"added device %x:%x\n" , kfd->adev->pdev- >vendor, kfd->adev->pdev->device);@@ -977,7 +978,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,return kfd->init_complete; } -void kgd2kfd_device_exit (struct kfd_dev *kfd) +void kgd2kfd_device_fini (struct kfd_dev *kfd) {if (kfd->init_complete) {/* Cleanup KFD nodes */
drivers/gpu/drm/amd/amdkfd/kfd_device.c void kgd2kfd_device_exit(struct kfd_dev *kfd) · line 997
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +3
-0@@ -991,6 +992,11 @@ void kgd2kfd_device_exit(struct kfd_dev *kfd) }kfree (kfd); +} + +void kgd2kfd_device_exit (struct kfd_dev *kfd) {
Open brace should be on the next line.
Maybe run it through checkpatch once if not already done.
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1
-0+kgd2kfd_device_fini (kfd);/* after remove a kfd device unlock kfd driver */ kgd2kfd_unlock_kfd (NULL );drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c +0
-0
drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c int kfd_init_apertures(struct kfd_process *process) · line 400
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c +2
-0@@ -396,6 +396,12 @@ int kfd_init_apertures(struct kfd_process *process)continue ; } +/* acquire pairs with the release in kgd2kfd_device_init() */ +if (!smp_load_acquire (&dev->kfd->init_complete)) {
Do we need similar smp_load_acquire() at other call sites of this kfd->init_complete?
With the minor nit-picks above fixed, this series is:
Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c +4
-1+ id++; +continue ; + } + pdd =kfd_create_process_device_data (dev, process);if (!pdd) { dev_err(dev->adev->dev, --2.49.0
AMD General
Quoted 109 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Joshi, Mukul
Sent: Wednesday, September 9, 2026 1:14 PM
To: Lazar, Lijo <[email protected]>; [email protected]
Cc: Zhang, Hawking <[email protected]>; Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Liu, Xiang(Dean) <[email protected]>
Subject: RE: [PATCH 1/2] drm/amdgpu: Fix kfd device lock during partitionAMD General
Quoted 6 lines
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Wednesday, September 9, 2026 3:51 AM
To: [email protected]
Cc: Zhang, Hawking <[email protected]>; Deucher, Alexander <[email protected]>; Kuehling, Felixdrivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h void amdgpu_amdkfd_interrupt(struct
Quoted 69 lines
Joshi, Mukul <[email protected]>; Liu, Xiang(Dean) <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: Fix kfd device lock during partitionThe compute partition switch locked the kfd device, freed it during the pre- switch teardown, and then unlocked at the end - by which point
adev->kfd.devreferenced the newly reinitialized device, so the unlock landed on the wrong object and the per-device lock was never balanced.Add a dedicated prepare function to unwind and clean up kfd device. Don't use the global lock during teardown. A new device is created after partition switch; hence there is no need to do 'unlock' on a different object after the switch.
User is not supposed to run any process on the device during partition switch. Keep acquire/release semantics for
init_completeso that the device remains unavailable for a process till its reinitialization is complete.Signed-off-by Lijo Lazar <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 19 +++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 6 ++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 6 ++++-- drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c | 11 +---------- drivers/gpu/drm/amd/amdgpu/soc_v1_0.c | 11 +---------- drivers/gpu/drm/amd/amdkfd/kfd_device.c | 10 ++++++++-- drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c | 6 ++++++ 7 files changed, 45 insertions(+), 24 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +19
-0@@ -245,6 +245,25 @@ void amdgpu_amdkfd_device_fini_sw(struct amdgpu_device *adev) } } +int amdgpu_amdkfd_prepare_partition_switch (struct amdgpu_device *adev) +{ +struct kfd_dev *kfd = adev->kfd.dev; +int r; + +if (!kfd) +return 0 ; + r =kgd2kfd_check_and_lock_kfd (kfd); +if (r) +return r; + + adev->kfd.init_complete = false; +kgd2kfd_device_fini (kfd); + adev->kfd.dev =NULL ; + amdgpu_amdkfd_total_mem_size -= adev->gmc.real_vram_size; + +return 0 ; +} + void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,const void *ih_ring_entry) { diff --git
a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.hb/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
index1b7dc0d3963b..36e222f3ac5b100644drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0
-0@@ -170,6 +170,7 @@ void amdgpu_amdkfd_interrupt(struct
amdgpu_deviceQuoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0
-0*adev, void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev);void
amdgpu_amdkfd_device_init(struct amdgpu_device *adev); void amdgpu_amdkfd_device_fini_sw(structamdgpu_device*adev);
+intamdgpu_amdkfd_prepare_partition_switch(struct amdgpu_device *adev);
intamdgpu_amdkfd_check_and_lock_kfd(struct amdgpu_device *adev);
voidamdgpu_amdkfd_unlock_kfd(struct amdgpu_device *adev); int amdgpu_amdkfd_submit_ib(structamdgpu_device*adev, @@ -431,6+432,7
Quoted 10 lines
@@ struct
kfd_dev*kgd2kfd_probe(structamdgpu_device*adev, bool vf); bool kgd2kfd_device_init(structkfd_dev*kfd,
const structkgd2kfd_shared_resources*gpu_resources); voidkgd2kfd_device_exit(struct kfd_dev *kfd); +voidkgd2kfd_device_fini(struct kfd_dev *kfd);
voidkgd2kfd_suspend(struct kfd_dev *kfd, bool suspend_proc); intkgd2kfd_resume(struct kfd_dev *kfd, bool resume_proc); voidkgd2kfd_suspend_process(struct kfd_dev *kfd); @@ -485,6 +487,10 @@ static inline voidkgd2kfd_device_exit(struct kfd_dev *kfd){ }
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2
-0+static inline void kgd2kfd_device_fini (struct kfd_dev *kfd) { } +Maybe follow the pattern of having open and close branch on new lines as done for the other functions.
Sorry I meant open and close brace here.
Regards,
Mukul
Quoted 168 lines
Quoted 9 lines
static inline void kgd2kfd_suspend(struct
kfd_dev*kfd, boolsuspend_proc) { } diff --gita/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.cb/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
indexa8c79a66df1d..92fa15ace438100644drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +0
-0@@ -694,14 +694,16 @@ voidamdgpu_xcp_update_supported_modes(struct
amdgpu_xcp_mgr
*xcp_mgr)Quoted 117 lines
int amdgpu_xcp_pre_partition_switch(struct
amdgpu_xcp_mgr*xcp_mgr,
u32 flags) {drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +2
-0+int ret =0 ; +/* TODO: * Stop user queues and threads, and make sure GPU is empty of work.
*/if (flags &
AMDGPU_XCP_OPS_KFD)drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +1
-1-amdgpu_amdkfd_device_fini_sw (xcp_mgr->adev); + ret = amdgpu_amdkfd_prepare_partition_switch(xcp_mgr-Quoted 1 line
adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +1
-1-return 0 ; +return ret; } int amdgpu_xcp_post_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr,u32 flags) diff --git
a/drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.cb/drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c
indexcddfe4015f53..699e797679ec100644drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c +1
-7@@ -391,15 +391,9 @@ static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, !adev->in_suspend) flags |=AMDGPU_XCP_OPS_KFD ; -if (flags &AMDGPU_XCP_OPS_KFD ) { - ret =amdgpu_amdkfd_check_and_lock_kfd (adev); -if (ret) - goto out; - } - ret =amdgpu_xcp_pre_partition_switch (xcp_mgr, flags);if (ret) - goto unlock; + goto out; num_xcc_per_xcp = __aqua_vanjaram_get_xcc_per_xcp(xcp_mgr,mode); if (adev->gfx.funcs->switch_partition_mode)drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c +0
-3@@ -413,9 +407,6 @@ static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, ret =amdgpu_xcp_post_partition_switch (xcp_mgr, flags);if (!ret)__aqua_vanjaram_update_available_partition_mode (xcp_mgr); -unlock: -if (flags &AMDGPU_XCP_OPS_KFD ) -amdgpu_amdkfd_unlock_kfd (adev); out:return ret;}
drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +0
-0@@ -1097,15 +1097,9 @@ static intsoc_v1_0_switch_partition_mode(struct
amdgpu_xcp_mgr*xcp_mgr,if (adev->kfd.init_complete && !amdgpu_in_reset(adev)) flags |= AMDGPU_XCP_OPS_KFD;drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +1
-10-if (flags &AMDGPU_XCP_OPS_KFD ) { - ret =amdgpu_amdkfd_check_and_lock_kfd (adev); -if (ret) - goto out; - } - ret =amdgpu_xcp_pre_partition_switch (xcp_mgr, flags);if (ret) - goto unlock; + goto out; num_xcc_per_xcp =__soc_v1_0_get_xcc_per_xcp (xcp_mgr, mode); if (adev->gfx.imu.funcs &&@@ -1126,9 +1120,6 @@ static int soc_v1_0_switch_partition_mode(structamdgpu_xcp_mgr *xcp_mgr, ret =amdgpu_xcp_post_partition_switch (xcp_mgr, flags);if (!ret)__soc_v1_0_update_available_partition_mode (xcp_mgr); -unlock: -if (flags &AMDGPU_XCP_OPS_KFD ) -amdgpu_amdkfd_unlock_kfd (adev); out:return ret;}
drivers/gpu/drm/amd/amdkfd/kfd_device.c +3
-2@@ -952,7 +952,8 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,svm_range_set_max_pages (kfd->adev); - kfd->init_complete = true; +/* Release pairs with the acquire in kfd_init_apertures(). */ +smp_store_release (&kfd->init_complete, true);dev_info (kfd_device,"added device %x:%x\n" , kfd->adev->pdev- >vendor, kfd->adev->pdev->device);@@ -977,7 +978,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,return kfd->init_complete; } -void kgd2kfd_device_exit (struct kfd_dev *kfd) +void kgd2kfd_device_fini (struct kfd_dev *kfd) {if (kfd->init_complete) {/* Cleanup KFD nodes */ drivers/gpu/drm/amd/amdkfd/kfd_device.c void kgd2kfd_device_exit(struct kfd_dev *kfd) · line 997
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +3
-0@@ -991,6 +992,11 @@ void kgd2kfd_device_exit(struct kfd_dev *kfd) }kfree (kfd); +} + +void kgd2kfd_device_exit (struct kfd_dev *kfd) {Open brace should be on the next line.
Maybe run it through checkpatch once if not already done.Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1
-0+kgd2kfd_device_fini (kfd);/* after remove a kfd device unlock kfd driver */ kgd2kfd_unlock_kfd (NULL );drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c +0
-0drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c int kfd_init_apertures(struct kfd_process *process) · line 400
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c +2
-0@@ -396,6 +396,12 @@ int kfd_init_apertures(struct kfd_process *process)continue ; } +/* acquire pairs with the release in kgd2kfd_device_init() */ +if (!smp_load_acquire (&dev->kfd->init_complete)) {Do we need similar
smp_load_acquire()at other call sites of this kfd-Quoted 1 line
init_complete?With the minor nit-picks above fixed, this series is:
Reviewed-by Mukul Joshi <[email protected]>Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c +4
-1+ id++; +continue ; + } + pdd =kfd_create_process_device_data (dev, process);if (!pdd) { dev_err(dev->adev->dev, --2.49.0
Quoted 249 lines · Mukul
AMD General
Quoted 94 lines
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Wednesday, September 9, 2026 3:51 AM
To: [email protected]
Cc: Zhang, Hawking <[email protected]>; Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Joshi, Mukul <[email protected]>; Liu, Xiang(Dean) <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: Fix kfd device lock during partitionThe compute partition switch locked the kfd device, freed it during the pre- switch teardown, and then unlocked at the end - by which point
adev->kfd.devreferenced the newly reinitialized device, so the unlock landed on the wrong object and the per-device lock was never balanced.Add a dedicated prepare function to unwind and clean up kfd device. Don't use the global lock during teardown. A new device is created after partition switch; hence there is no need to do 'unlock' on a different object after the switch.
User is not supposed to run any process on the device during partition switch. Keep acquire/release semantics for
init_completeso that the device remains unavailable for a process till its reinitialization is complete.Signed-off-by Lijo Lazar <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 19 +++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 6 ++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 6 ++++-- drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c | 11 +---------- drivers/gpu/drm/amd/amdgpu/soc_v1_0.c | 11 +---------- drivers/gpu/drm/amd/amdkfd/kfd_device.c | 10 ++++++++-- drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c | 6 ++++++ 7 files changed, 45 insertions(+), 24 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +19
-0@@ -245,6 +245,25 @@ void amdgpu_amdkfd_device_fini_sw(struct amdgpu_device *adev) } } +int amdgpu_amdkfd_prepare_partition_switch (struct amdgpu_device *adev) +{ +struct kfd_dev *kfd = adev->kfd.dev; +int r; + +if (!kfd) +return 0 ; + r =kgd2kfd_check_and_lock_kfd (kfd); +if (r) +return r; + + adev->kfd.init_complete = false; +kgd2kfd_device_fini (kfd); + adev->kfd.dev =NULL ; + amdgpu_amdkfd_total_mem_size -= adev->gmc.real_vram_size; + +return 0 ; +} + void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,const void
*ih_ring_entry)
{drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0
-0@@ -170,6 +170,7 @@ void amdgpu_amdkfd_interrupt(struct
amdgpu_device*adev, voidamdgpu_amdkfd_device_probe(struct amdgpu_device *adev); voidamdgpu_amdkfd_device_init(struct amdgpu_device *adev); voidamdgpu_amdkfd_device_fini_sw(struct amdgpu_device *adev);
+intamdgpu_amdkfd_prepare_partition_switch(struct amdgpu_device *adev);
intamdgpu_amdkfd_check_and_lock_kfd(struct amdgpu_device *adev);
voidamdgpu_amdkfd_unlock_kfd(struct amdgpu_device *adev); int amdgpu_amdkfd_submit_ib(structamdgpu_device*adev, @@ -431,6 +432,7 @@ structkfd_dev*kgd2kfd_probe(structamdgpu_device*adev, bool vf); bool kgd2kfd_device_init(structkfd_dev*kfd,
const structkgd2kfd_shared_resources*gpu_resources); voidkgd2kfd_device_exit(struct kfd_dev *kfd); +voidkgd2kfd_device_fini(struct kfd_dev *kfd);
voidkgd2kfd_suspend(struct kfd_dev *kfd, bool suspend_proc); intkgd2kfd_resume(struct kfd_dev *kfd, bool resume_proc); voidkgd2kfd_suspend_process(struct kfd_dev *kfd); @@ -485,6 +487,10 @@ static inline voidkgd2kfd_device_exit(struct kfd_dev *kfd){ }drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2
-0+static inline void kgd2kfd_device_fini (struct kfd_dev *kfd) { } +Maybe follow the pattern of having open and close branch on new lines as done for the other functions.
Quoted 124 lines
static inline void
kgd2kfd_suspend(struct kfd_dev *kfd, bool suspend_proc){
} diff --gita/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.cb/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
indexa8c79a66df1d..92fa15ace438100644drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +2
-0@@ -694,14 +694,16 @@ void amdgpu_xcp_update_supported_modes(struct amdgpu_xcp_mgr *xcp_mgr) int amdgpu_xcp_pre_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr,u32 flags) { +int ret =0 ; +/* TODO: * Stop user queues and threads, and make sure GPU is empty of work.
*/if (flags &
AMDGPU_XCP_OPS_KFD)drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +1
-1-amdgpu_amdkfd_device_fini_sw (xcp_mgr->adev); + ret = amdgpu_amdkfd_prepare_partition_switch(xcp_mgr-Quoted 1 line
adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +1
-1-return 0 ; +return ret; } int amdgpu_xcp_post_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr,u32 flags) diff --git
a/drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.cb/drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c
indexcddfe4015f53..699e797679ec100644drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c +1
-7@@ -391,15 +391,9 @@ static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, !adev->in_suspend) flags |=AMDGPU_XCP_OPS_KFD ; -if (flags &AMDGPU_XCP_OPS_KFD ) { - ret =amdgpu_amdkfd_check_and_lock_kfd (adev); -if (ret) - goto out; - } - ret =amdgpu_xcp_pre_partition_switch (xcp_mgr, flags);if (ret) - goto unlock; + goto out; num_xcc_per_xcp = __aqua_vanjaram_get_xcc_per_xcp(xcp_mgr,mode); if (adev->gfx.funcs->switch_partition_mode)drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c +0
-3@@ -413,9 +407,6 @@ static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, ret =amdgpu_xcp_post_partition_switch (xcp_mgr, flags);if (!ret)__aqua_vanjaram_update_available_partition_mode (xcp_mgr); -unlock: -if (flags &AMDGPU_XCP_OPS_KFD ) -amdgpu_amdkfd_unlock_kfd (adev); out:return ret;}
drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +1
-7@@ -1097,15 +1097,9 @@ static int soc_v1_0_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, if (adev->kfd.init_complete && !amdgpu_in_reset(adev)) flags |= AMDGPU_XCP_OPS_KFD; -if (flags &AMDGPU_XCP_OPS_KFD ) { - ret =amdgpu_amdkfd_check_and_lock_kfd (adev); -if (ret) - goto out; - } - ret =amdgpu_xcp_pre_partition_switch (xcp_mgr, flags);if (ret) - goto unlock; + goto out; num_xcc_per_xcp =__soc_v1_0_get_xcc_per_xcp (xcp_mgr, mode);if (adev->gfx.imu.funcs &&drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +0
-3@@ -1126,9 +1120,6 @@ static int soc_v1_0_switch_partition_mode(structamdgpu_xcp_mgr *xcp_mgr, ret =amdgpu_xcp_post_partition_switch (xcp_mgr, flags);if (!ret)__soc_v1_0_update_available_partition_mode (xcp_mgr); -unlock: -if (flags &AMDGPU_XCP_OPS_KFD ) -amdgpu_amdkfd_unlock_kfd (adev); out:return ret;}drivers/gpu/drm/amd/amdkfd/kfd_device.c +3
-2@@ -952,7 +952,8 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,svm_range_set_max_pages (kfd->adev); - kfd->init_complete = true; +/* Release pairs with the acquire in kfd_init_apertures(). */ +smp_store_release (&kfd->init_complete, true);dev_info (kfd_device,"added device %x:%x\n" , kfd->adev->pdev- > vendor, kfd->adev->pdev->device);@@ -977,7 +978,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,return kfd->init_complete; } -void kgd2kfd_device_exit (struct kfd_dev *kfd) +void kgd2kfd_device_fini (struct kfd_dev *kfd) {if (kfd->init_complete) {/* Cleanup KFD nodes */ drivers/gpu/drm/amd/amdkfd/kfd_device.c void kgd2kfd_device_exit(struct kfd_dev *kfd) · line 997
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +3
-0@@ -991,6 +992,11 @@ void kgd2kfd_device_exit(struct kfd_dev *kfd) }kfree (kfd); +} + +void kgd2kfd_device_exit (struct kfd_dev *kfd) {Open brace should be on the next line.
Maybe run it through checkpatch once if not already done.Quoted 13 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1
-0+kgd2kfd_device_fini (kfd);/* after remove a kfd device unlock kfd driver */ kgd2kfd_unlock_kfd (NULL );drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c +0
-0@@ -396,6 +396,12 @@ int kfd_init_apertures(struct kfd_process *process)continue ; }
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c int kfd_init_apertures(struct kfd_process *process) · line 400
Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c +2
-0+/* acquire pairs with the release in kgd2kfd_device_init() */ +if (!smp_load_acquire (&dev->kfd->init_complete)) {Do we need similar
smp_load_acquire()at other call sites of thiskfd->init_complete?
Presently, it's done only at this place so that no process is bound to the device till reinit is complete. The other places are opertions after a process is bound, so they are not checked.
BTW, looks like Outlook mail client is the culprit behind style issues seen. Checkpatch didn't show any issues, and Thunderbird shows the patch properly.
Thanks,
Lijo
Quoted 13 lines
With the minor nit-picks above fixed, this series is:
Reviewed-by Mukul Joshi <[email protected]>Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_flat_memory.c +4
-1+ id++; +continue ; + } + pdd =kfd_create_process_device_data (dev, process);if (!pdd) { dev_err(dev->adev->dev, --2.49.0