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