Commit | Line | Data |
---|---|---|
295cbf6d | 1 | /* |
b633648c | 2 | * General MIPS MT support routines, usable in AP/SP and SMVP. |
295cbf6d RB |
3 | * Copyright (C) 2005 Mips Technologies, Inc |
4 | */ | |
5 | #include <linux/cpu.h> | |
17c04139 | 6 | #include <linux/cpuset.h> |
295cbf6d RB |
7 | #include <linux/cpumask.h> |
8 | #include <linux/delay.h> | |
9 | #include <linux/kernel.h> | |
10 | #include <linux/init.h> | |
11 | #include <linux/sched.h> | |
5b825c3a | 12 | #include <linux/cred.h> |
295cbf6d RB |
13 | #include <linux/security.h> |
14 | #include <linux/types.h> | |
7c0f6ba6 | 15 | #include <linux/uaccess.h> |
295cbf6d RB |
16 | |
17 | /* | |
18 | * CPU mask used to set process affinity for MT VPEs/TCs with FPUs | |
19 | */ | |
20 | cpumask_t mt_fpu_cpumask; | |
21 | ||
22 | static int fpaff_threshold = -1; | |
982f6ffe | 23 | unsigned long mt_fpemul_threshold; |
295cbf6d RB |
24 | |
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 | |
0a0fca9d | 31 | * updated when kernel/sched/core.c changes. |
295cbf6d RB |
32 | */ |
33 | ||
34 | /* | |
35 | * find_process_by_pid - find a process with a matching PID value. | |
0a0fca9d | 36 | * used in sys_sched_set/getaffinity() in kernel/sched/core.c, so |
295cbf6d RB |
37 | * cloned here. |
38 | */ | |
39 | static inline struct task_struct *find_process_by_pid(pid_t pid) | |
40 | { | |
0e568536 | 41 | return pid ? find_task_by_vpid(pid) : current; |
295cbf6d RB |
42 | } |
43 | ||
17c04139 RB |
44 | /* |
45 | * check the target process has a UID that matches the current process's | |
46 | */ | |
47 | static 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); | |
b88fb18e FF |
54 | match = (uid_eq(cred->euid, pcred->euid) || |
55 | uid_eq(cred->euid, pcred->uid)); | |
17c04139 RB |
56 | rcu_read_unlock(); |
57 | return match; | |
58 | } | |
295cbf6d RB |
59 | |
60 | /* | |
61 | * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process | |
62 | */ | |
63 | asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len, | |
64 | unsigned long __user *user_mask_ptr) | |
65 | { | |
17c04139 | 66 | cpumask_var_t cpus_allowed, new_mask, effective_mask; |
293c5bd1 | 67 | struct thread_info *ti; |
17c04139 RB |
68 | struct task_struct *p; |
69 | int retval; | |
295cbf6d RB |
70 | |
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 | ||
86ef5c9a | 77 | get_online_cpus(); |
17c04139 | 78 | rcu_read_lock(); |
295cbf6d RB |
79 | |
80 | p = find_process_by_pid(pid); | |
81 | if (!p) { | |
17c04139 | 82 | rcu_read_unlock(); |
86ef5c9a | 83 | put_online_cpus(); |
295cbf6d RB |
84 | return -ESRCH; |
85 | } | |
86 | ||
17c04139 | 87 | /* Prevent p going away */ |
295cbf6d | 88 | get_task_struct(p); |
17c04139 | 89 | rcu_read_unlock(); |
295cbf6d | 90 | |
17c04139 RB |
91 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { |
92 | retval = -ENOMEM; | |
93 | goto out_put_task; | |
94 | } | |
95 | 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 | } | |
a45526bb ME |
103 | if (!check_same_owner(p) && !capable(CAP_SYS_NICE)) { |
104 | retval = -EPERM; | |
295cbf6d | 105 | goto out_unlock; |
a45526bb | 106 | } |
295cbf6d | 107 | |
a7f505c6 | 108 | retval = security_task_setscheduler(p); |
295cbf6d RB |
109 | if (retval) |
110 | goto out_unlock; | |
111 | ||
112 | /* Record new user-specified CPU set for future reference */ | |
17c04139 | 113 | cpumask_copy(&p->thread.user_cpus_allowed, new_mask); |
295cbf6d | 114 | |
17c04139 | 115 | again: |
295cbf6d | 116 | /* Compute new global allowed CPU set if necessary */ |
293c5bd1 RB |
117 | ti = task_thread_info(p); |
118 | if (test_ti_thread_flag(ti, TIF_FPUBOUND) && | |
8dd92891 RR |
119 | cpumask_intersects(new_mask, &mt_fpu_cpumask)) { |
120 | cpumask_and(effective_mask, new_mask, &mt_fpu_cpumask); | |
17c04139 | 121 | retval = set_cpus_allowed_ptr(p, effective_mask); |
295cbf6d | 122 | } else { |
17c04139 | 123 | cpumask_copy(effective_mask, new_mask); |
293c5bd1 | 124 | clear_ti_thread_flag(ti, TIF_FPUBOUND); |
17c04139 | 125 | retval = set_cpus_allowed_ptr(p, new_mask); |
295cbf6d RB |
126 | } |
127 | ||
17c04139 RB |
128 | 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 | } | |
295cbf6d | 140 | out_unlock: |
17c04139 RB |
141 | free_cpumask_var(effective_mask); |
142 | out_free_new_mask: | |
143 | free_cpumask_var(new_mask); | |
144 | out_free_cpus_allowed: | |
145 | free_cpumask_var(cpus_allowed); | |
146 | out_put_task: | |
295cbf6d | 147 | put_task_struct(p); |
86ef5c9a | 148 | put_online_cpus(); |
295cbf6d RB |
149 | return retval; |
150 | } | |
151 | ||
152 | /* | |
153 | * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process | |
154 | */ | |
155 | asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len, | |
156 | unsigned long __user *user_mask_ptr) | |
157 | { | |
158 | unsigned int real_len; | |
1d62d737 | 159 | cpumask_t allowed, mask; |
295cbf6d RB |
160 | int retval; |
161 | struct task_struct *p; | |
162 | ||
163 | real_len = sizeof(mask); | |
164 | if (len < real_len) | |
165 | return -EINVAL; | |
166 | ||
86ef5c9a | 167 | get_online_cpus(); |
295cbf6d RB |
168 | 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 | ||
1d62d737 FF |
178 | cpumask_or(&allowed, &p->thread.user_cpus_allowed, &p->cpus_allowed); |
179 | cpumask_and(&mask, &allowed, cpu_active_mask); | |
295cbf6d RB |
180 | |
181 | out_unlock: | |
182 | read_unlock(&tasklist_lock); | |
86ef5c9a | 183 | put_online_cpus(); |
295cbf6d RB |
184 | 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 | ||
192 | static 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 | */ | |
9cc12363 | 202 | #define FPUSEFACTOR 2000 |
295cbf6d RB |
203 | |
204 | static __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 | } | |
217 | arch_initcall(mt_fp_affinity_init); |