mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tejun Heo <tj@kernel.org>
To: lizefan@huawei.com, hannes@cmpxchg.org
Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
	guro@fb.com, kernel-team@fb.com, Tejun Heo <tj@kernel.org>
Subject: [PATCH 8/8] cgroup: Add memory barriers to plug cgroup_rstat_updated() race window
Date: Fri, 23 Mar 2018 16:13:13 -0700	[thread overview]
Message-ID: <20180323231313.1254142-9-tj@kernel.org> (raw)
In-Reply-To: <20180323231313.1254142-1-tj@kernel.org>

cgroup_rstat_updated() has a small race window where an updated
signaling can race with flush and could be lost till the next update.
This wasn't a problem for the existing usages, but we plan to use
rstat to track counters which need to be accurate.

This patch plugs the race window by synchronizing
cgroup_rstat_updated() and flush path with memory barriers around
cgroup_rstat_cpu->updated_next pointer.

Signed-off-by: Tejun Heo <tj@kernel.org>
---
 kernel/cgroup/rstat.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c
index 18b464a..662d7ae 100644
--- a/kernel/cgroup/rstat.c
+++ b/kernel/cgroup/rstat.c
@@ -28,9 +28,12 @@ void cgroup_rstat_updated(struct cgroup *cgrp, int cpu)
 	unsigned long flags;
 
 	/*
-	 * Speculative already-on-list test.  This may race leading to
-	 * temporary inaccuracies, which is fine.
-	 *
+	 * Paired with the one in cgroup_rstat_cpu_pop_upated().  Either we
+	 * see NULL updated_next or they see our updated stat.
+	 */
+	smp_mb();
+
+	/*
 	 * Because @parent's updated_children is terminated with @parent
 	 * instead of NULL, we can tell whether @cgrp is on the list by
 	 * testing the next pointer for NULL.
@@ -125,6 +128,13 @@ static struct cgroup *cgroup_rstat_cpu_pop_updated(struct cgroup *pos,
 
 		*nextp = rstatc->updated_next;
 		rstatc->updated_next = NULL;
+
+		/*
+		 * Paired with the one in cgroup_rstat_cpu_updated().
+		 * Either they see NULL updated_next or we see their
+		 * updated stat.
+		 */
+		smp_mb();
 	}
 
 	return pos;
-- 
2.9.5

  parent reply	other threads:[~2018-03-23 23:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-23 23:13 [PATCHSET] cgroup/for-4.17: Make cgroup_rstat available to controllers Tejun Heo
2018-03-23 23:13 ` [PATCH 1/8] cgroup: Rename kernel/cgroup/stat.c to kernel/cgroup/rstat.c Tejun Heo
2018-03-23 23:13 ` [PATCH 2/8] cgroup: Rename stat to rstat Tejun Heo
2018-03-23 23:13 ` [PATCH 3/8] cgroup: Distinguish base resource stat implementation from rstat Tejun Heo
2018-03-23 23:13 ` [PATCH 4/8] cgroup: Reorganize kernel/cgroup/rstat.c Tejun Heo
2018-03-23 23:13 ` [PATCH 5/8] cgroup: Factor out and expose cgroup_rstat_*() interface functions Tejun Heo
2018-03-24 20:44   ` [PATCH v2 " Tejun Heo
2018-03-23 23:13 ` [PATCH 6/8] cgroup: Replace cgroup_rstat_mutex with a spinlock Tejun Heo
2018-03-23 23:13 ` [PATCH 7/8] cgroup: Add cgroup_subsys->css_rstat_flush() Tejun Heo
2018-03-23 23:13 ` Tejun Heo [this message]
2018-04-02 21:49 ` [PATCH] cgroup: Make cgroup_rstat_updated() ready for root cgroup usage Tejun Heo
2018-04-26 21:36   ` Tejun Heo
2018-04-26 21:35 ` [PATCHSET] cgroup/for-4.17: Make cgroup_rstat available to controllers Tejun Heo

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=20180323231313.1254142-9-tj@kernel.org \
    --to=tj@kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=guro@fb.com \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@huawei.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