From patchwork Tue Oct 6 19:19:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 527032 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A4327140D93 for ; Wed, 7 Oct 2015 09:23:57 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=QbKrsK6n; dkim-atps=neutral Received: from localhost ([::1]:54376 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjaeI-0001WH-T3 for incoming@patchwork.ozlabs.org; Tue, 06 Oct 2015 18:23:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58158) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zjae3-0001FP-HE for qemu-devel@nongnu.org; Tue, 06 Oct 2015 18:23:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zjae0-0004a8-1Z for qemu-devel@nongnu.org; Tue, 06 Oct 2015 18:23:39 -0400 Received: from mail-qg0-x22c.google.com ([2607:f8b0:400d:c04::22c]:35286) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjXoP-0007RB-IO for qemu-devel@nongnu.org; Tue, 06 Oct 2015 15:22:09 -0400 Received: by qgt47 with SMTP id 47so183794216qgt.2 for ; Tue, 06 Oct 2015 12:22:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=aSlyKE7uDv+O0RGUc1P2mtPv8lKOGrXe8FDtEbJSNLE=; b=QbKrsK6njPwArGzsaWPtTul5Ua+F1sxYXewcWBdgM0z6iIVRsvoWwAAVprD7VVGsNJ CZaOqCJzoW/1F0ai16Ps1dghBAU59IKQZeZn0zJfw6t/oaKA3WN146o+8dFsOlpl3KvH 2GjhKIpk2e+FVE9Dkb3TlxNyAArLUfThrCa1ihL3kv92/DMxKhHZkxgx9zoohJpwSrQH 5YndjIsLUWQeIC40/aFin2lZPrQiweLudcW7Q9Be4TehUqkMA3BlC06cS2rSxlq0zidp XyB18c9QA4q7joI2URufLBUkI3g5OWzlxrN+iL8RG7gs1y7vJfLIkX3ieDtBwAelsxkk 32Tw== X-Received: by 10.140.20.168 with SMTP id 37mr48888845qgj.16.1444159329290; Tue, 06 Oct 2015 12:22:09 -0700 (PDT) Received: from localhost (bne75-h02-31-39-163-232.dsl.sta.abo.bbox.fr. [31.39.163.232]) by smtp.gmail.com with ESMTPSA id 8sm14536410qgi.1.2015.10.06.12.22.07 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Oct 2015 12:22:08 -0700 (PDT) From: marcandre.lureau@redhat.com To: peter.maydell@linaro.org Date: Tue, 6 Oct 2015 21:19:38 +0200 Message-Id: <1444159184-18153-43-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1444159184-18153-1-git-send-email-marcandre.lureau@redhat.com> References: <1444159184-18153-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::22c Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , qemu-devel@nongnu.org Subject: [Qemu-devel] [PULL 42/48] ivshmem: do not keep shm_fd open X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 Reviewed-by: Claudio Fontana --- hw/misc/ivshmem.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 3b6acd6..b873c23 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; @@ -356,8 +355,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)); @@ -535,7 +532,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; @@ -564,9 +561,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; } @@ -707,8 +702,6 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp) PCI_BASE_ADDRESS_MEM_PREFETCH; Error *local_err = NULL; - s->shm_fd = -1; - if (s->sizearg == NULL) { s->ivshmem_size = 4 << 20; /* 4 MB default */ } else { @@ -830,6 +823,7 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp) } create_shared_memory_BAR(s, fd, attr, errp); + close(fd); } } @@ -845,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));