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>,
	Jianchao Wang <jianchao.w.wang@oracle.com>,
	Ming Lei <ming.lei@redhat.com>,
	Alan Stern <stern@rowland.harvard.edu>,
	Johannes Thumshirn <jthumshirn@suse.de>
Subject: [PATCH v4 06/10] percpu-refcount: Introduce percpu_ref_read()
Date: Fri,  3 Aug 2018 17:03:21 -0700	[thread overview]
Message-ID: <20180804000325.3610-7-bart.vanassche@wdc.com> (raw)
In-Reply-To: <20180804000325.3610-1-bart.vanassche@wdc.com>

Introduce a function that allows to read the value of a per-cpu counter.
This function will be used in the next patch to check whether a per-cpu
counter has the value one.

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

diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h
index 009cdf3d65b6..5707289ba828 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);
 }
 
+unsigned long percpu_ref_read(struct percpu_ref *ref);
+
 #endif
diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c
index 9f96fa7bc000..c0b9fc8efa6b 100644
--- a/lib/percpu-refcount.c
+++ b/lib/percpu-refcount.c
@@ -369,3 +369,32 @@ void percpu_ref_reinit(struct percpu_ref *ref)
 	spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
 }
 EXPORT_SYMBOL_GPL(percpu_ref_reinit);
+
+/**
+ * percpu_ref_read - read a percpu refcount
+ * @ref: percpu_ref to test
+ *
+ * This function is safe to call as long as @ref is between init and exit. It
+ * is the responsibility of the caller to handle changes of @ref concurrently
+ * with this function. If this function is called while @ref is in per-cpu
+ * mode the returned value may be incorrect if e.g. percpu_ref_get() is called
+ * from one CPU and percpu_ref_put() is called from another CPU.
+ */
+unsigned long percpu_ref_read(struct percpu_ref *ref)
+{
+	unsigned long __percpu *percpu_count;
+	unsigned long sum = 0;
+	int cpu;
+
+	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;
+
+	return sum;
+}
+EXPORT_SYMBOL_GPL(percpu_ref_read);
-- 
2.18.0

  parent reply	other threads:[~2018-08-04  0:03 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-04  0:03 [PATCH v4 00/10] blk-mq: Enable runtime power management Bart Van Assche
2018-08-04  0:03 ` [PATCH v4 01/10] block: Change the preempt-only flag into a counter Bart Van Assche
2018-08-06  6:25   ` Hannes Reinecke
2018-09-14 12:53   ` Christoph Hellwig
2018-08-04  0:03 ` [PATCH v4 02/10] block, scsi: Give RQF_PREEMPT back its original meaning Bart Van Assche
2018-08-04  1:16   ` Ming Lei
2018-08-06 14:15     ` Bart Van Assche
2018-08-06  6:27   ` Hannes Reinecke
2018-08-06 13:54     ` Bart Van Assche
2018-09-14 12:58       ` hch
2018-08-04  0:03 ` [PATCH v4 03/10] block, ide: Remove flag BLK_MQ_REQ_PREEMPT Bart Van Assche
2018-08-04  0:03 ` [PATCH v4 04/10] block: Move power management code into a new source file Bart Van Assche
2018-09-14 13:01   ` Christoph Hellwig
2018-09-14 14:10     ` Alan Stern
2018-08-04  0:03 ` [PATCH v4 05/10] block: Serialize queue freezing and blk_pre_runtime_suspend() Bart Van Assche
2018-08-04 10:23   ` Ming Lei
2018-08-06 14:19     ` Bart Van Assche
2018-08-04  0:03 ` Bart Van Assche [this message]
2018-08-04  0:03 ` [PATCH v4 07/10] block, scsi: Rework runtime power management Bart Van Assche
2018-08-04 10:01   ` Ming Lei
2018-08-06 15:20     ` Bart Van Assche
2018-08-06 22:30       ` Ming Lei
2018-08-06  2:46   ` jianchao.wang
2018-08-06 15:07     ` Bart Van Assche
2018-08-04  0:03 ` [PATCH v4 08/10] block: Remove blk_pm_requeue_request() Bart Van Assche
2018-08-04  0:03 ` [PATCH v4 09/10] blk-mq: Insert blk_pm_{add,put}_request() calls Bart Van Assche
2018-08-04  0:03 ` [PATCH v4 10/10] 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=20180804000325.3610-7-bart.vanassche@wdc.com \
    --to=bart.vanassche@wdc.com \
    --cc=axboe@kernel.dk \
    --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.