From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9385C4332F for ; Sun, 1 May 2022 20:57:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354840AbiEAVAn (ORCPT ); Sun, 1 May 2022 17:00:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354856AbiEAVAj (ORCPT ); Sun, 1 May 2022 17:00:39 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 332B72B27A for ; Sun, 1 May 2022 13:57:12 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id k14so10405113pga.0 for ; Sun, 01 May 2022 13:57:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8xrUl8vq9UAaO0ej1+ezSFhUbjubgwISv0VHwCw1GtM=; b=gwYI/uUWf+wTCalfzPg+qjqviIU9CGTwlajKraUgT9vQEwZHHVKVFEi2HioLh5Ee4q kzCxY5EeU3HjGurqnfOa0jLLZ7lS36YjI+8F8nE2mZQYCOHfqnKv8wqGTuXkW1FIBRul T8xPorcR5aDTUjSEztmlEdYzFYkE4SaIPalPnO4NwNCocRPvRaHD1hGADG0yUR9BzEpj KY7ZvUsygFAiIKXILY5QKHMMR9rLCIxQbSfHe4Msj6eMpQ1wtFUQZzsl1zIZ8seqIfFR 8s6yJDnh9UEQGWofIVxNukLOTuWl5Z/1DbY72MMaH5IBuIRosH8p0RdNKkeh0wt23fj7 s6mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8xrUl8vq9UAaO0ej1+ezSFhUbjubgwISv0VHwCw1GtM=; b=ymrZTrJe3lGNQXH4TGyvcAc+IVkYsulnSWYejfcErT/FOemr9SvI82TimeCyjySI1N 5oVtZj1yI6Nf66kH2B44p6OQkkn2ul+VJrXArjvmpG0br2NFo5VP1tZ2c2dAKynesnDX crHvL0iNUmyJpJsFhdHp6jPRx7Egnj4QAgMM/lBkWICzfIeQHtqnDHFF9TkCNl3NcUBc wYYXC9r6mrMlA4HFpMqNOl2C+Qt3pR03jjOhy3ln1pUsVkLp2PCuFkzsIzdXnqZfTIrK jpjn2XSe2/TrmYWKQ4etKAp6dOVVefYnP3z0ZjROcUdXa5z53zrpjH1UQZK1OtjSt+yy uHTg== X-Gm-Message-State: AOAM532HIT+Qgy60XdOFx3CuCwkbUQlymocUdyO6f8TNJxHbxojc7rdp K+gCwiD8EOpFo34b+fa+ht7sm1O5vFcVnw== X-Google-Smtp-Source: ABdhPJySc90+6RKP5NRgNZSrfzWxxNyWWbFR/HAHOSk3TAVFduaXRFdlFFjzj8ldgBVjCSPe3+gZZQ== X-Received: by 2002:aa7:9472:0:b0:50d:cc22:5269 with SMTP id t18-20020aa79472000000b0050dcc225269mr7469403pfq.58.1651438631439; Sun, 01 May 2022 13:57:11 -0700 (PDT) Received: from localhost.localdomain (cpe-72-132-29-68.dc.res.rr.com. [72.132.29.68]) by smtp.gmail.com with ESMTPSA id l8-20020a17090270c800b0015e8d4eb1e9sm1894013plt.51.2022.05.01.13.57.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 May 2022 13:57:11 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: asml.silence@gmail.com, Jens Axboe Subject: [PATCH 15/16] io_uring: add io_pin_pages() helper Date: Sun, 1 May 2022 14:56:52 -0600 Message-Id: <20220501205653.15775-16-axboe@kernel.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220501205653.15775-1-axboe@kernel.dk> References: <20220501205653.15775-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org Abstract this out from io_sqe_buffer_register() so we can use it elsewhere too without duplicating this code. No intended functional changes in this patch. Signed-off-by: Jens Axboe --- fs/io_uring.c | 77 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 27 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 850125c02c9d..505c1d8cad30 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -10193,30 +10193,18 @@ static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, return ret; } -static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, - struct io_mapped_ubuf **pimu, - struct page **last_hpage) +static struct page **io_pin_pages(unsigned long ubuf, unsigned long len, + int *npages) { - struct io_mapped_ubuf *imu = NULL; + unsigned long start, end, nr_pages; struct vm_area_struct **vmas = NULL; struct page **pages = NULL; - unsigned long off, start, end, ubuf; - size_t size; - int ret, pret, nr_pages, i; - - if (!iov->iov_base) { - *pimu = ctx->dummy_ubuf; - return 0; - } + int i, pret, ret = -ENOMEM; - ubuf = (unsigned long) iov->iov_base; - end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; + end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT; start = ubuf >> PAGE_SHIFT; nr_pages = end - start; - *pimu = NULL; - ret = -ENOMEM; - pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); if (!pages) goto done; @@ -10226,10 +10214,6 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, if (!vmas) goto done; - imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); - if (!imu) - goto done; - ret = 0; mmap_read_lock(current->mm); pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, @@ -10247,6 +10231,7 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, break; } } + *npages = nr_pages; } else { ret = pret < 0 ? pret : -EFAULT; } @@ -10260,14 +10245,53 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, unpin_user_pages(pages, pret); goto done; } + ret = 0; +done: + kvfree(vmas); + if (ret < 0) { + kvfree(pages); + pages = ERR_PTR(ret); + } + return pages; +} - ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); +static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, + struct io_mapped_ubuf **pimu, + struct page **last_hpage) +{ + struct io_mapped_ubuf *imu = NULL; + struct page **pages = NULL; + unsigned long off; + size_t size; + int ret, nr_pages, i; + + if (!iov->iov_base) { + *pimu = ctx->dummy_ubuf; + return 0; + } + + *pimu = NULL; + ret = -ENOMEM; + + pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len, + &nr_pages); + if (IS_ERR(pages)) { + ret = PTR_ERR(pages); + pages = NULL; + goto done; + } + + imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); + if (!imu) + goto done; + + ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage); if (ret) { - unpin_user_pages(pages, pret); + unpin_user_pages(pages, nr_pages); goto done; } - off = ubuf & ~PAGE_MASK; + off = (unsigned long) iov->iov_base & ~PAGE_MASK; size = iov->iov_len; for (i = 0; i < nr_pages; i++) { size_t vec_len; @@ -10280,8 +10304,8 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, size -= vec_len; } /* store original address for later verification */ - imu->ubuf = ubuf; - imu->ubuf_end = ubuf + iov->iov_len; + imu->ubuf = (unsigned long) iov->iov_base; + imu->ubuf_end = imu->ubuf + iov->iov_len; imu->nr_bvecs = nr_pages; *pimu = imu; ret = 0; @@ -10289,7 +10313,6 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, if (ret) kvfree(imu); kvfree(pages); - kvfree(vmas); return ret; } -- 2.35.1