criu_get_process_object_info() holds p->mutex while counting events
in p->event_idr. Event creation and destruction use p->event_mutex,
so the event IDR can change during the walk. If an event is removed,
the walk can access a freed IDR node.

Hold p->event_mutex while counting events.
kfd_criu_checkpoint_events() uses the same lock when walking the
event IDR. Event creation and destruction do not acquire p->mutex,
so taking p->event_mutex while holding p->mutex preserves the lock
order.

Fixes: 40e8a766a761 ("drm/amdkfd: CRIU checkpoint and restore events")
Signed-off-by: Changyul Lee <[email protected]>
---
Found with a static analysis tool we are developing.

Compile-tested only, with x86_64_defconfig plus DRM_AMDGPU, HSA_AMD
and W=1.

 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 3d2136adb62b..20e3bbd073a5 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -2234,7 +2234,9 @@ static int criu_get_process_object_info(struct 
kfd_process *p,
        if (ret)
                return ret;
 
+       mutex_lock(&p->event_mutex);
        num_events = kfd_get_num_events(p);
+       mutex_unlock(&p->event_mutex);
 
        svm_range_get_info(p, &num_svm_ranges, &svm_priv_data_size);
 

base-commit: 8dd68a79583aaf6df6fe4feea3ee9e9f26b02993
-- 
2.43.0

Reply via email to