// SPDX-License-Identifier: GPL-2.0-only /* * * Copyright (c) 2009, Microsoft Corporation. * * Authors: * Haiyang Zhang <[email protected]> * Hank Janssen <[email protected]> * K. Y. Srinivasan <[email protected]> */ #define pr_fmt(fmt) … #include <linux/kernel.h> #include <linux/mm.h> #include <linux/hyperv.h> #include <linux/uio.h> #include <linux/vmalloc.h> #include <linux/slab.h> #include <linux/prefetch.h> #include <linux/io.h> #include <asm/mshyperv.h> #include "hyperv_vmbus.h" #define VMBUS_PKT_TRAILER … /* * When we write to the ring buffer, check if the host needs to * be signaled. Here is the details of this protocol: * * 1. The host guarantees that while it is draining the * ring buffer, it will set the interrupt_mask to * indicate it does not need to be interrupted when * new data is placed. * * 2. The host guarantees that it will completely drain * the ring buffer before exiting the read loop. Further, * once the ring buffer is empty, it will clear the * interrupt_mask and re-check to see if new data has * arrived. * * KYS: Oct. 30, 2016: * It looks like Windows hosts have logic to deal with DOS attacks that * can be triggered if it receives interrupts when it is not expecting * the interrupt. The host expects interrupts only when the ring * transitions from empty to non-empty (or full to non full on the guest * to host ring). * So, base the signaling decision solely on the ring state until the * host logic is fixed. */ static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel) { … } /* Get the next write location for the specified ring buffer. */ static inline u32 hv_get_next_write_location(struct hv_ring_buffer_info *ring_info) { … } /* Set the next write location for the specified ring buffer. */ static inline void hv_set_next_write_location(struct hv_ring_buffer_info *ring_info, u32 next_write_location) { … } /* Get the size of the ring buffer. */ static inline u32 hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info) { … } /* Get the read and write indices as u64 of the specified ring buffer. */ static inline u64 hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info) { … } /* * Helper routine to copy from source to ring buffer. * Assume there is enough room. Handles wrap-around in dest case only!! */ static u32 hv_copyto_ringbuffer( struct hv_ring_buffer_info *ring_info, u32 start_write_offset, const void *src, u32 srclen) { … } /* * * 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) { … } /* 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) { … } EXPORT_SYMBOL_GPL(…); /* Initialize a channel's ring buffer info mutex locks */ void hv_ringbuffer_pre_init(struct vmbus_channel *channel) { … } /* Initialize the ring buffer. */ int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, struct page *pages, u32 page_cnt, u32 max_pkt_size) { … } /* Cleanup the ring buffer. */ void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info) { … } /* * Check if the ring buffer spinlock is available to take or not; used on * atomic contexts, like panic path (see the Hyper-V framebuffer driver). */ bool hv_ringbuffer_spinlock_busy(struct vmbus_channel *channel) { … } EXPORT_SYMBOL_GPL(…); /* Write to the ring buffer. */ int hv_ringbuffer_write(struct vmbus_channel *channel, const struct kvec *kv_list, u32 kv_count, u64 requestid, u64 *trans_id) { … } int hv_ringbuffer_read(struct vmbus_channel *channel, void *buffer, u32 buflen, u32 *buffer_actual_len, u64 *requestid, bool raw) { … } /* * Determine number of bytes available in ring buffer after * the current iterator (priv_read_index) location. * * This is similar to hv_get_bytes_to_read but with private * read index instead. */ static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi) { … } /* * Get first vmbus packet from ring buffer after read_index * * If ring buffer is empty, returns NULL and no other action needed. */ struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel) { … } EXPORT_SYMBOL_GPL(…); /* * Get next vmbus packet from ring buffer. * * Advances the current location (priv_read_index) and checks for more * data. If the end of the ring buffer is reached, then return NULL. */ struct vmpacket_descriptor * __hv_pkt_iter_next(struct vmbus_channel *channel, const struct vmpacket_descriptor *desc) { … } EXPORT_SYMBOL_GPL(…); /* How many bytes were read in this iterator cycle */ static u32 hv_pkt_iter_bytes_read(const struct hv_ring_buffer_info *rbi, u32 start_read_index) { … } /* * Update host ring buffer after iterating over packets. If the host has * stopped queuing new entries because it found the ring buffer full, and * sufficient space is being freed up, signal the host. But be careful to * only signal the host when necessary, both for performance reasons and * because Hyper-V protects itself by throttling guests that signal * inappropriately. * * Determining when to signal is tricky. There are three key data inputs * that must be handled in this order to avoid race conditions: * * 1. Update the read_index * 2. Read the pending_send_sz * 3. Read the current write_index * * The interrupt_mask is not used to determine when to signal. The * interrupt_mask is used only on the guest->host ring buffer when * sending requests to the host. The host does not use it on the host-> * guest ring buffer to indicate whether it should be signaled. */ void hv_pkt_iter_close(struct vmbus_channel *channel) { … } EXPORT_SYMBOL_GPL(…);