indirect_call_wrapper.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #else
  37. #define INDIRECT_CALL_1(f, f1, ...) f(__VA_ARGS__)
  38. #define INDIRECT_CALL_2(f, f2, f1, ...) f(__VA_ARGS__)
  39. #define INDIRECT_CALL_3(f, f3, f2, f1, ...) f(__VA_ARGS__)
  40. #define INDIRECT_CALL_4(f, f4, f3, f2, f1, ...) f(__VA_ARGS__)
  41. #define INDIRECT_CALLABLE_DECLARE(f)
  42. #define INDIRECT_CALLABLE_SCOPE static
  43. #endif
  44. /*
  45. * We can use INDIRECT_CALL_$NR for ipv6 related functions only if ipv6 is
  46. * builtin, this macro simplify dealing with indirect calls with only ipv4/ipv6
  47. * alternatives
  48. */
  49. #if IS_BUILTIN(CONFIG_IPV6)
  50. #define INDIRECT_CALL_INET(f, f2, f1, ...) \
  51. INDIRECT_CALL_2(f, f2, f1, __VA_ARGS__)
  52. #elif IS_ENABLED(CONFIG_INET)
  53. #define INDIRECT_CALL_INET(f, f2, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
  54. #else
  55. #define INDIRECT_CALL_INET(f, f2, f1, ...) f(__VA_ARGS__)
  56. #endif
  57. #endif