fs/timerfd.c
Source file repositories/reference/linux-study-clean/fs/timerfd.c
File Facts
- System
- Linux kernel
- Corpus path
fs/timerfd.c- Extension
.c- Size
- 15009 bytes
- Lines
- 622
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: syscall or user/kernel boundary
- Status
- core implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- 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/alarmtimer.hlinux/file.hlinux/poll.hlinux/init.hlinux/fs.hlinux/sched.hlinux/kernel.hlinux/slab.hlinux/list.hlinux/spinlock.hlinux/time.hlinux/hrtimer.hlinux/anon_inodes.hlinux/timerfd.hlinux/syscalls.hlinux/compat.hlinux/rcupdate.hlinux/time_namespace.h
Detected Declarations
syscall timerfd_createsyscall timerfd_settimesyscall timerfd_gettimesyscall timerfd_settime32syscall timerfd_gettime32struct timerfd_ctxfunction isalarmfunction __timerfd_triggeredfunction timerfd_triggeredfunction timerfd_tmrprocfunction timerfd_alarmprocfunction wake_up_lockedfunction timerfd_resume_workfunction timekeeping_resumefunction __timerfd_remove_cancelfunction timerfd_remove_cancelfunction timerfd_canceledfunction timerfd_setup_cancelfunction timerfd_get_remainingfunction timerfd_alarm_startfunction timerfd_alarm_restartfunction timerfd_hrtimer_startfunction timerfd_hrtimer_restartfunction timerfd_restartfunction timerfd_setupfunction timerfd_releasefunction timerfd_pollfunction timerfd_read_iterfunction timerfd_showfunction timerfd_ioctlfunction do_timerfd_settimefunction do_timerfd_gettime
Annotated Snippet
SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
{
struct timerfd_ctx *ctx __free(kfree) = NULL;
int ret;
/* Check the TFD_* constants for consistency. */
BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
if ((flags & ~TFD_CREATE_FLAGS) ||
(clockid != CLOCK_MONOTONIC &&
clockid != CLOCK_REALTIME &&
clockid != CLOCK_REALTIME_ALARM &&
clockid != CLOCK_BOOTTIME &&
clockid != CLOCK_BOOTTIME_ALARM))
return -EINVAL;
if ((clockid == CLOCK_REALTIME_ALARM ||
clockid == CLOCK_BOOTTIME_ALARM) &&
!capable(CAP_WAKE_ALARM))
return -EPERM;
ctx = kzalloc_obj(*ctx);
if (!ctx)
return -ENOMEM;
init_waitqueue_head(&ctx->wqh);
spin_lock_init(&ctx->cancel_lock);
ctx->clockid = clockid;
if (isalarm(ctx))
alarm_init(&ctx->t.alarm,
ctx->clockid == CLOCK_REALTIME_ALARM ?
ALARM_REALTIME : ALARM_BOOTTIME,
timerfd_alarmproc);
else
hrtimer_setup(&ctx->t.tmr, timerfd_tmrproc, clockid, HRTIMER_MODE_ABS);
ctx->moffs = ktime_mono_to_real(0);
ret = FD_ADD(flags & TFD_SHARED_FCNTL_FLAGS,
anon_inode_getfile_fmode("[timerfd]", &timerfd_fops, ctx,
O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS),
FMODE_NOWAIT));
if (ret >= 0)
retain_and_null_ptr(ctx);
return ret;
}
static int do_timerfd_settime(int ufd, int flags,
const struct itimerspec64 *new,
struct itimerspec64 *old)
{
struct timerfd_ctx *ctx;
int ret;
if ((flags & ~TFD_SETTIME_FLAGS) ||
!itimerspec64_valid(new))
return -EINVAL;
CLASS(fd, f)(ufd);
if (fd_empty(f))
return -EBADF;
if (fd_file(f)->f_op != &timerfd_fops)
return -EINVAL;
ctx = fd_file(f)->private_data;
if (isalarm(ctx) && !capable(CAP_WAKE_ALARM))
return -EPERM;
timerfd_setup_cancel(ctx, flags);
/*
* We need to stop the existing timer before reprogramming
* it to the new values.
*/
for (;;) {
spin_lock_irq(&ctx->wqh.lock);
if (isalarm(ctx)) {
if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
break;
} else {
if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
break;
}
spin_unlock_irq(&ctx->wqh.lock);
Annotation
- Immediate include surface: `linux/alarmtimer.h`, `linux/file.h`, `linux/poll.h`, `linux/init.h`, `linux/fs.h`, `linux/sched.h`, `linux/kernel.h`, `linux/slab.h`.
- Detected declarations: `syscall timerfd_create`, `syscall timerfd_settime`, `syscall timerfd_gettime`, `syscall timerfd_settime32`, `syscall timerfd_gettime32`, `struct timerfd_ctx`, `function isalarm`, `function __timerfd_triggered`, `function timerfd_triggered`, `function timerfd_tmrproc`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- 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.