drivers/gpu/drm/drm_file.c
Source file repositories/reference/linux-study-clean/drivers/gpu/drm/drm_file.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/gpu/drm/drm_file.c- Extension
.c- Size
- 31452 bytes
- Lines
- 1082
- Domain
- Driver Families
- Bucket
- drivers/gpu
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern 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.
- 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.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/anon_inodes.hlinux/dma-fence.hlinux/export.hlinux/file.hlinux/module.hlinux/pci.hlinux/poll.hlinux/slab.hlinux/vga_switcheroo.hdrm/drm_client_event.hdrm/drm_drv.hdrm/drm_file.hdrm/drm_gem.hdrm/drm_print.hdrm/drm_debugfs.hdrm_crtc_internal.hdrm_internal.h
Detected Declarations
function drm_dev_needs_global_mutexfunction DEFINE_DRM_GEM_FOPSfunction drm_events_releasefunction drm_file_allocfunction drm_close_helperfunction drm_cpu_validfunction drm_open_helperfunction drm_openfunction drm_lastclosefunction drm_releasefunction drm_file_update_pidfunction drm_release_noglobalfunction drm_pollfunction drm_readfunction drm_event_cancel_freefunction drm_event_cancel_freefunction drm_event_cancel_freefunction drm_send_event_helperfunction drm_send_event_timestamp_lockedfunction drm_send_event_lockedfunction drm_send_eventfunction drm_fdinfo_print_sizefunction drm_memory_stats_is_zerofunction drm_print_memory_statsfunction drm_show_memory_statsfunction drm_show_fdinfofunction drm_file_errexport drm_openexport drm_releaseexport drm_release_noglobalexport drm_readexport drm_pollexport drm_event_reserve_init_lockedexport drm_event_reserve_initexport drm_event_cancel_freeexport drm_send_event_timestamp_lockedexport drm_send_event_lockedexport drm_send_eventexport drm_fdinfo_print_sizeexport drm_memory_stats_is_zeroexport drm_print_memory_statsexport drm_show_memory_statsexport drm_show_fdinfoexport drm_file_err
Annotated Snippet
* implemented in the DRM core. The resulting &struct file_operations must be
* stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
* drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
* Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
* need to sprinkle #ifdef into the code. Drivers which implement private ioctls
* that require 32/64 bit compatibility support must provide their own
* &file_operations.compat_ioctl handler that processes private ioctls and calls
* drm_compat_ioctl() for core ioctls.
*
* In addition drm_read() and drm_poll() provide support for DRM events. DRM
* events are a generic and extensible means to send asynchronous events to
* userspace through the file descriptor. They are used to send vblank event and
* page flip completions by the KMS API. But drivers can also use it for their
* own needs, e.g. to signal completion of rendering.
*
* For the driver-side event interface see drm_event_reserve_init() and
* drm_send_event() as the main starting points.
*
* The memory mapping implementation will vary depending on how the driver
* manages memory. For GEM-based drivers this is drm_gem_mmap().
*
* No other file operations are supported by the DRM userspace API. Overall the
* following is an example &file_operations structure::
*
* static const example_drm_fops = {
* .owner = THIS_MODULE,
* .open = drm_open,
* .release = drm_release,
* .unlocked_ioctl = drm_ioctl,
* .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
* .poll = drm_poll,
* .read = drm_read,
* .mmap = drm_gem_mmap,
* };
*
* For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
* DMA based drivers there is the DEFINE_DRM_GEM_DMA_FOPS() macro to make this
* simpler.
*
* The driver's &file_operations must be stored in &drm_driver.fops.
*
* For driver-private IOCTL handling see the more detailed discussion in
* :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
*/
/**
* drm_file_alloc - allocate file context
* @minor: minor to allocate on
*
* This allocates a new DRM file context. It is not linked into any context and
* can be used by the caller freely. Note that the context keeps a pointer to
* @minor, so it must be freed before @minor is.
*
* RETURNS:
* Pointer to newly allocated context, ERR_PTR on failure.
*/
struct drm_file *drm_file_alloc(struct drm_minor *minor)
{
static atomic64_t ident = ATOMIC64_INIT(0);
struct drm_device *dev = minor->dev;
struct drm_file *file;
int ret;
file = kzalloc_obj(*file);
if (!file)
return ERR_PTR(-ENOMEM);
/* Get a unique identifier for fdinfo: */
file->client_id = atomic64_inc_return(&ident);
rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
file->minor = minor;
/* for compatibility root is always authenticated */
file->authenticated = capable(CAP_SYS_ADMIN);
INIT_LIST_HEAD(&file->lhead);
INIT_LIST_HEAD(&file->fbs);
mutex_init(&file->fbs_lock);
INIT_LIST_HEAD(&file->blobs);
INIT_LIST_HEAD(&file->pending_event_list);
INIT_LIST_HEAD(&file->event_list);
init_waitqueue_head(&file->event_wait);
file->event_space = 4096; /* set aside 4k for event buffer */
spin_lock_init(&file->master_lookup_lock);
mutex_init(&file->event_read_lock);
mutex_init(&file->client_name_lock);
if (drm_core_check_feature(dev, DRIVER_GEM))
drm_gem_open(dev, file);
Annotation
- Immediate include surface: `linux/anon_inodes.h`, `linux/dma-fence.h`, `linux/export.h`, `linux/file.h`, `linux/module.h`, `linux/pci.h`, `linux/poll.h`, `linux/slab.h`.
- Detected declarations: `function drm_dev_needs_global_mutex`, `function DEFINE_DRM_GEM_FOPS`, `function drm_events_release`, `function drm_file_alloc`, `function drm_close_helper`, `function drm_cpu_valid`, `function drm_open_helper`, `function drm_open`, `function drm_lastclose`, `function drm_release`.
- Atlas domain: Driver Families / drivers/gpu.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.