xref: /linux/tools/sched_ext/include/scx/user_exit_info.h (revision a23cd25baed2316e50597f8b67192bdc904f955b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Define struct user_exit_info which is shared between BPF and userspace parts
4  * to communicate exit status and other information.
5  *
6  * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
7  * Copyright (c) 2022 Tejun Heo <tj@kernel.org>
8  * Copyright (c) 2022 David Vernet <dvernet@meta.com>
9  */
10 #ifndef __USER_EXIT_INFO_H
11 #define __USER_EXIT_INFO_H
12 
13 #include <stdio.h>
14 #include <stdbool.h>
15 
16 #include "user_exit_info_common.h"
17 
18 /* no need to call the following explicitly if SCX_OPS_LOAD() is used */
19 #define UEI_SET_SIZE(__skel, __ops_name, __uei_name) ({					\
20 	u32 __len = (__skel)->struct_ops.__ops_name->exit_dump_len ?: UEI_DUMP_DFL_LEN;	\
21 	(__skel)->rodata->__uei_name##_dump_len = __len;				\
22 	RESIZE_ARRAY((__skel), data, __uei_name##_dump, __len);				\
23 })
24 
25 #define UEI_EXITED(__skel, __uei_name) ({					\
26 	/* use __sync to force memory barrier */				\
27 	__sync_val_compare_and_swap(&(__skel)->data->__uei_name.kind, -1, -1);	\
28 })
29 
30 #define UEI_REPORT(__skel, __uei_name) ({					\
31 	struct user_exit_info *__uei = &(__skel)->data->__uei_name;		\
32 	char *__uei_dump = (__skel)->data_##__uei_name##_dump->__uei_name##_dump; \
33 	if (__uei_dump[0] != '\0') {						\
34 		fputs("\nDEBUG DUMP\n", stderr);				\
35 		fputs("================================================================================\n\n", stderr); \
36 		fputs(__uei_dump, stderr);					\
37 		fputs("\n================================================================================\n\n", stderr); \
38 	}									\
39 	fprintf(stderr, "EXIT: %s", __uei->reason);				\
40 	if (__uei->msg[0] != '\0')						\
41 		fprintf(stderr, " (%s)", __uei->msg);				\
42 	fputs("\n", stderr);							\
43 	__uei->exit_code;							\
44 })
45 
46 /*
47  * We can't import vmlinux.h while compiling user C code. Let's duplicate
48  * scx_exit_code definition.
49  */
50 enum scx_exit_code {
51 	/* Reasons */
52 	SCX_ECODE_RSN_HOTPLUG		= 1LLU << 32,
53 
54 	/* Actions */
55 	SCX_ECODE_ACT_RESTART		= 1LLU << 48,
56 };
57 
58 enum uei_ecode_mask {
59 	UEI_ECODE_USER_MASK		= ((1LLU << 32) - 1),
60 	UEI_ECODE_SYS_RSN_MASK		= ((1LLU << 16) - 1) << 32,
61 	UEI_ECODE_SYS_ACT_MASK		= ((1LLU << 16) - 1) << 48,
62 };
63 
64 /*
65  * These macro interpret the ecode returned from UEI_REPORT().
66  */
67 #define UEI_ECODE_USER(__ecode)		((__ecode) & UEI_ECODE_USER_MASK)
68 #define UEI_ECODE_SYS_RSN(__ecode)	((__ecode) & UEI_ECODE_SYS_RSN_MASK)
69 #define UEI_ECODE_SYS_ACT(__ecode)	((__ecode) & UEI_ECODE_SYS_ACT_MASK)
70 
71 #define UEI_ECODE_RESTART(__ecode)	(UEI_ECODE_SYS_ACT((__ecode)) == SCX_ECODE_ACT_RESTART)
72 
73 #endif	/* __USER_EXIT_INFO_H */
74