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>
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
];
34 static struct test_thread_data thread_data
[MAX_RT_TEST_THREADS
];
35 static struct task_struct
*threads
[MAX_RT_TEST_THREADS
];
36 static struct rt_mutex mutexes
[MAX_RT_TEST_MUTEXES
];
40 RTTEST_SCHEDOT
, /* 1 Sched other, data = nice */
41 RTTEST_SCHEDRT
, /* 2 Sched fifo, data = prio */
42 RTTEST_LOCK
, /* 3 Lock uninterruptible, data = lockindex */
43 RTTEST_LOCKNOWAIT
, /* 4 Lock uninterruptible no wait in wakeup, data = lockindex */
44 RTTEST_LOCKINT
, /* 5 Lock interruptible, data = lockindex */
45 RTTEST_LOCKINTNOWAIT
, /* 6 Lock interruptible no wait in wakeup, data = lockindex */
46 RTTEST_LOCKCONT
, /* 7 Continue locking after the wakeup delay */
47 RTTEST_UNLOCK
, /* 8 Unlock, data = lockindex */
48 /* 9, 10 - reserved for BKL commemoration */
49 RTTEST_SIGNAL
= 11, /* 11 Signal other test thread, data = thread id */
50 RTTEST_RESETEVENT
= 98, /* 98 Reset event counter */
51 RTTEST_RESET
= 99, /* 99 Reset all pending operations */
54 static int handle_op(struct test_thread_data
*td
, int lockwakeup
)
56 int i
, id
, ret
= -EINVAL
;
64 td
->mutexes
[td
->opdata
] = 1;
65 td
->event
= atomic_add_return(1, &rttest_event
);
69 for (i
= 0; i
< MAX_RT_TEST_MUTEXES
; i
++) {
70 if (td
->mutexes
[i
] == 4) {
71 rt_mutex_unlock(&mutexes
[i
]);
77 case RTTEST_RESETEVENT
:
78 atomic_set(&rttest_event
, 0);
89 case RTTEST_LOCKNOWAIT
:
91 if (id
< 0 || id
>= MAX_RT_TEST_MUTEXES
)
95 td
->event
= atomic_add_return(1, &rttest_event
);
96 rt_mutex_lock(&mutexes
[id
]);
97 td
->event
= atomic_add_return(1, &rttest_event
);
102 case RTTEST_LOCKINTNOWAIT
:
104 if (id
< 0 || id
>= MAX_RT_TEST_MUTEXES
)
108 td
->event
= atomic_add_return(1, &rttest_event
);
109 ret
= rt_mutex_lock_interruptible(&mutexes
[id
], 0);
110 td
->event
= atomic_add_return(1, &rttest_event
);
111 td
->mutexes
[id
] = ret
? 0 : 4;
112 return ret
? -EINTR
: 0;
116 if (id
< 0 || id
>= MAX_RT_TEST_MUTEXES
|| td
->mutexes
[id
] != 4)
119 td
->event
= atomic_add_return(1, &rttest_event
);
120 rt_mutex_unlock(&mutexes
[id
]);
121 td
->event
= atomic_add_return(1, &rttest_event
);
132 * Schedule replacement for rtsem_down(). Only called for threads with
133 * PF_MUTEX_TESTER set.
135 * This allows us to have finegrained control over the event flow.
138 void schedule_rt_mutex_test(struct rt_mutex
*mutex
)
141 struct test_thread_data
*td
;
143 /* We have to lookup the task */
144 for (tid
= 0; tid
< MAX_RT_TEST_THREADS
; tid
++) {
145 if (threads
[tid
] == current
)
149 BUG_ON(tid
== MAX_RT_TEST_THREADS
);
151 td
= &thread_data
[tid
];
159 case RTTEST_LOCKNOWAIT
:
160 case RTTEST_LOCKINTNOWAIT
:
161 if (mutex
!= &mutexes
[dat
])
164 if (td
->mutexes
[dat
] != 1)
167 td
->mutexes
[dat
] = 2;
168 td
->event
= atomic_add_return(1, &rttest_event
);
181 if (mutex
!= &mutexes
[dat
])
184 if (td
->mutexes
[dat
] != 2)
187 td
->mutexes
[dat
] = 3;
188 td
->event
= atomic_add_return(1, &rttest_event
);
191 case RTTEST_LOCKNOWAIT
:
192 case RTTEST_LOCKINTNOWAIT
:
193 if (mutex
!= &mutexes
[dat
])
196 if (td
->mutexes
[dat
] != 2)
199 td
->mutexes
[dat
] = 1;
200 td
->event
= atomic_add_return(1, &rttest_event
);
210 set_current_state(TASK_INTERRUPTIBLE
);
212 if (td
->opcode
> 0) {
215 set_current_state(TASK_RUNNING
);
216 ret
= handle_op(td
, 1);
217 set_current_state(TASK_INTERRUPTIBLE
);
218 if (td
->opcode
== RTTEST_LOCKCONT
)
223 /* Wait for the next command to be executed */
227 /* Restore previous command and data */
232 static int test_func(void *data
)
234 struct test_thread_data
*td
= data
;
237 current
->flags
|= PF_MUTEX_TESTER
;
239 allow_signal(SIGHUP
);
243 set_current_state(TASK_INTERRUPTIBLE
);
245 if (td
->opcode
> 0) {
246 set_current_state(TASK_RUNNING
);
247 ret
= handle_op(td
, 0);
248 set_current_state(TASK_INTERRUPTIBLE
);
252 /* Wait for the next command to be executed */
256 if (signal_pending(current
))
257 flush_signals(current
);
259 if(kthread_should_stop())
266 * sysfs_test_command - interface for test commands
267 * @dev: thread reference
268 * @buf: command for actual step
269 * @count: length of buffer
275 static ssize_t
sysfs_test_command(struct device
*dev
, struct device_attribute
*attr
,
276 const char *buf
, size_t count
)
278 struct sched_param schedpar
;
279 struct test_thread_data
*td
;
281 int op
, dat
, tid
, ret
;
283 td
= container_of(dev
, struct test_thread_data
, dev
);
286 /* strings from sysfs write are not 0 terminated! */
287 if (count
>= sizeof(cmdbuf
))
291 if (buf
[count
-1] == '\n')
296 memcpy(cmdbuf
, buf
, count
);
299 if (sscanf(cmdbuf
, "%d:%d", &op
, &dat
) != 2)
304 schedpar
.sched_priority
= 0;
305 ret
= sched_setscheduler(threads
[tid
], SCHED_NORMAL
, &schedpar
);
308 set_user_nice(current
, 0);
312 schedpar
.sched_priority
= dat
;
313 ret
= sched_setscheduler(threads
[tid
], SCHED_FIFO
, &schedpar
);
319 send_sig(SIGHUP
, threads
[tid
], 0);
327 wake_up_process(threads
[tid
]);
334 * sysfs_test_status - sysfs interface for rt tester
335 * @dev: thread to query
336 * @buf: char buffer to be filled with thread status info
338 static ssize_t
sysfs_test_status(struct device
*dev
, struct device_attribute
*attr
,
341 struct test_thread_data
*td
;
342 struct task_struct
*tsk
;
346 td
= container_of(dev
, struct test_thread_data
, dev
);
347 tsk
= threads
[td
->dev
.id
];
349 spin_lock(&rttest_lock
);
351 curr
+= sprintf(curr
,
352 "O: %4d, E:%8d, S: 0x%08lx, P: %4d, N: %4d, B: %p, M:",
353 td
->opcode
, td
->event
, tsk
->state
,
354 (MAX_RT_PRIO
- 1) - tsk
->prio
,
355 (MAX_RT_PRIO
- 1) - tsk
->normal_prio
,
358 for (i
= MAX_RT_TEST_MUTEXES
- 1; i
>=0 ; i
--)
359 curr
+= sprintf(curr
, "%d", td
->mutexes
[i
]);
361 spin_unlock(&rttest_lock
);
363 curr
+= sprintf(curr
, ", T: %p, R: %p\n", tsk
,
364 mutexes
[td
->dev
.id
].owner
);
369 static DEVICE_ATTR(status
, 0600, sysfs_test_status
, NULL
);
370 static DEVICE_ATTR(command
, 0600, NULL
, sysfs_test_command
);
372 static struct bus_type rttest_subsys
= {
374 .dev_name
= "rttest",
377 static int init_test_thread(int id
)
379 thread_data
[id
].dev
.bus
= &rttest_subsys
;
380 thread_data
[id
].dev
.id
= id
;
382 threads
[id
] = kthread_run(test_func
, &thread_data
[id
], "rt-test-%d", id
);
383 if (IS_ERR(threads
[id
]))
384 return PTR_ERR(threads
[id
]);
386 return device_register(&thread_data
[id
].dev
);
389 static int init_rttest(void)
393 spin_lock_init(&rttest_lock
);
395 for (i
= 0; i
< MAX_RT_TEST_MUTEXES
; i
++)
396 rt_mutex_init(&mutexes
[i
]);
398 ret
= subsys_system_register(&rttest_subsys
, NULL
);
402 for (i
= 0; i
< MAX_RT_TEST_THREADS
; i
++) {
403 ret
= init_test_thread(i
);
406 ret
= device_create_file(&thread_data
[i
].dev
, &dev_attr_status
);
409 ret
= device_create_file(&thread_data
[i
].dev
, &dev_attr_command
);
414 printk("Initializing RT-Tester: %s\n", ret
? "Failed" : "OK" );
419 device_initcall(init_rttest
);