blob: 5e77a3a21f98e7926cc789915ad65bbe64911a1a [file] [log] [blame]
Ralf Baechle295cbf62007-07-03 14:37:43 +01001/*
2 * General MIPS MT support routines, usable in AP/SP, SMVP, or SMTC kernels
3 * Copyright (C) 2005 Mips Technologies, Inc
4 */
5#include <linux/cpu.h>
6#include <linux/cpumask.h>
7#include <linux/delay.h>
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/sched.h>
11#include <linux/security.h>
12#include <linux/types.h>
13#include <asm/uaccess.h>
14
15/*
16 * CPU mask used to set process affinity for MT VPEs/TCs with FPUs
17 */
18cpumask_t mt_fpu_cpumask;
19
20static int fpaff_threshold = -1;
21unsigned long mt_fpemul_threshold = 0;
22
23/*
24 * Replacement functions for the sys_sched_setaffinity() and
25 * sys_sched_getaffinity() system calls, so that we can integrate
26 * FPU affinity with the user's requested processor affinity.
27 * This code is 98% identical with the sys_sched_setaffinity()
28 * and sys_sched_getaffinity() system calls, and should be
29 * updated when kernel/sched.c changes.
30 */
31
32/*
33 * find_process_by_pid - find a process with a matching PID value.
34 * used in sys_sched_set/getaffinity() in kernel/sched.c, so
35 * cloned here.
36 */
37static inline struct task_struct *find_process_by_pid(pid_t pid)
38{
Pavel Emelyanov0e568532008-02-04 23:44:24 -080039 return pid ? find_task_by_vpid(pid) : current;
Ralf Baechle295cbf62007-07-03 14:37:43 +010040}
41
42
43/*
44 * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process
45 */
46asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
47 unsigned long __user *user_mask_ptr)
48{
49 cpumask_t new_mask;
50 cpumask_t effective_mask;
51 int retval;
52 struct task_struct *p;
Ralf Baechle293c5bd2007-07-25 16:19:33 +010053 struct thread_info *ti;
David Howellsb4212732008-11-14 10:38:37 +110054 uid_t euid;
Ralf Baechle295cbf62007-07-03 14:37:43 +010055
56 if (len < sizeof(new_mask))
57 return -EINVAL;
58
59 if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
60 return -EFAULT;
61
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010062 get_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +010063 read_lock(&tasklist_lock);
64
65 p = find_process_by_pid(pid);
66 if (!p) {
67 read_unlock(&tasklist_lock);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010068 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +010069 return -ESRCH;
70 }
71
72 /*
73 * It is not safe to call set_cpus_allowed with the
74 * tasklist_lock held. We will bump the task_struct's
75 * usage count and drop tasklist_lock before invoking
76 * set_cpus_allowed.
77 */
78 get_task_struct(p);
79
David Howellsb4212732008-11-14 10:38:37 +110080 euid = current_euid();
Ralf Baechle295cbf62007-07-03 14:37:43 +010081 retval = -EPERM;
David Howellsb4212732008-11-14 10:38:37 +110082 if (euid != p->euid && euid != p->uid && !capable(CAP_SYS_NICE)) {
Ralf Baechle295cbf62007-07-03 14:37:43 +010083 read_unlock(&tasklist_lock);
84 goto out_unlock;
85 }
86
87 retval = security_task_setscheduler(p, 0, NULL);
88 if (retval)
89 goto out_unlock;
90
91 /* Record new user-specified CPU set for future reference */
92 p->thread.user_cpus_allowed = new_mask;
93
94 /* Unlock the task list */
95 read_unlock(&tasklist_lock);
96
97 /* Compute new global allowed CPU set if necessary */
Ralf Baechle293c5bd2007-07-25 16:19:33 +010098 ti = task_thread_info(p);
99 if (test_ti_thread_flag(ti, TIF_FPUBOUND) &&
100 cpus_intersects(new_mask, mt_fpu_cpumask)) {
Ralf Baechle295cbf62007-07-03 14:37:43 +0100101 cpus_and(effective_mask, new_mask, mt_fpu_cpumask);
102 retval = set_cpus_allowed(p, effective_mask);
103 } else {
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100104 clear_ti_thread_flag(ti, TIF_FPUBOUND);
Ralf Baechle295cbf62007-07-03 14:37:43 +0100105 retval = set_cpus_allowed(p, new_mask);
106 }
107
Ralf Baechle295cbf62007-07-03 14:37:43 +0100108out_unlock:
109 put_task_struct(p);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100110 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100111 return retval;
112}
113
114/*
115 * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
116 */
117asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
118 unsigned long __user *user_mask_ptr)
119{
120 unsigned int real_len;
121 cpumask_t mask;
122 int retval;
123 struct task_struct *p;
124
125 real_len = sizeof(mask);
126 if (len < real_len)
127 return -EINVAL;
128
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100129 get_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100130 read_lock(&tasklist_lock);
131
132 retval = -ESRCH;
133 p = find_process_by_pid(pid);
134 if (!p)
135 goto out_unlock;
136 retval = security_task_getscheduler(p);
137 if (retval)
138 goto out_unlock;
139
140 cpus_and(mask, p->thread.user_cpus_allowed, cpu_possible_map);
141
142out_unlock:
143 read_unlock(&tasklist_lock);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100144 put_online_cpus();
Ralf Baechle295cbf62007-07-03 14:37:43 +0100145 if (retval)
146 return retval;
147 if (copy_to_user(user_mask_ptr, &mask, real_len))
148 return -EFAULT;
149 return real_len;
150}
151
152
153static int __init fpaff_thresh(char *str)
154{
155 get_option(&str, &fpaff_threshold);
156 return 1;
157}
158__setup("fpaff=", fpaff_thresh);
159
160/*
161 * FPU Use Factor empirically derived from experiments on 34K
162 */
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200163#define FPUSEFACTOR 2000
Ralf Baechle295cbf62007-07-03 14:37:43 +0100164
165static __init int mt_fp_affinity_init(void)
166{
167 if (fpaff_threshold >= 0) {
168 mt_fpemul_threshold = fpaff_threshold;
169 } else {
170 mt_fpemul_threshold =
171 (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
172 }
173 printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n",
174 mt_fpemul_threshold);
175
176 return 0;
177}
178arch_initcall(mt_fp_affinity_init);