xref: /linux/drivers/iommu/iommufd/iova_bitmap.c (revision a84c690e10ae03f1cddec908ac7f5068ceed67a8)
18c9c727bSJoao Martins // SPDX-License-Identifier: GPL-2.0
28c9c727bSJoao Martins /*
38c9c727bSJoao Martins  * Copyright (c) 2022, Oracle and/or its affiliates.
48c9c727bSJoao Martins  * Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved
58c9c727bSJoao Martins  */
68c9c727bSJoao Martins #include <linux/iova_bitmap.h>
78c9c727bSJoao Martins #include <linux/mm.h>
88c9c727bSJoao Martins #include <linux/slab.h>
98c9c727bSJoao Martins #include <linux/highmem.h>
108c9c727bSJoao Martins 
118c9c727bSJoao Martins #define BITS_PER_PAGE (PAGE_SIZE * BITS_PER_BYTE)
128c9c727bSJoao Martins 
138c9c727bSJoao Martins /*
148c9c727bSJoao Martins  * struct iova_bitmap_map - A bitmap representing an IOVA range
158c9c727bSJoao Martins  *
168c9c727bSJoao Martins  * Main data structure for tracking mapped user pages of bitmap data.
178c9c727bSJoao Martins  *
188c9c727bSJoao Martins  * For example, for something recording dirty IOVAs, it will be provided a
198c9c727bSJoao Martins  * struct iova_bitmap structure, as a general structure for iterating the
208c9c727bSJoao Martins  * total IOVA range. The struct iova_bitmap_map, though, represents the
218c9c727bSJoao Martins  * subset of said IOVA space that is pinned by its parent structure (struct
228c9c727bSJoao Martins  * iova_bitmap).
238c9c727bSJoao Martins  *
248c9c727bSJoao Martins  * The user does not need to exact location of the bits in the bitmap.
258c9c727bSJoao Martins  * From user perspective the only API available is iova_bitmap_set() which
268c9c727bSJoao Martins  * records the IOVA *range* in the bitmap by setting the corresponding
278c9c727bSJoao Martins  * bits.
288c9c727bSJoao Martins  *
298c9c727bSJoao Martins  * The bitmap is an array of u64 whereas each bit represents an IOVA of
308c9c727bSJoao Martins  * range of (1 << pgshift). Thus formula for the bitmap data to be set is:
318c9c727bSJoao Martins  *
328c9c727bSJoao Martins  *   data[(iova / page_size) / 64] & (1ULL << (iova % 64))
338c9c727bSJoao Martins  */
348c9c727bSJoao Martins struct iova_bitmap_map {
358c9c727bSJoao Martins 	/* base IOVA representing bit 0 of the first page */
368c9c727bSJoao Martins 	unsigned long iova;
378c9c727bSJoao Martins 
38*a84c690eSJoao Martins 	/* mapped length */
39*a84c690eSJoao Martins 	unsigned long length;
40*a84c690eSJoao Martins 
418c9c727bSJoao Martins 	/* page size order that each bit granules to */
428c9c727bSJoao Martins 	unsigned long pgshift;
438c9c727bSJoao Martins 
448c9c727bSJoao Martins 	/* page offset of the first user page pinned */
458c9c727bSJoao Martins 	unsigned long pgoff;
468c9c727bSJoao Martins 
478c9c727bSJoao Martins 	/* number of pages pinned */
488c9c727bSJoao Martins 	unsigned long npages;
498c9c727bSJoao Martins 
508c9c727bSJoao Martins 	/* pinned pages representing the bitmap data */
518c9c727bSJoao Martins 	struct page **pages;
528c9c727bSJoao Martins };
538c9c727bSJoao Martins 
548c9c727bSJoao Martins /*
558c9c727bSJoao Martins  * struct iova_bitmap - The IOVA bitmap object
568c9c727bSJoao Martins  *
578c9c727bSJoao Martins  * Main data structure for iterating over the bitmap data.
588c9c727bSJoao Martins  *
598c9c727bSJoao Martins  * Abstracts the pinning work and iterates in IOVA ranges.
608c9c727bSJoao Martins  * It uses a windowing scheme and pins the bitmap in relatively
618c9c727bSJoao Martins  * big ranges e.g.
628c9c727bSJoao Martins  *
638c9c727bSJoao Martins  * The bitmap object uses one base page to store all the pinned pages
648c9c727bSJoao Martins  * pointers related to the bitmap. For sizeof(struct page*) == 8 it stores
658c9c727bSJoao Martins  * 512 struct page pointers which, if the base page size is 4K, it means
668c9c727bSJoao Martins  * 2M of bitmap data is pinned at a time. If the iova_bitmap page size is
678c9c727bSJoao Martins  * also 4K then the range window to iterate is 64G.
688c9c727bSJoao Martins  *
698c9c727bSJoao Martins  * For example iterating on a total IOVA range of 4G..128G, it will walk
708c9c727bSJoao Martins  * through this set of ranges:
718c9c727bSJoao Martins  *
728c9c727bSJoao Martins  *    4G  -  68G-1 (64G)
738c9c727bSJoao Martins  *    68G - 128G-1 (64G)
748c9c727bSJoao Martins  *
758c9c727bSJoao Martins  * An example of the APIs on how to use/iterate over the IOVA bitmap:
768c9c727bSJoao Martins  *
778c9c727bSJoao Martins  *   bitmap = iova_bitmap_alloc(iova, length, page_size, data);
788c9c727bSJoao Martins  *   if (IS_ERR(bitmap))
798c9c727bSJoao Martins  *       return PTR_ERR(bitmap);
808c9c727bSJoao Martins  *
818c9c727bSJoao Martins  *   ret = iova_bitmap_for_each(bitmap, arg, dirty_reporter_fn);
828c9c727bSJoao Martins  *
838c9c727bSJoao Martins  *   iova_bitmap_free(bitmap);
848c9c727bSJoao Martins  *
858c9c727bSJoao Martins  * Each iteration of the @dirty_reporter_fn is called with a unique @iova
868c9c727bSJoao Martins  * and @length argument, indicating the current range available through the
878c9c727bSJoao Martins  * iova_bitmap. The @dirty_reporter_fn uses iova_bitmap_set() to mark dirty
888c9c727bSJoao Martins  * areas (@iova_length) within that provided range, as following:
898c9c727bSJoao Martins  *
908c9c727bSJoao Martins  *   iova_bitmap_set(bitmap, iova, iova_length);
918c9c727bSJoao Martins  *
928c9c727bSJoao Martins  * The internals of the object uses an index @mapped_base_index that indexes
938c9c727bSJoao Martins  * which u64 word of the bitmap is mapped, up to @mapped_total_index.
948c9c727bSJoao Martins  * Those keep being incremented until @mapped_total_index is reached while
958c9c727bSJoao Martins  * mapping up to PAGE_SIZE / sizeof(struct page*) maximum of pages.
968c9c727bSJoao Martins  *
978c9c727bSJoao Martins  * The IOVA bitmap is usually located on what tracks DMA mapped ranges or
988c9c727bSJoao Martins  * some form of IOVA range tracking that co-relates to the user passed
998c9c727bSJoao Martins  * bitmap.
1008c9c727bSJoao Martins  */
1018c9c727bSJoao Martins struct iova_bitmap {
1028c9c727bSJoao Martins 	/* IOVA range representing the currently mapped bitmap data */
1038c9c727bSJoao Martins 	struct iova_bitmap_map mapped;
1048c9c727bSJoao Martins 
1058c9c727bSJoao Martins 	/* userspace address of the bitmap */
106d18411ecSJoao Martins 	u8 __user *bitmap;
1078c9c727bSJoao Martins 
1088c9c727bSJoao Martins 	/* u64 index that @mapped points to */
1098c9c727bSJoao Martins 	unsigned long mapped_base_index;
1108c9c727bSJoao Martins 
1118c9c727bSJoao Martins 	/* how many u64 can we walk in total */
1128c9c727bSJoao Martins 	unsigned long mapped_total_index;
1138c9c727bSJoao Martins 
1148c9c727bSJoao Martins 	/* base IOVA of the whole bitmap */
1158c9c727bSJoao Martins 	unsigned long iova;
1168c9c727bSJoao Martins 
1178c9c727bSJoao Martins 	/* length of the IOVA range for the whole bitmap */
1188c9c727bSJoao Martins 	size_t length;
1192780025eSJoao Martins 
1202780025eSJoao Martins 	/* length of the IOVA range set ahead the pinned pages */
1212780025eSJoao Martins 	unsigned long set_ahead_length;
1228c9c727bSJoao Martins };
1238c9c727bSJoao Martins 
1248c9c727bSJoao Martins /*
1258c9c727bSJoao Martins  * Converts a relative IOVA to a bitmap index.
1268c9c727bSJoao Martins  * This function provides the index into the u64 array (bitmap::bitmap)
1278c9c727bSJoao Martins  * for a given IOVA offset.
1288c9c727bSJoao Martins  * Relative IOVA means relative to the bitmap::mapped base IOVA
1298c9c727bSJoao Martins  * (stored in mapped::iova). All computations in this file are done using
1308c9c727bSJoao Martins  * relative IOVAs and thus avoid an extra subtraction against mapped::iova.
1318c9c727bSJoao Martins  * The user API iova_bitmap_set() always uses a regular absolute IOVAs.
1328c9c727bSJoao Martins  */
1338c9c727bSJoao Martins static unsigned long iova_bitmap_offset_to_index(struct iova_bitmap *bitmap,
1348c9c727bSJoao Martins 						 unsigned long iova)
1358c9c727bSJoao Martins {
1368c9c727bSJoao Martins 	unsigned long pgsize = 1 << bitmap->mapped.pgshift;
1378c9c727bSJoao Martins 
1388c9c727bSJoao Martins 	return iova / (BITS_PER_TYPE(*bitmap->bitmap) * pgsize);
1398c9c727bSJoao Martins }
1408c9c727bSJoao Martins 
1418c9c727bSJoao Martins /*
1428c9c727bSJoao Martins  * Converts a bitmap index to a *relative* IOVA.
1438c9c727bSJoao Martins  */
1448c9c727bSJoao Martins static unsigned long iova_bitmap_index_to_offset(struct iova_bitmap *bitmap,
1458c9c727bSJoao Martins 						 unsigned long index)
1468c9c727bSJoao Martins {
1478c9c727bSJoao Martins 	unsigned long pgshift = bitmap->mapped.pgshift;
1488c9c727bSJoao Martins 
1498c9c727bSJoao Martins 	return (index * BITS_PER_TYPE(*bitmap->bitmap)) << pgshift;
1508c9c727bSJoao Martins }
1518c9c727bSJoao Martins 
1528c9c727bSJoao Martins /*
1538c9c727bSJoao Martins  * Returns the base IOVA of the mapped range.
1548c9c727bSJoao Martins  */
1558c9c727bSJoao Martins static unsigned long iova_bitmap_mapped_iova(struct iova_bitmap *bitmap)
1568c9c727bSJoao Martins {
1578c9c727bSJoao Martins 	unsigned long skip = bitmap->mapped_base_index;
1588c9c727bSJoao Martins 
1598c9c727bSJoao Martins 	return bitmap->iova + iova_bitmap_index_to_offset(bitmap, skip);
1608c9c727bSJoao Martins }
1618c9c727bSJoao Martins 
162*a84c690eSJoao Martins static unsigned long iova_bitmap_mapped_length(struct iova_bitmap *bitmap);
163*a84c690eSJoao Martins 
1648c9c727bSJoao Martins /*
1658c9c727bSJoao Martins  * Pins the bitmap user pages for the current range window.
1668c9c727bSJoao Martins  * This is internal to IOVA bitmap and called when advancing the
1678c9c727bSJoao Martins  * index (@mapped_base_index) or allocating the bitmap.
1688c9c727bSJoao Martins  */
1698c9c727bSJoao Martins static int iova_bitmap_get(struct iova_bitmap *bitmap)
1708c9c727bSJoao Martins {
1718c9c727bSJoao Martins 	struct iova_bitmap_map *mapped = &bitmap->mapped;
1728c9c727bSJoao Martins 	unsigned long npages;
173d18411ecSJoao Martins 	u8 __user *addr;
1748c9c727bSJoao Martins 	long ret;
1758c9c727bSJoao Martins 
1768c9c727bSJoao Martins 	/*
1778c9c727bSJoao Martins 	 * @mapped_base_index is the index of the currently mapped u64 words
1788c9c727bSJoao Martins 	 * that we have access. Anything before @mapped_base_index is not
1798c9c727bSJoao Martins 	 * mapped. The range @mapped_base_index .. @mapped_total_index-1 is
1808c9c727bSJoao Martins 	 * mapped but capped at a maximum number of pages.
1818c9c727bSJoao Martins 	 */
1828c9c727bSJoao Martins 	npages = DIV_ROUND_UP((bitmap->mapped_total_index -
1838c9c727bSJoao Martins 			       bitmap->mapped_base_index) *
1848c9c727bSJoao Martins 			       sizeof(*bitmap->bitmap), PAGE_SIZE);
1858c9c727bSJoao Martins 
1868c9c727bSJoao Martins 	/*
1878c9c727bSJoao Martins 	 * Bitmap address to be pinned is calculated via pointer arithmetic
1888c9c727bSJoao Martins 	 * with bitmap u64 word index.
1898c9c727bSJoao Martins 	 */
1908c9c727bSJoao Martins 	addr = bitmap->bitmap + bitmap->mapped_base_index;
1918c9c727bSJoao Martins 
1924bbcbc6eSJoao Martins 	/*
1934bbcbc6eSJoao Martins 	 * We always cap at max number of 'struct page' a base page can fit.
1944bbcbc6eSJoao Martins 	 * This is, for example, on x86 means 2M of bitmap data max.
1954bbcbc6eSJoao Martins 	 */
1964bbcbc6eSJoao Martins 	npages = min(npages + !!offset_in_page(addr),
1974bbcbc6eSJoao Martins 		     PAGE_SIZE / sizeof(struct page *));
1984bbcbc6eSJoao Martins 
1998c9c727bSJoao Martins 	ret = pin_user_pages_fast((unsigned long)addr, npages,
2008c9c727bSJoao Martins 				  FOLL_WRITE, mapped->pages);
2018c9c727bSJoao Martins 	if (ret <= 0)
2028c9c727bSJoao Martins 		return -EFAULT;
2038c9c727bSJoao Martins 
2048c9c727bSJoao Martins 	mapped->npages = (unsigned long)ret;
2058c9c727bSJoao Martins 	/* Base IOVA where @pages point to i.e. bit 0 of the first page */
2068c9c727bSJoao Martins 	mapped->iova = iova_bitmap_mapped_iova(bitmap);
2078c9c727bSJoao Martins 
2088c9c727bSJoao Martins 	/*
2098c9c727bSJoao Martins 	 * offset of the page where pinned pages bit 0 is located.
2108c9c727bSJoao Martins 	 * This handles the case where the bitmap is not PAGE_SIZE
2118c9c727bSJoao Martins 	 * aligned.
2128c9c727bSJoao Martins 	 */
2138c9c727bSJoao Martins 	mapped->pgoff = offset_in_page(addr);
214*a84c690eSJoao Martins 	mapped->length = iova_bitmap_mapped_length(bitmap);
2158c9c727bSJoao Martins 	return 0;
2168c9c727bSJoao Martins }
2178c9c727bSJoao Martins 
2188c9c727bSJoao Martins /*
2198c9c727bSJoao Martins  * Unpins the bitmap user pages and clears @npages
2208c9c727bSJoao Martins  * (un)pinning is abstracted from API user and it's done when advancing
2218c9c727bSJoao Martins  * the index or freeing the bitmap.
2228c9c727bSJoao Martins  */
2238c9c727bSJoao Martins static void iova_bitmap_put(struct iova_bitmap *bitmap)
2248c9c727bSJoao Martins {
2258c9c727bSJoao Martins 	struct iova_bitmap_map *mapped = &bitmap->mapped;
2268c9c727bSJoao Martins 
2278c9c727bSJoao Martins 	if (mapped->npages) {
2288c9c727bSJoao Martins 		unpin_user_pages(mapped->pages, mapped->npages);
2298c9c727bSJoao Martins 		mapped->npages = 0;
2308c9c727bSJoao Martins 	}
2318c9c727bSJoao Martins }
2328c9c727bSJoao Martins 
2338c9c727bSJoao Martins /**
2348c9c727bSJoao Martins  * iova_bitmap_alloc() - Allocates an IOVA bitmap object
2358c9c727bSJoao Martins  * @iova: Start address of the IOVA range
2368c9c727bSJoao Martins  * @length: Length of the IOVA range
2378c9c727bSJoao Martins  * @page_size: Page size of the IOVA bitmap. It defines what each bit
2388c9c727bSJoao Martins  *             granularity represents
2398c9c727bSJoao Martins  * @data: Userspace address of the bitmap
2408c9c727bSJoao Martins  *
2418c9c727bSJoao Martins  * Allocates an IOVA object and initializes all its fields including the
2428c9c727bSJoao Martins  * first user pages of @data.
2438c9c727bSJoao Martins  *
2448c9c727bSJoao Martins  * Return: A pointer to a newly allocated struct iova_bitmap
2458c9c727bSJoao Martins  * or ERR_PTR() on error.
2468c9c727bSJoao Martins  */
2478c9c727bSJoao Martins struct iova_bitmap *iova_bitmap_alloc(unsigned long iova, size_t length,
2488c9c727bSJoao Martins 				      unsigned long page_size, u64 __user *data)
2498c9c727bSJoao Martins {
2508c9c727bSJoao Martins 	struct iova_bitmap_map *mapped;
2518c9c727bSJoao Martins 	struct iova_bitmap *bitmap;
2528c9c727bSJoao Martins 	int rc;
2538c9c727bSJoao Martins 
2548c9c727bSJoao Martins 	bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
2558c9c727bSJoao Martins 	if (!bitmap)
2568c9c727bSJoao Martins 		return ERR_PTR(-ENOMEM);
2578c9c727bSJoao Martins 
2588c9c727bSJoao Martins 	mapped = &bitmap->mapped;
2598c9c727bSJoao Martins 	mapped->pgshift = __ffs(page_size);
260d18411ecSJoao Martins 	bitmap->bitmap = (u8 __user *)data;
2618c9c727bSJoao Martins 	bitmap->mapped_total_index =
2628c9c727bSJoao Martins 		iova_bitmap_offset_to_index(bitmap, length - 1) + 1;
2638c9c727bSJoao Martins 	bitmap->iova = iova;
2648c9c727bSJoao Martins 	bitmap->length = length;
2658c9c727bSJoao Martins 	mapped->iova = iova;
2668c9c727bSJoao Martins 	mapped->pages = (struct page **)__get_free_page(GFP_KERNEL);
2678c9c727bSJoao Martins 	if (!mapped->pages) {
2688c9c727bSJoao Martins 		rc = -ENOMEM;
2698c9c727bSJoao Martins 		goto err;
2708c9c727bSJoao Martins 	}
2718c9c727bSJoao Martins 
2728c9c727bSJoao Martins 	rc = iova_bitmap_get(bitmap);
2738c9c727bSJoao Martins 	if (rc)
2748c9c727bSJoao Martins 		goto err;
2758c9c727bSJoao Martins 	return bitmap;
2768c9c727bSJoao Martins 
2778c9c727bSJoao Martins err:
2788c9c727bSJoao Martins 	iova_bitmap_free(bitmap);
2798c9c727bSJoao Martins 	return ERR_PTR(rc);
2808c9c727bSJoao Martins }
28113578d4eSJoao Martins EXPORT_SYMBOL_NS_GPL(iova_bitmap_alloc, IOMMUFD);
2828c9c727bSJoao Martins 
2838c9c727bSJoao Martins /**
2848c9c727bSJoao Martins  * iova_bitmap_free() - Frees an IOVA bitmap object
2858c9c727bSJoao Martins  * @bitmap: IOVA bitmap to free
2868c9c727bSJoao Martins  *
2878c9c727bSJoao Martins  * It unpins and releases pages array memory and clears any leftover
2888c9c727bSJoao Martins  * state.
2898c9c727bSJoao Martins  */
2908c9c727bSJoao Martins void iova_bitmap_free(struct iova_bitmap *bitmap)
2918c9c727bSJoao Martins {
2928c9c727bSJoao Martins 	struct iova_bitmap_map *mapped = &bitmap->mapped;
2938c9c727bSJoao Martins 
2948c9c727bSJoao Martins 	iova_bitmap_put(bitmap);
2958c9c727bSJoao Martins 
2968c9c727bSJoao Martins 	if (mapped->pages) {
2978c9c727bSJoao Martins 		free_page((unsigned long)mapped->pages);
2988c9c727bSJoao Martins 		mapped->pages = NULL;
2998c9c727bSJoao Martins 	}
3008c9c727bSJoao Martins 
3018c9c727bSJoao Martins 	kfree(bitmap);
3028c9c727bSJoao Martins }
30313578d4eSJoao Martins EXPORT_SYMBOL_NS_GPL(iova_bitmap_free, IOMMUFD);
3048c9c727bSJoao Martins 
3058c9c727bSJoao Martins /*
3068c9c727bSJoao Martins  * Returns the remaining bitmap indexes from mapped_total_index to process for
3078c9c727bSJoao Martins  * the currently pinned bitmap pages.
3088c9c727bSJoao Martins  */
3098c9c727bSJoao Martins static unsigned long iova_bitmap_mapped_remaining(struct iova_bitmap *bitmap)
3108c9c727bSJoao Martins {
3118c9c727bSJoao Martins 	unsigned long remaining, bytes;
3128c9c727bSJoao Martins 
3138c9c727bSJoao Martins 	bytes = (bitmap->mapped.npages << PAGE_SHIFT) - bitmap->mapped.pgoff;
3148c9c727bSJoao Martins 
3158c9c727bSJoao Martins 	remaining = bitmap->mapped_total_index - bitmap->mapped_base_index;
3168c9c727bSJoao Martins 	remaining = min_t(unsigned long, remaining,
317d18411ecSJoao Martins 			  DIV_ROUND_UP(bytes, sizeof(*bitmap->bitmap)));
3188c9c727bSJoao Martins 
3198c9c727bSJoao Martins 	return remaining;
3208c9c727bSJoao Martins }
3218c9c727bSJoao Martins 
3228c9c727bSJoao Martins /*
3238c9c727bSJoao Martins  * Returns the length of the mapped IOVA range.
3248c9c727bSJoao Martins  */
3258c9c727bSJoao Martins static unsigned long iova_bitmap_mapped_length(struct iova_bitmap *bitmap)
3268c9c727bSJoao Martins {
3278c9c727bSJoao Martins 	unsigned long max_iova = bitmap->iova + bitmap->length - 1;
3288c9c727bSJoao Martins 	unsigned long iova = iova_bitmap_mapped_iova(bitmap);
3298c9c727bSJoao Martins 	unsigned long remaining;
3308c9c727bSJoao Martins 
3318c9c727bSJoao Martins 	/*
3328c9c727bSJoao Martins 	 * iova_bitmap_mapped_remaining() returns a number of indexes which
3338c9c727bSJoao Martins 	 * when converted to IOVA gives us a max length that the bitmap
3348c9c727bSJoao Martins 	 * pinned data can cover. Afterwards, that is capped to
3358c9c727bSJoao Martins 	 * only cover the IOVA range in @bitmap::iova .. @bitmap::length.
3368c9c727bSJoao Martins 	 */
3378c9c727bSJoao Martins 	remaining = iova_bitmap_index_to_offset(bitmap,
3388c9c727bSJoao Martins 			iova_bitmap_mapped_remaining(bitmap));
3398c9c727bSJoao Martins 
3408c9c727bSJoao Martins 	if (iova + remaining - 1 > max_iova)
3418c9c727bSJoao Martins 		remaining -= ((iova + remaining - 1) - max_iova);
3428c9c727bSJoao Martins 
3438c9c727bSJoao Martins 	return remaining;
3448c9c727bSJoao Martins }
3458c9c727bSJoao Martins 
3468c9c727bSJoao Martins /*
3478c9c727bSJoao Martins  * Returns true if there's not more data to iterate.
3488c9c727bSJoao Martins  */
3498c9c727bSJoao Martins static bool iova_bitmap_done(struct iova_bitmap *bitmap)
3508c9c727bSJoao Martins {
3518c9c727bSJoao Martins 	return bitmap->mapped_base_index >= bitmap->mapped_total_index;
3528c9c727bSJoao Martins }
3538c9c727bSJoao Martins 
3542780025eSJoao Martins static int iova_bitmap_set_ahead(struct iova_bitmap *bitmap,
3552780025eSJoao Martins 				 size_t set_ahead_length)
3562780025eSJoao Martins {
3572780025eSJoao Martins 	int ret = 0;
3582780025eSJoao Martins 
3592780025eSJoao Martins 	while (set_ahead_length > 0 && !iova_bitmap_done(bitmap)) {
3602780025eSJoao Martins 		unsigned long length = iova_bitmap_mapped_length(bitmap);
3612780025eSJoao Martins 		unsigned long iova = iova_bitmap_mapped_iova(bitmap);
3622780025eSJoao Martins 
3632780025eSJoao Martins 		ret = iova_bitmap_get(bitmap);
3642780025eSJoao Martins 		if (ret)
3652780025eSJoao Martins 			break;
3662780025eSJoao Martins 
3672780025eSJoao Martins 		length = min(length, set_ahead_length);
3682780025eSJoao Martins 		iova_bitmap_set(bitmap, iova, length);
3692780025eSJoao Martins 
3702780025eSJoao Martins 		set_ahead_length -= length;
3712780025eSJoao Martins 		bitmap->mapped_base_index +=
3722780025eSJoao Martins 			iova_bitmap_offset_to_index(bitmap, length - 1) + 1;
3732780025eSJoao Martins 		iova_bitmap_put(bitmap);
3742780025eSJoao Martins 	}
3752780025eSJoao Martins 
3762780025eSJoao Martins 	bitmap->set_ahead_length = 0;
3772780025eSJoao Martins 	return ret;
3782780025eSJoao Martins }
3792780025eSJoao Martins 
3808c9c727bSJoao Martins /*
3818c9c727bSJoao Martins  * Advances to the next range, releases the current pinned
3828c9c727bSJoao Martins  * pages and pins the next set of bitmap pages.
3838c9c727bSJoao Martins  * Returns 0 on success or otherwise errno.
3848c9c727bSJoao Martins  */
3858c9c727bSJoao Martins static int iova_bitmap_advance(struct iova_bitmap *bitmap)
3868c9c727bSJoao Martins {
3878c9c727bSJoao Martins 	unsigned long iova = iova_bitmap_mapped_length(bitmap) - 1;
3888c9c727bSJoao Martins 	unsigned long count = iova_bitmap_offset_to_index(bitmap, iova) + 1;
3898c9c727bSJoao Martins 
3908c9c727bSJoao Martins 	bitmap->mapped_base_index += count;
3918c9c727bSJoao Martins 
3928c9c727bSJoao Martins 	iova_bitmap_put(bitmap);
3938c9c727bSJoao Martins 
3942780025eSJoao Martins 	/* Iterate, set and skip any bits requested for next iteration */
3952780025eSJoao Martins 	if (bitmap->set_ahead_length) {
3962780025eSJoao Martins 		int ret;
3972780025eSJoao Martins 
3982780025eSJoao Martins 		ret = iova_bitmap_set_ahead(bitmap, bitmap->set_ahead_length);
3992780025eSJoao Martins 		if (ret)
4002780025eSJoao Martins 			return ret;
4012780025eSJoao Martins 	}
4022780025eSJoao Martins 
40379258365SJoao Martins 	if (iova_bitmap_done(bitmap))
40479258365SJoao Martins 		return 0;
40579258365SJoao Martins 
4068c9c727bSJoao Martins 	/* When advancing the index we pin the next set of bitmap pages */
4078c9c727bSJoao Martins 	return iova_bitmap_get(bitmap);
4088c9c727bSJoao Martins }
4098c9c727bSJoao Martins 
4108c9c727bSJoao Martins /**
4118c9c727bSJoao Martins  * iova_bitmap_for_each() - Iterates over the bitmap
4128c9c727bSJoao Martins  * @bitmap: IOVA bitmap to iterate
4138c9c727bSJoao Martins  * @opaque: Additional argument to pass to the callback
4148c9c727bSJoao Martins  * @fn: Function that gets called for each IOVA range
4158c9c727bSJoao Martins  *
4168c9c727bSJoao Martins  * Helper function to iterate over bitmap data representing a portion of IOVA
4178c9c727bSJoao Martins  * space. It hides the complexity of iterating bitmaps and translating the
4188c9c727bSJoao Martins  * mapped bitmap user pages into IOVA ranges to process.
4198c9c727bSJoao Martins  *
4208c9c727bSJoao Martins  * Return: 0 on success, and an error on failure either upon
4218c9c727bSJoao Martins  * iteration or when the callback returns an error.
4228c9c727bSJoao Martins  */
4238c9c727bSJoao Martins int iova_bitmap_for_each(struct iova_bitmap *bitmap, void *opaque,
4248c9c727bSJoao Martins 			 iova_bitmap_fn_t fn)
4258c9c727bSJoao Martins {
4268c9c727bSJoao Martins 	int ret = 0;
4278c9c727bSJoao Martins 
4288c9c727bSJoao Martins 	for (; !iova_bitmap_done(bitmap) && !ret;
4298c9c727bSJoao Martins 	     ret = iova_bitmap_advance(bitmap)) {
4308c9c727bSJoao Martins 		ret = fn(bitmap, iova_bitmap_mapped_iova(bitmap),
4318c9c727bSJoao Martins 			 iova_bitmap_mapped_length(bitmap), opaque);
4328c9c727bSJoao Martins 		if (ret)
4338c9c727bSJoao Martins 			break;
4348c9c727bSJoao Martins 	}
4358c9c727bSJoao Martins 
4368c9c727bSJoao Martins 	return ret;
4378c9c727bSJoao Martins }
43813578d4eSJoao Martins EXPORT_SYMBOL_NS_GPL(iova_bitmap_for_each, IOMMUFD);
4398c9c727bSJoao Martins 
4408c9c727bSJoao Martins /**
4418c9c727bSJoao Martins  * iova_bitmap_set() - Records an IOVA range in bitmap
4428c9c727bSJoao Martins  * @bitmap: IOVA bitmap
4438c9c727bSJoao Martins  * @iova: IOVA to start
4448c9c727bSJoao Martins  * @length: IOVA range length
4458c9c727bSJoao Martins  *
4468c9c727bSJoao Martins  * Set the bits corresponding to the range [iova .. iova+length-1] in
4478c9c727bSJoao Martins  * the user bitmap.
4488c9c727bSJoao Martins  *
4498c9c727bSJoao Martins  */
4508c9c727bSJoao Martins void iova_bitmap_set(struct iova_bitmap *bitmap,
4518c9c727bSJoao Martins 		     unsigned long iova, size_t length)
4528c9c727bSJoao Martins {
4538c9c727bSJoao Martins 	struct iova_bitmap_map *mapped = &bitmap->mapped;
4548c9c727bSJoao Martins 	unsigned long cur_bit = ((iova - mapped->iova) >>
4558c9c727bSJoao Martins 			mapped->pgshift) + mapped->pgoff * BITS_PER_BYTE;
4568c9c727bSJoao Martins 	unsigned long last_bit = (((iova + length - 1) - mapped->iova) >>
4578c9c727bSJoao Martins 			mapped->pgshift) + mapped->pgoff * BITS_PER_BYTE;
458a4ab7dedSJoao Martins 	unsigned long last_page_idx = mapped->npages - 1;
4598c9c727bSJoao Martins 
4608c9c727bSJoao Martins 	do {
4618c9c727bSJoao Martins 		unsigned int page_idx = cur_bit / BITS_PER_PAGE;
4628c9c727bSJoao Martins 		unsigned int offset = cur_bit % BITS_PER_PAGE;
4638c9c727bSJoao Martins 		unsigned int nbits = min(BITS_PER_PAGE - offset,
4648c9c727bSJoao Martins 					 last_bit - cur_bit + 1);
4658c9c727bSJoao Martins 		void *kaddr;
4668c9c727bSJoao Martins 
467a4ab7dedSJoao Martins 		if (unlikely(page_idx > last_page_idx))
468a4ab7dedSJoao Martins 			break;
469a4ab7dedSJoao Martins 
4708c9c727bSJoao Martins 		kaddr = kmap_local_page(mapped->pages[page_idx]);
4718c9c727bSJoao Martins 		bitmap_set(kaddr, offset, nbits);
4728c9c727bSJoao Martins 		kunmap_local(kaddr);
4738c9c727bSJoao Martins 		cur_bit += nbits;
4748c9c727bSJoao Martins 	} while (cur_bit <= last_bit);
4752780025eSJoao Martins 
4762780025eSJoao Martins 	if (unlikely(cur_bit <= last_bit)) {
4772780025eSJoao Martins 		bitmap->set_ahead_length =
4782780025eSJoao Martins 			((last_bit - cur_bit + 1) << bitmap->mapped.pgshift);
4792780025eSJoao Martins 	}
4808c9c727bSJoao Martins }
48113578d4eSJoao Martins EXPORT_SYMBOL_NS_GPL(iova_bitmap_set, IOMMUFD);
482