Skip to contentSkip to conversation

mm/memcontrol: fix data-race on reading jiffies_64

PATCH Applied Jiayuan Chen 4 participants 4 messages Aug 27

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/memcontrol: fix data-race on reading jiffies_64

KCSAN reported a data-race between tick_do_update_jiffies64() updating jiffies_64 and mem_cgroup_flush_stats_ratelimited() reading it directly.

Unlike jiffies, jiffies_64 is not volatile, so raw reads are plain accesses and can even be torn on 32-bit. Use get_jiffies_64() instead, and fix the same pattern in mem_cgroup_flush_foreign().

Fixes 508bed884767 ("mm: memcg: change flush_next_time to flush_last_time")
Fixes 97b27821b485 ("writeback, memcg: Implement foreign dirty flushing")
Reported-by [email protected]

---
feel free to drop Fixes tag.

---
 mm/memcontrol.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/memcontrol.c +3 -3
index 11b85f4b6828..5a3fe057debc 100644
@@ -757,7 +757,7 @@ static void __mem_cgroup_flush_stats(struct mem_cgroup *memcg, bool force)
 		return;
 
 	if (mem_cgroup_is_root(memcg))
-		WRITE_ONCE(flush_last_time, jiffies_64);
+		WRITE_ONCE(flush_last_time, get_jiffies_64());
 
 	css_rstat_flush(&memcg->css);
 }
@@ -785,7 +785,7 @@ void mem_cgroup_flush_stats(struct mem_cgroup *memcg)
 void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg)
 {
 	/* Only flush if the periodic flusher is one full cycle late */
-	if (time_after64(jiffies_64, READ_ONCE(flush_last_time) + 2*FLUSH_TIME))
+	if (time_after64(get_jiffies_64(), READ_ONCE(flush_last_time) + 2 * FLUSH_TIME))
 		mem_cgroup_flush_stats(memcg);
 }
 
@@ -3945,7 +3945,7 @@ void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
 	unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10);
-	u64 now = jiffies_64;
+	u64 now = get_jiffies_64();
 	int i;
 
 	for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
Signature · 1 line
2.43.0
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jiayuan Chen

KCSAN reported a data-race between tick_do_update_jiffies64() updating jiffies_64 and mem_cgroup_flush_stats_ratelimited() reading it directly.

Unlike jiffies, jiffies_64 is not volatile, so raw reads are plain accesses and can even be torn on 32-bit. Use get_jiffies_64() instead, and fix the same pattern in mem_cgroup_flush_foreign().

Quoted 5 lines
Fixes 508bed884767 ("mm: memcg: change flush_next_time to flush_last_time")
Fixes 97b27821b485 ("writeback, memcg: Implement foreign dirty flushing")
Reported-by [email protected]

Thanks.

Acked-by on v1 1/1
#2 lore raw
In reply to Jiayuan Chen
Quoted 6 lines · Jiayuan Chen

KCSAN reported a data-race between tick_do_update_jiffies64() updating jiffies_64 and mem_cgroup_flush_stats_ratelimited() reading it directly.

Unlike jiffies, jiffies_64 is not volatile, so raw reads are plain accesses and can even be torn on 32-bit. Use get_jiffies_64() instead, and fix the same pattern in mem_cgroup_flush_foreign().

Yes, this looks more correct. I do not expect this to have any practically visible effects, right?

Quoted 5 lines
Fixes 508bed884767 ("mm: memcg: change flush_next_time to flush_last_time")
Fixes 97b27821b485 ("writeback, memcg: Implement foreign dirty flushing")
Reported-by [email protected]
Quoted 40 lines

---
feel free to drop Fixes tag.

---
 mm/memcontrol.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/memcontrol.c +3 -3
index 11b85f4b6828..5a3fe057debc 100644
@@ -757,7 +757,7 @@ static void __mem_cgroup_flush_stats(struct mem_cgroup *memcg, bool force)
 		return;
 
 	if (mem_cgroup_is_root(memcg))
-		WRITE_ONCE(flush_last_time, jiffies_64);
+		WRITE_ONCE(flush_last_time, get_jiffies_64());
 
 	css_rstat_flush(&memcg->css);
 }
@@ -785,7 +785,7 @@ void mem_cgroup_flush_stats(struct mem_cgroup *memcg)
 void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg)
 {
 	/* Only flush if the periodic flusher is one full cycle late */
-	if (time_after64(jiffies_64, READ_ONCE(flush_last_time) + 2*FLUSH_TIME))
+	if (time_after64(get_jiffies_64(), READ_ONCE(flush_last_time) + 2 * FLUSH_TIME))
 		mem_cgroup_flush_stats(memcg);
 }
 
@@ -3945,7 +3945,7 @@ void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
 	unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10);
-	u64 now = jiffies_64;
+	u64 now = get_jiffies_64();
 	int i;
 
 	for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
Signature · 1 line
2.43.0
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Jiayuan Chen
Quoted 7 lines · Jiayuan Chen

KCSAN reported a data-race between tick_do_update_jiffies64() updating jiffies_64 and mem_cgroup_flush_stats_ratelimited() reading it directly.

Unlike jiffies, jiffies_64 is not volatile, so raw reads are plain accesses and can even be torn on 32-bit. Use get_jiffies_64() instead, and fix the same pattern in mem_cgroup_flush_foreign().

Quoted 5 lines
Fixes 508bed884767 ("mm: memcg: change flush_next_time to flush_last_time")
Fixes 97b27821b485 ("writeback, memcg: Implement foreign dirty flushing")
Reported-by [email protected]

Minor nit:

mm/memcontrol.c void mem_cgroup_flush_stats(struct mem_cgroup *memcg) · line 789

Quoted 7 lines
mm/memcontrol.c +1 -1
@@ -785,7 +785,7 @@ void mem_cgroup_flush_stats(struct mem_cgroup *memcg)
 void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg)
 {
 	/* Only flush if the periodic flusher is one full cycle late */
-	if (time_after64(jiffies_64, READ_ONCE(flush_last_time) + 2*FLUSH_TIME))
+	if (time_after64(get_jiffies_64(), READ_ONCE(flush_last_time) + 2 * FLUSH_TIME))
 		mem_cgroup_flush_stats(memcg);

time_is_after_jiffies64()