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 static 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 static 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 static 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 static unsigned long sched_core_update_cookie(struct task_struct *p,
59 unsigned long old_cookie;
64 rq = task_rq_lock(p, &rf);
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
72 SCHED_WARN_ON((p->core_cookie || cookie) && !sched_core_enabled(rq));
74 enqueued = sched_core_enqueued(p);
76 sched_core_dequeue(rq, p, DEQUEUE_SAVE);
78 old_cookie = p->core_cookie;
79 p->core_cookie = cookie;
82 sched_core_enqueue(rq, p);
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
89 * Note that it is possible that as a result of this cookie change, the
90 * core has now entered/left forced idle state. Defer accounting to the
91 * next scheduling edge, rather than always forcing a reschedule here.
93 if (task_running(rq, p))
96 task_rq_unlock(rq, p, &rf);
101 static unsigned long sched_core_clone_cookie(struct task_struct *p)
103 unsigned long cookie, flags;
105 raw_spin_lock_irqsave(&p->pi_lock, flags);
106 cookie = sched_core_get_cookie(p->core_cookie);
107 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
112 void sched_core_fork(struct task_struct *p)
114 RB_CLEAR_NODE(&p->core_node);
115 p->core_cookie = sched_core_clone_cookie(current);
118 void sched_core_free(struct task_struct *p)
120 sched_core_put_cookie(p->core_cookie);
123 static void __sched_core_set(struct task_struct *p, unsigned long cookie)
125 cookie = sched_core_get_cookie(cookie);
126 cookie = sched_core_update_cookie(p, cookie);
127 sched_core_put_cookie(cookie);
130 /* Called from prctl interface: PR_SCHED_CORE */
131 int sched_core_share_pid(unsigned int cmd, pid_t pid, enum pid_type type,
134 unsigned long cookie = 0, id = 0;
135 struct task_struct *task, *p;
139 if (!static_branch_likely(&sched_smt_present))
142 BUILD_BUG_ON(PR_SCHED_CORE_SCOPE_THREAD != PIDTYPE_PID);
143 BUILD_BUG_ON(PR_SCHED_CORE_SCOPE_THREAD_GROUP != PIDTYPE_TGID);
144 BUILD_BUG_ON(PR_SCHED_CORE_SCOPE_PROCESS_GROUP != PIDTYPE_PGID);
146 if (type > PIDTYPE_PGID || cmd >= PR_SCHED_CORE_MAX || pid < 0 ||
147 (cmd != PR_SCHED_CORE_GET && uaddr))
154 task = find_task_by_vpid(pid);
160 get_task_struct(task);
164 * Check if this process has the right to modify the specified
165 * process. Use the regular "ptrace_may_access()" checks.
167 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
173 case PR_SCHED_CORE_GET:
174 if (type != PIDTYPE_PID || uaddr & 7) {
178 cookie = sched_core_clone_cookie(task);
181 ptr_to_hashval((void *)cookie, &id);
183 err = put_user(id, (u64 __user *)uaddr);
186 case PR_SCHED_CORE_CREATE:
187 cookie = sched_core_alloc_cookie();
194 case PR_SCHED_CORE_SHARE_TO:
195 cookie = sched_core_clone_cookie(current);
198 case PR_SCHED_CORE_SHARE_FROM:
199 if (type != PIDTYPE_PID) {
203 cookie = sched_core_clone_cookie(task);
204 __sched_core_set(current, cookie);
212 if (type == PIDTYPE_PID) {
213 __sched_core_set(task, cookie);
217 read_lock(&tasklist_lock);
218 grp = task_pid_type(task, type);
220 do_each_pid_thread(grp, type, p) {
221 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS)) {
225 } while_each_pid_thread(grp, type, p);
227 do_each_pid_thread(grp, type, p) {
228 __sched_core_set(p, cookie);
229 } while_each_pid_thread(grp, type, p);
231 read_unlock(&tasklist_lock);
234 sched_core_put_cookie(cookie);
235 put_task_struct(task);
239 #ifdef CONFIG_SCHEDSTATS
241 /* REQUIRES: rq->core's clock recently updated. */
242 void __sched_core_account_forceidle(struct rq *rq)
244 const struct cpumask *smt_mask = cpu_smt_mask(cpu_of(rq));
245 u64 delta, now = rq_clock(rq->core);
247 struct task_struct *p;
250 lockdep_assert_rq_held(rq);
252 WARN_ON_ONCE(!rq->core->core_forceidle_count);
254 if (rq->core->core_forceidle_start == 0)
257 delta = now - rq->core->core_forceidle_start;
258 if (unlikely((s64)delta <= 0))
261 rq->core->core_forceidle_start = now;
263 if (WARN_ON_ONCE(!rq->core->core_forceidle_occupation)) {
264 /* can't be forced idle without a running task */
265 } else if (rq->core->core_forceidle_count > 1 ||
266 rq->core->core_forceidle_occupation > 1) {
268 * For larger SMT configurations, we need to scale the charged
269 * forced idle amount since there can be more than one forced
270 * idle sibling and more than one running cookied task.
272 delta *= rq->core->core_forceidle_count;
273 delta = div_u64(delta, rq->core->core_forceidle_occupation);
276 for_each_cpu(i, smt_mask) {
278 p = rq_i->core_pick ?: rq_i->curr;
283 __schedstat_add(p->stats.core_forceidle_sum, delta);
287 void __sched_core_tick(struct rq *rq)
289 if (!rq->core->core_forceidle_count)
293 update_rq_clock(rq->core);
295 __sched_core_account_forceidle(rq);
298 #endif /* CONFIG_SCHEDSTATS */