fs/pipe.c
Source file repositories/reference/linux-study-clean/fs/pipe.c
File Facts
- System
- Linux kernel
- Corpus path
fs/pipe.c- Extension
.c- Size
- 40605 bytes
- Lines
- 1651
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: syscall or user/kernel boundary
- Status
- core implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/mm.hlinux/file.hlinux/poll.hlinux/slab.hlinux/module.hlinux/init.hlinux/fs.hlinux/log2.hlinux/mount.hlinux/pseudo_fs.hlinux/magic.hlinux/pipe_fs_i.hlinux/uio.hlinux/highmem.hlinux/pagemap.hlinux/audit.hlinux/syscalls.hlinux/fcntl.hlinux/memcontrol.hlinux/watch_queue.hlinux/sysctl.hlinux/sort.hlinux/uaccess.hasm/ioctls.hinternal.h
Detected Declarations
syscall pipe2syscall pipestruct anon_pipe_preallocfunction pipe_lock_cmp_fnfunction pipe_lockfunction pipe_unlockfunction pipe_double_lockfunction anon_pipe_get_page_preallocfunction anon_pipe_put_pagefunction anon_pipe_refill_tmp_pagesfunction anon_pipe_free_pagesfunction anon_pipe_buf_releasefunction anon_pipe_buf_try_stealfunction generic_pipe_buf_try_stealfunction teefunction generic_pipe_buf_releasefunction pipe_readablefunction pipe_update_tailfunction anon_pipe_readfunction fifo_pipe_readfunction is_packetizedfunction pipe_writablefunction anon_pipe_writefunction fifo_pipe_writefunction pipe_ioctlfunction pipe_pollfunction put_pipe_infofunction pipe_releasefunction pipe_fasyncfunction account_pipe_buffersfunction too_many_pipe_buffers_softfunction too_many_pipe_buffers_hardfunction pipe_is_unprivileged_userfunction free_pipe_infofunction pipefs_dnamefunction get_pipe_inodefunction create_pipe_filesfunction __do_pipe_flagsfunction do_pipe_flagsfunction sys_pipefunction everythingfunction pipe_wait_writablefunction waitfunction wake_up_partnerfunction fifo_openfunction round_pipe_sizefunction pipe_resize_ringfunction pipe_set_size
Annotated Snippet
SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
{
return do_pipe2(fildes, flags);
}
SYSCALL_DEFINE1(pipe, int __user *, fildes)
{
return do_pipe2(fildes, 0);
}
/*
* This is the stupid "wait for pipe to be readable or writable"
* model.
*
* See pipe_read/write() for the proper kind of exclusive wait,
* but that requires that we wake up any other readers/writers
* if we then do not end up reading everything (ie the whole
* "wake_next_reader/writer" logic in pipe_read/write()).
*/
void pipe_wait_readable(struct pipe_inode_info *pipe)
{
pipe_unlock(pipe);
wait_event_interruptible(pipe->rd_wait, pipe_readable(pipe));
pipe_lock(pipe);
}
void pipe_wait_writable(struct pipe_inode_info *pipe)
{
pipe_unlock(pipe);
wait_event_interruptible(pipe->wr_wait, pipe_writable(pipe));
pipe_lock(pipe);
}
/*
* This depends on both the wait (here) and the wakeup (wake_up_partner)
* holding the pipe lock, so "*cnt" is stable and we know a wakeup cannot
* race with the count check and waitqueue prep.
*
* Normally in order to avoid races, you'd do the prepare_to_wait() first,
* then check the condition you're waiting for, and only then sleep. But
* because of the pipe lock, we can check the condition before being on
* the wait queue.
*
* We use the 'rd_wait' waitqueue for pipe partner waiting.
*/
static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
{
DEFINE_WAIT(rdwait);
int cur = *cnt;
while (cur == *cnt) {
prepare_to_wait(&pipe->rd_wait, &rdwait, TASK_INTERRUPTIBLE);
pipe_unlock(pipe);
schedule();
finish_wait(&pipe->rd_wait, &rdwait);
pipe_lock(pipe);
if (signal_pending(current))
break;
}
return cur == *cnt ? -ERESTARTSYS : 0;
}
static void wake_up_partner(struct pipe_inode_info *pipe)
{
wake_up_interruptible_all(&pipe->rd_wait);
}
static int fifo_open(struct inode *inode, struct file *filp)
{
bool is_pipe = inode->i_fop == &pipeanon_fops;
struct pipe_inode_info *pipe;
int ret;
filp->f_pipe = 0;
spin_lock(&inode->i_lock);
if (inode->i_pipe) {
pipe = inode->i_pipe;
pipe->files++;
spin_unlock(&inode->i_lock);
} else {
spin_unlock(&inode->i_lock);
pipe = alloc_pipe_info();
if (!pipe)
return -ENOMEM;
pipe->files = 1;
spin_lock(&inode->i_lock);
if (unlikely(inode->i_pipe)) {
inode->i_pipe->files++;
spin_unlock(&inode->i_lock);
Annotation
- Immediate include surface: `linux/mm.h`, `linux/file.h`, `linux/poll.h`, `linux/slab.h`, `linux/module.h`, `linux/init.h`, `linux/fs.h`, `linux/log2.h`.
- Detected declarations: `syscall pipe2`, `syscall pipe`, `struct anon_pipe_prealloc`, `function pipe_lock_cmp_fn`, `function pipe_lock`, `function pipe_unlock`, `function pipe_double_lock`, `function anon_pipe_get_page_prealloc`, `function anon_pipe_put_page`, `function anon_pipe_refill_tmp_pages`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: core implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.