mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Ming Lei <tom.leiming@gmail.com>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Paul Gortmaker <paul.gortmaker@windriver.com>,
	Shaohua Li <shli@fusionio.com>, Jens Axboe <axboe@kernel.dk>,
	Fan Du <fan.du@windriver.com>, Tejun Heo <tj@kernel.org>
Subject: Re: [PATCH] lib/percpu_counter.c: disable local irq when updating percpu couter
Date: Tue, 7 Jan 2014 17:36:45 -0800	[thread overview]
Message-ID: <20140107173645.64d6838e.akpm@linux-foundation.org> (raw)
In-Reply-To: <CACVXFVMVBbSqLfCwX9Q2EvAH6K4u1Y_nbtcxEixH8-g-4j6oPA@mail.gmail.com>

On Wed, 8 Jan 2014 09:12:19 +0800 Ming Lei <tom.leiming@gmail.com> wrote:

> Hi Andrew,
> 
> >> --- a/lib/percpu_counter.c
> >> +++ b/lib/percpu_counter.c
> >> @@ -75,19 +75,19 @@ EXPORT_SYMBOL(percpu_counter_set);
> >>  void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
> >>  {
> >>       s64 count;
> >> +     unsigned long flags;
> >>
> >> -     preempt_disable();
> >> +     raw_local_irq_save(flags);
> >>       count = __this_cpu_read(*fbc->counters) + amount;
> >>       if (count >= batch || count <= -batch) {
> >> -             unsigned long flags;
> >> -             raw_spin_lock_irqsave(&fbc->lock, flags);
> >> +             raw_spin_lock(&fbc->lock);
> >>               fbc->count += count;
> >> -             raw_spin_unlock_irqrestore(&fbc->lock, flags);
> >> +             raw_spin_unlock(&fbc->lock);
> >>               __this_cpu_write(*fbc->counters, 0);
> >>       } else {
> >>               __this_cpu_write(*fbc->counters, count);
> >>       }
> >> -     preempt_enable();
> >> +     raw_local_irq_restore(flags);
> >>  }
> >>  EXPORT_SYMBOL(__percpu_counter_add);
> >
> > Can this be made more efficient?
> >
> > The this_cpu_foo() documentation is fairly dreadful, but way down at
> > the end of Documentation/this_cpu_ops.txt we find "this_cpu ops are
> > interrupt safe".  So I think this is a more efficient fix:
> >
> > --- a/lib/percpu_counter.c~a
> > +++ a/lib/percpu_counter.c
> > @@ -82,10 +82,10 @@ void __percpu_counter_add(struct percpu_
> >                 unsigned long flags;
> >                 raw_spin_lock_irqsave(&fbc->lock, flags);
> >                 fbc->count += count;
> > +               __this_cpu_sub(*fbc->counters, count);
> >                 raw_spin_unlock_irqrestore(&fbc->lock, flags);
> > -               __this_cpu_write(*fbc->counters, 0);
> >         } else {
> > -               __this_cpu_write(*fbc->counters, count);
> > +               this_cpu_add(*fbc->counters, amount);
> >         }
> >         preempt_enable();
> >  }
> >
> > It avoids the local_irq_disable() in the common case, when the CPU
> > supports efficient this_cpu_add().  It will in rare race situations
> > permit the cpu-local counter to exceed `batch', but that should be
> > harmless.
> 
> I am wondering if the above patch is more efficient, because:
> 
> - raw_local_irq_save()/raw_local_irq_restore() should be cheaper
> than preempt_enable() in theory

Don't think so - local_irq_disable() requires quite some internal
synchronization in the CPU and is expensive.  preempt_disable() is just
an add+barrier, minus the add if the kernel is non-preemptable.

> - except for x86 and s390, other ARCHs have not their own implementation
> of  this_cpu_foo(), and the generic one just disables local interrupt
> when operating the percpu variable.

Yup.  But other CPUs should and will optimise their this_cpu
implementations over time.

  reply	other threads:[~2014-01-08  1:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-07 10:29 Ming Lei
2014-01-07 22:27 ` Andrew Morton
2014-01-08  1:12   ` Ming Lei
2014-01-08  1:36     ` Andrew Morton [this message]
2014-01-08  3:29       ` Ming Lei
2014-01-08  4:30         ` Ming Lei

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=20140107173645.64d6838e.akpm@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=axboe@kernel.dk \
    --cc=fan.du@windriver.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paul.gortmaker@windriver.com \
    --cc=shli@fusionio.com \
    --cc=tj@kernel.org \
    --cc=tom.leiming@gmail.com \
    /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

Powered by JetHome