1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/errno.h>
6 #include <linux/mman.h>
7 #include <linux/slab.h>
8 #include <linux/vmalloc.h>
9 #include <linux/io_uring.h>
10 #include <linux/io_uring_types.h>
11 #include <asm/shmparam.h>
17 static void *io_mem_alloc_compound(struct page **pages, int nr_pages,
18 size_t size, gfp_t gfp)
23 order = get_order(size);
24 if (order > MAX_PAGE_ORDER)
25 return ERR_PTR(-ENOMEM);
29 page = alloc_pages(gfp, order);
31 return ERR_PTR(-ENOMEM);
33 for (i = 0; i < nr_pages; i++)
36 return page_address(page);
39 static void *io_mem_alloc_single(struct page **pages, int nr_pages, size_t size,
45 for (i = 0; i < nr_pages; i++) {
46 pages[i] = alloc_page(gfp);
51 ret = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
57 return ERR_PTR(-ENOMEM);
60 void *io_pages_map(struct page ***out_pages, unsigned short *npages,
63 gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN;
68 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
69 pages = kvmalloc_array(nr_pages, sizeof(struct page *), gfp);
71 return ERR_PTR(-ENOMEM);
73 ret = io_mem_alloc_compound(pages, nr_pages, size, gfp);
77 ret = io_mem_alloc_single(pages, nr_pages, size, gfp);
91 void io_pages_unmap(void *ptr, struct page ***pages, unsigned short *npages,
94 bool do_vunmap = false;
99 if (put_pages && *npages) {
100 struct page **to_free = *pages;
104 * Only did vmap for the non-compound multiple page case.
105 * For the compound page, we just need to put the head.
107 if (PageCompound(to_free[0]))
109 else if (*npages > 1)
111 for (i = 0; i < *npages; i++)
112 put_page(to_free[i]);
121 void io_pages_free(struct page ***pages, int npages)
123 struct page **page_array = *pages;
128 unpin_user_pages(page_array, npages);
133 struct page **io_pin_pages(unsigned long uaddr, unsigned long len, int *npages)
135 unsigned long start, end, nr_pages;
139 end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
140 start = uaddr >> PAGE_SHIFT;
141 nr_pages = end - start;
142 if (WARN_ON_ONCE(!nr_pages))
143 return ERR_PTR(-EINVAL);
144 if (WARN_ON_ONCE(nr_pages > INT_MAX))
145 return ERR_PTR(-EOVERFLOW);
147 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
149 return ERR_PTR(-ENOMEM);
151 ret = pin_user_pages_fast(uaddr, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
153 /* success, mapped all pages */
154 if (ret == nr_pages) {
159 /* partial map, or didn't map anything */
161 /* if we did partial map, release any pages we did get */
163 unpin_user_pages(pages, ret);
170 void *__io_uaddr_map(struct page ***pages, unsigned short *npages,
171 unsigned long uaddr, size_t size)
173 struct page **page_array;
174 unsigned int nr_pages;
179 if (uaddr & (PAGE_SIZE - 1) || !size)
180 return ERR_PTR(-EINVAL);
183 page_array = io_pin_pages(uaddr, size, &nr_pages);
184 if (IS_ERR(page_array))
187 page_addr = vmap(page_array, nr_pages, VM_MAP, PAGE_KERNEL);
194 io_pages_free(&page_array, nr_pages);
195 return ERR_PTR(-ENOMEM);
198 void io_free_region(struct io_ring_ctx *ctx, struct io_mapped_region *mr)
201 unpin_user_pages(mr->pages, mr->nr_pages);
205 vunmap(mr->vmap_ptr);
206 if (mr->nr_pages && ctx->user)
207 __io_unaccount_mem(ctx->user, mr->nr_pages);
209 memset(mr, 0, sizeof(*mr));
212 int io_create_region(struct io_ring_ctx *ctx, struct io_mapped_region *mr,
213 struct io_uring_region_desc *reg)
215 int pages_accounted = 0;
221 if (WARN_ON_ONCE(mr->pages || mr->vmap_ptr || mr->nr_pages))
223 if (memchr_inv(®->__resv, 0, sizeof(reg->__resv)))
225 if (reg->flags != IORING_MEM_REGION_TYPE_USER)
229 if (!reg->size || reg->mmap_offset || reg->id)
231 if ((reg->size >> PAGE_SHIFT) > INT_MAX)
233 if ((reg->user_addr | reg->size) & ~PAGE_MASK)
235 if (check_add_overflow(reg->user_addr, reg->size, &end))
238 pages = io_pin_pages(reg->user_addr, reg->size, &nr_pages);
240 return PTR_ERR(pages);
243 ret = __io_account_mem(ctx->user, nr_pages);
246 pages_accounted = nr_pages;
249 vptr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
255 mr->nr_pages = nr_pages;
259 __io_unaccount_mem(ctx->user, pages_accounted);
260 io_pages_free(&pages, nr_pages);
264 static void *io_uring_validate_mmap_request(struct file *file, loff_t pgoff,
267 struct io_ring_ctx *ctx = file->private_data;
268 loff_t offset = pgoff << PAGE_SHIFT;
270 switch ((pgoff << PAGE_SHIFT) & IORING_OFF_MMAP_MASK) {
271 case IORING_OFF_SQ_RING:
272 case IORING_OFF_CQ_RING:
273 /* Don't allow mmap if the ring was setup without it */
274 if (ctx->flags & IORING_SETUP_NO_MMAP)
275 return ERR_PTR(-EINVAL);
277 return ERR_PTR(-EFAULT);
279 case IORING_OFF_SQES:
280 /* Don't allow mmap if the ring was setup without it */
281 if (ctx->flags & IORING_SETUP_NO_MMAP)
282 return ERR_PTR(-EINVAL);
284 return ERR_PTR(-EFAULT);
286 case IORING_OFF_PBUF_RING: {
287 struct io_buffer_list *bl;
291 bgid = (offset & ~IORING_OFF_MMAP_MASK) >> IORING_OFF_PBUF_SHIFT;
292 bl = io_pbuf_get_bl(ctx, bgid);
301 return ERR_PTR(-EINVAL);
304 int io_uring_mmap_pages(struct io_ring_ctx *ctx, struct vm_area_struct *vma,
305 struct page **pages, int npages)
307 unsigned long nr_pages = npages;
309 vm_flags_set(vma, VM_DONTEXPAND);
310 return vm_insert_pages(vma, vma->vm_start, pages, &nr_pages);
315 __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
317 struct io_ring_ctx *ctx = file->private_data;
318 size_t sz = vma->vm_end - vma->vm_start;
319 long offset = vma->vm_pgoff << PAGE_SHIFT;
323 guard(mutex)(&ctx->resize_lock);
325 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
329 switch (offset & IORING_OFF_MMAP_MASK) {
330 case IORING_OFF_SQ_RING:
331 case IORING_OFF_CQ_RING:
332 npages = min(ctx->n_ring_pages, (sz + PAGE_SIZE - 1) >> PAGE_SHIFT);
333 return io_uring_mmap_pages(ctx, vma, ctx->ring_pages, npages);
334 case IORING_OFF_SQES:
335 return io_uring_mmap_pages(ctx, vma, ctx->sqe_pages,
337 case IORING_OFF_PBUF_RING:
338 return io_pbuf_mmap(file, vma);
344 unsigned long io_uring_get_unmapped_area(struct file *filp, unsigned long addr,
345 unsigned long len, unsigned long pgoff,
348 struct io_ring_ctx *ctx = filp->private_data;
352 * Do not allow to map to user-provided address to avoid breaking the
353 * aliasing rules. Userspace is not able to guess the offset address of
354 * kernel kmalloc()ed memory area.
359 guard(mutex)(&ctx->resize_lock);
361 ptr = io_uring_validate_mmap_request(filp, pgoff, len);
366 * Some architectures have strong cache aliasing requirements.
367 * For such architectures we need a coherent mapping which aliases
368 * kernel memory *and* userspace memory. To achieve that:
369 * - use a NULL file pointer to reference physical memory, and
370 * - use the kernel virtual address of the shared io_uring context
371 * (instead of the userspace-provided address, which has to be 0UL
373 * - use the same pgoff which the get_unmapped_area() uses to
374 * calculate the page colouring.
375 * For architectures without such aliasing requirements, the
376 * architecture will return any suitable mapping because addr is 0.
380 pgoff = 0; /* has been translated to ptr above */
382 addr = (uintptr_t) ptr;
383 pgoff = addr >> PAGE_SHIFT;
387 return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags);
390 #else /* !CONFIG_MMU */
392 int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
394 return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -EINVAL;
397 unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
399 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
402 unsigned long io_uring_get_unmapped_area(struct file *file, unsigned long addr,
403 unsigned long len, unsigned long pgoff,
406 struct io_ring_ctx *ctx = file->private_data;
409 guard(mutex)(&ctx->resize_lock);
411 ptr = io_uring_validate_mmap_request(file, pgoff, len);
415 return (unsigned long) ptr;
418 #endif /* !CONFIG_MMU */