From patchwork Thu Oct 21 13:30:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12575121 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79A55C433EF for ; Thu, 21 Oct 2021 13:31:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 58E2B61212 for ; Thu, 21 Oct 2021 13:31:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231524AbhJUNdP (ORCPT ); Thu, 21 Oct 2021 09:33:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231485AbhJUNdO (ORCPT ); Thu, 21 Oct 2021 09:33:14 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D21EC0613B9; Thu, 21 Oct 2021 06:30:58 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id u8-20020a05600c440800b0030d90076dabso7238498wmn.1; Thu, 21 Oct 2021 06:30:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=E65W5rHcRhwN48L7vjwZwLiwyU/1qg2FCqyrEx/BzFo=; b=A4Db4wOCuN4vfKzOeCn6fAKweaT7ITHTa5kuw6/YGTHV6ttOFo4kWjnssYmv6jaMRF Gfdn8DL2mVcKkW0lSly0/ROor4GCHXnrCLqdMfxgfi3dJ1lKXAXAf6tMX5QCfQidGUDE 0OaQh9/xTdsXXR7jMZFU2Vsz5Yrrt0HFMwWABV7sPUbhezb/e0+SDUu34ytTTM1T3FG4 PBSpUjg0jhAn+7XY7icgULt3E/wMvO9xuUvG04YXVTZSuix9L23CxbxMWIA2TAsWijzO AkTTnnvjdR30umdjFNPLqg2mgn63z4bcPUh7IsIP5FGG7JXHe7aus0aVs38HCSbJj5O4 vuhQ== 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=E65W5rHcRhwN48L7vjwZwLiwyU/1qg2FCqyrEx/BzFo=; b=wcgICp9Ko9rE04KNGLE8oELhn7Xvwn08sNdrN9AtlO6K6oou2jJNlVgF9Q0q2M5eVM 2i6rfxc1aRKoAjRDKJ5phH9epn03tXEWhwK4vWccGi8KnOEhrefjeUjUHZ0xdttR0eqQ mfGZsOiqg16Qn9ynPLFeNvjlMIfEeqgjgv1xiGfMhsye8obz/WAOHOByO53bd/IOyyts bBHEyV+Evm++PAYi5a4LKUwZ/uTYITUgQD/O/B5FRQh8D6U1yRjWTQ73XDx3I0BKexcS HCXNirjVdqZ6GIfDrxqAzhv/Xq+UQqPxuIrJj5iVfOPUO6sqUo93cnfH8jmRPjICfHM1 EkWA== X-Gm-Message-State: AOAM531k03sy1ZJJyty71QGYy9bbYYeBiE5lG+rLF54C/In8OD0ml6/F c7/L51E43PibN5Lp+LhqIYezUTYgaHNMyA== X-Google-Smtp-Source: ABdhPJyNo19TUXl1tAqCwlEvPsAOMdycnYyGTzCu7W5uXLrZT4E7XWnC+iUocVNRyw6J8dkZ2OfdNw== X-Received: by 2002:a1c:f609:: with SMTP id w9mr6721166wmc.24.1634823056793; Thu, 21 Oct 2021 06:30:56 -0700 (PDT) Received: from 127.0.0.1localhost ([185.69.145.206]) by smtp.gmail.com with ESMTPSA id q188sm1753987wma.1.2021.10.21.06.30.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Oct 2021 06:30:56 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: Jens Axboe , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Christoph Lameter , Tejun Heo , Dennis Zhou , Pavel Begunkov Subject: [PATCH v2 1/2] percpu_ref: percpu_ref_tryget_live() version holding RCU Date: Thu, 21 Oct 2021 14:30:51 +0100 Message-Id: <3066500d7a6eb3e03f10adf98b87fdb3b1c49db8.1634822969.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Add percpu_ref_tryget_live_rcu(), which is a version of percpu_ref_tryget_live() but the user is responsible for enclosing it in a RCU read lock section. Signed-off-by: Pavel Begunkov Acked-by: Dennis Zhou --- include/linux/percpu-refcount.h | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h index ae16a9856305..b31d3f3312ce 100644 --- a/include/linux/percpu-refcount.h +++ b/include/linux/percpu-refcount.h @@ -266,6 +266,28 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) return percpu_ref_tryget_many(ref, 1); } +/** + * percpu_ref_tryget_live_rcu - same as percpu_ref_tryget_live() but the + * caller is responsible for taking RCU. + * + * This function is safe to call as long as @ref is between init and exit. + */ +static inline bool percpu_ref_tryget_live_rcu(struct percpu_ref *ref) +{ + unsigned long __percpu *percpu_count; + bool ret = false; + + WARN_ON_ONCE(!rcu_read_lock_held()); + + if (likely(__ref_is_percpu(ref, &percpu_count))) { + this_cpu_inc(*percpu_count); + ret = true; + } else if (!(ref->percpu_count_ptr & __PERCPU_REF_DEAD)) { + ret = atomic_long_inc_not_zero(&ref->data->count); + } + return ret; +} + /** * percpu_ref_tryget_live - try to increment a live percpu refcount * @ref: percpu_ref to try-get @@ -283,20 +305,11 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) */ static inline bool percpu_ref_tryget_live(struct percpu_ref *ref) { - unsigned long __percpu *percpu_count; bool ret = false; rcu_read_lock(); - - if (__ref_is_percpu(ref, &percpu_count)) { - this_cpu_inc(*percpu_count); - ret = true; - } else if (!(ref->percpu_count_ptr & __PERCPU_REF_DEAD)) { - ret = atomic_long_inc_not_zero(&ref->data->count); - } - + ret = percpu_ref_tryget_live_rcu(ref); rcu_read_unlock(); - return ret; } From patchwork Thu Oct 21 13:30:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12575123 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50EAAC433EF for ; Thu, 21 Oct 2021 13:31:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3DB9960EE2 for ; Thu, 21 Oct 2021 13:31:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231558AbhJUNdR (ORCPT ); Thu, 21 Oct 2021 09:33:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231530AbhJUNdP (ORCPT ); Thu, 21 Oct 2021 09:33:15 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BEB5C0613B9; Thu, 21 Oct 2021 06:30:59 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id p21so724599wmq.1; Thu, 21 Oct 2021 06:30:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=q1EZc8F3LGa37Y9tu7Tob6o+fpihU/+4iG2rIzfXu/A=; b=DSLfMGOH+4Vf+I5tVUhKt6iPK9QhlIK1peDZ/Rd1UA+3pGfTuY94UfTBLfe5O4tBWf /x+vRpqShCTOZp7w5M9oCPv5WxNrKMlf3XchuqaAXjLa2LrN4eauOYqDn/z+byCurgQG sxTQPqjBkQJpbIXMxnbruNnFS/VSx3lacEPdxKVowgsAD0etuK4dquzD3uHozStluJE8 WpApuuhYMmhnKHyELAJ150q90k//oKBI0og1Snx+/yl6wp2kNRjsBPHPrJw0vOZYyhdm WneyBOd6yzDTTxUHJd3ziLgB70JVFCGBwcF31E/hEV7YUfEAccTrziTy80O/5N4HfgKy wrvw== 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=q1EZc8F3LGa37Y9tu7Tob6o+fpihU/+4iG2rIzfXu/A=; b=7KRgSsGltZeOZ7y8MNRHMuxkFykA00+QHbVbfOxgMijhYqdJ+VHwh16ChF+9G+SG1o qfTngd23MXbRZyTVKETzVpu4uagFrN/vebZn/J84TfmRBFFM8Ij41SuLRMsINObWBLwR AxeTHDBsQ3FLSaZHXeSwvtTzYlI5JNX09EY/9rC8okigwxc8Xhe8bLbqmaroiccMPMHY 33z5ibsqlCWvIUQML7TW3Pxp7iaqDD9eyRkHweNwzLZvBXndGnDoAbzHNjyWxxMzMVxN xwLtyqq228WLnGyhBIVo2lbyagLfJLrOQ8x3CcREUGfkVlOA6yIpvo8BtBTsFsC5z81U m4eA== X-Gm-Message-State: AOAM530eMupQBP/G4xyeORYYMpNXy0iccg6I8/8qq0NKbLnAcZxmkBUb DHl2JVoBIeIMHMAEAE46NS5MbTZ7iRJkTA== X-Google-Smtp-Source: ABdhPJzePBePS8id/gMcA7dyBwnpRlvyOO68ZUrBYCXG1JbQEWIUK/7kzEExvq99mreV58pAFWYDAQ== X-Received: by 2002:a1c:7f0a:: with SMTP id a10mr21627983wmd.60.1634823057945; Thu, 21 Oct 2021 06:30:57 -0700 (PDT) Received: from 127.0.0.1localhost ([185.69.145.206]) by smtp.gmail.com with ESMTPSA id q188sm1753987wma.1.2021.10.21.06.30.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Oct 2021 06:30:57 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: Jens Axboe , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Christoph Lameter , Tejun Heo , Dennis Zhou , Pavel Begunkov Subject: [PATCH v2 2/2] block: kill extra rcu lock/unlock in queue enter Date: Thu, 21 Oct 2021 14:30:52 +0100 Message-Id: <6b11c67ea495ed9d44f067622d852de4a510ce65.1634822969.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org blk_try_enter_queue() already takes rcu_read_lock/unlock, so we can avoid the second pair in percpu_ref_tryget_live(), use a newly added percpu_ref_tryget_live_rcu(). As rcu_read_lock/unlock imply barrier()s, it's pretty noticeable, especially for for !CONFIG_PREEMPT_RCU (default for some distributions), where __rcu_read_lock/unlock() are not inlined. 3.20% io_uring [kernel.vmlinux] [k] __rcu_read_unlock 3.05% io_uring [kernel.vmlinux] [k] __rcu_read_lock 2.52% io_uring [kernel.vmlinux] [k] __rcu_read_unlock 2.28% io_uring [kernel.vmlinux] [k] __rcu_read_lock Signed-off-by: Pavel Begunkov --- block/blk-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/blk-core.c b/block/blk-core.c index 88752e51d2b6..20e76aeb50f5 100644 --- a/block/blk-core.c +++ b/block/blk-core.c @@ -389,7 +389,7 @@ EXPORT_SYMBOL(blk_cleanup_queue); static bool blk_try_enter_queue(struct request_queue *q, bool pm) { rcu_read_lock(); - if (!percpu_ref_tryget_live(&q->q_usage_counter)) + if (!percpu_ref_tryget_live_rcu(&q->q_usage_counter)) goto fail; /*