user_exit_info.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include <stdio.h>
  13. #include <stdbool.h>
  14. #include "user_exit_info_common.h"
  15. /* no need to call the following explicitly if SCX_OPS_LOAD() is used */
  16. #define UEI_SET_SIZE(__skel, __ops_name, __uei_name) ({ \
  17. u32 __len = (__skel)->struct_ops.__ops_name->exit_dump_len ?: UEI_DUMP_DFL_LEN; \
  18. (__skel)->rodata->__uei_name##_dump_len = __len; \
  19. RESIZE_ARRAY((__skel), data, __uei_name##_dump, __len); \
  20. })
  21. #define UEI_EXITED(__skel, __uei_name) ({ \
  22. /* use __sync to force memory barrier */ \
  23. __sync_val_compare_and_swap(&(__skel)->data->__uei_name.kind, -1, -1); \
  24. })
  25. #define UEI_REPORT(__skel, __uei_name) ({ \
  26. struct user_exit_info *__uei = &(__skel)->data->__uei_name; \
  27. char *__uei_dump = (__skel)->data_##__uei_name##_dump->__uei_name##_dump; \
  28. if (__uei_dump[0] != '\0') { \
  29. fputs("\nDEBUG DUMP\n", stderr); \
  30. fputs("================================================================================\n\n", stderr); \
  31. fputs(__uei_dump, stderr); \
  32. fputs("\n================================================================================\n\n", stderr); \
  33. } \
  34. fprintf(stderr, "EXIT: %s", __uei->reason); \
  35. if (__uei->msg[0] != '\0') \
  36. fprintf(stderr, " (%s)", __uei->msg); \
  37. fputs("\n", stderr); \
  38. __uei->exit_code; \
  39. })
  40. /*
  41. * We can't import vmlinux.h while compiling user C code. Let's duplicate
  42. * scx_exit_code definition.
  43. */
  44. enum scx_exit_code {
  45. /* Reasons */
  46. SCX_ECODE_RSN_HOTPLUG = 1LLU << 32,
  47. /* Actions */
  48. SCX_ECODE_ACT_RESTART = 1LLU << 48,
  49. };
  50. enum uei_ecode_mask {
  51. UEI_ECODE_USER_MASK = ((1LLU << 32) - 1),
  52. UEI_ECODE_SYS_RSN_MASK = ((1LLU << 16) - 1) << 32,
  53. UEI_ECODE_SYS_ACT_MASK = ((1LLU << 16) - 1) << 48,
  54. };
  55. /*
  56. * These macro interpret the ecode returned from UEI_REPORT().
  57. */
  58. #define UEI_ECODE_USER(__ecode) ((__ecode) & UEI_ECODE_USER_MASK)
  59. #define UEI_ECODE_SYS_RSN(__ecode) ((__ecode) & UEI_ECODE_SYS_RSN_MASK)
  60. #define UEI_ECODE_SYS_ACT(__ecode) ((__ecode) & UEI_ECODE_SYS_ACT_MASK)
  61. #define UEI_ECODE_RESTART(__ecode) (UEI_ECODE_SYS_ACT((__ecode)) == SCX_ECODE_ACT_RESTART)
  62. #endif /* __USER_EXIT_INFO_H */