xref: /linux/tools/sched_ext/include/scx/common.h (revision 566ab427f827b0256d3e8ce0235d088e6a9c28bd)
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 
20 typedef uint8_t u8;
21 typedef uint16_t u16;
22 typedef uint32_t u32;
23 typedef uint64_t u64;
24 typedef int8_t s8;
25 typedef int16_t s16;
26 typedef int32_t s32;
27 typedef int64_t s64;
28 
29 #define SCX_BUG(__fmt, ...)							\
30 	do {									\
31 		fprintf(stderr, "[SCX_BUG] %s:%d", __FILE__, __LINE__);		\
32 		if (errno)							\
33 			fprintf(stderr, " (%s)\n", strerror(errno));		\
34 		else								\
35 			fprintf(stderr, "\n");					\
36 		fprintf(stderr, __fmt __VA_OPT__(,) __VA_ARGS__);		\
37 		fprintf(stderr, "\n");						\
38 										\
39 		exit(EXIT_FAILURE);						\
40 	} while (0)
41 
42 #define SCX_BUG_ON(__cond, __fmt, ...)					\
43 	do {								\
44 		if (__cond)						\
45 			SCX_BUG((__fmt) __VA_OPT__(,) __VA_ARGS__);	\
46 	} while (0)
47 
48 /**
49  * RESIZE_ARRAY - Convenience macro for resizing a BPF array
50  * @__skel: the skeleton containing the array
51  * @elfsec: the data section of the BPF program in which the array exists
52  * @arr: the name of the array
53  * @n: the desired array element count
54  *
55  * For BPF arrays declared with RESIZABLE_ARRAY(), this macro performs two
56  * operations. It resizes the map which corresponds to the custom data
57  * section that contains the target array. As a side effect, the BTF info for
58  * the array is adjusted so that the array length is sized to cover the new
59  * data section size. The second operation is reassigning the skeleton pointer
60  * for that custom data section so that it points to the newly memory mapped
61  * region.
62  */
63 #define RESIZE_ARRAY(__skel, elfsec, arr, n)						\
64 	do {										\
65 		size_t __sz;								\
66 		bpf_map__set_value_size((__skel)->maps.elfsec##_##arr,			\
67 				sizeof((__skel)->elfsec##_##arr->arr[0]) * (n));	\
68 		(__skel)->elfsec##_##arr =						\
69 			bpf_map__initial_value((__skel)->maps.elfsec##_##arr, &__sz);	\
70 	} while (0)
71 
72 #include "user_exit_info.h"
73 #include "compat.h"
74 
75 #endif	/* __SCHED_EXT_COMMON_H */
76