mips-mt-fpaff.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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->cred->euid && euid != p->cred->uid &&
  71. !capable(CAP_SYS_NICE)) {
  72. read_unlock(&tasklist_lock);
  73. goto out_unlock;
  74. }
  75. retval = security_task_setscheduler(p, 0, NULL);
  76. if (retval)
  77. goto out_unlock;
  78. /* Record new user-specified CPU set for future reference */
  79. p->thread.user_cpus_allowed = new_mask;
  80. /* Unlock the task list */
  81. read_unlock(&tasklist_lock);
  82. /* Compute new global allowed CPU set if necessary */
  83. ti = task_thread_info(p);
  84. if (test_ti_thread_flag(ti, TIF_FPUBOUND) &&
  85. cpus_intersects(new_mask, mt_fpu_cpumask)) {
  86. cpus_and(effective_mask, new_mask, mt_fpu_cpumask);
  87. retval = set_cpus_allowed(p, effective_mask);
  88. } else {
  89. clear_ti_thread_flag(ti, TIF_FPUBOUND);
  90. retval = set_cpus_allowed(p, new_mask);
  91. }
  92. out_unlock:
  93. put_task_struct(p);
  94. put_online_cpus();
  95. return retval;
  96. }
  97. /*
  98. * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
  99. */
  100. asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
  101. unsigned long __user *user_mask_ptr)
  102. {
  103. unsigned int real_len;
  104. cpumask_t mask;
  105. int retval;
  106. struct task_struct *p;
  107. real_len = sizeof(mask);
  108. if (len < real_len)
  109. return -EINVAL;
  110. get_online_cpus();
  111. read_lock(&tasklist_lock);
  112. retval = -ESRCH;
  113. p = find_process_by_pid(pid);
  114. if (!p)
  115. goto out_unlock;
  116. retval = security_task_getscheduler(p);
  117. if (retval)
  118. goto out_unlock;
  119. cpus_and(mask, p->thread.user_cpus_allowed, cpu_possible_map);
  120. out_unlock:
  121. read_unlock(&tasklist_lock);
  122. put_online_cpus();
  123. if (retval)
  124. return retval;
  125. if (copy_to_user(user_mask_ptr, &mask, real_len))
  126. return -EFAULT;
  127. return real_len;
  128. }
  129. static int __init fpaff_thresh(char *str)
  130. {
  131. get_option(&str, &fpaff_threshold);
  132. return 1;
  133. }
  134. __setup("fpaff=", fpaff_thresh);
  135. /*
  136. * FPU Use Factor empirically derived from experiments on 34K
  137. */
  138. #define FPUSEFACTOR 2000
  139. static __init int mt_fp_affinity_init(void)
  140. {
  141. if (fpaff_threshold >= 0) {
  142. mt_fpemul_threshold = fpaff_threshold;
  143. } else {
  144. mt_fpemul_threshold =
  145. (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
  146. }
  147. printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n",
  148. mt_fpemul_threshold);
  149. return 0;
  150. }
  151. arch_initcall(mt_fp_affinity_init);