rtmutex-tester.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * RT-Mutex-tester: scriptable tester for rt mutexes
  3. *
  4. * started by Thomas Gleixner:
  5. *
  6. * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  7. *
  8. */
  9. #include <linux/kthread.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/sysdev.h>
  14. #include <linux/timer.h>
  15. #include <linux/freezer.h>
  16. #include "rtmutex.h"
  17. #define MAX_RT_TEST_THREADS 8
  18. #define MAX_RT_TEST_MUTEXES 8
  19. static spinlock_t rttest_lock;
  20. static atomic_t rttest_event;
  21. struct test_thread_data {
  22. int opcode;
  23. int opdata;
  24. int mutexes[MAX_RT_TEST_MUTEXES];
  25. int event;
  26. struct sys_device sysdev;
  27. };
  28. static struct test_thread_data thread_data[MAX_RT_TEST_THREADS];
  29. static struct task_struct *threads[MAX_RT_TEST_THREADS];
  30. static struct rt_mutex mutexes[MAX_RT_TEST_MUTEXES];
  31. enum test_opcodes {
  32. RTTEST_NOP = 0,
  33. RTTEST_SCHEDOT, /* 1 Sched other, data = nice */
  34. RTTEST_SCHEDRT, /* 2 Sched fifo, data = prio */
  35. RTTEST_LOCK, /* 3 Lock uninterruptible, data = lockindex */
  36. RTTEST_LOCKNOWAIT, /* 4 Lock uninterruptible no wait in wakeup, data = lockindex */
  37. RTTEST_LOCKINT, /* 5 Lock interruptible, data = lockindex */
  38. RTTEST_LOCKINTNOWAIT, /* 6 Lock interruptible no wait in wakeup, data = lockindex */
  39. RTTEST_LOCKCONT, /* 7 Continue locking after the wakeup delay */
  40. RTTEST_UNLOCK, /* 8 Unlock, data = lockindex */
  41. /* 9, 10 - reserved for BKL commemoration */
  42. RTTEST_SIGNAL = 11, /* 11 Signal other test thread, data = thread id */
  43. RTTEST_RESETEVENT = 98, /* 98 Reset event counter */
  44. RTTEST_RESET = 99, /* 99 Reset all pending operations */
  45. };
  46. static int handle_op(struct test_thread_data *td, int lockwakeup)
  47. {
  48. int i, id, ret = -EINVAL;
  49. switch(td->opcode) {
  50. case RTTEST_NOP:
  51. return 0;
  52. case RTTEST_LOCKCONT:
  53. td->mutexes[td->opdata] = 1;
  54. td->event = atomic_add_return(1, &rttest_event);
  55. return 0;
  56. case RTTEST_RESET:
  57. for (i = 0; i < MAX_RT_TEST_MUTEXES; i++) {
  58. if (td->mutexes[i] == 4) {
  59. rt_mutex_unlock(&mutexes[i]);
  60. td->mutexes[i] = 0;
  61. }
  62. }
  63. return 0;
  64. case RTTEST_RESETEVENT:
  65. atomic_set(&rttest_event, 0);
  66. return 0;
  67. default:
  68. if (lockwakeup)
  69. return ret;
  70. }
  71. switch(td->opcode) {
  72. case RTTEST_LOCK:
  73. case RTTEST_LOCKNOWAIT:
  74. id = td->opdata;
  75. if (id < 0 || id >= MAX_RT_TEST_MUTEXES)
  76. return ret;
  77. td->mutexes[id] = 1;
  78. td->event = atomic_add_return(1, &rttest_event);
  79. rt_mutex_lock(&mutexes[id]);
  80. td->event = atomic_add_return(1, &rttest_event);
  81. td->mutexes[id] = 4;
  82. return 0;
  83. case RTTEST_LOCKINT:
  84. case RTTEST_LOCKINTNOWAIT:
  85. id = td->opdata;
  86. if (id < 0 || id >= MAX_RT_TEST_MUTEXES)
  87. return ret;
  88. td->mutexes[id] = 1;
  89. td->event = atomic_add_return(1, &rttest_event);
  90. ret = rt_mutex_lock_interruptible(&mutexes[id], 0);
  91. td->event = atomic_add_return(1, &rttest_event);
  92. td->mutexes[id] = ret ? 0 : 4;
  93. return ret ? -EINTR : 0;
  94. case RTTEST_UNLOCK:
  95. id = td->opdata;
  96. if (id < 0 || id >= MAX_RT_TEST_MUTEXES || td->mutexes[id] != 4)
  97. return ret;
  98. td->event = atomic_add_return(1, &rttest_event);
  99. rt_mutex_unlock(&mutexes[id]);
  100. td->event = atomic_add_return(1, &rttest_event);
  101. td->mutexes[id] = 0;
  102. return 0;
  103. default:
  104. break;
  105. }
  106. return ret;
  107. }
  108. /*
  109. * Schedule replacement for rtsem_down(). Only called for threads with
  110. * PF_MUTEX_TESTER set.
  111. *
  112. * This allows us to have finegrained control over the event flow.
  113. *
  114. */
  115. void schedule_rt_mutex_test(struct rt_mutex *mutex)
  116. {
  117. int tid, op, dat;
  118. struct test_thread_data *td;
  119. /* We have to lookup the task */
  120. for (tid = 0; tid < MAX_RT_TEST_THREADS; tid++) {
  121. if (threads[tid] == current)
  122. break;
  123. }
  124. BUG_ON(tid == MAX_RT_TEST_THREADS);
  125. td = &thread_data[tid];
  126. op = td->opcode;
  127. dat = td->opdata;
  128. switch (op) {
  129. case RTTEST_LOCK:
  130. case RTTEST_LOCKINT:
  131. case RTTEST_LOCKNOWAIT:
  132. case RTTEST_LOCKINTNOWAIT:
  133. if (mutex != &mutexes[dat])
  134. break;
  135. if (td->mutexes[dat] != 1)
  136. break;
  137. td->mutexes[dat] = 2;
  138. td->event = atomic_add_return(1, &rttest_event);
  139. break;
  140. default:
  141. break;
  142. }
  143. schedule();
  144. switch (op) {
  145. case RTTEST_LOCK:
  146. case RTTEST_LOCKINT:
  147. if (mutex != &mutexes[dat])
  148. return;
  149. if (td->mutexes[dat] != 2)
  150. return;
  151. td->mutexes[dat] = 3;
  152. td->event = atomic_add_return(1, &rttest_event);
  153. break;
  154. case RTTEST_LOCKNOWAIT:
  155. case RTTEST_LOCKINTNOWAIT:
  156. if (mutex != &mutexes[dat])
  157. return;
  158. if (td->mutexes[dat] != 2)
  159. return;
  160. td->mutexes[dat] = 1;
  161. td->event = atomic_add_return(1, &rttest_event);
  162. return;
  163. default:
  164. return;
  165. }
  166. td->opcode = 0;
  167. for (;;) {
  168. set_current_state(TASK_INTERRUPTIBLE);
  169. if (td->opcode > 0) {
  170. int ret;
  171. set_current_state(TASK_RUNNING);
  172. ret = handle_op(td, 1);
  173. set_current_state(TASK_INTERRUPTIBLE);
  174. if (td->opcode == RTTEST_LOCKCONT)
  175. break;
  176. td->opcode = ret;
  177. }
  178. /* Wait for the next command to be executed */
  179. schedule();
  180. }
  181. /* Restore previous command and data */
  182. td->opcode = op;
  183. td->opdata = dat;
  184. }
  185. static int test_func(void *data)
  186. {
  187. struct test_thread_data *td = data;
  188. int ret;
  189. current->flags |= PF_MUTEX_TESTER;
  190. set_freezable();
  191. allow_signal(SIGHUP);
  192. for(;;) {
  193. set_current_state(TASK_INTERRUPTIBLE);
  194. if (td->opcode > 0) {
  195. set_current_state(TASK_RUNNING);
  196. ret = handle_op(td, 0);
  197. set_current_state(TASK_INTERRUPTIBLE);
  198. td->opcode = ret;
  199. }
  200. /* Wait for the next command to be executed */
  201. schedule();
  202. try_to_freeze();
  203. if (signal_pending(current))
  204. flush_signals(current);
  205. if(kthread_should_stop())
  206. break;
  207. }
  208. return 0;
  209. }
  210. /**
  211. * sysfs_test_command - interface for test commands
  212. * @dev: thread reference
  213. * @buf: command for actual step
  214. * @count: length of buffer
  215. *
  216. * command syntax:
  217. *
  218. * opcode:data
  219. */
  220. static ssize_t sysfs_test_command(struct sys_device *dev, struct sysdev_attribute *attr,
  221. const char *buf, size_t count)
  222. {
  223. struct sched_param schedpar;
  224. struct test_thread_data *td;
  225. char cmdbuf[32];
  226. int op, dat, tid, ret;
  227. td = container_of(dev, struct test_thread_data, sysdev);
  228. tid = td->sysdev.id;
  229. /* strings from sysfs write are not 0 terminated! */
  230. if (count >= sizeof(cmdbuf))
  231. return -EINVAL;
  232. /* strip of \n: */
  233. if (buf[count-1] == '\n')
  234. count--;
  235. if (count < 1)
  236. return -EINVAL;
  237. memcpy(cmdbuf, buf, count);
  238. cmdbuf[count] = 0;
  239. if (sscanf(cmdbuf, "%d:%d", &op, &dat) != 2)
  240. return -EINVAL;
  241. switch (op) {
  242. case RTTEST_SCHEDOT:
  243. schedpar.sched_priority = 0;
  244. ret = sched_setscheduler(threads[tid], SCHED_NORMAL, &schedpar);
  245. if (ret)
  246. return ret;
  247. set_user_nice(current, 0);
  248. break;
  249. case RTTEST_SCHEDRT:
  250. schedpar.sched_priority = dat;
  251. ret = sched_setscheduler(threads[tid], SCHED_FIFO, &schedpar);
  252. if (ret)
  253. return ret;
  254. break;
  255. case RTTEST_SIGNAL:
  256. send_sig(SIGHUP, threads[tid], 0);
  257. break;
  258. default:
  259. if (td->opcode > 0)
  260. return -EBUSY;
  261. td->opdata = dat;
  262. td->opcode = op;
  263. wake_up_process(threads[tid]);
  264. }
  265. return count;
  266. }
  267. /**
  268. * sysfs_test_status - sysfs interface for rt tester
  269. * @dev: thread to query
  270. * @buf: char buffer to be filled with thread status info
  271. */
  272. static ssize_t sysfs_test_status(struct sys_device *dev, struct sysdev_attribute *attr,
  273. char *buf)
  274. {
  275. struct test_thread_data *td;
  276. struct task_struct *tsk;
  277. char *curr = buf;
  278. int i;
  279. td = container_of(dev, struct test_thread_data, sysdev);
  280. tsk = threads[td->sysdev.id];
  281. spin_lock(&rttest_lock);
  282. curr += sprintf(curr,
  283. "O: %4d, E:%8d, S: 0x%08lx, P: %4d, N: %4d, B: %p, M:",
  284. td->opcode, td->event, tsk->state,
  285. (MAX_RT_PRIO - 1) - tsk->prio,
  286. (MAX_RT_PRIO - 1) - tsk->normal_prio,
  287. tsk->pi_blocked_on);
  288. for (i = MAX_RT_TEST_MUTEXES - 1; i >=0 ; i--)
  289. curr += sprintf(curr, "%d", td->mutexes[i]);
  290. spin_unlock(&rttest_lock);
  291. curr += sprintf(curr, ", T: %p, R: %p\n", tsk,
  292. mutexes[td->sysdev.id].owner);
  293. return curr - buf;
  294. }
  295. static SYSDEV_ATTR(status, 0600, sysfs_test_status, NULL);
  296. static SYSDEV_ATTR(command, 0600, NULL, sysfs_test_command);
  297. static struct sysdev_class rttest_sysclass = {
  298. .name = "rttest",
  299. };
  300. static int init_test_thread(int id)
  301. {
  302. thread_data[id].sysdev.cls = &rttest_sysclass;
  303. thread_data[id].sysdev.id = id;
  304. threads[id] = kthread_run(test_func, &thread_data[id], "rt-test-%d", id);
  305. if (IS_ERR(threads[id]))
  306. return PTR_ERR(threads[id]);
  307. return sysdev_register(&thread_data[id].sysdev);
  308. }
  309. static int init_rttest(void)
  310. {
  311. int ret, i;
  312. spin_lock_init(&rttest_lock);
  313. for (i = 0; i < MAX_RT_TEST_MUTEXES; i++)
  314. rt_mutex_init(&mutexes[i]);
  315. ret = sysdev_class_register(&rttest_sysclass);
  316. if (ret)
  317. return ret;
  318. for (i = 0; i < MAX_RT_TEST_THREADS; i++) {
  319. ret = init_test_thread(i);
  320. if (ret)
  321. break;
  322. ret = sysdev_create_file(&thread_data[i].sysdev, &attr_status);
  323. if (ret)
  324. break;
  325. ret = sysdev_create_file(&thread_data[i].sysdev, &attr_command);
  326. if (ret)
  327. break;
  328. }
  329. printk("Initializing RT-Tester: %s\n", ret ? "Failed" : "OK" );
  330. return ret;
  331. }
  332. device_initcall(init_rttest);