1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4  * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
5  * Copyright (c) 2023 David Vernet <dvernet@meta.com>
6  */
7 #ifndef __SCHED_EXT_COMMON_H
8 #define __SCHED_EXT_COMMON_H
9 
10 #ifdef __KERNEL__
11 #error "Should not be included by BPF programs"
12 #endif
13 
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdint.h>
18 #include <errno.h>
19 #include "enum_defs.autogen.h"
20 
21 typedef uint8_t u8;
22 typedef uint16_t u16;
23 typedef uint32_t u32;
24 typedef uint64_t u64;
25 typedef int8_t s8;
26 typedef int16_t s16;
27 typedef int32_t s32;
28 typedef int64_t s64;
29 
30 #define SCX_BUG(__fmt, ...)							\
31 	do {									\
32 		fprintf(stderr, "[SCX_BUG] %s:%d", __FILE__, __LINE__);		\
33 		if (errno)							\
34 			fprintf(stderr, " (%s)\n", strerror(errno));		\
35 		else								\
36 			fprintf(stderr, "\n");					\
37 		fprintf(stderr, __fmt __VA_OPT__(,) __VA_ARGS__);		\
38 		fprintf(stderr, "\n");						\
39 										\
40 		exit(EXIT_FAILURE);						\
41 	} while (0)
42 
43 #define SCX_BUG_ON(__cond, __fmt, ...)					\
44 	do {								\
45 		if (__cond)						\
46 			SCX_BUG((__fmt) __VA_OPT__(,) __VA_ARGS__);	\
47 	} while (0)
48 
49 /**
50  * RESIZE_ARRAY - Convenience macro for resizing a BPF array
51  * @__skel: the skeleton containing the array
52  * @elfsec: the data section of the BPF program in which the array exists
53  * @arr: the name of the array
54  * @n: the desired array element count
55  *
56  * For BPF arrays declared with RESIZABLE_ARRAY(), this macro performs two
57  * operations. It resizes the map which corresponds to the custom data
58  * section that contains the target array. As a side effect, the BTF info for
59  * the array is adjusted so that the array length is sized to cover the new
60  * data section size. The second operation is reassigning the skeleton pointer
61  * for that custom data section so that it points to the newly memory mapped
62  * region.
63  */
64 #define RESIZE_ARRAY(__skel, elfsec, arr, n)						\
65 	do {										\
66 		size_t __sz;								\
67 		bpf_map__set_value_size((__skel)->maps.elfsec##_##arr,			\
68 				sizeof((__skel)->elfsec##_##arr->arr[0]) * (n));	\
69 		(__skel)->elfsec##_##arr =						\
70 			bpf_map__initial_value((__skel)->maps.elfsec##_##arr, &__sz);	\
71 	} while (0)
72 
73 #include "user_exit_info.h"
74 #include "compat.h"
75 #include "enums.h"
76 
77 /* not available when building kernel tools/sched_ext */
78 #if __has_include(<lib/sdt_task.h>)
79 #include <lib/sdt_task.h>
80 #endif
81 
82 #endif	/* __SCHED_EXT_COMMON_H */
83