All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bart Van Assche <bart.vanassche@wdc.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org, Christoph Hellwig <hch@lst.de>,
	Bart Van Assche <bart.vanassche@wdc.com>,
	Tejun Heo <tj@kernel.org>, Ming Lei <ming.lei@redhat.com>,
	Jianchao Wang <jianchao.w.wang@oracle.com>,
	Hannes Reinecke <hare@suse.com>,
	Johannes Thumshirn <jthumshirn@suse.de>,
	Alan Stern <stern@rowland.harvard.edu>
Subject: [PATCH v5 4/9] percpu-refcount: Introduce percpu_ref_is_in_use()
Date: Tue,  7 Aug 2018 15:51:28 -0700	[thread overview]
Message-ID: <20180807225133.27221-5-bart.vanassche@wdc.com> (raw)
In-Reply-To: <20180807225133.27221-1-bart.vanassche@wdc.com>

Introduce a function that allows to determine whether a per-cpu refcount
is in use. This function will be used in a later patch to determine
whether or not any block layer requests are being executed.

Signed-off-by: Bart Van Assche <bart.vanassche@wdc.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Jianchao Wang <jianchao.w.wang@oracle.com>
Cc: Hannes Reinecke <hare@suse.com>
Cc: Johannes Thumshirn <jthumshirn@suse.de>
Cc: Alan Stern <stern@rowland.harvard.edu>
---
 include/linux/percpu-refcount.h |  2 ++
 lib/percpu-refcount.c           | 40 +++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h
index 009cdf3d65b6..dd247756d634 100644
--- a/include/linux/percpu-refcount.h
+++ b/include/linux/percpu-refcount.h
@@ -331,4 +331,6 @@ static inline bool percpu_ref_is_zero(struct percpu_ref *ref)
 	return !atomic_long_read(&ref->count);
 }
 
+bool percpu_ref_is_in_use(struct percpu_ref *ref);
+
 #endif
diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c
index 9f96fa7bc000..1dcb47e2c561 100644
--- a/lib/percpu-refcount.c
+++ b/lib/percpu-refcount.c
@@ -369,3 +369,43 @@ void percpu_ref_reinit(struct percpu_ref *ref)
 	spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
 }
 EXPORT_SYMBOL_GPL(percpu_ref_reinit);
+
+/**
+ * percpu_ref_is_in_use - verify whether or not a percpu refcount is in use
+ * @ref: percpu_ref to test
+ *
+ * For a percpu refcount that is in percpu-mode, verify whether the reference
+ * count is above one. For a percpu refcount that is in atomic mode, verify
+ * whether the reference count is above zero. This function allows to verify
+ * whether any references are held on a percpu refcount that is switched
+ * between atomic and percpu mode with percpu_ref_reinit() /
+ * percpu_ref_kill().
+ *
+ * This function is safe to call as long as @ref is between init and exit. It
+ * is the responsibility of the caller to handle percpu_ref_get() and
+ * percpu_ref_put() calls that occur concurrently with this function.
+ */
+bool percpu_ref_is_in_use(struct percpu_ref *ref)
+{
+	unsigned long __percpu *percpu_count;
+	unsigned long sum = 0;
+	int cpu;
+	unsigned long flags;
+
+	/* Obtain percpu_ref_switch_lock to serialize against mode switches. */
+	spin_lock_irqsave(&percpu_ref_switch_lock, flags);
+	rcu_read_lock_sched();
+	if (__ref_is_percpu(ref, &percpu_count)) {
+		for_each_possible_cpu(cpu)
+			sum += *per_cpu_ptr(percpu_count, cpu);
+	}
+	rcu_read_unlock_sched();
+	sum += atomic_long_read(&ref->count);
+	sum &= ~PERCPU_COUNT_BIAS;
+	sum += (ref->percpu_count_ptr & __PERCPU_REF_DEAD);
+	spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
+
+	WARN_ON_ONCE(sum == 0);
+	return sum > 1;
+}
+EXPORT_SYMBOL_GPL(percpu_ref_is_in_use);
-- 
2.18.0

  parent reply	other threads:[~2018-08-07 22:51 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-07 22:51 [PATCH v5 0/9] blk-mq: Implement runtime power management Bart Van Assche
2018-08-07 22:51 ` [PATCH v5 1/9] block: Change the preempt-only flag into a counter Bart Van Assche
2018-08-08  8:21   ` Ming Lei
2018-08-08 15:27     ` Bart Van Assche
2018-08-07 22:51 ` [PATCH v5 2/9] block: Move power management code into a new source file Bart Van Assche
2018-08-07 22:51 ` [PATCH v5 3/9] block, scsi: Introduce blk_pm_runtime_exit() Bart Van Assche
2018-08-07 22:51 ` Bart Van Assche [this message]
2018-08-08 15:23   ` [PATCH v5 4/9] percpu-refcount: Introduce percpu_ref_is_in_use() Tejun Heo
2018-08-09 14:32     ` Bart Van Assche
2018-08-07 22:51 ` [PATCH v5 5/9] block: Change the runtime power management approach (1/2) Bart Van Assche
2018-08-08  6:11   ` jianchao.wang
2018-08-08  6:43     ` jianchao.wang
2018-08-08 17:28       ` Bart Van Assche
2018-08-09  2:52         ` Ming Lei
2018-08-09 17:12           ` Bart Van Assche
2018-08-07 22:51 ` [PATCH v5 6/9] block: Change the runtime power management approach (2/2) Bart Van Assche
2018-08-08  8:50   ` Ming Lei
2018-08-08 17:32     ` Bart Van Assche
2018-08-07 22:51 ` [PATCH v5 7/9] block: Remove blk_pm_requeue_request() Bart Van Assche
2018-08-07 22:51 ` [PATCH v5 8/9] blk-mq: Insert a blk_pm_put_request() call Bart Van Assche
2018-08-07 22:51 ` [PATCH v5 9/9] blk-mq: Enable support for runtime power management Bart Van Assche

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180807225133.27221-5-bart.vanassche@wdc.com \
    --to=bart.vanassche@wdc.com \
    --cc=axboe@kernel.dk \
    --cc=hare@suse.com \
    --cc=hch@lst.de \
    --cc=jianchao.w.wang@oracle.com \
    --cc=jthumshirn@suse.de \
    --cc=linux-block@vger.kernel.org \
    --cc=ming.lei@redhat.com \
    --cc=stern@rowland.harvard.edu \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.