scx_lavd 1.1.0

A Latency-criticality Aware Virtual Deadline (LAVD) scheduler based on sched_ext, which is a Linux kernel feature which enables implementing kernel thread schedulers in BPF and dynamically loading them. https://github.com/sched-ext/scx/tree/main
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
/*
 * SPDX-License-Identifier: GPL-2.0
 * Copyright (c) 2025 Meta Platforms, Inc. and affiliates.
 * Copyright (c) 2025 Daniel Hodges <hodgesd@meta.com>
 */

#include <scx/common.bpf.h>

#include <lib/arena.h>
#include <lib/percpu.h>
#include <lib/cpumask.h>
#include <lib/topology.h>

#include "selftest.h"

#define SCX_TOPOLOGY_SELFTEST(suffix) SCX_SELFTEST(scx_selftest_arena_topology_timer_ ## suffix)

struct test_timer {
	struct bpf_timer timer;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, u32);
	__type(value, struct test_timer);
} topology_timer_map SEC(".maps");

volatile bool timer_callback_executed = false;
volatile bool topology_accessed_in_timer = false;

struct topology_data {
	u64 magic_value;
	u32 access_count;
	u32 cpu_id;
};

static struct topology_data __arena * topo_get_data(topo_ptr topo)
{
	if (!topo || !topo->data)
		return NULL;

	return (struct topology_data __arena *)topo->data;
}

static int topo_update_data(topo_ptr topo, u64 magic, u32 cpu)
{
	struct topology_data __arena *data = topo_get_data(topo);

	if (!data)
		return -EINVAL;

	data->magic_value = magic;
	data->access_count++;
	data->cpu_id = cpu;

	return 0;
}

static bool topo_verify_data(topo_ptr topo, u64 expected_magic)
{
	struct topology_data __arena *data = topo_get_data(topo);

	if (!data)
		return false;

	return data->magic_value == expected_magic && data->access_count > 0;
}

volatile bool timer_with_helpers_executed = false;
volatile u64 timer_arena_data_magic = 0;
volatile u32 timer_arena_data_count = 0;

static int topology_timer_callback(void *map, int *key, struct bpf_timer *timer)
{
	topo_ptr topo;
	int i;

	timer_callback_executed = true;

	if (!topo_all) {
		bpf_printk("TOPO TIMER: topo_all is NULL");
		return 0;
	}

	if (topo_all->mask && topo_all->nr_children > 0) {
		topology_accessed_in_timer = true;

		for (i = 0; i < topo_all->nr_children && i < TOPO_MAX_CHILDREN && can_loop; i++) {
			topo = topo_all->children[i];
			/* make sure we can deference arena pointers */
			if (topo && topo->mask) {
				break;
			}
		}
	}

	bpf_printk("TOPO TIMER: accessed topology, children=%d",
		   topo_all->nr_children);

	return 0;
}

static int topology_timer_callback_with_helpers(void *map, int *key,
						struct bpf_timer *timer)
{
	struct topology_data __arena *data;
	topo_ptr child;
	struct topo_iter iter;
	u64 expected_magic = 0xCAFEBABE00000000ULL;
	int ret, i;

	timer_with_helpers_executed = true;

	if (!topo_all) {
		bpf_printk("TOPO TIMER HELPERS: topo_all is NULL");
		return 0;
	}

	data = topo_get_data(topo_all);
	if (!data) {
		bpf_printk("TOPO TIMER HELPERS: failed to get data via helper");
		return 0;
	}

	timer_arena_data_magic = data->magic_value;
	timer_arena_data_count = data->access_count;

	ret = topo_update_data(topo_all, expected_magic + 100, bpf_get_smp_processor_id());
	if (ret) {
		bpf_printk("TOPO TIMER HELPERS: failed to update data: %d", ret);
		return 0;
	}

	if (!topo_verify_data(topo_all, expected_magic + 100)) {
		bpf_printk("TOPO TIMER HELPERS: data verification failed");
		return 0;
	}

	i = 0;
	TOPO_FOR_EACH_CPU(&iter, child) {
		if (i >= 2)  /* Limit iteration */
			break;

		data = topo_get_data(child);
		if (data) {
			bpf_printk("TOPO TIMER HELPERS: child %d magic=0x%llx count=%d",
				   i, data->magic_value, data->access_count);
		}
		i++;
	}

	bpf_printk("TOPO TIMER HELPERS: test passed, accessed %d children", i);

	return 0;
}

__weak
int scx_selftest_arena_topology_timer_timer_access(void)
{
	struct test_timer *timer_elem;
	u32 timer_key = 0;
	int ret;

	timer_callback_executed = false;
	topology_accessed_in_timer = false;

	if (!topo_all) {
		bpf_printk("TOPO TIMER: topology not initialized");
		return -EINVAL;
	}

	timer_elem = bpf_map_lookup_elem(&topology_timer_map, &timer_key);
	if (!timer_elem) {
		bpf_printk("TOPO TIMER: failed to lookup timer");
		return -EINVAL;
	}

	ret = bpf_timer_init(&timer_elem->timer, &topology_timer_map, CLOCK_MONOTONIC);
	if (ret) {
		bpf_printk("TOPO TIMER: bpf_timer_init failed: %d", ret);
		return ret;
	}

	ret = bpf_timer_set_callback(&timer_elem->timer, topology_timer_callback);
	if (ret) {
		bpf_printk("TOPO TIMER: bpf_timer_set_callback failed: %d", ret);
		return ret;
	}

	ret = bpf_timer_start(&timer_elem->timer, 0, 0);
	if (ret) {
		bpf_printk("TOPO TIMER: bpf_timer_start failed: %d", ret);
		return ret;
	}

	/*
	 * Give the timer a chance to fire. BPF timers run asynchronously in
	 * softirq context, so we need to yield/wait for it to execute.
	 * Use a simple busy-wait loop to allow the callback to run.
	 */
	bpf_for(ret, 0, 1000000) {
		if (timer_callback_executed)
			break;
	}

	/*
	 * Verify that:
	 * 1. The callback was executed
	 * 2. Arena topology was successfully accessed from the callback
	 */
	if (!timer_callback_executed) {
		bpf_printk("TOPO TIMER: callback not executed after waiting");
		return -EINVAL;
	}

	if (!topology_accessed_in_timer) {
		bpf_printk("TOPO TIMER: failed to access topology in callback");
		return -EINVAL;
	}

	ret = bpf_timer_cancel(&timer_elem->timer);
	if (ret && ret != -EDEADLK) {
		bpf_printk("TOPO TIMER: bpf_timer_cancel failed: %d", ret);
	}

	bpf_printk("TOPO TIMER: test passed");
	return 0;
}

__weak
int scx_selftest_arena_topology_timer_timer_with_helpers(void)
{
	struct topology_data __arena *data;
	struct test_timer *timer_elem;
	struct topo_iter iter;
	topo_ptr child;
	u32 timer_key = 0;
	int ret, i;
	u64 magic = 0xCAFEBABE00000000ULL;

	timer_with_helpers_executed = false;
	timer_arena_data_magic = 0;
	timer_arena_data_count = 0;

	if (!topo_all) {
		bpf_printk("TOPO TIMER HELPERS: topology not initialized");
		return -EINVAL;
	}

	data = (struct topology_data __arena *)scx_static_alloc(sizeof(struct topology_data), 8);
	if (!data) {
		bpf_printk("TOPO TIMER HELPERS: failed to allocate arena data");
		return -ENOMEM;
	}

	data->magic_value = magic;
	data->access_count = 0;
	data->cpu_id = 0;
	topo_all->data = (void __arena *)data;

	i = 0;
	TOPO_FOR_EACH_CPU(&iter, child) {
		struct topology_data __arena *child_data;

		if (i >= 2)
			break;

		child_data = (struct topology_data __arena *)
			scx_static_alloc(sizeof(struct topology_data), 8);
		if (!child_data)
			continue;

		child_data->magic_value = magic + i + 1;
		child_data->access_count = i;
		child_data->cpu_id = child->id;
		child->data = (void __arena *)child_data;
		i++;
	}

	timer_elem = bpf_map_lookup_elem(&topology_timer_map, &timer_key);
	if (!timer_elem) {
		bpf_printk("TOPO TIMER HELPERS: failed to lookup timer");
		return -EINVAL;
	}

	/* Cancel any existing timer first (from previous test) */
	bpf_timer_cancel(&timer_elem->timer);

	ret = bpf_timer_init(&timer_elem->timer, &topology_timer_map, CLOCK_MONOTONIC);
	if (ret) {
		bpf_printk("TOPO TIMER HELPERS: bpf_timer_init failed: %d", ret);
		return ret;
	}

	ret = bpf_timer_set_callback(&timer_elem->timer, topology_timer_callback_with_helpers);
	if (ret) {
		bpf_printk("TOPO TIMER HELPERS: bpf_timer_set_callback failed: %d", ret);
		return ret;
	}

	ret = bpf_timer_start(&timer_elem->timer, 0, 0);
	if (ret) {
		bpf_printk("TOPO TIMER HELPERS: bpf_timer_start failed: %d", ret);
		return ret;
	}

	bpf_for(ret, 0, 1000000) {
		if (timer_with_helpers_executed)
			break;
	}

	if (!timer_with_helpers_executed) {
		bpf_printk("TOPO TIMER HELPERS: callback not executed after waiting");
		return -EINVAL;
	}

	if (timer_arena_data_magic == 0) {
		bpf_printk("TOPO TIMER HELPERS: failed to read arena data in timer");
		return -EINVAL;
	}

	data = topo_get_data(topo_all);
	if (!data || data->magic_value != magic + 100) {
		bpf_printk("TOPO TIMER HELPERS: arena data not updated correctly");
		return -EINVAL;
	}

	if (data->access_count < 1) {
		bpf_printk("TOPO TIMER HELPERS: access count not incremented");
		return -EINVAL;
	}

	bpf_printk("TOPO TIMER HELPERS: test passed - helpers work in timer context");
	return 0;
}

__weak
int scx_selftest_arena_topology_timer_arena_data(void)
{
	struct topology_data __arena *data;
	topo_ptr child;
	struct topo_iter iter;
	int ret, i;
	u64 magic = 0xDEADBEEF12345678ULL;

	if (!topo_all) {
		bpf_printk("TOPO ARENA DATA: topology not initialized");
		return -EINVAL;
	}

	/*
	 * Allocate arena data for the root topology node.
	 * Note: In production code, this would typically be done during
	 * topology initialization via topo_init() with a non-zero data_size.
	 */
	data = (struct topology_data __arena *)scx_static_alloc(sizeof(struct topology_data), 8);
	if (!data) {
		bpf_printk("TOPO ARENA DATA: failed to allocate arena data");
		return -ENOMEM;
	}

	data->magic_value = magic;
	data->access_count = 0;
	data->cpu_id = 0;

	topo_all->data = (void __arena *)data;

	data = topo_get_data(topo_all);
	if (!data) {
		bpf_printk("TOPO ARENA DATA: failed to get data via helper");
		return -EINVAL;
	}

	if (data->magic_value != magic) {
		bpf_printk("TOPO ARENA DATA: magic mismatch got=%llx want=%llx",
			   data->magic_value, magic);
		return -EINVAL;
	}

	ret = topo_update_data(topo_all, magic + 1, bpf_get_smp_processor_id());
	if (ret) {
		bpf_printk("TOPO ARENA DATA: failed to update data: %d", ret);
		return ret;
	}

	if (!topo_verify_data(topo_all, magic + 1)) {
		bpf_printk("TOPO ARENA DATA: data verification failed after update");
		return -EINVAL;
	}

	data = topo_get_data(topo_all);
	if (data->access_count != 1) {
		bpf_printk("TOPO ARENA DATA: unexpected access count: %d", data->access_count);
		return -EINVAL;
	}

	i = 0;
	TOPO_FOR_EACH_CPU(&iter, child) {
		struct topology_data __arena *child_data;

		if (i >= 2)  /* Limit iteration for testing */
			break;

		child_data = (struct topology_data __arena *)
			scx_static_alloc(sizeof(struct topology_data), 8);
		if (!child_data) {
			bpf_printk("TOPO ARENA DATA: failed to allocate child data");
			continue;
		}

		child_data->magic_value = magic + i + 2;
		child_data->access_count = 0;
		child_data->cpu_id = child->id;
		child->data = (void __arena *)child_data;

		ret = topo_update_data(child, magic + i + 2, child->id);
		if (ret) {
			bpf_printk("TOPO ARENA DATA: failed to update child %d data", i);
			return ret;
		}

		if (!topo_verify_data(child, magic + i + 2)) {
			bpf_printk("TOPO ARENA DATA: child %d verification failed", i);
			return -EINVAL;
		}

		i++;
	}

	bpf_printk("TOPO ARENA DATA: test passed, processed %d nodes", i + 1);
	return 0;
}


__weak
int scx_selftest_arena_topology_timer(void)
{
	// Assume topology has been initialized before the test
	if (!topo_all) {
		bpf_printk("TOPO: failed to initialize topology");
		return -EINVAL;
	}

	SCX_TOPOLOGY_SELFTEST(timer_access);
	SCX_TOPOLOGY_SELFTEST(timer_with_helpers);
	SCX_TOPOLOGY_SELFTEST(arena_data);

	return 0;
}