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