usercopy.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * User address space access functions.
  3. *
  4. * For licencing details see kernel-base/COPYING
  5. */
  6. #include <linux/uaccess.h>
  7. #include <linux/export.h>
  8. #include <linux/instrumented.h>
  9. #include <asm/tlbflush.h>
  10. /**
  11. * copy_from_user_nmi - NMI safe copy from user
  12. * @to: Pointer to the destination buffer
  13. * @from: Pointer to a user space address of the current task
  14. * @n: Number of bytes to copy
  15. *
  16. * Returns: The number of not copied bytes. 0 is success, i.e. all bytes copied
  17. *
  18. * Contrary to other copy_from_user() variants this function can be called
  19. * from NMI context. Despite the name it is not restricted to be called
  20. * from NMI context. It is safe to be called from any other context as
  21. * well. It disables pagefaults across the copy which means a fault will
  22. * abort the copy.
  23. *
  24. * For NMI context invocations this relies on the nested NMI work to allow
  25. * atomic faults from the NMI path; the nested NMI paths are careful to
  26. * preserve CR2.
  27. */
  28. unsigned long
  29. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  30. {
  31. unsigned long ret;
  32. if (!__access_ok(from, n))
  33. return n;
  34. if (!nmi_uaccess_okay())
  35. return n;
  36. /*
  37. * Even though this function is typically called from NMI/IRQ context
  38. * disable pagefaults so that its behaviour is consistent even when
  39. * called from other contexts.
  40. */
  41. pagefault_disable();
  42. instrument_copy_from_user_before(to, from, n);
  43. ret = raw_copy_from_user(to, from, n);
  44. instrument_copy_from_user_after(to, from, n, ret);
  45. pagefault_enable();
  46. return ret;
  47. }
  48. EXPORT_SYMBOL_GPL(copy_from_user_nmi);