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