// SPDX-License-Identifier: GPL-2.0-only1/*2* KVM binary statistics interface implementation3*4* Copyright 2021 Google LLC5*/67#include <linux/kvm_host.h>8#include <linux/kvm.h>9#include <linux/errno.h>10#include <linux/uaccess.h>1112/**13* kvm_stats_read() - Common function to read from the binary statistics14* file descriptor.15*16* @id: identification string of the stats17* @header: stats header for a vm or a vcpu18* @desc: start address of an array of stats descriptors for a vm or a vcpu19* @stats: start address of stats data block for a vm or a vcpu20* @size_stats: the size of stats data block pointed by @stats21* @user_buffer: start address of userspace buffer22* @size: requested read size from userspace23* @offset: the start position from which the content will be read for the24* corresponding vm or vcp file descriptor25*26* The file content of a vm/vcpu file descriptor is now defined as below:27* +-------------+28* | Header |29* +-------------+30* | id string |31* +-------------+32* | Descriptors |33* +-------------+34* | Stats Data |35* +-------------+36* Although this function allows userspace to read any amount of data (as long37* as in the limit) from any position, the typical usage would follow below38* steps:39* 1. Read header from offset 0. Get the offset of descriptors and stats data40* and some other necessary information. This is a one-time work for the41* lifecycle of the corresponding vm/vcpu stats fd.42* 2. Read id string from its offset. This is a one-time work for the lifecycle43* of the corresponding vm/vcpu stats fd.44* 3. Read descriptors from its offset and discover all the stats by parsing45* descriptors. This is a one-time work for the lifecycle of the46* corresponding vm/vcpu stats fd.47* 4. Periodically read stats data from its offset using pread.48*49* Return: the number of bytes that has been successfully read50*/51ssize_t kvm_stats_read(char *id, const struct kvm_stats_header *header,52const struct _kvm_stats_desc *desc,53void *stats, size_t size_stats,54char __user *user_buffer, size_t size, loff_t *offset)55{56ssize_t len;57ssize_t copylen;58ssize_t remain = size;59size_t size_desc;60size_t size_header;61void *src;62loff_t pos = *offset;63char __user *dest = user_buffer;6465size_header = sizeof(*header);66size_desc = header->num_desc * sizeof(*desc);6768len = KVM_STATS_NAME_SIZE + size_header + size_desc + size_stats - pos;69len = min(len, remain);70if (len <= 0)71return 0;72remain = len;7374/*75* Copy kvm stats header.76* The header is the first block of content userspace usually read out.77* The pos is 0 and the copylen and remain would be the size of header.78* The copy of the header would be skipped if offset is larger than the79* size of header. That usually happens when userspace reads stats80* descriptors and stats data.81*/82copylen = size_header - pos;83copylen = min(copylen, remain);84if (copylen > 0) {85src = (void *)header + pos;86if (copy_to_user(dest, src, copylen))87return -EFAULT;88remain -= copylen;89pos += copylen;90dest += copylen;91}9293/*94* Copy kvm stats header id string.95* The id string is unique for every vm/vcpu, which is stored in kvm96* and kvm_vcpu structure.97* The id string is part of the stat header from the perspective of98* userspace, it is usually read out together with previous constant99* header part and could be skipped for later descriptors and stats100* data readings.101*/102copylen = header->id_offset + KVM_STATS_NAME_SIZE - pos;103copylen = min(copylen, remain);104if (copylen > 0) {105src = id + pos - header->id_offset;106if (copy_to_user(dest, src, copylen))107return -EFAULT;108remain -= copylen;109pos += copylen;110dest += copylen;111}112113/*114* Copy kvm stats descriptors.115* The descriptors copy would be skipped in the typical case that116* userspace periodically read stats data, since the pos would be117* greater than the end address of descriptors118* (header->header.desc_offset + size_desc) causing copylen <= 0.119*/120copylen = header->desc_offset + size_desc - pos;121copylen = min(copylen, remain);122if (copylen > 0) {123src = (void *)desc + pos - header->desc_offset;124if (copy_to_user(dest, src, copylen))125return -EFAULT;126remain -= copylen;127pos += copylen;128dest += copylen;129}130131/* Copy kvm stats values */132copylen = header->data_offset + size_stats - pos;133copylen = min(copylen, remain);134if (copylen > 0) {135src = stats + pos - header->data_offset;136if (copy_to_user(dest, src, copylen))137return -EFAULT;138pos += copylen;139}140141*offset = pos;142return len;143}144145146