From patchwork Tue Jan 15 02:55:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 10763889 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3B15F6C5 for ; Tue, 15 Jan 2019 02:56:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2D3FB2CAF0 for ; Tue, 15 Jan 2019 02:56:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 211072CB35; Tue, 15 Jan 2019 02:56:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B43B82CB3C for ; Tue, 15 Jan 2019 02:56:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727974AbfAOC4A (ORCPT ); Mon, 14 Jan 2019 21:56:00 -0500 Received: from mail-pg1-f195.google.com ([209.85.215.195]:39030 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727610AbfAOC4A (ORCPT ); Mon, 14 Jan 2019 21:56:00 -0500 Received: by mail-pg1-f195.google.com with SMTP id w6so557707pgl.6 for ; Mon, 14 Jan 2019 18:56:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=VoYrPtRXJ9nQQpUfIFiP2jxPJWS+L1ggj48G/VBnDmk=; b=LJg8fvyMx9ZXqRNN/UyOrzI0pPVIGoGv0PyE8ldZ3O4gDwqsPTiYAJdWNqsrv0owRo jN8w4t/LxaTfiijA1Q9MVmJUp3+SZfoJ6Ko8WN7G0NqmmfSlRKX/bi9BOn9UJ2VmpKcM gRlyoxaFmycrSsKPaupXvmQQ4IP4eAyXQxppTggkfeX1w9Ft8PQ26ZkbqcVNRya+qeQ9 URmZJVZyeEUUAGCeOL3THCPvLoyuw6++/4v/PKun2x8W6GPj/a+mriv7fP5Vsxou1vhQ rzCAimXqIDqZUnczSfWwVNQU3HADo5UAHtzbnZqigdv1q7PqNig8TgEbNR3DuClKiXgc 9Daw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=VoYrPtRXJ9nQQpUfIFiP2jxPJWS+L1ggj48G/VBnDmk=; b=Z8ey6IW7JuZPjr8E3eR/+U/85lusyxczjPK9dIJ0JNTXZDFcRCq1PAV1nnr+sRb34R CFjXPmuSUjz+Kkm3lfsdcNzDA9pOL4TssimBVyxxp0F6nxhoeKeZaxqlNugtgslPbBXK StrUwg59TzSFbCTbZb9bJv0ZfeaBhPvA/EVvF7KrHd2sjFLULdF4fVajlCnvhG73REvU S1+VOpBVWnpefjt3cU43Eia1et+Otw+mnHyQuLI+2kis44wPD5WPMYhd+IaI8UhjfaN3 hPSdUZ4lMqMhmhAD2xEOcnCj6vY2fxeY32/iChXCNVQ0dxsfShaZBiAGjaS38YNYtlH5 FDVA== X-Gm-Message-State: AJcUuke2AI0mmlYokkJbwDpOpHTU12F0TzDJeR8FfiZetWr4T53SYSyp QwCiq6ojgV6E8iX0R3fvQYucxmRZ+5TaIA== X-Google-Smtp-Source: ALg8bN4TQmvTcn37kaEb7ABZtiuctisdY9ll5iS2uXAugW1QYZ2AnC5clRriS+99sJpufM0IGLwsHg== X-Received: by 2002:a62:d206:: with SMTP id c6mr572884pfg.245.1547520959286; Mon, 14 Jan 2019 18:55:59 -0800 (PST) Received: from x1.localdomain (66.29.188.166.static.utbb.net. [66.29.188.166]) by smtp.gmail.com with ESMTPSA id c23sm2415544pfi.83.2019.01.14.18.55.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Jan 2019 18:55:58 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org, linux-aio@kvack.org, linux-block@vger.kernel.org, linux-arch@vger.kernel.org Cc: hch@lst.de, jmoyer@redhat.com, avi@scylladb.com, Jens Axboe Subject: [PATCH 11/16] io_uring: batch io_kiocb allocation Date: Mon, 14 Jan 2019 19:55:26 -0700 Message-Id: <20190115025531.13985-12-axboe@kernel.dk> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190115025531.13985-1-axboe@kernel.dk> References: <20190115025531.13985-1-axboe@kernel.dk> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Similarly to how we use the state->ios_left to know how many references to get to a file, we can use it to allocate the io_kiocb's we need in bulk. Signed-off-by: Jens Axboe --- fs/io_uring.c | 66 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 0d7c44a2d424..d0e4e37592fe 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -140,6 +140,13 @@ struct io_submit_state { struct list_multi req_list; unsigned int req_count; + /* + * io_kiocb alloc cache + */ + void *reqs[IO_IOPOLL_BATCH]; + unsigned int free_reqs; + unsigned int cur_req; + /* * File reference cache */ @@ -244,29 +251,52 @@ static void io_fill_cq_error(struct io_ring_ctx *ctx, struct sqe_submit *s, wake_up(&ctx->wait); } -static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx) +static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs) { + percpu_ref_put_many(&ctx->refs, refs); + + if (waitqueue_active(&ctx->wait)) + wake_up(&ctx->wait); +} + +static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, + struct io_submit_state *state) +{ + gfp_t gfp = GFP_ATOMIC | __GFP_NOWARN; struct io_kiocb *req; if (!percpu_ref_tryget(&ctx->refs)) return NULL; - req = kmem_cache_alloc(req_cachep, GFP_ATOMIC | __GFP_NOWARN); - if (!req) - return NULL; - - req->ctx = ctx; - INIT_LIST_HEAD(&req->list); - req->flags = 0; - return req; -} + if (!state) + req = kmem_cache_alloc(req_cachep, gfp); + else if (!state->free_reqs) { + size_t sz; + int ret; + + sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); + ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, + state->reqs); + if (ret <= 0) + goto out; + state->free_reqs = ret - 1; + state->cur_req = 1; + req = state->reqs[0]; + } else { + req = state->reqs[state->cur_req]; + state->free_reqs--; + state->cur_req++; + } -static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs) -{ - percpu_ref_put_many(&ctx->refs, refs); + if (req) { + req->ctx = ctx; + req->flags = 0; + return req; + } - if (waitqueue_active(&ctx->wait)) - wake_up(&ctx->wait); +out: + io_ring_drop_ctx_refs(ctx, 1); + return NULL; } static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr) @@ -910,7 +940,7 @@ static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s, struct io_kiocb *req; ssize_t ret; - req = io_get_req(ctx); + req = io_get_req(ctx, state); if (unlikely(!req)) return -EAGAIN; @@ -947,6 +977,9 @@ static void io_submit_state_end(struct io_submit_state *state) if (!list_empty(&state->req_list.list)) io_flush_state_reqs(state->ctx, state); io_file_put(state, NULL); + if (state->free_reqs) + kmem_cache_free_bulk(req_cachep, state->free_reqs, + &state->reqs[state->cur_req]); } /* @@ -958,6 +991,7 @@ static void io_submit_state_start(struct io_submit_state *state, state->ctx = ctx; INIT_LIST_HEAD(&state->req_list.list); state->req_count = 0; + state->free_reqs = 0; state->file = NULL; state->ios_left = max_ios; #ifdef CONFIG_BLOCK