summaryrefslogtreecommitdiff
path: root/include/linux/hyperv.h
diff options
context:
space:
mode:
authorKameron Carr <kameroncarr@linux.microsoft.com>2026-10-19 16:09:48 -0700
committerWei Liu <wei.liu@kernel.org>2026-10-21 00:00:29 -0700
commita8e60890cb86a181d8216ac7e60873ba9d7d2ff9 (patch)
tree80ed5622a9829cfe91df363e97b33be7905d2676 /include/linux/hyperv.h
parent10e2eba7ccb507af62bfff72687c8c559fa4d689 (diff)
downloadlinux-a8e60890cb86a181d8216ac7e60873ba9d7d2ff9.tar.gz
linux-a8e60890cb86a181d8216ac7e60873ba9d7d2ff9.zip
Drivers: hv: vmbus: Compute ring byte counts from a caller-held snapshot
hv_get_ringbuffer_availbytes() reads the indices directly, so a caller that also wants the index values has to read them a second time. The host can change them in between, resulting in the byte counts and the indices reflecting two different states of the ring. Replace it with hv_ringbuffer_avail_write() and hv_ringbuffer_avail_read(), which take the indices as arguments and return a single count. They are separate because most callers want only one of the two values. Use them in hv_ringbuffer_write() in place of the open-coded equivalent. hv_get_bytes_to_read(), hv_get_bytes_to_write() and hv_pkt_iter_avail() duplicated the same arithmetic, so put the helpers in include/linux/hyperv.h and use them there too. Add a hv_ringbuffer_indices_invalid() helper that bounds checks a pair of read and write indices, and use it for the check in hv_ringbuffer_write(). Convert the remaining callers to use a single snapshot: - hv_ringbuffer_get_debuginfo() now reports the indices and the computed byte counts from the same snapshot. - hv_pkt_iter_close() computes the free space from priv_read_index instead of re-reading the shared read index. The two agree unless a misbehaving host has rewritten the value. No functional change intended for a well-behaved host. Signed-off-by: Kameron Carr <kameroncarr@linux.microsoft.com> Reviewed-by: Michael Kelley <mhklinux@outlook.com> Signed-off-by: Wei Liu <wei.liu@kernel.org>
Diffstat (limited to 'include/linux/hyperv.h')
-rw-r--r--include/linux/hyperv.h50
1 files changed, 39 insertions, 11 deletions
diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h
index 5c6582085b16..09f25ade88b0 100644
--- a/include/linux/hyperv.h
+++ b/include/linux/hyperv.h
@@ -209,31 +209,59 @@ struct hv_ring_buffer_info {
};
+/*
+ * The indices live in memory shared with the untrusted host, so check a
+ * snapshot of them before using it as an offset or to compute a byte count.
+ */
+static inline bool
+hv_ringbuffer_indices_invalid(const struct hv_ring_buffer_info *rbi,
+ u32 read_loc, u32 write_loc)
+{
+ return read_loc >= rbi->ring_datasize ||
+ write_loc >= rbi->ring_datasize;
+}
+
+/*
+ * Byte counts for a caller-supplied snapshot of the indices, so that the
+ * counts and the indices the caller goes on to use describe one state of the
+ * ring.
+ */
+static inline u32
+hv_ringbuffer_avail_write(const struct hv_ring_buffer_info *rbi,
+ u32 read_loc, u32 write_loc)
+{
+ u32 dsize = rbi->ring_datasize;
+
+ return write_loc >= read_loc ? dsize - (write_loc - read_loc) :
+ read_loc - write_loc;
+}
+
+static inline u32
+hv_ringbuffer_avail_read(const struct hv_ring_buffer_info *rbi,
+ u32 read_loc, u32 write_loc)
+{
+ return rbi->ring_datasize -
+ hv_ringbuffer_avail_write(rbi, read_loc, write_loc);
+}
+
static inline u32 hv_get_bytes_to_read(const struct hv_ring_buffer_info *rbi)
{
- u32 read_loc, write_loc, dsize, read;
+ u32 read_loc, write_loc;
- dsize = rbi->ring_datasize;
read_loc = READ_ONCE(rbi->ring_buffer->read_index);
write_loc = READ_ONCE(rbi->ring_buffer->write_index);
- read = write_loc >= read_loc ? (write_loc - read_loc) :
- (dsize - read_loc) + write_loc;
-
- return read;
+ return hv_ringbuffer_avail_read(rbi, read_loc, write_loc);
}
static inline u32 hv_get_bytes_to_write(const struct hv_ring_buffer_info *rbi)
{
- u32 read_loc, write_loc, dsize, write;
+ u32 read_loc, write_loc;
- dsize = rbi->ring_datasize;
read_loc = READ_ONCE(rbi->ring_buffer->read_index);
write_loc = READ_ONCE(rbi->ring_buffer->write_index);
- write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
- read_loc - write_loc;
- return write;
+ return hv_ringbuffer_avail_write(rbi, read_loc, write_loc);
}
static inline u32 hv_get_avail_to_write_percent(