mips-mt-fpaff.c 5.1 KB

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