OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
From: "Denis V. Lunev" <den@openvz.org>
To: andrey.zhadchenko@virtuozzo.com, alexander.ivanov@virtuozzo.com,
	vasileios.almpanis@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2 2/9] dm-qcow2: free md pages after an RCU grace period
Date: Wed, 25 Nov 2026 02:25:18 +0100	[thread overview]
Message-ID: <20261125012525.681662-3-den@openvz.org> (raw)
In-Reply-To: <20261125012525.681662-1-den@openvz.org>

From: Denis V. Lunev <den@openvz.org>

Metadata lookups are going to walk the md_pages tree without
md_pages_lock. Such a walk may still be on a node that is erased and
freed at the same time, e.g. by do_md_page_read_complete() on a failed
read.

Free md pages from an RCU callback and link new nodes with
rb_link_node_rcu(), so a lookup under rcu_read_lock() never touches
freed memory. rb_find_rcu() tolerates concurrent rotations with a
false negative at worst. The rotations of rb_insert_color() may store
a pointer to the new node again, so order its initialization before
them too. Wait for pending callbacks with rcu_barrier() on module
exit.

qcow2_md_page_renumber() erases and relinks a node in place without a
grace period. A walker on it can only miss, and it only moves R1
pages, which lockless lookups never accept.

https://virtuozzo.atlassian.net/browse/VSTOR-95494
Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
 drivers/md/dm-qcow2-target.c | 17 ++++++++++++++---
 drivers/md/dm-qcow2.h        |  1 +
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index b7754f6ca9c4..478e8cdf6d90 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -8,6 +8,7 @@
 #include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/error-injection.h>
+#include <linux/rcupdate.h>
 
 #include "dm.h"
 #include "dm-qcow2.h"
@@ -148,13 +149,20 @@ void qcow2_call_rw_iter(struct qcow2 *qcow2, loff_t pos, unsigned int rw,
 		iocb->ki_complete(iocb, ret);
 }
 
-void qcow2_free_md_page(struct md_page *md)
+static void qcow2_free_md_page_rcu(struct rcu_head *head)
 {
-	WARN_ON_ONCE(md->wbd || md->lockd);
+	struct md_page *md = container_of(head, struct md_page, rcu);
+
 	put_page(md->page);
 	kfree(md);
 }
 
+void qcow2_free_md_page(struct md_page *md)
+{
+	WARN_ON_ONCE(md->wbd || md->lockd);
+	call_rcu(&md->rcu, qcow2_free_md_page_rcu);
+}
+
 static void qcow2_free_md_pages_tree(struct rb_root *root)
 {
 	struct rb_node *node;
@@ -350,7 +358,9 @@ static int md_page_try_insert(struct qcow2 *qcow2, struct md_page *new_md)
 			return -EEXIST;
 	}
 
-	rb_link_node(&new_md->node, parent, node);
+	rb_link_node_rcu(&new_md->node, parent, node);
+	/* Rotations may publish the node again, after its initialization */
+	smp_wmb();
 	rb_insert_color(&new_md->node, root);
 	return 0;
 }
@@ -1280,6 +1290,7 @@ static int __init dm_qcow2_init(void)
 static void __exit dm_qcow2_exit(void)
 {
 	dm_unregister_target(&qcow2_target);
+	rcu_barrier();
 	kmem_cache_destroy(qrq_cache);
 }
 
diff --git a/drivers/md/dm-qcow2.h b/drivers/md/dm-qcow2.h
index 1647ffa363aa..2107499ea947 100644
--- a/drivers/md/dm-qcow2.h
+++ b/drivers/md/dm-qcow2.h
@@ -130,6 +130,7 @@ struct lock_desc {
 
 struct md_page {
 	struct rb_node node;
+	struct rcu_head rcu;
 	u64 id; /* Number of this page starting from hdr */
 #define MD_UPTODATE	(1U << 0) /* Page was read from disk */
 #define MD_DIRTY	(1U << 1) /* Page contains changes and wants writeback */
-- 
2.55.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

  parent reply	other threads:[~2026-11-25  1:26 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  1:25 [Devel] [PATCH hci-8.2 0/9] dm-qcow2: lockless fast path, direct completion, no I/O scheduler Denis V. Lunev
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 1/9] dm-qcow2: move md_pages_lock off the deferred_lock cache line Denis V. Lunev
2026-12-01  0:05   ` no-reply
2026-11-25  1:25 ` Denis V. Lunev [this message]
2026-11-25 11:19   ` [Devel] [PATCH hci-8.2 2/9] dm-qcow2: free md pages after an RCU grace period Konstantin Khorenko
2026-12-01  0:06   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 3/9] dm-qcow2: map fast path reads without md_pages_lock Denis V. Lunev
2026-11-25 11:19   ` Konstantin Khorenko
2026-12-01  0:17   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 4/9] dm-qcow2: map fast path writes " Denis V. Lunev
2026-11-25 11:19   ` Konstantin Khorenko
2026-12-01  0:25   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 5/9] dm-qcow2: submit fast path qios without deferred_lock Denis V. Lunev
2026-12-01  0:27   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 6/9] dm-rq: complete dm-qcow2 requests directly in task context Denis V. Lunev
2026-11-25 11:19   ` Konstantin Khorenko
2026-12-01  0:35   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 7/9] dm-qcow2: requeue requests when the request pool is empty Denis V. Lunev
2026-12-01  0:37   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 8/9] dm-ploop: " Denis V. Lunev
2026-11-25 11:19   ` Konstantin Khorenko
2026-12-01  0:38   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 9/9] dm: default dm-qcow2 devices to no I/O scheduler Denis V. Lunev
2026-11-25 11:19   ` Konstantin Khorenko
2026-12-01  0:39   ` no-reply
2026-11-25 11:19 ` [Devel] [PATCH hci-8.2 0/9] dm-qcow2: lockless fast path, direct completion, " Konstantin Khorenko
2026-11-26 13:58 ` Andrey Zhadchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125012525.681662-3-den@openvz.org \
    --to=den@openvz.org \
    --cc=alexander.ivanov@virtuozzo.com \
    --cc=andrey.zhadchenko@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=vasileios.almpanis@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox