Skip to content

Commit 7cd7b95

Browse files
isilenceaxboe
authored andcommitted
io_uring/memmap: unify io_uring mmap'ing code
All mapped memory is now backed by regions and we can unify and clean up io_region_validate_mmap() and io_uring_mmap(). Extract a function looking up a region, the rest of the handling should be generic and just needs the region. There is one more ring type specific code, i.e. the mmaping size truncation quirk for IORING_OFF_[S,C]Q_RING, which is left as is. Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/f5e1eda1562bfd34276de07465525ae5f10e1e84.1732886067.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent ef62de3 commit 7cd7b95

File tree

2 files changed

+31
-53
lines changed

2 files changed

+31
-53
lines changed

io_uring/kbuf.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,5 @@ struct io_mapped_region *io_pbuf_get_region(struct io_ring_ctx *ctx,
748748
bl = xa_load(&ctx->io_bl_xa, bgid);
749749
if (!bl || !(bl->flags & IOBL_BUF_RING))
750750
return NULL;
751-
if (WARN_ON_ONCE(!io_region_is_set(&bl->region)))
752-
return NULL;
753-
754751
return &bl->region;
755752
}

io_uring/memmap.c

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,27 @@ int io_create_region_mmap_safe(struct io_ring_ctx *ctx, struct io_mapped_region
254254
return 0;
255255
}
256256

257+
static struct io_mapped_region *io_mmap_get_region(struct io_ring_ctx *ctx,
258+
loff_t pgoff)
259+
{
260+
loff_t offset = pgoff << PAGE_SHIFT;
261+
unsigned int bgid;
262+
263+
switch (offset & IORING_OFF_MMAP_MASK) {
264+
case IORING_OFF_SQ_RING:
265+
case IORING_OFF_CQ_RING:
266+
return &ctx->ring_region;
267+
case IORING_OFF_SQES:
268+
return &ctx->sq_region;
269+
case IORING_OFF_PBUF_RING:
270+
bgid = (offset & ~IORING_OFF_MMAP_MASK) >> IORING_OFF_PBUF_SHIFT;
271+
return io_pbuf_get_region(ctx, bgid);
272+
case IORING_MAP_OFF_PARAM_REGION:
273+
return &ctx->param_region;
274+
}
275+
return NULL;
276+
}
277+
257278
static void *io_region_validate_mmap(struct io_ring_ctx *ctx,
258279
struct io_mapped_region *mr)
259280
{
@@ -271,39 +292,12 @@ static void *io_uring_validate_mmap_request(struct file *file, loff_t pgoff,
271292
size_t sz)
272293
{
273294
struct io_ring_ctx *ctx = file->private_data;
274-
loff_t offset = pgoff << PAGE_SHIFT;
295+
struct io_mapped_region *region;
275296

276-
switch ((pgoff << PAGE_SHIFT) & IORING_OFF_MMAP_MASK) {
277-
case IORING_OFF_SQ_RING:
278-
case IORING_OFF_CQ_RING:
279-
/* Don't allow mmap if the ring was setup without it */
280-
if (ctx->flags & IORING_SETUP_NO_MMAP)
281-
return ERR_PTR(-EINVAL);
282-
if (!ctx->rings)
283-
return ERR_PTR(-EFAULT);
284-
return ctx->rings;
285-
case IORING_OFF_SQES:
286-
/* Don't allow mmap if the ring was setup without it */
287-
if (ctx->flags & IORING_SETUP_NO_MMAP)
288-
return ERR_PTR(-EINVAL);
289-
if (!ctx->sq_sqes)
290-
return ERR_PTR(-EFAULT);
291-
return ctx->sq_sqes;
292-
case IORING_OFF_PBUF_RING: {
293-
struct io_mapped_region *region;
294-
unsigned int bgid;
295-
296-
bgid = (offset & ~IORING_OFF_MMAP_MASK) >> IORING_OFF_PBUF_SHIFT;
297-
region = io_pbuf_get_region(ctx, bgid);
298-
if (!region)
299-
return ERR_PTR(-EINVAL);
300-
return io_region_validate_mmap(ctx, region);
301-
}
302-
case IORING_MAP_OFF_PARAM_REGION:
303-
return io_region_validate_mmap(ctx, &ctx->param_region);
304-
}
305-
306-
return ERR_PTR(-EINVAL);
297+
region = io_mmap_get_region(ctx, pgoff);
298+
if (!region)
299+
return ERR_PTR(-EINVAL);
300+
return io_region_validate_mmap(ctx, region);
307301
}
308302

309303
#ifdef CONFIG_MMU
@@ -324,7 +318,8 @@ __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
324318
struct io_ring_ctx *ctx = file->private_data;
325319
size_t sz = vma->vm_end - vma->vm_start;
326320
long offset = vma->vm_pgoff << PAGE_SHIFT;
327-
unsigned int page_limit;
321+
unsigned int page_limit = UINT_MAX;
322+
struct io_mapped_region *region;
328323
void *ptr;
329324

330325
guard(mutex)(&ctx->mmap_lock);
@@ -337,25 +332,11 @@ __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
337332
case IORING_OFF_SQ_RING:
338333
case IORING_OFF_CQ_RING:
339334
page_limit = (sz + PAGE_SIZE - 1) >> PAGE_SHIFT;
340-
return io_region_mmap(ctx, &ctx->ring_region, vma, page_limit);
341-
case IORING_OFF_SQES:
342-
return io_region_mmap(ctx, &ctx->sq_region, vma, UINT_MAX);
343-
case IORING_OFF_PBUF_RING: {
344-
struct io_mapped_region *region;
345-
unsigned int bgid;
346-
347-
bgid = (offset & ~IORING_OFF_MMAP_MASK) >> IORING_OFF_PBUF_SHIFT;
348-
region = io_pbuf_get_region(ctx, bgid);
349-
if (!region)
350-
return -EINVAL;
351-
352-
return io_region_mmap(ctx, region, vma, UINT_MAX);
353-
}
354-
case IORING_MAP_OFF_PARAM_REGION:
355-
return io_region_mmap(ctx, &ctx->param_region, vma, UINT_MAX);
335+
break;
356336
}
357337

358-
return -EINVAL;
338+
region = io_mmap_get_region(ctx, vma->vm_pgoff);
339+
return io_region_mmap(ctx, region, vma, page_limit);
359340
}
360341

361342
unsigned long io_uring_get_unmapped_area(struct file *filp, unsigned long addr,

0 commit comments

Comments
 (0)