Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / mips / kernel / spinlock_test.c
blobeaed550e79a2efe7ee34c8587257704b3361c016
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/kthread.h>
4 #include <linux/hrtimer.h>
5 #include <linux/fs.h>
6 #include <linux/debugfs.h>
7 #include <linux/export.h>
8 #include <linux/spinlock.h>
9 #include <asm/debug.h>
11 static int ss_get(void *data, u64 *val)
13 ktime_t start, finish;
14 int loops;
15 int cont;
16 DEFINE_RAW_SPINLOCK(ss_spin);
18 loops = 1000000;
19 cont = 1;
21 start = ktime_get();
23 while (cont) {
24 raw_spin_lock(&ss_spin);
25 loops--;
26 if (loops == 0)
27 cont = 0;
28 raw_spin_unlock(&ss_spin);
31 finish = ktime_get();
33 *val = ktime_us_delta(finish, start);
35 return 0;
38 DEFINE_SIMPLE_ATTRIBUTE(fops_ss, ss_get, NULL, "%llu\n");
42 struct spin_multi_state {
43 raw_spinlock_t lock;
44 atomic_t start_wait;
45 atomic_t enter_wait;
46 atomic_t exit_wait;
47 int loops;
50 struct spin_multi_per_thread {
51 struct spin_multi_state *state;
52 ktime_t start;
55 static int multi_other(void *data)
57 int loops;
58 int cont;
59 struct spin_multi_per_thread *pt = data;
60 struct spin_multi_state *s = pt->state;
62 loops = s->loops;
63 cont = 1;
65 atomic_dec(&s->enter_wait);
67 while (atomic_read(&s->enter_wait))
68 ; /* spin */
70 pt->start = ktime_get();
72 atomic_dec(&s->start_wait);
74 while (atomic_read(&s->start_wait))
75 ; /* spin */
77 while (cont) {
78 raw_spin_lock(&s->lock);
79 loops--;
80 if (loops == 0)
81 cont = 0;
82 raw_spin_unlock(&s->lock);
85 atomic_dec(&s->exit_wait);
86 while (atomic_read(&s->exit_wait))
87 ; /* spin */
88 return 0;
91 static int multi_get(void *data, u64 *val)
93 ktime_t finish;
94 struct spin_multi_state ms;
95 struct spin_multi_per_thread t1, t2;
97 ms.lock = __RAW_SPIN_LOCK_UNLOCKED("multi_get");
98 ms.loops = 1000000;
100 atomic_set(&ms.start_wait, 2);
101 atomic_set(&ms.enter_wait, 2);
102 atomic_set(&ms.exit_wait, 2);
103 t1.state = &ms;
104 t2.state = &ms;
106 kthread_run(multi_other, &t2, "multi_get");
108 multi_other(&t1);
110 finish = ktime_get();
112 *val = ktime_us_delta(finish, t1.start);
114 return 0;
117 DEFINE_SIMPLE_ATTRIBUTE(fops_multi, multi_get, NULL, "%llu\n");
119 static int __init spinlock_test(void)
121 struct dentry *d;
123 if (!mips_debugfs_dir)
124 return -ENODEV;
126 d = debugfs_create_file("spin_single", S_IRUGO,
127 mips_debugfs_dir, NULL,
128 &fops_ss);
129 if (!d)
130 return -ENOMEM;
132 d = debugfs_create_file("spin_multi", S_IRUGO,
133 mips_debugfs_dir, NULL,
134 &fops_multi);
135 if (!d)
136 return -ENOMEM;
138 return 0;
140 device_initcall(spinlock_test);