xref: /linux/drivers/gpu/drm/xe/xe_hmm.c (revision 3ef18b236690af5f6427c5b6d8636881116aa73a)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2024 Intel Corporation
4  */
5 
6 #include <linux/scatterlist.h>
7 #include <linux/mmu_notifier.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/memremap.h>
10 #include <linux/swap.h>
11 #include <linux/hmm.h>
12 #include <linux/mm.h>
13 #include "xe_hmm.h"
14 #include "xe_vm.h"
15 #include "xe_bo.h"
16 
xe_npages_in_range(unsigned long start,unsigned long end)17 static u64 xe_npages_in_range(unsigned long start, unsigned long end)
18 {
19 	return (end - start) >> PAGE_SHIFT;
20 }
21 
22 /**
23  * xe_mark_range_accessed() - mark a range is accessed, so core mm
24  * have such information for memory eviction or write back to
25  * hard disk
26  * @range: the range to mark
27  * @write: if write to this range, we mark pages in this range
28  * as dirty
29  */
xe_mark_range_accessed(struct hmm_range * range,bool write)30 static void xe_mark_range_accessed(struct hmm_range *range, bool write)
31 {
32 	struct page *page;
33 	u64 i, npages;
34 
35 	npages = xe_npages_in_range(range->start, range->end);
36 	for (i = 0; i < npages; i++) {
37 		page = hmm_pfn_to_page(range->hmm_pfns[i]);
38 		if (write)
39 			set_page_dirty_lock(page);
40 
41 		mark_page_accessed(page);
42 	}
43 }
44 
xe_alloc_sg(struct xe_device * xe,struct sg_table * st,struct hmm_range * range,struct rw_semaphore * notifier_sem)45 static int xe_alloc_sg(struct xe_device *xe, struct sg_table *st,
46 		       struct hmm_range *range, struct rw_semaphore *notifier_sem)
47 {
48 	unsigned long i, npages, hmm_pfn;
49 	unsigned long num_chunks = 0;
50 	int ret;
51 
52 	/* HMM docs says this is needed. */
53 	ret = down_read_interruptible(notifier_sem);
54 	if (ret)
55 		return ret;
56 
57 	if (mmu_interval_read_retry(range->notifier, range->notifier_seq)) {
58 		up_read(notifier_sem);
59 		return -EAGAIN;
60 	}
61 
62 	npages = xe_npages_in_range(range->start, range->end);
63 	for (i = 0; i < npages;) {
64 		unsigned long len;
65 
66 		hmm_pfn = range->hmm_pfns[i];
67 		xe_assert(xe, hmm_pfn & HMM_PFN_VALID);
68 
69 		len = 1UL << hmm_pfn_to_map_order(hmm_pfn);
70 
71 		/* If order > 0 the page may extend beyond range->start */
72 		len -= (hmm_pfn & ~HMM_PFN_FLAGS) & (len - 1);
73 		i += len;
74 		num_chunks++;
75 	}
76 	up_read(notifier_sem);
77 
78 	return sg_alloc_table(st, num_chunks, GFP_KERNEL);
79 }
80 
81 /**
82  * xe_build_sg() - build a scatter gather table for all the physical pages/pfn
83  * in a hmm_range. dma-map pages if necessary. dma-address is save in sg table
84  * and will be used to program GPU page table later.
85  * @xe: the xe device who will access the dma-address in sg table
86  * @range: the hmm range that we build the sg table from. range->hmm_pfns[]
87  * has the pfn numbers of pages that back up this hmm address range.
88  * @st: pointer to the sg table.
89  * @notifier_sem: The xe notifier lock.
90  * @write: whether we write to this range. This decides dma map direction
91  * for system pages. If write we map it bi-diretional; otherwise
92  * DMA_TO_DEVICE
93  *
94  * All the contiguous pfns will be collapsed into one entry in
95  * the scatter gather table. This is for the purpose of efficiently
96  * programming GPU page table.
97  *
98  * The dma_address in the sg table will later be used by GPU to
99  * access memory. So if the memory is system memory, we need to
100  * do a dma-mapping so it can be accessed by GPU/DMA.
101  *
102  * FIXME: This function currently only support pages in system
103  * memory. If the memory is GPU local memory (of the GPU who
104  * is going to access memory), we need gpu dpa (device physical
105  * address), and there is no need of dma-mapping. This is TBD.
106  *
107  * FIXME: dma-mapping for peer gpu device to access remote gpu's
108  * memory. Add this when you support p2p
109  *
110  * This function allocates the storage of the sg table. It is
111  * caller's responsibility to free it calling sg_free_table.
112  *
113  * Returns 0 if successful; -ENOMEM if fails to allocate memory
114  */
xe_build_sg(struct xe_device * xe,struct hmm_range * range,struct sg_table * st,struct rw_semaphore * notifier_sem,bool write)115 static int xe_build_sg(struct xe_device *xe, struct hmm_range *range,
116 		       struct sg_table *st,
117 		       struct rw_semaphore *notifier_sem,
118 		       bool write)
119 {
120 	unsigned long npages = xe_npages_in_range(range->start, range->end);
121 	struct device *dev = xe->drm.dev;
122 	struct scatterlist *sgl;
123 	struct page *page;
124 	unsigned long i, j;
125 
126 	lockdep_assert_held(notifier_sem);
127 
128 	i = 0;
129 	for_each_sg(st->sgl, sgl, st->nents, j) {
130 		unsigned long hmm_pfn, size;
131 
132 		hmm_pfn = range->hmm_pfns[i];
133 		page = hmm_pfn_to_page(hmm_pfn);
134 		xe_assert(xe, !is_device_private_page(page));
135 
136 		size = 1UL << hmm_pfn_to_map_order(hmm_pfn);
137 		size -= page_to_pfn(page) & (size - 1);
138 		i += size;
139 
140 		if (unlikely(j == st->nents - 1)) {
141 			xe_assert(xe, i >= npages);
142 			if (i > npages)
143 				size -= (i - npages);
144 
145 			sg_mark_end(sgl);
146 		} else {
147 			xe_assert(xe, i < npages);
148 		}
149 
150 		sg_set_page(sgl, page, size << PAGE_SHIFT, 0);
151 	}
152 
153 	return dma_map_sgtable(dev, st, write ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE,
154 			       DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING);
155 }
156 
xe_hmm_userptr_set_mapped(struct xe_userptr_vma * uvma)157 static void xe_hmm_userptr_set_mapped(struct xe_userptr_vma *uvma)
158 {
159 	struct xe_userptr *userptr = &uvma->userptr;
160 	struct xe_vm *vm = xe_vma_vm(&uvma->vma);
161 
162 	lockdep_assert_held_write(&vm->lock);
163 	lockdep_assert_held(&vm->userptr.notifier_lock);
164 
165 	mutex_lock(&userptr->unmap_mutex);
166 	xe_assert(vm->xe, !userptr->mapped);
167 	userptr->mapped = true;
168 	mutex_unlock(&userptr->unmap_mutex);
169 }
170 
xe_hmm_userptr_unmap(struct xe_userptr_vma * uvma)171 void xe_hmm_userptr_unmap(struct xe_userptr_vma *uvma)
172 {
173 	struct xe_userptr *userptr = &uvma->userptr;
174 	struct xe_vma *vma = &uvma->vma;
175 	bool write = !xe_vma_read_only(vma);
176 	struct xe_vm *vm = xe_vma_vm(vma);
177 	struct xe_device *xe = vm->xe;
178 
179 	if (!lockdep_is_held_type(&vm->userptr.notifier_lock, 0) &&
180 	    !lockdep_is_held_type(&vm->lock, 0) &&
181 	    !(vma->gpuva.flags & XE_VMA_DESTROYED)) {
182 		/* Don't unmap in exec critical section. */
183 		xe_vm_assert_held(vm);
184 		/* Don't unmap while mapping the sg. */
185 		lockdep_assert_held(&vm->lock);
186 	}
187 
188 	mutex_lock(&userptr->unmap_mutex);
189 	if (userptr->sg && userptr->mapped)
190 		dma_unmap_sgtable(xe->drm.dev, userptr->sg,
191 				  write ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE, 0);
192 	userptr->mapped = false;
193 	mutex_unlock(&userptr->unmap_mutex);
194 }
195 
196 /**
197  * xe_hmm_userptr_free_sg() - Free the scatter gather table of userptr
198  * @uvma: the userptr vma which hold the scatter gather table
199  *
200  * With function xe_userptr_populate_range, we allocate storage of
201  * the userptr sg table. This is a helper function to free this
202  * sg table, and dma unmap the address in the table.
203  */
xe_hmm_userptr_free_sg(struct xe_userptr_vma * uvma)204 void xe_hmm_userptr_free_sg(struct xe_userptr_vma *uvma)
205 {
206 	struct xe_userptr *userptr = &uvma->userptr;
207 
208 	xe_assert(xe_vma_vm(&uvma->vma)->xe, userptr->sg);
209 	xe_hmm_userptr_unmap(uvma);
210 	sg_free_table(userptr->sg);
211 	userptr->sg = NULL;
212 }
213 
214 /**
215  * xe_hmm_userptr_populate_range() - Populate physical pages of a virtual
216  * address range
217  *
218  * @uvma: userptr vma which has information of the range to populate.
219  * @is_mm_mmap_locked: True if mmap_read_lock is already acquired by caller.
220  *
221  * This function populate the physical pages of a virtual
222  * address range. The populated physical pages is saved in
223  * userptr's sg table. It is similar to get_user_pages but call
224  * hmm_range_fault.
225  *
226  * This function also read mmu notifier sequence # (
227  * mmu_interval_read_begin), for the purpose of later
228  * comparison (through mmu_interval_read_retry).
229  *
230  * This must be called with mmap read or write lock held.
231  *
232  * This function allocates the storage of the userptr sg table.
233  * It is caller's responsibility to free it calling sg_free_table.
234  *
235  * returns: 0 for success; negative error no on failure
236  */
xe_hmm_userptr_populate_range(struct xe_userptr_vma * uvma,bool is_mm_mmap_locked)237 int xe_hmm_userptr_populate_range(struct xe_userptr_vma *uvma,
238 				  bool is_mm_mmap_locked)
239 {
240 	unsigned long timeout =
241 		jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
242 	unsigned long *pfns;
243 	struct xe_userptr *userptr;
244 	struct xe_vma *vma = &uvma->vma;
245 	u64 userptr_start = xe_vma_userptr(vma);
246 	u64 userptr_end = userptr_start + xe_vma_size(vma);
247 	struct xe_vm *vm = xe_vma_vm(vma);
248 	struct hmm_range hmm_range = {
249 		.pfn_flags_mask = 0, /* ignore pfns */
250 		.default_flags = HMM_PFN_REQ_FAULT,
251 		.start = userptr_start,
252 		.end = userptr_end,
253 		.notifier = &uvma->userptr.notifier,
254 		.dev_private_owner = vm->xe,
255 	};
256 	bool write = !xe_vma_read_only(vma);
257 	unsigned long notifier_seq;
258 	u64 npages;
259 	int ret;
260 
261 	userptr = &uvma->userptr;
262 
263 	if (is_mm_mmap_locked)
264 		mmap_assert_locked(userptr->notifier.mm);
265 
266 	if (vma->gpuva.flags & XE_VMA_DESTROYED)
267 		return 0;
268 
269 	notifier_seq = mmu_interval_read_begin(&userptr->notifier);
270 	if (notifier_seq == userptr->notifier_seq)
271 		return 0;
272 
273 	if (userptr->sg)
274 		xe_hmm_userptr_free_sg(uvma);
275 
276 	npages = xe_npages_in_range(userptr_start, userptr_end);
277 	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
278 	if (unlikely(!pfns))
279 		return -ENOMEM;
280 
281 	if (write)
282 		hmm_range.default_flags |= HMM_PFN_REQ_WRITE;
283 
284 	if (!mmget_not_zero(userptr->notifier.mm)) {
285 		ret = -EFAULT;
286 		goto free_pfns;
287 	}
288 
289 	hmm_range.hmm_pfns = pfns;
290 
291 	while (true) {
292 		hmm_range.notifier_seq = mmu_interval_read_begin(&userptr->notifier);
293 
294 		if (!is_mm_mmap_locked)
295 			mmap_read_lock(userptr->notifier.mm);
296 
297 		ret = hmm_range_fault(&hmm_range);
298 
299 		if (!is_mm_mmap_locked)
300 			mmap_read_unlock(userptr->notifier.mm);
301 
302 		if (ret == -EBUSY) {
303 			if (time_after(jiffies, timeout))
304 				break;
305 
306 			continue;
307 		}
308 		break;
309 	}
310 
311 	mmput(userptr->notifier.mm);
312 
313 	if (ret)
314 		goto free_pfns;
315 
316 	ret = xe_alloc_sg(vm->xe, &userptr->sgt, &hmm_range, &vm->userptr.notifier_lock);
317 	if (ret)
318 		goto free_pfns;
319 
320 	ret = down_read_interruptible(&vm->userptr.notifier_lock);
321 	if (ret)
322 		goto free_st;
323 
324 	if (mmu_interval_read_retry(hmm_range.notifier, hmm_range.notifier_seq)) {
325 		ret = -EAGAIN;
326 		goto out_unlock;
327 	}
328 
329 	ret = xe_build_sg(vm->xe, &hmm_range, &userptr->sgt,
330 			  &vm->userptr.notifier_lock, write);
331 	if (ret)
332 		goto out_unlock;
333 
334 	xe_mark_range_accessed(&hmm_range, write);
335 	userptr->sg = &userptr->sgt;
336 	xe_hmm_userptr_set_mapped(uvma);
337 	userptr->notifier_seq = hmm_range.notifier_seq;
338 	up_read(&vm->userptr.notifier_lock);
339 	kvfree(pfns);
340 	return 0;
341 
342 out_unlock:
343 	up_read(&vm->userptr.notifier_lock);
344 free_st:
345 	sg_free_table(&userptr->sgt);
346 free_pfns:
347 	kvfree(pfns);
348 	return ret;
349 }
350