Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4710002ybl; Sat, 21 Dec 2019 12:23:55 -0800 (PST) X-Google-Smtp-Source: APXvYqz4UfzHdGDCwNXg8nH/oJmSsfhLKA5OuaZd/ghN5M55ZV5gQ8QY2f0b7bFJ+NZm7E3pOP0U X-Received: by 2002:a9d:c02:: with SMTP id 2mr23369641otr.183.1576959835560; Sat, 21 Dec 2019 12:23:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576959835; cv=none; d=google.com; s=arc-20160816; b=R5oBUh091sjuCwHQWvXDHD7rGsYseLNqb26rNHYfu+tT2DqHXxTpz4ePVHzS2M9vOE QE9PB3s6KxcZx8O+4vwNjp64uxgX63I3CsJ3g6RCPl1NoFZ7xvItf52wjgP9uTovMcA8 1ubI7kBsFr3DMX0POPK971OglPMQzmGTIgalfqbYiz9czV6uXcvTZ5fxR6Qp9t1w1pN4 l9BboaOWnty4iPt4ryYZorStJrr6njnHhX1o7JUV0SWalIQuB2a5vdM5p7IXT996XrH7 eutGSF7yIOY3JYVHzwOYPJVT51syq0dLePAgayEoR7/x4/qQE88YEN0yOrHfkya+sZpA cMnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ncxg9b/1MfJvBW44JauWnlBevZqV28zJC8M4jJXK7AU=; b=L9QOfdr9WZTxx7aDQ3Vz/NdclXkEMgamV6AWoP5XZwvoLWjBB3a1CNZmv4iC4xQFLc jhk5D4sLdyy6GAdsb5aFMEaaIr9CqgGyo+krKBaTOC/CF3Q0GvxM/TjDNNanNVWDB0/b 1/UK5n4JDG203Gk4xecJ85rfnahIEv7GW0CBHXfJHfN5oBySIXWyRGYGzj+3vwk8fhUL rm2xp+mSzukjgeXkWp8IpH7EbAMMSfb8a62GdBT137EWCslsj+EIqpRodZDwhkmlqybc uCgJLUe0Mh8uyF9mN2nucHXLBijMtTArMFzawmbYlc7xMRUO8ofcPUcQepW5SaO5ZEeT WvUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=IPb0f46T; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id m67si6968839oib.117.2019.12.21.12.23.43; Sat, 21 Dec 2019 12:23:55 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=IPb0f46T; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727397AbfLUUN3 (ORCPT + 99 others); Sat, 21 Dec 2019 15:13:29 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:38658 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726763AbfLUUN3 (ORCPT ); Sat, 21 Dec 2019 15:13:29 -0500 Received: by mail-wr1-f67.google.com with SMTP id y17so12693709wrh.5; Sat, 21 Dec 2019 12:13:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ncxg9b/1MfJvBW44JauWnlBevZqV28zJC8M4jJXK7AU=; b=IPb0f46TBXzjUkxbWwMcHMGBOuxd6Z8GMYX8VmjPUUah+NIZ9qM3HRbwA8as3RTuDq Ax56SHBcl4ibr2e5FabDpi1/0q9qDJmIm6d0E7TvdKtk/cRTK58A3RmMP/eKp83Qg2nF 8HjSsTeizWNcEHOWL0iCpBQnqNbjOOwSjn8mqeEdqs5NRAGkVJ3ymi5Ps1L/guRdpX4Z dN3Mdyd3JUjkrs0jT2DW4/AlJaEIpqEAqOfPxP9KNniSPk0QHKVb+uxiUITT7jKHXill dvk+IXyM3Q9lxZZXNLDRCB3wXixuOpz2Zd1OQjbqroix4aTD1xrRmkDqlXU9AmcexnQK J4jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ncxg9b/1MfJvBW44JauWnlBevZqV28zJC8M4jJXK7AU=; b=bLagN+mIGmivmS9eOXwYijih+qnrU4tYkG5SXQ/EZKGnuL0PKGLf7qN3WHDOKSxvkf mjKYMvJ0OoxZukCU1lmJ+3YEKNMgn9R23Y5OHs5NLvR4h6GVQv2ZmGJqAlzoGrPcCLCq LhfQaRbVD5gfPwJpoebz5ZtrUgF9CTYyxerLhiiDtSfknR9+X3du4Qii68SgNeEc9hYh 0gLFINwE+A4RSwoOxRublF0ECVesRFYPu27Oa8neg+TsERLDnPxZ1zInzLwZdkfdadju 6CSkZxqTJXwgDsiM55DBPSbqB43uZBJNyWw2gucfiBVlWwTV93BiAZxwIL4nLfFr6+QD GoeA== X-Gm-Message-State: APjAAAW6JIKKbrJozgRXEjuCyxa1TtRCkXKGp4vuHjytQQfG9eBI4xP8 Qgj3072HNjeKRJjf7mEtv3I= X-Received: by 2002:a5d:43c7:: with SMTP id v7mr20564731wrr.32.1576959206814; Sat, 21 Dec 2019 12:13:26 -0800 (PST) Received: from localhost.localdomain ([109.126.149.134]) by smtp.gmail.com with ESMTPSA id l7sm14470821wrq.61.2019.12.21.12.13.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Dec 2019 12:13:26 -0800 (PST) From: Pavel Begunkov To: Jens Axboe , io-uring@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Tejun Heo , Dennis Zhou , Christoph Lameter Subject: [PATCH v3 1/2] pcpu_ref: add percpu_ref_tryget_many() Date: Sat, 21 Dec 2019 23:12:53 +0300 Message-Id: <8f663b99e6f30dc51d41456771d4a94567ab31f4.1576958402.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.24.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add percpu_ref_tryget_many(), which works the same way as percpu_ref_tryget(), but grabs specified number of refs. Signed-off-by: Pavel Begunkov Acked-by: Tejun Heo Acked-by: Dennis Zhou Cc: Christoph Lameter --- include/linux/percpu-refcount.h | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h index 390031e816dc..22d9d183950d 100644 --- a/include/linux/percpu-refcount.h +++ b/include/linux/percpu-refcount.h @@ -210,15 +210,17 @@ static inline void percpu_ref_get(struct percpu_ref *ref) } /** - * percpu_ref_tryget - try to increment a percpu refcount + * percpu_ref_tryget_many - try to increment a percpu refcount * @ref: percpu_ref to try-get + * @nr: number of references to get * - * Increment a percpu refcount unless its count already reached zero. + * Increment a percpu refcount by @nr unless its count already reached zero. * Returns %true on success; %false on failure. * * This function is safe to call as long as @ref is between init and exit. */ -static inline bool percpu_ref_tryget(struct percpu_ref *ref) +static inline bool percpu_ref_tryget_many(struct percpu_ref *ref, + unsigned long nr) { unsigned long __percpu *percpu_count; bool ret; @@ -226,10 +228,10 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) rcu_read_lock(); if (__ref_is_percpu(ref, &percpu_count)) { - this_cpu_inc(*percpu_count); + this_cpu_add(*percpu_count, nr); ret = true; } else { - ret = atomic_long_inc_not_zero(&ref->count); + ret = atomic_long_add_unless(&ref->count, nr, 0); } rcu_read_unlock(); @@ -237,6 +239,20 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) return ret; } +/** + * percpu_ref_tryget - try to increment a percpu refcount + * @ref: percpu_ref to try-get + * + * Increment a percpu refcount unless its count already reached zero. + * Returns %true on success; %false on failure. + * + * This function is safe to call as long as @ref is between init and exit. + */ +static inline bool percpu_ref_tryget(struct percpu_ref *ref) +{ + return percpu_ref_tryget_many(ref, 1); +} + /** * percpu_ref_tryget_live - try to increment a live percpu refcount * @ref: percpu_ref to try-get -- 2.24.0