From: Tim Chen <tim.c.chen@linux.intel.com>
To: Jiebin Sun <jiebin.sun@intel.com>,
akpm@linux-foundation.org, vasily.averin@linux.dev,
shakeelb@google.com, dennis@kernel.org, tj@kernel.org,
cl@linux.com, ebiederm@xmission.com, legion@kernel.org,
manfred@colorfullife.com, alexander.mikhalitsyn@virtuozzo.com,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: tim.c.chen@intel.com, feng.tang@intel.com, ying.huang@intel.com,
tianyou.li@intel.com, wangyang.guo@intel.com
Subject: Re: [PATCH v5 1/2] percpu: Add percpu_counter_add_local and percpu_counter_sub_local
Date: Fri, 09 Sep 2022 09:37:09 -0700 [thread overview]
Message-ID: <346eb3cc79a657f3e45d3fa2bea2fdffa741af3d.camel@linux.intel.com> (raw)
In-Reply-To: <20220909203636.2652466-2-jiebin.sun@intel.com>
On Sat, 2022-09-10 at 04:36 +0800, Jiebin Sun wrote:
> The batch size in percpu_counter_add_batch should be very large
> in heavy writing and rare reading case. Add the "_local" version,
> and mostly it will do local adding, reduce the global updating
> and mitigate lock contention in writing.
>
> Signed-off-by: Jiebin Sun <jiebin.sun@intel.com>
> ---
> include/linux/percpu_counter.h | 38 ++++++++++++++++++++++++++++++++++
> 1 file changed, 38 insertions(+)
>
> diff --git a/include/linux/percpu_counter.h b/include/linux/percpu_counter.h
> index 01861eebed79..6dd7eaba8527 100644
> --- a/include/linux/percpu_counter.h
> +++ b/include/linux/percpu_counter.h
> @@ -15,6 +15,9 @@
> #include <linux/types.h>
> #include <linux/gfp.h>
>
> +/* percpu_counter batch for local add or sub */
> +#define PERCPU_COUNTER_LOCAL_BATCH INT_MAX
> +
> #ifdef CONFIG_SMP
>
> struct percpu_counter {
> @@ -56,6 +59,27 @@ static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
> percpu_counter_add_batch(fbc, amount, percpu_counter_batch);
> }
>
> +/*
> + * Use this function in heavy writing but rare reading case. The large
> + * batch size will reduce the global updating.
Suggest revising the comment, so it is clear we need to use
percpu_counter_sum() to access the counter:
With percpu_counter_add_local() and percpu_counter_sub_local(),
counts are accumulated in local per cpu counter and not in
fbc->count until local count overflows PERCPU_COUNTER_LOCAL_BATCH.
This makes counter write efficient.
But percpu_counter_sum(), instead of percpu_counter_read(),
needs to be used to add up the counts
from each CPU to account for all the local counts.
So percpu_counter_add_local() and percpu_counter_sub_local()
should be used when a counter is updated frequently and read
rarely.
> + */
> +static inline void
> +percpu_counter_add_local(struct percpu_counter *fbc, s64 amount)
> +{
> + percpu_counter_add_batch(fbc, amount, PERCPU_COUNTER_LOCAL_BATCH);
> +}
> +
> +/*
> + * Similar with percpu_counter_add_local, use it in heavy writing but
> + * rare reading case. The large batch size will reduce the global
> + * updating.
> + */
> +static inline void
> +percpu_counter_sub_local(struct percpu_counter *fbc, s64 amount)
> +{
> + percpu_counter_add_batch(fbc, -amount, PERCPU_COUNTER_LOCAL_BATCH);
> +}
> +
> static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
> {
> s64 ret = __percpu_counter_sum(fbc);
> @@ -138,6 +162,20 @@ percpu_counter_add(struct percpu_counter *fbc, s64 amount)
> preempt_enable();
> }
>
> +/* no smp percpu_counter_add_local is the same with percpu_counter_add */
> +static inline void
> +percpu_counter_add_local(struct percpu_counter *fbc, s64 amount)
> +{
> + percpu_counter_add(fbc, amount);
> +}
> +
> +/* no smp percpu_counter_sub_local is the same with percpu_counter_sub */
> +static inline void
> +percpu_counter_sub_local(struct percpu_counter *fbc, s64 amount)
> +{
> + percpu_counter_sub(fbc, amount);
> +}
> +
> static inline void
> percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
> {
next prev parent reply other threads:[~2022-09-09 16:37 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-02 15:22 [PATCH] ipc/msg.c: mitigate the lock contention with percpu counter Jiebin Sun
2022-09-02 16:06 ` Andrew Morton
2022-09-05 11:54 ` Sun, Jiebin
2022-09-02 16:27 ` Shakeel Butt
2022-09-05 12:02 ` Sun, Jiebin
2022-09-06 18:44 ` Tim Chen
2022-09-07 9:39 ` Sun, Jiebin
2022-09-07 20:43 ` Andrew Morton
2022-09-07 17:25 ` [PATCH v4] ipc/msg: " Jiebin Sun
2022-09-07 16:01 ` Tim Chen
2022-09-07 21:34 ` Andrew Morton
2022-09-07 22:10 ` Tim Chen
2022-09-08 8:25 ` Sun, Jiebin
2022-09-08 15:38 ` Andrew Morton
2022-09-08 16:15 ` Dennis Zhou
2022-09-03 19:35 ` [PATCH] ipc/msg.c: " Manfred Spraul
2022-09-05 12:12 ` Sun, Jiebin
[not found] ` <20220905193516.846647-1-jiebin.sun@intel.com>
[not found] ` <20220905193516.846647-3-jiebin.sun@intel.com>
2022-09-05 19:31 ` [PATCH v2 1/2] percpu: Add percpu_counter_add_local Shakeel Butt
2022-09-06 8:41 ` Sun, Jiebin
2022-09-05 19:35 ` [PATCH v2 2/2] ipc/msg: mitigate the lock contention with percpu counter Jiebin Sun
2022-09-06 16:54 ` [PATCH v3 0/2] ipc/msg: mitigate the lock contention in ipc/msg Jiebin Sun
2022-09-06 16:54 ` [PATCH v3 2/2] ipc/msg: mitigate the lock contention with percpu counter Jiebin Sun
2022-09-09 20:36 ` [PATCH v5 0/2] ipc/msg: mitigate the lock contention in ipc/msg Jiebin Sun
2022-09-09 20:36 ` [PATCH v5 1/2] percpu: Add percpu_counter_add_local and percpu_counter_sub_local Jiebin Sun
2022-09-09 16:37 ` Tim Chen [this message]
2022-09-10 1:37 ` kernel test robot
2022-09-10 8:15 ` kernel test robot
2022-09-10 8:26 ` kernel test robot
2022-09-09 20:36 ` [PATCH v5 2/2] ipc/msg: mitigate the lock contention with percpu counter Jiebin Sun
2022-09-09 16:11 ` Tim Chen
2022-09-13 19:25 ` [PATCH v6 0/2] ipc/msg: mitigate the lock contention in ipc/msg Jiebin Sun
2022-09-13 19:25 ` [PATCH v6 1/2] percpu: Add percpu_counter_add_local and percpu_counter_sub_local Jiebin Sun
2022-09-18 11:08 ` Manfred Spraul
2022-09-20 6:01 ` Sun, Jiebin
2022-09-13 19:25 ` [PATCH v6 2/2] ipc/msg: mitigate the lock contention with percpu counter Jiebin Sun
2022-09-18 12:53 ` Manfred Spraul
2022-09-20 2:36 ` Sun, Jiebin
2022-09-20 4:53 ` Manfred Spraul
2022-09-20 5:50 ` Sun, Jiebin
2022-09-20 15:08 ` [PATCH] ipc/msg: avoid negative value by overflow in msginfo Jiebin Sun
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=346eb3cc79a657f3e45d3fa2bea2fdffa741af3d.camel@linux.intel.com \
--to=tim.c.chen@linux.intel.com \
--cc=akpm@linux-foundation.org \
--cc=alexander.mikhalitsyn@virtuozzo.com \
--cc=cl@linux.com \
--cc=dennis@kernel.org \
--cc=ebiederm@xmission.com \
--cc=feng.tang@intel.com \
--cc=jiebin.sun@intel.com \
--cc=legion@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=manfred@colorfullife.com \
--cc=shakeelb@google.com \
--cc=tianyou.li@intel.com \
--cc=tim.c.chen@intel.com \
--cc=tj@kernel.org \
--cc=vasily.averin@linux.dev \
--cc=wangyang.guo@intel.com \
--cc=ying.huang@intel.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;
as well as URLs for NNTP newsgroup(s).