drivers/tee/tee_shm.c
Source file repositories/reference/linux-study-clean/drivers/tee/tee_shm.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/tee/tee_shm.c- Extension
.c- Size
- 17747 bytes
- Lines
- 720
- Domain
- Driver Families
- Bucket
- drivers/tee
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- 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/device.hlinux/dma-buf.hlinux/dma-mapping.hlinux/highmem.hlinux/idr.hlinux/io.hlinux/mm.hlinux/sched.hlinux/slab.hlinux/tee_core.hlinux/uaccess.hlinux/uio.htee_private.h
Detected Declarations
struct tee_shm_dma_memfunction release_registered_pagesfunction tee_shm_releasefunction tee_shm_alloc_user_buffunction tee_shm_alloc_kernel_buffunction tee_shm_alloc_priv_buffunction tee_shm_alloc_dma_memfunction tee_dyn_shm_alloc_helperfunction tee_dyn_shm_free_helperfunction register_shm_helperfunction tee_shm_register_user_buffunction tee_shm_register_kernel_buffunction tee_shm_fop_releasefunction tee_shm_fop_mmapfunction tee_shm_get_fdfunction tee_shm_freefunction tee_shm_get_vafunction tee_shm_get_pafunction tee_shm_get_from_idfunction tee_shm_putexport tee_shm_alloc_kernel_bufexport tee_shm_register_fdexport tee_shm_alloc_priv_bufexport tee_shm_alloc_dma_memexport tee_shm_alloc_dma_memexport tee_dyn_shm_alloc_helperexport tee_dyn_shm_free_helperexport tee_shm_register_kernel_bufexport tee_shm_freeexport tee_shm_get_vaexport tee_shm_get_paexport tee_shm_get_from_idexport tee_shm_put
Annotated Snippet
static const struct file_operations tee_shm_fops = {
.owner = THIS_MODULE,
.release = tee_shm_fop_release,
.mmap = tee_shm_fop_mmap,
};
/**
* tee_shm_get_fd() - Increase reference count and return file descriptor
* @shm: Shared memory handle
* @returns user space file descriptor to shared memory
*/
int tee_shm_get_fd(struct tee_shm *shm)
{
int fd;
if (shm->id < 0)
return -EINVAL;
/* matched by tee_shm_put() in tee_shm_op_release() */
refcount_inc(&shm->refcount);
fd = anon_inode_getfd("tee_shm", &tee_shm_fops, shm, O_RDWR);
if (fd < 0)
tee_shm_put(shm);
return fd;
}
/**
* tee_shm_free() - Free shared memory
* @shm: Handle to shared memory to free
*/
void tee_shm_free(struct tee_shm *shm)
{
tee_shm_put(shm);
}
EXPORT_SYMBOL_GPL(tee_shm_free);
/**
* tee_shm_get_va() - Get virtual address of a shared memory plus an offset
* @shm: Shared memory handle
* @offs: Offset from start of this shared memory
* @returns virtual address of the shared memory + offs if offs is within
* the bounds of this shared memory, else an ERR_PTR
*/
void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
{
if (!shm->kaddr)
return ERR_PTR(-EINVAL);
if (offs >= shm->size)
return ERR_PTR(-EINVAL);
return (char *)shm->kaddr + offs;
}
EXPORT_SYMBOL_GPL(tee_shm_get_va);
/**
* tee_shm_get_pa() - Get physical address of a shared memory plus an offset
* @shm: Shared memory handle
* @offs: Offset from start of this shared memory
* @pa: Physical address to return
* @returns 0 if offs is within the bounds of this shared memory, else an
* error code.
*/
int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
{
if (offs >= shm->size)
return -EINVAL;
if (pa)
*pa = shm->paddr + offs;
return 0;
}
EXPORT_SYMBOL_GPL(tee_shm_get_pa);
/**
* tee_shm_get_from_id() - Find shared memory object and increase reference
* count
* @ctx: Context owning the shared memory
* @id: Id of shared memory object
* @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
*/
struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
{
struct tee_device *teedev;
struct tee_shm *shm;
if (!ctx)
return ERR_PTR(-EINVAL);
teedev = ctx->teedev;
mutex_lock(&teedev->mutex);
shm = idr_find(&teedev->idr, id);
/*
Annotation
- Immediate include surface: `linux/anon_inodes.h`, `linux/device.h`, `linux/dma-buf.h`, `linux/dma-mapping.h`, `linux/highmem.h`, `linux/idr.h`, `linux/io.h`, `linux/mm.h`.
- Detected declarations: `struct tee_shm_dma_mem`, `function release_registered_pages`, `function tee_shm_release`, `function tee_shm_alloc_user_buf`, `function tee_shm_alloc_kernel_buf`, `function tee_shm_alloc_priv_buf`, `function tee_shm_alloc_dma_mem`, `function tee_dyn_shm_alloc_helper`, `function tee_dyn_shm_free_helper`, `function register_shm_helper`.
- Atlas domain: Driver Families / drivers/tee.
- Implementation status: pattern implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
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.