summaryrefslogtreecommitdiff
path: root/lib/test_context-analysis.c
blob: 140efa8a97636ef3fc81903eb88ae04cda0dccc9 (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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Compile-only tests for common patterns that should not generate false
 * positive errors when compiled with Clang's context analysis.
 */

#include <linux/bit_spinlock.h>
#include <linux/build_bug.h>
#include <linux/local_lock.h>
#include <linux/mutex.h>
#include <linux/percpu.h>
#include <linux/rcupdate.h>
#include <linux/rwsem.h>
#include <linux/seqlock.h>
#include <linux/spinlock.h>
#include <linux/srcu.h>
#include <linux/ww_mutex.h>

/*
 * Test that helper macros work as expected.
 */
static void __used test_common_helpers(void)
{
	BUILD_BUG_ON(context_unsafe(3) != 3); /* plain expression */
	BUILD_BUG_ON(context_unsafe((void)2; 3) != 3); /* does not swallow semi-colon */
	BUILD_BUG_ON(context_unsafe((void)2, 3) != 3); /* does not swallow commas */
	context_unsafe(do { } while (0)); /* works with void statements */
}

#define TEST_SPINLOCK_COMMON(class, type, type_init, type_lock, type_unlock, type_trylock, op)	\
	struct test_##class##_data {								\
		type lock;									\
		int counter __guarded_by(&lock);						\
		int *pointer __pt_guarded_by(&lock);						\
	};											\
	static void __used test_##class##_init(struct test_##class##_data *d)			\
	{											\
		guard(type_init)(&d->lock);							\
		d->counter = 0;									\
	}											\
	static void __used test_##class(struct test_##class##_data *d)				\
	{											\
		unsigned long flags;								\
		d->pointer++;									\
		type_lock(&d->lock);								\
		op(d->counter);									\
		op(*d->pointer);								\
		type_unlock(&d->lock);								\
		type_lock##_irq(&d->lock);							\
		op(d->counter);									\
		op(*d->pointer);								\
		type_unlock##_irq(&d->lock);							\
		type_lock##_bh(&d->lock);							\
		op(d->counter);									\
		op(*d->pointer);								\
		type_unlock##_bh(&d->lock);							\
		type_lock##_irqsave(&d->lock, flags);						\
		op(d->counter);									\
		op(*d->pointer);								\
		type_unlock##_irqrestore(&d->lock, flags);					\
	}											\
	static void __used test_##class##_trylock(struct test_##class##_data *d)		\
	{											\
		if (type_trylock(&d->lock)) {							\
			op(d->counter);								\
			type_unlock(&d->lock);							\
		}										\
	}											\
	static void __used test_##class##_assert(struct test_##class##_data *d)			\
	{											\
		lockdep_assert_held(&d->lock);							\
		op(d->counter);									\
	}											\
	static void __used test_##class##_guard(struct test_##class##_data *d)			\
	{											\
		{ guard(class)(&d->lock);		op(d->counter); }			\
		{ guard(class##_irq)(&d->lock);		op(d->counter); }			\
		{ guard(class##_irqsave)(&d->lock);	op(d->counter); }			\
	}

#define TEST_OP_RW(x) (x)++
#define TEST_OP_RO(x) ((void)(x))

TEST_SPINLOCK_COMMON(raw_spinlock,
		     raw_spinlock_t,
		     raw_spinlock_init,
		     raw_spin_lock,
		     raw_spin_unlock,
		     raw_spin_trylock,
		     TEST_OP_RW);
static void __used test_raw_spinlock_trylock_extra(struct test_raw_spinlock_data *d)
{
	unsigned long flags;

	data_race(d->counter++); /* no warning */

	if (raw_spin_trylock_irq(&d->lock)) {
		d->counter++;
		raw_spin_unlock_irq(&d->lock);
	}
	if (raw_spin_trylock_irqsave(&d->lock, flags)) {
		d->counter++;
		raw_spin_unlock_irqrestore(&d->lock, flags);
	}
	scoped_cond_guard(raw_spinlock_try, return, &d->lock) {
		d->counter++;
	}
}

TEST_SPINLOCK_COMMON(spinlock,
		     spinlock_t,
		     spinlock_init,
		     spin_lock,
		     spin_unlock,
		     spin_trylock,
		     TEST_OP_RW);
static void __used test_spinlock_trylock_extra(struct test_spinlock_data *d)
{
	unsigned long flags;

	if (spin_trylock_irq(&d->lock)) {
		d->counter++;
		spin_unlock_irq(&d->lock);
	}
	if (spin_trylock_irqsave(&d->lock, flags)) {
		d->counter++;
		spin_unlock_irqrestore(&d->lock, flags);
	}
	scoped_cond_guard(spinlock_try, return, &d->lock) {
		d->counter++;
	}
}

TEST_SPINLOCK_COMMON(write_lock,
		     rwlock_t,
		     rwlock_init,
		     write_lock,
		     write_unlock,
		     write_trylock,
		     TEST_OP_RW);
static void __used test_write_trylock_extra(struct test_write_lock_data *d)
{
	unsigned long flags;

	if (write_trylock_irqsave(&d->lock, flags)) {
		d->counter++;
		write_unlock_irqrestore(&d->lock, flags);
	}
}

TEST_SPINLOCK_COMMON(read_lock,
		     rwlock_t,
		     rwlock_init,
		     read_lock,
		     read_unlock,
		     read_trylock,
		     TEST_OP_RO);

struct test_mutex_data {
	struct mutex mtx;
	int counter __guarded_by(&mtx);
};

static void __used test_mutex_init(struct test_mutex_data *d)
{
	guard(mutex_init)(&d->mtx);
	d->counter = 0;
}

static void __used test_mutex_lock(struct test_mutex_data *d)
{
	mutex_lock(&d->mtx);
	d->counter++;
	mutex_unlock(&d->mtx);
	mutex_lock_io(&d->mtx);
	d->counter++;
	mutex_unlock(&d->mtx);
}

static void __used test_mutex_trylock(struct test_mutex_data *d, atomic_t *a)
{
	if (!mutex_lock_interruptible(&d->mtx)) {
		d->counter++;
		mutex_unlock(&d->mtx);
	}
	if (!mutex_lock_killable(&d->mtx)) {
		d->counter++;
		mutex_unlock(&d->mtx);
	}
	if (mutex_trylock(&d->mtx)) {
		d->counter++;
		mutex_unlock(&d->mtx);
	}
	if (atomic_dec_and_mutex_lock(a, &d->mtx)) {
		d->counter++;
		mutex_unlock(&d->mtx);
	}
}

static void __used test_mutex_assert(struct test_mutex_data *d)
{
	lockdep_assert_held(&d->mtx);
	d->counter++;
}

static void __used test_mutex_guard(struct test_mutex_data *d)
{
	guard(mutex)(&d->mtx);
	d->counter++;
}

static void __used test_mutex_cond_guard(struct test_mutex_data *d)
{
	scoped_cond_guard(mutex_try, return, &d->mtx) {
		d->counter++;
	}
	scoped_cond_guard(mutex_intr, return, &d->mtx) {
		d->counter++;
	}
}

struct test_seqlock_data {
	seqlock_t sl;
	int counter __guarded_by(&sl);
};

static void __used test_seqlock_init(struct test_seqlock_data *d)
{
	guard(seqlock_init)(&d->sl);
	d->counter = 0;
}

static void __used test_seqlock_reader(struct test_seqlock_data *d)
{
	unsigned int seq;

	do {
		seq = read_seqbegin(&d->sl);
		(void)d->counter;
	} while (read_seqretry(&d->sl, seq));
}

static void __used test_seqlock_writer(struct test_seqlock_data *d)
{
	unsigned long flags;

	write_seqlock(&d->sl);
	d->counter++;
	write_sequnlock(&d->sl);

	write_seqlock_irq(&d->sl);
	d->counter++;
	write_sequnlock_irq(&d->sl);

	write_seqlock_bh(&d->sl);
	d->counter++;
	write_sequnlock_bh(&d->sl);

	write_seqlock_irqsave(&d->sl, flags);
	d->counter++;
	write_sequnlock_irqrestore(&d->sl, flags);
}

static void __used test_seqlock_scoped(struct test_seqlock_data *d)
{
	scoped_seqlock_read (&d->sl, ss_lockless) {
		(void)d->counter;
	}
}

struct test_rwsem_data {
	struct rw_semaphore sem;
	int counter __guarded_by(&sem);
};

static void __used test_rwsem_init(struct test_rwsem_data *d)
{
	guard(rwsem_init)(&d->sem);
	d->counter = 0;
}

static void __used test_rwsem_reader(struct test_rwsem_data *d)
{
	down_read(&d->sem);
	(void)d->counter;
	up_read(&d->sem);

	if (down_read_trylock(&d->sem)) {
		(void)d->counter;
		up_read(&d->sem);
	}
}

static void __used test_rwsem_writer(struct test_rwsem_data *d)
{
	down_write(&d->sem);
	d->counter++;
	up_write(&d->sem);

	down_write(&d->sem);
	d->counter++;
	downgrade_write(&d->sem);
	(void)d->counter;
	up_read(&d->sem);

	if (down_write_trylock(&d->sem)) {
		d->counter++;
		up_write(&d->sem);
	}
}

static void __used test_rwsem_assert(struct test_rwsem_data *d)
{
	rwsem_assert_held_nolockdep(&d->sem);
	d->counter++;
}

static void __used test_rwsem_guard(struct test_rwsem_data *d)
{
	{ guard(rwsem_read)(&d->sem); (void)d->counter; }
	{ guard(rwsem_write)(&d->sem); d->counter++; }
}

static void __used test_rwsem_cond_guard(struct test_rwsem_data *d)
{
	scoped_cond_guard(rwsem_read_try, return, &d->sem) {
		(void)d->counter;
	}
	scoped_cond_guard(rwsem_write_try, return, &d->sem) {
		d->counter++;
	}
}

struct test_bit_spinlock_data {
	unsigned long bits;
	int counter __guarded_by(__bitlock(3, &bits));
};

static void __used test_bit_spin_lock(struct test_bit_spinlock_data *d)
{
	/*
	 * Note, the analysis seems to have false negatives, because it won't
	 * precisely recognize the bit of the fake __bitlock() token.
	 */
	bit_spin_lock(3, &d->bits);
	d->counter++;
	bit_spin_unlock(3, &d->bits);

	bit_spin_lock(3, &d->bits);
	d->counter++;
	__bit_spin_unlock(3, &d->bits);

	if (bit_spin_trylock(3, &d->bits)) {
		d->counter++;
		bit_spin_unlock(3, &d->bits);
	}
}

/*
 * Test that we can mark a variable guarded by RCU, and we can dereference and
 * write to the pointer with RCU's primitives.
 */
struct test_rcu_data {
	long __rcu_guarded *data;
};

static void __used test_rcu_guarded_reader(struct test_rcu_data *d)
{
	rcu_read_lock();
	(void)rcu_dereference(d->data);
	rcu_read_unlock();

	rcu_read_lock_bh();
	(void)rcu_dereference(d->data);
	rcu_read_unlock_bh();

	rcu_read_lock_sched();
	(void)rcu_dereference(d->data);
	rcu_read_unlock_sched();
}

static void __used test_rcu_guard(struct test_rcu_data *d)
{
	guard(rcu)();
	(void)rcu_dereference(d->data);
}

static void __used test_rcu_guarded_updater(struct test_rcu_data *d)
{
	rcu_assign_pointer(d->data, NULL);
	RCU_INIT_POINTER(d->data, NULL);
	(void)unrcu_pointer(d->data);
}

static void wants_rcu_held(void)	__must_hold_shared(RCU)       { }
static void wants_rcu_held_bh(void)	__must_hold_shared(RCU_BH)    { }
static void wants_rcu_held_sched(void)	__must_hold_shared(RCU_SCHED) { }

static void __used test_rcu_lock_variants(void)
{
	rcu_read_lock();
	wants_rcu_held();
	rcu_read_unlock();

	rcu_read_lock_bh();
	wants_rcu_held_bh();
	rcu_read_unlock_bh();

	rcu_read_lock_sched();
	wants_rcu_held_sched();
	rcu_read_unlock_sched();
}

static void __used test_rcu_lock_reentrant(void)
{
	rcu_read_lock();
	rcu_read_lock();
	rcu_read_lock_bh();
	rcu_read_lock_bh();
	rcu_read_lock_sched();
	rcu_read_lock_sched();

	rcu_read_unlock_sched();
	rcu_read_unlock_sched();
	rcu_read_unlock_bh();
	rcu_read_unlock_bh();
	rcu_read_unlock();
	rcu_read_unlock();
}

static void __used test_rcu_assert_variants(void)
{
	lockdep_assert_in_rcu_read_lock();
	wants_rcu_held();

	lockdep_assert_in_rcu_read_lock_bh();
	wants_rcu_held_bh();

	lockdep_assert_in_rcu_read_lock_sched();
	wants_rcu_held_sched();
}

struct test_srcu_data {
	struct srcu_struct srcu;
	long __rcu_guarded *data;
};

static void __used test_srcu(struct test_srcu_data *d)
{
	init_srcu_struct(&d->srcu);

	int idx = srcu_read_lock(&d->srcu);
	long *data = srcu_dereference(d->data, &d->srcu);
	(void)data;
	srcu_read_unlock(&d->srcu, idx);

	rcu_assign_pointer(d->data, NULL);
}

static void __used test_srcu_guard(struct test_srcu_data *d)
{
	{ guard(srcu)(&d->srcu); (void)srcu_dereference(d->data, &d->srcu); }
	{ guard(srcu_fast)(&d->srcu); (void)srcu_dereference(d->data, &d->srcu); }
	{ guard(srcu_fast_notrace)(&d->srcu); (void)srcu_dereference(d->data, &d->srcu); }
}

struct test_local_lock_data {
	local_lock_t lock;
	int counter __guarded_by(&lock);
};

static DEFINE_PER_CPU(struct test_local_lock_data, test_local_lock_data) = {
	.lock = INIT_LOCAL_LOCK(lock),
};

static void __used test_local_lock_init(struct test_local_lock_data *d)
{
	guard(local_lock_init)(&d->lock);
	d->counter = 0;
}

static void __used test_local_lock(void)
{
	unsigned long flags;

	local_lock(&test_local_lock_data.lock);
	this_cpu_add(test_local_lock_data.counter, 1);
	local_unlock(&test_local_lock_data.lock);

	local_lock_irq(&test_local_lock_data.lock);
	this_cpu_add(test_local_lock_data.counter, 1);
	local_unlock_irq(&test_local_lock_data.lock);

	local_lock_irqsave(&test_local_lock_data.lock, flags);
	this_cpu_add(test_local_lock_data.counter, 1);
	local_unlock_irqrestore(&test_local_lock_data.lock, flags);

	local_lock_nested_bh(&test_local_lock_data.lock);
	this_cpu_add(test_local_lock_data.counter, 1);
	local_unlock_nested_bh(&test_local_lock_data.lock);
}

static void __used test_local_lock_guard(void)
{
	{ guard(local_lock)(&test_local_lock_data.lock); this_cpu_add(test_local_lock_data.counter, 1); }
	{ guard(local_lock_irq)(&test_local_lock_data.lock); this_cpu_add(test_local_lock_data.counter, 1); }
	{ guard(local_lock_irqsave)(&test_local_lock_data.lock); this_cpu_add(test_local_lock_data.counter, 1); }
	{ guard(local_lock_nested_bh)(&test_local_lock_data.lock); this_cpu_add(test_local_lock_data.counter, 1); }
}

struct test_local_trylock_data {
	local_trylock_t lock;
	int counter __guarded_by(&lock);
};

static DEFINE_PER_CPU(struct test_local_trylock_data, test_local_trylock_data) = {
	.lock = INIT_LOCAL_TRYLOCK(lock),
};

static void __used test_local_trylock_init(struct test_local_trylock_data *d)
{
	guard(local_trylock_init)(&d->lock);
	d->counter = 0;
}

static void __used test_local_trylock(void)
{
	local_lock(&test_local_trylock_data.lock);
	this_cpu_add(test_local_trylock_data.counter, 1);
	local_unlock(&test_local_trylock_data.lock);

	if (local_trylock(&test_local_trylock_data.lock)) {
		this_cpu_add(test_local_trylock_data.counter, 1);
		local_unlock(&test_local_trylock_data.lock);
	}
}

static DEFINE_WD_CLASS(ww_class);

struct test_ww_mutex_data {
	struct ww_mutex mtx;
	int counter __guarded_by(&mtx);
};

static void __used test_ww_mutex_lock_noctx(struct test_ww_mutex_data *d)
{
	if (!ww_mutex_lock(&d->mtx, NULL)) {
		d->counter++;
		ww_mutex_unlock(&d->mtx);
	}

	if (!ww_mutex_lock_interruptible(&d->mtx, NULL)) {
		d->counter++;
		ww_mutex_unlock(&d->mtx);
	}

	if (ww_mutex_trylock(&d->mtx, NULL)) {
		d->counter++;
		ww_mutex_unlock(&d->mtx);
	}

	ww_mutex_lock_slow(&d->mtx, NULL);
	d->counter++;
	ww_mutex_unlock(&d->mtx);

	ww_mutex_destroy(&d->mtx);
}

static void __used test_ww_mutex_lock_ctx(struct test_ww_mutex_data *d)
{
	struct ww_acquire_ctx ctx;

	ww_acquire_init(&ctx, &ww_class);

	if (!ww_mutex_lock(&d->mtx, &ctx)) {
		d->counter++;
		ww_mutex_unlock(&d->mtx);
	}

	if (!ww_mutex_lock_interruptible(&d->mtx, &ctx)) {
		d->counter++;
		ww_mutex_unlock(&d->mtx);
	}

	if (ww_mutex_trylock(&d->mtx, &ctx)) {
		d->counter++;
		ww_mutex_unlock(&d->mtx);
	}

	ww_mutex_lock_slow(&d->mtx, &ctx);
	d->counter++;
	ww_mutex_unlock(&d->mtx);

	ww_acquire_done(&ctx);
	ww_acquire_fini(&ctx);

	ww_mutex_destroy(&d->mtx);
}