summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/sched_ext/enq_blocked.c
blob: 26204548bbf0843e10b89a3a4a8f13c898082504 (plain) (blame)
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES
 *
 * Exercise a priority inversion with the owner and donor first pinned to the
 * same CPU, then with each on a different CPU. A high-priority donor blocks on
 * a mutex held by a low-priority owner while one medium-priority contender per
 * available CPU keeps the system busy. A weighted-vruntime BPF scheduler runs
 * both CPU placement configurations with SCX_OPS_ENQ_BLOCKED first disabled
 * and then enabled. The test validates blocked-donor admission and reports the
 * average mutex hold and wait times, plus their enabled-minus-disabled deltas,
 * for each configuration. The timing data is informational.
 *
 * CONFIG_SCHED_PROXY_EXEC=y is required to exercise the proxy-execution paths.
 */
#define _GNU_SOURCE

#include <bpf/bpf.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <sched.h>
#include <scx/common.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>

#include "enq_blocked.bpf.skel.h"
#include "enq_blocked.h"
#include "scx_test.h"

#define MODULE_NAME	"scx_enq_blocked_test"
#define MODULE_FILE	"test_modules/" MODULE_NAME ".ko"
#define DEVICE_PATH	"/dev/scx_enq_blocked"
#define WAIT_STEP_US	1000
#define WAIT_TIMEOUT_MS	2000
#define NR_WARMUP_TRIALS	1
#define NR_MEASURED_TRIALS	10
#define NR_TRIALS	(NR_WARMUP_TRIALS + NR_MEASURED_TRIALS)
#define JOIN_TIMEOUT_MS	((NR_TRIALS + 1) * WAIT_TIMEOUT_MS)
#define OWNER_NICE	19
#define DONOR_NICE	-20
#define CONTENDER_NICE	0

struct thread_ctx {
	atomic_bool start_donor;
	atomic_bool abort;
	atomic_bool stop_contender;
	atomic_bool measurement_ready;
	atomic_int donor_pid;
	atomic_int donor_completed;
	int fd;
	int donor_cpu;
	int owner_cpu;
};

struct contender_ctx {
	struct thread_ctx *thread_ctx;
	atomic_int status;
	int cpu;
};

struct run_result {
	struct enq_blocked_stats stats;
	u64 nr_blocked_enqueues;
	u64 nr_blocked_enqueues_donor_cpu;
	u64 nr_blocked_enqueues_owner_cpu;
	u64 nr_blocked_enqueues_other_cpu;
	u64 nr_blocked_wakeups;
};

static bool parse_bool(const char *value, bool *result)
{
	if (!strcasecmp(value, "1") || !strcasecmp(value, "y") ||
	    !strcasecmp(value, "yes") || !strcasecmp(value, "on") ||
	    !strcasecmp(value, "true")) {
		*result = true;
		return true;
	}

	if (!strcasecmp(value, "0") || !strcasecmp(value, "n") ||
	    !strcasecmp(value, "no") || !strcasecmp(value, "off") ||
	    !strcasecmp(value, "false")) {
		*result = false;
		return true;
	}

	return false;
}

static bool cmdline_bool(const char *name, bool default_value)
{
	char cmdline[4096], *newline, *saveptr = NULL, *token;
	size_t name_len = strlen(name);
	bool value = default_value;
	FILE *file;

	file = fopen("/proc/cmdline", "r");
	if (!file)
		return default_value;

	if (!fgets(cmdline, sizeof(cmdline), file)) {
		fclose(file);
		return default_value;
	}
	fclose(file);
	newline = strchr(cmdline, '\n');
	if (newline)
		*newline = '\0';

	for (token = strtok_r(cmdline, " ", &saveptr); token;
	     token = strtok_r(NULL, " ", &saveptr)) {
		bool parsed;

		if (strncmp(token, name, name_len) || token[name_len] != '=')
			continue;
		if (parse_bool(token + name_len + 1, &parsed))
			value = parsed;
	}

	return value;
}

static int module_path(char *path, size_t size)
{
	ssize_t len;
	char *slash;

	len = readlink("/proc/self/exe", path, size - 1);
	if (len < 0)
		return -errno;
	path[len] = '\0';

	slash = strrchr(path, '/');
	if (!slash)
		return -EINVAL;
	*slash = '\0';

	if (snprintf(slash, size - (slash - path), "/%s", MODULE_FILE) >=
	    size - (slash - path))
		return -ENAMETOOLONG;

	return 0;
}

static int load_test_module(bool *loaded_here)
{
	char path[PATH_MAX];
	int fd, err;

	err = module_path(path, sizeof(path));
	if (err)
		return err;

	fd = open(path, O_RDONLY | O_CLOEXEC);
	if (fd < 0)
		return -errno;

	if (syscall(SYS_finit_module, fd, "", 0)) {
		err = errno;
		close(fd);
		if (err == EEXIST)
			return 0;
		return -err;
	}

	close(fd);
	*loaded_here = true;
	return 0;
}

static void unload_test_module(bool loaded_here)
{
	if (loaded_here && syscall(SYS_delete_module, MODULE_NAME, O_NONBLOCK))
		SCX_ERR("Failed to unload %s (%d)", MODULE_NAME, errno);
}

static int pin_to_cpu(int cpu)
{
	cpu_set_t mask;

	CPU_ZERO(&mask);
	CPU_SET(cpu, &mask);
	return sched_setaffinity(0, sizeof(mask), &mask) ? errno : 0;
}

static int select_test_cpus(bool cross_cpu, cpu_set_t *mask, int *donor_cpu,
			    int *owner_cpu)
{
	int cpu, first = -1;

	if (sched_getaffinity(0, sizeof(*mask), mask))
		return -errno;

	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
		if (!CPU_ISSET(cpu, mask))
			continue;
		if (first < 0) {
			first = cpu;
			if (!cross_cpu)
				break;
		} else {
			*donor_cpu = first;
			*owner_cpu = cpu;
			return 0;
		}
	}

	if (first < 0)
		return -ENODEV;
	if (cross_cpu)
		return -EAGAIN;

	*donor_cpu = first;
	*owner_cpu = first;
	return 0;
}

static int set_nice(int nice)
{
	return setpriority(PRIO_PROCESS, 0, nice) ? errno : 0;
}

static bool wait_for_pid(atomic_int *pid)
{
	int waited_ms;

	for (waited_ms = 0; waited_ms < WAIT_TIMEOUT_MS; waited_ms++) {
		if (atomic_load_explicit(pid, memory_order_acquire) > 0)
			return true;
		usleep(WAIT_STEP_US);
	}

	return false;
}

static int wait_for_contenders(struct contender_ctx *contenders,
			       size_t nr_contenders)
{
	size_t i, nr_ready;
	int status, waited_ms;

	for (waited_ms = 0; waited_ms < WAIT_TIMEOUT_MS; waited_ms++) {
		nr_ready = 0;
		for (i = 0; i < nr_contenders; i++) {
			status = atomic_load_explicit(&contenders[i].status,
						      memory_order_acquire);
			if (status < 0)
				return status;
			if (status > 0)
				nr_ready++;
		}
		if (nr_ready == nr_contenders)
			return 1;
		usleep(WAIT_STEP_US);
	}

	return -ETIMEDOUT;
}

static int wait_for_donor_state(struct thread_ctx *ctx, int expected)
{
	int state, waited_ms;

	for (waited_ms = 0; waited_ms < WAIT_TIMEOUT_MS; waited_ms++) {
		state = ioctl(ctx->fd, ENQ_BLOCKED_IOCTL_DONOR_STATE);
		if (state == expected)
			return state;
		if (state < 0 && errno != ENOENT)
			return -errno;
		usleep(WAIT_STEP_US);
	}

	return -ETIMEDOUT;
}

static bool wait_for_donor(struct thread_ctx *ctx, int trial)
{
	int waited_ms;

	for (waited_ms = 0; waited_ms < WAIT_TIMEOUT_MS; waited_ms++) {
		if (atomic_load_explicit(&ctx->donor_completed,
					 memory_order_acquire) >= trial)
			return true;
		if (atomic_load_explicit(&ctx->abort, memory_order_relaxed))
			return false;
		usleep(WAIT_STEP_US);
	}

	return false;
}

static bool wait_for_measurement(struct thread_ctx *ctx)
{
	while (!atomic_load_explicit(&ctx->measurement_ready,
				     memory_order_acquire) &&
	       !atomic_load_explicit(&ctx->abort, memory_order_relaxed))
		sched_yield();

	return !atomic_load_explicit(&ctx->abort, memory_order_relaxed);
}

static void *contender_fn(void *arg)
{
	struct contender_ctx *contender = arg;
	struct thread_ctx *ctx = contender->thread_ctx;
	int err;

	err = pin_to_cpu(contender->cpu);
	if (!err)
		err = set_nice(CONTENDER_NICE);
	atomic_store_explicit(&contender->status, err ? -err : 1,
			      memory_order_release);
	if (err)
		return (void *)(uintptr_t)err;

	while (!atomic_load_explicit(&ctx->stop_contender,
				     memory_order_relaxed))
		;

	return NULL;
}

static void *owner_fn(void *arg)
{
	struct thread_ctx *ctx = arg;
	int err, i;

	err = pin_to_cpu(ctx->owner_cpu);
	if (err)
		return (void *)(uintptr_t)err;
	err = set_nice(OWNER_NICE);
	if (err)
		return (void *)(uintptr_t)err;

	for (i = 0; i < NR_TRIALS; i++) {
		if (ioctl(ctx->fd, ENQ_BLOCKED_IOCTL_OWNER))
			return (void *)(uintptr_t)errno;
		if (!wait_for_donor(ctx, i + 1))
			return (void *)(uintptr_t)ETIMEDOUT;

		if (i + 1 == NR_WARMUP_TRIALS && !wait_for_measurement(ctx))
			return NULL;
	}

	return NULL;
}

static int run_donor_trial(struct thread_ctx *ctx)
{
	int waited_ms;

	for (waited_ms = 0; waited_ms < WAIT_TIMEOUT_MS; waited_ms++) {
		if (!ioctl(ctx->fd, ENQ_BLOCKED_IOCTL_DONOR))
			return 0;
		if (errno != EAGAIN)
			return -errno;
		usleep(WAIT_STEP_US);
	}

	return -ETIMEDOUT;
}

static void *donor_fn(void *arg)
{
	struct thread_ctx *ctx = arg;
	int err, i;

	err = pin_to_cpu(ctx->donor_cpu);
	if (err)
		return (void *)(uintptr_t)err;
	err = set_nice(DONOR_NICE);
	if (err)
		return (void *)(uintptr_t)err;

	atomic_store_explicit(&ctx->donor_pid, syscall(SYS_gettid),
			      memory_order_release);
	while (!atomic_load_explicit(&ctx->start_donor, memory_order_acquire) &&
	       !atomic_load_explicit(&ctx->abort, memory_order_relaxed))
		sched_yield();

	if (atomic_load_explicit(&ctx->abort, memory_order_relaxed))
		return NULL;

	for (i = 0; i < NR_TRIALS; i++) {
		err = run_donor_trial(ctx);
		if (err)
			return (void *)(uintptr_t)-err;
		atomic_store_explicit(&ctx->donor_completed, i + 1,
				      memory_order_release);
	}

	return NULL;
}

static void print_avg_time(const char *name, u64 total_ns, u64 samples)
{
	u64 avg_ns = samples ? total_ns / samples : 0;

	printf("  %s_avg_ns=%llu (%llu.%03llu ms, samples=%llu)\n", name,
	       (unsigned long long)avg_ns,
	       (unsigned long long)(avg_ns / 1000000),
	       (unsigned long long)((avg_ns / 1000) % 1000),
	       (unsigned long long)samples);
}

static void print_avg_delta(const char *name, u64 disabled_total,
			    u64 disabled_samples, u64 enabled_total,
			    u64 enabled_samples)
{
	u64 disabled_avg, enabled_avg;
	s64 delta_ns;
	double delta_pct;

	if (!disabled_samples || !enabled_samples)
		return;

	disabled_avg = disabled_total / disabled_samples;
	enabled_avg = enabled_total / enabled_samples;
	delta_ns = (s64)enabled_avg - (s64)disabled_avg;
	delta_pct = disabled_avg ? 100.0 * delta_ns / disabled_avg : 0.0;

	printf("  %s_delta_ns=%+lld (%+.2f%%)\n", name,
	       (long long)delta_ns, delta_pct);
}

static int join_thread(pthread_t thread, const struct timespec *deadline,
		       int *thread_err)
{
	void *result;
	int err;

	err = pthread_timedjoin_np(thread, &result, deadline);
	if (err)
		return err;

	*thread_err = (int)(uintptr_t)result;
	return 0;
}

static void set_join_deadline(struct timespec *deadline)
{
	clock_gettime(CLOCK_REALTIME, deadline);
	deadline->tv_sec += JOIN_TIMEOUT_MS / 1000;
	deadline->tv_nsec += (JOIN_TIMEOUT_MS % 1000) * 1000000;
	if (deadline->tv_nsec >= 1000000000) {
		deadline->tv_sec++;
		deadline->tv_nsec -= 1000000000;
	}
}

static enum scx_test_status setup(void **ctx)
{
	struct enq_blocked *skel;
	u64 flag;

	skel = enq_blocked__open();
	SCX_FAIL_IF(!skel, "Failed to open skel");
	SCX_ENUM_INIT(skel);

	flag = SCX_OPS_ENQ_BLOCKED;
	if (!flag) {
		enq_blocked__destroy(skel);
		fprintf(stderr, "SKIP: SCX_OPS_ENQ_BLOCKED is unavailable\n");
		return SCX_TEST_SKIP;
	}

	enq_blocked__destroy(skel);
	*ctx = NULL;
	return SCX_TEST_PASS;
}

static enum scx_test_status run_one(bool enq_blocked, bool cross_cpu,
				    struct run_result *result)
{
	struct enq_blocked *skel;
	struct thread_ctx thread_ctx = {};
	struct contender_ctx *contender_ctxs = NULL;
	struct bpf_link *link = NULL;
	pthread_t owner, donor, *contenders = NULL;
	struct timespec join_deadline;
	cpu_set_t allowed_cpus;
	bool module_loaded = false;
	bool owner_started = false, donor_started = false;
	bool join_timed_out = false;
	bool proxy_enabled;
	enum scx_test_status status = SCX_TEST_PASS;
	int cpu, donor_pid, donor_state, err, thread_err;
	size_t i, nr_contenders, nr_contenders_started = 0;
	size_t nr_contenders_joined = 0;
	u64 nr_blocked, nr_blocked_donor_cpu, nr_blocked_owner_cpu;
	u64 nr_blocked_other_cpu, nr_blocked_wakeups;
	struct enq_blocked_stats stats;

	err = select_test_cpus(cross_cpu, &allowed_cpus, &thread_ctx.donor_cpu,
			       &thread_ctx.owner_cpu);
	if (err == -EAGAIN) {
		fprintf(stderr, "SKIP: cross-CPU case requires two allowed CPUs\n");
		return SCX_TEST_SKIP;
	}
	if (err) {
		SCX_ERR("Failed to select test CPUs (%d)", -err);
		return SCX_TEST_FAIL;
	}
	nr_contenders = CPU_COUNT(&allowed_cpus);
	contenders = calloc(nr_contenders, sizeof(*contenders));
	contender_ctxs = calloc(nr_contenders, sizeof(*contender_ctxs));
	if (!contenders || !contender_ctxs) {
		SCX_ERR("Failed to allocate %zu contender threads", nr_contenders);
		status = SCX_TEST_FAIL;
		goto out_contenders;
	}

	skel = enq_blocked__open();
	if (!skel) {
		SCX_ERR("Failed to open skel");
		status = SCX_TEST_FAIL;
		goto out_contenders;
	}
	SCX_ENUM_INIT(skel);
	skel->struct_ops.enq_blocked_ops->flags =
		SCX_OPS_ENQ_LAST |
		(enq_blocked ? SCX_OPS_ENQ_BLOCKED : 0);
	if (enq_blocked__load(skel)) {
		SCX_ERR("Failed to load skel");
		status = SCX_TEST_FAIL;
		goto out_skel;
	}

	err = load_test_module(&module_loaded);
	if (err == -EPERM || err == -ENOENT) {
		fprintf(stderr, "SKIP: cannot load mutex fixture (%d)\n", -err);
		status = SCX_TEST_SKIP;
		goto out_skel;
	}
	if (err) {
		SCX_ERR("Failed to load mutex fixture (%d)", -err);
		status = SCX_TEST_FAIL;
		goto out_skel;
	}

	thread_ctx.fd = open(DEVICE_PATH, O_RDONLY | O_CLOEXEC);
	if (thread_ctx.fd < 0) {
		SCX_ERR("Failed to open %s (%d)", DEVICE_PATH, errno);
		status = SCX_TEST_FAIL;
		goto out_module;
	}
	err = ioctl(thread_ctx.fd, ENQ_BLOCKED_IOCTL_PROXY_SUPPORTED);
	if (err < 0) {
		SCX_ERR("Failed to query proxy-exec support (%d)", errno);
		status = SCX_TEST_FAIL;
		goto out_fd;
	}
	proxy_enabled = err && cmdline_bool("sched_proxy_exec", true);
	if (!proxy_enabled) {
		fprintf(stderr, "SKIP: proxy execution is not enabled\n");
		status = SCX_TEST_SKIP;
		goto out_fd;
	}
	if (ioctl(thread_ctx.fd, ENQ_BLOCKED_IOCTL_RESET_STATS)) {
		SCX_ERR("Failed to reset mutex statistics (%d)", errno);
		status = SCX_TEST_FAIL;
		goto out_fd;
	}

	if (ioctl(thread_ctx.fd, ENQ_BLOCKED_IOCTL_PREP_ATTACH)) {
		SCX_ERR("Failed to prepare scheduler attachment (%d)", errno);
		status = SCX_TEST_FAIL;
		goto out_fd;
	}

	err = pthread_create(&owner, NULL, owner_fn, &thread_ctx);
	if (err) {
		SCX_ERR("Failed to create owner thread (%d)", err);
		status = SCX_TEST_FAIL;
		goto out;
	}
	owner_started = true;

	err = pthread_create(&donor, NULL, donor_fn, &thread_ctx);
	if (err) {
		SCX_ERR("Failed to create donor thread (%d)", err);
		status = SCX_TEST_FAIL;
		goto out;
	}
	donor_started = true;

	if (!wait_for_pid(&thread_ctx.donor_pid)) {
		SCX_ERR("Timed out waiting for donor thread");
		status = SCX_TEST_FAIL;
		goto out;
	}

	donor_pid = atomic_load_explicit(&thread_ctx.donor_pid,
					 memory_order_acquire);
	skel->bss->donor_pid = donor_pid;
	skel->data->donor_cpu = thread_ctx.donor_cpu;
	skel->data->owner_cpu = thread_ctx.owner_cpu;
	atomic_store_explicit(&thread_ctx.start_donor, true,
			      memory_order_release);

	donor_state = ENQ_BLOCKED_DONOR_SLEEPING;
	if (proxy_enabled)
		donor_state |= ENQ_BLOCKED_DONOR_ON_RQ;
	err = wait_for_donor_state(&thread_ctx, donor_state);
	if (err < 0) {
		SCX_ERR("Donor did not block before scheduler attachment (%d)", -err);
		status = SCX_TEST_FAIL;
		goto out;
	}

	link = bpf_map__attach_struct_ops(skel->maps.enq_blocked_ops);
	if (!link) {
		SCX_ERR("Failed to attach scheduler");
		status = SCX_TEST_FAIL;
		goto out;
	}

	/* Scheduler ownership changes start from a fully blocked donor. */
	donor_state = ENQ_BLOCKED_DONOR_SLEEPING;
	err = wait_for_donor_state(&thread_ctx, donor_state);
	if (err < 0) {
		SCX_ERR("Unexpected donor state after scheduler attachment (%d)",
			-err);
		status = SCX_TEST_FAIL;
		goto out;
	}

	if (ioctl(thread_ctx.fd, ENQ_BLOCKED_IOCTL_ATTACH_DONE)) {
		SCX_ERR("Failed to complete scheduler attachment (%d)", errno);
		status = SCX_TEST_FAIL;
		goto out;
	}

	i = 0;
	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
		if (!CPU_ISSET(cpu, &allowed_cpus))
			continue;

		contender_ctxs[i].thread_ctx = &thread_ctx;
		contender_ctxs[i].cpu = cpu;
		atomic_init(&contender_ctxs[i].status, 0);
		err = pthread_create(&contenders[i], NULL, contender_fn,
				     &contender_ctxs[i]);
		if (err) {
			SCX_ERR("Failed to create contender for CPU %d (%d)",
				cpu, err);
			status = SCX_TEST_FAIL;
			goto out;
		}
		nr_contenders_started++;
		i++;
	}

	err = wait_for_contenders(contender_ctxs, nr_contenders);
	if (err != 1) {
		SCX_ERR("Contender threads failed (%d)", -err);
		status = SCX_TEST_FAIL;
		goto out;
	}

	/*
	 * The first trial spans scheduler attachment and validates the state
	 * transition, but including it would skew scheduling latency. Exclude it
	 * from both the mutex and BPF enqueue measurements.
	 */
	if (!wait_for_donor(&thread_ctx, NR_WARMUP_TRIALS)) {
		SCX_ERR("Timed out waiting for warm-up trial");
		status = SCX_TEST_FAIL;
		goto out;
	}
	if (ioctl(thread_ctx.fd, ENQ_BLOCKED_IOCTL_RESET_STATS)) {
		SCX_ERR("Failed to reset mutex statistics after warm-up (%d)",
			errno);
		status = SCX_TEST_FAIL;
		goto out;
	}
	skel->bss->nr_blocked_enqueues = 0;
	skel->bss->nr_blocked_enqueues_donor_cpu = 0;
	skel->bss->nr_blocked_enqueues_owner_cpu = 0;
	skel->bss->nr_blocked_enqueues_other_cpu = 0;
	skel->bss->nr_blocked_wakeups = 0;
	atomic_store_explicit(&thread_ctx.measurement_ready, true,
			      memory_order_release);

out:
	ioctl(thread_ctx.fd, ENQ_BLOCKED_IOCTL_ATTACH_DONE);
	if (status != SCX_TEST_PASS) {
		atomic_store_explicit(&thread_ctx.abort, true, memory_order_release);
		atomic_store_explicit(&thread_ctx.start_donor, true,
				      memory_order_release);
		atomic_store_explicit(&thread_ctx.measurement_ready, true,
				      memory_order_release);
	}

	set_join_deadline(&join_deadline);
	if (donor_started) {
		err = join_thread(donor, &join_deadline, &thread_err);
		if (err == ETIMEDOUT) {
			SCX_ERR("Timed out waiting for donor thread");
			join_timed_out = true;
			status = SCX_TEST_FAIL;
		} else if (err) {
			SCX_ERR("Failed to join donor thread (%d)", err);
			status = SCX_TEST_FAIL;
		} else {
			donor_started = false;
			if (thread_err) {
				SCX_ERR("Donor thread failed (%d)", thread_err);
				status = SCX_TEST_FAIL;
			}
		}
	}
	if (!join_timed_out && owner_started) {
		err = join_thread(owner, &join_deadline, &thread_err);
		if (err == ETIMEDOUT) {
			SCX_ERR("Timed out waiting for owner thread");
			join_timed_out = true;
			status = SCX_TEST_FAIL;
		} else if (err) {
			SCX_ERR("Failed to join owner thread (%d)", err);
			status = SCX_TEST_FAIL;
		} else {
			owner_started = false;
			if (thread_err) {
				SCX_ERR("Owner thread failed (%d)", thread_err);
				status = SCX_TEST_FAIL;
			}
		}
	}
	atomic_store_explicit(&thread_ctx.stop_contender, true,
			      memory_order_release);
	for (i = 0; !join_timed_out && i < nr_contenders_started; i++) {
		err = join_thread(contenders[i], &join_deadline, &thread_err);
		if (err == ETIMEDOUT) {
			SCX_ERR("Timed out waiting for contender on CPU %d",
				contender_ctxs[i].cpu);
			join_timed_out = true;
			status = SCX_TEST_FAIL;
		} else if (err) {
			SCX_ERR("Failed to join contender on CPU %d (%d)",
				contender_ctxs[i].cpu, err);
			status = SCX_TEST_FAIL;
		} else {
			nr_contenders_joined++;
			if (thread_err) {
				SCX_ERR("Contender on CPU %d failed (%d)",
					contender_ctxs[i].cpu, thread_err);
				status = SCX_TEST_FAIL;
			}
		}
	}

	/* Restore the fair scheduler before waiting for any stranded thread. */
	if (join_timed_out) {
		atomic_store_explicit(&thread_ctx.abort, true,
				      memory_order_release);
		if (link) {
			bpf_link__destroy(link);
			link = NULL;
		}
		if (donor_started)
			pthread_join(donor, NULL);
		if (owner_started)
			pthread_join(owner, NULL);
		for (i = nr_contenders_joined;
		     i < nr_contenders_started; i++)
			pthread_join(contenders[i], NULL);
	}

	if (ioctl(thread_ctx.fd, ENQ_BLOCKED_IOCTL_GET_STATS, &stats)) {
		SCX_ERR("Failed to read mutex statistics (%d)", errno);
		status = SCX_TEST_FAIL;
	} else {
		result->stats = stats;
		printf("\n[topology=%s SCX_OPS_ENQ_BLOCKED=%s]\n",
		       cross_cpu ? "cross-cpu" : "same-cpu",
		       enq_blocked ? "enabled" : "disabled");
		printf("  proxy_exec=%s\n",
		       proxy_enabled ? "enabled" : "disabled");
		printf("  donor_cpu=%d\n", thread_ctx.donor_cpu);
		printf("  owner_cpu=%d\n", thread_ctx.owner_cpu);
		printf("  nr_contenders=%zu\n", nr_contenders);
		printf("  measured_trials=%d\n", NR_MEASURED_TRIALS);
		printf("  owner_nice=%d\n", OWNER_NICE);
		printf("  donor_nice=%d\n", DONOR_NICE);
		printf("  contender_nice=%d\n", CONTENDER_NICE);
		print_avg_time("mutex_hold", stats.hold_time_ns, stats.nr_holds);
		print_avg_time("mutex_wait", stats.wait_time_ns, stats.nr_waits);
		if (stats.nr_holds != NR_MEASURED_TRIALS ||
		    stats.nr_waits != NR_MEASURED_TRIALS) {
			SCX_ERR("Expected %d measured trials, got %llu holds and %llu waits",
				NR_MEASURED_TRIALS,
				(unsigned long long)stats.nr_holds,
				(unsigned long long)stats.nr_waits);
			status = SCX_TEST_FAIL;
		}
	}

	nr_blocked = skel->bss->nr_blocked_enqueues;
	nr_blocked_donor_cpu = skel->bss->nr_blocked_enqueues_donor_cpu;
	nr_blocked_owner_cpu = skel->bss->nr_blocked_enqueues_owner_cpu;
	nr_blocked_other_cpu = skel->bss->nr_blocked_enqueues_other_cpu;
	nr_blocked_wakeups = skel->bss->nr_blocked_wakeups;
	result->nr_blocked_enqueues = nr_blocked;
	result->nr_blocked_enqueues_donor_cpu = nr_blocked_donor_cpu;
	result->nr_blocked_enqueues_owner_cpu = nr_blocked_owner_cpu;
	result->nr_blocked_enqueues_other_cpu = nr_blocked_other_cpu;
	result->nr_blocked_wakeups = nr_blocked_wakeups;
	printf("  nr_blocked_enqueues=%llu\n",
	       (unsigned long long)nr_blocked);
	printf("  nr_blocked_enqueues_donor_cpu=%llu\n",
	       (unsigned long long)nr_blocked_donor_cpu);
	printf("  nr_blocked_enqueues_owner_cpu=%llu\n",
	       (unsigned long long)nr_blocked_owner_cpu);
	printf("  nr_blocked_enqueues_other_cpu=%llu\n",
	       (unsigned long long)nr_blocked_other_cpu);
	printf("  nr_blocked_wakeups=%llu\n",
	       (unsigned long long)nr_blocked_wakeups);
	if (status == SCX_TEST_PASS) {
		if (enq_blocked && proxy_enabled && !nr_blocked) {
			SCX_ERR("ops.enqueue() did not receive the blocked donor");
			status = SCX_TEST_FAIL;
		} else if ((!enq_blocked || !proxy_enabled) && nr_blocked) {
			SCX_ERR("ops.enqueue() unexpectedly received %llu blocked donors",
				(unsigned long long)nr_blocked);
			status = SCX_TEST_FAIL;
		} else if (nr_blocked_wakeups) {
			SCX_ERR("Ordinary wakeups received %llu blocked enqueue flags",
				(unsigned long long)nr_blocked_wakeups);
			status = SCX_TEST_FAIL;
		} else if (nr_blocked_other_cpu) {
			SCX_ERR("Blocked donor had %llu enqueues on unexpected CPUs",
				(unsigned long long)nr_blocked_other_cpu);
			status = SCX_TEST_FAIL;
		}
	}

	if (skel->data->uei.kind != EXIT_KIND(SCX_EXIT_NONE)) {
		SCX_ERR("Scheduler exited unexpectedly (kind=%llu code=%lld)",
			(unsigned long long)skel->data->uei.kind,
			(long long)skel->data->uei.exit_code);
		status = SCX_TEST_FAIL;
	}

	if (link)
		bpf_link__destroy(link);
out_fd:
	close(thread_ctx.fd);
out_module:
	unload_test_module(module_loaded);
out_skel:
	enq_blocked__destroy(skel);
out_contenders:
	free(contender_ctxs);
	free(contenders);
	return status;
}

static enum scx_test_status run_topology(bool cross_cpu)
{
	struct run_result disabled = {}, enabled = {};
	enum scx_test_status status;

	status = run_one(false, cross_cpu, &disabled);
	if (status != SCX_TEST_PASS)
		return status;

	status = run_one(true, cross_cpu, &enabled);
	if (status != SCX_TEST_PASS)
		return status;

	printf("\n[topology=%s delta: enabled - disabled]\n",
	       cross_cpu ? "cross-cpu" : "same-cpu");
	print_avg_delta("mutex_hold", disabled.stats.hold_time_ns,
			disabled.stats.nr_holds, enabled.stats.hold_time_ns,
			enabled.stats.nr_holds);
	print_avg_delta("mutex_wait", disabled.stats.wait_time_ns,
			disabled.stats.nr_waits, enabled.stats.wait_time_ns,
			enabled.stats.nr_waits);

	return SCX_TEST_PASS;
}

static enum scx_test_status run(void *ctx)
{
	enum scx_test_status status;

	(void)ctx;

	status = run_topology(false);
	if (status != SCX_TEST_PASS)
		return status;

	status = run_topology(true);
	if (status == SCX_TEST_SKIP)
		return SCX_TEST_PASS;

	return status;
}

struct scx_test enq_blocked = {
	.name = "enq_blocked",
	.description = "Verify proxy donor admission under CPU-wide contention",
	.setup = setup,
	.run = run,
};

REGISTER_SCX_TEST(&enq_blocked)