Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
295cbf6d | 2 | /* |
b633648c | 3 | * General MIPS MT support routines, usable in AP/SP and SMVP. |
295cbf6d RB |
4 | * Copyright (C) 2005 Mips Technologies, Inc |
5 | */ | |
6 | #include <linux/cpu.h> | |
17c04139 | 7 | #include <linux/cpuset.h> |
295cbf6d RB |
8 | #include <linux/cpumask.h> |
9 | #include <linux/delay.h> | |
10 | #include <linux/kernel.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/sched.h> | |
29930025 | 13 | #include <linux/sched/task.h> |
5b825c3a | 14 | #include <linux/cred.h> |
295cbf6d RB |
15 | #include <linux/security.h> |
16 | #include <linux/types.h> | |
7c0f6ba6 | 17 | #include <linux/uaccess.h> |
be018aaa | 18 | #include <asm/syscalls.h> |
295cbf6d RB |
19 | |
20 | /* | |
21 | * CPU mask used to set process affinity for MT VPEs/TCs with FPUs | |
22 | */ | |
23 | cpumask_t mt_fpu_cpumask; | |
24 | ||
25 | static int fpaff_threshold = -1; | |
982f6ffe | 26 | unsigned long mt_fpemul_threshold; |
295cbf6d RB |
27 | |
28 | /* | |
29 | * Replacement functions for the sys_sched_setaffinity() and | |
30 | * sys_sched_getaffinity() system calls, so that we can integrate | |
31 | * FPU affinity with the user's requested processor affinity. | |
32 | * This code is 98% identical with the sys_sched_setaffinity() | |
33 | * and sys_sched_getaffinity() system calls, and should be | |
0a0fca9d | 34 | * updated when kernel/sched/core.c changes. |
295cbf6d RB |
35 | */ |
36 | ||
37 | /* | |
38 | * find_process_by_pid - find a process with a matching PID value. | |
0a0fca9d | 39 | * used in sys_sched_set/getaffinity() in kernel/sched/core.c, so |
295cbf6d RB |
40 | * cloned here. |
41 | */ | |
42 | static inline struct task_struct *find_process_by_pid(pid_t pid) | |
43 | { | |
0e568536 | 44 | return pid ? find_task_by_vpid(pid) : current; |
295cbf6d RB |
45 | } |
46 | ||
17c04139 RB |
47 | /* |
48 | * check the target process has a UID that matches the current process's | |
49 | */ | |
50 | static bool check_same_owner(struct task_struct *p) | |
51 | { | |
52 | const struct cred *cred = current_cred(), *pcred; | |
53 | bool match; | |
54 | ||
55 | rcu_read_lock(); | |
56 | pcred = __task_cred(p); | |
b88fb18e FF |
57 | match = (uid_eq(cred->euid, pcred->euid) || |
58 | uid_eq(cred->euid, pcred->uid)); | |
17c04139 RB |
59 | rcu_read_unlock(); |
60 | return match; | |
61 | } | |
295cbf6d RB |
62 | |
63 | /* | |
64 | * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process | |
65 | */ | |
66 | asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len, | |
67 | unsigned long __user *user_mask_ptr) | |
68 | { | |
17c04139 | 69 | cpumask_var_t cpus_allowed, new_mask, effective_mask; |
293c5bd1 | 70 | struct thread_info *ti; |
17c04139 RB |
71 | struct task_struct *p; |
72 | int retval; | |
295cbf6d RB |
73 | |
74 | if (len < sizeof(new_mask)) | |
75 | return -EINVAL; | |
76 | ||
77 | if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask))) | |
78 | return -EFAULT; | |
79 | ||
730d070a | 80 | cpus_read_lock(); |
17c04139 | 81 | rcu_read_lock(); |
295cbf6d RB |
82 | |
83 | p = find_process_by_pid(pid); | |
84 | if (!p) { | |
17c04139 | 85 | rcu_read_unlock(); |
730d070a | 86 | cpus_read_unlock(); |
295cbf6d RB |
87 | return -ESRCH; |
88 | } | |
89 | ||
17c04139 | 90 | /* Prevent p going away */ |
295cbf6d | 91 | get_task_struct(p); |
17c04139 | 92 | rcu_read_unlock(); |
295cbf6d | 93 | |
17c04139 RB |
94 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { |
95 | retval = -ENOMEM; | |
96 | goto out_put_task; | |
97 | } | |
98 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { | |
99 | retval = -ENOMEM; | |
100 | goto out_free_cpus_allowed; | |
101 | } | |
102 | if (!alloc_cpumask_var(&effective_mask, GFP_KERNEL)) { | |
103 | retval = -ENOMEM; | |
104 | goto out_free_new_mask; | |
105 | } | |
a45526bb ME |
106 | if (!check_same_owner(p) && !capable(CAP_SYS_NICE)) { |
107 | retval = -EPERM; | |
295cbf6d | 108 | goto out_unlock; |
a45526bb | 109 | } |
295cbf6d | 110 | |
a7f505c6 | 111 | retval = security_task_setscheduler(p); |
295cbf6d RB |
112 | if (retval) |
113 | goto out_unlock; | |
114 | ||
115 | /* Record new user-specified CPU set for future reference */ | |
17c04139 | 116 | cpumask_copy(&p->thread.user_cpus_allowed, new_mask); |
295cbf6d | 117 | |
17c04139 | 118 | again: |
295cbf6d | 119 | /* Compute new global allowed CPU set if necessary */ |
293c5bd1 RB |
120 | ti = task_thread_info(p); |
121 | if (test_ti_thread_flag(ti, TIF_FPUBOUND) && | |
8dd92891 RR |
122 | cpumask_intersects(new_mask, &mt_fpu_cpumask)) { |
123 | cpumask_and(effective_mask, new_mask, &mt_fpu_cpumask); | |
17c04139 | 124 | retval = set_cpus_allowed_ptr(p, effective_mask); |
295cbf6d | 125 | } else { |
17c04139 | 126 | cpumask_copy(effective_mask, new_mask); |
293c5bd1 | 127 | clear_ti_thread_flag(ti, TIF_FPUBOUND); |
17c04139 | 128 | retval = set_cpus_allowed_ptr(p, new_mask); |
295cbf6d RB |
129 | } |
130 | ||
17c04139 RB |
131 | if (!retval) { |
132 | cpuset_cpus_allowed(p, cpus_allowed); | |
133 | if (!cpumask_subset(effective_mask, cpus_allowed)) { | |
134 | /* | |
135 | * We must have raced with a concurrent cpuset | |
136 | * update. Just reset the cpus_allowed to the | |
137 | * cpuset's cpus_allowed | |
138 | */ | |
139 | cpumask_copy(new_mask, cpus_allowed); | |
140 | goto again; | |
141 | } | |
142 | } | |
295cbf6d | 143 | out_unlock: |
17c04139 RB |
144 | free_cpumask_var(effective_mask); |
145 | out_free_new_mask: | |
146 | free_cpumask_var(new_mask); | |
147 | out_free_cpus_allowed: | |
148 | free_cpumask_var(cpus_allowed); | |
149 | out_put_task: | |
295cbf6d | 150 | put_task_struct(p); |
730d070a | 151 | cpus_read_unlock(); |
295cbf6d RB |
152 | return retval; |
153 | } | |
154 | ||
155 | /* | |
156 | * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process | |
157 | */ | |
158 | asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len, | |
159 | unsigned long __user *user_mask_ptr) | |
160 | { | |
161 | unsigned int real_len; | |
1d62d737 | 162 | cpumask_t allowed, mask; |
295cbf6d RB |
163 | int retval; |
164 | struct task_struct *p; | |
165 | ||
166 | real_len = sizeof(mask); | |
167 | if (len < real_len) | |
168 | return -EINVAL; | |
169 | ||
730d070a | 170 | cpus_read_lock(); |
f0100c7f | 171 | rcu_read_lock(); |
295cbf6d RB |
172 | |
173 | retval = -ESRCH; | |
174 | p = find_process_by_pid(pid); | |
175 | if (!p) | |
176 | goto out_unlock; | |
177 | retval = security_task_getscheduler(p); | |
178 | if (retval) | |
179 | goto out_unlock; | |
180 | ||
3bd37062 | 181 | cpumask_or(&allowed, &p->thread.user_cpus_allowed, p->cpus_ptr); |
1d62d737 | 182 | cpumask_and(&mask, &allowed, cpu_active_mask); |
295cbf6d RB |
183 | |
184 | out_unlock: | |
f0100c7f | 185 | rcu_read_unlock(); |
730d070a | 186 | cpus_read_unlock(); |
295cbf6d RB |
187 | if (retval) |
188 | return retval; | |
189 | if (copy_to_user(user_mask_ptr, &mask, real_len)) | |
190 | return -EFAULT; | |
191 | return real_len; | |
192 | } | |
193 | ||
194 | ||
195 | static int __init fpaff_thresh(char *str) | |
196 | { | |
197 | get_option(&str, &fpaff_threshold); | |
198 | return 1; | |
199 | } | |
200 | __setup("fpaff=", fpaff_thresh); | |
201 | ||
202 | /* | |
203 | * FPU Use Factor empirically derived from experiments on 34K | |
204 | */ | |
9cc12363 | 205 | #define FPUSEFACTOR 2000 |
295cbf6d RB |
206 | |
207 | static __init int mt_fp_affinity_init(void) | |
208 | { | |
209 | if (fpaff_threshold >= 0) { | |
210 | mt_fpemul_threshold = fpaff_threshold; | |
211 | } else { | |
212 | mt_fpemul_threshold = | |
213 | (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ; | |
214 | } | |
215 | printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n", | |
216 | mt_fpemul_threshold); | |
217 | ||
218 | return 0; | |
219 | } | |
220 | arch_initcall(mt_fp_affinity_init); |