From patchwork Tue Jul 28 00:32:23 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: 500958 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 1BE6114027F for ; Tue, 28 Jul 2015 10:39:35 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=cjwc6G0B; dkim-atps=neutral Received: from localhost ([::1]:56389 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZJsvd-0001OZ-5f for incoming@patchwork.ozlabs.org; Mon, 27 Jul 2015 20:39:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47388) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZJsq8-0007XH-3U for qemu-devel@nongnu.org; Mon, 27 Jul 2015 20:33:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZJsq7-0005HB-4a for qemu-devel@nongnu.org; Mon, 27 Jul 2015 20:33:52 -0400 Received: from mail-qg0-x234.google.com ([2607:f8b0:400d:c04::234]:34657) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZJsq6-0005Gd-WA for qemu-devel@nongnu.org; Mon, 27 Jul 2015 20:33:51 -0400 Received: by qgeu79 with SMTP id u79so64798341qge.1 for ; Mon, 27 Jul 2015 17:33:50 -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=vSS4U1pCkwQq37YP/mcn5gI7BpVu3krKwZm70qeX3C4=; b=cjwc6G0BcDt7S1a6HxhvAi5f24WnROMFtTfL+ggUkrxB26gR5PZ9EFCiJGxGPdpDLV 8UyQxSmkol7tBicQ0HakJJ4Q3N3ZUGEOgup7ZuJSarqr+tFoC+XpFV5Z/XFlG0hN0/sz nKkXgLXIDuTm7900cICITO7xG72gYDyACLyEAP4i9N0doNc3etRwQ2zk5z1cYTa7XS8s WK1cStw94U3fTx99Vxia/j10qsiEnhMAWlI1S2OP77qxAOUcN1cKEsIpDTS9Ko3RcCb5 otp1pot7fdhKONuQr8Ij4FPvlyCraHRNEFUwVveDqHoIjSjbs7oRDBvpFJ1vvWyaKTv0 R6Lg== X-Received: by 10.140.107.101 with SMTP id g92mr44423514qgf.58.1438043630740; Mon, 27 Jul 2015 17:33:50 -0700 (PDT) Received: from localhost (APoitiers-257-1-37-212.w90-38.abo.wanadoo.fr. [90.38.212.212]) by smtp.gmail.com with ESMTPSA id j62sm10187536qhc.33.2015.07.27.17.33.48 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Jul 2015 17:33:49 -0700 (PDT) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Tue, 28 Jul 2015 02:32:23 +0200 Message-Id: <1438043577-28636-12-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.4.3 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 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::234 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 Subject: [Qemu-devel] [PATCH v2 11/45] ivshmem: increase_dynamic_storage() improvements 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 - rename the function resize_peers() to better reflect the content - limit the maximum number of peers to IVSHMEM_MAX_PEERS as MAXUINT16, which is more realistic and also better reflects the limit of the doorbell register. - change the argument to be the size, not the 0-index max value - immediately allocate the eventfds instead of lazily Signed-off-by: Marc-André Lureau --- hw/misc/ivshmem.c | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 7ba93c0..66f9c0b 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -34,6 +34,7 @@ #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET #define PCI_DEVICE_ID_IVSHMEM 0x1110 +#define IVSHMEM_MAX_PEERS G_MAXUINT16 #define IVSHMEM_IOEVENTFD 0 #define IVSHMEM_MSI 1 @@ -416,31 +417,28 @@ static void close_guest_eventfds(IVShmemState *s, int posn) /* this function increase the dynamic storage need to store data about other * guests */ -static int increase_dynamic_storage(IVShmemState *s, int new_min_size) +static int resize_peers(IVShmemState *s, int new_min_size) { - int j, old_nb_alloc; + int j, old_size; - /* check for integer overflow */ - if (new_min_size >= INT_MAX / sizeof(Peer) - 1 || new_min_size <= 0) { + /* limit number of max peers */ + if (new_min_size <= 0 || new_min_size > IVSHMEM_MAX_PEERS) { return -1; } - - old_nb_alloc = s->nb_peers; - - if (new_min_size >= s->nb_peers) { - /* +1 because #new_min_size is used as last array index */ - s->nb_peers = new_min_size + 1; - } else { + if (new_min_size <= s->nb_peers) { return 0; } + old_size = s->nb_peers; + s->nb_peers = new_min_size; + IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers); + s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer)); - /* zero out new pointers */ - for (j = old_nb_alloc; j < s->nb_peers; j++) { - s->peers[j].eventfds = NULL; + for (j = old_size; j < s->nb_peers; j++) { + s->peers[j].eventfds = g_new(EventNotifier, s->vectors); s->peers[j].nb_eventfds = 0; } @@ -507,8 +505,8 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size) /* make sure we have enough space for this guest */ if (incoming_posn >= s->nb_peers) { - if (increase_dynamic_storage(s, incoming_posn) < 0) { - error_report("increase_dynamic_storage() failed"); + if (resize_peers(s, incoming_posn + 1) < 0) { + error_report("failed to resize peers array"); if (incoming_fd != -1) { close(incoming_fd); } @@ -518,8 +516,7 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size) if (incoming_fd == -1) { /* if posn is positive and unseen before then this is our posn*/ - if ((incoming_posn >= 0) && - (s->peers[incoming_posn].eventfds == NULL)) { + if (incoming_posn >= 0 && s->vm_id == -1) { /* receive our posn */ s->vm_id = incoming_posn; return; @@ -570,11 +567,6 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size) * guests for each VM */ guest_max_eventfd = s->peers[incoming_posn].nb_eventfds; - if (guest_max_eventfd == 0) { - /* one eventfd per MSI vector */ - s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors); - } - /* this is an eventfd for a particular guest VM */ IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn, guest_max_eventfd, incoming_fd); @@ -811,12 +803,9 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp) } /* we allocate enough space for 16 guests and grow as needed */ - s->nb_peers = 16; + resize_peers(s, 16); s->vm_id = -1; - /* allocate/initialize space for interrupt handling */ - s->peers = g_malloc0(s->nb_peers * sizeof(Peer)); - pci_register_bar(dev, 2, attr, &s->bar); s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));