mips-mt-fpaff.c 5.2 KB

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