indirect_call_wrapper.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_INDIRECT_CALL_WRAPPER_H
  3. #define _LINUX_INDIRECT_CALL_WRAPPER_H
  4. #ifdef CONFIG_RETPOLINE
  5. /*
  6. * INDIRECT_CALL_$NR - wrapper for indirect calls with $NR known builtin
  7. * @f: function pointer
  8. * @f$NR: builtin functions names, up to $NR of them
  9. * @__VA_ARGS__: arguments for @f
  10. *
  11. * Avoid retpoline overhead for known builtin, checking @f vs each of them and
  12. * eventually invoking directly the builtin function. The functions are check
  13. * in the given order. Fallback to the indirect call.
  14. */
  15. #define INDIRECT_CALL_1(f, f1, ...) \
  16. ({ \
  17. likely(f == f1) ? f1(__VA_ARGS__) : f(__VA_ARGS__); \
  18. })
  19. #define INDIRECT_CALL_2(f, f2, f1, ...) \
  20. ({ \
  21. likely(f == f2) ? f2(__VA_ARGS__) : \
  22. INDIRECT_CALL_1(f, f1, __VA_ARGS__); \
  23. })
  24. #define INDIRECT_CALL_3(f, f3, f2, f1, ...) \
  25. ({ \
  26. likely(f == f3) ? f3(__VA_ARGS__) : \
  27. INDIRECT_CALL_2(f, f2, f1, __VA_ARGS__); \
  28. })
  29. #define INDIRECT_CALL_4(f, f4, f3, f2, f1, ...) \
  30. ({ \
  31. likely(f == f4) ? f4(__VA_ARGS__) : \
  32. INDIRECT_CALL_3(f, f3, f2, f1, __VA_ARGS__); \
  33. })
  34. #define INDIRECT_CALLABLE_DECLARE(f) f
  35. #define INDIRECT_CALLABLE_SCOPE
  36. #define EXPORT_INDIRECT_CALLABLE(f) EXPORT_SYMBOL(f)
  37. #else
  38. #define INDIRECT_CALL_1(f, f1, ...) f(__VA_ARGS__)
  39. #define INDIRECT_CALL_2(f, f2, f1, ...) f(__VA_ARGS__)
  40. #define INDIRECT_CALL_3(f, f3, f2, f1, ...) f(__VA_ARGS__)
  41. #define INDIRECT_CALL_4(f, f4, f3, f2, f1, ...) f(__VA_ARGS__)
  42. #define INDIRECT_CALLABLE_DECLARE(f)
  43. #define INDIRECT_CALLABLE_SCOPE static
  44. #define EXPORT_INDIRECT_CALLABLE(f)
  45. #endif
  46. /*
  47. * We can use INDIRECT_CALL_$NR for ipv6 related functions only if ipv6 is
  48. * builtin, this macro simplify dealing with indirect calls with only ipv4/ipv6
  49. * alternatives
  50. */
  51. #if IS_BUILTIN(CONFIG_IPV6)
  52. #define INDIRECT_CALL_INET(f, f2, f1, ...) \
  53. INDIRECT_CALL_2(f, f2, f1, __VA_ARGS__)
  54. #elif IS_ENABLED(CONFIG_INET)
  55. #define INDIRECT_CALL_INET(f, f2, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
  56. #else
  57. #define INDIRECT_CALL_INET(f, f2, f1, ...) f(__VA_ARGS__)
  58. #endif
  59. #if IS_ENABLED(CONFIG_INET)
  60. #define INDIRECT_CALL_INET_1(f, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
  61. #else
  62. #define INDIRECT_CALL_INET_1(f, f1, ...) f(__VA_ARGS__)
  63. #endif
  64. #endif