From patchwork Tue Feb 14 09:27:39 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avi Kivity X-Patchwork-Id: 141091 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 46AA51007D1 for ; Tue, 14 Feb 2012 21:18:14 +1100 (EST) Received: from localhost ([::1]:32807 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RxEh4-0004t7-Cw for incoming@patchwork.ozlabs.org; Tue, 14 Feb 2012 04:29:02 -0500 Received: from eggs.gnu.org ([140.186.70.92]:39646) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RxEgF-0002z2-Kn for qemu-devel@nongnu.org; Tue, 14 Feb 2012 04:28:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RxEgA-0003Uw-DW for qemu-devel@nongnu.org; Tue, 14 Feb 2012 04:28:11 -0500 Received: from mx1.redhat.com ([209.132.183.28]:8955) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RxEgA-0003UV-5R for qemu-devel@nongnu.org; Tue, 14 Feb 2012 04:28:06 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q1E9S5Ag014232 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 14 Feb 2012 04:28:05 -0500 Received: from cleopatra.tlv.redhat.com (cleopatra.tlv.redhat.com [10.35.255.11]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q1E9S2ax013201 for ; Tue, 14 Feb 2012 04:28:05 -0500 Received: from s01.tlv.redhat.com (s01.tlv.redhat.com [10.35.255.8]) by cleopatra.tlv.redhat.com (Postfix) with ESMTP id 84144250BB3; Tue, 14 Feb 2012 11:27:58 +0200 (IST) From: Avi Kivity To: qemu-devel@nongnu.org Date: Tue, 14 Feb 2012 11:27:39 +0200 Message-Id: <1329211670-11548-10-git-send-email-avi@redhat.com> In-Reply-To: <1329211670-11548-1-git-send-email-avi@redhat.com> References: <1329211670-11548-1-git-send-email-avi@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 09/20] memory: compress phys_map node pointers to 16 bits 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 Use an expanding vector to store nodes. Allocation is baroque to g_renew() potentially invalidating pointers; this will be addressed later. Signed-off-by: Avi Kivity --- exec.c | 54 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 45 insertions(+), 9 deletions(-) diff --git a/exec.c b/exec.c index 957bc6d..0756919 100644 --- a/exec.c +++ b/exec.c @@ -201,13 +201,19 @@ struct PhysPageEntry { union { uint16_t leaf; /* index into phys_sections */ - PhysPageEntry *node; + uint16_t node; /* index into phys_map_nodes */ } u; }; +/* Simple allocator for PhysPageEntry nodes */ +static PhysPageEntry (*phys_map_nodes)[L2_SIZE]; +static unsigned phys_map_nodes_nb, phys_map_nodes_nb_alloc; + +#define PHYS_MAP_NODE_NIL ((uint16_t)~0) + /* This is a multi-level map on the physical address space. The bottom level has pointers to PhysPageDesc. */ -static PhysPageEntry phys_map; +static PhysPageEntry phys_map = { .u.node = PHYS_MAP_NODE_NIL }; static void io_mem_init(void); static void memory_map_init(void); @@ -403,6 +409,32 @@ static inline PageDesc *page_find(tb_page_addr_t index) } #if !defined(CONFIG_USER_ONLY) + +static PhysPageEntry *phys_map_node_alloc(uint16_t *ptr) +{ + unsigned i; + uint16_t ret; + + /* Assign early to avoid the pointer being invalidated by g_renew() */ + *ptr = ret = phys_map_nodes_nb++; + assert(ret != PHYS_MAP_NODE_NIL); + if (ret == phys_map_nodes_nb_alloc) { + typedef PhysPageEntry Node[L2_SIZE]; + phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc * 2, 16); + phys_map_nodes = g_renew(Node, phys_map_nodes, + phys_map_nodes_nb_alloc); + } + for (i = 0; i < L2_SIZE; ++i) { + phys_map_nodes[ret][i].u.node = PHYS_MAP_NODE_NIL; + } + return phys_map_nodes[ret]; +} + +static void phys_map_nodes_reset(void) +{ + phys_map_nodes_nb = 0; +} + static uint16_t *phys_page_find_alloc(target_phys_addr_t index, int alloc) { PhysPageEntry *lp, *p; @@ -412,18 +444,20 @@ static uint16_t *phys_page_find_alloc(target_phys_addr_t index, int alloc) /* Level 1..N. */ for (i = P_L2_LEVELS - 1; i >= 0; i--) { - if (lp->u.node == NULL) { + if (lp->u.node == PHYS_MAP_NODE_NIL) { if (!alloc) { return NULL; } - lp->u.node = p = g_malloc0(sizeof(PhysPageEntry) * L2_SIZE); + p = phys_map_node_alloc(&lp->u.node); if (i == 0) { for (j = 0; j < L2_SIZE; j++) { p[j].u.leaf = phys_section_unassigned; } } + } else { + p = phys_map_nodes[lp->u.node]; } - lp = &lp->u.node[(index >> (i * L2_BITS)) & (L2_SIZE - 1)]; + lp = &p[(index >> (i * L2_BITS)) & (L2_SIZE - 1)]; } return &lp->u.leaf; @@ -2538,12 +2572,13 @@ static void destroy_page_desc(uint16_t section_index) static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level) { unsigned i; - PhysPageEntry *p = lp->u.node; + PhysPageEntry *p; - if (!p) { + if (lp->u.node == PHYS_MAP_NODE_NIL) { return; } + p = phys_map_nodes[lp->u.node]; for (i = 0; i < L2_SIZE; ++i) { if (level > 0) { destroy_l2_mapping(&p[i], level - 1); @@ -2551,13 +2586,13 @@ static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level) destroy_page_desc(p[i].u.leaf); } } - g_free(p); - lp->u.node = NULL; + lp->u.node = PHYS_MAP_NODE_NIL; } static void destroy_all_mappings(void) { destroy_l2_mapping(&phys_map, P_L2_LEVELS - 1); + phys_map_nodes_reset(); } static uint16_t phys_section_add(MemoryRegionSection *section) @@ -3543,6 +3578,7 @@ static void core_begin(MemoryListener *listener) { destroy_all_mappings(); phys_sections_clear(); + phys_map.u.node = PHYS_MAP_NODE_NIL; phys_section_unassigned = dummy_section(&io_mem_unassigned); }