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