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 1/4] memory: Make memory_listeners RCU-safe for real
Date: Sat, 25 Feb 2023 11:31:38 -0500	[thread overview]
Message-ID: <20230225163141.1209368-2-peterx@redhat.com> (raw)
In-Reply-To: <20230225163141.1209368-1-peterx@redhat.com>

I think the plan was making memory_listeners rcu-safe, but maybe not
really.  This patch does it for real, by using RCU variances of qtailq
helpers when modifying memory_listeners.  The modification should be
serialized by BQL, add assertions to register/unregister functions.

Wait for a quiecent period before returning from unregister of memory
listener to make sure any rcu reader is accessing valid listeners.

AddressSpace.listeners are protected in the same way, so do the same.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 softmmu/memory.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/softmmu/memory.c b/softmmu/memory.c
index 9d64efca26..a63e0bcbb7 100644
--- a/softmmu/memory.c
+++ b/softmmu/memory.c
@@ -3016,30 +3016,32 @@ void memory_listener_register(MemoryListener *listener, AddressSpace *as)
 
     /* Only one of them can be defined for a listener */
     assert(!(listener->log_sync && listener->log_sync_global));
+    /* Ownership of memory_listeners & as->listeners for modifications */
+    assert(qemu_mutex_iothread_locked());
 
     listener->address_space = as;
     if (QTAILQ_EMPTY(&memory_listeners)
         || listener->priority >= QTAILQ_LAST(&memory_listeners)->priority) {
-        QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
+        QTAILQ_INSERT_TAIL_RCU(&memory_listeners, listener, link);
     } else {
         QTAILQ_FOREACH(other, &memory_listeners, link) {
             if (listener->priority < other->priority) {
                 break;
             }
         }
-        QTAILQ_INSERT_BEFORE(other, listener, link);
+        QTAILQ_INSERT_BEFORE_RCU(other, listener, link);
     }
 
     if (QTAILQ_EMPTY(&as->listeners)
         || listener->priority >= QTAILQ_LAST(&as->listeners)->priority) {
-        QTAILQ_INSERT_TAIL(&as->listeners, listener, link_as);
+        QTAILQ_INSERT_TAIL_RCU(&as->listeners, listener, link_as);
     } else {
         QTAILQ_FOREACH(other, &as->listeners, link_as) {
             if (listener->priority < other->priority) {
                 break;
             }
         }
-        QTAILQ_INSERT_BEFORE(other, listener, link_as);
+        QTAILQ_INSERT_BEFORE_RCU(other, listener, link_as);
     }
 
     listener_add_address_space(listener, as);
@@ -3051,9 +3053,14 @@ void memory_listener_unregister(MemoryListener *listener)
         return;
     }
 
+    /* Ownership of memory_listeners & as->listeners for modifications */
+    assert(qemu_mutex_iothread_locked());
+
     listener_del_address_space(listener, listener->address_space);
-    QTAILQ_REMOVE(&memory_listeners, listener, link);
-    QTAILQ_REMOVE(&listener->address_space->listeners, listener, link_as);
+    QTAILQ_REMOVE_RCU(&memory_listeners, listener, link);
+    QTAILQ_REMOVE_RCU(&listener->address_space->listeners, listener, link_as);
+    /* Wait for RCU readers to finish.  NOTE!  this may release BQL */
+    drain_call_rcu();
     listener->address_space = NULL;
 }
 
-- 
2.39.1



  reply	other threads:[~2023-02-25 16:32 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 ` Peter Xu [this message]
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 ` [PATCH RFC 3/4] memory: Protect memory_region_clear_dirty_bitmap with RCU Peter Xu
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-2-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).