qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Maxim Levitsky" <mlevitsk@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Juan Quintela" <quintela@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"David Hildenbrand" <david@redhat.com>,
	"Dr . David Alan Gilbert" <dgilbert@redhat.com>,
	"Chuang Xu" <xuchuangxclwt@bytedance.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	peterx@redhat.com
Subject: [PATCH RFC 3/4] memory: Protect memory_region_clear_dirty_bitmap with RCU
Date: Sat, 25 Feb 2023 11:31:40 -0500	[thread overview]
Message-ID: <20230225163141.1209368-4-peterx@redhat.com> (raw)
In-Reply-To: <20230225163141.1209368-1-peterx@redhat.com>

Clear dirty bitmap operation needs to walk memory_listeners but the context
may not hold BQL.

These callers hold BQL for it:
        cpu_physical_memory_sync_dirty_bitmap
        dirtyrate_manual_reset_protect

These callers hold RCU for it:
        migration_clear_memory_region_dirty_bitmap [1]
        cpu_physical_memory_test_and_clear_dirty

The above case [1] is extremely unobvious and probably still buggy,
because:

  - Either the RCU read lock was taken very high from the stack in
  ram_save_iterate() or ram_save_queue_pages() where the RCU lock was
  probably taken for the sake of ramblock references (which is also
  protected by RCU), or,

  - I _think_ there's path that leaks taking any lock (e.g. the other path
  migration_clear_memory_region_dirty_bitmap_range which is used by
  virtio-mem or virtio-balloon that may or may not really take RCU at all,
  neither BQL).

Add the RCU read lock in memory_region_clear_dirty_bitmap() to make sure
it's not missed.

The RCU is also needed for address_space_get_flatview(), so this will
generally making the RCU section larger to cover the whole walking process
when not taken, but wanted.

This should be the only place that we referenced memory_listeners (or
as->listeners) without guaranteed to hold BQL nor RCU.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 softmmu/memory.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/softmmu/memory.c b/softmmu/memory.c
index c48e9cc6ed..95cdcaeccf 100644
--- a/softmmu/memory.c
+++ b/softmmu/memory.c
@@ -2270,7 +2270,8 @@ void memory_region_clear_dirty_bitmap(MemoryRegion *mr, hwaddr start,
     FlatRange *fr;
     hwaddr sec_start, sec_end, sec_size;
 
-    QTAILQ_FOREACH(listener, &memory_listeners, link) {
+    RCU_READ_LOCK_GUARD();
+    QTAILQ_FOREACH_RCU(listener, &memory_listeners, link) {
         if (!listener->log_clear) {
             continue;
         }
-- 
2.39.1



  parent reply	other threads:[~2023-02-25 16:33 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-25 16:31 [PATCH RFC 0/4] memory: Fix (/ Discuss) a few rcu issues Peter Xu
2023-02-25 16:31 ` [PATCH RFC 1/4] memory: Make memory_listeners RCU-safe for real Peter Xu
2023-02-25 16:31 ` [PATCH RFC 2/4] memory: Use rcu list variance for address_spaces modifications Peter Xu
2023-02-25 16:31 ` Peter Xu [this message]
2023-02-25 16:31 ` [PATCH RFC 4/4] memory: Use rcu traversal in memory_region_to_address_space Peter Xu
2023-03-01  0:09 ` [PATCH RFC 0/4] memory: Fix (/ Discuss) a few rcu issues Stefan Hajnoczi
2023-03-01 16:08   ` Peter Xu
2023-03-02  9:46 ` David Hildenbrand
2023-03-02 14:45   ` Peter Xu
2023-03-02 14:56     ` Peter Xu
2023-03-02 15:11     ` David Hildenbrand
2023-03-02 21:50       ` Peter Xu
2023-03-03  9:10         ` David Hildenbrand
2023-03-03 16:20           ` Peter Xu
2023-03-03 16:58             ` David Hildenbrand

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=20230225163141.1209368-4-peterx@redhat.com \
    --to=peterx@redhat.com \
    --cc=david@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=mlevitsk@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=stefanha@redhat.com \
    --cc=xuchuangxclwt@bytedance.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;
as well as URLs for NNTP newsgroup(s).