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/device.h>
10 #include <linux/kthread.h>
11 #include <linux/export.h>
12 #include <linux/sched.h>
13 #include <linux/sched/rt.h>
14 #include <linux/spinlock.h>
15 #include <linux/timer.h>
16 #include <linux/freezer.h>
17 #include <linux/stat.h>
21 #define MAX_RT_TEST_THREADS 8
22 #define MAX_RT_TEST_MUTEXES 8
24 static spinlock_t rttest_lock;
25 static atomic_t rttest_event;
27 struct test_thread_data {
30 int mutexes[MAX_RT_TEST_MUTEXES];
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 /* 9, 10 - reserved for BKL commemoration */
50 RTTEST_SIGNAL = 11, /* 11 Signal other test thread, data = thread id */
51 RTTEST_RESETEVENT = 98, /* 98 Reset event counter */
52 RTTEST_RESET = 99, /* 99 Reset all pending operations */
55 static int handle_op(struct test_thread_data *td, int lockwakeup)
57 int i, id, ret = -EINVAL;
65 td->mutexes[td->opdata] = 1;
66 td->event = atomic_add_return(1, &rttest_event);
70 for (i = 0; i < MAX_RT_TEST_MUTEXES; i++) {
71 if (td->mutexes[i] == 4) {
72 rt_mutex_unlock(&mutexes[i]);
78 case RTTEST_RESETEVENT:
79 atomic_set(&rttest_event, 0);
90 case RTTEST_LOCKNOWAIT:
92 if (id < 0 || id >= MAX_RT_TEST_MUTEXES)
96 td->event = atomic_add_return(1, &rttest_event);
97 rt_mutex_lock(&mutexes[id]);
98 td->event = atomic_add_return(1, &rttest_event);
103 case RTTEST_LOCKINTNOWAIT:
105 if (id < 0 || id >= MAX_RT_TEST_MUTEXES)
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;
117 if (id < 0 || id >= MAX_RT_TEST_MUTEXES || td->mutexes[id] != 4)
120 td->event = atomic_add_return(1, &rttest_event);
121 rt_mutex_unlock(&mutexes[id]);
122 td->event = atomic_add_return(1, &rttest_event);
133 * Schedule replacement for rtsem_down(). Only called for threads with
134 * PF_MUTEX_TESTER set.
136 * This allows us to have finegrained control over the event flow.
139 void schedule_rt_mutex_test(struct rt_mutex *mutex)
142 struct test_thread_data *td;
144 /* We have to lookup the task */
145 for (tid = 0; tid < MAX_RT_TEST_THREADS; tid++) {
146 if (threads[tid] == current)
150 BUG_ON(tid == MAX_RT_TEST_THREADS);
152 td = &thread_data[tid];
160 case RTTEST_LOCKNOWAIT:
161 case RTTEST_LOCKINTNOWAIT:
162 if (mutex != &mutexes[dat])
165 if (td->mutexes[dat] != 1)
168 td->mutexes[dat] = 2;
169 td->event = atomic_add_return(1, &rttest_event);
182 if (mutex != &mutexes[dat])
185 if (td->mutexes[dat] != 2)
188 td->mutexes[dat] = 3;
189 td->event = atomic_add_return(1, &rttest_event);
192 case RTTEST_LOCKNOWAIT:
193 case RTTEST_LOCKINTNOWAIT:
194 if (mutex != &mutexes[dat])
197 if (td->mutexes[dat] != 2)
200 td->mutexes[dat] = 1;
201 td->event = atomic_add_return(1, &rttest_event);
211 set_current_state(TASK_INTERRUPTIBLE);
213 if (td->opcode > 0) {
216 set_current_state(TASK_RUNNING);
217 ret = handle_op(td, 1);
218 set_current_state(TASK_INTERRUPTIBLE);
219 if (td->opcode == RTTEST_LOCKCONT)
224 /* Wait for the next command to be executed */
228 /* Restore previous command and data */
233 static int test_func(void *data)
235 struct test_thread_data *td = data;
238 current->flags |= PF_MUTEX_TESTER;
240 allow_signal(SIGHUP);
244 set_current_state(TASK_INTERRUPTIBLE);
246 if (td->opcode > 0) {
247 set_current_state(TASK_RUNNING);
248 ret = handle_op(td, 0);
249 set_current_state(TASK_INTERRUPTIBLE);
253 /* Wait for the next command to be executed */
257 if (signal_pending(current))
258 flush_signals(current);
260 if(kthread_should_stop())
267 * sysfs_test_command - interface for test commands
268 * @dev: thread reference
269 * @buf: command for actual step
270 * @count: length of buffer
276 static ssize_t sysfs_test_command(struct device *dev, struct device_attribute *attr,
277 const char *buf, size_t count)
279 struct sched_param schedpar;
280 struct test_thread_data *td;
282 int op, dat, tid, ret;
284 td = container_of(dev, struct test_thread_data, dev);
287 /* strings from sysfs write are not 0 terminated! */
288 if (count >= sizeof(cmdbuf))
292 if (buf[count-1] == '\n')
297 memcpy(cmdbuf, buf, count);
300 if (sscanf(cmdbuf, "%d:%d", &op, &dat) != 2)
305 schedpar.sched_priority = 0;
306 ret = sched_setscheduler(threads[tid], SCHED_NORMAL, &schedpar);
309 set_user_nice(current, 0);
313 schedpar.sched_priority = dat;
314 ret = sched_setscheduler(threads[tid], SCHED_FIFO, &schedpar);
320 send_sig(SIGHUP, threads[tid], 0);
328 wake_up_process(threads[tid]);
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
339 static ssize_t sysfs_test_status(struct device *dev, struct device_attribute *attr,
342 struct test_thread_data *td;
343 struct task_struct *tsk;
347 td = container_of(dev, struct test_thread_data, dev);
348 tsk = threads[td->dev.id];
350 spin_lock(&rttest_lock);
352 curr += sprintf(curr,
353 "O: %4d, E:%8d, S: 0x%08lx, P: %4d, N: %4d, B: %p, M:",
354 td->opcode, td->event, tsk->state,
355 (MAX_RT_PRIO - 1) - tsk->prio,
356 (MAX_RT_PRIO - 1) - tsk->normal_prio,
359 for (i = MAX_RT_TEST_MUTEXES - 1; i >=0 ; i--)
360 curr += sprintf(curr, "%d", td->mutexes[i]);
362 spin_unlock(&rttest_lock);
364 curr += sprintf(curr, ", T: %p, R: %p\n", tsk,
365 mutexes[td->dev.id].owner);
370 static DEVICE_ATTR(status, S_IRUSR, sysfs_test_status, NULL);
371 static DEVICE_ATTR(command, S_IWUSR, NULL, sysfs_test_command);
373 static struct bus_type rttest_subsys = {
375 .dev_name = "rttest",
378 static int init_test_thread(int id)
380 thread_data[id].dev.bus = &rttest_subsys;
381 thread_data[id].dev.id = id;
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]);
387 return device_register(&thread_data[id].dev);
390 static int init_rttest(void)
394 spin_lock_init(&rttest_lock);
396 for (i = 0; i < MAX_RT_TEST_MUTEXES; i++)
397 rt_mutex_init(&mutexes[i]);
399 ret = subsys_system_register(&rttest_subsys, NULL);
403 for (i = 0; i < MAX_RT_TEST_THREADS; i++) {
404 ret = init_test_thread(i);
407 ret = device_create_file(&thread_data[i].dev, &dev_attr_status);
410 ret = device_create_file(&thread_data[i].dev, &dev_attr_command);
415 printk("Initializing RT-Tester: %s\n", ret ? "Failed" : "OK" );
420 device_initcall(init_rttest);