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