| .. | .. |
|---|
| 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 | } |
|---|
| 248 | + |
|---|
| 249 | +/* |
|---|
| 250 | + * Check if the ring buffer spinlock is available to take or not; used on |
|---|
| 251 | + * atomic contexts, like panic path (see the Hyper-V framebuffer driver). |
|---|
| 252 | + */ |
|---|
| 253 | + |
|---|
| 254 | +bool hv_ringbuffer_spinlock_busy(struct vmbus_channel *channel) |
|---|
| 255 | +{ |
|---|
| 256 | + struct hv_ring_buffer_info *rinfo = &channel->outbound; |
|---|
| 257 | + |
|---|
| 258 | + return spin_is_locked(&rinfo->ring_lock); |
|---|
| 259 | +} |
|---|
| 260 | +EXPORT_SYMBOL_GPL(hv_ringbuffer_spinlock_busy); |
|---|
| 244 | 261 | |
|---|
| 245 | 262 | /* Write to the ring buffer. */ |
|---|
| 246 | 263 | int hv_ringbuffer_write(struct vmbus_channel *channel, |
|---|
| .. | .. |
|---|
| 271 | 288 | * is empty since the read index == write index. |
|---|
| 272 | 289 | */ |
|---|
| 273 | 290 | if (bytes_avail_towrite <= totalbytes_towrite) { |
|---|
| 291 | + ++channel->out_full_total; |
|---|
| 292 | + |
|---|
| 293 | + if (!channel->out_full_flag) { |
|---|
| 294 | + ++channel->out_full_first; |
|---|
| 295 | + channel->out_full_flag = true; |
|---|
| 296 | + } |
|---|
| 297 | + |
|---|
| 274 | 298 | spin_unlock_irqrestore(&outring_info->ring_lock, flags); |
|---|
| 275 | 299 | return -EAGAIN; |
|---|
| 276 | 300 | } |
|---|
| 301 | + |
|---|
| 302 | + channel->out_full_flag = false; |
|---|
| 277 | 303 | |
|---|
| 278 | 304 | /* Write to the ring buffer */ |
|---|
| 279 | 305 | next_write_location = hv_get_next_write_location(outring_info); |
|---|
| .. | .. |
|---|
| 365 | 391 | static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi) |
|---|
| 366 | 392 | { |
|---|
| 367 | 393 | u32 priv_read_loc = rbi->priv_read_index; |
|---|
| 368 | | - u32 write_loc = READ_ONCE(rbi->ring_buffer->write_index); |
|---|
| 394 | + u32 write_loc; |
|---|
| 395 | + |
|---|
| 396 | + /* |
|---|
| 397 | + * The Hyper-V host writes the packet data, then uses |
|---|
| 398 | + * store_release() to update the write_index. Use load_acquire() |
|---|
| 399 | + * here to prevent loads of the packet data from being re-ordered |
|---|
| 400 | + * before the read of the write_index and potentially getting |
|---|
| 401 | + * stale data. |
|---|
| 402 | + */ |
|---|
| 403 | + write_loc = virt_load_acquire(&rbi->ring_buffer->write_index); |
|---|
| 369 | 404 | |
|---|
| 370 | 405 | if (write_loc >= priv_read_loc) |
|---|
| 371 | 406 | return write_loc - priv_read_loc; |
|---|
| .. | .. |
|---|
| 383 | 418 | struct hv_ring_buffer_info *rbi = &channel->inbound; |
|---|
| 384 | 419 | struct vmpacket_descriptor *desc; |
|---|
| 385 | 420 | |
|---|
| 421 | + hv_debug_delay_test(channel, MESSAGE_DELAY); |
|---|
| 386 | 422 | if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor)) |
|---|
| 387 | 423 | return NULL; |
|---|
| 388 | 424 | |
|---|
| .. | .. |
|---|
| 408 | 444 | u32 packetlen = desc->len8 << 3; |
|---|
| 409 | 445 | u32 dsize = rbi->ring_datasize; |
|---|
| 410 | 446 | |
|---|
| 447 | + hv_debug_delay_test(channel, MESSAGE_DELAY); |
|---|
| 411 | 448 | /* bump offset to next potential packet */ |
|---|
| 412 | 449 | rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER; |
|---|
| 413 | 450 | if (rbi->priv_read_index >= dsize) |
|---|
| .. | .. |
|---|
| 529 | 566 | if (curr_write_sz <= pending_sz) |
|---|
| 530 | 567 | return; |
|---|
| 531 | 568 | |
|---|
| 569 | + ++channel->intr_in_full; |
|---|
| 532 | 570 | vmbus_setevent(channel); |
|---|
| 533 | 571 | } |
|---|
| 534 | 572 | EXPORT_SYMBOL_GPL(hv_pkt_iter_close); |
|---|