aboutsummaryrefslogtreecommitdiffstats
path: root/module/splat/splat-atomic.c
blob: 8aaa0835d604417644ef9264312ae30f8064f715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
 *  Copyright (C) 2007 The Regents of the University of California.
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
 *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
 *  UCRL-CODE-235197
 *
 *  This file is part of the SPL, Solaris Porting Layer.
 *  For details, see <http://zfsonlinux.org/>.
 *
 *  The SPL is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *
 *  The SPL is distributed in the hope that it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************
 *  Solaris Porting LAyer Tests (SPLAT) Atomic Tests.
 */

#include <sys/atomic.h>
#include <sys/thread.h>
#include <sys/mutex.h>
#include <linux/mm_compat.h>
#include <linux/wait_compat.h>
#include <linux/slab.h>
#include "splat-internal.h"

#define SPLAT_ATOMIC_NAME		"atomic"
#define SPLAT_ATOMIC_DESC		"Kernel Atomic Tests"

#define SPLAT_ATOMIC_TEST1_ID		0x0b01
#define SPLAT_ATOMIC_TEST1_NAME		"64-bit"
#define SPLAT_ATOMIC_TEST1_DESC		"Validate 64-bit atomic ops"

#define SPLAT_ATOMIC_TEST_MAGIC		0x43435454UL
#define SPLAT_ATOMIC_INIT_VALUE		10000000UL

typedef enum {
	SPLAT_ATOMIC_INC_64    = 0,
	SPLAT_ATOMIC_DEC_64    = 1,
	SPLAT_ATOMIC_ADD_64    = 2,
	SPLAT_ATOMIC_SUB_64    = 3,
	SPLAT_ATOMIC_ADD_64_NV = 4,
	SPLAT_ATOMIC_SUB_64_NV = 5,
	SPLAT_ATOMIC_COUNT_64  = 6
} atomic_op_t;

typedef struct atomic_priv {
        unsigned long ap_magic;
        struct file *ap_file;
	kmutex_t ap_lock;
        spl_wait_queue_head_t ap_waitq;
	volatile uint64_t ap_atomic;
	volatile uint64_t ap_atomic_exited;
	atomic_op_t ap_op;

} atomic_priv_t;

static void
splat_atomic_work(void *priv)
{
	atomic_priv_t *ap;
	atomic_op_t op;
	int i;

	ap = (atomic_priv_t *)priv;
	ASSERT(ap->ap_magic == SPLAT_ATOMIC_TEST_MAGIC);

	mutex_enter(&ap->ap_lock);
	op = ap->ap_op;
	wake_up(&ap->ap_waitq);
	mutex_exit(&ap->ap_lock);

        splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
	             "Thread %d successfully started: %lu/%lu\n", op,
		     (long unsigned)ap->ap_atomic,
		     (long unsigned)ap->ap_atomic_exited);

	for (i = 0; i < SPLAT_ATOMIC_INIT_VALUE / 10; i++) {

		/* Periodically sleep to mix up the ordering */
		if ((i % (SPLAT_ATOMIC_INIT_VALUE / 100)) == 0) {
		        splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
			     "Thread %d sleeping: %lu/%lu\n", op,
			     (long unsigned)ap->ap_atomic,
			     (long unsigned)ap->ap_atomic_exited);
		        set_current_state(TASK_INTERRUPTIBLE);
			schedule_timeout(HZ / 100);
		}

		switch (op) {
			case SPLAT_ATOMIC_INC_64:
				atomic_inc_64(&ap->ap_atomic);
				break;
			case SPLAT_ATOMIC_DEC_64:
				atomic_dec_64(&ap->ap_atomic);
				break;
			case SPLAT_ATOMIC_ADD_64:
				atomic_add_64(&ap->ap_atomic, 3);
				break;
			case SPLAT_ATOMIC_SUB_64:
				atomic_sub_64(&ap->ap_atomic, 3);
				break;
			case SPLAT_ATOMIC_ADD_64_NV:
				atomic_add_64_nv(&ap->ap_atomic, 5);
				break;
			case SPLAT_ATOMIC_SUB_64_NV:
				atomic_sub_64_nv(&ap->ap_atomic, 5);
				break;
			default:
				PANIC("Undefined op %d\n", op);
		}
	}

	atomic_inc_64(&ap->ap_atomic_exited);

        splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
	             "Thread %d successfully exited: %lu/%lu\n", op,
		     (long unsigned)ap->ap_atomic,
		     (long unsigned)ap->ap_atomic_exited);

	wake_up(&ap->ap_waitq);
	thread_exit();
}

static int
splat_atomic_test1_cond(atomic_priv_t *ap, int started)
{
	return (ap->ap_atomic_exited == started);
}

static int
splat_atomic_test1(struct file *file, void *arg)
{
	atomic_priv_t ap;
        DEFINE_WAIT(wait);
	kthread_t *thr;
	int i, rc = 0;

	ap.ap_magic = SPLAT_ATOMIC_TEST_MAGIC;
	ap.ap_file = file;
	mutex_init(&ap.ap_lock, SPLAT_ATOMIC_TEST1_NAME, MUTEX_DEFAULT, NULL);
	init_waitqueue_head(&ap.ap_waitq);
	ap.ap_atomic = SPLAT_ATOMIC_INIT_VALUE;
	ap.ap_atomic_exited = 0;

	for (i = 0; i < SPLAT_ATOMIC_COUNT_64; i++) {
		mutex_enter(&ap.ap_lock);
		ap.ap_op = i;

		thr = (kthread_t *)thread_create(NULL, 0, splat_atomic_work,
						 &ap, 0, &p0, TS_RUN,
						 defclsyspri);
		if (thr == NULL) {
			rc = -ESRCH;
			mutex_exit(&ap.ap_lock);
			break;
		}

		/* Prepare to wait, the new thread will wake us once it
		 * has made a copy of the unique private passed data */
                prepare_to_wait(&ap.ap_waitq, &wait, TASK_UNINTERRUPTIBLE);
		mutex_exit(&ap.ap_lock);
		schedule();
	}

	wait_event(ap.ap_waitq, splat_atomic_test1_cond(&ap, i));

	if (rc) {
		splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME, "Only started "
			     "%d/%d test threads\n", i, SPLAT_ATOMIC_COUNT_64);
		return rc;
	}

	if (ap.ap_atomic != SPLAT_ATOMIC_INIT_VALUE) {
		splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
			     "Final value %lu does not match initial value %lu\n",
			     (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
		return -EINVAL;
	}

        splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
	           "Success initial and final values match, %lu == %lu\n",
	           (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);

	mutex_destroy(&ap.ap_lock);

	return 0;
}

splat_subsystem_t *
splat_atomic_init(void)
{
        splat_subsystem_t *sub;

        sub = kmalloc(sizeof(*sub), GFP_KERNEL);
        if (sub == NULL)
                return NULL;

        memset(sub, 0, sizeof(*sub));
        strncpy(sub->desc.name, SPLAT_ATOMIC_NAME, SPLAT_NAME_SIZE);
        strncpy(sub->desc.desc, SPLAT_ATOMIC_DESC, SPLAT_DESC_SIZE);
        INIT_LIST_HEAD(&sub->subsystem_list);
        INIT_LIST_HEAD(&sub->test_list);
        spin_lock_init(&sub->test_lock);
        sub->desc.id = SPLAT_SUBSYSTEM_ATOMIC;

        splat_test_init(sub, SPLAT_ATOMIC_TEST1_NAME, SPLAT_ATOMIC_TEST1_DESC,
                      SPLAT_ATOMIC_TEST1_ID, splat_atomic_test1);

        return sub;
}

void
splat_atomic_fini(splat_subsystem_t *sub)
{
        ASSERT(sub);
        splat_test_fini(sub, SPLAT_ATOMIC_TEST1_ID);

        kfree(sub);
}

int
splat_atomic_id(void) {
        return SPLAT_SUBSYSTEM_ATOMIC;
}