hung_task_enh.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/syscore_ops.h>
  10. #include <linux/sched/walt.h>
  11. #include <trace/hooks/hung_task.h>
  12. #define DEFAULT_MAX_IOWAIT_TASK 5
  13. enum {
  14. TASK_DEFAULT = 0,
  15. TASK_IN_WHITELIST,
  16. TASK_IN_BLACKLIST,
  17. };
  18. enum {
  19. HUNG_TASK_MODE_WHITELIST = 0,
  20. HUNG_TASK_MODE_BLACKLIST,
  21. };
  22. /**
  23. * struct hung_task_enh_data
  24. * @read_pid: PID of the process will be read
  25. * @global_detect_mode: The global detect mode switch, 0-Whitelist/1-Blacklist
  26. * @curr_iowait_task_cnt: Count the number of iowait processes in one scan cycle
  27. * @max_iowait_task_cnt: Max number of iowait processes
  28. * @curr_iowait_timeout_cnt: Count how many times the iowait condition is met
  29. * @max_iowait_timeout_cnt: Max times of the iowait condition is met
  30. * @ctl_table_hdr: hung_task_enh ctl table header
  31. */
  32. struct hung_task_enh_data {
  33. int read_pid;
  34. int global_detect_mode;
  35. int curr_iowait_task_cnt;
  36. int max_iowait_task_cnt;
  37. int curr_iowait_timeout_cnt;
  38. int max_iowait_timeout_cnt;
  39. struct ctl_table_header *ctl_table_hdr;
  40. };
  41. static struct hung_task_enh_data hung_task_enh;
  42. void qcom_before_check_tasks(void *ignore, struct task_struct *t, unsigned long timeout,
  43. bool *need_check)
  44. {
  45. struct walt_task_struct *wts = (struct walt_task_struct *) t->android_vendor_data1;
  46. if ((hung_task_enh.global_detect_mode == HUNG_TASK_MODE_WHITELIST &&
  47. wts->hung_detect_status != TASK_IN_WHITELIST) ||
  48. (hung_task_enh.global_detect_mode == HUNG_TASK_MODE_BLACKLIST &&
  49. wts->hung_detect_status == TASK_IN_BLACKLIST)) {
  50. *need_check = false;
  51. return;
  52. }
  53. *need_check = true;
  54. if (unlikely(t->in_iowait) && (t->__state == TASK_UNINTERRUPTIBLE ||
  55. t->__state == TASK_STOPPED || t->__state == TASK_TRACED) &&
  56. t->last_switch_time != 0 &&
  57. time_is_before_jiffies(t->last_switch_time + timeout * HZ) &&
  58. (t->mm != NULL && t == t->group_leader))
  59. hung_task_enh.curr_iowait_task_cnt++;
  60. }
  61. void qcom_check_tasks_done(void *ignore, void *extra)
  62. {
  63. if (hung_task_enh.curr_iowait_task_cnt >= hung_task_enh.max_iowait_task_cnt)
  64. hung_task_enh.curr_iowait_timeout_cnt++;
  65. else
  66. hung_task_enh.curr_iowait_timeout_cnt = 0;
  67. if (hung_task_enh.max_iowait_timeout_cnt != 0 &&
  68. hung_task_enh.curr_iowait_timeout_cnt >= hung_task_enh.max_iowait_timeout_cnt)
  69. panic("Detect IO wait too long time for multiple tasks!\n");
  70. hung_task_enh.curr_iowait_task_cnt = 0;
  71. }
  72. static DEFINE_MUTEX(readpid_mutex);
  73. static int read_pid_handler(struct ctl_table *table, int write,
  74. void __user *buffer, size_t *lenp, loff_t *ppos)
  75. {
  76. int ret;
  77. mutex_lock(&readpid_mutex);
  78. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  79. mutex_unlock(&readpid_mutex);
  80. return ret;
  81. }
  82. static int hung_task_handler(struct ctl_table *table, int write,
  83. void __user *buffer, size_t *lenp, loff_t *ppos)
  84. {
  85. int ret;
  86. struct task_struct *task;
  87. struct walt_task_struct *wts;
  88. int pid_and_val[2] = {-1, -1};
  89. struct ctl_table tmp = {
  90. .data = &pid_and_val,
  91. .maxlen = sizeof(pid_and_val),
  92. .mode = table->mode,
  93. };
  94. mutex_lock(&readpid_mutex);
  95. if (!write) {
  96. task = get_pid_task(find_vpid(hung_task_enh.read_pid),
  97. PIDTYPE_PID);
  98. if (!task) {
  99. ret = -ENOENT;
  100. goto unlock_mutex;
  101. }
  102. wts = (struct walt_task_struct *) task->android_vendor_data1;
  103. pid_and_val[0] = hung_task_enh.read_pid;
  104. pid_and_val[1] = wts->hung_detect_status;
  105. ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
  106. goto put_task;
  107. }
  108. ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
  109. if (ret)
  110. goto unlock_mutex;
  111. if (pid_and_val[0] <= 0 || pid_and_val[1] < 0 || pid_and_val[1] > 1) {
  112. ret = -ENOENT;
  113. goto unlock_mutex;
  114. }
  115. task = get_pid_task(find_vpid(pid_and_val[0]), PIDTYPE_PID);
  116. if (!task) {
  117. ret = -ENOENT;
  118. goto unlock_mutex;
  119. }
  120. wts = (struct walt_task_struct *) task->android_vendor_data1;
  121. if (pid_and_val[1] == 1) {
  122. if (hung_task_enh.global_detect_mode == HUNG_TASK_MODE_WHITELIST)
  123. wts->hung_detect_status = TASK_IN_WHITELIST;
  124. else
  125. wts->hung_detect_status = TASK_IN_BLACKLIST;
  126. } else {
  127. wts->hung_detect_status = TASK_DEFAULT;
  128. }
  129. put_task:
  130. put_task_struct(task);
  131. unlock_mutex:
  132. mutex_unlock(&readpid_mutex);
  133. return ret;
  134. }
  135. struct ctl_table hung_task_table[] = {
  136. {
  137. .procname = "global_detect_mode",
  138. .data = &hung_task_enh.global_detect_mode,
  139. .maxlen = sizeof(int),
  140. .mode = 0644,
  141. .proc_handler = proc_dointvec_minmax,
  142. .extra1 = SYSCTL_ZERO,
  143. .extra2 = SYSCTL_ONE,
  144. },
  145. {
  146. .procname = "per_task_detect_mode",
  147. .data = (int *) 0,
  148. .maxlen = sizeof(unsigned int) * 2,
  149. .mode = 0644,
  150. .proc_handler = hung_task_handler,
  151. },
  152. {
  153. .procname = "max_iowait_timeout_cnt",
  154. .data = &hung_task_enh.max_iowait_timeout_cnt,
  155. .maxlen = sizeof(int),
  156. .mode = 0644,
  157. .proc_handler = proc_dointvec_minmax,
  158. .extra1 = SYSCTL_ZERO,
  159. .extra2 = SYSCTL_INT_MAX,
  160. },
  161. {
  162. .procname = "max_iowait_task_cnt",
  163. .data = &hung_task_enh.max_iowait_task_cnt,
  164. .maxlen = sizeof(int),
  165. .mode = 0644,
  166. .proc_handler = proc_dointvec_minmax,
  167. .extra1 = SYSCTL_ONE,
  168. .extra2 = SYSCTL_INT_MAX,
  169. },
  170. {
  171. .procname = "read_pid",
  172. .data = &hung_task_enh.read_pid,
  173. .maxlen = sizeof(int),
  174. .mode = 0644,
  175. .proc_handler = read_pid_handler,
  176. .extra1 = SYSCTL_ONE,
  177. .extra2 = SYSCTL_INT_MAX,
  178. },
  179. { }
  180. };
  181. struct ctl_table hung_task_base_table[] = {
  182. {
  183. .procname = "hung_task_enh",
  184. .mode = 0555,
  185. .child = hung_task_table,
  186. },
  187. { }
  188. };
  189. static int __init hung_task_enh_init(void)
  190. {
  191. int ret;
  192. hung_task_enh.max_iowait_task_cnt = DEFAULT_MAX_IOWAIT_TASK;
  193. ret = register_trace_android_vh_check_uninterrupt_tasks(
  194. qcom_before_check_tasks, NULL);
  195. if (ret)
  196. return ret;
  197. ret = register_trace_android_vh_check_uninterrupt_tasks_done(
  198. qcom_check_tasks_done, NULL);
  199. if (ret) {
  200. unregister_trace_android_vh_check_uninterrupt_tasks(
  201. qcom_before_check_tasks, NULL);
  202. return ret;
  203. }
  204. hung_task_enh.ctl_table_hdr = register_sysctl_table(
  205. hung_task_base_table);
  206. if (!hung_task_enh.ctl_table_hdr) {
  207. unregister_trace_android_vh_check_uninterrupt_tasks(
  208. qcom_before_check_tasks, NULL);
  209. unregister_trace_android_vh_check_uninterrupt_tasks_done(
  210. qcom_check_tasks_done, NULL);
  211. return -ENOMEM;
  212. }
  213. return ret;
  214. }
  215. late_initcall(hung_task_enh_init);
  216. static void __exit hung_task_enh_exit(void)
  217. {
  218. unregister_sysctl_table(hung_task_enh.ctl_table_hdr);
  219. unregister_trace_android_vh_check_uninterrupt_tasks(
  220. qcom_before_check_tasks, NULL);
  221. unregister_trace_android_vh_check_uninterrupt_tasks_done(
  222. qcom_check_tasks_done, NULL);
  223. }
  224. module_exit(hung_task_enh_exit);
  225. MODULE_DESCRIPTION("QCOM Hung Task Enhancement");
  226. MODULE_LICENSE("GPL");