All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chi Wu <wuchi.zero@gmail.com>
To: akpm@linux-foundation.org
Cc: jack@suse.cz, linux-kernel@vger.kernel.org, tan.hu@zte.com.cn,
	Chi Wu <wuchi.zero@gmail.com>
Subject: [PATCH] lib/flex_proportions.c: Use abs() when percpu_counter is negative.
Date: Mon, 17 May 2021 23:53:06 +0800	[thread overview]
Message-ID: <20210517155306.11113-1-wuchi.zero@gmail.com> (raw)

The value of percpu_counter_read() may become negative after
running percpu_counter_sum() in fprop_reflect_period_percpu().
The value of variable 'num' will be zero in fprop_fraction_percpu()
when using percpu_counter_read_positive(), but if using the abs of
percpu_counter_read() will be close to the correct value.

Signed-off-by: Chi Wu <wuchi.zero@gmail.com>
---
 lib/flex_proportions.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/flex_proportions.c b/lib/flex_proportions.c
index 451543937524..3ac79ca2c441 100644
--- a/lib/flex_proportions.c
+++ b/lib/flex_proportions.c
@@ -147,7 +147,7 @@ void fprop_fraction_single(struct fprop_global *p,
 		seq = read_seqcount_begin(&p->sequence);
 		fprop_reflect_period_single(p, pl);
 		num = pl->events;
-		den = percpu_counter_read_positive(&p->events);
+		den = abs(percpu_counter_read(&p->events));
 	} while (read_seqcount_retry(&p->sequence, seq));
 
 	/*
@@ -209,7 +209,7 @@ static void fprop_reflect_period_percpu(struct fprop_global *p,
 			val = percpu_counter_sum(&pl->events);
 
 		percpu_counter_add_batch(&pl->events,
-			-val + (val >> (period-pl->period)), PROP_BATCH);
+			-val + (val >> (period - pl->period)), PROP_BATCH);
 	} else
 		percpu_counter_set(&pl->events, 0);
 	pl->period = period;
@@ -234,8 +234,8 @@ void fprop_fraction_percpu(struct fprop_global *p,
 	do {
 		seq = read_seqcount_begin(&p->sequence);
 		fprop_reflect_period_percpu(p, pl);
-		num = percpu_counter_read_positive(&pl->events);
-		den = percpu_counter_read_positive(&p->events);
+		num = abs(percpu_counter_read(&pl->events));
+		den = abs(percpu_counter_read(&p->events));
 	} while (read_seqcount_retry(&p->sequence, seq));
 
 	/*
-- 
2.17.1


             reply	other threads:[~2021-05-17 16:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-17 15:53 Chi Wu [this message]
2021-05-18  3:42 ` [PATCH] lib/flex_proportions.c: Use abs() when percpu_counter is negative chi wu
2021-05-18  8:59   ` Jan Kara
2021-05-18 10:22     ` chi wu
2021-05-18 10:39       ` Jan Kara
2021-05-18 11:55         ` chi wu

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=20210517155306.11113-1-wuchi.zero@gmail.com \
    --to=wuchi.zero@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=jack@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tan.hu@zte.com.cn \
    /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.