hmm.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright 2013 Red Hat Inc.
  4. *
  5. * Authors: Jérôme Glisse <[email protected]>
  6. *
  7. * See Documentation/mm/hmm.rst for reasons and overview of what HMM is.
  8. */
  9. #ifndef LINUX_HMM_H
  10. #define LINUX_HMM_H
  11. #include <linux/mm.h>
  12. struct mmu_interval_notifier;
  13. /*
  14. * On output:
  15. * 0 - The page is faultable and a future call with
  16. * HMM_PFN_REQ_FAULT could succeed.
  17. * HMM_PFN_VALID - the pfn field points to a valid PFN. This PFN is at
  18. * least readable. If dev_private_owner is !NULL then this could
  19. * point at a DEVICE_PRIVATE page.
  20. * HMM_PFN_WRITE - if the page memory can be written to (requires HMM_PFN_VALID)
  21. * HMM_PFN_ERROR - accessing the pfn is impossible and the device should
  22. * fail. ie poisoned memory, special pages, no vma, etc
  23. *
  24. * On input:
  25. * 0 - Return the current state of the page, do not fault it.
  26. * HMM_PFN_REQ_FAULT - The output must have HMM_PFN_VALID or hmm_range_fault()
  27. * will fail
  28. * HMM_PFN_REQ_WRITE - The output must have HMM_PFN_WRITE or hmm_range_fault()
  29. * will fail. Must be combined with HMM_PFN_REQ_FAULT.
  30. */
  31. enum hmm_pfn_flags {
  32. /* Output fields and flags */
  33. HMM_PFN_VALID = 1UL << (BITS_PER_LONG - 1),
  34. HMM_PFN_WRITE = 1UL << (BITS_PER_LONG - 2),
  35. HMM_PFN_ERROR = 1UL << (BITS_PER_LONG - 3),
  36. HMM_PFN_ORDER_SHIFT = (BITS_PER_LONG - 8),
  37. /* Input flags */
  38. HMM_PFN_REQ_FAULT = HMM_PFN_VALID,
  39. HMM_PFN_REQ_WRITE = HMM_PFN_WRITE,
  40. HMM_PFN_FLAGS = 0xFFUL << HMM_PFN_ORDER_SHIFT,
  41. };
  42. /*
  43. * hmm_pfn_to_page() - return struct page pointed to by a device entry
  44. *
  45. * This must be called under the caller 'user_lock' after a successful
  46. * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
  47. * already.
  48. */
  49. static inline struct page *hmm_pfn_to_page(unsigned long hmm_pfn)
  50. {
  51. return pfn_to_page(hmm_pfn & ~HMM_PFN_FLAGS);
  52. }
  53. /*
  54. * hmm_pfn_to_map_order() - return the CPU mapping size order
  55. *
  56. * This is optionally useful to optimize processing of the pfn result
  57. * array. It indicates that the page starts at the order aligned VA and is
  58. * 1<<order bytes long. Every pfn within an high order page will have the
  59. * same pfn flags, both access protections and the map_order. The caller must
  60. * be careful with edge cases as the start and end VA of the given page may
  61. * extend past the range used with hmm_range_fault().
  62. *
  63. * This must be called under the caller 'user_lock' after a successful
  64. * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
  65. * already.
  66. */
  67. static inline unsigned int hmm_pfn_to_map_order(unsigned long hmm_pfn)
  68. {
  69. return (hmm_pfn >> HMM_PFN_ORDER_SHIFT) & 0x1F;
  70. }
  71. /*
  72. * struct hmm_range - track invalidation lock on virtual address range
  73. *
  74. * @notifier: a mmu_interval_notifier that includes the start/end
  75. * @notifier_seq: result of mmu_interval_read_begin()
  76. * @start: range virtual start address (inclusive)
  77. * @end: range virtual end address (exclusive)
  78. * @hmm_pfns: array of pfns (big enough for the range)
  79. * @default_flags: default flags for the range (write, read, ... see hmm doc)
  80. * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
  81. * @dev_private_owner: owner of device private pages
  82. */
  83. struct hmm_range {
  84. struct mmu_interval_notifier *notifier;
  85. unsigned long notifier_seq;
  86. unsigned long start;
  87. unsigned long end;
  88. unsigned long *hmm_pfns;
  89. unsigned long default_flags;
  90. unsigned long pfn_flags_mask;
  91. void *dev_private_owner;
  92. };
  93. /*
  94. * Please see Documentation/mm/hmm.rst for how to use the range API.
  95. */
  96. int hmm_range_fault(struct hmm_range *range);
  97. /*
  98. * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
  99. *
  100. * When waiting for mmu notifiers we need some kind of time out otherwise we
  101. * could potentially wait for ever, 1000ms ie 1s sounds like a long time to
  102. * wait already.
  103. */
  104. #define HMM_RANGE_DEFAULT_TIMEOUT 1000
  105. #endif /* LINUX_HMM_H */