rtmutex-tester.c 9.0 KB

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