blob: ffc6bd3464d970f60e3738bd7ff3bd023e546d24 [file] [log] [blame]
Ralf Baechle295cbf62007-07-03 14:37:43 +01001/*
Ralf Baechleb6336482014-05-23 16:29:44 +02002 * General MIPS MT support routines, usable in AP/SP and SMVP.
Ralf Baechle295cbf62007-07-03 14:37:43 +01003 * Copyright (C) 2005 Mips Technologies, Inc
4 */
5#include <linux/cpu.h>
Ralf Baechle17c04132010-05-29 03:19:57 +01006#include <linux/cpuset.h>
Ralf Baechle295cbf62007-07-03 14:37:43 +01007#include <linux/cpumask.h>
8#include <linux/delay.h>
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010012#include <linux/cred.h>
Ralf Baechle295cbf62007-07-03 14:37:43 +010013#include <linux/security.h>
14#include <linux/types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080015#include <linux/uaccess.h>
Ralf Baechle295cbf62007-07-03 14:37:43 +010016
17/*
18 * CPU mask used to set process affinity for MT VPEs/TCs with FPUs
19 */
20cpumask_t mt_fpu_cpumask;
21
22static int fpaff_threshold = -1;
Ralf Baechle982f6ff2009-09-17 02:25:07 +020023unsigned long mt_fpemul_threshold;
Ralf Baechle295cbf62007-07-03 14:37:43 +010024
25/*
26 * Replacement functions for the sys_sched_setaffinity() and
27 * sys_sched_getaffinity() system calls, so that we can integrate
28 * FPU affinity with the user's requested processor affinity.
29 * This code is 98% identical with the sys_sched_setaffinity()
30 * and sys_sched_getaffinity() system calls, and should be
Viresh Kumar0a0fca92013-06-04 13:10:24 +053031 * updated when kernel/sched/core.c changes.
Ralf Baechle295cbf62007-07-03 14:37:43 +010032 */
33
34/*
35 * find_process_by_pid - find a process with a matching PID value.
Viresh Kumar0a0fca92013-06-04 13:10:24 +053036 * used in sys_sched_set/getaffinity() in kernel/sched/core.c, so
Ralf Baechle295cbf62007-07-03 14:37:43 +010037 * cloned here.
38 */
39static inline struct task_struct *find_process_by_pid(pid_t pid)
40{
Pavel Emelyanov0e568532008-02-04 23:44:24 -080041 return pid ? find_task_by_vpid(pid) : current;
Ralf Baechle295cbf62007-07-03 14:37:43 +010042}
43
Ralf Baechle17c04132010-05-29 03:19:57 +010044/*
45 * check the target process has a UID that matches the current process's
46 */
47static bool check_same_owner(struct task_struct *p)
48{
49 const struct cred *cred = current_cred(), *pcred;
50 bool match;
51
52 rcu_read_lock();
53 pcred = __task_cred(p);
Florian Fainellib88fb182012-12-10 15:56:44 +010054 match = (uid_eq(cred->euid, pcred->euid) ||
55 uid_eq(cred->euid, pcred->uid));
Ralf Baechle17c04132010-05-29 03:19:57 +010056 rcu_read_unlock();
57 return match;
58}
Ralf Baechle295cbf62007-07-03 14:37:43 +010059
60/*
61 * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process
62 */
63asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
64 unsigned long __user *user_mask_ptr)
65{
Ralf Baechle17c04132010-05-29 03:19:57 +010066 cpumask_var_t cpus_allowed, new_mask, effective_mask;
Ralf Baechle293c5bd2007-07-25 16:19:33 +010067 struct thread_info *ti;
Ralf Baechle17c04132010-05-29 03:19:57 +010068 struct task_struct *p;
69 int retval;
Ralf Baechle295cbf62007-07-03 14:37:43 +010070
71 if (len < sizeof(new_mask))
72 return -EINVAL;
73
74 if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
75 return -EFAULT;
76
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010077 get_online_cpus();
Ralf Baechle17c04132010-05-29 03:19:57 +010078 rcu_read_lock();
Ralf Baechle295cbf62007-07-03 14:37:43 +010079
80 p = find_process_by_pid(pid);
81 if (!p) {
Ralf Baechle17c04132010-05-29 03:19:57 +010082 rcu_read_unlock();
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010083 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +010084 return -ESRCH;
85 }
86
Ralf Baechle17c04132010-05-29 03:19:57 +010087 /* Prevent p going away */
Ralf Baechle295cbf62007-07-03 14:37:43 +010088 get_task_struct(p);
Ralf Baechle17c04132010-05-29 03:19:57 +010089 rcu_read_unlock();
Ralf Baechle295cbf62007-07-03 14:37:43 +010090
Ralf Baechle17c04132010-05-29 03:19:57 +010091 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
92 retval = -ENOMEM;
93 goto out_put_task;
Ralf Baechle295cbf62007-07-03 14:37:43 +010094 }
Ralf Baechle17c04132010-05-29 03:19:57 +010095 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
96 retval = -ENOMEM;
97 goto out_free_cpus_allowed;
98 }
99 if (!alloc_cpumask_var(&effective_mask, GFP_KERNEL)) {
100 retval = -ENOMEM;
101 goto out_free_new_mask;
102 }
Markus Elfringa45526b2017-01-18 19:18:37 +0100103 if (!check_same_owner(p) && !capable(CAP_SYS_NICE)) {
104 retval = -EPERM;
Ralf Baechle17c04132010-05-29 03:19:57 +0100105 goto out_unlock;
Markus Elfringa45526b2017-01-18 19:18:37 +0100106 }
Ralf Baechle295cbf62007-07-03 14:37:43 +0100107
Ralf Baechlea7f505c2010-10-24 22:23:50 +0100108 retval = security_task_setscheduler(p);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100109 if (retval)
110 goto out_unlock;
111
112 /* Record new user-specified CPU set for future reference */
Ralf Baechle17c04132010-05-29 03:19:57 +0100113 cpumask_copy(&p->thread.user_cpus_allowed, new_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100114
Ralf Baechle17c04132010-05-29 03:19:57 +0100115 again:
Ralf Baechle295cbf62007-07-03 14:37:43 +0100116 /* Compute new global allowed CPU set if necessary */
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100117 ti = task_thread_info(p);
118 if (test_ti_thread_flag(ti, TIF_FPUBOUND) &&
Rusty Russell8dd92892015-03-05 10:49:17 +1030119 cpumask_intersects(new_mask, &mt_fpu_cpumask)) {
120 cpumask_and(effective_mask, new_mask, &mt_fpu_cpumask);
Ralf Baechle17c04132010-05-29 03:19:57 +0100121 retval = set_cpus_allowed_ptr(p, effective_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100122 } else {
Ralf Baechle17c04132010-05-29 03:19:57 +0100123 cpumask_copy(effective_mask, new_mask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100124 clear_ti_thread_flag(ti, TIF_FPUBOUND);
Ralf Baechle17c04132010-05-29 03:19:57 +0100125 retval = set_cpus_allowed_ptr(p, new_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100126 }
127
Ralf Baechle17c04132010-05-29 03:19:57 +0100128 if (!retval) {
129 cpuset_cpus_allowed(p, cpus_allowed);
130 if (!cpumask_subset(effective_mask, cpus_allowed)) {
131 /*
132 * We must have raced with a concurrent cpuset
133 * update. Just reset the cpus_allowed to the
134 * cpuset's cpus_allowed
135 */
136 cpumask_copy(new_mask, cpus_allowed);
137 goto again;
138 }
139 }
Ralf Baechle295cbf62007-07-03 14:37:43 +0100140out_unlock:
Ralf Baechle17c04132010-05-29 03:19:57 +0100141 free_cpumask_var(effective_mask);
142out_free_new_mask:
143 free_cpumask_var(new_mask);
144out_free_cpus_allowed:
145 free_cpumask_var(cpus_allowed);
146out_put_task:
Ralf Baechle295cbf62007-07-03 14:37:43 +0100147 put_task_struct(p);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100148 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100149 return retval;
150}
151
152/*
153 * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
154 */
155asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
156 unsigned long __user *user_mask_ptr)
157{
158 unsigned int real_len;
Felix Fietkau1d62d732015-07-19 00:38:41 +0200159 cpumask_t allowed, mask;
Ralf Baechle295cbf62007-07-03 14:37:43 +0100160 int retval;
161 struct task_struct *p;
162
163 real_len = sizeof(mask);
164 if (len < real_len)
165 return -EINVAL;
166
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100167 get_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100168 read_lock(&tasklist_lock);
169
170 retval = -ESRCH;
171 p = find_process_by_pid(pid);
172 if (!p)
173 goto out_unlock;
174 retval = security_task_getscheduler(p);
175 if (retval)
176 goto out_unlock;
177
Felix Fietkau1d62d732015-07-19 00:38:41 +0200178 cpumask_or(&allowed, &p->thread.user_cpus_allowed, &p->cpus_allowed);
179 cpumask_and(&mask, &allowed, cpu_active_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100180
181out_unlock:
182 read_unlock(&tasklist_lock);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100183 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100184 if (retval)
185 return retval;
186 if (copy_to_user(user_mask_ptr, &mask, real_len))
187 return -EFAULT;
188 return real_len;
189}
190
191
192static int __init fpaff_thresh(char *str)
193{
194 get_option(&str, &fpaff_threshold);
195 return 1;
196}
197__setup("fpaff=", fpaff_thresh);
198
199/*
200 * FPU Use Factor empirically derived from experiments on 34K
201 */
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200202#define FPUSEFACTOR 2000
Ralf Baechle295cbf62007-07-03 14:37:43 +0100203
204static __init int mt_fp_affinity_init(void)
205{
206 if (fpaff_threshold >= 0) {
207 mt_fpemul_threshold = fpaff_threshold;
208 } else {
209 mt_fpemul_threshold =
210 (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
211 }
212 printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n",
213 mt_fpemul_threshold);
214
215 return 0;
216}
217arch_initcall(mt_fp_affinity_init);