From patchwork Thu Mar 14 20:44:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 10853555 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 ABF731874 for ; Thu, 14 Mar 2019 20:44:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9EBB12A650 for ; Thu, 14 Mar 2019 20:44:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 90C5E2A652; Thu, 14 Mar 2019 20:44:44 +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 2B3A82A656 for ; Thu, 14 Mar 2019 20:44:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727853AbfCNUon (ORCPT ); Thu, 14 Mar 2019 16:44:43 -0400 Received: from mail-it1-f195.google.com ([209.85.166.195]:40057 "EHLO mail-it1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727837AbfCNUon (ORCPT ); Thu, 14 Mar 2019 16:44:43 -0400 Received: by mail-it1-f195.google.com with SMTP id l139so7323005ita.5 for ; Thu, 14 Mar 2019 13:44:42 -0700 (PDT) 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=VBY/WCiVRIxbJHK0kuOFwZ092MB3IurzrK4hVX4OtTY=; b=JBpRAYhxAe886ZlTGumenA/bmp0aXfOIQRv7lYKKicY+m4mpSvszGd2WXd8whaex/7 J1C4bnT4YmPQhYQd2YofGQ5OkpMx1mJRMKbGyO/pigiTf5FyIUDSkQSgMr+Qzn8ax8+k tYzk9mrnSp3DfgTSA7CH8QreWOzJ0dKNiQBZ0Vs57ywQLEQK9153lKkHFnxlm6amY6U1 i+n3BuQddvNyBMqLMHB5V+zoNQgYJyoCG2DcPhp1aDlkr4vCYT2C+t07YxRgWjwUeF9p KhbFISCHUOvYQvV7cOtqPQLTOgJzJxwLZk62cILjC+2PmbbOXY+8dtYfNoyoT4cEsDrm dcNQ== 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=VBY/WCiVRIxbJHK0kuOFwZ092MB3IurzrK4hVX4OtTY=; b=T1/lgEG9GnWh0K0ijMXEV8MHiOFNBwpuJpiv3Yo0pGO4kwW/lY8/q6m2iQ4fkCS16H /gOKyOjWEeVnYFxrOk5NyMe0HadafBH1pa01c1A2XqJXEat+LUukitMD9aU00WvpLaPp OPSVE6E29Y68uGCYEKYic5mis26yIB3wDhK7apkG4BdpwW9SrIdUmOnwasBB7hBNwJwW hjG4SHN5iKpvQPP1E2zDNGEf8gKz8Ps/A6cmvU+X5otXdSu98TPONqSBqXDfLTP0Cywj DmwTODBzM0QVm18PQE4m9KgSAYVcfDf4/ZzHscy3ba/Yg+C4xiK/ZNHrkkInXs9yBwKy W3IQ== X-Gm-Message-State: APjAAAWVcsuLOsNGhPtMpuPzBGMj6JDLOq8K1SpvQAXF/Ml22Xxwe7ZU EHl671waX/KmJqpsGGV5fRCrdwE5Khw3SA== X-Google-Smtp-Source: APXvYqz19xrvsclOb7WZ+9n2HEubMwEqy2CZApJhX7+Iikb3e6RLK6cgj0IJz3vB5eoEJitWzMdKSQ== X-Received: by 2002:a02:4084:: with SMTP id n126mr150388jaa.78.1552596281319; Thu, 14 Mar 2019 13:44:41 -0700 (PDT) Received: from x1.thefacebook.com ([216.160.245.98]) by smtp.gmail.com with ESMTPSA id w19sm1820538ita.33.2019.03.14.13.44.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Mar 2019 13:44:40 -0700 (PDT) From: Jens Axboe To: linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org Cc: viro@ZenIV.linux.org.uk, Jens Axboe Subject: [PATCH 1/8] io_uring: use regular request ref counts Date: Thu, 14 Mar 2019 14:44:28 -0600 Message-Id: <20190314204435.7692-2-axboe@kernel.dk> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190314204435.7692-1-axboe@kernel.dk> References: <20190314204435.7692-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 Get rid of the special casing of "normal" requests not having any references to the io_kiocb. We initialize the ref count to 2, one for the submission side, and one or the completion side. Signed-off-by: Jens Axboe --- fs/io_uring.c | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 5d99376d2369..c54af70c72fd 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -411,7 +411,8 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, req->ctx = ctx; req->flags = 0; - refcount_set(&req->refs, 0); + /* one is dropped after submission, the other at completion */ + refcount_set(&req->refs, 2); return req; out: io_ring_drop_ctx_refs(ctx, 1); @@ -429,10 +430,14 @@ static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr) static void io_free_req(struct io_kiocb *req) { - if (!refcount_read(&req->refs) || refcount_dec_and_test(&req->refs)) { - io_ring_drop_ctx_refs(req->ctx, 1); - kmem_cache_free(req_cachep, req); - } + io_ring_drop_ctx_refs(req->ctx, 1); + kmem_cache_free(req_cachep, req); +} + +static void io_put_req(struct io_kiocb *req) +{ + if (refcount_dec_and_test(&req->refs)) + io_free_req(req); } /* @@ -453,7 +458,8 @@ static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, io_cqring_fill_event(ctx, req->user_data, req->error, 0); - reqs[to_free++] = req; + if (refcount_dec_and_test(&req->refs)) + reqs[to_free++] = req; (*nr_events)++; /* @@ -616,7 +622,7 @@ static void io_complete_rw(struct kiocb *kiocb, long res, long res2) io_fput(req); io_cqring_add_event(req->ctx, req->user_data, res, 0); - io_free_req(req); + io_put_req(req); } static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) @@ -1083,7 +1089,7 @@ static int io_nop(struct io_kiocb *req, u64 user_data) io_fput(req); } io_cqring_add_event(ctx, user_data, err, 0); - io_free_req(req); + io_put_req(req); return 0; } @@ -1146,7 +1152,7 @@ static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe, io_fput(req); io_cqring_add_event(req->ctx, sqe->user_data, ret, 0); - io_free_req(req); + io_put_req(req); return 0; } @@ -1204,7 +1210,7 @@ static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe) spin_unlock_irq(&ctx->completion_lock); io_cqring_add_event(req->ctx, sqe->user_data, ret, 0); - io_free_req(req); + io_put_req(req); return 0; } @@ -1212,7 +1218,7 @@ static void io_poll_complete(struct io_kiocb *req, __poll_t mask) { io_cqring_add_event(req->ctx, req->user_data, mangle_poll(mask), 0); io_fput(req); - io_free_req(req); + io_put_req(req); } static void io_poll_complete_work(struct work_struct *work) @@ -1346,9 +1352,6 @@ static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe) INIT_LIST_HEAD(&poll->wait.entry); init_waitqueue_func_entry(&poll->wait, io_poll_wake); - /* one for removal from waitqueue, one for this function */ - refcount_set(&req->refs, 2); - mask = vfs_poll(poll->file, &ipt.pt) & poll->events; if (unlikely(!poll->head)) { /* we did not manage to set up a waitqueue, done */ @@ -1380,13 +1383,12 @@ static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe) * Drop one of our refs to this req, __io_submit_sqe() will * drop the other one since we're returning an error. */ - io_free_req(req); + io_put_req(req); return ipt.error; } if (mask) io_poll_complete(req, mask); - io_free_req(req); return 0; } @@ -1524,10 +1526,13 @@ static void io_sq_wq_submit_work(struct work_struct *work) break; cond_resched(); } while (1); + + /* drop submission reference */ + io_put_req(req); } if (ret) { io_cqring_add_event(ctx, sqe->user_data, ret, 0); - io_free_req(req); + io_put_req(req); } /* async context always use a copy of the sqe */ @@ -1617,6 +1622,7 @@ static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req) static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s, struct io_submit_state *state) { + bool did_submit = true; struct io_kiocb *req; ssize_t ret; @@ -1650,10 +1656,16 @@ static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s, queue_work(ctx->sqo_wq, &req->work); } ret = 0; + did_submit = false; } } + + /* drop submission reference, if we did the submit */ + if (did_submit) + io_put_req(req); + if (ret) - io_free_req(req); + io_put_req(req); return ret; }