From patchwork Wed Mar 27 19:13:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13607313 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B95B1150999 for ; Wed, 27 Mar 2024 19:19:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567182; cv=none; b=kC5V5Ve/gdAgijkWli8jlXwLhLFOvMiaM7M87aKNng+gnU1xOjzgn4czy/N+v5pjVYlOX8W+nHLYy8yMo8b8hMMMCBs7EFHbytpzNSyKdGb4UVqhi+Gs112Wuu2YRRIOkUZRy+jcPzZgUTyURms/ep/oFoC77y54kqGz4SsgMps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567182; c=relaxed/simple; bh=IA363V3fu31t0PPCUsgoiiOfsNv8iW0Wnt/MjZXXrdg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D/pB3/Q6IZfKVpm7usEQ77xvh8OOBWrRPBb1S60bJaC4pu8dddoGXpfjEr3suspvpJPvkf5UIL+ko/WSVEmCC/5JAFPrwn+nv+GU/qGLGm+AHvrpq/qL4pp+lKGBgPNB1L5Ne1TFCXDlAvl8Uyd3R/dcYozv0OAIp6oIPN6K7Hk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=MGQAUAg8; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="MGQAUAg8" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6e6c38be762so41801b3a.1 for ; Wed, 27 Mar 2024 12:19:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1711567178; x=1712171978; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wQ3pMD8RptEK8J/o1yry4PoQVSvIoeVGC8WVz8qkQnE=; b=MGQAUAg8JBiuAndO1P/LkBeEsL+kBWIM5rKFLkh8WKIrfaiZ4pYwNz0FDtmqtTbBbN r9XtBJgJyRPVCvspvAEmaloOHoUZiZi7TkDfB+HC/3dCpSMLv8418wxZif0+EF1XgMyP gtfvNpIzIHtlMvF8sAXP5A5dJr40by4G98LMpXJ/PlMhTtW/GuivS5Noh1YURkccHwHb tThfeiUh++7XVFUAG1yqr6HYfGAOOsB4eOCv/yjExVaXDjQIB0P7oUqz8un/SF/ZQ/+a Ra/jVmJ+T2kcK7X1kO3B0UPjyA0esSodTfNh1cKz8UoYlVh/1BvkYFvjKofdpsv0jhhZ qwUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567178; x=1712171978; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wQ3pMD8RptEK8J/o1yry4PoQVSvIoeVGC8WVz8qkQnE=; b=gPgDC9ID5s5/hfHNEqwa7jja2rNhVeXWYn/07zWGmLNEXV00MEwOPLJ5Ypi099y61g Z6K6CtGJ67bq+7pPzF53a1ZpPe2EZr2lkctFtQZm//aus1JTffRLloK+7N0hwsX5xFSp rI/fZ3YQ653cmvnsoRoBegjz3dEvdZ8lh9iBc6D318qNRnRXpkVLTJJXfN2E/19qtfvC rK8uKd0p1dsaTW/LVK31DrcntU9CsLGklsZSNALpTpzCMIBVlpiy6CVzmxAKpoCjKpVK lrGgtRK6tNGLQ0J5l2IthtID0jnY31aSeKemcRaELvyaMip2E9uOhnHAWhjhu9EbXV/o /CKg== X-Gm-Message-State: AOJu0YxNhztb4+YiVYrN6FBAlXOsu+QazHpPaDK+qfeUKgLhFbDE8M/q ppb79oSRSGG6OCU0KWIvE0gDH6VBdXh+bHOrfhQNNDbYlpSVpnFUo2gzdUO4Qky2ui5GHnR5GL+ w X-Google-Smtp-Source: AGHT+IGDi776sKRTAaThr1/xhwh1sZyR+nsGH01NhnQYNzLClxWWhJ//PUYFuHG/OhE6yF4O4gzraA== X-Received: by 2002:a05:6a00:39a3:b0:6e9:ca7b:c150 with SMTP id fi35-20020a056a0039a300b006e9ca7bc150mr830072pfb.3.1711567178023; Wed, 27 Mar 2024 12:19:38 -0700 (PDT) Received: from m2max.thefacebook.com ([2620:10d:c090:600::1:bb1e]) by smtp.gmail.com with ESMTPSA id n2-20020aa79842000000b006e6c3753786sm8278882pfq.41.2024.03.27.12.19.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:19:37 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 01/10] mm: add nommu variant of vm_insert_pages() Date: Wed, 27 Mar 2024 13:13:36 -0600 Message-ID: <20240327191933.607220-2-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240327191933.607220-1-axboe@kernel.dk> References: <20240327191933.607220-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 An identical one exists for vm_insert_page(), add one for vm_insert_pages() to avoid needing to check for CONFIG_MMU in code using it. Signed-off-by: Jens Axboe Acked-by: Johannes Weiner --- mm/nommu.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mm/nommu.c b/mm/nommu.c index 5ec8f44e7ce9..a34a0e376611 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -355,6 +355,13 @@ int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, } EXPORT_SYMBOL(vm_insert_page); +int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, + struct page **pages, unsigned long *num) +{ + return -EINVAL; +} +EXPORT_SYMBOL(vm_insert_pages); + int vm_map_pages(struct vm_area_struct *vma, struct page **pages, unsigned long num) { From patchwork Wed Mar 27 19:13:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13607314 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A56412E1F6 for ; Wed, 27 Mar 2024 19:19:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567182; cv=none; b=NCRIOWfbgZxS2fN4o44sAadXJnZ3zlmYt3xjCWHeQI+cyPQr2YPq282eI89JtvEbeZZ0T2HQoaCIMgLOKai3R9+gkrBuFZeu+itQ9O4p5TAmwYLknwzuyksOQOBd5PZDw/M5FGp0WyFVxck3L3I4CIgpC/R5CA/tsOhDcmaWH78= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567182; c=relaxed/simple; bh=i0gg2I8Wvze9K+4XovDNf37+pVA0JTmeTXyZpCy7Rc4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j7UxYptz0jgZ7uR0iPPcVnb6jDbYZQwDstRdMaw4EZSdFfJWJLpsjh7ZIeoOD0qYDRlT0KubfouyA8iyLcrjgUL/2YyDm6DIHUny754AOXd/B5st2rGr8SUaL5PhAvSM3QJ9aaKPR59uMVxAwN7kI0y9YnaVKnndsr+YaJGam+c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=VfbwFr7b; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="VfbwFr7b" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6e6c38be762so41806b3a.1 for ; Wed, 27 Mar 2024 12:19:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1711567179; x=1712171979; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uuOe3XlWJv29sqx0Tp2SbJ3PRazdewWUbq1djwJUsNw=; b=VfbwFr7bzucR7XjdEAyVE3lCYVmdkbwimKfZWdbIaE2VpkpOyyc6HW2YJlom/6zfT7 N0Hv7NXNMhYVGc3uVM/ZrBcZ+3x9UTRBJ4AZ6lNI3cBmRBevSljM1O/7fSCj0lkNlL0Q 9f48GXYfaM+S7cUe7noe8rlTnd8Y5sdo6TPWV8m/+yqgSOf9tpOqq34DERZE0ZhJrsVw +Zk2bRm8dD55VQ3eKgjgcJ2N9hB887UBvnpGgSURiU0k0zK0RCekRycvjTk+/u1drJnY JLznPaeZsexJKHFIvxyGmVrnp+0tO+qqeHqIy2Oi7gQpMWJTRrHGP5ib6LILeKftSwt5 klRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567179; x=1712171979; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uuOe3XlWJv29sqx0Tp2SbJ3PRazdewWUbq1djwJUsNw=; b=wjUh4jrGCMNLIEtvjreLseXtohkhzgxdQIS4ajocveE2dqk1qfkVsfmxsT0IY2jWLp i9t8b5+jCUyYVc+k9uY02FqC4R5rqmkUGpHtLfuT+Rnk3n0hCmnxTr6A3v1REv/YMNeb ZVCkpUQ/Jgu+QqssYReS2srfIgVTrIwSfOldeBXPNIbNKBNFEhqvZTbX/QmiZKu9IZ8U ogazsy3oeAHzQaxcVo8Vp9q8ZVf/DfWU/o2l2VhsTDaLMOWCtV9Txyez0JifysCiFnXN aKx+Cz7XHvTQhGBkhQy/z3cSRlrPpSrxozeGCMBmG5eXhxWD7G3vICNSdAd6mb77NPUH Jvag== X-Gm-Message-State: AOJu0YwW8cfy7MGPzhc+zfm/NvePYSLIwAcDED1SYw6KGSVy1H3CCiCW qJM3HSSiilwbzSwwN734P6VEBD8/xTy+Hbrir63v3/uZJl4qppuPasKYOmNi/2GShJrW63x+YKO X X-Google-Smtp-Source: AGHT+IE3yfaYbrwpWzllfExSPt6tkMj/aItR55D/Qqo/XaZDjoygbDahSWMzk2J7Xwf8355nOOvTpw== X-Received: by 2002:a05:6a20:3ca1:b0:1a3:b0a8:fbe9 with SMTP id b33-20020a056a203ca100b001a3b0a8fbe9mr1034935pzj.1.1711567179057; Wed, 27 Mar 2024 12:19:39 -0700 (PDT) Received: from m2max.thefacebook.com ([2620:10d:c090:600::1:bb1e]) by smtp.gmail.com with ESMTPSA id n2-20020aa79842000000b006e6c3753786sm8278882pfq.41.2024.03.27.12.19.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:19:38 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 02/10] io_uring: get rid of remap_pfn_range() for mapping rings/sqes Date: Wed, 27 Mar 2024 13:13:37 -0600 Message-ID: <20240327191933.607220-3-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240327191933.607220-1-axboe@kernel.dk> References: <20240327191933.607220-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Rather than use remap_pfn_range() for this and manually free later, switch to using vm_insert_pages() and have it Just Work. If possible, allocate a single compound page that covers the range that is needed. If that works, then we can just use page_address() on that page. If we fail to get a compound page, allocate single pages and use vmap() to map them into the kernel virtual address space. This just covers the rings/sqes, the other remaining user of the mmap remap_pfn_range() user will be converted separately. Once that is done, we can kill the old alloc/free code. Signed-off-by: Jens Axboe --- io_uring/io_uring.c | 134 +++++++++++++++++++++++++++++++++++++++++--- io_uring/io_uring.h | 2 + 2 files changed, 128 insertions(+), 8 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 585fbc363eaf..29d0c1764aab 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2601,6 +2601,27 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; } +static void io_pages_unmap(void *ptr, struct page ***pages, + unsigned short *npages) +{ + bool do_vunmap = false; + + if (*npages) { + struct page **to_free = *pages; + int i; + + /* only did vmap for non-compound and multiple pages */ + do_vunmap = !PageCompound(to_free[0]) && *npages > 1; + for (i = 0; i < *npages; i++) + put_page(to_free[i]); + } + if (do_vunmap) + vunmap(ptr); + kvfree(*pages); + *pages = NULL; + *npages = 0; +} + void io_mem_free(void *ptr) { if (!ptr) @@ -2701,8 +2722,8 @@ static void *io_sqes_map(struct io_ring_ctx *ctx, unsigned long uaddr, static void io_rings_free(struct io_ring_ctx *ctx) { if (!(ctx->flags & IORING_SETUP_NO_MMAP)) { - io_mem_free(ctx->rings); - io_mem_free(ctx->sq_sqes); + io_pages_unmap(ctx->rings, &ctx->ring_pages, &ctx->n_ring_pages); + io_pages_unmap(ctx->sq_sqes, &ctx->sqe_pages, &ctx->n_sqe_pages); } else { io_pages_free(&ctx->ring_pages, ctx->n_ring_pages); ctx->n_ring_pages = 0; @@ -2714,6 +2735,84 @@ static void io_rings_free(struct io_ring_ctx *ctx) ctx->sq_sqes = NULL; } +static void *io_mem_alloc_compound(struct page **pages, int nr_pages, + size_t size, gfp_t gfp) +{ + struct page *page; + int i, order; + + order = get_order(size); + if (order > MAX_PAGE_ORDER) + return NULL; + else if (order) + gfp |= __GFP_COMP; + + page = alloc_pages(gfp, order); + if (!page) + return NULL; + + /* add pages, grab a ref to tail pages */ + for (i = 0; i < nr_pages; i++) { + pages[i] = page + i; + if (i) + get_page(pages[i]); + } + + return page_address(page); +} + +static void *io_mem_alloc_single(struct page **pages, int nr_pages, size_t size, + gfp_t gfp) +{ + void *ret; + int i; + + for (i = 0; i < nr_pages; i++) { + pages[i] = alloc_page(gfp); + if (!pages[i]) + goto err; + } + + ret = vmap(pages, nr_pages, VM_MAP | VM_ALLOW_HUGE_VMAP, PAGE_KERNEL); + if (ret) + return ret; +err: + while (i--) + put_page(pages[i]); + return ERR_PTR(-ENOMEM); +} + +static void *io_pages_map(struct page ***out_pages, unsigned short *npages, + size_t size) +{ + gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN; + struct page **pages; + int nr_pages; + void *ret; + + nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; + pages = kvmalloc_array(nr_pages, sizeof(struct page *), gfp); + if (!pages) + return ERR_PTR(-ENOMEM); + + ret = io_mem_alloc_compound(pages, nr_pages, size, gfp); + if (ret) + goto done; + + ret = io_mem_alloc_single(pages, nr_pages, size, gfp); + if (ret) { +done: + *out_pages = pages; + *npages = nr_pages; + return ret; + } + + kvfree(pages); + *out_pages = NULL; + *npages = 0; + return ERR_PTR(-ENOMEM); +} + void *io_mem_alloc(size_t size) { gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; @@ -3301,14 +3400,12 @@ static void *io_uring_validate_mmap_request(struct file *file, /* Don't allow mmap if the ring was setup without it */ if (ctx->flags & IORING_SETUP_NO_MMAP) return ERR_PTR(-EINVAL); - ptr = ctx->rings; - break; + return ctx->rings; case IORING_OFF_SQES: /* Don't allow mmap if the ring was setup without it */ if (ctx->flags & IORING_SETUP_NO_MMAP) return ERR_PTR(-EINVAL); - ptr = ctx->sq_sqes; - break; + return ctx->sq_sqes; case IORING_OFF_PBUF_RING: { unsigned int bgid; @@ -3331,11 +3428,22 @@ static void *io_uring_validate_mmap_request(struct file *file, return ptr; } +int io_uring_mmap_pages(struct io_ring_ctx *ctx, struct vm_area_struct *vma, + struct page **pages, int npages) +{ + unsigned long nr_pages = npages; + + vm_flags_set(vma, VM_DONTEXPAND); + return vm_insert_pages(vma, vma->vm_start, pages, &nr_pages); +} + #ifdef CONFIG_MMU static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) { + struct io_ring_ctx *ctx = file->private_data; size_t sz = vma->vm_end - vma->vm_start; + long offset = vma->vm_pgoff << PAGE_SHIFT; unsigned long pfn; void *ptr; @@ -3343,6 +3451,16 @@ static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) if (IS_ERR(ptr)) return PTR_ERR(ptr); + switch (offset & IORING_OFF_MMAP_MASK) { + case IORING_OFF_SQ_RING: + case IORING_OFF_CQ_RING: + return io_uring_mmap_pages(ctx, vma, ctx->ring_pages, + ctx->n_ring_pages); + case IORING_OFF_SQES: + return io_uring_mmap_pages(ctx, vma, ctx->sqe_pages, + ctx->n_sqe_pages); + } + pfn = virt_to_phys(ptr) >> PAGE_SHIFT; return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); } @@ -3632,7 +3750,7 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, return -EOVERFLOW; if (!(ctx->flags & IORING_SETUP_NO_MMAP)) - rings = io_mem_alloc(size); + rings = io_pages_map(&ctx->ring_pages, &ctx->n_ring_pages, size); else rings = io_rings_map(ctx, p->cq_off.user_addr, size); @@ -3657,7 +3775,7 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, } if (!(ctx->flags & IORING_SETUP_NO_MMAP)) - ptr = io_mem_alloc(size); + ptr = io_pages_map(&ctx->sqe_pages, &ctx->n_sqe_pages, size); else ptr = io_sqes_map(ctx, p->sq_off.user_addr, size); diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h index 7654dfb34c2e..ac2a84542417 100644 --- a/io_uring/io_uring.h +++ b/io_uring/io_uring.h @@ -70,6 +70,8 @@ bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags); void __io_commit_cqring_flush(struct io_ring_ctx *ctx); struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages); +int io_uring_mmap_pages(struct io_ring_ctx *ctx, struct vm_area_struct *vma, + struct page **pages, int npages); struct file *io_file_get_normal(struct io_kiocb *req, int fd); struct file *io_file_get_fixed(struct io_kiocb *req, int fd, From patchwork Wed Mar 27 19:13:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13607315 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BCB014D280 for ; Wed, 27 Mar 2024 19:19:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567184; cv=none; b=EPy2wjc+a/7yXwSiqH+yMpK8mwu98Euy8BFtEvKbYzCbcX4H6jWITbbjUUH1iAKAnikTI2O8f2D1mcIe1Rr1qRSQp2LtdWuKCHKyQlR26fhGlpFQNxHH5iMPKizNyWHWL1BPs06xzqXCnGPIREhQD7ZUeiazsvUuBd48eh7tSIk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567184; c=relaxed/simple; bh=kvVE6febIb04jrE/F9NLWAAO/39newEvIn7hbro7q98=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L+1yJC6pi0XVwJ3HSvuBmKg9k94xP1VAwG799zvHCZIE9n0q0yZLguxD+QlryLYzLXtV+497qO8hrgWaerYmPWyB2Bj/iTnwDX+R6rsjtnY02GEdsXIEtgQvoio0Gg9PnLjrXuvGozT7Yr6NFA1GLDVC9bsTRzihVlPDnerXBUU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=QN+zwuZL; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="QN+zwuZL" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-58962bf3f89so25573a12.0 for ; Wed, 27 Mar 2024 12:19:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1711567181; x=1712171981; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SBdcP6DeDOrKct7/HMwyFlsXehHONUqtIcZ6bDvoZsc=; b=QN+zwuZLeqtOwzj4xT7/awceCemJ9PKD6G7yXn6Ms8hSsK+l6IWq7Vx+cEiSnAlXfr 4/dGht5RDDg/D4nm0HQsgiHg3LDSTBN9jciN1nOPj3KUKiaC9ddkKDUkDNNnQF/NgnlN T3jTARWUKMidwbZz8aE2C9BPrOGWSw6m0GN6pf7Wis4s0rIcqKl0+U+eq/l9vSCfduzA QnItdH3NLR2gAqOwGx4qorRMmRT4HMf/tb0E444c2USzgJg67vl9krEqPad1Ls6e2bU2 CpmBVQs68z2sPatIZFN4EXlLpcvY6Cyrw2ZAG8hEQmXOfal4UbVues0W0AP/Wi65SfCX 5hHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567181; x=1712171981; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SBdcP6DeDOrKct7/HMwyFlsXehHONUqtIcZ6bDvoZsc=; b=cPr3fsDbUzFGexRKtc0t8No5GrmENvW9CvI81o4wGuMWboWF8hV9h8eLeyb0r1cBlC kkJp0pbh7S8AUxIyw4GBmfSAOKMXHUofJSUvyyhocIVbVPMTQ3tHZCi+5dcQiCPrYTqL R72eKhB3FXE87zorNIBC1JmgIIeclHFMYDKJRCMHCslTvVKicmlAKajGbCveB/+oMkjL 9TsIQWCFJ1zM+QcUB4KwlYuElZ8B0teZUs2Y/A7BVUC/z0aUfAsXrUL4hBVaoJt16i09 dWF1e/nupg++jk1NUmcSS/FCioPcVhK7i0WTVMA8mrQgp0EYqXTOusLNe4elB2tYYEOw P5ug== X-Gm-Message-State: AOJu0Yw1Qy3FvKG0sQZNESviEvqSjyU1jLHj+EXiPFz6lyuKdJeUSw1F JGQcbxxgPZozVa3e6ehzGK7sYN/zSpKpGKVb6Dx8GlAfJ+zQqBH+9bix5g4Q/YSpE1d73OBSU+o 9 X-Google-Smtp-Source: AGHT+IEZHEyuY1mT+EZP0LQ0Rpw3LkvHb5879hfoiiKPyLDGqpEmFO6DFVwS1lNt4MXL9O66XGyLyQ== X-Received: by 2002:a05:6a21:7881:b0:1a3:66d8:d978 with SMTP id bf1-20020a056a21788100b001a366d8d978mr1089487pzc.3.1711567181083; Wed, 27 Mar 2024 12:19:41 -0700 (PDT) Received: from m2max.thefacebook.com ([2620:10d:c090:600::1:bb1e]) by smtp.gmail.com with ESMTPSA id n2-20020aa79842000000b006e6c3753786sm8278882pfq.41.2024.03.27.12.19.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:19:39 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 03/10] io_uring: use vmap() for ring mapping Date: Wed, 27 Mar 2024 13:13:38 -0600 Message-ID: <20240327191933.607220-4-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240327191933.607220-1-axboe@kernel.dk> References: <20240327191933.607220-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This is the last holdout which does odd page checking, convert it to vmap just like what is done for the non-mmap path. Signed-off-by: Jens Axboe --- io_uring/io_uring.c | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 29d0c1764aab..67c93b290ed9 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -63,7 +63,6 @@ #include #include #include -#include #include #include #include @@ -2650,7 +2649,7 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, struct page **page_array; unsigned int nr_pages; void *page_addr; - int ret, i, pinned; + int ret, pinned; *npages = 0; @@ -2659,8 +2658,6 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; if (nr_pages > USHRT_MAX) - return ERR_PTR(-EINVAL); - page_array = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); if (!page_array) return ERR_PTR(-ENOMEM); @@ -2672,34 +2669,13 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, goto free_pages; } - page_addr = page_address(page_array[0]); - for (i = 0; i < nr_pages; i++) { - ret = -EINVAL; - - /* - * Can't support mapping user allocated ring memory on 32-bit - * archs where it could potentially reside in highmem. Just - * fail those with -EINVAL, just like we did on kernels that - * didn't support this feature. - */ - if (PageHighMem(page_array[i])) - goto free_pages; - - /* - * No support for discontig pages for now, should either be a - * single normal page, or a huge page. Later on we can add - * support for remapping discontig pages, for now we will - * just fail them with EINVAL. - */ - if (page_address(page_array[i]) != page_addr) - goto free_pages; - page_addr += PAGE_SIZE; + page_addr = vmap(page_array, nr_pages, VM_MAP, PAGE_KERNEL); + if (page_addr) { + *pages = page_array; + *npages = nr_pages; + return page_addr; } - - *pages = page_array; - *npages = nr_pages; - return page_to_virt(page_array[0]); - + ret = -ENOMEM; free_pages: io_pages_free(&page_array, pinned > 0 ? pinned : 0); return ERR_PTR(ret); @@ -2729,6 +2705,8 @@ static void io_rings_free(struct io_ring_ctx *ctx) ctx->n_ring_pages = 0; io_pages_free(&ctx->sqe_pages, ctx->n_sqe_pages); ctx->n_sqe_pages = 0; + vunmap(ctx->rings); + vunmap(ctx->sq_sqes); } ctx->rings = NULL; From patchwork Wed Mar 27 19:13:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13607316 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DAC812E1F6 for ; Wed, 27 Mar 2024 19:19:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567186; cv=none; b=ZEWCCXqljJ/kZZ+XR8WvGL2EG3o4bK3rbcEBWQnuVG0ZVOZCzGxseFKoJBmYEhrthpqxtq6jGC+xgt8gtmd6uzOCQzBAa/4pGAX2UHbzZu+I2EDz2kOxnmf4Ou4U3V2jh5ASsFvexdVv/5YRYFt8y712pShQt0Z0xLEulxLOkLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567186; c=relaxed/simple; bh=10rApHkF9zSklfaPE7lnFCjhYUOdaSjtacrcM0x8rVw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pHwixjHnUL4OeWyPDeur+efW+XrNYs2caNdQITLoOQ2M7AYGY2vrfT34zCk1NqEyJM8cjyGdQSJaDVtUeRDhfKtmI5oqfKPhq3O0tIwdKHX1glzsUAgCu0G3CUKPfYRsKMRFk758Z1drA8T1Gq6/9p9739+I3WNj/iigDpEg+2I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=wygDKyR+; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="wygDKyR+" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6e694337fffso59607b3a.1 for ; Wed, 27 Mar 2024 12:19:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1711567183; x=1712171983; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8RMlGq1Gwhpk4UAA96yMefndlkDhs2bhrF9FMUSrKbo=; b=wygDKyR+tK+ko+d83xBsuh+9Hru46zpzbyEC5H1Tf7IQ2q9++DtnR2NicLdW5lNI/j XqFfKCfit+Nc8yaD1t/3NfIYH6EX/ymWbg2/f7EgZ5Cj1egNk/o+DUYAwNrCn/icWpTr g3Y+IkExrxPj6ePAQ9UzNe/5R5rU/MdLvB8rVuqPiBuOsslSMWxjaeWgQoEqMCTRu0DB RNDNUWcPEaR+1oYz2oB7FyAKKpuJvyceE6ByH3ZuHL3mMHdlvnOeaHgzhxEq4ldtG61V crXo2AAUbCEK4C9xmGeeCPfWCMH5i/SZVKpnAZBOpHgvNNSFyuPDx3e6kWVSZLCUqMuW igtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567183; x=1712171983; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8RMlGq1Gwhpk4UAA96yMefndlkDhs2bhrF9FMUSrKbo=; b=CLWaZS9/GcXO7FKhmRxjWXsFETYdnECD2HJJDOtcFDj4KiotuHIYolhptLS+3NQ5DA nG1qXpn5nRKBiuR6XtekcwgRUZ9iZW3YVuaCJHZXBNXYdGEL100rlA7igRyDaoaLLhCQ GJQsTmFVp7YJyhmsOPucPQ0HMGPYQMXIXFq8J2+cmGDJgKor7lRMH/5fCriHR/ZIJCYa lQlYn8ykFDkowmIaopmQY+9xcel+i2KGorTbj5Vm9xncchieP5fIKbUVNr5MXNAKQ1hh DAipA2tBf8WMLAw4MFYwx0Y6KLTF/ZtK026N2TJNEOXhkEuK/yHPWjlopBpQ0qRFBQLB J4Sw== X-Gm-Message-State: AOJu0YyztDRoMJ4KsKlU7f7FwneTYMGUxZoFHexhLdRoW/+rSD6Gzatr TNMYSWPgq2LVqv6yDDypGUug+Ed2sRT+GdFCFgska9eoWLD0YbP9xHVO4b7X7VRgbAobbDCI/jq y X-Google-Smtp-Source: AGHT+IGK0oyuCfqYYMTrX2DTfSnfVufYGirs8t+UBSVUwwuaBpnmJqYeo/TDMgcYs59fK4JC5W49Ng== X-Received: by 2002:a05:6a20:da8d:b0:1a3:4721:df94 with SMTP id iy13-20020a056a20da8d00b001a34721df94mr1099753pzb.0.1711567183174; Wed, 27 Mar 2024 12:19:43 -0700 (PDT) Received: from m2max.thefacebook.com ([2620:10d:c090:600::1:bb1e]) by smtp.gmail.com with ESMTPSA id n2-20020aa79842000000b006e6c3753786sm8278882pfq.41.2024.03.27.12.19.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:19:41 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 04/10] io_uring: unify io_pin_pages() Date: Wed, 27 Mar 2024 13:13:39 -0600 Message-ID: <20240327191933.607220-5-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240327191933.607220-1-axboe@kernel.dk> References: <20240327191933.607220-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Move it into io_uring.c where it belongs, and use it in there as well rather than have two implementations of this. Signed-off-by: Jens Axboe --- io_uring/io_uring.c | 58 ++++++++++++++++++++++++++++++++------------- io_uring/rsrc.c | 36 ---------------------------- 2 files changed, 41 insertions(+), 53 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 67c93b290ed9..e3d2e2655e95 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2643,31 +2643,56 @@ static void io_pages_free(struct page ***pages, int npages) *pages = NULL; } +struct page **io_pin_pages(unsigned long uaddr, unsigned long len, int *npages) +{ + unsigned long start, end, nr_pages; + struct page **pages; + int ret; + + end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; + start = uaddr >> PAGE_SHIFT; + nr_pages = end - start; + WARN_ON(!nr_pages); + + pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); + if (!pages) + return ERR_PTR(-ENOMEM); + + ret = pin_user_pages_fast(uaddr, nr_pages, FOLL_WRITE | FOLL_LONGTERM, + pages); + /* success, mapped all pages */ + if (ret == nr_pages) { + *npages = nr_pages; + return pages; + } + + /* partial map, or didn't map anything */ + if (ret >= 0) { + /* if we did partial map, release any pages we did get */ + if (ret) + unpin_user_pages(pages, ret); + ret = -EFAULT; + } + kvfree(pages); + return ERR_PTR(ret); +} + static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, unsigned long uaddr, size_t size) { struct page **page_array; unsigned int nr_pages; void *page_addr; - int ret, pinned; *npages = 0; if (uaddr & (PAGE_SIZE - 1) || !size) return ERR_PTR(-EINVAL); - nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; - if (nr_pages > USHRT_MAX) - if (!page_array) - return ERR_PTR(-ENOMEM); - - - pinned = pin_user_pages_fast(uaddr, nr_pages, FOLL_WRITE | FOLL_LONGTERM, - page_array); - if (pinned != nr_pages) { - ret = (pinned < 0) ? pinned : -EFAULT; - goto free_pages; - } + nr_pages = 0; + page_array = io_pin_pages(uaddr, size, &nr_pages); + if (IS_ERR(page_array)) + return page_array; page_addr = vmap(page_array, nr_pages, VM_MAP, PAGE_KERNEL); if (page_addr) { @@ -2675,10 +2700,9 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, *npages = nr_pages; return page_addr; } - ret = -ENOMEM; -free_pages: - io_pages_free(&page_array, pinned > 0 ? pinned : 0); - return ERR_PTR(ret); + + io_pages_free(&page_array, nr_pages); + return ERR_PTR(-ENOMEM); } static void *io_rings_map(struct io_ring_ctx *ctx, unsigned long uaddr, diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index 7b8a056f98ed..8a34181c97ab 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -870,42 +870,6 @@ static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, return ret; } -struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages) -{ - unsigned long start, end, nr_pages; - struct page **pages = NULL; - int ret; - - end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT; - start = ubuf >> PAGE_SHIFT; - nr_pages = end - start; - WARN_ON(!nr_pages); - - pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); - if (!pages) - return ERR_PTR(-ENOMEM); - - mmap_read_lock(current->mm); - ret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, pages); - mmap_read_unlock(current->mm); - - /* success, mapped all pages */ - if (ret == nr_pages) { - *npages = nr_pages; - return pages; - } - - /* partial map, or didn't map anything */ - if (ret >= 0) { - /* if we did partial map, release any pages we did get */ - if (ret) - unpin_user_pages(pages, ret); - ret = -EFAULT; - } - kvfree(pages); - return ERR_PTR(ret); -} - static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, struct io_mapped_ubuf **pimu, struct page **last_hpage) From patchwork Wed Mar 27 19:13:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13607317 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A6E614D280 for ; Wed, 27 Mar 2024 19:19:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567188; cv=none; b=ZWk5ZLzvak1I25gN/0pIKQdTgUtKQbcvVFhI8YJEco49W95OVPEovt6Ep4rV2YedgLWk4Tl8BPneiz7PgKBuYnUxpVdCwtT+bw/3Fc8w3cnRJb3RJ+Qn9QmaFnEJKYSjuu7IuC6FCfk0KO3pzNubxPU1rpksfnNbVW85IfkDUZQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567188; c=relaxed/simple; bh=IPj6VKN7BleBl24Q46XxZI4LhiYDh51nW7RVel9Fv6w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YT0Le/y3E19NtE9UYMKODsBogPeazADtXvfLNCwMmaQuAaiAucfYN6LQ1NlOGG3oqJCmwckm7IBAar0yaIQ++Firb0dBVv8oOTZu1e7d3qihIBbl8V1nuKen7JMTbktV5OGLZWyn55BT0ZfUEmQsNi2ENIk3rCmKP8sYBOUCw90= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=JICYg6nN; arc=none smtp.client-ip=209.85.215.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="JICYg6nN" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-58962bf3f89so25593a12.0 for ; Wed, 27 Mar 2024 12:19:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1711567185; x=1712171985; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=O9hRezk9QcZPei+uCZVczfTP+TTF/FpzdoAoLtJHj44=; b=JICYg6nNW9jpu7A62SczalOQFWZNSW27o+Eh4OzxDcGDn6Sg9w/qq03RyPhZkBkhDQ DWgmCvxJ2fY2bsQ/BJ4eRH32coz7HzqSs9XNP1CdvCrRMpjwIOpp6h/two1aumlb9Qyx Qxob4+E6lIl9M8bgVs+dz83r897J0MIJISBNzMnD1uUo/BJxYtOymuxT09HGZA8h9H75 ZdBX5zcDeuWscc+22yfDtoEqzJCRIv3aAN8OM9/jXh+wbhcLDEa2RQGWNp8BqPkyrPyX k1bbSQ3f5vASsGoEH3Rwyid9PYJiQB6MQ/f911Ze9a7H91kAqUGVcPjwH9DGz+c8Qv09 NN7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567185; x=1712171985; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O9hRezk9QcZPei+uCZVczfTP+TTF/FpzdoAoLtJHj44=; b=VFZGLTBRETYdBZDdK4bxdDeIkRh6TIKgeIiyBAU4l20a+Gi2hFDAHQ4yS0YVmuV3Up hnlouEmxCnluViM3Ib1g6CkJYCY1werWXzCo6rUffozkmxFFUhVU6+y1k3cFCS8ViAzc TtArnLUohxiHWo9yR6rMKxef4MhXg1H+xpBQpjGW+3IIpuzf7CFUOvxOG3wM5LbW9UUj 9DT4Q5E/AeB7QazBFfAvx53APhZSmhWd75g3P0DdtZEtc9+KihebsuhVN9Qlt4nskkDw DdNiUukxupFBD65qVnu567aC8xvuaCkgB1UO8d7EEx2EpK5mLwIwBbFDjit1iniP7elS 7V2g== X-Gm-Message-State: AOJu0YyG0Ky3625xKHcbIy9NYxeL3lUcf/SxmXftKQ1YljJMZegZWpQn mybVN3mlEsirnYI7+gDVODnjsEk5YRQi51edAZN4B5w/7QHxkBQiXSD3FqQ8MDu+vHh9yOgLH6h 1 X-Google-Smtp-Source: AGHT+IEP8lLbb7dN6Wq8niC5PC2/9TOdFZfU0JUwmqSB4C+a5/2AoBr9fl+ydSQX5ljqBkT70AGFNg== X-Received: by 2002:a05:6a20:daa9:b0:1a3:b00a:7921 with SMTP id iy41-20020a056a20daa900b001a3b00a7921mr1085117pzb.5.1711567184990; Wed, 27 Mar 2024 12:19:44 -0700 (PDT) Received: from m2max.thefacebook.com ([2620:10d:c090:600::1:bb1e]) by smtp.gmail.com with ESMTPSA id n2-20020aa79842000000b006e6c3753786sm8278882pfq.41.2024.03.27.12.19.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:19:43 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 05/10] io_uring/kbuf: get rid of lower BGID lists Date: Wed, 27 Mar 2024 13:13:40 -0600 Message-ID: <20240327191933.607220-6-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240327191933.607220-1-axboe@kernel.dk> References: <20240327191933.607220-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Just rely on the xarray for any kind of bgid. This simplifies things, and it really doesn't bring us much, if anything. Signed-off-by: Jens Axboe --- include/linux/io_uring_types.h | 1 - io_uring/io_uring.c | 2 - io_uring/kbuf.c | 70 ++++------------------------------ 3 files changed, 8 insertions(+), 65 deletions(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index b191710bec4f..8c64c303dee8 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -295,7 +295,6 @@ struct io_ring_ctx { struct io_submit_state submit_state; - struct io_buffer_list *io_bl; struct xarray io_bl_xa; struct io_hash_table cancel_table_locked; diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index e3d2e2655e95..31b686c5cb23 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -354,7 +354,6 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) io_futex_cache_free(ctx); kfree(ctx->cancel_table.hbs); kfree(ctx->cancel_table_locked.hbs); - kfree(ctx->io_bl); xa_destroy(&ctx->io_bl_xa); kfree(ctx); return NULL; @@ -2932,7 +2931,6 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) io_napi_free(ctx); kfree(ctx->cancel_table.hbs); kfree(ctx->cancel_table_locked.hbs); - kfree(ctx->io_bl); xa_destroy(&ctx->io_bl_xa); kfree(ctx); } diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c index 693c26da4ee1..8bf0121f00af 100644 --- a/io_uring/kbuf.c +++ b/io_uring/kbuf.c @@ -17,8 +17,6 @@ #define IO_BUFFER_LIST_BUF_PER_PAGE (PAGE_SIZE / sizeof(struct io_uring_buf)) -#define BGID_ARRAY 64 - /* BIDs are addressed by a 16-bit field in a CQE */ #define MAX_BIDS_PER_BGID (1 << 16) @@ -40,13 +38,9 @@ struct io_buf_free { int inuse; }; -static struct io_buffer_list *__io_buffer_get_list(struct io_ring_ctx *ctx, - struct io_buffer_list *bl, - unsigned int bgid) +static inline struct io_buffer_list *__io_buffer_get_list(struct io_ring_ctx *ctx, + unsigned int bgid) { - if (bl && bgid < BGID_ARRAY) - return &bl[bgid]; - return xa_load(&ctx->io_bl_xa, bgid); } @@ -55,7 +49,7 @@ static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx, { lockdep_assert_held(&ctx->uring_lock); - return __io_buffer_get_list(ctx, ctx->io_bl, bgid); + return __io_buffer_get_list(ctx, bgid); } static int io_buffer_add_list(struct io_ring_ctx *ctx, @@ -68,10 +62,6 @@ static int io_buffer_add_list(struct io_ring_ctx *ctx, */ bl->bgid = bgid; smp_store_release(&bl->is_ready, 1); - - if (bgid < BGID_ARRAY) - return 0; - return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL)); } @@ -208,24 +198,6 @@ void __user *io_buffer_select(struct io_kiocb *req, size_t *len, return ret; } -static __cold int io_init_bl_list(struct io_ring_ctx *ctx) -{ - struct io_buffer_list *bl; - int i; - - bl = kcalloc(BGID_ARRAY, sizeof(struct io_buffer_list), GFP_KERNEL); - if (!bl) - return -ENOMEM; - - for (i = 0; i < BGID_ARRAY; i++) { - INIT_LIST_HEAD(&bl[i].buf_list); - bl[i].bgid = i; - } - - smp_store_release(&ctx->io_bl, bl); - return 0; -} - /* * Mark the given mapped range as free for reuse */ @@ -300,13 +272,6 @@ void io_destroy_buffers(struct io_ring_ctx *ctx) struct list_head *item, *tmp; struct io_buffer *buf; unsigned long index; - int i; - - for (i = 0; i < BGID_ARRAY; i++) { - if (!ctx->io_bl) - break; - __io_remove_buffers(ctx, &ctx->io_bl[i], -1U); - } xa_for_each(&ctx->io_bl_xa, index, bl) { xa_erase(&ctx->io_bl_xa, bl->bgid); @@ -489,12 +454,6 @@ int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) io_ring_submit_lock(ctx, issue_flags); - if (unlikely(p->bgid < BGID_ARRAY && !ctx->io_bl)) { - ret = io_init_bl_list(ctx); - if (ret) - goto err; - } - bl = io_buffer_get_list(ctx, p->bgid); if (unlikely(!bl)) { bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT); @@ -507,14 +466,9 @@ int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) if (ret) { /* * Doesn't need rcu free as it was never visible, but - * let's keep it consistent throughout. Also can't - * be a lower indexed array group, as adding one - * where lookup failed cannot happen. + * let's keep it consistent throughout. */ - if (p->bgid >= BGID_ARRAY) - kfree_rcu(bl, rcu); - else - WARN_ON_ONCE(1); + kfree_rcu(bl, rcu); goto err; } } @@ -679,12 +633,6 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg) if (reg.ring_entries >= 65536) return -EINVAL; - if (unlikely(reg.bgid < BGID_ARRAY && !ctx->io_bl)) { - int ret = io_init_bl_list(ctx); - if (ret) - return ret; - } - bl = io_buffer_get_list(ctx, reg.bgid); if (bl) { /* if mapped buffer ring OR classic exists, don't allow */ @@ -734,10 +682,8 @@ int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg) return -EINVAL; __io_remove_buffers(ctx, bl, -1U); - if (bl->bgid >= BGID_ARRAY) { - xa_erase(&ctx->io_bl_xa, bl->bgid); - kfree_rcu(bl, rcu); - } + xa_erase(&ctx->io_bl_xa, bl->bgid); + kfree_rcu(bl, rcu); return 0; } @@ -771,7 +717,7 @@ void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid) { struct io_buffer_list *bl; - bl = __io_buffer_get_list(ctx, smp_load_acquire(&ctx->io_bl), bgid); + bl = __io_buffer_get_list(ctx, bgid); if (!bl || !bl->is_mmap) return NULL; From patchwork Wed Mar 27 19:13:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13607318 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCC2312E1F6 for ; Wed, 27 Mar 2024 19:19:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567189; cv=none; b=NGHNlvpCRG9CavhjTQszNhGp9VI8gdURVV43+2/UHNweK6n5mrbbFOh1C8TN4SasReKH7ruFe4UkPi1hgP014HVweGngukBIK7viCUbQ/uy53aZjcNfEsmyyQcNq37pgCebPjKTvmPlSM7A/Qypnq6LuePFN+hD124s3vRCASmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567189; c=relaxed/simple; bh=8G3QN6wuHYeuEtMo+UdWrxs8c//P5kJMH+4+MKrhbiw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C/++33TdddQxIhm5tTC3LwlEq/ci+57WGBpUZtzhNWgHdHepjp3yplMk/YyQ/FVtzYTdtNg0Nfx4PjjEjMBE1BQV3uw7hrPd/MQNyIOuBnALTB3pGfQiti3uG2Xk6CYyXilmlSGPeJtMpglPFuTTKorqSsouFrvp7+/fYvH0MpI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=xlE3xr6C; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="xlE3xr6C" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6e6c38be762so41826b3a.1 for ; Wed, 27 Mar 2024 12:19:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1711567187; x=1712171987; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QtvzBKul3lnX4UwRp+DUDb1X15hTCGMb7+BkwEEbcgU=; b=xlE3xr6CLZCNPI7eUHuHtHylNkDDBYbbv6ncr0RE9F0dm6bJjczOdPvyEgQAEQYocA m0jirY2dmAkGgYmAnqeKFVu26sEkxJcO18EQwHTpjxvQYRLRlbmTS/VZgBexithY1WYM pey3JpOaj3WIq3HBvF3Zg7VpxMdyDtU0xTmcs0kfbQvkeKnUotTzjpvkDZHUrwB1dvdE Q5uAKk5z2T7VNfGwidh+/HpGRhYVjK2Xd1knShNxeRLPuF84B0VXZrn/1JrsjeGhJEKd PweqjFFNI7rTlpJMK+RlAFCXmobk/PkzCRvZHRAU0lyVZjzjVAzV8C+VVaNbRv5fJ5f+ aeLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567187; x=1712171987; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QtvzBKul3lnX4UwRp+DUDb1X15hTCGMb7+BkwEEbcgU=; b=cUFmKw6sC5H79OCCjlGBJIZkDmhX+dwzi42NFCHHLU3wW/9iT0Yyn4WpZ2+eOYKntj ZRacuDiD937DmaV6MTPbroKPSw7tpc38VxaZijtNG6zuau3QnRjfpxl5W+qzU62i8ADi eNFXkffALuH9qXqFjibY0o+3hS6Pfm16PZDjYq6z9TC/XNAt0eIRSQP3XlkMkgboeGNK IXhcFPDH4QCMFaARw8s3F77ySR6ltswN97a6IHmyEioFq7qZvJMXigGFLbnR+fjFyKY/ J5RX43ig6En1A89b0YkbzOp9Mmrujq5Tu9Min5I9oF4q9N/i3i21Gerl+5EIflwVpGwN +wrA== X-Gm-Message-State: AOJu0YwUHC/E7yU51yuK7XM8fSED4k5Fg78v0TY690dsI9msQBg6gx+3 3PkyplV02o569zSJ8iWXEcOwFQ225MokNOkHvUjU/gV5O1kR0Z/FDBf1hd64tWwhSSEP/66mxal Q X-Google-Smtp-Source: AGHT+IE9aMH6EqLndqN3GSJUpuA8ZNoFlJCufQhyoApbOXdCJjHvWo/Av0qISLikK60X7/QrLvb4zw== X-Received: by 2002:a05:6a00:39a3:b0:6e9:ca7b:c150 with SMTP id fi35-20020a056a0039a300b006e9ca7bc150mr830480pfb.3.1711567186770; Wed, 27 Mar 2024 12:19:46 -0700 (PDT) Received: from m2max.thefacebook.com ([2620:10d:c090:600::1:bb1e]) by smtp.gmail.com with ESMTPSA id n2-20020aa79842000000b006e6c3753786sm8278882pfq.41.2024.03.27.12.19.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:19:45 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 06/10] io_uring/kbuf: get rid of bl->is_ready Date: Wed, 27 Mar 2024 13:13:41 -0600 Message-ID: <20240327191933.607220-7-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240327191933.607220-1-axboe@kernel.dk> References: <20240327191933.607220-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Now that xarray is being exclusively used for the buffer_list lookup, this check is no longer needed. Get rid of it and the is_ready member. Signed-off-by: Jens Axboe --- io_uring/kbuf.c | 8 -------- io_uring/kbuf.h | 2 -- 2 files changed, 10 deletions(-) diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c index 8bf0121f00af..011280d873e7 100644 --- a/io_uring/kbuf.c +++ b/io_uring/kbuf.c @@ -61,7 +61,6 @@ static int io_buffer_add_list(struct io_ring_ctx *ctx, * always under the ->uring_lock, but the RCU lookup from mmap does. */ bl->bgid = bgid; - smp_store_release(&bl->is_ready, 1); return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL)); } @@ -721,13 +720,6 @@ void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid) if (!bl || !bl->is_mmap) return NULL; - /* - * Ensure the list is fully setup. Only strictly needed for RCU lookup - * via mmap, and in that case only for the array indexed groups. For - * the xarray lookups, it's either visible and ready, or not at all. - */ - if (!smp_load_acquire(&bl->is_ready)) - return NULL; return bl->buf_ring; } diff --git a/io_uring/kbuf.h b/io_uring/kbuf.h index 1c7b654ee726..fdbb10449513 100644 --- a/io_uring/kbuf.h +++ b/io_uring/kbuf.h @@ -29,8 +29,6 @@ struct io_buffer_list { __u8 is_buf_ring; /* ring mapped provided buffers, but mmap'ed by application */ __u8 is_mmap; - /* bl is visible from an RCU point of view for lookup */ - __u8 is_ready; }; struct io_buffer { From patchwork Wed Mar 27 19:13:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13607319 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A192D1514EC for ; Wed, 27 Mar 2024 19:19:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567191; cv=none; b=QNwiJGfiVbYLyPAIO3kEraz3RVTa91fkonyWmf/oEh5xtKe20FeRtFW6uzUOlZw2r+AH1cxP8COAMQl+ue7X0k5LFwQG2VEgZxLH5ivFiFoZ8lXCDqXIgScUcFmNj0ARLdoGOc6EDEtuCN1twj4se0WRRpMq2uuOhfeh3Bb4X2Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567191; c=relaxed/simple; bh=0W2obeekuSQdzZWXiQybp+acO7ke81Dc2YvaIvoTuQY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XvTBcwyk3ATY60rewBI9g4gkcO4ElRWgmCl+/3/jZ6BozNaoRrJ688C1KhZyYtR1+54u/0iOje/Pmxh7AJQaA89gRtYuE7E0GyTvcC8dXILbAGSz4cVXoEZsQnDgwFiFUdWLJ7pTHvXLWOt8wL1pwxWHnsQh41I0I6vqCmLSXbE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=Ldhv9mFl; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="Ldhv9mFl" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6e694337fffso59622b3a.1 for ; Wed, 27 Mar 2024 12:19:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1711567188; x=1712171988; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IrcjwXvOlSphD9f4BvWGSMDrK92UD29qUBOBEFOPsVY=; b=Ldhv9mFlbBRqz46nKlHY9aM6BTJjKe6MgfWfR6raSuckqYyO9UhLCj2xLuBX9FDVXo 4K9hiEWtCOiUmdMRaWykC5WY53dvzu3CfWqnBruCQq5K7UfjGx/SEKQNX3N/YCAndjsl ibX4lHcVIOccm/FvoWFpgV7a+N5w5bC1K0uVsRz8SMpY+RJCDJbwpoQp6xG2wfWWCUfB c1LtijGKRb5/LcisWr2dsl6A3KoasXjXkCCEwqW4F9nLK+/6dm6wgtEkkiHQkDDg3V3J MOGtpmivKJG4+9t+91Xhk1srmAfx74qq34A+Os9qpRf4ppXFwB2fprm8bkgRmsp3xeWK BydQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567188; x=1712171988; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IrcjwXvOlSphD9f4BvWGSMDrK92UD29qUBOBEFOPsVY=; b=DzRfISsSNv7pgc2bVXW2ZCvqdL8yoVvpVfds9B+USdTvtT+1pkruP8bnPBwlYu+ZwW +A9hBMuKr5zHNfY648BGkVhSuNfRF7mVfY1MTfs2BCGkK4GvMPP9g3n6gnqTju0sDNuI 79w8gQdAaAbvnMrAsFoy2HAskmesr/petT1IDeCs25ccU1GiN0neoLaR4PsEiK2gJwf0 3Or+kyS7dJn6V9nEA3fx2ngmfWvFDo7ihn6OJd6Vo2N8WEiNcn1mdgHSczQQ3Tn39u4f chAxC8GkJJ8zTiOTsxjsguXrsGp53tc+bB/8DzYPxUJNBVmvAjnoHMfY1dgIMydyhute Z+wA== X-Gm-Message-State: AOJu0Yx0111lBW9erUszuHwsmDuIdNQi35in+T4KSykOGCwqgN3Ra7dJ lCEG+VW5x/voGtdpqRUCBk/4S4Uog1VrqQ6gWmp7++I5oepHFtgtOJlZBfjBxqjpaW++9E3R0zy 1 X-Google-Smtp-Source: AGHT+IE3PERzd/nfFQlwPYyGtmnXFESfpmk0lyii1SPkAo4DLSnpdbwRUYXczCwCXJyLOT1auraYFg== X-Received: by 2002:a05:6a00:929e:b0:6ea:b1f5:8aa with SMTP id jw30-20020a056a00929e00b006eab1f508aamr858051pfb.3.1711567188530; Wed, 27 Mar 2024 12:19:48 -0700 (PDT) Received: from m2max.thefacebook.com ([2620:10d:c090:600::1:bb1e]) by smtp.gmail.com with ESMTPSA id n2-20020aa79842000000b006e6c3753786sm8278882pfq.41.2024.03.27.12.19.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:19:47 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 07/10] io_uring/kbuf: vmap pinned buffer ring Date: Wed, 27 Mar 2024 13:13:42 -0600 Message-ID: <20240327191933.607220-8-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240327191933.607220-1-axboe@kernel.dk> References: <20240327191933.607220-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This avoids needing to care about HIGHMEM, and it makes the buffer indexing easier as both ring provided buffer methods are now virtually mapped in a contigious fashion. Signed-off-by: Jens Axboe --- io_uring/kbuf.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c index 011280d873e7..72c15dde34d3 100644 --- a/io_uring/kbuf.c +++ b/io_uring/kbuf.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -145,15 +146,7 @@ static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len, req->flags |= REQ_F_BL_EMPTY; head &= bl->mask; - /* mmaped buffers are always contig */ - if (bl->is_mmap || head < IO_BUFFER_LIST_BUF_PER_PAGE) { - buf = &br->bufs[head]; - } else { - int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1); - int index = head / IO_BUFFER_LIST_BUF_PER_PAGE; - buf = page_address(bl->buf_pages[index]); - buf += off; - } + buf = &br->bufs[head]; if (*len == 0 || *len > buf->len) *len = buf->len; req->flags |= REQ_F_BUFFER_RING; @@ -240,6 +233,7 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx, for (j = 0; j < bl->buf_nr_pages; j++) unpin_user_page(bl->buf_pages[j]); kvfree(bl->buf_pages); + vunmap(bl->buf_ring); bl->buf_pages = NULL; bl->buf_nr_pages = 0; } @@ -490,9 +484,9 @@ int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg, struct io_buffer_list *bl) { - struct io_uring_buf_ring *br; + struct io_uring_buf_ring *br = NULL; + int nr_pages, ret, i; struct page **pages; - int i, nr_pages; pages = io_pin_pages(reg->ring_addr, flex_array_size(br, bufs, reg->ring_entries), @@ -500,18 +494,12 @@ static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg, if (IS_ERR(pages)) return PTR_ERR(pages); - /* - * Apparently some 32-bit boxes (ARM) will return highmem pages, - * which then need to be mapped. We could support that, but it'd - * complicate the code and slowdown the common cases quite a bit. - * So just error out, returning -EINVAL just like we did on kernels - * that didn't support mapped buffer rings. - */ - for (i = 0; i < nr_pages; i++) - if (PageHighMem(pages[i])) - goto error_unpin; + br = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL); + if (!br) { + ret = -ENOMEM; + goto error_unpin; + } - br = page_address(pages[0]); #ifdef SHM_COLOUR /* * On platforms that have specific aliasing requirements, SHM_COLOUR @@ -522,8 +510,10 @@ static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg, * should use IOU_PBUF_RING_MMAP instead, and liburing will handle * this transparently. */ - if ((reg->ring_addr | (unsigned long) br) & (SHM_COLOUR - 1)) + if ((reg->ring_addr | (unsigned long) br) & (SHM_COLOUR - 1)) { + ret = -EINVAL; goto error_unpin; + } #endif bl->buf_pages = pages; bl->buf_nr_pages = nr_pages; @@ -535,7 +525,8 @@ static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg, for (i = 0; i < nr_pages; i++) unpin_user_page(pages[i]); kvfree(pages); - return -EINVAL; + vunmap(br); + return ret; } /* From patchwork Wed Mar 27 19:13:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13607320 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62E08152167 for ; Wed, 27 Mar 2024 19:19:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567193; cv=none; b=aJTAkymndi4/TEVj9J+j+74EvgAxiZnNvvoUsYEpqctz6lDdprz6IH6NyAN2Ro9X3+gdDCqIqzGNmGIXlHNKgVJZOxXOwTotvYUOfAG9d2w/uu0WLBjFoBjL/2P5VxvtUXqmCV/mZTgEh2fFfnXT1byu/F2P93HEfB2JPn1BJEE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567193; c=relaxed/simple; bh=5YqocHr5mxuGOr5Z0D+Jj5Z+RZ0wDYj67RIhWvHLoys=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NqNgpAdiT8Ky+R8Gak/fY6RXZfZZqzuP6c9eUgqaKhn27AxSfoieSj5HwTY9YYQuOZBeoooM5HVQIhj9Vi8ncMDxaNlm/MrK8WJz7wMWEt7UK9u4n3AbdIp67GDkoi2JZZsMiy9nUX8DDIucjJw7rq5PNxg+dbzueUXP3R2uMeE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=044Xn4DQ; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="044Xn4DQ" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6e694337fffso59632b3a.1 for ; Wed, 27 Mar 2024 12:19:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1711567190; x=1712171990; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dRbXBnmBssLCefthvGjqIke1i8J8DEDsrgn5OvhdPnw=; b=044Xn4DQoUHScYlFpXeIw3ZeM3g6qOZGq7lwhRIi0QhAE26PmahorWuWjpbrU20JEO Oyw0IsE3yyotht8U1czYqg1QtuJUSkGjHGTbVU5ZiHLfuZ8OSqNfg2HRR2CO8zYEcAWC HifKvDmAPlbnOxNpkVf+j6F+DpdOuDOzTnTM1GBDyceTFpT90+gLud7qAiz7CSf9PXKq Nlr9BSHoyeme4P4uTW7mMNt0c3A4469qv+q+XWCXEBwPIcpJ6HCeXwVEaD9pEC8SbsI6 73hIN5ks596yuPJWS00G+X9GdNBWuYhzXg7xBLYcmD9WC+JSZxaAAW63/eCYdQl+FJqq /yKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567190; x=1712171990; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dRbXBnmBssLCefthvGjqIke1i8J8DEDsrgn5OvhdPnw=; b=F76oihS0jh2mfQ7ob+67H3MOIqgvlJ7DF5VtljwIVTjCpXEDzzH6SLfRZvxyuEZmZW KGCEnJVLeF5WTTEYWttdJPTvQ0Gu0XnW4husM4sKeumdAtp9mA/mH0IBA33zogNFZSTU JXIXHo0F8WmwybEN1A4O7cb/bz6okvBEyZYTUmgftcx5u9MxvOKVUMG9KkTq4EqN1hh+ 3h5s9VJXry4UWmeTptDzcrew0SkrNLYtXx2CA9Wmf4hEKTtl2wZqgIo7QodmMzPKClO0 0Jj2eLMDvsvxLU4yMwrap9majRvIxuLruqNZjdrkwP39jSsAm7YAgJsCvgWw/bSVhZQE nBVA== X-Gm-Message-State: AOJu0YwxHIaYeF/4fGv3IMysmMMRXPbByhvmGYLv13NdUabsgFbt801d Z/cKgPXoZL/6vRtr9LmDzSUmw791QiB0ysO+bhH3K0naTRcP10cfgPQtSGhGj+4Utg5WcfklxEe r X-Google-Smtp-Source: AGHT+IEMifsDN6osR6tIPxBi9aDAJ8gAjN6bActUpN5W6XLr7k0j8vne4Hd40xna4ZZkyF2PEXOq6Q== X-Received: by 2002:a05:6a20:7f9b:b0:1a3:bd72:b8de with SMTP id d27-20020a056a207f9b00b001a3bd72b8demr965327pzj.3.1711567190293; Wed, 27 Mar 2024 12:19:50 -0700 (PDT) Received: from m2max.thefacebook.com ([2620:10d:c090:600::1:bb1e]) by smtp.gmail.com with ESMTPSA id n2-20020aa79842000000b006e6c3753786sm8278882pfq.41.2024.03.27.12.19.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:19:48 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 08/10] io_uring/kbuf: protect io_buffer_list teardown with a reference Date: Wed, 27 Mar 2024 13:13:43 -0600 Message-ID: <20240327191933.607220-9-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240327191933.607220-1-axboe@kernel.dk> References: <20240327191933.607220-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 No functional changes in this patch, just in preparation for being able to keep the buffer list alive outside of the ctx->uring_lock. Signed-off-by: Jens Axboe --- io_uring/kbuf.c | 15 +++++++++++---- io_uring/kbuf.h | 2 ++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c index 72c15dde34d3..206f4d352e15 100644 --- a/io_uring/kbuf.c +++ b/io_uring/kbuf.c @@ -62,6 +62,7 @@ static int io_buffer_add_list(struct io_ring_ctx *ctx, * always under the ->uring_lock, but the RCU lookup from mmap does. */ bl->bgid = bgid; + atomic_set(&bl->refs, 1); return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL)); } @@ -259,6 +260,14 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx, return i; } +static void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl) +{ + if (atomic_dec_and_test(&bl->refs)) { + __io_remove_buffers(ctx, bl, -1U); + kfree_rcu(bl, rcu); + } +} + void io_destroy_buffers(struct io_ring_ctx *ctx) { struct io_buffer_list *bl; @@ -268,8 +277,7 @@ void io_destroy_buffers(struct io_ring_ctx *ctx) xa_for_each(&ctx->io_bl_xa, index, bl) { xa_erase(&ctx->io_bl_xa, bl->bgid); - __io_remove_buffers(ctx, bl, -1U); - kfree_rcu(bl, rcu); + io_put_bl(ctx, bl); } /* @@ -671,9 +679,8 @@ int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg) if (!bl->is_buf_ring) return -EINVAL; - __io_remove_buffers(ctx, bl, -1U); xa_erase(&ctx->io_bl_xa, bl->bgid); - kfree_rcu(bl, rcu); + io_put_bl(ctx, bl); return 0; } diff --git a/io_uring/kbuf.h b/io_uring/kbuf.h index fdbb10449513..8b868a1744e2 100644 --- a/io_uring/kbuf.h +++ b/io_uring/kbuf.h @@ -25,6 +25,8 @@ struct io_buffer_list { __u16 head; __u16 mask; + atomic_t refs; + /* ring mapped provided buffers */ __u8 is_buf_ring; /* ring mapped provided buffers, but mmap'ed by application */ From patchwork Wed Mar 27 19:13:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13607322 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4957152167 for ; Wed, 27 Mar 2024 19:19:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567196; cv=none; b=iUhmOVyEKSibG1/Hp3Z/ntP5zSfFOy0tkRnkn7jGKIqgyWhZpFLieGEK5DtmWb6i43J63UQy1ZmKRXR2VMZQ1XwWOn2wdcglVtUV4bKdeFy/fDZkNe2m4CPR9zZLpGHNXXZMLiegId5eMy0wQePtkSElqdUYjP4jSsdmgtrBNQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567196; c=relaxed/simple; bh=qumXygsVJxKhTf+P9KS4IvjYsrW7eBf6PGZhXbAIdSI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B/M3Z/PUnGE94NQzn3wYqgZ4c9Oz/L9j5veyGoYSaL7HlLfMwfcmc/GN2nbedI4a0a9XA0IxsMxdC+Z05aVVeQ/eku5Ne9DqzGvFfeduTo+oA23AFKQEtJ72rfP/xhsOIB8WXab+LHVjAMlvAU27eUrw+1Zbbe3uqaFNYZsBtj4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=Vkufz5Gy; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="Vkufz5Gy" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6ea729f2e38so54228b3a.1 for ; Wed, 27 Mar 2024 12:19:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1711567192; x=1712171992; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Idr8nNiULEqhe+F7HvoO4bBSwsnHb0chrJlG9XWOXcQ=; b=Vkufz5GyCMJcQc7jOvcHbYlc3lICXnDBb7G9s8gayKTsD+yzcMD0Lc18jS0Qc+3+2C 7XufcEHKGWDOe2Xm6FWjUhuEGZgBP5dOH2mtu2+h9U4KZXN3X+kbMfJnpwSkxMgPMUkm zqetikbmNoriOR7LumiXGrCNnQXV4SabtIRQxXghawJzA9cnidPqk16NsdNs1DZfdnvO nI+A/la1SR2As8BUtf6fe4jnRc8MyRQJMQYx52u6zOTBuYKf8ke3+LKJSO3BWqRqSjpz uFa9KcZ5rHZTSaGBabJFedE0k++AB8Hf4QWqaM4ROzd0IiHRUosssMJ6Y0Y00mLnC2Uo 4tug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567192; x=1712171992; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Idr8nNiULEqhe+F7HvoO4bBSwsnHb0chrJlG9XWOXcQ=; b=phl47oHBhfpKLXnWH+C6zBSYs0UKtWzxKIjaehffMarCq7x+hO8aOTuUuem5SRXvMY MvwB6eoc7b5u+TXqoxzLMbkRJhkg6AKkzz+ww6Z6SjsQOlXDs81FzyV6Uqt4LAA06zD/ WLSCfB3tGb0dj+k548Mn+nV2qHbdsS6pfXcEqdKPJPSxFDYmHuJct+tSrvSJ5saYk12+ 9GqdT+jZMypPhqz964XbhWbQ0HQkJjF5uJkFdXvuhNDc/hvNmI3Es37nRG6mh1XIa8/N 2ZVPuaB545ngaBOm6By3MsSq9mOii7ryQUP0qKRnfX2ZllDjdmv5xXExJpI3o2TYfmNi lJDQ== X-Gm-Message-State: AOJu0YxRuYeglbbr007dmG36+MZ6ktPAKQ6EX7Mnlcdlo91pk3ynkM5I YhodRN1u0hTLeQGktmvjIkajoH4vix9/kJizjDW4sqfOaaJyO4OYhjEp3mXxSq6Blt9vPpEQ27l 3 X-Google-Smtp-Source: AGHT+IGX1iFPvZPftF/HlMieTtWp16JXUDDiJberAZ5KhmkbucT67ji5bIQRz1YCmN7aUtwoH9bD/Q== X-Received: by 2002:a05:6a21:7881:b0:1a3:6c0a:4f9b with SMTP id bf1-20020a056a21788100b001a36c0a4f9bmr1113282pzc.2.1711567192103; Wed, 27 Mar 2024 12:19:52 -0700 (PDT) Received: from m2max.thefacebook.com ([2620:10d:c090:600::1:bb1e]) by smtp.gmail.com with ESMTPSA id n2-20020aa79842000000b006e6c3753786sm8278882pfq.41.2024.03.27.12.19.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:19:50 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 09/10] io_uring/kbuf: use vm_insert_pages() for mmap'ed pbuf ring Date: Wed, 27 Mar 2024 13:13:44 -0600 Message-ID: <20240327191933.607220-10-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240327191933.607220-1-axboe@kernel.dk> References: <20240327191933.607220-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Rather than use remap_pfn_range() for this and manually free later, switch to using vm_insert_page() and have it Just Work. This requires a bit of effort on the mmap lookup side, as the ctx uring_lock isn't held, which otherwise protects buffer_lists from being torn down, and it's not safe to grab from mmap context that would introduce an ABBA deadlock between the mmap lock and the ctx uring_lock. Instead, lookup the buffer_list under RCU, as the the list is RCU freed already. Use the existing reference count to determine whether it's possible to safely grab a reference to it (eg if it's not zero already), and drop that reference when done with the mapping. If the mmap reference is the last one, the buffer_list and the associated memory can go away, since the vma insertion has references to the inserted pages at that point. Signed-off-by: Jens Axboe --- include/linux/io_uring_types.h | 3 - io_uring/io_uring.c | 69 +++++-------- io_uring/io_uring.h | 6 +- io_uring/kbuf.c | 171 +++++++++++---------------------- io_uring/kbuf.h | 7 +- 5 files changed, 85 insertions(+), 171 deletions(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index 8c64c303dee8..aeb4639785b5 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -372,9 +372,6 @@ struct io_ring_ctx { struct list_head io_buffers_cache; - /* deferred free list, protected by ->uring_lock */ - struct hlist_head io_buf_list; - /* Keep this last, we don't need it for the fast path */ struct wait_queue_head poll_wq; struct io_restriction restrictions; diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 31b686c5cb23..ff7276699a2c 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -303,7 +303,6 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) INIT_LIST_HEAD(&ctx->sqd_list); INIT_LIST_HEAD(&ctx->cq_overflow_list); INIT_LIST_HEAD(&ctx->io_buffers_cache); - INIT_HLIST_HEAD(&ctx->io_buf_list); ret = io_alloc_cache_init(&ctx->rsrc_node_cache, IO_NODE_ALLOC_CACHE_MAX, sizeof(struct io_rsrc_node)); ret |= io_alloc_cache_init(&ctx->apoll_cache, IO_POLL_ALLOC_CACHE_MAX, @@ -2599,12 +2598,12 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; } -static void io_pages_unmap(void *ptr, struct page ***pages, - unsigned short *npages) +void io_pages_unmap(void *ptr, struct page ***pages, unsigned short *npages, + bool put_pages) { bool do_vunmap = false; - if (*npages) { + if (put_pages && *npages) { struct page **to_free = *pages; int i; @@ -2620,14 +2619,6 @@ static void io_pages_unmap(void *ptr, struct page ***pages, *npages = 0; } -void io_mem_free(void *ptr) -{ - if (!ptr) - return; - - folio_put(virt_to_folio(ptr)); -} - static void io_pages_free(struct page ***pages, int npages) { struct page **page_array = *pages; @@ -2721,8 +2712,10 @@ static void *io_sqes_map(struct io_ring_ctx *ctx, unsigned long uaddr, static void io_rings_free(struct io_ring_ctx *ctx) { if (!(ctx->flags & IORING_SETUP_NO_MMAP)) { - io_pages_unmap(ctx->rings, &ctx->ring_pages, &ctx->n_ring_pages); - io_pages_unmap(ctx->sq_sqes, &ctx->sqe_pages, &ctx->n_sqe_pages); + io_pages_unmap(ctx->rings, &ctx->ring_pages, &ctx->n_ring_pages, + true); + io_pages_unmap(ctx->sq_sqes, &ctx->sqe_pages, &ctx->n_sqe_pages, + true); } else { io_pages_free(&ctx->ring_pages, ctx->n_ring_pages); ctx->n_ring_pages = 0; @@ -2783,8 +2776,8 @@ static void *io_mem_alloc_single(struct page **pages, int nr_pages, size_t size, return ERR_PTR(-ENOMEM); } -static void *io_pages_map(struct page ***out_pages, unsigned short *npages, - size_t size) +void *io_pages_map(struct page ***out_pages, unsigned short *npages, + size_t size) { gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN; struct page **pages; @@ -2814,17 +2807,6 @@ static void *io_pages_map(struct page ***out_pages, unsigned short *npages, return ERR_PTR(-ENOMEM); } -void *io_mem_alloc(size_t size) -{ - gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; - void *ret; - - ret = (void *) __get_free_pages(gfp, get_order(size)); - if (ret) - return ret; - return ERR_PTR(-ENOMEM); -} - static unsigned long rings_size(struct io_ring_ctx *ctx, unsigned int sq_entries, unsigned int cq_entries, size_t *sq_offset) { @@ -2921,7 +2903,6 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) ctx->mm_account = NULL; } io_rings_free(ctx); - io_kbuf_mmap_list_free(ctx); percpu_ref_exit(&ctx->refs); free_uid(ctx->user); @@ -3391,10 +3372,8 @@ static void *io_uring_validate_mmap_request(struct file *file, { struct io_ring_ctx *ctx = file->private_data; loff_t offset = pgoff << PAGE_SHIFT; - struct page *page; - void *ptr; - switch (offset & IORING_OFF_MMAP_MASK) { + switch ((pgoff << PAGE_SHIFT) & IORING_OFF_MMAP_MASK) { case IORING_OFF_SQ_RING: case IORING_OFF_CQ_RING: /* Don't allow mmap if the ring was setup without it */ @@ -3407,25 +3386,21 @@ static void *io_uring_validate_mmap_request(struct file *file, return ERR_PTR(-EINVAL); return ctx->sq_sqes; case IORING_OFF_PBUF_RING: { + struct io_buffer_list *bl; unsigned int bgid; + void *ret; bgid = (offset & ~IORING_OFF_MMAP_MASK) >> IORING_OFF_PBUF_SHIFT; - rcu_read_lock(); - ptr = io_pbuf_get_address(ctx, bgid); - rcu_read_unlock(); - if (!ptr) - return ERR_PTR(-EINVAL); - break; + bl = io_pbuf_get_bl(ctx, bgid); + if (IS_ERR(bl)) + return bl; + ret = bl->buf_ring; + io_put_bl(ctx, bl); + return ret; } - default: - return ERR_PTR(-EINVAL); } - page = virt_to_head_page(ptr); - if (sz > page_size(page)) - return ERR_PTR(-EINVAL); - - return ptr; + return ERR_PTR(-EINVAL); } int io_uring_mmap_pages(struct io_ring_ctx *ctx, struct vm_area_struct *vma, @@ -3444,7 +3419,6 @@ static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) struct io_ring_ctx *ctx = file->private_data; size_t sz = vma->vm_end - vma->vm_start; long offset = vma->vm_pgoff << PAGE_SHIFT; - unsigned long pfn; void *ptr; ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); @@ -3459,10 +3433,11 @@ static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) case IORING_OFF_SQES: return io_uring_mmap_pages(ctx, vma, ctx->sqe_pages, ctx->n_sqe_pages); + case IORING_OFF_PBUF_RING: + return io_pbuf_mmap(file, vma); } - pfn = virt_to_phys(ptr) >> PAGE_SHIFT; - return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); + return -EINVAL; } static unsigned long io_uring_mmu_get_unmapped_area(struct file *filp, diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h index ac2a84542417..23106dd06309 100644 --- a/io_uring/io_uring.h +++ b/io_uring/io_uring.h @@ -109,8 +109,10 @@ bool __io_alloc_req_refill(struct io_ring_ctx *ctx); bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, bool cancel_all); -void *io_mem_alloc(size_t size); -void io_mem_free(void *ptr); +void *io_pages_map(struct page ***out_pages, unsigned short *npages, + size_t size); +void io_pages_unmap(void *ptr, struct page ***pages, unsigned short *npages, + bool put_pages); enum { IO_EVENTFD_OP_SIGNAL_BIT, diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c index 206f4d352e15..99b349930a1a 100644 --- a/io_uring/kbuf.c +++ b/io_uring/kbuf.c @@ -32,25 +32,12 @@ struct io_provide_buf { __u16 bid; }; -struct io_buf_free { - struct hlist_node list; - void *mem; - size_t size; - int inuse; -}; - -static inline struct io_buffer_list *__io_buffer_get_list(struct io_ring_ctx *ctx, - unsigned int bgid) -{ - return xa_load(&ctx->io_bl_xa, bgid); -} - static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx, unsigned int bgid) { lockdep_assert_held(&ctx->uring_lock); - return __io_buffer_get_list(ctx, bgid); + return xa_load(&ctx->io_bl_xa, bgid); } static int io_buffer_add_list(struct io_ring_ctx *ctx, @@ -191,24 +178,6 @@ void __user *io_buffer_select(struct io_kiocb *req, size_t *len, return ret; } -/* - * Mark the given mapped range as free for reuse - */ -static void io_kbuf_mark_free(struct io_ring_ctx *ctx, struct io_buffer_list *bl) -{ - struct io_buf_free *ibf; - - hlist_for_each_entry(ibf, &ctx->io_buf_list, list) { - if (bl->buf_ring == ibf->mem) { - ibf->inuse = 0; - return; - } - } - - /* can't happen... */ - WARN_ON_ONCE(1); -} - static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer_list *bl, unsigned nbufs) { @@ -220,23 +189,18 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx, if (bl->is_buf_ring) { i = bl->buf_ring->tail - bl->head; - if (bl->is_mmap) { - /* - * io_kbuf_list_free() will free the page(s) at - * ->release() time. - */ - io_kbuf_mark_free(ctx, bl); - bl->buf_ring = NULL; - bl->is_mmap = 0; - } else if (bl->buf_nr_pages) { + if (bl->buf_nr_pages) { int j; - for (j = 0; j < bl->buf_nr_pages; j++) - unpin_user_page(bl->buf_pages[j]); - kvfree(bl->buf_pages); - vunmap(bl->buf_ring); - bl->buf_pages = NULL; - bl->buf_nr_pages = 0; + for (j = 0; j < bl->buf_nr_pages; j++) { + if (bl->is_mmap) + put_page(bl->buf_pages[j]); + else + unpin_user_page(bl->buf_pages[j]); + } + io_pages_unmap(bl->buf_ring, &bl->buf_pages, + &bl->buf_nr_pages, false); + bl->is_mmap = 0; } /* make sure it's seen as empty */ INIT_LIST_HEAD(&bl->buf_list); @@ -260,7 +224,7 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx, return i; } -static void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl) +void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl) { if (atomic_dec_and_test(&bl->refs)) { __io_remove_buffers(ctx, bl, -1U); @@ -537,63 +501,18 @@ static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg, return ret; } -/* - * See if we have a suitable region that we can reuse, rather than allocate - * both a new io_buf_free and mem region again. We leave it on the list as - * even a reused entry will need freeing at ring release. - */ -static struct io_buf_free *io_lookup_buf_free_entry(struct io_ring_ctx *ctx, - size_t ring_size) -{ - struct io_buf_free *ibf, *best = NULL; - size_t best_dist; - - hlist_for_each_entry(ibf, &ctx->io_buf_list, list) { - size_t dist; - - if (ibf->inuse || ibf->size < ring_size) - continue; - dist = ibf->size - ring_size; - if (!best || dist < best_dist) { - best = ibf; - if (!dist) - break; - best_dist = dist; - } - } - - return best; -} - static int io_alloc_pbuf_ring(struct io_ring_ctx *ctx, struct io_uring_buf_reg *reg, struct io_buffer_list *bl) { - struct io_buf_free *ibf; size_t ring_size; - void *ptr; ring_size = reg->ring_entries * sizeof(struct io_uring_buf_ring); - /* Reuse existing entry, if we can */ - ibf = io_lookup_buf_free_entry(ctx, ring_size); - if (!ibf) { - ptr = io_mem_alloc(ring_size); - if (IS_ERR(ptr)) - return PTR_ERR(ptr); - - /* Allocate and store deferred free entry */ - ibf = kmalloc(sizeof(*ibf), GFP_KERNEL_ACCOUNT); - if (!ibf) { - io_mem_free(ptr); - return -ENOMEM; - } - ibf->mem = ptr; - ibf->size = ring_size; - hlist_add_head(&ibf->list, &ctx->io_buf_list); - } - ibf->inuse = 1; - bl->buf_ring = ibf->mem; + bl->buf_ring = io_pages_map(&bl->buf_pages, &bl->buf_nr_pages, ring_size); + if (!bl->buf_ring) + return -ENOMEM; + bl->is_buf_ring = 1; bl->is_mmap = 1; return 0; @@ -710,30 +629,50 @@ int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg) return 0; } -void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid) +struct io_buffer_list *io_pbuf_get_bl(struct io_ring_ctx *ctx, + unsigned long bgid) { struct io_buffer_list *bl; + int ret; - bl = __io_buffer_get_list(ctx, bgid); - - if (!bl || !bl->is_mmap) - return NULL; - - return bl->buf_ring; + /* + * We have to be a bit careful here - we're inside mmap and cannot + * grab the uring_lock. This means the buffer_list could be + * simultaneously going away, if someone is trying to be sneaky. + * Look it up under rcu so we now it's not going away, and attempt + * to grab a reference to it. If the ref is already zero, then fail + * the mapping. If successful, we'll drop the reference at at the end. + * This may then safely free the buffer_list (and drop the pages) at + * that point, vm_insert_pages() would've already grabbed the + * necessary vma references. + */ + rcu_read_lock(); + bl = xa_load(&ctx->io_bl_xa, bgid); + /* must be a mmap'able buffer ring and have pages */ + if (bl && bl->is_mmap && bl->buf_nr_pages) + ret = atomic_inc_not_zero(&bl->refs); + rcu_read_unlock(); + + if (!ret) + return ERR_PTR(-EINVAL); + + return bl; } -/* - * Called at or after ->release(), free the mmap'ed buffers that we used - * for memory mapped provided buffer rings. - */ -void io_kbuf_mmap_list_free(struct io_ring_ctx *ctx) +int io_pbuf_mmap(struct file *file, struct vm_area_struct *vma) { - struct io_buf_free *ibf; - struct hlist_node *tmp; + struct io_ring_ctx *ctx = file->private_data; + loff_t pgoff = vma->vm_pgoff << PAGE_SHIFT; + struct io_buffer_list *bl; + int bgid, ret; - hlist_for_each_entry_safe(ibf, tmp, &ctx->io_buf_list, list) { - hlist_del(&ibf->list); - io_mem_free(ibf->mem); - kfree(ibf); - } + bgid = (pgoff & ~IORING_OFF_MMAP_MASK) >> IORING_OFF_PBUF_SHIFT; + + bl = io_pbuf_get_bl(ctx, bgid); + if (IS_ERR(bl)) + return PTR_ERR(bl); + + ret = io_uring_mmap_pages(ctx, vma, bl->buf_pages, bl->buf_nr_pages); + io_put_bl(ctx, bl); + return ret; } diff --git a/io_uring/kbuf.h b/io_uring/kbuf.h index 8b868a1744e2..53c141d9a8b2 100644 --- a/io_uring/kbuf.h +++ b/io_uring/kbuf.h @@ -55,13 +55,14 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg); int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg); int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg); -void io_kbuf_mmap_list_free(struct io_ring_ctx *ctx); - void __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags); bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags); -void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid); +void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl); +struct io_buffer_list *io_pbuf_get_bl(struct io_ring_ctx *ctx, + unsigned long bgid); +int io_pbuf_mmap(struct file *file, struct vm_area_struct *vma); static inline bool io_kbuf_recycle_ring(struct io_kiocb *req) { From patchwork Wed Mar 27 19:13:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13607321 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4279B1514EA for ; Wed, 27 Mar 2024 19:19:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567195; cv=none; b=QOR6T2iYOfNuyZiY8AJEJTlWnmna+1NSTZHlV9s+rFaganoOGnm0nzzoMMVQTNv0/9Yfs47uBAxPuK2aeFjTgOy5c1Lw5GeZ2a7NDwaYlWkSXYMtDlNznLbjntrcdHZHJdNBbSS08KGpc8wCIr6AoGHgJ4MkBa2AH6eh/xLidgw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711567195; c=relaxed/simple; bh=YSpQj8fqX+tbLB6tYf9HPldNaAuc27V0fHVqVAkaBSs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tztLx34AcCFOJ0qa8NchImawZLBkWqWK/6tTS0CipVBINhFXqjFJji0Dpgm4it8Fg4LujUJBivUUA7en15YJv444zKTlvad9DzJ8RxXpcVkfKc7TVhRbWthkL1LWr6u/7QPYmcjGKaO28Q9RcE+NYASN1IIQt9ZUzXDBV4qwoPA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=brCBvRU+; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="brCBvRU+" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6e6c38be762so41841b3a.1 for ; Wed, 27 Mar 2024 12:19:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1711567193; x=1712171993; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bRPtHKt58oVWeh2+NDBEfZ2xFmX8Q/Yl8QC5Ndz+dR8=; b=brCBvRU+tTRwtV9Go1Id/1mBbIjKpAn6ejjlZ2NcWPeeMpoBXs9rXKjRyatWr6Mt6r LpbQ6iOjUjt0/eoc7aAn4cw2daClWUnpiiBdz8AYb9Owi8OGlD342BYLGuUwPKdxMFOH O7Br1jScgaQdupI9Y5ZaVEGly1tESuTDpszKH1DLB78Vybwe+z5e3LNqiwKhuwbBF/hJ eNZVxLNDQzaNBnEnXwboLuM+hplYt/BBOIG8NOdlOqniLQcWYfrXPQV5GYRl8BaqcQ0v M4MT/2OT+437M4OgvxIVHtC93bzIsmfwZLZvs4mjMP6/Qk+34Hqs7Moi/HSeLSEr+Ec/ wOjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567193; x=1712171993; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bRPtHKt58oVWeh2+NDBEfZ2xFmX8Q/Yl8QC5Ndz+dR8=; b=JL+4G4sDHDm1JzFgBDynypFMOCvJW+t/BjizrcKqfQ9FY3Y950CD1jKO/1ym0OfZ4Z l9LbRnCjKA+4fB/SGjW93ijrTstNGVGM6+AR1rjGpXJ9wbVeLiy88PtiApPvjLXUyLxj +VSObliETCCYPf2kmCnDdrLvhXz/Zm6TDawJzJlQD8NWFvw1FgxcrUSQc3951rl91CsV mLTzhGhXnEeTAW+fIKiv7TAFQHLd1LBija5qN4jS0AUyVvwtIYVf6P4xgAeBkp6fIcXu AhNfBmpoer1KZJTAI/uoMAFAelBw7F4zX6JDg0Rh4Ze9LjZj4AfFD+f2BhMbMmh+cCMa vLJA== X-Gm-Message-State: AOJu0YxV16w1/sb2tHPBYdseuOo/Qsl4c94eRhfPnwj9jVnhwxynUzSX CIGKj/MarV7ZUjOj3nzIUnE0SQq936RZAlhx1FfSNhDbbeizepDdb1i3m+vHsM8dQHvsfy5Nm4V 7 X-Google-Smtp-Source: AGHT+IFPA7J3ZmjEoLJpSmrKQxynFA62CCUwF1XAQ+S08zx+FFmTCCYBDcsACOYcPak7ortvwkQlBg== X-Received: by 2002:a05:6a00:2d2a:b0:6ea:7f2e:633 with SMTP id fa42-20020a056a002d2a00b006ea7f2e0633mr836611pfb.2.1711567193024; Wed, 27 Mar 2024 12:19:53 -0700 (PDT) Received: from m2max.thefacebook.com ([2620:10d:c090:600::1:bb1e]) by smtp.gmail.com with ESMTPSA id n2-20020aa79842000000b006e6c3753786sm8278882pfq.41.2024.03.27.12.19.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:19:52 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 10/10] io_uring: use unpin_user_pages() where appropriate Date: Wed, 27 Mar 2024 13:13:45 -0600 Message-ID: <20240327191933.607220-11-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240327191933.607220-1-axboe@kernel.dk> References: <20240327191933.607220-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There are a few cases of open-rolled loops around unpin_user_page(), use the generic helper instead. Signed-off-by: Jens Axboe --- io_uring/io_uring.c | 4 +--- io_uring/kbuf.c | 5 ++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index ff7276699a2c..fe9233958b4a 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2622,13 +2622,11 @@ void io_pages_unmap(void *ptr, struct page ***pages, unsigned short *npages, static void io_pages_free(struct page ***pages, int npages) { struct page **page_array = *pages; - int i; if (!page_array) return; - for (i = 0; i < npages; i++) - unpin_user_page(page_array[i]); + unpin_user_pages(page_array, npages); kvfree(page_array); *pages = NULL; } diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c index 99b349930a1a..3ba576ccb1d9 100644 --- a/io_uring/kbuf.c +++ b/io_uring/kbuf.c @@ -457,8 +457,8 @@ static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg, struct io_buffer_list *bl) { struct io_uring_buf_ring *br = NULL; - int nr_pages, ret, i; struct page **pages; + int nr_pages, ret; pages = io_pin_pages(reg->ring_addr, flex_array_size(br, bufs, reg->ring_entries), @@ -494,8 +494,7 @@ static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg, bl->is_mmap = 0; return 0; error_unpin: - for (i = 0; i < nr_pages; i++) - unpin_user_page(pages[i]); + unpin_user_pages(pages, nr_pages); kvfree(pages); vunmap(br); return ret;