From nobody Mon Jun 19 05:57:58 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QkzZb24PPz4fCFq; Mon, 19 Jun 2023 05:57:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QkzZZ2k8pz4KNr; Mon, 19 Jun 2023 05:57:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1687154278; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2qnsUeCOGOW3pmRUpnSdq/THJTCFxZKjdSABIXKJzh0=; b=fEIBxgFepHjqCrTKBm5Ciwm9plf01N22/6jYMfV3LxwrxGN3zYvE1y5JLmavX6jeoiq2zp 8Ifoc1idfO2gJn9Hgjgi3tZ2dN4u35CN3pI6RwY+OHhpcNxK5Slfyio1qjwZCnIJA9PRH+ g2QbTCBH/6A4ToUYPMrsJY7SD5Qv3dXYydiV4oOZKTJcKzUYEb/W0meZG5wYxPPToQl3Dl GenOY7oFNEUQG41yIV8TjutYYO1NUiXwCe5L5PpOrAY419oqTq+6mCpSf1Mg5LCQCnXQnk R+aiTnzpT4n0zNnhEH56WbQ2IY1ochymtEb1fshT92/+MrndbmkwOR2FVQVjew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1687154278; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2qnsUeCOGOW3pmRUpnSdq/THJTCFxZKjdSABIXKJzh0=; b=MFHdidVbuA6OIU0uUr5cfp5P4SNQAeDZqXovXSN4l6G18nLhUNRXZklkjmjjPyPbklNvZL mxhiV+OgFlRTpsIk2mwIskbxLH9iuUenQBIA/xO5tZLzrE1Rx6Dw3bpKN/jGN0AUnNfYoD RQs9TGhjaIolI6On4Eyycjm65TCKlXPsCW3+8HmRjydIrCbwuFrOt9cZeBxagC/tqkIARM x5fDoRvPoicTPcX7BpZCZMdD6/0Bvr0AB9zNUP/7wzHm8b1UlB2RpWcBK2OkzE46PDz/DC eBOne7ttj7DKozNxKzXaeOIysn8g7mFjAQ/lVEwGpEF82ul4lRQw7F+Xd5B+9A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1687154278; a=rsa-sha256; cv=none; b=fneKdeOlqdTJ0JJOpWYHV+O0MtvEJxRRjYDgqfy0ciTlSKJ6XPd2vdIWLn3LVQlb0F5+UJ Cp+xWn0sJG+Xo8BfTYePZAX9g6fixuvhZv2FmBBtZBbp4LjmYBjJwdJS+6SttkL8Gb3hOF aKFfoH9Tn1VfbAL539+JwNm2KptIEZsQxDVqXEJzXC+Pk/TT3uo3XRkHJvPBloaZfXhaCR r2LoVWfTImKktcKsr+VWx4/5Pf+h2TDNuvUPvxZ9M0anszhWMFMuneXp2PRavDWamcsdqi +4/3qnO1uo4i7iqy8M4FXCWrYIs9xaMkizSTgeicOj3djmDzwt3ZSqsaoA+liw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4QkzZZ14YSz15fS; Mon, 19 Jun 2023 05:57:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 35J5vwDi025967; Mon, 19 Jun 2023 05:57:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 35J5vwbh025966; Mon, 19 Jun 2023 05:57:58 GMT (envelope-from git) Date: Mon, 19 Jun 2023 05:57:58 GMT Message-Id: <202306190557.35J5vwbh025966@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: =?utf-8?Q?Corvin=20K=C3=B6hne?= Subject: git: ee5023f3c2f4 - main - bhyve: simplify restore of kernel structs List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: corvink X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: ee5023f3c2f46987fd051de2696c1743e4226a37 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by corvink: URL: https://cgit.FreeBSD.org/src/commit/?id=ee5023f3c2f46987fd051de2696c1743e4226a37 commit ee5023f3c2f46987fd051de2696c1743e4226a37 Author: Vitaliy Gusev AuthorDate: 2023-05-15 14:27:29 +0000 Commit: Corvin Köhne CommitDate: 2023-06-19 05:51:33 +0000 bhyve: simplify restore of kernel structs Both devices and kernel struct can use the same 'lookup_dev' function instead of having duplicated code. Reviewed by: corvink, rew MFC after: 1 week Sponsored by: vStack Differential Revision: https://reviews.freebsd.org/D40105 --- usr.sbin/bhyve/snapshot.c | 149 ++++++++++++---------------------------------- 1 file changed, 39 insertions(+), 110 deletions(-) diff --git a/usr.sbin/bhyve/snapshot.c b/usr.sbin/bhyve/snapshot.c index 7e635c13ef31..dfe341c598b8 100644 --- a/usr.sbin/bhyve/snapshot.c +++ b/usr.sbin/bhyve/snapshot.c @@ -415,51 +415,6 @@ do { \ } \ } while(0) -static void * -lookup_struct(enum snapshot_req struct_id, struct restore_state *rstate, - size_t *struct_size) -{ - const ucl_object_t *structs = NULL, *obj = NULL; - ucl_object_iter_t it = NULL; - int64_t snapshot_req, size, file_offset; - - structs = ucl_object_lookup(rstate->meta_root_obj, JSON_KERNEL_ARR_KEY); - if (structs == NULL) { - fprintf(stderr, "Failed to find '%s' object.\n", - JSON_KERNEL_ARR_KEY); - return (NULL); - } - - if (ucl_object_type(structs) != UCL_ARRAY) { - fprintf(stderr, "Object '%s' is not an array.\n", - JSON_KERNEL_ARR_KEY); - return (NULL); - } - - while ((obj = ucl_object_iterate(structs, &it, true)) != NULL) { - snapshot_req = -1; - JSON_GET_INT_OR_RETURN(JSON_SNAPSHOT_REQ_KEY, obj, - &snapshot_req, NULL); - assert(snapshot_req >= 0); - if ((enum snapshot_req) snapshot_req == struct_id) { - JSON_GET_INT_OR_RETURN(JSON_SIZE_KEY, obj, - &size, NULL); - assert(size >= 0); - - JSON_GET_INT_OR_RETURN(JSON_FILE_OFFSET_KEY, obj, - &file_offset, NULL); - assert(file_offset >= 0); - assert((uint64_t)file_offset + size <= - rstate->kdata_len); - - *struct_size = (size_t)size; - return ((uint8_t *)rstate->kdata_map + file_offset); - } - } - - return (NULL); -} - static void * lookup_check_dev(const char *dev_name, struct restore_state *rstate, const ucl_object_t *obj, size_t *data_size) @@ -488,15 +443,15 @@ lookup_check_dev(const char *dev_name, struct restore_state *rstate, return (NULL); } -static void* -lookup_dev(const char *dev_name, struct restore_state *rstate, - size_t *data_size) +static void * +lookup_dev(const char *dev_name, const char *key, struct restore_state *rstate, + size_t *data_size) { const ucl_object_t *devs = NULL, *obj = NULL; ucl_object_iter_t it = NULL; void *ret; - devs = ucl_object_lookup(rstate->meta_root_obj, JSON_DEV_ARR_KEY); + devs = ucl_object_lookup(rstate->meta_root_obj, key); if (devs == NULL) { fprintf(stderr, "Failed to find '%s' object.\n", JSON_DEV_ARR_KEY); @@ -861,67 +816,42 @@ restore_vm_mem(struct vmctx *ctx, struct restore_state *rstate) return (0); } -static int -vm_restore_kern_struct(struct vmctx *ctx, struct restore_state *rstate, - const struct vm_snapshot_kern_info *info) +int +vm_restore_kern_structs(struct vmctx *ctx, struct restore_state *rstate) { - void *struct_ptr; - size_t struct_size; - int ret; - struct vm_snapshot_meta *meta; - - struct_ptr = lookup_struct(info->req, rstate, &struct_size); - if (struct_ptr == NULL) { - fprintf(stderr, "%s: Failed to lookup struct %s\r\n", - __func__, info->struct_name); - ret = -1; - goto done; - } + for (unsigned i = 0; i < nitems(snapshot_kern_structs); i++) { + const struct vm_snapshot_kern_info *info; + struct vm_snapshot_meta *meta; + void *data; + size_t size; - if (struct_size == 0) { - fprintf(stderr, "%s: Kernel struct size was 0 for: %s\r\n", - __func__, info->struct_name); - ret = -1; - goto done; - } + info = &snapshot_kern_structs[i]; + data = lookup_dev(info->struct_name, JSON_KERNEL_ARR_KEY, rstate, &size); + if (data == NULL) + errx(EX_DATAERR, "Cannot find kern struct %s", + info->struct_name); - meta = &(struct vm_snapshot_meta) { - .dev_name = info->struct_name, - .dev_req = info->req, - - .buffer.buf_start = struct_ptr, - .buffer.buf_size = struct_size, + if (size == 0) + errx(EX_DATAERR, "data with zero size for %s", + info->struct_name); - .buffer.buf = struct_ptr, - .buffer.buf_rem = struct_size, + meta = &(struct vm_snapshot_meta) { + .dev_name = info->struct_name, + .dev_req = info->req, - .op = VM_SNAPSHOT_RESTORE, - }; + .buffer.buf_start = data, + .buffer.buf_size = size, - ret = vm_snapshot_req(ctx, meta); - if (ret != 0) { - fprintf(stderr, "%s: Failed to restore struct: %s\r\n", - __func__, info->struct_name); - goto done; - } + .buffer.buf = data, + .buffer.buf_rem = size, -done: - return (ret); -} - -int -vm_restore_kern_structs(struct vmctx *ctx, struct restore_state *rstate) -{ - size_t i; - int ret; + .op = VM_SNAPSHOT_RESTORE, + }; - for (i = 0; i < nitems(snapshot_kern_structs); i++) { - ret = vm_restore_kern_struct(ctx, rstate, - &snapshot_kern_structs[i]); - if (ret != 0) - return (ret); + if (vm_snapshot_req(ctx, meta)) + err(EX_DATAERR, "Failed to restore %s", + info->struct_name); } - return (0); } @@ -934,7 +864,8 @@ vm_restore_user_dev(struct restore_state *rstate, int ret; struct vm_snapshot_meta *meta; - dev_ptr = lookup_dev(info->dev_name, rstate, &dev_size); + dev_ptr = lookup_dev(info->dev_name, JSON_DEV_ARR_KEY, rstate, + &dev_size); if (dev_ptr == NULL) { fprintf(stderr, "Failed to lookup dev: %s\r\n", info->dev_name); fprintf(stderr, "Continuing the restore/migration process\r\n"); @@ -1027,7 +958,7 @@ vm_resume_user_devs(void) } static int -vm_snapshot_kern_struct(struct vmctx *ctx, int data_fd, xo_handle_t *xop, +vm_save_kern_struct(struct vmctx *ctx, int data_fd, xo_handle_t *xop, const char *array_key, struct vm_snapshot_meta *meta, off_t *offset) { int ret; @@ -1054,9 +985,8 @@ vm_snapshot_kern_struct(struct vmctx *ctx, int data_fd, xo_handle_t *xop, /* Write metadata. */ xo_open_instance_h(xop, array_key); - xo_emit_h(xop, "{:debug_name/%s}\n", meta->dev_name); - xo_emit_h(xop, "{:" JSON_SNAPSHOT_REQ_KEY "/%d}\n", - meta->dev_req); + xo_emit_h(xop, "{:" JSON_SNAPSHOT_REQ_KEY "/%s}\n", + meta->dev_name); xo_emit_h(xop, "{:" JSON_SIZE_KEY "/%lu}\n", data_size); xo_emit_h(xop, "{:" JSON_FILE_OFFSET_KEY "/%lu}\n", *offset); xo_close_instance_h(xop, JSON_KERNEL_ARR_KEY); @@ -1068,7 +998,7 @@ done: } static int -vm_snapshot_kern_structs(struct vmctx *ctx, int data_fd, xo_handle_t *xop) +vm_save_kern_structs(struct vmctx *ctx, int data_fd, xo_handle_t *xop) { int ret, error; size_t buf_size, i, offset; @@ -1102,7 +1032,7 @@ vm_snapshot_kern_structs(struct vmctx *ctx, int data_fd, xo_handle_t *xop) meta->buffer.buf = meta->buffer.buf_start; meta->buffer.buf_rem = meta->buffer.buf_size; - ret = vm_snapshot_kern_struct(ctx, data_fd, xop, + ret = vm_save_kern_struct(ctx, data_fd, xop, JSON_DEV_ARR_KEY, meta, &offset); if (ret != 0) { error = -1; @@ -1385,8 +1315,7 @@ vm_checkpoint(struct vmctx *ctx, int fddir, const char *checkpoint_file, goto done; } - - ret = vm_snapshot_kern_structs(ctx, kdata_fd, xop); + ret = vm_save_kern_structs(ctx, kdata_fd, xop); if (ret != 0) { fprintf(stderr, "Failed to snapshot vm kernel data.\n"); error = -1;