summaryrefslogtreecommitdiffstats
path: root/module/splat/splat-list.c
blob: 5a610cee4c97f22ce12715d2238b28bce0e74674 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 *  This file is part of the SPL: Solaris Porting Layer.
 *
 *  Copyright (c) 2008 Lawrence Livermore National Security, LLC.
 *  Produced at Lawrence Livermore National Laboratory
 *  Written by:
 *          Brian Behlendorf <behlendorf1@llnl.gov>,
 *          Herb Wartens <wartens2@llnl.gov>,
 *          Jim Garlick <garlick@llnl.gov>
 *  UCRL-CODE-235197
 *
 *  This 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.
 *
 *  This 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 this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
 */

#include "splat-internal.h"

#define SPLAT_SUBSYSTEM_LIST		0x0c00
#define SPLAT_LIST_NAME			"list"
#define SPLAT_LIST_DESC			"Kernel List Tests"

#define SPLAT_LIST_TEST1_ID		0x0c01
#define SPLAT_LIST_TEST1_NAME		"create/destroy"
#define SPLAT_LIST_TEST1_DESC		"Create/destroy Test"

#define SPLAT_LIST_TEST2_ID		0x0c02
#define SPLAT_LIST_TEST2_NAME		"ins/rm head"
#define SPLAT_LIST_TEST2_DESC		"Insert/remove head Test"

#define SPLAT_LIST_TEST3_ID		0x0c03
#define SPLAT_LIST_TEST3_NAME		"ins/rm tail"
#define SPLAT_LIST_TEST3_DESC		"Insert/remove tail Test"

#define SPLAT_LIST_TEST4_ID		0x0c04
#define SPLAT_LIST_TEST4_NAME		"insert_after"
#define SPLAT_LIST_TEST4_DESC		"Insert_after Test"

#define SPLAT_LIST_TEST5_ID		0x0c05
#define SPLAT_LIST_TEST5_NAME		"insert_before"
#define SPLAT_LIST_TEST5_DESC		"Insert_before Test"

#define SPLAT_LIST_TEST6_ID		0x0c06
#define SPLAT_LIST_TEST6_NAME		"remove"
#define SPLAT_LIST_TEST6_DESC		"Remove Test"

#define SPLAT_LIST_TEST7_ID		0x0c7
#define SPLAT_LIST_TEST7_NAME		"active"
#define SPLAT_LIST_TEST7_DESC		"Active Test"

/* It is important that li_node is not the first element, this
 * ensures the list_d2l/list_object macros are working correctly. */
typedef struct list_item {
	int li_data;
	list_node_t li_node;
} list_item_t;

#define LIST_ORDER_STACK		0
#define LIST_ORDER_QUEUE		1

static int
splat_list_test1(struct file *file, void *arg)
{
	list_t list;

	splat_vprint(file, SPLAT_LIST_TEST1_NAME, "Creating list\n%s", "");
	list_create(&list, sizeof(list_item_t), offsetof(list_item_t, li_node));

	if (!list_is_empty(&list)) {
		splat_vprint(file, SPLAT_LIST_TEST1_NAME,
			     "New list NOT empty%s\n", "");
		/* list_destroy() intentionally skipped to avoid assert */
		return -EEXIST;
	}

	splat_vprint(file, SPLAT_LIST_TEST1_NAME, "Destroying list\n%s", "");
	list_destroy(&list);

	/* Validate the list has been destroyed */
	if (list_link_active(&list.list_head)) {
		splat_vprint(file, SPLAT_LIST_TEST1_NAME,
			     "Destroyed list still active%s", "");
		return -EIO;
	}

        return 0;
}

static int
splat_list_validate(list_t *list, int size, int order, int mult)
{
	list_item_t *li;
	int i;

	/* Walk all items in list from head to verify stack or queue
	 * ordering.  We bound the for loop by size+1 to ensure that
	 * we still terminate if there is list corruption.  We also
	 * intentionally make things a little more complex than they
	 * need to be by using list_head/list_next for queues, and
	 * list_tail/list_prev for stacks.  This is simply done for
	 * coverage and to ensure these function are working right.
	 */
	for (i = 0, li = (order ? list_head(list) : list_tail(list));
	     i < size + 1 && li != NULL;
	     i++, li = (order ? list_next(list, li) : list_prev(list, li)))
		if (li->li_data != i * mult)
			return -EIDRM;

	if (i != size)
		return -E2BIG;

	return 0;
}

static int
splat_list_test2(struct file *file, void *arg)
{
	list_t list;
	list_item_t *li;
	int i, list_size = 8, rc = 0;

	splat_vprint(file, SPLAT_LIST_TEST2_NAME, "Creating list\n%s", "");
	list_create(&list, sizeof(list_item_t), offsetof(list_item_t, li_node));

	/* Insert all items at the list head to form a stack */
	splat_vprint(file, SPLAT_LIST_TEST2_NAME,
		     "Adding %d items to list head\n", list_size);
	for (i = 0; i < list_size; i++) {
		li = kmem_alloc(sizeof(list_item_t), KM_SLEEP);
		if (li == NULL) {
			rc = -ENOMEM;
			goto out;
		}

		list_link_init(&li->li_node);
		li->li_data = i;
		list_insert_head(&list, li);
	}

	splat_vprint(file, SPLAT_LIST_TEST2_NAME,
		     "Validating %d item list is a stack\n", list_size);
	rc = splat_list_validate(&list, list_size, LIST_ORDER_STACK, 1);
	if (rc)
		splat_vprint(file, SPLAT_LIST_TEST2_NAME,
			     "List validation failed, %d\n", rc);
out:
	/* Remove all items */
	splat_vprint(file, SPLAT_LIST_TEST2_NAME,
		     "Removing %d items from list head\n", list_size);
	while ((li = list_remove_head(&list)))
		kmem_free(li, sizeof(list_item_t));

	splat_vprint(file, SPLAT_LIST_TEST2_NAME, "Destroying list\n%s", "");
	list_destroy(&list);

        return rc;
}

static int
splat_list_test3(struct file *file, void *arg)
{
	list_t list;
	list_item_t *li;
	int i, list_size = 8, rc = 0;

	splat_vprint(file, SPLAT_LIST_TEST3_NAME, "Creating list\n%s", "");
	list_create(&list, sizeof(list_item_t), offsetof(list_item_t, li_node));

	/* Insert all items at the list tail to form a queue */
	splat_vprint(file, SPLAT_LIST_TEST3_NAME,
		     "Adding %d items to list tail\n", list_size);
	for (i = 0; i < list_size; i++) {
		li = kmem_alloc(sizeof(list_item_t), KM_SLEEP);
		if (li == NULL) {
			rc = -ENOMEM;
			goto out;
		}

		list_link_init(&li->li_node);
		li->li_data = i;
		list_insert_tail(&list, li);
	}

	splat_vprint(file, SPLAT_LIST_TEST3_NAME,
		     "Validating %d item list is a queue\n", list_size);
	rc = splat_list_validate(&list, list_size, LIST_ORDER_QUEUE, 1);
	if (rc)
		splat_vprint(file, SPLAT_LIST_TEST3_NAME,
			     "List validation failed, %d\n", rc);
out:
	/* Remove all items */
	splat_vprint(file, SPLAT_LIST_TEST3_NAME,
		     "Removing %d items from list tail\n", list_size);
	while ((li = list_remove_tail(&list)))
		kmem_free(li, sizeof(list_item_t));

	splat_vprint(file, SPLAT_LIST_TEST3_NAME, "Destroying list\n%s", "");
	list_destroy(&list);

        return rc;
}

static int
splat_list_test4(struct file *file, void *arg)
{
	list_t list;
	list_item_t *li_new, *li_last = NULL;
	int i, list_size = 8, rc = 0;

	splat_vprint(file, SPLAT_LIST_TEST4_NAME, "Creating list\n%s", "");
	list_create(&list, sizeof(list_item_t), offsetof(list_item_t, li_node));

	/* Insert all items after the last item to form a queue */
	splat_vprint(file, SPLAT_LIST_TEST4_NAME,
		     "Adding %d items each after the last item\n", list_size);
	for (i = 0; i < list_size; i++) {
		li_new = kmem_alloc(sizeof(list_item_t), KM_SLEEP);
		if (li_new == NULL) {
			rc = -ENOMEM;
			goto out;
		}

		list_link_init(&li_new->li_node);
		li_new->li_data = i;
		list_insert_after(&list, li_last, li_new);
		li_last = li_new;
	}

	splat_vprint(file, SPLAT_LIST_TEST4_NAME,
		     "Validating %d item list is a queue\n", list_size);
	rc = splat_list_validate(&list, list_size, LIST_ORDER_QUEUE, 1);
	if (rc)
		splat_vprint(file, SPLAT_LIST_TEST4_NAME,
			     "List validation failed, %d\n", rc);
out:
	/* Remove all items */
	splat_vprint(file, SPLAT_LIST_TEST4_NAME,
		     "Removing %d items from list tail\n", list_size);
	while ((li_new = list_remove_head(&list)))
		kmem_free(li_new, sizeof(list_item_t));

	splat_vprint(file, SPLAT_LIST_TEST4_NAME, "Destroying list\n%s", "");
	list_destroy(&list);

        return rc;
}

static int
splat_list_test5(struct file *file, void *arg)
{
	list_t list;
	list_item_t *li_new, *li_last = NULL;
	int i, list_size = 8, rc = 0;

	splat_vprint(file, SPLAT_LIST_TEST5_NAME, "Creating list\n%s", "");
	list_create(&list, sizeof(list_item_t), offsetof(list_item_t, li_node));

	/* Insert all items before the last item to form a stack */
	splat_vprint(file, SPLAT_LIST_TEST5_NAME,
		     "Adding %d items each before the last item\n", list_size);
	for (i = 0; i < list_size; i++) {
		li_new = kmem_alloc(sizeof(list_item_t), KM_SLEEP);
		if (li_new == NULL) {
			rc = -ENOMEM;
			goto out;
		}

		list_link_init(&li_new->li_node);
		li_new->li_data = i;
		list_insert_before(&list, li_last, li_new);
		li_last = li_new;
	}

	splat_vprint(file, SPLAT_LIST_TEST5_NAME,
		     "Validating %d item list is a queue\n", list_size);
	rc = splat_list_validate(&list, list_size, LIST_ORDER_STACK, 1);
	if (rc)
		splat_vprint(file, SPLAT_LIST_TEST5_NAME,
			     "List validation failed, %d\n", rc);
out:
	/* Remove all items */
	splat_vprint(file, SPLAT_LIST_TEST5_NAME,
		     "Removing %d items from list tail\n", list_size);
	while ((li_new = list_remove_tail(&list)))
		kmem_free(li_new, sizeof(list_item_t));

	splat_vprint(file, SPLAT_LIST_TEST5_NAME, "Destroying list\n%s", "");
	list_destroy(&list);

        return rc;
}

static int
splat_list_test6(struct file *file, void *arg)
{
	list_t list;
	list_item_t *li, *li_prev;
	int i, list_size = 8, rc = 0;

	splat_vprint(file, SPLAT_LIST_TEST6_NAME, "Creating list\n%s", "");
	list_create(&list, sizeof(list_item_t), offsetof(list_item_t, li_node));

	/* Insert all items at the list tail to form a queue */
	splat_vprint(file, SPLAT_LIST_TEST6_NAME,
		     "Adding %d items to list tail\n", list_size);
	for (i = 0; i < list_size; i++) {
		li = kmem_alloc(sizeof(list_item_t), KM_SLEEP);
		if (li == NULL) {
			rc = -ENOMEM;
			goto out;
		}

		list_link_init(&li->li_node);
		li->li_data = i;
		list_insert_tail(&list, li);
	}

	/* Remove all odd items from the queue */
	splat_vprint(file, SPLAT_LIST_TEST6_NAME,
		     "Removing %d odd items from the list\n", list_size / 2);
	for (li = list_head(&list); li != NULL; li = list_next(&list, li)) {
		if (li->li_data % 2 == 1) {
			li_prev = list_prev(&list, li);
			list_remove(&list, li);
			kmem_free(li, sizeof(list_item_t));
			li = li_prev;
		}
	}

	splat_vprint(file, SPLAT_LIST_TEST6_NAME, "Validating %d item "
		     "list is a queue of only even elements\n", list_size / 2);
	rc = splat_list_validate(&list, list_size / 2, LIST_ORDER_QUEUE, 2);
	if (rc)
		splat_vprint(file, SPLAT_LIST_TEST6_NAME,
			     "List validation failed, %d\n", rc);
out:
	/* Remove all items */
	splat_vprint(file, SPLAT_LIST_TEST6_NAME,
		     "Removing %d items from list tail\n", list_size / 2);
	while ((li = list_remove_tail(&list)))
		kmem_free(li, sizeof(list_item_t));

	splat_vprint(file, SPLAT_LIST_TEST6_NAME, "Destroying list\n%s", "");
	list_destroy(&list);

        return rc;
}

static int
splat_list_test7(struct file *file, void *arg)
{
	list_t list;
	list_item_t *li;
	int rc = 0;

	splat_vprint(file, SPLAT_LIST_TEST7_NAME, "Creating list\n%s", "");
	list_create(&list, sizeof(list_item_t), offsetof(list_item_t, li_node));

	li = kmem_alloc(sizeof(list_item_t), KM_SLEEP);
	if (li == NULL) {
		rc = -ENOMEM;
		goto out;
	}

	/* Validate newly initialized node is inactive */
	splat_vprint(file, SPLAT_LIST_TEST7_NAME, "Init list node\n%s", "");
	list_link_init(&li->li_node);
	if (list_link_active(&li->li_node)) {
		splat_vprint(file, SPLAT_LIST_TEST7_NAME, "Newly initialized "
			    "list node should inactive %p/%p\n",
			    li->li_node.prev, li->li_node.next);
		rc = -EINVAL;
		goto out_li;
	}

	/* Validate node is active when linked in to a list */
	splat_vprint(file, SPLAT_LIST_TEST7_NAME, "Insert list node\n%s", "");
	list_insert_head(&list, li);
	if (!list_link_active(&li->li_node)) {
		splat_vprint(file, SPLAT_LIST_TEST7_NAME, "List node "
			    "inserted in list should be active %p/%p\n",
			    li->li_node.prev, li->li_node.next);
		rc = -EINVAL;
		goto out;
	}

	/* Validate node is inactive when removed from list */
	splat_vprint(file, SPLAT_LIST_TEST7_NAME, "Remove list node\n%s", "");
	list_remove(&list, li);
	if (list_link_active(&li->li_node)) {
		splat_vprint(file, SPLAT_LIST_TEST7_NAME, "List node "
			    "removed from list should be inactive %p/%p\n",
			    li->li_node.prev, li->li_node.next);
		rc = -EINVAL;
	}
out_li:
	kmem_free(li, sizeof(list_item_t));
out:
	/* Remove all items */
	while ((li = list_remove_head(&list)))
		kmem_free(li, sizeof(list_item_t));

	splat_vprint(file, SPLAT_LIST_TEST7_NAME, "Destroying list\n%s", "");
	list_destroy(&list);

        return rc;
}

splat_subsystem_t *
splat_list_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_LIST_NAME, SPLAT_NAME_SIZE);
	strncpy(sub->desc.desc, SPLAT_LIST_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_LIST;

        SPLAT_TEST_INIT(sub, SPLAT_LIST_TEST1_NAME, SPLAT_LIST_TEST1_DESC,
	                SPLAT_LIST_TEST1_ID, splat_list_test1);
        SPLAT_TEST_INIT(sub, SPLAT_LIST_TEST2_NAME, SPLAT_LIST_TEST2_DESC,
	                SPLAT_LIST_TEST2_ID, splat_list_test2);
        SPLAT_TEST_INIT(sub, SPLAT_LIST_TEST3_NAME, SPLAT_LIST_TEST3_DESC,
	                SPLAT_LIST_TEST3_ID, splat_list_test3);
        SPLAT_TEST_INIT(sub, SPLAT_LIST_TEST4_NAME, SPLAT_LIST_TEST4_DESC,
	                SPLAT_LIST_TEST4_ID, splat_list_test4);
        SPLAT_TEST_INIT(sub, SPLAT_LIST_TEST5_NAME, SPLAT_LIST_TEST5_DESC,
	                SPLAT_LIST_TEST5_ID, splat_list_test5);
        SPLAT_TEST_INIT(sub, SPLAT_LIST_TEST6_NAME, SPLAT_LIST_TEST6_DESC,
	                SPLAT_LIST_TEST6_ID, splat_list_test6);
        SPLAT_TEST_INIT(sub, SPLAT_LIST_TEST7_NAME, SPLAT_LIST_TEST7_DESC,
	                SPLAT_LIST_TEST7_ID, splat_list_test7);

        return sub;
}

void
splat_list_fini(splat_subsystem_t *sub)
{
        ASSERT(sub);

        SPLAT_TEST_FINI(sub, SPLAT_LIST_TEST7_ID);
        SPLAT_TEST_FINI(sub, SPLAT_LIST_TEST6_ID);
        SPLAT_TEST_FINI(sub, SPLAT_LIST_TEST5_ID);
        SPLAT_TEST_FINI(sub, SPLAT_LIST_TEST4_ID);
        SPLAT_TEST_FINI(sub, SPLAT_LIST_TEST3_ID);
        SPLAT_TEST_FINI(sub, SPLAT_LIST_TEST2_ID);
        SPLAT_TEST_FINI(sub, SPLAT_LIST_TEST1_ID);

        kfree(sub);
}

int
splat_list_id(void)
{
        return SPLAT_SUBSYSTEM_LIST;
}