kernel/futex/syscalls.c

Source file repositories/reference/linux-study-clean/kernel/futex/syscalls.c

File Facts

System
Linux kernel
Corpus path
kernel/futex/syscalls.c
Extension
.c
Size
13858 bytes
Lines
527
Domain
Core OS
Bucket
Scheduler, Processes, Timers, Sync, And Syscalls
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.

Dependency Surface

Detected Declarations

Annotated Snippet

SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head, size_t, len)
{
	/* The kernel knows only one size for now. */
	if (unlikely(len != sizeof(*head)))
		return -EINVAL;

	current->futex.robust_list = head;
	return 0;
}

static inline void __user *futex_task_robust_list(struct task_struct *p, bool compat)
{
#ifdef CONFIG_COMPAT
	if (compat)
		return p->futex.compat_robust_list;
#endif
	return p->futex.robust_list;
}

static void __user *futex_get_robust_list_common(int pid, bool compat)
{
	struct task_struct *p = current;
	void __user *head;
	int ret;

	scoped_guard(rcu) {
		if (pid) {
			p = find_task_by_vpid(pid);
			if (!p)
				return (void __user *)ERR_PTR(-ESRCH);
		}
		get_task_struct(p);
	}

	/*
	 * Hold exec_update_lock to serialize with concurrent exec()
	 * so ptrace_may_access() is checked against stable credentials
	 */
	ret = down_read_killable(&p->signal->exec_update_lock);
	if (ret)
		goto err_put;

	ret = -EPERM;
	if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
		goto err_unlock;

	head = futex_task_robust_list(p, compat);

	up_read(&p->signal->exec_update_lock);
	put_task_struct(p);

	return head;

err_unlock:
	up_read(&p->signal->exec_update_lock);
err_put:
	put_task_struct(p);
	return (void __user *)ERR_PTR(ret);
}

/**
 * sys_get_robust_list() - Get the robust-futex list head of a task
 * @pid:	pid of the process [zero for current task]
 * @head_ptr:	pointer to a list-head pointer, the kernel fills it in
 * @len_ptr:	pointer to a length field, the kernel fills in the header size
 */
SYSCALL_DEFINE3(get_robust_list, int, pid,
		struct robust_list_head __user * __user *, head_ptr,
		size_t __user *, len_ptr)
{
	struct robust_list_head __user *head = futex_get_robust_list_common(pid, false);

	if (IS_ERR(head))
		return PTR_ERR(head);

	if (put_user(sizeof(*head), len_ptr))
		return -EFAULT;
	return put_user(head, head_ptr);
}

long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
		u32 __user *uaddr2, u32 val2, u32 val3)
{
	unsigned int flags = futex_to_flags(op);
	int cmd = op & FUTEX_CMD_MASK;

	if (flags & FLAGS_CLOCKRT) {
		if (cmd != FUTEX_WAIT_BITSET &&
		    cmd != FUTEX_WAIT_REQUEUE_PI &&
		    cmd != FUTEX_LOCK_PI2)

Annotation

Implementation Notes