From patchwork Wed Oct 20 20:03:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12573281 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 3224FC433FE for ; Wed, 20 Oct 2021 20:03:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CD92060F56 for ; Wed, 20 Oct 2021 20:03:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org CD92060F56 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 2A28E6B0075; Wed, 20 Oct 2021 16:03:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2014E940007; Wed, 20 Oct 2021 16:03:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0034B6B0080; Wed, 20 Oct 2021 16:03:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0135.hostedemail.com [216.40.44.135]) by kanga.kvack.org (Postfix) with ESMTP id E60946B0075 for ; Wed, 20 Oct 2021 16:03:26 -0400 (EDT) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 9A2A81815A4A9 for ; Wed, 20 Oct 2021 20:03:26 +0000 (UTC) X-FDA: 78717890412.31.693E0B1 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) by imf20.hostedemail.com (Postfix) with ESMTP id A76B8D0000A9 for ; Wed, 20 Oct 2021 20:03:22 +0000 (UTC) Received: by mail-wr1-f47.google.com with SMTP id k7so657940wrd.13 for ; Wed, 20 Oct 2021 13:03:25 -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=lqvGiRpysvfIvZi1p/Sn/aAVIjsT/b9K5dksSdMrSoY=; b=cWuRWk5S1CRqG3q/d+rkURGCqlYEvwkYRUqyTLWS4/5tF9RqNtsQGgswqhlGcRXdLu ZyS8TnEAh2zGEKobN7O9CDqEQGGG8m7dibMPTqZGdXRCkqhgtY6zhJ9EgLvPw/wrQ8VI jioysauEyb6VzpfESEelAw/bgOiXcDJLeBUGx3LpZJ549ze0pfaIiUuNME5tSOza1WcD SrJrEh6DuBanfO8y0QJTRVyGZHKoFEerHfrYNjCAE7w7VmWMl6YlmzmyQUo65rfPfY2f 2TPcZWNcSYIHSSiqJzhRIrzfl7hSV7PR/RZ8MZXGQrR6O7vtWI7trLopfKAgVec3WupJ IddQ== 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=lqvGiRpysvfIvZi1p/Sn/aAVIjsT/b9K5dksSdMrSoY=; b=uWgmbfgvaAGvpRWZ3JLGs+5sRgDXe3lpgKRmXG362iIjH+PVvB4h4IDI9hDrFVROXk UoH/Sx4XpoEBgMZknHYjNAIR1ZGw3BP3Aw5Lyb3/zk2TmqEarp/Ir6f3jwstzTO1NmB1 QBmciYPf6FXU7YdRu9ewPIP15eiYTjtWryT+J0n8y3ejfQqKQyJdxgEEeRfTmh8HEft0 9WzxXY1NxkdX5KUYK95g6Cnu4Y5cMs1rSFOiZQY/aBoKXJc78bf45Y/0R8+eGPuUoTkq BbfFDKMjM7JMlJ6NXGNClkZVhkuDk7ngJrtvehfPWahvY9YDNZpXTcVft4oWdhiNuoLL 5JGA== X-Gm-Message-State: AOAM533zypwQL1VuuKlV3ZsMcCpqcAI70C7vs/1WSC3JtasZa26zx/TF EvCNzgIU1B+91ZPEgLMgt9Y= X-Google-Smtp-Source: ABdhPJyFBwhXslFRXmjC0EbulCHD1qDNtij5tJ2u4UmyXrF3BTQbLcf8Qk11SX+HkuZyx8uWeZGsmg== X-Received: by 2002:adf:f305:: with SMTP id i5mr1606208wro.155.1634760205048; Wed, 20 Oct 2021 13:03:25 -0700 (PDT) Received: from 127.0.0.1localhost ([185.69.145.206]) by smtp.gmail.com with ESMTPSA id m14sm3600119wms.25.2021.10.20.13.03.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 13:03:24 -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 1/2] percpu_ref: percpu_ref_tryget_live() version holding RCU Date: Wed, 20 Oct 2021 21:03:18 +0100 Message-Id: X-Mailer: git-send-email 2.33.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A76B8D0000A9 X-Stat-Signature: r65qbtgoa9wit14ckrth8py8qqr97hu8 Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=cWuRWk5S; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.221.47 as permitted sender) smtp.mailfrom=asml.silence@gmail.com X-HE-Tag: 1634760202-176177 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- include/linux/percpu-refcount.h | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h index ae16a9856305..13e26add7d5c 100644 --- a/include/linux/percpu-refcount.h +++ b/include/linux/percpu-refcount.h @@ -266,6 +266,26 @@ 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; + + 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 +303,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 Wed Oct 20 20:03:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12573283 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 39E9EC433F5 for ; Wed, 20 Oct 2021 20:03:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C0391610E7 for ; Wed, 20 Oct 2021 20:03:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C0391610E7 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id B777E940009; Wed, 20 Oct 2021 16:03:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AFF61940007; Wed, 20 Oct 2021 16:03:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9EFB4940009; Wed, 20 Oct 2021 16:03:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0145.hostedemail.com [216.40.44.145]) by kanga.kvack.org (Postfix) with ESMTP id 8E9EC940007 for ; Wed, 20 Oct 2021 16:03:27 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 4D7DB2C6B8 for ; Wed, 20 Oct 2021 20:03:27 +0000 (UTC) X-FDA: 78717890454.09.7181C53 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) by imf27.hostedemail.com (Postfix) with ESMTP id A5BBC70000A2 for ; Wed, 20 Oct 2021 20:03:25 +0000 (UTC) Received: by mail-wr1-f52.google.com with SMTP id g25so673617wrb.2 for ; Wed, 20 Oct 2021 13:03:26 -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=WbgNFPzpqQwP0xWC2uli9FeYZOhufR5CXwXu9ncgcBUanmWs0Ez75Z5+fFvqOG5bt/ eJpYOZzGZ9tAkQgonvDFNrrYSFzu5esd6IyZyysSWI/h4WpmX6UDDLnVD27MjESegLuu 3NmuuSKZhY4d0/i2CFq9mvSrdzwWkLm5oC1Z4KiE+qQJQqTx15w3dHHYmkE36FUHQzk1 xvtbkRz63rAMB3yVc3yAWs5Exet7k2+Jg3/Yq1KtDHEytbGiJ9I/YC38HULEosaZ7ulB gxycBcHHjBN0VfIQQAIK58bdOyYVPQ/qXWa94VOYsXQyA7GqpxKeyeJJBA2Wvadnt4EG wXSA== 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=hsJSRm8lu8VbO/rDSInvUNe5qh4g5yndRtXPuqnD93Ip0sio4nvlXI5JkGB5F9U7CI dmiZFsD2vJSWJ1xHV0EiUraFIIAjPzvEe5DzX/1Frn0YshWQKNpBKzfdVRIKmMXmOZWH uuhV2O2LHicqTa3VDZus+ppkhvRQGh4yXaOHinD4EVwcdsIW8N8lEyf+MlYFGwY0uxUi n0ekQCCBfF9RsZ40chQ4pkqvhkCbRWzcNWixXNYhxwj+9JlUWx1v6Lyyv3fFVFBz0A4g 6f+nfC1qRZsvlKGUvnsz3t4De8PJ3xj7l3ZXERbmOBbBYad91vIWvbEhuVg+mUKXnU8e guzg== X-Gm-Message-State: AOAM530Yw83cpKvdtz2vU9ehVL2fnyio9W78WsssXL0AISKRRfLz8T8Y xMwUfdDViTRgoudzDBcayyo= X-Google-Smtp-Source: ABdhPJyHaPBBNKhdLB85ZPk4rJy40dUZ0AZiN0c283LaQx4WPK7+fyH8BjtbhoxBpccA3tSED0163A== X-Received: by 2002:a5d:414e:: with SMTP id c14mr1557870wrq.415.1634760205953; Wed, 20 Oct 2021 13:03:25 -0700 (PDT) Received: from 127.0.0.1localhost ([185.69.145.206]) by smtp.gmail.com with ESMTPSA id m14sm3600119wms.25.2021.10.20.13.03.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 13:03:25 -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 2/2] block: kill extra rcu lock/unlock in queue enter Date: Wed, 20 Oct 2021 21:03:19 +0100 Message-Id: <159884e23f494a4b658c9cb66e36900c3c5800f5.1634759754.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: A5BBC70000A2 X-Stat-Signature: rs8kjooncgajawmm4ex58t5ejitkos7d Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=WbgNFPzp; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.221.52 as permitted sender) smtp.mailfrom=asml.silence@gmail.com X-Rspamd-Server: rspam01 X-HE-Tag: 1634760205-655108 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000069, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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; /*