drivers/char/random.c
Source file repositories/reference/linux-study-clean/drivers/char/random.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/char/random.c- Extension
.c- Size
- 53486 bytes
- Lines
- 1713
- Domain
- Driver Families
- Bucket
- drivers/char
- Inferred role
- Driver Families: syscall or user/kernel boundary
- Status
- core 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 or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- 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/utsname.hlinux/module.hlinux/kernel.hlinux/major.hlinux/string.hlinux/fcntl.hlinux/slab.hlinux/random.hlinux/poll.hlinux/init.hlinux/fs.hlinux/blkdev.hlinux/interrupt.hlinux/mm.hlinux/nodemask.hlinux/spinlock.hlinux/kthread.hlinux/percpu.hlinux/ptrace.hlinux/workqueue.hlinux/irq.hlinux/ratelimit.hlinux/syscalls.hlinux/completion.hlinux/uuid.hlinux/uaccess.hlinux/suspend.hlinux/siphash.hlinux/sched/isolation.hcrypto/chacha.hcrypto/blake2s.hvdso/datapage.h
Detected Declarations
syscall getrandomstruct crngstruct fast_poolstruct timer_rand_statestruct entropy_timer_statefunction rng_is_initializedfunction crng_set_readyfunction wait_for_random_bytesfunction execute_with_initialized_rngfunction crng_reseed_intervalfunction crng_reseedfunction crng_fast_key_erasurefunction crng_make_statefunction _get_random_bytesfunction wait_for_random_bytesfunction get_random_bytes_userfunction __get_random_u32_belowfunction random_prepare_cpufunction _mix_pool_bytesfunction mix_pool_bytesfunction extract_entropyfunction _credit_init_bitsfunction parse_trust_cpufunction parse_trust_bootloaderfunction random_pm_notificationfunction random_init_earlyfunction random_initfunction add_device_randomnessfunction add_hwgenerator_randomnessfunction add_bootloader_randomnessfunction add_vmfork_randomnessfunction register_random_vmfork_notifierfunction unregister_random_vmfork_notifierfunction fast_mixfunction random_online_cpufunction mix_interrupt_randomnessfunction add_interrupt_randomnessfunction add_timer_randomnessfunction add_input_randomnessfunction add_disk_randomnessfunction rand_initialize_diskfunction entropy_timerfunction try_to_generate_entropyfunction getrandomfunction random_pollfunction write_pool_userfunction random_write_iterfunction urandom_read_iter
Annotated Snippet
SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags)
{
struct iov_iter iter;
int ret;
if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE))
return -EINVAL;
/*
* Requesting insecure and blocking randomness at the same time makes
* no sense.
*/
if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM))
return -EINVAL;
if (!crng_ready() && !(flags & GRND_INSECURE)) {
if (flags & GRND_NONBLOCK)
return -EAGAIN;
ret = wait_for_random_bytes();
if (unlikely(ret))
return ret;
}
ret = import_ubuf(ITER_DEST, ubuf, len, &iter);
if (unlikely(ret))
return ret;
return get_random_bytes_user(&iter);
}
static __poll_t random_poll(struct file *file, poll_table *wait)
{
poll_wait(file, &crng_init_wait, wait);
return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM;
}
static ssize_t write_pool_user(struct iov_iter *iter)
{
u8 block[BLAKE2S_BLOCK_SIZE];
ssize_t ret = 0;
size_t copied;
if (unlikely(!iov_iter_count(iter)))
return 0;
for (;;) {
copied = copy_from_iter(block, sizeof(block), iter);
ret += copied;
mix_pool_bytes(block, copied);
if (!iov_iter_count(iter) || copied != sizeof(block))
break;
BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0);
if (ret % PAGE_SIZE == 0) {
if (signal_pending(current))
break;
cond_resched();
}
}
memzero_explicit(block, sizeof(block));
return ret ? ret : -EFAULT;
}
static ssize_t random_write_iter(struct kiocb *kiocb, struct iov_iter *iter)
{
return write_pool_user(iter);
}
static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *iter)
{
static int maxwarn = 10;
/*
* Opportunistically attempt to initialize the RNG on platforms that
* have fast cycle counters, but don't (for now) require it to succeed.
*/
if (!crng_ready())
try_to_generate_entropy();
if (!crng_ready()) {
if (!ratelimit_disable && maxwarn <= 0)
ratelimit_state_inc_miss(&urandom_warning);
else if (ratelimit_disable || __ratelimit(&urandom_warning)) {
--maxwarn;
pr_notice("%s: uninitialized urandom read (%zu bytes read)\n",
current->comm, iov_iter_count(iter));
}
}
return get_random_bytes_user(iter);
Annotation
- Immediate include surface: `linux/utsname.h`, `linux/module.h`, `linux/kernel.h`, `linux/major.h`, `linux/string.h`, `linux/fcntl.h`, `linux/slab.h`, `linux/random.h`.
- Detected declarations: `syscall getrandom`, `struct crng`, `struct fast_pool`, `struct timer_rand_state`, `struct entropy_timer_state`, `function rng_is_initialized`, `function crng_set_ready`, `function wait_for_random_bytes`, `function execute_with_initialized_rng`, `function crng_reseed_interval`.
- Atlas domain: Driver Families / drivers/char.
- Implementation status: core 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.