Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp629718pxb; Thu, 21 Oct 2021 06:34:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJEv9/PTWiP3XgM7AZsYaWmxp7vCnwhfnI45cqYHdVxKWUgXnmWwo/o3/+ZgxmNQWwZrPA X-Received: by 2002:a05:6a00:1a56:b0:44d:23ae:37dd with SMTP id h22-20020a056a001a5600b0044d23ae37ddmr5832998pfv.8.1634823289099; Thu, 21 Oct 2021 06:34:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634823289; cv=none; d=google.com; s=arc-20160816; b=onGRzjiM3GVh5lStL+jRWyuUXD5RvJGTfp+x0KtCNhYdoEkx3BW6lVGjn4rlAIZdQc A/jIushtalea6DHqeBSkVdskPTPh7uUS36QNq4+SahUqns38MaaBkSvZ0DrO0eew6Wwc huoueq+ZLVVEkaOqWq6YJVCET3OJThXhQwWzZIXgTvMD0X8xwXNMbGWBe/oC8G8M11Zf JBsKNGRpmWhV223mxcryq6D0bdf6WkM9bHmzWMZGrCFByNpwQu6WaYjRUvmdNe7cB6bd UaBNF1b/s+leaoCK2qgycTwkOnhhUH8P8IxaeYfVWZ5q5ak3nYiAkDTMIIpD3ZDlx1Q8 4eYA== 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=E65W5rHcRhwN48L7vjwZwLiwyU/1qg2FCqyrEx/BzFo=; b=jhQ1+pxtrCbEuGhrjZYU07kCjpbMziJ0dWZnOtzTz8/dJSkZxLL5S/fc3FRBcqB+0G Z+bdyy94OaUTdWxI0kLCWV0XHInWdJrv7n3IceFoCsqTLB0z2opEEdZ0q2W3qegID1Dx wsWq2o1OPJ2JiFbpLv+t+Y0zrvtvQD0c0gvYYsFTdUM/T4HFuxnbZXIMyiB3yccEm0tE DCui1sOIWKHc0TBMMayMgKOmXGR9KB3fmtFzQBvecDRu42zfothWSbzHaw4QRbmiXw5v ysV5jhlyF/19LN1WrUUID1gxcKQjHz+T9IRUcmmlmlT6Wc/WXqs2dFqPbBkj26bkkqh0 L+2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=A4Db4wOC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f20si8237309pfj.254.2021.10.21.06.34.25; Thu, 21 Oct 2021 06:34:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=A4Db4wOC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S231533AbhJUNdQ (ORCPT + 99 others); Thu, 21 Oct 2021 09:33:16 -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-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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 --- 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; } -- 2.33.1