-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
core-lock.c
773 lines (643 loc) · 15.8 KB
/
core-lock.c
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
* Copyright (C) 2022-2024 Colin Ian King.
*
* This program 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 program 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 "stress-ng.h"
#include "core-arch.h"
#include "core-asm-arm.h"
#include "core-asm-ppc64.h"
#include "core-asm-loong64.h"
#include "core-asm-riscv.h"
#include "core-asm-x86.h"
#include "core-attribute.h"
#include "core-builtin.h"
#include "core-pthread.h"
#include "core-lock.h"
#if defined(HAVE_LINUX_FUTEX_H)
#include <linux/futex.h>
#endif
#if defined(HAVE_SEMAPHORE_H)
#include <semaphore.h>
#endif
#if defined(HAVE_SEM_SYSV)
#include <sys/sem.h>
#endif
#if defined(HAVE_THREADS_H)
#include <threads.h>
#endif
#define STRESS_LOCK_MAGIC (0x387cb9e5) /* magic when lock is used */
#define STRESS_LOCK_MAGIC_FREE (0x00000000) /* magic when lock is free */
#define STRESS_LOCK_MAX (STRESS_PROCS_MAX * 2) /* max for 2 per instance */
#define STRESS_LOCK_MAX_BACKOFF (1U << 18)
#if defined(HAVE_LIB_PTHREAD) && \
defined(HAVE_LIB_PTHREAD_SPINLOCK) && \
!defined(__DragonFly__) && \
!defined(__OpenBSD__)
#define LOCK_METHOD_PTHREAD_SPINLOCK (0x0001)
#else
#define LOCK_METHOD_PTHREAD_SPINLOCK (0)
#endif
#if defined(HAVE_LIB_PTHREAD) && \
defined(HAVE_PTHREAD_MUTEX_T) && \
defined(HAVE_PTHREAD_MUTEX_DESTROY) && \
defined(HAVE_PTHREAD_MUTEX_INIT)
#define LOCK_METHOD_PTHREAD_MUTEX (0x0002)
#else
#define LOCK_METHOD_PTHREAD_MUTEX (0)
#endif
#if defined(HAVE_THREADS_H) && \
defined(HAVE_MTX_T) && \
defined(HAVE_MTX_DESTROY) && \
defined(HAVE_MTX_INIT)
#define LOCK_METHOD_OSI_C_MTX (0x0004)
#else
#define LOCK_METHOD_OSI_C_MTX (0)
#endif
#if defined(HAVE_LINUX_FUTEX_H) && \
defined(__NR_futex) && \
defined(FUTEX_LOCK_PI) && \
defined(FUTEX_UNLOCK_PI) && \
defined(HAVE_SYSCALL)
#define LOCK_METHOD_FUTEX (0x0008)
#else
#define LOCK_METHOD_FUTEX (0)
#endif
#if defined(HAVE_ATOMIC_TEST_AND_SET) && \
!defined(STRESS_ARCH_ARM)
#define LOCK_METHOD_ATOMIC_SPINLOCK (0x0010)
#else
#define LOCK_METHOD_ATOMIC_SPINLOCK (0)
#endif
#if defined(HAVE_SEMAPHORE_H) && \
defined(HAVE_LIB_PTHREAD) && \
defined(HAVE_SEM_POSIX)
#define LOCK_METHOD_SEM_POSIX (0x0020)
#else
#define LOCK_METHOD_SEM_POSIX (0)
#endif
#if defined(HAVE_SEM_SYSV) && \
defined(HAVE_KEY_T)
#define LOCK_METHOD_SEM_SYSV (0x0040)
#else
#define LOCK_METHOD_SEM_SYSV (0)
#endif
#define LOCK_METHOD_ALL \
(LOCK_METHOD_ATOMIC_SPINLOCK | \
LOCK_METHOD_PTHREAD_SPINLOCK | \
LOCK_METHOD_PTHREAD_MUTEX | \
LOCK_METHOD_FUTEX | \
LOCK_METHOD_SEM_POSIX | \
LOCK_METHOD_SEM_SYSV)
typedef union {
#if LOCK_METHOD_ATOMIC_SPINLOCK != 0
bool flag; /* atomic spinlock flag */
#elif LOCK_METHOD_PTHREAD_SPINLOCK != 0
pthread_spinlock_t pthread_spinlock; /* spinlock */
#elif LOCK_METHOD_PTHREAD_MUTEX != 0
pthread_mutex_t pthread_mutex; /* mutex */
#elif LOCK_METHOD_OSI_C_MTX != 0
mtx_t mtx; /* ISO C mutex */
#elif LOCK_METHOD_FUTEX != 0
int futex; /* futex */
#elif LOCK_METHOD_SEM_POSIX != 0
sem_t sem_posix; /* POSIX semaphore */
#elif LOCK_METHOD_SEM_SYSV != 0
int sem_id; /* SYS V semaphore */
#endif
} stress_lock_u_t;
typedef struct stress_lock {
uint32_t magic; /* Lock magic struct pattern, zero when not in use */
stress_lock_u_t u; /* Lock union */
} stress_lock_t;
typedef struct stress_lock_funcs {
const char *type;
int (*init)(struct stress_lock *lock);
int (*deinit)(struct stress_lock *lock);
int (*acquire)(struct stress_lock *lock);
int (*acquire_relax)(struct stress_lock *lock);
int (*release)(struct stress_lock *lock);
} stress_lock_funcs_t;
static stress_lock_t *stress_locks;
static stress_lock_t *stress_lock_big_lock;
static stress_lock_t *stress_lock_get(void);
static int stress_lock_put(stress_lock_t *lock);
/*
* stress_lock_valid()
* return true of lock magic is valid
*/
static inline ALWAYS_INLINE bool stress_lock_valid(const stress_lock_t *lock)
{
return (lock && (lock->magic == STRESS_LOCK_MAGIC));
}
/*
* Locking via atomic spinlock
*/
#if LOCK_METHOD_ATOMIC_SPINLOCK != 0
static inline bool test_and_set(bool *addr)
{
return __atomic_test_and_set((void *)addr, __ATOMIC_ACQ_REL);
}
static int stress_atomic_lock_init(stress_lock_t *lock)
{
lock->u.flag = 0;
return 0;
}
static int PURE stress_atomic_lock_deinit(stress_lock_t *lock)
{
(void)lock;
return 0;
}
static int stress_atomic_lock_acquire(stress_lock_t *lock)
{
if (lock) {
double t = stress_time_now();
while (test_and_set(&lock->u.flag) == true) {
if (((stress_time_now() - t) > 5.0) && !stress_continue_flag()) {
errno = EAGAIN;
return -1;
}
}
return 0;
}
errno = EINVAL;
return -1;
}
#if defined(HAVE_ASM_X86_PAUSE) || \
defined(HAVE_ASM_LOONG64_DBAR) || \
defined(STRESS_ARCH_PPC64) || \
defined(STRESS_ARCH_RISCV)
#define STRESS_LOCK_BACKOFF
#endif
static int stress_atomic_lock_acquire_relax(stress_lock_t *lock)
{
if (lock) {
double t = stress_time_now();
#if defined(STRESS_LOCK_BACKOFF)
uint32_t backoff = 1;
#endif
while (test_and_set(&lock->u.flag) == true) {
#if defined(STRESS_LOCK_BACKOFF)
register uint32_t i;
for (i = 0; i < backoff; i++) {
#if defined(HAVE_ASM_X86_PAUSE)
stress_asm_x86_pause();
#elif defined(HAVE_ASM_LOONG64_DBAR)
stress_asm_loong64_dbar();
#elif defined(STRESS_ARCH_PPC64)
stress_asm_ppc64_yield();
#elif defined(STRESS_ARCH_RISCV)
stress_asm_riscv_pause();
#endif
}
/*
* multiple fast cpu pauses on a failed lock acquire
* benefit from exponential backoff
*/
backoff = backoff << 1;
if (backoff > STRESS_LOCK_MAX_BACKOFF)
backoff = STRESS_LOCK_MAX_BACKOFF;
#else
(void)shim_sched_yield();
#endif
if (((stress_time_now() - t) > 5.0) && !stress_continue_flag()) {
errno = EAGAIN;
return -1;
}
}
return 0;
}
errno = EINVAL;
return -1;
}
static int stress_atomic_lock_release(stress_lock_t *lock)
{
lock->u.flag = false;
return 0;
}
static const stress_lock_funcs_t stress_lock_funcs = {
"atomic",
stress_atomic_lock_init,
stress_atomic_lock_deinit,
stress_atomic_lock_acquire,
stress_atomic_lock_acquire_relax,
stress_atomic_lock_release
};
/*
* Locking via pthread spinlock
*/
#elif LOCK_METHOD_PTHREAD_SPINLOCK != 0
static int stress_pthread_spinlock_init(stress_lock_t *lock)
{
int ret;
ret = pthread_spin_init(&lock->u.pthread_spinlock, PTHREAD_PROCESS_SHARED);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static int stress_pthread_spinlock_deinit(stress_lock_t *lock)
{
int ret;
ret = pthread_spin_destroy(&lock->u.pthread_spinlock);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static int stress_pthread_spinlock_acquire(stress_lock_t *lock)
{
int ret;
ret = pthread_spin_lock(&lock->u.pthread_spinlock);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static int stress_pthread_spinlock_release(stress_lock_t *lock)
{
int ret;
ret = pthread_spin_unlock(&lock->u.pthread_spinlock);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static const stress_lock_funcs_t stress_lock_funcs = {
"spinlock",
stress_pthread_spinlock_init,
stress_pthread_spinlock_deinit,
stress_pthread_spinlock_acquire,
stress_pthread_spinlock_acquire,
stress_pthread_spinlock_release
};
/*
* Locking via pthread mutex
*/
#elif LOCK_METHOD_PTHREAD_MUTEX != 0
static int stress_pthread_mutex_init(stress_lock_t *lock)
{
int ret;
ret = pthread_mutex_init(&lock->u.pthread_mutex, NULL);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static int PURE stress_pthread_mutex_deinit(stress_lock_t *lock)
{
(void)lock;
return 0;
}
static int stress_pthread_mutex_acquire(stress_lock_t *lock)
{
int ret;
ret = pthread_mutex_lock(&lock->u.pthread_mutex);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static int stress_pthread_mutex_release(stress_lock_t *lock)
{
int ret;
ret = pthread_mutex_unlock(&lock->u.pthread_mutex);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static const stress_lock_funcs_t stress_lock_funcs = {
"pthread-mutex",
stress_pthread_mutex_init,
stress_pthread_mutex_deinit,
stress_pthread_mutex_acquire,
stress_pthread_mutex_acquire,
stress_pthread_mutex_release
};
/*
* Locking via OSI C mtx Mutex
*/
#elif LOCK_METHOD_OSI_C_MTX != 0
static int stress_mtx_init(stress_lock_t *lock)
{
if (mtx_init(&lock->u.mtx, mtx_plain) == thrd_success)
return 0;
errno = -ENOSYS;
return -1;
}
static int stress_mtx_deinit(stress_lock_t *lock)
{
mtx_destroy(&lock->u.mtx);
return 0;
}
static int stress_mtx_acquire(stress_lock_t *lock)
{
if (mtx_lock(&lock->u.mtx) == thrd_success)
return 0;
errno = -ENOSYS;
return -1;
}
static int stress_mtx_release(stress_lock_t *lock)
{
if (mtx_unlock(&lock->u.mtx) == thrd_success)
return 0;
errno = -ENOSYS;
return -1;
}
static const stress_lock_funcs_t stress_lock_funcs = {
"OSI-C-mtx",
stress_mtx_init,
stress_mtx_deinit,
stress_mtx_acquire,
stress_mtx_acquire,
stress_mtx_release
};
/*
* Locking via Linux futex system call API
*/
#elif LOCK_METHOD_FUTEX != 0
static int stress_futex_init(stress_lock_t *lock)
{
lock->u.futex = 0;
return 0;
}
static int PURE stress_futex_deinit(stress_lock_t *lock)
{
(void)lock;
return 0;
}
static int stress_futex_acquire(stress_lock_t *lock)
{
return (int)syscall(__NR_futex, &lock->u.futex, FUTEX_LOCK_PI, 0, 0, 0, 0);
}
static int stress_futex_release(stress_lock_t *lock)
{
return (int)syscall(__NR_futex, &lock->u.futex, FUTEX_UNLOCK_PI, 0, 0, 0, 0);
}
static const stress_lock_funcs_t stress_lock_funcs = {
"futex",
stress_futex_init,
stress_futex_deinit,
stress_futex_acquire,
stress_futex_acquire,
stress_futex_release
};
/*
* Locking via POSIX semaphore
*/
#elif LOCK_METHOD_SEM_POSIX != 0
static int stress_sem_posix_init(stress_lock_t *lock)
{
return sem_init(&lock->u.sem_posix, 0, 1);
}
static int PURE stress_sem_posix_deinit(stress_lock_t *lock)
{
(void)lock;
return 0;
}
static int stress_sem_posix_acquire(stress_lock_t *lock)
{
return sem_wait(&lock->u.sem_posix);
}
static int stress_sem_posix_release(stress_lock_t *lock)
{
return sem_post(&lock->u.sem_posix);
}
static const stress_lock_funcs_t stress_lock_funcs = {
"sem-posix",
stress_sem_posix_init,
stress_sem_posix_deinit,
stress_sem_posix_acquire,
stress_sem_posix_acquire,
stress_sem_posix_release
};
/*
* Locking via SYSV semaphore
*/
#elif LOCK_METHOD_SEM_SYSV != 0
static int stress_sem_sysv_init(stress_lock_t *lock)
{
int i;
for (i = 0; i < 256; i++) {
const key_t key_id = (key_t)stress_mwc16();
const int sem_id = semget(key_id, 1, IPC_CREAT | S_IRUSR | S_IWUSR);
if (sem_id >= 0) {
union semun {
int val;
} arg;
arg.val = 1;
if (semctl(sem_id, 0, SETVAL, arg) == 0) {
lock->u.sem_id = sem_id;
return 0;
}
break;
}
}
errno = ENOENT;
return -1;
}
static int stress_sem_sysv_deinit(stress_lock_t *lock)
{
return semctl(lock->u.sem_id, 0, IPC_RMID);
}
static int stress_sem_sysv_acquire(stress_lock_t *lock)
{
struct sembuf sops[1];
sops[0].sem_num = 0;
sops[0].sem_op = -1;
sops[0].sem_flg = SEM_UNDO;
return semop(lock->u.sem_id, sops, 1);
}
static int stress_sem_sysv_release(stress_lock_t *lock)
{
struct sembuf sops[1];
sops[0].sem_num = 0;
sops[0].sem_op = 1;
sops[0].sem_flg = SEM_UNDO;
return semop(lock->u.sem_id, sops, 1);
}
static const stress_lock_funcs_t stress_lock_funcs = {
"sem-sysv",
stress_sem_sysv_init,
stress_sem_sysv_deinit,
stress_sem_sysv_acquire,
stress_sem_sysv_acquire,
stress_sem_sysv_release
};
#else
static int PURE stress_no_lock_fail(stress_lock_t *lock)
{
(void)lock;
return -1;
}
static const stress_lock_funcs_t stress_lock_funcs = {
"no-lock",
stress_no_lock_fail,
stress_no_lock_fail,
stress_no_lock_fail,
stress_no_lock_fail,
stress_no_lock_fail
};
#endif
/*
* stress_lock_create()
* generic lock creation and initialization
*/
void *stress_lock_create(const char *name)
{
stress_lock_t *lock;
(void)name;
if (LOCK_METHOD_ALL == (0)) {
/* Critical, we need to be able to lock somehow! */
pr_err("core-lock: no locking primitives available\n");
return NULL;
}
lock = stress_lock_get();
if (!lock)
return NULL;
if (stress_lock_funcs.init(lock) == 0)
return lock;
VOID_RET(int, stress_lock_destroy(lock));
return NULL;
}
/*
* stress_lock_destroy()
* generic lock destruction
*/
int stress_lock_destroy(void *lock_handle)
{
stress_lock_t *lock = (stress_lock_t *)lock_handle;
if (stress_lock_valid(lock)) {
(void)stress_lock_funcs.deinit(lock);
return stress_lock_put(lock);
}
errno = EINVAL;
return -1;
}
/*
* stress_lock_acquire()
* generic lock acquire (lock)
*/
int stress_lock_acquire(void *lock_handle)
{
stress_lock_t *lock = (stress_lock_t *)lock_handle;
if (stress_lock_valid(lock))
return stress_lock_funcs.acquire(lock);
errno = EINVAL;
return -1;
}
/*
* stress_lock_acquire_relax()
* generic lock acquire (lock) with relaxed backoff
*/
int stress_lock_acquire_relax(void *lock_handle)
{
stress_lock_t *lock = (stress_lock_t *)lock_handle;
if (stress_lock_valid(lock))
return stress_lock_funcs.acquire_relax(lock);
errno = EINVAL;
return -1;
}
/*
* stress_lock_release()
* generic lock release (unlock)
*/
int stress_lock_release(void *lock_handle)
{
stress_lock_t *lock = (stress_lock_t *)lock_handle;
if (stress_lock_valid(lock))
return stress_lock_funcs.release(lock);
errno = EINVAL;
return -1;
}
/*
* stress_lock_get()
* get next free lock from shared locks mapping
*/
static stress_lock_t *stress_lock_get(void)
{
register size_t i;
stress_lock_t *lock = NULL;
if (!stress_lock_big_lock)
return NULL;
if (!stress_lock_valid(stress_lock_big_lock))
return NULL;
if (stress_lock_funcs.acquire(stress_lock_big_lock) < 0)
return NULL;
for (i = 0; i < STRESS_LOCK_MAX; i++) {
if (stress_locks[i].magic == STRESS_LOCK_MAGIC_FREE) {
lock = &stress_locks[i];
lock->magic = STRESS_LOCK_MAGIC;
break;
}
}
stress_lock_funcs.release(stress_lock_big_lock);
return lock;
}
/*
* stress_lock_put()
* mark a lock as new free to be re-used
*/
static int stress_lock_put(stress_lock_t *lock)
{
if (!lock)
return -1;
if (!stress_lock_valid(lock))
return -1;
if (!stress_lock_big_lock)
return -1;
if (!stress_lock_valid(stress_lock_big_lock))
return -1;
if (stress_lock_funcs.acquire(stress_lock_big_lock) < 0)
return -1;
(void)shim_memset(lock, 0, sizeof(*lock));
stress_lock_funcs.release(stress_lock_big_lock);
return 0;
}
/*
* stress_lock_mem_map()
* mmap 1 page of shared locks
*/
int stress_lock_mem_map(void)
{
size_t mmap_size;
char name[64];
mmap_size = STRESS_LOCK_MAX * sizeof(*stress_locks);
stress_locks = (stress_lock_t *)mmap(NULL, mmap_size,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED,
-1, 0);
if (stress_locks == MAP_FAILED)
return -1;
(void)snprintf(name, sizeof(name), "lock-%s", stress_lock_funcs.type);
stress_set_vma_anon_name(stress_locks, mmap_size, name);
stress_lock_big_lock = &stress_locks[0];
stress_lock_funcs.init(stress_lock_big_lock);
stress_lock_big_lock->magic = STRESS_LOCK_MAGIC;
return 0;
}
/*
* stress_lock_mem_unmap()
* unmap shared locks
*/
void stress_lock_mem_unmap(void)
{
const size_t mmap_size = STRESS_LOCK_MAX * sizeof(*stress_locks);
(void)munmap((void *)stress_locks, mmap_size);
stress_locks = NULL;
stress_lock_big_lock = NULL;
}