public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
From: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
To: cmm@us.ibm.com, tytso@mit.edu, sandeen@redhat.com
Cc: linux-ext4@vger.kernel.org,
	"Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
Subject: [RFC PATCH] percpu_counters: Add new function percpu_counter_sum_and_sub
Date: Fri, 22 Aug 2008 19:04:33 +0530	[thread overview]
Message-ID: <1219412074-30584-2-git-send-email-aneesh.kumar@linux.vnet.ibm.com> (raw)
In-Reply-To: <1219412074-30584-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com>

percpu_counter_sum_and_sub(struct percpu_counter *fbc, s64 amount)

This will be later used by ext4 code. It adds up the local
per cpu counter values to global count and subtract amount
from the gloabl count if gloabl count is > amount. This is
used during block resrevation to check within a lock we
have sufficient free blocks if so also claim the blocks.

Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
---
 include/linux/percpu_counter.h |   17 +++++++++++++++++
 lib/percpu_counter.c           |   27 +++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/include/linux/percpu_counter.h b/include/linux/percpu_counter.h
index af485b1..978db67 100644
--- a/include/linux/percpu_counter.h
+++ b/include/linux/percpu_counter.h
@@ -36,6 +36,7 @@ void percpu_counter_destroy(struct percpu_counter *fbc);
 void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
 void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
 s64 __percpu_counter_sum(struct percpu_counter *fbc);
+s64 __percpu_counter_sum_and_sub(struct percpu_counter *fbc, s64 amount);
 
 static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
 {
@@ -53,6 +54,12 @@ static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
 	return __percpu_counter_sum(fbc);
 }
 
+static inline int percpu_counter_sum_and_sub(struct percpu_counter *fbc,
+						s64 amount)
+{
+	return __percpu_counter_sum_and_sub(fbc, amount);
+}
+
 #if BITS_PER_LONG == 64
 static inline s64 fbc_count(struct percpu_counter *fbc)
 {
@@ -146,6 +153,16 @@ static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
 	return percpu_counter_read(fbc);
 }
 
+static inline int percpu_counter_sum_and_sub(struct percpu_counter *fbc,
+						s64 amount)
+{
+	if (fbc->count >= amount) {
+		percpu_counter_sub(fbc, amount);
+		return 0;
+	}
+	return -E2BIG;
+}
+
 #endif	/* CONFIG_SMP */
 
 static inline void percpu_counter_inc(struct percpu_counter *fbc)
diff --git a/lib/percpu_counter.c b/lib/percpu_counter.c
index a866389..0336062 100644
--- a/lib/percpu_counter.c
+++ b/lib/percpu_counter.c
@@ -71,6 +71,33 @@ s64 __percpu_counter_sum(struct percpu_counter *fbc)
 }
 EXPORT_SYMBOL(__percpu_counter_sum);
 
+ /*
+  * Add up all the per-cpu counts. If the result is greater than
+  * amount subtract amount from result and return 0. Otherwise return
+  * -E2BIG
+  */
+s64 __percpu_counter_sum_and_sub(struct percpu_counter *fbc, s64 amount)
+{
+	s64 ret;
+	int cpu;
+
+	spin_lock(&fbc->lock);
+	ret = fbc->count;
+	for_each_online_cpu(cpu) {
+		s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
+		ret += *pcount;
+		*pcount = 0;
+	}
+	if (ret >= amount) {
+		fbc->count = ret - amount;
+		spin_unlock(&fbc->lock);
+		return 0;
+	}
+	spin_unlock(&fbc->lock);
+	return -E2BIG;
+}
+EXPORT_SYMBOL(__percpu_counter_sum_and_sub);
+
 static struct lock_class_key percpu_counter_irqsafe;
 
 int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
-- 
1.6.0.2.g2ebc0


  reply	other threads:[~2008-08-22 13:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-22 13:34 [RFC PATCH] percpu_counters: make fbc->count read atomic on 32 bit architecture Aneesh Kumar K.V
2008-08-22 13:34 ` Aneesh Kumar K.V [this message]
2008-08-22 13:34   ` [RFC PATCH] ext4: Make sure all the block allocation patch reserve blocks Aneesh Kumar K.V
2008-08-22 18:22     ` Mingming Cao
2008-08-22 18:01   ` [RFC PATCH] percpu_counters: Add new function percpu_counter_sum_and_sub Mingming Cao
2008-08-22 18:29 ` [RFC PATCH] percpu_counters: make fbc->count read atomic on 32 bit architecture Mingming Cao
2008-08-22 18:33   ` Peter Zijlstra

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=1219412074-30584-2-git-send-email-aneesh.kumar@linux.vnet.ibm.com \
    --to=aneesh.kumar@linux.vnet.ibm.com \
    --cc=cmm@us.ibm.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=sandeen@redhat.com \
    --cc=tytso@mit.edu \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox