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>
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>
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
{
29 int mutexes
[MAX_RT_TEST_MUTEXES
];
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
];
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
;
66 td
->mutexes
[td
->opdata
] = 1;
67 td
->event
= atomic_add_return(1, &rttest_event
);
71 for (i
= 0; i
< MAX_RT_TEST_MUTEXES
; i
++) {
72 if (td
->mutexes
[i
] == 4) {
73 rt_mutex_unlock(&mutexes
[i
]);
78 if (!lockwakeup
&& td
->bkl
== 4) {
84 case RTTEST_RESETEVENT
:
85 atomic_set(&rttest_event
, 0);
96 case RTTEST_LOCKNOWAIT
:
98 if (id
< 0 || id
>= MAX_RT_TEST_MUTEXES
)
102 td
->event
= atomic_add_return(1, &rttest_event
);
103 rt_mutex_lock(&mutexes
[id
]);
104 td
->event
= atomic_add_return(1, &rttest_event
);
109 case RTTEST_LOCKINTNOWAIT
:
111 if (id
< 0 || id
>= MAX_RT_TEST_MUTEXES
)
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;
123 if (id
< 0 || id
>= MAX_RT_TEST_MUTEXES
|| td
->mutexes
[id
] != 4)
126 td
->event
= atomic_add_return(1, &rttest_event
);
127 rt_mutex_unlock(&mutexes
[id
]);
128 td
->event
= atomic_add_return(1, &rttest_event
);
140 case RTTEST_UNLOCKBKL
:
154 * Schedule replacement for rtsem_down(). Only called for threads with
155 * PF_MUTEX_TESTER set.
157 * This allows us to have finegrained control over the event flow.
160 void schedule_rt_mutex_test(struct rt_mutex
*mutex
)
163 struct test_thread_data
*td
;
165 /* We have to lookup the task */
166 for (tid
= 0; tid
< MAX_RT_TEST_THREADS
; tid
++) {
167 if (threads
[tid
] == current
)
171 BUG_ON(tid
== MAX_RT_TEST_THREADS
);
173 td
= &thread_data
[tid
];
181 case RTTEST_LOCKNOWAIT
:
182 case RTTEST_LOCKINTNOWAIT
:
183 if (mutex
!= &mutexes
[dat
])
186 if (td
->mutexes
[dat
] != 1)
189 td
->mutexes
[dat
] = 2;
190 td
->event
= atomic_add_return(1, &rttest_event
);
204 if (mutex
!= &mutexes
[dat
])
207 if (td
->mutexes
[dat
] != 2)
210 td
->mutexes
[dat
] = 3;
211 td
->event
= atomic_add_return(1, &rttest_event
);
214 case RTTEST_LOCKNOWAIT
:
215 case RTTEST_LOCKINTNOWAIT
:
216 if (mutex
!= &mutexes
[dat
])
219 if (td
->mutexes
[dat
] != 2)
222 td
->mutexes
[dat
] = 1;
223 td
->event
= atomic_add_return(1, &rttest_event
);
235 set_current_state(TASK_INTERRUPTIBLE
);
237 if (td
->opcode
> 0) {
240 set_current_state(TASK_RUNNING
);
241 ret
= handle_op(td
, 1);
242 set_current_state(TASK_INTERRUPTIBLE
);
243 if (td
->opcode
== RTTEST_LOCKCONT
)
248 /* Wait for the next command to be executed */
252 /* Restore previous command and data */
257 static int test_func(void *data
)
259 struct test_thread_data
*td
= data
;
262 current
->flags
|= PF_MUTEX_TESTER
;
264 allow_signal(SIGHUP
);
268 set_current_state(TASK_INTERRUPTIBLE
);
270 if (td
->opcode
> 0) {
271 set_current_state(TASK_RUNNING
);
272 ret
= handle_op(td
, 0);
273 set_current_state(TASK_INTERRUPTIBLE
);
277 /* Wait for the next command to be executed */
281 if (signal_pending(current
))
282 flush_signals(current
);
284 if(kthread_should_stop())
291 * sysfs_test_command - interface for test commands
292 * @dev: thread reference
293 * @buf: command for actual step
294 * @count: length of buffer
300 static ssize_t
sysfs_test_command(struct sys_device
*dev
, const char *buf
,
303 struct sched_param schedpar
;
304 struct test_thread_data
*td
;
306 int op
, dat
, tid
, ret
;
308 td
= container_of(dev
, struct test_thread_data
, sysdev
);
311 /* strings from sysfs write are not 0 terminated! */
312 if (count
>= sizeof(cmdbuf
))
316 if (buf
[count
-1] == '\n')
321 memcpy(cmdbuf
, buf
, count
);
324 if (sscanf(cmdbuf
, "%d:%d", &op
, &dat
) != 2)
329 schedpar
.sched_priority
= 0;
330 ret
= sched_setscheduler(threads
[tid
], SCHED_NORMAL
, &schedpar
);
333 set_user_nice(current
, 0);
337 schedpar
.sched_priority
= dat
;
338 ret
= sched_setscheduler(threads
[tid
], SCHED_FIFO
, &schedpar
);
344 send_sig(SIGHUP
, threads
[tid
], 0);
352 wake_up_process(threads
[tid
]);
359 * sysfs_test_status - sysfs interface for rt tester
360 * @dev: thread to query
361 * @buf: char buffer to be filled with thread status info
363 static ssize_t
sysfs_test_status(struct sys_device
*dev
, char *buf
)
365 struct test_thread_data
*td
;
366 struct task_struct
*tsk
;
370 td
= container_of(dev
, struct test_thread_data
, sysdev
);
371 tsk
= threads
[td
->sysdev
.id
];
373 spin_lock(&rttest_lock
);
375 curr
+= sprintf(curr
,
376 "O: %4d, E:%8d, S: 0x%08lx, P: %4d, N: %4d, B: %p, K: %d, M:",
377 td
->opcode
, td
->event
, tsk
->state
,
378 (MAX_RT_PRIO
- 1) - tsk
->prio
,
379 (MAX_RT_PRIO
- 1) - tsk
->normal_prio
,
380 tsk
->pi_blocked_on
, td
->bkl
);
382 for (i
= MAX_RT_TEST_MUTEXES
- 1; i
>=0 ; i
--)
383 curr
+= sprintf(curr
, "%d", td
->mutexes
[i
]);
385 spin_unlock(&rttest_lock
);
387 curr
+= sprintf(curr
, ", T: %p, R: %p\n", tsk
,
388 mutexes
[td
->sysdev
.id
].owner
);
393 static SYSDEV_ATTR(status
, 0600, sysfs_test_status
, NULL
);
394 static SYSDEV_ATTR(command
, 0600, NULL
, sysfs_test_command
);
396 static struct sysdev_class rttest_sysclass
= {
397 set_kset_name("rttest"),
400 static int init_test_thread(int id
)
402 thread_data
[id
].sysdev
.cls
= &rttest_sysclass
;
403 thread_data
[id
].sysdev
.id
= id
;
405 threads
[id
] = kthread_run(test_func
, &thread_data
[id
], "rt-test-%d", id
);
406 if (IS_ERR(threads
[id
]))
407 return PTR_ERR(threads
[id
]);
409 return sysdev_register(&thread_data
[id
].sysdev
);
412 static int init_rttest(void)
416 spin_lock_init(&rttest_lock
);
418 for (i
= 0; i
< MAX_RT_TEST_MUTEXES
; i
++)
419 rt_mutex_init(&mutexes
[i
]);
421 ret
= sysdev_class_register(&rttest_sysclass
);
425 for (i
= 0; i
< MAX_RT_TEST_THREADS
; i
++) {
426 ret
= init_test_thread(i
);
429 ret
= sysdev_create_file(&thread_data
[i
].sysdev
, &attr_status
);
432 ret
= sysdev_create_file(&thread_data
[i
].sysdev
, &attr_command
);
437 printk("Initializing RT-Tester: %s\n", ret
? "Failed" : "OK" );
442 device_initcall(init_rttest
);