linux/include/drm/drm_suballoc.h
Satyanarayana K V P 16843e6638 drm/sa: Split drm_suballoc_new() into SA alloc and init helpers
drm_suballoc_new() currently both allocates the SA object using kmalloc()
and searches for a suitable hole in the sub-allocator for the requested
size. If SA allocation is done by holding sub-allocator mutex, this design
can lead to reclaim safety issues.

By splitting the kmalloc() step outside of the critical section, we allow
the memory allocation to use GFP_KERNEL (reclaim-safe) while ensuring that
the initialization step that holds reclaim-tainted locks (sub-allocator
mutex) operates in a reclaim-unsafe context with pre-allocated memory.

This separation prevents potential deadlocks where memory reclaim could
attempt to acquire locks that are already held during the sub-allocator
operations.

Signed-off-by: Satyanarayana K V P <satyanarayana.k.v.p@intel.com>
Suggested-by: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: Maarten Lankhorst <dev@lankhorst.se>
Reviewed-by: Christian König <christian.koenig@amd.com>
Reviewed-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Reviewed-by: Matthew Brost <matthew.brost@intel.com>
Acked-by: Maarten Lankhorst <dev@lankhorst.se>
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
Link: https://patch.msgid.link/20260220055519.2485681-6-satyanarayana.k.v.p@intel.com
2026-02-20 10:54:02 -08:00

115 lines
2.9 KiB
C

/* SPDX-License-Identifier: GPL-2.0 OR MIT */
/*
* Copyright 2011 Red Hat Inc.
* Copyright © 2022 Intel Corporation
*/
#ifndef _DRM_SUBALLOC_H_
#define _DRM_SUBALLOC_H_
#include <drm/drm_mm.h>
#include <linux/dma-fence.h>
#include <linux/types.h>
#define DRM_SUBALLOC_MAX_QUEUES 32
/**
* struct drm_suballoc_manager - fenced range allocations
* @wq: Wait queue for sleeping allocations on contention.
* @hole: Pointer to first hole node.
* @olist: List of allocated ranges.
* @flist: Array[fence context hash] of queues of fenced allocated ranges.
* @size: Size of the managed range.
* @align: Default alignment for the managed range.
*/
struct drm_suballoc_manager {
wait_queue_head_t wq;
struct list_head *hole;
struct list_head olist;
struct list_head flist[DRM_SUBALLOC_MAX_QUEUES];
size_t size;
size_t align;
};
/**
* struct drm_suballoc - Sub-allocated range
* @olist: List link for list of allocated ranges.
* @flist: List linkk for the manager fenced allocated ranges queues.
* @manager: The drm_suballoc_manager.
* @soffset: Start offset.
* @eoffset: End offset + 1 so that @eoffset - @soffset = size.
* @fence: The fence protecting the allocation.
*/
struct drm_suballoc {
struct list_head olist;
struct list_head flist;
struct drm_suballoc_manager *manager;
size_t soffset;
size_t eoffset;
struct dma_fence *fence;
};
void drm_suballoc_manager_init(struct drm_suballoc_manager *sa_manager,
size_t size, size_t align);
void drm_suballoc_manager_fini(struct drm_suballoc_manager *sa_manager);
struct drm_suballoc *drm_suballoc_alloc(gfp_t gfp);
int drm_suballoc_insert(struct drm_suballoc_manager *sa_manager,
struct drm_suballoc *sa, size_t size, bool intr,
size_t align);
struct drm_suballoc *
drm_suballoc_new(struct drm_suballoc_manager *sa_manager, size_t size,
gfp_t gfp, bool intr, size_t align);
void drm_suballoc_free(struct drm_suballoc *sa, struct dma_fence *fence);
/**
* drm_suballoc_soffset - Range start.
* @sa: The struct drm_suballoc.
*
* Return: The start of the allocated range.
*/
static inline size_t drm_suballoc_soffset(struct drm_suballoc *sa)
{
return sa->soffset;
}
/**
* drm_suballoc_eoffset - Range end.
* @sa: The struct drm_suballoc.
*
* Return: The end of the allocated range + 1.
*/
static inline size_t drm_suballoc_eoffset(struct drm_suballoc *sa)
{
return sa->eoffset;
}
/**
* drm_suballoc_size - Range size.
* @sa: The struct drm_suballoc.
*
* Return: The size of the allocated range.
*/
static inline size_t drm_suballoc_size(struct drm_suballoc *sa)
{
return sa->eoffset - sa->soffset;
}
#ifdef CONFIG_DEBUG_FS
void drm_suballoc_dump_debug_info(struct drm_suballoc_manager *sa_manager,
struct drm_printer *p,
unsigned long long suballoc_base);
#else
static inline void
drm_suballoc_dump_debug_info(struct drm_suballoc_manager *sa_manager,
struct drm_printer *p,
unsigned long long suballoc_base)
{ }
#endif
#endif /* _DRM_SUBALLOC_H_ */