All of lore.kernel.org
 help / color / mirror / Atom feed
From: Akira Yokosawa <akiyks@gmail.com>
To: "Paul E. McKenney" <paulmck@linux.ibm.com>
Cc: perfbook@vger.kernel.org, Akira Yokosawa <akiyks@gmail.com>
Subject: [PATCH 5/5] count: Fix uses of READ/WRITE_ONCE() in count_lim
Date: Mon, 8 Oct 2018 10:49:11 +0900	[thread overview]
Message-ID: <2e0b71c0-051a-dcf2-be99-e92cfcfe2031@gmail.com> (raw)
In-Reply-To: <9c31cb06-b79f-3b9c-e86a-68fb3840f688@gmail.com>

From 83f496142e594e69f89b154bebcfd8d278ac7831 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <akiyks@gmail.com>
Date: Mon, 8 Oct 2018 10:26:24 +0900
Subject: [PATCH 5/5] count: Fix uses of READ/WRITE_ONCE() in count_lim

Add a few READ_ONCE()/WRITE_ONCE()s to prevent load/store tearing
of per-thread variables "counter" and "countermax".

NOTE: Updates of those per-thread variables in utility functions
don't need WRITE_ONCE()s because they are protected by glbcnt_mutex
and can't be seen by read_count().

Signed-off-by: Akira Yokosawa <akiyks@gmail.com>
---
 CodeSamples/count/count_lim.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/CodeSamples/count/count_lim.c b/CodeSamples/count/count_lim.c
index 69ced9a..c207beb 100644
--- a/CodeSamples/count/count_lim.c
+++ b/CodeSamples/count/count_lim.c
@@ -37,7 +37,7 @@ DEFINE_SPINLOCK(gblcnt_mutex);
 static __inline__ int add_count(unsigned long delta)	//\lnlbl{add:b}
 {
 	if (countermax - counter >= delta) {		//\lnlbl{add:checklocal}
-		counter += delta);			//\lnlbl{add:add}
+		WRITE_ONCE(counter, counter + delta);	//\lnlbl{add:add}
 		return 1;				//\lnlbl{add:return:ls}
 	}
 	spin_lock(&gblcnt_mutex);			//\lnlbl{add:acquire}
@@ -56,7 +56,7 @@ static __inline__ int add_count(unsigned long delta)	//\lnlbl{add:b}
 static __inline__ int sub_count(unsigned long delta)	//\lnlbl{sub:b}
 {
 	if (counter >= delta) {				//\lnlbl{sub:checklocal}
-		counter -= delta;			//\lnlbl{sub:sub}
+		WRITE_ONCE(counter, counter - delta);	//\lnlbl{sub:sub}
 		return 1;				//\lnlbl{sub:return:ls}
 	}
 	spin_lock(&gblcnt_mutex);			//\lnlbl{sub:acquire}
@@ -80,7 +80,7 @@ static __inline__ unsigned long read_count(void)	//\lnlbl{read:b}
 	sum = globalcount;				//\lnlbl{read:initsum}
 	for_each_thread(t)				//\lnlbl{read:loop:b}
 		if (counterp[t] != NULL)
-			sum += *counterp[t];		//\lnlbl{read:loop:e}
+			sum += READ_ONCE(*counterp[t]);	//\lnlbl{read:loop:e}
 	spin_unlock(&gblcnt_mutex);			//\lnlbl{read:release}
 	return sum;					//\lnlbl{read:return}
 }							//\lnlbl{read:e}
-- 
2.7.4



  parent reply	other threads:[~2018-10-08  1:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-08  1:40 [PATCH 0/5] count: Employ new code-snippet scheme (cont.) Akira Yokosawa
2018-10-08  1:42 ` [PATCH 1/5] count: Employ new scheme for snippet of count_end and count_tstat Akira Yokosawa
2018-10-08  1:44 ` [PATCH 2/5] count: Fix uses of READ/WRITE_ONCE()s in " Akira Yokosawa
2018-10-08  1:46 ` [PATCH 3/5] count: Tweak counttorture.h to avoid segfault Akira Yokosawa
2018-10-08  1:47 ` [PATCH 4/5] count: Employ new scheme for snippet of count_lim Akira Yokosawa
2018-10-08  1:49 ` Akira Yokosawa [this message]
2018-10-08 14:02 ` [PATCH 6/5] count: Fix typo (\lnlbl{} -> \lnref{}) Akira Yokosawa
2018-10-09  3:54 ` [PATCH 0/5] count: Employ new code-snippet scheme (cont.) Paul E. McKenney

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=2e0b71c0-051a-dcf2-be99-e92cfcfe2031@gmail.com \
    --to=akiyks@gmail.com \
    --cc=paulmck@linux.ibm.com \
    --cc=perfbook@vger.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 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.