]>
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> | |
12 | #include <linux/security.h> | |
13 | #include <linux/types.h> | |
7c0f6ba6 | 14 | #include <linux/uaccess.h> |
295cbf6d RB |
15 | |
16 | /* | |
17 | * CPU mask used to set process affinity for MT VPEs/TCs with FPUs | |
18 | */ | |
19 | cpumask_t mt_fpu_cpumask; | |
20 | ||
21 | static int fpaff_threshold = -1; | |
982f6ffe | 22 | unsigned long mt_fpemul_threshold; |
295cbf6d RB |
23 | |
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 | |
0a0fca9d | 30 | * updated when kernel/sched/core.c changes. |
295cbf6d RB |
31 | */ |
32 | ||
33 | /* | |
34 | * find_process_by_pid - find a process with a matching PID value. | |
0a0fca9d | 35 | * used in sys_sched_set/getaffinity() in kernel/sched/core.c, so |
295cbf6d RB |
36 | * cloned here. |
37 | */ | |
38 | static inline struct task_struct *find_process_by_pid(pid_t pid) | |
39 | { | |
0e568536 | 40 | return pid ? find_task_by_vpid(pid) : current; |
295cbf6d RB |
41 | } |
42 | ||
17c04139 RB |
43 | /* |
44 | * check the target process has a UID that matches the current process's | |
45 | */ | |
46 | static 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); | |
b88fb18e FF |
53 | match = (uid_eq(cred->euid, pcred->euid) || |
54 | uid_eq(cred->euid, pcred->uid)); | |
17c04139 RB |
55 | rcu_read_unlock(); |
56 | return match; | |
57 | } | |
295cbf6d RB |
58 | |
59 | /* | |
60 | * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process | |
61 | */ | |
62 | asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len, | |
63 | unsigned long __user *user_mask_ptr) | |
64 | { | |
17c04139 | 65 | cpumask_var_t cpus_allowed, new_mask, effective_mask; |
293c5bd1 | 66 | struct thread_info *ti; |
17c04139 RB |
67 | struct task_struct *p; |
68 | int retval; | |
295cbf6d RB |
69 | |
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 | ||
86ef5c9a | 76 | get_online_cpus(); |
17c04139 | 77 | rcu_read_lock(); |
295cbf6d RB |
78 | |
79 | p = find_process_by_pid(pid); | |
80 | if (!p) { | |
17c04139 | 81 | rcu_read_unlock(); |
86ef5c9a | 82 | put_online_cpus(); |
295cbf6d RB |
83 | return -ESRCH; |
84 | } | |
85 | ||
17c04139 | 86 | /* Prevent p going away */ |
295cbf6d | 87 | get_task_struct(p); |
17c04139 | 88 | rcu_read_unlock(); |
295cbf6d | 89 | |
17c04139 RB |
90 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { |
91 | retval = -ENOMEM; | |
92 | goto out_put_task; | |
93 | } | |
94 | 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 | } | |
a45526bb ME |
102 | if (!check_same_owner(p) && !capable(CAP_SYS_NICE)) { |
103 | retval = -EPERM; | |
295cbf6d | 104 | goto out_unlock; |
a45526bb | 105 | } |
295cbf6d | 106 | |
a7f505c6 | 107 | retval = security_task_setscheduler(p); |
295cbf6d RB |
108 | if (retval) |
109 | goto out_unlock; | |
110 | ||
111 | /* Record new user-specified CPU set for future reference */ | |
17c04139 | 112 | cpumask_copy(&p->thread.user_cpus_allowed, new_mask); |
295cbf6d | 113 | |
17c04139 | 114 | again: |
295cbf6d | 115 | /* Compute new global allowed CPU set if necessary */ |
293c5bd1 RB |
116 | ti = task_thread_info(p); |
117 | if (test_ti_thread_flag(ti, TIF_FPUBOUND) && | |
8dd92891 RR |
118 | cpumask_intersects(new_mask, &mt_fpu_cpumask)) { |
119 | cpumask_and(effective_mask, new_mask, &mt_fpu_cpumask); | |
17c04139 | 120 | retval = set_cpus_allowed_ptr(p, effective_mask); |
295cbf6d | 121 | } else { |
17c04139 | 122 | cpumask_copy(effective_mask, new_mask); |
293c5bd1 | 123 | clear_ti_thread_flag(ti, TIF_FPUBOUND); |
17c04139 | 124 | retval = set_cpus_allowed_ptr(p, new_mask); |
295cbf6d RB |
125 | } |
126 | ||
17c04139 RB |
127 | if (!retval) { |
128 | cpuset_cpus_allowed(p, cpus_allowed); | |
129 | if (!cpumask_subset(effective_mask, cpus_allowed)) { | |
130 | /* | |
131 | * We must have raced with a concurrent cpuset | |
132 | * update. Just reset the cpus_allowed to the | |
133 | * cpuset's cpus_allowed | |
134 | */ | |
135 | cpumask_copy(new_mask, cpus_allowed); | |
136 | goto again; | |
137 | } | |
138 | } | |
295cbf6d | 139 | out_unlock: |
17c04139 RB |
140 | free_cpumask_var(effective_mask); |
141 | out_free_new_mask: | |
142 | free_cpumask_var(new_mask); | |
143 | out_free_cpus_allowed: | |
144 | free_cpumask_var(cpus_allowed); | |
145 | out_put_task: | |
295cbf6d | 146 | put_task_struct(p); |
86ef5c9a | 147 | put_online_cpus(); |
295cbf6d RB |
148 | return retval; |
149 | } | |
150 | ||
151 | /* | |
152 | * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process | |
153 | */ | |
154 | asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len, | |
155 | unsigned long __user *user_mask_ptr) | |
156 | { | |
157 | unsigned int real_len; | |
1d62d737 | 158 | cpumask_t allowed, mask; |
295cbf6d RB |
159 | int retval; |
160 | struct task_struct *p; | |
161 | ||
162 | real_len = sizeof(mask); | |
163 | if (len < real_len) | |
164 | return -EINVAL; | |
165 | ||
86ef5c9a | 166 | get_online_cpus(); |
295cbf6d RB |
167 | read_lock(&tasklist_lock); |
168 | ||
169 | retval = -ESRCH; | |
170 | p = find_process_by_pid(pid); | |
171 | if (!p) | |
172 | goto out_unlock; | |
173 | retval = security_task_getscheduler(p); | |
174 | if (retval) | |
175 | goto out_unlock; | |
176 | ||
1d62d737 FF |
177 | cpumask_or(&allowed, &p->thread.user_cpus_allowed, &p->cpus_allowed); |
178 | cpumask_and(&mask, &allowed, cpu_active_mask); | |
295cbf6d RB |
179 | |
180 | out_unlock: | |
181 | read_unlock(&tasklist_lock); | |
86ef5c9a | 182 | put_online_cpus(); |
295cbf6d RB |
183 | if (retval) |
184 | return retval; | |
185 | if (copy_to_user(user_mask_ptr, &mask, real_len)) | |
186 | return -EFAULT; | |
187 | return real_len; | |
188 | } | |
189 | ||
190 | ||
191 | static int __init fpaff_thresh(char *str) | |
192 | { | |
193 | get_option(&str, &fpaff_threshold); | |
194 | return 1; | |
195 | } | |
196 | __setup("fpaff=", fpaff_thresh); | |
197 | ||
198 | /* | |
199 | * FPU Use Factor empirically derived from experiments on 34K | |
200 | */ | |
9cc12363 | 201 | #define FPUSEFACTOR 2000 |
295cbf6d RB |
202 | |
203 | static __init int mt_fp_affinity_init(void) | |
204 | { | |
205 | if (fpaff_threshold >= 0) { | |
206 | mt_fpemul_threshold = fpaff_threshold; | |
207 | } else { | |
208 | mt_fpemul_threshold = | |
209 | (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ; | |
210 | } | |
211 | printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n", | |
212 | mt_fpemul_threshold); | |
213 | ||
214 | return 0; | |
215 | } | |
216 | arch_initcall(mt_fp_affinity_init); |