xref: /linux/arch/mips/kernel/mips-mt-fpaff.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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 #include <asm/syscalls.h>
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;
26 unsigned long mt_fpemul_threshold;
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
34  * updated when kernel/sched/core.c changes.
35  */
36 
37 /*
38  * find_process_by_pid - find a process with a matching PID value.
39  * used in sys_sched_set/getaffinity() in kernel/sched/core.c, so
40  * cloned here.
41  */
42 static inline struct task_struct *find_process_by_pid(pid_t pid)
43 {
44 	return pid ? find_task_by_vpid(pid) : current;
45 }
46 
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);
57 	match = (uid_eq(cred->euid, pcred->euid) ||
58 		 uid_eq(cred->euid, pcred->uid));
59 	rcu_read_unlock();
60 	return match;
61 }
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 {
69 	cpumask_var_t cpus_allowed, new_mask, effective_mask;
70 	struct thread_info *ti;
71 	struct task_struct *p;
72 	int retval;
73 
74 	if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
75 		return -ENOMEM;
76 	if (len < cpumask_size())
77 		cpumask_clear(new_mask);
78 	else if (len > cpumask_size())
79 		len = cpumask_size();
80 	if (copy_from_user(new_mask, user_mask_ptr, len)) {
81 		retval = -EFAULT;
82 		goto out_free_new_mask;
83 	}
84 
85 	cpus_read_lock();
86 	rcu_read_lock();
87 
88 	p = find_process_by_pid(pid);
89 	if (!p) {
90 		rcu_read_unlock();
91 		cpus_read_unlock();
92 		retval = -ESRCH;
93 		goto out_free_new_mask;
94 	}
95 
96 	/* Prevent p going away */
97 	get_task_struct(p);
98 	rcu_read_unlock();
99 
100 	if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
101 		retval = -ENOMEM;
102 		goto out_put_task;
103 	}
104 	if (!alloc_cpumask_var(&effective_mask, GFP_KERNEL)) {
105 		retval = -ENOMEM;
106 		goto out_free_cpus_allowed;
107 	}
108 	if (!check_same_owner(p) && !capable(CAP_SYS_NICE)) {
109 		retval = -EPERM;
110 		goto out_unlock;
111 	}
112 
113 	retval = security_task_setscheduler(p);
114 	if (retval)
115 		goto out_unlock;
116 
117 	/* Record new user-specified CPU set for future reference */
118 	cpumask_copy(&p->thread.user_cpus_allowed, new_mask);
119 
120  again:
121 	/* Compute new global allowed CPU set if necessary */
122 	ti = task_thread_info(p);
123 	if (test_ti_thread_flag(ti, TIF_FPUBOUND) &&
124 	    cpumask_intersects(new_mask, &mt_fpu_cpumask)) {
125 		cpumask_and(effective_mask, new_mask, &mt_fpu_cpumask);
126 		retval = set_cpus_allowed_ptr(p, effective_mask);
127 	} else {
128 		cpumask_copy(effective_mask, new_mask);
129 		clear_ti_thread_flag(ti, TIF_FPUBOUND);
130 		retval = set_cpus_allowed_ptr(p, new_mask);
131 	}
132 
133 	if (!retval) {
134 		cpuset_cpus_allowed(p, cpus_allowed);
135 		if (!cpumask_subset(effective_mask, cpus_allowed)) {
136 			/*
137 			 * We must have raced with a concurrent cpuset
138 			 * update. Just reset the cpus_allowed to the
139 			 * cpuset's cpus_allowed
140 			 */
141 			cpumask_copy(new_mask, cpus_allowed);
142 			goto again;
143 		}
144 	}
145 out_unlock:
146 	free_cpumask_var(effective_mask);
147 out_free_cpus_allowed:
148 	free_cpumask_var(cpus_allowed);
149 out_put_task:
150 	put_task_struct(p);
151 	cpus_read_unlock();
152 out_free_new_mask:
153 	free_cpumask_var(new_mask);
154 	return retval;
155 }
156 
157 /*
158  * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
159  */
160 asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
161 				      unsigned long __user *user_mask_ptr)
162 {
163 	unsigned int real_len;
164 	cpumask_t allowed, mask;
165 	int retval;
166 	struct task_struct *p;
167 
168 	real_len = sizeof(mask);
169 	if (len < real_len)
170 		return -EINVAL;
171 
172 	cpus_read_lock();
173 	rcu_read_lock();
174 
175 	retval = -ESRCH;
176 	p = find_process_by_pid(pid);
177 	if (!p)
178 		goto out_unlock;
179 	retval = security_task_getscheduler(p);
180 	if (retval)
181 		goto out_unlock;
182 
183 	cpumask_or(&allowed, &p->thread.user_cpus_allowed, p->cpus_ptr);
184 	cpumask_and(&mask, &allowed, cpu_active_mask);
185 
186 out_unlock:
187 	rcu_read_unlock();
188 	cpus_read_unlock();
189 	if (retval)
190 		return retval;
191 	if (copy_to_user(user_mask_ptr, &mask, real_len))
192 		return -EFAULT;
193 	return real_len;
194 }
195 
196 
197 static int __init fpaff_thresh(char *str)
198 {
199 	get_option(&str, &fpaff_threshold);
200 	return 1;
201 }
202 __setup("fpaff=", fpaff_thresh);
203 
204 /*
205  * FPU Use Factor empirically derived from experiments on 34K
206  */
207 #define FPUSEFACTOR 2000
208 
209 static __init int mt_fp_affinity_init(void)
210 {
211 	if (fpaff_threshold >= 0) {
212 		mt_fpemul_threshold = fpaff_threshold;
213 	} else {
214 		mt_fpemul_threshold =
215 			(FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
216 	}
217 	printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n",
218 	       mt_fpemul_threshold);
219 
220 	return 0;
221 }
222 arch_initcall(mt_fp_affinity_init);
223