Skip to contentSkip to conversation

block: Use WRITE_ONCE for cache->nr_irq update

PATCH New Zilin Guan 1 participant 1 message Nov 4, 2024 Block layer

lore
Share & tools
lore

Zilin Guan

author
#0 lore raw

PATCH 1/1 block: Use WRITE_ONCE for cache->nr_irq update

In function bio_put_percpu_cache(), cache->nr_irq is read indirectly in line 776 using READ_ONCE(). This ensures safe access in a multi-threaded environment.

776 if (READ_ONCE(cache->nr_irq) + cache->nr > ALLOC_CACHE_MAX)
777 goto out_free;

However, the increment operation is performed directly in line 792 without using WRITE_ONCE(), which can lead to potential inconsistencies in a multi-threaded context.

792 cache->nr_irq++;

To ensure consistent protection against data races, this update should utilize WRITE_ONCE.

---
 block/bio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/bio.c +1 -1
index ac4d77c88932..e2648ba71bac 100644
@@ -789,7 +789,7 @@ static inline void bio_put_percpu_cache(struct bio *bio)
 		bio_uninit(bio);
 		bio->bi_next = cache->free_list_irq;
 		cache->free_list_irq = bio;
-		cache->nr_irq++;
+		WRITE_ONCE(cache->nr_irq, cache->nr_irq + 1);
 	} else {
 		goto out_free;
 	}
Signature · 1 line
2.34.1