ring_buffer.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *
 * Copyright (c) 2009, Microsoft Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
 * Authors:
 *   Haiyang Zhang <haiyangz@microsoft.com>
 *   Hank Janssen  <hjanssen@microsoft.com>
21
 *   K. Y. Srinivasan <kys@microsoft.com>
22 23
 *
 */
24
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25

26 27
#include <linux/kernel.h>
#include <linux/mm.h>
28
#include <linux/hyperv.h>
29
#include <linux/uio.h>
30 31
#include <linux/vmalloc.h>
#include <linux/slab.h>
32

33
#include "hyperv_vmbus.h"
34

35 36 37
void hv_begin_read(struct hv_ring_buffer_info *rbi)
{
	rbi->ring_buffer->interrupt_mask = 1;
38
	virt_mb();
39 40 41 42 43 44
}

u32 hv_end_read(struct hv_ring_buffer_info *rbi)
{

	rbi->ring_buffer->interrupt_mask = 0;
45
	virt_mb();
46 47 48 49 50 51

	/*
	 * Now check to see if the ring buffer is still empty.
	 * If it is not, we raced and we need to process new
	 * incoming messages.
	 */
52
	return hv_get_bytes_to_read(rbi);
53 54
}

55 56 57 58 59 60 61 62 63 64 65 66 67 68
/*
 * 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.
69 70 71 72 73 74 75 76 77
 *
 * 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.
78 79
 */

80
static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel)
81
{
82 83
	struct hv_ring_buffer_info *rbi = &channel->outbound;

84
	virt_mb();
85
	if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
86
		return;
87

88
	/* check interrupt_mask before read_index */
89
	virt_rmb();
90 91 92 93
	/*
	 * This is the only case we need to signal when the
	 * ring transitions from being empty to non-empty.
	 */
94
	if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
95
		vmbus_setevent(channel);
96

97
	return;
98 99
}

100
/* Get the next write location for the specified ring buffer. */
101
static inline u32
102
hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
103
{
104
	u32 next = ring_info->ring_buffer->write_index;
105 106 107 108

	return next;
}

109
/* Set the next write location for the specified ring buffer. */
110
static inline void
111
hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
112
		     u32 next_write_location)
113
{
114
	ring_info->ring_buffer->write_index = next_write_location;
115 116
}

117
/* Get the next read location for the specified ring buffer. */
118
static inline u32
119
hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
120
{
121
	u32 next = ring_info->ring_buffer->read_index;
122 123 124 125

	return next;
}

126 127
/*
 * Get the next read location + offset for the specified ring buffer.
128
 * This allows the caller to skip.
129
 */
130
static inline u32
131
hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
132
				 u32 offset)
133
{
134
	u32 next = ring_info->ring_buffer->read_index;
135

136 137
	next += offset;
	next %= ring_info->ring_datasize;
138 139 140 141

	return next;
}

142
/* Set the next read location for the specified ring buffer. */
143
static inline void
144
hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
145
		    u32 next_read_location)
146
{
147
	ring_info->ring_buffer->read_index = next_read_location;
148
	ring_info->priv_read_index = next_read_location;
149 150
}

151
/* Get the size of the ring buffer. */
152
static inline u32
153
hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
154
{
155
	return ring_info->ring_datasize;
156 157
}

158
/* Get the read and write indices as u64 of the specified ring buffer. */
159
static inline u64
160
hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
161
{
162
	return (u64)ring_info->ring_buffer->write_index << 32;
163 164
}

165 166 167 168 169 170 171 172 173 174 175 176 177
/*
 * Helper routine to copy to source from ring buffer.
 * Assume there is enough room. Handles wrap-around in src case only!!
 */
static u32 hv_copyfrom_ringbuffer(
	struct hv_ring_buffer_info	*ring_info,
	void				*dest,
	u32				destlen,
	u32				start_read_offset)
{
	void *ring_buffer = hv_get_ring_buffer(ring_info);
	u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);

178
	memcpy(dest, ring_buffer + start_read_offset, destlen);
179 180 181 182 183 184 185 186

	start_read_offset += destlen;
	start_read_offset %= ring_buffer_size;

	return start_read_offset;
}


187 188 189 190 191
/*
 * 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(
192 193 194
	struct hv_ring_buffer_info	*ring_info,
	u32				start_write_offset,
	void				*src,
195 196 197 198
	u32				srclen)
{
	void *ring_buffer = hv_get_ring_buffer(ring_info);
	u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
199 200

	memcpy(ring_buffer + start_write_offset, src, srclen);
201

202 203 204 205 206
	start_write_offset += srclen;
	start_write_offset %= ring_buffer_size;

	return start_write_offset;
}
207

208
/* Get various debug metrics for the specified ring buffer. */
209
void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
210
			    struct hv_ring_buffer_debug_info *debug_info)
211
{
212 213
	u32 bytes_avail_towrite;
	u32 bytes_avail_toread;
214

215
	if (ring_info->ring_buffer) {
216
		hv_get_ringbuffer_availbytes(ring_info,
217 218
					&bytes_avail_toread,
					&bytes_avail_towrite);
219

220 221
		debug_info->bytes_avail_toread = bytes_avail_toread;
		debug_info->bytes_avail_towrite = bytes_avail_towrite;
222
		debug_info->current_read_index =
223
			ring_info->ring_buffer->read_index;
224
		debug_info->current_write_index =
225
			ring_info->ring_buffer->write_index;
226
		debug_info->current_interrupt_mask =
227
			ring_info->ring_buffer->interrupt_mask;
228 229 230
	}
}

231
/* Initialize the ring buffer. */
232
int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
233
		       struct page *pages, u32 page_cnt)
234
{
235 236 237 238
	int i;
	struct page **pages_wraparound;

	BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
239

240
	memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
241

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	/*
	 * First page holds struct hv_ring_buffer, do wraparound mapping for
	 * the rest.
	 */
	pages_wraparound = kzalloc(sizeof(struct page *) * (page_cnt * 2 - 1),
				   GFP_KERNEL);
	if (!pages_wraparound)
		return -ENOMEM;

	pages_wraparound[0] = pages;
	for (i = 0; i < 2 * (page_cnt - 1); i++)
		pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1];

	ring_info->ring_buffer = (struct hv_ring_buffer *)
		vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL);

	kfree(pages_wraparound);


	if (!ring_info->ring_buffer)
		return -ENOMEM;

264 265
	ring_info->ring_buffer->read_index =
		ring_info->ring_buffer->write_index = 0;
266

267
	/* Set the feature bit for enabling flow control. */
268 269
	ring_info->ring_buffer->feature_bits.value = 1;

270 271 272
	ring_info->ring_size = page_cnt << PAGE_SHIFT;
	ring_info->ring_datasize = ring_info->ring_size -
		sizeof(struct hv_ring_buffer);
273

274
	spin_lock_init(&ring_info->ring_lock);
275 276 277 278

	return 0;
}

279
/* Cleanup the ring buffer. */
280
void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
281
{
282
	vunmap(ring_info->ring_buffer);
283 284
}

285
/* Write to the ring buffer. */
286
int hv_ringbuffer_write(struct vmbus_channel *channel,
287
			struct kvec *kv_list, u32 kv_count, bool lock)
288
{
289
	int i = 0;
290 291
	u32 bytes_avail_towrite;
	u32 totalbytes_towrite = 0;
292

293
	u32 next_write_location;
294
	u32 old_write;
295
	u64 prev_indices = 0;
296
	unsigned long flags = 0;
297
	struct hv_ring_buffer_info *outring_info = &channel->outbound;
298

299 300 301
	if (channel->rescind)
		return -ENODEV;

302 303
	for (i = 0; i < kv_count; i++)
		totalbytes_towrite += kv_list[i].iov_len;
304

305
	totalbytes_towrite += sizeof(u64);
306

307 308
	if (lock)
		spin_lock_irqsave(&outring_info->ring_lock, flags);
309

310
	bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
311

312 313 314 315 316
	/*
	 * If there is only room for the packet, assume it is full.
	 * Otherwise, the next time around, we think the ring buffer
	 * is empty since the read index == write index.
	 */
317
	if (bytes_avail_towrite <= totalbytes_towrite) {
318 319
		if (lock)
			spin_unlock_irqrestore(&outring_info->ring_lock, flags);
320
		return -EAGAIN;
321 322
	}

323
	/* Write to the ring buffer */
324
	next_write_location = hv_get_next_write_location(outring_info);
325

326 327
	old_write = next_write_location;

328
	for (i = 0; i < kv_count; i++) {
329
		next_write_location = hv_copyto_ringbuffer(outring_info,
330
						     next_write_location,
331 332
						     kv_list[i].iov_base,
						     kv_list[i].iov_len);
333 334
	}

335
	/* Set previous packet start */
336
	prev_indices = hv_get_ring_bufferindices(outring_info);
337

338
	next_write_location = hv_copyto_ringbuffer(outring_info,
339 340
					     next_write_location,
					     &prev_indices,
341
					     sizeof(u64));
342

343
	/* Issue a full memory barrier before updating the write index */
344
	virt_mb();
345

346
	/* Now, update the write location */
347
	hv_set_next_write_location(outring_info, next_write_location);
348 349


350 351
	if (lock)
		spin_unlock_irqrestore(&outring_info->ring_lock, flags);
352

353
	hv_signal_on_write(old_write, channel);
354 355 356 357

	if (channel->rescind)
		return -ENODEV;

358 359 360
	return 0;
}

361
int hv_ringbuffer_read(struct vmbus_channel *channel,
362
		       void *buffer, u32 buflen, u32 *buffer_actual_len,
363
		       u64 *requestid, bool raw)
364
{
365 366 367
	u32 bytes_avail_toread;
	u32 next_read_location = 0;
	u64 prev_indices = 0;
368 369 370 371
	struct vmpacket_descriptor desc;
	u32 offset;
	u32 packetlen;
	int ret = 0;
372
	struct hv_ring_buffer_info *inring_info = &channel->inbound;
373

374
	if (buflen <= 0)
375
		return -EINVAL;
376 377


378 379 380
	*buffer_actual_len = 0;
	*requestid = 0;

381
	bytes_avail_toread = hv_get_bytes_to_read(inring_info);
382
	/* Make sure there is something to read */
383 384 385 386 387
	if (bytes_avail_toread < sizeof(desc)) {
		/*
		 * No error is set when there is even no header, drivers are
		 * supposed to analyze buffer_actual_len.
		 */
388
		return ret;
389
	}
390

391
	init_cached_read_index(channel);
392 393 394 395 396 397 398 399 400 401
	next_read_location = hv_get_next_read_location(inring_info);
	next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc,
						    sizeof(desc),
						    next_read_location);

	offset = raw ? 0 : (desc.offset8 << 3);
	packetlen = (desc.len8 << 3) - offset;
	*buffer_actual_len = packetlen;
	*requestid = desc.trans_id;

402 403
	if (bytes_avail_toread < packetlen + offset)
		return -EAGAIN;
404

405 406
	if (packetlen > buflen)
		return -ENOBUFS;
407

408
	next_read_location =
409
		hv_get_next_readlocation_withoffset(inring_info, offset);
410

411
	next_read_location = hv_copyfrom_ringbuffer(inring_info,
412
						buffer,
413
						packetlen,
414
						next_read_location);
415

416
	next_read_location = hv_copyfrom_ringbuffer(inring_info,
417
						&prev_indices,
418
						sizeof(u64),
419
						next_read_location);
420

421 422 423 424 425
	/*
	 * Make sure all reads are done before we update the read index since
	 * the writer may start writing to the read area once the read index
	 * is updated.
	 */
426
	virt_mb();
427

428
	/* Update the read index */
429
	hv_set_next_read_location(inring_info, next_read_location);
430

431
	hv_signal_on_read(channel);
432

433
	return ret;
434
}