xref: /linux/drivers/iommu/iommufd/iova_bitmap.c (revision d18411ec305728c6371806c4fb09be07016aad0b)
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 
388c9c727bSJoao Martins 	/* page size order that each bit granules to */
398c9c727bSJoao Martins 	unsigned long pgshift;
408c9c727bSJoao Martins 
418c9c727bSJoao Martins 	/* page offset of the first user page pinned */
428c9c727bSJoao Martins 	unsigned long pgoff;
438c9c727bSJoao Martins 
448c9c727bSJoao Martins 	/* number of pages pinned */
458c9c727bSJoao Martins 	unsigned long npages;
468c9c727bSJoao Martins 
478c9c727bSJoao Martins 	/* pinned pages representing the bitmap data */
488c9c727bSJoao Martins 	struct page **pages;
498c9c727bSJoao Martins };
508c9c727bSJoao Martins 
518c9c727bSJoao Martins /*
528c9c727bSJoao Martins  * struct iova_bitmap - The IOVA bitmap object
538c9c727bSJoao Martins  *
548c9c727bSJoao Martins  * Main data structure for iterating over the bitmap data.
558c9c727bSJoao Martins  *
568c9c727bSJoao Martins  * Abstracts the pinning work and iterates in IOVA ranges.
578c9c727bSJoao Martins  * It uses a windowing scheme and pins the bitmap in relatively
588c9c727bSJoao Martins  * big ranges e.g.
598c9c727bSJoao Martins  *
608c9c727bSJoao Martins  * The bitmap object uses one base page to store all the pinned pages
618c9c727bSJoao Martins  * pointers related to the bitmap. For sizeof(struct page*) == 8 it stores
628c9c727bSJoao Martins  * 512 struct page pointers which, if the base page size is 4K, it means
638c9c727bSJoao Martins  * 2M of bitmap data is pinned at a time. If the iova_bitmap page size is
648c9c727bSJoao Martins  * also 4K then the range window to iterate is 64G.
658c9c727bSJoao Martins  *
668c9c727bSJoao Martins  * For example iterating on a total IOVA range of 4G..128G, it will walk
678c9c727bSJoao Martins  * through this set of ranges:
688c9c727bSJoao Martins  *
698c9c727bSJoao Martins  *    4G  -  68G-1 (64G)
708c9c727bSJoao Martins  *    68G - 128G-1 (64G)
718c9c727bSJoao Martins  *
728c9c727bSJoao Martins  * An example of the APIs on how to use/iterate over the IOVA bitmap:
738c9c727bSJoao Martins  *
748c9c727bSJoao Martins  *   bitmap = iova_bitmap_alloc(iova, length, page_size, data);
758c9c727bSJoao Martins  *   if (IS_ERR(bitmap))
768c9c727bSJoao Martins  *       return PTR_ERR(bitmap);
778c9c727bSJoao Martins  *
788c9c727bSJoao Martins  *   ret = iova_bitmap_for_each(bitmap, arg, dirty_reporter_fn);
798c9c727bSJoao Martins  *
808c9c727bSJoao Martins  *   iova_bitmap_free(bitmap);
818c9c727bSJoao Martins  *
828c9c727bSJoao Martins  * Each iteration of the @dirty_reporter_fn is called with a unique @iova
838c9c727bSJoao Martins  * and @length argument, indicating the current range available through the
848c9c727bSJoao Martins  * iova_bitmap. The @dirty_reporter_fn uses iova_bitmap_set() to mark dirty
858c9c727bSJoao Martins  * areas (@iova_length) within that provided range, as following:
868c9c727bSJoao Martins  *
878c9c727bSJoao Martins  *   iova_bitmap_set(bitmap, iova, iova_length);
888c9c727bSJoao Martins  *
898c9c727bSJoao Martins  * The internals of the object uses an index @mapped_base_index that indexes
908c9c727bSJoao Martins  * which u64 word of the bitmap is mapped, up to @mapped_total_index.
918c9c727bSJoao Martins  * Those keep being incremented until @mapped_total_index is reached while
928c9c727bSJoao Martins  * mapping up to PAGE_SIZE / sizeof(struct page*) maximum of pages.
938c9c727bSJoao Martins  *
948c9c727bSJoao Martins  * The IOVA bitmap is usually located on what tracks DMA mapped ranges or
958c9c727bSJoao Martins  * some form of IOVA range tracking that co-relates to the user passed
968c9c727bSJoao Martins  * bitmap.
978c9c727bSJoao Martins  */
988c9c727bSJoao Martins struct iova_bitmap {
998c9c727bSJoao Martins 	/* IOVA range representing the currently mapped bitmap data */
1008c9c727bSJoao Martins 	struct iova_bitmap_map mapped;
1018c9c727bSJoao Martins 
1028c9c727bSJoao Martins 	/* userspace address of the bitmap */
103*d18411ecSJoao Martins 	u8 __user *bitmap;
1048c9c727bSJoao Martins 
1058c9c727bSJoao Martins 	/* u64 index that @mapped points to */
1068c9c727bSJoao Martins 	unsigned long mapped_base_index;
1078c9c727bSJoao Martins 
1088c9c727bSJoao Martins 	/* how many u64 can we walk in total */
1098c9c727bSJoao Martins 	unsigned long mapped_total_index;
1108c9c727bSJoao Martins 
1118c9c727bSJoao Martins 	/* base IOVA of the whole bitmap */
1128c9c727bSJoao Martins 	unsigned long iova;
1138c9c727bSJoao Martins 
1148c9c727bSJoao Martins 	/* length of the IOVA range for the whole bitmap */
1158c9c727bSJoao Martins 	size_t length;
1168c9c727bSJoao Martins };
1178c9c727bSJoao Martins 
1188c9c727bSJoao Martins /*
1198c9c727bSJoao Martins  * Converts a relative IOVA to a bitmap index.
1208c9c727bSJoao Martins  * This function provides the index into the u64 array (bitmap::bitmap)
1218c9c727bSJoao Martins  * for a given IOVA offset.
1228c9c727bSJoao Martins  * Relative IOVA means relative to the bitmap::mapped base IOVA
1238c9c727bSJoao Martins  * (stored in mapped::iova). All computations in this file are done using
1248c9c727bSJoao Martins  * relative IOVAs and thus avoid an extra subtraction against mapped::iova.
1258c9c727bSJoao Martins  * The user API iova_bitmap_set() always uses a regular absolute IOVAs.
1268c9c727bSJoao Martins  */
1278c9c727bSJoao Martins static unsigned long iova_bitmap_offset_to_index(struct iova_bitmap *bitmap,
1288c9c727bSJoao Martins 						 unsigned long iova)
1298c9c727bSJoao Martins {
1308c9c727bSJoao Martins 	unsigned long pgsize = 1 << bitmap->mapped.pgshift;
1318c9c727bSJoao Martins 
1328c9c727bSJoao Martins 	return iova / (BITS_PER_TYPE(*bitmap->bitmap) * pgsize);
1338c9c727bSJoao Martins }
1348c9c727bSJoao Martins 
1358c9c727bSJoao Martins /*
1368c9c727bSJoao Martins  * Converts a bitmap index to a *relative* IOVA.
1378c9c727bSJoao Martins  */
1388c9c727bSJoao Martins static unsigned long iova_bitmap_index_to_offset(struct iova_bitmap *bitmap,
1398c9c727bSJoao Martins 						 unsigned long index)
1408c9c727bSJoao Martins {
1418c9c727bSJoao Martins 	unsigned long pgshift = bitmap->mapped.pgshift;
1428c9c727bSJoao Martins 
1438c9c727bSJoao Martins 	return (index * BITS_PER_TYPE(*bitmap->bitmap)) << pgshift;
1448c9c727bSJoao Martins }
1458c9c727bSJoao Martins 
1468c9c727bSJoao Martins /*
1478c9c727bSJoao Martins  * Returns the base IOVA of the mapped range.
1488c9c727bSJoao Martins  */
1498c9c727bSJoao Martins static unsigned long iova_bitmap_mapped_iova(struct iova_bitmap *bitmap)
1508c9c727bSJoao Martins {
1518c9c727bSJoao Martins 	unsigned long skip = bitmap->mapped_base_index;
1528c9c727bSJoao Martins 
1538c9c727bSJoao Martins 	return bitmap->iova + iova_bitmap_index_to_offset(bitmap, skip);
1548c9c727bSJoao Martins }
1558c9c727bSJoao Martins 
1568c9c727bSJoao Martins /*
1578c9c727bSJoao Martins  * Pins the bitmap user pages for the current range window.
1588c9c727bSJoao Martins  * This is internal to IOVA bitmap and called when advancing the
1598c9c727bSJoao Martins  * index (@mapped_base_index) or allocating the bitmap.
1608c9c727bSJoao Martins  */
1618c9c727bSJoao Martins static int iova_bitmap_get(struct iova_bitmap *bitmap)
1628c9c727bSJoao Martins {
1638c9c727bSJoao Martins 	struct iova_bitmap_map *mapped = &bitmap->mapped;
1648c9c727bSJoao Martins 	unsigned long npages;
165*d18411ecSJoao Martins 	u8 __user *addr;
1668c9c727bSJoao Martins 	long ret;
1678c9c727bSJoao Martins 
1688c9c727bSJoao Martins 	/*
1698c9c727bSJoao Martins 	 * @mapped_base_index is the index of the currently mapped u64 words
1708c9c727bSJoao Martins 	 * that we have access. Anything before @mapped_base_index is not
1718c9c727bSJoao Martins 	 * mapped. The range @mapped_base_index .. @mapped_total_index-1 is
1728c9c727bSJoao Martins 	 * mapped but capped at a maximum number of pages.
1738c9c727bSJoao Martins 	 */
1748c9c727bSJoao Martins 	npages = DIV_ROUND_UP((bitmap->mapped_total_index -
1758c9c727bSJoao Martins 			       bitmap->mapped_base_index) *
1768c9c727bSJoao Martins 			       sizeof(*bitmap->bitmap), PAGE_SIZE);
1778c9c727bSJoao Martins 
1788c9c727bSJoao Martins 	/*
1798c9c727bSJoao Martins 	 * We always cap at max number of 'struct page' a base page can fit.
1808c9c727bSJoao Martins 	 * This is, for example, on x86 means 2M of bitmap data max.
1818c9c727bSJoao Martins 	 */
1828c9c727bSJoao Martins 	npages = min(npages,  PAGE_SIZE / sizeof(struct page *));
1838c9c727bSJoao Martins 
1848c9c727bSJoao Martins 	/*
1858c9c727bSJoao Martins 	 * Bitmap address to be pinned is calculated via pointer arithmetic
1868c9c727bSJoao Martins 	 * with bitmap u64 word index.
1878c9c727bSJoao Martins 	 */
1888c9c727bSJoao Martins 	addr = bitmap->bitmap + bitmap->mapped_base_index;
1898c9c727bSJoao Martins 
1908c9c727bSJoao Martins 	ret = pin_user_pages_fast((unsigned long)addr, npages,
1918c9c727bSJoao Martins 				  FOLL_WRITE, mapped->pages);
1928c9c727bSJoao Martins 	if (ret <= 0)
1938c9c727bSJoao Martins 		return -EFAULT;
1948c9c727bSJoao Martins 
1958c9c727bSJoao Martins 	mapped->npages = (unsigned long)ret;
1968c9c727bSJoao Martins 	/* Base IOVA where @pages point to i.e. bit 0 of the first page */
1978c9c727bSJoao Martins 	mapped->iova = iova_bitmap_mapped_iova(bitmap);
1988c9c727bSJoao Martins 
1998c9c727bSJoao Martins 	/*
2008c9c727bSJoao Martins 	 * offset of the page where pinned pages bit 0 is located.
2018c9c727bSJoao Martins 	 * This handles the case where the bitmap is not PAGE_SIZE
2028c9c727bSJoao Martins 	 * aligned.
2038c9c727bSJoao Martins 	 */
2048c9c727bSJoao Martins 	mapped->pgoff = offset_in_page(addr);
2058c9c727bSJoao Martins 	return 0;
2068c9c727bSJoao Martins }
2078c9c727bSJoao Martins 
2088c9c727bSJoao Martins /*
2098c9c727bSJoao Martins  * Unpins the bitmap user pages and clears @npages
2108c9c727bSJoao Martins  * (un)pinning is abstracted from API user and it's done when advancing
2118c9c727bSJoao Martins  * the index or freeing the bitmap.
2128c9c727bSJoao Martins  */
2138c9c727bSJoao Martins static void iova_bitmap_put(struct iova_bitmap *bitmap)
2148c9c727bSJoao Martins {
2158c9c727bSJoao Martins 	struct iova_bitmap_map *mapped = &bitmap->mapped;
2168c9c727bSJoao Martins 
2178c9c727bSJoao Martins 	if (mapped->npages) {
2188c9c727bSJoao Martins 		unpin_user_pages(mapped->pages, mapped->npages);
2198c9c727bSJoao Martins 		mapped->npages = 0;
2208c9c727bSJoao Martins 	}
2218c9c727bSJoao Martins }
2228c9c727bSJoao Martins 
2238c9c727bSJoao Martins /**
2248c9c727bSJoao Martins  * iova_bitmap_alloc() - Allocates an IOVA bitmap object
2258c9c727bSJoao Martins  * @iova: Start address of the IOVA range
2268c9c727bSJoao Martins  * @length: Length of the IOVA range
2278c9c727bSJoao Martins  * @page_size: Page size of the IOVA bitmap. It defines what each bit
2288c9c727bSJoao Martins  *             granularity represents
2298c9c727bSJoao Martins  * @data: Userspace address of the bitmap
2308c9c727bSJoao Martins  *
2318c9c727bSJoao Martins  * Allocates an IOVA object and initializes all its fields including the
2328c9c727bSJoao Martins  * first user pages of @data.
2338c9c727bSJoao Martins  *
2348c9c727bSJoao Martins  * Return: A pointer to a newly allocated struct iova_bitmap
2358c9c727bSJoao Martins  * or ERR_PTR() on error.
2368c9c727bSJoao Martins  */
2378c9c727bSJoao Martins struct iova_bitmap *iova_bitmap_alloc(unsigned long iova, size_t length,
2388c9c727bSJoao Martins 				      unsigned long page_size, u64 __user *data)
2398c9c727bSJoao Martins {
2408c9c727bSJoao Martins 	struct iova_bitmap_map *mapped;
2418c9c727bSJoao Martins 	struct iova_bitmap *bitmap;
2428c9c727bSJoao Martins 	int rc;
2438c9c727bSJoao Martins 
2448c9c727bSJoao Martins 	bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
2458c9c727bSJoao Martins 	if (!bitmap)
2468c9c727bSJoao Martins 		return ERR_PTR(-ENOMEM);
2478c9c727bSJoao Martins 
2488c9c727bSJoao Martins 	mapped = &bitmap->mapped;
2498c9c727bSJoao Martins 	mapped->pgshift = __ffs(page_size);
250*d18411ecSJoao Martins 	bitmap->bitmap = (u8 __user *)data;
2518c9c727bSJoao Martins 	bitmap->mapped_total_index =
2528c9c727bSJoao Martins 		iova_bitmap_offset_to_index(bitmap, length - 1) + 1;
2538c9c727bSJoao Martins 	bitmap->iova = iova;
2548c9c727bSJoao Martins 	bitmap->length = length;
2558c9c727bSJoao Martins 	mapped->iova = iova;
2568c9c727bSJoao Martins 	mapped->pages = (struct page **)__get_free_page(GFP_KERNEL);
2578c9c727bSJoao Martins 	if (!mapped->pages) {
2588c9c727bSJoao Martins 		rc = -ENOMEM;
2598c9c727bSJoao Martins 		goto err;
2608c9c727bSJoao Martins 	}
2618c9c727bSJoao Martins 
2628c9c727bSJoao Martins 	rc = iova_bitmap_get(bitmap);
2638c9c727bSJoao Martins 	if (rc)
2648c9c727bSJoao Martins 		goto err;
2658c9c727bSJoao Martins 	return bitmap;
2668c9c727bSJoao Martins 
2678c9c727bSJoao Martins err:
2688c9c727bSJoao Martins 	iova_bitmap_free(bitmap);
2698c9c727bSJoao Martins 	return ERR_PTR(rc);
2708c9c727bSJoao Martins }
27113578d4eSJoao Martins EXPORT_SYMBOL_NS_GPL(iova_bitmap_alloc, IOMMUFD);
2728c9c727bSJoao Martins 
2738c9c727bSJoao Martins /**
2748c9c727bSJoao Martins  * iova_bitmap_free() - Frees an IOVA bitmap object
2758c9c727bSJoao Martins  * @bitmap: IOVA bitmap to free
2768c9c727bSJoao Martins  *
2778c9c727bSJoao Martins  * It unpins and releases pages array memory and clears any leftover
2788c9c727bSJoao Martins  * state.
2798c9c727bSJoao Martins  */
2808c9c727bSJoao Martins void iova_bitmap_free(struct iova_bitmap *bitmap)
2818c9c727bSJoao Martins {
2828c9c727bSJoao Martins 	struct iova_bitmap_map *mapped = &bitmap->mapped;
2838c9c727bSJoao Martins 
2848c9c727bSJoao Martins 	iova_bitmap_put(bitmap);
2858c9c727bSJoao Martins 
2868c9c727bSJoao Martins 	if (mapped->pages) {
2878c9c727bSJoao Martins 		free_page((unsigned long)mapped->pages);
2888c9c727bSJoao Martins 		mapped->pages = NULL;
2898c9c727bSJoao Martins 	}
2908c9c727bSJoao Martins 
2918c9c727bSJoao Martins 	kfree(bitmap);
2928c9c727bSJoao Martins }
29313578d4eSJoao Martins EXPORT_SYMBOL_NS_GPL(iova_bitmap_free, IOMMUFD);
2948c9c727bSJoao Martins 
2958c9c727bSJoao Martins /*
2968c9c727bSJoao Martins  * Returns the remaining bitmap indexes from mapped_total_index to process for
2978c9c727bSJoao Martins  * the currently pinned bitmap pages.
2988c9c727bSJoao Martins  */
2998c9c727bSJoao Martins static unsigned long iova_bitmap_mapped_remaining(struct iova_bitmap *bitmap)
3008c9c727bSJoao Martins {
3018c9c727bSJoao Martins 	unsigned long remaining, bytes;
3028c9c727bSJoao Martins 
3038c9c727bSJoao Martins 	bytes = (bitmap->mapped.npages << PAGE_SHIFT) - bitmap->mapped.pgoff;
3048c9c727bSJoao Martins 
3058c9c727bSJoao Martins 	remaining = bitmap->mapped_total_index - bitmap->mapped_base_index;
3068c9c727bSJoao Martins 	remaining = min_t(unsigned long, remaining,
307*d18411ecSJoao Martins 			  DIV_ROUND_UP(bytes, sizeof(*bitmap->bitmap)));
3088c9c727bSJoao Martins 
3098c9c727bSJoao Martins 	return remaining;
3108c9c727bSJoao Martins }
3118c9c727bSJoao Martins 
3128c9c727bSJoao Martins /*
3138c9c727bSJoao Martins  * Returns the length of the mapped IOVA range.
3148c9c727bSJoao Martins  */
3158c9c727bSJoao Martins static unsigned long iova_bitmap_mapped_length(struct iova_bitmap *bitmap)
3168c9c727bSJoao Martins {
3178c9c727bSJoao Martins 	unsigned long max_iova = bitmap->iova + bitmap->length - 1;
3188c9c727bSJoao Martins 	unsigned long iova = iova_bitmap_mapped_iova(bitmap);
3198c9c727bSJoao Martins 	unsigned long remaining;
3208c9c727bSJoao Martins 
3218c9c727bSJoao Martins 	/*
3228c9c727bSJoao Martins 	 * iova_bitmap_mapped_remaining() returns a number of indexes which
3238c9c727bSJoao Martins 	 * when converted to IOVA gives us a max length that the bitmap
3248c9c727bSJoao Martins 	 * pinned data can cover. Afterwards, that is capped to
3258c9c727bSJoao Martins 	 * only cover the IOVA range in @bitmap::iova .. @bitmap::length.
3268c9c727bSJoao Martins 	 */
3278c9c727bSJoao Martins 	remaining = iova_bitmap_index_to_offset(bitmap,
3288c9c727bSJoao Martins 			iova_bitmap_mapped_remaining(bitmap));
3298c9c727bSJoao Martins 
3308c9c727bSJoao Martins 	if (iova + remaining - 1 > max_iova)
3318c9c727bSJoao Martins 		remaining -= ((iova + remaining - 1) - max_iova);
3328c9c727bSJoao Martins 
3338c9c727bSJoao Martins 	return remaining;
3348c9c727bSJoao Martins }
3358c9c727bSJoao Martins 
3368c9c727bSJoao Martins /*
3378c9c727bSJoao Martins  * Returns true if there's not more data to iterate.
3388c9c727bSJoao Martins  */
3398c9c727bSJoao Martins static bool iova_bitmap_done(struct iova_bitmap *bitmap)
3408c9c727bSJoao Martins {
3418c9c727bSJoao Martins 	return bitmap->mapped_base_index >= bitmap->mapped_total_index;
3428c9c727bSJoao Martins }
3438c9c727bSJoao Martins 
3448c9c727bSJoao Martins /*
3458c9c727bSJoao Martins  * Advances to the next range, releases the current pinned
3468c9c727bSJoao Martins  * pages and pins the next set of bitmap pages.
3478c9c727bSJoao Martins  * Returns 0 on success or otherwise errno.
3488c9c727bSJoao Martins  */
3498c9c727bSJoao Martins static int iova_bitmap_advance(struct iova_bitmap *bitmap)
3508c9c727bSJoao Martins {
3518c9c727bSJoao Martins 	unsigned long iova = iova_bitmap_mapped_length(bitmap) - 1;
3528c9c727bSJoao Martins 	unsigned long count = iova_bitmap_offset_to_index(bitmap, iova) + 1;
3538c9c727bSJoao Martins 
3548c9c727bSJoao Martins 	bitmap->mapped_base_index += count;
3558c9c727bSJoao Martins 
3568c9c727bSJoao Martins 	iova_bitmap_put(bitmap);
3578c9c727bSJoao Martins 	if (iova_bitmap_done(bitmap))
3588c9c727bSJoao Martins 		return 0;
3598c9c727bSJoao Martins 
3608c9c727bSJoao Martins 	/* When advancing the index we pin the next set of bitmap pages */
3618c9c727bSJoao Martins 	return iova_bitmap_get(bitmap);
3628c9c727bSJoao Martins }
3638c9c727bSJoao Martins 
3648c9c727bSJoao Martins /**
3658c9c727bSJoao Martins  * iova_bitmap_for_each() - Iterates over the bitmap
3668c9c727bSJoao Martins  * @bitmap: IOVA bitmap to iterate
3678c9c727bSJoao Martins  * @opaque: Additional argument to pass to the callback
3688c9c727bSJoao Martins  * @fn: Function that gets called for each IOVA range
3698c9c727bSJoao Martins  *
3708c9c727bSJoao Martins  * Helper function to iterate over bitmap data representing a portion of IOVA
3718c9c727bSJoao Martins  * space. It hides the complexity of iterating bitmaps and translating the
3728c9c727bSJoao Martins  * mapped bitmap user pages into IOVA ranges to process.
3738c9c727bSJoao Martins  *
3748c9c727bSJoao Martins  * Return: 0 on success, and an error on failure either upon
3758c9c727bSJoao Martins  * iteration or when the callback returns an error.
3768c9c727bSJoao Martins  */
3778c9c727bSJoao Martins int iova_bitmap_for_each(struct iova_bitmap *bitmap, void *opaque,
3788c9c727bSJoao Martins 			 iova_bitmap_fn_t fn)
3798c9c727bSJoao Martins {
3808c9c727bSJoao Martins 	int ret = 0;
3818c9c727bSJoao Martins 
3828c9c727bSJoao Martins 	for (; !iova_bitmap_done(bitmap) && !ret;
3838c9c727bSJoao Martins 	     ret = iova_bitmap_advance(bitmap)) {
3848c9c727bSJoao Martins 		ret = fn(bitmap, iova_bitmap_mapped_iova(bitmap),
3858c9c727bSJoao Martins 			 iova_bitmap_mapped_length(bitmap), opaque);
3868c9c727bSJoao Martins 		if (ret)
3878c9c727bSJoao Martins 			break;
3888c9c727bSJoao Martins 	}
3898c9c727bSJoao Martins 
3908c9c727bSJoao Martins 	return ret;
3918c9c727bSJoao Martins }
39213578d4eSJoao Martins EXPORT_SYMBOL_NS_GPL(iova_bitmap_for_each, IOMMUFD);
3938c9c727bSJoao Martins 
3948c9c727bSJoao Martins /**
3958c9c727bSJoao Martins  * iova_bitmap_set() - Records an IOVA range in bitmap
3968c9c727bSJoao Martins  * @bitmap: IOVA bitmap
3978c9c727bSJoao Martins  * @iova: IOVA to start
3988c9c727bSJoao Martins  * @length: IOVA range length
3998c9c727bSJoao Martins  *
4008c9c727bSJoao Martins  * Set the bits corresponding to the range [iova .. iova+length-1] in
4018c9c727bSJoao Martins  * the user bitmap.
4028c9c727bSJoao Martins  *
4038c9c727bSJoao Martins  */
4048c9c727bSJoao Martins void iova_bitmap_set(struct iova_bitmap *bitmap,
4058c9c727bSJoao Martins 		     unsigned long iova, size_t length)
4068c9c727bSJoao Martins {
4078c9c727bSJoao Martins 	struct iova_bitmap_map *mapped = &bitmap->mapped;
4088c9c727bSJoao Martins 	unsigned long cur_bit = ((iova - mapped->iova) >>
4098c9c727bSJoao Martins 			mapped->pgshift) + mapped->pgoff * BITS_PER_BYTE;
4108c9c727bSJoao Martins 	unsigned long last_bit = (((iova + length - 1) - mapped->iova) >>
4118c9c727bSJoao Martins 			mapped->pgshift) + mapped->pgoff * BITS_PER_BYTE;
412a4ab7dedSJoao Martins 	unsigned long last_page_idx = mapped->npages - 1;
4138c9c727bSJoao Martins 
4148c9c727bSJoao Martins 	do {
4158c9c727bSJoao Martins 		unsigned int page_idx = cur_bit / BITS_PER_PAGE;
4168c9c727bSJoao Martins 		unsigned int offset = cur_bit % BITS_PER_PAGE;
4178c9c727bSJoao Martins 		unsigned int nbits = min(BITS_PER_PAGE - offset,
4188c9c727bSJoao Martins 					 last_bit - cur_bit + 1);
4198c9c727bSJoao Martins 		void *kaddr;
4208c9c727bSJoao Martins 
421a4ab7dedSJoao Martins 		if (unlikely(page_idx > last_page_idx))
422a4ab7dedSJoao Martins 			break;
423a4ab7dedSJoao Martins 
4248c9c727bSJoao Martins 		kaddr = kmap_local_page(mapped->pages[page_idx]);
4258c9c727bSJoao Martins 		bitmap_set(kaddr, offset, nbits);
4268c9c727bSJoao Martins 		kunmap_local(kaddr);
4278c9c727bSJoao Martins 		cur_bit += nbits;
4288c9c727bSJoao Martins 	} while (cur_bit <= last_bit);
4298c9c727bSJoao Martins }
43013578d4eSJoao Martins EXPORT_SYMBOL_NS_GPL(iova_bitmap_set, IOMMUFD);
431