| 0/0 |
2026/05/18 16:05 |
flow |
assessment-security |
21m
Results: map[DenialOfService:false Explanation:Based on the analysis of the provided kernel bug report and the source code,
here is the assessment of the bug's security impact:
### Bug Analysis
The bug report is a Kernel Concurrency Sanitizer (KCSAN) warning about a data
race in the kernel's timekeeping subsystem.
- **The Writer:** The timer interrupt (`hrtimer_interrupt` ->
`timekeeping_update_from_shadow`) updates the core timekeeper structure
(`tk_core.timekeeper`). It does so by copying the shadow timekeeper using a
plain `memcpy()` inside a seqlock write critical section
(`write_seqcount_begin()` / `write_seqcount_end()`).
- **The Reader:** A process (in this case, processing an incoming network packet
via `tpacket_rcv` -> `ktime_get_real_ts64`) reads the timekeeper structure to
get the current time. It performs plain reads (e.g., reading `tkr->cycle_last`)
inside a seqlock read critical section (`read_seqcount_begin()` /
`read_seqcount_retry()`).
KCSAN flags this as a data race because a plain read is racing with a plain
write (`memcpy`). However, this is a well-known and standard seqlock pattern in
the Linux kernel. The seqlock mechanism ensures that if the writer modifies the
data while the reader is reading it, the sequence number will change, and the
reader's `read_seqcount_retry()` will fail, forcing the reader to loop and read
the data again until it gets a consistent snapshot.
Furthermore, the plain reads performed by the reader (such as `tkr->cycle_last`,
`tkr->mult`, `tkr->shift`) are only used in safe arithmetic operations
(calculating time deltas) before the sequence number is validated. There are no
pointer dereferences or divisions by zero that could cause a crash from a torn
read before the retry logic kicks in. (Pointers like `tkr->clock` are safely
read using `READ_ONCE()` inside `tk_clock_read()`).
### Security Impact
- **Exploitable: No.** This is a completely benign data race handled correctly
by the seqlock retry logic. It cannot lead to memory corruption, wild pointer
dereferences, or privilege escalation.
- **Denial of Service: No.** The race does not cause crashes, hangs, deadlocks,
or resource leaks. In production builds, KCSAN is disabled, so this will not
even generate a warning in the kernel logs.
- **Triggers:** Because timekeeping functions like `ktime_get_real_ts64()` are
heavily used throughout the kernel, the read-side of this race can be triggered
by almost anything. Unprivileged processes can trigger it directly via the
`clock_gettime(CLOCK_REALTIME)` syscall or indirectly by receiving network
packets (as seen in the stack trace), interacting with filesystems, or using
peripherals. It is equally reachable from user namespaces, VM guests, and VM
hosts. Exploitable:false FilesystemTrigger:true NetworkTrigger:true PeripheralTrigger:true RemoteTrigger:true Unprivileged:true UserNamespace:true VMGuestTrigger:true VMHostTrigger:true]
|
| 1/1 |
2026/05/18 16:05 |
action |
syz-repro-to-c-repro |
0m
Results:
|
| 2/1 |
2026/05/18 16:05 |
action |
kernel-checkouter |
1m
Results: map[KernelSrc:/app/workdir/cache/src/39056aef3a53503f1a05892aad8990e208e793fc]
|
| 3/1 |
2026/05/18 16:06 |
action |
kernel-builder |
9m
Results: map[KernelObj:/app/workdir/cache/build/b973c5f6f2e5f17ebaaf39ce43b84b03b5b532c2]
|
| 4/1 |
2026/05/18 16:16 |
action |
codesearch-prepare |
7m
Results: map[Index:codesearch-index]
|
| 5/1 |
2026/05/18 16:24 |
agent |
expert |
3m
Model:
Results: map[DenialOfService:false Exploitable:false FilesystemTrigger:true NetworkTrigger:true PeripheralTrigger:true RemoteTrigger:true Unprivileged:true UserNamespace:true VMGuestTrigger:true VMHostTrigger:true]
Instruction:
You are an experienced Linux kernel security engineer. Your task is to analyze given kernel bug report
and determine its security impact based on the following dimensions.
Use the provided tools to examine the source code, check for capability checks (e.g., capable(), ns_capable()),
and understand the nature of the bug. Analyze the given kernel build and configuration.
You can check the kernel config by grepping ".config" file; you can check kernel cmdline by greeping
".config" file for "CONFIG_CMDLINE=". Assume sysctl parameters have default values.
But analyze for the corresponding production build w/o debugging tools enabled (like KASAN, KMSAN, UBSAN).
Don't make assumptions; verify them with source code access. Try different strategies when analyzing the bug:
- think of ways in which the vulnerable code is unreachable
- or the other way around: try to come up with different ideas of how an unprivileged user can reach the bug
If still unsure err on the side of the bug being non-exploitable/not-accessible.
In the final reply, provide a reasoning for your assessment.
Analysis dimensions:
* Exploitable:
Determine if the bug can result in memory corruption or elevated privileges.
Memory safety issues are almost always exploitable (KASAN or UBSAN reports for use-after-free, out-of-bounds;
refcounting issues, corrupted lists, etc). When kernel is crashing on a completly wild pointer access
(e.g. user-space address, or non-canonical address, but not on NULL or address corresponding to KASAN shadow
for NULL address), including both data accesses and control tranfers, that's also usually implies possibility
of exploitation. Such reports usually say "unable to handle kernel paging request".
Uses of uninitialized values detected by KMSAN may be exploitable b/c attacker frequently can affect uninit
values with spraying techniques. However, for these exploitabability depends on how exactly the uninit value
is used in the code, and what it affects.
Think of what happens after the bug is triggered. Some bugs cause kernel panic and halt execution,
they are harder to exploit. For example, BUG reports halts the kernel. However, WARNING reports don't halt
execution in production builds. Debug bug detection tools (like KASAN, KMSAN, KCSAN, UBSAN) are also not enabled
in production builds, so attacker can freely exploit these bugs w/o being detected by these tools.
If you see an integer overflow, think how the overflowed value used later (if it's used as allocation size,
or an array index). If you see an out-of-bounds read, think if it's followed by an out-of-bounds write as well.
Some KCSAN data-races may be exploitable by skilled attackers as well. Think what data structures got corrupted
as the result of data races and how. However, note that kernel has lots of "benign" data races that don't lead
to any runtime misbehavior at all.
* Denial Of Service:
Determine if the bug can result in denial-of-service. Most bugs can, since they cause system crash,
hangs, deadlocks, or resource leaks. This is mostly applicable to WARNING bugs that won't cause system crash
in production. For these think what will be consequences of the violation of the kernel assumptions flagged
by the WARNING. In some cases the unexpected condition is also properly handled by the normal control flow
(e.g. with "if (WARN_ON(...))"), these won't cause denial-of-service. If the condition is not handled,
then it may or may not cause denial-of-service.
* Accessible From Unprivileged Processes:
Determine if the bug can be reached from a typical (non-root) user process that does NOT have any special capabilities
(like CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON) or access to device nodes restricted to root.
Assume that unprivileged_bpf_disabled=1, that is eBPF loading is not accessible. However, cBPF (classical BPF)
is still accessible to non-root processes.
Assume that user namespaces are not accessible, that is, the process cannot get the mentioned capabilities even
within a new user namespace (checked by ns_capable() function in the kernel sources).
* Accessible From User Namespaces:
Determine if the bug can be reached within a user-namespace where the process has all capabilities
(including CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON). Such capabilities are checked with ns_capable()
function in the kernel sources.
* VM Guest Trigger:
Determine if the bug can be triggered from the context of a typical KVM guest (e.g., set up by a QEMU VMM).
Consider accesses to standard Linux host paravirtualized features (virtio-blk, virtio-net, etc.),
and handling of VM exits in the KVM code.
* VM Host Trigger in The Confidetial Computing Context:
Determine if the bug can be triggered in a confidential computing guest kernel from the context of a KVM host.
Consider access to standard Linux guest paravirtualized features (virtio-blk, virtio-net, etc.).
* Ethernet Network Trigger:
Determine if the bug can be triggered by processing ingress network Ethernet traffic, either directly (network stack)
or via drivers exposed to network data.
* Other Remote Trigger:
Determine if the bug can be triggered by processing remote traffic other than Ethernet (Wifi, Bluetooth, NFC, etc).
* Peripheral Trigger:
Determine if the bug can be triggered via an untrusted peripheral device that can be physically plugged
into a system, such as a USB device or a niche hardware driver handling external hardware inputs.
This is particularly important for mobile and desktop environments where users can plug in unknown devices.
* Malicious Filesystem Trigger:
Determine if the bug can be triggered by the kernel mounting and parsing a malicious filesystem image.
This is highly critical for Desktop and Mobile environments where external media or downloaded images
might be auto-mounted.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The kernel bug report is:
af_packet: tpacket_rcv: packet too big, clamped from 32818 to 3966. macoff=82
==================================================================
BUG: KCSAN: data-race in ktime_get_real_ts64 / timekeeping_update_from_shadow
write to 0xffffffff895d9488 of 304 bytes by interrupt on cpu 0:
timekeeping_update_from_shadow+0x40d/0x440 kernel/time/timekeeping.c:829
__timekeeping_advance+0xa5d/0xc10 kernel/time/timekeeping.c:2532
timekeeping_advance kernel/time/timekeeping.c:2540 [inline]
update_wall_time+0x21/0x50 kernel/time/timekeeping.c:2550
tick_do_update_jiffies64+0x169/0x1c0 kernel/time/tick-sched.c:149
tick_sched_do_timer kernel/time/tick-sched.c:253 [inline]
tick_nohz_handler+0x8d/0x3d0 kernel/time/tick-sched.c:312
__run_hrtimer kernel/time/hrtimer.c:1930 [inline]
__hrtimer_run_queues+0x276/0x4f0 kernel/time/hrtimer.c:1994
hrtimer_interrupt+0x261/0x850 kernel/time/hrtimer.c:2113
local_apic_timer_interrupt arch/x86/kernel/apic/apic.c:1050 [inline]
__sysvec_apic_timer_interrupt+0x5f/0x1c0 arch/x86/kernel/apic/apic.c:1067
instr_sysvec_apic_timer_interrupt arch/x86/kernel/apic/apic.c:1061 [inline]
sysvec_apic_timer_interrupt+0x6f/0x80 arch/x86/kernel/apic/apic.c:1061
asm_sysvec_apic_timer_interrupt+0x1a/0x20 arch/x86/include/asm/idtentry.h:697
kcsan_setup_watchpoint+0x404/0x410 kernel/kcsan/core.c:705
scm_set_cred include/net/scm.h:73 [inline]
unix_skb_to_scm net/unix/af_unix.c:2005 [inline]
__unix_dgram_recvmsg+0x5ca/0x870 net/unix/af_unix.c:2638
unix_dgram_recvmsg+0x7e/0x90 net/unix/af_unix.c:2686
sock_recvmsg_nosec+0xc2/0xf0 net/socket.c:1137
____sys_recvmsg+0x26f/0x280 net/socket.c:2916
___sys_recvmsg+0x11f/0x3b0 net/socket.c:2960
do_recvmmsg+0x1ef/0x560 net/socket.c:3055
__sys_recvmmsg net/socket.c:3129 [inline]
__do_sys_recvmmsg net/socket.c:3152 [inline]
__se_sys_recvmmsg net/socket.c:3145 [inline]
__x64_sys_recvmmsg+0xe5/0x170 net/socket.c:3145
x64_sys_call+0x80f/0x3020 arch/x86/include/generated/asm/syscalls_64.h:300
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0x12c/0x3b0 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffffffff895d9498 of 8 bytes by task 1037 on cpu 1:
timekeeping_cycles_to_ns kernel/time/timekeeping.c:426 [inline]
timekeeping_get_ns kernel/time/timekeeping.c:449 [inline]
ktime_get_real_ts64+0xee/0x300 kernel/time/timekeeping.c:946
prb_open_block net/packet/af_packet.c:857 [inline]
prb_dispatch_next_block net/packet/af_packet.c:936 [inline]
__packet_lookup_frame_in_block net/packet/af_packet.c:1094 [inline]
packet_current_rx_frame net/packet/af_packet.c:1120 [inline]
tpacket_rcv+0xe93/0x1c10 net/packet/af_packet.c:2350
packet_rcv_fanout+0x2a7/0x2d0 net/packet/af_packet.c:1505
dev_queue_xmit_nit+0x4cc/0x510 net/core/dev.c:2606
xmit_one net/core/dev.c:3884 [inline]
dev_hard_start_xmit+0xd8/0x3a0 net/core/dev.c:3904
__dev_queue_xmit+0xbd5/0x1ec0 net/core/dev.c:4870
dev_queue_xmit include/linux/netdevice.h:3421 [inline]
neigh_hh_output include/net/neighbour.h:540 [inline]
neigh_output include/net/neighbour.h:554 [inline]
ip_finish_output2+0x705/0x8c0 net/ipv4/ip_output.c:237
__ip_finish_output net/ipv4/ip_output.c:-1 [inline]
ip_finish_output+0x112/0x290 net/ipv4/ip_output.c:325
NF_HOOK_COND include/linux/netfilter.h:307 [inline]
ip_output+0xbd/0x1c0 net/ipv4/ip_output.c:438
dst_output include/net/dst.h:470 [inline]
ip_local_out net/ipv4/ip_output.c:131 [inline]
__ip_queue_xmit+0xb68/0xba0 net/ipv4/ip_output.c:534
ip_queue_xmit+0x39/0x50 net/ipv4/ip_output.c:548
__tcp_transmit_skb+0x1c2a/0x20b0 net/ipv4/tcp_output.c:1715
tcp_transmit_skb net/ipv4/tcp_output.c:1733 [inline]
tcp_write_xmit+0x129e/0x36a0 net/ipv4/tcp_output.c:3060
__tcp_push_pending_frames+0x6d/0x1b0 net/ipv4/tcp_output.c:3243
tcp_push+0x311/0x330 net/ipv4/tcp.c:776
mptcp_push_release net/mptcp/protocol.c:1569 [inline]
__mptcp_push_pending+0x3d5/0x550 net/mptcp/protocol.c:1709
mptcp_release_cb+0x49c/0x7f0 net/mptcp/protocol.c:3671
release_sock+0x168/0x180 net/core/sock.c:3819
mptcp_worker+0xa41/0xd60 net/mptcp/protocol.c:3007
process_one_work kernel/workqueue.c:3302 [inline]
process_scheduled_works+0x4f0/0x9c0 kernel/workqueue.c:3385
worker_thread+0x58a/0x780 kernel/workqueue.c:3466
kthread+0x22a/0x280 kernel/kthread.c:436
ret_from_fork+0x146/0x330 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
value changed: 0x000000831bc8e013 -> 0x000000831d189180
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 1037 Comm: kworker/1:2 Tainted: G W syzkaller #0 PREEMPT(full)
Tainted: [W]=WARN
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 04/18/2026
Workqueue: events mptcp_worker
==================================================================
Reply: Based on the analysis of the provided kernel bug report and the source code, here is the assessment of the bug's security impact:
### Bug Analysis
The bug report is a Kernel Concurrency Sanitizer (KCSAN) warning about a data race in the kernel's timekeeping subsystem.
- **The Writer:** The timer interrupt (`hrtimer_interrupt` -> `timekeeping_update_from_shadow`) updates the core timekeeper structure (`tk_core.timekeeper`). It does so by copying the shadow timekeeper using a plain `memcpy()` inside a seqlock write critical section (`write_seqcount_begin()` / `write_seqcount_end()`).
- **The Reader:** A process (in this case, processing an incoming network packet via `tpacket_rcv` -> `ktime_get_real_ts64`) reads the timekeeper structure to get the current time. It performs plain reads (e.g., reading `tkr->cycle_last`) inside a seqlock read critical section (`read_seqcount_begin()` / `read_seqcount_retry()`).
KCSAN flags this as a data race because a plain read is racing with a plain write (`memcpy`). However, this is a well-known and standard seqlock pattern in the Linux kernel. The seqlock mechanism ensures that if the writer modifies the data while the reader is reading it, the sequence number will change, and the reader's `read_seqcount_retry()` will fail, forcing the reader to loop and read the data again until it gets a consistent snapshot.
Furthermore, the plain reads performed by the reader (such as `tkr->cycle_last`, `tkr->mult`, `tkr->shift`) are only used in safe arithmetic operations (calculating time deltas) before the sequence number is validated. There are no pointer dereferences or divisions by zero that could cause a crash from a torn read before the retry logic kicks in. (Pointers like `tkr->clock` are safely read using `READ_ONCE()` inside `tk_clock_read()`).
### Security Impact
- **Exploitable: No.** This is a completely benign data race handled correctly by the seqlock retry logic. It cannot lead to memory corruption, wild pointer dereferences, or privilege escalation.
- **Denial of Service: No.** The race does not cause crashes, hangs, deadlocks, or resource leaks. In production builds, KCSAN is disabled, so this will not even generate a warning in the kernel logs.
- **Triggers:** Because timekeeping functions like `ktime_get_real_ts64()` are heavily used throughout the kernel, the read-side of this race can be triggered by almost anything. Unprivileged processes can trigger it directly via the `clock_gettime(CLOCK_REALTIME)` syscall or indirectly by receiving network packets (as seen in the stack trace), interacting with filesystems, or using peripherals. It is equally reachable from user namespaces, VM guests, and VM hosts.
|
| 6/2 |
2026/05/18 16:24 |
llm |
expert |
0m
Model:
Tokens:
input: 6761
output: 22
thoughts: 495
Thoughts: **Analyzing a KCSAN Data Race Report**
Okay, so I've got a KCSAN data-race report here. The race is between `timekeeping_update_from_shadow` (a write operation) and `ktime_get_real_ts64` (a read). This looks interesting. Let's dig into the stack traces.
The write stack trace shows `timekeeping_update_from_shadow` calls through `__timekeeping_advance`, `update_wall_time`, and finally `tick_do_update_jiffies64`. Seems like this is updating the shadow timekeeping structures, probably in response to a tick or system event.
On the read side, `ktime_get_real_ts64` is being used, and it calls `timekeeping_cycles_to_ns` and `timekeeping_get_ns`. The read path goes through the packet processing.
Aha, `ktime_get_real_ts64` is reading from `tk_core.timekeeper` or something similar, which is the core timekeeping data structure. This is the likely culprit. Let me quickly jump into the code and check the surrounding lines. Specifically, I'm going to look around lines 946 and 829 in `kernel/time/timekeeping.c`. I need to see how the write and read paths are interacting and if there is missing locking or synchronization here. This looks like a race on the timekeeping data, and it's likely happening in the packet processing path. I'll bet it's something around the tick handling and how it interacts with the time stamps the packet code is using. Let's see...
|
| 7/2 |
2026/05/18 16:24 |
tool |
grepper |
0m
Args: map[Expression:timekeeping_update_from_shadow]
Results: map[Output:kernel/time/timekeeping.c=777=static void timekeeping_restore_shadow(struct tk_data *tkd)
--
kernel/time/timekeeping.c-782-
kernel/time/timekeeping.c:783:static void timekeeping_update_from_shadow(struct tk_data *tkd, unsigned int action)
kernel/time/timekeeping.c-784-{
--
kernel/time/timekeeping.c=1577=int do_settimeofday64(const struct timespec64 *ts)
--
kernel/time/timekeeping.c-1598- tk_set_xtime(tks, ts);
kernel/time/timekeeping.c:1599: timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL);
kernel/time/timekeeping.c-1600- }
--
kernel/time/timekeeping.c=1623=static int __timekeeping_inject_offset(struct tk_data *tkd, const struct timespec64 *ts)
--
kernel/time/timekeeping.c-1660-
kernel/time/timekeeping.c:1661: timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL);
kernel/time/timekeeping.c-1662- return 0;
--
kernel/time/timekeeping.c=1726=static int change_clocksource(void *data)
--
kernel/time/timekeeping.c-1749- tk_setup_internals(tks, new);
kernel/time/timekeeping.c:1750: timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL);
kernel/time/timekeeping.c-1751- }
--
kernel/time/timekeeping.c=1956=void __init timekeeping_init(void)
--
kernel/time/timekeeping.c-1996-
kernel/time/timekeeping.c:1997: timekeeping_update_from_shadow(&tk_core, TK_CLOCK_WAS_SET);
kernel/time/timekeeping.c-1998-}
--
kernel/time/timekeeping.c=2073=void timekeeping_inject_sleeptime64(const struct timespec64 *delta)
--
kernel/time/timekeeping.c-2080- __timekeeping_inject_sleeptime(tks, delta);
kernel/time/timekeeping.c:2081: timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL);
kernel/time/timekeeping.c-2082- }
--
kernel/time/timekeeping.c=2092=void timekeeping_resume(void)
--
kernel/time/timekeeping.c-2140- timekeeping_suspended = 0;
kernel/time/timekeeping.c:2141: timekeeping_update_from_shadow(&tk_core, TK_CLOCK_WAS_SET);
kernel/time/timekeeping.c-2142- raw_spin_unlock_irqrestore(&tk_core.lock, flags);
--
kernel/time/timekeeping.c=2157=int timekeeping_suspend(void)
--
kernel/time/timekeeping.c-2212-
kernel/time/timekeeping.c:2213: timekeeping_update_from_shadow(&tk_core, 0);
kernel/time/timekeeping.c-2214- halt_fast_timekeeper(tks);
--
kernel/time/timekeeping.c=2476=static bool __timekeeping_advance(struct tk_data *tkd, enum timekeeping_adv_mode mode)
--
kernel/time/timekeeping.c-2531-
kernel/time/timekeeping.c:2532: timekeeping_update_from_shadow(tkd, clock_set);
kernel/time/timekeeping.c-2533-
--
kernel/time/timekeeping.c=2849=static int __do_adjtimex(struct tk_data *tkd, struct __kernel_timex *txc,
--
kernel/time/timekeeping.c-2891- __timekeeping_set_tai_offset(tks, tai);
kernel/time/timekeeping.c:2892: timekeeping_update_from_shadow(tkd, TK_CLOCK_WAS_SET);
kernel/time/timekeeping.c-2893- result->clock_set = true;
--
kernel/time/timekeeping.c=2978=static void tk_aux_update_clocksource(void)
--
kernel/time/timekeeping.c-2992- tk_setup_internals(tks, tk_core.timekeeper.tkr_raw.clock);
kernel/time/timekeeping.c:2993: timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL);
kernel/time/timekeeping.c-2994- }
--
kernel/time/timekeeping.c=3080=static int aux_clock_set(const clockid_t id, const struct timespec64 *tnew)
--
kernel/time/timekeeping.c-3101- * updated yet, so do that first. That makes the update
kernel/time/timekeeping.c:3102: * in timekeeping_update_from_shadow() redundant, but
kernel/time/timekeeping.c-3103- * that's harmless. After that @tnow can be calculated
--
kernel/time/timekeeping.c-3118-
kernel/time/timekeeping.c:3119: timekeeping_update_from_shadow(aux_tkd, TK_UPDATE_ALL);
kernel/time/timekeeping.c-3120- return 0;
--
kernel/time/timekeeping.c=3145=static void aux_clock_enable(clockid_t id)
--
kernel/time/timekeeping.c-3169- aux_tks->clock_valid = true;
kernel/time/timekeeping.c:3170: timekeeping_update_from_shadow(aux_tkd, TK_UPDATE_ALL);
kernel/time/timekeeping.c-3171-}
--
kernel/time/timekeeping.c=3173=static void aux_clock_disable(clockid_t id)
--
kernel/time/timekeeping.c-3178- aux_tkd->shadow_timekeeper.clock_valid = false;
kernel/time/timekeeping.c:3179: timekeeping_update_from_shadow(aux_tkd, TK_UPDATE_ALL);
kernel/time/timekeeping.c-3180-}
]
|
| 8/2 |
2026/05/18 16:24 |
llm |
expert |
0m
Model:
Tokens:
input: 9333
output: 47
thoughts: 32
Thoughts: Okay, here's my take on those thoughts, framed as my own mental process, given my expertise:
**Diving into the Timekeeping Conundrum**
Alright, a data race... that's never fun. The hint points me directly to `kernel/time/timekeeping.c`, so that's the first place to focus my attention. Timekeeping is crucial, especially in a system-level kernel. A race here could cause all sorts of subtle, hard-to-debug issues – think inconsistent timestamps, corrupted logs, even potential security vulnerabilities if timing is tied to privilege escalation or other critical checks.
Let's cut right to it, the likely culprit is `ktime_get_real_ts64`. This is the typical point where we grab the 'real' wall clock time, and that's something that threads will constantly be using, so it's a prime target. I'll need to immediately start considering the potential for concurrent access: Are there multiple CPUs reading and writing to the same timekeeping data structures without proper synchronization? Are there any obvious locks or atomic operations in place? What's the context in which `ktime_get_real_ts64` is being called? Understanding the call stack and how this function integrates with the rest of the timekeeping infrastructure will be key to understanding the issue. I know this function well, so I'll review its code for the implementation details.
|
| 9/2 |
2026/05/18 16:24 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:kernel/time/timekeeping.c IncludeLines:true Name:ktime_get_real_ts64]
Results: map[SourceCode: 928: /**
929: * ktime_get_real_ts64 - Returns the time of day in a timespec64.
930: * @ts: pointer to the timespec to be set
931: *
932: * Returns the time of day in a timespec64 (WARN if suspended).
933: */
934: void ktime_get_real_ts64(struct timespec64 *ts)
935: {
936: struct timekeeper *tk = &tk_core.timekeeper;
937: unsigned int seq;
938: u64 nsecs;
939:
940: WARN_ON(timekeeping_suspended);
941:
942: do {
943: seq = read_seqcount_begin(&tk_core.seq);
944:
945: ts->tv_sec = tk->xtime_sec;
946: nsecs = timekeeping_get_ns(&tk->tkr_mono);
947:
948: } while (read_seqcount_retry(&tk_core.seq, seq));
949:
950: ts->tv_nsec = 0;
951: timespec64_add_ns(ts, nsecs);
952: }
SourceFile:kernel/time/timekeeping.c]
|
| 10/2 |
2026/05/18 16:24 |
llm |
expert |
0m
Model:
Tokens:
input: 10117
output: 45
thoughts: 90
Thoughts: Here's my analysis of that snippet:
**Diving Deep into Timekeeping**
Okay, so I see `ktime_get_real_ts64` is grabbing `tk->tkr_mono` and using a `read_seqcount_begin`/`read_seqcount_retry` loop. That's standard practice for reading shared data, protecting against concurrent updates. But hang on... I recall that the `timekeeping_update_from_shadow` function does something with `tk_core.timekeeper` and `tk_core.shadow_timekeeper`. This suggests a shadow timekeeper mechanism. This deserves closer examination. Let's dig into `timekeeping_update_from_shadow` and see what's going on with these timekeepers. I need to understand how the shadow timekeeper influences the main one.
|
| 11/2 |
2026/05/18 16:24 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:kernel/time/timekeeping.c IncludeLines:true Name:timekeeping_update_from_shadow]
Results: map[SourceCode: 783: static void timekeeping_update_from_shadow(struct tk_data *tkd, unsigned int action)
784: {
785: struct timekeeper *tk = &tkd->shadow_timekeeper;
786:
787: lockdep_assert_held(&tkd->lock);
788:
789: /*
790: * Block out readers before running the updates below because that
791: * updates VDSO and other time related infrastructure. Not blocking
792: * the readers might let a reader see time going backwards when
793: * reading from the VDSO after the VDSO update and then reading in
794: * the kernel from the timekeeper before that got updated.
795: */
796: write_seqcount_begin(&tkd->seq);
797:
798: if (action & TK_CLEAR_NTP) {
799: tk->ntp_error = 0;
800: ntp_clear(tk->id);
801: }
802:
803: tk_update_leap_state(tk);
804: tk_update_ktime_data(tk);
805: tk->tkr_mono.base_real = tk->tkr_mono.base + tk->offs_real;
806:
807: if (tk->id == TIMEKEEPER_CORE) {
808: tk_update_ns_to_cyc(tk, &tkd->timekeeper);
809: update_vsyscall(tk);
810: update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
811:
812: update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono);
813: update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw);
814: } else if (tk_is_aux(tk)) {
815: vdso_time_update_aux(tk);
816: }
817:
818: if (action & TK_CLOCK_WAS_SET)
819: tk->clock_was_set_seq++;
820:
821: /*
822: * Update the real timekeeper.
823: *
824: * We could avoid this memcpy() by switching pointers, but that has
825: * the downside that the reader side does not longer benefit from
826: * the cacheline optimized data layout of the timekeeper and requires
827: * another indirection.
828: */
829: memcpy(&tkd->timekeeper, tk, sizeof(*tk));
830: write_seqcount_end(&tkd->seq);
831: }
SourceFile:kernel/time/timekeeping.c]
|
| 12/2 |
2026/05/18 16:24 |
llm |
expert |
0m
Model:
Tokens:
input: 11242
output: 24
thoughts: 333
Thoughts: **Analyzing a Seqlock Data Race Report**
Okay, so I'm looking at this code and immediately see a classic seqlock pattern. There's a `memcpy` in `write_seqcount_begin` and `write_seqcount_end`, copying some data into `tkd->timekeeper`, and reads happening in `read_seqcount_begin` and `read_seqcount_retry`. Seems pretty straightforward.
But KCSAN is flagging a data race. My first instinct is: hang on, seqlock readers are *supposed* to do plain reads, and writers do plain writes. That's the whole point, relying on the sequence counter to detect conflicts. `read_seqcount_begin` and `read_seqcount_retry` *should* be handling this.
Then, a thought hits me - is KCSAN complaining because the read isn't explicitly marked with `READ_ONCE` or something similar? Or maybe it's something more subtle, like the data being read isn't actually a `seqcount_t` but something else? I need to consider all the nuances here.
Wait a second, doesn't `read_seqcount_begin` use `kcsan_atomic_next(0)` or something similar to effectively ignore the races *within* the critical section? Surely that's the intended behavior. But then I have another thought... maybe it *doesn't* automatically suppress those reports unless the reads are explicitly marked or KCSAN is configured to ignore them?
Okay, time to check my assumptions here. I remember seeing this done in the Linux kernel implementation, that it *does* suppress the KCSAN reports for reads inside the critical section. Or maybe it's using something like `kcsan_nestable_atomic_begin()`? I'm going to need to dig into the `include/linux/seqlock.h` or `include/linux/seqcount.h` headers. Gotta verify how the kernel actually handles this. That's where the answer lies.
|
| 13/2 |
2026/05/18 16:24 |
tool |
grepper |
0m
Args: map[Expression:kcsan_nestable_atomic_begin]
Results: map[Output:include/linux/kcsan-checks.h=82=void kcsan_enable_current_nowarn(void); /* Safe in uaccess regions. */
--
include/linux/kcsan-checks.h-84-/**
include/linux/kcsan-checks.h:85: * kcsan_nestable_atomic_begin - begin nestable atomic region
include/linux/kcsan-checks.h-86- *
--
include/linux/kcsan-checks.h-89- */
include/linux/kcsan-checks.h:90:void kcsan_nestable_atomic_begin(void);
include/linux/kcsan-checks.h-91-
--
include/linux/kcsan-checks.h=198=static inline void kcsan_enable_current_nowarn(void) { }
include/linux/kcsan-checks.h:199:static inline void kcsan_nestable_atomic_begin(void) { }
include/linux/kcsan-checks.h-200-static inline void kcsan_nestable_atomic_end(void) { }
--
include/linux/seqlock.h=429=static inline void do_raw_write_seqcount_begin(seqcount_t *s)
include/linux/seqlock.h-430-{
include/linux/seqlock.h:431: kcsan_nestable_atomic_begin();
include/linux/seqlock.h-432- s->sequence++;
--
include/linux/seqlock.h=572=static inline void do_raw_write_seqcount_barrier(seqcount_t *s)
include/linux/seqlock.h-573-{
include/linux/seqlock.h:574: kcsan_nestable_atomic_begin();
include/linux/seqlock.h-575- s->sequence++;
--
include/linux/seqlock.h=592=static inline void do_write_seqcount_invalidate(seqcount_t *s)
--
include/linux/seqlock.h-594- smp_wmb();
include/linux/seqlock.h:595: kcsan_nestable_atomic_begin();
include/linux/seqlock.h-596- s->sequence+=2;
--
include/linux/seqlock.h=779=static __always_inline void write_seqcount_latch_begin(seqcount_latch_t *s)
include/linux/seqlock.h-780-{
include/linux/seqlock.h:781: kcsan_nestable_atomic_begin();
include/linux/seqlock.h-782- raw_write_seqcount_latch(s);
--
kernel/kcsan/core.c=851=EXPORT_SYMBOL(kcsan_enable_current_nowarn);
kernel/kcsan/core.c-852-
kernel/kcsan/core.c:853:void kcsan_nestable_atomic_begin(void)
kernel/kcsan/core.c-854-{
--
kernel/kcsan/core.c-863-}
kernel/kcsan/core.c:864:EXPORT_SYMBOL(kcsan_nestable_atomic_begin);
kernel/kcsan/core.c-865-
kernel/kcsan/core.c=866=void kcsan_nestable_atomic_end(void)
--
kernel/kcsan/core.c-870- * Warn if kcsan_nestable_atomic_end() calls are unbalanced with
kernel/kcsan/core.c:871: * kcsan_nestable_atomic_begin() calls, which causes
kernel/kcsan/core.c-872- * atomic_nest_count to become negative and should not happen.
kernel/kcsan/core.c-873- */
kernel/kcsan/core.c:874: kcsan_nestable_atomic_begin(); /* restore to 0 */
kernel/kcsan/core.c-875- kcsan_disable_current(); /* disable to generate warning */
--
kernel/kcsan/kcsan_test.c=376=static noinline void test_kernel_change_bits(void)
--
kernel/kcsan/kcsan_test.c-382- */
kernel/kcsan/kcsan_test.c:383: kcsan_nestable_atomic_begin();
kernel/kcsan/kcsan_test.c-384- test_var ^= TEST_CHANGE_BITS;
--
kernel/kcsan/kcsan_test.c=484=static noinline void test_kernel_xor_1bit(void)
--
kernel/kcsan/kcsan_test.c-486- /* Do not report data races between the read-writes. */
kernel/kcsan/kcsan_test.c:487: kcsan_nestable_atomic_begin();
kernel/kcsan/kcsan_test.c-488- test_var ^= 0x10000;
--
kernel/kcsan/kcsan_test.c=528=static void test_barrier_nothreads(struct kunit *test)
--
kernel/kcsan/kcsan_test.c-568-
kernel/kcsan/kcsan_test.c:569: kcsan_nestable_atomic_begin(); /* No watchpoints in called functions. */
kernel/kcsan/kcsan_test.c-570-
--
kernel/kcsan/selftest.c=110=static bool __init test_barrier(void)
--
kernel/kcsan/selftest.c-138-
kernel/kcsan/selftest.c:139: kcsan_nestable_atomic_begin(); /* No watchpoints in called functions. */
kernel/kcsan/selftest.c-140-
--
kernel/time/sched_clock.c=106=unsigned long long notrace sched_clock(void)
--
kernel/time/sched_clock.c-114- */
kernel/time/sched_clock.c:115: kcsan_nestable_atomic_begin();
kernel/time/sched_clock.c-116- ns = __sched_clock();
]
|
| 14/2 |
2026/05/18 16:24 |
llm |
expert |
0m
Model:
Tokens:
input: 13680
output: 20
thoughts: 42
Thoughts: Okay, here's my take on those thoughts, framed as an expert's internal monologue:
**Investigating Sequence Counters and Atomic Operations**
Right, so `do_raw_write_seqcount_begin` is leveraging `kcsan_nestable_atomic_begin()`. Interesting, a good choice given the context. It's the expected way to handle the beginning of a sequence write within that KCSAN environment, ensuring atomicity and appropriate nesting behavior. My concern now is about how that interacts with the read side of these counters, let me consider `read_seqcount_begin` now. I need to make sure the read process works well as the write process begins.
|
| 15/2 |
2026/05/18 16:24 |
tool |
grepper |
0m
Args: map[Expression:read_seqcount_begin]
Results: map[Output:
Full output is too long, showing 500 out of 612 lines.
Use more precise expression if possible.
[Documentation/filesystems/path-lookup.rst=699=REF-walk increments a reference count or takes a spinlock, RCU-walk
Documentation/filesystems/path-lookup.rst:700:samples the status of a seqlock using ``read_seqcount_begin()`` or a
Documentation/filesystems/path-lookup.rst-701-similar function. When REF-walk decrements the count or drops the
--
Documentation/locking/seqlock.rst=82=Read path::
--
Documentation/locking/seqlock.rst-84- do {
Documentation/locking/seqlock.rst:85: seq = read_seqcount_begin(&foo_seqcount);
Documentation/locking/seqlock.rst-86-
--
arch/x86/kernel/cpu/aperfmperf.c=506=int arch_freq_get_on_cpu(int cpu)
--
arch/x86/kernel/cpu/aperfmperf.c-516- do {
arch/x86/kernel/cpu/aperfmperf.c:517: seq = raw_read_seqcount_begin(&s->seq);
arch/x86/kernel/cpu/aperfmperf.c-518- last = s->last_update;
--
arch/x86/kvm/x86.c=2969=static int do_kvmclock_base(s64 *t, u64 *tsc_timestamp)
--
arch/x86/kvm/x86.c-2976- do {
arch/x86/kvm/x86.c:2977: seq = read_seqcount_begin(>od->seq);
arch/x86/kvm/x86.c-2978- ns = gtod->raw_clock.base_cycles;
--
arch/x86/kvm/x86.c=2992=static int do_monotonic(s64 *t, u64 *tsc_timestamp)
--
arch/x86/kvm/x86.c-2999- do {
arch/x86/kvm/x86.c:3000: seq = read_seqcount_begin(>od->seq);
arch/x86/kvm/x86.c-3001- ns = gtod->clock.base_cycles;
--
arch/x86/kvm/x86.c=3011=static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
--
arch/x86/kvm/x86.c-3018- do {
arch/x86/kvm/x86.c:3019: seq = read_seqcount_begin(>od->seq);
arch/x86/kvm/x86.c-3020- ts->tv_sec = gtod->wall_time_sec;
--
arch/x86/kvm/x86.c=3203=static unsigned long get_cpu_tsc_khz(void)
--
arch/x86/kvm/x86.c-3210-
arch/x86/kvm/x86.c:3211:/* Called within read_seqcount_begin/retry for kvm->pvclock_sc. */
arch/x86/kvm/x86.c-3212-static void __get_kvmclock(struct kvm *kvm, struct kvm_clock_data *data)
--
arch/x86/kvm/x86.c=3247=static void get_kvmclock(struct kvm *kvm, struct kvm_clock_data *data)
--
arch/x86/kvm/x86.c-3252- do {
arch/x86/kvm/x86.c:3253: seq = read_seqcount_begin(&ka->pvclock_sc);
arch/x86/kvm/x86.c-3254- __get_kvmclock(kvm, data);
--
arch/x86/kvm/x86.c=3314=int kvm_guest_time_update(struct kvm_vcpu *v)
--
arch/x86/kvm/x86.c-3332- do {
arch/x86/kvm/x86.c:3333: seq = read_seqcount_begin(&ka->pvclock_sc);
arch/x86/kvm/x86.c-3334- use_master_clock = ka->use_master_clock;
--
arch/x86/kvm/x86.c=3459=uint64_t kvm_get_wall_clock_epoch(struct kvm *kvm)
--
arch/x86/kvm/x86.c-3468- do {
arch/x86/kvm/x86.c:3469: seq = read_seqcount_begin(&ka->pvclock_sc);
arch/x86/kvm/x86.c-3470-
--
block/blk-iocost.c=1042=static void ioc_now(struct ioc *ioc, struct ioc_now *now)
--
block/blk-iocost.c-1059- do {
block/blk-iocost.c:1060: seq = read_seqcount_begin(&ioc->period_seqcount);
block/blk-iocost.c-1061- now->vnow = ioc->period_at_vtime +
--
drivers/gpu/drm/i915/gt/intel_execlists_submission.c=3411=static ktime_t execlists_engine_busyness(struct intel_engine_cs *engine,
--
drivers/gpu/drm/i915/gt/intel_execlists_submission.c-3418- do {
drivers/gpu/drm/i915/gt/intel_execlists_submission.c:3419: seq = read_seqcount_begin(&stats->lock);
drivers/gpu/drm/i915/gt/intel_execlists_submission.c-3420- total = __execlists_engine_busyness(engine, now);
--
drivers/gpu/drm/i915/gt/intel_gt_pm.c=419=ktime_t intel_gt_get_awake_time(const struct intel_gt *gt)
--
drivers/gpu/drm/i915/gt/intel_gt_pm.c-424- do {
drivers/gpu/drm/i915/gt/intel_gt_pm.c:425: seq = read_seqcount_begin(>->stats.lock);
drivers/gpu/drm/i915/gt/intel_gt_pm.c-426- total = __intel_gt_get_awake_time(gt);
--
drivers/gpu/drm/v3d/v3d_drv.c=186=void v3d_get_stats(const struct v3d_stats *stats, u64 timestamp,
--
drivers/gpu/drm/v3d/v3d_drv.c-191- do {
drivers/gpu/drm/v3d/v3d_drv.c:192: seq = raw_read_seqcount_begin(&stats->lock);
drivers/gpu/drm/v3d/v3d_drv.c-193- *active_runtime = stats->enabled_ns;
--
drivers/hv/mshv_eventfd.c=195=static void mshv_assert_irq_slow(struct mshv_irqfd *irqfd)
--
drivers/hv/mshv_eventfd.c-214- do {
drivers/hv/mshv_eventfd.c:215: seq = read_seqcount_begin(&irqfd->irqfd_irqe_sc);
drivers/hv/mshv_eventfd.c-216- } while (read_seqcount_retry(&irqfd->irqfd_irqe_sc, seq));
--
drivers/hv/mshv_eventfd.c=295=static int mshv_irqfd_wakeup(wait_queue_entry_t *wait, unsigned int mode,
--
drivers/hv/mshv_eventfd.c-311- do {
drivers/hv/mshv_eventfd.c:312: seq = read_seqcount_begin(&irqfd->irqfd_irqe_sc);
drivers/hv/mshv_eventfd.c-313- } while (read_seqcount_retry(&irqfd->irqfd_irqe_sc, seq));
--
drivers/md/raid5.c=580=static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
--
drivers/md/raid5.c-592-retry:
drivers/md/raid5.c:593: seq = read_seqcount_begin(&conf->gen_lock);
drivers/md/raid5.c-594- sh->generation = conf->generation - previous;
--
drivers/md/raid5.c=5949=static enum stripe_result make_stripe_request(struct mddev *mddev,
--
drivers/md/raid5.c-5960-
drivers/md/raid5.c:5961: seq = read_seqcount_begin(&conf->gen_lock);
drivers/md/raid5.c-5962- loc = get_reshape_loc(mddev, conf, logical_sector);
--
drivers/net/ethernet/fungible/funeth/funeth_ethtool.c=203=static int fun_get_link_ksettings(struct net_device *netdev,
--
drivers/net/ethernet/fungible/funeth/funeth_ethtool.c-216- do {
drivers/net/ethernet/fungible/funeth/funeth_ethtool.c:217: seq = read_seqcount_begin(&fp->link_seq);
drivers/net/ethernet/fungible/funeth/funeth_ethtool.c-218- link_up = netif_carrier_ok(netdev);
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c=1838=axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-1860- do {
drivers/net/ethernet/xilinx/xilinx_axienet_main.c:1861: start = read_seqcount_begin(&lp->hw_stats_seqcount);
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-1862- stats->rx_length_errors =
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c=2306=static void axienet_ethtools_get_ethtool_stats(struct net_device *dev,
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-2313- do {
drivers/net/ethernet/xilinx/xilinx_axienet_main.c:2314: start = read_seqcount_begin(&lp->hw_stats_seqcount);
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-2315- data[0] = axienet_stat(lp, STAT_RX_BYTES);
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c=2364=axienet_ethtools_get_pause_stats(struct net_device *dev,
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-2373- do {
drivers/net/ethernet/xilinx/xilinx_axienet_main.c:2374: start = read_seqcount_begin(&lp->hw_stats_seqcount);
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-2375- pause_stats->tx_pause_frames =
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c=2383=axienet_ethtool_get_eth_mac_stats(struct net_device *dev,
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-2392- do {
drivers/net/ethernet/xilinx/xilinx_axienet_main.c:2393: start = read_seqcount_begin(&lp->hw_stats_seqcount);
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-2394- mac_stats->FramesTransmittedOK =
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c=2428=axienet_ethtool_get_eth_ctrl_stats(struct net_device *dev,
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-2437- do {
drivers/net/ethernet/xilinx/xilinx_axienet_main.c:2438: start = read_seqcount_begin(&lp->hw_stats_seqcount);
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-2439- ctrl_stats->MACControlFramesTransmitted =
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c=2460=axienet_ethtool_get_rmon_stats(struct net_device *dev,
--
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-2470- do {
drivers/net/ethernet/xilinx/xilinx_axienet_main.c:2471: start = read_seqcount_begin(&lp->hw_stats_seqcount);
drivers/net/ethernet/xilinx/xilinx_axienet_main.c-2472- rmon_stats->undersize_pkts =
--
fs/btrfs/volumes.h=257=btrfs_device_get_##name(const struct btrfs_device *dev) \
--
fs/btrfs/volumes.h-262- do { \
fs/btrfs/volumes.h:263: seq = read_seqcount_begin(&dev->data_seqcount); \
fs/btrfs/volumes.h-264- size = dev->name; \
--
fs/dcache.c=361=void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
--
fs/dcache.c-367-retry:
fs/dcache.c:368: seq = read_seqcount_begin(&dentry->d_seq);
fs/dcache.c-369- s = READ_ONCE(dentry->d_name.name);
--
fs/namei.c=1100=static int set_root(struct nameidata *nd)
--
fs/namei.c-1117- nd->root = fs->root;
fs/namei.c:1118: nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
fs/namei.c-1119- } while (read_seqretry(&fs->seq, seq));
--
fs/namei.c=1483=static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
--
fs/namei.c-1495- path->dentry = mountpoint;
fs/namei.c:1496: *seqp = read_seqcount_begin(&mountpoint->d_seq);
fs/namei.c-1497- return true;
--
fs/namei.c=1682=static bool __follow_mount_rcu(struct nameidata *nd, struct path *path)
--
fs/namei.c-1707- nd->state |= ND_JUMPED;
fs/namei.c:1708: nd->next_seq = read_seqcount_begin(&dentry->d_seq);
fs/namei.c-1709- flags = dentry->d_flags;
--
fs/namei.c=2151=static struct dentry *follow_dotdot_rcu(struct nameidata *nd)
--
fs/namei.c-2174- parent = old->d_parent;
fs/namei.c:2175: nd->next_seq = read_seqcount_begin(&parent->d_seq);
fs/namei.c-2176- // makes sure that non-RCU pathwalk could reach this state
--
fs/namei.c=2673=static const char *path_init(struct nameidata *nd, unsigned flags)
--
fs/namei.c-2691-
fs/namei.c:2692: nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
fs/namei.c:2693: nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
fs/namei.c-2694- smp_rmb();
--
fs/namei.c-2703- if (flags & LOOKUP_RCU) {
fs/namei.c:2704: nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
fs/namei.c-2705- nd->root_seq = nd->seq;
--
fs/namei.c-2731- nd->inode = nd->path.dentry->d_inode;
fs/namei.c:2732: nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
fs/namei.c-2733- } while (read_seqretry(&fs->seq, seq));
--
fs/namei.c-2759- nd->inode = nd->path.dentry->d_inode;
fs/namei.c:2760: nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
fs/namei.c-2761- } else {
--
fs/userfaultfd.c=1201=static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
--
fs/userfaultfd.c-1222- do {
fs/userfaultfd.c:1223: seq = read_seqcount_begin(&ctx->refile_seq);
fs/userfaultfd.c-1224- need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
--
include/linux/cpuset.h=144=static inline unsigned int read_mems_allowed_begin(void)
--
include/linux/cpuset.h-148-
include/linux/cpuset.h:149: return read_seqcount_begin(¤t->mems_allowed_seq);
include/linux/cpuset.h-150-}
--
include/linux/fs.h=1128=static inline loff_t i_size_read(const struct inode *inode)
--
include/linux/fs.h-1134- do {
include/linux/fs.h:1135: seq = read_seqcount_begin(&inode->i_size_seqcount);
include/linux/fs.h-1136- i_size = inode->i_size;
--
include/linux/netfilter/x_tables.h=326=void xt_free_table_info(struct xt_table_info *info);
--
include/linux/netfilter/x_tables.h-331- * Packet processing changes the seqcount only if no recursion happened
include/linux/netfilter/x_tables.h:332: * get_counters() can use read_seqcount_begin()/read_seqcount_retry(),
include/linux/netfilter/x_tables.h-333- * because we use the normal seqcount convention :
--
include/linux/seqlock.h-29- * read begin/retry/end. For readers, typically there is a call to
include/linux/seqlock.h:30: * read_seqcount_begin() and read_seqcount_retry(), however, there are more
include/linux/seqlock.h-31- * esoteric cases which do not follow this pattern.
--
include/linux/seqlock.h=230=SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
--
include/linux/seqlock.h-266-/**
include/linux/seqlock.h:267: * __read_seqcount_begin() - begin a seqcount_t read section
include/linux/seqlock.h-268- * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
--
include/linux/seqlock.h-271- */
include/linux/seqlock.h:272:#define __read_seqcount_begin(s) \
include/linux/seqlock.h-273-({ \
--
include/linux/seqlock.h-283-/**
include/linux/seqlock.h:284: * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
include/linux/seqlock.h-285- * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
--
include/linux/seqlock.h-288- */
include/linux/seqlock.h:289:#define raw_read_seqcount_begin(s) __read_seqcount_begin(s)
include/linux/seqlock.h-290-
include/linux/seqlock.h-291-/**
include/linux/seqlock.h:292: * read_seqcount_begin() - begin a seqcount_t read critical section
include/linux/seqlock.h-293- * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
--
include/linux/seqlock.h-296- */
include/linux/seqlock.h:297:#define read_seqcount_begin(s) \
include/linux/seqlock.h-298-({ \
include/linux/seqlock.h-299- seqcount_lockdep_reader_access(seqprop_const_ptr(s)); \
include/linux/seqlock.h:300: raw_read_seqcount_begin(s); \
include/linux/seqlock.h-301-})
--
include/linux/seqlock.h-350- * raw_seqcount_begin opens a read critical section of the given
include/linux/seqlock.h:351: * seqcount_t. Unlike read_seqcount_begin(), this function will not wait
include/linux/seqlock.h-352- * for the count to stabilize. If a writer is active when it begins, it
--
include/linux/seqlock.h-373- * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
include/linux/seqlock.h:374: * @start: count, from read_seqcount_begin()
include/linux/seqlock.h-375- *
--
include/linux/seqlock.h=389=static inline int do___read_seqcount_retry(const seqcount_t *s, unsigned start)
--
include/linux/seqlock.h-397- * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
include/linux/seqlock.h:398: * @start: count, from read_seqcount_begin()
include/linux/seqlock.h-399- *
--
include/linux/seqlock.h=522=static inline void do_write_seqcount_end(seqcount_t *s)
--
include/linux/seqlock.h-550- * do {
include/linux/seqlock.h:551: * int s = read_seqcount_begin(&seq);
include/linux/seqlock.h-552- *
--
include/linux/seqlock.h=835=static inline unsigned read_seqbegin(const seqlock_t *sl)
--
include/linux/seqlock.h-837-{
include/linux/seqlock.h:838: return read_seqcount_begin(&sl->seqcount);
include/linux/seqlock.h-839-}
--
include/linux/u64_stats_sync.h=194=static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
include/linux/u64_stats_sync.h-195-{
include/linux/u64_stats_sync.h:196: return read_seqcount_begin(&syncp->seq);
include/linux/u64_stats_sync.h-197-}
--
include/net/ip_vs.h=505=struct ip_vs_rht {
--
include/net/ip_vs.h-568- for (retry = 1, sc = &(t)->seqc[(bucket) & (t)->seqc_mask]; \
include/net/ip_vs.h:569: retry && ({ seq = read_seqcount_begin(sc); 1; }); \
include/net/ip_vs.h-570- retry = read_seqcount_retry(sc, seq))
--
include/net/netfilter/nf_conntrack.h=337=nf_conntrack_get_ht(struct hlist_nulls_head **hash, unsigned int *hsize)
--
include/net/netfilter/nf_conntrack.h-342- do {
include/net/netfilter/nf_conntrack.h:343: sequence = read_seqcount_begin(&nf_conntrack_generation);
include/net/netfilter/nf_conntrack.h-344- hsz = nf_conntrack_htable_size;
--
kernel/cgroup/cgroup.c=3806=static int cgroup_core_local_stat_show(struct seq_file *seq, void *v)
--
kernel/cgroup/cgroup.c-3812- do {
kernel/cgroup/cgroup.c:3813: sequence = read_seqcount_begin(&cgrp->freezer.freeze_seq);
kernel/cgroup/cgroup.c-3814- freeze_time = cgrp->freezer.frozen_nsec;
--
kernel/events/uprobes.c=901=static struct uprobe *find_uprobe_rcu(struct inode *inode, loff_t offset)
--
kernel/events/uprobes.c-912- do {
kernel/events/uprobes.c:913: seq = read_seqcount_begin(&uprobes_seqcount);
kernel/events/uprobes.c-914- node = rb_find_rcu(&key, &uprobes_tree, __uprobe_cmp_key);
--
kernel/sched/cputime.c=822=u64 task_gtime(struct task_struct *t)
--
kernel/sched/cputime.c-831- do {
kernel/sched/cputime.c:832: seq = read_seqcount_begin(&vtime->seqcount);
kernel/sched/cputime.c-833-
--
kernel/sched/cputime.c=848=bool task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
--
kernel/sched/cputime.c-862- ret = false;
kernel/sched/cputime.c:863: seq = read_seqcount_begin(&vtime->seqcount);
kernel/sched/cputime.c-864-
--
kernel/sched/cputime.c=923=static int kcpustat_field_vtime(u64 *cpustat,
--
kernel/sched/cputime.c-933-
kernel/sched/cputime.c:934: seq = read_seqcount_begin(&vtime->seqcount);
kernel/sched/cputime.c-935-
--
kernel/sched/cputime.c=1012=static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
--
kernel/sched/cputime.c-1023-
kernel/sched/cputime.c:1024: seq = read_seqcount_begin(&vtime->seqcount);
kernel/sched/cputime.c-1025-
--
kernel/sched/psi.c=191=static inline u32 psi_read_begin(int cpu)
kernel/sched/psi.c-192-{
kernel/sched/psi.c:193: return read_seqcount_begin(per_cpu_ptr(&psi_seq, cpu));
kernel/sched/psi.c-194-}
--
kernel/time/hrtimer.c=1853=bool hrtimer_active(const struct hrtimer *timer)
--
kernel/time/hrtimer.c-1859- base = READ_ONCE(timer->base);
kernel/time/hrtimer.c:1860: seq = raw_read_seqcount_begin(&base->seq);
kernel/time/hrtimer.c-1861-
--
kernel/time/jiffies.c=47=u64 get_jiffies_64(void)
--
kernel/time/jiffies.c-52- do {
kernel/time/jiffies.c:53: seq = read_seqcount_begin(&jiffies_seq);
kernel/time/jiffies.c-54- ret = jiffies_64;
--
kernel/time/tick-common.c=152=void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
--
kernel/time/tick-common.c-167- do {
kernel/time/tick-common.c:168: seq = read_seqcount_begin(&jiffies_seq);
kernel/time/tick-common.c-169- next = tick_next_period;
--
kernel/time/tick-sched.c=57=static void tick_do_update_jiffies64(ktime_t now)
--
kernel/time/tick-sched.c-81- do {
kernel/time/tick-sched.c:82: seq = read_seqcount_begin(&jiffies_seq);
kernel/time/tick-sched.c-83- nextp = tick_next_period;
--
kernel/time/tick-sched.c=786=static u64 get_cpu_sleep_time_us(struct tick_sched *ts, ktime_t *sleeptime,
--
kernel/time/tick-sched.c-799- do {
kernel/time/tick-sched.c:800: seq = read_seqcount_begin(&ts->idle_sleeptime_seq);
kernel/time/tick-sched.c-801-
--
kernel/time/tick-sched.c=910=u64 get_jiffies_update(unsigned long *basej)
--
kernel/time/tick-sched.c-916- do {
kernel/time/tick-sched.c:917: seq = read_seqcount_begin(&jiffies_seq);
kernel/time/tick-sched.c-918- basemono = last_jiffies_update;
--
kernel/time/timekeeping.c=884=bool ktime_expiry_to_cycles(enum clocksource_ids id, ktime_t expires_ns, u64 *cycles)
--
kernel/time/timekeeping.c-903- do {
kernel/time/timekeeping.c:904: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-905-
--
kernel/time/timekeeping.c=934=void ktime_get_real_ts64(struct timespec64 *ts)
--
kernel/time/timekeeping.c-942- do {
kernel/time/timekeeping.c:943: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-944-
--
kernel/time/timekeeping.c=955=ktime_t ktime_get(void)
--
kernel/time/timekeeping.c-964- do {
kernel/time/timekeeping.c:965: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-966- base = tk->tkr_mono.base;
--
kernel/time/timekeeping.c=975=u32 ktime_get_resolution_ns(void)
--
kernel/time/timekeeping.c-983- do {
kernel/time/timekeeping.c:984: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-985- nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift;
--
kernel/time/timekeeping.c=998=ktime_t ktime_get_with_offset(enum tk_offsets offs)
--
kernel/time/timekeeping.c-1008- do {
kernel/time/timekeeping.c:1009: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1010- base = ktime_add(tk->tkr_mono.base, *offset);
--
kernel/time/timekeeping.c=1020=ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs)
--
kernel/time/timekeeping.c-1030- do {
kernel/time/timekeeping.c:1031: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1032- base = ktime_add(tk->tkr_mono.base, *offset);
--
kernel/time/timekeeping.c=1046=ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
--
kernel/time/timekeeping.c-1060- do {
kernel/time/timekeeping.c:1061: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1062- tconv = ktime_add(tmono, *offset);
--
kernel/time/timekeeping.c=1072=ktime_t ktime_get_raw(void)
--
kernel/time/timekeeping.c-1079- do {
kernel/time/timekeeping.c:1080: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1081- base = tk->tkr_raw.base;
--
kernel/time/timekeeping.c=1098=void ktime_get_ts64(struct timespec64 *ts)
--
kernel/time/timekeeping.c-1107- do {
kernel/time/timekeeping.c:1108: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1109- ts->tv_sec = tk->xtime_sec;
--
kernel/time/timekeeping.c=1149=time64_t ktime_get_real_seconds(void)
--
kernel/time/timekeeping.c-1158- do {
kernel/time/timekeeping.c:1159: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1160- seconds = tk->xtime_sec;
--
kernel/time/timekeeping.c=1189=void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
--
kernel/time/timekeeping.c-1202- do {
kernel/time/timekeeping.c:1203: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1204- now = tk_clock_read(&tk->tkr_mono);
--
kernel/time/timekeeping.c=1414=bool ktime_real_to_base_clock(ktime_t treal, enum clocksource_ids base_id, u64 *cycles)
--
kernel/time/timekeeping.c-1420- do {
kernel/time/timekeeping.c:1421: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1422- if ((u64)treal < tk->tkr_mono.base_real)
--
kernel/time/timekeeping.c=1447=int get_device_system_crosststamp(int (*get_time_fn)
--
kernel/time/timekeeping.c-1466- do {
kernel/time/timekeeping.c:1467: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1468- /*
--
kernel/time/timekeeping.c=1800=void ktime_get_raw_ts64(struct timespec64 *ts)
--
kernel/time/timekeeping.c-1806- do {
kernel/time/timekeeping.c:1807: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1808- ts->tv_sec = tk->raw_sec;
--
kernel/time/timekeeping.c=1855=int timekeeping_valid_for_hres(void)
--
kernel/time/timekeeping.c-1861- do {
kernel/time/timekeeping.c:1862: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1863-
--
kernel/time/timekeeping.c=1874=u64 timekeeping_max_deferment(void)
--
kernel/time/timekeeping.c-1880- do {
kernel/time/timekeeping.c:1881: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-1882-
--
kernel/time/timekeeping.c=2575=void ktime_get_coarse_real_ts64(struct timespec64 *ts)
--
kernel/time/timekeeping.c-2580- do {
kernel/time/timekeeping.c:2581: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-2582-
--
kernel/time/timekeeping.c=2597=void ktime_get_coarse_real_ts64_mg(struct timespec64 *ts)
--
kernel/time/timekeeping.c-2604- do {
kernel/time/timekeeping.c:2605: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-2606- *ts = tk_xtime_coarse(tk);
--
kernel/time/timekeeping.c=2635=void ktime_get_real_ts64_mg(struct timespec64 *ts)
--
kernel/time/timekeeping.c-2643- do {
kernel/time/timekeeping.c:2644: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-2645-
--
kernel/time/timekeeping.c=2676=void ktime_get_coarse_ts64(struct timespec64 *ts)
--
kernel/time/timekeeping.c-2682- do {
kernel/time/timekeeping.c:2683: seq = read_seqcount_begin(&tk_core.seq);
kernel/time/timekeeping.c-2684-
--
kernel/time/timekeeping.c=2716=ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,
]
]
|
| 16/2 |
2026/05/18 16:24 |
llm |
expert |
0m
Model:
Tokens:
input: 24468
output: 41
thoughts: 5004
Thoughts: **Benign Data Race in Timekeeping Seqlock: Analysis of Exploitability and Triggering Conditions**
Okay, so I've been digging into this KCSAN report. It's flagging a data race between the `timekeeping_update_from_shadow` function, the writer that uses `memcpy` and a seqlock, and `ktime_get_real_ts64`, the reader, which uses `read_seqcount_begin` and `read_seqcount_retry`. `timekeeping_update_from_shadow` is called by the timer interrupt, and `ktime_get_real_ts64` is called by network packet processing and other code paths. Essentially, the reader performs plain reads of the timekeeping data, while the writer does a plain write. KCSAN sees this as a race.
The crux of the matter is that this is a *classic seqlock pattern* in Linux. The read operation is designed to tolerate this race. If a writer is active during a read, the sequence number changes, and the reader will retry.
However, KCSAN highlights this, which has led me to think through all potential points of failure. The question is, can this benign data race *become* exploitable somehow? Could compiler optimizations break the seqlock? The writer uses memory barriers before and after the `memcpy`, and the reader also has barriers. The values are safely read into local variables before the retry. I’m satisfied that the seqlock design prevents the use of torn values. The compiler cannot reload values after a retry because of the memory barrier. This is a standard seqlock pattern that generates a KCSAN warning because the accesses are not marked with `READ_ONCE`, `WRITE_ONCE` or `data_race()`.
I also verified that the critical parts in the reader – the calculations within the `timekeeping_get_ns` function – don't contain any division-by-zero or other exploitable operations. The reader reads `tkr->cycle_last`, `tkr->mask`, and related variables. The critical aspect to consider is whether a torn read can cause a crash before a retry due to pointer corruption. A `memcpy` call in `timekeeping_update_from_shadow` is the root of the issue. The contents of `tkr->clock` is copied via `memcpy` and a torn read could have caused a crash if `memcpy` were to update `tkr->clock` in a way that an intermediary value may be accessed by the reader.
I reviewed the source to verify that the pointer is safely read inside the lock.
Given that the seqlock retry mechanism handles the race, I'm confident that this data race is *not* exploitable. It's just a KCSAN false positive, a known issue. It's not a denial-of-service vulnerability either because KCSAN is not enabled in production builds by default.
Now, let's consider the triggering conditions. This can be triggered by any unprivileged user process that receives a network packet or reads the system clock, e.g. using `clock_gettime`. It can be reached from user namespaces, by a VM guest and host, and through the filesystem.
The bug can definitely be reached from a typical unprivileged user process.
Therefore:
* **Exploitable: False**
* **DenialOfService: False**
|
| 17/2 |
2026/05/18 16:25 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:kernel/time/timekeeping.c IncludeLines:true Name:change_clocksource]
Results: map[SourceCode:1721: /*
1722: * change_clocksource - Swaps clocksources if a new one is available
1723: *
1724: * Accumulates current time interval and initializes new clocksource
1725: */
1726: static int change_clocksource(void *data)
1727: {
1728: struct clocksource *new = data, *old = NULL;
1729:
1730: /*
1731: * If the clocksource is in a module, get a module reference.
1732: * Succeeds for built-in code (owner == NULL) as well. Abort if the
1733: * reference can't be acquired.
1734: */
1735: if (!try_module_get(new->owner))
1736: return 0;
1737:
1738: /* Abort if the device can't be enabled */
1739: if (new->enable && new->enable(new) != 0) {
1740: module_put(new->owner);
1741: return 0;
1742: }
1743:
1744: scoped_guard (raw_spinlock_irqsave, &tk_core.lock) {
1745: struct timekeeper *tks = &tk_core.shadow_timekeeper;
1746:
1747: timekeeping_forward_now(tks);
1748: old = tks->tkr_mono.clock;
1749: tk_setup_internals(tks, new);
1750: timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL);
1751: }
1752:
1753: tk_aux_update_clocksource();
1754:
1755: if (old) {
1756: if (old->disable)
1757: old->disable(old);
1758: module_put(old->owner);
1759: }
1760:
1761: return 0;
1762: }
SourceFile:kernel/time/timekeeping.c]
|
| 18/2 |
2026/05/18 16:25 |
llm |
expert |
0m
Model:
Tokens:
input: 30861
output: 42
thoughts: 182
Thoughts: **My Analysis of a Potential Clock Source Issue**
Okay, so I'm looking at this clocksource implementation and I've got a potential race condition in my head. If `change_clocksource` is called, `tk_setup_internals` updates the `tks->tkr_mono.clock` pointer to point to a new clock source. That's straightforward. Then, `timekeeping_update_from_shadow` copies that new `clock` pointer to the `tk_core.timekeeper`. Now, here's where it gets interesting and potentially problematic. If a `memcpy` operation is interrupted, it could tear the pointer – leading to a partially copied, garbage value.
My concern is that if the `tk_clock_read` function were to be called *during* that `memcpy` operation, it could end up dereferencing that garbage pointer, and that’s a crash waiting to happen. I need to investigate how `tk_clock_read` handles this.
Hold on, I see it… `tk_clock_read` actually *does* use a `clock` pointer. Let me look at the code:
```c
static inline u64 tk_clock_read(const struct tk_read_base *tkr)
{
struct clocksource *clock = READ_ONCE(tkr->clock);
return clock->read(clock);
}
```
Wait, I see it. It uses `READ_ONCE(tkr->clock)`! That's how it is protected against the race condition. I'm going to verify this assumption but I'm pretty confident now.
|
| 19/2 |
2026/05/18 16:25 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:kernel/time/timekeeping.c IncludeLines:true Name:tk_clock_read]
Results: map[SourceCode: 279: /*
280: * tk_clock_read - atomic clocksource read() helper
281: *
282: * This helper is necessary to use in the read paths because, while the
283: * seqcount ensures we don't return a bad value while structures are updated,
284: * it doesn't protect from potential crashes. There is the possibility that
285: * the tkr's clocksource may change between the read reference, and the
286: * clock reference passed to the read function. This can cause crashes if
287: * the wrong clocksource is passed to the wrong read function.
288: * This isn't necessary to use when holding the tk_core.lock or doing
289: * a read of the fast-timekeeper tkrs (which is protected by its own locking
290: * and update logic).
291: */
292: static __always_inline u64 tk_clock_read(const struct tk_read_base *tkr)
293: {
294: struct clocksource *clock = READ_ONCE(tkr->clock);
295:
296: if (static_branch_likely(&clocksource_read_inlined))
297: return arch_inlined_clocksource_read(clock);
298:
299: return clock->read(clock);
300: }
SourceFile:kernel/time/timekeeping.c]
|
| 20/2 |
2026/05/18 16:25 |
llm |
expert |
0m
Model:
Tokens:
input: 31854
output: 34
thoughts: 476
Thoughts: **Analyzing Potential Data Races in the Timekeeper Structure**
Okay, so I'm looking at this `tk_clock_read` function and the use of `READ_ONCE(tkr->clock)` to avoid compiler reloads of the pointer. My initial thought was, "Ah, that's clever, preventing a stale pointer." But then the writer uses `memcpy` in the writer function, which is a plain write. Immediately, alarm bells start ringing. If `memcpy` does a byte-by-byte copy of the struct, there's a serious chance of tearing that pointer! Even if the struct contains other members.
I started thinking, "Typically, `memcpy` of a `struct timekeeper` (304 bytes) is optimized to use word-sized or vector instructions." But, and this is crucial, `memcpy` *isn't* guaranteed to be atomic. So, even with those optimizations, there's a window for problems.
Then I went back to the comment in `tk_clock_read`, which mentions the risk of the clocksource changing between the read reference and the function call, potentially leading to crashes if the wrong clocksource is passed. This makes me realize that `READ_ONCE` isn't there to protect against `memcpy` tearing a pointer. Instead, it's about ensuring the same clock pointer is used consistently in the `clock->read(clock)` function call and the argument.
The implications hit me then: If `memcpy` does tear the pointer, `READ_ONCE` will then read this torn address, and the `clock->read` call would jump to a garbage address! This has a major problem! I know `memcpy` can be optimized in the kernel, but is there any guarantee of no pointer tearing if it's aligned? It is optimized, but KCSAN is reporting a data race because of this plain write.
The KCSAN report confirms my concerns. It shows a write of 304 bytes by an interrupt and a read of 8 bytes by a task, so that's a u64 in `struct timekeeper`, with a data race! The report shows the read offset is 16 bytes (0x10) from the write, this is not good! I need to determine what member is at that offset within the `struct timekeeper` to understand the data corruption implications. This needs further investigation.
|
| 21/2 |
2026/05/18 16:25 |
tool |
codesearch-struct-layout |
0m
Args: map[ContextFile:kernel/time/timekeeping.c Name:timekeeper]
Results: map[Fields:[map[Name:tkr_mono OffsetBits:0 SizeBits:448] map[Name:xtime_sec OffsetBits:448 SizeBits:64] map[Name:ktime_sec OffsetBits:512 SizeBits:64] map[Name:wall_to_monotonic OffsetBits:576 SizeBits:128] map[Name:offs_real OffsetBits:704 SizeBits:64] map[Name:offs_boot OffsetBits:768 SizeBits:64] map[Name: OffsetBits:832 SizeBits:64] map[Name:coarse_nsec OffsetBits:896 SizeBits:32] map[Name:id OffsetBits:928 SizeBits:32] map[Name:tkr_raw OffsetBits:960 SizeBits:448] map[Name:raw_sec OffsetBits:1408 SizeBits:64] map[Name:cs_id OffsetBits:1472 SizeBits:32] map[Name:cs_ns_to_cyc_mult OffsetBits:1504 SizeBits:32] map[Name:cs_ns_to_cyc_shift OffsetBits:1536 SizeBits:32] map[Name:cs_ns_to_cyc_maxns OffsetBits:1600 SizeBits:64] map[Name:clock_was_set_seq OffsetBits:1664 SizeBits:32] map[Name:cs_was_changed_seq OffsetBits:1696 SizeBits:8] map[Name:clock_valid OffsetBits:1704 SizeBits:8] map[Name: OffsetBits:1728 SizeBits:128] map[Name:cycle_interval OffsetBits:1856 SizeBits:64] map[Name:xtime_interval OffsetBits:1920 SizeBits:64] map[Name:xtime_remainder OffsetBits:1984 SizeBits:64] map[Name:raw_interval OffsetBits:2048 SizeBits:64] map[Name:next_leap_ktime OffsetBits:2112 SizeBits:64] map[Name:ntp_tick OffsetBits:2176 SizeBits:64] map[Name:ntp_error OffsetBits:2240 SizeBits:64] map[Name:ntp_error_shift OffsetBits:2304 SizeBits:32] map[Name:ntp_err_mult OffsetBits:2336 SizeBits:32] map[Name:skip_second_overflow OffsetBits:2368 SizeBits:32] map[Name:tai_offset OffsetBits:2400 SizeBits:32]]]
|
| 22/2 |
2026/05/18 16:25 |
llm |
expert |
0m
Model:
Tokens:
input: 33576
output: 37
thoughts: 31
Thoughts: Here's my summary, as though I'm thinking through this process:
**Decoding This Read Operation**
Okay, so I'm looking at a read operation, and it seems to have started at byte offset 16, which is represented as 0x10 in hexadecimal – no surprises there. My primary thought is, let's nail down what that data structure at this offset *actually* is. I need to refresh my understanding of how this read is structured, so let's check the layout of `struct tk_read_base`. Knowing the details of that struct is key to understanding the context of that data. I'm basically gearing up to understand what's *actually* happening during this read operation, by first understanding the basics. Let me pull up that `struct tk_read_base` definition...
|
| 23/2 |
2026/05/18 16:25 |
tool |
codesearch-struct-layout |
0m
Args: map[ContextFile:kernel/time/timekeeping.c Name:tk_read_base]
Results: map[Fields:[map[Name:clock OffsetBits:0 SizeBits:64] map[Name:mask OffsetBits:64 SizeBits:64] map[Name:cycle_last OffsetBits:128 SizeBits:64] map[Name:mult OffsetBits:192 SizeBits:32] map[Name:shift OffsetBits:224 SizeBits:32] map[Name:xtime_nsec OffsetBits:256 SizeBits:64] map[Name:base OffsetBits:320 SizeBits:64] map[Name:base_real OffsetBits:384 SizeBits:64]]]
|
| 24/2 |
2026/05/18 16:25 |
llm |
expert |
0m
Model:
Tokens:
input: 33987
output: 43
thoughts: 237
Thoughts: **Analyzing a KCSAN Report: Dissecting a Data Race**
Okay, so I'm looking at this KCSAN report and I need to figure out what's going on. Let's see...offset 16 (0x10) bytes...that's 128 bits. The report says the read involves `cycle_last`! Right, `cycle_last` is a `u64`.
The KCSAN report specifically flags a read to `0xffffffff895d9498` of 8 bytes by task 1037 on CPU 1, where the value changes from `0x000000831bc8e013` to `0x000000831d189180`. Bingo! That's precisely `cycle_last` being updated, most likely by a timer interrupt, I'd bet.
Now, who's reading this value? The report points to `timekeeping_cycles_to_ns` which calls `timekeeping_get_ns`, and that in turn calls `ktime_get_real_ts64`. Hmm, `timekeeping_get_ns` *should* be the one reading `cycle_last`. And yes, that's right... `timekeeping_get_ns` calls `timekeeping_get_delta`, which, of course, *does* read `tkr->cycle_last`.
The million-dollar question is: is `cycle_last` being read with `READ_ONCE`? I need to check how `timekeeping_get_delta` is implemented to confirm. That's the key to understanding the potential data race. Let's dive in.
|
| 25/2 |
2026/05/18 16:26 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:kernel/time/timekeeping.c IncludeLines:true Name:timekeeping_get_delta]
Results: map[SourceCode: SourceFile:]
|
| 26/2 |
2026/05/18 16:26 |
llm |
expert |
0m
Model:
Tokens:
input: 34668
output: 20
thoughts: 24
Thoughts: Okay, here's my interpretation of those thoughts, tailored for an expert audience, and delivered as a first-person summary:
**Initial Assessment: Potential Code Optimization Opportunity**
Right, let me see... "timekeeping_get_delta"... that's a familiar function name. My immediate hunch is that this might be a potential candidate for optimization. I suspect it's likely implemented as a macro or a static inline function. Given the frequency of calls to timekeeping functions in performance-critical code, this would be the first thing I would check. I'll need to know whether the function has side effects, and what the source is.
My plan then is, rather than immediately diving into the code, I'll start with a `grep` to quickly locate all instances of its usage within the codebase. That should give me the context, and allow me to move onto the next phase of the investigation.
|
| 27/2 |
2026/05/18 16:26 |
tool |
grepper |
0m
Error:
Args: map[Expression:timekeeping_get_delta]
Results:
|
| 28/2 |
2026/05/18 16:26 |
llm |
expert |
0m
Model:
Tokens:
input: 34915
output: 43
thoughts: 42
Thoughts: Okay, here's my take on those thoughts, framed as an expert's internal monologue:
**Checking Kernel Timekeeping**
Hmm, I could have sworn I encountered something relevant to this during my last review, but I'm getting a nagging feeling that I might be misremembering, or even worse, hallucinating! Best to verify the information. Let's not waste any time. Instead of relying on memory, let's go straight to the source. I need to cross-reference my thoughts. I'll need to check the `timekeeping_get_ns` function. Specifically, I'll examine the definition located in `kernel/time/timekeeping.c`. This should provide the definitive answer and make my conclusions more sound.
|
| 29/2 |
2026/05/18 16:26 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:kernel/time/timekeeping.c IncludeLines:true Name:timekeeping_get_ns]
Results: map[SourceCode: 447: static __always_inline u64 timekeeping_get_ns(const struct tk_read_base *tkr)
448: {
449: return timekeeping_cycles_to_ns(tkr, tk_clock_read(tkr));
450: }
SourceFile:kernel/time/timekeeping.c]
|
| 30/2 |
2026/05/18 16:26 |
llm |
expert |
0m
Model:
Tokens:
input: 35266
output: 45
thoughts: 14
Thoughts: Okay, here's my attempt at summarizing that thought process, tailored for an expert audience:
**Checking `timekeeping_cycles_to_ns`**
Right, let's take a quick look at `timekeeping_cycles_to_ns`. Seems like we're in a situation where we need to convert cycles to nanoseconds, and this function is presumably the key to that. I need to make sure I understand the implementation assumptions and any potential caveats before I go any further. I'll need to remember to scrutinize the underlying clock source it relies upon, whether it's calibrated, and if there are any known limitations or edge cases regarding its precision, drift, or how it handles wrap-around. It's also worth thinking about how it handles multi-threaded environments, if there are any locks or atomics, or potential for contention. This function is likely crucial for performance measurements or time-sensitive operations, so ensuring its accuracy and efficiency is paramount. I'll want to quickly check for any related errata or known issues in the documentation or code comments. Essentially, I'm verifying the reliability and correctness of this time conversion routine before depending on it for any critical calculations.
|
| 31/2 |
2026/05/18 16:26 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:kernel/time/timekeeping.c IncludeLines:true Name:timekeeping_cycles_to_ns]
Results: map[SourceCode: 423: static __always_inline u64 timekeeping_cycles_to_ns(const struct tk_read_base *tkr, u64 cycles)
424: {
425: /* Calculate the delta since the last update_wall_time() */
426: u64 mask = tkr->mask, delta = (cycles - tkr->cycle_last) & mask;
427:
428: /*
429: * This detects both negative motion and the case where the delta
430: * overflows the multiplication with tkr->mult.
431: */
432: if (unlikely(delta > tkr->clock->max_cycles)) {
433: /*
434: * Handle clocksource inconsistency between CPUs to prevent
435: * time from going backwards by checking for the MSB of the
436: * mask being set in the delta.
437: */
438: if (delta & ~(mask >> 1))
439: return tkr->xtime_nsec >> tkr->shift;
440:
441: return delta_to_ns_safe(tkr, delta);
442: }
443:
444: return ((delta * tkr->mult) + tkr->xtime_nsec) >> tkr->shift;
445: }
SourceFile:kernel/time/timekeeping.c]
|
| 32/2 |
2026/05/18 16:26 |
llm |
expert |
0m
Model:
Tokens:
input: 35973
output: 20
thoughts: 711
Thoughts: **Analyzing the KCSAN Data Race Report**
Okay, so KCSAN flagged a plain read of `tkr->cycle_last`, which is part of a timestamping mechanism. My first thought is: can this read, in isolation, cause a crash? Let me break it down. The code reads `tkr->cycle_last` directly, calculates `delta` by subtracting it from the current cycle count, and applying a mask. Even if `tkr->cycle_last` is torn during the read, the resulting `delta` will be garbage. However, the subsequent checks make this harmless.
The code then checks if `delta` exceeds a maximum cycle value, and if so, branches to further checks on the garbage `delta`. Even if `delta` is nonsensical, these checks won't result in any kind of crash. The code then either returns a shifted timestamp or calls `delta_to_ns_safe()`. Neither will crash. Crucially, I see this is all within a `read_seqcount_begin` / `read_seqcount_retry` block. This means any torn value is going to be discarded, the sequence number is checked at the end, and if it changed (as it likely will, because the writer is active), the loop will retry, effectively discarding the bad read. Therefore, there's no path to a crash or observable misbehavior. It's a benign data race.
Now, let's assess the impact. Because there's no exploitable path or crash, and the seqlock retry logic properly handles the torn value, I'm confident in saying:
* Exploitable: false
* DenialOfService: false
* Accessible From Unprivileged Processes: true
* Accessible From User Namespaces: true
* VM Guest Trigger: true
* VM Host Trigger: true
* Ethernet Network Trigger: true
* Other Remote Trigger: true
* Peripheral Trigger: true
* Malicious Filesystem Trigger: true
The prompt is making me think about DoS. Because this is a KCSAN report, and KCSAN isn't enabled in production, and because the race is handled safely by the seqlock retry logic, there's no crash, no hang, no deadlock, or resource leak. It's really a false positive from KCSAN, in that KCSAN correctly identifies the race, but the kernel correctly handles it. Therefore, DoS is indeed false.
Let's review the triggers quickly. Can an unprivileged process trigger this? Yes, by calling `clock_gettime(CLOCK_REALTIME)` or by receiving a packet. I was thinking about vdso here, but a system call will force the execution into kernel land where `ktime_get_real_ts64` might get called, so this is triggerable by a user process. I should double check if `sys_clock_gettime` actually calls `ktime_get_real_ts64`.
|
| 33/2 |
2026/05/18 16:26 |
tool |
grepper |
0m
Args: map[Expression:sys_clock_gettime]
Results: map[Output:arch/alpha/kernel/syscalls/syscall.tbl-350-419 common clock_settime sys_clock_settime
arch/alpha/kernel/syscalls/syscall.tbl:351:420 common clock_gettime sys_clock_gettime
arch/alpha/kernel/syscalls/syscall.tbl-352-421 common clock_getres sys_clock_getres
--
arch/arm/tools/syscall.tbl-280-262 common clock_settime sys_clock_settime32
arch/arm/tools/syscall.tbl:281:263 common clock_gettime sys_clock_gettime32
arch/arm/tools/syscall.tbl-282-264 common clock_getres sys_clock_getres_time32
--
arch/arm/tools/syscall.tbl-420-# 402 is unused
arch/arm/tools/syscall.tbl:421:403 common clock_gettime64 sys_clock_gettime
arch/arm/tools/syscall.tbl-422-404 common clock_settime64 sys_clock_settime
--
arch/arm64/tools/syscall_32.tbl-277-262 common clock_settime sys_clock_settime32
arch/arm64/tools/syscall_32.tbl:278:263 common clock_gettime sys_clock_gettime32
arch/arm64/tools/syscall_32.tbl-279-264 common clock_getres sys_clock_getres_time32
--
arch/arm64/tools/syscall_32.tbl-417-# 402 is unused
arch/arm64/tools/syscall_32.tbl:418:403 common clock_gettime64 sys_clock_gettime
arch/arm64/tools/syscall_32.tbl-419-404 common clock_settime64 sys_clock_settime
--
arch/m68k/kernel/syscalls/syscall.tbl-269-259 common clock_settime sys_clock_settime32
arch/m68k/kernel/syscalls/syscall.tbl:270:260 common clock_gettime sys_clock_gettime32
arch/m68k/kernel/syscalls/syscall.tbl-271-261 common clock_getres sys_clock_getres_time32
--
arch/m68k/kernel/syscalls/syscall.tbl-405-402 common msgctl sys_msgctl
arch/m68k/kernel/syscalls/syscall.tbl:406:403 common clock_gettime64 sys_clock_gettime
arch/m68k/kernel/syscalls/syscall.tbl-407-404 common clock_settime64 sys_clock_settime
--
arch/microblaze/kernel/syscalls/syscall.tbl-274-264 common clock_settime sys_clock_settime32
arch/microblaze/kernel/syscalls/syscall.tbl:275:265 common clock_gettime sys_clock_gettime32
arch/microblaze/kernel/syscalls/syscall.tbl-276-266 common clock_getres sys_clock_getres_time32
--
arch/microblaze/kernel/syscalls/syscall.tbl-411-# 401 and 402 are unused
arch/microblaze/kernel/syscalls/syscall.tbl:412:403 common clock_gettime64 sys_clock_gettime
arch/microblaze/kernel/syscalls/syscall.tbl-413-404 common clock_settime64 sys_clock_settime
--
arch/mips/kernel/syscalls/syscall_n32.tbl-236-225 n32 clock_settime sys_clock_settime32
arch/mips/kernel/syscalls/syscall_n32.tbl:237:226 n32 clock_gettime sys_clock_gettime32
arch/mips/kernel/syscalls/syscall_n32.tbl-238-227 n32 clock_getres sys_clock_getres_time32
--
arch/mips/kernel/syscalls/syscall_n32.tbl-344-# 333 through 402 are unassigned to sync up with generic numbers
arch/mips/kernel/syscalls/syscall_n32.tbl:345:403 n32 clock_gettime64 sys_clock_gettime
arch/mips/kernel/syscalls/syscall_n32.tbl-346-404 n32 clock_settime64 sys_clock_settime
--
arch/mips/kernel/syscalls/syscall_n64.tbl-232-221 n64 clock_settime sys_clock_settime
arch/mips/kernel/syscalls/syscall_n64.tbl:233:222 n64 clock_gettime sys_clock_gettime
arch/mips/kernel/syscalls/syscall_n64.tbl-234-223 n64 clock_getres sys_clock_getres
--
arch/mips/kernel/syscalls/syscall_o32.tbl-276-262 o32 clock_settime sys_clock_settime32
arch/mips/kernel/syscalls/syscall_o32.tbl:277:263 o32 clock_gettime sys_clock_gettime32
arch/mips/kernel/syscalls/syscall_o32.tbl-278-264 o32 clock_getres sys_clock_getres_time32
--
arch/mips/kernel/syscalls/syscall_o32.tbl-393-402 o32 msgctl sys_msgctl compat_sys_msgctl
arch/mips/kernel/syscalls/syscall_o32.tbl:394:403 o32 clock_gettime64 sys_clock_gettime sys_clock_gettime
arch/mips/kernel/syscalls/syscall_o32.tbl-395-404 o32 clock_settime64 sys_clock_settime sys_clock_settime
--
arch/parisc/kernel/syscalls/syscall.tbl-287-255 64 clock_settime sys_clock_settime
arch/parisc/kernel/syscalls/syscall.tbl:288:256 32 clock_gettime sys_clock_gettime32
arch/parisc/kernel/syscalls/syscall.tbl:289:256 64 clock_gettime sys_clock_gettime
arch/parisc/kernel/syscalls/syscall.tbl-290-257 32 clock_getres sys_clock_getres_time32
--
arch/parisc/kernel/syscalls/syscall.tbl-404-# up to 402 is unassigned and reserved for arch specific syscalls
arch/parisc/kernel/syscalls/syscall.tbl:405:403 32 clock_gettime64 sys_clock_gettime sys_clock_gettime
arch/parisc/kernel/syscalls/syscall.tbl-406-404 32 clock_settime64 sys_clock_settime sys_clock_settime
--
arch/powerpc/kernel/syscalls/syscall.tbl-317-245 spu clock_settime sys_clock_settime
arch/powerpc/kernel/syscalls/syscall.tbl:318:246 32 clock_gettime sys_clock_gettime32
arch/powerpc/kernel/syscalls/syscall.tbl:319:246 64 clock_gettime sys_clock_gettime
arch/powerpc/kernel/syscalls/syscall.tbl:320:246 spu clock_gettime sys_clock_gettime
arch/powerpc/kernel/syscalls/syscall.tbl-321-247 32 clock_getres sys_clock_getres_time32
--
arch/powerpc/kernel/syscalls/syscall.tbl-496-402 common msgctl sys_msgctl compat_sys_msgctl
arch/powerpc/kernel/syscalls/syscall.tbl:497:403 32 clock_gettime64 sys_clock_gettime sys_clock_gettime
arch/powerpc/kernel/syscalls/syscall.tbl-498-404 32 clock_settime64 sys_clock_settime sys_clock_settime
--
arch/s390/kernel/syscalls/syscall.tbl-215-259 common clock_settime sys_clock_settime
arch/s390/kernel/syscalls/syscall.tbl:216:260 common clock_gettime sys_clock_gettime
arch/s390/kernel/syscalls/syscall.tbl-217-261 common clock_getres sys_clock_getres
--
arch/sh/kernel/syscalls/syscall.tbl-274-264 common clock_settime sys_clock_settime32
arch/sh/kernel/syscalls/syscall.tbl:275:265 common clock_gettime sys_clock_gettime32
arch/sh/kernel/syscalls/syscall.tbl-276-266 common clock_getres sys_clock_getres_time32
--
arch/sh/kernel/syscalls/syscall.tbl-409-402 common msgctl sys_msgctl
arch/sh/kernel/syscalls/syscall.tbl:410:403 common clock_gettime64 sys_clock_gettime
arch/sh/kernel/syscalls/syscall.tbl-411-404 common clock_settime64 sys_clock_settime
--
arch/sparc/kernel/syscalls/syscall.tbl-310-256 64 clock_settime sys_clock_settime
arch/sparc/kernel/syscalls/syscall.tbl:311:257 32 clock_gettime sys_clock_gettime32
arch/sparc/kernel/syscalls/syscall.tbl:312:257 64 clock_gettime sys_clock_gettime
arch/sparc/kernel/syscalls/syscall.tbl-313-258 32 clock_getres sys_clock_getres_time32
--
arch/sparc/kernel/syscalls/syscall.tbl-451-402 common msgctl sys_msgctl compat_sys_msgctl
arch/sparc/kernel/syscalls/syscall.tbl:452:403 32 clock_gettime64 sys_clock_gettime sys_clock_gettime
arch/sparc/kernel/syscalls/syscall.tbl-453-404 32 clock_settime64 sys_clock_settime sys_clock_settime
--
arch/x86/entry/syscalls/syscall_32.tbl-279-264 i386 clock_settime sys_clock_settime32
arch/x86/entry/syscalls/syscall_32.tbl:280:265 i386 clock_gettime sys_clock_gettime32
arch/x86/entry/syscalls/syscall_32.tbl-281-266 i386 clock_getres sys_clock_getres_time32
--
arch/x86/entry/syscalls/syscall_32.tbl-411-402 i386 msgctl sys_msgctl compat_sys_msgctl
arch/x86/entry/syscalls/syscall_32.tbl:412:403 i386 clock_gettime64 sys_clock_gettime
arch/x86/entry/syscalls/syscall_32.tbl-413-404 i386 clock_settime64 sys_clock_settime
--
arch/x86/entry/syscalls/syscall_64.tbl-239-227 common clock_settime sys_clock_settime
arch/x86/entry/syscalls/syscall_64.tbl:240:228 common clock_gettime sys_clock_gettime
arch/x86/entry/syscalls/syscall_64.tbl-241-229 common clock_getres sys_clock_getres
--
arch/xtensa/kernel/syscalls/syscall.tbl-264-244 common clock_settime sys_clock_settime32
arch/xtensa/kernel/syscalls/syscall.tbl:265:245 common clock_gettime sys_clock_gettime32
arch/xtensa/kernel/syscalls/syscall.tbl-266-246 common clock_getres sys_clock_getres_time32
--
arch/xtensa/kernel/syscalls/syscall.tbl-376-# 353 through 402 are unassigned to sync up with generic numbers
arch/xtensa/kernel/syscalls/syscall.tbl:377:403 common clock_gettime64 sys_clock_gettime
arch/xtensa/kernel/syscalls/syscall.tbl-378-404 common clock_settime64 sys_clock_settime
--
include/linux/syscalls.h=619=asmlinkage long sys_clock_settime(clockid_t which_clock,
include/linux/syscalls.h-620- const struct __kernel_timespec __user *tp);
include/linux/syscalls.h:621:asmlinkage long sys_clock_gettime(clockid_t which_clock,
include/linux/syscalls.h-622- struct __kernel_timespec __user *tp);
--
include/linux/syscalls.h=633=asmlinkage long sys_clock_settime32(clockid_t which_clock,
include/linux/syscalls.h-634- struct old_timespec32 __user *tp);
include/linux/syscalls.h:635:asmlinkage long sys_clock_gettime32(clockid_t which_clock,
include/linux/syscalls.h-636- struct old_timespec32 __user *tp);
--
include/uapi/asm-generic/unistd.h=317=__SC_3264(__NR_clock_settime, sys_clock_settime32, sys_clock_settime)
include/uapi/asm-generic/unistd.h-318-#define __NR_clock_gettime 113
include/uapi/asm-generic/unistd.h:319:__SC_3264(__NR_clock_gettime, sys_clock_gettime32, sys_clock_gettime)
include/uapi/asm-generic/unistd.h-320-#define __NR_clock_getres 114
--
include/uapi/asm-generic/unistd.h=710=__SYSCALL(__NR_kexec_file_load, sys_kexec_file_load)
--
include/uapi/asm-generic/unistd.h-715-#define __NR_clock_gettime64 403
include/uapi/asm-generic/unistd.h:716:__SYSCALL(__NR_clock_gettime64, sys_clock_gettime)
include/uapi/asm-generic/unistd.h-717-#define __NR_clock_settime64 404
--
kernel/time/posix-timers.c=1157=SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
--
kernel/time/posix-timers.c-1237- * used to read the time from. The resolution of that device only
kernel/time/posix-timers.c:1238: * affects the precision of the time returned by sys_clock_gettime().
kernel/time/posix-timers.c-1239- *
--
scripts/syscall.tbl-144-112 64 clock_settime sys_clock_settime
scripts/syscall.tbl:145:113 time32 clock_gettime sys_clock_gettime32
scripts/syscall.tbl:146:113 64 clock_gettime sys_clock_gettime
scripts/syscall.tbl-147-114 time32 clock_getres sys_clock_getres_time32
--
scripts/syscall.tbl-346-# 295 through 402 are unassigned to sync up with generic numbers don't use
scripts/syscall.tbl:347:403 32 clock_gettime64 sys_clock_gettime
scripts/syscall.tbl-348-404 32 clock_settime64 sys_clock_settime
--
tools/include/nolibc/sys/time.h-15-
tools/include/nolibc/sys/time.h:16:static int _sys_clock_gettime(clockid_t clockid, struct timespec *tp);
tools/include/nolibc/sys/time.h-17-
--
tools/include/nolibc/sys/time.h=23=int _sys_gettimeofday(struct timeval *tv, struct timezone *tz)
--
tools/include/nolibc/sys/time.h-29-
tools/include/nolibc/sys/time.h:30: ret = _sys_clock_gettime(CLOCK_REALTIME, &tp);
tools/include/nolibc/sys/time.h-31- if (!ret && tv) {
--
tools/include/nolibc/time.h=53=static __attribute__((unused))
tools/include/nolibc/time.h:54:int _sys_clock_gettime(clockid_t clockid, struct timespec *tp)
tools/include/nolibc/time.h-55-{
--
tools/include/nolibc/time.h=66=int clock_gettime(clockid_t clockid, struct timespec *tp)
tools/include/nolibc/time.h-67-{
tools/include/nolibc/time.h:68: return __sysret(_sys_clock_gettime(clockid, tp));
tools/include/nolibc/time.h-69-}
--
tools/include/uapi/asm-generic/unistd.h=317=__SC_3264(__NR_clock_settime, sys_clock_settime32, sys_clock_settime)
tools/include/uapi/asm-generic/unistd.h-318-#define __NR_clock_gettime 113
tools/include/uapi/asm-generic/unistd.h:319:__SC_3264(__NR_clock_gettime, sys_clock_gettime32, sys_clock_gettime)
tools/include/uapi/asm-generic/unistd.h-320-#define __NR_clock_getres 114
--
tools/include/uapi/asm-generic/unistd.h=710=__SYSCALL(__NR_kexec_file_load, sys_kexec_file_load)
--
tools/include/uapi/asm-generic/unistd.h-715-#define __NR_clock_gettime64 403
tools/include/uapi/asm-generic/unistd.h:716:__SYSCALL(__NR_clock_gettime64, sys_clock_gettime)
tools/include/uapi/asm-generic/unistd.h-717-#define __NR_clock_settime64 404
--
tools/perf/arch/alpha/entry/syscalls/syscall.tbl-350-419 common clock_settime sys_clock_settime
tools/perf/arch/alpha/entry/syscalls/syscall.tbl:351:420 common clock_gettime sys_clock_gettime
tools/perf/arch/alpha/entry/syscalls/syscall.tbl-352-421 common clock_getres sys_clock_getres
--
tools/perf/arch/arm/entry/syscalls/syscall.tbl-280-262 common clock_settime sys_clock_settime32
tools/perf/arch/arm/entry/syscalls/syscall.tbl:281:263 common clock_gettime sys_clock_gettime32
tools/perf/arch/arm/entry/syscalls/syscall.tbl-282-264 common clock_getres sys_clock_getres_time32
--
tools/perf/arch/arm/entry/syscalls/syscall.tbl-420-# 402 is unused
tools/perf/arch/arm/entry/syscalls/syscall.tbl:421:403 common clock_gettime64 sys_clock_gettime
tools/perf/arch/arm/entry/syscalls/syscall.tbl-422-404 common clock_settime64 sys_clock_settime
--
tools/perf/arch/arm64/entry/syscalls/syscall_32.tbl-277-262 common clock_settime sys_clock_settime32
tools/perf/arch/arm64/entry/syscalls/syscall_32.tbl:278:263 common clock_gettime sys_clock_gettime32
tools/perf/arch/arm64/entry/syscalls/syscall_32.tbl-279-264 common clock_getres sys_clock_getres_time32
--
tools/perf/arch/arm64/entry/syscalls/syscall_32.tbl-417-# 402 is unused
tools/perf/arch/arm64/entry/syscalls/syscall_32.tbl:418:403 common clock_gettime64 sys_clock_gettime
tools/perf/arch/arm64/entry/syscalls/syscall_32.tbl-419-404 common clock_settime64 sys_clock_settime
--
tools/perf/arch/mips/entry/syscalls/syscall_n64.tbl-232-221 n64 clock_settime sys_clock_settime
tools/perf/arch/mips/entry/syscalls/syscall_n64.tbl:233:222 n64 clock_gettime sys_clock_gettime
tools/perf/arch/mips/entry/syscalls/syscall_n64.tbl-234-223 n64 clock_getres sys_clock_getres
--
tools/perf/arch/parisc/entry/syscalls/syscall.tbl-287-255 64 clock_settime sys_clock_settime
tools/perf/arch/parisc/entry/syscalls/syscall.tbl:288:256 32 clock_gettime sys_clock_gettime32
tools/perf/arch/parisc/entry/syscalls/syscall.tbl:289:256 64 clock_gettime sys_clock_gettime
tools/perf/arch/parisc/entry/syscalls/syscall.tbl-290-257 32 clock_getres sys_clock_getres_time32
--
tools/perf/arch/parisc/entry/syscalls/syscall.tbl-404-# up to 402 is unassigned and reserved for arch specific syscalls
tools/perf/arch/parisc/entry/syscalls/syscall.tbl:405:403 32 clock_gettime64 sys_clock_gettime sys_clock_gettime
tools/perf/arch/parisc/entry/syscalls/syscall.tbl-406-404 32 clock_settime64 sys_clock_settime sys_clock_settime
--
tools/perf/arch/powerpc/entry/syscalls/syscall.tbl-317-245 spu clock_settime sys_clock_settime
tools/perf/arch/powerpc/entry/syscalls/syscall.tbl:318:246 32 clock_gettime sys_clock_gettime32
tools/perf/arch/powerpc/entry/syscalls/syscall.tbl:319:246 64 clock_gettime sys_clock_gettime
tools/perf/arch/powerpc/entry/syscalls/syscall.tbl:320:246 spu clock_gettime sys_clock_gettime
tools/perf/arch/powerpc/entry/syscalls/syscall.tbl-321-247 32 clock_getres sys_clock_getres_time32
--
tools/perf/arch/powerpc/entry/syscalls/syscall.tbl-496-402 common msgctl sys_msgctl compat_sys_msgctl
tools/perf/arch/powerpc/entry/syscalls/syscall.tbl:497:403 32 clock_gettime64 sys_clock_gettime sys_clock_gettime
tools/perf/arch/powerpc/entry/syscalls/syscall.tbl-498-404 32 clock_settime64 sys_clock_settime sys_clock_settime
--
tools/perf/arch/s390/entry/syscalls/syscall.tbl-215-259 common clock_settime sys_clock_settime
tools/perf/arch/s390/entry/syscalls/syscall.tbl:216:260 common clock_gettime sys_clock_gettime
tools/perf/arch/s390/entry/syscalls/syscall.tbl-217-261 common clock_getres sys_clock_getres
--
tools/perf/arch/sh/entry/syscalls/syscall.tbl-274-264 common clock_settime sys_clock_settime32
tools/perf/arch/sh/entry/syscalls/syscall.tbl:275:265 common clock_gettime sys_clock_gettime32
tools/perf/arch/sh/entry/syscalls/syscall.tbl-276-266 common clock_getres sys_clock_getres_time32
--
tools/perf/arch/sh/entry/syscalls/syscall.tbl-409-402 common msgctl sys_msgctl
tools/perf/arch/sh/entry/syscalls/syscall.tbl:410:403 common clock_gettime64 sys_clock_gettime
tools/perf/arch/sh/entry/syscalls/syscall.tbl-411-404 common clock_settime64 sys_clock_settime
--
tools/perf/arch/sparc/entry/syscalls/syscall.tbl-310-256 64 clock_settime sys_clock_settime
tools/perf/arch/sparc/entry/syscalls/syscall.tbl:311:257 32 clock_gettime sys_clock_gettime32
tools/perf/arch/sparc/entry/syscalls/syscall.tbl:312:257 64 clock_gettime sys_clock_gettime
tools/perf/arch/sparc/entry/syscalls/syscall.tbl-313-258 32 clock_getres sys_clock_getres_time32
--
tools/perf/arch/sparc/entry/syscalls/syscall.tbl-451-402 common msgctl sys_msgctl compat_sys_msgctl
tools/perf/arch/sparc/entry/syscalls/syscall.tbl:452:403 32 clock_gettime64 sys_clock_gettime sys_clock_gettime
tools/perf/arch/sparc/entry/syscalls/syscall.tbl-453-404 32 clock_settime64 sys_clock_settime sys_clock_settime
--
tools/perf/arch/x86/entry/syscalls/syscall_32.tbl-279-264 i386 clock_settime sys_clock_settime32
tools/perf/arch/x86/entry/syscalls/syscall_32.tbl:280:265 i386 clock_gettime sys_clock_gettime32
tools/perf/arch/x86/entry/syscalls/syscall_32.tbl-281-266 i386 clock_getres sys_clock_getres_time32
--
tools/perf/arch/x86/entry/syscalls/syscall_32.tbl-411-402 i386 msgctl sys_msgctl compat_sys_msgctl
tools/perf/arch/x86/entry/syscalls/syscall_32.tbl:412:403 i386 clock_gettime64 sys_clock_gettime
tools/perf/arch/x86/entry/syscalls/syscall_32.tbl-413-404 i386 clock_settime64 sys_clock_settime
--
tools/perf/arch/x86/entry/syscalls/syscall_64.tbl-239-227 common clock_settime sys_clock_settime
tools/perf/arch/x86/entry/syscalls/syscall_64.tbl:240:228 common clock_gettime sys_clock_gettime
tools/perf/arch/x86/entry/syscalls/syscall_64.tbl-241-229 common clock_getres sys_clock_getres
--
tools/perf/arch/xtensa/entry/syscalls/syscall.tbl-264-244 common clock_settime sys_clock_settime32
tools/perf/arch/xtensa/entry/syscalls/syscall.tbl:265:245 common clock_gettime sys_clock_gettime32
tools/perf/arch/xtensa/entry/syscalls/syscall.tbl-266-246 common clock_getres sys_clock_getres_time32
--
tools/perf/arch/xtensa/entry/syscalls/syscall.tbl-376-# 353 through 402 are unassigned to sync up with generic numbers
tools/perf/arch/xtensa/entry/syscalls/syscall.tbl:377:403 common clock_gettime64 sys_clock_gettime
tools/perf/arch/xtensa/entry/syscalls/syscall.tbl-378-404 common clock_settime64 sys_clock_settime
--
tools/scripts/syscall.tbl-144-112 64 clock_settime sys_clock_settime
tools/scripts/syscall.tbl:145:113 time32 clock_gettime sys_clock_gettime32
tools/scripts/syscall.tbl:146:113 64 clock_gettime sys_clock_gettime
tools/scripts/syscall.tbl-147-114 time32 clock_getres sys_clock_getres_time32
--
tools/scripts/syscall.tbl-346-# 295 through 402 are unassigned to sync up with generic numbers don't use
tools/scripts/syscall.tbl:347:403 32 clock_gettime64 sys_clock_gettime
tools/scripts/syscall.tbl-348-404 32 clock_settime64 sys_clock_settime
--
tools/testing/selftests/vDSO/vdso_test_correctness.c=146=static long sys_getcpu(unsigned * cpu, unsigned * node,
--
tools/testing/selftests/vDSO/vdso_test_correctness.c-151-
tools/testing/selftests/vDSO/vdso_test_correctness.c:152:static inline int sys_clock_gettime(clockid_t id, struct timespec *ts)
tools/testing/selftests/vDSO/vdso_test_correctness.c-153-{
--
tools/testing/selftests/vDSO/vdso_test_correctness.c-156-
tools/testing/selftests/vDSO/vdso_test_correctness.c:157:static inline int sys_clock_gettime64(clockid_t id, struct __kernel_timespec *ts)
tools/testing/selftests/vDSO/vdso_test_correctness.c-158-{
--
tools/testing/selftests/vDSO/vdso_test_correctness.c=269=static void test_one_clock_gettime(int clock, const char *name)
--
tools/testing/selftests/vDSO/vdso_test_correctness.c-275-
tools/testing/selftests/vDSO/vdso_test_correctness.c:276: if (sys_clock_gettime(clock, &start) < 0) {
tools/testing/selftests/vDSO/vdso_test_correctness.c-277- if (errno == EINVAL) {
--
tools/testing/selftests/vDSO/vdso_test_correctness.c-291- vdso_ret = VDSO_CALL(vdso_clock_gettime, 2, clock, &vdso);
tools/testing/selftests/vDSO/vdso_test_correctness.c:292: end_ret = sys_clock_gettime(clock, &end);
tools/testing/selftests/vDSO/vdso_test_correctness.c-293-
--
tools/testing/selftests/vDSO/vdso_test_correctness.c=331=static void test_one_clock_gettime64(int clock, const char *name)
--
tools/testing/selftests/vDSO/vdso_test_correctness.c-337-
tools/testing/selftests/vDSO/vdso_test_correctness.c:338: if (sys_clock_gettime64(clock, &start) < 0) {
tools/testing/selftests/vDSO/vdso_test_correctness.c-339- if (errno == EINVAL) {
--
tools/testing/selftests/vDSO/vdso_test_correctness.c-353- vdso_ret = VDSO_CALL(vdso_clock_gettime64, 2, clock, &vdso);
tools/testing/selftests/vDSO/vdso_test_correctness.c:354: end_ret = sys_clock_gettime64(clock, &end);
tools/testing/selftests/vDSO/vdso_test_correctness.c-355-
]
|
| 34/2 |
2026/05/18 16:26 |
llm |
expert |
0m
Model:
Tokens:
input: 46415
output: 45
thoughts: 29
Thoughts: Okay, here's my take on those thoughts, framed as an expert's internal monologue:
**Checking `sys_clock_gettime` Implementation**
Right, `sys_clock_gettime` is definitely a syscall, no question there. So, the logical next step is to dive into its implementation. I need to understand exactly how it's handled at the kernel level. What are the specific data structures it's accessing? How does it interact with the hardware clock, or the virtualized clock if we're in a container or VM? I'm curious about the specific clock IDs it supports – what about monotonic clocks, real-time clocks, and the others? I'll need to examine the associated architecture-specific code to see how it's optimized for the target platform. I need to look for any potential bottlenecks, race conditions, or security vulnerabilities in this critical path. Time to see what the source code has to offer!
|
| 35/2 |
2026/05/18 16:26 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:kernel/time/posix-timers.c IncludeLines:true Name:sys_clock_gettime]
Results: map[SourceCode: SourceFile:]
|
| 36/2 |
2026/05/18 16:26 |
llm |
expert |
0m
Model:
Tokens:
input: 46708
output: 25
thoughts: 34
Thoughts: Okay, here's my interpretation of that thought process, assuming I'm well-versed in kernel development:
**Kernel Timekeeping Investigation**
Alright, let me think. Where would I expect to find the definition for `clock_gettime` in the Linux kernel? Given its function, my initial hunch is that it's going to be defined with `SYSCALL_DEFINE2`... that's a pretty standard macro for defining system calls. And where are the POSIX timer related functions usually handled? That points me towards `kernel/time/posix-timers.c`. So my guess is that within that file is where I'd find the macro expanding to define the system call `clock_gettime`
|
| 37/2 |
2026/05/18 16:26 |
tool |
grepper |
0m
Args: map[Expression:SYSCALL_DEFINE2\(clock_gettime]
Results: map[Output:kernel/time/posix-stubs.c=39=static int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)
--
kernel/time/posix-stubs.c-59-
kernel/time/posix-stubs.c:60:SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
kernel/time/posix-stubs.c-61- struct __kernel_timespec __user *, tp)
--
kernel/time/posix-stubs.c=129=SYSCALL_DEFINE2(clock_settime32, const clockid_t, which_clock,
--
kernel/time/posix-stubs.c-141-
kernel/time/posix-stubs.c:142:SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
kernel/time/posix-stubs.c-143- struct old_timespec32 __user *, tp)
--
kernel/time/posix-timers.c=1108=SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
--
kernel/time/posix-timers.c-1126-
kernel/time/posix-timers.c:1127:SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
kernel/time/posix-timers.c-1128- struct __kernel_timespec __user *, tp)
--
kernel/time/posix-timers.c=1267=SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
--
kernel/time/posix-timers.c-1281-
kernel/time/posix-timers.c:1282:SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
kernel/time/posix-timers.c-1283- struct old_timespec32 __user *, tp)
]
|
| 38/2 |
2026/05/18 16:26 |
llm |
expert |
0m
Model:
Tokens:
input: 47447
output: 45
thoughts: 23
Thoughts: Here's a summary of my immediate thought process:
**Diving into the POSIX Timers Implementation**
Okay, I need to get to the bottom of this timer issue. My initial thought is to dive right into the `kernel/time/posix-timers.c` file. Specifically, I'm going to jump to line 1127. I'm assuming that's where the heart of the problem or related code lies, given the context. Time to roll up my sleeves and see what's going on at that specific juncture of the timer implementation. Let's see what secrets line 1127 holds!
|
| 39/2 |
2026/05/18 16:26 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:kernel/time/posix-timers.c IncludeLines:true Name:SYSCALL_DEFINE2]
Results: map[SourceCode: SourceFile:]
|
| 40/2 |
2026/05/18 16:26 |
llm |
expert |
0m
Model:
Tokens:
input: 47672
output: 25
thoughts: 8
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**Initial Assessment: A Direct Path**
Alright, the objective is straightforward. Faced with this file, my immediate instinct is to simply *read* it. No elaborate preprocessing, no complex initial analysis – just direct engagement with the source material. My brain is already starting to scope out the relevant context and potential research questions and how this text may be able to answer them. This 'just read the file' approach is the baseline. We will see where that leads.
|
| 41/2 |
2026/05/18 16:26 |
tool |
read-file |
0m
Args: map[File:kernel/time/posix-timers.c]
Results: map[Contents:// SPDX-License-Identifier: GPL-2.0+
/*
* 2002-10-15 Posix Clocks & timers
* by George Anzinger george@mvista.com
* Copyright (C) 2002 2003 by MontaVista Software.
*
* 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
* Copyright (C) 2004 Boris Hu
*
* These are all the functions necessary to implement POSIX clocks & timers
*/
#include <linux/compat.h>
#include <linux/compiler.h>
#include <linux/init.h>
#include <linux/jhash.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/memblock.h>
#include <linux/nospec.h>
#include <linux/posix-clock.h>
#include <linux/posix-timers.h>
#include <linux/prctl.h>
#include <linux/sched/task.h>
#include <linux/slab.h>
#include <linux/syscalls.h>
#include <linux/time.h>
#include <linux/time_namespace.h>
#include <linux/uaccess.h>
#include "timekeeping.h"
#include "posix-timers.h"
/*
* Timers are managed in a hash table for lockless lookup. The hash key is
* constructed from current::signal and the timer ID and the timer is
* matched against current::signal and the timer ID when walking the hash
* bucket list.
*
* This allows checkpoint/restore to reconstruct the exact timer IDs for
* a process.
*/
struct timer_hash_bucket {
spinlock_t lock;
struct hlist_head head;
};
static struct {
struct timer_hash_bucket *buckets;
unsigned long mask;
struct kmem_cache *cache;
} __timer_data __ro_after_init __aligned(4*sizeof(long));
#define timer_buckets (__timer_data.buckets)
#define timer_hashmask (__timer_data.mask)
#define posix_timers_cache (__timer_data.cache)
static const struct k_clock * const posix_clocks[];
static const struct k_clock *clockid_to_kclock(const clockid_t id);
static const struct k_clock clock_realtime, clock_monotonic;
#define TIMER_ANY_ID INT_MIN
/* SIGEV_THREAD_ID cannot share a bit with the other SIGEV values. */
#if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
#error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
#endif
static struct k_itimer *lock_timer(timer_t timer_id);
static inline void unlock_timer(struct k_itimer *timr)
{
if (likely((timr)))
spin_unlock_irq(&timr->it_lock);
}
#define scoped_timer_get_or_fail(_id) \
scoped_cond_guard(lock_timer, return -EINVAL, _id)
#define scoped_timer (scope)
DEFINE_CLASS(lock_timer, struct k_itimer *, unlock_timer(_T), lock_timer(id), timer_t id);
DEFINE_CLASS_IS_COND_GUARD(lock_timer);
static struct timer_hash_bucket *hash_bucket(struct signal_struct *sig, unsigned int nr)
{
return &timer_buckets[jhash2((u32 *)&sig, sizeof(sig) / sizeof(u32), nr) & timer_hashmask];
}
static struct k_itimer *posix_timer_by_id(timer_t id)
{
struct signal_struct *sig = current->signal;
struct timer_hash_bucket *bucket = hash_bucket(sig, id);
struct k_itimer *timer;
hlist_for_each_entry_rcu(timer, &bucket->head, t_hash) {
/* timer->it_signal can be set concurrently */
if ((READ_ONCE(timer->it_signal) == sig) && (timer->it_id == id))
return timer;
}
return NULL;
}
static inline struct signal_struct *posix_sig_owner(const struct k_itimer *timer)
{
unsigned long val = (unsigned long)timer->it_signal;
/*
* Mask out bit 0, which acts as invalid marker to prevent
* posix_timer_by_id() detecting it as valid.
*/
return (struct signal_struct *)(val & ~1UL);
}
static bool posix_timer_hashed(struct timer_hash_bucket *bucket, struct signal_struct *sig,
timer_t id)
{
struct hlist_head *head = &bucket->head;
struct k_itimer *timer;
hlist_for_each_entry_rcu(timer, head, t_hash, lockdep_is_held(&bucket->lock)) {
if ((posix_sig_owner(timer) == sig) && (timer->it_id == id))
return true;
}
return false;
}
static bool posix_timer_add_at(struct k_itimer *timer, struct signal_struct *sig, unsigned int id)
{
struct timer_hash_bucket *bucket = hash_bucket(sig, id);
scoped_guard (spinlock, &bucket->lock) {
/*
* Validate under the lock as this could have raced against
* another thread ending up with the same ID, which is
* highly unlikely, but possible.
*/
if (!posix_timer_hashed(bucket, sig, id)) {
/*
* Set the timer ID and the signal pointer to make
* it identifiable in the hash table. The signal
* pointer has bit 0 set to indicate that it is not
* yet fully initialized. posix_timer_hashed()
* masks this bit out, but the syscall lookup fails
* to match due to it being set. This guarantees
* that there can't be duplicate timer IDs handed
* out.
*/
timer->it_id = (timer_t)id;
timer->it_signal = (struct signal_struct *)((unsigned long)sig | 1UL);
hlist_add_head_rcu(&timer->t_hash, &bucket->head);
return true;
}
}
return false;
}
static int posix_timer_add(struct k_itimer *timer, int req_id)
{
struct signal_struct *sig = current->signal;
if (unlikely(req_id != TIMER_ANY_ID)) {
if (!posix_timer_add_at(timer, sig, req_id))
return -EBUSY;
/*
* Move the ID counter past the requested ID, so that after
* switching back to normal mode the IDs are outside of the
* exact allocated region. That avoids ID collisions on the
* next regular timer_create() invocations.
*/
atomic_set(&sig->next_posix_timer_id, req_id + 1);
return req_id;
}
for (unsigned int cnt = 0; cnt <= INT_MAX; cnt++) {
/* Get the next timer ID and clamp it to positive space */
unsigned int id = atomic_fetch_inc(&sig->next_posix_timer_id) & INT_MAX;
if (posix_timer_add_at(timer, sig, id))
return id;
cond_resched();
}
/* POSIX return code when no timer ID could be allocated */
return -EAGAIN;
}
static int posix_get_realtime_timespec(clockid_t which_clock, struct timespec64 *tp)
{
ktime_get_real_ts64(tp);
return 0;
}
static ktime_t posix_get_realtime_ktime(clockid_t which_clock)
{
return ktime_get_real();
}
static int posix_clock_realtime_set(const clockid_t which_clock,
const struct timespec64 *tp)
{
return do_sys_settimeofday64(tp, NULL);
}
static int posix_clock_realtime_adj(const clockid_t which_clock,
struct __kernel_timex *t)
{
return do_adjtimex(t);
}
static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64 *tp)
{
ktime_get_ts64(tp);
timens_add_monotonic(tp);
return 0;
}
static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
{
return ktime_get();
}
static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
{
ktime_get_raw_ts64(tp);
timens_add_monotonic(tp);
return 0;
}
static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
{
ktime_get_coarse_real_ts64(tp);
return 0;
}
static int posix_get_monotonic_coarse(clockid_t which_clock,
struct timespec64 *tp)
{
ktime_get_coarse_ts64(tp);
timens_add_monotonic(tp);
return 0;
}
static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
{
*tp = ktime_to_timespec64(KTIME_LOW_RES);
return 0;
}
static int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *tp)
{
ktime_get_boottime_ts64(tp);
timens_add_boottime(tp);
return 0;
}
static ktime_t posix_get_boottime_ktime(const clockid_t which_clock)
{
return ktime_get_boottime();
}
static int posix_get_tai_timespec(clockid_t which_clock, struct timespec64 *tp)
{
ktime_get_clocktai_ts64(tp);
return 0;
}
static ktime_t posix_get_tai_ktime(clockid_t which_clock)
{
return ktime_get_clocktai();
}
static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
{
tp->tv_sec = 0;
tp->tv_nsec = hrtimer_resolution;
return 0;
}
/*
* The siginfo si_overrun field and the return value of timer_getoverrun(2)
* are of type int. Clamp the overrun value to INT_MAX
*/
static inline int timer_overrun_to_int(struct k_itimer *timr)
{
if (timr->it_overrun_last > (s64)INT_MAX)
return INT_MAX;
return (int)timr->it_overrun_last;
}
static void common_hrtimer_rearm(struct k_itimer *timr)
{
struct hrtimer *timer = &timr->it.real.timer;
timr->it_overrun += hrtimer_forward_now(timer, timr->it_interval);
hrtimer_restart(timer);
}
static bool __posixtimer_deliver_signal(struct kernel_siginfo *info, struct k_itimer *timr)
{
guard(spinlock)(&timr->it_lock);
/*
* Check if the timer is still alive or whether it got modified
* since the signal was queued. In either case, don't rearm and
* drop the signal.
*/
if (timr->it_signal_seq != timr->it_sigqueue_seq || WARN_ON_ONCE(!posixtimer_valid(timr)))
return false;
if (!timr->it_interval || WARN_ON_ONCE(timr->it_status != POSIX_TIMER_REQUEUE_PENDING))
return true;
timr->kclock->timer_rearm(timr);
timr->it_status = POSIX_TIMER_ARMED;
timr->it_overrun_last = timr->it_overrun;
timr->it_overrun = -1LL;
++timr->it_signal_seq;
info->si_overrun = timer_overrun_to_int(timr);
return true;
}
/*
* This function is called from the signal delivery code. It decides
* whether the signal should be dropped and rearms interval timers. The
* timer can be unconditionally accessed as there is a reference held on
* it.
*/
bool posixtimer_deliver_signal(struct kernel_siginfo *info, struct sigqueue *timer_sigq)
{
struct k_itimer *timr = container_of(timer_sigq, struct k_itimer, sigq);
bool ret;
/*
* Release siglock to ensure proper locking order versus
* timr::it_lock. Keep interrupts disabled.
*/
spin_unlock(¤t->sighand->siglock);
ret = __posixtimer_deliver_signal(info, timr);
/* Drop the reference which was acquired when the signal was queued */
posixtimer_putref(timr);
spin_lock(¤t->sighand->siglock);
return ret;
}
void posix_timer_queue_signal(struct k_itimer *timr)
{
lockdep_assert_held(&timr->it_lock);
if (!posixtimer_valid(timr))
return;
timr->it_status = timr->it_interval ? POSIX_TIMER_REQUEUE_PENDING : POSIX_TIMER_DISARMED;
posixtimer_send_sigqueue(timr);
}
/*
* This function gets called when a POSIX.1b interval timer expires from
* the HRTIMER interrupt (soft interrupt on RT kernels).
*
* Handles CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME and CLOCK_TAI
* based timers.
*/
static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
{
struct k_itimer *timr = container_of(timer, struct k_itimer, it.real.timer);
guard(spinlock_irqsave)(&timr->it_lock);
posix_timer_queue_signal(timr);
return HRTIMER_NORESTART;
}
long posixtimer_create_prctl(unsigned long ctrl)
{
switch (ctrl) {
case PR_TIMER_CREATE_RESTORE_IDS_OFF:
current->signal->timer_create_restore_ids = 0;
return 0;
case PR_TIMER_CREATE_RESTORE_IDS_ON:
current->signal->timer_create_restore_ids = 1;
return 0;
case PR_TIMER_CREATE_RESTORE_IDS_GET:
return current->signal->timer_create_restore_ids;
}
return -EINVAL;
}
static struct pid *good_sigevent(sigevent_t * event)
{
struct pid *pid = task_tgid(current);
struct task_struct *rtn;
switch (event->sigev_notify) {
case SIGEV_SIGNAL | SIGEV_THREAD_ID:
pid = find_vpid(event->sigev_notify_thread_id);
rtn = pid_task(pid, PIDTYPE_PID);
if (!rtn || !same_thread_group(rtn, current))
return NULL;
fallthrough;
case SIGEV_SIGNAL:
case SIGEV_THREAD:
if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
return NULL;
fallthrough;
case SIGEV_NONE:
return pid;
default:
return NULL;
}
}
static struct k_itimer *alloc_posix_timer(void)
{
struct k_itimer *tmr;
if (unlikely(!posix_timers_cache))
return NULL;
tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
if (!tmr)
return tmr;
if (unlikely(!posixtimer_init_sigqueue(&tmr->sigq))) {
kmem_cache_free(posix_timers_cache, tmr);
return NULL;
}
rcuref_init(&tmr->rcuref, 1);
return tmr;
}
void posixtimer_free_timer(struct k_itimer *tmr)
{
put_pid(tmr->it_pid);
if (tmr->sigq.ucounts)
dec_rlimit_put_ucounts(tmr->sigq.ucounts, UCOUNT_RLIMIT_SIGPENDING);
kfree_rcu(tmr, rcu);
}
static void posix_timer_unhash_and_free(struct k_itimer *tmr)
{
struct timer_hash_bucket *bucket = hash_bucket(posix_sig_owner(tmr), tmr->it_id);
scoped_guard (spinlock, &bucket->lock)
hlist_del_rcu(&tmr->t_hash);
posixtimer_putref(tmr);
}
static int common_timer_create(struct k_itimer *new_timer)
{
hrtimer_setup(&new_timer->it.real.timer, posix_timer_fn, new_timer->it_clock, 0);
return 0;
}
/* Create a POSIX.1b interval timer. */
static int do_timer_create(clockid_t which_clock, struct sigevent *event,
timer_t __user *created_timer_id)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
timer_t req_id = TIMER_ANY_ID;
struct k_itimer *new_timer;
int error, new_timer_id;
if (!kc)
return -EINVAL;
if (!kc->timer_create)
return -EOPNOTSUPP;
/* Special case for CRIU to restore timers with a given timer ID. */
if (unlikely(current->signal->timer_create_restore_ids)) {
if (copy_from_user(&req_id, created_timer_id, sizeof(req_id)))
return -EFAULT;
/* Valid IDs are 0..INT_MAX */
if ((unsigned int)req_id > INT_MAX)
return -EINVAL;
}
new_timer = alloc_posix_timer();
if (unlikely(!new_timer))
return -EAGAIN;
spin_lock_init(&new_timer->it_lock);
/*
* Add the timer to the hash table. The timer is not yet valid
* after insertion, but has a unique ID allocated.
*/
new_timer_id = posix_timer_add(new_timer, req_id);
if (new_timer_id < 0) {
posixtimer_free_timer(new_timer);
return new_timer_id;
}
new_timer->it_clock = which_clock;
new_timer->kclock = kc;
new_timer->it_overrun = -1LL;
if (event) {
scoped_guard (rcu)
new_timer->it_pid = get_pid(good_sigevent(event));
if (!new_timer->it_pid) {
error = -EINVAL;
goto out;
}
new_timer->it_sigev_notify = event->sigev_notify;
new_timer->sigq.info.si_signo = event->sigev_signo;
new_timer->sigq.info.si_value = event->sigev_value;
} else {
new_timer->it_sigev_notify = SIGEV_SIGNAL;
new_timer->sigq.info.si_signo = SIGALRM;
new_timer->sigq.info.si_value.sival_int = new_timer->it_id;
new_timer->it_pid = get_pid(task_tgid(current));
}
if (new_timer->it_sigev_notify & SIGEV_THREAD_ID)
new_timer->it_pid_type = PIDTYPE_PID;
else
new_timer->it_pid_type = PIDTYPE_TGID;
new_timer->sigq.info.si_tid = new_timer->it_id;
new_timer->sigq.info.si_code = SI_TIMER;
if (copy_to_user(created_timer_id, &new_timer_id, sizeof (new_timer_id))) {
error = -EFAULT;
goto out;
}
/*
* After successful copy out, the timer ID is visible to user space
* now but not yet valid because new_timer::signal low order bit is 1.
*
* Complete the initialization with the clock specific create
* callback.
*/
error = kc->timer_create(new_timer);
if (error)
goto out;
/*
* timer::it_lock ensures that __lock_timer() observes a fully
* initialized timer when it observes a valid timer::it_signal.
*
* sighand::siglock is required to protect signal::posix_timers.
*/
scoped_guard (spinlock_irq, &new_timer->it_lock) {
guard(spinlock)(¤t->sighand->siglock);
/*
* new_timer::it_signal contains the signal pointer with
* bit 0 set, which makes it invalid for syscall operations.
* Store the unmodified signal pointer to make it valid.
*/
WRITE_ONCE(new_timer->it_signal, current->signal);
hlist_add_head_rcu(&new_timer->list, ¤t->signal->posix_timers);
}
/*
* After unlocking @new_timer is subject to concurrent removal and
* cannot be touched anymore
*/
return 0;
out:
posix_timer_unhash_and_free(new_timer);
return error;
}
SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
struct sigevent __user *, timer_event_spec,
timer_t __user *, created_timer_id)
{
if (timer_event_spec) {
sigevent_t event;
if (copy_from_user(&event, timer_event_spec, sizeof (event)))
return -EFAULT;
return do_timer_create(which_clock, &event, created_timer_id);
}
return do_timer_create(which_clock, NULL, created_timer_id);
}
#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
struct compat_sigevent __user *, timer_event_spec,
timer_t __user *, created_timer_id)
{
if (timer_event_spec) {
sigevent_t event;
if (get_compat_sigevent(&event, timer_event_spec))
return -EFAULT;
return do_timer_create(which_clock, &event, created_timer_id);
}
return do_timer_create(which_clock, NULL, created_timer_id);
}
#endif
static struct k_itimer *lock_timer(timer_t timer_id)
{
struct k_itimer *timr;
/*
* timer_t could be any type >= int and we want to make sure any
* @timer_id outside positive int range fails lookup.
*/
if ((unsigned long long)timer_id > INT_MAX)
return NULL;
/*
* The hash lookup and the timers are RCU protected.
*
* Timers are added to the hash in invalid state where
* timr::it_signal is marked invalid. timer::it_signal is only set
* after the rest of the initialization succeeded.
*
* Timer destruction happens in steps:
* 1) Set timr::it_signal marked invalid with timr::it_lock held
* 2) Release timr::it_lock
* 3) Remove from the hash under hash_lock
* 4) Put the reference count.
*
* The reference count might not drop to zero if timr::sigq is
* queued. In that case the signal delivery or flush will put the
* last reference count.
*
* When the reference count reaches zero, the timer is scheduled
* for RCU removal after the grace period.
*
* Holding rcu_read_lock() across the lookup ensures that
* the timer cannot be freed.
*
* The lookup validates locklessly that timr::it_signal ==
* current::it_signal and timr::it_id == @timer_id. timr::it_id
* can't change, but timr::it_signal can become invalid during
* destruction, which makes the locked check fail.
*/
guard(rcu)();
timr = posix_timer_by_id(timer_id);
if (timr) {
spin_lock_irq(&timr->it_lock);
/*
* Validate under timr::it_lock that timr::it_signal is
* still valid. Pairs with #1 above.
*/
if (timr->it_signal == current->signal)
return timr;
spin_unlock_irq(&timr->it_lock);
}
return NULL;
}
static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
{
struct hrtimer *timer = &timr->it.real.timer;
return __hrtimer_expires_remaining_adjusted(timer, now);
}
static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
{
struct hrtimer *timer = &timr->it.real.timer;
return hrtimer_forward(timer, now, timr->it_interval);
}
/*
* Get the time remaining on a POSIX.1b interval timer.
*
* Two issues to handle here:
*
* 1) The timer has a requeue pending. The return value must appear as
* if the timer has been requeued right now.
*
* 2) The timer is a SIGEV_NONE timer. These timers are never enqueued
* into the hrtimer queue and therefore never expired. Emulate expiry
* here taking #1 into account.
*/
void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
{
const struct k_clock *kc = timr->kclock;
ktime_t now, remaining, iv;
bool sig_none;
sig_none = timr->it_sigev_notify == SIGEV_NONE;
iv = timr->it_interval;
/* interval timer ? */
if (iv) {
cur_setting->it_interval = ktime_to_timespec64(iv);
} else if (timr->it_status == POSIX_TIMER_DISARMED) {
/*
* SIGEV_NONE oneshot timers are never queued and therefore
* timr->it_status is always DISARMED. The check below
* vs. remaining time will handle this case.
*
* For all other timers there is nothing to update here, so
* return.
*/
if (!sig_none)
return;
}
now = kc->clock_get_ktime(timr->it_clock);
/*
* If this is an interval timer and either has requeue pending or
* is a SIGEV_NONE timer move the expiry time forward by intervals,
* so expiry is > now.
*/
if (iv && timr->it_status != POSIX_TIMER_ARMED)
timr->it_overrun += kc->timer_forward(timr, now);
remaining = kc->timer_remaining(timr, now);
/*
* As @now is retrieved before a possible timer_forward() and
* cannot be reevaluated by the compiler @remaining is based on the
* same @now value. Therefore @remaining is consistent vs. @now.
*
* Consequently all interval timers, i.e. @iv > 0, cannot have a
* remaining time <= 0 because timer_forward() guarantees to move
* them forward so that the next timer expiry is > @now.
*/
if (remaining <= 0) {
/*
* A single shot SIGEV_NONE timer must return 0, when it is
* expired! Timers which have a real signal delivery mode
* must return a remaining time greater than 0 because the
* signal has not yet been delivered.
*/
if (!sig_none)
cur_setting->it_value.tv_nsec = 1;
} else {
cur_setting->it_value = ktime_to_timespec64(remaining);
}
}
static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
{
memset(setting, 0, sizeof(*setting));
scoped_timer_get_or_fail(timer_id)
scoped_timer->kclock->timer_get(scoped_timer, setting);
return 0;
}
/* Get the time remaining on a POSIX.1b interval timer. */
SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
struct __kernel_itimerspec __user *, setting)
{
struct itimerspec64 cur_setting;
int ret = do_timer_gettime(timer_id, &cur_setting);
if (!ret) {
if (put_itimerspec64(&cur_setting, setting))
ret = -EFAULT;
}
return ret;
}
#ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
struct old_itimerspec32 __user *, setting)
{
struct itimerspec64 cur_setting;
int ret = do_timer_gettime(timer_id, &cur_setting);
if (!ret) {
if (put_old_itimerspec32(&cur_setting, setting))
ret = -EFAULT;
}
return ret;
}
#endif
/**
* sys_timer_getoverrun - Get the number of overruns of a POSIX.1b interval timer
* @timer_id: The timer ID which identifies the timer
*
* The "overrun count" of a timer is one plus the number of expiration
* intervals which have elapsed between the first expiry, which queues the
* signal and the actual signal delivery. On signal delivery the "overrun
* count" is calculated and cached, so it can be returned directly here.
*
* As this is relative to the last queued signal the returned overrun count
* is meaningless outside of the signal delivery path and even there it
* does not accurately reflect the current state when user space evaluates
* it.
*
* Returns:
* -EINVAL @timer_id is invalid
* 1..INT_MAX The number of overruns related to the last delivered signal
*/
SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
{
scoped_timer_get_or_fail(timer_id)
return timer_overrun_to_int(scoped_timer);
}
static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
bool absolute, bool sigev_none)
{
struct hrtimer *timer = &timr->it.real.timer;
enum hrtimer_mode mode;
mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
/*
* Posix magic: Relative CLOCK_REALTIME timers are not affected by
* clock modifications, so they become CLOCK_MONOTONIC based under the
* hood. See hrtimer_setup(). Update timr->kclock, so the generic
* functions which use timr->kclock->clock_get_*() work.
*
* Note: it_clock stays unmodified, because the next timer_set() might
* use ABSTIME, so it needs to switch back.
*/
if (timr->it_clock == CLOCK_REALTIME)
timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
hrtimer_setup(&timr->it.real.timer, posix_timer_fn, timr->it_clock, mode);
if (!absolute)
expires = ktime_add_safe(expires, hrtimer_cb_get_time(timer));
hrtimer_set_expires(timer, expires);
if (!sigev_none)
hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
}
static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
{
return hrtimer_try_to_cancel(&timr->it.real.timer);
}
static void common_timer_wait_running(struct k_itimer *timer)
{
hrtimer_cancel_wait_running(&timer->it.real.timer);
}
/*
* On PREEMPT_RT this prevents priority inversion and a potential livelock
* against the ksoftirqd thread in case that ksoftirqd gets preempted while
* executing a hrtimer callback.
*
* See the comments in hrtimer_cancel_wait_running(). For PREEMPT_RT=n this
* just results in a cpu_relax().
*
* For POSIX CPU timers with CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n this is
* just a cpu_relax(). With CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y this
* prevents spinning on an eventually scheduled out task and a livelock
* when the task which tries to delete or disarm the timer has preempted
* the task which runs the expiry in task work context.
*/
static void timer_wait_running(struct k_itimer *timer)
{
/*
* kc->timer_wait_running() might drop RCU lock. So @timer
* cannot be touched anymore after the function returns!
*/
timer->kclock->timer_wait_running(timer);
}
/*
* Set up the new interval and reset the signal delivery data
*/
void posix_timer_set_common(struct k_itimer *timer, struct itimerspec64 *new_setting)
{
if (new_setting->it_value.tv_sec || new_setting->it_value.tv_nsec)
timer->it_interval = timespec64_to_ktime(new_setting->it_interval);
else
timer->it_interval = 0;
/* Reset overrun accounting */
timer->it_overrun_last = 0;
timer->it_overrun = -1LL;
}
/* Set a POSIX.1b interval timer. */
int common_timer_set(struct k_itimer *timr, int flags,
struct itimerspec64 *new_setting,
struct itimerspec64 *old_setting)
{
const struct k_clock *kc = timr->kclock;
bool sigev_none;
ktime_t expires;
if (old_setting)
common_timer_get(timr, old_setting);
/*
* Careful here. On SMP systems the timer expiry function could be
* active and spinning on timr->it_lock.
*/
if (kc->timer_try_to_cancel(timr) < 0)
return TIMER_RETRY;
timr->it_status = POSIX_TIMER_DISARMED;
posix_timer_set_common(timr, new_setting);
/* Keep timer disarmed when it_value is zero */
if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
return 0;
expires = timespec64_to_ktime(new_setting->it_value);
if (flags & TIMER_ABSTIME)
expires = timens_ktime_to_host(timr->it_clock, expires);
sigev_none = timr->it_sigev_notify == SIGEV_NONE;
kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
if (!sigev_none)
timr->it_status = POSIX_TIMER_ARMED;
return 0;
}
static int do_timer_settime(timer_t timer_id, int tmr_flags, struct itimerspec64 *new_spec64,
struct itimerspec64 *old_spec64)
{
if (!timespec64_valid(&new_spec64->it_interval) ||
!timespec64_valid(&new_spec64->it_value))
return -EINVAL;
if (old_spec64)
memset(old_spec64, 0, sizeof(*old_spec64));
for (; ; old_spec64 = NULL) {
struct k_itimer *timr;
scoped_timer_get_or_fail(timer_id) {
timr = scoped_timer;
if (old_spec64)
old_spec64->it_interval = ktime_to_timespec64(timr->it_interval);
/* Prevent signal delivery and rearming. */
timr->it_signal_seq++;
int ret = timr->kclock->timer_set(timr, tmr_flags, new_spec64, old_spec64);
if (ret != TIMER_RETRY)
return ret;
/* Protect the timer from being freed when leaving the lock scope */
rcu_read_lock();
}
timer_wait_running(timr);
rcu_read_unlock();
}
}
/* Set a POSIX.1b interval timer */
SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
const struct __kernel_itimerspec __user *, new_setting,
struct __kernel_itimerspec __user *, old_setting)
{
struct itimerspec64 new_spec, old_spec, *rtn;
int error = 0;
if (!new_setting)
return -EINVAL;
if (get_itimerspec64(&new_spec, new_setting))
return -EFAULT;
rtn = old_setting ? &old_spec : NULL;
error = do_timer_settime(timer_id, flags, &new_spec, rtn);
if (!error && old_setting) {
if (put_itimerspec64(&old_spec, old_setting))
error = -EFAULT;
}
return error;
}
#ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
struct old_itimerspec32 __user *, new,
struct old_itimerspec32 __user *, old)
{
struct itimerspec64 new_spec, old_spec;
struct itimerspec64 *rtn = old ? &old_spec : NULL;
int error = 0;
if (!new)
return -EINVAL;
if (get_old_itimerspec32(&new_spec, new))
return -EFAULT;
error = do_timer_settime(timer_id, flags, &new_spec, rtn);
if (!error && old) {
if (put_old_itimerspec32(&old_spec, old))
error = -EFAULT;
}
return error;
}
#endif
int common_timer_del(struct k_itimer *timer)
{
const struct k_clock *kc = timer->kclock;
if (kc->timer_try_to_cancel(timer) < 0)
return TIMER_RETRY;
timer->it_status = POSIX_TIMER_DISARMED;
return 0;
}
/*
* If the deleted timer is on the ignored list, remove it and
* drop the associated reference.
*/
static inline void posix_timer_cleanup_ignored(struct k_itimer *tmr)
{
if (!hlist_unhashed(&tmr->ignored_list)) {
hlist_del_init(&tmr->ignored_list);
posixtimer_putref(tmr);
}
}
static void posix_timer_delete(struct k_itimer *timer)
{
/*
* Invalidate the timer, remove it from the linked list and remove
* it from the ignored list if pending.
*
* The invalidation must be written with siglock held so that the
* signal code observes the invalidated timer::it_signal in
* do_sigaction(), which prevents it from moving a pending signal
* of a deleted timer to the ignore list.
*
* The invalidation also prevents signal queueing, signal delivery
* and therefore rearming from the signal delivery path.
*
* A concurrent lookup can still find the timer in the hash, but it
* will check timer::it_signal with timer::it_lock held and observe
* bit 0 set, which invalidates it. That also prevents the timer ID
* from being handed out before this timer is completely gone.
*/
timer->it_signal_seq++;
scoped_guard (spinlock, ¤t->sighand->siglock) {
unsigned long sig = (unsigned long)timer->it_signal | 1UL;
WRITE_ONCE(timer->it_signal, (struct signal_struct *)sig);
hlist_del_rcu(&timer->list);
posix_timer_cleanup_ignored(timer);
}
while (timer->kclock->timer_del(timer) == TIMER_RETRY) {
guard(rcu)();
spin_unlock_irq(&timer->it_lock);
timer_wait_running(timer);
spin_lock_irq(&timer->it_lock);
}
}
/* Delete a POSIX.1b interval timer. */
SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
{
struct k_itimer *timer;
scoped_timer_get_or_fail(timer_id) {
timer = scoped_timer;
posix_timer_delete(timer);
}
/* Remove it from the hash, which frees up the timer ID */
posix_timer_unhash_and_free(timer);
return 0;
}
/*
* Invoked from do_exit() when the last thread of a thread group exits.
* At that point no other task can access the timers of the dying
* task anymore.
*/
void exit_itimers(struct task_struct *tsk)
{
struct hlist_head timers;
struct hlist_node *next;
struct k_itimer *timer;
/* Clear restore mode for exec() */
tsk->signal->timer_create_restore_ids = 0;
if (hlist_empty(&tsk->signal->posix_timers))
return;
/* Protect against concurrent read via /proc/$PID/timers */
scoped_guard (spinlock_irq, &tsk->sighand->siglock)
hlist_move_list(&tsk->signal->posix_timers, &timers);
/* The timers are not longer accessible via tsk::signal */
hlist_for_each_entry_safe(timer, next, &timers, list) {
scoped_guard (spinlock_irq, &timer->it_lock)
posix_timer_delete(timer);
posix_timer_unhash_and_free(timer);
cond_resched();
}
/*
* There should be no timers on the ignored list. posix_timer_delete() has
* mopped them up.
*/
if (!WARN_ON_ONCE(!hlist_empty(&tsk->signal->ignored_posix_timers)))
return;
hlist_move_list(&tsk->signal->ignored_posix_timers, &timers);
while (!hlist_empty(&timers)) {
posix_timer_cleanup_ignored(hlist_entry(timers.first, struct k_itimer,
ignored_list));
}
}
SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
const struct __kernel_timespec __user *, tp)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
struct timespec64 new_tp;
if (!kc || !kc->clock_set)
return -EINVAL;
if (get_timespec64(&new_tp, tp))
return -EFAULT;
/*
* Permission checks have to be done inside the clock specific
* setter callback.
*/
return kc->clock_set(which_clock, &new_tp);
}
SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
struct __kernel_timespec __user *, tp)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
struct timespec64 kernel_tp;
int error;
if (!kc)
return -EINVAL;
error = kc->clock_get_timespec(which_clock, &kernel_tp);
if (!error && put_timespec64(&kernel_tp, tp))
error = -EFAULT;
return error;
}
int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
if (!kc)
return -EINVAL;
if (!kc->clock_adj)
return -EOPNOTSUPP;
return kc->clock_adj(which_clock, ktx);
}
SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
struct __kernel_timex __user *, utx)
{
struct __kernel_timex ktx;
int err;
if (copy_from_user(&ktx, utx, sizeof(ktx)))
return -EFAULT;
err = do_clock_adjtime(which_clock, &ktx);
if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
return -EFAULT;
return err;
}
/**
* sys_clock_getres - Get the resolution of a clock
* @which_clock: The clock to get the resolution for
* @tp: Pointer to a a user space timespec64 for storage
*
* POSIX defines:
*
* "The clock_getres() function shall return the resolution of any
* clock. Clock resolutions are implementation-defined and cannot be set by
* a process. If the argument res is not NULL, the resolution of the
* specified clock shall be stored in the location pointed to by res. If
* res is NULL, the clock resolution is not returned. If the time argument
* of clock_settime() is not a multiple of res, then the value is truncated
* to a multiple of res."
*
* Due to the various hardware constraints the real resolution can vary
* wildly and even change during runtime when the underlying devices are
* replaced. The kernel also can use hardware devices with different
* resolutions for reading the time and for arming timers.
*
* The kernel therefore deviates from the POSIX spec in various aspects:
*
* 1) The resolution returned to user space
*
* For CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_TAI,
* CLOCK_REALTIME_ALARM, CLOCK_BOOTTIME_ALAREM and CLOCK_MONOTONIC_RAW
* the kernel differentiates only two cases:
*
* I) Low resolution mode:
*
* When high resolution timers are disabled at compile or runtime
* the resolution returned is nanoseconds per tick, which represents
* the precision at which timers expire.
*
* II) High resolution mode:
*
* When high resolution timers are enabled the resolution returned
* is always one nanosecond independent of the actual resolution of
* the underlying hardware devices.
*
* For CLOCK_*_ALARM the actual resolution depends on system
* state. When system is running the resolution is the same as the
* resolution of the other clocks. During suspend the actual
* resolution is the resolution of the underlying RTC device which
* might be way less precise than the clockevent device used during
* running state.
*
* For CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE the resolution
* returned is always nanoseconds per tick.
*
* For CLOCK_PROCESS_CPUTIME and CLOCK_THREAD_CPUTIME the resolution
* returned is always one nanosecond under the assumption that the
* underlying scheduler clock has a better resolution than nanoseconds
* per tick.
*
* For dynamic POSIX clocks (PTP devices) the resolution returned is
* always one nanosecond.
*
* 2) Affect on sys_clock_settime()
*
* The kernel does not truncate the time which is handed in to
* sys_clock_settime(). The kernel internal timekeeping is always using
* nanoseconds precision independent of the clocksource device which is
* used to read the time from. The resolution of that device only
* affects the precision of the time returned by sys_clock_gettime().
*
* Returns:
* 0 Success. @tp contains the resolution
* -EINVAL @which_clock is not a valid clock ID
* -EFAULT Copying the resolution to @tp faulted
* -ENODEV Dynamic POSIX clock is not backed by a device
* -EOPNOTSUPP Dynamic POSIX clock does not support getres()
*/
SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
struct __kernel_timespec __user *, tp)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
struct timespec64 rtn_tp;
int error;
if (!kc)
return -EINVAL;
error = kc->clock_getres(which_clock, &rtn_tp);
if (!error && tp && put_timespec64(&rtn_tp, tp))
error = -EFAULT;
return error;
}
#ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
struct old_timespec32 __user *, tp)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
struct timespec64 ts;
if (!kc || !kc->clock_set)
return -EINVAL;
if (get_old_timespec32(&ts, tp))
return -EFAULT;
return kc->clock_set(which_clock, &ts);
}
SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
struct old_timespec32 __user *, tp)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
struct timespec64 ts;
int err;
if (!kc)
return -EINVAL;
err = kc->clock_get_timespec(which_clock, &ts);
if (!err && put_old_timespec32(&ts, tp))
err = -EFAULT;
return err;
}
SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
struct old_timex32 __user *, utp)
{
struct __kernel_timex ktx;
int err;
err = get_old_timex32(&ktx, utp);
if (err)
return err;
err = do_clock_adjtime(which_clock, &ktx);
if (err >= 0 && put_old_timex32(utp, &ktx))
return -EFAULT;
return err;
}
SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
struct old_timespec32 __user *, tp)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
struct timespec64 ts;
int err;
if (!kc)
return -EINVAL;
err = kc->clock_getres(which_clock, &ts);
if (!err && tp && put_old_timespec32(&ts, tp))
return -EFAULT;
return err;
}
#endif
/*
* sys_clock_nanosleep() for CLOCK_REALTIME and CLOCK_TAI
*/
static int common_nsleep(const clockid_t which_clock, int flags,
const struct timespec64 *rqtp)
{
ktime_t texp = timespec64_to_ktime(*rqtp);
return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
which_clock);
}
/*
* sys_clock_nanosleep() for CLOCK_MONOTONIC and CLOCK_BOOTTIME
*
* Absolute nanosleeps for these clocks are time-namespace adjusted.
*/
static int common_nsleep_timens(const clockid_t which_clock, int flags,
const struct timespec64 *rqtp)
{
ktime_t texp = timespec64_to_ktime(*rqtp);
if (flags & TIMER_ABSTIME)
texp = timens_ktime_to_host(which_clock, texp);
return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
which_clock);
}
SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
const struct __kernel_timespec __user *, rqtp,
struct __kernel_timespec __user *, rmtp)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
struct timespec64 t;
if (!kc)
return -EINVAL;
if (!kc->nsleep)
return -EOPNOTSUPP;
if (get_timespec64(&t, rqtp))
return -EFAULT;
if (!timespec64_valid(&t))
return -EINVAL;
if (flags & TIMER_ABSTIME)
rmtp = NULL;
current->restart_block.fn = do_no_restart_syscall;
current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
current->restart_block.nanosleep.rmtp = rmtp;
return kc->nsleep(which_clock, flags, &t);
}
#ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
struct old_timespec32 __user *, rqtp,
struct old_timespec32 __user *, rmtp)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
struct timespec64 t;
if (!kc)
return -EINVAL;
if (!kc->nsleep)
return -EOPNOTSUPP;
if (get_old_timespec32(&t, rqtp))
return -EFAULT;
if (!timespec64_valid(&t))
return -EINVAL;
if (flags & TIMER_ABSTIME)
rmtp = NULL;
current->restart_block.fn = do_no_restart_syscall;
current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
current->restart_block.nanosleep.compat_rmtp = rmtp;
return kc->nsleep(which_clock, flags, &t);
}
#endif
static const struct k_clock clock_realtime = {
.clock_getres = posix_get_hrtimer_res,
.clock_get_timespec = posix_get_realtime_timespec,
.clock_get_ktime = posix_get_realtime_ktime,
.clock_set = posix_clock_realtime_set,
.clock_adj = posix_clock_realtime_adj,
.nsleep = common_nsleep,
.timer_create = common_timer_create,
.timer_set = common_timer_set,
.timer_get = common_timer_get,
.timer_del = common_timer_del,
.timer_rearm = common_hrtimer_rearm,
.timer_forward = common_hrtimer_forward,
.timer_remaining = common_hrtimer_remaining,
.timer_try_to_cancel = common_hrtimer_try_to_cancel,
.timer_wait_running = common_timer_wait_running,
.timer_arm = common_hrtimer_arm,
};
static const struct k_clock clock_monotonic = {
.clock_getres = posix_get_hrtimer_res,
.clock_get_timespec = posix_get_monotonic_timespec,
.clock_get_ktime = posix_get_monotonic_ktime,
.nsleep = common_nsleep_timens,
.timer_create = common_timer_create,
.timer_set = common_timer_set,
.timer_get = common_timer_get,
.timer_del = common_timer_del,
.timer_rearm = common_hrtimer_rearm,
.timer_forward = common_hrtimer_forward,
.timer_remaining = common_hrtimer_remaining,
.timer_try_to_cancel = common_hrtimer_try_to_cancel,
.timer_wait_running = common_timer_wait_running,
.timer_arm = common_hrtimer_arm,
};
static const struct k_clock clock_monotonic_raw = {
.clock_getres = posix_get_hrtimer_res,
.clock_get_timespec = posix_get_monotonic_raw,
};
static const struct k_clock clock_realtime_coarse = {
.clock_getres = posix_get_coarse_res,
.clock_get_timespec = posix_get_realtime_coarse,
};
static const struct k_clock clock_monotonic_coarse = {
.clock_getres = posix_get_coarse_res,
.clock_get_timespec = posix_get_monotonic_coarse,
};
static const struct k_clock clock_tai = {
.clock_getres = posix_get_hrtimer_res,
.clock_get_ktime = posix_get_tai_ktime,
.clock_get_timespec = posix_get_tai_timespec,
.nsleep = common_nsleep,
.timer_create = common_timer_create,
.timer_set = common_timer_set,
.timer_get = common_timer_get,
.timer_del = common_timer_del,
.timer_rearm = common_hrtimer_rearm,
.timer_forward = common_hrtimer_forward,
.timer_remaining = common_hrtimer_remaining,
.timer_try_to_cancel = common_hrtimer_try_to_cancel,
.timer_wait_running = common_timer_wait_running,
.timer_arm = common_hrtimer_arm,
};
static const struct k_clock clock_boottime = {
.clock_getres = posix_get_hrtimer_res,
.clock_get_ktime = posix_get_boottime_ktime,
.clock_get_timespec = posix_get_boottime_timespec,
.nsleep = common_nsleep_timens,
.timer_create = common_timer_create,
.timer_set = common_timer_set,
.timer_get = common_timer_get,
.timer_del = common_timer_del,
.timer_rearm = common_hrtimer_rearm,
.timer_forward = common_hrtimer_forward,
.timer_remaining = common_hrtimer_remaining,
.timer_try_to_cancel = common_hrtimer_try_to_cancel,
.timer_wait_running = common_timer_wait_running,
.timer_arm = common_hrtimer_arm,
};
static const struct k_clock * const posix_clocks[] = {
[CLOCK_REALTIME] = &clock_realtime,
[CLOCK_MONOTONIC] = &clock_monotonic,
[CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
[CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
[CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
[CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
[CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
[CLOCK_BOOTTIME] = &clock_boottime,
[CLOCK_REALTIME_ALARM] = &alarm_clock,
[CLOCK_BOOTTIME_ALARM] = &alarm_clock,
[CLOCK_TAI] = &clock_tai,
#ifdef CONFIG_POSIX_AUX_CLOCKS
[CLOCK_AUX ... CLOCK_AUX_LAST] = &clock_aux,
#endif
};
static const struct k_clock *clockid_to_kclock(const clockid_t id)
{
clockid_t idx = id;
if (id < 0) {
return (id & CLOCKFD_MASK) == CLOCKFD ?
&clock_posix_dynamic : &clock_posix_cpu;
}
if (id >= ARRAY_SIZE(posix_clocks))
return NULL;
return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
}
static int __init posixtimer_init(void)
{
unsigned long i, size;
unsigned int shift;
posix_timers_cache = kmem_cache_create("posix_timers_cache",
sizeof(struct k_itimer),
__alignof__(struct k_itimer),
SLAB_ACCOUNT, NULL);
if (IS_ENABLED(CONFIG_BASE_SMALL))
size = 512;
else
size = roundup_pow_of_two(512 * num_possible_cpus());
timer_buckets = alloc_large_system_hash("posixtimers", sizeof(*timer_buckets),
size, 0, 0, &shift, NULL, size, size);
size = 1UL << shift;
timer_hashmask = size - 1;
for (i = 0; i < size; i++) {
spin_lock_init(&timer_buckets[i].lock);
INIT_HLIST_HEAD(&timer_buckets[i].head);
}
return 0;
}
core_initcall(posixtimer_init);
]
|
| 42/2 |
2026/05/18 16:26 |
llm |
expert |
0m
Model:
Tokens:
input: 63901
output: 66
thoughts: 448
Thoughts: **My Assessment of the KCSAN Data Race Report**
Okay, let me break this down. Yes, I see that the `SYSCALL_DEFINE2(clock_gettime, ...)` macro is indeed defined, and it correctly calls `kc->clock_get_timespec`. For `CLOCK_REALTIME`, that resolves to `posix_get_realtime_timespec`, which then calls `ktime_get_real_ts64`. So, any unprivileged process can trigger that code path simply by calling `clock_gettime(CLOCK_REALTIME, ...)`. That confirms the accessibility from unprivileged contexts.
Essentially, KCSAN is flagging a data race in `timekeeping.c`. The race is between `timekeeping_update_from_shadow` (the writer) and `ktime_get_real_ts64` (the reader). The writer is updating the `timekeeper` structure within a seqlock write critical section, using `write_seqcount_begin` and `write_seqcount_end` to protect it. The reader, on the other hand, is accessing the same data, but under a seqlock read critical section, using `read_seqcount_begin` and `read_seqcount_retry`.
The KCSAN report is triggered because the reader is performing plain reads (like `tkr->cycle_last`), while the writer is doing a plain `memcpy`. This is a pretty standard pattern in the kernel, where seqlocks are used to guard data, and KCSAN understandably flags these plain accesses within the seqlock sections if they lack any explicit annotation. The seqlock retry logic is the crucial point here; it ensures that if the reader encounters a race condition and gets inconsistent data, it will retry and ultimately get consistent results.
Given that the seqlock retry logic is in place, the torn reads that KCSAN is reporting **cannot** cause a crash or any observable, incorrect behavior *before* the retry mechanism kicks in. Therefore, this looks like a benign data race, and not a real bug. It's not exploitable, and it certainly won't lead to a denial of service. The code path can be triggered easily by unprivileged users, network packets, VM guests, etc., but it's fundamentally safe due to the seqlock retry mechanism.
I am confident in my assessment. I will now call `set-results`.
|
| 43/2 |
2026/05/18 16:27 |
tool |
set-results |
0m
Args: map[DenialOfService:false Exploitable:false FilesystemTrigger:true NetworkTrigger:true PeripheralTrigger:true RemoteTrigger:true Unprivileged:true UserNamespace:true VMGuestTrigger:true VMHostTrigger:true]
Results: map[DenialOfService:false Exploitable:false FilesystemTrigger:true NetworkTrigger:true PeripheralTrigger:true RemoteTrigger:true Unprivileged:true UserNamespace:true VMGuestTrigger:true VMHostTrigger:true]
|
| 44/2 |
2026/05/18 16:27 |
llm |
expert |
0m
Model:
Tokens:
input: 64994
output: 645
thoughts: 132
Thoughts: Okay, here's my take on those results, distilled into a concise summary I can share:
**Final Analysis: Timekeeping Seqlock KCSAN Report**
Alright, the data is in, and it's time to craft the final response. My immediate assessment is that this KCSAN report highlights a data race on the timekeeping seqlock. The core of the problem lies in the interaction between the read and write sides of this seqlock. Specifically, any operation that touches the system time – things like handling incoming network packets or calling `clock_gettime` – can trigger the read side. Meanwhile, the timer interrupt itself is the likely culprit triggering the write side, as it updates the system time.
However, and this is crucial, the impact is minimal. We're dealing with a standard seqlock pattern. That means the reader will simply retry if it encounters a data race. Critically, these kinds of "torn reads" *won't* cause any crashes or system instability. This is all standard seqlock behavior. The mechanism handles these kinds of interactions as designed.
I'm confident in this assessment. Everything aligns as expected.
|
| 45/1 |
2026/05/18 16:27 |
action |
format-explanation |
0m
Results: map[Explanation:Based on the analysis of the provided kernel bug report and the source code,
here is the assessment of the bug's security impact:
### Bug Analysis
The bug report is a Kernel Concurrency Sanitizer (KCSAN) warning about a data
race in the kernel's timekeeping subsystem.
- **The Writer:** The timer interrupt (`hrtimer_interrupt` ->
`timekeeping_update_from_shadow`) updates the core timekeeper structure
(`tk_core.timekeeper`). It does so by copying the shadow timekeeper using a
plain `memcpy()` inside a seqlock write critical section
(`write_seqcount_begin()` / `write_seqcount_end()`).
- **The Reader:** A process (in this case, processing an incoming network packet
via `tpacket_rcv` -> `ktime_get_real_ts64`) reads the timekeeper structure to
get the current time. It performs plain reads (e.g., reading `tkr->cycle_last`)
inside a seqlock read critical section (`read_seqcount_begin()` /
`read_seqcount_retry()`).
KCSAN flags this as a data race because a plain read is racing with a plain
write (`memcpy`). However, this is a well-known and standard seqlock pattern in
the Linux kernel. The seqlock mechanism ensures that if the writer modifies the
data while the reader is reading it, the sequence number will change, and the
reader's `read_seqcount_retry()` will fail, forcing the reader to loop and read
the data again until it gets a consistent snapshot.
Furthermore, the plain reads performed by the reader (such as `tkr->cycle_last`,
`tkr->mult`, `tkr->shift`) are only used in safe arithmetic operations
(calculating time deltas) before the sequence number is validated. There are no
pointer dereferences or divisions by zero that could cause a crash from a torn
read before the retry logic kicks in. (Pointers like `tkr->clock` are safely
read using `READ_ONCE()` inside `tk_clock_read()`).
### Security Impact
- **Exploitable: No.** This is a completely benign data race handled correctly
by the seqlock retry logic. It cannot lead to memory corruption, wild pointer
dereferences, or privilege escalation.
- **Denial of Service: No.** The race does not cause crashes, hangs, deadlocks,
or resource leaks. In production builds, KCSAN is disabled, so this will not
even generate a warning in the kernel logs.
- **Triggers:** Because timekeeping functions like `ktime_get_real_ts64()` are
heavily used throughout the kernel, the read-side of this race can be triggered
by almost anything. Unprivileged processes can trigger it directly via the
`clock_gettime(CLOCK_REALTIME)` syscall or indirectly by receiving network
packets (as seen in the stack trace), interacting with filesystems, or using
peripherals. It is equally reachable from user namespaces, VM guests, and VM
hosts.]
|