include/linux/gpu_buddy.h
Source file repositories/reference/linux-study-clean/include/linux/gpu_buddy.h
File Facts
- System
- Linux kernel
- Corpus path
include/linux/gpu_buddy.h- Extension
.h- Size
- 8746 bytes
- Lines
- 283
- Domain
- Core OS
- Bucket
- Core Kernel Interface
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bitops.hlinux/list.hlinux/slab.hlinux/sched.hlinux/rbtree.hlinux/rbtree_augmented.h
Detected Declarations
struct gpu_buddy_blockstruct gpu_buddyenum gpu_buddy_free_treefunction gpu_buddy_driver_lock_heldfunction gpu_buddy_driver_lock_heldfunction gpu_buddy_block_orderfunction gpu_buddy_block_is_freefunction gpu_buddy_block_is_clearfunction gpu_buddy_block_size
Annotated Snippet
struct gpu_buddy_block {
/* private: */
/*
* Header bit layout:
* - Bits 63:12: block offset within the address space
* - Bits 11:10: state (ALLOCATED, FREE, or SPLIT)
* - Bit 9: clear bit (1 if memory is zeroed)
* - Bits 8:6: reserved
* - Bits 5:0: order (log2 of size relative to chunk_size)
*/
#define GPU_BUDDY_HEADER_OFFSET GENMASK_ULL(63, 12)
#define GPU_BUDDY_HEADER_STATE GENMASK_ULL(11, 10)
#define GPU_BUDDY_ALLOCATED (1 << 10)
#define GPU_BUDDY_FREE (2 << 10)
#define GPU_BUDDY_SPLIT (3 << 10)
#define GPU_BUDDY_HEADER_CLEAR GENMASK_ULL(9, 9)
/* Free to be used, if needed in the future */
#define GPU_BUDDY_HEADER_UNUSED GENMASK_ULL(8, 6)
#define GPU_BUDDY_HEADER_ORDER GENMASK_ULL(5, 0)
u64 header;
struct gpu_buddy_block *left;
struct gpu_buddy_block *right;
struct gpu_buddy_block *parent;
/* public: */
void *private; /* owned by creator */
/*
* While the block is allocated by the user through gpu_buddy_alloc*,
* the user has ownership of the link, for example to maintain within
* a list, if so desired. As soon as the block is freed with
* gpu_buddy_free* ownership is given back to the mm.
*/
union {
/* private: */
struct rb_node rb;
/* public: */
struct list_head link;
};
/* private: */
struct list_head tmp_link;
unsigned int subtree_max_alignment;
};
/* Order-zero must be at least SZ_4K */
#define GPU_BUDDY_MAX_ORDER (63 - 12)
/**
* struct gpu_buddy - GPU binary buddy allocator
*
* The buddy allocator provides efficient power-of-two memory allocation
* with fast allocation and free operations. It is commonly used for GPU
* memory management where allocations can be split into power-of-two
* block sizes.
*
* Locking should be handled by the user; a simple mutex around
* gpu_buddy_alloc_blocks() and gpu_buddy_free_block()/gpu_buddy_free_list()
* should suffice.
*
* @n_roots: Number of root blocks in the roots array.
* @max_order: Maximum block order (log2 of largest block size / chunk_size).
* @chunk_size: Minimum allocation granularity in bytes. Must be at least SZ_4K.
* @size: Total size of the address space managed by this allocator in bytes.
* @avail: Total free space currently available for allocation in bytes.
* @clear_avail: Free space available in the clear tree (zeroed memory) in bytes.
* This is a subset of @avail.
* @lock_dep_map: Annotates gpu_buddy API with a driver provided lock.
*/
struct gpu_buddy {
/* private: */
/*
* Array of red-black trees for free block management.
* Indexed as free_trees[clear/dirty][order] where:
* - Index 0 (GPU_BUDDY_CLEAR_TREE): blocks with zeroed content
* - Index 1 (GPU_BUDDY_DIRTY_TREE): blocks with unknown content
* Each tree holds free blocks of the corresponding order.
*/
struct rb_root **free_trees;
/*
* Array of root blocks representing the top-level blocks of the
* binary tree(s). Multiple roots exist when the total size is not
* a power of two, with each root being the largest power-of-two
* that fits in the remaining space.
*/
struct gpu_buddy_block **roots;
/* public: */
unsigned int n_roots;
unsigned int max_order;
u64 chunk_size;
u64 size;
Annotation
- Immediate include surface: `linux/bitops.h`, `linux/list.h`, `linux/slab.h`, `linux/sched.h`, `linux/rbtree.h`, `linux/rbtree_augmented.h`.
- Detected declarations: `struct gpu_buddy_block`, `struct gpu_buddy`, `enum gpu_buddy_free_tree`, `function gpu_buddy_driver_lock_held`, `function gpu_buddy_driver_lock_held`, `function gpu_buddy_block_order`, `function gpu_buddy_block_is_free`, `function gpu_buddy_block_is_clear`, `function gpu_buddy_block_size`.
- Atlas domain: Core OS / Core Kernel Interface.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.