xref: /openbmc/linux/drivers/vfio/iova_bitmap.c (revision 93696d8f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2022, Oracle and/or its affiliates.
4  * Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved
5  */
6 #include <linux/iova_bitmap.h>
7 #include <linux/mm.h>
8 #include <linux/slab.h>
9 #include <linux/highmem.h>
10 
11 #define BITS_PER_PAGE (PAGE_SIZE * BITS_PER_BYTE)
12 
13 /*
14  * struct iova_bitmap_map - A bitmap representing an IOVA range
15  *
16  * Main data structure for tracking mapped user pages of bitmap data.
17  *
18  * For example, for something recording dirty IOVAs, it will be provided a
19  * struct iova_bitmap structure, as a general structure for iterating the
20  * total IOVA range. The struct iova_bitmap_map, though, represents the
21  * subset of said IOVA space that is pinned by its parent structure (struct
22  * iova_bitmap).
23  *
24  * The user does not need to exact location of the bits in the bitmap.
25  * From user perspective the only API available is iova_bitmap_set() which
26  * records the IOVA *range* in the bitmap by setting the corresponding
27  * bits.
28  *
29  * The bitmap is an array of u64 whereas each bit represents an IOVA of
30  * range of (1 << pgshift). Thus formula for the bitmap data to be set is:
31  *
32  *   data[(iova / page_size) / 64] & (1ULL << (iova % 64))
33  */
34 struct iova_bitmap_map {
35 	/* base IOVA representing bit 0 of the first page */
36 	unsigned long iova;
37 
38 	/* page size order that each bit granules to */
39 	unsigned long pgshift;
40 
41 	/* page offset of the first user page pinned */
42 	unsigned long pgoff;
43 
44 	/* number of pages pinned */
45 	unsigned long npages;
46 
47 	/* pinned pages representing the bitmap data */
48 	struct page **pages;
49 };
50 
51 /*
52  * struct iova_bitmap - The IOVA bitmap object
53  *
54  * Main data structure for iterating over the bitmap data.
55  *
56  * Abstracts the pinning work and iterates in IOVA ranges.
57  * It uses a windowing scheme and pins the bitmap in relatively
58  * big ranges e.g.
59  *
60  * The bitmap object uses one base page to store all the pinned pages
61  * pointers related to the bitmap. For sizeof(struct page*) == 8 it stores
62  * 512 struct page pointers which, if the base page size is 4K, it means
63  * 2M of bitmap data is pinned at a time. If the iova_bitmap page size is
64  * also 4K then the range window to iterate is 64G.
65  *
66  * For example iterating on a total IOVA range of 4G..128G, it will walk
67  * through this set of ranges:
68  *
69  *    4G  -  68G-1 (64G)
70  *    68G - 128G-1 (64G)
71  *
72  * An example of the APIs on how to use/iterate over the IOVA bitmap:
73  *
74  *   bitmap = iova_bitmap_alloc(iova, length, page_size, data);
75  *   if (IS_ERR(bitmap))
76  *       return PTR_ERR(bitmap);
77  *
78  *   ret = iova_bitmap_for_each(bitmap, arg, dirty_reporter_fn);
79  *
80  *   iova_bitmap_free(bitmap);
81  *
82  * Each iteration of the @dirty_reporter_fn is called with a unique @iova
83  * and @length argument, indicating the current range available through the
84  * iova_bitmap. The @dirty_reporter_fn uses iova_bitmap_set() to mark dirty
85  * areas (@iova_length) within that provided range, as following:
86  *
87  *   iova_bitmap_set(bitmap, iova, iova_length);
88  *
89  * The internals of the object uses an index @mapped_base_index that indexes
90  * which u64 word of the bitmap is mapped, up to @mapped_total_index.
91  * Those keep being incremented until @mapped_total_index is reached while
92  * mapping up to PAGE_SIZE / sizeof(struct page*) maximum of pages.
93  *
94  * The IOVA bitmap is usually located on what tracks DMA mapped ranges or
95  * some form of IOVA range tracking that co-relates to the user passed
96  * bitmap.
97  */
98 struct iova_bitmap {
99 	/* IOVA range representing the currently mapped bitmap data */
100 	struct iova_bitmap_map mapped;
101 
102 	/* userspace address of the bitmap */
103 	u8 __user *bitmap;
104 
105 	/* u64 index that @mapped points to */
106 	unsigned long mapped_base_index;
107 
108 	/* how many u64 can we walk in total */
109 	unsigned long mapped_total_index;
110 
111 	/* base IOVA of the whole bitmap */
112 	unsigned long iova;
113 
114 	/* length of the IOVA range for the whole bitmap */
115 	size_t length;
116 };
117 
118 /*
119  * Converts a relative IOVA to a bitmap index.
120  * This function provides the index into the u64 array (bitmap::bitmap)
121  * for a given IOVA offset.
122  * Relative IOVA means relative to the bitmap::mapped base IOVA
123  * (stored in mapped::iova). All computations in this file are done using
124  * relative IOVAs and thus avoid an extra subtraction against mapped::iova.
125  * The user API iova_bitmap_set() always uses a regular absolute IOVAs.
126  */
127 static unsigned long iova_bitmap_offset_to_index(struct iova_bitmap *bitmap,
128 						 unsigned long iova)
129 {
130 	unsigned long pgsize = 1 << bitmap->mapped.pgshift;
131 
132 	return iova / (BITS_PER_TYPE(*bitmap->bitmap) * pgsize);
133 }
134 
135 /*
136  * Converts a bitmap index to a *relative* IOVA.
137  */
138 static unsigned long iova_bitmap_index_to_offset(struct iova_bitmap *bitmap,
139 						 unsigned long index)
140 {
141 	unsigned long pgshift = bitmap->mapped.pgshift;
142 
143 	return (index * BITS_PER_TYPE(*bitmap->bitmap)) << pgshift;
144 }
145 
146 /*
147  * Returns the base IOVA of the mapped range.
148  */
149 static unsigned long iova_bitmap_mapped_iova(struct iova_bitmap *bitmap)
150 {
151 	unsigned long skip = bitmap->mapped_base_index;
152 
153 	return bitmap->iova + iova_bitmap_index_to_offset(bitmap, skip);
154 }
155 
156 /*
157  * Pins the bitmap user pages for the current range window.
158  * This is internal to IOVA bitmap and called when advancing the
159  * index (@mapped_base_index) or allocating the bitmap.
160  */
161 static int iova_bitmap_get(struct iova_bitmap *bitmap)
162 {
163 	struct iova_bitmap_map *mapped = &bitmap->mapped;
164 	unsigned long npages;
165 	u8 __user *addr;
166 	long ret;
167 
168 	/*
169 	 * @mapped_base_index is the index of the currently mapped u64 words
170 	 * that we have access. Anything before @mapped_base_index is not
171 	 * mapped. The range @mapped_base_index .. @mapped_total_index-1 is
172 	 * mapped but capped at a maximum number of pages.
173 	 */
174 	npages = DIV_ROUND_UP((bitmap->mapped_total_index -
175 			       bitmap->mapped_base_index) *
176 			       sizeof(*bitmap->bitmap), PAGE_SIZE);
177 
178 	/*
179 	 * Bitmap address to be pinned is calculated via pointer arithmetic
180 	 * with bitmap u64 word index.
181 	 */
182 	addr = bitmap->bitmap + bitmap->mapped_base_index;
183 
184 	/*
185 	 * We always cap at max number of 'struct page' a base page can fit.
186 	 * This is, for example, on x86 means 2M of bitmap data max.
187 	 */
188 	npages = min(npages + !!offset_in_page(addr),
189 		     PAGE_SIZE / sizeof(struct page *));
190 
191 	ret = pin_user_pages_fast((unsigned long)addr, npages,
192 				  FOLL_WRITE, mapped->pages);
193 	if (ret <= 0)
194 		return -EFAULT;
195 
196 	mapped->npages = (unsigned long)ret;
197 	/* Base IOVA where @pages point to i.e. bit 0 of the first page */
198 	mapped->iova = iova_bitmap_mapped_iova(bitmap);
199 
200 	/*
201 	 * offset of the page where pinned pages bit 0 is located.
202 	 * This handles the case where the bitmap is not PAGE_SIZE
203 	 * aligned.
204 	 */
205 	mapped->pgoff = offset_in_page(addr);
206 	return 0;
207 }
208 
209 /*
210  * Unpins the bitmap user pages and clears @npages
211  * (un)pinning is abstracted from API user and it's done when advancing
212  * the index or freeing the bitmap.
213  */
214 static void iova_bitmap_put(struct iova_bitmap *bitmap)
215 {
216 	struct iova_bitmap_map *mapped = &bitmap->mapped;
217 
218 	if (mapped->npages) {
219 		unpin_user_pages(mapped->pages, mapped->npages);
220 		mapped->npages = 0;
221 	}
222 }
223 
224 /**
225  * iova_bitmap_alloc() - Allocates an IOVA bitmap object
226  * @iova: Start address of the IOVA range
227  * @length: Length of the IOVA range
228  * @page_size: Page size of the IOVA bitmap. It defines what each bit
229  *             granularity represents
230  * @data: Userspace address of the bitmap
231  *
232  * Allocates an IOVA object and initializes all its fields including the
233  * first user pages of @data.
234  *
235  * Return: A pointer to a newly allocated struct iova_bitmap
236  * or ERR_PTR() on error.
237  */
238 struct iova_bitmap *iova_bitmap_alloc(unsigned long iova, size_t length,
239 				      unsigned long page_size, u64 __user *data)
240 {
241 	struct iova_bitmap_map *mapped;
242 	struct iova_bitmap *bitmap;
243 	int rc;
244 
245 	bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
246 	if (!bitmap)
247 		return ERR_PTR(-ENOMEM);
248 
249 	mapped = &bitmap->mapped;
250 	mapped->pgshift = __ffs(page_size);
251 	bitmap->bitmap = (u8 __user *)data;
252 	bitmap->mapped_total_index =
253 		iova_bitmap_offset_to_index(bitmap, length - 1) + 1;
254 	bitmap->iova = iova;
255 	bitmap->length = length;
256 	mapped->iova = iova;
257 	mapped->pages = (struct page **)__get_free_page(GFP_KERNEL);
258 	if (!mapped->pages) {
259 		rc = -ENOMEM;
260 		goto err;
261 	}
262 
263 	rc = iova_bitmap_get(bitmap);
264 	if (rc)
265 		goto err;
266 	return bitmap;
267 
268 err:
269 	iova_bitmap_free(bitmap);
270 	return ERR_PTR(rc);
271 }
272 
273 /**
274  * iova_bitmap_free() - Frees an IOVA bitmap object
275  * @bitmap: IOVA bitmap to free
276  *
277  * It unpins and releases pages array memory and clears any leftover
278  * state.
279  */
280 void iova_bitmap_free(struct iova_bitmap *bitmap)
281 {
282 	struct iova_bitmap_map *mapped = &bitmap->mapped;
283 
284 	iova_bitmap_put(bitmap);
285 
286 	if (mapped->pages) {
287 		free_page((unsigned long)mapped->pages);
288 		mapped->pages = NULL;
289 	}
290 
291 	kfree(bitmap);
292 }
293 
294 /*
295  * Returns the remaining bitmap indexes from mapped_total_index to process for
296  * the currently pinned bitmap pages.
297  */
298 static unsigned long iova_bitmap_mapped_remaining(struct iova_bitmap *bitmap)
299 {
300 	unsigned long remaining, bytes;
301 
302 	bytes = (bitmap->mapped.npages << PAGE_SHIFT) - bitmap->mapped.pgoff;
303 
304 	remaining = bitmap->mapped_total_index - bitmap->mapped_base_index;
305 	remaining = min_t(unsigned long, remaining,
306 			  DIV_ROUND_UP(bytes, sizeof(*bitmap->bitmap)));
307 
308 	return remaining;
309 }
310 
311 /*
312  * Returns the length of the mapped IOVA range.
313  */
314 static unsigned long iova_bitmap_mapped_length(struct iova_bitmap *bitmap)
315 {
316 	unsigned long max_iova = bitmap->iova + bitmap->length - 1;
317 	unsigned long iova = iova_bitmap_mapped_iova(bitmap);
318 	unsigned long remaining;
319 
320 	/*
321 	 * iova_bitmap_mapped_remaining() returns a number of indexes which
322 	 * when converted to IOVA gives us a max length that the bitmap
323 	 * pinned data can cover. Afterwards, that is capped to
324 	 * only cover the IOVA range in @bitmap::iova .. @bitmap::length.
325 	 */
326 	remaining = iova_bitmap_index_to_offset(bitmap,
327 			iova_bitmap_mapped_remaining(bitmap));
328 
329 	if (iova + remaining - 1 > max_iova)
330 		remaining -= ((iova + remaining - 1) - max_iova);
331 
332 	return remaining;
333 }
334 
335 /*
336  * Returns true if there's not more data to iterate.
337  */
338 static bool iova_bitmap_done(struct iova_bitmap *bitmap)
339 {
340 	return bitmap->mapped_base_index >= bitmap->mapped_total_index;
341 }
342 
343 /*
344  * Advances to the next range, releases the current pinned
345  * pages and pins the next set of bitmap pages.
346  * Returns 0 on success or otherwise errno.
347  */
348 static int iova_bitmap_advance(struct iova_bitmap *bitmap)
349 {
350 	unsigned long iova = iova_bitmap_mapped_length(bitmap) - 1;
351 	unsigned long count = iova_bitmap_offset_to_index(bitmap, iova) + 1;
352 
353 	bitmap->mapped_base_index += count;
354 
355 	iova_bitmap_put(bitmap);
356 	if (iova_bitmap_done(bitmap))
357 		return 0;
358 
359 	/* When advancing the index we pin the next set of bitmap pages */
360 	return iova_bitmap_get(bitmap);
361 }
362 
363 /**
364  * iova_bitmap_for_each() - Iterates over the bitmap
365  * @bitmap: IOVA bitmap to iterate
366  * @opaque: Additional argument to pass to the callback
367  * @fn: Function that gets called for each IOVA range
368  *
369  * Helper function to iterate over bitmap data representing a portion of IOVA
370  * space. It hides the complexity of iterating bitmaps and translating the
371  * mapped bitmap user pages into IOVA ranges to process.
372  *
373  * Return: 0 on success, and an error on failure either upon
374  * iteration or when the callback returns an error.
375  */
376 int iova_bitmap_for_each(struct iova_bitmap *bitmap, void *opaque,
377 			 iova_bitmap_fn_t fn)
378 {
379 	int ret = 0;
380 
381 	for (; !iova_bitmap_done(bitmap) && !ret;
382 	     ret = iova_bitmap_advance(bitmap)) {
383 		ret = fn(bitmap, iova_bitmap_mapped_iova(bitmap),
384 			 iova_bitmap_mapped_length(bitmap), opaque);
385 		if (ret)
386 			break;
387 	}
388 
389 	return ret;
390 }
391 
392 /**
393  * iova_bitmap_set() - Records an IOVA range in bitmap
394  * @bitmap: IOVA bitmap
395  * @iova: IOVA to start
396  * @length: IOVA range length
397  *
398  * Set the bits corresponding to the range [iova .. iova+length-1] in
399  * the user bitmap.
400  *
401  */
402 void iova_bitmap_set(struct iova_bitmap *bitmap,
403 		     unsigned long iova, size_t length)
404 {
405 	struct iova_bitmap_map *mapped = &bitmap->mapped;
406 	unsigned long cur_bit = ((iova - mapped->iova) >>
407 			mapped->pgshift) + mapped->pgoff * BITS_PER_BYTE;
408 	unsigned long last_bit = (((iova + length - 1) - mapped->iova) >>
409 			mapped->pgshift) + mapped->pgoff * BITS_PER_BYTE;
410 	unsigned long last_page_idx = mapped->npages - 1;
411 
412 	do {
413 		unsigned int page_idx = cur_bit / BITS_PER_PAGE;
414 		unsigned int offset = cur_bit % BITS_PER_PAGE;
415 		unsigned int nbits = min(BITS_PER_PAGE - offset,
416 					 last_bit - cur_bit + 1);
417 		void *kaddr;
418 
419 		if (unlikely(page_idx > last_page_idx))
420 			break;
421 
422 		kaddr = kmap_local_page(mapped->pages[page_idx]);
423 		bitmap_set(kaddr, offset, nbits);
424 		kunmap_local(kaddr);
425 		cur_bit += nbits;
426 	} while (cur_bit <= last_bit);
427 }
428 EXPORT_SYMBOL_GPL(iova_bitmap_set);
429