blob: 789d7bf4fef3203b3038a9ddaf20c5f70f1bc948 [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>
12#include <linux/security.h>
13#include <linux/types.h>
14#include <asm/uaccess.h>
15
16/*
17 * CPU mask used to set process affinity for MT VPEs/TCs with FPUs
18 */
19cpumask_t mt_fpu_cpumask;
20
21static int fpaff_threshold = -1;
Ralf Baechle982f6ff2009-09-17 02:25:07 +020022unsigned long mt_fpemul_threshold;
Ralf Baechle295cbf62007-07-03 14:37:43 +010023
24/*
25 * Replacement functions for the sys_sched_setaffinity() and
26 * sys_sched_getaffinity() system calls, so that we can integrate
27 * FPU affinity with the user's requested processor affinity.
28 * This code is 98% identical with the sys_sched_setaffinity()
29 * and sys_sched_getaffinity() system calls, and should be
Viresh Kumar0a0fca92013-06-04 13:10:24 +053030 * updated when kernel/sched/core.c changes.
Ralf Baechle295cbf62007-07-03 14:37:43 +010031 */
32
33/*
34 * find_process_by_pid - find a process with a matching PID value.
Viresh Kumar0a0fca92013-06-04 13:10:24 +053035 * used in sys_sched_set/getaffinity() in kernel/sched/core.c, so
Ralf Baechle295cbf62007-07-03 14:37:43 +010036 * cloned here.
37 */
38static inline struct task_struct *find_process_by_pid(pid_t pid)
39{
Pavel Emelyanov0e568532008-02-04 23:44:24 -080040 return pid ? find_task_by_vpid(pid) : current;
Ralf Baechle295cbf62007-07-03 14:37:43 +010041}
42
Ralf Baechle17c04132010-05-29 03:19:57 +010043/*
44 * check the target process has a UID that matches the current process's
45 */
46static bool check_same_owner(struct task_struct *p)
47{
48 const struct cred *cred = current_cred(), *pcred;
49 bool match;
50
51 rcu_read_lock();
52 pcred = __task_cred(p);
Florian Fainellib88fb182012-12-10 15:56:44 +010053 match = (uid_eq(cred->euid, pcred->euid) ||
54 uid_eq(cred->euid, pcred->uid));
Ralf Baechle17c04132010-05-29 03:19:57 +010055 rcu_read_unlock();
56 return match;
57}
Ralf Baechle295cbf62007-07-03 14:37:43 +010058
59/*
60 * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process
61 */
62asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
63 unsigned long __user *user_mask_ptr)
64{
Ralf Baechle17c04132010-05-29 03:19:57 +010065 cpumask_var_t cpus_allowed, new_mask, effective_mask;
Ralf Baechle293c5bd2007-07-25 16:19:33 +010066 struct thread_info *ti;
Ralf Baechle17c04132010-05-29 03:19:57 +010067 struct task_struct *p;
68 int retval;
Ralf Baechle295cbf62007-07-03 14:37:43 +010069
70 if (len < sizeof(new_mask))
71 return -EINVAL;
72
73 if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
74 return -EFAULT;
75
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010076 get_online_cpus();
Ralf Baechle17c04132010-05-29 03:19:57 +010077 rcu_read_lock();
Ralf Baechle295cbf62007-07-03 14:37:43 +010078
79 p = find_process_by_pid(pid);
80 if (!p) {
Ralf Baechle17c04132010-05-29 03:19:57 +010081 rcu_read_unlock();
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010082 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +010083 return -ESRCH;
84 }
85
Ralf Baechle17c04132010-05-29 03:19:57 +010086 /* Prevent p going away */
Ralf Baechle295cbf62007-07-03 14:37:43 +010087 get_task_struct(p);
Ralf Baechle17c04132010-05-29 03:19:57 +010088 rcu_read_unlock();
Ralf Baechle295cbf62007-07-03 14:37:43 +010089
Ralf Baechle17c04132010-05-29 03:19:57 +010090 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
91 retval = -ENOMEM;
92 goto out_put_task;
Ralf Baechle295cbf62007-07-03 14:37:43 +010093 }
Ralf Baechle17c04132010-05-29 03:19:57 +010094 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
95 retval = -ENOMEM;
96 goto out_free_cpus_allowed;
97 }
98 if (!alloc_cpumask_var(&effective_mask, GFP_KERNEL)) {
99 retval = -ENOMEM;
100 goto out_free_new_mask;
101 }
102 retval = -EPERM;
103 if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
104 goto out_unlock;
Ralf Baechle295cbf62007-07-03 14:37:43 +0100105
Ralf Baechlea7f505c2010-10-24 22:23:50 +0100106 retval = security_task_setscheduler(p);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100107 if (retval)
108 goto out_unlock;
109
110 /* Record new user-specified CPU set for future reference */
Ralf Baechle17c04132010-05-29 03:19:57 +0100111 cpumask_copy(&p->thread.user_cpus_allowed, new_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100112
Ralf Baechle17c04132010-05-29 03:19:57 +0100113 again:
Ralf Baechle295cbf62007-07-03 14:37:43 +0100114 /* Compute new global allowed CPU set if necessary */
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100115 ti = task_thread_info(p);
116 if (test_ti_thread_flag(ti, TIF_FPUBOUND) &&
Rusty Russell8dd92892015-03-05 10:49:17 +1030117 cpumask_intersects(new_mask, &mt_fpu_cpumask)) {
118 cpumask_and(effective_mask, new_mask, &mt_fpu_cpumask);
Ralf Baechle17c04132010-05-29 03:19:57 +0100119 retval = set_cpus_allowed_ptr(p, effective_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100120 } else {
Ralf Baechle17c04132010-05-29 03:19:57 +0100121 cpumask_copy(effective_mask, new_mask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100122 clear_ti_thread_flag(ti, TIF_FPUBOUND);
Ralf Baechle17c04132010-05-29 03:19:57 +0100123 retval = set_cpus_allowed_ptr(p, new_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100124 }
125
Ralf Baechle17c04132010-05-29 03:19:57 +0100126 if (!retval) {
127 cpuset_cpus_allowed(p, cpus_allowed);
128 if (!cpumask_subset(effective_mask, cpus_allowed)) {
129 /*
130 * We must have raced with a concurrent cpuset
131 * update. Just reset the cpus_allowed to the
132 * cpuset's cpus_allowed
133 */
134 cpumask_copy(new_mask, cpus_allowed);
135 goto again;
136 }
137 }
Ralf Baechle295cbf62007-07-03 14:37:43 +0100138out_unlock:
Ralf Baechle17c04132010-05-29 03:19:57 +0100139 free_cpumask_var(effective_mask);
140out_free_new_mask:
141 free_cpumask_var(new_mask);
142out_free_cpus_allowed:
143 free_cpumask_var(cpus_allowed);
144out_put_task:
Ralf Baechle295cbf62007-07-03 14:37:43 +0100145 put_task_struct(p);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100146 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100147 return retval;
148}
149
150/*
151 * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
152 */
153asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
154 unsigned long __user *user_mask_ptr)
155{
156 unsigned int real_len;
Felix Fietkau1d62d732015-07-19 00:38:41 +0200157 cpumask_t allowed, mask;
Ralf Baechle295cbf62007-07-03 14:37:43 +0100158 int retval;
159 struct task_struct *p;
160
161 real_len = sizeof(mask);
162 if (len < real_len)
163 return -EINVAL;
164
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100165 get_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100166 read_lock(&tasklist_lock);
167
168 retval = -ESRCH;
169 p = find_process_by_pid(pid);
170 if (!p)
171 goto out_unlock;
172 retval = security_task_getscheduler(p);
173 if (retval)
174 goto out_unlock;
175
Felix Fietkau1d62d732015-07-19 00:38:41 +0200176 cpumask_or(&allowed, &p->thread.user_cpus_allowed, &p->cpus_allowed);
177 cpumask_and(&mask, &allowed, cpu_active_mask);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100178
179out_unlock:
180 read_unlock(&tasklist_lock);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100181 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100182 if (retval)
183 return retval;
184 if (copy_to_user(user_mask_ptr, &mask, real_len))
185 return -EFAULT;
186 return real_len;
187}
188
189
190static int __init fpaff_thresh(char *str)
191{
192 get_option(&str, &fpaff_threshold);
193 return 1;
194}
195__setup("fpaff=", fpaff_thresh);
196
197/*
198 * FPU Use Factor empirically derived from experiments on 34K
199 */
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200200#define FPUSEFACTOR 2000
Ralf Baechle295cbf62007-07-03 14:37:43 +0100201
202static __init int mt_fp_affinity_init(void)
203{
204 if (fpaff_threshold >= 0) {
205 mt_fpemul_threshold = fpaff_threshold;
206 } else {
207 mt_fpemul_threshold =
208 (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
209 }
210 printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n",
211 mt_fpemul_threshold);
212
213 return 0;
214}
215arch_initcall(mt_fp_affinity_init);