hung_task_tests.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * hung_task_tests.c - Sample code for testing hung tasks with mutex,
  4. * semaphore, etc.
  5. *
  6. * Usage: Load this module and read `<debugfs>/hung_task/mutex`,
  7. * `<debugfs>/hung_task/semaphore`, `<debugfs>/hung_task/rw_semaphore_read`,
  8. * `<debugfs>/hung_task/rw_semaphore_write`, etc., with 2 or more processes.
  9. *
  10. * This is for testing kernel hung_task error messages with various locking
  11. * mechanisms (e.g., mutex, semaphore, rw_semaphore_read, rw_semaphore_write, etc.).
  12. * Note that this may freeze your system or cause a panic. Use only for testing purposes.
  13. */
  14. #include <linux/debugfs.h>
  15. #include <linux/delay.h>
  16. #include <linux/fs.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/semaphore.h>
  20. #include <linux/rwsem.h>
  21. #define HUNG_TASK_DIR "hung_task"
  22. #define HUNG_TASK_MUTEX_FILE "mutex"
  23. #define HUNG_TASK_SEM_FILE "semaphore"
  24. #define HUNG_TASK_RWSEM_READ_FILE "rw_semaphore_read"
  25. #define HUNG_TASK_RWSEM_WRITE_FILE "rw_semaphore_write"
  26. #define SLEEP_SECOND 256
  27. static const char dummy_string[] = "This is a dummy string.";
  28. static DEFINE_MUTEX(dummy_mutex);
  29. static DEFINE_SEMAPHORE(dummy_sem, 1);
  30. static DECLARE_RWSEM(dummy_rwsem);
  31. static struct dentry *hung_task_dir;
  32. /* Mutex-based read function */
  33. static ssize_t read_dummy_mutex(struct file *file, char __user *user_buf,
  34. size_t count, loff_t *ppos)
  35. {
  36. /* Check if data is already read */
  37. if (*ppos >= sizeof(dummy_string))
  38. return 0;
  39. /* Second task waits on mutex, entering uninterruptible sleep */
  40. guard(mutex)(&dummy_mutex);
  41. /* First task sleeps here, interruptible */
  42. msleep_interruptible(SLEEP_SECOND * 1000);
  43. return simple_read_from_buffer(user_buf, count, ppos, dummy_string,
  44. sizeof(dummy_string));
  45. }
  46. /* Semaphore-based read function */
  47. static ssize_t read_dummy_semaphore(struct file *file, char __user *user_buf,
  48. size_t count, loff_t *ppos)
  49. {
  50. /* Check if data is already read */
  51. if (*ppos >= sizeof(dummy_string))
  52. return 0;
  53. /* Second task waits on semaphore, entering uninterruptible sleep */
  54. down(&dummy_sem);
  55. /* First task sleeps here, interruptible */
  56. msleep_interruptible(SLEEP_SECOND * 1000);
  57. up(&dummy_sem);
  58. return simple_read_from_buffer(user_buf, count, ppos, dummy_string,
  59. sizeof(dummy_string));
  60. }
  61. /* Read-write semaphore read function */
  62. static ssize_t read_dummy_rwsem_read(struct file *file, char __user *user_buf,
  63. size_t count, loff_t *ppos)
  64. {
  65. /* Check if data is already read */
  66. if (*ppos >= sizeof(dummy_string))
  67. return 0;
  68. /* Acquires read lock, allowing concurrent readers but blocks if write lock is held */
  69. down_read(&dummy_rwsem);
  70. /* Sleeps here, potentially triggering hung task detection if lock is held too long */
  71. msleep_interruptible(SLEEP_SECOND * 1000);
  72. up_read(&dummy_rwsem);
  73. return simple_read_from_buffer(user_buf, count, ppos, dummy_string,
  74. sizeof(dummy_string));
  75. }
  76. /* Read-write semaphore write function */
  77. static ssize_t read_dummy_rwsem_write(struct file *file, char __user *user_buf,
  78. size_t count, loff_t *ppos)
  79. {
  80. /* Check if data is already read */
  81. if (*ppos >= sizeof(dummy_string))
  82. return 0;
  83. /* Acquires exclusive write lock, blocking all other readers and writers */
  84. down_write(&dummy_rwsem);
  85. /* Sleeps here, potentially triggering hung task detection if lock is held too long */
  86. msleep_interruptible(SLEEP_SECOND * 1000);
  87. up_write(&dummy_rwsem);
  88. return simple_read_from_buffer(user_buf, count, ppos, dummy_string,
  89. sizeof(dummy_string));
  90. }
  91. /* File operations for mutex */
  92. static const struct file_operations hung_task_mutex_fops = {
  93. .read = read_dummy_mutex,
  94. };
  95. /* File operations for semaphore */
  96. static const struct file_operations hung_task_sem_fops = {
  97. .read = read_dummy_semaphore,
  98. };
  99. /* File operations for rw_semaphore read */
  100. static const struct file_operations hung_task_rwsem_read_fops = {
  101. .read = read_dummy_rwsem_read,
  102. };
  103. /* File operations for rw_semaphore write */
  104. static const struct file_operations hung_task_rwsem_write_fops = {
  105. .read = read_dummy_rwsem_write,
  106. };
  107. static int __init hung_task_tests_init(void)
  108. {
  109. hung_task_dir = debugfs_create_dir(HUNG_TASK_DIR, NULL);
  110. if (IS_ERR(hung_task_dir))
  111. return PTR_ERR(hung_task_dir);
  112. /* Create debugfs files for mutex and semaphore tests */
  113. debugfs_create_file(HUNG_TASK_MUTEX_FILE, 0400, hung_task_dir, NULL,
  114. &hung_task_mutex_fops);
  115. debugfs_create_file(HUNG_TASK_SEM_FILE, 0400, hung_task_dir, NULL,
  116. &hung_task_sem_fops);
  117. debugfs_create_file(HUNG_TASK_RWSEM_READ_FILE, 0400, hung_task_dir, NULL,
  118. &hung_task_rwsem_read_fops);
  119. debugfs_create_file(HUNG_TASK_RWSEM_WRITE_FILE, 0400, hung_task_dir, NULL,
  120. &hung_task_rwsem_write_fops);
  121. return 0;
  122. }
  123. static void __exit hung_task_tests_exit(void)
  124. {
  125. debugfs_remove_recursive(hung_task_dir);
  126. }
  127. module_init(hung_task_tests_init);
  128. module_exit(hung_task_tests_exit);
  129. MODULE_LICENSE("GPL");
  130. MODULE_AUTHOR("Masami Hiramatsu <mhiramat@kernel.org>");
  131. MODULE_AUTHOR("Zi Li <amaindex@outlook.com>");
  132. MODULE_DESCRIPTION("Simple sleep under lock files for testing hung task");