[Devel] [PATCH RHEL9 COMMIT] dm-ploop: Use READ_ONCE/WRITE_ONCE to access md page data

Konstantin Khorenko khorenko at virtuozzo.com
Mon Jan 27 16:12:32 MSK 2025


The commit is pushed to "branch-rh9-5.14.0-427.44.1.vz9.80.x-ovz" and will appear at git at bitbucket.org:openvz/vzkernel.git
after rh9-5.14.0-427.44.1.vz9.80.6
------>
commit 329b26c848f1c0a6dce91678dc87b13b36e5ff8c
Author: Alexander Atanasov <alexander.atanasov at virtuozzo.com>
Date:   Fri Jan 24 17:35:36 2025 +0200

    dm-ploop: Use READ_ONCE/WRITE_ONCE to access md page data
    
    Prepare to reduce locking by using atomic 32 bit access to the fields.
    To ensure this we need to use the _ONCE macros.
    
    https://virtuozzo.atlassian.net/browse/VSTOR-91659
    Signed-off-by: Alexander Atanasov <alexander.atanasov at virtuozzo.com>
    
    ======
    Patchset description:
    ploop: optimistations and scalling
    
    Ploop processes requsts in a different threads in parallel
    where possible which results in significant improvement in
    performance and makes further optimistations possible.
    
    Known bugs:
      - delayed metadata writeback is not working and is missing error handling
         - patch to disable it until fixed
      - fast path is not working - causes rcu lockups - patch to disable it
    
    Further improvements:
      - optimize md pages lookups
    
    Alexander Atanasov (50):
      dm-ploop: md_pages map all pages at creation time
      dm-ploop: Use READ_ONCE/WRITE_ONCE to access md page data
      dm-ploop: fsync after all pios are sent
      dm-ploop: move md status to use proper bitops
      dm-ploop: convert wait_list and wb_batch_llist to use lockless lists
      dm-ploop: convert enospc handling to use lockless lists
      dm-ploop: convert suspended_pios list to use lockless list
      dm-ploop: convert the rest of the lists to use llist variant
      dm-ploop: combine processing of pios thru prepare list and remove
        fsync worker
      dm-ploop: move from wq to kthread
      dm-ploop: move preparations of pios into the caller from worker
      dm-ploop: fast path execution for reads
      dm-ploop: do not use a wrapper for set_bit to make a page writeback
      dm-ploop: BAT use only one list for writeback
      dm-ploop: make md writeback timeout to be per page
      dm-ploop: add interface to disable bat writeback delay
      dm-ploop: convert wb_batch_list to lockless variant
      dm-ploop: convert high_prio to status
      dm-ploop: split cow processing into two functions
      dm-ploop: convert md page rw lock to spin lock
      dm-ploop: convert bat_rwlock to bat_lock spinlock
      dm-ploop: prepare bat updates under bat_lock
      dm-ploop: make ploop_bat_write_complete ready for parallel pio
        completion
      dm-ploop: make ploop_submit_metadata_writeback return number of
        requests sent
      dm-ploop: introduce pio runner threads
      dm-ploop: add pio list ids to be used when passing pios to runners
      dm-ploop: process pios via runners
      dm-ploop: disable metadata writeback delay
      dm-ploop: disable fast path
      dm-ploop: use lockless lists for chained cow updates list
      dm-ploop: use lockless lists for data ready pios
      dm-ploop: give runner threads better name
      dm-ploop: resize operation - add holes bitmap locking
      dm-ploop: remove unnecessary operations
      dm-ploop: use filp per thread
      dm-ploop: catch if we try to advance pio past bio end
      dm-ploop: support REQ_FUA for data pios
      dm-ploop: proplerly access nr_bat_entries
      dm-ploop: fix locking and improve error handling when submitting pios
      dm-ploop: fix how ENOTBLK is handled
      dm-ploop: sync when suspended or stopping
      dm-ploop: rework bat completion logic
      dm-ploop: rework logic in pio processing
      dm-ploop: end fsync pios in parallel
      dm-ploop: make filespace preallocations async
      dm-ploop: resubmit enospc pios from dispatcher thread
      dm-ploop: dm-ploop: simplify discard completion
      dm-ploop: use GFP_ATOMIC instead of GFP_NOIO
      dm-ploop: fix locks used in mixed context
      dm-ploop: fix how current flags are managed inside threads
    
    Andrey Zhadchenko (13):
      dm-ploop: do not flush after metadata writes
      dm-ploop: set IOCB_DSYNC on all FUA requests
      dm-ploop: remove extra ploop_cluster_is_in_top_delta()
      dm-ploop: introduce per-md page locking
      dm-ploop: reduce BAT accesses on discard completion
      dm-ploop: simplify llseek
      dm-ploop: speed up ploop_prepare_bat_update()
      dm-ploop: make new allocations immediately visible in BAT
      dm-ploop: drop ploop_cluster_is_in_top_delta()
      dm-ploop: do not wait for BAT update for non-FUA requests
      dm-ploop: add delay for metadata writeback
      dm-ploop: submit all postponed metadata on REQ_OP_FLUSH
      dm-ploop: handle REQ_PREFLUSH
    
    Feature: dm-ploop: ploop target driver
---
 drivers/md/dm-ploop-bat.c | 20 ++++++++++----------
 drivers/md/dm-ploop-cmd.c | 28 ++++++++++++++--------------
 drivers/md/dm-ploop-map.c | 24 +++++++++++++-----------
 drivers/md/dm-ploop.h     |  2 +-
 4 files changed, 38 insertions(+), 36 deletions(-)

diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index f28bd5d7b322..886a05cdd23a 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -143,9 +143,9 @@ bool ploop_try_update_bat_entry(struct ploop *ploop, u32 clu, u8 level, u32 dst_
 
 	clu = ploop_bat_clu_idx_in_page(clu); /* relative offset */
 
-	if (md->bat_levels[clu] == level) {
+	if (READ_ONCE(md->bat_levels[clu]) == level) {
 		bat_entries = md->kmpage;
-		bat_entries[clu] = dst_clu;
+		WRITE_ONCE(bat_entries[clu], dst_clu);
 		return true;
 	}
 	return false;
@@ -287,11 +287,11 @@ static int ploop_convert_bat_entries(struct ploop *ploop, struct rb_root *md_roo
 		page_id++;
 
 		for (; i <= end; i++) {
-			if (bat_entries[i] > max_file_clu)
+			if (READ_ONCE(bat_entries[i]) > max_file_clu)
 				ret = -EPROTO;
-			if (!bat_entries[i])
-				bat_entries[i] = BAT_ENTRY_NONE;
-			if (bat_entries[i] < bat_clusters)
+			if (!READ_ONCE(bat_entries[i]))
+				WRITE_ONCE(bat_entries[i], BAT_ENTRY_NONE);
+			if (READ_ONCE(bat_entries[i]) < bat_clusters)
 				ret = -EXDEV;
 		}
 
@@ -431,21 +431,21 @@ static void ploop_apply_delta_mappings(struct ploop *ploop,
 			if (clu == nr_be - 1)
 				stop = true;
 
-			if (bat_entries[i] != BAT_ENTRY_NONE) {
+			if (READ_ONCE(bat_entries[i]) != BAT_ENTRY_NONE) {
 				/* md0 is already populated */
 				WARN_ON_ONCE(md->id && is_top_level);
 				goto set_not_hole;
 			}
 
 			if (!is_raw)
-				dst_clu = d_bat_entries[i];
+				dst_clu = READ_ONCE(d_bat_entries[i]);
 			else
 				dst_clu = clu;
 
 			if (dst_clu == BAT_ENTRY_NONE)
 				continue;
-			md->bat_levels[i] = level;
-			bat_entries[i] = dst_clu;
+			WRITE_ONCE(md->bat_levels[i], level);
+			WRITE_ONCE(bat_entries[i], dst_clu);
 set_not_hole:
 			if (is_top_level)
 				ploop_set_not_hole(ploop, bat_entries[i]);
diff --git a/drivers/md/dm-ploop-cmd.c b/drivers/md/dm-ploop-cmd.c
index d9d41d79d937..25ca9db4eeb8 100644
--- a/drivers/md/dm-ploop-cmd.c
+++ b/drivers/md/dm-ploop-cmd.c
@@ -47,7 +47,7 @@ static void ploop_advance_holes_bitmap(struct ploop *ploop,
 		for (; i <= end; i++) {
 			if (!ploop_md_page_cluster_is_in_top_delta(ploop, md, i))
 				continue;
-			dst_clu = bat_entries[i];
+			dst_clu = READ_ONCE(bat_entries[i]);
 			/* This may happen after grow->shrink->(now) grow */
 			if (dst_clu < ploop->hb_nr &&
 			    test_bit(dst_clu, ploop->holes_bitmap)) {
@@ -162,7 +162,7 @@ static u32 ploop_find_bat_entry(struct ploop *ploop, u32 dst_clu, bool *is_locke
 		ploop_init_be_iter(ploop, md->id, &i, &end);
 		bat_entries = md->kmpage;
 		for (; i <= end; i++) {
-			if (bat_entries[i] != dst_clu)
+			if (READ_ONCE(bat_entries[i]) != dst_clu)
 				continue;
 			if (ploop_md_page_cluster_is_in_top_delta(ploop, md, i)) {
 				clu = ploop_page_clu_idx_to_bat_clu(md->id, i);
@@ -735,17 +735,17 @@ static void notify_delta_merged(struct ploop *ploop, u8 level,
 				stop = true;
 
 			/* deltas above @level become renumbered */
-			if (bat_entries[i] != BAT_ENTRY_NONE &&
-			    md->bat_levels[i] > level) {
+			if (READ_ONCE(bat_entries[i]) != BAT_ENTRY_NONE &&
+			    READ_ONCE(md->bat_levels[i]) > level) {
 				md->bat_levels[i]--;
 				continue;
 			}
 
-			if (bat_entries[i] != BAT_ENTRY_NONE &&
-			     md->bat_levels[i] < level)
+			if (READ_ONCE(bat_entries[i]) != BAT_ENTRY_NONE &&
+			     READ_ONCE(md->bat_levels[i]) < level)
 				continue;
 
-			if (d_bat_entries[i] == BAT_ENTRY_NONE) {
+			if (READ_ONCE(d_bat_entries[i]) == BAT_ENTRY_NONE) {
 				WARN_ON_ONCE(bat_entries[i] != BAT_ENTRY_NONE);
 				continue;
 			}
@@ -757,9 +757,9 @@ static void notify_delta_merged(struct ploop *ploop, u8 level,
 			 */
 			WRITE_ONCE(bat_entries[i], READ_ONCE(d_bat_entries[i]));
 			if (!forward)
-				md->bat_levels[i] = level - 1;
+				WRITE_ONCE(md->bat_levels[i], level - 1);
 			else
-				md->bat_levels[i] = level;
+				WRITE_ONCE(md->bat_levels[i], level);
 		}
 		if (stop)
 			break;
@@ -985,13 +985,13 @@ static int process_flip_upper_deltas(struct ploop *ploop)
 		ploop_init_be_iter(ploop, md->id, &i, &end);
 		bat_entries = md->kmpage;
 		for (; i <= end; i++) {
-			if (bat_entries[i] == BAT_ENTRY_NONE)
+			if (READ_ONCE(bat_entries[i]) == BAT_ENTRY_NONE)
 				continue;
-			if (md->bat_levels[i] == level) {
-				md->bat_levels[i] = ploop_top_level(ploop);
+			if (READ_ONCE(md->bat_levels[i]) == level) {
+				WRITE_ONCE(md->bat_levels[i], ploop_top_level(ploop));
 				clear_bit(bat_entries[i], holes_bitmap);
-			} else if (md->bat_levels[i] == ploop_top_level(ploop)) {
-				md->bat_levels[i] = level;
+			} else if (READ_ONCE(md->bat_levels[i]) == ploop_top_level(ploop)) {
+				WRITE_ONCE(md->bat_levels[i], level);
 			}
 		}
 	}
diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
index 634f0679be3f..221e97ba84a9 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -723,9 +723,9 @@ static void ploop_release_cluster(struct ploop *ploop, u32 clu)
 	clu = ploop_bat_clu_idx_in_page(clu); /* relative to page */
 
 	bat_entries = md->kmpage;
-	dst_clu = bat_entries[clu];
-	bat_entries[clu] = BAT_ENTRY_NONE;
-	md->bat_levels[clu] = 0;
+	dst_clu = READ_ONCE(bat_entries[clu]);
+	WRITE_ONCE(bat_entries[clu], BAT_ENTRY_NONE);
+	WRITE_ONCE(md->bat_levels[clu], 0);
 
 	ploop_hole_set_bit(dst_clu, ploop);
 }
@@ -793,8 +793,8 @@ static void ploop_advance_local_after_bat_wb(struct ploop *ploop,
 		}
 
 		if (success) {
-			bat_entries[i] = dst_clu[i];
-			md->bat_levels[i] = ploop_top_level(ploop);
+			WRITE_ONCE(bat_entries[i], READ_ONCE(dst_clu[i]));
+			WRITE_ONCE(md->bat_levels[i], ploop_top_level(ploop));
 		} else {
 			ploop_hole_set_bit(i + off, ploop);
 		}
@@ -977,7 +977,7 @@ static void ploop_bat_page_zero_cluster(struct ploop *ploop,
 	clu = ploop_bat_clu_idx_in_page(clu);
 
 	to = kmap_local_page(piwb->bat_page);
-	to[clu] = 0;
+	WRITE_ONCE(to[clu], 0);
 	kunmap_local(to);
 }
 
@@ -1085,14 +1085,16 @@ static int ploop_alloc_cluster(struct ploop *ploop, struct ploop_index_wb *piwb,
 	bool already_alloced = false;
 	map_index_t *to;
 	int ret = 0;
+	u32 tmp_clu;
 
 	/* Cluster index related to the page[page_id] start */
 	clu -= piwb->page_id * PAGE_SIZE / sizeof(map_index_t) - PLOOP_MAP_OFFSET;
 
 	to = kmap_local_page(piwb->bat_page);
-	if (to[clu]) {
+	tmp_clu = READ_ONCE(to[clu]);
+	if (tmp_clu) {
 		/* Already mapped by one of previous bios */
-		*dst_clu = to[clu];
+		*dst_clu = tmp_clu;
 		already_alloced = true;
 	}
 	kunmap_local(to);
@@ -1105,7 +1107,7 @@ static int ploop_alloc_cluster(struct ploop *ploop, struct ploop_index_wb *piwb,
 		goto out;
 
 	to = kmap_local_page(piwb->bat_page);
-	to[clu] = *dst_clu;
+	WRITE_ONCE(to[clu], *dst_clu);
 	kunmap_local(to);
 out:
 	return ret;
@@ -1388,7 +1390,7 @@ static void ploop_submit_cow_index_wb(struct ploop_cow *cow)
 
 	to = kmap_local_page(piwb->bat_page);
 	WARN_ON(to[clu]);
-	to[clu] = cow->dst_clu;
+	WRITE_ONCE(to[clu], cow->dst_clu);
 	kunmap_local(to);
 
 	/* Prevent double clearing of holes_bitmap bit on complete_cow() */
@@ -1725,7 +1727,7 @@ static void ploop_process_one_discard_pio(struct ploop *ploop, struct pio *pio)
 		pio->bi_status = BLK_STS_IOERR;
 		goto err;
 	} else {
-		to[clu] = 0;
+		WRITE_ONCE(to[clu], 0);
 		list_add_tail(&pio->list, &piwb->ready_data_pios);
 	}
 	kunmap_local(to);
diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h
index 3783d42a6162..b42baccf9a00 100644
--- a/drivers/md/dm-ploop.h
+++ b/drivers/md/dm-ploop.h
@@ -427,7 +427,7 @@ static inline u32 ploop_bat_entries(struct ploop *ploop, u32 clu,
 	clu = ploop_bat_clu_idx_in_page(clu);
 
 	if (bat_level)
-		*bat_level = md->bat_levels[clu];
+		*bat_level = READ_ONCE(md->bat_levels[clu]);
 	if (md_ret)
 		*md_ret = md;
 


More information about the Devel mailing list