1 // SPDX-License-Identifier: GPL-2.0-only
3 * KVM binary statistics interface implementation
5 * Copyright 2021 Google LLC
8 #include <linux/kvm_host.h>
10 #include <linux/errno.h>
11 #include <linux/uaccess.h>
14 * kvm_stats_read() - Common function to read from the binary statistics
17 * @id: identification string of the stats
18 * @header: stats header for a vm or a vcpu
19 * @desc: start address of an array of stats descriptors for a vm or a vcpu
20 * @stats: start address of stats data block for a vm or a vcpu
21 * @size_stats: the size of stats data block pointed by @stats
22 * @user_buffer: start address of userspace buffer
23 * @size: requested read size from userspace
24 * @offset: the start position from which the content will be read for the
25 * corresponding vm or vcp file descriptor
27 * The file content of a vm/vcpu file descriptor is now defined as below:
37 * Although this function allows userspace to read any amount of data (as long
38 * as in the limit) from any position, the typical usage would follow below
40 * 1. Read header from offset 0. Get the offset of descriptors and stats data
41 * and some other necessary information. This is a one-time work for the
42 * lifecycle of the corresponding vm/vcpu stats fd.
43 * 2. Read id string from its offset. This is a one-time work for the lifecycle
44 * of the corresponding vm/vcpu stats fd.
45 * 3. Read descriptors from its offset and discover all the stats by parsing
46 * descriptors. This is a one-time work for the lifecycle of the
47 * corresponding vm/vcpu stats fd.
48 * 4. Periodically read stats data from its offset using pread.
50 * Return: the number of bytes that has been successfully read
52 ssize_t
kvm_stats_read(char *id
, const struct kvm_stats_header
*header
,
53 const struct _kvm_stats_desc
*desc
,
54 void *stats
, size_t size_stats
,
55 char __user
*user_buffer
, size_t size
, loff_t
*offset
)
59 ssize_t remain
= size
;
64 char __user
*dest
= user_buffer
;
66 size_header
= sizeof(*header
);
67 size_desc
= header
->num_desc
* sizeof(*desc
);
69 len
= KVM_STATS_NAME_SIZE
+ size_header
+ size_desc
+ size_stats
- pos
;
70 len
= min(len
, remain
);
76 * Copy kvm stats header.
77 * The header is the first block of content userspace usually read out.
78 * The pos is 0 and the copylen and remain would be the size of header.
79 * The copy of the header would be skipped if offset is larger than the
80 * size of header. That usually happens when userspace reads stats
81 * descriptors and stats data.
83 copylen
= size_header
- pos
;
84 copylen
= min(copylen
, remain
);
86 src
= (void *)header
+ pos
;
87 if (copy_to_user(dest
, src
, copylen
))
95 * Copy kvm stats header id string.
96 * The id string is unique for every vm/vcpu, which is stored in kvm
97 * and kvm_vcpu structure.
98 * The id string is part of the stat header from the perspective of
99 * userspace, it is usually read out together with previous constant
100 * header part and could be skipped for later descriptors and stats
103 copylen
= header
->id_offset
+ KVM_STATS_NAME_SIZE
- pos
;
104 copylen
= min(copylen
, remain
);
106 src
= id
+ pos
- header
->id_offset
;
107 if (copy_to_user(dest
, src
, copylen
))
115 * Copy kvm stats descriptors.
116 * The descriptors copy would be skipped in the typical case that
117 * userspace periodically read stats data, since the pos would be
118 * greater than the end address of descriptors
119 * (header->header.desc_offset + size_desc) causing copylen <= 0.
121 copylen
= header
->desc_offset
+ size_desc
- pos
;
122 copylen
= min(copylen
, remain
);
124 src
= (void *)desc
+ pos
- header
->desc_offset
;
125 if (copy_to_user(dest
, src
, copylen
))
132 /* Copy kvm stats values */
133 copylen
= header
->data_offset
+ size_stats
- pos
;
134 copylen
= min(copylen
, remain
);
136 src
= stats
+ pos
- header
->data_offset
;
137 if (copy_to_user(dest
, src
, copylen
))