]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hv/ring_buffer.c
UBUNTU: Start new release
[mirror_ubuntu-artful-kernel.git] / drivers / hv / ring_buffer.c
CommitLineData
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>
4b4b0b91 32#include <linux/prefetch.h>
3f335ea2 33
0f2a6619 34#include "hyperv_vmbus.h"
3e7ee490 35
f3dd3f47 36#define VMBUS_PKT_TRAILER 8
37
98fa8cf4
S
38/*
39 * When we write to the ring buffer, check if the host needs to
40 * be signaled. Here is the details of this protocol:
41 *
42 * 1. The host guarantees that while it is draining the
43 * ring buffer, it will set the interrupt_mask to
44 * indicate it does not need to be interrupted when
45 * new data is placed.
46 *
47 * 2. The host guarantees that it will completely drain
48 * the ring buffer before exiting the read loop. Further,
49 * once the ring buffer is empty, it will clear the
50 * interrupt_mask and re-check to see if new data has
51 * arrived.
1f6ee4e7
S
52 *
53 * KYS: Oct. 30, 2016:
54 * It looks like Windows hosts have logic to deal with DOS attacks that
55 * can be triggered if it receives interrupts when it is not expecting
56 * the interrupt. The host expects interrupts only when the ring
57 * transitions from empty to non-empty (or full to non full on the guest
58 * to host ring).
59 * So, base the signaling decision solely on the ring state until the
60 * host logic is fixed.
98fa8cf4
S
61 */
62
b103a56f 63static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel)
98fa8cf4 64{
1f6ee4e7
S
65 struct hv_ring_buffer_info *rbi = &channel->outbound;
66
dcd0eeca 67 virt_mb();
d45faaee 68 if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
1f6ee4e7 69 return;
98fa8cf4 70
e91e84fa 71 /* check interrupt_mask before read_index */
dcd0eeca 72 virt_rmb();
98fa8cf4
S
73 /*
74 * This is the only case we need to signal when the
75 * ring transitions from being empty to non-empty.
76 */
d45faaee 77 if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
1f6ee4e7 78 vmbus_setevent(channel);
98fa8cf4
S
79}
80
822f18d4 81/* Get the next write location for the specified ring buffer. */
4d643114 82static inline u32
2b8a912e 83hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
3e7ee490 84{
fc8c72eb 85 u32 next = ring_info->ring_buffer->write_index;
3e7ee490 86
3e7ee490
HJ
87 return next;
88}
89
822f18d4 90/* Set the next write location for the specified ring buffer. */
3e7ee490 91static inline void
2b8a912e 92hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
fc8c72eb 93 u32 next_write_location)
3e7ee490 94{
fc8c72eb 95 ring_info->ring_buffer->write_index = next_write_location;
3e7ee490
HJ
96}
97
822f18d4 98/* Set the next read location for the specified ring buffer. */
3e7ee490 99static inline void
2b8a912e 100hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
fc8c72eb 101 u32 next_read_location)
3e7ee490 102{
fc8c72eb 103 ring_info->ring_buffer->read_index = next_read_location;
ab028db4 104 ring_info->priv_read_index = next_read_location;
3e7ee490
HJ
105}
106
822f18d4 107/* Get the size of the ring buffer. */
4d643114 108static inline u32
e4165a0f 109hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info)
3e7ee490 110{
fc8c72eb 111 return ring_info->ring_datasize;
3e7ee490
HJ
112}
113
822f18d4 114/* Get the read and write indices as u64 of the specified ring buffer. */
59471438 115static inline u64
2b8a912e 116hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
3e7ee490 117{
fc8c72eb 118 return (u64)ring_info->ring_buffer->write_index << 32;
3e7ee490
HJ
119}
120
7581578d 121/*
7581578d
S
122 * Helper routine to copy from source to ring buffer.
123 * Assume there is enough room. Handles wrap-around in dest case only!!
7581578d
S
124 */
125static u32 hv_copyto_ringbuffer(
fc8c72eb
HZ
126 struct hv_ring_buffer_info *ring_info,
127 u32 start_write_offset,
e4165a0f 128 const void *src,
7581578d
S
129 u32 srclen)
130{
131 void *ring_buffer = hv_get_ring_buffer(ring_info);
132 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
f24f0b49
VK
133
134 memcpy(ring_buffer + start_write_offset, src, srclen);
3e7ee490 135
7581578d 136 start_write_offset += srclen;
8d12f882
SH
137 if (start_write_offset >= ring_buffer_size)
138 start_write_offset -= ring_buffer_size;
7581578d
S
139
140 return start_write_offset;
141}
3e7ee490 142
822f18d4 143/* Get various debug metrics for the specified ring buffer. */
e4165a0f
SH
144void hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
145 struct hv_ring_buffer_debug_info *debug_info)
3e7ee490 146{
fc8c72eb
HZ
147 u32 bytes_avail_towrite;
148 u32 bytes_avail_toread;
3e7ee490 149
fc8c72eb 150 if (ring_info->ring_buffer) {
2b8a912e 151 hv_get_ringbuffer_availbytes(ring_info,
fc8c72eb
HZ
152 &bytes_avail_toread,
153 &bytes_avail_towrite);
3e7ee490 154
fc8c72eb
HZ
155 debug_info->bytes_avail_toread = bytes_avail_toread;
156 debug_info->bytes_avail_towrite = bytes_avail_towrite;
82f8bd40 157 debug_info->current_read_index =
fc8c72eb 158 ring_info->ring_buffer->read_index;
82f8bd40 159 debug_info->current_write_index =
fc8c72eb 160 ring_info->ring_buffer->write_index;
82f8bd40 161 debug_info->current_interrupt_mask =
fc8c72eb 162 ring_info->ring_buffer->interrupt_mask;
3e7ee490
HJ
163 }
164}
4827ee1d 165EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);
3e7ee490 166
822f18d4 167/* Initialize the ring buffer. */
72a95cbc 168int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
9988ce68 169 struct page *pages, u32 page_cnt)
3e7ee490 170{
9988ce68
VK
171 int i;
172 struct page **pages_wraparound;
173
174 BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
3e7ee490 175
fc8c72eb 176 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
3e7ee490 177
9988ce68
VK
178 /*
179 * First page holds struct hv_ring_buffer, do wraparound mapping for
180 * the rest.
181 */
182 pages_wraparound = kzalloc(sizeof(struct page *) * (page_cnt * 2 - 1),
183 GFP_KERNEL);
184 if (!pages_wraparound)
185 return -ENOMEM;
186
187 pages_wraparound[0] = pages;
188 for (i = 0; i < 2 * (page_cnt - 1); i++)
189 pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1];
190
191 ring_info->ring_buffer = (struct hv_ring_buffer *)
192 vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL);
193
194 kfree(pages_wraparound);
195
196
197 if (!ring_info->ring_buffer)
198 return -ENOMEM;
199
fc8c72eb
HZ
200 ring_info->ring_buffer->read_index =
201 ring_info->ring_buffer->write_index = 0;
3e7ee490 202
822f18d4 203 /* Set the feature bit for enabling flow control. */
046c7911
S
204 ring_info->ring_buffer->feature_bits.value = 1;
205
9988ce68
VK
206 ring_info->ring_size = page_cnt << PAGE_SHIFT;
207 ring_info->ring_datasize = ring_info->ring_size -
208 sizeof(struct hv_ring_buffer);
3e7ee490 209
fc8c72eb 210 spin_lock_init(&ring_info->ring_lock);
3e7ee490
HJ
211
212 return 0;
213}
214
822f18d4 215/* Cleanup the ring buffer. */
2dba688b 216void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
3e7ee490 217{
9988ce68 218 vunmap(ring_info->ring_buffer);
3e7ee490
HJ
219}
220
822f18d4 221/* Write to the ring buffer. */
1f6ee4e7 222int hv_ringbuffer_write(struct vmbus_channel *channel,
e4165a0f 223 const struct kvec *kv_list, u32 kv_count)
3e7ee490 224{
2c616a8b 225 int i;
fc8c72eb 226 u32 bytes_avail_towrite;
2c616a8b 227 u32 totalbytes_towrite = sizeof(u64);
66a60543 228 u32 next_write_location;
98fa8cf4 229 u32 old_write;
2c616a8b
SH
230 u64 prev_indices;
231 unsigned long flags;
1f6ee4e7 232 struct hv_ring_buffer_info *outring_info = &channel->outbound;
3e7ee490 233
e7e97dd8
S
234 if (channel->rescind)
235 return -ENODEV;
236
011a7c3c
S
237 for (i = 0; i < kv_count; i++)
238 totalbytes_towrite += kv_list[i].iov_len;
3e7ee490 239
5529eaf6 240 spin_lock_irqsave(&outring_info->ring_lock, flags);
3e7ee490 241
a6341f00 242 bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
3e7ee490 243
822f18d4
VK
244 /*
245 * If there is only room for the packet, assume it is full.
246 * Otherwise, the next time around, we think the ring buffer
247 * is empty since the read index == write index.
248 */
fc8c72eb 249 if (bytes_avail_towrite <= totalbytes_towrite) {
5529eaf6 250 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
d2598f01 251 return -EAGAIN;
3e7ee490
HJ
252 }
253
454f18a9 254 /* Write to the ring buffer */
2b8a912e 255 next_write_location = hv_get_next_write_location(outring_info);
3e7ee490 256
98fa8cf4
S
257 old_write = next_write_location;
258
011a7c3c 259 for (i = 0; i < kv_count; i++) {
2b8a912e 260 next_write_location = hv_copyto_ringbuffer(outring_info,
fc8c72eb 261 next_write_location,
011a7c3c
S
262 kv_list[i].iov_base,
263 kv_list[i].iov_len);
3e7ee490
HJ
264 }
265
454f18a9 266 /* Set previous packet start */
2b8a912e 267 prev_indices = hv_get_ring_bufferindices(outring_info);
3e7ee490 268
2b8a912e 269 next_write_location = hv_copyto_ringbuffer(outring_info,
fc8c72eb
HZ
270 next_write_location,
271 &prev_indices,
b219b3f7 272 sizeof(u64));
3e7ee490 273
98fa8cf4 274 /* Issue a full memory barrier before updating the write index */
dcd0eeca 275 virt_mb();
3e7ee490 276
454f18a9 277 /* Now, update the write location */
2b8a912e 278 hv_set_next_write_location(outring_info, next_write_location);
3e7ee490 279
3e7ee490 280
5529eaf6 281 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
98fa8cf4 282
b103a56f 283 hv_signal_on_write(old_write, channel);
e7e97dd8
S
284
285 if (channel->rescind)
286 return -ENODEV;
287
3e7ee490
HJ
288 return 0;
289}
290
3372592a 291int hv_ringbuffer_read(struct vmbus_channel *channel,
940b68e2 292 void *buffer, u32 buflen, u32 *buffer_actual_len,
3372592a 293 u64 *requestid, bool raw)
3e7ee490 294{
beca01c6
SH
295 struct vmpacket_descriptor *desc;
296 u32 packetlen, offset;
297
298 if (unlikely(buflen == 0))
a16e1485 299 return -EINVAL;
3e7ee490 300
940b68e2
VK
301 *buffer_actual_len = 0;
302 *requestid = 0;
303
454f18a9 304 /* Make sure there is something to read */
beca01c6
SH
305 desc = hv_pkt_iter_first(channel);
306 if (desc == NULL) {
940b68e2
VK
307 /*
308 * No error is set when there is even no header, drivers are
309 * supposed to analyze buffer_actual_len.
310 */
42dd2715 311 return 0;
940b68e2 312 }
3e7ee490 313
beca01c6
SH
314 offset = raw ? 0 : (desc->offset8 << 3);
315 packetlen = (desc->len8 << 3) - offset;
940b68e2 316 *buffer_actual_len = packetlen;
beca01c6 317 *requestid = desc->trans_id;
940b68e2 318
beca01c6 319 if (unlikely(packetlen > buflen))
3eba9a77 320 return -ENOBUFS;
3e7ee490 321
beca01c6
SH
322 /* since ring is double mapped, only one copy is necessary */
323 memcpy(buffer, (const char *)desc + offset, packetlen);
3e7ee490 324
beca01c6
SH
325 /* Advance ring index to next packet descriptor */
326 __hv_pkt_iter_next(channel, desc);
3e7ee490 327
beca01c6
SH
328 /* Notify host of update */
329 hv_pkt_iter_close(channel);
c2b8e520 330
42dd2715 331 return 0;
b5f53dde 332}
f3dd3f47 333
334/*
335 * Determine number of bytes available in ring buffer after
336 * the current iterator (priv_read_index) location.
337 *
338 * This is similar to hv_get_bytes_to_read but with private
339 * read index instead.
340 */
341static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
342{
343 u32 priv_read_loc = rbi->priv_read_index;
344 u32 write_loc = READ_ONCE(rbi->ring_buffer->write_index);
345
346 if (write_loc >= priv_read_loc)
347 return write_loc - priv_read_loc;
348 else
349 return (rbi->ring_datasize - priv_read_loc) + write_loc;
350}
351
352/*
353 * Get first vmbus packet from ring buffer after read_index
354 *
355 * If ring buffer is empty, returns NULL and no other action needed.
356 */
357struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel)
358{
359 struct hv_ring_buffer_info *rbi = &channel->inbound;
360
f3dd3f47 361 if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
362 return NULL;
363
364 return hv_get_ring_buffer(rbi) + rbi->priv_read_index;
365}
366EXPORT_SYMBOL_GPL(hv_pkt_iter_first);
367
368/*
369 * Get next vmbus packet from ring buffer.
370 *
371 * Advances the current location (priv_read_index) and checks for more
372 * data. If the end of the ring buffer is reached, then return NULL.
373 */
374struct vmpacket_descriptor *
375__hv_pkt_iter_next(struct vmbus_channel *channel,
376 const struct vmpacket_descriptor *desc)
377{
378 struct hv_ring_buffer_info *rbi = &channel->inbound;
379 u32 packetlen = desc->len8 << 3;
380 u32 dsize = rbi->ring_datasize;
381
382 /* bump offset to next potential packet */
383 rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
384 if (rbi->priv_read_index >= dsize)
385 rbi->priv_read_index -= dsize;
386
387 /* more data? */
008da35b 388 return hv_pkt_iter_first(channel);
f3dd3f47 389}
390EXPORT_SYMBOL_GPL(__hv_pkt_iter_next);
391
392/*
393 * Update host ring buffer after iterating over packets.
394 */
395void hv_pkt_iter_close(struct vmbus_channel *channel)
396{
397 struct hv_ring_buffer_info *rbi = &channel->inbound;
008da35b 398 u32 orig_write_sz = hv_get_bytes_to_write(rbi);
f3dd3f47 399
400 /*
401 * Make sure all reads are done before we update the read index since
402 * the writer may start writing to the read area once the read index
403 * is updated.
404 */
405 virt_rmb();
406 rbi->ring_buffer->read_index = rbi->priv_read_index;
407
4b4b0b91
SH
408 /*
409 * Issue a full memory barrier before making the signaling decision.
410 * Here is the reason for having this barrier:
411 * If the reading of the pend_sz (in this function)
412 * were to be reordered and read before we commit the new read
413 * index (in the calling function) we could
414 * have a problem. If the host were to set the pending_sz after we
415 * have sampled pending_sz and go to sleep before we commit the
416 * read index, we could miss sending the interrupt. Issue a full
417 * memory barrier to address this.
418 */
419 virt_mb();
420
abeb1409
SH
421 /* If host has disabled notifications then skip */
422 if (rbi->ring_buffer->interrupt_mask)
4b4b0b91
SH
423 return;
424
abeb1409
SH
425 if (rbi->ring_buffer->feature_bits.feat_pending_send_sz) {
426 u32 pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz);
4b4b0b91 427
abeb1409
SH
428 /*
429 * If there was space before we began iteration,
430 * then host was not blocked. Also handles case where
431 * pending_sz is zero then host has nothing pending
432 * and does not need to be signaled.
433 */
434 if (orig_write_sz > pending_sz)
435 return;
436
437 /* If pending write will not fit, don't give false hope. */
438 if (hv_get_bytes_to_write(rbi) < pending_sz)
439 return;
440 }
441
442 vmbus_setevent(channel);
f3dd3f47 443}
444EXPORT_SYMBOL_GPL(hv_pkt_iter_close);