From patchwork Thu Aug 24 10:14:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olaf Hering X-Patchwork-Id: 9919731 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1B01C60353 for ; Thu, 24 Aug 2017 10:17:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0C11928B66 for ; Thu, 24 Aug 2017 10:17:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 00F6228BD3; Thu, 24 Aug 2017 10:17:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3AB3928B66 for ; Thu, 24 Aug 2017 10:17:18 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dkpAU-0002oF-11; Thu, 24 Aug 2017 10:15:18 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dkpAT-0002nb-9I for xen-devel@lists.xen.org; Thu, 24 Aug 2017 10:15:17 +0000 Received: from [85.158.139.211] by server-5.bemta-5.messagelabs.com id 55/22-02177-437AE995; Thu, 24 Aug 2017 10:15:16 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrJLMWRWlGSWpSXmKPExsUSuHLSHV2D5fM iDb5csbFY8nExiwOjx9Hdv5kCGKNYM/OS8isSWDMm/Qkt+GNZMe/aXZYGxpM6XYxcHCwCn5gk NvcvYOpi5OCQEMiUWDYvDMIUkXjyPw2kREjgIJPEtQPzgEo4OdgElCT2HjzOCJIQEZjIKHHw9 D1GkASzgILEi+dbwYqEBbwkpt98xAZiswioStx7Oo8ZxOYVMJY4PX0nC4gtISAv0b9jOVg9p4 CJxLmF+8FsIaCa7d+us0xg5F3AyLCKUaM4tagstUjXyEIvqSgzPaMkNzEzR9fQwFQvN7W4ODE 9NScxqVgvOT93EyMwGOoZGBh3MPat8jvEKMnBpCTK+0R6XqQQX1J+SmVGYnFGfFFpTmrxIUYZ Dg4lCV7dZUA5waLU9NSKtMwcYFjCpCU4eJREeEtB0rzFBYm5xZnpEKlTjLocG1av/8IkxJKXn 5cqJc7rAlIkAFKUUZoHNwIWI5cYZaWEeRkZGBiEeApSi3IzS1DlXzGKczAqCfNeWwo0hSczrw Ru0yugI5iAjph0Yg7IESWJCCmpBkb3NX+uzK86KjN1385/wQpXdL5z7i+e0L9BID4y2yP78Js 3rFpuxzZdCkrfbxKRlqzZyHYqcYJ3f6zkrsn71rXVzFk/wbPX4sUexyk3S1Xr7p1xvhh9awPH dRtT7ZLCjNdcU36zpb7xenZu8b5GzriUym/ztZ6utz9ablxSU3WxyjL70Xxb+StKLMUZiYZaz EXFiQAsLiRFjAIAAA== X-Env-Sender: olaf@aepfle.de X-Msg-Ref: server-6.tower-206.messagelabs.com!1503569712!105813413!1 X-Originating-IP: [81.169.146.220] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 18556 invoked from network); 24 Aug 2017 10:15:12 -0000 Received: from mo4-p00-ob.smtp.rzone.de (HELO mo4-p00-ob.smtp.rzone.de) (81.169.146.220) by server-6.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 24 Aug 2017 10:15:12 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1503569712; s=domk; d=aepfle.de; h=References:In-Reply-To:Date:Subject:Cc:To:From; bh=SBMxxa4THFpsj1rb+FCRl2F0EGJa7FdKjwkD12qGpGk=; b=LsCeeEsc7Sg75zIon393bRAhTgvR/UR+RWYjO++pHhdnNpw4dADewGU3NSsbHYZx08 jEZPrNb16KGB22rgNfUQlxEqs163J8zFdOvTLW0KbXe14sTrAgfO56H4JpsMf8w04oK9 Nck8mkzLTT5jbvypmILtPIOHrWv0LVfYEHRUg= X-RZG-AUTH: :P2EQZWCpfu+qG7CngxMFH1J+yackYocTD1iAi8x+OWi/zfN1cLnAYQz4mzReZKAqPT2tb6Nx5EL7HQvAw3tBGHnS2eB9 X-RZG-CLASS-ID: mo00 Received: from sender ([2001:a61:3430:dff:75aa:c36f:225e:fcee]) by smtp.strato.de (RZmta 41.4 AUTH) with ESMTPSA id q03d67t7OAF3AJY (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA (curve secp521r1 with 521 ECDH bits, eq. 15360 bits RSA)) (Client did not present a certificate); Thu, 24 Aug 2017 12:15:03 +0200 (CEST) From: Olaf Hering To: xen-devel@lists.xen.org, Andrew Cooper , Ian Jackson , Wei Liu Date: Thu, 24 Aug 2017 12:14:42 +0200 Message-Id: <20170824101443.31345-3-olaf@aepfle.de> X-Mailer: git-send-email 2.14.0 In-Reply-To: <20170824101443.31345-1-olaf@aepfle.de> References: <20170824101443.31345-1-olaf@aepfle.de> Cc: Olaf Hering Subject: [Xen-devel] [PATCH v3 2/3] tools/libxc: add API for bitmap access for restore X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Extend API for managing bitmaps. Each bitmap is now represented by a generic struct xc_sr_bitmap. Switch the existing populated_pfns to this API. Signed-off-by: Olaf Hering Acked-by: Wei Liu --- tools/libxc/xc_sr_common.c | 41 +++++++++++++++++++++++++++ tools/libxc/xc_sr_common.h | 68 +++++++++++++++++++++++++++++++++++++++++++-- tools/libxc/xc_sr_restore.c | 66 ++----------------------------------------- 3 files changed, 110 insertions(+), 65 deletions(-) diff --git a/tools/libxc/xc_sr_common.c b/tools/libxc/xc_sr_common.c index 79b9c3e940..4d221ca90c 100644 --- a/tools/libxc/xc_sr_common.c +++ b/tools/libxc/xc_sr_common.c @@ -155,6 +155,47 @@ static void __attribute__((unused)) build_assertions(void) BUILD_BUG_ON(sizeof(struct xc_sr_rec_hvm_params) != 8); } +/* + * Expand the tracking structures as needed. + * To avoid realloc()ing too excessively, the size increased to the nearest power + * of two large enough to contain the required number of bits. + */ +bool _xc_sr_bitmap_resize(struct xc_sr_bitmap *bm, unsigned long bits) +{ + if (bits > bm->bits) + { + size_t new_max; + size_t old_sz, new_sz; + void *p; + + /* Round up to the nearest power of two larger than bit, less 1. */ + new_max = bits; + new_max |= new_max >> 1; + new_max |= new_max >> 2; + new_max |= new_max >> 4; + new_max |= new_max >> 8; + new_max |= new_max >> 16; +#ifdef __x86_64__ + new_max |= new_max >> 32; +#endif + + old_sz = bitmap_size(bm->bits + 1); + new_sz = bitmap_size(new_max + 1); + p = realloc(bm->p, new_sz); + if (!p) + return false; + + if (bm->p) + memset(p + old_sz, 0, new_sz - old_sz); + else + memset(p, 0, new_sz); + + bm->p = p; + bm->bits = new_max; + } + return true; +} + /* * Local variables: * mode: C diff --git a/tools/libxc/xc_sr_common.h b/tools/libxc/xc_sr_common.h index a83f22af4e..8901af112a 100644 --- a/tools/libxc/xc_sr_common.h +++ b/tools/libxc/xc_sr_common.h @@ -172,6 +172,12 @@ struct xc_sr_x86_pv_restore_vcpu size_t basicsz, extdsz, xsavesz, msrsz; }; +struct xc_sr_bitmap +{ + void *p; + unsigned long bits; +}; + struct xc_sr_context { xc_interface *xch; @@ -255,8 +261,7 @@ struct xc_sr_context domid_t xenstore_domid, console_domid; /* Bitmap of currently populated PFNs during restore. */ - unsigned long *populated_pfns; - xen_pfn_t max_populated_pfn; + struct xc_sr_bitmap populated_pfns; /* Sender has invoked verify mode on the stream. */ bool verify; @@ -343,6 +348,65 @@ extern struct xc_sr_save_ops save_ops_x86_hvm; extern struct xc_sr_restore_ops restore_ops_x86_pv; extern struct xc_sr_restore_ops restore_ops_x86_hvm; +extern bool _xc_sr_bitmap_resize(struct xc_sr_bitmap *bm, unsigned long bits); + +static inline bool xc_sr_bitmap_resize(struct xc_sr_bitmap *bm, unsigned long bits) +{ + if (bits > bm->bits) + return _xc_sr_bitmap_resize(bm, bits); + return true; +} + +static inline void xc_sr_bitmap_free(struct xc_sr_bitmap *bm) +{ + free(bm->p); + bm->p = NULL; +} + +static inline bool xc_sr_set(unsigned long bit, struct xc_sr_bitmap *bm) +{ + if (!xc_sr_bitmap_resize(bm, bit)) + return false; + + set_bit(bit, bm->p); + return true; +} + +static inline bool xc_sr_test(unsigned long bit, struct xc_sr_bitmap *bm) +{ + if (bit > bm->bits) + return false; + return !!test_bit(bit, bm->p); +} + +static inline int xc_sr_test_and_clear(unsigned long bit, struct xc_sr_bitmap *bm) +{ + return test_and_clear_bit(bit, bm->p); +} + +static inline int xc_sr_test_and_set(unsigned long bit, struct xc_sr_bitmap *bm) +{ + return test_and_set_bit(bit, bm->p); +} + +static inline bool pfn_is_populated(struct xc_sr_context *ctx, xen_pfn_t pfn) +{ + return xc_sr_test(pfn, &ctx->restore.populated_pfns); +} + +static inline int pfn_set_populated(struct xc_sr_context *ctx, xen_pfn_t pfn) +{ + xc_interface *xch = ctx->xch; + + if ( !xc_sr_set(pfn, &ctx->restore.populated_pfns) ) + { + ERROR("Failed to realloc populated_pfns bitmap"); + errno = ENOMEM; + return -1; + } + return 0; +} + struct xc_sr_record { uint32_t type; diff --git a/tools/libxc/xc_sr_restore.c b/tools/libxc/xc_sr_restore.c index a016678332..d53948e1a6 100644 --- a/tools/libxc/xc_sr_restore.c +++ b/tools/libxc/xc_sr_restore.c @@ -68,64 +68,6 @@ static int read_headers(struct xc_sr_context *ctx) return 0; } -/* - * Is a pfn populated? - */ -static bool pfn_is_populated(const struct xc_sr_context *ctx, xen_pfn_t pfn) -{ - if ( pfn > ctx->restore.max_populated_pfn ) - return false; - return test_bit(pfn, ctx->restore.populated_pfns); -} - -/* - * Set a pfn as populated, expanding the tracking structures if needed. To - * avoid realloc()ing too excessively, the size increased to the nearest power - * of two large enough to contain the required pfn. - */ -static int pfn_set_populated(struct xc_sr_context *ctx, xen_pfn_t pfn) -{ - xc_interface *xch = ctx->xch; - - if ( pfn > ctx->restore.max_populated_pfn ) - { - xen_pfn_t new_max; - size_t old_sz, new_sz; - unsigned long *p; - - /* Round up to the nearest power of two larger than pfn, less 1. */ - new_max = pfn; - new_max |= new_max >> 1; - new_max |= new_max >> 2; - new_max |= new_max >> 4; - new_max |= new_max >> 8; - new_max |= new_max >> 16; -#ifdef __x86_64__ - new_max |= new_max >> 32; -#endif - - old_sz = bitmap_size(ctx->restore.max_populated_pfn + 1); - new_sz = bitmap_size(new_max + 1); - p = realloc(ctx->restore.populated_pfns, new_sz); - if ( !p ) - { - ERROR("Failed to realloc populated bitmap"); - errno = ENOMEM; - return -1; - } - - memset((uint8_t *)p + old_sz, 0x00, new_sz - old_sz); - - ctx->restore.populated_pfns = p; - ctx->restore.max_populated_pfn = new_max; - } - - assert(!test_bit(pfn, ctx->restore.populated_pfns)); - set_bit(pfn, ctx->restore.populated_pfns); - - return 0; -} - /* * Given a set of pfns, obtain memory from Xen to fill the physmap for the * unpopulated subset. If types is NULL, no page type checking is performed @@ -684,10 +626,8 @@ static int setup(struct xc_sr_context *ctx) if ( rc ) goto err; - ctx->restore.max_populated_pfn = (32 * 1024 / 4) - 1; - ctx->restore.populated_pfns = bitmap_alloc( - ctx->restore.max_populated_pfn + 1); - if ( !ctx->restore.populated_pfns ) + rc = !xc_sr_bitmap_resize(&ctx->restore.populated_pfns, 32 * 1024 / 4); + if ( rc ) { ERROR("Unable to allocate memory for populated_pfns bitmap"); rc = -1; @@ -722,7 +662,7 @@ static void cleanup(struct xc_sr_context *ctx) xc_hypercall_buffer_free_pages(xch, dirty_bitmap, NRPAGES(bitmap_size(ctx->restore.p2m_size))); free(ctx->restore.buffered_records); - free(ctx->restore.populated_pfns); + xc_sr_bitmap_free(&ctx->restore.populated_pfns); if ( ctx->restore.ops.cleanup(ctx) ) PERROR("Failed to clean up"); }