drivers/android/binder_alloc.c
Source file repositories/reference/linux-study-clean/drivers/android/binder_alloc.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/android/binder_alloc.c- Extension
.c- Size
- 38061 bytes
- Lines
- 1410
- Domain
- Driver Families
- Bucket
- drivers/android
- Inferred role
- Driver Families: exported/initcall integration point
- Status
- integration 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.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- 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/list.hlinux/sched/mm.hlinux/module.hlinux/rtmutex.hlinux/rbtree.hlinux/seq_file.hlinux/vmalloc.hlinux/slab.hlinux/sched.hlinux/list_lru.hlinux/ratelimit.hasm/cacheflush.hlinux/uaccess.hlinux/highmem.hlinux/sizes.hkunit/visibility.hbinder_alloc.hbinder_trace.h
Detected Declarations
function binder_alloc_buffer_sizefunction binder_insert_free_bufferfunction binder_insert_allocated_buffer_lockedfunction binder_alloc_prepare_to_freefunction binder_set_installed_pagefunction binder_get_installed_pagefunction binder_lru_freelist_addfunction binder_alloc_set_mappedfunction binder_alloc_is_mappedfunction binder_page_insertfunction binder_free_pagefunction binder_install_single_pagefunction binder_install_buffer_pagesfunction binder_lru_freelist_delfunction debug_no_space_lockedfunction debug_low_async_space_lockedfunction leftfunction spacefunction sanitized_sizefunction binder_alloc_new_buffunction buffer_start_pagefunction prev_buffer_end_pagefunction binder_delete_free_bufferfunction binder_free_buf_lockedfunction binder_alloc_get_pagefunction binder_alloc_clear_buffunction binder_alloc_free_buffunction binder_alloc_mmap_handlerfunction binder_alloc_deferred_releasefunction binder_alloc_print_allocatedfunction binder_alloc_print_pagesfunction binder_alloc_get_allocated_countfunction binder_alloc_vma_closefunction binder_alloc_free_pagefunction binder_shrink_countfunction binder_shrink_scanfunction __binder_alloc_initfunction binder_alloc_initfunction binder_alloc_shrinker_initfunction binder_alloc_shrinker_exitfunction check_bufferfunction binder_alloc_copy_user_to_bufferfunction binder_alloc_do_buffer_copyfunction binder_alloc_copy_to_bufferfunction binder_alloc_copy_from_buffer
Annotated Snippet
if (user_ptr < buffer->user_data) {
n = n->rb_left;
} else if (user_ptr > buffer->user_data) {
n = n->rb_right;
} else {
/*
* Guard against user threads attempting to
* free the buffer when in use by kernel or
* after it's already been freed.
*/
if (!buffer->allow_user_free)
return ERR_PTR(-EPERM);
buffer->allow_user_free = 0;
return buffer;
}
}
return NULL;
}
/**
* binder_alloc_prepare_to_free() - get buffer given user ptr
* @alloc: binder_alloc for this proc
* @user_ptr: User pointer to buffer data
*
* Validate userspace pointer to buffer data and return buffer corresponding to
* that user pointer. Search the rb tree for buffer that matches user data
* pointer.
*
* Return: Pointer to buffer or NULL
*/
struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
unsigned long user_ptr)
{
guard(mutex)(&alloc->mutex);
return binder_alloc_prepare_to_free_locked(alloc, user_ptr);
}
static inline void
binder_set_installed_page(struct binder_alloc *alloc,
unsigned long index,
struct page *page)
{
/* Pairs with acquire in binder_get_installed_page() */
smp_store_release(&alloc->pages[index], page);
}
static inline struct page *
binder_get_installed_page(struct binder_alloc *alloc, unsigned long index)
{
/* Pairs with release in binder_set_installed_page() */
return smp_load_acquire(&alloc->pages[index]);
}
static void binder_lru_freelist_add(struct binder_alloc *alloc,
unsigned long start, unsigned long end)
{
unsigned long page_addr;
struct page *page;
trace_binder_update_page_range(alloc, false, start, end);
for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
size_t index;
int ret;
index = (page_addr - alloc->vm_start) / PAGE_SIZE;
page = binder_get_installed_page(alloc, index);
if (!page)
continue;
trace_binder_free_lru_start(alloc, index);
ret = list_lru_add(alloc->freelist,
page_to_lru(page),
page_to_nid(page),
NULL);
WARN_ON(!ret);
trace_binder_free_lru_end(alloc, index);
}
}
static inline
void binder_alloc_set_mapped(struct binder_alloc *alloc, bool state)
{
/* pairs with smp_load_acquire in binder_alloc_is_mapped() */
smp_store_release(&alloc->mapped, state);
}
static inline bool binder_alloc_is_mapped(struct binder_alloc *alloc)
Annotation
- Immediate include surface: `linux/list.h`, `linux/sched/mm.h`, `linux/module.h`, `linux/rtmutex.h`, `linux/rbtree.h`, `linux/seq_file.h`, `linux/vmalloc.h`, `linux/slab.h`.
- Detected declarations: `function binder_alloc_buffer_size`, `function binder_insert_free_buffer`, `function binder_insert_allocated_buffer_locked`, `function binder_alloc_prepare_to_free`, `function binder_set_installed_page`, `function binder_get_installed_page`, `function binder_lru_freelist_add`, `function binder_alloc_set_mapped`, `function binder_alloc_is_mapped`, `function binder_page_insert`.
- Atlas domain: Driver Families / drivers/android.
- Implementation status: integration 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.