1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/prctl.h>
7 * A simple wrapper around refcount. An allocated sched_core_cookie's
8 * address is used to compute the cookie of the task.
10 struct sched_core_cookie {
14 unsigned long sched_core_alloc_cookie(void)
16 struct sched_core_cookie *ck = kmalloc(sizeof(*ck), GFP_KERNEL);
20 refcount_set(&ck->refcnt, 1);
23 return (unsigned long)ck;
26 void sched_core_put_cookie(unsigned long cookie)
28 struct sched_core_cookie *ptr = (void *)cookie;
30 if (ptr && refcount_dec_and_test(&ptr->refcnt)) {
36 unsigned long sched_core_get_cookie(unsigned long cookie)
38 struct sched_core_cookie *ptr = (void *)cookie;
41 refcount_inc(&ptr->refcnt);
47 * sched_core_update_cookie - replace the cookie on a task
48 * @p: the task to update
49 * @cookie: the new cookie
51 * Effectively exchange the task cookie; caller is responsible for lifetimes on
54 * Returns: the old cookie
56 unsigned long sched_core_update_cookie(struct task_struct *p, unsigned long cookie)
58 unsigned long old_cookie;
63 rq = task_rq_lock(p, &rf);
66 * Since creating a cookie implies sched_core_get(), and we cannot set
67 * a cookie until after we've created it, similarly, we cannot destroy
68 * a cookie until after we've removed it, we must have core scheduling
71 SCHED_WARN_ON((p->core_cookie || cookie) && !sched_core_enabled(rq));
73 enqueued = sched_core_enqueued(p);
75 sched_core_dequeue(rq, p);
77 old_cookie = p->core_cookie;
78 p->core_cookie = cookie;
81 sched_core_enqueue(rq, p);
84 * If task is currently running, it may not be compatible anymore after
85 * the cookie change, so enter the scheduler on its CPU to schedule it
88 if (task_running(rq, p))
91 task_rq_unlock(rq, p, &rf);
96 static unsigned long sched_core_clone_cookie(struct task_struct *p)
98 unsigned long cookie, flags;
100 raw_spin_lock_irqsave(&p->pi_lock, flags);
101 cookie = sched_core_get_cookie(p->core_cookie);
102 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
107 void sched_core_fork(struct task_struct *p)
109 RB_CLEAR_NODE(&p->core_node);
110 p->core_cookie = sched_core_clone_cookie(current);
113 void sched_core_free(struct task_struct *p)
115 sched_core_put_cookie(p->core_cookie);
118 static void __sched_core_set(struct task_struct *p, unsigned long cookie)
120 cookie = sched_core_get_cookie(cookie);
121 cookie = sched_core_update_cookie(p, cookie);
122 sched_core_put_cookie(cookie);
125 /* Called from prctl interface: PR_SCHED_CORE */
126 int sched_core_share_pid(unsigned int cmd, pid_t pid, enum pid_type type,
129 unsigned long cookie = 0, id = 0;
130 struct task_struct *task, *p;
134 if (!static_branch_likely(&sched_smt_present))
137 BUILD_BUG_ON(PR_SCHED_CORE_SCOPE_THREAD != PIDTYPE_PID);
138 BUILD_BUG_ON(PR_SCHED_CORE_SCOPE_THREAD_GROUP != PIDTYPE_TGID);
139 BUILD_BUG_ON(PR_SCHED_CORE_SCOPE_PROCESS_GROUP != PIDTYPE_PGID);
141 if (type > PIDTYPE_PGID || cmd >= PR_SCHED_CORE_MAX || pid < 0 ||
142 (cmd != PR_SCHED_CORE_GET && uaddr))
149 task = find_task_by_vpid(pid);
155 get_task_struct(task);
159 * Check if this process has the right to modify the specified
160 * process. Use the regular "ptrace_may_access()" checks.
162 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
168 case PR_SCHED_CORE_GET:
169 if (type != PIDTYPE_PID || uaddr & 7) {
173 cookie = sched_core_clone_cookie(task);
176 ptr_to_hashval((void *)cookie, &id);
178 err = put_user(id, (u64 __user *)uaddr);
181 case PR_SCHED_CORE_CREATE:
182 cookie = sched_core_alloc_cookie();
189 case PR_SCHED_CORE_SHARE_TO:
190 cookie = sched_core_clone_cookie(current);
193 case PR_SCHED_CORE_SHARE_FROM:
194 if (type != PIDTYPE_PID) {
198 cookie = sched_core_clone_cookie(task);
199 __sched_core_set(current, cookie);
207 if (type == PIDTYPE_PID) {
208 __sched_core_set(task, cookie);
212 read_lock(&tasklist_lock);
213 grp = task_pid_type(task, type);
215 do_each_pid_thread(grp, type, p) {
216 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS)) {
220 } while_each_pid_thread(grp, type, p);
222 do_each_pid_thread(grp, type, p) {
223 __sched_core_set(p, cookie);
224 } while_each_pid_thread(grp, type, p);
226 read_unlock(&tasklist_lock);
229 sched_core_put_cookie(cookie);
230 put_task_struct(task);