1f15ed8b4SJens Axboe // SPDX-License-Identifier: GPL-2.0
2f15ed8b4SJens Axboe #include <linux/kernel.h>
3f15ed8b4SJens Axboe #include <linux/init.h>
4f15ed8b4SJens Axboe #include <linux/errno.h>
5f15ed8b4SJens Axboe #include <linux/mm.h>
6f15ed8b4SJens Axboe #include <linux/mman.h>
7f15ed8b4SJens Axboe #include <linux/slab.h>
8f15ed8b4SJens Axboe #include <linux/vmalloc.h>
9f15ed8b4SJens Axboe #include <linux/io_uring.h>
10f15ed8b4SJens Axboe #include <linux/io_uring_types.h>
11f15ed8b4SJens Axboe #include <asm/shmparam.h>
12f15ed8b4SJens Axboe
13f15ed8b4SJens Axboe #include "memmap.h"
14f15ed8b4SJens Axboe #include "kbuf.h"
15dfbbfbf1SPavel Begunkov #include "rsrc.h"
16f15ed8b4SJens Axboe
io_mem_alloc_compound(struct page ** pages,int nr_pages,size_t size,gfp_t gfp)17f15ed8b4SJens Axboe static void *io_mem_alloc_compound(struct page **pages, int nr_pages,
18f15ed8b4SJens Axboe size_t size, gfp_t gfp)
19f15ed8b4SJens Axboe {
20f15ed8b4SJens Axboe struct page *page;
21f15ed8b4SJens Axboe int i, order;
22f15ed8b4SJens Axboe
23f15ed8b4SJens Axboe order = get_order(size);
24f15ed8b4SJens Axboe if (order > MAX_PAGE_ORDER)
25f15ed8b4SJens Axboe return ERR_PTR(-ENOMEM);
26f15ed8b4SJens Axboe else if (order)
27f15ed8b4SJens Axboe gfp |= __GFP_COMP;
28f15ed8b4SJens Axboe
29f15ed8b4SJens Axboe page = alloc_pages(gfp, order);
30f15ed8b4SJens Axboe if (!page)
31f15ed8b4SJens Axboe return ERR_PTR(-ENOMEM);
32f15ed8b4SJens Axboe
33f15ed8b4SJens Axboe for (i = 0; i < nr_pages; i++)
34f15ed8b4SJens Axboe pages[i] = page + i;
35f15ed8b4SJens Axboe
36f15ed8b4SJens Axboe return page_address(page);
37f15ed8b4SJens Axboe }
38f15ed8b4SJens Axboe
io_pin_pages(unsigned long uaddr,unsigned long len,int * npages)39f15ed8b4SJens Axboe struct page **io_pin_pages(unsigned long uaddr, unsigned long len, int *npages)
40f15ed8b4SJens Axboe {
41f15ed8b4SJens Axboe unsigned long start, end, nr_pages;
42f15ed8b4SJens Axboe struct page **pages;
43f15ed8b4SJens Axboe int ret;
44f15ed8b4SJens Axboe
450c0a4eaeSPavel Begunkov if (check_add_overflow(uaddr, len, &end))
460c0a4eaeSPavel Begunkov return ERR_PTR(-EOVERFLOW);
470c0a4eaeSPavel Begunkov if (check_add_overflow(end, PAGE_SIZE - 1, &end))
480c0a4eaeSPavel Begunkov return ERR_PTR(-EOVERFLOW);
490c0a4eaeSPavel Begunkov
500c0a4eaeSPavel Begunkov end = end >> PAGE_SHIFT;
51f15ed8b4SJens Axboe start = uaddr >> PAGE_SHIFT;
52f15ed8b4SJens Axboe nr_pages = end - start;
53f15ed8b4SJens Axboe if (WARN_ON_ONCE(!nr_pages))
54f15ed8b4SJens Axboe return ERR_PTR(-EINVAL);
5568685fa2SPavel Begunkov if (WARN_ON_ONCE(nr_pages > INT_MAX))
5668685fa2SPavel Begunkov return ERR_PTR(-EOVERFLOW);
57f15ed8b4SJens Axboe
58f15ed8b4SJens Axboe pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
59f15ed8b4SJens Axboe if (!pages)
60f15ed8b4SJens Axboe return ERR_PTR(-ENOMEM);
61f15ed8b4SJens Axboe
62f15ed8b4SJens Axboe ret = pin_user_pages_fast(uaddr, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
63f15ed8b4SJens Axboe pages);
64f15ed8b4SJens Axboe /* success, mapped all pages */
65f15ed8b4SJens Axboe if (ret == nr_pages) {
66f15ed8b4SJens Axboe *npages = nr_pages;
67f15ed8b4SJens Axboe return pages;
68f15ed8b4SJens Axboe }
69f15ed8b4SJens Axboe
70f15ed8b4SJens Axboe /* partial map, or didn't map anything */
71f15ed8b4SJens Axboe if (ret >= 0) {
72f15ed8b4SJens Axboe /* if we did partial map, release any pages we did get */
73f15ed8b4SJens Axboe if (ret)
74f15ed8b4SJens Axboe unpin_user_pages(pages, ret);
75f15ed8b4SJens Axboe ret = -EFAULT;
76f15ed8b4SJens Axboe }
77f15ed8b4SJens Axboe kvfree(pages);
78f15ed8b4SJens Axboe return ERR_PTR(ret);
79f15ed8b4SJens Axboe }
80f15ed8b4SJens Axboe
81a730d204SPavel Begunkov enum {
82a730d204SPavel Begunkov /* memory was vmap'ed for the kernel, freeing the region vunmap's it */
83a730d204SPavel Begunkov IO_REGION_F_VMAP = 1,
8416375af3SPavel Begunkov /* memory is provided by user and pinned by the kernel */
8516375af3SPavel Begunkov IO_REGION_F_USER_PROVIDED = 2,
864b851d20SPavel Begunkov /* only the first page in the array is ref'ed */
874b851d20SPavel Begunkov IO_REGION_F_SINGLE_REF = 4,
88a730d204SPavel Begunkov };
89a730d204SPavel Begunkov
io_free_region(struct io_ring_ctx * ctx,struct io_mapped_region * mr)90dfbbfbf1SPavel Begunkov void io_free_region(struct io_ring_ctx *ctx, struct io_mapped_region *mr)
91dfbbfbf1SPavel Begunkov {
92dfbbfbf1SPavel Begunkov if (mr->pages) {
934b851d20SPavel Begunkov long nr_refs = mr->nr_pages;
944b851d20SPavel Begunkov
954b851d20SPavel Begunkov if (mr->flags & IO_REGION_F_SINGLE_REF)
964b851d20SPavel Begunkov nr_refs = 1;
974b851d20SPavel Begunkov
9816375af3SPavel Begunkov if (mr->flags & IO_REGION_F_USER_PROVIDED)
994b851d20SPavel Begunkov unpin_user_pages(mr->pages, nr_refs);
10016375af3SPavel Begunkov else
1014b851d20SPavel Begunkov release_pages(mr->pages, nr_refs);
1024b851d20SPavel Begunkov
103dfbbfbf1SPavel Begunkov kvfree(mr->pages);
104dfbbfbf1SPavel Begunkov }
105a730d204SPavel Begunkov if ((mr->flags & IO_REGION_F_VMAP) && mr->ptr)
106a730d204SPavel Begunkov vunmap(mr->ptr);
107dfbbfbf1SPavel Begunkov if (mr->nr_pages && ctx->user)
108dfbbfbf1SPavel Begunkov __io_unaccount_mem(ctx->user, mr->nr_pages);
109dfbbfbf1SPavel Begunkov
110dfbbfbf1SPavel Begunkov memset(mr, 0, sizeof(*mr));
111dfbbfbf1SPavel Begunkov }
112dfbbfbf1SPavel Begunkov
io_region_init_ptr(struct io_mapped_region * mr)113c4d0ac1cSPavel Begunkov static int io_region_init_ptr(struct io_mapped_region *mr)
114c4d0ac1cSPavel Begunkov {
115c4d0ac1cSPavel Begunkov struct io_imu_folio_data ifd;
116c4d0ac1cSPavel Begunkov void *ptr;
117c4d0ac1cSPavel Begunkov
118c4d0ac1cSPavel Begunkov if (io_check_coalesce_buffer(mr->pages, mr->nr_pages, &ifd)) {
119*f446c631SJens Axboe if (ifd.nr_folios == 1 && !PageHighMem(mr->pages[0])) {
120c4d0ac1cSPavel Begunkov mr->ptr = page_address(mr->pages[0]);
121c4d0ac1cSPavel Begunkov return 0;
122c4d0ac1cSPavel Begunkov }
123c4d0ac1cSPavel Begunkov }
124c4d0ac1cSPavel Begunkov ptr = vmap(mr->pages, mr->nr_pages, VM_MAP, PAGE_KERNEL);
125c4d0ac1cSPavel Begunkov if (!ptr)
126c4d0ac1cSPavel Begunkov return -ENOMEM;
127c4d0ac1cSPavel Begunkov
128c4d0ac1cSPavel Begunkov mr->ptr = ptr;
129c4d0ac1cSPavel Begunkov mr->flags |= IO_REGION_F_VMAP;
130c4d0ac1cSPavel Begunkov return 0;
131c4d0ac1cSPavel Begunkov }
132c4d0ac1cSPavel Begunkov
io_region_pin_pages(struct io_ring_ctx * ctx,struct io_mapped_region * mr,struct io_uring_region_desc * reg)133a90558b3SPavel Begunkov static int io_region_pin_pages(struct io_ring_ctx *ctx,
134a90558b3SPavel Begunkov struct io_mapped_region *mr,
135a90558b3SPavel Begunkov struct io_uring_region_desc *reg)
136a90558b3SPavel Begunkov {
137a90558b3SPavel Begunkov unsigned long size = mr->nr_pages << PAGE_SHIFT;
138a90558b3SPavel Begunkov struct page **pages;
139a90558b3SPavel Begunkov int nr_pages;
140a90558b3SPavel Begunkov
141a90558b3SPavel Begunkov pages = io_pin_pages(reg->user_addr, size, &nr_pages);
142a90558b3SPavel Begunkov if (IS_ERR(pages))
143a90558b3SPavel Begunkov return PTR_ERR(pages);
144a90558b3SPavel Begunkov if (WARN_ON_ONCE(nr_pages != mr->nr_pages))
145a90558b3SPavel Begunkov return -EFAULT;
146a90558b3SPavel Begunkov
147a90558b3SPavel Begunkov mr->pages = pages;
148a90558b3SPavel Begunkov mr->flags |= IO_REGION_F_USER_PROVIDED;
149a90558b3SPavel Begunkov return 0;
150a90558b3SPavel Begunkov }
151a90558b3SPavel Begunkov
io_region_allocate_pages(struct io_ring_ctx * ctx,struct io_mapped_region * mr,struct io_uring_region_desc * reg,unsigned long mmap_offset)1521e21df69SPavel Begunkov static int io_region_allocate_pages(struct io_ring_ctx *ctx,
1531e21df69SPavel Begunkov struct io_mapped_region *mr,
154087f9978SPavel Begunkov struct io_uring_region_desc *reg,
155087f9978SPavel Begunkov unsigned long mmap_offset)
1561e21df69SPavel Begunkov {
1571e21df69SPavel Begunkov gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN;
1581e21df69SPavel Begunkov unsigned long size = mr->nr_pages << PAGE_SHIFT;
1591e21df69SPavel Begunkov unsigned long nr_allocated;
1601e21df69SPavel Begunkov struct page **pages;
1611e21df69SPavel Begunkov void *p;
1621e21df69SPavel Begunkov
1631e21df69SPavel Begunkov pages = kvmalloc_array(mr->nr_pages, sizeof(*pages), gfp);
1641e21df69SPavel Begunkov if (!pages)
1651e21df69SPavel Begunkov return -ENOMEM;
1661e21df69SPavel Begunkov
1671e21df69SPavel Begunkov p = io_mem_alloc_compound(pages, mr->nr_pages, size, gfp);
1681e21df69SPavel Begunkov if (!IS_ERR(p)) {
1691e21df69SPavel Begunkov mr->flags |= IO_REGION_F_SINGLE_REF;
170087f9978SPavel Begunkov goto done;
1711e21df69SPavel Begunkov }
1721e21df69SPavel Begunkov
1739c5968dbSLinus Torvalds nr_allocated = alloc_pages_bulk_node(gfp, NUMA_NO_NODE,
1741e21df69SPavel Begunkov mr->nr_pages, pages);
1751e21df69SPavel Begunkov if (nr_allocated != mr->nr_pages) {
1761e21df69SPavel Begunkov if (nr_allocated)
1771e21df69SPavel Begunkov release_pages(pages, nr_allocated);
1781e21df69SPavel Begunkov kvfree(pages);
1791e21df69SPavel Begunkov return -ENOMEM;
1801e21df69SPavel Begunkov }
181087f9978SPavel Begunkov done:
182087f9978SPavel Begunkov reg->mmap_offset = mmap_offset;
1831e21df69SPavel Begunkov mr->pages = pages;
1841e21df69SPavel Begunkov return 0;
1851e21df69SPavel Begunkov }
1861e21df69SPavel Begunkov
io_create_region(struct io_ring_ctx * ctx,struct io_mapped_region * mr,struct io_uring_region_desc * reg,unsigned long mmap_offset)187dfbbfbf1SPavel Begunkov int io_create_region(struct io_ring_ctx *ctx, struct io_mapped_region *mr,
188087f9978SPavel Begunkov struct io_uring_region_desc *reg,
189087f9978SPavel Begunkov unsigned long mmap_offset)
190dfbbfbf1SPavel Begunkov {
191dfbbfbf1SPavel Begunkov int nr_pages, ret;
192dfbbfbf1SPavel Begunkov u64 end;
193dfbbfbf1SPavel Begunkov
194a730d204SPavel Begunkov if (WARN_ON_ONCE(mr->pages || mr->ptr || mr->nr_pages))
195dfbbfbf1SPavel Begunkov return -EFAULT;
196dfbbfbf1SPavel Begunkov if (memchr_inv(®->__resv, 0, sizeof(reg->__resv)))
197dfbbfbf1SPavel Begunkov return -EINVAL;
1981e21df69SPavel Begunkov if (reg->flags & ~IORING_MEM_REGION_TYPE_USER)
199dfbbfbf1SPavel Begunkov return -EINVAL;
2001e21df69SPavel Begunkov /* user_addr should be set IFF it's a user memory backed region */
2011e21df69SPavel Begunkov if ((reg->flags & IORING_MEM_REGION_TYPE_USER) != !!reg->user_addr)
202dfbbfbf1SPavel Begunkov return -EFAULT;
203dfbbfbf1SPavel Begunkov if (!reg->size || reg->mmap_offset || reg->id)
204dfbbfbf1SPavel Begunkov return -EINVAL;
205dfbbfbf1SPavel Begunkov if ((reg->size >> PAGE_SHIFT) > INT_MAX)
2062ae6bdb1SDan Carpenter return -E2BIG;
207dfbbfbf1SPavel Begunkov if ((reg->user_addr | reg->size) & ~PAGE_MASK)
208dfbbfbf1SPavel Begunkov return -EINVAL;
209dfbbfbf1SPavel Begunkov if (check_add_overflow(reg->user_addr, reg->size, &end))
210dfbbfbf1SPavel Begunkov return -EOVERFLOW;
211dfbbfbf1SPavel Begunkov
212fc5f22a6SPavel Begunkov nr_pages = reg->size >> PAGE_SHIFT;
213dfbbfbf1SPavel Begunkov if (ctx->user) {
214dfbbfbf1SPavel Begunkov ret = __io_account_mem(ctx->user, nr_pages);
215dfbbfbf1SPavel Begunkov if (ret)
216fc5f22a6SPavel Begunkov return ret;
217dfbbfbf1SPavel Begunkov }
218226ae1b4SPavel Begunkov mr->nr_pages = nr_pages;
219dfbbfbf1SPavel Begunkov
2201e21df69SPavel Begunkov if (reg->flags & IORING_MEM_REGION_TYPE_USER)
221a90558b3SPavel Begunkov ret = io_region_pin_pages(ctx, mr, reg);
2221e21df69SPavel Begunkov else
223087f9978SPavel Begunkov ret = io_region_allocate_pages(ctx, mr, reg, mmap_offset);
224a90558b3SPavel Begunkov if (ret)
225fc5f22a6SPavel Begunkov goto out_free;
226fc5f22a6SPavel Begunkov
227c4d0ac1cSPavel Begunkov ret = io_region_init_ptr(mr);
228c4d0ac1cSPavel Begunkov if (ret)
229dfbbfbf1SPavel Begunkov goto out_free;
230dfbbfbf1SPavel Begunkov return 0;
231dfbbfbf1SPavel Begunkov out_free:
232226ae1b4SPavel Begunkov io_free_region(ctx, mr);
233dfbbfbf1SPavel Begunkov return ret;
234dfbbfbf1SPavel Begunkov }
235dfbbfbf1SPavel Begunkov
io_create_region_mmap_safe(struct io_ring_ctx * ctx,struct io_mapped_region * mr,struct io_uring_region_desc * reg,unsigned long mmap_offset)236087f9978SPavel Begunkov int io_create_region_mmap_safe(struct io_ring_ctx *ctx, struct io_mapped_region *mr,
237087f9978SPavel Begunkov struct io_uring_region_desc *reg,
238087f9978SPavel Begunkov unsigned long mmap_offset)
239087f9978SPavel Begunkov {
240087f9978SPavel Begunkov struct io_mapped_region tmp_mr;
241087f9978SPavel Begunkov int ret;
242087f9978SPavel Begunkov
243087f9978SPavel Begunkov memcpy(&tmp_mr, mr, sizeof(tmp_mr));
244087f9978SPavel Begunkov ret = io_create_region(ctx, &tmp_mr, reg, mmap_offset);
245087f9978SPavel Begunkov if (ret)
246087f9978SPavel Begunkov return ret;
247087f9978SPavel Begunkov
248087f9978SPavel Begunkov /*
249087f9978SPavel Begunkov * Once published mmap can find it without holding only the ->mmap_lock
250087f9978SPavel Begunkov * and not ->uring_lock.
251087f9978SPavel Begunkov */
252087f9978SPavel Begunkov guard(mutex)(&ctx->mmap_lock);
253087f9978SPavel Begunkov memcpy(mr, &tmp_mr, sizeof(tmp_mr));
254087f9978SPavel Begunkov return 0;
255087f9978SPavel Begunkov }
256087f9978SPavel Begunkov
io_mmap_get_region(struct io_ring_ctx * ctx,loff_t pgoff)2577cd7b957SPavel Begunkov static struct io_mapped_region *io_mmap_get_region(struct io_ring_ctx *ctx,
2587cd7b957SPavel Begunkov loff_t pgoff)
2597cd7b957SPavel Begunkov {
2607cd7b957SPavel Begunkov loff_t offset = pgoff << PAGE_SHIFT;
2617cd7b957SPavel Begunkov unsigned int bgid;
2627cd7b957SPavel Begunkov
2637cd7b957SPavel Begunkov switch (offset & IORING_OFF_MMAP_MASK) {
2647cd7b957SPavel Begunkov case IORING_OFF_SQ_RING:
2657cd7b957SPavel Begunkov case IORING_OFF_CQ_RING:
2667cd7b957SPavel Begunkov return &ctx->ring_region;
2677cd7b957SPavel Begunkov case IORING_OFF_SQES:
2687cd7b957SPavel Begunkov return &ctx->sq_region;
2697cd7b957SPavel Begunkov case IORING_OFF_PBUF_RING:
2707cd7b957SPavel Begunkov bgid = (offset & ~IORING_OFF_MMAP_MASK) >> IORING_OFF_PBUF_SHIFT;
2717cd7b957SPavel Begunkov return io_pbuf_get_region(ctx, bgid);
2727cd7b957SPavel Begunkov case IORING_MAP_OFF_PARAM_REGION:
2737cd7b957SPavel Begunkov return &ctx->param_region;
27492ade52fSBui Quang Minh case IORING_MAP_OFF_ZCRX_REGION:
27592ade52fSBui Quang Minh return &ctx->zcrx_region;
2767cd7b957SPavel Begunkov }
2777cd7b957SPavel Begunkov return NULL;
2787cd7b957SPavel Begunkov }
2797cd7b957SPavel Begunkov
io_region_validate_mmap(struct io_ring_ctx * ctx,struct io_mapped_region * mr)280087f9978SPavel Begunkov static void *io_region_validate_mmap(struct io_ring_ctx *ctx,
281087f9978SPavel Begunkov struct io_mapped_region *mr)
282087f9978SPavel Begunkov {
283087f9978SPavel Begunkov lockdep_assert_held(&ctx->mmap_lock);
284087f9978SPavel Begunkov
285087f9978SPavel Begunkov if (!io_region_is_set(mr))
286087f9978SPavel Begunkov return ERR_PTR(-EINVAL);
287087f9978SPavel Begunkov if (mr->flags & IO_REGION_F_USER_PROVIDED)
288087f9978SPavel Begunkov return ERR_PTR(-EINVAL);
289087f9978SPavel Begunkov
290087f9978SPavel Begunkov return io_region_get_ptr(mr);
291087f9978SPavel Begunkov }
292087f9978SPavel Begunkov
io_uring_validate_mmap_request(struct file * file,loff_t pgoff,size_t sz)293f15ed8b4SJens Axboe static void *io_uring_validate_mmap_request(struct file *file, loff_t pgoff,
294f15ed8b4SJens Axboe size_t sz)
295f15ed8b4SJens Axboe {
296f15ed8b4SJens Axboe struct io_ring_ctx *ctx = file->private_data;
297ef62de3cSPavel Begunkov struct io_mapped_region *region;
298f15ed8b4SJens Axboe
2997cd7b957SPavel Begunkov region = io_mmap_get_region(ctx, pgoff);
300ef62de3cSPavel Begunkov if (!region)
301ef62de3cSPavel Begunkov return ERR_PTR(-EINVAL);
302ef62de3cSPavel Begunkov return io_region_validate_mmap(ctx, region);
303f15ed8b4SJens Axboe }
304f15ed8b4SJens Axboe
305f15ed8b4SJens Axboe #ifdef CONFIG_MMU
306f15ed8b4SJens Axboe
io_region_mmap(struct io_ring_ctx * ctx,struct io_mapped_region * mr,struct vm_area_struct * vma,unsigned max_pages)307087f9978SPavel Begunkov static int io_region_mmap(struct io_ring_ctx *ctx,
308087f9978SPavel Begunkov struct io_mapped_region *mr,
30981a4058eSPavel Begunkov struct vm_area_struct *vma,
31081a4058eSPavel Begunkov unsigned max_pages)
311087f9978SPavel Begunkov {
31281a4058eSPavel Begunkov unsigned long nr_pages = min(mr->nr_pages, max_pages);
313087f9978SPavel Begunkov
314087f9978SPavel Begunkov vm_flags_set(vma, VM_DONTEXPAND);
315087f9978SPavel Begunkov return vm_insert_pages(vma, vma->vm_start, mr->pages, &nr_pages);
316087f9978SPavel Begunkov }
317087f9978SPavel Begunkov
io_uring_mmap(struct file * file,struct vm_area_struct * vma)318f15ed8b4SJens Axboe __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
319f15ed8b4SJens Axboe {
320f15ed8b4SJens Axboe struct io_ring_ctx *ctx = file->private_data;
321f15ed8b4SJens Axboe size_t sz = vma->vm_end - vma->vm_start;
322f15ed8b4SJens Axboe long offset = vma->vm_pgoff << PAGE_SHIFT;
3237cd7b957SPavel Begunkov unsigned int page_limit = UINT_MAX;
3247cd7b957SPavel Begunkov struct io_mapped_region *region;
325f15ed8b4SJens Axboe void *ptr;
326f15ed8b4SJens Axboe
327943d0609SPavel Begunkov guard(mutex)(&ctx->mmap_lock);
32879cfe9e5SJens Axboe
329f15ed8b4SJens Axboe ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
330f15ed8b4SJens Axboe if (IS_ERR(ptr))
331f15ed8b4SJens Axboe return PTR_ERR(ptr);
332f15ed8b4SJens Axboe
333f15ed8b4SJens Axboe switch (offset & IORING_OFF_MMAP_MASK) {
334f15ed8b4SJens Axboe case IORING_OFF_SQ_RING:
335f15ed8b4SJens Axboe case IORING_OFF_CQ_RING:
33681a4058eSPavel Begunkov page_limit = (sz + PAGE_SIZE - 1) >> PAGE_SHIFT;
3377cd7b957SPavel Begunkov break;
338f15ed8b4SJens Axboe }
339f15ed8b4SJens Axboe
3407cd7b957SPavel Begunkov region = io_mmap_get_region(ctx, vma->vm_pgoff);
3417cd7b957SPavel Begunkov return io_region_mmap(ctx, region, vma, page_limit);
342f15ed8b4SJens Axboe }
343f15ed8b4SJens Axboe
io_uring_get_unmapped_area(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)344f15ed8b4SJens Axboe unsigned long io_uring_get_unmapped_area(struct file *filp, unsigned long addr,
345f15ed8b4SJens Axboe unsigned long len, unsigned long pgoff,
346f15ed8b4SJens Axboe unsigned long flags)
347f15ed8b4SJens Axboe {
34879cfe9e5SJens Axboe struct io_ring_ctx *ctx = filp->private_data;
349f15ed8b4SJens Axboe void *ptr;
350f15ed8b4SJens Axboe
351f15ed8b4SJens Axboe /*
352f15ed8b4SJens Axboe * Do not allow to map to user-provided address to avoid breaking the
353f15ed8b4SJens Axboe * aliasing rules. Userspace is not able to guess the offset address of
354f15ed8b4SJens Axboe * kernel kmalloc()ed memory area.
355f15ed8b4SJens Axboe */
356f15ed8b4SJens Axboe if (addr)
357f15ed8b4SJens Axboe return -EINVAL;
358f15ed8b4SJens Axboe
359943d0609SPavel Begunkov guard(mutex)(&ctx->mmap_lock);
36079cfe9e5SJens Axboe
361f15ed8b4SJens Axboe ptr = io_uring_validate_mmap_request(filp, pgoff, len);
362f15ed8b4SJens Axboe if (IS_ERR(ptr))
363f15ed8b4SJens Axboe return -ENOMEM;
364f15ed8b4SJens Axboe
365f15ed8b4SJens Axboe /*
366f15ed8b4SJens Axboe * Some architectures have strong cache aliasing requirements.
367f15ed8b4SJens Axboe * For such architectures we need a coherent mapping which aliases
368f15ed8b4SJens Axboe * kernel memory *and* userspace memory. To achieve that:
369f15ed8b4SJens Axboe * - use a NULL file pointer to reference physical memory, and
370f15ed8b4SJens Axboe * - use the kernel virtual address of the shared io_uring context
371f15ed8b4SJens Axboe * (instead of the userspace-provided address, which has to be 0UL
372f15ed8b4SJens Axboe * anyway).
373f15ed8b4SJens Axboe * - use the same pgoff which the get_unmapped_area() uses to
374f15ed8b4SJens Axboe * calculate the page colouring.
375f15ed8b4SJens Axboe * For architectures without such aliasing requirements, the
376f15ed8b4SJens Axboe * architecture will return any suitable mapping because addr is 0.
377f15ed8b4SJens Axboe */
378f15ed8b4SJens Axboe filp = NULL;
379f15ed8b4SJens Axboe flags |= MAP_SHARED;
380f15ed8b4SJens Axboe pgoff = 0; /* has been translated to ptr above */
381f15ed8b4SJens Axboe #ifdef SHM_COLOUR
382f15ed8b4SJens Axboe addr = (uintptr_t) ptr;
383f15ed8b4SJens Axboe pgoff = addr >> PAGE_SHIFT;
384f15ed8b4SJens Axboe #else
385f15ed8b4SJens Axboe addr = 0UL;
386f15ed8b4SJens Axboe #endif
38761307b7bSLinus Torvalds return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags);
388f15ed8b4SJens Axboe }
389f15ed8b4SJens Axboe
390f15ed8b4SJens Axboe #else /* !CONFIG_MMU */
391f15ed8b4SJens Axboe
io_uring_mmap(struct file * file,struct vm_area_struct * vma)392f15ed8b4SJens Axboe int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
393f15ed8b4SJens Axboe {
394f15ed8b4SJens Axboe return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -EINVAL;
395f15ed8b4SJens Axboe }
396f15ed8b4SJens Axboe
io_uring_nommu_mmap_capabilities(struct file * file)397f15ed8b4SJens Axboe unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
398f15ed8b4SJens Axboe {
399f15ed8b4SJens Axboe return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
400f15ed8b4SJens Axboe }
401f15ed8b4SJens Axboe
io_uring_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)402f15ed8b4SJens Axboe unsigned long io_uring_get_unmapped_area(struct file *file, unsigned long addr,
403f15ed8b4SJens Axboe unsigned long len, unsigned long pgoff,
404f15ed8b4SJens Axboe unsigned long flags)
405f15ed8b4SJens Axboe {
40679cfe9e5SJens Axboe struct io_ring_ctx *ctx = file->private_data;
407f15ed8b4SJens Axboe void *ptr;
408f15ed8b4SJens Axboe
409943d0609SPavel Begunkov guard(mutex)(&ctx->mmap_lock);
41079cfe9e5SJens Axboe
411f15ed8b4SJens Axboe ptr = io_uring_validate_mmap_request(file, pgoff, len);
412f15ed8b4SJens Axboe if (IS_ERR(ptr))
413f15ed8b4SJens Axboe return PTR_ERR(ptr);
414f15ed8b4SJens Axboe
415f15ed8b4SJens Axboe return (unsigned long) ptr;
416f15ed8b4SJens Axboe }
417f15ed8b4SJens Axboe
418f15ed8b4SJens Axboe #endif /* !CONFIG_MMU */
419