KVM
Functions
binary_stats.c File Reference
#include <linux/kvm_host.h>
#include <linux/kvm.h>
#include <linux/errno.h>
#include <linux/uaccess.h>
Include dependency graph for binary_stats.c:

Go to the source code of this file.

Functions

ssize_t kvm_stats_read (char *id, const struct kvm_stats_header *header, const struct _kvm_stats_desc *desc, void *stats, size_t size_stats, char __user *user_buffer, size_t size, loff_t *offset)
 

Function Documentation

◆ kvm_stats_read()

ssize_t kvm_stats_read ( char *  id,
const struct kvm_stats_header *  header,
const struct _kvm_stats_desc *  desc,
void *  stats,
size_t  size_stats,
char __user *  user_buffer,
size_t  size,
loff_t *  offset 
)

kvm_stats_read() - Common function to read from the binary statistics file descriptor.

@id: identification string of the stats @header: stats header for a vm or a vcpu @desc: start address of an array of stats descriptors for a vm or a vcpu @stats: start address of stats data block for a vm or a vcpu @size_stats: the size of stats data block pointed by @stats @user_buffer: start address of userspace buffer @size: requested read size from userspace @offset: the start position from which the content will be read for the corresponding vm or vcp file descriptor

The file content of a vm/vcpu file descriptor is now defined as below: +----------—+ | Header | +----------—+ | id string | +----------—+ | Descriptors | +----------—+ | Stats Data | +----------—+ Although this function allows userspace to read any amount of data (as long as in the limit) from any position, the typical usage would follow below steps:

  1. Read header from offset 0. Get the offset of descriptors and stats data and some other necessary information. This is a one-time work for the lifecycle of the corresponding vm/vcpu stats fd.
  2. Read id string from its offset. This is a one-time work for the lifecycle of the corresponding vm/vcpu stats fd.
  3. Read descriptors from its offset and discover all the stats by parsing descriptors. This is a one-time work for the lifecycle of the corresponding vm/vcpu stats fd.
  4. Periodically read stats data from its offset using pread.

Return: the number of bytes that has been successfully read

Definition at line 52 of file binary_stats.c.

56 {
57  ssize_t len;
58  ssize_t copylen;
59  ssize_t remain = size;
60  size_t size_desc;
61  size_t size_header;
62  void *src;
63  loff_t pos = *offset;
64  char __user *dest = user_buffer;
65 
66  size_header = sizeof(*header);
67  size_desc = header->num_desc * sizeof(*desc);
68 
69  len = KVM_STATS_NAME_SIZE + size_header + size_desc + size_stats - pos;
70  len = min(len, remain);
71  if (len <= 0)
72  return 0;
73  remain = len;
74 
75  /*
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.
82  */
83  copylen = size_header - pos;
84  copylen = min(copylen, remain);
85  if (copylen > 0) {
86  src = (void *)header + pos;
87  if (copy_to_user(dest, src, copylen))
88  return -EFAULT;
89  remain -= copylen;
90  pos += copylen;
91  dest += copylen;
92  }
93 
94  /*
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
101  * data readings.
102  */
103  copylen = header->id_offset + KVM_STATS_NAME_SIZE - pos;
104  copylen = min(copylen, remain);
105  if (copylen > 0) {
106  src = id + pos - header->id_offset;
107  if (copy_to_user(dest, src, copylen))
108  return -EFAULT;
109  remain -= copylen;
110  pos += copylen;
111  dest += copylen;
112  }
113 
114  /*
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.
120  */
121  copylen = header->desc_offset + size_desc - pos;
122  copylen = min(copylen, remain);
123  if (copylen > 0) {
124  src = (void *)desc + pos - header->desc_offset;
125  if (copy_to_user(dest, src, copylen))
126  return -EFAULT;
127  remain -= copylen;
128  pos += copylen;
129  dest += copylen;
130  }
131 
132  /* Copy kvm stats values */
133  copylen = header->data_offset + size_stats - pos;
134  copylen = min(copylen, remain);
135  if (copylen > 0) {
136  src = stats + pos - header->data_offset;
137  if (copy_to_user(dest, src, copylen))
138  return -EFAULT;
139  pos += copylen;
140  }
141 
142  *offset = pos;
143  return len;
144 }
size_t size
Definition: gen-hyprel.c:133
Here is the caller graph for this function: