mips-mt-fpaff.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_pid(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. if (len < sizeof(new_mask))
  49. return -EINVAL;
  50. if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
  51. return -EFAULT;
  52. lock_cpu_hotplug();
  53. read_lock(&tasklist_lock);
  54. p = find_process_by_pid(pid);
  55. if (!p) {
  56. read_unlock(&tasklist_lock);
  57. unlock_cpu_hotplug();
  58. return -ESRCH;
  59. }
  60. /*
  61. * It is not safe to call set_cpus_allowed with the
  62. * tasklist_lock held. We will bump the task_struct's
  63. * usage count and drop tasklist_lock before invoking
  64. * set_cpus_allowed.
  65. */
  66. get_task_struct(p);
  67. retval = -EPERM;
  68. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  69. !capable(CAP_SYS_NICE)) {
  70. read_unlock(&tasklist_lock);
  71. goto out_unlock;
  72. }
  73. retval = security_task_setscheduler(p, 0, NULL);
  74. if (retval)
  75. goto out_unlock;
  76. /* Record new user-specified CPU set for future reference */
  77. p->thread.user_cpus_allowed = new_mask;
  78. /* Unlock the task list */
  79. read_unlock(&tasklist_lock);
  80. /* Compute new global allowed CPU set if necessary */
  81. ti = task_thread_info(p);
  82. if (test_ti_thread_flag(ti, TIF_FPUBOUND) &&
  83. cpus_intersects(new_mask, mt_fpu_cpumask)) {
  84. cpus_and(effective_mask, new_mask, mt_fpu_cpumask);
  85. retval = set_cpus_allowed(p, effective_mask);
  86. } else {
  87. clear_ti_thread_flag(ti, TIF_FPUBOUND);
  88. retval = set_cpus_allowed(p, new_mask);
  89. }
  90. out_unlock:
  91. put_task_struct(p);
  92. unlock_cpu_hotplug();
  93. return retval;
  94. }
  95. /*
  96. * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
  97. */
  98. asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
  99. unsigned long __user *user_mask_ptr)
  100. {
  101. unsigned int real_len;
  102. cpumask_t mask;
  103. int retval;
  104. struct task_struct *p;
  105. real_len = sizeof(mask);
  106. if (len < real_len)
  107. return -EINVAL;
  108. lock_cpu_hotplug();
  109. read_lock(&tasklist_lock);
  110. retval = -ESRCH;
  111. p = find_process_by_pid(pid);
  112. if (!p)
  113. goto out_unlock;
  114. retval = security_task_getscheduler(p);
  115. if (retval)
  116. goto out_unlock;
  117. cpus_and(mask, p->thread.user_cpus_allowed, cpu_possible_map);
  118. out_unlock:
  119. read_unlock(&tasklist_lock);
  120. unlock_cpu_hotplug();
  121. if (retval)
  122. return retval;
  123. if (copy_to_user(user_mask_ptr, &mask, real_len))
  124. return -EFAULT;
  125. return real_len;
  126. }
  127. static int __init fpaff_thresh(char *str)
  128. {
  129. get_option(&str, &fpaff_threshold);
  130. return 1;
  131. }
  132. __setup("fpaff=", fpaff_thresh);
  133. /*
  134. * FPU Use Factor empirically derived from experiments on 34K
  135. */
  136. #define FPUSEFACTOR 333
  137. static __init int mt_fp_affinity_init(void)
  138. {
  139. if (fpaff_threshold >= 0) {
  140. mt_fpemul_threshold = fpaff_threshold;
  141. } else {
  142. mt_fpemul_threshold =
  143. (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
  144. }
  145. printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n",
  146. mt_fpemul_threshold);
  147. return 0;
  148. }
  149. arch_initcall(mt_fp_affinity_init);