mips-mt-fpaff.c 5.2 KB

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