An AMDGPU BO is described jointly by four layers of structures. The lower two layers, drm_gem_object and ttm_buffer_object, are provided by the DRM core and by the TTM memory manager respectively, and are shared by every DRM driver built on TTM. The GEM layer gives a buffer its identity: reference count, user-space handles, mmap offset, synchronization object and export to other devices. The TTM layer manages the storage behind the buffer: where it is placed, which pages back it, and how it is moved, evicted and destroyed. This article analyzes how these two layers are embedded in amdgpu_bo, the members and callbacks through which each layer fulfills its responsibilities, the cooperation of the two reference counts during creation and release, and the use of both layers in the KFD allocation path.

1 Embedding Relationship

amdgpu_bo embeds ttm_buffer_object as member tbo, and ttm_buffer_object embeds drm_gem_object as its first member base. Figure 1 shows the nesting of the three structures and the external objects referenced by each layer.

Figure 1 Embedding of the GEM and TTM layers in amdgpu_bo

Figure 1 Embedding of the GEM and TTM layers in amdgpu_bo

Structure embedding implements single inheritance in C. The three layers occupy one contiguous allocation made by a single kvzalloc() in amdgpu_bo_create(), and a pointer to any layer can be converted to the enclosing layer without an additional pointer member.

FromToMethod
amdgpu_bottm_buffer_object&bo->tbo
amdgpu_bodrm_gem_object&bo->tbo.base
ttm_buffer_objectamdgpu_bottm_to_amdgpu_bo(), i.e. container_of(tbo, struct amdgpu_bo, tbo)
drm_gem_objectamdgpu_bogem_to_amdgpu_bo(), i.e. container_of(gobj, struct amdgpu_bo, tbo.base)

Because base is the first member of ttm_buffer_object, a GEM pointer and the corresponding TTM pointer hold the same address. tbo is not the first member of amdgpu_bo, since preferred_domains, allowed_domains and the placement array precede it, so conversions to amdgpu_bo always use container_of. The allocation size is taken from bp->bo_ptr_size, which allows amdgpu_bo itself to be embedded in a larger structure such as amdgpu_bo_user or amdgpu_bo_vm. BOs created through amdgpu_gem_object_create(), including all KFD BOs, use the plain amdgpu_bo.

The direction of dependency follows the embedding. The GEM layer is unaware of TTM, and TTM is unaware of AMDGPU; each lower layer reaches the upper layers only through callback tables installed by the driver. drm_gem_object.funcs points to amdgpu_gem_object_funcs, and ttm_buffer_object.bdev->funcs points to amdgpu_bo_driver. These two tables are the points at which AMDGPU policy enters the generic frameworks.

2 Responsibilities of the GEM Layer

2.1 Identity and External References

drm_gem_object records the size of the buffer in size and holds the reference count refcount. Every long-lived holder of the BO owns one GEM reference: each user-space handle in a drm_file (counted additionally in handle_count), each dma-buf exported from the BO, and each kernel user that takes a reference through amdgpu_bo_ref(). amdgpu_bo_ref() and amdgpu_bo_unref() are implemented as drm_gem_object_get() and drm_gem_object_put() on tbo.base, so the AMDGPU driver uses the GEM count as the external lifetime of a BO.

The remaining identity members serve sharing. name is the global name created by the legacy flink interface. dma_buf caches the dma-buf exported from this object, and import_attach records the attachment when the object was imported from another device.

2.2 mmap Offset

User space maps a BO into the CPU address space by calling mmap on a DRM device file with a fake offset that identifies the BO. vma_node is the node of the BO in the device’s drm_vma_offset_manager, and the offset range it occupies is allocated by ttm_bo_init_reserved() for BOs of type ttm_bo_type_device and ttm_bo_type_sg. amdgpu_bo_mmap_offset() returns this offset through drm_vma_node_offset_addr().

The offset alone does not grant access. vma_node also maintains a list of drm_file instances that are allowed to map the BO, and drm_vma_node_allow() adds a file to this list. An mmap call through a file that is not on the list is rejected. When the mapping is established, amdgpu_gem_object_mmap() refuses Userptr BOs and BOs created with AMDGPU_GEM_CREATE_NO_CPU_ACCESS, and then hands the VMA to drm_gem_ttm_mmap(). Page faults on the VMA are handled by amdgpu_gem_fault() in amdgpu_gem_vm_ops, which fills the CPU page tables according to the current TTM placement of the BO.

2.3 Reservation Object

resv points to a dma_resv. By default it points to _resv embedded in the same object; a caller can instead pass a shared dma_resv at creation time so that a group of BOs uses one lock and one fence set. The reservation object has two roles. It is a fence container: every GPU operation that reads or writes the BO adds its fence with a usage class, and waiters use these fences to determine when the BO is idle. It is also the lock that TTM calls the reservation: the members resource, ttm, pin_count and the LRU state of ttm_buffer_object are protected by holding resv. The GEM layer defines the object, and the TTM layer defines the locking rules built upon it.

2.4 GEM Callback Table

amdgpu_bo_create() sets tbo.base.funcs to amdgpu_gem_object_funcs. The table connects generic GEM operations to AMDGPU implementations.

CallbackAMDGPU implementationFunction
freeamdgpu_gem_object_free()Invoked when the GEM count reaches zero; drops the TTM reference
open / closeamdgpu_gem_object_open() / amdgpu_gem_object_close()Create or remove the amdgpu_bo_va of the BO in the VM of a render node file when a handle is opened or closed
exportamdgpu_gem_prime_export()Export the BO as a dma-buf
vmap / vunmapdrm_gem_ttm_vmap() / drm_gem_ttm_vunmap()Kernel virtual mapping through TTM
mmapamdgpu_gem_object_mmap()Validate and establish a user-space mapping
vm_opsamdgpu_gem_vm_opsPage fault, VMA open, close and access handlers

3 Responsibilities of the TTM Layer

3.1 Device and Object Type

bdev points to the TTM device adev->mman.bdev of the GPU, which holds the resource managers of all memory types and the driver callback table. type takes one of three values, set at creation and constant afterwards.

Typemmap offsetMeaningKFD usage
ttm_bo_type_deviceYesOrdinary buffer that user space may mapVRAM, GTT and Userptr BOs
ttm_bo_type_kernelNoBuffer used only inside the kernelNot used for KFD user allocations
ttm_bo_type_sgYesBuffer whose pages come from an external sg_tableDoorbell and MMIO BOs

page_alignment is the required alignment of the placement, and destroy is the function that frees the enclosing structure once TTM has released the storage. For AMDGPU it is amdgpu_bo_destroy() or a variant for derived types.

3.2 Placement: resource

resource points to a ttm_resource that describes the current placement. mem_type identifies the memory type, start is the position within that memory type in pages, size is the size in bytes, and bus describes how the CPU reaches the placement through an I/O aperture such as the VRAM BAR. AMDGPU memory domains map to TTM memory types in amdgpu_bo_placement_from_domain().

AMDGPU domainTTM memory typePhysical storage
AMDGPU_GEM_DOMAIN_VRAMTTM_PL_VRAMLocal VRAM
AMDGPU_GEM_DOMAIN_GTTTTM_PL_TT, or AMDGPU_PL_PREEMPT for BOs created with AMDGPU_GEM_CREATE_PREEMPTIBLESystem memory accessible to the GPU
AMDGPU_GEM_DOMAIN_CPUTTM_PL_SYSTEMSystem memory not mapped for the GPU
AMDGPU_GEM_DOMAIN_DOORBELLAMDGPU_PL_DOORBELLDoorbell aperture

amdgpu_bo_placement_from_domain() fills amdgpu_bo.placements[] with one ttm_place per allowed memory type, and ttm_bo_validate() uses this list to select a memory type and obtain a ttm_resource from its resource manager, evicting other BOs when the preferred memory type is full. The KFD allocation path creates Userptr, Doorbell and MMIO BOs with AMDGPU_GEM_CREATE_PREEMPTIBLE, so these BOs reside in AMDGPU_PL_PREEMPT rather than TTM_PL_TT when they are placed in GTT.

3.3 System Memory Backing: ttm and sg

ttm points to a ttm_tt that records the system memory pages backing the BO. pages[] holds the struct page pointers, dma_address[] holds the DMA address of each page as seen by the GPU, and sg holds an external sg_table when the pages are not owned by TTM. AMDGPU creates the object through the ttm_tt_create callback as amdgpu_ttm_tt, which embeds ttm_tt and adds the Userptr fields userptr and usertask together with GART binding state. A BO placed in VRAM does not use ttm_tt to hold its storage.

ttm_buffer_object.sg holds the external sg_table of ttm_bo_type_sg BOs. For KFD Doorbell and MMIO BOs, the table describes a region in a device BAR and is assigned to both tbo.sg and tbo.ttm->sg after creation.

3.4 Runtime State

pin_count prevents eviction and migration while it is non-zero. priority and bulk_move determine the position of the BO on the LRU lists that TTM scans when it searches for eviction candidates. deleted and delayed_delete support deferred destruction, described in section 4. kref is the reference count of the TTM layer.

3.5 TTM Driver Callbacks

The TTM device of AMDGPU is initialized with amdgpu_bo_driver, whose callbacks implement the memory operations that the generic layer cannot perform alone.

CallbackAMDGPU implementationFunction
ttm_tt_create / ttm_tt_populate / ttm_tt_unpopulate / ttm_tt_destroyamdgpu_ttm_tt_*, amdgpu_ttm_backend_destroy()Create the amdgpu_ttm_tt, allocate or obtain its pages, and release them
eviction_valuable / evict_flagsamdgpu_ttm_bo_eviction_valuable() / amdgpu_evict_flags()Decide whether a BO may be evicted and where it should go
moveamdgpu_bo_move()Move a BO between memory types, using the SDMA engine for copies
delete_mem_notify / release_notifyamdgpu_bo_delete_mem_notify() / amdgpu_bo_release_notify()Notify the driver before storage is freed and when a BO is released
io_mem_reserve / io_mem_pfnamdgpu_ttm_io_mem_reserve() / amdgpu_ttm_io_mem_pfn()Provide the bus address and page frame numbers for CPU access to VRAM and doorbells
access_memoryamdgpu_ttm_access_memory()Access BO contents for debuggers

4 Two Reference Counts and the BO Lifetime

The GEM layer and the TTM layer each hold a reference count: drm_gem_object.refcount and ttm_buffer_object.kref. Figure 2 shows how the two counts are initialized during creation and consumed during release.

Figure 2 Creation and release of amdgpu_bo across the GEM and TTM layers

Figure 2 Creation and release of amdgpu_bo across the GEM and TTM layers

During creation, amdgpu_bo_create() allocates the structure and calls drm_gem_private_object_init(), which sets the GEM count to one, initializes _resv and resets vma_node. After installing the GEM callback table and computing the placement from the requested domain, it calls ttm_bo_init_reserved(). This function sets the TTM count to one, records bdev, type and destroy, selects the external or embedded reservation object, allocates the mmap offset for device and sg BOs, locks the reservation object and calls ttm_bo_validate() to allocate the initial ttm_resource. amdgpu_bo_create() then performs AMDGPU-specific initialization such as VRAM clearing and unlocks the reservation object.

The two counts have different holders. The GEM count is held by external users: handles, dma-bufs and driver code. The TTM count is held by TTM itself. TTM functions that operate on BOs found on LRU lists, such as eviction and swapout, take a temporary reference with ttm_bo_get_unless_zero(), which fails for an object whose count has already reached zero; the plain ttm_bo_get() is internal to TTM. In normal operation the TTM count stays at one, and the release of the last GEM reference drives the release of the TTM reference.

During release, drm_gem_object_put() drops a GEM reference. When the count reaches zero, the GEM core calls amdgpu_gem_object_free(), which unregisters the HMM notifier of Userptr BOs and calls ttm_bo_fini(), dropping the TTM reference through ttm_bo_put(). When the TTM count reaches zero, ttm_bo_release() calls the driver’s release_notify callback, removes the mmap offset and examines the fences in the reservation object. For KFD BOs, amdgpu_bo_release_notify() removes the eviction fences of the process and, for VRAM allocated with AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE, clears the VRAM before it is returned.

If all fences have signaled, ttm_bo_release() frees the ttm_tt and the ttm_resource through ttm_bo_cleanup_memtype_use() and then calls bo->destroy. If the GPU is still using the BO, the release cannot complete immediately. TTM then sets deleted, reinitializes kref to one and queues delayed_delete on a work queue; the worker waits for the fences and completes the same cleanup. The BO therefore outlives its last external reference for as long as the hardware needs it, and this deferral is invisible to the GEM layer, whose count has already reached zero. Finally, amdgpu_bo_destroy() calls drm_gem_object_release() to release the GEM-side resources, including the embedded _resv, and frees the memory with kvfree().

5 The GEM and TTM Layers in the KFD Allocation Path

amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu() creates the BO for a KFD allocation and connects it to kgd_mem. The steps that act on the GEM and TTM layers are listed below.

StepCallLayer
Create the BOamdgpu_gem_object_create(adev, size, 1, alloc_domain, alloc_flags, bo_type, NULL, &gobj, xcp_id + 1)GEM and TTM
Authorize CPU mappingdrm_vma_node_allow(&gobj->vma_node, drm_priv)GEM
Create a GEM handledrm_gem_handle_create(adev->kfd.client.file, gobj, &mem->gem_handle)GEM
Attach the external sg tablebo->tbo.sg = sg; bo->tbo.ttm->sg = sg (Doorbell and MMIO only)TTM
Link to kgd_membo->kfd_bo = mem; mem->bo = boAMDGPU and KFD
Return the mmap offsetamdgpu_bo_mmap_offset(bo)GEM

bo_type is ttm_bo_type_device except for Doorbell and MMIO BOs, which use ttm_bo_type_sg. The reservation argument is NULL, so every KFD BO uses its own embedded _resv. drm_priv is the render node file associated with the process by AMDKFD_IOC_ACQUIRE_VM, so the returned mmap offset is valid only for mmap on that render node.

The GEM handle is not created in a user-space file. amdgpu registers an in-kernel DRM client named kfd for each device, and KFD creates the handle in the file of this client. The handle keeps a GEM reference for the lifetime of the allocation and is the input of drm_gem_prime_handle_to_dmabuf() when KFD exports the BO as a dma-buf, for example to establish a KFD_MEM_ATT_DMABUF attachment on another GPU. amdgpu_amdkfd_gpuvm_free_memory_of_gpu() deletes the handle with drm_gem_handle_delete(), and the release of this reference starts the release sequence of section 4 once no other holder remains.

The GEM and TTM layers thus provide the generic object model and memory management of a BO: identity, sharing, synchronization, placement, backing pages and deferred destruction. The AMDGPU-specific policy, including the preferred and allowed domains, the creation flags, the association with GPU virtual memory and the link to kgd_mem, resides in the members of amdgpu_bo outside tbo.