xref: /openbmc/linux/kernel/sched/core_sched.c (revision 0c5c62ddf88c34bc83b66e4ac9beb2bb0e1887d4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/prctl.h>
4 #include "sched.h"
5 
6 /*
7  * A simple wrapper around refcount. An allocated sched_core_cookie's
8  * address is used to compute the cookie of the task.
9  */
10 struct sched_core_cookie {
11 	refcount_t refcnt;
12 };
13 
14 static unsigned long sched_core_alloc_cookie(void)
15 {
16 	struct sched_core_cookie *ck = kmalloc(sizeof(*ck), GFP_KERNEL);
17 	if (!ck)
18 		return 0;
19 
20 	refcount_set(&ck->refcnt, 1);
21 	sched_core_get();
22 
23 	return (unsigned long)ck;
24 }
25 
26 static void sched_core_put_cookie(unsigned long cookie)
27 {
28 	struct sched_core_cookie *ptr = (void *)cookie;
29 
30 	if (ptr && refcount_dec_and_test(&ptr->refcnt)) {
31 		kfree(ptr);
32 		sched_core_put();
33 	}
34 }
35 
36 static unsigned long sched_core_get_cookie(unsigned long cookie)
37 {
38 	struct sched_core_cookie *ptr = (void *)cookie;
39 
40 	if (ptr)
41 		refcount_inc(&ptr->refcnt);
42 
43 	return cookie;
44 }
45 
46 /*
47  * sched_core_update_cookie - replace the cookie on a task
48  * @p: the task to update
49  * @cookie: the new cookie
50  *
51  * Effectively exchange the task cookie; caller is responsible for lifetimes on
52  * both ends.
53  *
54  * Returns: the old cookie
55  */
56 static unsigned long sched_core_update_cookie(struct task_struct *p,
57 					      unsigned long cookie)
58 {
59 	unsigned long old_cookie;
60 	struct rq_flags rf;
61 	struct rq *rq;
62 	bool enqueued;
63 
64 	rq = task_rq_lock(p, &rf);
65 
66 	/*
67 	 * Since creating a cookie implies sched_core_get(), and we cannot set
68 	 * a cookie until after we've created it, similarly, we cannot destroy
69 	 * a cookie until after we've removed it, we must have core scheduling
70 	 * enabled here.
71 	 */
72 	SCHED_WARN_ON((p->core_cookie || cookie) && !sched_core_enabled(rq));
73 
74 	enqueued = sched_core_enqueued(p);
75 	if (enqueued)
76 		sched_core_dequeue(rq, p);
77 
78 	old_cookie = p->core_cookie;
79 	p->core_cookie = cookie;
80 
81 	if (enqueued)
82 		sched_core_enqueue(rq, p);
83 
84 	/*
85 	 * If task is currently running, it may not be compatible anymore after
86 	 * the cookie change, so enter the scheduler on its CPU to schedule it
87 	 * away.
88 	 */
89 	if (task_running(rq, p))
90 		resched_curr(rq);
91 
92 	task_rq_unlock(rq, p, &rf);
93 
94 	return old_cookie;
95 }
96 
97 static unsigned long sched_core_clone_cookie(struct task_struct *p)
98 {
99 	unsigned long cookie, flags;
100 
101 	raw_spin_lock_irqsave(&p->pi_lock, flags);
102 	cookie = sched_core_get_cookie(p->core_cookie);
103 	raw_spin_unlock_irqrestore(&p->pi_lock, flags);
104 
105 	return cookie;
106 }
107 
108 void sched_core_fork(struct task_struct *p)
109 {
110 	RB_CLEAR_NODE(&p->core_node);
111 	p->core_cookie = sched_core_clone_cookie(current);
112 }
113 
114 void sched_core_free(struct task_struct *p)
115 {
116 	sched_core_put_cookie(p->core_cookie);
117 }
118 
119 static void __sched_core_set(struct task_struct *p, unsigned long cookie)
120 {
121 	cookie = sched_core_get_cookie(cookie);
122 	cookie = sched_core_update_cookie(p, cookie);
123 	sched_core_put_cookie(cookie);
124 }
125 
126 /* Called from prctl interface: PR_SCHED_CORE */
127 int sched_core_share_pid(unsigned int cmd, pid_t pid, enum pid_type type,
128 			 unsigned long uaddr)
129 {
130 	unsigned long cookie = 0, id = 0;
131 	struct task_struct *task, *p;
132 	struct pid *grp;
133 	int err = 0;
134 
135 	if (!static_branch_likely(&sched_smt_present))
136 		return -ENODEV;
137 
138 	if (type > PIDTYPE_PGID || cmd >= PR_SCHED_CORE_MAX || pid < 0 ||
139 	    (cmd != PR_SCHED_CORE_GET && uaddr))
140 		return -EINVAL;
141 
142 	rcu_read_lock();
143 	if (pid == 0) {
144 		task = current;
145 	} else {
146 		task = find_task_by_vpid(pid);
147 		if (!task) {
148 			rcu_read_unlock();
149 			return -ESRCH;
150 		}
151 	}
152 	get_task_struct(task);
153 	rcu_read_unlock();
154 
155 	/*
156 	 * Check if this process has the right to modify the specified
157 	 * process. Use the regular "ptrace_may_access()" checks.
158 	 */
159 	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
160 		err = -EPERM;
161 		goto out;
162 	}
163 
164 	switch (cmd) {
165 	case PR_SCHED_CORE_GET:
166 		if (type != PIDTYPE_PID || uaddr & 7) {
167 			err = -EINVAL;
168 			goto out;
169 		}
170 		cookie = sched_core_clone_cookie(task);
171 		if (cookie) {
172 			/* XXX improve ? */
173 			ptr_to_hashval((void *)cookie, &id);
174 		}
175 		err = put_user(id, (u64 __user *)uaddr);
176 		goto out;
177 
178 	case PR_SCHED_CORE_CREATE:
179 		cookie = sched_core_alloc_cookie();
180 		if (!cookie) {
181 			err = -ENOMEM;
182 			goto out;
183 		}
184 		break;
185 
186 	case PR_SCHED_CORE_SHARE_TO:
187 		cookie = sched_core_clone_cookie(current);
188 		break;
189 
190 	case PR_SCHED_CORE_SHARE_FROM:
191 		if (type != PIDTYPE_PID) {
192 			err = -EINVAL;
193 			goto out;
194 		}
195 		cookie = sched_core_clone_cookie(task);
196 		__sched_core_set(current, cookie);
197 		goto out;
198 
199 	default:
200 		err = -EINVAL;
201 		goto out;
202 	};
203 
204 	if (type == PIDTYPE_PID) {
205 		__sched_core_set(task, cookie);
206 		goto out;
207 	}
208 
209 	read_lock(&tasklist_lock);
210 	grp = task_pid_type(task, type);
211 
212 	do_each_pid_thread(grp, type, p) {
213 		if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS)) {
214 			err = -EPERM;
215 			goto out_tasklist;
216 		}
217 	} while_each_pid_thread(grp, type, p);
218 
219 	do_each_pid_thread(grp, type, p) {
220 		__sched_core_set(p, cookie);
221 	} while_each_pid_thread(grp, type, p);
222 out_tasklist:
223 	read_unlock(&tasklist_lock);
224 
225 out:
226 	sched_core_put_cookie(cookie);
227 	put_task_struct(task);
228 	return err;
229 }
230 
231