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