blob: 01b3627999304ec3dfbb5bd5f17da4aa26915145 [file] [log] [blame]
Christoph Hellwig1fbaf8f2021-04-29 22:57:32 -07001// SPDX-License-Identifier: GPL-2.0-only
2
3#include <linux/mm.h>
4#include <linux/io-mapping.h>
5
6/**
7 * io_mapping_map_user - remap an I/O mapping to userspace
8 * @iomap: the source io_mapping
9 * @vma: user vma to map to
10 * @addr: target user address to start at
11 * @pfn: physical address of kernel memory
12 * @size: size of map area
13 *
14 * Note: this is only safe if the mm semaphore is held when called.
15 */
16int io_mapping_map_user(struct io_mapping *iomap, struct vm_area_struct *vma,
17 unsigned long addr, unsigned long pfn, unsigned long size)
18{
19 vm_flags_t expected_flags = VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
20
21 if (WARN_ON_ONCE((vma->vm_flags & expected_flags) != expected_flags))
22 return -EINVAL;
23
24 /* We rely on prevalidation of the io-mapping to skip track_pfn(). */
25 return remap_pfn_range_notrack(vma, addr, pfn, size,
26 __pgprot((pgprot_val(iomap->prot) & _PAGE_CACHE_MASK) |
27 (pgprot_val(vma->vm_page_prot) & ~_PAGE_CACHE_MASK)));
28}
29EXPORT_SYMBOL_GPL(io_mapping_map_user);