drivers/gpu/drm/amd/amdgpu/amdgpu_lockdep.c

Source file repositories/reference/linux-study-clean/drivers/gpu/drm/amd/amdgpu/amdgpu_lockdep.c

File Facts

System
Linux kernel
Corpus path
drivers/gpu/drm/amd/amdgpu/amdgpu_lockdep.c
Extension
.c
Size
6252 bytes
Lines
196
Domain
Driver Families
Bucket
drivers/gpu
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: MIT
/*
 * Copyright 2024 Advanced Micro Devices, Inc.
 *
 * Lockdep annotation for AMDGPU lock ordering
 *
 * This module teaches lockdep the correct lock ordering to catch
 * potential deadlocks at development time rather than runtime.
 *
 * Based on dma-resv lockdep approach from:
 * drivers/dma-buf/dma-resv.c:dma_resv_lockdep()
 */

#include "amdgpu.h"
#include "amdgpu_reset.h"

#ifdef CONFIG_LOCKDEP

/* Lock class keys for associating with real driver locks */
static struct lock_class_key amdgpu_userq_sch_mutex_key;
static struct lock_class_key amdgpu_userq_mutex_key;
static struct lock_class_key amdgpu_notifier_lock_key;
static struct lock_class_key amdgpu_vram_lock_key;
static struct lock_class_key amdgpu_reset_sem_key;
static struct lock_class_key amdgpu_reset_lock_key;
static struct lock_class_key amdgpu_srbm_lock_key;
static struct lock_class_key amdgpu_grbm_lock_key;
static struct lock_class_key amdgpu_mmio_lock_key;

/**
 * amdgpu_lockdep_set_class - Associate lock class keys with real locks
 * @adev: AMDGPU device
 *
 * Call during device init to associate lock classes with actual locks
 * so lockdep can track them properly.
 */
void amdgpu_lockdep_set_class(struct amdgpu_device *adev)
{
	lockdep_set_class(&adev->gfx.userq_sch_mutex,
			  &amdgpu_userq_sch_mutex_key);
	lockdep_set_class(&adev->notifier_lock, &amdgpu_notifier_lock_key);
	lockdep_set_class(&adev->srbm_mutex, &amdgpu_srbm_lock_key);
	lockdep_set_class(&adev->grbm_idx_mutex, &amdgpu_grbm_lock_key);
	lockdep_set_class(&adev->mmio_idx_lock, &amdgpu_mmio_lock_key);

	if (adev->reset_domain)
		lockdep_set_class(&adev->reset_domain->sem,
				  &amdgpu_reset_sem_key);
}

/**
 * amdgpu_lockdep_init - Teach lockdep the correct lock ordering
 *
 * Instantiates dummy objects and takes locks in the correct order to
 * train lockdep. This helps catch lock ordering violations during
 * development.
 *
 * Lock ordering hierarchy (outermost to innermost):
 *
 * 1. userq_sch_mutex     - Global userq scheduler (enforce_isolation)
 * 2. userq_mutex         - Per-context userq (held across queue create/destroy)
 * 3. notifier_lock       - MMU notifier lock
 * 4. vram_lock           - VRAM allocator lock
 * 5. reset_domain->sem   - GPU reset synchronization
 * 6. reset_lock          - Reset control lock
 * 7. srbm_mutex          - SRBM register access
 * 8. grbm_idx_mutex      - GRBM index access
 * 9. mmio_idx_lock       - MMIO index access (spinlock)
 *
 * Evidence:
 * - userq_sch_mutex -> userq_mutex: amdgpu_gfx_kfd_sch_ctrl() calls
 *   amdgpu_userq_stop_sched_for_enforce_isolation() which takes userq_mutex
 * - userq_mutex -> notifier_lock: userq paths may trigger MMU notifier
 *   invalidation which acquires notifier_lock
 * - notifier_lock -> reset_domain->sem: HMM invalidation callback holds
 *   notifier_lock and can wait for GPU reset completion, so notifier_lock
 *   must be outer to reset_domain->sem
 * - vram_lock -> reset_domain->sem: VRAM management paths may need to
 *   wait for ongoing reset to complete
 *
 * Note: mmap_lock ordering relative to GPU locks is already taught
 * by dma-resv (drivers/dma-buf/dma-resv.c).
 */
int amdgpu_lockdep_init(void)
{
	struct amdgpu_reset_domain *reset_domain = NULL;
	struct amdgpu_reset_control reset_ctl;
	struct mutex userq_sch_mutex;
	struct mutex userq_mutex;
	struct mutex notifier_lock;

Annotation

Implementation Notes