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; }