Skip to contentSkip to conversation

mm/page_counter: annotate an intentional data race

PATCH In review Qian Cai 2 participants 3 messages Jan 29, 2020 – Feb 11, 2020

Subsystems (2)
lore
Share & tools
lore

Qian Cai

author
#0 lore raw

PATCH 1/1 mm/page_counter: annotate an intentional data race

The commit 3e32cb2e0a12 ("mm: memcontrol: lockless page counters") could had memcg->memsw->failcnt been accessed concurrently as reported by KCSAN,

BUG: KCSAN: data-race in page_counter_try_charge / page_counter_try_charge

write to 0xffff88809bbf2158 of 8 bytes by task 11782 on cpu 0:

 page_counter_try_charge+0x100/0x170 mm/page_counter.c:129
 try_charge+0x185/0xbf0 mm/memcontrol.c:2405
 __memcg_kmem_charge_memcg+0x4a/0xe0 mm/memcontrol.c:2837
 __memcg_kmem_charge+0xcf/0x1b0 mm/memcontrol.c:2877
 __alloc_pages_nodemask+0x26c/0x310 mm/page_alloc.c:4780

read to 0xffff88809bbf2158 of 8 bytes by task 11814 on cpu 1:

 page_counter_try_charge+0xef/0x170 mm/page_counter.c:129
 try_charge+0x185/0xbf0 mm/memcontrol.c:2405
 __memcg_kmem_charge_memcg+0x4a/0xe0 mm/memcontrol.c:2837
 __memcg_kmem_charge+0xcf/0x1b0 mm/memcontrol.c:2877
 __alloc_pages_nodemask+0x26c/0x310 mm/page_alloc.c:4780

Since the "failcnt" counter is tolerant of some degree of inaccuracy and is only used to report stats, a data race will not be harmful, thus mark it as an intentional data races with the data_race() macro.

---
 mm/page_counter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_counter.c +1 -1
index a17841150906..7c82072cda25 100644
@@ -126,7 +126,7 @@ bool page_counter_try_charge(struct page_counter *counter,
 			 * This is racy, but we can live with some
 			 * inaccuracy in the failcnt.
 			 */
-			c->failcnt++;
+			data_race(c->failcnt++);
 			*fail = c;
 			goto failed;
 		}
Signature · 1 line
2.21.0 (Apple Git-122.2)
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 20 lines · Qian Cai

The commit 3e32cb2e0a12 ("mm: memcontrol: lockless page counters") could had memcg->memsw->failcnt been accessed concurrently as reported by KCSAN,

BUG: KCSAN: data-race in page_counter_try_charge / page_counter_try_charge

write to 0xffff88809bbf2158 of 8 bytes by task 11782 on cpu 0:

 page_counter_try_charge+0x100/0x170 mm/page_counter.c:129
 try_charge+0x185/0xbf0 mm/memcontrol.c:2405
 __memcg_kmem_charge_memcg+0x4a/0xe0 mm/memcontrol.c:2837
 __memcg_kmem_charge+0xcf/0x1b0 mm/memcontrol.c:2877
 __alloc_pages_nodemask+0x26c/0x310 mm/page_alloc.c:4780

read to 0xffff88809bbf2158 of 8 bytes by task 11814 on cpu 1:

 page_counter_try_charge+0xef/0x170 mm/page_counter.c:129
 try_charge+0x185/0xbf0 mm/memcontrol.c:2405
 __memcg_kmem_charge_memcg+0x4a/0xe0 mm/memcontrol.c:2837
 __memcg_kmem_charge+0xcf/0x1b0 mm/memcontrol.c:2877
 __alloc_pages_nodemask+0x26c/0x310 mm/page_alloc.c:4780
Quoted 6 lines

Since the "failcnt" counter is tolerant of some degree of inaccuracy and is only used to report stats, a data race will not be harmful, thus mark it as an intentional data races with the data_race() macro.

Quoted 19 lines
---
 mm/page_counter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_counter.c +1 -1
index a17841150906..7c82072cda25 100644
@@ -126,7 +126,7 @@ bool page_counter_try_charge(struct page_counter *counter,
 			 * This is racy, but we can live with some
 			 * inaccuracy in the failcnt.
 			 */
-			c->failcnt++;
+			data_race(c->failcnt++);
 			*fail = c;
 			goto failed;
 		}
Signature · 1 line
2.21.0 (Apple Git-122.2)
Signature · 2 lines
Michal Hocko
SUSE Labs