mips-mt-fpaff.c 5.1 KB

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