copy_mc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2016-2020 Intel Corporation. All rights reserved. */
  3. #include <linux/jump_label.h>
  4. #include <linux/uaccess.h>
  5. #include <linux/export.h>
  6. #include <linux/instrumented.h>
  7. #include <linux/string.h>
  8. #include <linux/types.h>
  9. #include <asm/mce.h>
  10. #ifdef CONFIG_X86_MCE
  11. static DEFINE_STATIC_KEY_FALSE(copy_mc_fragile_key);
  12. void enable_copy_mc_fragile(void)
  13. {
  14. static_branch_inc(&copy_mc_fragile_key);
  15. }
  16. #define copy_mc_fragile_enabled (static_branch_unlikely(&copy_mc_fragile_key))
  17. /*
  18. * Similar to copy_user_handle_tail, probe for the write fault point, or
  19. * source exception point.
  20. */
  21. __visible notrace unsigned long
  22. copy_mc_fragile_handle_tail(char *to, char *from, unsigned len)
  23. {
  24. for (; len; --len, to++, from++)
  25. if (copy_mc_fragile(to, from, 1))
  26. break;
  27. return len;
  28. }
  29. #else
  30. /*
  31. * No point in doing careful copying, or consulting a static key when
  32. * there is no #MC handler in the CONFIG_X86_MCE=n case.
  33. */
  34. void enable_copy_mc_fragile(void)
  35. {
  36. }
  37. #define copy_mc_fragile_enabled (0)
  38. #endif
  39. unsigned long copy_mc_enhanced_fast_string(void *dst, const void *src, unsigned len);
  40. /**
  41. * copy_mc_to_kernel - memory copy that handles source exceptions
  42. *
  43. * @dst: destination address
  44. * @src: source address
  45. * @len: number of bytes to copy
  46. *
  47. * Call into the 'fragile' version on systems that benefit from avoiding
  48. * corner case poison consumption scenarios, For example, accessing
  49. * poison across 2 cachelines with a single instruction. Almost all
  50. * other uses case can use copy_mc_enhanced_fast_string() for a fast
  51. * recoverable copy, or fallback to plain memcpy.
  52. *
  53. * Return 0 for success, or number of bytes not copied if there was an
  54. * exception.
  55. */
  56. unsigned long __must_check copy_mc_to_kernel(void *dst, const void *src, unsigned len)
  57. {
  58. unsigned long ret;
  59. if (copy_mc_fragile_enabled) {
  60. instrument_memcpy_before(dst, src, len);
  61. ret = copy_mc_fragile(dst, src, len);
  62. instrument_memcpy_after(dst, src, len, ret);
  63. return ret;
  64. }
  65. if (static_cpu_has(X86_FEATURE_ERMS)) {
  66. instrument_memcpy_before(dst, src, len);
  67. ret = copy_mc_enhanced_fast_string(dst, src, len);
  68. instrument_memcpy_after(dst, src, len, ret);
  69. return ret;
  70. }
  71. memcpy(dst, src, len);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(copy_mc_to_kernel);
  75. unsigned long __must_check copy_mc_to_user(void __user *dst, const void *src, unsigned len)
  76. {
  77. unsigned long ret;
  78. if (copy_mc_fragile_enabled) {
  79. instrument_copy_to_user(dst, src, len);
  80. __uaccess_begin();
  81. ret = copy_mc_fragile((__force void *)dst, src, len);
  82. __uaccess_end();
  83. return ret;
  84. }
  85. if (static_cpu_has(X86_FEATURE_ERMS)) {
  86. instrument_copy_to_user(dst, src, len);
  87. __uaccess_begin();
  88. ret = copy_mc_enhanced_fast_string((__force void *)dst, src, len);
  89. __uaccess_end();
  90. return ret;
  91. }
  92. return copy_user_generic((__force void *)dst, src, len);
  93. }