Root Cause Analysis
1. Raced Memory Location and Access Points
The reported KCSAN data race occurs on the 8-byte field struct file::f_pos (loff_t, defined at include/linux/fs.h:1280):
- Read access: In
do_writev() (fs/read_write.c:1081), the caller inspects the file position via pos = *ppos;, where ppos is obtained from file_ppos(fd_file(f)).
- Write access: In
do_writev() (fs/read_write.c:1086), after vfs_writev() returns, the updated position is written back using plain assignment: fd_file(f)->f_pos = pos;.
(Analogous unmarked reads and writes exist in ksys_read(), ksys_write(), and do_readv().)
2. Why file->f_pos_lock Is Not Acquired
do_writev() uses CLASS(fd_pos, f)(fd), which calls fdget_pos(fd) (fs/file.c:1406β1416). fdget_pos() conditionally takes file->f_pos_lock:
struct fd fdget_pos(unsigned int fd)
{
struct fd f = fdget(fd);
struct file *file = fd_file(f);
if (likely(file) && file_needs_f_pos_lock(file)) {
f.word |= FDPUT_POS_UNLOCK;
mutex_lock(&file->f_pos_lock);
}
return f;
}
file_needs_f_pos_lock(file) (fs/file.c:1377β1386) checks:
static inline bool file_needs_f_pos_lock(struct file *file)
{
if (!(file->f_mode & FMODE_ATOMIC_POS))
return false;
...
Per POSIX.1-2008 / SUSv4 Section XSI 2.9.7, do_dentry_open() (fs/open.c:968β969) sets FMODE_ATOMIC_POS only for regular files (S_ISREG(inode->i_mode)) and directories (S_ISDIR(inode->i_mode)). For other files (e.g., character devices, block devices), FMODE_ATOMIC_POS is not set. Consequently, file_needs_f_pos_lock() evaluates to false and f_pos_lock is omitted.
3. Why file_ppos() Still Exposes &file->f_pos
In fs/read_write.c:
static inline loff_t *file_ppos(struct file *file)
{
return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
}
file_ppos() returns non-NULL (&file->f_pos) for any file where FMODE_STREAM is not set. Devices that implement custom seek or are not opened via stream_open() (for example, /dev/kmsg via kmsg_fops in kernel/printk/printk.c, which implements .llseek = devkmsg_llseek) do not have FMODE_STREAM set.
When two tasks share the same open file description lacking both FMODE_ATOMIC_POS and FMODE_STREAM (such as udevd and its worker threads/forked processes writing logs to an inherited /dev/kmsg file descriptor using writev()), neither process acquires file->f_pos_lock. Because both execute do_writev() concurrently, one task performs an unsynchronized read of file->f_pos at line 1081 while the other performs an unsynchronized write to file->f_pos at line 1086, triggering KCSAN.