mips-mt-fpaff.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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>
  14. /*
  15. * CPU mask used to set process affinity for MT VPEs/TCs with FPUs
  16. */
  17. cpumask_t mt_fpu_cpumask;
  18. static int fpaff_threshold = -1;
  19. unsigned long mt_fpemul_threshold = 0;
  20. /*
  21. * Replacement functions for the sys_sched_setaffinity() and
  22. * sys_sched_getaffinity() system calls, so that we can integrate
  23. * FPU affinity with the user's requested processor affinity.
  24. * This code is 98% identical with the sys_sched_setaffinity()
  25. * and sys_sched_getaffinity() system calls, and should be
  26. * updated when kernel/sched.c changes.
  27. */
  28. /*
  29. * find_process_by_pid - find a process with a matching PID value.
  30. * used in sys_sched_set/getaffinity() in kernel/sched.c, so
  31. * cloned here.
  32. */
  33. static inline struct task_struct *find_process_by_pid(pid_t pid)
  34. {
  35. return pid ? find_task_by_vpid(pid) : current;
  36. }
  37. /*
  38. * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process
  39. */
  40. asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
  41. unsigned long __user *user_mask_ptr)
  42. {
  43. cpumask_t new_mask;
  44. cpumask_t effective_mask;
  45. int retval;
  46. struct task_struct *p;
  47. struct thread_info *ti;
  48. uid_t euid;
  49. if (len < sizeof(new_mask))
  50. return -EINVAL;
  51. if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
  52. return -EFAULT;
  53. get_online_cpus();
  54. read_lock(&tasklist_lock);
  55. p = find_process_by_pid(pid);
  56. if (!p) {
  57. read_unlock(&tasklist_lock);
  58. put_online_cpus();
  59. return -ESRCH;
  60. }
  61. /*
  62. * It is not safe to call set_cpus_allowed with the
  63. * tasklist_lock held. We will bump the task_struct's
  64. * usage count and drop tasklist_lock before invoking
  65. * set_cpus_allowed.
  66. */
  67. get_task_struct(p);
  68. euid = current_euid();
  69. retval = -EPERM;
  70. if (euid != p->euid && euid != p->uid && !capable(CAP_SYS_NICE)) {
  71. read_unlock(&tasklist_lock);
  72. goto out_unlock;
  73. }
  74. retval = security_task_setscheduler(p, 0, NULL);
  75. if (retval)
  76. goto out_unlock;
  77. /* Record new user-specified CPU set for future reference */
  78. p->thread.user_cpus_allowed = new_mask;
  79. /* Unlock the task list */
  80. read_unlock(&tasklist_lock);
  81. /* Compute new global allowed CPU set if necessary */
  82. ti = task_thread_info(p);
  83. if (test_ti_thread_flag(ti, TIF_FPUBOUND) &&
  84. cpus_intersects(new_mask, mt_fpu_cpumask)) {
  85. cpus_and(effective_mask, new_mask, mt_fpu_cpumask);
  86. retval = set_cpus_allowed(p, effective_mask);
  87. } else {
  88. clear_ti_thread_flag(ti, TIF_FPUBOUND);
  89. retval = set_cpus_allowed(p, new_mask);
  90. }
  91. out_unlock:
  92. put_task_struct(p);
  93. put_online_cpus();
  94. return retval;
  95. }
  96. /*
  97. * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
  98. */
  99. asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
  100. unsigned long __user *user_mask_ptr)
  101. {
  102. unsigned int real_len;
  103. cpumask_t mask;
  104. int retval;
  105. struct task_struct *p;
  106. real_len = sizeof(mask);
  107. if (len < real_len)
  108. return -EINVAL;
  109. get_online_cpus();
  110. read_lock(&tasklist_lock);
  111. retval = -ESRCH;
  112. p = find_process_by_pid(pid);
  113. if (!p)
  114. goto out_unlock;
  115. retval = security_task_getscheduler(p);
  116. if (retval)
  117. goto out_unlock;
  118. cpus_and(mask, p->thread.user_cpus_allowed, cpu_possible_map);
  119. out_unlock:
  120. read_unlock(&tasklist_lock);
  121. put_online_cpus();
  122. if (retval)
  123. return retval;
  124. if (copy_to_user(user_mask_ptr, &mask, real_len))
  125. return -EFAULT;
  126. return real_len;
  127. }
  128. static int __init fpaff_thresh(char *str)
  129. {
  130. get_option(&str, &fpaff_threshold);
  131. return 1;
  132. }
  133. __setup("fpaff=", fpaff_thresh);
  134. /*
  135. * FPU Use Factor empirically derived from experiments on 34K
  136. */
  137. #define FPUSEFACTOR 2000
  138. static __init int mt_fp_affinity_init(void)
  139. {
  140. if (fpaff_threshold >= 0) {
  141. mt_fpemul_threshold = fpaff_threshold;
  142. } else {
  143. mt_fpemul_threshold =
  144. (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
  145. }
  146. printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n",
  147. mt_fpemul_threshold);
  148. return 0;
  149. }
  150. arch_initcall(mt_fp_affinity_init);