[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH RFC v2 06/23] libxc/xc_sr_restore: factor helpers out of handle_page_data()
From: Joshua Otto <jtotto@xxxxxxxxxxxx> When processing a PAGE_DATA record, the restore code: 1) applies a number of sanity checks on the record's headers and size 2) decodes the list of packed page info into pfns and their types 3) using the pfn and type info, populates and fills the pages into the guest using process_page_data() Steps 1) and 2) are also useful for other types of pages records introduced by postcopy live migration, so factor them into reusable helper routines. No functional change. Signed-off-by: Joshua Otto <jtotto@xxxxxxxxxxxx> --- tools/libxc/xc_sr_common.c | 36 ++++++++++++++++++ tools/libxc/xc_sr_common.h | 10 +++++ tools/libxc/xc_sr_restore.c | 89 ++++++++++++++++++++++++++------------------- 3 files changed, 97 insertions(+), 38 deletions(-) diff --git a/tools/libxc/xc_sr_common.c b/tools/libxc/xc_sr_common.c index 08abe9a..f443974 100644 --- a/tools/libxc/xc_sr_common.c +++ b/tools/libxc/xc_sr_common.c @@ -140,6 +140,42 @@ int read_record(struct xc_sr_context *ctx, int fd, struct xc_sr_record *rec) return 0; }; +int validate_pages_record(struct xc_sr_context *ctx, struct xc_sr_record *rec, + uint32_t expected_type) +{ + xc_interface *xch = ctx->xch; + struct xc_sr_rec_pages_header *pages = rec->data; + + if ( rec->type != expected_type ) + { + ERROR("%s record type expected, instead received record of type " + "%08x (%s)", rec_type_to_str(expected_type), rec->type, + rec_type_to_str(rec->type)); + return -1; + } + else if ( rec->length < sizeof(*pages) ) + { + ERROR("%s record truncated: length %u, min %zu", + rec_type_to_str(rec->type), rec->length, sizeof(*pages)); + return -1; + } + else if ( pages->count < 1 ) + { + ERROR("Expected at least 1 pfn in %s record", + rec_type_to_str(rec->type)); + return -1; + } + else if ( rec->length < sizeof(*pages) + (pages->count * sizeof(uint64_t)) ) + { + ERROR("%s record (length %u) too short to contain %u" + " pfns worth of information", rec_type_to_str(rec->type), + rec->length, pages->count); + return -1; + } + + return 0; +} + static void __attribute__((unused)) build_assertions(void) { BUILD_BUG_ON(sizeof(struct xc_sr_ihdr) != 24); diff --git a/tools/libxc/xc_sr_common.h b/tools/libxc/xc_sr_common.h index 2f33ccc..b1aa88e 100644 --- a/tools/libxc/xc_sr_common.h +++ b/tools/libxc/xc_sr_common.h @@ -392,6 +392,16 @@ static inline int write_record(struct xc_sr_context *ctx, int fd, int read_record(struct xc_sr_context *ctx, int fd, struct xc_sr_record *rec); /* + * Given a record of one of the page data types, validate it by: + * - checking its actual type against its specific expected type + * - sanity checking its actual length against its claimed length + * + * Returns 0 on success and non-0 on failure. + */ +int validate_pages_record(struct xc_sr_context *ctx, struct xc_sr_record *rec, + uint32_t expected_type); + +/* * This would ideally be private in restore.c, but is needed by * x86_pv_localise_page() if we receive pagetables frames ahead of the * contents of the frames they point at. diff --git a/tools/libxc/xc_sr_restore.c b/tools/libxc/xc_sr_restore.c index fc47a25..00fad7d 100644 --- a/tools/libxc/xc_sr_restore.c +++ b/tools/libxc/xc_sr_restore.c @@ -326,45 +326,21 @@ static int process_page_data(struct xc_sr_context *ctx, unsigned count, } /* - * Validate a PAGE_DATA record from the stream, and pass the results to - * process_page_data() to actually perform the legwork. + * Given a PAGE_DATA record, decode each packed entry into its encoded pfn and + * type, storing the results in the pfns and types buffers. + * + * Returns the number of pages of real data, or < 0 on error. */ -static int handle_page_data(struct xc_sr_context *ctx, struct xc_sr_record *rec) +static int decode_pages_record(struct xc_sr_context *ctx, + struct xc_sr_rec_pages_header *pages, + /* OUT */ xen_pfn_t *pfns, + /* OUT */ uint32_t *types) { xc_interface *xch = ctx->xch; - struct xc_sr_rec_pages_header *pages = rec->data; - unsigned i, pages_of_data = 0; - int rc = -1; - - xen_pfn_t *pfns = NULL, pfn; - uint32_t *types = NULL, type; - - if ( rec->length < sizeof(*pages) ) - { - ERROR("PAGE_DATA record truncated: length %u, min %zu", - rec->length, sizeof(*pages)); - goto err; - } - else if ( pages->count < 1 ) - { - ERROR("Expected at least 1 pfn in PAGE_DATA record"); - goto err; - } - else if ( rec->length < sizeof(*pages) + (pages->count * sizeof(uint64_t)) ) - { - ERROR("PAGE_DATA record (length %u) too short to contain %u" - " pfns worth of information", rec->length, pages->count); - goto err; - } - - pfns = malloc(pages->count * sizeof(*pfns)); - types = malloc(pages->count * sizeof(*types)); - if ( !pfns || !types ) - { - ERROR("Unable to allocate enough memory for %u pfns", - pages->count); - goto err; - } + unsigned int i; + int pages_of_data = 0; + xen_pfn_t pfn; + uint32_t type; for ( i = 0; i < pages->count; ++i ) { @@ -384,14 +360,51 @@ static int handle_page_data(struct xc_sr_context *ctx, struct xc_sr_record *rec) goto err; } else if ( type < XEN_DOMCTL_PFINFO_BROKEN ) - /* NOTAB and all L1 through L4 tables (including pinned) should - * have a page worth of data in the record. */ + /* NOTAB and all L1 through L4 tables (including pinned) require the + * migration of a page of real data. */ pages_of_data++; pfns[i] = pfn; types[i] = type; } + return pages_of_data; + + err: + return -1; +} + +/* + * Validate a PAGE_DATA record from the stream, and pass the results to + * process_page_data() to actually perform the legwork. + */ +static int handle_page_data(struct xc_sr_context *ctx, struct xc_sr_record *rec) +{ + xc_interface *xch = ctx->xch; + struct xc_sr_rec_pages_header *pages = rec->data; + int pages_of_data; + int rc = -1; + + xen_pfn_t *pfns = NULL; + uint32_t *types = NULL; + + rc = validate_pages_record(ctx, rec, REC_TYPE_PAGE_DATA); + if ( rc ) + goto err; + + pfns = malloc(pages->count * sizeof(*pfns)); + types = malloc(pages->count * sizeof(*types)); + if ( !pfns || !types ) + { + ERROR("Unable to allocate enough memory for %u pfns", + pages->count); + goto err; + } + + pages_of_data = decode_pages_record(ctx, pages, pfns, types); + if ( pages_of_data < 0 ) + goto err; + if ( rec->length != (sizeof(*pages) + (sizeof(uint64_t) * pages->count) + (PAGE_SIZE * pages_of_data)) ) -- 2.7.4 _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxxxxxxxxx https://lists.xenproject.org/mailman/listinfo/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |