From patchwork Fri Jul 2 17:13:17 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 57715 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 3D7C31007D2 for ; Sat, 3 Jul 2010 03:55:26 +1000 (EST) Received: from localhost ([127.0.0.1]:50657 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OUkSN-0004j8-CX for incoming@patchwork.ozlabs.org; Fri, 02 Jul 2010 13:55:19 -0400 Received: from [140.186.70.92] (port=43014 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OUjnm-0003vE-5w for qemu-devel@nongnu.org; Fri, 02 Jul 2010 13:13:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OUjnk-0007SS-SO for qemu-devel@nongnu.org; Fri, 02 Jul 2010 13:13:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60196) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OUjnk-0007SL-JF for qemu-devel@nongnu.org; Fri, 02 Jul 2010 13:13:20 -0400 Received: from int-mx04.intmail.prod.int.phx2.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.17]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o62HDISH017911 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 2 Jul 2010 13:13:19 -0400 Received: from localhost.localdomain (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx04.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o62HDHLC000513; Fri, 2 Jul 2010 13:13:18 -0400 From: Alex Williamson To: qemu-devel@nongnu.org Date: Fri, 02 Jul 2010 11:13:17 -0600 Message-ID: <20100702171317.30243.38827.stgit@localhost.localdomain> In-Reply-To: <20100702171054.30243.19599.stgit@localhost.localdomain> References: <20100702171054.30243.19599.stgit@localhost.localdomain> User-Agent: StGIT/0.14.3 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.17 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: alex.williamson@redhat.com Subject: [Qemu-devel] [PATCH v3 14/16] qemu_ram_free: Implement it X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Now that we can support a ram_addr_t space with holes, we can implement qemu_ram_free(). Signed-off-by: Alex Williamson --- cpu-all.h | 3 +++ exec.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/cpu-all.h b/cpu-all.h index 5d8342b..224ca40 100644 --- a/cpu-all.h +++ b/cpu-all.h @@ -867,6 +867,9 @@ typedef struct RAMBlock { ram_addr_t length; char idstr[256]; QLIST_ENTRY(RAMBlock) next; +#if defined(__linux__) && !defined(TARGET_S390X) + int fd; +#endif } RAMBlock; typedef struct RAMList { diff --git a/exec.c b/exec.c index fd47d5b..b9a1471 100644 --- a/exec.c +++ b/exec.c @@ -2701,7 +2701,9 @@ static long gethugepagesize(const char *path) return fs.f_bsize; } -static void *file_ram_alloc(ram_addr_t memory, const char *path) +static void *file_ram_alloc(RAMBlock *block, + ram_addr_t memory, + const char *path) { char *filename; void *area; @@ -2764,12 +2766,39 @@ static void *file_ram_alloc(ram_addr_t memory, const char *path) close(fd); return (NULL); } + block->fd = fd; return area; } #endif static ram_addr_t find_ram_offset(ram_addr_t size) { + RAMBlock *block, *next_block; + ram_addr_t offset, mingap = ULONG_MAX; + + if (QLIST_EMPTY(&ram_list.blocks)) + return 0; + + QLIST_FOREACH(block, &ram_list.blocks, next) { + ram_addr_t end, next = ULONG_MAX; + + end = block->offset + block->length; + + QLIST_FOREACH(next_block, &ram_list.blocks, next) { + if (next_block->offset >= end) { + next = MIN(next, next_block->offset); + } + } + if (next - end >= size && next - end < mingap) { + offset = end; + mingap = next - end; + } + } + return offset; +} + +static ram_addr_t last_ram_offset(void) +{ RAMBlock *block; ram_addr_t last = 0; @@ -2812,7 +2841,7 @@ ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size) if (mem_path) { #if defined (__linux__) && !defined(TARGET_S390X) - new_block->host = file_ram_alloc(size, mem_path); + new_block->host = file_ram_alloc(new_block, size, mem_path); if (!new_block->host) { new_block->host = qemu_vmalloc(size); #ifdef MADV_MERGEABLE @@ -2842,7 +2871,7 @@ ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size) QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next); ram_list.phys_dirty = qemu_realloc(ram_list.phys_dirty, - (new_block->offset + size) >> TARGET_PAGE_BITS); + last_ram_offset() >> TARGET_PAGE_BITS); memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS), 0xff, size >> TARGET_PAGE_BITS); @@ -2854,7 +2883,32 @@ ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size) void qemu_ram_free(ram_addr_t addr) { - /* TODO: implement this. */ + RAMBlock *block; + + QLIST_FOREACH(block, &ram_list.blocks, next) { + if (addr == block->offset) { + QLIST_REMOVE(block, next); + if (mem_path) { +#if defined (__linux__) && !defined(TARGET_S390X) + if (block->fd) { + munmap(block->host, block->length); + close(block->fd); + } else { + qemu_vfree(block->host); + } +#endif + } else { +#if defined(TARGET_S390X) && defined(CONFIG_KVM) + munmap(block->host, block->length); +#else + qemu_vfree(block->host); +#endif + } + qemu_free(block); + return; + } + } + } /* Return a host pointer to ram allocated with qemu_ram_alloc.