diff mbox

[30/38] ivshmem: Simplify memory regions for BAR 2 (shared memory)

Message ID 1456771254-17511-31-git-send-email-armbru@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Markus Armbruster Feb. 29, 2016, 6:40 p.m. UTC
ivshmem_realize() puts the shared memory region in a container region.
Used to be necessary to permit delayed mapping of the shared memory.
Now we don't do that anymore, the container is redundant.  Drop it.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 hw/misc/ivshmem.c | 44 +++++++++++++++-----------------------------
 1 file changed, 15 insertions(+), 29 deletions(-)

Comments

Paolo Bonzini March 1, 2016, 11:42 a.m. UTC | #1
On 29/02/2016 19:40, Markus Armbruster wrote:
> ivshmem_realize() puts the shared memory region in a container region.
> Used to be necessary to permit delayed mapping of the shared memory.
> Now we don't do that anymore, the container is redundant.  Drop it.

Can you explain why we don't do that anymore to someone who hasn't read
patches 4 to 28? :-)  Is it patch 23?

Paolo
Paolo Bonzini March 1, 2016, 11:46 a.m. UTC | #2
On 29/02/2016 19:40, Markus Armbruster wrote:
> -    memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
> +    s->ivshmem_bar2 = g_new(MemoryRegion, 1);
> +    memory_region_init_ram_ptr(s->ivshmem_bar2, OBJECT(s),
>                                 "ivshmem.bar2", s->ivshmem_size, ptr);
> -    qemu_set_ram_fd(s->ivshmem.ram_addr, fd);
> -    vmstate_register_ram(&s->ivshmem, DEVICE(s));
> -    memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
> +    qemu_set_ram_fd(s->ivshmem_bar2->ram_addr, fd);

This is missing an instance_finalize callback to do

    if (s->ivshmem_bar2) {
        object_unparent(s->ivshmem_bar2);
        g_free(s->ivshmem_bar2);
    }

or, alternatively just use a flag (e.g. s->bar2_mapped) and allocate it
directly in the IVShmemState struct.

Paolo
Markus Armbruster March 1, 2016, 12:14 p.m. UTC | #3
Paolo Bonzini <pbonzini@redhat.com> writes:

> On 29/02/2016 19:40, Markus Armbruster wrote:
>> ivshmem_realize() puts the shared memory region in a container region.
>> Used to be necessary to permit delayed mapping of the shared memory.
>> Now we don't do that anymore, the container is redundant.  Drop it.
>
> Can you explain why we don't do that anymore to someone who hasn't read
> patches 4 to 28? :-)  Is it patch 23?

Yes, but you also need 24 to complete the job.

Commit message could perhaps explain it like this:

    ivshmem_realize() puts the shared memory region in a container
    region.  Used to be necessary to permit delayed mapping of the
    shared memory.  However, we recently moved to synchronous mapping,
    in "ivshmem: Receive shared memory synchronously in realize()" and
    the commit following it.  The container is redundant since then.
    Drop it.

Better?
Paolo Bonzini March 1, 2016, 12:17 p.m. UTC | #4
On 01/03/2016 13:14, Markus Armbruster wrote:
>> > Can you explain why we don't do that anymore to someone who hasn't read
>> > patches 4 to 28? :-)  Is it patch 23?
> Yes, but you also need 24 to complete the job.
> 
> Commit message could perhaps explain it like this:
> 
>     ivshmem_realize() puts the shared memory region in a container
>     region.  Used to be necessary to permit delayed mapping of the
>     shared memory.  However, we recently moved to synchronous mapping,
>     in "ivshmem: Receive shared memory synchronously in realize()" and
>     the commit following it.  The container is redundant since then.
>     Drop it.
> 
> Better?

Yes, thanks!

Paolo
Markus Armbruster March 1, 2016, 2:06 p.m. UTC | #5
Paolo Bonzini <pbonzini@redhat.com> writes:

> On 29/02/2016 19:40, Markus Armbruster wrote:
>> -    memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
>> +    s->ivshmem_bar2 = g_new(MemoryRegion, 1);
>> +    memory_region_init_ram_ptr(s->ivshmem_bar2, OBJECT(s),
>>                                 "ivshmem.bar2", s->ivshmem_size, ptr);
>> -    qemu_set_ram_fd(s->ivshmem.ram_addr, fd);
>> -    vmstate_register_ram(&s->ivshmem, DEVICE(s));
>> -    memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
>> +    qemu_set_ram_fd(s->ivshmem_bar2->ram_addr, fd);
>
> This is missing an instance_finalize callback to do
>
>     if (s->ivshmem_bar2) {
>         object_unparent(s->ivshmem_bar2);
>         g_free(s->ivshmem_bar2);
>     }

Since it's allocated within ivshmem_realize(), I guess I could free it
in ivshmem_exit().

> or, alternatively just use a flag (e.g. s->bar2_mapped) and allocate it
> directly in the IVShmemState struct.

I'll see what comes out nicer.  Thanks!
Paolo Bonzini March 1, 2016, 3:15 p.m. UTC | #6
On 01/03/2016 15:06, Markus Armbruster wrote:
> Paolo Bonzini <pbonzini@redhat.com> writes:
> 
>> On 29/02/2016 19:40, Markus Armbruster wrote:
>>> -    memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
>>> +    s->ivshmem_bar2 = g_new(MemoryRegion, 1);
>>> +    memory_region_init_ram_ptr(s->ivshmem_bar2, OBJECT(s),
>>>                                 "ivshmem.bar2", s->ivshmem_size, ptr);
>>> -    qemu_set_ram_fd(s->ivshmem.ram_addr, fd);
>>> -    vmstate_register_ram(&s->ivshmem, DEVICE(s));
>>> -    memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
>>> +    qemu_set_ram_fd(s->ivshmem_bar2->ram_addr, fd);
>>
>> This is missing an instance_finalize callback to do
>>
>>     if (s->ivshmem_bar2) {
>>         object_unparent(s->ivshmem_bar2);
>>         g_free(s->ivshmem_bar2);
>>     }
> 
> Since it's allocated within ivshmem_realize(), I guess I could free it
> in ivshmem_exit().

Unfortunately you can't, because the guest might be using it at the time
of hot-unplug (e.g. DMAing from disk to it).  Unrealize is the place
where you hide stuff, and in this case the PCI core does it for you;
finalize is the place where you free stuff.

This is mentioned (though not really in these terms) in docs/memory.txt.

Paolo

>> or, alternatively just use a flag (e.g. s->bar2_mapped) and allocate it
>> directly in the IVShmemState struct.
> 
> I'll see what comes out nicer.  Thanks!
>
Markus Armbruster March 2, 2016, 11:06 a.m. UTC | #7
Paolo Bonzini <pbonzini@redhat.com> writes:

> On 01/03/2016 15:06, Markus Armbruster wrote:
>> Paolo Bonzini <pbonzini@redhat.com> writes:
>> 
>>> On 29/02/2016 19:40, Markus Armbruster wrote:
>>>> -    memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
>>>> +    s->ivshmem_bar2 = g_new(MemoryRegion, 1);
>>>> +    memory_region_init_ram_ptr(s->ivshmem_bar2, OBJECT(s),
>>>>                                 "ivshmem.bar2", s->ivshmem_size, ptr);
>>>> -    qemu_set_ram_fd(s->ivshmem.ram_addr, fd);
>>>> -    vmstate_register_ram(&s->ivshmem, DEVICE(s));
>>>> -    memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
>>>> +    qemu_set_ram_fd(s->ivshmem_bar2->ram_addr, fd);
>>>
>>> This is missing an instance_finalize callback to do
>>>
>>>     if (s->ivshmem_bar2) {
>>>         object_unparent(s->ivshmem_bar2);
>>>         g_free(s->ivshmem_bar2);
>>>     }
>> 
>> Since it's allocated within ivshmem_realize(), I guess I could free it
>> in ivshmem_exit().
>
> Unfortunately you can't, because the guest might be using it at the time
> of hot-unplug (e.g. DMAing from disk to it).  Unrealize is the place
> where you hide stuff, and in this case the PCI core does it for you;
> finalize is the place where you free stuff.
>
> This is mentioned (though not really in these terms) in docs/memory.txt.

You mean I'm supposed to have read and understood that?!?  ;-}
Thanks!

[...]
diff mbox

Patch

diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c
index 9931d5e..0440bca 100644
--- a/hw/misc/ivshmem.c
+++ b/hw/misc/ivshmem.c
@@ -82,12 +82,7 @@  typedef struct IVShmemState {
     CharDriverState *server_chr;
     MemoryRegion ivshmem_mmio;
 
-    /* We might need to register the BAR before we actually have the memory.
-     * So prepare a container MemoryRegion for the BAR immediately and
-     * add a subregion when we have the memory.
-     */
-    MemoryRegion bar;
-    MemoryRegion ivshmem;
+    MemoryRegion *ivshmem_bar2; /* BAR 2 (shared memory) */
     size_t ivshmem_size; /* size of shared memory region */
     uint32_t ivshmem_64bit;
 
@@ -487,7 +482,7 @@  static void process_msg_shmem(IVShmemState *s, int fd, Error **errp)
     Error *err = NULL;
     void *ptr;
 
-    if (memory_region_is_mapped(&s->ivshmem)) {
+    if (s->ivshmem_bar2) {
         error_setg(errp, "server sent unexpected shared memory message");
         close(fd);
         return;
@@ -506,11 +501,10 @@  static void process_msg_shmem(IVShmemState *s, int fd, Error **errp)
         close(fd);
         return;
     }
-    memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
+    s->ivshmem_bar2 = g_new(MemoryRegion, 1);
+    memory_region_init_ram_ptr(s->ivshmem_bar2, OBJECT(s),
                                "ivshmem.bar2", s->ivshmem_size, ptr);
-    qemu_set_ram_fd(s->ivshmem.ram_addr, fd);
-    vmstate_register_ram(&s->ivshmem, DEVICE(s));
-    memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
+    qemu_set_ram_fd(s->ivshmem_bar2->ram_addr, fd);
 }
 
 static void process_msg_disconnect(IVShmemState *s, uint16_t posn,
@@ -696,7 +690,7 @@  static void ivshmem_recv_setup(IVShmemState *s, Error **errp)
         }
     } while (msg != -1);
 
-    assert(memory_region_is_mapped(&s->ivshmem));
+    assert(s->ivshmem_bar2);
 }
 
 /* Select the MSI-X vectors used by device.
@@ -909,7 +903,6 @@  static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
                      &s->ivshmem_mmio);
 
-    memory_region_init(&s->bar, OBJECT(s), "ivshmem-bar2-container", s->ivshmem_size);
     if (s->ivshmem_64bit) {
         attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
     }
@@ -919,15 +912,10 @@  static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
     }
 
     if (s->hostmem != NULL) {
-        MemoryRegion *mr;
-
         IVSHMEM_DPRINTF("using hostmem\n");
 
-        mr = host_memory_backend_get_memory(MEMORY_BACKEND(s->hostmem),
-                                            &error_abort);
-        vmstate_register_ram(mr, DEVICE(s));
-        memory_region_add_subregion(&s->bar, 0, mr);
-        pci_register_bar(PCI_DEVICE(s), 2, attr, &s->bar);
+        s->ivshmem_bar2 = host_memory_backend_get_memory(s->hostmem,
+                                                         &error_abort);
     } else {
         IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
                         s->server_chr->filename);
@@ -935,8 +923,6 @@  static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
         /* we allocate enough space for 16 peers and grow as needed */
         resize_peers(s, 16);
 
-        pci_register_bar(dev, 2, attr, &s->bar);
-
         /*
          * Receive setup messages from server synchronously.
          * Older versions did it asynchronously, but that creates a
@@ -957,6 +943,9 @@  static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
         }
     }
 
+    vmstate_register_ram(s->ivshmem_bar2, DEVICE(s));
+    pci_register_bar(PCI_DEVICE(s), 2, attr, s->ivshmem_bar2);
+
     if (s->role_val == IVSHMEM_PEER) {
         error_setg(&s->migration_blocker,
                    "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
@@ -974,22 +963,19 @@  static void pci_ivshmem_exit(PCIDevice *dev)
         error_free(s->migration_blocker);
     }
 
-    if (memory_region_is_mapped(&s->ivshmem)) {
+    if (memory_region_is_mapped(s->ivshmem_bar2)) {
         if (!s->hostmem) {
-            void *addr = memory_region_get_ram_ptr(&s->ivshmem);
-            int fd;
+            void *addr = memory_region_get_ram_ptr(s->ivshmem_bar2);
 
             if (munmap(addr, s->ivshmem_size) == -1) {
                 error_report("Failed to munmap shared memory %s",
                              strerror(errno));
             }
 
-            if ((fd = qemu_get_ram_fd(s->ivshmem.ram_addr)) != -1)
-                close(fd);
+            close(qemu_get_ram_fd(s->ivshmem_bar2->ram_addr));
         }
 
-        vmstate_unregister_ram(&s->ivshmem, DEVICE(dev));
-        memory_region_del_subregion(&s->bar, &s->ivshmem);
+        vmstate_unregister_ram(s->ivshmem_bar2, DEVICE(dev));
     }
 
     if (s->peers) {