mips-mt-fpaff.c 4.1 KB

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