git: 14e782708d37 - stable/14 - vmm: Fix compiling error with BHYVE_SNAPSHOT

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 20 Feb 2024 14:23:26 UTC
The branch stable/14 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=14e782708d375f19a8e19a4c7a42c5032f9997ee

commit 14e782708d375f19a8e19a4c7a42c5032f9997ee
Author:     Vitaliy Gusev <gusev.vitaliy@gmail.com>
AuthorDate: 2024-02-06 15:36:17 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-02-20 14:21:14 +0000

    vmm: Fix compiling error with BHYVE_SNAPSHOT
    
    The return values of copyin() and copyout() must be checked.
    vm_snapshot_buf_cmp() is unused by the kernel and was incorrectly
    implemented, so just remove it.
    
    Reviewed by:    markj
    Sponsored by:   vStack
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D43754
    
    (cherry picked from commit 7572fe89ada63719b558c6b844e2743cd3ff6b6a)
---
 sys/amd64/include/vmm_snapshot.h |  7 +++++--
 sys/amd64/vmm/vmm_snapshot.c     | 44 +++++++---------------------------------
 2 files changed, 12 insertions(+), 39 deletions(-)

diff --git a/sys/amd64/include/vmm_snapshot.h b/sys/amd64/include/vmm_snapshot.h
index 5ed00c71d58a..b39c342bf6d9 100644
--- a/sys/amd64/include/vmm_snapshot.h
+++ b/sys/amd64/include/vmm_snapshot.h
@@ -98,8 +98,6 @@ void vm_snapshot_buf_err(const char *bufname, const enum vm_snapshot_op op);
 int vm_snapshot_buf(void *data, size_t data_size,
     struct vm_snapshot_meta *meta);
 size_t vm_get_snapshot_size(struct vm_snapshot_meta *meta);
-int vm_snapshot_buf_cmp(void *data, size_t data_size,
-    struct vm_snapshot_meta *meta);
 
 #define	SNAPSHOT_BUF_OR_LEAVE(DATA, LEN, META, RES, LABEL)			\
 do {										\
@@ -113,6 +111,10 @@ do {										\
 #define	SNAPSHOT_VAR_OR_LEAVE(DATA, META, RES, LABEL)				\
 	SNAPSHOT_BUF_OR_LEAVE(&(DATA), sizeof(DATA), (META), (RES), LABEL)
 
+#ifndef _KERNEL
+int vm_snapshot_buf_cmp(void *data, size_t data_size,
+    struct vm_snapshot_meta *meta);
+
 /* compare the value in the meta buffer with the data */
 #define	SNAPSHOT_BUF_CMP_OR_LEAVE(DATA, LEN, META, RES, LABEL)			\
 do {										\
@@ -126,4 +128,5 @@ do {										\
 #define	SNAPSHOT_VAR_CMP_OR_LEAVE(DATA, META, RES, LABEL)			\
 	SNAPSHOT_BUF_CMP_OR_LEAVE(&(DATA), sizeof(DATA), (META), (RES), LABEL)
 
+#endif	/* _KERNEL */
 #endif
diff --git a/sys/amd64/vmm/vmm_snapshot.c b/sys/amd64/vmm/vmm_snapshot.c
index dae4b1001182..5f078397eb95 100644
--- a/sys/amd64/vmm/vmm_snapshot.c
+++ b/sys/amd64/vmm/vmm_snapshot.c
@@ -58,7 +58,7 @@ int
 vm_snapshot_buf(void *data, size_t data_size, struct vm_snapshot_meta *meta)
 {
 	struct vm_snapshot_buffer *buffer;
-	int op;
+	int op, error;
 
 	buffer = &meta->buffer;
 	op = meta->op;
@@ -69,11 +69,14 @@ vm_snapshot_buf(void *data, size_t data_size, struct vm_snapshot_meta *meta)
 	}
 
 	if (op == VM_SNAPSHOT_SAVE)
-		copyout(data, buffer->buf, data_size);
+		error = copyout(data, buffer->buf, data_size);
 	else if (op == VM_SNAPSHOT_RESTORE)
-		copyin(buffer->buf, data, data_size);
+		error = copyin(buffer->buf, data, data_size);
 	else
-		return (EINVAL);
+		error = EINVAL;
+
+	if (error)
+		return (error);
 
 	buffer->buf += data_size;
 	buffer->buf_rem -= data_size;
@@ -99,36 +102,3 @@ vm_get_snapshot_size(struct vm_snapshot_meta *meta)
 
 	return (length);
 }
-
-int
-vm_snapshot_buf_cmp(void *data, size_t data_size, struct vm_snapshot_meta *meta)
-{
-	struct vm_snapshot_buffer *buffer;
-	int op;
-	int ret;
-
-	buffer = &meta->buffer;
-	op = meta->op;
-
-	if (buffer->buf_rem < data_size) {
-		printf("%s: buffer too small\r\n", __func__);
-		ret = E2BIG;
-		goto done;
-	}
-
-	if (op == VM_SNAPSHOT_SAVE) {
-		ret = 0;
-		copyout(data, buffer->buf, data_size);
-	} else if (op == VM_SNAPSHOT_RESTORE) {
-		ret = memcmp(data, buffer->buf, data_size);
-	} else {
-		ret = EINVAL;
-		goto done;
-	}
-
-	buffer->buf += data_size;
-	buffer->buf_rem -= data_size;
-
-done:
-	return (ret);
-}