Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/sched_ext/include/scx/user_exit_info.h
26295 views
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 <[email protected]>
8
* Copyright (c) 2022 David Vernet <[email protected]>
9
*/
10
#ifndef __USER_EXIT_INFO_H
11
#define __USER_EXIT_INFO_H
12
13
#ifdef LSP
14
#define __bpf__
15
#include "../vmlinux.h"
16
#endif
17
18
enum uei_sizes {
19
UEI_REASON_LEN = 128,
20
UEI_MSG_LEN = 1024,
21
UEI_DUMP_DFL_LEN = 32768,
22
};
23
24
struct user_exit_info {
25
int kind;
26
s64 exit_code;
27
char reason[UEI_REASON_LEN];
28
char msg[UEI_MSG_LEN];
29
};
30
31
#ifdef __bpf__
32
33
#ifndef LSP
34
#include "vmlinux.h"
35
#endif
36
#include <bpf/bpf_core_read.h>
37
38
#define UEI_DEFINE(__name) \
39
char RESIZABLE_ARRAY(data, __name##_dump); \
40
const volatile u32 __name##_dump_len; \
41
struct user_exit_info __name SEC(".data")
42
43
#define UEI_RECORD(__uei_name, __ei) ({ \
44
bpf_probe_read_kernel_str(__uei_name.reason, \
45
sizeof(__uei_name.reason), (__ei)->reason); \
46
bpf_probe_read_kernel_str(__uei_name.msg, \
47
sizeof(__uei_name.msg), (__ei)->msg); \
48
bpf_probe_read_kernel_str(__uei_name##_dump, \
49
__uei_name##_dump_len, (__ei)->dump); \
50
if (bpf_core_field_exists((__ei)->exit_code)) \
51
__uei_name.exit_code = (__ei)->exit_code; \
52
/* use __sync to force memory barrier */ \
53
__sync_val_compare_and_swap(&__uei_name.kind, __uei_name.kind, \
54
(__ei)->kind); \
55
})
56
57
#else /* !__bpf__ */
58
59
#include <stdio.h>
60
#include <stdbool.h>
61
62
/* no need to call the following explicitly if SCX_OPS_LOAD() is used */
63
#define UEI_SET_SIZE(__skel, __ops_name, __uei_name) ({ \
64
u32 __len = (__skel)->struct_ops.__ops_name->exit_dump_len ?: UEI_DUMP_DFL_LEN; \
65
(__skel)->rodata->__uei_name##_dump_len = __len; \
66
RESIZE_ARRAY((__skel), data, __uei_name##_dump, __len); \
67
})
68
69
#define UEI_EXITED(__skel, __uei_name) ({ \
70
/* use __sync to force memory barrier */ \
71
__sync_val_compare_and_swap(&(__skel)->data->__uei_name.kind, -1, -1); \
72
})
73
74
#define UEI_REPORT(__skel, __uei_name) ({ \
75
struct user_exit_info *__uei = &(__skel)->data->__uei_name; \
76
char *__uei_dump = (__skel)->data_##__uei_name##_dump->__uei_name##_dump; \
77
if (__uei_dump[0] != '\0') { \
78
fputs("\nDEBUG DUMP\n", stderr); \
79
fputs("================================================================================\n\n", stderr); \
80
fputs(__uei_dump, stderr); \
81
fputs("\n================================================================================\n\n", stderr); \
82
} \
83
fprintf(stderr, "EXIT: %s", __uei->reason); \
84
if (__uei->msg[0] != '\0') \
85
fprintf(stderr, " (%s)", __uei->msg); \
86
fputs("\n", stderr); \
87
__uei->exit_code; \
88
})
89
90
/*
91
* We can't import vmlinux.h while compiling user C code. Let's duplicate
92
* scx_exit_code definition.
93
*/
94
enum scx_exit_code {
95
/* Reasons */
96
SCX_ECODE_RSN_HOTPLUG = 1LLU << 32,
97
98
/* Actions */
99
SCX_ECODE_ACT_RESTART = 1LLU << 48,
100
};
101
102
enum uei_ecode_mask {
103
UEI_ECODE_USER_MASK = ((1LLU << 32) - 1),
104
UEI_ECODE_SYS_RSN_MASK = ((1LLU << 16) - 1) << 32,
105
UEI_ECODE_SYS_ACT_MASK = ((1LLU << 16) - 1) << 48,
106
};
107
108
/*
109
* These macro interpret the ecode returned from UEI_REPORT().
110
*/
111
#define UEI_ECODE_USER(__ecode) ((__ecode) & UEI_ECODE_USER_MASK)
112
#define UEI_ECODE_SYS_RSN(__ecode) ((__ecode) & UEI_ECODE_SYS_RSN_MASK)
113
#define UEI_ECODE_SYS_ACT(__ecode) ((__ecode) & UEI_ECODE_SYS_ACT_MASK)
114
115
#define UEI_ECODE_RESTART(__ecode) (UEI_ECODE_SYS_ACT((__ecode)) == SCX_ECODE_ACT_RESTART)
116
117
#endif /* __bpf__ */
118
#endif /* __USER_EXIT_INFO_H */
119
120