linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Christoph Lameter <cl@linux.com>
To: Tejun Heo <tj@kernel.org>
Cc: Eric Dumazet <eric.dumazet@gmail.com>,
	akpm@linux-foundation.org, linux-mm@kvack.org,
	shaohua.li@intel.com
Subject: Re: [PATCH] percpu: preemptless __per_cpu_counter_add
Date: Fri, 15 Apr 2011 12:37:16 -0500 (CDT)	[thread overview]
Message-ID: <alpine.DEB.2.00.1104151235350.8055@router.home> (raw)
In-Reply-To: <20110414211522.GE21397@mtj.dyndns.org>

On Fri, 15 Apr 2011, Tejun Heo wrote:

> > The preempt on/off seems to be a bigger deal for realtime.
>
> Also, the cmpxchg used is local one w/o LOCK prefix.  It might not
> bring anything to table on !PREEMPT kernels but at the same time it
> shouldn't hurt either.  One way or the other, some benchmark numbers
> showing that it at least doesn't hurt would be nice.

Maybe just fall back to this_cpu_write()?
> > > Maybe use here latest cmpxchg16b stuff instead and get rid of spinlock ?
> >
> > Shaohua already got an atomic in there. You mean get rid of his preempt
> > disable/enable in the slow path?
>
> I personally care much less about slow path.  According to Shaohua,
> atomic64_t behaves pretty nice and it isn't too complex, so I'd like
> to stick with that unless complex this_cpu ops can deliver something
> much better.

Ok here is a new patch that will allow Shaohua to simply convert the
slowpath to a single atomic op. No preemption anymore anywhere.



Subject: percpu: preemptless __per_cpu_counter_add V3

Use this_cpu_cmpxchg to avoid preempt_disable/enable in __percpu_add.

V3 - separate out the slow path so that the slowpath can also be done with
	a simple atomic add without preemption enable/disable.
   - Fallback in the !PREEMPT case to a simple this_cpu_write().

Signed-off-by: Christoph Lameter <cl@linux.com>

---
 lib/percpu_counter.c |   29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

Index: linux-2.6/lib/percpu_counter.c
===================================================================
--- linux-2.6.orig/lib/percpu_counter.c	2011-04-13 17:12:59.000000000 -0500
+++ linux-2.6/lib/percpu_counter.c	2011-04-15 12:34:39.000000000 -0500
@@ -71,19 +71,30 @@ EXPORT_SYMBOL(percpu_counter_set);

 void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
 {
-	s64 count;
+	s64 count, new, overflow;

-	preempt_disable();
-	count = __this_cpu_read(*fbc->counters) + amount;
-	if (count >= batch || count <= -batch) {
+	do {
+		overflow = 0;
+		count = this_cpu_read(*fbc->counters);
+
+		new = count + amount;
+		/* In case of overflow fold it into the global counter instead */
+		if (new >= batch || new <= -batch) {
+			overflow = new;
+			new = 0;
+		}
+#ifdef CONFIG_PREEMPT
+	} while (this_cpu_cmpxchg(*fbc->counters, count, new) != count);
+#else
+	} while (0);
+	this_cpu_write(*fbc->counters, new);
+#endif
+
+	if (unlikely(overflow)) {
 		spin_lock(&fbc->lock);
-		fbc->count += count;
-		__this_cpu_write(*fbc->counters, 0);
+		fbc->count += overflow;
 		spin_unlock(&fbc->lock);
-	} else {
-		__this_cpu_write(*fbc->counters, count);
 	}
-	preempt_enable();
 }
 EXPORT_SYMBOL(__percpu_counter_add);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  reply	other threads:[~2011-04-15 17:37 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-13 14:45 Christoph Lameter
2011-04-13 16:49 ` Christoph Lameter
2011-04-13 18:56   ` Tejun Heo
2011-04-13 20:22     ` [PATCH] " Christoph Lameter
2011-04-13 21:50       ` Tejun Heo
2011-04-13 22:17         ` Christoph Lameter
2011-04-13 22:23           ` Christoph Lameter
2011-04-13 23:55             ` Tejun Heo
2011-04-14  2:00               ` Eric Dumazet
2011-04-14  2:14       ` Eric Dumazet
2011-04-14 21:10         ` Christoph Lameter
2011-04-14 21:15           ` Tejun Heo
2011-04-15 17:37             ` Christoph Lameter [this message]
2011-04-15 18:27               ` Tejun Heo
2011-04-15 19:43                 ` Christoph Lameter
2011-04-15 23:52                   ` Tejun Heo
2011-04-18 14:38                     ` Christoph Lameter
2011-04-21 14:43                       ` Tejun Heo
2011-04-21 14:58                         ` Tejun Heo
2011-04-21 17:50                           ` Christoph Lameter
2011-04-21 18:01                             ` Tejun Heo
2011-04-21 18:20                               ` Christoph Lameter
2011-04-21 18:37                                 ` Tejun Heo
2011-04-21 18:54                                   ` Christoph Lameter
2011-04-21 19:08                                     ` Tejun Heo
2011-04-22  2:33                                       ` Shaohua Li
2011-04-26 12:10                                         ` Tejun Heo
2011-04-26 19:02                                           ` Hugh Dickins
2011-04-27 10:28                                             ` Tejun Heo
2011-04-27  5:43                                           ` Shaohua Li
2011-04-27 10:20                                             ` Tejun Heo
2011-04-28  3:28                                               ` Shaohua Li
2011-04-28 10:09                                                 ` Tejun Heo
2011-04-28 14:11                                                   ` Christoph Lameter
2011-04-28 14:23                                                     ` Tejun Heo
2011-04-28 14:30                                                       ` Tejun Heo
2011-04-28 14:58                                                         ` Christoph Lameter
2011-04-28 14:42                                                       ` Christoph Lameter
2011-04-28 14:44                                                         ` Tejun Heo
2011-04-28 14:52                                                           ` Christoph Lameter
2011-04-28 14:56                                                             ` Tejun Heo
2011-04-28 15:05                                                               ` Christoph Lameter
2011-04-28 15:12                                                                 ` Tejun Heo
2011-04-28 15:22                                                                   ` Christoph Lameter
2011-04-28 15:31                                                                     ` Tejun Heo
2011-04-28 15:40                                                                       ` Tejun Heo
2011-04-28 15:47                                                                         ` Christoph Lameter
2011-04-28 15:48                                                                     ` Eric Dumazet
2011-04-28 15:59                                                                       ` Eric Dumazet
2011-04-28 16:17                                                                         ` Christoph Lameter
2011-04-28 16:35                                                                           ` Eric Dumazet
2011-04-28 16:52                                                                             ` Christoph Lameter
2011-04-28 16:59                                                                               ` Eric Dumazet
2011-04-29  8:52                                                                                 ` Tejun Heo
2011-04-29  8:32                                                                             ` Shaohua Li
2011-04-29  8:19                                                   ` Shaohua Li
2011-04-29  8:44                                                     ` Tejun Heo
2011-04-29 14:02                                                       ` Christoph Lameter
2011-04-29 14:03                                                         ` Christoph Lameter
2011-04-29 14:18                                                         ` Tejun Heo
2011-04-29 14:25                                                           ` Christoph Lameter
2011-04-29 14:43                                                             ` Tejun Heo
2011-04-29 14:55                                                               ` Christoph Lameter
2011-05-05  4:08                                                               ` Shaohua Li

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=alpine.DEB.2.00.1104151235350.8055@router.home \
    --to=cl@linux.com \
    --cc=akpm@linux-foundation.org \
    --cc=eric.dumazet@gmail.com \
    --cc=linux-mm@kvack.org \
    --cc=shaohua.li@intel.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox