Skip to contentSkip to conversation

printk: annotate the intentional data race on the data block id

PATCH In review Nguyen Ngoc Thang 3 participants 4 messages Oct 12 – Oct 27 Printk

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 printk: annotate the intentional data race on the data block id

data_alloc() stores the descriptor ID into a newly reserved data block while data_make_reusable(), running from data_push_tail() on another CPU, may still be loading the ID from the same area. The ringbuffer allows this race: a stale or garbage ID simply fails the descriptor lookup or the blk_lpos check. KCSAN nevertheless reports it.

Annotate the accesses with data_race() instead of READ_ONCE()/WRITE_ONCE(), which would needlessly restrict compiler optimizations for a race that is allowed by design. No functional change.

Reported-by [email protected]
Fixes 896fbe20b4e2 ("printk: use the lockless ringbuffer")

---
v2: use data_race() instead of READ_ONCE()/WRITE_ONCE(), as suggested by

    John Ogness. v1: https://lore.kernel.org/all/[email protected]/
 kernel/printk/printk_ringbuffer.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/printk/printk_ringbuffer.c +4 -4
index 85c0c854b3ce..ff25c55971e8 100644
@@ -605,7 +605,7 @@ static bool data_make_reusable(struct printk_ringbuffer *rb,
 		 * sure it points back to this data block. If the check fails,
 		 * the data area has been recycled by another writer.
 		 */
-		id = blk->id; /* LMM(data_make_reusable:A) */
+		id = data_race(blk->id); /* LMM(data_make_reusable:A) */
 
 		d_state = desc_read(desc_ring, id, &desc,
 				    NULL, NULL); /* LMM(data_make_reusable:B) */
@@ -1113,7 +1113,7 @@ static char *data_alloc(struct printk_ringbuffer *rb, unsigned int size,
 					  next_lpos)); /* LMM(data_alloc:A) */
 
 	blk = to_block(data_ring, begin_lpos);
-	blk->id = id; /* LMM(data_alloc:B) */
+	data_race(blk->id = id); /* LMM(data_alloc:B) */
 
 	if (is_blk_wrapped(data_ring, begin_lpos, next_lpos)) {
 		/* Wrapping data blocks store their data at the beginning. */
@@ -1123,7 +1123,7 @@ static char *data_alloc(struct printk_ringbuffer *rb, unsigned int size,
 		 * Store the ID on the wrapped block for consistency.
 		 * The printk_ringbuffer does not actually use it.
 		 */
-		blk->id = id;
+		data_race(blk->id = id); /* see data_make_reusable:A */
 	}
 
 	blk_lpos->begin = begin_lpos;
@@ -1212,7 +1212,7 @@ static char *data_realloc(struct printk_ringbuffer *rb, unsigned int size,
 		 * Store the ID on the wrapped block for consistency.
 		 * The printk_ringbuffer does not actually use it.
 		 */
-		blk->id = id;
+		data_race(blk->id = id); /* see data_make_reusable:A */
 
 		if (!wrapped) {
 			/*
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Nguyen Ngoc Thang

data_alloc() stores the descriptor ID into a newly reserved data block while data_make_reusable(), running from data_push_tail() on another CPU, may still be loading the ID from the same area. The ringbuffer allows this race: a stale or garbage ID simply fails the descriptor lookup or the blk_lpos check. KCSAN nevertheless reports it.

Quoted 8 lines

Annotate the accesses with data_race() instead of READ_ONCE()/WRITE_ONCE(), which would needlessly restrict compiler optimizations for a race that is allowed by design. No functional change.

Reported-by [email protected]
Fixes 896fbe20b4e2 ("printk: use the lockless ringbuffer")
maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Nguyen Ngoc Thang
Quoted 6 lines · Nguyen Ngoc Thang

data_alloc() stores the descriptor ID into a newly reserved data block while data_make_reusable(), running from data_push_tail() on another CPU, may still be loading the ID from the same area. The ringbuffer allows this race: a stale or garbage ID simply fails the descriptor lookup or the blk_lpos check. KCSAN nevertheless reports it.

Quoted 8 lines

Annotate the accesses with data_race() instead of READ_ONCE()/WRITE_ONCE(), which would needlessly restrict compiler optimizations for a race that is allowed by design. No functional change.

Reported-by [email protected]
Fixes 896fbe20b4e2 ("printk: use the lockless ringbuffer")

Looks good to me:

Best Regards,
Petr

maintainer
#3 lore raw
In reply to Nguyen Ngoc Thang
Quoted 6 lines · Nguyen Ngoc Thang

data_alloc() stores the descriptor ID into a newly reserved data block while data_make_reusable(), running from data_push_tail() on another CPU, may still be loading the ID from the same area. The ringbuffer allows this race: a stale or garbage ID simply fails the descriptor lookup or the blk_lpos check. KCSAN nevertheless reports it.

Quoted 8 lines

Annotate the accesses with data_race() instead of READ_ONCE()/WRITE_ONCE(), which would needlessly restrict compiler optimizations for a race that is allowed by design. No functional change.

Reported-by [email protected]
Fixes 896fbe20b4e2 ("printk: use the lockless ringbuffer")

JFYI, the patch has been committed into printk/linux.git, branch for-7.5-trivial.

Best Regards,
Petr