From patchwork Thu Jul 7 23:23:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 12910384 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 D0D98C43334 for ; Thu, 7 Jul 2022 23:24:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236058AbiGGXYV (ORCPT ); Thu, 7 Jul 2022 19:24:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236354AbiGGXYU (ORCPT ); Thu, 7 Jul 2022 19:24:20 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D98CC4F198 for ; Thu, 7 Jul 2022 16:24:19 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id o18so19745968pgu.9 for ; Thu, 07 Jul 2022 16:24:19 -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=8TenpyQ7kAr/OpTPiY+oFdS6T5ffBnYy9vk7ymSI9mw=; b=fBARGdOJRaKeY6Uj8JAMYFMJ2wZzY4iSXEbbg/omO0Rezz0GXU68lm/Y7ZeKWE5ZFM mo6Rs+KPkrx6xqcXq6q/sCkgYby2sX3YmJELt0j5Ncg3jORkEwBiyXqDIQhhVdlbsuXd agmwRjNB521zqc2Tbz+xfh/YHlCEac8q8H7aawv8S+em8Hh9qRbyuzxE6cfm5WgeMLah ZZ0WU8uV+/KlAZjTHozGdbvkSo/ZNwj7FSrIWITO9YzZit989TTst/Bf+T8AoThJzv+x 8U/TR0z7XmskPU80/6gXxsqEC/VjTgYlF9WMfJB1e7q7DBC9z/JCTwprFIzcfEtR3znr JO4w== 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=8TenpyQ7kAr/OpTPiY+oFdS6T5ffBnYy9vk7ymSI9mw=; b=4cTlzf+acQdrnf8ANWVFlx0ZJ11RX81C+1HeCEAyWicj2RCNIx0CeX3mu8oAdOwitD rQLIeQyX2OY8TTEkVk5/3V/tL478yCoR3JBCYvqKwZdS4CZC6SPP3TWKu2sjAr6uvfYH HNtJAm1GX1z09bD19cll3o6UGo6Meatgn4cHM2XvKFTYdrNCxjF3lTXt2Jam4f4AzHFi W/sXiwmoD+xpUldjVvHO9kL9smabZtMPgg0EtvkvireCHnGS2OA2ZjfO7KAjhiVTFaue tB8QgOUFZRSRhvkm/XYQH/u29an7HeeNJzRJwOK6NeHvvSzLqOycPfkOtr5W35Syfpso 26WQ== X-Gm-Message-State: AJIora/QzqyIeb0IFdcMlrUqLunHg8NzpnNh4Z71TP3H54Fi6eRPuH+w 0sZ1Xnv6zsp9QKsxdmxv97jsHMzALd/DOQ== X-Google-Smtp-Source: AGRyM1te30gT4WDkJSV2XDPkM0UV+iGzdZu4vUtleQGiFIkXe9Kdw65n/BYYxIzZ8sE1SC3YLMA75g== X-Received: by 2002:a63:a748:0:b0:40c:9a36:ff9a with SMTP id w8-20020a63a748000000b0040c9a36ff9amr453411pgo.545.1657236259119; Thu, 07 Jul 2022 16:24:19 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s65-20020a17090a69c700b001efeb4c813csm94014pjj.13.2022.07.07.16.24.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jul 2022 16:24:18 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: asml.silence@gmail.com, Jens Axboe Subject: [PATCH 1/4] io_uring: move apoll cache to poll.c Date: Thu, 7 Jul 2022 17:23:43 -0600 Message-Id: <20220707232345.54424-2-axboe@kernel.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220707232345.54424-1-axboe@kernel.dk> References: <20220707232345.54424-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org This is where it's used, move the flush handler in there. Signed-off-by: Jens Axboe --- io_uring/io_uring.c | 12 ------------ io_uring/poll.c | 12 ++++++++++++ io_uring/poll.h | 2 ++ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index caf979cd4327..4d1ce58b015e 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2445,18 +2445,6 @@ static void io_req_caches_free(struct io_ring_ctx *ctx) mutex_unlock(&ctx->uring_lock); } -static void io_flush_apoll_cache(struct io_ring_ctx *ctx) -{ - struct async_poll *apoll; - - while (!list_empty(&ctx->apoll_cache)) { - apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, - poll.wait.entry); - list_del(&apoll->poll.wait.entry); - kfree(apoll); - } -} - static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) { io_sq_thread_finish(ctx); diff --git a/io_uring/poll.c b/io_uring/poll.c index 57747d92bba4..f0fe209490d8 100644 --- a/io_uring/poll.c +++ b/io_uring/poll.c @@ -958,3 +958,15 @@ int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) io_req_set_res(req, ret, 0); return IOU_OK; } + +void io_flush_apoll_cache(struct io_ring_ctx *ctx) +{ + struct async_poll *apoll; + + while (!list_empty(&ctx->apoll_cache)) { + apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, + poll.wait.entry); + list_del(&apoll->poll.wait.entry); + kfree(apoll); + } +} diff --git a/io_uring/poll.h b/io_uring/poll.h index c40673d7da01..95f192c7babb 100644 --- a/io_uring/poll.h +++ b/io_uring/poll.h @@ -30,3 +30,5 @@ int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd, int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags); bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, bool cancel_all); + +void io_flush_apoll_cache(struct io_ring_ctx *ctx); From patchwork Thu Jul 7 23:23:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 12910386 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 0AA38CCA479 for ; Thu, 7 Jul 2022 23:24:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236535AbiGGXYW (ORCPT ); Thu, 7 Jul 2022 19:24:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236354AbiGGXYV (ORCPT ); Thu, 7 Jul 2022 19:24:21 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA4C724BE7 for ; Thu, 7 Jul 2022 16:24:20 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id s27so20899846pga.13 for ; Thu, 07 Jul 2022 16:24:20 -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=Il7y05J9bjSxLZ5WBWnYpwNQ/5889v50bfC9zbQfEgo=; b=LGcrEGNnSoGfgRoXn4Hw40WxleUoeRf59alefGMN0dIb5w4p3TTF1CHrFcbEIe5Ndj TxKsjdTjSZ2TIzKZhTJRHLTMPvUZ2BgF15NXCkTpD4UZfOnA44QfXrNlsK+dBa0IDu/L RchkaVJ4kxvxqZTwodBavAOP9KsK7nb7IwJxcs54h8gxbSq78h4pQitS8IghdbIFFxX8 H0S6l08B088GKh3fw53fzaXgnNaRidJyJ9natOrmKVVOKNumDtwCvCV9oxcHS8ngoTL/ 4P1pbP6MiVv225rRLZ+FKg8hCz1RzBjI6pWWDoiDcG59dDHIx7QElmGqflnKxsImKZvs /Hzw== 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=Il7y05J9bjSxLZ5WBWnYpwNQ/5889v50bfC9zbQfEgo=; b=SOvY9PhzFGiAgDxRqi49fRDFwfd8GwQE1DFD0fKkf2oiHH5F/4awl61BY/fgDVdmXr PEmBtduQUUlDvRZvIuy9xOcPl9xTemv+EXWhk5YK1bKvbAFJloIDFG8Ih/sIK/RRYu8H tzhtQObIJESSlHPQS3VgKkSbdHAvbGMeybL55BuqSw/7i0xEKFJ2Qj5dIvc5WS+e/n2L SezhDVtVC5RQvGajFt9evHAFAAaxBiS05DaEVgSRebv5g9bbfeS61VMQG0KIEcIuvSb8 Hei/L4lKCs5TlyhVerJq1bdM41+ojK4Q63y94+Dn5H5olWnZSUFpmb8N/28AHUXE1IAf MhVQ== X-Gm-Message-State: AJIora/6qv/MX1Myk1WrHkGvKw+Ls93CwQ7wvVhfBQkWhAJoNWQSD05v kCsk3MDqxaew4JqV2Js36YeeGNlDneXS9Q== X-Google-Smtp-Source: AGRyM1t3XX1PmtJxkMECwqNQffcCuYD9xgpDj6qm7yPcgH/tuKN5ADx61uwZpdkzDctRAC7gzGqV5A== X-Received: by 2002:a63:1607:0:b0:412:8fc0:756b with SMTP id w7-20020a631607000000b004128fc0756bmr511022pgl.142.1657236260051; Thu, 07 Jul 2022 16:24:20 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s65-20020a17090a69c700b001efeb4c813csm94014pjj.13.2022.07.07.16.24.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jul 2022 16:24:19 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: asml.silence@gmail.com, Jens Axboe Subject: [PATCH 2/4] io_uring: add abstraction around apoll cache Date: Thu, 7 Jul 2022 17:23:44 -0600 Message-Id: <20220707232345.54424-3-axboe@kernel.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220707232345.54424-1-axboe@kernel.dk> References: <20220707232345.54424-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org In preparation for adding limits, and one more user, abstract out the core bits of the allocation+free cache. Signed-off-by: Jens Axboe --- include/linux/io_uring_types.h | 6 +++++- io_uring/alloc_cache.h | 4 ++++ io_uring/io_uring.c | 7 ++++--- io_uring/poll.c | 16 ++++++++-------- io_uring/poll.h | 5 ++++- 5 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 io_uring/alloc_cache.h diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index 26ef11e978d4..b548da03b563 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -158,6 +158,10 @@ struct io_ev_fd { struct rcu_head rcu; }; +struct io_alloc_cache { + struct hlist_head list; +}; + struct io_ring_ctx { /* const or read-mostly hot data */ struct { @@ -216,7 +220,7 @@ struct io_ring_ctx { struct io_hash_table cancel_table_locked; struct list_head cq_overflow_list; - struct list_head apoll_cache; + struct io_alloc_cache apoll_cache; struct xarray personalities; u32 pers_next; } ____cacheline_aligned_in_smp; diff --git a/io_uring/alloc_cache.h b/io_uring/alloc_cache.h new file mode 100644 index 000000000000..49ac6ae237ef --- /dev/null +++ b/io_uring/alloc_cache.h @@ -0,0 +1,4 @@ +static inline void io_alloc_cache_init(struct io_alloc_cache *cache) +{ + INIT_HLIST_HEAD(&cache->list); +} diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 4d1ce58b015e..3b9033c401bf 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -92,6 +92,7 @@ #include "timeout.h" #include "poll.h" +#include "alloc_cache.h" #define IORING_MAX_ENTRIES 32768 #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) @@ -295,7 +296,7 @@ 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_LIST_HEAD(&ctx->apoll_cache); + io_alloc_cache_init(&ctx->apoll_cache); init_completion(&ctx->ref_comp); xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); mutex_init(&ctx->uring_lock); @@ -1180,8 +1181,8 @@ void io_free_batch_list(struct io_ring_ctx *ctx, struct io_wq_work_node *node) if (apoll->double_poll) kfree(apoll->double_poll); - list_add(&apoll->poll.wait.entry, - &ctx->apoll_cache); + hlist_add_head(&apoll->cache_list, + &ctx->apoll_cache.list); req->flags &= ~REQ_F_POLLED; } if (req->flags & IO_REQ_LINK_FLAGS) diff --git a/io_uring/poll.c b/io_uring/poll.c index f0fe209490d8..f3aae3cc6501 100644 --- a/io_uring/poll.c +++ b/io_uring/poll.c @@ -589,10 +589,10 @@ static struct async_poll *io_req_alloc_apoll(struct io_kiocb *req, apoll = req->apoll; kfree(apoll->double_poll); } else if (!(issue_flags & IO_URING_F_UNLOCKED) && - !list_empty(&ctx->apoll_cache)) { - apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, - poll.wait.entry); - list_del_init(&apoll->poll.wait.entry); + !hlist_empty(&ctx->apoll_cache.list)) { + apoll = hlist_entry(ctx->apoll_cache.list.first, + struct async_poll, cache_list); + hlist_del(&apoll->cache_list); } else { apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); if (unlikely(!apoll)) @@ -963,10 +963,10 @@ void io_flush_apoll_cache(struct io_ring_ctx *ctx) { struct async_poll *apoll; - while (!list_empty(&ctx->apoll_cache)) { - apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, - poll.wait.entry); - list_del(&apoll->poll.wait.entry); + while (!hlist_empty(&ctx->apoll_cache.list)) { + apoll = hlist_entry(ctx->apoll_cache.list.first, + struct async_poll, cache_list); + hlist_del(&apoll->cache_list); kfree(apoll); } } diff --git a/io_uring/poll.h b/io_uring/poll.h index 95f192c7babb..cb528f8ef203 100644 --- a/io_uring/poll.h +++ b/io_uring/poll.h @@ -14,7 +14,10 @@ struct io_poll { }; struct async_poll { - struct io_poll poll; + union { + struct io_poll poll; + struct hlist_node cache_list; + }; struct io_poll *double_poll; }; From patchwork Thu Jul 7 23:23:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 12910385 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 643EACCA482 for ; Thu, 7 Jul 2022 23:24:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236354AbiGGXYW (ORCPT ); Thu, 7 Jul 2022 19:24:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236491AbiGGXYW (ORCPT ); Thu, 7 Jul 2022 19:24:22 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9895E313B0 for ; Thu, 7 Jul 2022 16:24:21 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id 5so9081638plk.9 for ; Thu, 07 Jul 2022 16:24:21 -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=zaGUxvs10Ulhn93ZjMjOX9W/ckj62/WTCRGEn6oMv7I=; b=7mAgbMNxUT+U/28M4ozadcpFyT6ozoPs0vaONbhkb6QiDlcg0/LjkFZQXQYbb25J1p fIBDFKIkPk3uoqbV6/rY0LFUY04Z62yGtv8KPXlT6qeE8B06Mx1g4qq98abg3+1fzGEO uLL5a7NxiW77q0pmwuJvnTFoHIF3RG8tsMWrcBnA0BSbAPz0Y81ADrVkeJH9LuaVRtxS h3wVPgkSbGz/NExUzgEc3ZaRn1cWJGk6NRHt0kFoUcU2wjVI0may7MMGIc2mMF+45K2a dv033fjTOgaJgrB9Une6EdfUSO0tysqY07D6YjZloiPaajSJpWzgquXUHPlURiR2aZ13 W2QA== 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=zaGUxvs10Ulhn93ZjMjOX9W/ckj62/WTCRGEn6oMv7I=; b=Onue4rfnIfEGxB7OTpNlmgW5ySFjQwZrihJK5l6bf0pPC+CN7q0/gWMXCU8WczRyPX yQ7c671WSJSN7E8OTs0gpjjMZUng7o3haY4B6y2MF7XKu/vJGRUPMWSLihFn/qvcOLZp PYXtSwRlLm0KpPxkROY9xJsxR0/5MbuSbCP874UBsdosB2xOtqIhkd8u2SLqlU0fJ6GE x9V9+EYwBxVTeu/nlyjxPFIlgzzmq3UCtm7nKyNTJur3f+TEHn8EdfZpVlhXPCotctua Hj8gzaU17KK4KHkwAUC8zsU+LZ5AiueVxcIK4mf4rW1n2f6waJLjzuSHLdW9DjYOsdvk I73g== X-Gm-Message-State: AJIora8aJD/F4x8X+nit7DhZ7ZIwgk4UXUJmvxB6RF6nSanzwbcqJqPC 4ga4TfZlKvHXrQlqPitmdfdZWcbYcTK+Hg== X-Google-Smtp-Source: AGRyM1sQQPT65Z9vOWDjdPaX3NHA2ru7exYW2zo0z3uL7QqF3+L6vlcDNNTGWqYC6atDYcci7JBUAA== X-Received: by 2002:a17:90b:4acb:b0:1ed:fef:5657 with SMTP id mh11-20020a17090b4acb00b001ed0fef5657mr7852450pjb.142.1657236260963; Thu, 07 Jul 2022 16:24:20 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s65-20020a17090a69c700b001efeb4c813csm94014pjj.13.2022.07.07.16.24.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jul 2022 16:24:20 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: asml.silence@gmail.com, Jens Axboe Subject: [PATCH 3/4] io_uring: impose max limit on apoll cache Date: Thu, 7 Jul 2022 17:23:45 -0600 Message-Id: <20220707232345.54424-4-axboe@kernel.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220707232345.54424-1-axboe@kernel.dk> References: <20220707232345.54424-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org Caches like this tend to grow to the peak size, and then never get any smaller. Impose a max limit on the size, to prevent it from growing too big. A somewhat randomly chosen 512 is the max size we'll allow the cache to get. If a batch of frees come in and would bring it over that, we simply start kfree'ing the surplus. Signed-off-by: Jens Axboe --- include/linux/io_uring_types.h | 1 + io_uring/alloc_cache.h | 15 +++++++++++++++ io_uring/io_uring.c | 8 ++++++-- io_uring/poll.c | 2 ++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index b548da03b563..bf8f95332eda 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -160,6 +160,7 @@ struct io_ev_fd { struct io_alloc_cache { struct hlist_head list; + unsigned int nr_cached; }; struct io_ring_ctx { diff --git a/io_uring/alloc_cache.h b/io_uring/alloc_cache.h index 49ac6ae237ef..0e64030f1ae0 100644 --- a/io_uring/alloc_cache.h +++ b/io_uring/alloc_cache.h @@ -1,4 +1,19 @@ +/* + * Don't allow the cache to grow beyond this size. + */ +#define IO_ALLOC_CACHE_MAX 512 + +static inline bool io_alloc_cache_store(struct io_alloc_cache *cache) +{ + if (cache->nr_cached < IO_ALLOC_CACHE_MAX) { + cache->nr_cached++; + return true; + } + return false; +} + static inline void io_alloc_cache_init(struct io_alloc_cache *cache) { INIT_HLIST_HEAD(&cache->list); + cache->nr_cached = 0; } diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 3b9033c401bf..b5098773d924 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -1181,8 +1181,12 @@ void io_free_batch_list(struct io_ring_ctx *ctx, struct io_wq_work_node *node) if (apoll->double_poll) kfree(apoll->double_poll); - hlist_add_head(&apoll->cache_list, - &ctx->apoll_cache.list); + if (io_alloc_cache_store(&ctx->apoll_cache)) { + hlist_add_head(&apoll->cache_list, + &ctx->apoll_cache.list); + } else { + kfree(apoll); + } req->flags &= ~REQ_F_POLLED; } if (req->flags & IO_REQ_LINK_FLAGS) diff --git a/io_uring/poll.c b/io_uring/poll.c index f3aae3cc6501..cc49160975cb 100644 --- a/io_uring/poll.c +++ b/io_uring/poll.c @@ -593,6 +593,7 @@ static struct async_poll *io_req_alloc_apoll(struct io_kiocb *req, apoll = hlist_entry(ctx->apoll_cache.list.first, struct async_poll, cache_list); hlist_del(&apoll->cache_list); + ctx->apoll_cache.nr_cached--; } else { apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); if (unlikely(!apoll)) @@ -969,4 +970,5 @@ void io_flush_apoll_cache(struct io_ring_ctx *ctx) hlist_del(&apoll->cache_list); kfree(apoll); } + ctx->apoll_cache.nr_cached = 0; } From patchwork Thu Jul 7 23:23:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 12910387 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 031EFC43334 for ; Thu, 7 Jul 2022 23:24:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236588AbiGGXYY (ORCPT ); Thu, 7 Jul 2022 19:24:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236553AbiGGXYX (ORCPT ); Thu, 7 Jul 2022 19:24:23 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B567E24BE7 for ; Thu, 7 Jul 2022 16:24:22 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id r22so14030990pgr.2 for ; Thu, 07 Jul 2022 16:24:22 -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=JGIYQNoCNQyFDgOi5BBnQH0Ka82LgrDzI6+kpC1AjqQ=; b=iSThwNZ1znE657nL0l7zklqrL6vZLXqO7pFuAKMMo/ZaqCHnsxzMAVenIwataTRaZs QD3Z9u0ActDe4Nq6LY6f685uRwVNjIIgKnJXOlazpgXLfy7lrOEHbqPJajD6NkWL8hwT g14wEqfPpvFCtRa2Nj1ZjnPZtt4oWuBp8sNcm2sb+MNNeNhho6TohlhOF/bZhM5K1bIR bHdlOwGfbUS97I7ndR/POUjXwlnEUdvv23VaI9lzzXxmEE5VIoN3m65QEbmSXhbJtRyS Yq4S5ZjZJdnzOYIN1Tb83VeFUchzcsHOqlE2OzTPgLpOMDXCAIYfqa/SoUHacOIAcDHE yFow== 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=JGIYQNoCNQyFDgOi5BBnQH0Ka82LgrDzI6+kpC1AjqQ=; b=X7nqVtgYpblP7wvHJADxVBtJ0i8MoHZcn4nfOxEY/onD7DtZj4GSOkUX0pKfylKOVZ FTjRcsQau4NGiJ+zFjCVi/4C0jMORYWUmcmiL0QmujFJGlERmLwLafWtg6MQsSEc/zQh JxrIU9fUIdgT5JZx2wI2xESSpByDZIEPW6FhX1XaRL0wuaOJld1Azxo9eGfLjRxwNSKN SNihpc0+J/pRlCOP3Ijh1Sr6zP0s8N8SDolYzXaJAUdxFQRcQ0t5V9rL0ihzo881wk7G tv7WQOLdxhwZ+aNFz87JSBdyQgcw4IoNkp70OJk4dk7M2mxvZiN6+X6cohjDxIvhIX46 C0rg== X-Gm-Message-State: AJIora+P40/H3HES3TCXbeYpmFfhz6OW0Qyi5/YhDN3UqKiYM1EzOCjp jxWWb1klIU7DpKOn6oytOezvV3Ecyu//mQ== X-Google-Smtp-Source: AGRyM1sUyzJKZoFDrFmPszUhyZ1rbKynBtCZ1CGc+DQBFLH/mE/yD7Oy/QTuyBQRy1xbeRwz8Yc3/Q== X-Received: by 2002:a63:9701:0:b0:40c:a588:b488 with SMTP id n1-20020a639701000000b0040ca588b488mr488060pge.303.1657236261784; Thu, 07 Jul 2022 16:24:21 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s65-20020a17090a69c700b001efeb4c813csm94014pjj.13.2022.07.07.16.24.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jul 2022 16:24:21 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: asml.silence@gmail.com, Jens Axboe Subject: [PATCH 4/4] io_uring: add netmsg cache Date: Thu, 7 Jul 2022 17:23:46 -0600 Message-Id: <20220707232345.54424-5-axboe@kernel.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220707232345.54424-1-axboe@kernel.dk> References: <20220707232345.54424-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org For recvmsg/sendmsg, if they don't complete inline, we currently need to allocate a struct io_async_msghdr for each request. This is a somewhat large struct. Hook up sendmsg/recvmsg to use the io_alloc_cache. This reduces the alloc + free overhead considerably, yielding 4-5% of extra performance running netbench. Signed-off-by: Jens Axboe --- include/linux/io_uring_types.h | 6 ++- io_uring/io_uring.c | 3 ++ io_uring/net.c | 73 +++++++++++++++++++++++++++++----- io_uring/net.h | 11 ++++- 4 files changed, 81 insertions(+), 12 deletions(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index bf8f95332eda..d54b8b7e0746 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -222,8 +222,7 @@ struct io_ring_ctx { struct io_hash_table cancel_table_locked; struct list_head cq_overflow_list; struct io_alloc_cache apoll_cache; - struct xarray personalities; - u32 pers_next; + struct io_alloc_cache netmsg_cache; } ____cacheline_aligned_in_smp; /* IRQ completion list, under ->completion_lock */ @@ -241,6 +240,9 @@ struct io_ring_ctx { unsigned int file_alloc_start; unsigned int file_alloc_end; + struct xarray personalities; + u32 pers_next; + struct { /* * We cache a range of free CQEs we can use, once exhausted it diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index b5098773d924..32110c5b4059 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -89,6 +89,7 @@ #include "kbuf.h" #include "rsrc.h" #include "cancel.h" +#include "net.h" #include "timeout.h" #include "poll.h" @@ -297,6 +298,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) INIT_LIST_HEAD(&ctx->cq_overflow_list); INIT_LIST_HEAD(&ctx->io_buffers_cache); io_alloc_cache_init(&ctx->apoll_cache); + io_alloc_cache_init(&ctx->netmsg_cache); init_completion(&ctx->ref_comp); xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); mutex_init(&ctx->uring_lock); @@ -2473,6 +2475,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) __io_cqring_overflow_flush(ctx, true); io_eventfd_unregister(ctx); io_flush_apoll_cache(ctx); + io_flush_netmsg_cache(ctx); mutex_unlock(&ctx->uring_lock); io_destroy_buffers(ctx); if (ctx->sq_creds) diff --git a/io_uring/net.c b/io_uring/net.c index 6679069eeef1..ba7e94ff287c 100644 --- a/io_uring/net.c +++ b/io_uring/net.c @@ -12,6 +12,7 @@ #include "io_uring.h" #include "kbuf.h" +#include "alloc_cache.h" #include "net.h" #if defined(CONFIG_NET) @@ -97,18 +98,57 @@ static bool io_net_retry(struct socket *sock, int flags) return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET; } +static void io_netmsg_recycle(struct io_kiocb *req, unsigned int issue_flags) +{ + struct io_async_msghdr *hdr = req->async_data; + + if (!hdr || issue_flags & IO_URING_F_UNLOCKED) + return; + + if (io_alloc_cache_store(&req->ctx->netmsg_cache)) { + hlist_add_head(&hdr->cache_list, &req->ctx->netmsg_cache.list); + req->async_data = NULL; + req->flags &= ~REQ_F_ASYNC_DATA; + } +} + +static struct io_async_msghdr *io_recvmsg_alloc_async(struct io_kiocb *req, + unsigned int issue_flags) +{ + struct io_ring_ctx *ctx = req->ctx; + + if (!(issue_flags & IO_URING_F_UNLOCKED) && + !hlist_empty(&ctx->netmsg_cache.list)) { + struct io_async_msghdr *hdr; + + hdr = hlist_entry(ctx->netmsg_cache.list.first, + struct io_async_msghdr, cache_list); + ctx->netmsg_cache.nr_cached--; + hlist_del(&hdr->cache_list); + req->flags |= REQ_F_ASYNC_DATA; + req->async_data = hdr; + return hdr; + } + + if (!io_alloc_async_data(req)) + return req->async_data; + + return NULL; +} + static int io_setup_async_msg(struct io_kiocb *req, - struct io_async_msghdr *kmsg) + struct io_async_msghdr *kmsg, + unsigned int issue_flags) { struct io_async_msghdr *async_msg = req->async_data; if (async_msg) return -EAGAIN; - if (io_alloc_async_data(req)) { + async_msg = io_recvmsg_alloc_async(req, issue_flags); + if (!async_msg) { kfree(kmsg->free_iov); return -ENOMEM; } - async_msg = req->async_data; req->flags |= REQ_F_NEED_CLEANUP; memcpy(async_msg, kmsg, sizeof(*kmsg)); async_msg->msg.msg_name = &async_msg->addr; @@ -195,7 +235,7 @@ int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) if (!(req->flags & REQ_F_POLLED) && (sr->flags & IORING_RECVSEND_POLL_FIRST)) - return io_setup_async_msg(req, kmsg); + return io_setup_async_msg(req, kmsg, issue_flags); flags = sr->msg_flags; if (issue_flags & IO_URING_F_NONBLOCK) @@ -207,13 +247,13 @@ int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) if (ret < min_ret) { if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) - return io_setup_async_msg(req, kmsg); + return io_setup_async_msg(req, kmsg, issue_flags); if (ret == -ERESTARTSYS) ret = -EINTR; if (ret > 0 && io_net_retry(sock, flags)) { sr->done_io += ret; req->flags |= REQ_F_PARTIAL_IO; - return io_setup_async_msg(req, kmsg); + return io_setup_async_msg(req, kmsg, issue_flags); } req_set_fail(req); } @@ -221,6 +261,7 @@ int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) if (kmsg->free_iov) kfree(kmsg->free_iov); req->flags &= ~REQ_F_NEED_CLEANUP; + io_netmsg_recycle(req, issue_flags); if (ret >= 0) ret += sr->done_io; else if (sr->done_io) @@ -495,7 +536,7 @@ int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) if (!(req->flags & REQ_F_POLLED) && (sr->flags & IORING_RECVSEND_POLL_FIRST)) - return io_setup_async_msg(req, kmsg); + return io_setup_async_msg(req, kmsg, issue_flags); if (io_do_buffer_select(req)) { void __user *buf; @@ -519,13 +560,13 @@ int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) ret = __sys_recvmsg_sock(sock, &kmsg->msg, sr->umsg, kmsg->uaddr, flags); if (ret < min_ret) { if (ret == -EAGAIN && force_nonblock) - return io_setup_async_msg(req, kmsg); + return io_setup_async_msg(req, kmsg, issue_flags); if (ret == -ERESTARTSYS) ret = -EINTR; if (ret > 0 && io_net_retry(sock, flags)) { sr->done_io += ret; req->flags |= REQ_F_PARTIAL_IO; - return io_setup_async_msg(req, kmsg); + return io_setup_async_msg(req, kmsg, issue_flags); } req_set_fail(req); } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { @@ -535,6 +576,7 @@ int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) /* fast path, check for non-NULL to avoid function call */ if (kmsg->free_iov) kfree(kmsg->free_iov); + io_netmsg_recycle(req, issue_flags); req->flags &= ~REQ_F_NEED_CLEANUP; if (ret > 0) ret += sr->done_io; @@ -848,4 +890,17 @@ int io_connect(struct io_kiocb *req, unsigned int issue_flags) io_req_set_res(req, ret, 0); return IOU_OK; } + +void io_flush_netmsg_cache(struct io_ring_ctx *ctx) +{ + while (!hlist_empty(&ctx->netmsg_cache.list)) { + struct io_async_msghdr *hdr; + + hdr = hlist_entry(ctx->netmsg_cache.list.first, + struct io_async_msghdr, cache_list); + hlist_del(&hdr->cache_list); + kfree(hdr); + } + ctx->netmsg_cache.nr_cached = 0; +} #endif diff --git a/io_uring/net.h b/io_uring/net.h index 81d71d164770..576efb602c7f 100644 --- a/io_uring/net.h +++ b/io_uring/net.h @@ -5,7 +5,10 @@ #if defined(CONFIG_NET) struct io_async_msghdr { - struct iovec fast_iov[UIO_FASTIOV]; + union { + struct iovec fast_iov[UIO_FASTIOV]; + struct hlist_node cache_list; + }; /* points to an allocated iov, if NULL we use fast_iov instead */ struct iovec *free_iov; struct sockaddr __user *uaddr; @@ -40,4 +43,10 @@ int io_socket(struct io_kiocb *req, unsigned int issue_flags); int io_connect_prep_async(struct io_kiocb *req); int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); int io_connect(struct io_kiocb *req, unsigned int issue_flags); + +void io_flush_netmsg_cache(struct io_ring_ctx *ctx); +#else +static inline void io_flush_netmsg_cache(struct io_ring_ctx *ctx) +{ +} #endif