push mmio dispatch out of big lock
PATCH v5 1/8 atomic: introduce atomic operations
In reply to Liu Ping FanIf 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
@@ -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 { +volatile 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
PATCH v5 2/8 qom: apply atomic on object's refcount
In reply to Liu Ping Fan--- include/qemu/object.h | 3 ++- qom/object.c | 11 +++++------ 2 files changed, 7 insertions(+), 7 deletions(-)
include/qemu/object.h +2 -1
@@ -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
@@ -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
PATCH v5 4/8 pci: remove pci device from mem view when unplug
In reply to Liu Ping Fan--- 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
@@ -306,7 +306,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
@@ -869,7 +869,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) {
@@ -880,6 +879,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) {
@@ -2105,6 +2115,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
@@ -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
PATCH v5 5/8 memory: introduce local lock for address space
In reply to Liu Ping FanFor those address spaces which want to be able out of big lock, they will be protected by their own local.
--- memory.c | 11 ++++++++++- memory.h | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-)
memory.c +10 -1
@@ -1532,9 +1532,15 @@ void memory_listener_unregister(MemoryListener *listener)QTAILQ_REMOVE (&memory_listeners, listener, link); } -void address_space_init (AddressSpace *as,MemoryRegion *root) +void address_space_init (AddressSpace *as,MemoryRegion *root,bool lock) {memory_region_transaction_begin (); +if (lock) { + as->lock =g_new (QemuMutex,1 ); +qemu_mutex_init (as->lock); + }else { + as->lock =NULL ; + } as->root = root; as->current_map =g_new (FlatView,1 );flatview_init (as->current_map);
@@ -1553,6 +1559,9 @@ void address_space_destroy(AddressSpace *as)QTAILQ_REMOVE (&address_spaces, as, address_spaces_link);address_space_destroy_dispatch (as);flatview_destroy (as->current_map); +if (as->lock) { +g_free (as->lock); + }g_free (as->current_map); }
memory.h +4 -1
@@ -22,6 +22,7 @@#include "cpu-common.h" #include "targphys.h" #include "qemu-queue.h" +#include "qemu-thread.h" #include "iorange.h" #include "ioport.h" #include "int128.h"
@@ -164,6 +165,7 @@ typedef struct AddressSpace AddressSpace;*/ struct AddressSpace {/* All fields are private. */ +QemuMutex *lock;const char *name;MemoryRegion *root;struct FlatView *current_map;
@@ -801,8 +803,9 @@ void mtree_info(fprintf_function mon_printf, void *f);* * @as: an uninitialized #AddressSpace * @root: a #MemoryRegion that routes addesses for the address space +* @lock: if true, the physmap protected by local lock, otherwise big lock */ -void address_space_init (AddressSpace *as,MemoryRegion *root); +void address_space_init (AddressSpace *as,MemoryRegion *root,bool lock);/**
Signature · 1 line
1.7.4.4
PATCH v5 7/8 memory: introduce tls context to record nested dma
In reply to Liu Ping Fan--- cpus.c | 3 ++ exec.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread.h | 8 +++++++ vl.c | 1 + 4 files changed, 70 insertions(+), 0 deletions(-)
cpus.c +3 -0
@@ -733,6 +733,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)qemu_mutex_lock (&qemu_global_mutex);qemu_thread_get_self (cpu->thread); +qemu_thread_init_context (); env->thread_id =qemu_get_thread_id (); cpu_single_env = env;
@@ -774,6 +775,7 @@ static void *qemu_dummy_cpu_thread_fn(void *arg)qemu_mutex_lock_iothread ();qemu_thread_get_self (cpu->thread); +qemu_thread_init_context (); env->thread_id =qemu_get_thread_id ();sigemptyset (&waitset);
@@ -813,6 +815,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)qemu_tcg_init_cpu_signals ();qemu_thread_get_self (cpu->thread); +qemu_thread_init_context ();/* signal CPU creation */ qemu_mutex_lock (&qemu_global_mutex);
exec.c +58 -0
92 lines not on this page · raw message
qemu-thread.h +8 -0
@@ -7,6 +7,11 @@typedef struct QemuMutex QemuMutex ;typedef struct QemuCond QemuCond ;typedef struct QemuThread QemuThread ; +typedef enum { +DISPATCH_INIT =0 , +DISPATCH_MMIO , +DISPATCH_IO , +}DispatchType ;#ifdef _WIN32#include "qemu-thread-win32.h"
@@ -46,4 +51,7 @@ void qemu_thread_get_self(QemuThread *thread);bool qemu_thread_is_self (QemuThread *thread);void qemu_thread_exit (void *retval); +void qemu_thread_init_context (void ); +void qemu_thread_set_dispatch_type (DispatchType type); +void qemu_thread_reset_dispatch_type (void );#endif
vl.c +1 -0
@@ -3439,6 +3439,7 @@ int main(int argc, char **argv, char **envp)add_device_config (DEV_VIRTCON ,"vc:80Cx24C" ); } +qemu_thread_init_context ();socket_init ();if (qemu_opts_foreach (qemu_find_opts ("chardev" ), chardev_init_func,NULL ,1 ) !=0 )
Signature · 1 line
1.7.4.4
PATCH v5 6/8 memory: make mmio dispatch able to be out of biglock
In reply to Liu Ping FanWithout 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.
--- docs/memory.txt | 4 + exec.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++----- hw/pci.c | 3 +- memory-internal.h | 1 + memory.h | 2 + 5 files changed, 162 insertions(+), 18 deletions(-)
docs/memory.txt +4 -0
@@ -170,3 +170,7 @@ various constraints can be supplied to control how these callbacks are called: - .old_portio and .old_mmio can be used to ease porting from code using cpu_register_io_memory() and register_ioport(). They should not be used in new code. + +MMIO regions are provided with ->ref() and ->unref() callbacks; This pair callbacks +are optional. When ref() return non-zero, Both MemoryRegion and its opaque are +safe to use.
exec.c +153 -17
296 lines not on this page · raw message
hw/pci.c +2 -1
@@ -786,7 +786,8 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,get_system_memory (),0 , memory_region_size(get_system_memory()));memory_region_set_enabled (&pci_dev->bus_master_enable_region, false); -address_space_init (&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region); +address_space_init (&pci_dev->bus_master_as, + &pci_dev->bus_master_enable_region, true); pci_dev->dma =g_new (DMAContext,1 );dma_context_init (pci_dev->dma, &pci_dev->bus_master_as,NULL ,NULL ,NULL ); }
memory-internal.h +1 -0
@@ -38,6 +38,7 @@ struct AddressSpaceDispatch {*/ PhysPageEntry phys_map;MemoryListener listener; +AddressSpace *as; };void address_space_init_dispatch (AddressSpace *as);
memory.h +2 -0
@@ -67,6 +67,8 @@ struct MemoryRegionOps {target_phys_addr_t addr, uint64_t data, unsigned size); +bool (*ref)(MemoryRegion *mr); +void (*unref)(MemoryRegion *mr);enum device_endian endianness;/* Guest-visible constraints: */
Signature · 1 line
1.7.4.4
PATCH v5 8/8 vcpu: push mmio dispatcher out of big lock
In reply to Liu Ping Fan--- kvm-all.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
kvm-all.c +4 -0
@@ -1573,10 +1573,14 @@ int kvm_cpu_exec(CPUArchState *env)break ;case KVM_EXIT_MMIO :DPRINTF ("handle_mmio\n" ); +qemu_mutex_unlock_iothread (); +qemu_thread_set_dispatch_type (DISPATCH_MMIO );cpu_physical_memory_rw (run->mmio.phys_addr, run->mmio.data, run->mmio.len, run->mmio.is_write); +qemu_thread_reset_dispatch_type (); +qemu_mutex_lock_iothread (); ret =0 ;break ;case KVM_EXIT_IRQ_WINDOW_OPEN :
Signature · 1 line
1.7.4.4
PATCH v5 3/8 hotplug: introduce qdev_unplug_complete() to remove device from views
In reply to Liu Ping FanWhen 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
@@ -98,6 +98,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. */
@@ -187,6 +195,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
@@ -47,7 +47,7 @@ typedef struct DeviceClass {/* callbacks */ void (*reset)(DeviceState *dev); - +void (*unmap)(DeviceState *dev);/* device state */ const VMStateDescription *vmsd;
@@ -160,6 +160,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
v5 cover push mmio dispatch out of big 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
v3:
http://lists.nongnu.org/archive/html/qemu-devel/2012-09/msg01474.html
v4:
http://lists.gnu.org/archive/html/qemu-devel/2012-10/msg03857.html
changes v4->v5:
Peel away the example of e1000.
Rebase to Avi's patch "Integrate DMA into the memory API"
Liu Ping Fan (8):
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 local lock for address space
memory: make mmio dispatch able to be out of biglock
memory: introduce tls context to record nested dma
vcpu: push mmio dispatcher out of big lock
cpus.c | 3 + docs/memory.txt | 4 + exec.c | 228 +++++++++++++++++++++++++++++++++++++++++++++---- hw/acpi_piix4.c | 2 +- hw/pci.c | 16 +++- hw/pci.h | 1 + hw/qdev.c | 26 ++++++ hw/qdev.h | 3 +- include/qemu/atomic.h | 63 ++++++++++++++ include/qemu/object.h | 3 +- kvm-all.c | 4 + memory-internal.h | 1 + memory.c | 11 ++- memory.h | 7 ++- qemu-thread.h | 8 ++ qom/object.c | 11 +-- vl.c | 1 + 17 files changed, 362 insertions(+), 30 deletions(-) create mode 100644 include/qemu/atomic.h
Signature · 1 line
1.7.4.4
Re: [Qemu-devel] [patch v5 5/8] memory: introduce local lock for address space
In reply to Liu Ping FanQuoted 2 lines · Liu Ping Fan
For those address spaces which want to be able out of big lock, they will be protected by their own local.
Are you sure this patch compiles? It seems to only be changing the prototype and implementation of address_space_init() to take an extra parameter, but not any of the callers...
-- PMM
Re: [Qemu-devel] [patch v5 5/8] memory: introduce local lock for address space
Quoted 8 lines · Peter Maydell
Quoted 2 lines · Liu Ping Fan
For those address spaces which want to be able out of big lock, they will be protected by their own local.
Are you sure this patch compiles? It seems to only be changing the prototype and implementation of
address_space_init()to take an extra parameter, but not any of the callers...
The caller is in the next patch. Need to rearrange.
Thanks,
pingfan
Quoted 2 lines
-- PMM
Re: [Qemu-devel] [patch v5 7/8] memory: introduce tls context to record nested dma
In reply to Liu Ping FanIl 29/10/2012 00:48, Liu Ping Fan ha scritto:
Quoted 47 lines
Signed-off-by Liu Ping Fan <[email protected]>--- cpus.c | 3 ++ exec.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread.h | 8 +++++++ vl.c | 1 + 4 files changed, 70 insertions(+), 0 deletions(-)cpus.c +3
-0@@ -733,6 +733,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)qemu_mutex_lock (&qemu_global_mutex);qemu_thread_get_self (cpu->thread); +qemu_thread_init_context (); env->thread_id =qemu_get_thread_id (); cpu_single_env = env;@@ -774,6 +775,7 @@ static void *qemu_dummy_cpu_thread_fn(void *arg)qemu_mutex_lock_iothread ();qemu_thread_get_self (cpu->thread); +qemu_thread_init_context (); env->thread_id =qemu_get_thread_id ();sigemptyset (&waitset);@@ -813,6 +815,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)qemu_tcg_init_cpu_signals ();qemu_thread_get_self (cpu->thread); +qemu_thread_init_context ();/* signal CPU creation */ qemu_mutex_lock (&qemu_global_mutex);exec.c +3
-0@@ -3449,6 +3449,49 @@ static bool address_space_section_lookup_ref(AddressSpace *as,return safe_ref; } +typedef struct ThreadContext { + DispatchType dispatch_type; + unsigned int mmio_req_pending;
exec.c static bool address_space_section_lookup_ref(AddressSpace *as, · line 3464
Quoted 10 lines
exec.c +10
-0+} ThreadContext; + +static __threadThreadContext *thread_context; + +void qemu_thread_init_context (void ) +{ + thread_context =g_new (ThreadContext,1 ); + thread_context->dispatch_type =DISPATCH_INIT ; + thread_context->mmio_req_pending =0 ; +}
No need for this:
static __thread ThreadContext thread_context = {
.dispatch_type = DISPATCH_INIT,
.mmio_req_pending = 0
};
Paolo
Re: [Qemu-devel] [patch v5 5/8] memory: introduce local lock for address space
In reply to Liu Ping FanQuoted 14 lines · Liu Ping Fan
For those address spaces which want to be able out of big lock, they will be protected by their own local.
Signed-off-by Liu Ping Fan <[email protected]>--- memory.c | 11 ++++++++++- memory.h | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-)memory.c +0
-0
memory.c void memory_listener_unregister(MemoryListener *listener) · line 1535
Quoted 5 lines
memory.c +1
-1@@ -1532,9 +1532,15 @@ void memory_listener_unregister(MemoryListener *listener)QTAILQ_REMOVE (&memory_listeners, listener, link); } -void address_space_init (AddressSpace *as,MemoryRegion *root) +void address_space_init (AddressSpace *as,MemoryRegion *root,bool lock)
Why not always use the lock? Even if the big lock is taken, it doesn't hurt. And eventually all address spaces will be fine-grained.
Quoted 53 lines
memory.c +9
-0{memory_region_transaction_begin (); +if (lock) { + as->lock =g_new (QemuMutex,1 ); +qemu_mutex_init (as->lock); + }else { + as->lock =NULL ; + } as->root = root; as->current_map =g_new (FlatView,1 );flatview_init (as->current_map);@@ -1553,6 +1559,9 @@ void address_space_destroy(AddressSpace *as)QTAILQ_REMOVE (&address_spaces, as, address_spaces_link);address_space_destroy_dispatch (as);flatview_destroy (as->current_map); +if (as->lock) { +g_free (as->lock); + }g_free (as->current_map); }memory.h +4
-1@@ -22,6 +22,7 @@#include "cpu-common.h" #include "targphys.h" #include "qemu-queue.h" +#include "qemu-thread.h" #include "iorange.h" #include "ioport.h" #include "int128.h" @@ -164,6 +165,7 @@ typedef struct AddressSpace AddressSpace;*/ struct AddressSpace {/* All fields are private. */ +QemuMutex *lock;const char *name;MemoryRegion *root;struct FlatView *current_map;@@ -801,8 +803,9 @@ void mtree_info(fprintf_function mon_printf, void *f);* * @as: an uninitialized #AddressSpace * @root: a #MemoryRegion that routes addesses for the address space +* @lock: if true, the physmap protected by local lock, otherwise big lock */ -void address_space_init (AddressSpace *as,MemoryRegion *root); +void address_space_init (AddressSpace *as,MemoryRegion *root,bool lock);/**
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Qemu-devel] [patch v5 6/8] memory: make mmio dispatch able to be out of biglock
In reply to Liu Ping FanQuoted 8 lines · Liu Ping Fan
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 invalidate_and_set_dirty(target_phys_addr_t addr, · line 3404
Quoted 8 lines
exec.c +8
-0+static bool memory_region_section_ref (MemoryRegionSection *mrs) +{ +MemoryRegion *mr; +bool ret = false; + + mr = mrs->mr; +if (mr->ops && mr->ops->ref) { + ret = mr->ops->ref (mr);
I still don't see why ->ref() needs to return something.
Quoted 19 lines
exec.c +12
-1+ } +return ret; +} +while (len >0 ) { page = addr &TARGET_PAGE_MASK ; l = (page +TARGET_PAGE_SIZE ) - addr;if (l > len) l = len; - section =phys_page_find (d, page >>TARGET_PAGE_BITS ); + +if (as->lock) { +qemu_mutex_lock (as->lock); + safe_ref =memory_region_section_lookup_ref (d, page, &obj_mrs); +qemu_mutex_unlock (as->lock); +if (!safe_ref) { +qemu_mutex_lock_iothread (); +qemu_mutex_lock (as->lock);
exec.c void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf, · line 3485
Quoted 10 lines
exec.c +10
-0+/* when 2nd try, mem map can change, need to judge it again */ + safe_ref =memory_region_section_lookup_ref (d, page, &obj_mrs); +qemu_mutex_unlock (as->lock); +if (safe_ref) { +qemu_mutex_unlock_iothread (); + } + } + }else { +/* Caller hold the big lock */ +memory_region_section_lookup_ref (d, page, &obj_mrs);
It's not a property of the address space, it's a property of the caller.
Quoted 5 lines
exec.c +2
-0+ } + section = &obj_mrs;if (is_write) {if (!memory_region_is_ram (section->mr)) {
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Qemu-devel] [patch v5 5/8] memory: introduce local lock for address space
In reply to Avi KivityQuoted 18 lines · Avi Kivity
Quoted 17 lines · Liu Ping Fan
For those address spaces which want to be able out of big lock, they will be protected by their own local.
Signed-off-by Liu Ping Fan <[email protected]>--- memory.c | 11 ++++++++++- memory.h | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-)memory.c +0
-0@@ -1532,9 +1532,15 @@ void memory_listener_unregister(MemoryListener *listener)QTAILQ_REMOVE (&memory_listeners, listener, link); }
Quoted 7 lines
memory.c void memory_listener_unregister(MemoryListener *listener) · line 1535
Quoted 2 lines
memory.c +1
-1-void address_space_init (AddressSpace *as,MemoryRegion *root) +void address_space_init (AddressSpace *as,MemoryRegion *root,bool lock)Why not always use the lock? Even if the big lock is taken, it doesn't hurt. And eventually all address spaces will be fine-grained.
I had thought only mmio is out of big lock's protection. While others address space will take extra expense. So leave them until they are ready to be out of big lock.
Quoted 58 lines
Quoted 43 lines
memory.c +9
-0{memory_region_transaction_begin (); +if (lock) { + as->lock =g_new (QemuMutex,1 ); +qemu_mutex_init (as->lock); + }else { + as->lock =NULL ; + } as->root = root; as->current_map =g_new (FlatView,1 );flatview_init (as->current_map);@@ -1553,6 +1559,9 @@ void address_space_destroy(AddressSpace *as)QTAILQ_REMOVE (&address_spaces, as, address_spaces_link);address_space_destroy_dispatch (as);flatview_destroy (as->current_map); +if (as->lock) { +g_free (as->lock); + }g_free (as->current_map); }memory.h +2
-0@@ -22,6 +22,7 @@#include "cpu-common.h" #include "targphys.h" #include "qemu-queue.h" +#include "qemu-thread.h" #include "iorange.h" #include "ioport.h" #include "int128.h" @@ -164,6 +165,7 @@ typedef struct AddressSpace AddressSpace;*/ struct AddressSpace {/* All fields are private. */ +QemuMutex *lock;const char *name;MemoryRegion *root;struct FlatView *current_map;memory.h void mtree_info(fprintf_function mon_printf, void *f); · line 811
Quoted 10 lines
memory.h +2
-1@@ -801,8 +803,9 @@ void mtree_info(fprintf_function mon_printf, void *f);* * @as: an uninitialized #AddressSpace * @root: a #MemoryRegion that routes addesses for the address space +* @lock: if true, the physmap protected by local lock, otherwise big lock */ -void address_space_init (AddressSpace *as,MemoryRegion *root); +void address_space_init (AddressSpace *as,MemoryRegion *root,bool lock);/** --
error compilingcommittee.c: too many arguments to function
Re: [Qemu-devel] [patch v5 6/8] memory: make mmio dispatch able to be out of biglock
In reply to Avi KivityQuoted 9 lines · Avi Kivity
Quoted 7 lines · Liu Ping Fan
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.
Quoted 11 lines
exec.c static void invalidate_and_set_dirty(target_phys_addr_t addr, · line 3404
Quoted 8 lines
exec.c +8
-0+static bool memory_region_section_ref (MemoryRegionSection *mrs) +{ +MemoryRegion *mr; +bool ret = false; + + mr = mrs->mr; +if (mr->ops && mr->ops->ref) { + ret = mr->ops->ref (mr);I still don't see why ->ref() needs to return something.
My original design use it to trace refcnt on object, but now it abandons. Will drop it.
Quoted 23 lines
Quoted 23 lines
exec.c +16
-1+ } +return ret; +} +while (len >0 ) { page = addr &TARGET_PAGE_MASK ; l = (page +TARGET_PAGE_SIZE ) - addr;if (l > len) l = len; - section =phys_page_find (d, page >>TARGET_PAGE_BITS ); + +if (as->lock) { +qemu_mutex_lock (as->lock); + safe_ref =memory_region_section_lookup_ref (d, page, &obj_mrs); +qemu_mutex_unlock (as->lock); +if (!safe_ref) { +qemu_mutex_lock_iothread (); +qemu_mutex_lock (as->lock); +/* when 2nd try, mem map can change, need to judge it again */ + safe_ref =memory_region_section_lookup_ref (d, page, &obj_mrs); +qemu_mutex_unlock (as->lock); +if (safe_ref) {
Quoted 9 lines
exec.c void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf, · line 3485
Quoted 6 lines
exec.c +6
-0+qemu_mutex_unlock_iothread (); + } + } + }else { +/* Caller hold the big lock */ +memory_region_section_lookup_ref (d, page, &obj_mrs);It's not a property of the address space, it's a property of the caller.
Sorry, what is your meaning?
Quoted 10 lines
exec.c void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf, · line 3490
Quoted 5 lines
exec.c +2
-0+ } + section = &obj_mrs;if (is_write) {if (!memory_region_is_ram (section->mr)) {--
error compilingcommittee.c: too many arguments to function
Re: [Qemu-devel] [patch v5 6/8] memory: make mmio dispatch able to be out of biglock
Quoted 55 lines · liu ping fan
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Liu Ping Fan
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.
Quoted 11 lines
exec.c static void invalidate_and_set_dirty(target_phys_addr_t addr, · line 3404
Quoted 8 lines
exec.c +8
-0+static bool memory_region_section_ref (MemoryRegionSection *mrs) +{ +MemoryRegion *mr; +bool ret = false; + + mr = mrs->mr; +if (mr->ops && mr->ops->ref) { + ret = mr->ops->ref (mr);I still don't see why ->ref() needs to return something.
My original design use it to trace refcnt on object, but now it abandons. Will drop it.
Quoted 23 lines
Quoted 23 lines
exec.c +16
-1+ } +return ret; +} +while (len >0 ) { page = addr &TARGET_PAGE_MASK ; l = (page +TARGET_PAGE_SIZE ) - addr;if (l > len) l = len; - section =phys_page_find (d, page >>TARGET_PAGE_BITS ); + +if (as->lock) { +qemu_mutex_lock (as->lock); + safe_ref =memory_region_section_lookup_ref (d, page, &obj_mrs); +qemu_mutex_unlock (as->lock); +if (!safe_ref) { +qemu_mutex_lock_iothread (); +qemu_mutex_lock (as->lock); +/* when 2nd try, mem map can change, need to judge it again */ + safe_ref =memory_region_section_lookup_ref (d, page, &obj_mrs); +qemu_mutex_unlock (as->lock); +if (safe_ref) {Quoted 9 lines
exec.c void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf, · line 3485
Quoted 6 lines
exec.c +6
-0+qemu_mutex_unlock_iothread (); + } + } + }else { +/* Caller hold the big lock */ +memory_region_section_lookup_ref (d, page, &obj_mrs);It's not a property of the address space, it's a property of the caller.
Quoted 2 lines
Sorry, what is your meaning?
ping?
Quoted 10 lines
Quoted 10 lines
exec.c void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf, · line 3490
Quoted 5 lines
exec.c +2
-0+ } + section = &obj_mrs;if (is_write) {if (!memory_region_is_ram (section->mr)) {--
error compilingcommittee.c: too many arguments to function
Re: [Qemu-devel] [patch v5 5/8] memory: introduce local lock for address space
In reply to liu ping fanQuoted 26 lines · liu ping fan
Quoted 18 lines · Avi Kivity
Quoted 17 lines · Liu Ping Fan
For those address spaces which want to be able out of big lock, they will be protected by their own local.
Signed-off-by Liu Ping Fan <[email protected]>--- memory.c | 11 ++++++++++- memory.h | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-)memory.c +0
-0@@ -1532,9 +1532,15 @@ void memory_listener_unregister(MemoryListener *listener)QTAILQ_REMOVE (&memory_listeners, listener, link); }Quoted 7 lines
memory.c void memory_listener_unregister(MemoryListener *listener) · line 1535
Quoted 2 lines
memory.c +1
-1-void address_space_init (AddressSpace *as,MemoryRegion *root) +void address_space_init (AddressSpace *as,MemoryRegion *root,bool lock)Why not always use the lock? Even if the big lock is taken, it doesn't hurt. And eventually all address spaces will be fine-grained.
Quoted 3 lines
I had thought only mmio is out of big lock's protection. While others address space will take extra expense. So leave them until they are ready to be out of big lock.
The other address spaces are pio (which also needs fine-grained locking) and the dma address spaces (which are like address_space_memory, except they are accessed via DMA instead of from the vcpu).
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Qemu-devel] [patch v5 6/8] memory: make mmio dispatch able to be out of biglock
In reply to liu ping fanQuoted 25 lines · liu ping fan
Quoted 25 lines
Quoted 24 lines
while (len > 0) { page = addr & TARGET_PAGE_MASK; l = (page + TARGET_PAGE_SIZE) - addr; if (l > len) l = len;exec.c +18
-1- section =phys_page_find (d, page >>TARGET_PAGE_BITS ); + +if (as->lock) { +qemu_mutex_lock (as->lock); + safe_ref =memory_region_section_lookup_ref (d, page, &obj_mrs); +qemu_mutex_unlock (as->lock); +if (!safe_ref) { +qemu_mutex_lock_iothread (); +qemu_mutex_lock (as->lock); +/* when 2nd try, mem map can change, need to judge it again */ + safe_ref =memory_region_section_lookup_ref (d, page, &obj_mrs); +qemu_mutex_unlock (as->lock); +if (safe_ref) { +qemu_mutex_unlock_iothread (); + } + } + }else { +/* Caller hold the big lock */ +memory_region_section_lookup_ref (d, page, &obj_mrs);
Quoted 3 lines
Quoted 2 lines
It's not a property of the address space, it's a property of the caller.
Sorry, what is your meaning?
Whether the caller holds the big lock or not is not known by the address space. It is only known by the caller itself.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Qemu-devel] [patch v5 5/8] memory: introduce local lock for address space
In reply to Avi KivityQuoted 27 lines · Avi Kivity
Quoted 26 lines · liu ping fan
Quoted 25 lines · Avi Kivity
Quoted 14 lines · Liu Ping Fan
For those address spaces which want to be able out of big lock, they will be protected by their own local.
Signed-off-by Liu Ping Fan <[email protected]>--- memory.c | 11 ++++++++++- memory.h | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-)memory.c +0
-0memory.c void memory_listener_unregister(MemoryListener *listener) · line 1535
Quoted 5 lines
memory.c +1
-1@@ -1532,9 +1532,15 @@ void memory_listener_unregister(MemoryListener *listener)QTAILQ_REMOVE (&memory_listeners, listener, link); } -void address_space_init (AddressSpace *as,MemoryRegion *root) +void address_space_init (AddressSpace *as,MemoryRegion *root,bool lock)Why not always use the lock? Even if the big lock is taken, it doesn't hurt. And eventually all address spaces will be fine-grained.
Quoted 7 lines
Quoted 3 lines
I had thought only mmio is out of big lock's protection. While others address space will take extra expense. So leave them until they are ready to be out of big lock.
The other address spaces are pio (which also needs fine-grained locking) and the dma address spaces (which are like
address_space_memory, except they are accessed via DMA instead of from the vcpu).
The problem is with memory regions that don't do fine-grained locking yet, thus don't provide ref/unref. Then we fall back to taking BQL across dispatch. If the dispatch caller already holds the BQL, we will bail out.
As I understand the series, as->lock == NULL means that we will never take any lock during dispatch as the caller is not yet ready for fine-grained locking. This prevents the problem - for PIO at least. But this series should break TCG as it calls into MMIO dispatch from the VCPU while holding the BQL.
Jan
Re: [Qemu-devel] [patch v5 5/8] memory: introduce local lock for address space
Quoted 32 lines · Jan Kiszka
Quoted 31 lines · Avi Kivity
Quoted 29 lines · liu ping fan
Quoted 18 lines · Avi Kivity
Quoted 17 lines · Liu Ping Fan
For those address spaces which want to be able out of big lock, they will be protected by their own local.
Signed-off-by Liu Ping Fan <[email protected]>--- memory.c | 11 ++++++++++- memory.h | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-)memory.c +0
-0@@ -1532,9 +1532,15 @@ void memory_listener_unregister(MemoryListener *listener)QTAILQ_REMOVE (&memory_listeners, listener, link); }Quoted 7 lines
memory.c void memory_listener_unregister(MemoryListener *listener) · line 1535
Quoted 2 lines
memory.c +1
-1-void address_space_init (AddressSpace *as,MemoryRegion *root) +void address_space_init (AddressSpace *as,MemoryRegion *root,bool lock)Why not always use the lock? Even if the big lock is taken, it doesn't hurt. And eventually all address spaces will be fine-grained.
I had thought only mmio is out of big lock's protection. While others address space will take extra expense. So leave them until they are ready to be out of big lock.
Quoted 9 lines
Quoted 3 lines
The other address spaces are pio (which also needs fine-grained locking) and the dma address spaces (which are like
address_space_memory, except they are accessed via DMA instead of from the vcpu).The problem is with memory regions that don't do fine-grained locking yet, thus don't provide ref/unref. Then we fall back to taking BQL across dispatch. If the dispatch caller already holds the BQL, we will bail out.
Yes, these asymmetrice callers are bothering. Currently, I just make exceptions for them, and would like to make the biglock recursive. But this motivation may make bug not easy to find.
Quoted 6 lines
As I understand the series,
as->lock== NULL means that we will never take any lock during dispatch as the caller is not yet ready for fine-grained locking. This prevents the problem - for PIO at least. But this series should break TCG as it calls into MMIO dispatch from the VCPU while holding the BQL.
What about add another condition "dispatch_type == DISPATCH_MMIO" to tell this situation.
Regards,
Pingfan
Quoted 3 lines
Jan
Re: [Qemu-devel] [patch v5 5/8] memory: introduce local lock for address space
Quoted 46 lines · liu ping fan
Quoted 32 lines · Jan Kiszka
Quoted 31 lines · Avi Kivity
Quoted 29 lines · liu ping fan
Quoted 18 lines · Avi Kivity
Quoted 17 lines · Liu Ping Fan
For those address spaces which want to be able out of big lock, they will be protected by their own local.
Signed-off-by Liu Ping Fan <[email protected]>--- memory.c | 11 ++++++++++- memory.h | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-)memory.c +0
-0@@ -1532,9 +1532,15 @@ void memory_listener_unregister(MemoryListener *listener)QTAILQ_REMOVE (&memory_listeners, listener, link); }Quoted 7 lines
memory.c void memory_listener_unregister(MemoryListener *listener) · line 1535
Quoted 2 lines
memory.c +1
-1-void address_space_init (AddressSpace *as,MemoryRegion *root) +void address_space_init (AddressSpace *as,MemoryRegion *root,bool lock)Why not always use the lock? Even if the big lock is taken, it doesn't hurt. And eventually all address spaces will be fine-grained.
I had thought only mmio is out of big lock's protection. While others address space will take extra expense. So leave them until they are ready to be out of big lock.
Quoted 9 lines
Quoted 3 lines
The other address spaces are pio (which also needs fine-grained locking) and the dma address spaces (which are like
address_space_memory, except they are accessed via DMA instead of from the vcpu).The problem is with memory regions that don't do fine-grained locking yet, thus don't provide ref/unref. Then we fall back to taking BQL across dispatch. If the dispatch caller already holds the BQL, we will bail out.
Yes, these asymmetrice callers are bothering. Currently, I just make exceptions for them, and would like to make the biglock recursive. But this motivation may make bug not easy to find.
Quoted 8 lines
Quoted 6 lines
As I understand the series,
as->lock== NULL means that we will never take any lock during dispatch as the caller is not yet ready for fine-grained locking. This prevents the problem - for PIO at least. But this series should break TCG as it calls into MMIO dispatch from the VCPU while holding the BQL.What about add another condition "
dispatch_type==DISPATCH_MMIO" to tell this situation.
An alternative pattern that we will also use for core services is to provide an additional entry point, one that indicates that the caller doesn't hold the BQL. Then we will gradually move things over until the existing entry point is obsolete.
Jan
Re: [Qemu-devel] [patch v5 7/8] memory: introduce tls context to record nested dma
In reply to Liu Ping FanQuoted 167 lines · Liu Ping Fan
Signed-off-by Liu Ping Fan <[email protected]>--- cpus.c | 3 ++ exec.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread.h | 8 +++++++ vl.c | 1 + 4 files changed, 70 insertions(+), 0 deletions(-)cpus.c +3
-0@@ -733,6 +733,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)qemu_mutex_lock (&qemu_global_mutex);qemu_thread_get_self (cpu->thread); +qemu_thread_init_context (); env->thread_id =qemu_get_thread_id (); cpu_single_env = env;@@ -774,6 +775,7 @@ static void *qemu_dummy_cpu_thread_fn(void *arg)qemu_mutex_lock_iothread ();qemu_thread_get_self (cpu->thread); +qemu_thread_init_context (); env->thread_id =qemu_get_thread_id ();sigemptyset (&waitset);@@ -813,6 +815,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)qemu_tcg_init_cpu_signals ();qemu_thread_get_self (cpu->thread); +qemu_thread_init_context ();/* signal CPU creation */ qemu_mutex_lock (&qemu_global_mutex);exec.c +58
-0@@ -3449,6 +3449,49 @@ static bool address_space_section_lookup_ref(AddressSpace *as,return safe_ref; } +typedef struct ThreadContext { +DispatchType dispatch_type; +unsigned int mmio_req_pending; +}ThreadContext ; + +static __threadThreadContext *thread_context; + +void qemu_thread_init_context (void ) +{ + thread_context =g_new (ThreadContext,1 ); + thread_context->dispatch_type =DISPATCH_INIT ; + thread_context->mmio_req_pending =0 ; +} + +void qemu_thread_set_dispatch_type (DispatchType type) +{ + thread_context->dispatch_type = type; +} + +void qemu_thread_reset_dispatch_type (void ) +{ + thread_context->dispatch_type =DISPATCH_INIT ; +} + +static bool address_space_inc_req_pending (void ) +{ +bool nested = false; + +/* currently, only mmio out of big lock, and need this to avoid dead lock */ +if (thread_context->dispatch_type ==DISPATCH_MMIO ) { + nested = ++thread_context->mmio_req_pending >1 ? true : false; + } + +return nested; +} + +static void address_space_dec_req_pending (void ) +{ +if (thread_context->dispatch_type ==DISPATCH_MMIO ) { + thread_context->mmio_req_pending--; + } +} +void address_space_rw (AddressSpace *as,target_phys_addr_t addr,uint8_t *buf,int len,bool is_write) {@@ -3459,6 +3502,7 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf,target_phys_addr_t page;bool safe_ref = false;MemoryRegionSection *section, obj_mrs; +bool nested_dma = false;while (len >0 ) { page = addr &TARGET_PAGE_MASK ;@@ -3485,10 +3529,17 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf,memory_region_section_lookup_ref (d, page, &obj_mrs); } section = &obj_mrs; + nested_dma =address_space_inc_req_pending ();if (is_write) {if (!memory_region_is_ram (section->mr)) {target_phys_addr_t addr1; + +/* To fix, will filter iommu case */ +if (nested_dma) { +fprintf (stderr,"can not support nested DMA" ); +abort (); + } addr1 =memory_region_section_addr (section, addr);/* XXX: could force cpu_single_env to NULL to avoid potential bugs */ @@ -3522,6 +3573,12 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf,if (!(memory_region_is_ram (section->mr) ||memory_region_is_romd (section->mr))) {target_phys_addr_t addr1; + +/* To fix, will filter iommu case */ +if (nested_dma) { +fprintf (stderr,"can not support nested DMA" ); +abort (); + }/* I/O case */ addr1 =memory_region_section_addr (section, addr);if (l >=4 && ((addr1 &3 ) ==0 )) {@@ -3549,6 +3606,7 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf,qemu_put_ram_ptr (ptr); } } +address_space_dec_req_pending ();memory_region_section_unref (&obj_mrs); len -= l; buf += l;qemu-thread.h +8
-0@@ -7,6 +7,11 @@typedef struct QemuMutex QemuMutex ;typedef struct QemuCond QemuCond ;typedef struct QemuThread QemuThread ; +typedef enum { +DISPATCH_INIT =0 , +DISPATCH_MMIO , +DISPATCH_IO , +}DispatchType ;#ifdef _WIN32#include "qemu-thread-win32.h" @@ -46,4 +51,7 @@ void qemu_thread_get_self(QemuThread *thread);bool qemu_thread_is_self (QemuThread *thread);void qemu_thread_exit (void *retval); +void qemu_thread_init_context (void ); +void qemu_thread_set_dispatch_type (DispatchType type); +void qemu_thread_reset_dispatch_type (void );#endif vl.c +0
-0
vl.c int main(int argc, char **argv, char **envp) · line 3445
Quoted 7 lines
vl.c +1
-0@@ -3439,6 +3439,7 @@ int main(int argc, char **argv, char **envp)add_device_config (DEV_VIRTCON ,"vc:80Cx24C" ); } +qemu_thread_init_context ();socket_init ();if (qemu_opts_foreach (qemu_find_opts ("chardev" ), chardev_init_func,NULL ,1 ) !=0 )
In fact, this is not targeting nested DMA but nesting of memory region callbacks, and there potential deadlock issue. Triggering a DMA with RAM as target from within an MMIO (or later PIO) handler is perfectly fine and not rejected here. You should clarify the scope and purpose of this patch in its changelog.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE Corporate Competence Center Embedded Linux
liu ping fan
AppliedRe: [Qemu-devel] [patch v5 7/8] memory: introduce tls context to record nested dma
Quoted 173 lines · Jan Kiszka
Quoted 172 lines · Liu Ping Fan
Signed-off-by Liu Ping Fan <[email protected]>--- cpus.c | 3 ++ exec.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread.h | 8 +++++++ vl.c | 1 + 4 files changed, 70 insertions(+), 0 deletions(-)cpus.c +3
-0@@ -733,6 +733,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)qemu_mutex_lock (&qemu_global_mutex);qemu_thread_get_self (cpu->thread); +qemu_thread_init_context (); env->thread_id =qemu_get_thread_id (); cpu_single_env = env;@@ -774,6 +775,7 @@ static void *qemu_dummy_cpu_thread_fn(void *arg)qemu_mutex_lock_iothread ();qemu_thread_get_self (cpu->thread); +qemu_thread_init_context (); env->thread_id =qemu_get_thread_id ();sigemptyset (&waitset);@@ -813,6 +815,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)qemu_tcg_init_cpu_signals ();qemu_thread_get_self (cpu->thread); +qemu_thread_init_context ();/* signal CPU creation */ qemu_mutex_lock (&qemu_global_mutex);exec.c +58
-0@@ -3449,6 +3449,49 @@ static bool address_space_section_lookup_ref(AddressSpace *as,return safe_ref; } +typedef struct ThreadContext { +DispatchType dispatch_type; +unsigned int mmio_req_pending; +}ThreadContext ; + +static __threadThreadContext *thread_context; + +void qemu_thread_init_context (void ) +{ + thread_context =g_new (ThreadContext,1 ); + thread_context->dispatch_type =DISPATCH_INIT ; + thread_context->mmio_req_pending =0 ; +} + +void qemu_thread_set_dispatch_type (DispatchType type) +{ + thread_context->dispatch_type = type; +} + +void qemu_thread_reset_dispatch_type (void ) +{ + thread_context->dispatch_type =DISPATCH_INIT ; +} + +static bool address_space_inc_req_pending (void ) +{ +bool nested = false; + +/* currently, only mmio out of big lock, and need this to avoid dead lock */ +if (thread_context->dispatch_type ==DISPATCH_MMIO ) { + nested = ++thread_context->mmio_req_pending >1 ? true : false; + } + +return nested; +} + +static void address_space_dec_req_pending (void ) +{ +if (thread_context->dispatch_type ==DISPATCH_MMIO ) { + thread_context->mmio_req_pending--; + } +} +void address_space_rw (AddressSpace *as,target_phys_addr_t addr,uint8_t *buf,int len,bool is_write) {@@ -3459,6 +3502,7 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf,target_phys_addr_t page;bool safe_ref = false;MemoryRegionSection *section, obj_mrs; +bool nested_dma = false;while (len >0 ) { page = addr &TARGET_PAGE_MASK ;@@ -3485,10 +3529,17 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf,memory_region_section_lookup_ref (d, page, &obj_mrs); } section = &obj_mrs; + nested_dma =address_space_inc_req_pending ();if (is_write) {if (!memory_region_is_ram (section->mr)) {target_phys_addr_t addr1; + +/* To fix, will filter iommu case */ +if (nested_dma) { +fprintf (stderr,"can not support nested DMA" ); +abort (); + } addr1 =memory_region_section_addr (section, addr);/* XXX: could force cpu_single_env to NULL to avoid potential bugs */ @@ -3522,6 +3573,12 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf,if (!(memory_region_is_ram (section->mr) ||memory_region_is_romd (section->mr))) {target_phys_addr_t addr1; + +/* To fix, will filter iommu case */ +if (nested_dma) { +fprintf (stderr,"can not support nested DMA" ); +abort (); + }/* I/O case */ addr1 =memory_region_section_addr (section, addr);if (l >=4 && ((addr1 &3 ) ==0 )) {@@ -3549,6 +3606,7 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf,qemu_put_ram_ptr (ptr); } } +address_space_dec_req_pending ();memory_region_section_unref (&obj_mrs); len -= l; buf += l;qemu-thread.h +8
-0@@ -7,6 +7,11 @@typedef struct QemuMutex QemuMutex ;typedef struct QemuCond QemuCond ;typedef struct QemuThread QemuThread ; +typedef enum { +DISPATCH_INIT =0 , +DISPATCH_MMIO , +DISPATCH_IO , +}DispatchType ;#ifdef _WIN32#include "qemu-thread-win32.h" @@ -46,4 +51,7 @@ void qemu_thread_get_self(QemuThread *thread);bool qemu_thread_is_self (QemuThread *thread);void qemu_thread_exit (void *retval); +void qemu_thread_init_context (void ); +void qemu_thread_set_dispatch_type (DispatchType type); +void qemu_thread_reset_dispatch_type (void );#endif vl.c +1
-0@@ -3439,6 +3439,7 @@ int main(int argc, char **argv, char **envp)add_device_config (DEV_VIRTCON ,"vc:80Cx24C" ); } +qemu_thread_init_context ();socket_init ();
Quoted 9 lines
vl.c int main(int argc, char **argv, char **envp) · line 3445
Quoted 2 lines
vl.c +0
-0if (qemu_opts_foreach (qemu_find_opts ("chardev" ), chardev_init_func,NULL ,1 ) !=0 )In fact, this is not targeting nested DMA but nesting of memory region callbacks, and there potential deadlock issue. Triggering a DMA with RAM as target from within an MMIO (or later PIO) handler is perfectly fine and not rejected here. You should clarify the scope and purpose of this patch in its changelog.
Applied, thanks.
Quoted 6 lines
Jan
--
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE Corporate Competence Center Embedded Linux
liu ping fan
AppliedRe: [Qemu-devel] [patch v5 7/8] memory: introduce tls context to record nested dma
In reply to Paolo BonziniQuoted 61 lines · Paolo Bonzini
Il 29/10/2012 00:48, Liu Ping Fan ha scritto:
Quoted 47 lines
Signed-off-by Liu Ping Fan <[email protected]>--- cpus.c | 3 ++ exec.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread.h | 8 +++++++ vl.c | 1 + 4 files changed, 70 insertions(+), 0 deletions(-)cpus.c +3
-0@@ -733,6 +733,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)qemu_mutex_lock (&qemu_global_mutex);qemu_thread_get_self (cpu->thread); +qemu_thread_init_context (); env->thread_id =qemu_get_thread_id (); cpu_single_env = env;@@ -774,6 +775,7 @@ static void *qemu_dummy_cpu_thread_fn(void *arg)qemu_mutex_lock_iothread ();qemu_thread_get_self (cpu->thread); +qemu_thread_init_context (); env->thread_id =qemu_get_thread_id ();sigemptyset (&waitset);@@ -813,6 +815,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)qemu_tcg_init_cpu_signals ();qemu_thread_get_self (cpu->thread); +qemu_thread_init_context ();/* signal CPU creation */ qemu_mutex_lock (&qemu_global_mutex);exec.c +3
-0@@ -3449,6 +3449,49 @@ static bool address_space_section_lookup_ref(AddressSpace *as,return safe_ref; } +typedef struct ThreadContext { + DispatchType dispatch_type; + unsigned int mmio_req_pending;exec.c static bool address_space_section_lookup_ref(AddressSpace *as, · line 3464
Quoted 10 lines
exec.c +10
-0+} ThreadContext; + +static __threadThreadContext *thread_context; + +void qemu_thread_init_context (void ) +{ + thread_context =g_new (ThreadContext,1 ); + thread_context->dispatch_type =DISPATCH_INIT ; + thread_context->mmio_req_pending =0 ; +}No need for this:
Quoted 5 lines
static
__threadThreadContextthread_context= {
.dispatch_type =DISPATCH_INIT,
.mmio_req_pending = 0
};
applied, thanks.
Quoted 3 lines
Paolo
Re: [Qemu-devel] [patch v5 5/8] memory: introduce local lock for address space
In reply to Jan KiszkaQuoted 7 lines · Jan Kiszka
Quoted 7 lines
Quoted 7 lines
As I understand the series,
as->lock== NULL means that we will never take any lock during dispatch as the caller is not yet ready for fine-grained locking. This prevents the problem - for PIO at least. But this series should break TCG as it calls into MMIO dispatch from the VCPU while holding the BQL.
Quoted 8 lines
Quoted 2 lines
What about add another condition "
dispatch_type==DISPATCH_MMIO" to tell this situation.An alternative pattern that we will also use for core services is to provide an additional entry point, one that indicates that the caller doesn't hold the BQL. Then we will gradually move things over until the existing entry point is obsolete.
I like it (or rather, least dislike it). So we'd have address_space_rw() and address_space_rw_unlocked() (or maybe, address_space_rw() and address_space_rw_locked(), to indicate the preferred way of doing things).
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.