From patchwork Tue Oct 18 19:50:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 4308 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp2134149wrs; Tue, 18 Oct 2022 12:55:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5tesnaa4n6VbWVhOkabblJJYVGXfNauN7ECQMiFv91kPc0LnnLNI4E98eUH1YgRySJYZ4z X-Received: by 2002:a17:907:86a2:b0:791:910e:cce4 with SMTP id qa34-20020a17090786a200b00791910ecce4mr3878285ejc.36.1666122951588; Tue, 18 Oct 2022 12:55:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666122951; cv=none; d=google.com; s=arc-20160816; b=G5/hAGlsK0gDYQJUb5go7Yq2u3Oz0gcUIW2iXgrtOZfcwGkmTmUGI4dEAhc44KSglO 6H1pOU17aAiKz9QBh/srPuHfwpTtQYI+8B1hP5vBP/ts8PxWRJAGxVQG8MX4stH+cQsN FlUFCSOu7CfNtLtARbyyFGpCefQGlmncJmSkFYYpp4dGQQJpVPlH2bTblbTXnWWbKS1K L+eD5gqXunbt8Cpcjr38kwac8pi6aanwBEsJ8EV3kdrylgRJ9c8MSckE+BQ6HVzCcldz dqf/DnCDeJYCqQhmmpARk+q3+LdnJUrt5u17qfRqQh7NYne3YhIrRTjSgX8Rs/4yHrea INxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=F/aQ8vmh1jZL17zaP9aQue+c8W/pbna/0wkMpy2D0Vo=; b=oaHYWsDdML4DAZLEWOkb7CMCs90Nl3nK/B0ZmJpX9g4bdYbIU79Ai3TDaOR9MIWm7j Auh58ZG72plkbUaGjuzz6pwAmH0ey0Phjws5yktOcC3FdA4y7NRdeiEwZfHXj/vndxzi QQfz9lmK9hUlehBjpQPahV3kL4mgh7ygpTTvvTiJn+/MyNgRoY8zC7X+EH4q+c8phxhN yw6f5+6rVJMCKMnuuUTVulEKTFiXjNOxmcbi8I/mgY3FziwmKuXxER2y/oNXnCGSc1lg zqW9YrUF5AVBO3NAHda0Dgjd+a/Y9ugN4c6RxZGvLT3ycjqWmBRyoX3zTvXg1kW2aFVu MK6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jvWw9ru2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id he18-20020a1709073d9200b0078243145257si13572368ejc.921.2022.10.18.12.55.19; Tue, 18 Oct 2022 12:55:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jvWw9ru2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229926AbiJRTw7 (ORCPT + 99 others); Tue, 18 Oct 2022 15:52:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229852AbiJRTwk (ORCPT ); Tue, 18 Oct 2022 15:52:40 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C5E7857F5; Tue, 18 Oct 2022 12:52:30 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id y14so34869847ejd.9; Tue, 18 Oct 2022 12:52:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=F/aQ8vmh1jZL17zaP9aQue+c8W/pbna/0wkMpy2D0Vo=; b=jvWw9ru2o3Oj8dramvcuYh4qUbMzZVFrRRtnU0R2o9qlGMM30cKLMz+hL0+8Mg7C6B 6gQBjOV42HYC3k7jQ9TbK+Q3s+0rEUCX3mUsyyoZiEHxdLk8I+yv1LFO5p/SlaBP1U5z 5OGe+4LYLTIwynUdRhvA4oXyILoHVRxxWKGbXUAhx0KsXoHw3aOutng6yz3QutlAC0Ac Oq6eO67IRQC4rY0AXprrZ/uSpqCq7OFNfXeh5zZHHKF/YFHRHl06jYZQ4WPh8w9U4j8n 8UVxAOsWxttfx42qdlj0hawDwMu5UfmgmsyqhrpHqjLB+uwM29flR3KsqvSSUXiMaxF9 +G4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=F/aQ8vmh1jZL17zaP9aQue+c8W/pbna/0wkMpy2D0Vo=; b=WTb8o+CZeg1HWKbNruNERDVwZ/3NjBzJkoOQf3Q5fq0eII9/sZRmolEQDyYxBCfGq2 7Jypz25v3nAbmlsCvOcleAHyDzOtTJY/tCsN6qyEQs88PixwSVg4kpPy1yBba9ZQm+Oy eTlGz/jP896Kta387ZUT02N8wC23BPw3EHajMSvVtroQZ3fKE8uWwE6VjfGDshKOwrqS lotm5rPl3u4Khtt3i+eTLsmdT3D0Okreakl+E7zS3f/MfFrAIwaUgMhYx0gXaW3l8m9p Z/pbugVehyz51LBC59N7rbKJdkfySLzB6HpaXyEXPySBtk5Tfwb4jLBBw+6LqES42aWP bbMA== X-Gm-Message-State: ACrzQf2Ep39kLDLfSfrnA7nkgLVt5F6FqKjy1x104XhnkuV2lmBDUww5 +qEbeSUIYnM5bx9RTxYhAzp/3JdYYmI= X-Received: by 2002:a17:907:d04:b0:76e:e208:27ba with SMTP id gn4-20020a1709070d0400b0076ee20827bamr3931422ejc.652.1666122727949; Tue, 18 Oct 2022 12:52:07 -0700 (PDT) Received: from 127.0.0.1localhost (94.197.72.2.threembb.co.uk. [94.197.72.2]) by smtp.gmail.com with ESMTPSA id r1-20020a1709061ba100b0072a881b21d8sm7945858ejg.119.2022.10.18.12.52.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Oct 2022 12:52:07 -0700 (PDT) From: Pavel Begunkov To: Jens Axboe , linux-block@vger.kernel.org Cc: io-uring@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Pavel Begunkov Subject: [RFC for-next v2 3/4] block/bio: add pcpu caching for non-polling bio_put Date: Tue, 18 Oct 2022 20:50:57 +0100 Message-Id: <9fd04486d972c1f3ef273fa26b4b6bf51a5e4270.1666122465.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.38.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747056539915405517?= X-GMAIL-MSGID: =?utf-8?q?1747056539915405517?= This patch extends REQ_ALLOC_CACHE to IRQ completions, whenever currently it's only limited to iopoll. Instead of guarding the list with irq toggling on alloc, which is expensive, it keeps an additional irq-safe list from which bios are spliced in batches to ammortise overhead. On the put side it toggles irqs, but in many cases they're already disabled and so cheap. Signed-off-by: Pavel Begunkov --- block/bio.c | 64 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/block/bio.c b/block/bio.c index ac16cc154476..c2dda2759df5 100644 --- a/block/bio.c +++ b/block/bio.c @@ -25,9 +25,15 @@ #include "blk-rq-qos.h" #include "blk-cgroup.h" +#define ALLOC_CACHE_THRESHOLD 16 +#define ALLOC_CACHE_SLACK 64 +#define ALLOC_CACHE_MAX 512 + struct bio_alloc_cache { struct bio *free_list; + struct bio *free_list_irq; unsigned int nr; + unsigned int nr_irq; }; static struct biovec_slab { @@ -408,6 +414,22 @@ static void punt_bios_to_rescuer(struct bio_set *bs) queue_work(bs->rescue_workqueue, &bs->rescue_work); } +static void bio_alloc_irq_cache_splice(struct bio_alloc_cache *cache) +{ + unsigned long flags; + + /* cache->free_list must be empty */ + if (WARN_ON_ONCE(cache->free_list)) + return; + + local_irq_save(flags); + cache->free_list = cache->free_list_irq; + cache->free_list_irq = NULL; + cache->nr += cache->nr_irq; + cache->nr_irq = 0; + local_irq_restore(flags); +} + static struct bio *bio_alloc_percpu_cache(struct block_device *bdev, unsigned short nr_vecs, blk_opf_t opf, gfp_t gfp, struct bio_set *bs) @@ -417,9 +439,17 @@ static struct bio *bio_alloc_percpu_cache(struct block_device *bdev, cache = per_cpu_ptr(bs->cache, get_cpu()); if (!cache->free_list) { - put_cpu(); - return NULL; + if (READ_ONCE(cache->nr_irq) < ALLOC_CACHE_THRESHOLD) { + put_cpu(); + return NULL; + } + bio_alloc_irq_cache_splice(cache); + if (!cache->free_list) { + put_cpu(); + return NULL; + } } + bio = cache->free_list; cache->free_list = bio->bi_next; cache->nr--; @@ -676,11 +706,8 @@ void guard_bio_eod(struct bio *bio) bio_truncate(bio, maxsector << 9); } -#define ALLOC_CACHE_MAX 512 -#define ALLOC_CACHE_SLACK 64 - -static void bio_alloc_cache_prune(struct bio_alloc_cache *cache, - unsigned int nr) +static int __bio_alloc_cache_prune(struct bio_alloc_cache *cache, + unsigned int nr) { unsigned int i = 0; struct bio *bio; @@ -692,6 +719,17 @@ static void bio_alloc_cache_prune(struct bio_alloc_cache *cache, if (++i == nr) break; } + return i; +} + +static void bio_alloc_cache_prune(struct bio_alloc_cache *cache, + unsigned int nr) +{ + nr -= __bio_alloc_cache_prune(cache, nr); + if (!READ_ONCE(cache->free_list)) { + bio_alloc_irq_cache_splice(cache); + __bio_alloc_cache_prune(cache, nr); + } } static int bio_cpu_dead(unsigned int cpu, struct hlist_node *node) @@ -728,6 +766,7 @@ static void bio_alloc_cache_destroy(struct bio_set *bs) static inline void bio_put_percpu_cache(struct bio *bio) { struct bio_alloc_cache *cache; + unsigned long flags; cache = per_cpu_ptr(bio->bi_pool->cache, get_cpu()); bio_uninit(bio); @@ -737,12 +776,15 @@ static inline void bio_put_percpu_cache(struct bio *bio) cache->free_list = bio; cache->nr++; } else { - put_cpu(); - bio_free(bio); - return; + local_irq_save(flags); + bio->bi_next = cache->free_list_irq; + cache->free_list_irq = bio; + cache->nr_irq++; + local_irq_restore(flags); } - if (cache->nr > ALLOC_CACHE_MAX + ALLOC_CACHE_SLACK) + if (READ_ONCE(cache->nr_irq) + cache->nr > + ALLOC_CACHE_MAX + ALLOC_CACHE_SLACK) bio_alloc_cache_prune(cache, ALLOC_CACHE_SLACK); put_cpu(); }