Skip to contentSkip to conversation

prepare unplug out of protection of global lock

PATCH Superseded Liu Ping Fan 10 participants 72 messages Sep 11, 2012 – Sep 21, 2012 Atomic infrastructure

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover prepare unplug out of protection of global lock

v1:
https://lists.gnu.org/archive/html/qemu-devel/2012-07/msg03312.html

v2:
http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01275.html

changes v2->v3

--remove refcnt on MemroyRegion
--add ref/unref for MemoryRegionOps
--make memory view protected by mem_map_lock, delay to adopt rcu style.

Liu Ping Fan (11):
atomic: introduce atomic operations
qom: apply atomic on object's refcount
hotplug: introduce qdev_unplug_complete() to remove device from views
pci: remove pci device from mem view when unplug
memory: introduce ref,unref interface for MemoryRegionOps
memory: make mmio dispatch able to be out of biglock
memory: implement e1000's MemoryRegionOps ref/unref
qom: introduce reclaimer to release obj in async
vcpu: make QemuThread as tls to store thread-self info
vcpu: introduce lockmap
vcpu: push mmio dispatcher out of big lock

 cpus.c                   |   19 +++++++++
 exec.c                   |   95 +++++++++++++++++++++++++++++++++++++++++++++-
 hw/acpi_piix4.c          |    2 +-
 hw/e1000.c               |   17 ++++++++
 hw/pci.c                 |   13 ++++++-
 hw/pci.h                 |    1 +
 hw/qdev.c                |   26 ++++++++++++
 hw/qdev.h                |    3 +-
 include/qemu/atomic.h    |   63 ++++++++++++++++++++++++++++++
 include/qemu/object.h    |    3 +-
 include/qemu/reclaimer.h |   30 ++++++++++++++
 kvm-all.c                |    3 +
 main-loop.c              |    5 ++
 memory.h                 |    3 +
 qemu-thread-posix.c      |   30 ++++++++++++++
 qemu-thread-posix.h      |    7 +++
 qemu-thread.h            |    4 ++
 qemu-tool.c              |    5 ++
 qom/Makefile.objs        |    2 +-
 qom/object.c             |   11 ++---
 qom/reclaimer.c          |   54 ++++++++++++++++++++++++++
 vl.c                     |    4 ++
 22 files changed, 388 insertions(+), 12 deletions(-)
 create mode 100644 include/qemu/atomic.h
 create mode 100644 include/qemu/reclaimer.h
 create mode 100644 qom/reclaimer.c
Signature · 1 line
1.7.4.4
#1 lore raw

PATCH v3 1/11 atomic: introduce atomic operations

From: Liu Ping Fan <[email protected]>

If out of global lock, we will be challenged by SMP in low level, so need atomic ops.

This file is a wrapper of GCC atomic builtin.

---
 include/qemu/atomic.h |   63 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 63 insertions(+), 0 deletions(-)
 create mode 100644 include/qemu/atomic.h
include/qemu/atomic.h new file +63 -0
new file mode 100644
index 0000000..f17145d
@@ -0,0 +1,63 @@
+/*
+ * Simple wrapper of gcc Atomic-Builtins
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#ifndef __QEMU_ATOMIC_H
+#define __QEMU_ATOMIC_H
+
+typedef struct Atomic {
+    int counter;
+} Atomic;
+
+static inline void atomic_set(Atomic *v, int i)
+{
+    v->counter = i;
+}
+
+static inline int atomic_read(Atomic *v)
+{
+    return v->counter;
+}
+
+static inline int atomic_return_and_add(int i, Atomic *v)
+{
+    int ret;
+
+    ret = __sync_fetch_and_add(&v->counter, i);
+    return ret;
+}
+
+static inline int atomic_return_and_sub(int i, Atomic *v)
+{
+    int ret;
+
+    ret = __sync_fetch_and_sub(&v->counter, i);
+    return ret;
+}
+
+/**
+ *  * atomic_inc - increment atomic variable
+ *   * @v: pointer of type Atomic
+ *    *
+ *     * Atomically increments @v by 1.
+ *      */
+static inline void atomic_inc(Atomic *v)
+{
+    __sync_fetch_and_add(&v->counter, 1);
+}
+
+/**
+ *  * atomic_dec - decrement atomic variable
+ *   * @v: pointer of type Atomic
+ *    *
+ *     * Atomically decrements @v by 1.
+ *      */
+static inline void atomic_dec(Atomic *v)
+{
+    __sync_fetch_and_sub(&v->counter, 1);
+}
+
+#endif
Signature · 1 line
1.7.4.4
#2 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

Quoted 22 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

If out of global lock, we will be challenged by SMP in low level, so need atomic ops.

This file is a wrapper of GCC atomic builtin.

---
 include/qemu/atomic.h |   63 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 63 insertions(+), 0 deletions(-)
 create mode 100644 include/qemu/atomic.h
include/qemu/atomic.h new file +3 -0
new file mode 100644
index 0000000..f17145d
@@ -0,0 +1,63 @@
+/*
+ * Simple wrapper of gcc Atomic-Builtins
+ *

include/qemu/atomic.h line 13

Quoted 10 lines
include/qemu/atomic.h new file +10 -0
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#ifndef __QEMU_ATOMIC_H
+#define __QEMU_ATOMIC_H
+
+typedef struct Atomic {
+    int counter;
+} Atomic;

Best to mark counter 'volatile'.

include/qemu/atomic.h line 23

Quoted 11 lines
include/qemu/atomic.h new file +10 -0
+
+static inline void atomic_set(Atomic *v, int i)
+{
+    v->counter = i;
+}
+
+static inline int atomic_read(Atomic *v)
+{
+    return v->counter;
+}

So these two operations don't get mangled by the optimizer.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

Quoted 36 lines · Avi Kivity
Quoted 22 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

If out of global lock, we will be challenged by SMP in low level, so need atomic ops.

This file is a wrapper of GCC atomic builtin.

---
 include/qemu/atomic.h |   63 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 63 insertions(+), 0 deletions(-)
 create mode 100644 include/qemu/atomic.h
include/qemu/atomic.h new file +3 -0
new file mode 100644
index 0000000..f17145d
@@ -0,0 +1,63 @@
+/*
+ * Simple wrapper of gcc Atomic-Builtins
+ *

include/qemu/atomic.h line 13

Quoted 10 lines
include/qemu/atomic.h new file +10 -0
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#ifndef __QEMU_ATOMIC_H
+#define __QEMU_ATOMIC_H
+
+typedef struct Atomic {
+    int counter;
+} Atomic;

Best to mark counter 'volatile'.

Quoted 14 lines

include/qemu/atomic.h line 23

Quoted 11 lines
include/qemu/atomic.h new file +10 -0
+
+static inline void atomic_set(Atomic *v, int i)
+{
+    v->counter = i;
+}
+
+static inline int atomic_read(Atomic *v)
+{
+    return v->counter;
+}

So these two operations don't get mangled by the optimizer.

Browsing linux code and reading lkml, find some similar material. But they have moved volatile from ->counter to function - atomic_read(). As to atomic_read(), I think it need to prevent optimizer from refetching issue, but as to atomic_set(), do we need ?

Regards,
pingfan

Quoted 5 lines

--
error compiling committee.c: too many arguments to function

#4 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

Quoted 20 lines · liu ping fan
Quoted 6 lines

include/qemu/atomic.h line 13

Quoted 3 lines
include/qemu/atomic.h new file +3 -0
+typedef struct Atomic {
+    int counter;
+} Atomic;

Best to mark counter 'volatile'.

Quoted 14 lines

include/qemu/atomic.h line 23

Quoted 11 lines
include/qemu/atomic.h new file +10 -0
+
+static inline void atomic_set(Atomic *v, int i)
+{
+    v->counter = i;
+}
+
+static inline int atomic_read(Atomic *v)
+{
+    return v->counter;
+}

So these two operations don't get mangled by the optimizer.

Quoted 4 lines

Browsing linux code and reading lkml, find some similar material. But they have moved volatile from ->counter to function - atomic_read(). As to atomic_read(), I think it need to prevent optimizer from refetching issue, but as to atomic_set(), do we need ?

I think so, to prevent reordering.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

Il 13/09/2012 10:14, Avi Kivity ha scritto:

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines

include/qemu/atomic.h line 23

Quoted 10 lines
include/qemu/atomic.h new file +9 -0
+static inline void atomic_set(Atomic *v, int i)
+{
+    v->counter = i;
+}
+
+static inline int atomic_read(Atomic *v)
+{
+    return v->counter;
+}

So these two operations don't get mangled by the optimizer.

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

Browsing linux code and reading lkml, find some similar material. But they have moved volatile from ->counter to function - atomic_read(). As to atomic_read(), I think it need to prevent optimizer from refetching issue, but as to atomic_set(), do we need ?

I think so, to prevent reordering.

Agreed. Alternatively, stick a barrier() before and after the assignment and read.

But I don't really see the point in wrapping atomically-accessed variables in a struct. Are we going to add a variant for long, a variant for pointers, etc.?

I already proposed my version of this at
http://github.com/bonzini/qemu/commit/1b439343.

Paolo

#6 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

Quoted 18 lines · Paolo Bonzini

Il 13/09/2012 10:14, Avi Kivity ha scritto:

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
include/qemu/atomic.h new file +5 -0
+static inline void atomic_set(Atomic *v, int i)
+{
+    v->counter = i;
+}
+
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

include/qemu/atomic.h line 23

Quoted 5 lines
include/qemu/atomic.h new file +4 -0
+static inline int atomic_read(Atomic *v)
+{
+    return v->counter;
+}

So these two operations don't get mangled by the optimizer.

Browsing linux code and reading lkml, find some similar material. But they have moved volatile from ->counter to function - atomic_read(). As to atomic_read(), I think it need to prevent optimizer from refetching issue, but as to atomic_set(), do we need ?

Quoted 7 lines
Quoted 1 line

I think so, to prevent reordering.

Agreed. Alternatively, stick a barrier() before and after the assignment and read.

But I don't really see the point in wrapping atomically-accessed variables in a struct.

Preventing accidental naked access (to be reported in patch review as "wardrobe malfunction").

Quoted 2 lines

Are we going to add a variant for long, a
variant for pointers, etc.?

template <typename T> ...;

Quoted 2 lines

I already proposed my version of this at
http://github.com/bonzini/qemu/commit/1b439343.

Atomic operations are sufficiently rare and sufficiently important to warrant extra effort, so I prefer the explicitly typed variants.

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

Il 13/09/2012 10:23, Avi Kivity ha scritto:

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

But I don't really see the point in wrapping atomically-accessed variables in a struct.

Preventing accidental naked access (to be reported in patch review as "wardrobe malfunction").

Yeah, I understand that, but it's rare enough that I don't think it's worth the complication. With C++ and templates it would be a different story.

Paolo

#8 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

In reply to Paolo Bonzini
Quoted 14 lines · Paolo Bonzini

Il 13/09/2012 10:14, Avi Kivity ha scritto:

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines

include/qemu/atomic.h line 23

Quoted 10 lines
include/qemu/atomic.h new file +9 -0
+static inline void atomic_set(Atomic *v, int i)
+{
+    v->counter = i;
+}
+
+static inline int atomic_read(Atomic *v)
+{
+    return v->counter;
+}

So these two operations don't get mangled by the optimizer.

Quoted 9 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

Browsing linux code and reading lkml, find some similar material. But they have moved volatile from ->counter to function - atomic_read(). As to atomic_read(), I think it need to prevent optimizer from refetching issue, but as to atomic_set(), do we need ?

I think so, to prevent reordering.

Agreed. Alternatively, stick a barrier() before and after the assignment and read.

Yes, the linux just leave this barrier() as caller's choice at the exact point, not right in atomic_set(). And embed barrier() in atomic_set() can easy the caller's job.

Thanks and regards,
pingfan

Quoted 8 lines

But I don't really see the point in wrapping atomically-accessed variables in a struct. Are we going to add a variant for long, a variant for pointers, etc.?

I already proposed my version of this at
http://github.com/bonzini/qemu/commit/1b439343.

Paolo

#9 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

In reply to Avi Kivity
Quoted 22 lines · Avi Kivity
Quoted 20 lines · liu ping fan
Quoted 20 lines

include/qemu/atomic.h line 13

Quoted 3 lines
include/qemu/atomic.h new file +3 -0
+typedef struct Atomic {
+    int counter;
+} Atomic;

Best to mark counter 'volatile'.

include/qemu/atomic.h line 23

Quoted 11 lines
include/qemu/atomic.h new file +10 -0
+
+static inline void atomic_set(Atomic *v, int i)
+{
+    v->counter = i;
+}
+
+static inline int atomic_read(Atomic *v)
+{
+    return v->counter;
+}

So these two operations don't get mangled by the optimizer.

Quoted 6 lines
Quoted 4 lines

Browsing linux code and reading lkml, find some similar material. But they have moved volatile from ->counter to function - atomic_read(). As to atomic_read(), I think it need to prevent optimizer from refetching issue, but as to atomic_set(), do we need ?

I think so, to prevent reordering.

Hi,

I don't think volatile makes any difference to reordering here.

The compiler is not going to move the atomic_set() store before or after another instruction on the same atomic variable anyway, just like it wouldn't do that for an ordinary assignment.

If you're concerned about ordering with respect to other memory, then volatile wouldn't make much difference. barrier() before and after would.

If you're copying Linux's semantics, Linux's atomic_set() doesn't include any barriers, nor imply any. atomic_read() uses volatile to ensure that each call re-reads the value, for example in a loop. (Same as ACCESS_ONCE()). If there was a call to atomic_set() in a loop, it doesn't guarantee that will be written each time.

-- Jamie

#10 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

In reply to liu ping fan
Quoted 4 lines · liu ping fan
Quoted 4 lines

include/qemu/atomic.h line 18

Quoted 4 lines
include/qemu/atomic.h new file +4 -0
+static inline void atomic_set(Atomic *v, int i)
+{
+    v->counter = i;
+}

Hi,

When running on ARM Linux kernels prior to 2.6.32, userspace atomic_set() needs to use "clrex" or "strex" too.

See Linux commit 200b812d, "Clear the exclusive monitor when returning from an exception".

You can see ARM's atomic_set() used to use "strex", and warns it's important. The kernel patch allows atomic_set() to be simplified, and that includes for userspace, by putting clrex/strex in the exception return path instead.

However, someone may run QEMU on a kernel before 2.6.32, which isn't that old. (E.g. my phone is running 2.6.28).

Otherwise you can have this situation:

Initially: a = 0.

Thread
atomic_inc(&a, 1)
= ldrex, add, [strex interrupted]

Interrupted by signal handler
atomic_set(&a, 3) = str
Signal return

Resume thread
= strex (succeeds because CPU-local exclusive-flag still set)

Result: a = 1, should be impossible when the signal triggered, and
information about the signal is lost.

A more realistic example would use atomic_compare_exchange(), to atomic-read-and-clear, atomic-read-and-dec-if-not-zero a variable set in a signal handler, however I've used atomic_inc() to illustrate because that's in your patch.

Best,
-- Jamie

#11 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

Quoted 2 lines · Jamie Lokier

However, someone may run QEMU on a kernel before 2.6.32, which isn't that old. (E.g. my phone is running 2.6.28).

NB that ARM kernels that old have other amusing bugs, such as not saving the floating point registers when invoking signal handlers. I would be happy for QEMU to just say "your kernel is too old!"...

-- PMM

#12 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

Quoted 7 lines · Peter Maydell
Quoted 2 lines · Jamie Lokier

However, someone may run QEMU on a kernel before 2.6.32, which isn't that old. (E.g. my phone is running 2.6.28).

NB that ARM kernels that old have other amusing bugs, such as not saving the floating point registers when invoking signal handlers.

Hi Peter,

It's not that old (< 3 years). Granted that's not a nice one, but I'm under the impression it occurs only when the signal handler uses (VFP hardware) floating point. I.e. most programs don't do that, they keep the signal handlers simple (probably including QEMU).

(I've read about other platforms that have similar issues using floating point in signal handlers; best avoided.)

Anyway, people are running those kernels, someone will try to run QEMU on it unless...

Quoted 1 line

I would be happy for QEMU to just say "your kernel is too old!"...

I'd be quite happy with that as well, if you want to put a check in and refuse to run (like Glibc does).

Less happy with obscure, rare failures of atomicity that are practically undebuggable, and easily fixed.

Cheers,
-- Jamie

#13 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

In reply to Liu Ping Fan

include/qemu/atomic.h line 46

Quoted 7 lines · Liu Ping Fan
include/qemu/atomic.h new file +7 -0
+
+/**
+ *  * atomic_inc - increment atomic variable
+ *   * @v: pointer of type Atomic
+ *    *
+ *     * Atomically increments @v by 1.
+ *      */

Your editor has done something weird with these comments.

-- PMM

#14 lore raw

Re: [Qemu-devel] [PATCH V3 01/11] atomic: introduce atomic operations

Quoted 11 lines · Peter Maydell

include/qemu/atomic.h line 46

Quoted 7 lines · Liu Ping Fan
include/qemu/atomic.h new file +7 -0
+
+/**
+ *  * atomic_inc - increment atomic variable
+ *   * @v: pointer of type Atomic
+ *    *
+ *     * Atomically increments @v by 1.
+ *      */

Your editor has done something weird with these comments.

Yes, will fix it. Thanx, pingfan

Quoted 1 line

-- PMM

#15 lore raw

PATCH v3 2/11 qom: apply atomic on object's refcount

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

---
 include/qemu/object.h |    3 ++-
 qom/object.c          |   11 +++++------
 2 files changed, 7 insertions(+), 7 deletions(-)
include/qemu/object.h +2 -1
index cc75fee..0c02614 100644
@@ -18,6 +18,7 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include "qemu-queue.h"
+#include "qemu/atomic.h"
 
 struct Visitor;
 struct Error;
@@ -262,7 +263,7 @@ struct Object
     /*< private >*/
     ObjectClass *class;
     QTAILQ_HEAD(, ObjectProperty) properties;
-    uint32_t ref;
+    Atomic ref;
     Object *parent;
 };
 
qom/object.c +5 -6
index e3e9242..34ec2a1 100644
@@ -383,7 +383,7 @@ void object_finalize(void *data)
     object_deinit(obj, ti);
     object_property_del_all(obj);
 
-    g_assert(obj->ref == 0);
+    g_assert(atomic_read(&obj->ref) == 0);
 }
 
 Object *object_new_with_type(Type type)
@@ -410,7 +410,7 @@ Object *object_new(const char *typename)
 void object_delete(Object *obj)
 {
     object_unparent(obj);
-    g_assert(obj->ref == 1);
+    g_assert(atomic_read(&obj->ref) == 1);
     object_unref(obj);
     g_free(obj);
 }
@@ -600,16 +600,15 @@ GSList *object_class_get_list(const char *implements_type,
 
 void object_ref(Object *obj)
 {
-    obj->ref++;
+    atomic_inc(&obj->ref);
 }
 
 void object_unref(Object *obj)
 {
-    g_assert(obj->ref > 0);
-    obj->ref--;
+    g_assert(atomic_read(&obj->ref) > 0);
 
     /* parent always holds a reference to its children */
-    if (obj->ref == 0) {
+    if (atomic_return_and_sub(1, &obj->ref) == 1) {
         object_finalize(obj);
     }
 }
Signature · 1 line
1.7.4.4
#16 lore raw

PATCH v3 3/11 hotplug: introduce qdev_unplug_complete() to remove device from views

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

When device unplug has been ack by guest, we first remove it from memory to prevent incoming access from dispatcher. Then we isolate it from device composition tree

---
 hw/qdev.c |   26 ++++++++++++++++++++++++++
 hw/qdev.h |    3 ++-
 2 files changed, 28 insertions(+), 1 deletions(-)
hw/qdev.c +26 -0
index b5a52ac..73df046 100644
@@ -104,6 +104,14 @@ void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
     bus_add_child(bus, dev);
 }
 
+static void qdev_unset_parent(DeviceState *dev)
+{
+    BusState *b = dev->parent_bus;
+
+    object_unparent(OBJECT(dev));
+    bus_remove_child(b, dev);
+}
+
 /* Create a new device.  This only initializes the device state structure
    and allows properties to be set.  qdev_init should be called to
    initialize the actual device emulation.  */
@@ -193,6 +201,24 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+static int qdev_unmap(DeviceState *dev)
+{
+    DeviceClass *dc =  DEVICE_GET_CLASS(dev);
+    if (dc->unmap) {
+        dc->unmap(dev);
+    }
+    return 0;
+}
+
+void qdev_unplug_complete(DeviceState *dev, Error **errp)
+{
+    /* isolate from mem view */
+    qdev_unmap(dev);
+    /* isolate from device tree */
+    qdev_unset_parent(dev);
+    object_unref(OBJECT(dev));
+}
+
 void qdev_unplug(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
hw/qdev.h +2 -1
index d699194..aeae29e 100644
@@ -47,7 +47,7 @@ typedef struct DeviceClass {
 
     /* callbacks */
     void (*reset)(DeviceState *dev);
-
+    void (*unmap)(DeviceState *dev);
     /* device state */
     const VMStateDescription *vmsd;
 
@@ -161,6 +161,7 @@ void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
 void qdev_unplug(DeviceState *dev, Error **errp);
+void qdev_unplug_complete(DeviceState *dev, Error **errp);
 void qdev_free(DeviceState *dev);
 int qdev_simple_unplug_cb(DeviceState *dev);
 void qdev_machine_creation_done(void);
Signature · 1 line
1.7.4.4
#17 lore raw

PATCH v3 4/11 pci: remove pci device from mem view when unplug

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

---
 hw/acpi_piix4.c |    2 +-
 hw/pci.c        |   13 ++++++++++++-
 hw/pci.h        |    1 +
 3 files changed, 14 insertions(+), 2 deletions(-)
hw/acpi_piix4.c +1 -1
index c56220b..a78b0e3 100644
@@ -305,7 +305,7 @@ static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots)
             if (pc->no_hotplug) {
                 slot_free = false;
             } else {
-                qdev_free(qdev);
+                qdev_unplug_complete(qdev, NULL);
             }
         }
     }
hw/pci.c +12 -1
index 4d95984..3e2a081 100644
@@ -850,7 +850,6 @@ static int pci_unregister_device(DeviceState *dev)
     PCIDevice *pci_dev = PCI_DEVICE(dev);
     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
 
-    pci_unregister_io_regions(pci_dev);
     pci_del_option_rom(pci_dev);
 
     if (pc->exit) {
@@ -861,6 +860,17 @@ static int pci_unregister_device(DeviceState *dev)
     return 0;
 }
 
+static void pci_unmap_device(DeviceState *dev)
+{
+    PCIDevice *pci_dev = PCI_DEVICE(dev);
+    PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
+
+    pci_unregister_io_regions(pci_dev);
+    if (pc->unmap) {
+        pc->unmap(pci_dev);
+    }
+}
+
 void pci_register_bar(PCIDevice *pci_dev, int region_num,
                       uint8_t type, MemoryRegion *memory)
 {
@@ -2064,6 +2074,7 @@ static void pci_device_class_init(ObjectClass *klass, void *data)
     DeviceClass *k = DEVICE_CLASS(klass);
     k->init = pci_qdev_init;
     k->unplug = pci_unplug_device;
+    k->unmap = pci_unmap_device;
     k->exit = pci_unregister_device;
     k->bus_type = TYPE_PCI_BUS;
     k->props = pci_props;
hw/pci.h +1 -0
index 4b6ab3d..09bbe2b 100644
@@ -154,6 +154,7 @@ typedef struct PCIDeviceClass {
     DeviceClass parent_class;
 
     int (*init)(PCIDevice *dev);
+    void (*unmap)(PCIDevice *dev);
     PCIUnregisterFunc *exit;
     PCIConfigReadFunc *config_read;
     PCIConfigWriteFunc *config_write;
Signature · 1 line
1.7.4.4
#18 lore raw

PATCH v3 5/11 memory: introduce ref, unref interface for MemoryRegionOps

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

This pair of interface help to decide when dispatching, whether we can pin mr without big lock or not.

---
 memory.h |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
memory.h +3 -0
index bd1bbae..9039411 100644
@@ -25,6 +25,7 @@
 #include "iorange.h"
 #include "ioport.h"
 #include "int128.h"
+#include "qemu/object.h"
 
 typedef struct MemoryRegionOps MemoryRegionOps;
 typedef struct MemoryRegion MemoryRegion;
@@ -66,6 +67,8 @@ struct MemoryRegionOps {
                   target_phys_addr_t addr,
                   uint64_t data,
                   unsigned size);
+    int (*ref)(MemoryRegion *mr);
+    void (*unref)(MemoryRegion *mr);
 
     enum device_endian endianness;
     /* Guest-visible constraints: */
Signature · 1 line
1.7.4.4
#19 lore raw

Re: [Qemu-devel] [PATCH V3 05/11] memory: introduce ref, unref interface for MemoryRegionOps

memory.h line 28

Quoted 12 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

This pair of interface help to decide when dispatching, whether we can pin mr without big lock or not.

memory.h +1 -0
@@ -25,6 +25,7 @@
 #include "iorange.h"
 #include "ioport.h"
 #include "int128.h"
+#include "qemu/object.h"

Not needed.

memory.h struct MemoryRegionOps { · line 72

Quoted 10 lines
memory.h +2 -0
 typedef struct MemoryRegionOps MemoryRegionOps;
 typedef struct MemoryRegion MemoryRegion;
@@ -66,6 +67,8 @@ struct MemoryRegionOps {
                   target_phys_addr_t addr,
                   uint64_t data,
                   unsigned size);
+    int (*ref)(MemoryRegion *mr);
+    void (*unref)(MemoryRegion *mr);
 

Please add documentation, esp. that the operations are optional.

Signature · 1 line
error compiling committee.c: too many arguments to function
#20 lore raw

PATCH v3 6/11 memory: make mmio dispatch able to be out of biglock

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Without biglock, we try to protect the mr by increase refcnt. If we can inc refcnt, go backward and resort to biglock.

Another point is memory radix-tree can be flushed by another thread, so we should get the copy of terminal mr to survive from such issue.

---
 exec.c |   95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 94 insertions(+), 1 deletions(-)
exec.c +94 -1
index 5834766..93205b1 100644
@@ -3163,8 +3163,11 @@ static void io_mem_init(void)
                           "watch", UINT64_MAX);
 }
 
+static QemuMutex mem_map_lock;
+
 static void core_begin(MemoryListener *listener)
 {
+    qemu_mutex_lock(&mem_map_lock);
     destroy_all_mappings();
     phys_sections_clear();
     phys_map.ptr = PHYS_MAP_NODE_NIL;
@@ -3184,17 +3187,32 @@ static void core_commit(MemoryListener *listener)
     for(env = first_cpu; env != NULL; env = env->next_cpu) {
         tlb_flush(env, 1);
     }
+    qemu_mutex_unlock(&mem_map_lock);
 }
 
 static void core_region_add(MemoryListener *listener,
                             MemoryRegionSection *section)
 {
+    MemoryRegion *mr = section->mr;
+
+    if (mr->ops != NULL) {
+        if (mr->ops->ref != NULL) {
+            mr->ops->ref(mr);
+        }
+    }
     cpu_register_physical_memory_log(section, section->readonly);
 }
 
 static void core_region_del(MemoryListener *listener,
                             MemoryRegionSection *section)
 {
+    MemoryRegion *mr = section->mr;
+
+    if (mr->ops != NULL) {
+        if (mr->ops->unref != NULL) {
+            mr->ops->unref(mr);
+        }
+    }
 }
 
 static void core_region_nop(MemoryListener *listener,
@@ -3348,6 +3366,8 @@ static void memory_map_init(void)
     memory_region_init(system_io, "io", 65536);
     set_system_io_map(system_io);
 
+    qemu_mutex_init(&mem_map_lock);
+
     memory_listener_register(&core_memory_listener, system_memory);
     memory_listener_register(&io_memory_listener, system_io);
 }
@@ -3406,6 +3426,52 @@ int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
 }
 
 #else
+
+static MemoryRegionSection *subpage_get_terminal(subpage_t *mmio,
+    target_phys_addr_t addr)
+{
+    MemoryRegionSection *section;
+    unsigned int idx = SUBPAGE_IDX(addr);
+
+    section = &phys_sections[mmio->sub_section[idx]];
+    return section;
+}
+
+static MemoryRegionSection mrs_get_terminal(MemoryRegionSection *mrs,
+    target_phys_addr_t addr)
+{
+    if (mrs->mr->subpage) {
+        mrs = subpage_get_terminal(mrs->mr->opaque, addr);
+    }
+    return *mrs;
+}
+
+static int mrs_ref(MemoryRegionSection *mrs)
+{
+    MemoryRegion *mr;
+    int ret;
+
+    mr = mrs->mr;
+    if (mr->ops != NULL) {
+        if (mr->ops->ref != NULL) {
+            ret = mr->ops->ref(mr);
+        }
+    }
+    return ret;
+}
+
+static void mrs_unref(MemoryRegionSection *mrs)
+{
+    MemoryRegion *mr;
+
+    mr = mrs->mr;
+    if (mr->ops != NULL) {
+        if (mr->ops->unref != NULL) {
+            mr->ops->unref(mr);
+        }
+    }
+}
+
 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
                             int len, int is_write)
 {
@@ -3413,14 +3479,35 @@ void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
     uint8_t *ptr;
     uint32_t val;
     target_phys_addr_t page;
-    MemoryRegionSection *section;
+    MemoryRegionSection *section, obj_mrs;
+    int ret;
+    int need_biglock = 0;
 
+    /* Will finally removed after all mr->ops implement ref/unref() */
+try_big_lock:
+    if (need_biglock == 1) {
+        qemu_mutex_lock_iothread();
+    }
     while (len > 0) {
         page = addr & TARGET_PAGE_MASK;
         l = (page + TARGET_PAGE_SIZE) - addr;
         if (l > len)
             l = len;
+
+        qemu_mutex_lock(&mem_map_lock);
         section = phys_page_find(page >> TARGET_PAGE_BITS);
+        if (need_biglock == 0) {
+            obj_mrs = mrs_get_terminal(section, addr);
+            ret = mrs_ref(&obj_mrs);
+            if (ret <= 0) {
+                need_biglock = 1;
+                qemu_mutex_unlock(&mem_map_lock);
+                goto try_big_lock;
+            }
+            /* rely on local variable */
+            section = &obj_mrs;
+        }
+        qemu_mutex_unlock(&mem_map_lock);
 
         if (is_write) {
             if (!memory_region_is_ram(section->mr)) {
@@ -3491,10 +3578,16 @@ void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
                 qemu_put_ram_ptr(ptr);
             }
         }
+
+        mrs_unref(&obj_mrs);
         len -= l;
         buf += l;
         addr += l;
     }
+
+    if (need_biglock == 1) {
+        qemu_mutex_unlock_iothread();
+    }
 }
 
 /* used for ROM loading : can write in RAM and ROM */
Signature · 1 line
1.7.4.4
#21 lore raw

Re: [Qemu-devel] [PATCH V3 06/11] memory: make mmio dispatch able to be out of biglock

Quoted 10 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Without biglock, we try to protect the mr by increase refcnt. If we can inc refcnt, go backward and resort to biglock.

Another point is memory radix-tree can be flushed by another thread, so we should get the copy of terminal mr to survive from such issue.

exec.c static void io_mem_init(void) · line 3170

Quoted 5 lines
exec.c +3 -0
+static QemuMutex mem_map_lock;
+
 static void core_begin(MemoryListener *listener)
 {
+    qemu_mutex_lock(&mem_map_lock);

Since we have an unbalanced lock here, please add a comment that explains the lock holding region.

Quoted 10 lines
     destroy_all_mappings();
     phys_sections_clear();
     phys_map.ptr = PHYS_MAP_NODE_NIL;
exec.c +1 -0
@@ -3184,17 +3187,32 @@ static void core_commit(MemoryListener *listener)
     for(env = first_cpu; env != NULL; env = env->next_cpu) {
         tlb_flush(env, 1);
     }
+    qemu_mutex_unlock(&mem_map_lock);
 }
 

exec.c static void core_commit(MemoryListener *listener) · line 3202

Quoted 10 lines
exec.c +7 -0
 static void core_region_add(MemoryListener *listener,
                             MemoryRegionSection *section)
 {
+    MemoryRegion *mr = section->mr;
+
+    if (mr->ops != NULL) {
+        if (mr->ops->ref != NULL) {
+            mr->ops->ref(mr);
+        }
+    }

Can drop '!= NULL'.

Quoted 4 lines
exec.c +0 -0
     cpu_register_physical_memory_log(section, section->readonly);
 }
 
 static void core_region_del(MemoryListener *listener,

exec.c static void core_commit(MemoryListener *listener) · line 3216

Quoted 10 lines
exec.c +7 -0
                             MemoryRegionSection *section)
 {
+    MemoryRegion *mr = section->mr;
+
+    if (mr->ops != NULL) {
+        if (mr->ops->unref != NULL) {
+            mr->ops->unref(mr);
+        }
+    }
 }

Here too.

Quoted 14 lines
exec.c +10 -0
@@ -3406,6 +3426,52 @@ int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
 }
 
 #else
+
+static MemoryRegionSection *subpage_get_terminal(subpage_t *mmio,
+    target_phys_addr_t addr)
+{
+    MemoryRegionSection *section;
+    unsigned int idx = SUBPAGE_IDX(addr);
+
+    section = &phys_sections[mmio->sub_section[idx]];
+    return section;
+}

exec.c int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr, · line 3448

Quoted 10 lines
exec.c +10 -0
+
+static MemoryRegionSection mrs_get_terminal(MemoryRegionSection *mrs,
+    target_phys_addr_t addr)
+{
+    if (mrs->mr->subpage) {
+        mrs = subpage_get_terminal(mrs->mr->opaque, addr);
+    }
+    return *mrs;
+}
+

Please spell out memory_region_section_ in function names, like elsewhere in the file.

exec.c int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr, · line 3461

Quoted 13 lines
exec.c +13 -0
+static int mrs_ref(MemoryRegionSection *mrs)
+{
+    MemoryRegion *mr;
+    int ret;
+
+    mr = mrs->mr;
+    if (mr->ops != NULL) {
+        if (mr->ops->ref != NULL) {
+            ret = mr->ops->ref(mr);
+        }
+    }
+    return ret;
+}

What could 'ret' possibly be?

Quoted 40 lines
exec.c +25 -1
+
+static void mrs_unref(MemoryRegionSection *mrs)
+{
+    MemoryRegion *mr;
+
+    mr = mrs->mr;
+    if (mr->ops != NULL) {
+        if (mr->ops->unref != NULL) {
+            mr->ops->unref(mr);
+        }
+    }
+}
+
 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
                             int len, int is_write)
 {
@@ -3413,14 +3479,35 @@ void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
     uint8_t *ptr;
     uint32_t val;
     target_phys_addr_t page;
-    MemoryRegionSection *section;
+    MemoryRegionSection *section, obj_mrs;
+    int ret;
+    int need_biglock = 0;
 
+    /* Will finally removed after all mr->ops implement ref/unref() */
+try_big_lock:
+    if (need_biglock == 1) {
+        qemu_mutex_lock_iothread();
+    }
     while (len > 0) {
         page = addr & TARGET_PAGE_MASK;
         l = (page + TARGET_PAGE_SIZE) - addr;
         if (l > len)
             l = len;
+
+        qemu_mutex_lock(&mem_map_lock);
         section = phys_page_find(page >> TARGET_PAGE_BITS);
+        if (need_biglock == 0) {
+            obj_mrs = mrs_get_terminal(section, addr);

exec.c void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, · line 3510

Quoted 10 lines
exec.c +10 -0
+            ret = mrs_ref(&obj_mrs);
+            if (ret <= 0) {
+                need_biglock = 1;
+                qemu_mutex_unlock(&mem_map_lock);
+                goto try_big_lock;
+            }
+            /* rely on local variable */
+            section = &obj_mrs;
+        }
+        qemu_mutex_unlock(&mem_map_lock);

Please split out this code to a separate function (can return MemoryRegionSection by value, with either the big lock held or the object referenced).

Quoted 7 lines
exec.c +0 -0
         if (is_write) {
             if (!memory_region_is_ram(section->mr)) {
@@ -3491,10 +3578,16 @@ void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
                 qemu_put_ram_ptr(ptr);
             }
         }

exec.c void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, · line 3590

Quoted 10 lines
exec.c +6 -0
+
+        mrs_unref(&obj_mrs);
         len -= l;
         buf += l;
         addr += l;
     }`
+
+    if (need_biglock == 1) {
+        qemu_mutex_unlock_iothread();
+    }

Similarly this should be in a separate function, either unrefing or dropping the big lock as appropriate.

exec.c void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, · line 3593

Quoted 4 lines
exec.c +0 -0
 }
 
 /* used for ROM loading : can write in RAM and ROM */

I see other calls to phys_page_find(), they all need to be protected.

Signature · 1 line
error compiling committee.c: too many arguments to function
#22 lore raw

Re: [Qemu-devel] [PATCH V3 06/11] memory: make mmio dispatch able to be out of biglock

In reply to Liu Ping Fan
Quoted 37 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Without biglock, we try to protect the mr by increase refcnt. If we can inc refcnt, go backward and resort to biglock.

Another point is memory radix-tree can be flushed by another thread, so we should get the copy of terminal mr to survive from such issue.

void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
int len, int is_write)
{

exec.c +13 -1
@@ -3413,14 +3479,35 @@ void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
     uint8_t *ptr;
     uint32_t val;
     target_phys_addr_t page;
-    MemoryRegionSection *section;
+    MemoryRegionSection *section, obj_mrs;
+    int ret;
+    int need_biglock = 0;
 
+    /* Will finally removed after all mr->ops implement ref/unref() */
+try_big_lock:
+    if (need_biglock == 1) {
+        qemu_mutex_lock_iothread();
+    }
     while (len > 0) {
         page = addr & TARGET_PAGE_MASK;
         l = (page + TARGET_PAGE_SIZE) - addr;
         if (l > len)
             l = len;
+
+        qemu_mutex_lock(&mem_map_lock);
         section = phys_page_find(page >> TARGET_PAGE_BITS);
+        if (need_biglock == 0) {
+            obj_mrs = mrs_get_terminal(section, addr);
+            ret = mrs_ref(&obj_mrs);

exec.c void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, · line 3511

Quoted 10 lines
exec.c +9 -0
+            if (ret <= 0) {
+                need_biglock = 1;
+                qemu_mutex_unlock(&mem_map_lock);
+                goto try_big_lock;
+            }
+            /* rely on local variable */
+            section = &obj_mrs;
+        }
+        qemu_mutex_unlock(&mem_map_lock);
 

If on the first pass we find that we need the big lock, we may find on the second pass that we don't need it since the memory map has changed.
So on the second pass we might actually need to drop the big lock.

Could code it like

section, need_lock = lookup(...)
if need_lock:
lock(big_lock)
section, need_lock = lookup(...)
if not need_lock:
unlock(big_lock)
dispatch(section)
if need_lock:
unlock(big_lock)

It's ugly as hell, so we'll to apologize to readers in a big comment explaining what's going on.

Signature · 1 line
error compiling committee.c: too many arguments to function
#23 lore raw

PATCH v3 7/11 memory: implement e1000's MemoryRegionOps ref/unref

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

With this, e1000 show to memory core that it can be protected by refcnt.

---
 hw/e1000.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)
hw/e1000.c +17 -0
index ae8a6c5..df3a349 100644
@@ -1037,9 +1037,26 @@ e1000_mmio_read(void *opaque, target_phys_addr_t addr, unsigned size)
     return 0;
 }
 
+static int e1000_mmio_ref(MemoryRegion *mr)
+{
+    E1000State *e1000 = container_of(mr, E1000State, mmio);
+
+    object_ref(OBJECT(e1000));
+    return 1;
+}
+
+static void e1000_mmio_unref(MemoryRegion *mr)
+{
+    E1000State *e1000 = container_of(mr, E1000State, mmio);
+
+    object_unref(OBJECT(e1000));
+}
+
 static const MemoryRegionOps e1000_mmio_ops = {
     .read = e1000_mmio_read,
     .write = e1000_mmio_write,
+    .ref = e1000_mmio_ref,
+    .unref = e1000_mmio_unref,
     .endianness = DEVICE_LITTLE_ENDIAN,
     .impl = {
         .min_access_size = 4,
Signature · 1 line
1.7.4.4
#24 lore raw

Re: [Qemu-devel] [PATCH V3 07/11] memory: implement e1000's MemoryRegionOps ref/unref

Quoted 27 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

With this, e1000 show to memory core that it can be protected by refcnt.

hw/e1000.c +13 -0
index ae8a6c5..df3a349 100644
@@ -1037,9 +1037,26 @@ e1000_mmio_read(void *opaque, target_phys_addr_t addr, unsigned size)
     return 0;
 }
 
+static int e1000_mmio_ref(MemoryRegion *mr)
+{
+    E1000State *e1000 = container_of(mr, E1000State, mmio);
+
+    object_ref(OBJECT(e1000));
+    return 1;
+}
+
+static void e1000_mmio_unref(MemoryRegion *mr)
+{
+    E1000State *e1000 = container_of(mr, E1000State, mmio);
+
+    object_unref(OBJECT(e1000));

hw/e1000.c e1000_mmio_read(void *opaque, target_phys_addr_t addr, unsigned size) · line 1062

Quoted 10 lines
hw/e1000.c +4 -0
+}
+
 static const MemoryRegionOps e1000_mmio_ops = {
     .read = e1000_mmio_read,
     .write = e1000_mmio_write,
+    .ref = e1000_mmio_ref,
+    .unref = e1000_mmio_unref,
     .endianness = DEVICE_LITTLE_ENDIAN,
     .impl = {
         .min_access_size = 4,

Does e1000_mmio_write() never call functions that assume bql protection?
The network layer, timers, and qemu_irq all need protection.

Signature · 1 line
error compiling committee.c: too many arguments to function
#25 lore raw

PATCH v3 8/11 qom: introduce reclaimer to release obj in async

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

DeviceState will be protected by refcnt from disappearing during dispatching. But when refcnt comes down to zero, DeviceState may be still in use by iohandler, timer etc in main loop, we just delay its free untill no reader.

This patch aim to build this delay reclaimer.

---
 include/qemu/reclaimer.h |   30 +++++++++++++++++++++++++
 main-loop.c              |    5 ++++
 qemu-tool.c              |    5 ++++
 qom/Makefile.objs        |    2 +-
 qom/reclaimer.c          |   54 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 95 insertions(+), 1 deletions(-)
 create mode 100644 include/qemu/reclaimer.h
 create mode 100644 qom/reclaimer.c
include/qemu/reclaimer.h new file +30 -0
new file mode 100644
index 0000000..5143c4f
@@ -0,0 +1,30 @@
+/*
+ * QEMU reclaimer
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef QEMU_RECLAIMER
+#define QEMU_RECLAIMER
+
+#include "qemu-thread.h"
+
+typedef void ReleaseHandler(void *opaque);
+typedef struct Chunk {
+    QLIST_ENTRY(Chunk) list;
+    void *opaque;
+    ReleaseHandler *release;
+} Chunk;
+
+typedef struct ChunkHead {
+    struct QemuMutex lock;
+    QLIST_HEAD(, Chunk) reclaim_list;
+} ChunkHead;
+
+extern ChunkHead qdev_reclaimer;
+void reclaimer_enqueue(ChunkHead *head, void *opaque, ReleaseHandler *release);
+void reclaimer_worker(ChunkHead *head);
+void qemu_reclaimer(void);
+#endif
main-loop.c +5 -0
index eb3b6e6..be9d095 100644
@@ -26,6 +26,7 @@
 #include "qemu-timer.h"
 #include "slirp/slirp.h"
 #include "main-loop.h"
+#include "qemu/reclaimer.h"
 
 #ifndef _WIN32
 
@@ -505,5 +506,9 @@ int main_loop_wait(int nonblocking)
        them.  */
     qemu_bh_poll();
 
+    /* ref to device from iohandler/bh/timer do not obey the rules, so delay
+     * reclaiming until now.
+     */
+    qemu_reclaimer();
     return ret;
 }
qemu-tool.c +5 -0
index 18205ba..f250c87 100644
@@ -21,6 +21,7 @@
 #include "main-loop.h"
 #include "qemu_socket.h"
 #include "slirp/libslirp.h"
+#include "qemu/reclaimer.h"
 
 #include <sys/time.h>
 
@@ -100,6 +101,10 @@ void qemu_mutex_unlock_iothread(void)
 {
 }
 
+void qemu_reclaimer(void)
+{
+}
+
 int use_icount;
 
 void qemu_clock_warp(QEMUClock *clock)
qom/Makefile.objs +1 -1
index 5ef060a..a579261 100644
@@ -1,4 +1,4 @@
-qom-obj-y = object.o container.o qom-qobject.o
+qom-obj-y = object.o container.o qom-qobject.o reclaimer.o
 qom-obj-twice-y = cpu.o
 common-obj-y = $(qom-obj-twice-y)
 user-obj-y = $(qom-obj-twice-y)
qom/reclaimer.c new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
1.7.4.4
#26 lore raw

Re: [Qemu-devel] [PATCH V3 08/11] qom: introduce reclaimer to release obj in async

Quoted 7 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

DeviceState will be protected by refcnt from disappearing during dispatching. But when refcnt comes down to zero, DeviceState may be still in use by iohandler, timer etc in main loop, we just delay its free untill no reader.

How can this be? We elevate the refcount while dispatching I/O. If we have similar problems with the timer, we need to employ a similar solution.

Signature · 1 line
error compiling committee.c: too many arguments to function
#27 lore raw

Re: [Qemu-devel] [PATCH V3 08/11] qom: introduce reclaimer to release obj in async

Quoted 12 lines · Avi Kivity
Quoted 7 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

DeviceState will be protected by refcnt from disappearing during dispatching. But when refcnt comes down to zero, DeviceState may be still in use by iohandler, timer etc in main loop, we just delay its free untill no reader.

How can this be? We elevate the refcount while dispatching I/O. If we have similar problems with the timer, we need to employ a similar solution.

Yes, at the next step, plan to covert iohandler, timer etc to use refcount as memory. Here just a temp solution.

Regards,
pingfan

Quoted 3 lines

--
error compiling committee.c: too many arguments to function

#28 lore raw

Re: [Qemu-devel] [PATCH V3 08/11] qom: introduce reclaimer to release obj in async

Quoted 10 lines · liu ping fan
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

DeviceState will be protected by refcnt from disappearing during dispatching. But when refcnt comes down to zero, DeviceState may be still in use by iohandler, timer etc in main loop, we just delay its free untill no reader.

Quoted 5 lines
Quoted 3 lines

How can this be? We elevate the refcount while dispatching I/O. If we have similar problems with the timer, we need to employ a similar solution.

Yes, at the next step, plan to covert iohandler, timer etc to use refcount as memory. Here just a temp solution.

I prefer not to ever introduce it.

What we can do is introduce a sub-region for e1000's mmio that will take only the device lock, and let original region use the old dispatch path (and also take the device lock). As we thread the various subsystems e1000 uses, we can expand the sub-region until it covers all of e1000's functions, then fold it back into the main region.

To start with the sub-region can only include registers that call no qemu infrastructure code: simple read/writes.

Signature · 1 line
error compiling committee.c: too many arguments to function
#29 lore raw

Re: [Qemu-devel] [PATCH V3 08/11] qom: introduce reclaimer to release obj in async

Quoted 17 lines · Avi Kivity
Quoted 10 lines · liu ping fan
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

DeviceState will be protected by refcnt from disappearing during dispatching. But when refcnt comes down to zero, DeviceState may be still in use by iohandler, timer etc in main loop, we just delay its free untill no reader.

Quoted 5 lines
Quoted 3 lines

How can this be? We elevate the refcount while dispatching I/O. If we have similar problems with the timer, we need to employ a similar solution.

Yes, at the next step, plan to covert iohandler, timer etc to use refcount as memory. Here just a temp solution.

Quoted 8 lines

I prefer not to ever introduce it.

What we can do is introduce a sub-region for e1000's mmio that will take only the device lock, and let original region use the old dispatch path (and also take the device lock). As we thread the various subsystems e1000 uses, we can expand the sub-region until it covers all of e1000's functions, then fold it back into the main region.

Introducing new sub-region for e1000 seems no help to resolve this issue. It can not tell whether main-loop still use it or not. I think the key point is that original code SYNC eliminate all the readers of DeviceState at acpi_piix_eject_slot() by dev->unit()/exit(), so each subsystem will no access it in future. But now, we can delete the DeviceState async.
Currently, we can just use e1000->unmap() to detach itself from each subsystem(Not implemented in this series patches for timer,...) to achieve the goal, because their readers are still under the protection of big lock, but when they are out of big lock, we need extra effort like memory system.

Regards,
pingfan

Quoted 6 lines

To start with the sub-region can only include registers that call no qemu infrastructure code: simple read/writes.

--
error compiling committee.c: too many arguments to function

#30 lore raw

Re: [Qemu-devel] [PATCH V3 08/11] qom: introduce reclaimer to release obj in async

Quoted 20 lines · liu ping fan
Quoted 19 lines · Avi Kivity
Quoted 10 lines · liu ping fan
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

DeviceState will be protected by refcnt from disappearing during dispatching. But when refcnt comes down to zero, DeviceState may be still in use by iohandler, timer etc in main loop, we just delay its free untill no reader.

Quoted 5 lines
Quoted 3 lines

How can this be? We elevate the refcount while dispatching I/O. If we have similar problems with the timer, we need to employ a similar solution.

Yes, at the next step, plan to covert iohandler, timer etc to use refcount as memory. Here just a temp solution.

I prefer not to ever introduce it.

Quoted 8 lines
Quoted 6 lines

What we can do is introduce a sub-region for e1000's mmio that will take only the device lock, and let original region use the old dispatch path (and also take the device lock). As we thread the various subsystems e1000 uses, we can expand the sub-region until it covers all of e1000's functions, then fold it back into the main region.

Introducing new sub-region for e1000 seems no help to resolve this issue. It can not tell whether main-loop still use it or not.

What is "it" here? (actually two of them).

Quoted 4 lines

I think the key point is that original code SYNC eliminate all the readers of DeviceState at acpi_piix_eject_slot() by dev->unit()/exit(), so each subsystem will no access it in future. But now, we can delete the DeviceState async.

But deleting happens when we are guaranteed to have no I/O dispatch.

Quoted 5 lines

Currently, we can just use e1000->unmap() to detach itself from each subsystem(Not implemented in this series patches for timer,...) to achieve the goal, because their readers are still under the protection of big lock, but when they are out of big lock, we need extra effort like memory system.

I see what you mean. So you defer the deletion to a context where the big lock is held.

But this solves nothing. The device model accesses the network stack and timer subsystem without the big lock held. So you either need to thread those two subsystems, or take the big lock in the I/O handlers. If you do that, you can also take the big lock in the destructor. If we make the big lock a recursive lock, then the destructor can be invoked in any context.

To summarize, I propose:
- drop the reclaimer
- make the bql recursive
- take the bql in the e1000 destructor
- take the bql in the e1000 I/O handlers when it accesses the timer or network subsystems
(rest for a bit)
- thread the timer subsystem
- drop bql from around timer accesses
- thread the network subsystem
- drop bql from e1000 I/O handlers and destructor

does this work?

Signature · 1 line
error compiling committee.c: too many arguments to function
#31 lore raw

Re: [Qemu-devel] [PATCH V3 08/11] qom: introduce reclaimer to release obj in async

Quoted 27 lines · Avi Kivity
Quoted 26 lines · liu ping fan
Quoted 25 lines · Avi Kivity
Quoted 10 lines · liu ping fan
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

DeviceState will be protected by refcnt from disappearing during dispatching. But when refcnt comes down to zero, DeviceState may be still in use by iohandler, timer etc in main loop, we just delay its free untill no reader.

Quoted 5 lines
Quoted 3 lines

How can this be? We elevate the refcount while dispatching I/O. If we have similar problems with the timer, we need to employ a similar solution.

Yes, at the next step, plan to covert iohandler, timer etc to use refcount as memory. Here just a temp solution.

I prefer not to ever introduce it.

What we can do is introduce a sub-region for e1000's mmio that will take only the device lock, and let original region use the old dispatch path (and also take the device lock). As we thread the various subsystems e1000 uses, we can expand the sub-region until it covers all of e1000's functions, then fold it back into the main region.

Quoted 5 lines
Quoted 2 lines

Introducing new sub-region for e1000 seems no help to resolve this issue. It can not tell whether main-loop still use it or not.

What is "it" here? (actually two of them).

Should expressed as "The sub-region's dispatcher can not tell whether main-loop still use e1000 or not"

Quoted 13 lines
Quoted 4 lines

I think the key point is that original code SYNC eliminate all the readers of DeviceState at acpi_piix_eject_slot() by dev->unit()/exit(), so each subsystem will no access it in future. But now, we can delete the DeviceState async.

But deleting happens when we are guaranteed to have no I/O dispatch.

Quoted 5 lines

Currently, we can just use e1000->unmap() to detach itself from each subsystem(Not implemented in this series patches for timer,...) to achieve the goal, because their readers are still under the protection of big lock, but when they are out of big lock, we need extra effort like memory system.

Quoted 6 lines

I see what you mean. So you defer the deletion to a context where the big lock is held.

But this solves nothing. The device model accesses the network stack and timer subsystem without the big lock held. So you either need to thread those two subsystems, or take the big lock in the I/O handlers.

Yes, at present, I tend to use big lock to protect around the call to subsystem in the e1000's I/O handlers. And verify the current changes, then thread other subsystems as the next step.

Quoted 1 line

If you do that, you can also take the big lock in the destructor. If we

We do not call qemu_del_timer() etc at the destructor, instead, we will call it in qdev_unplug_complete() -->e1000::unmap(). And e1000::unmap() is the only function definitely called under bql. When coming to destructor, the DeviceState has been completely isolated from all of the subsystem. So no need to require big lock in destructor.

Quoted 5 lines

make the big lock a recursive lock, then the destructor can be invoked in any context.

To summarize, I propose:
- drop the reclaimer

Agree

Quoted 2 lines

- make the bql recursive
- take the bql in the e1000 destructor

Change to e1000::unmap()

Quoted 2 lines

- take the bql in the e1000 I/O handlers when it accesses the timer or network subsystems

Agree

Quoted 5 lines

(rest for a bit)
- thread the timer subsystem
- drop bql from around timer accesses
- thread the network subsystem
- drop bql from e1000 I/O handlers and destructor

Agree

Quoted 1 line

Thanks and regards,
pingfan

Quoted 4 lines

does this work?

--
error compiling committee.c: too many arguments to function

#32 lore raw

Re: [Qemu-devel] [PATCH V3 08/11] qom: introduce reclaimer to release obj in async

Quoted 28 lines · liu ping fan
Quoted 27 lines · Avi Kivity
Quoted 26 lines · liu ping fan
Quoted 25 lines · Avi Kivity
Quoted 10 lines · liu ping fan
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

DeviceState will be protected by refcnt from disappearing during dispatching. But when refcnt comes down to zero, DeviceState may be still in use by iohandler, timer etc in main loop, we just delay its free untill no reader.

Quoted 5 lines
Quoted 3 lines

How can this be? We elevate the refcount while dispatching I/O. If we have similar problems with the timer, we need to employ a similar solution.

Yes, at the next step, plan to covert iohandler, timer etc to use refcount as memory. Here just a temp solution.

I prefer not to ever introduce it.

What we can do is introduce a sub-region for e1000's mmio that will take only the device lock, and let original region use the old dispatch path (and also take the device lock). As we thread the various subsystems e1000 uses, we can expand the sub-region until it covers all of e1000's functions, then fold it back into the main region.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Introducing new sub-region for e1000 seems no help to resolve this issue. It can not tell whether main-loop still use it or not.

What is "it" here? (actually two of them).

Should expressed as "The sub-region's dispatcher can not tell whether main-loop still use e1000 or not"

The sub-region will not use any unthreaded subsystems, so it need not care about the main loop.

At first, it would only access registers in device state.

But if we go with the plan below, we can drop it.

Quoted 9 lines
Quoted 2 lines

If you do that, you can also take the big lock in the destructor. If we

We do not call qemu_del_timer() etc at the destructor, instead, we will call it in qdev_unplug_complete() -->e1000::unmap(). And e1000::unmap() is the only function definitely called under bql. When coming to destructor, the DeviceState has been completely isolated from all of the subsystem. So no need to require big lock in destructor.

But between unmap() and the destructor, accesses can still occur (an access that was started before unmap() was called, but was delayed and is dispatched after it completes). These accesses will find the timer deleted, and so must be prepared to check if the timer is there or not.

So we have a choice, either move timer destruction to the destructor, or add checks in the dispatch code.

Signature · 1 line
error compiling committee.c: too many arguments to function
#33 lore raw

PATCH v3 9/11 vcpu: make QemuThread as tls to store thread-self info

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

We store the thread self info in QemuThread.

---
 cpus.c              |    1 +
 qemu-thread-posix.c |    7 +++++++
 qemu-thread-posix.h |    2 ++
 qemu-thread.h       |    1 +
 vl.c                |    4 ++++
 5 files changed, 15 insertions(+), 0 deletions(-)
cpus.c +1 -0
index e476a3c..4cd7f85 100644
@@ -735,6 +735,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)
     CPUState *cpu = ENV_GET_CPU(env);
     int r;
 
+    pthread_setspecific(qemu_thread_key, cpu->thread);
     qemu_mutex_lock(&qemu_global_mutex);
     qemu_thread_get_self(cpu->thread);
     env->thread_id = qemu_get_thread_id();
qemu-thread-posix.c +7 -0
index 8fbabda..f448fcb 100644
@@ -19,6 +19,8 @@
 #include <string.h>
 #include "qemu-thread.h"
 
+pthread_key_t qemu_thread_key;
+
 static void error_exit(int err, const char *msg)
 {
     fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
@@ -151,6 +153,11 @@ void qemu_thread_get_self(QemuThread *thread)
     thread->thread = pthread_self();
 }
 
+void qemu_thread_key_create(void)
+{
+    pthread_key_create(&qemu_thread_key, NULL);
+}
+
 bool qemu_thread_is_self(QemuThread *thread)
 {
    return pthread_equal(pthread_self(), thread->thread);
qemu-thread-posix.h +2 -0
index ee4618e..2607b1c 100644
@@ -14,4 +14,6 @@ struct QemuThread {
     pthread_t thread;
 };
 
+extern pthread_key_t qemu_thread_key;
+
 #endif
qemu-thread.h +1 -0
index 05fdaaf..4a6427d 100644
@@ -46,4 +46,5 @@ void qemu_thread_get_self(QemuThread *thread);
 bool qemu_thread_is_self(QemuThread *thread);
 void qemu_thread_exit(void *retval);
 
+void qemu_thread_key_create(void);
 #endif
vl.c +4 -0
index 7c577fa..44e2783 100644
@@ -149,6 +149,7 @@ int main(int argc, char **argv)
 #include "qemu-options.h"
 #include "qmp-commands.h"
 #include "main-loop.h"
+#include "qemu-thread.h"
 #ifdef CONFIG_VIRTFS
 #include "fsdev/qemu-fsdev.h"
 #endif
@@ -2342,6 +2343,7 @@ int qemu_init_main_loop(void)
     return main_loop_init();
 }
 
+
 int main(int argc, char **argv, char **envp)
 {
     int i;
@@ -3483,6 +3485,8 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
+    qemu_thread_key_create();
+
     os_set_line_buffering();
 
     if (init_timer_alarm() < 0) {
Signature · 1 line
1.7.4.4
#34 lore raw

PATCH v3 10/11 vcpu: introduce lockmap

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

---
 cpus.c              |   18 ++++++++++++++++++
 qemu-thread-posix.c |   23 +++++++++++++++++++++++
 qemu-thread-posix.h |    5 +++++
 qemu-thread.h       |    3 +++
 4 files changed, 49 insertions(+), 0 deletions(-)
cpus.c +18 -0
index 4cd7f85..09f6670 100644
@@ -736,6 +736,8 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)
     int r;
 
     pthread_setspecific(qemu_thread_key, cpu->thread);
+    cpu->thread->lockmap.biglock = 0;
+    qemu_big_lockmap_inc();
     qemu_mutex_lock(&qemu_global_mutex);
     qemu_thread_get_self(cpu->thread);
     env->thread_id = qemu_get_thread_id();
@@ -905,6 +907,14 @@ int qemu_cpu_is_self(void *_env)
 
 void qemu_mutex_lock_iothread(void)
 {
+    unsigned int map;
+
+    if (!qemu_thread_is_self(&io_thread)) {
+        map = qemu_big_lockmap_inc();
+        if (map > 1) {
+            return;
+        }
+    }
     if (!tcg_enabled()) {
         qemu_mutex_lock(&qemu_global_mutex);
     } else {
@@ -920,6 +930,14 @@ void qemu_mutex_lock_iothread(void)
 
 void qemu_mutex_unlock_iothread(void)
 {
+    unsigned int map;
+
+    if (!qemu_thread_is_self(&io_thread)) {
+        map = qemu_big_lockmap_dec();
+        if (map != 0) {
+            return;
+        }
+    }
     qemu_mutex_unlock(&qemu_global_mutex);
 }
 
qemu-thread-posix.c +23 -0
index f448fcb..1e07dc2 100644
@@ -17,6 +17,7 @@
 #include <signal.h>
 #include <stdint.h>
 #include <string.h>
+#include <glib.h>
 #include "qemu-thread.h"
 
 pthread_key_t qemu_thread_key;
@@ -158,6 +159,28 @@ void qemu_thread_key_create(void)
     pthread_key_create(&qemu_thread_key, NULL);
 }
 
+int16_t qemu_big_lockmap_inc(void)
+{
+    QemuThread *t = pthread_getspecific(qemu_thread_key);
+
+    return ++t->lockmap.biglock;
+}
+
+int16_t qemu_big_lockmap_dec(void)
+{
+    QemuThread *t = pthread_getspecific(qemu_thread_key);
+    g_assert(t->lockmap.biglock > 0);
+
+    return --t->lockmap.biglock;
+}
+
+int16_t qemu_big_lockmap_get(void)
+{
+    QemuThread *t = pthread_getspecific(qemu_thread_key);
+
+    return t->lockmap.biglock;
+}
+
 bool qemu_thread_is_self(QemuThread *thread)
 {
    return pthread_equal(pthread_self(), thread->thread);
qemu-thread-posix.h +5 -0
index 2607b1c..8f9506b 100644
@@ -10,8 +10,13 @@ struct QemuCond {
     pthread_cond_t cond;
 };
 
+typedef struct Lockmap {
+    int16_t biglock;
+} Lockmap;
+
 struct QemuThread {
     pthread_t thread;
+    Lockmap lockmap;
 };
 
 extern pthread_key_t qemu_thread_key;
qemu-thread.h +3 -0
index 4a6427d..529850b 100644
@@ -47,4 +47,7 @@ bool qemu_thread_is_self(QemuThread *thread);
 void qemu_thread_exit(void *retval);
 
 void qemu_thread_key_create(void);
+int16_t qemu_big_lockmap_inc(void);
+int16_t qemu_big_lockmap_dec(void);
+int16_t qemu_big_lockmap_get(void);
 #endif
Signature · 1 line
1.7.4.4
#35 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

What is the root cause? io handlers initiating I/O?

Perhaps we can have a solution that is local to exec.c.

Signature · 1 line
error compiling committee.c: too many arguments to function
#36 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 9 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

Regards,
pingfan

Quoted 5 lines

Perhaps we can have a solution that is local to exec.c.

--
error compiling committee.c: too many arguments to function

#37 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 4 lines · liu ping fan
Quoted 3 lines · Avi Kivity
Quoted 2 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.

Then we should look for a solution that is local to exec.c (and the nested dispatch problem). I think we can identify and fix all nested dispatches (converting them to either async dma, or letting the memory core do a single dispatch without indirection through I/O handlers).

Quoted 3 lines

I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

DMA is inherently asynchronous, so we already drop the lock between initiation and completion; we need to find a way to make it easy to use the API without taking the lock while the transfer takes place.

Signature · 1 line
error compiling committee.c: too many arguments to function
#38 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 19 lines · Avi Kivity
Quoted 4 lines · liu ping fan
Quoted 3 lines · Avi Kivity
Quoted 2 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.

Then we should look for a solution that is local to exec.c (and the nested dispatch problem). I think we can identify and fix all nested dispatches (converting them to either async dma, or letting the memory core do a single dispatch without indirection through I/O handlers).

Quoted 7 lines
Quoted 3 lines

I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

DMA is inherently asynchronous, so we already drop the lock between initiation and completion; we need to find a way to make it easy to use the API without taking the lock while the transfer takes place.

We will have to review/rework device models that want to use the new locking scheme in a way that they can drop their own lock while issuing DMA. But that is surely non-trivial.

The other option is to keep DMA requests issued by devices synchronous but let them fail if we are about to lock up. Still requires changes, but is probably more comprehensible for device model developers.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#39 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 7 lines · Jan Kiszka
Quoted 3 lines

DMA is inherently asynchronous, so we already drop the lock between initiation and completion; we need to find a way to make it easy to use the API without taking the lock while the transfer takes place.

We will have to review/rework device models that want to use the new locking scheme in a way that they can drop their own lock while issuing DMA. But that is surely non-trivial.

Most DMA today happens without the big qemu lock. We only need to convert the paths that actually access memory, these are the block and network layers (for the respective devices).

Quoted 3 lines

The other option is to keep DMA requests issued by devices synchronous but let them fail if we are about to lock up. Still requires changes, but is probably more comprehensible for device model developers.

How do you handle failures?

Signature · 1 line
error compiling committee.c: too many arguments to function
#40 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 6 lines · Avi Kivity
Quoted 4 lines · Jan Kiszka
Quoted 3 lines

DMA is inherently asynchronous, so we already drop the lock between initiation and completion; we need to find a way to make it easy to use the API without taking the lock while the transfer takes place.

Quoted 7 lines
Quoted 3 lines

We will have to review/rework device models that want to use the new locking scheme in a way that they can drop their own lock while issuing DMA. But that is surely non-trivial.

Most DMA today happens without the big qemu lock. We only need to convert the paths that actually access memory, these are the block and network layers (for the respective devices).

...and the devices themselves, of course.

Quoted 6 lines
Quoted 4 lines

The other option is to keep DMA requests issued by devices synchronous but let them fail if we are about to lock up. Still requires changes, but is probably more comprehensible for device model developers.

How do you handle failures?

By not sending a network frame or dropping an incoming one, e.g., and signaling this in a device specific way.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#41 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 11 lines · Jan Kiszka
Quoted 10 lines · Avi Kivity
Quoted 7 lines · Jan Kiszka
Quoted 3 lines

DMA is inherently asynchronous, so we already drop the lock between initiation and completion; we need to find a way to make it easy to use the API without taking the lock while the transfer takes place.

We will have to review/rework device models that want to use the new locking scheme in a way that they can drop their own lock while issuing DMA. But that is surely non-trivial.

Quoted 5 lines
Quoted 3 lines

Most DMA today happens without the big qemu lock. We only need to convert the paths that actually access memory, these are the block and network layers (for the respective devices).

...and the devices themselves, of course.

Right, for descriptors and stuff. So a guest can set a DMA address to point at its own BAR, and cause qemu to deadlock.

The problem is not limited to locking. A guest can also cause a write to a BAR to initiate a synchronous write with the same address and data, triggering infinite recursion.

Perhaps one fix is the mythical DMA API, which will provide each DMA initiator with its own view of memory (a private MemoryRegion root node). Even that can be worked around with a pair of devices accessing each other.

Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

The other option is to keep DMA requests issued by devices synchronous but let them fail if we are about to lock up. Still requires changes, but is probably more comprehensible for device model developers.

How do you handle failures?

By not sending a network frame or dropping an incoming one, e.g., and signaling this in a device specific way.

Doesn't work for block devices.

Signature · 1 line
error compiling committee.c: too many arguments to function
#42 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 21 lines · Avi Kivity
Quoted 11 lines · Jan Kiszka
Quoted 10 lines · Avi Kivity
Quoted 7 lines · Jan Kiszka
Quoted 3 lines

DMA is inherently asynchronous, so we already drop the lock between initiation and completion; we need to find a way to make it easy to use the API without taking the lock while the transfer takes place.

We will have to review/rework device models that want to use the new locking scheme in a way that they can drop their own lock while issuing DMA. But that is surely non-trivial.

Quoted 5 lines
Quoted 3 lines

Most DMA today happens without the big qemu lock. We only need to convert the paths that actually access memory, these are the block and network layers (for the respective devices).

...and the devices themselves, of course.

Right, for descriptors and stuff. So a guest can set a DMA address to point at its own BAR, and cause qemu to deadlock.

Quoted 8 lines

The problem is not limited to locking. A guest can also cause a write to a BAR to initiate a synchronous write with the same address and data, triggering infinite recursion.

Perhaps one fix is the mythical DMA API, which will provide each DMA initiator with its own view of memory (a private MemoryRegion root node). Even that can be worked around with a pair of devices accessing each other.

Hmm, don't see an alternative to runtime loop detection yet.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The other option is to keep DMA requests issued by devices synchronous but let them fail if we are about to lock up. Still requires changes, but is probably more comprehensible for device model developers.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

How do you handle failures?

By not sending a network frame or dropping an incoming one, e.g., and signaling this in a device specific way.

Doesn't work for block devices.

Because the block layer API cannot report errors to the devices? What happens if there is a real I/O error?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#43 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 14 lines · Jan Kiszka
Quoted 14 lines
Quoted 6 lines
Quoted 4 lines

Most DMA today happens without the big qemu lock. We only need to convert the paths that actually access memory, these are the block and network layers (for the respective devices).

...and the devices themselves, of course.

Right, for descriptors and stuff. So a guest can set a DMA address to point at its own BAR, and cause qemu to deadlock.

The problem is not limited to locking. A guest can also cause a write to a BAR to initiate a synchronous write with the same address and data, triggering infinite recursion.

Quoted 6 lines
Quoted 4 lines

Perhaps one fix is the mythical DMA API, which will provide each DMA initiator with its own view of memory (a private MemoryRegion root node). Even that can be worked around with a pair of devices accessing each other.

Hmm, don't see an alternative to runtime loop detection yet.

See an expensive one on the other branch of this thread.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 7 lines

The other option is to keep DMA requests issued by devices synchronous but let them fail if we are about to lock up. Still requires changes, but is probably more comprehensible for device model developers.

How do you handle failures?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

By not sending a network frame or dropping an incoming one, e.g., and signaling this in a device specific way.

Doesn't work for block devices.

Because the block layer API cannot report errors to the devices? What happens if there is a real I/O error?

We report real I/O errors. But if we report a transient error due to some lock being taken as an I/O error, the guest will take unwarranted action.

If the errors are not expected in normal operation (we can avoid them if all DMA is to real RAM) then this is an acceptable solution. Still it generates a lot of rarely used code paths and so isn't very good for security.

Signature · 1 line
error compiling committee.c: too many arguments to function
#44 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 14 lines · Avi Kivity
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

The other option is to keep DMA requests issued by devices synchronous but let them fail if we are about to lock up. Still requires changes, but is probably more comprehensible for device model developers.

How do you handle failures?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

By not sending a network frame or dropping an incoming one, e.g., and signaling this in a device specific way.

Doesn't work for block devices.

Because the block layer API cannot report errors to the devices? What happens if there is a real I/O error?

Quoted 8 lines

We report real I/O errors. But if we report a transient error due to some lock being taken as an I/O error, the guest will take unwarranted action.

If the errors are not expected in normal operation (we can avoid them if all DMA is to real RAM) then this is an acceptable solution. Still it generates a lot of rarely used code paths and so isn't very good for security.

I'm not talking about transient errors. Recursions like this are always guest configuration errors that would cause real devices to lock up or timeout as well. This is practically about avoiding that a malicious guest can lock up QEMU, leaving it inoperative even for management tools.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#45 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 24 lines · Jan Kiszka
Quoted 14 lines · Avi Kivity
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

The other option is to keep DMA requests issued by devices synchronous but let them fail if we are about to lock up. Still requires changes, but is probably more comprehensible for device model developers.

How do you handle failures?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

By not sending a network frame or dropping an incoming one, e.g., and signaling this in a device specific way.

Doesn't work for block devices.

Because the block layer API cannot report errors to the devices? What happens if there is a real I/O error?

Quoted 8 lines

We report real I/O errors. But if we report a transient error due to some lock being taken as an I/O error, the guest will take unwarranted action.

If the errors are not expected in normal operation (we can avoid them if all DMA is to real RAM) then this is an acceptable solution. Still it generates a lot of rarely used code paths and so isn't very good for security.

Quoted 4 lines

I'm not talking about transient errors. Recursions like this are always guest configuration errors that would cause real devices to lock up or timeout as well. This is practically about avoiding that a malicious guest can lock up QEMU, leaving it inoperative even for management tools.

Ok. That's more palatable. We don't even have to report an error in that case, we can just perform the operation incorrectly (as I'm sure real hardware will), log an error, and continue.

Signature · 1 line
error compiling committee.c: too many arguments to function
#46 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Ping for PMM,

This is the root case of your block on the SDHCI series - this is a discussion on resolution to bogus infinite looping DMA. For current participants in this discussion, heres our thread on the same topic over in SD land:

http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01017.html

With the findings here and acknowledgement that this is a general problem, we either need to declare this issue of scope for SDHCI, or work with these guys (in the immediate future) on the DMA infinite loop problem flagged here. I dont mind if SDHCI+ADMA is the guinea pig here, but can we get a decisive plan for going forward with this issue if it is going to continue to block SDHCI.

Thanks to Igor for identifying the overlap between these discussions.

Regards,
Peter

Quoted 38 lines · Avi Kivity
Quoted 24 lines · Jan Kiszka
Quoted 14 lines · Avi Kivity
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

The other option is to keep DMA requests issued by devices synchronous but let them fail if we are about to lock up. Still requires changes, but is probably more comprehensible for device model developers.

How do you handle failures?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

By not sending a network frame or dropping an incoming one, e.g., and signaling this in a device specific way.

Doesn't work for block devices.

Because the block layer API cannot report errors to the devices? What happens if there is a real I/O error?

Quoted 8 lines

We report real I/O errors. But if we report a transient error due to some lock being taken as an I/O error, the guest will take unwarranted action.

If the errors are not expected in normal operation (we can avoid them if all DMA is to real RAM) then this is an acceptable solution. Still it generates a lot of rarely used code paths and so isn't very good for security.

Quoted 4 lines

I'm not talking about transient errors. Recursions like this are always guest configuration errors that would cause real devices to lock up or timeout as well. This is practically about avoiding that a malicious guest can lock up QEMU, leaving it inoperative even for management tools.

Ok. That's more palatable. We don't even have to report an error in that case, we can just perform the operation incorrectly (as I'm sure real hardware will), log an error, and continue.

--
error compiling committee.c: too many arguments to function

#47 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 9 lines · Peter Crosthwaite

Ping for PMM,

This is the root case of your block on the SDHCI series - this is a discussion on resolution to bogus infinite looping DMA. For current participants in this discussion, heres our thread on the same topic over in SD land:

http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01017.html

Quoted 8 lines

With the findings here and acknowledgement that this is a general problem, we either need to declare this issue of scope for SDHCI, or work with these guys (in the immediate future) on the DMA infinite loop problem flagged here. I dont mind if SDHCI+ADMA is the guinea pig here, but can we get a decisive plan for going forward with this issue if it is going to continue to block SDHCI.

Thanks to Igor for identifying the overlap between these discussions.

Hi,

A couple of dumb questions.

What is the reason for the blocker? that possible guest dos is worse than no functionality at all?

Can't you put the DMA/IO processing into the background?

what's the diff between setup of bad descriptors and writing a while (1) loop in the guest CPU?

Cheers,
E

#48 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 19 lines · Edgar E. Iglesias
Quoted 9 lines · Peter Crosthwaite

Ping for PMM,

This is the root case of your block on the SDHCI series - this is a discussion on resolution to bogus infinite looping DMA. For current participants in this discussion, heres our thread on the same topic over in SD land:

http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01017.html

Quoted 8 lines

With the findings here and acknowledgement that this is a general problem, we either need to declare this issue of scope for SDHCI, or work with these guys (in the immediate future) on the DMA infinite loop problem flagged here. I dont mind if SDHCI+ADMA is the guinea pig here, but can we get a decisive plan for going forward with this issue if it is going to continue to block SDHCI.

Thanks to Igor for identifying the overlap between these discussions.

Quoted 8 lines

Hi,

A couple of dumb questions.

What is the reason for the blocker? that possible guest dos is worse than no functionality at all?

Can't you put the DMA/IO processing into the background?

I dont know a way do do asynchronous DMA unless I am missing something here. So what happens is we have a device that walks a SG list synchronously while holding all the locks and stuff being discussed here. If that SG list infinite loops then crash.

Quoted 4 lines

what's the diff between setup of bad descriptors and writing a while (1) loop in the guest CPU?

While(1) in guest doesnt freeze all of QEMU (monitor still works and stuff), wheras the DMA ops going in circles does, as locks are taken by an infinite loop.

Regards,
peter

Quoted 2 lines

Cheers,
E

#49 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 25 lines · Peter Crosthwaite
Quoted 23 lines · Edgar E. Iglesias
Quoted 9 lines · Peter Crosthwaite

Ping for PMM,

This is the root case of your block on the SDHCI series - this is a discussion on resolution to bogus infinite looping DMA. For current participants in this discussion, heres our thread on the same topic over in SD land:

http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01017.html

Quoted 8 lines

With the findings here and acknowledgement that this is a general problem, we either need to declare this issue of scope for SDHCI, or work with these guys (in the immediate future) on the DMA infinite loop problem flagged here. I dont mind if SDHCI+ADMA is the guinea pig here, but can we get a decisive plan for going forward with this issue if it is going to continue to block SDHCI.

Thanks to Igor for identifying the overlap between these discussions.

Hi,

A couple of dumb questions.

Quoted 7 lines
Quoted 4 lines

What is the reason for the blocker? that possible guest dos is worse than no functionality at all?

Can't you put the DMA/IO processing into the background?

I dont know a way do do asynchronous DMA unless I am missing something here.

You could schedule a bottom half and do the accesses from there. Solves nothing though.

Quoted 3 lines

So what happens is we have a device that walks a SG list synchronously while holding all the locks and stuff being discussed here. If that SG list infinite loops then crash.

Did you mean loop, or recursion into the memory region that initiates DMA?

As this problem already exists I don't think new device models need block on it.

Maybe a simple fix is:

void my_device_write(...)
{

   if (dma_in_progress) {
       return;

}
...
s->dma_in_progress = true;
cpu_physical_memory_rw(...)
s->dma_in_progress = false;
}

and we could try to figure out how to move this into common code.

Signature · 1 line
error compiling committee.c: too many arguments to function
#50 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 34 lines · Avi Kivity
Quoted 25 lines · Peter Crosthwaite
Quoted 23 lines · Edgar E. Iglesias
Quoted 9 lines · Peter Crosthwaite

Ping for PMM,

This is the root case of your block on the SDHCI series - this is a discussion on resolution to bogus infinite looping DMA. For current participants in this discussion, heres our thread on the same topic over in SD land:

http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01017.html

Quoted 8 lines

With the findings here and acknowledgement that this is a general problem, we either need to declare this issue of scope for SDHCI, or work with these guys (in the immediate future) on the DMA infinite loop problem flagged here. I dont mind if SDHCI+ADMA is the guinea pig here, but can we get a decisive plan for going forward with this issue if it is going to continue to block SDHCI.

Thanks to Igor for identifying the overlap between these discussions.

Hi,

A couple of dumb questions.

Quoted 7 lines
Quoted 4 lines

What is the reason for the blocker? that possible guest dos is worse than no functionality at all?

Can't you put the DMA/IO processing into the background?

I dont know a way do do asynchronous DMA unless I am missing something here.

Quoted 8 lines

You could schedule a bottom half and do the accesses from there. Solves nothing though.

Quoted 3 lines

So what happens is we have a device that walks a SG list synchronously while holding all the locks and stuff being discussed here. If that SG list infinite loops then crash.

Did you mean loop, or recursion into the memory region that initiates DMA?

I think we were discussing the loop issue (I hadn't thought of recursion issues before) :)

Jan's point of resetability was interesting.

Processing a finite amount of dma descriptors and scheduling bh's to continously get back and continue processing should be OK no?

That would avoid the lockups and allow the device to be reset at any time. Or am I missing something?

IIRC the etrax dma does something like that but only for receive dma rings...

Cheers,
Edgar

#51 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 44 lines · Edgar E. Iglesias
Quoted 34 lines · Avi Kivity
Quoted 25 lines · Peter Crosthwaite
Quoted 23 lines · Edgar E. Iglesias
Quoted 9 lines · Peter Crosthwaite

Ping for PMM,

This is the root case of your block on the SDHCI series - this is a discussion on resolution to bogus infinite looping DMA. For current participants in this discussion, heres our thread on the same topic over in SD land:

http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01017.html

Quoted 8 lines

With the findings here and acknowledgement that this is a general problem, we either need to declare this issue of scope for SDHCI, or work with these guys (in the immediate future) on the DMA infinite loop problem flagged here. I dont mind if SDHCI+ADMA is the guinea pig here, but can we get a decisive plan for going forward with this issue if it is going to continue to block SDHCI.

Thanks to Igor for identifying the overlap between these discussions.

Hi,

A couple of dumb questions.

Quoted 7 lines
Quoted 4 lines

What is the reason for the blocker? that possible guest dos is worse than no functionality at all?

Can't you put the DMA/IO processing into the background?

I dont know a way do do asynchronous DMA unless I am missing something here.

Quoted 8 lines

You could schedule a bottom half and do the accesses from there. Solves nothing though.

Quoted 3 lines

So what happens is we have a device that walks a SG list synchronously while holding all the locks and stuff being discussed here. If that SG list infinite loops then crash.

Did you mean loop, or recursion into the memory region that initiates DMA?

Quoted 8 lines

I think we were discussing the loop issue (I hadn't thought of recursion issues before) :)

Jan's point of resetability was interesting.

Processing a finite amount of dma descriptors and scheduling bh's to continously get back and continue processing should be OK no?

Yes.

Quoted 3 lines

That would avoid the lockups and allow the device to be reset at any time. Or am I missing something?

Not that I can see. If real hardware can be looped, so can qemu. I'm only worried about recursion and deadlocks (while real hardware can deadlock, we'd prefer to avoid that).

Signature · 1 line
error compiling committee.c: too many arguments to function
#52 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 56 lines · Avi Kivity
Quoted 44 lines · Edgar E. Iglesias
Quoted 34 lines · Avi Kivity
Quoted 25 lines · Peter Crosthwaite
Quoted 23 lines · Edgar E. Iglesias
Quoted 9 lines · Peter Crosthwaite

Ping for PMM,

This is the root case of your block on the SDHCI series - this is a discussion on resolution to bogus infinite looping DMA. For current participants in this discussion, heres our thread on the same topic over in SD land:

http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01017.html

Quoted 8 lines

With the findings here and acknowledgement that this is a general problem, we either need to declare this issue of scope for SDHCI, or work with these guys (in the immediate future) on the DMA infinite loop problem flagged here. I dont mind if SDHCI+ADMA is the guinea pig here, but can we get a decisive plan for going forward with this issue if it is going to continue to block SDHCI.

Thanks to Igor for identifying the overlap between these discussions.

Hi,

A couple of dumb questions.

Quoted 7 lines
Quoted 4 lines

What is the reason for the blocker? that possible guest dos is worse than no functionality at all?

Can't you put the DMA/IO processing into the background?

I dont know a way do do asynchronous DMA unless I am missing something here.

Quoted 8 lines

You could schedule a bottom half and do the accesses from there. Solves nothing though.

Quoted 3 lines

So what happens is we have a device that walks a SG list synchronously while holding all the locks and stuff being discussed here. If that SG list infinite loops then crash.

Did you mean loop, or recursion into the memory region that initiates DMA?

Quoted 8 lines

I think we were discussing the loop issue (I hadn't thought of recursion issues before) :)

Jan's point of resetability was interesting.

Processing a finite amount of dma descriptors and scheduling bh's to continously get back and continue processing should be OK no?

Yes.

Quoted 7 lines
Quoted 3 lines

That would avoid the lockups and allow the device to be reset at any time. Or am I missing something?

Not that I can see. If real hardware can be looped, so can qemu. I'm only worried about recursion and deadlocks (while real hardware can deadlock, we'd prefer to avoid that).

Agreed, thanks

#53 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

In reply to Avi Kivity
Quoted 43 lines · Avi Kivity
Quoted 42 lines · Edgar E. Iglesias
Quoted 31 lines · Avi Kivity
Quoted 22 lines · Peter Crosthwaite
Quoted 20 lines · Edgar E. Iglesias
Quoted 9 lines · Peter Crosthwaite

Ping for PMM,

This is the root case of your block on the SDHCI series - this is a discussion on resolution to bogus infinite looping DMA. For current participants in this discussion, heres our thread on the same topic over in SD land:

http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01017.html

Quoted 8 lines

With the findings here and acknowledgement that this is a general problem, we either need to declare this issue of scope for SDHCI, or work with these guys (in the immediate future) on the DMA infinite loop problem flagged here. I dont mind if SDHCI+ADMA is the guinea pig here, but can we get a decisive plan for going forward with this issue if it is going to continue to block SDHCI.

Thanks to Igor for identifying the overlap between these discussions.

Hi,

Quoted 8 lines
Quoted 6 lines

A couple of dumb questions.

What is the reason for the blocker? that possible guest dos is worse than no functionality at all?

Can't you put the DMA/IO processing into the background?

I dont know a way do do asynchronous DMA unless I am missing something here.

Quoted 7 lines

You could schedule a bottom half and do the accesses from there. Solves nothing though.

Quoted 3 lines

So what happens is we have a device that walks a SG list synchronously while holding all the locks and stuff being discussed here. If that SG list infinite loops then crash.

Did you mean loop, or recursion into the memory region that initiates DMA?

I think we were discussing the loop issue (I hadn't thought of recursion issues before) :)

Quoted 6 lines
Quoted 5 lines

Jan's point of resetability was interesting.

Processing a finite amount of dma descriptors and scheduling bh's to continously get back and continue processing should be OK no?

Yes.

Bottom half idea sounds good.
Also, for this particular device, rather then limiting amount of processed descriptor entries in bottom half, it seems reasonable to interrupt bh handler if we encounter a LINK descriptor since only recursive LINKs can generate infinite loops. In that case, a very long descriptor table without a single LINK entry could potentially hung QEMU for a long time, but that time will be finite anyway. Long term, this problem will disappear when/if someone converts QEMU SD card model to use async io.

Quoted 8 lines
Quoted 3 lines

That would avoid the lockups and allow the device to be reset at any time. Or am I missing something?

Not that I can see. If real hardware can be looped, so can qemu. I'm only worried about recursion and deadlocks (while real hardware can deadlock, we'd prefer to avoid that).

So, I think the idea here is that if real hardware can be locked we should lock too, but provide a guest CPU a possibility to abort locked operation. For this particular example, SD card controller can deadlock on recursive descriptor LINK entries, but CPU can abort ongoing transaction at any time by issuing ABORT command. And if guest CPU busy-waits in infinite while() loop for a TRANSFER OVER flag to be set, then it had it coming.

#54 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 9 lines · Igor Mitsyanko
Quoted 8 lines
Quoted 3 lines

That would avoid the lockups and allow the device to be reset at any time. Or am I missing something?

Not that I can see. If real hardware can be looped, so can qemu. I'm only worried about recursion and deadlocks (while real hardware can deadlock, we'd prefer to avoid that).

Quoted 7 lines

So, I think the idea here is that if real hardware can be locked we should lock too, but provide a guest CPU a possibility to abort locked operation. For this particular example, SD card controller can deadlock on recursive descriptor LINK entries, but CPU can abort ongoing transaction at any time by issuing ABORT command. And if guest CPU busy-waits in infinite while() loop for a TRANSFER OVER flag to be set, then it had it coming.

If real hardware would lock up so should we (in the guest's eyes) but the qemu monitor should remain responsive. It is not part of emulated hardware.

Signature · 1 line
error compiling committee.c: too many arguments to function
#55 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

In reply to Peter Crosthwaite
Quoted 28 lines · Peter Crosthwaite
Quoted 26 lines · Edgar E. Iglesias
Quoted 9 lines · Peter Crosthwaite

Ping for PMM,

This is the root case of your block on the SDHCI series - this is a discussion on resolution to bogus infinite looping DMA. For current participants in this discussion, heres our thread on the same topic over in SD land:

http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01017.html

Quoted 8 lines

With the findings here and acknowledgement that this is a general problem, we either need to declare this issue of scope for SDHCI, or work with these guys (in the immediate future) on the DMA infinite loop problem flagged here. I dont mind if SDHCI+ADMA is the guinea pig here, but can we get a decisive plan for going forward with this issue if it is going to continue to block SDHCI.

Thanks to Igor for identifying the overlap between these discussions.

Hi,

A couple of dumb questions.

What is the reason for the blocker? that possible guest dos is worse than no functionality at all?

Quoted 6 lines
Quoted 1 line

Can't you put the DMA/IO processing into the background?

I dont know a way do do asynchronous DMA unless I am missing something here. So what happens is we have a device that walks a SG list synchronously while holding all the locks and stuff being discussed here. If that SG list infinite loops then crash.

We could switch to async DMA, but most DMA-capable devices aren't prepared for rescheduling points in the middle of their code. This will require quite a bit of code review.

Quoted 9 lines
Quoted 5 lines

what's the diff between setup of bad descriptors and writing a while (1) loop in the guest CPU?

While(1) in guest doesnt freeze all of QEMU (monitor still works and stuff), wheras the DMA ops going in circles does, as locks are taken by an infinite loop.

That's the point. If we loop, we need at least a way to stop it by issuing a device or system reset. But I tend to think that detecting loops and failing/ignoring requests is easier implementable.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#56 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 28 lines · Jan Kiszka
Quoted 27 lines · Peter Crosthwaite
Quoted 25 lines · Edgar E. Iglesias
Quoted 9 lines · Peter Crosthwaite

Ping for PMM,

This is the root case of your block on the SDHCI series - this is a discussion on resolution to bogus infinite looping DMA. For current participants in this discussion, heres our thread on the same topic over in SD land:

http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01017.html

Quoted 8 lines

With the findings here and acknowledgement that this is a general problem, we either need to declare this issue of scope for SDHCI, or work with these guys (in the immediate future) on the DMA infinite loop problem flagged here. I dont mind if SDHCI+ADMA is the guinea pig here, but can we get a decisive plan for going forward with this issue if it is going to continue to block SDHCI.

Thanks to Igor for identifying the overlap between these discussions.

Hi,

A couple of dumb questions.

What is the reason for the blocker? that possible guest dos is worse than no functionality at all?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Can't you put the DMA/IO processing into the background?

I dont know a way do do asynchronous DMA unless I am missing something here. So what happens is we have a device that walks a SG list synchronously while holding all the locks and stuff being discussed here. If that SG list infinite loops then crash.

We could switch to async DMA, but most DMA-capable devices aren't prepared for rescheduling points in the middle of their code. This will require quite a bit of code review.

From what I understand, we can't switch to async DMA while SD card model doesn't support it. And introducing this support would require to convert every existing SD card user in QEMU. I can recall that such a conversion was already suggested in a mailing list a while ago, but no one (including me) volunteered to do it :)

Quoted 12 lines
Quoted 6 lines
Quoted 3 lines

what's the diff between setup of bad descriptors and writing a while (1) loop in the guest CPU?

While(1) in guest doesnt freeze all of QEMU (monitor still works and stuff), wheras the DMA ops going in circles does, as locks are taken by an infinite loop.

That's the point. If we loop, we need at least a way to stop it by issuing a device or system reset. But I tend to think that detecting loops and failing/ignoring requests is easier implementable.

Jan

#57 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

In reply to liu ping fan
Quoted 8 lines · liu ping fan
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

Quoted 7 lines
Quoted 2 lines

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

That is our core problem: inconsistent invocation of existing services /wrt locking. For portio, I was lucky that there is no nesting and I was able to drop the big lock around all (x86) call sites. But MMIO is way more tricky due to DMA nesting.

We could try to introduce a different version of cpu_physical_memory_rw, cpu_physical_memory_rw_unlocked. But the problem remains that an MMIO request can trigger the very same access in a nested fashion, and we will have to detect this to avoid locking up QEMU (locking up the guest might be OK).

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#58 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 17 lines · Jan Kiszka
Quoted 8 lines · liu ping fan
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

Quoted 7 lines
Quoted 2 lines

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

Quoted 4 lines

That is our core problem: inconsistent invocation of existing services /wrt locking. For portio, I was lucky that there is no nesting and I was able to drop the big lock around all (x86) call sites. But MMIO is way more tricky due to DMA nesting.

Maybe we need to switch to a continuation style. Instead of expecting cpu_physical_memory_rw() to complete synchronously, it becomes an asynchronous call and you provide it with a completion. That means devices which use it are forced to drop the lock in between. Block and network clients will be easy to convert since they already use APIs that drop the lock (except for accessing the descriptors).

Quoted 5 lines

We could try to introduce a different version of cpu_physical_memory_rw, cpu_physical_memory_rw_unlocked. But the problem remains that an MMIO request can trigger the very same access in a nested fashion, and we will have to detect this to avoid locking up QEMU (locking up the guest might be OK).

An async version of c_p_m_rw() will just cause a completion to bounce around, consuming cpu but not deadlocking anything. If we can keep a count of the bounces, we might be able to stall it indefinitely or at least ratelimit it.

Signature · 1 line
error compiling committee.c: too many arguments to function
#59 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 36 lines · Avi Kivity
Quoted 17 lines · Jan Kiszka
Quoted 8 lines · liu ping fan
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

Quoted 7 lines
Quoted 2 lines

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

Quoted 4 lines

That is our core problem: inconsistent invocation of existing services /wrt locking. For portio, I was lucky that there is no nesting and I was able to drop the big lock around all (x86) call sites. But MMIO is way more tricky due to DMA nesting.

Maybe we need to switch to a continuation style. Instead of expecting cpu_physical_memory_rw() to complete synchronously, it becomes an asynchronous call and you provide it with a completion. That means devices which use it are forced to drop the lock in between. Block and network clients will be easy to convert since they already use APIs that drop the lock (except for accessing the descriptors).

Quoted 5 lines

We could try to introduce a different version of cpu_physical_memory_rw, cpu_physical_memory_rw_unlocked. But the problem remains that an MMIO request can trigger the very same access in a nested fashion, and we will have to detect this to avoid locking up QEMU (locking up the guest might be OK).

Quoted 5 lines

An async version of c_p_m_rw() will just cause a completion to bounce around, consuming cpu but not deadlocking anything. If we can keep a count of the bounces, we might be able to stall it indefinitely or at least ratelimit it.

Another option is to require all users of c_p_m_rw() and related to use a coroutine or thread. That makes the programming easier (but still required a revalidation after the dropped lock).

Signature · 1 line
error compiling committee.c: too many arguments to function
#60 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 43 lines · Avi Kivity
Quoted 36 lines · Avi Kivity
Quoted 17 lines · Jan Kiszka
Quoted 8 lines · liu ping fan
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

Quoted 7 lines
Quoted 2 lines

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

Quoted 4 lines

That is our core problem: inconsistent invocation of existing services /wrt locking. For portio, I was lucky that there is no nesting and I was able to drop the big lock around all (x86) call sites. But MMIO is way more tricky due to DMA nesting.

Maybe we need to switch to a continuation style. Instead of expecting cpu_physical_memory_rw() to complete synchronously, it becomes an asynchronous call and you provide it with a completion. That means devices which use it are forced to drop the lock in between. Block and network clients will be easy to convert since they already use APIs that drop the lock (except for accessing the descriptors).

Quoted 5 lines

We could try to introduce a different version of cpu_physical_memory_rw, cpu_physical_memory_rw_unlocked. But the problem remains that an MMIO request can trigger the very same access in a nested fashion, and we will have to detect this to avoid locking up QEMU (locking up the guest might be OK).

Quoted 5 lines

An async version of c_p_m_rw() will just cause a completion to bounce around, consuming cpu but not deadlocking anything. If we can keep a count of the bounces, we might be able to stall it indefinitely or at least ratelimit it.

Quoted 3 lines

Another option is to require all users of c_p_m_rw() and related to use a coroutine or thread. That makes the programming easier (but still required a revalidation after the dropped lock).

Unless i am misunderstanding this thread, the iothread flow section of
http://lists.gnu.org/archive/html/qemu-devel/2012-06/msg04315.html
contains possible solutions for this.

Basically use trylock() and if it fails, then choose a bailout option (which there are more than one possibilities listed).

#61 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 48 lines · Marcelo Tosatti
Quoted 43 lines · Avi Kivity
Quoted 36 lines · Avi Kivity
Quoted 17 lines · Jan Kiszka
Quoted 8 lines · liu ping fan
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

Quoted 7 lines
Quoted 2 lines

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

Quoted 4 lines

That is our core problem: inconsistent invocation of existing services /wrt locking. For portio, I was lucky that there is no nesting and I was able to drop the big lock around all (x86) call sites. But MMIO is way more tricky due to DMA nesting.

Maybe we need to switch to a continuation style. Instead of expecting cpu_physical_memory_rw() to complete synchronously, it becomes an asynchronous call and you provide it with a completion. That means devices which use it are forced to drop the lock in between. Block and network clients will be easy to convert since they already use APIs that drop the lock (except for accessing the descriptors).

Quoted 5 lines

We could try to introduce a different version of cpu_physical_memory_rw, cpu_physical_memory_rw_unlocked. But the problem remains that an MMIO request can trigger the very same access in a nested fashion, and we will have to detect this to avoid locking up QEMU (locking up the guest might be OK).

Quoted 5 lines

An async version of c_p_m_rw() will just cause a completion to bounce around, consuming cpu but not deadlocking anything. If we can keep a count of the bounces, we might be able to stall it indefinitely or at least ratelimit it.

Quoted 3 lines

Another option is to require all users of c_p_m_rw() and related to use a coroutine or thread. That makes the programming easier (but still required a revalidation after the dropped lock).

Quoted 7 lines

Unless i am misunderstanding this thread, the iothread flow section of
http://lists.gnu.org/archive/html/qemu-devel/2012-06/msg04315.html
contains possible solutions for this.

Basically use trylock() and if it fails, then choose a bailout option (which there are more than one possibilities listed).

I think in that thread, the original goal of the trylock() is to solve the lock held by other thread, by here we want to resolve nested lock. But yes, there is similar point to solve it by async if we do not allow nested lock.

Regards,
pingfan

Quoted 1 line
#62 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

In reply to Avi Kivity
Quoted 43 lines · Avi Kivity
Quoted 36 lines · Avi Kivity
Quoted 17 lines · Jan Kiszka
Quoted 8 lines · liu ping fan
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

Quoted 7 lines
Quoted 2 lines

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

Quoted 4 lines

That is our core problem: inconsistent invocation of existing services /wrt locking. For portio, I was lucky that there is no nesting and I was able to drop the big lock around all (x86) call sites. But MMIO is way more tricky due to DMA nesting.

Maybe we need to switch to a continuation style. Instead of expecting cpu_physical_memory_rw() to complete synchronously, it becomes an asynchronous call and you provide it with a completion. That means devices which use it are forced to drop the lock in between. Block and network clients will be easy to convert since they already use APIs that drop the lock (except for accessing the descriptors).

Quoted 5 lines

We could try to introduce a different version of cpu_physical_memory_rw, cpu_physical_memory_rw_unlocked. But the problem remains that an MMIO request can trigger the very same access in a nested fashion, and we will have to detect this to avoid locking up QEMU (locking up the guest might be OK).

Quoted 5 lines

An async version of c_p_m_rw() will just cause a completion to bounce around, consuming cpu but not deadlocking anything. If we can keep a count of the bounces, we might be able to stall it indefinitely or at least ratelimit it.

Quoted 4 lines

Another option is to require all users of c_p_m_rw() and related to use a coroutine or thread. That makes the programming easier (but still required a revalidation after the dropped lock).

For the nested cpu_physical_memory_rw(), we change it internal but keep it sync API as it is. (Wrapper current cpu_physical_memory_rw() into cpu_physical_memory_rw_internal() )

LOCK() // can be device or big lock or both, depend on caller.
..............
cpu_physical_memory_rw()
{
UNLOCK() //unlock all the locks queue_work_on_thread(cpu_physical_memory_rw_internal, completion);
// cpu_physical_memory_rw_internal can take lock(device,biglock) again
wait_for_completion(completion)
LOCK()
}
..................
UNLOCK()

Although cpu_physical_memory_rw_internal() can be freed to use lock, but with precondition. -- We still need to trace lock stack taken by cpu_physical_memory_rw(), so that it can return to caller correctly. Is that OK?

Regards,
pingfan

Quoted 2 lines

--
error compiling committee.c: too many arguments to function

#63 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 53 lines · liu ping fan
Quoted 43 lines · Avi Kivity
Quoted 36 lines · Avi Kivity
Quoted 17 lines · Jan Kiszka
Quoted 8 lines · liu ping fan
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

Quoted 7 lines
Quoted 2 lines

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

Quoted 4 lines

That is our core problem: inconsistent invocation of existing services /wrt locking. For portio, I was lucky that there is no nesting and I was able to drop the big lock around all (x86) call sites. But MMIO is way more tricky due to DMA nesting.

Maybe we need to switch to a continuation style. Instead of expecting cpu_physical_memory_rw() to complete synchronously, it becomes an asynchronous call and you provide it with a completion. That means devices which use it are forced to drop the lock in between. Block and network clients will be easy to convert since they already use APIs that drop the lock (except for accessing the descriptors).

Quoted 5 lines

We could try to introduce a different version of cpu_physical_memory_rw, cpu_physical_memory_rw_unlocked. But the problem remains that an MMIO request can trigger the very same access in a nested fashion, and we will have to detect this to avoid locking up QEMU (locking up the guest might be OK).

Quoted 5 lines

An async version of c_p_m_rw() will just cause a completion to bounce around, consuming cpu but not deadlocking anything. If we can keep a count of the bounces, we might be able to stall it indefinitely or at least ratelimit it.

Quoted 4 lines

Another option is to require all users of c_p_m_rw() and related to use a coroutine or thread. That makes the programming easier (but still required a revalidation after the dropped lock).

For the nested cpu_physical_memory_rw(), we change it internal but keep it sync API as it is. (Wrapper current cpu_physical_memory_rw() into cpu_physical_memory_rw_internal() )

Quoted 13 lines

LOCK() // can be device or big lock or both, depend on caller.
..............
cpu_physical_memory_rw()
{
UNLOCK() //unlock all the locks queue_work_on_thread(cpu_physical_memory_rw_internal, completion);
// cpu_physical_memory_rw_internal can take lock(device,biglock) again
wait_for_completion(completion)
LOCK()
}
..................
UNLOCK()

This is dangerous. The caller expects to hold the lock across the call, and will not re-validate its state.

Quoted 4 lines

Although cpu_physical_memory_rw_internal() can be freed to use lock, but with precondition. -- We still need to trace lock stack taken by cpu_physical_memory_rw(), so that it can return to caller correctly. Is that OK?

I'm convinced that we need a recursive lock if we don't convert everything at once.

So how about:

- make bql a recursive lock (use pthreads, don't invent our own)
- change c_p_m_rw() calling convention to "caller may hold the BQL, but no device lock"

this allows devices to DMA each other. Unconverted device models will work as they used to. Converted device models will need to drop the device lock, re-acquire it, and revalidate any state. That will cause them to become more correct wrt recursive invocation.

Signature · 1 line
error compiling committee.c: too many arguments to function
#64 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 81 lines · Avi Kivity
Quoted 53 lines · liu ping fan
Quoted 43 lines · Avi Kivity
Quoted 36 lines · Avi Kivity
Quoted 17 lines · Jan Kiszka
Quoted 8 lines · liu ping fan
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

Quoted 7 lines
Quoted 2 lines

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

Quoted 4 lines

That is our core problem: inconsistent invocation of existing services /wrt locking. For portio, I was lucky that there is no nesting and I was able to drop the big lock around all (x86) call sites. But MMIO is way more tricky due to DMA nesting.

Maybe we need to switch to a continuation style. Instead of expecting cpu_physical_memory_rw() to complete synchronously, it becomes an asynchronous call and you provide it with a completion. That means devices which use it are forced to drop the lock in between. Block and network clients will be easy to convert since they already use APIs that drop the lock (except for accessing the descriptors).

Quoted 5 lines

We could try to introduce a different version of cpu_physical_memory_rw, cpu_physical_memory_rw_unlocked. But the problem remains that an MMIO request can trigger the very same access in a nested fashion, and we will have to detect this to avoid locking up QEMU (locking up the guest might be OK).

Quoted 5 lines

An async version of c_p_m_rw() will just cause a completion to bounce around, consuming cpu but not deadlocking anything. If we can keep a count of the bounces, we might be able to stall it indefinitely or at least ratelimit it.

Quoted 4 lines

Another option is to require all users of c_p_m_rw() and related to use a coroutine or thread. That makes the programming easier (but still required a revalidation after the dropped lock).

For the nested cpu_physical_memory_rw(), we change it internal but keep it sync API as it is. (Wrapper current cpu_physical_memory_rw() into cpu_physical_memory_rw_internal() )

Quoted 13 lines

LOCK() // can be device or big lock or both, depend on caller.
..............
cpu_physical_memory_rw()
{
UNLOCK() //unlock all the locks queue_work_on_thread(cpu_physical_memory_rw_internal, completion);
// cpu_physical_memory_rw_internal can take lock(device,biglock) again
wait_for_completion(completion)
LOCK()
}
..................
UNLOCK()

This is dangerous. The caller expects to hold the lock across the call, and will not re-validate its state.

Quoted 4 lines

Although cpu_physical_memory_rw_internal() can be freed to use lock, but with precondition. -- We still need to trace lock stack taken by cpu_physical_memory_rw(), so that it can return to caller correctly. Is that OK?

I'm convinced that we need a recursive lock if we don't convert everything at once.

So how about:

Quoted 7 lines

- make bql a recursive lock (use pthreads, don't invent our own)
- change c_p_m_rw() calling convention to "caller may hold the BQL, but no device lock"

this allows devices to DMA each other. Unconverted device models will work as they used to. Converted device models will need to drop the device lock, re-acquire it, and revalidate any state. That will cause

I think we are cornered by devices to DMA each other, and it raises the unavoidable nested lock. Also to avoid deadlock caused by device's lock sequence, we should drop the current device lock to acquire
another one. The only little diverge is about the "revalidate", do
we need to rollback? Or for converted device, we can just tag it a busy flag, that is check&set busy flag at the entry of device's mmio-dispatch. So when re-acquire device's lock, the device's state is intact.

Has anybody other suggestion?

Thanks and regards,
pingfan

Quoted 5 lines

them to become more correct wrt recursive invocation.

--
error compiling committee.c: too many arguments to function

#65 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 90 lines · liu ping fan
Quoted 81 lines · Avi Kivity
Quoted 53 lines · liu ping fan
Quoted 43 lines · Avi Kivity
Quoted 36 lines · Avi Kivity
Quoted 17 lines · Jan Kiszka
Quoted 8 lines · liu ping fan
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

Quoted 7 lines
Quoted 2 lines

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

Quoted 4 lines

That is our core problem: inconsistent invocation of existing services /wrt locking. For portio, I was lucky that there is no nesting and I was able to drop the big lock around all (x86) call sites. But MMIO is way more tricky due to DMA nesting.

Maybe we need to switch to a continuation style. Instead of expecting cpu_physical_memory_rw() to complete synchronously, it becomes an asynchronous call and you provide it with a completion. That means devices which use it are forced to drop the lock in between. Block and network clients will be easy to convert since they already use APIs that drop the lock (except for accessing the descriptors).

Quoted 5 lines

We could try to introduce a different version of cpu_physical_memory_rw, cpu_physical_memory_rw_unlocked. But the problem remains that an MMIO request can trigger the very same access in a nested fashion, and we will have to detect this to avoid locking up QEMU (locking up the guest might be OK).

Quoted 5 lines

An async version of c_p_m_rw() will just cause a completion to bounce around, consuming cpu but not deadlocking anything. If we can keep a count of the bounces, we might be able to stall it indefinitely or at least ratelimit it.

Quoted 4 lines

Another option is to require all users of c_p_m_rw() and related to use a coroutine or thread. That makes the programming easier (but still required a revalidation after the dropped lock).

For the nested cpu_physical_memory_rw(), we change it internal but keep it sync API as it is. (Wrapper current cpu_physical_memory_rw() into cpu_physical_memory_rw_internal() )

Quoted 13 lines

LOCK() // can be device or big lock or both, depend on caller.
..............
cpu_physical_memory_rw()
{
UNLOCK() //unlock all the locks queue_work_on_thread(cpu_physical_memory_rw_internal, completion);
// cpu_physical_memory_rw_internal can take lock(device,biglock) again
wait_for_completion(completion)
LOCK()
}
..................
UNLOCK()

This is dangerous. The caller expects to hold the lock across the call, and will not re-validate its state.

Quoted 4 lines

Although cpu_physical_memory_rw_internal() can be freed to use lock, but with precondition. -- We still need to trace lock stack taken by cpu_physical_memory_rw(), so that it can return to caller correctly. Is that OK?

I'm convinced that we need a recursive lock if we don't convert everything at once.

So how about:

Quoted 7 lines

- make bql a recursive lock (use pthreads, don't invent our own)
- change c_p_m_rw() calling convention to "caller may hold the BQL, but no device lock"

this allows devices to DMA each other. Unconverted device models will work as they used to. Converted device models will need to drop the device lock, re-acquire it, and revalidate any state. That will cause

Quoted 5 lines

I think we are cornered by devices to DMA each other, and it raises the unavoidable nested lock. Also to avoid deadlock caused by device's lock sequence, we should drop the current device lock to acquire
another one. The only little diverge is about the "revalidate", do
we need to rollback?

It basically means you can't hold contents of device state in local variables. You need to read everything again from the device. That includes things like DMA enable bits.

(btw: to we respect PCI_COMMAND_MASTER? it seems that we do not. This is a trivial example of an iommu, we should get that going).

Quoted 4 lines

Or for converted device, we can just tag it a
busy flag, that is check&set busy flag at the entry of device's mmio-dispatch. So when re-acquire device's lock, the device's state is intact.

The state can be changed by a parallel access to another register, which is valid.

Quoted 2 lines

Has anybody other suggestion?

Signature · 1 line
error compiling committee.c: too many arguments to function
#66 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 97 lines · Avi Kivity
Quoted 90 lines · liu ping fan
Quoted 81 lines · Avi Kivity
Quoted 53 lines · liu ping fan
Quoted 43 lines · Avi Kivity
Quoted 36 lines · Avi Kivity
Quoted 17 lines · Jan Kiszka
Quoted 8 lines · liu ping fan
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The func call chain can suffer from recursively hold qemu_mutex_lock_iothread. We introduce lockmap to record the lock depth.

Quoted 7 lines
Quoted 2 lines

What is the root cause? io handlers initiating I/O?

cpu_physical_memory_rw() can be called nested, and when called, it can be protected by no-lock/device lock/ big-lock.
I think without big lock, io-dispatcher should face the same issue. As to main-loop, have not carefully consider, but at least, dma-helper will call cpu_physical_memory_rw() with big-lock.

Quoted 4 lines

That is our core problem: inconsistent invocation of existing services /wrt locking. For portio, I was lucky that there is no nesting and I was able to drop the big lock around all (x86) call sites. But MMIO is way more tricky due to DMA nesting.

Maybe we need to switch to a continuation style. Instead of expecting cpu_physical_memory_rw() to complete synchronously, it becomes an asynchronous call and you provide it with a completion. That means devices which use it are forced to drop the lock in between. Block and network clients will be easy to convert since they already use APIs that drop the lock (except for accessing the descriptors).

Quoted 5 lines

We could try to introduce a different version of cpu_physical_memory_rw, cpu_physical_memory_rw_unlocked. But the problem remains that an MMIO request can trigger the very same access in a nested fashion, and we will have to detect this to avoid locking up QEMU (locking up the guest might be OK).

Quoted 5 lines

An async version of c_p_m_rw() will just cause a completion to bounce around, consuming cpu but not deadlocking anything. If we can keep a count of the bounces, we might be able to stall it indefinitely or at least ratelimit it.

Quoted 4 lines

Another option is to require all users of c_p_m_rw() and related to use a coroutine or thread. That makes the programming easier (but still required a revalidation after the dropped lock).

For the nested cpu_physical_memory_rw(), we change it internal but keep it sync API as it is. (Wrapper current cpu_physical_memory_rw() into cpu_physical_memory_rw_internal() )

Quoted 13 lines

LOCK() // can be device or big lock or both, depend on caller.
..............
cpu_physical_memory_rw()
{
UNLOCK() //unlock all the locks queue_work_on_thread(cpu_physical_memory_rw_internal, completion);
// cpu_physical_memory_rw_internal can take lock(device,biglock) again
wait_for_completion(completion)
LOCK()
}
..................
UNLOCK()

This is dangerous. The caller expects to hold the lock across the call, and will not re-validate its state.

Quoted 4 lines

Although cpu_physical_memory_rw_internal() can be freed to use lock, but with precondition. -- We still need to trace lock stack taken by cpu_physical_memory_rw(), so that it can return to caller correctly. Is that OK?

I'm convinced that we need a recursive lock if we don't convert everything at once.

So how about:

Quoted 7 lines

- make bql a recursive lock (use pthreads, don't invent our own)
- change c_p_m_rw() calling convention to "caller may hold the BQL, but no device lock"

this allows devices to DMA each other. Unconverted device models will work as they used to. Converted device models will need to drop the device lock, re-acquire it, and revalidate any state. That will cause

Quoted 5 lines

I think we are cornered by devices to DMA each other, and it raises the unavoidable nested lock. Also to avoid deadlock caused by device's lock sequence, we should drop the current device lock to acquire
another one. The only little diverge is about the "revalidate", do
we need to rollback?

Quoted 4 lines

It basically means you can't hold contents of device state in local variables. You need to read everything again from the device. That includes things like DMA enable bits.

I think that read everything again from the device can not work. Suppose the following scene: If the device's state contains the change of a series of internal registers (supposing partA+partB; partC+partD), after partA changed, the device's lock is broken. At this point, another access to this device, it will work on partA+partB to determine C+D, but since partB is not correctly updated yet. So C+D may be decided by broken context and be wrong.

Quoted 11 lines

(btw: to we respect PCI_COMMAND_MASTER? it seems that we do not. This is a trivial example of an iommu, we should get that going).

Quoted 4 lines

Or for converted device, we can just tag it a
busy flag, that is check&set busy flag at the entry of device's mmio-dispatch. So when re-acquire device's lock, the device's state is intact.

The state can be changed by a parallel access to another register, which is valid.

Do you mean that the device can be accessed in parallel? But how? we use device's lock.
What my suggestion is:
lock();
set_and_test(dev->busy);
if busy
unlock and return;
changing device registers;
do other things including calling to c_p_m_rw() //here,lock broken, but set_and_test() works
clear(dev->busy);
unlock();

So changing device registers is protected, and unbreakable.

Regards,
pingfan

Quoted 6 lines
Quoted 2 lines

Has anybody other suggestion?

--
error compiling committee.c: too many arguments to function

#67 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 5 lines · liu ping fan
Quoted 5 lines

It basically means you can't hold contents of device state in local variables. You need to read everything again from the device. That includes things like DMA enable bits.

Quoted 7 lines

I think that read everything again from the device can not work. Suppose the following scene: If the device's state contains the change of a series of internal registers (supposing partA+partB; partC+partD), after partA changed, the device's lock is broken. At this point, another access to this device, it will work on partA+partB to determine C+D, but since partB is not correctly updated yet. So C+D may be decided by broken context and be wrong.

That's the guest's problem. Note it could have happened even before the change, since the writes to A/B/C/D are unordered wrt the DMA.

Quoted 9 lines
Quoted 9 lines

(btw: to we respect PCI_COMMAND_MASTER? it seems that we do not. This is a trivial example of an iommu, we should get that going).

Quoted 4 lines

Or for converted device, we can just tag it a
busy flag, that is check&set busy flag at the entry of device's mmio-dispatch. So when re-acquire device's lock, the device's state is intact.

Quoted 5 lines
Quoted 3 lines

The state can be changed by a parallel access to another register, which is valid.

Do you mean that the device can be accessed in parallel? But how? we use device's lock.

Some DMA is asynchronous already, like networking and IDE DMA. So we drop the big lock while doing it. With the change to drop device locks around c_p_m_rw(), we have that problem everywhere.

Quoted 11 lines

What my suggestion is:
lock();
set_and_test(dev->busy);
if busy
unlock and return;
changing device registers;
do other things including calling to c_p_m_rw() //here,lock broken, but set_and_test() works
clear(dev->busy);
unlock();

Quoted 1 line

So changing device registers is protected, and unbreakable.

But the changes may be legitimate. Maybe you're writing to a completely unrelated register, from a different vcpu, now that write is lost.

Signature · 1 line
error compiling committee.c: too many arguments to function
#68 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 14 lines · Avi Kivity
Quoted 5 lines · liu ping fan
Quoted 5 lines

It basically means you can't hold contents of device state in local variables. You need to read everything again from the device. That includes things like DMA enable bits.

Quoted 7 lines

I think that read everything again from the device can not work. Suppose the following scene: If the device's state contains the change of a series of internal registers (supposing partA+partB; partC+partD), after partA changed, the device's lock is broken. At this point, another access to this device, it will work on partA+partB to determine C+D, but since partB is not correctly updated yet. So C+D may be decided by broken context and be wrong.

Quoted 3 lines

That's the guest's problem. Note it could have happened even before the change, since the writes to A/B/C/D are unordered wrt the DMA.

Yes, agree, it is the guest's problem. So it means that ready_of(A+B) is not signaled to guest, the guest should not launch operations on
(C+D). Right? But here comes the question, if ready not signaled to
guest, how can guest launch operation on (A+B) again?
i.e. although local lock is broken, the (A+B) is still intact when re-acquire local lock. So need not to read everything again from the device. Wrong?

Quoted 30 lines
Quoted 9 lines
Quoted 9 lines

(btw: to we respect PCI_COMMAND_MASTER? it seems that we do not. This is a trivial example of an iommu, we should get that going).

Quoted 4 lines

Or for converted device, we can just tag it a
busy flag, that is check&set busy flag at the entry of device's mmio-dispatch. So when re-acquire device's lock, the device's state is intact.

Quoted 5 lines
Quoted 3 lines

The state can be changed by a parallel access to another register, which is valid.

Do you mean that the device can be accessed in parallel? But how? we use device's lock.

Some DMA is asynchronous already, like networking and IDE DMA. So we drop the big lock while doing it. With the change to drop device locks around c_p_m_rw(), we have that problem everywhere.

Quoted 11 lines

What my suggestion is:
lock();
set_and_test(dev->busy);
if busy
unlock and return;
changing device registers;
do other things including calling to c_p_m_rw() //here,lock broken, but set_and_test() works
clear(dev->busy);
unlock();

Quoted 5 lines
Quoted 1 line

So changing device registers is protected, and unbreakable.

But the changes may be legitimate. Maybe you're writing to a completely unrelated register, from a different vcpu, now that write is lost.

But I think it will mean more-fine locks for each groups of unrelated register, and accordingly, the busy should be bitmap for each group.

Thanks and regards,
pingfan

Quoted 3 lines

--
error compiling committee.c: too many arguments to function

#69 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 15 lines · liu ping fan
Quoted 14 lines · Avi Kivity
Quoted 12 lines · liu ping fan
Quoted 5 lines

It basically means you can't hold contents of device state in local variables. You need to read everything again from the device. That includes things like DMA enable bits.

I think that read everything again from the device can not work. Suppose the following scene: If the device's state contains the change of a series of internal registers (supposing partA+partB; partC+partD), after partA changed, the device's lock is broken. At this point, another access to this device, it will work on partA+partB to determine C+D, but since partB is not correctly updated yet. So C+D may be decided by broken context and be wrong.

Quoted 7 lines
Quoted 3 lines

That's the guest's problem. Note it could have happened even before the change, since the writes to A/B/C/D are unordered wrt the DMA.

Yes, agree, it is the guest's problem. So it means that ready_of(A+B) is not signaled to guest, the guest should not launch operations on
(C+D). Right? But here comes the question, if ready not signaled to
guest, how can guest launch operation on (A+B) again?

It may be evil.

Quoted 3 lines

i.e. although local lock is broken, the (A+B) is still intact when re-acquire local lock. So need not to read everything again from the device. Wrong?

The device needs to perform according to its specifications. If the specifications allow for this kind of access, we must ensure it works. If they don't, we must ensure something sane happens, instead of a qemu crash or exploit. This means that anything dangerous like pointers must be revalidated. To be on the safe side, I recommend revalidating (or reloading) everything, but it may not be necessary in all cases.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

What my suggestion is:
lock();
set_and_test(dev->busy);
if busy
unlock and return;
changing device registers;
do other things including calling to c_p_m_rw() //here,lock broken, but set_and_test() works
clear(dev->busy);
unlock();

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

So changing device registers is protected, and unbreakable.

But the changes may be legitimate. Maybe you're writing to a completely unrelated register, from a different vcpu, now that write is lost.

But I think it will mean more-fine locks for each groups of unrelated register, and accordingly, the busy should be bitmap for each group.

It's possible. Let's defer the discussion until a concrete case is before us. It may be that different devices will want different solutions (though there is value in applying one solution everywhere).

Signature · 1 line
error compiling committee.c: too many arguments to function
#70 lore raw

Re: [Qemu-devel] [PATCH V3 10/11] vcpu: introduce lockmap

Quoted 30 lines · Avi Kivity
Quoted 15 lines · liu ping fan
Quoted 14 lines · Avi Kivity
Quoted 12 lines · liu ping fan
Quoted 5 lines

It basically means you can't hold contents of device state in local variables. You need to read everything again from the device. That includes things like DMA enable bits.

I think that read everything again from the device can not work. Suppose the following scene: If the device's state contains the change of a series of internal registers (supposing partA+partB; partC+partD), after partA changed, the device's lock is broken. At this point, another access to this device, it will work on partA+partB to determine C+D, but since partB is not correctly updated yet. So C+D may be decided by broken context and be wrong.

Quoted 7 lines
Quoted 3 lines

That's the guest's problem. Note it could have happened even before the change, since the writes to A/B/C/D are unordered wrt the DMA.

Yes, agree, it is the guest's problem. So it means that ready_of(A+B) is not signaled to guest, the guest should not launch operations on
(C+D). Right? But here comes the question, if ready not signaled to
guest, how can guest launch operation on (A+B) again?

It may be evil.

Quoted 3 lines

i.e. although local lock is broken, the (A+B) is still intact when re-acquire local lock. So need not to read everything again from the device. Wrong?

Quoted 7 lines

The device needs to perform according to its specifications. If the specifications allow for this kind of access, we must ensure it works. If they don't, we must ensure something sane happens, instead of a qemu crash or exploit. This means that anything dangerous like pointers must be revalidated. To be on the safe side, I recommend revalidating (or reloading) everything, but it may not be necessary in all cases.

Yeah, catch the two points exactly.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 11 lines

What my suggestion is:
lock();
set_and_test(dev->busy);
if busy
unlock and return;
changing device registers;
do other things including calling to c_p_m_rw() //here,lock broken, but set_and_test() works
clear(dev->busy);
unlock();

Quoted 1 line

So changing device registers is protected, and unbreakable.

But the changes may be legitimate. Maybe you're writing to a completely unrelated register, from a different vcpu, now that write is lost.

Quoted 7 lines
Quoted 2 lines

But I think it will mean more-fine locks for each groups of unrelated register, and accordingly, the busy should be bitmap for each group.

It's possible. Let's defer the discussion until a concrete case is before us. It may be that different devices will want different solutions (though there is value in applying one solution everywhere).

Okay. appreciate for the total detail explanation.

Regards,
pingfan

Quoted 3 lines

--
error compiling committee.c: too many arguments to function

#71 lore raw

PATCH v3 11/11 vcpu: push mmio dispatcher out of big lock

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

---
 kvm-all.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kvm-all.c +3 -0
index 34b02c1..ef7cd5f 100644
@@ -1562,10 +1562,13 @@ int kvm_cpu_exec(CPUArchState *env)
             break;
         case KVM_EXIT_MMIO:
             DPRINTF("handle_mmio\n");
+            qemu_mutex_unlock_iothread();
             cpu_physical_memory_rw(run->mmio.phys_addr,
                                    run->mmio.data,
                                    run->mmio.len,
                                    run->mmio.is_write);
+            qemu_mutex_lock_iothread();
+
             ret = 0;
             break;
         case KVM_EXIT_IRQ_WINDOW_OPEN:
Signature · 1 line
1.7.4.4