ocfs2: Make the left masklogs compat.
[taoma-kernel.git] / kernel / rtmutex-tester.c
blob66cb89bc5ef1203f08b5d17d140b0786a8f80bbd
1 /*
2 * RT-Mutex-tester: scriptable tester for rt mutexes
4 * started by Thomas Gleixner:
6 * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
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>
18 #include "rtmutex.h"
20 #define MAX_RT_TEST_THREADS 8
21 #define MAX_RT_TEST_MUTEXES 8
23 static spinlock_t rttest_lock;
24 static atomic_t rttest_event;
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;
35 static struct test_thread_data thread_data[MAX_RT_TEST_THREADS];
36 static struct task_struct *threads[MAX_RT_TEST_THREADS];
37 static struct rt_mutex mutexes[MAX_RT_TEST_MUTEXES];
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 */
49 RTTEST_LOCKBKL, /* 9 Lock BKL */
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 */
56 static int handle_op(struct test_thread_data *td, int lockwakeup)
58 int i, id, ret = -EINVAL;
60 switch(td->opcode) {
62 case RTTEST_NOP:
63 return 0;
65 case RTTEST_LOCKCONT:
66 td->mutexes[td->opdata] = 1;
67 td->event = atomic_add_return(1, &rttest_event);
68 return 0;
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;
78 if (!lockwakeup && td->bkl == 4) {
79 #ifdef CONFIG_LOCK_KERNEL
80 unlock_kernel();
81 #endif
82 td->bkl = 0;
84 return 0;
86 case RTTEST_RESETEVENT:
87 atomic_set(&rttest_event, 0);
88 return 0;
90 default:
91 if (lockwakeup)
92 return ret;
95 switch(td->opcode) {
97 case RTTEST_LOCK:
98 case RTTEST_LOCKNOWAIT:
99 id = td->opdata;
100 if (id < 0 || id >= MAX_RT_TEST_MUTEXES)
101 return ret;
103 td->mutexes[id] = 1;
104 td->event = atomic_add_return(1, &rttest_event);
105 rt_mutex_lock(&mutexes[id]);
106 td->event = atomic_add_return(1, &rttest_event);
107 td->mutexes[id] = 4;
108 return 0;
110 case RTTEST_LOCKINT:
111 case RTTEST_LOCKINTNOWAIT:
112 id = td->opdata;
113 if (id < 0 || id >= MAX_RT_TEST_MUTEXES)
114 return ret;
116 td->mutexes[id] = 1;
117 td->event = atomic_add_return(1, &rttest_event);
118 ret = rt_mutex_lock_interruptible(&mutexes[id], 0);
119 td->event = atomic_add_return(1, &rttest_event);
120 td->mutexes[id] = ret ? 0 : 4;
121 return ret ? -EINTR : 0;
123 case RTTEST_UNLOCK:
124 id = td->opdata;
125 if (id < 0 || id >= MAX_RT_TEST_MUTEXES || td->mutexes[id] != 4)
126 return ret;
128 td->event = atomic_add_return(1, &rttest_event);
129 rt_mutex_unlock(&mutexes[id]);
130 td->event = atomic_add_return(1, &rttest_event);
131 td->mutexes[id] = 0;
132 return 0;
134 case RTTEST_LOCKBKL:
135 if (td->bkl)
136 return 0;
137 td->bkl = 1;
138 #ifdef CONFIG_LOCK_KERNEL
139 lock_kernel();
140 #endif
141 td->bkl = 4;
142 return 0;
144 case RTTEST_UNLOCKBKL:
145 if (td->bkl != 4)
146 break;
147 #ifdef CONFIG_LOCK_KERNEL
148 unlock_kernel();
149 #endif
150 td->bkl = 0;
151 return 0;
153 default:
154 break;
156 return ret;
160 * Schedule replacement for rtsem_down(). Only called for threads with
161 * PF_MUTEX_TESTER set.
163 * This allows us to have finegrained control over the event flow.
166 void schedule_rt_mutex_test(struct rt_mutex *mutex)
168 int tid, op, dat;
169 struct test_thread_data *td;
171 /* We have to lookup the task */
172 for (tid = 0; tid < MAX_RT_TEST_THREADS; tid++) {
173 if (threads[tid] == current)
174 break;
177 BUG_ON(tid == MAX_RT_TEST_THREADS);
179 td = &thread_data[tid];
181 op = td->opcode;
182 dat = td->opdata;
184 switch (op) {
185 case RTTEST_LOCK:
186 case RTTEST_LOCKINT:
187 case RTTEST_LOCKNOWAIT:
188 case RTTEST_LOCKINTNOWAIT:
189 if (mutex != &mutexes[dat])
190 break;
192 if (td->mutexes[dat] != 1)
193 break;
195 td->mutexes[dat] = 2;
196 td->event = atomic_add_return(1, &rttest_event);
197 break;
199 case RTTEST_LOCKBKL:
200 default:
201 break;
204 schedule();
207 switch (op) {
208 case RTTEST_LOCK:
209 case RTTEST_LOCKINT:
210 if (mutex != &mutexes[dat])
211 return;
213 if (td->mutexes[dat] != 2)
214 return;
216 td->mutexes[dat] = 3;
217 td->event = atomic_add_return(1, &rttest_event);
218 break;
220 case RTTEST_LOCKNOWAIT:
221 case RTTEST_LOCKINTNOWAIT:
222 if (mutex != &mutexes[dat])
223 return;
225 if (td->mutexes[dat] != 2)
226 return;
228 td->mutexes[dat] = 1;
229 td->event = atomic_add_return(1, &rttest_event);
230 return;
232 case RTTEST_LOCKBKL:
233 return;
234 default:
235 return;
238 td->opcode = 0;
240 for (;;) {
241 set_current_state(TASK_INTERRUPTIBLE);
243 if (td->opcode > 0) {
244 int ret;
246 set_current_state(TASK_RUNNING);
247 ret = handle_op(td, 1);
248 set_current_state(TASK_INTERRUPTIBLE);
249 if (td->opcode == RTTEST_LOCKCONT)
250 break;
251 td->opcode = ret;
254 /* Wait for the next command to be executed */
255 schedule();
258 /* Restore previous command and data */
259 td->opcode = op;
260 td->opdata = dat;
263 static int test_func(void *data)
265 struct test_thread_data *td = data;
266 int ret;
268 current->flags |= PF_MUTEX_TESTER;
269 set_freezable();
270 allow_signal(SIGHUP);
272 for(;;) {
274 set_current_state(TASK_INTERRUPTIBLE);
276 if (td->opcode > 0) {
277 set_current_state(TASK_RUNNING);
278 ret = handle_op(td, 0);
279 set_current_state(TASK_INTERRUPTIBLE);
280 td->opcode = ret;
283 /* Wait for the next command to be executed */
284 schedule();
285 try_to_freeze();
287 if (signal_pending(current))
288 flush_signals(current);
290 if(kthread_should_stop())
291 break;
293 return 0;
297 * sysfs_test_command - interface for test commands
298 * @dev: thread reference
299 * @buf: command for actual step
300 * @count: length of buffer
302 * command syntax:
304 * opcode:data
306 static ssize_t sysfs_test_command(struct sys_device *dev, struct sysdev_attribute *attr,
307 const char *buf, size_t count)
309 struct sched_param schedpar;
310 struct test_thread_data *td;
311 char cmdbuf[32];
312 int op, dat, tid, ret;
314 td = container_of(dev, struct test_thread_data, sysdev);
315 tid = td->sysdev.id;
317 /* strings from sysfs write are not 0 terminated! */
318 if (count >= sizeof(cmdbuf))
319 return -EINVAL;
321 /* strip of \n: */
322 if (buf[count-1] == '\n')
323 count--;
324 if (count < 1)
325 return -EINVAL;
327 memcpy(cmdbuf, buf, count);
328 cmdbuf[count] = 0;
330 if (sscanf(cmdbuf, "%d:%d", &op, &dat) != 2)
331 return -EINVAL;
333 switch (op) {
334 case RTTEST_SCHEDOT:
335 schedpar.sched_priority = 0;
336 ret = sched_setscheduler(threads[tid], SCHED_NORMAL, &schedpar);
337 if (ret)
338 return ret;
339 set_user_nice(current, 0);
340 break;
342 case RTTEST_SCHEDRT:
343 schedpar.sched_priority = dat;
344 ret = sched_setscheduler(threads[tid], SCHED_FIFO, &schedpar);
345 if (ret)
346 return ret;
347 break;
349 case RTTEST_SIGNAL:
350 send_sig(SIGHUP, threads[tid], 0);
351 break;
353 default:
354 if (td->opcode > 0)
355 return -EBUSY;
356 td->opdata = dat;
357 td->opcode = op;
358 wake_up_process(threads[tid]);
361 return count;
365 * sysfs_test_status - sysfs interface for rt tester
366 * @dev: thread to query
367 * @buf: char buffer to be filled with thread status info
369 static ssize_t sysfs_test_status(struct sys_device *dev, struct sysdev_attribute *attr,
370 char *buf)
372 struct test_thread_data *td;
373 struct task_struct *tsk;
374 char *curr = buf;
375 int i;
377 td = container_of(dev, struct test_thread_data, sysdev);
378 tsk = threads[td->sysdev.id];
380 spin_lock(&rttest_lock);
382 curr += sprintf(curr,
383 "O: %4d, E:%8d, S: 0x%08lx, P: %4d, N: %4d, B: %p, K: %d, M:",
384 td->opcode, td->event, tsk->state,
385 (MAX_RT_PRIO - 1) - tsk->prio,
386 (MAX_RT_PRIO - 1) - tsk->normal_prio,
387 tsk->pi_blocked_on, td->bkl);
389 for (i = MAX_RT_TEST_MUTEXES - 1; i >=0 ; i--)
390 curr += sprintf(curr, "%d", td->mutexes[i]);
392 spin_unlock(&rttest_lock);
394 curr += sprintf(curr, ", T: %p, R: %p\n", tsk,
395 mutexes[td->sysdev.id].owner);
397 return curr - buf;
400 static SYSDEV_ATTR(status, 0600, sysfs_test_status, NULL);
401 static SYSDEV_ATTR(command, 0600, NULL, sysfs_test_command);
403 static struct sysdev_class rttest_sysclass = {
404 .name = "rttest",
407 static int init_test_thread(int id)
409 thread_data[id].sysdev.cls = &rttest_sysclass;
410 thread_data[id].sysdev.id = id;
412 threads[id] = kthread_run(test_func, &thread_data[id], "rt-test-%d", id);
413 if (IS_ERR(threads[id]))
414 return PTR_ERR(threads[id]);
416 return sysdev_register(&thread_data[id].sysdev);
419 static int init_rttest(void)
421 int ret, i;
423 spin_lock_init(&rttest_lock);
425 for (i = 0; i < MAX_RT_TEST_MUTEXES; i++)
426 rt_mutex_init(&mutexes[i]);
428 ret = sysdev_class_register(&rttest_sysclass);
429 if (ret)
430 return ret;
432 for (i = 0; i < MAX_RT_TEST_THREADS; i++) {
433 ret = init_test_thread(i);
434 if (ret)
435 break;
436 ret = sysdev_create_file(&thread_data[i].sysdev, &attr_status);
437 if (ret)
438 break;
439 ret = sysdev_create_file(&thread_data[i].sysdev, &attr_command);
440 if (ret)
441 break;
444 printk("Initializing RT-Tester: %s\n", ret ? "Failed" : "OK" );
446 return ret;
449 device_initcall(init_rttest);