History

mm/hmm: check the device private page owner in hmm_range_fault()

hmm_range_fault() will succeed for any kind of device private memory, even if it doesn't belong to the calling entity. While nouveau has some crude checks for that, they are broken because they assume nouveau is the only user of device private memory. Fix this by passing in an expected pgmap owner in the hmm_range_fault structure. If a device_private page is found and doesn't match the owner then it is treated as an non-present and non-faultable page. This prevents a bug in amdgpu, where it doesn't know how to handle device_private pages, but hmm_range_fault would return them anyhow.

Christoph Hellwig committed 6 years ago

Commit 08ddddd1 parent 17ffdc43 files · +11 −13Browse files

Unified · Split

3 files
  1. drivers/gpu/drm/nouveau/nouveau_dmem.c +0 −12
  2. include/linux/hmm.h +2 −0
  3. mm/hmm.c +9 −1
drivers/gpu/drm/nouveau/nouveau_dmem.c+0−12
@@ -672,12 +672,6 @@ out: · 671 unmodified lines
672 return ret;672 return ret;
673}673}
674 674
675static inline bool
676nouveau_dmem_page(struct nouveau_drm *drm, struct page *page)
677{
678 return is_device_private_page(page) && drm->dmem == page_to_dmem(page);
679}
680
681void675void
682nouveau_dmem_convert_pfn(struct nouveau_drm *drm,676nouveau_dmem_convert_pfn(struct nouveau_drm *drm,
683 struct hmm_range *range)677 struct hmm_range *range)
@@ -696,12 +690,6 @@ nouveau_dmem_convert_pfn(struct nouveau_drm *drm, · 12 unmodified lines
696 if (!is_device_private_page(page))690 if (!is_device_private_page(page))
697 continue;691 continue;
698 692
699 if (!nouveau_dmem_page(drm, page)) {
700 WARN(1, "Some unknown device memory !\n");
701 range->pfns[i] = 0;
702 continue;
703 }
704
705 addr = nouveau_dmem_page_addr(page);693 addr = nouveau_dmem_page_addr(page);
706 range->pfns[i] &= ((1UL << range->pfn_shift) - 1);694 range->pfns[i] &= ((1UL << range->pfn_shift) - 1);
707 range->pfns[i] |= (addr >> PAGE_SHIFT) << range->pfn_shift;695 range->pfns[i] |= (addr >> PAGE_SHIFT) << range->pfn_shift;
include/linux/hmm.h+2−0
@@ -132,6 +132,7 @@ enum hmm_pfn_value_e { · 131 unmodified lines
132 * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter132 * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
133 * @pfn_shifts: pfn shift value (should be <= PAGE_SHIFT)133 * @pfn_shifts: pfn shift value (should be <= PAGE_SHIFT)
134 * @valid: pfns array did not change since it has been fill by an HMM function134 * @valid: pfns array did not change since it has been fill by an HMM function
135 * @dev_private_owner: owner of device private pages
135 */136 */
136struct hmm_range {137struct hmm_range {
137 struct mmu_interval_notifier *notifier;138 struct mmu_interval_notifier *notifier;
@@ -144,6 +145,7 @@ struct hmm_range { · 6 unmodified lines
144 uint64_t default_flags;145 uint64_t default_flags;
145 uint64_t pfn_flags_mask;146 uint64_t pfn_flags_mask;
146 uint8_t pfn_shift;147 uint8_t pfn_shift;
148 void *dev_private_owner;
147};149};
148 150
149/*151/*
mm/hmm.c+9−1
@@ -218,6 +218,14 @@ int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr, · 217 unmodified lines
218 unsigned long end, uint64_t *pfns, pmd_t pmd);218 unsigned long end, uint64_t *pfns, pmd_t pmd);
219#endif /* CONFIG_TRANSPARENT_HUGEPAGE */219#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
220 220
221static inline bool hmm_is_device_private_entry(struct hmm_range *range,
222 swp_entry_t entry)
223{
224 return is_device_private_entry(entry) &&
225 device_private_entry_to_page(entry)->pgmap->owner ==
226 range->dev_private_owner;
227}
228
221static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)229static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
222{230{
223 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))231 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
@@ -256,7 +264,7 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, · 32 unmodified lines
256 * Never fault in device private pages pages, but just report264 * Never fault in device private pages pages, but just report
257 * the PFN even if not present.265 * the PFN even if not present.
258 */266 */
259 if (is_device_private_entry(entry)) {267 if (hmm_is_device_private_entry(range, entry)) {
260 *pfn = hmm_device_entry_from_pfn(range,268 *pfn = hmm_device_entry_from_pfn(range,
261 swp_offset(entry));269 swp_offset(entry));
262 *pfn |= range->flags[HMM_PFN_VALID];270 *pfn |= range->flags[HMM_PFN_VALID];