ipc/mqueue.c
Source file repositories/reference/linux-study-clean/ipc/mqueue.c
File Facts
- System
- Linux kernel
- Corpus path
ipc/mqueue.c- Extension
.c- Size
- 43791 bytes
- Lines
- 1681
- Domain
- Core OS
- Bucket
- IPC
- 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/capability.hlinux/init.hlinux/pagemap.hlinux/file.hlinux/mount.hlinux/fs_context.hlinux/namei.hlinux/sysctl.hlinux/poll.hlinux/mqueue.hlinux/msg.hlinux/skbuff.hlinux/vmalloc.hlinux/netlink.hlinux/syscalls.hlinux/audit.hlinux/signal.hlinux/mutex.hlinux/nsproxy.hlinux/pid.hlinux/ipc_namespace.hlinux/user_namespace.hlinux/slab.hlinux/sched/wake_q.hlinux/sched/signal.hlinux/sched/user.hnet/sock.hutil.h
Detected Declarations
syscall mq_opensyscall mq_unlinksyscall mq_timedsendsyscall mq_timedreceivesyscall mq_notifysyscall mq_getsetattrsyscall mq_timedsend_time32syscall mq_timedreceive_time32struct mqueue_fs_contextstruct posix_msg_tree_nodestruct ext_wait_queuestruct mqueue_inode_infostruct compat_mq_attrfunction msg_insertfunction msg_tree_erasefunction mqueue_fill_superfunction mqueue_get_treefunction mqueue_fs_context_freefunction mqueue_init_fs_contextfunction mq_init_nsfunction init_oncefunction mqueue_free_inodefunction mqueue_evict_inodefunction list_for_each_entry_safefunction mqueue_create_attrfunction mqueue_createfunction mqueue_unlinkfunction mqueue_read_filefunction mqueue_flush_filefunction mqueue_poll_filefunction wq_addfunction list_for_each_entryfunction wq_sleepfunction set_cookiefunction __do_notifyfunction prepare_timeoutfunction remove_notificationfunction prepare_openfunction do_mq_openfunction wakeupfunction sys_mq_timedreceivefunction queuefunction do_mq_timedsendfunction do_mq_timedreceivefunction do_mq_notifyfunction do_mq_getsetattrfunction get_compat_mq_attrfunction put_compat_mq_attr
Annotated Snippet
SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
struct mq_attr __user *, u_attr)
{
struct mq_attr attr;
if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
return -EFAULT;
return do_mq_open(u_name, oflag, mode, u_attr ? &attr : NULL);
}
SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
{
int err;
struct dentry *dentry;
struct inode *inode;
struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
struct vfsmount *mnt = ipc_ns->mq_mnt;
CLASS(filename, name)(u_name);
if (IS_ERR(name))
return PTR_ERR(name);
audit_inode_parent_hidden(name, mnt->mnt_root);
err = mnt_want_write(mnt);
if (err)
return err;
dentry = start_removing_noperm(mnt->mnt_root, &QSTR(name->name));
if (IS_ERR(dentry)) {
err = PTR_ERR(dentry);
goto out_drop_write;
}
inode = d_inode(dentry);
ihold(inode);
err = vfs_unlink(&nop_mnt_idmap, d_inode(mnt->mnt_root),
dentry, NULL);
end_removing(dentry);
iput(inode);
out_drop_write:
mnt_drop_write(mnt);
return err;
}
/* Pipelined send and receive functions.
*
* If a receiver finds no waiting message, then it registers itself in the
* list of waiting receivers. A sender checks that list before adding the new
* message into the message array. If there is a waiting receiver, then it
* bypasses the message array and directly hands the message over to the
* receiver. The receiver accepts the message and returns without grabbing the
* queue spinlock:
*
* - Set pointer to message.
* - Queue the receiver task for later wakeup (without the info->lock).
* - Update its state to STATE_READY. Now the receiver can continue.
* - Wake up the process after the lock is dropped. Should the process wake up
* before this wakeup (due to a timeout or a signal) it will either see
* STATE_READY and continue or acquire the lock to check the state again.
*
* The same algorithm is used for senders.
*/
static inline void __pipelined_op(struct wake_q_head *wake_q,
struct mqueue_inode_info *info,
struct ext_wait_queue *this)
{
struct task_struct *task;
list_del(&this->list);
task = get_task_struct(this->task);
/* see MQ_BARRIER for purpose/pairing */
smp_store_release(&this->state, STATE_READY);
wake_q_add_safe(wake_q, task);
}
/* pipelined_send() - send a message directly to the task waiting in
* sys_mq_timedreceive() (without inserting message into a queue).
*/
static inline void pipelined_send(struct wake_q_head *wake_q,
struct mqueue_inode_info *info,
struct msg_msg *message,
struct ext_wait_queue *receiver)
{
receiver->msg = message;
__pipelined_op(wake_q, info, receiver);
}
/* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
Annotation
- Immediate include surface: `linux/capability.h`, `linux/init.h`, `linux/pagemap.h`, `linux/file.h`, `linux/mount.h`, `linux/fs_context.h`, `linux/namei.h`, `linux/sysctl.h`.
- Detected declarations: `syscall mq_open`, `syscall mq_unlink`, `syscall mq_timedsend`, `syscall mq_timedreceive`, `syscall mq_notify`, `syscall mq_getsetattr`, `syscall mq_timedsend_time32`, `syscall mq_timedreceive_time32`, `struct mqueue_fs_context`, `struct posix_msg_tree_node`.
- Atlas domain: Core OS / IPC.
- 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.