1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/kthread.h>
4 #include <linux/hrtimer.h>
6 #include <linux/debugfs.h>
7 #include <linux/export.h>
8 #include <linux/spinlock.h>
11 static int ss_get(void *data, u64 *val)
13 ktime_t start, finish;
16 DEFINE_RAW_SPINLOCK(ss_spin);
24 raw_spin_lock(&ss_spin);
28 raw_spin_unlock(&ss_spin);
33 *val = ktime_us_delta(finish, start);
38 DEFINE_SIMPLE_ATTRIBUTE(fops_ss, ss_get, NULL, "%llu\n");
42 struct spin_multi_state {
50 struct spin_multi_per_thread {
51 struct spin_multi_state *state;
55 static int multi_other(void *data)
59 struct spin_multi_per_thread *pt = data;
60 struct spin_multi_state *s = pt->state;
65 atomic_dec(&s->enter_wait);
67 while (atomic_read(&s->enter_wait))
70 pt->start = ktime_get();
72 atomic_dec(&s->start_wait);
74 while (atomic_read(&s->start_wait))
78 raw_spin_lock(&s->lock);
82 raw_spin_unlock(&s->lock);
85 atomic_dec(&s->exit_wait);
86 while (atomic_read(&s->exit_wait))
91 static int multi_get(void *data, u64 *val)
94 struct spin_multi_state ms;
95 struct spin_multi_per_thread t1, t2;
97 ms.lock = __RAW_SPIN_LOCK_UNLOCKED("multi_get");
100 atomic_set(&ms.start_wait, 2);
101 atomic_set(&ms.enter_wait, 2);
102 atomic_set(&ms.exit_wait, 2);
106 kthread_run(multi_other, &t2, "multi_get");
110 finish = ktime_get();
112 *val = ktime_us_delta(finish, t1.start);
117 DEFINE_SIMPLE_ATTRIBUTE(fops_multi, multi_get, NULL, "%llu\n");
119 static int __init spinlock_test(void)
123 if (!mips_debugfs_dir)
126 d = debugfs_create_file("spin_single", S_IRUGO,
127 mips_debugfs_dir, NULL,
132 d = debugfs_create_file("spin_multi", S_IRUGO,
133 mips_debugfs_dir, NULL,
140 device_initcall(spinlock_test);