| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * |
|---|
| 3 | 4 | * Copyright (c) 2009, Microsoft Corporation. |
|---|
| 4 | | - * |
|---|
| 5 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 6 | | - * under the terms and conditions of the GNU General Public License, |
|---|
| 7 | | - * version 2, as published by the Free Software Foundation. |
|---|
| 8 | | - * |
|---|
| 9 | | - * This program is distributed in the hope it will be useful, but WITHOUT |
|---|
| 10 | | - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|---|
| 11 | | - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
|---|
| 12 | | - * more details. |
|---|
| 13 | | - * |
|---|
| 14 | | - * You should have received a copy of the GNU General Public License along with |
|---|
| 15 | | - * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 16 | | - * Place - Suite 330, Boston, MA 02111-1307 USA. |
|---|
| 17 | 5 | * |
|---|
| 18 | 6 | * Authors: |
|---|
| 19 | 7 | * Haiyang Zhang <haiyangz@microsoft.com> |
|---|
| 20 | 8 | * Hank Janssen <hjanssen@microsoft.com> |
|---|
| 21 | 9 | * K. Y. Srinivasan <kys@microsoft.com> |
|---|
| 22 | | - * |
|---|
| 23 | 10 | */ |
|---|
| 24 | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
|---|
| 25 | 12 | |
|---|
| .. | .. |
|---|
| 74 | 61 | * This is the only case we need to signal when the |
|---|
| 75 | 62 | * ring transitions from being empty to non-empty. |
|---|
| 76 | 63 | */ |
|---|
| 77 | | - if (old_write == READ_ONCE(rbi->ring_buffer->read_index)) |
|---|
| 64 | + if (old_write == READ_ONCE(rbi->ring_buffer->read_index)) { |
|---|
| 65 | + ++channel->intr_out_empty; |
|---|
| 78 | 66 | vmbus_setevent(channel); |
|---|
| 67 | + } |
|---|
| 79 | 68 | } |
|---|
| 80 | 69 | |
|---|
| 81 | 70 | /* Get the next write location for the specified ring buffer. */ |
|---|
| .. | .. |
|---|
| 164 | 153 | } |
|---|
| 165 | 154 | |
|---|
| 166 | 155 | /* Get various debug metrics for the specified ring buffer. */ |
|---|
| 167 | | -int hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info, |
|---|
| 156 | +int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info, |
|---|
| 168 | 157 | struct hv_ring_buffer_debug_info *debug_info) |
|---|
| 169 | 158 | { |
|---|
| 170 | 159 | u32 bytes_avail_towrite; |
|---|
| 171 | 160 | u32 bytes_avail_toread; |
|---|
| 172 | 161 | |
|---|
| 173 | | - if (!ring_info->ring_buffer) |
|---|
| 162 | + mutex_lock(&ring_info->ring_buffer_mutex); |
|---|
| 163 | + |
|---|
| 164 | + if (!ring_info->ring_buffer) { |
|---|
| 165 | + mutex_unlock(&ring_info->ring_buffer_mutex); |
|---|
| 174 | 166 | return -EINVAL; |
|---|
| 167 | + } |
|---|
| 175 | 168 | |
|---|
| 176 | 169 | hv_get_ringbuffer_availbytes(ring_info, |
|---|
| 177 | 170 | &bytes_avail_toread, |
|---|
| .. | .. |
|---|
| 182 | 175 | debug_info->current_write_index = ring_info->ring_buffer->write_index; |
|---|
| 183 | 176 | debug_info->current_interrupt_mask |
|---|
| 184 | 177 | = ring_info->ring_buffer->interrupt_mask; |
|---|
| 178 | + mutex_unlock(&ring_info->ring_buffer_mutex); |
|---|
| 179 | + |
|---|
| 185 | 180 | return 0; |
|---|
| 186 | 181 | } |
|---|
| 187 | 182 | EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo); |
|---|
| 183 | + |
|---|
| 184 | +/* Initialize a channel's ring buffer info mutex locks */ |
|---|
| 185 | +void hv_ringbuffer_pre_init(struct vmbus_channel *channel) |
|---|
| 186 | +{ |
|---|
| 187 | + mutex_init(&channel->inbound.ring_buffer_mutex); |
|---|
| 188 | + mutex_init(&channel->outbound.ring_buffer_mutex); |
|---|
| 189 | +} |
|---|
| 188 | 190 | |
|---|
| 189 | 191 | /* Initialize the ring buffer. */ |
|---|
| 190 | 192 | int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, |
|---|
| .. | .. |
|---|
| 194 | 196 | struct page **pages_wraparound; |
|---|
| 195 | 197 | |
|---|
| 196 | 198 | BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE)); |
|---|
| 197 | | - |
|---|
| 198 | | - memset(ring_info, 0, sizeof(struct hv_ring_buffer_info)); |
|---|
| 199 | 199 | |
|---|
| 200 | 200 | /* |
|---|
| 201 | 201 | * First page holds struct hv_ring_buffer, do wraparound mapping for |
|---|
| .. | .. |
|---|
| 230 | 230 | reciprocal_value(ring_info->ring_size / 10); |
|---|
| 231 | 231 | ring_info->ring_datasize = ring_info->ring_size - |
|---|
| 232 | 232 | sizeof(struct hv_ring_buffer); |
|---|
| 233 | + ring_info->priv_read_index = 0; |
|---|
| 233 | 234 | |
|---|
| 234 | 235 | spin_lock_init(&ring_info->ring_lock); |
|---|
| 235 | 236 | |
|---|
| .. | .. |
|---|
| 239 | 240 | /* Cleanup the ring buffer. */ |
|---|
| 240 | 241 | void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info) |
|---|
| 241 | 242 | { |
|---|
| 243 | + mutex_lock(&ring_info->ring_buffer_mutex); |
|---|
| 242 | 244 | vunmap(ring_info->ring_buffer); |
|---|
| 245 | + ring_info->ring_buffer = NULL; |
|---|
| 246 | + mutex_unlock(&ring_info->ring_buffer_mutex); |
|---|
| 243 | 247 | } |
|---|
| 244 | 248 | |
|---|
| 245 | 249 | /* Write to the ring buffer. */ |
|---|
| .. | .. |
|---|
| 271 | 275 | * is empty since the read index == write index. |
|---|
| 272 | 276 | */ |
|---|
| 273 | 277 | if (bytes_avail_towrite <= totalbytes_towrite) { |
|---|
| 278 | + ++channel->out_full_total; |
|---|
| 279 | + |
|---|
| 280 | + if (!channel->out_full_flag) { |
|---|
| 281 | + ++channel->out_full_first; |
|---|
| 282 | + channel->out_full_flag = true; |
|---|
| 283 | + } |
|---|
| 284 | + |
|---|
| 274 | 285 | spin_unlock_irqrestore(&outring_info->ring_lock, flags); |
|---|
| 275 | 286 | return -EAGAIN; |
|---|
| 276 | 287 | } |
|---|
| 288 | + |
|---|
| 289 | + channel->out_full_flag = false; |
|---|
| 277 | 290 | |
|---|
| 278 | 291 | /* Write to the ring buffer */ |
|---|
| 279 | 292 | next_write_location = hv_get_next_write_location(outring_info); |
|---|
| .. | .. |
|---|
| 365 | 378 | static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi) |
|---|
| 366 | 379 | { |
|---|
| 367 | 380 | u32 priv_read_loc = rbi->priv_read_index; |
|---|
| 368 | | - u32 write_loc = READ_ONCE(rbi->ring_buffer->write_index); |
|---|
| 381 | + u32 write_loc; |
|---|
| 382 | + |
|---|
| 383 | + /* |
|---|
| 384 | + * The Hyper-V host writes the packet data, then uses |
|---|
| 385 | + * store_release() to update the write_index. Use load_acquire() |
|---|
| 386 | + * here to prevent loads of the packet data from being re-ordered |
|---|
| 387 | + * before the read of the write_index and potentially getting |
|---|
| 388 | + * stale data. |
|---|
| 389 | + */ |
|---|
| 390 | + write_loc = virt_load_acquire(&rbi->ring_buffer->write_index); |
|---|
| 369 | 391 | |
|---|
| 370 | 392 | if (write_loc >= priv_read_loc) |
|---|
| 371 | 393 | return write_loc - priv_read_loc; |
|---|
| .. | .. |
|---|
| 383 | 405 | struct hv_ring_buffer_info *rbi = &channel->inbound; |
|---|
| 384 | 406 | struct vmpacket_descriptor *desc; |
|---|
| 385 | 407 | |
|---|
| 408 | + hv_debug_delay_test(channel, MESSAGE_DELAY); |
|---|
| 386 | 409 | if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor)) |
|---|
| 387 | 410 | return NULL; |
|---|
| 388 | 411 | |
|---|
| .. | .. |
|---|
| 408 | 431 | u32 packetlen = desc->len8 << 3; |
|---|
| 409 | 432 | u32 dsize = rbi->ring_datasize; |
|---|
| 410 | 433 | |
|---|
| 434 | + hv_debug_delay_test(channel, MESSAGE_DELAY); |
|---|
| 411 | 435 | /* bump offset to next potential packet */ |
|---|
| 412 | 436 | rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER; |
|---|
| 413 | 437 | if (rbi->priv_read_index >= dsize) |
|---|
| .. | .. |
|---|
| 529 | 553 | if (curr_write_sz <= pending_sz) |
|---|
| 530 | 554 | return; |
|---|
| 531 | 555 | |
|---|
| 556 | + ++channel->intr_in_full; |
|---|
| 532 | 557 | vmbus_setevent(channel); |
|---|
| 533 | 558 | } |
|---|
| 534 | 559 | EXPORT_SYMBOL_GPL(hv_pkt_iter_close); |
|---|