From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48092) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZJssE-0002ow-FT for qemu-devel@nongnu.org; Mon, 27 Jul 2015 20:36:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZJssD-0006BY-H1 for qemu-devel@nongnu.org; Mon, 27 Jul 2015 20:36:02 -0400 Received: from mail-qk0-x235.google.com ([2607:f8b0:400d:c09::235]:33597) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZJssD-0006BT-DF for qemu-devel@nongnu.org; Mon, 27 Jul 2015 20:36:01 -0400 Received: by qkdl129 with SMTP id l129so45875196qkd.0 for ; Mon, 27 Jul 2015 17:36:01 -0700 (PDT) Sender: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 28 Jul 2015 02:32:52 +0200 Message-Id: <1438043577-28636-41-git-send-email-marcandre.lureau@redhat.com> In-Reply-To: <1438043577-28636-1-git-send-email-marcandre.lureau@redhat.com> References: <1438043577-28636-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PATCH v2 40/45] ivshmem: do not keep shm_fd open List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , drjones@redhat.com, cam@cs.ualberta.ca, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , stefanha@redhat.com From: Marc-André Lureau Remove shm_fd from device state, closing it as early as possible to avoid leaks. Signed-off-by: Marc-André Lureau --- hw/misc/ivshmem.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 3dabcdd..b85fcd4 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -88,7 +88,6 @@ typedef struct IVShmemState { MemoryRegion ivshmem; uint64_t ivshmem_size; /* size of shared memory region */ uint32_t ivshmem_64bit; - int shm_fd; /* shared memory file descriptor */ Peer *peers; int nb_peers; /* how many peers we have space for */ @@ -235,7 +234,7 @@ static uint64_t ivshmem_io_read(void *opaque, hwaddr addr, case IVPOSITION: /* return my VM ID if the memory is mapped */ - if (s->shm_fd >= 0) { + if (memory_region_is_mapped(&s->ivshmem)) { ret = s->vm_id; } else { ret = -1; @@ -357,8 +356,6 @@ static int create_shared_memory_BAR(IVShmemState *s, int fd, uint8_t attr, return -1; } - s->shm_fd = fd; - memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s), "ivshmem.bar2", s->ivshmem_size, ptr); vmstate_register_ram(&s->ivshmem, DEVICE(s)); @@ -536,7 +533,7 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size) if (incoming_posn == -1) { void * map_ptr; - if (s->shm_fd >= 0) { + if (memory_region_is_mapped(&s->ivshmem)) { error_report("shm already initialized"); close(incoming_fd); return; @@ -565,9 +562,7 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size) memory_region_add_subregion(&s->bar, 0, &s->ivshmem); - /* only store the fd if it is successfully mapped */ - s->shm_fd = incoming_fd; - + close(incoming_fd); return; } @@ -828,6 +823,7 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp) } create_shared_memory_BAR(s, fd, attr, errp); + close(fd); } } @@ -843,7 +839,7 @@ static void pci_ivshmem_exit(PCIDevice *dev) error_free(s->migration_blocker); } - if (s->shm_fd >= 0) { + if (memory_region_is_mapped(&s->ivshmem)) { void *addr = memory_region_get_ram_ptr(&s->ivshmem); vmstate_unregister_ram(&s->ivshmem, DEVICE(dev)); -- 2.4.3