blob: a7c0f97e4b0d6eea56db2f9eedad24a9158d669b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ralf Baechle295cbf62007-07-03 14:37:43 +01002/*
Ralf Baechleb6336482014-05-23 16:29:44 +02003 * General MIPS MT support routines, usable in AP/SP and SMVP.
Ralf Baechle295cbf62007-07-03 14:37:43 +01004 * Copyright (C) 2005 Mips Technologies, Inc
5 */
6#include <linux/cpu.h>
Ralf Baechle17c04132010-05-29 03:19:57 +01007#include <linux/cpuset.h>
Ralf Baechle295cbf62007-07-03 14:37:43 +01008#include <linux/cpumask.h>
9#include <linux/delay.h>
10#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/sched.h>
Ingo Molnar29930022017-02-08 18:51:36 +010013#include <linux/sched/task.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010014#include <linux/cred.h>
Ralf Baechle295cbf62007-07-03 14:37:43 +010015#include <linux/security.h>
16#include <linux/types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080017#include <linux/uaccess.h>
Ralf Baechle295cbf62007-07-03 14:37:43 +010018
19/*
20 * CPU mask used to set process affinity for MT VPEs/TCs with FPUs
21 */
22cpumask_t mt_fpu_cpumask;
23
24static int fpaff_threshold = -1;
Ralf Baechle982f6ff2009-09-17 02:25:07 +020025unsigned long mt_fpemul_threshold;
Ralf Baechle295cbf62007-07-03 14:37:43 +010026
27/*
28 * Replacement functions for the sys_sched_setaffinity() and
29 * sys_sched_getaffinity() system calls, so that we can integrate
30 * FPU affinity with the user's requested processor affinity.
31 * This code is 98% identical with the sys_sched_setaffinity()
32 * and sys_sched_getaffinity() system calls, and should be
Viresh Kumar0a0fca92013-06-04 13:10:24 +053033 * updated when kernel/sched/core.c changes.
Ralf Baechle295cbf62007-07-03 14:37:43 +010034 */
35
36/*
37 * find_process_by_pid - find a process with a matching PID value.
Viresh Kumar0a0fca92013-06-04 13:10:24 +053038 * used in sys_sched_set/getaffinity() in kernel/sched/core.c, so
Ralf Baechle295cbf62007-07-03 14:37:43 +010039 * cloned here.
40 */
41static inline struct task_struct *find_process_by_pid(pid_t pid)
42{
Pavel Emelyanov0e568532008-02-04 23:44:24 -080043 return pid ? find_task_by_vpid(pid) : current;
Ralf Baechle295cbf62007-07-03 14:37:43 +010044}
45
Ralf Baechle17c04132010-05-29 03:19:57 +010046/*
47 * check the target process has a UID that matches the current process's
48 */
49static bool check_same_owner(struct task_struct *p)
50{
51 const struct cred *cred = current_cred(), *pcred;
52 bool match;
53
54 rcu_read_lock();
55 pcred = __task_cred(p);
Florian Fainellib88fb182012-12-10 15:56:44 +010056 match = (uid_eq(cred->euid, pcred->euid) ||
57 uid_eq(cred->euid, pcred->uid));
Ralf Baechle17c04132010-05-29 03:19:57 +010058 rcu_read_unlock();
59 return match;
60}
Ralf Baechle295cbf62007-07-03 14:37:43 +010061
62/*
63 * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process
64 */
65asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
66 unsigned long __user *user_mask_ptr)
67{
Ralf Baechle17c04132010-05-29 03:19:57 +010068 cpumask_var_t cpus_allowed, new_mask, effective_mask;
Ralf Baechle293c5bd2007-07-25 16:19:33 +010069 struct thread_info *ti;
Ralf Baechle17c04132010-05-29 03:19:57 +010070 struct task_struct *p;
71 int retval;
Ralf Baechle295cbf62007-07-03 14:37:43 +010072
73 if (len < sizeof(new_mask))
74 return -EINVAL;
75
76 if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
77 return -EFAULT;
78
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010079 get_online_cpus();
Ralf Baechle17c04132010-05-29 03:19:57 +010080 rcu_read_lock();
Ralf Baechle295cbf62007-07-03 14:37:43 +010081
82 p = find_process_by_pid(pid);
83 if (!p) {
Ralf Baechle17c04132010-05-29 03:19:57 +010084 rcu_read_unlock();
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010085 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +010086 return -ESRCH;
87 }
88
Ralf Baechle17c04132010-05-29 03:19:57 +010089 /* Prevent p going away */
Ralf Baechle295cbf62007-07-03 14:37:43 +010090 get_task_struct(p);
Ralf Baechle17c04132010-05-29 03:19:57 +010091 rcu_read_unlock();
Ralf Baechle295cbf62007-07-03 14:37:43 +010092
Ralf Baechle17c04132010-05-29 03:19:57 +010093 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
94 retval = -ENOMEM;
95 goto out_put_task;
Ralf Baechle295cbf62007-07-03 14:37:43 +010096 }
Ralf Baechle17c04132010-05-29 03:19:57 +010097 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
98 retval = -ENOMEM;
99 goto out_free_cpus_allowed;
100 }
101 if (!alloc_cpumask_var(&effective_mask, GFP_KERNEL)) {
102 retval = -ENOMEM;
103 goto out_free_new_mask;
104 }
Markus Elfringa45526b2017-01-18 19:18:37 +0100105 if (!check_same_owner(p) && !capable(CAP_SYS_NICE)) {
106 retval = -EPERM;
Ralf Baechle17c04132010-05-29 03:19:57 +0100107 goto out_unlock;
Markus Elfringa45526b2017-01-18 19:18:37 +0100108 }
Ralf Baechle295cbf62007-07-03 14:37:43 +0100109
Ralf Baechlea7f505c2010-10-24 22:23:50 +0100110 retval = security_task_setscheduler(p);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100111 if (retval)
112 goto out_unlock;
113
114 /* Record new user-specified CPU set for future reference */
Ralf Baechle17c04132010-05-29 03:19:57 +0100115 cpumask_copy(&p->thread.user_cpus_allowed, new_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100116
Ralf Baechle17c04132010-05-29 03:19:57 +0100117 again:
Ralf Baechle295cbf62007-07-03 14:37:43 +0100118 /* Compute new global allowed CPU set if necessary */
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100119 ti = task_thread_info(p);
120 if (test_ti_thread_flag(ti, TIF_FPUBOUND) &&
Rusty Russell8dd92892015-03-05 10:49:17 +1030121 cpumask_intersects(new_mask, &mt_fpu_cpumask)) {
122 cpumask_and(effective_mask, new_mask, &mt_fpu_cpumask);
Ralf Baechle17c04132010-05-29 03:19:57 +0100123 retval = set_cpus_allowed_ptr(p, effective_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100124 } else {
Ralf Baechle17c04132010-05-29 03:19:57 +0100125 cpumask_copy(effective_mask, new_mask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100126 clear_ti_thread_flag(ti, TIF_FPUBOUND);
Ralf Baechle17c04132010-05-29 03:19:57 +0100127 retval = set_cpus_allowed_ptr(p, new_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100128 }
129
Ralf Baechle17c04132010-05-29 03:19:57 +0100130 if (!retval) {
131 cpuset_cpus_allowed(p, cpus_allowed);
132 if (!cpumask_subset(effective_mask, cpus_allowed)) {
133 /*
134 * We must have raced with a concurrent cpuset
135 * update. Just reset the cpus_allowed to the
136 * cpuset's cpus_allowed
137 */
138 cpumask_copy(new_mask, cpus_allowed);
139 goto again;
140 }
141 }
Ralf Baechle295cbf62007-07-03 14:37:43 +0100142out_unlock:
Ralf Baechle17c04132010-05-29 03:19:57 +0100143 free_cpumask_var(effective_mask);
144out_free_new_mask:
145 free_cpumask_var(new_mask);
146out_free_cpus_allowed:
147 free_cpumask_var(cpus_allowed);
148out_put_task:
Ralf Baechle295cbf62007-07-03 14:37:43 +0100149 put_task_struct(p);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100150 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100151 return retval;
152}
153
154/*
155 * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
156 */
157asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
158 unsigned long __user *user_mask_ptr)
159{
160 unsigned int real_len;
Felix Fietkau1d62d732015-07-19 00:38:41 +0200161 cpumask_t allowed, mask;
Ralf Baechle295cbf62007-07-03 14:37:43 +0100162 int retval;
163 struct task_struct *p;
164
165 real_len = sizeof(mask);
166 if (len < real_len)
167 return -EINVAL;
168
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100169 get_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100170 read_lock(&tasklist_lock);
171
172 retval = -ESRCH;
173 p = find_process_by_pid(pid);
174 if (!p)
175 goto out_unlock;
176 retval = security_task_getscheduler(p);
177 if (retval)
178 goto out_unlock;
179
Felix Fietkau1d62d732015-07-19 00:38:41 +0200180 cpumask_or(&allowed, &p->thread.user_cpus_allowed, &p->cpus_allowed);
181 cpumask_and(&mask, &allowed, cpu_active_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100182
183out_unlock:
184 read_unlock(&tasklist_lock);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100185 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100186 if (retval)
187 return retval;
188 if (copy_to_user(user_mask_ptr, &mask, real_len))
189 return -EFAULT;
190 return real_len;
191}
192
193
194static int __init fpaff_thresh(char *str)
195{
196 get_option(&str, &fpaff_threshold);
197 return 1;
198}
199__setup("fpaff=", fpaff_thresh);
200
201/*
202 * FPU Use Factor empirically derived from experiments on 34K
203 */
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200204#define FPUSEFACTOR 2000
Ralf Baechle295cbf62007-07-03 14:37:43 +0100205
206static __init int mt_fp_affinity_init(void)
207{
208 if (fpaff_threshold >= 0) {
209 mt_fpemul_threshold = fpaff_threshold;
210 } else {
211 mt_fpemul_threshold =
212 (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
213 }
214 printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n",
215 mt_fpemul_threshold);
216
217 return 0;
218}
219arch_initcall(mt_fp_affinity_init);