Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / mips / kernel / mips-mt-fpaff.c
blobf54f51ad5b489d4b3f17d1916aceded12fee48b2
1 /*
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>
16 * CPU mask used to set process affinity for MT VPEs/TCs with FPUs
18 cpumask_t mt_fpu_cpumask;
20 static int fpaff_threshold = -1;
21 unsigned long mt_fpemul_threshold = 0;
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.
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.
37 static inline struct task_struct *find_process_by_pid(pid_t pid)
39 <<<<<<< HEAD:arch/mips/kernel/mips-mt-fpaff.c
40 return pid ? find_task_by_pid(pid) : current;
41 =======
42 return pid ? find_task_by_vpid(pid) : current;
43 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/mips/kernel/mips-mt-fpaff.c
48 * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process
50 asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
51 unsigned long __user *user_mask_ptr)
53 cpumask_t new_mask;
54 cpumask_t effective_mask;
55 int retval;
56 struct task_struct *p;
57 struct thread_info *ti;
59 if (len < sizeof(new_mask))
60 return -EINVAL;
62 if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
63 return -EFAULT;
65 get_online_cpus();
66 read_lock(&tasklist_lock);
68 p = find_process_by_pid(pid);
69 if (!p) {
70 read_unlock(&tasklist_lock);
71 put_online_cpus();
72 return -ESRCH;
76 * It is not safe to call set_cpus_allowed with the
77 * tasklist_lock held. We will bump the task_struct's
78 * usage count and drop tasklist_lock before invoking
79 * set_cpus_allowed.
81 get_task_struct(p);
83 retval = -EPERM;
84 if ((current->euid != p->euid) && (current->euid != p->uid) &&
85 !capable(CAP_SYS_NICE)) {
86 read_unlock(&tasklist_lock);
87 goto out_unlock;
90 retval = security_task_setscheduler(p, 0, NULL);
91 if (retval)
92 goto out_unlock;
94 /* Record new user-specified CPU set for future reference */
95 p->thread.user_cpus_allowed = new_mask;
97 /* Unlock the task list */
98 read_unlock(&tasklist_lock);
100 /* Compute new global allowed CPU set if necessary */
101 ti = task_thread_info(p);
102 if (test_ti_thread_flag(ti, TIF_FPUBOUND) &&
103 cpus_intersects(new_mask, mt_fpu_cpumask)) {
104 cpus_and(effective_mask, new_mask, mt_fpu_cpumask);
105 retval = set_cpus_allowed(p, effective_mask);
106 } else {
107 clear_ti_thread_flag(ti, TIF_FPUBOUND);
108 retval = set_cpus_allowed(p, new_mask);
111 out_unlock:
112 put_task_struct(p);
113 put_online_cpus();
114 return retval;
118 * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
120 asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
121 unsigned long __user *user_mask_ptr)
123 unsigned int real_len;
124 cpumask_t mask;
125 int retval;
126 struct task_struct *p;
128 real_len = sizeof(mask);
129 if (len < real_len)
130 return -EINVAL;
132 get_online_cpus();
133 read_lock(&tasklist_lock);
135 retval = -ESRCH;
136 p = find_process_by_pid(pid);
137 if (!p)
138 goto out_unlock;
139 retval = security_task_getscheduler(p);
140 if (retval)
141 goto out_unlock;
143 cpus_and(mask, p->thread.user_cpus_allowed, cpu_possible_map);
145 out_unlock:
146 read_unlock(&tasklist_lock);
147 put_online_cpus();
148 if (retval)
149 return retval;
150 if (copy_to_user(user_mask_ptr, &mask, real_len))
151 return -EFAULT;
152 return real_len;
156 static int __init fpaff_thresh(char *str)
158 get_option(&str, &fpaff_threshold);
159 return 1;
161 __setup("fpaff=", fpaff_thresh);
164 * FPU Use Factor empirically derived from experiments on 34K
166 #define FPUSEFACTOR 333
168 static __init int mt_fp_affinity_init(void)
170 if (fpaff_threshold >= 0) {
171 mt_fpemul_threshold = fpaff_threshold;
172 } else {
173 mt_fpemul_threshold =
174 (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
176 printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n",
177 mt_fpemul_threshold);
179 return 0;
181 arch_initcall(mt_fp_affinity_init);