]>
Commit | Line | Data |
---|---|---|
3e7ee490 HJ |
1 | /* |
2 | * | |
3 | * 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 | * | |
18 | * Authors: | |
19 | * Haiyang Zhang <haiyangz@microsoft.com> | |
20 | * Hank Janssen <hjanssen@microsoft.com> | |
b2a5a585 | 21 | * K. Y. Srinivasan <kys@microsoft.com> |
3e7ee490 HJ |
22 | * |
23 | */ | |
0a46618d | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
3e7ee490 | 25 | |
a0086dc5 GKH |
26 | #include <linux/kernel.h> |
27 | #include <linux/mm.h> | |
46a97191 | 28 | #include <linux/hyperv.h> |
011a7c3c | 29 | #include <linux/uio.h> |
9988ce68 VK |
30 | #include <linux/vmalloc.h> |
31 | #include <linux/slab.h> | |
3f335ea2 | 32 | |
0f2a6619 | 33 | #include "hyperv_vmbus.h" |
3e7ee490 | 34 | |
98fa8cf4 S |
35 | /* |
36 | * When we write to the ring buffer, check if the host needs to | |
37 | * be signaled. Here is the details of this protocol: | |
38 | * | |
39 | * 1. The host guarantees that while it is draining the | |
40 | * ring buffer, it will set the interrupt_mask to | |
41 | * indicate it does not need to be interrupted when | |
42 | * new data is placed. | |
43 | * | |
44 | * 2. The host guarantees that it will completely drain | |
45 | * the ring buffer before exiting the read loop. Further, | |
46 | * once the ring buffer is empty, it will clear the | |
47 | * interrupt_mask and re-check to see if new data has | |
48 | * arrived. | |
1f6ee4e7 S |
49 | * |
50 | * KYS: Oct. 30, 2016: | |
51 | * It looks like Windows hosts have logic to deal with DOS attacks that | |
52 | * can be triggered if it receives interrupts when it is not expecting | |
53 | * the interrupt. The host expects interrupts only when the ring | |
54 | * transitions from empty to non-empty (or full to non full on the guest | |
55 | * to host ring). | |
56 | * So, base the signaling decision solely on the ring state until the | |
57 | * host logic is fixed. | |
98fa8cf4 S |
58 | */ |
59 | ||
b103a56f | 60 | static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel) |
98fa8cf4 | 61 | { |
1f6ee4e7 S |
62 | struct hv_ring_buffer_info *rbi = &channel->outbound; |
63 | ||
dcd0eeca | 64 | virt_mb(); |
d45faaee | 65 | if (READ_ONCE(rbi->ring_buffer->interrupt_mask)) |
1f6ee4e7 | 66 | return; |
98fa8cf4 | 67 | |
e91e84fa | 68 | /* check interrupt_mask before read_index */ |
dcd0eeca | 69 | virt_rmb(); |
98fa8cf4 S |
70 | /* |
71 | * This is the only case we need to signal when the | |
72 | * ring transitions from being empty to non-empty. | |
73 | */ | |
d45faaee | 74 | if (old_write == READ_ONCE(rbi->ring_buffer->read_index)) |
1f6ee4e7 | 75 | vmbus_setevent(channel); |
98fa8cf4 | 76 | |
1f6ee4e7 | 77 | return; |
98fa8cf4 S |
78 | } |
79 | ||
822f18d4 | 80 | /* Get the next write location for the specified ring buffer. */ |
4d643114 | 81 | static inline u32 |
2b8a912e | 82 | hv_get_next_write_location(struct hv_ring_buffer_info *ring_info) |
3e7ee490 | 83 | { |
fc8c72eb | 84 | u32 next = ring_info->ring_buffer->write_index; |
3e7ee490 | 85 | |
3e7ee490 HJ |
86 | return next; |
87 | } | |
88 | ||
822f18d4 | 89 | /* Set the next write location for the specified ring buffer. */ |
3e7ee490 | 90 | static inline void |
2b8a912e | 91 | hv_set_next_write_location(struct hv_ring_buffer_info *ring_info, |
fc8c72eb | 92 | u32 next_write_location) |
3e7ee490 | 93 | { |
fc8c72eb | 94 | ring_info->ring_buffer->write_index = next_write_location; |
3e7ee490 HJ |
95 | } |
96 | ||
822f18d4 | 97 | /* Get the next read location for the specified ring buffer. */ |
4d643114 | 98 | static inline u32 |
e4165a0f | 99 | hv_get_next_read_location(const struct hv_ring_buffer_info *ring_info) |
3e7ee490 | 100 | { |
e4165a0f | 101 | return ring_info->ring_buffer->read_index; |
3e7ee490 HJ |
102 | } |
103 | ||
b2a5a585 | 104 | /* |
b2a5a585 | 105 | * Get the next read location + offset for the specified ring buffer. |
822f18d4 | 106 | * This allows the caller to skip. |
b2a5a585 | 107 | */ |
4d643114 | 108 | static inline u32 |
e4165a0f SH |
109 | hv_get_next_readlocation_withoffset(const struct hv_ring_buffer_info *ring_info, |
110 | u32 offset) | |
3e7ee490 | 111 | { |
fc8c72eb | 112 | u32 next = ring_info->ring_buffer->read_index; |
3e7ee490 | 113 | |
fc8c72eb HZ |
114 | next += offset; |
115 | next %= ring_info->ring_datasize; | |
3e7ee490 HJ |
116 | |
117 | return next; | |
118 | } | |
119 | ||
822f18d4 | 120 | /* Set the next read location for the specified ring buffer. */ |
3e7ee490 | 121 | static inline void |
2b8a912e | 122 | hv_set_next_read_location(struct hv_ring_buffer_info *ring_info, |
fc8c72eb | 123 | u32 next_read_location) |
3e7ee490 | 124 | { |
fc8c72eb | 125 | ring_info->ring_buffer->read_index = next_read_location; |
ab028db4 | 126 | ring_info->priv_read_index = next_read_location; |
3e7ee490 HJ |
127 | } |
128 | ||
822f18d4 | 129 | /* Get the size of the ring buffer. */ |
4d643114 | 130 | static inline u32 |
e4165a0f | 131 | hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info) |
3e7ee490 | 132 | { |
fc8c72eb | 133 | return ring_info->ring_datasize; |
3e7ee490 HJ |
134 | } |
135 | ||
822f18d4 | 136 | /* Get the read and write indices as u64 of the specified ring buffer. */ |
59471438 | 137 | static inline u64 |
2b8a912e | 138 | hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info) |
3e7ee490 | 139 | { |
fc8c72eb | 140 | return (u64)ring_info->ring_buffer->write_index << 32; |
3e7ee490 HJ |
141 | } |
142 | ||
8f1136ae | 143 | /* |
8f1136ae S |
144 | * Helper routine to copy to source from ring buffer. |
145 | * Assume there is enough room. Handles wrap-around in src case only!! | |
8f1136ae S |
146 | */ |
147 | static u32 hv_copyfrom_ringbuffer( | |
e4165a0f | 148 | const struct hv_ring_buffer_info *ring_info, |
8f1136ae S |
149 | void *dest, |
150 | u32 destlen, | |
151 | u32 start_read_offset) | |
152 | { | |
153 | void *ring_buffer = hv_get_ring_buffer(ring_info); | |
154 | u32 ring_buffer_size = hv_get_ring_buffersize(ring_info); | |
155 | ||
f24f0b49 | 156 | memcpy(dest, ring_buffer + start_read_offset, destlen); |
8f1136ae S |
157 | |
158 | start_read_offset += destlen; | |
159 | start_read_offset %= ring_buffer_size; | |
160 | ||
161 | return start_read_offset; | |
162 | } | |
163 | ||
164 | ||
7581578d | 165 | /* |
7581578d S |
166 | * Helper routine to copy from source to ring buffer. |
167 | * Assume there is enough room. Handles wrap-around in dest case only!! | |
7581578d S |
168 | */ |
169 | static u32 hv_copyto_ringbuffer( | |
fc8c72eb HZ |
170 | struct hv_ring_buffer_info *ring_info, |
171 | u32 start_write_offset, | |
e4165a0f | 172 | const void *src, |
7581578d S |
173 | u32 srclen) |
174 | { | |
175 | void *ring_buffer = hv_get_ring_buffer(ring_info); | |
176 | u32 ring_buffer_size = hv_get_ring_buffersize(ring_info); | |
f24f0b49 VK |
177 | |
178 | memcpy(ring_buffer + start_write_offset, src, srclen); | |
3e7ee490 | 179 | |
7581578d S |
180 | start_write_offset += srclen; |
181 | start_write_offset %= ring_buffer_size; | |
182 | ||
183 | return start_write_offset; | |
184 | } | |
3e7ee490 | 185 | |
822f18d4 | 186 | /* Get various debug metrics for the specified ring buffer. */ |
e4165a0f SH |
187 | void hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info, |
188 | struct hv_ring_buffer_debug_info *debug_info) | |
3e7ee490 | 189 | { |
fc8c72eb HZ |
190 | u32 bytes_avail_towrite; |
191 | u32 bytes_avail_toread; | |
3e7ee490 | 192 | |
fc8c72eb | 193 | if (ring_info->ring_buffer) { |
2b8a912e | 194 | hv_get_ringbuffer_availbytes(ring_info, |
fc8c72eb HZ |
195 | &bytes_avail_toread, |
196 | &bytes_avail_towrite); | |
3e7ee490 | 197 | |
fc8c72eb HZ |
198 | debug_info->bytes_avail_toread = bytes_avail_toread; |
199 | debug_info->bytes_avail_towrite = bytes_avail_towrite; | |
82f8bd40 | 200 | debug_info->current_read_index = |
fc8c72eb | 201 | ring_info->ring_buffer->read_index; |
82f8bd40 | 202 | debug_info->current_write_index = |
fc8c72eb | 203 | ring_info->ring_buffer->write_index; |
82f8bd40 | 204 | debug_info->current_interrupt_mask = |
fc8c72eb | 205 | ring_info->ring_buffer->interrupt_mask; |
3e7ee490 HJ |
206 | } |
207 | } | |
208 | ||
822f18d4 | 209 | /* Initialize the ring buffer. */ |
72a95cbc | 210 | int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, |
9988ce68 | 211 | struct page *pages, u32 page_cnt) |
3e7ee490 | 212 | { |
9988ce68 VK |
213 | int i; |
214 | struct page **pages_wraparound; | |
215 | ||
216 | BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE)); | |
3e7ee490 | 217 | |
fc8c72eb | 218 | memset(ring_info, 0, sizeof(struct hv_ring_buffer_info)); |
3e7ee490 | 219 | |
9988ce68 VK |
220 | /* |
221 | * First page holds struct hv_ring_buffer, do wraparound mapping for | |
222 | * the rest. | |
223 | */ | |
224 | pages_wraparound = kzalloc(sizeof(struct page *) * (page_cnt * 2 - 1), | |
225 | GFP_KERNEL); | |
226 | if (!pages_wraparound) | |
227 | return -ENOMEM; | |
228 | ||
229 | pages_wraparound[0] = pages; | |
230 | for (i = 0; i < 2 * (page_cnt - 1); i++) | |
231 | pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1]; | |
232 | ||
233 | ring_info->ring_buffer = (struct hv_ring_buffer *) | |
234 | vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL); | |
235 | ||
236 | kfree(pages_wraparound); | |
237 | ||
238 | ||
239 | if (!ring_info->ring_buffer) | |
240 | return -ENOMEM; | |
241 | ||
fc8c72eb HZ |
242 | ring_info->ring_buffer->read_index = |
243 | ring_info->ring_buffer->write_index = 0; | |
3e7ee490 | 244 | |
822f18d4 | 245 | /* Set the feature bit for enabling flow control. */ |
046c7911 S |
246 | ring_info->ring_buffer->feature_bits.value = 1; |
247 | ||
9988ce68 VK |
248 | ring_info->ring_size = page_cnt << PAGE_SHIFT; |
249 | ring_info->ring_datasize = ring_info->ring_size - | |
250 | sizeof(struct hv_ring_buffer); | |
3e7ee490 | 251 | |
fc8c72eb | 252 | spin_lock_init(&ring_info->ring_lock); |
3e7ee490 HJ |
253 | |
254 | return 0; | |
255 | } | |
256 | ||
822f18d4 | 257 | /* Cleanup the ring buffer. */ |
2dba688b | 258 | void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info) |
3e7ee490 | 259 | { |
9988ce68 | 260 | vunmap(ring_info->ring_buffer); |
3e7ee490 HJ |
261 | } |
262 | ||
822f18d4 | 263 | /* Write to the ring buffer. */ |
1f6ee4e7 | 264 | int hv_ringbuffer_write(struct vmbus_channel *channel, |
e4165a0f | 265 | const struct kvec *kv_list, u32 kv_count) |
3e7ee490 | 266 | { |
4408f531 | 267 | int i = 0; |
fc8c72eb | 268 | u32 bytes_avail_towrite; |
fc8c72eb | 269 | u32 totalbytes_towrite = 0; |
3e7ee490 | 270 | |
66a60543 | 271 | u32 next_write_location; |
98fa8cf4 | 272 | u32 old_write; |
fc8c72eb | 273 | u64 prev_indices = 0; |
fe760e4d | 274 | unsigned long flags = 0; |
1f6ee4e7 | 275 | struct hv_ring_buffer_info *outring_info = &channel->outbound; |
3e7ee490 | 276 | |
e7e97dd8 S |
277 | if (channel->rescind) |
278 | return -ENODEV; | |
279 | ||
011a7c3c S |
280 | for (i = 0; i < kv_count; i++) |
281 | totalbytes_towrite += kv_list[i].iov_len; | |
3e7ee490 | 282 | |
fc8c72eb | 283 | totalbytes_towrite += sizeof(u64); |
3e7ee490 | 284 | |
5529eaf6 | 285 | spin_lock_irqsave(&outring_info->ring_lock, flags); |
3e7ee490 | 286 | |
a6341f00 | 287 | bytes_avail_towrite = hv_get_bytes_to_write(outring_info); |
3e7ee490 | 288 | |
822f18d4 VK |
289 | /* |
290 | * If there is only room for the packet, assume it is full. | |
291 | * Otherwise, the next time around, we think the ring buffer | |
292 | * is empty since the read index == write index. | |
293 | */ | |
fc8c72eb | 294 | if (bytes_avail_towrite <= totalbytes_towrite) { |
5529eaf6 | 295 | spin_unlock_irqrestore(&outring_info->ring_lock, flags); |
d2598f01 | 296 | return -EAGAIN; |
3e7ee490 HJ |
297 | } |
298 | ||
454f18a9 | 299 | /* Write to the ring buffer */ |
2b8a912e | 300 | next_write_location = hv_get_next_write_location(outring_info); |
3e7ee490 | 301 | |
98fa8cf4 S |
302 | old_write = next_write_location; |
303 | ||
011a7c3c | 304 | for (i = 0; i < kv_count; i++) { |
2b8a912e | 305 | next_write_location = hv_copyto_ringbuffer(outring_info, |
fc8c72eb | 306 | next_write_location, |
011a7c3c S |
307 | kv_list[i].iov_base, |
308 | kv_list[i].iov_len); | |
3e7ee490 HJ |
309 | } |
310 | ||
454f18a9 | 311 | /* Set previous packet start */ |
2b8a912e | 312 | prev_indices = hv_get_ring_bufferindices(outring_info); |
3e7ee490 | 313 | |
2b8a912e | 314 | next_write_location = hv_copyto_ringbuffer(outring_info, |
fc8c72eb HZ |
315 | next_write_location, |
316 | &prev_indices, | |
b219b3f7 | 317 | sizeof(u64)); |
3e7ee490 | 318 | |
98fa8cf4 | 319 | /* Issue a full memory barrier before updating the write index */ |
dcd0eeca | 320 | virt_mb(); |
3e7ee490 | 321 | |
454f18a9 | 322 | /* Now, update the write location */ |
2b8a912e | 323 | hv_set_next_write_location(outring_info, next_write_location); |
3e7ee490 | 324 | |
3e7ee490 | 325 | |
5529eaf6 | 326 | spin_unlock_irqrestore(&outring_info->ring_lock, flags); |
98fa8cf4 | 327 | |
b103a56f | 328 | hv_signal_on_write(old_write, channel); |
e7e97dd8 S |
329 | |
330 | if (channel->rescind) | |
331 | return -ENODEV; | |
332 | ||
3e7ee490 HJ |
333 | return 0; |
334 | } | |
335 | ||
3372592a | 336 | int hv_ringbuffer_read(struct vmbus_channel *channel, |
940b68e2 | 337 | void *buffer, u32 buflen, u32 *buffer_actual_len, |
3372592a | 338 | u64 *requestid, bool raw) |
3e7ee490 | 339 | { |
fc8c72eb HZ |
340 | u32 bytes_avail_toread; |
341 | u32 next_read_location = 0; | |
342 | u64 prev_indices = 0; | |
940b68e2 VK |
343 | struct vmpacket_descriptor desc; |
344 | u32 offset; | |
345 | u32 packetlen; | |
346 | int ret = 0; | |
3372592a | 347 | struct hv_ring_buffer_info *inring_info = &channel->inbound; |
3e7ee490 | 348 | |
fc8c72eb | 349 | if (buflen <= 0) |
a16e1485 | 350 | return -EINVAL; |
3e7ee490 | 351 | |
3e7ee490 | 352 | |
940b68e2 VK |
353 | *buffer_actual_len = 0; |
354 | *requestid = 0; | |
355 | ||
a6341f00 | 356 | bytes_avail_toread = hv_get_bytes_to_read(inring_info); |
454f18a9 | 357 | /* Make sure there is something to read */ |
940b68e2 VK |
358 | if (bytes_avail_toread < sizeof(desc)) { |
359 | /* | |
360 | * No error is set when there is even no header, drivers are | |
361 | * supposed to analyze buffer_actual_len. | |
362 | */ | |
3eba9a77 | 363 | return ret; |
940b68e2 | 364 | } |
3e7ee490 | 365 | |
433e19cf | 366 | init_cached_read_index(channel); |
940b68e2 VK |
367 | next_read_location = hv_get_next_read_location(inring_info); |
368 | next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc, | |
369 | sizeof(desc), | |
370 | next_read_location); | |
371 | ||
372 | offset = raw ? 0 : (desc.offset8 << 3); | |
373 | packetlen = (desc.len8 << 3) - offset; | |
374 | *buffer_actual_len = packetlen; | |
375 | *requestid = desc.trans_id; | |
376 | ||
3eba9a77 S |
377 | if (bytes_avail_toread < packetlen + offset) |
378 | return -EAGAIN; | |
940b68e2 | 379 | |
3eba9a77 S |
380 | if (packetlen > buflen) |
381 | return -ENOBUFS; | |
3e7ee490 | 382 | |
1ac58644 | 383 | next_read_location = |
2b8a912e | 384 | hv_get_next_readlocation_withoffset(inring_info, offset); |
3e7ee490 | 385 | |
2b8a912e | 386 | next_read_location = hv_copyfrom_ringbuffer(inring_info, |
fc8c72eb | 387 | buffer, |
940b68e2 | 388 | packetlen, |
fc8c72eb | 389 | next_read_location); |
3e7ee490 | 390 | |
2b8a912e | 391 | next_read_location = hv_copyfrom_ringbuffer(inring_info, |
fc8c72eb | 392 | &prev_indices, |
4408f531 | 393 | sizeof(u64), |
fc8c72eb | 394 | next_read_location); |
3e7ee490 | 395 | |
822f18d4 VK |
396 | /* |
397 | * Make sure all reads are done before we update the read index since | |
398 | * the writer may start writing to the read area once the read index | |
399 | * is updated. | |
400 | */ | |
dcd0eeca | 401 | virt_mb(); |
3e7ee490 | 402 | |
454f18a9 | 403 | /* Update the read index */ |
2b8a912e | 404 | hv_set_next_read_location(inring_info, next_read_location); |
3e7ee490 | 405 | |
3372592a | 406 | hv_signal_on_read(channel); |
c2b8e520 | 407 | |
940b68e2 | 408 | return ret; |
b5f53dde | 409 | } |