mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Michael Kelley <mhklinux@outlook.com>
To: Kameron Carr <kameroncarr@linux.microsoft.com>,
	Haiyang Zhang <haiyangz@microsoft.com>,
	Wei Liu <wei.liu@kernel.org>, Dexuan Cui <decui@microsoft.com>,
	Michael Kelley <mhklinux@outlook.com>
Cc: "linux-hyperv@vger.kernel.org" <linux-hyperv@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH v2 3/5] Drivers: hv: vmbus: Compute ring byte counts from a caller-held snapshot
Date: Tue, 20 Oct 2026 18:04:10 +0000	[thread overview]
Message-ID: <MN2PR02MB6863B5BB274EC29FB15862A5D4672@MN2PR02MB6863.namprd02.prod.outlook.com> (raw)
In-Reply-To: <20261019230950.1573677-4-kameroncarr@linux.microsoft.com>

From: Kameron Carr <kameroncarr@linux.microsoft.com> Sent: Monday, October 19, 2026 4:10 PM
> 
> 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>

> ---
>  drivers/hv/ring_buffer.c | 68 ++++++++++++++++--------------------------------
>  include/linux/hyperv.h   | 50 +++++++++++++++++++++++++++--------
>  2 files changed, 61 insertions(+), 57 deletions(-)
> 
> diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c
> index 7f466c5..cda957d 100644
> --- a/drivers/hv/ring_buffer.c
> +++ b/drivers/hv/ring_buffer.c
> @@ -107,35 +107,12 @@ static u32 hv_copyto_ringbuffer(
>  	return start_write_offset;
>  }
> 
> -/*
> - *
> - * hv_get_ringbuffer_availbytes()
> - *
> - * Get number of bytes available to read and to write to
> - * for the specified ring buffer
> - */
> -static void
> -hv_get_ringbuffer_availbytes(const struct hv_ring_buffer_info *rbi,
> -			     u32 *read, u32 *write)
> -{
> -	u32 read_loc, write_loc, dsize;
> -
> -	/* Capture the read/write indices before they changed */
> -	read_loc = READ_ONCE(rbi->ring_buffer->read_index);
> -	write_loc = READ_ONCE(rbi->ring_buffer->write_index);
> -	dsize = rbi->ring_datasize;
> -
> -	*write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
> -		read_loc - write_loc;
> -	*read = dsize - *write;
> -}
> -
>  /* Get various debug metrics for the specified ring buffer. */
>  int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
>  				struct hv_ring_buffer_debug_info *debug_info)
>  {
> -	u32 bytes_avail_towrite;
> -	u32 bytes_avail_toread;
> +	u32 read_index;
> +	u32 write_index;
> 
>  	mutex_lock(&ring_info->ring_buffer_mutex);
> 
> @@ -144,13 +121,14 @@ int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
>  		return -EINVAL;
>  	}
> 
> -	hv_get_ringbuffer_availbytes(ring_info,
> -				     &bytes_avail_toread,
> -				     &bytes_avail_towrite);
> -	debug_info->bytes_avail_toread = bytes_avail_toread;
> -	debug_info->bytes_avail_towrite = bytes_avail_towrite;
> -	debug_info->current_read_index = ring_info->ring_buffer->read_index;
> -	debug_info->current_write_index = ring_info->ring_buffer->write_index;
> +	read_index = READ_ONCE(ring_info->ring_buffer->read_index);
> +	write_index = READ_ONCE(ring_info->ring_buffer->write_index);
> +	debug_info->bytes_avail_toread =
> +		hv_ringbuffer_avail_read(ring_info, read_index, write_index);
> +	debug_info->bytes_avail_towrite =
> +		hv_ringbuffer_avail_write(ring_info, read_index, write_index);
> +	debug_info->current_read_index = read_index;
> +	debug_info->current_write_index = write_index;
>  	debug_info->current_interrupt_mask
>  		= ring_info->ring_buffer->interrupt_mask;
>  	mutex_unlock(&ring_info->ring_buffer_mutex);
> @@ -282,8 +260,8 @@ int hv_ringbuffer_write(struct vmbus_channel *channel,
> 
>  	read_index = READ_ONCE(outring_info->ring_buffer->read_index);
>  	old_write = READ_ONCE(outring_info->ring_buffer->write_index);
> -	if (unlikely(read_index >= outring_info->ring_datasize ||
> -		     old_write >= outring_info->ring_datasize)) {
> +	if (unlikely(hv_ringbuffer_indices_invalid(outring_info, read_index,
> +						   old_write))) {
>  		spin_unlock_irqrestore(&outring_info->ring_lock, flags);
>  		pr_err_ratelimited("outbound ring indices out of range: relid %u read %u write %u size %u\n",
>  				   channel->offermsg.child_relid, read_index,
> @@ -291,9 +269,8 @@ int hv_ringbuffer_write(struct vmbus_channel *channel,
>  		return -EIO;
>  	}
> 
> -	bytes_avail_towrite = old_write >= read_index ?
> -		outring_info->ring_datasize - (old_write - read_index) :
> -		read_index - old_write;
> +	bytes_avail_towrite = hv_ringbuffer_avail_write(outring_info, read_index,
> +							old_write);
> 
>  	/*
>  	 * If there is only room for the packet, assume it is full.
> @@ -445,10 +422,7 @@ static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
>  	 */
>  	write_loc = virt_load_acquire(&rbi->ring_buffer->write_index);
> 
> -	if (write_loc >= priv_read_loc)
> -		return write_loc - priv_read_loc;
> -	else
> -		return (rbi->ring_datasize - priv_read_loc) + write_loc;
> +	return hv_ringbuffer_avail_read(rbi, priv_read_loc, write_loc);
>  }
> 
>  /*
> @@ -568,6 +542,7 @@ void hv_pkt_iter_close(struct vmbus_channel *channel)
>  {
>  	struct hv_ring_buffer_info *rbi = &channel->inbound;
>  	u32 curr_write_sz, pending_sz, bytes_read, start_read_index;
> +	u32 write_index;
> 
>  	/*
>  	 * Make sure all reads are done before we update the read index since
> @@ -607,11 +582,13 @@ void hv_pkt_iter_close(struct vmbus_channel *channel)
>  		return;
> 
>  	/*
> -	 * Ensure the read of write_index in hv_get_bytes_to_write()
> -	 * happens after the read of pending_send_sz.
> +	 * Ensure the read of write_index happens after the read of
> +	 * pending_send_sz.
>  	 */
>  	virt_rmb();
> -	curr_write_sz = hv_get_bytes_to_write(rbi);
> +	write_index = READ_ONCE(rbi->ring_buffer->write_index);
> +	curr_write_sz = hv_ringbuffer_avail_write(rbi, rbi->priv_read_index,
> +						  write_index);
>  	bytes_read = hv_pkt_iter_bytes_read(rbi, start_read_index);
> 
>  	/*
> @@ -627,8 +604,7 @@ void hv_pkt_iter_close(struct vmbus_channel *channel)
>  	 * Exactly filling the ring buffer is treated as "not enough
>  	 * space". The ring buffer always must have at least one byte
>  	 * empty so the empty and full conditions are distinguishable.
> -	 * hv_get_bytes_to_write() doesn't fully tell the truth in
> -	 * this regard.
> +	 * curr_write_sz doesn't fully tell the truth in this regard.
>  	 *
>  	 * So first check if we were in the "enough free space" state
>  	 * before we began the iteration. If so, the host was not
> diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h
> index 5c65820..09f25ad 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(
> 
> --
> 2.45.4


  reply	other threads:[~2026-10-20 18:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-19 23:09 [PATCH v2 0/5] Drivers: hv: vmbus: Harden the ring buffer against a malicious host Kameron Carr
2026-10-19 23:09 ` [PATCH v2 1/5] Drivers: hv: vmbus: Bounds check the shared ring buffer indices Kameron Carr
2026-10-19 23:09 ` [PATCH v2 2/5] Drivers: hv: vmbus: Use *_ONCE() to access " Kameron Carr
2026-10-19 23:09 ` [PATCH v2 3/5] Drivers: hv: vmbus: Compute ring byte counts from a caller-held snapshot Kameron Carr
2026-10-20 18:04   ` Michael Kelley [this message]
2026-10-19 23:09 ` [PATCH v2 4/5] Drivers: hv: vmbus: Keep the ring byte counts sane for a bad index Kameron Carr
2026-10-20 18:04   ` Michael Kelley
2026-10-19 23:09 ` [PATCH v2 5/5] Documentation: hyperv: Describe validation of the ring buffer indices Kameron Carr
2026-10-20 18:04   ` Michael Kelley
2026-10-23 18:23 ` [PATCH v2 0/5] Drivers: hv: vmbus: Harden the ring buffer against a malicious host Mitchell Levy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=MN2PR02MB6863B5BB274EC29FB15862A5D4672@MN2PR02MB6863.namprd02.prod.outlook.com \
    --to=mhklinux@outlook.com \
    --cc=decui@microsoft.com \
    --cc=haiyangz@microsoft.com \
    --cc=kameroncarr@linux.microsoft.com \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=wei.liu@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®