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.

Dependency Surface

Detected Declarations

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

Implementation Notes