]> git.proxmox.com Git - mirror_spl-debian.git/blob - modules/splat/splat-atomic.c
- Fix write-only behavior in vn-open()
[mirror_spl-debian.git] / modules / splat / splat-atomic.c
1 #include "splat-internal.h"
2
3 #define SPLAT_SUBSYSTEM_ATOMIC 0x0b00
4 #define SPLAT_ATOMIC_NAME "atomic"
5 #define SPLAT_ATOMIC_DESC "Kernel Atomic Tests"
6
7 #define SPLAT_ATOMIC_TEST1_ID 0x0b01
8 #define SPLAT_ATOMIC_TEST1_NAME "64-bit"
9 #define SPLAT_ATOMIC_TEST1_DESC "Validate 64-bit atomic ops"
10
11 #define SPLAT_ATOMIC_TEST_MAGIC 0x43435454UL
12 #define SPLAT_ATOMIC_INIT_VALUE 10000000UL
13
14 typedef enum {
15 SPLAT_ATOMIC_INC_64 = 0,
16 SPLAT_ATOMIC_DEC_64 = 1,
17 SPLAT_ATOMIC_ADD_64 = 2,
18 SPLAT_ATOMIC_SUB_64 = 3,
19 SPLAT_ATOMIC_ADD_64_NV = 4,
20 SPLAT_ATOMIC_SUB_64_NV = 5,
21 SPLAT_ATOMIC_COUNT_64 = 6
22 } atomic_op_t;
23
24 typedef struct atomic_priv {
25 unsigned long ap_magic;
26 struct file *ap_file;
27 spinlock_t ap_lock;
28 wait_queue_head_t ap_waitq;
29 volatile uint64_t ap_atomic;
30 volatile uint64_t ap_atomic_exited;
31 atomic_op_t ap_op;
32
33 } atomic_priv_t;
34
35 static void
36 splat_atomic_work(void *priv)
37 {
38 atomic_priv_t *ap;
39 atomic_op_t op;
40 int i;
41
42 ap = (atomic_priv_t *)priv;
43 ASSERT(ap->ap_magic == SPLAT_ATOMIC_TEST_MAGIC);
44
45 spin_lock(&ap->ap_lock);
46 op = ap->ap_op;
47 wake_up(&ap->ap_waitq);
48 spin_unlock(&ap->ap_lock);
49
50 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
51 "Thread %d successfully started: %lu/%lu\n", op,
52 (long unsigned)ap->ap_atomic,
53 (long unsigned)ap->ap_atomic_exited);
54
55 for (i = 0; i < SPLAT_ATOMIC_INIT_VALUE / 10; i++) {
56
57 /* Periodically sleep to mix up the ordering */
58 if ((i % (SPLAT_ATOMIC_INIT_VALUE / 100)) == 0) {
59 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
60 "Thread %d sleeping: %lu/%lu\n", op,
61 (long unsigned)ap->ap_atomic,
62 (long unsigned)ap->ap_atomic_exited);
63 set_current_state(TASK_INTERRUPTIBLE);
64 schedule_timeout(HZ / 100);
65 }
66
67 switch (op) {
68 case SPLAT_ATOMIC_INC_64:
69 atomic_inc_64(&ap->ap_atomic);
70 break;
71 case SPLAT_ATOMIC_DEC_64:
72 atomic_dec_64(&ap->ap_atomic);
73 break;
74 case SPLAT_ATOMIC_ADD_64:
75 atomic_add_64(&ap->ap_atomic, 3);
76 break;
77 case SPLAT_ATOMIC_SUB_64:
78 atomic_sub_64(&ap->ap_atomic, 3);
79 break;
80 case SPLAT_ATOMIC_ADD_64_NV:
81 atomic_add_64_nv(&ap->ap_atomic, 5);
82 break;
83 case SPLAT_ATOMIC_SUB_64_NV:
84 atomic_sub_64_nv(&ap->ap_atomic, 5);
85 break;
86 default:
87 BUG_ON(1);
88 }
89 }
90
91 atomic_inc_64(&ap->ap_atomic_exited);
92
93 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
94 "Thread %d successfully exited: %lu/%lu\n", op,
95 (long unsigned)ap->ap_atomic,
96 (long unsigned)ap->ap_atomic_exited);
97
98 wake_up(&ap->ap_waitq);
99 thread_exit();
100 }
101
102 static int
103 splat_atomic_test1_cond(atomic_priv_t *ap)
104 {
105 return (ap->ap_atomic_exited == SPLAT_ATOMIC_COUNT_64);
106 }
107
108 static int
109 splat_atomic_test1(struct file *file, void *arg)
110 {
111 atomic_priv_t ap;
112 DEFINE_WAIT(wait);
113 kthread_t *thr;
114 int i;
115
116 ap.ap_magic = SPLAT_ATOMIC_TEST_MAGIC;
117 ap.ap_file = file;
118 spin_lock_init(&ap.ap_lock);
119 init_waitqueue_head(&ap.ap_waitq);
120 ap.ap_atomic = SPLAT_ATOMIC_INIT_VALUE;
121 ap.ap_atomic_exited = 0;
122
123 for (i = 0; i < SPLAT_ATOMIC_COUNT_64; i++) {
124 spin_lock(&ap.ap_lock);
125 ap.ap_op = i;
126
127 thr = (kthread_t *)thread_create(NULL, 0, splat_atomic_work,
128 &ap, 0, &p0, TS_RUN,
129 minclsyspri);
130 BUG_ON(thr == NULL);
131
132 /* Prepare to wait, the new thread will wake us once it
133 * has made a copy of the unique private passed data */
134 prepare_to_wait(&ap.ap_waitq, &wait, TASK_UNINTERRUPTIBLE);
135 spin_unlock(&ap.ap_lock);
136 schedule();
137 }
138
139 wait_event_interruptible(ap.ap_waitq, splat_atomic_test1_cond(&ap));
140
141 if (ap.ap_atomic != SPLAT_ATOMIC_INIT_VALUE) {
142 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
143 "Final value %lu does not match initial value %lu\n",
144 (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
145 return -EINVAL;
146 }
147
148 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
149 "Success initial and final values match, %lu == %lu\n",
150 (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
151
152 return 0;
153 }
154
155 splat_subsystem_t *
156 splat_atomic_init(void)
157 {
158 splat_subsystem_t *sub;
159
160 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
161 if (sub == NULL)
162 return NULL;
163
164 memset(sub, 0, sizeof(*sub));
165 strncpy(sub->desc.name, SPLAT_ATOMIC_NAME, SPLAT_NAME_SIZE);
166 strncpy(sub->desc.desc, SPLAT_ATOMIC_DESC, SPLAT_DESC_SIZE);
167 INIT_LIST_HEAD(&sub->subsystem_list);
168 INIT_LIST_HEAD(&sub->test_list);
169 spin_lock_init(&sub->test_lock);
170 sub->desc.id = SPLAT_SUBSYSTEM_ATOMIC;
171
172 SPLAT_TEST_INIT(sub, SPLAT_ATOMIC_TEST1_NAME, SPLAT_ATOMIC_TEST1_DESC,
173 SPLAT_ATOMIC_TEST1_ID, splat_atomic_test1);
174
175 return sub;
176 }
177
178 void
179 splat_atomic_fini(splat_subsystem_t *sub)
180 {
181 ASSERT(sub);
182 SPLAT_TEST_FINI(sub, SPLAT_ATOMIC_TEST1_ID);
183
184 kfree(sub);
185 }
186
187 int
188 splat_atomic_id(void) {
189 return SPLAT_SUBSYSTEM_ATOMIC;
190 }