cpu-tunables.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* x86 CPU feature tuning.
  2. This file is part of the GNU C Library.
  3. Copyright (C) 2017-2026 Free Software Foundation, Inc.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #define TUNABLE_NAMESPACE cpu
  16. #include <stdbool.h>
  17. #include <stdint.h>
  18. #include <unistd.h> /* Get STDOUT_FILENO for _dl_printf. */
  19. #include <elf/dl-tunables.h>
  20. #include <string.h>
  21. #include <cpu-features.h>
  22. #include <ldsodefs.h>
  23. #include <dl-tunables-parse.h>
  24. #include <dl-symbol-redir-ifunc.h>
  25. #define CHECK_GLIBC_IFUNC_CPU_OFF(f, cpu_features, name, len) \
  26. _Static_assert (sizeof (#name) - 1 == len, #name " != " #len); \
  27. if (tunable_str_comma_strcmp_cte (&f, #name)) \
  28. { \
  29. CPU_FEATURE_UNSET (cpu_features, name) \
  30. break; \
  31. }
  32. #define CHECK_GLIBC_IFUNC_CPU_BOTH(f, cpu_features, name, len) \
  33. _Static_assert (sizeof (#name) - 1 == len, #name " != " #len); \
  34. if (tunable_str_comma_strcmp_cte (&f, #name)) \
  35. { \
  36. if (f.disable) \
  37. CPU_FEATURE_UNSET (cpu_features, name) \
  38. else \
  39. CPU_FEATURE_SET_ACTIVE (cpu_features, name) \
  40. break; \
  41. }
  42. /* Disable a preferred feature NAME. We don't enable a preferred feature
  43. which isn't available. */
  44. #define CHECK_GLIBC_IFUNC_PREFERRED_OFF(f, cpu_features, name, len) \
  45. _Static_assert (sizeof (#name) - 1 == len, #name " != " #len); \
  46. if (tunable_str_comma_strcmp_cte (&f, #name)) \
  47. { \
  48. cpu_features->preferred[index_arch_##name] \
  49. &= ~bit_arch_##name; \
  50. break; \
  51. }
  52. /* Enable/disable a preferred feature NAME. */
  53. #define CHECK_GLIBC_IFUNC_PREFERRED_BOTH(f, cpu_features, name, len) \
  54. _Static_assert (sizeof (#name) - 1 == len, #name " != " #len); \
  55. if (tunable_str_comma_strcmp_cte (&f, #name)) \
  56. { \
  57. if (f.disable) \
  58. cpu_features->preferred[index_arch_##name] &= ~bit_arch_##name; \
  59. else \
  60. cpu_features->preferred[index_arch_##name] |= bit_arch_##name; \
  61. break; \
  62. }
  63. /* Enable/disable a preferred feature NAME. Enable a preferred feature
  64. only if the feature NEED is usable. */
  65. #define CHECK_GLIBC_IFUNC_PREFERRED_NEED_BOTH(f, cpu_features, name, \
  66. need, len) \
  67. _Static_assert (sizeof (#name) - 1 == len, #name " != " #len); \
  68. if (tunable_str_comma_strcmp_cte (&f, #name)) \
  69. { \
  70. if (f.disable) \
  71. cpu_features->preferred[index_arch_##name] &= ~bit_arch_##name; \
  72. else if (CPU_FEATURE_USABLE_P (cpu_features, need)) \
  73. cpu_features->preferred[index_arch_##name] |= bit_arch_##name; \
  74. break; \
  75. }
  76. attribute_hidden
  77. void
  78. TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *valp)
  79. {
  80. /* The current IFUNC selection is based on microbenchmarks in glibc.
  81. It should give the best performance for most workloads. But other
  82. choices may have better performance for a particular workload or on
  83. the hardware which wasn't available when the selection was made.
  84. The environment variable:
  85. GLIBC_TUNABLES=glibc.cpu.hwcaps=-xxx,yyy,-zzz,....
  86. can be used to enable CPU/ARCH feature yyy, disable CPU/ARCH feature
  87. xxx and zzz, where the feature name is case-sensitive and has to
  88. match the ones in cpu-features.h. It can be used by glibc developers
  89. to tune for a new processor or override the IFUNC selection to
  90. improve performance for a particular workload.
  91. NOTE: the IFUNC selection may change over time. Please check all
  92. multiarch implementations when experimenting. */
  93. struct cpu_features *cpu_features = &GLRO(dl_x86_cpu_features);
  94. struct tunable_str_comma_state_t ts;
  95. tunable_str_comma_init (&ts, valp);
  96. struct tunable_str_comma_t n;
  97. while (tunable_str_comma_next (&ts, &n))
  98. {
  99. switch (n.len)
  100. {
  101. default:
  102. break;
  103. case 3:
  104. if (n.disable)
  105. {
  106. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX, 3);
  107. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, CX8, 3);
  108. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, FMA, 3);
  109. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, HTT, 3);
  110. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, IBT, 3);
  111. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, RTM, 3);
  112. }
  113. break;
  114. case 4:
  115. if (n.disable)
  116. {
  117. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX2, 4);
  118. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, BMI1, 4);
  119. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, BMI2, 4);
  120. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, CMOV, 4);
  121. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, ERMS, 4);
  122. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, FMA4, 4);
  123. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SSE2, 4);
  124. CHECK_GLIBC_IFUNC_PREFERRED_OFF (n, cpu_features, I586, 4);
  125. CHECK_GLIBC_IFUNC_PREFERRED_OFF (n, cpu_features, I686, 4);
  126. }
  127. break;
  128. case 5:
  129. {
  130. CHECK_GLIBC_IFUNC_CPU_BOTH (n, cpu_features, SHSTK, 5);
  131. }
  132. if (n.disable)
  133. {
  134. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, LZCNT, 5);
  135. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, MOVBE, 5);
  136. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SSSE3, 5);
  137. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, XSAVE, 5);
  138. }
  139. break;
  140. case 6:
  141. if (n.disable)
  142. {
  143. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, POPCNT, 6);
  144. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SSE4_1, 6);
  145. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SSE4_2, 6);
  146. if (memcmp (n.str, "XSAVEC", 6) == 0)
  147. {
  148. /* Update xsave_state_size to XSAVE state size. */
  149. cpu_features->xsave_state_size
  150. = cpu_features->xsave_state_full_size;
  151. _dl_x86_features_tlsdesc_state_size
  152. = cpu_features->xsave_state_full_size;
  153. CPU_FEATURE_UNSET (cpu_features, XSAVEC);
  154. }
  155. }
  156. break;
  157. case 7:
  158. if (n.disable)
  159. {
  160. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512F, 7);
  161. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, OSXSAVE, 7);
  162. }
  163. break;
  164. case 8:
  165. if (n.disable)
  166. {
  167. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512CD, 8);
  168. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512BW, 8);
  169. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512DQ, 8);
  170. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512ER, 8);
  171. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512PF, 8);
  172. CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512VL, 8);
  173. }
  174. CHECK_GLIBC_IFUNC_PREFERRED_BOTH (n, cpu_features, Slow_BSF, 8);
  175. break;
  176. case 11:
  177. {
  178. CHECK_GLIBC_IFUNC_PREFERRED_BOTH (n, cpu_features, Prefer_ERMS,
  179. 11);
  180. CHECK_GLIBC_IFUNC_PREFERRED_BOTH (n, cpu_features, Prefer_FSRM,
  181. 11);
  182. CHECK_GLIBC_IFUNC_PREFERRED_BOTH (n, cpu_features, Avoid_STOSB,
  183. 11);
  184. CHECK_GLIBC_IFUNC_PREFERRED_NEED_BOTH (n, cpu_features,
  185. Slow_SSE4_2,
  186. SSE4_2,
  187. 11);
  188. }
  189. break;
  190. case 15:
  191. {
  192. CHECK_GLIBC_IFUNC_PREFERRED_BOTH (n, cpu_features,
  193. Fast_Rep_String, 15);
  194. }
  195. break;
  196. case 16:
  197. {
  198. CHECK_GLIBC_IFUNC_PREFERRED_NEED_BOTH
  199. (n, cpu_features, Prefer_No_AVX512, AVX512F, 16);
  200. }
  201. break;
  202. case 18:
  203. {
  204. CHECK_GLIBC_IFUNC_PREFERRED_BOTH (n, cpu_features,
  205. Fast_Copy_Backward, 18);
  206. }
  207. break;
  208. case 19:
  209. {
  210. CHECK_GLIBC_IFUNC_PREFERRED_BOTH (n, cpu_features,
  211. Fast_Unaligned_Load, 19);
  212. CHECK_GLIBC_IFUNC_PREFERRED_BOTH (n, cpu_features,
  213. Fast_Unaligned_Copy, 19);
  214. }
  215. break;
  216. case 20:
  217. {
  218. CHECK_GLIBC_IFUNC_PREFERRED_NEED_BOTH
  219. (n, cpu_features, Prefer_No_VZEROUPPER, AVX, 20);
  220. }
  221. break;
  222. case 23:
  223. {
  224. CHECK_GLIBC_IFUNC_PREFERRED_NEED_BOTH
  225. (n, cpu_features, AVX_Fast_Unaligned_Load, AVX, 23);
  226. }
  227. break;
  228. case 24:
  229. {
  230. CHECK_GLIBC_IFUNC_PREFERRED_NEED_BOTH
  231. (n, cpu_features, MathVec_Prefer_No_AVX512, AVX512F, 24);
  232. }
  233. break;
  234. case 25:
  235. {
  236. CHECK_GLIBC_IFUNC_PREFERRED_BOTH (n, cpu_features,
  237. Avoid_Non_Temporal_Memset, 25);
  238. }
  239. break;
  240. case 26:
  241. {
  242. CHECK_GLIBC_IFUNC_PREFERRED_NEED_BOTH
  243. (n, cpu_features, Prefer_PMINUB_for_stringop, SSE2, 26);
  244. }
  245. break;
  246. }
  247. }
  248. }
  249. #if CET_ENABLED
  250. attribute_hidden
  251. void
  252. TUNABLE_CALLBACK (set_x86_ibt) (tunable_val_t *valp)
  253. {
  254. if (tunable_strcmp_cte (valp, "on"))
  255. GL(dl_x86_feature_control).ibt = cet_always_on;
  256. else if (tunable_strcmp_cte (valp, "off"))
  257. GL(dl_x86_feature_control).ibt = cet_always_off;
  258. else if (tunable_strcmp_cte (valp, "permissive"))
  259. GL(dl_x86_feature_control).ibt = cet_permissive;
  260. }
  261. attribute_hidden
  262. void
  263. TUNABLE_CALLBACK (set_x86_shstk) (tunable_val_t *valp)
  264. {
  265. if (tunable_strcmp_cte (valp, "on"))
  266. GL(dl_x86_feature_control).shstk = cet_always_on;
  267. else if (tunable_strcmp_cte (valp, "off"))
  268. GL(dl_x86_feature_control).shstk = cet_always_off;
  269. else if (tunable_strcmp_cte (valp, "permissive"))
  270. GL(dl_x86_feature_control).shstk = cet_permissive;
  271. }
  272. #endif