~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/virt/kvm/binary_stats.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * KVM binary statistics interface implementation
  4  *
  5  * Copyright 2021 Google LLC
  6  */
  7 
  8 #include <linux/kvm_host.h>
  9 #include <linux/kvm.h>
 10 #include <linux/errno.h>
 11 #include <linux/uaccess.h>
 12 
 13 /**
 14  * kvm_stats_read() - Common function to read from the binary statistics
 15  * file descriptor.
 16  *
 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
 26  *
 27  * The file content of a vm/vcpu file descriptor is now defined as below:
 28  * +-------------+
 29  * |   Header    |
 30  * +-------------+
 31  * |  id string  |
 32  * +-------------+
 33  * | Descriptors |
 34  * +-------------+
 35  * | Stats Data  |
 36  * +-------------+
 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
 39  * steps:
 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.
 49  *
 50  * Return: the number of bytes that has been successfully read
 51  */
 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)
 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 }
145 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php