Commit 9988ce68 authored by Vitaly Kuznetsov's avatar Vitaly Kuznetsov Committed by Greg Kroah-Hartman

Drivers: hv: ring_buffer: wrap around mappings for ring buffers

Make it possible to always use a single memcpy() or to provide a direct
link to a packet on the ring buffer by creating virtual mapping for two
copies of the ring buffer with vmap(). Utilize currently empty
hv_ringbuffer_cleanup() to do the unmap.

While on it, replace sizeof(struct hv_ring_buffer) check
in hv_ringbuffer_init() with BUILD_BUG_ON() as it is a compile time check.
Signed-off-by: default avatarVitaly Kuznetsov <vkuznets@redhat.com>
Tested-by: default avatarDexuan Cui <decui@microsoft.com>
Signed-off-by: default avatarK. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 98f531b1
...@@ -75,7 +75,6 @@ int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size, ...@@ -75,7 +75,6 @@ int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
{ {
struct vmbus_channel_open_channel *open_msg; struct vmbus_channel_open_channel *open_msg;
struct vmbus_channel_msginfo *open_info = NULL; struct vmbus_channel_msginfo *open_info = NULL;
void *in, *out;
unsigned long flags; unsigned long flags;
int ret, err = 0; int ret, err = 0;
struct page *page; struct page *page;
...@@ -112,23 +111,21 @@ int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size, ...@@ -112,23 +111,21 @@ int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
goto error_set_chnstate; goto error_set_chnstate;
} }
out = page_address(page); newchannel->ringbuffer_pages = page_address(page);
in = (void *)((unsigned long)out + send_ringbuffer_size);
newchannel->ringbuffer_pages = out;
newchannel->ringbuffer_pagecount = (send_ringbuffer_size + newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
recv_ringbuffer_size) >> PAGE_SHIFT; recv_ringbuffer_size) >> PAGE_SHIFT;
ret = hv_ringbuffer_init( ret = hv_ringbuffer_init(&newchannel->outbound, page,
&newchannel->outbound, out, send_ringbuffer_size); send_ringbuffer_size >> PAGE_SHIFT);
if (ret != 0) { if (ret != 0) {
err = ret; err = ret;
goto error_free_pages; goto error_free_pages;
} }
ret = hv_ringbuffer_init( ret = hv_ringbuffer_init(&newchannel->inbound,
&newchannel->inbound, in, recv_ringbuffer_size); &page[send_ringbuffer_size >> PAGE_SHIFT],
recv_ringbuffer_size >> PAGE_SHIFT);
if (ret != 0) { if (ret != 0) {
err = ret; err = ret;
goto error_free_pages; goto error_free_pages;
...@@ -139,7 +136,7 @@ int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size, ...@@ -139,7 +136,7 @@ int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
newchannel->ringbuffer_gpadlhandle = 0; newchannel->ringbuffer_gpadlhandle = 0;
ret = vmbus_establish_gpadl(newchannel, ret = vmbus_establish_gpadl(newchannel,
newchannel->outbound.ring_buffer, page_address(page),
send_ringbuffer_size + send_ringbuffer_size +
recv_ringbuffer_size, recv_ringbuffer_size,
&newchannel->ringbuffer_gpadlhandle); &newchannel->ringbuffer_gpadlhandle);
...@@ -214,7 +211,9 @@ int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size, ...@@ -214,7 +211,9 @@ int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle); vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
kfree(open_info); kfree(open_info);
error_free_pages: error_free_pages:
free_pages((unsigned long)out, hv_ringbuffer_cleanup(&newchannel->outbound);
hv_ringbuffer_cleanup(&newchannel->inbound);
__free_pages(page,
get_order(send_ringbuffer_size + recv_ringbuffer_size)); get_order(send_ringbuffer_size + recv_ringbuffer_size));
error_set_chnstate: error_set_chnstate:
newchannel->state = CHANNEL_OPEN_STATE; newchannel->state = CHANNEL_OPEN_STATE;
......
...@@ -522,8 +522,8 @@ extern unsigned int host_info_edx; ...@@ -522,8 +522,8 @@ extern unsigned int host_info_edx;
/* Interface */ /* Interface */
int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, void *buffer, int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
u32 buflen); struct page *pages, u32 pagecnt);
void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info); void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
......
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/hyperv.h> #include <linux/hyperv.h>
#include <linux/uio.h> #include <linux/uio.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include "hyperv_vmbus.h" #include "hyperv_vmbus.h"
...@@ -243,22 +245,46 @@ void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info, ...@@ -243,22 +245,46 @@ void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
/* Initialize the ring buffer. */ /* Initialize the ring buffer. */
int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
void *buffer, u32 buflen) struct page *pages, u32 page_cnt)
{ {
if (sizeof(struct hv_ring_buffer) != PAGE_SIZE) int i;
return -EINVAL; struct page **pages_wraparound;
BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
memset(ring_info, 0, sizeof(struct hv_ring_buffer_info)); memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
ring_info->ring_buffer = (struct hv_ring_buffer *)buffer; /*
* 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;
ring_info->ring_buffer->read_index = ring_info->ring_buffer->read_index =
ring_info->ring_buffer->write_index = 0; ring_info->ring_buffer->write_index = 0;
/* Set the feature bit for enabling flow control. */ /* Set the feature bit for enabling flow control. */
ring_info->ring_buffer->feature_bits.value = 1; ring_info->ring_buffer->feature_bits.value = 1;
ring_info->ring_size = buflen; ring_info->ring_size = page_cnt << PAGE_SHIFT;
ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer); ring_info->ring_datasize = ring_info->ring_size -
sizeof(struct hv_ring_buffer);
spin_lock_init(&ring_info->ring_lock); spin_lock_init(&ring_info->ring_lock);
...@@ -268,6 +294,7 @@ int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, ...@@ -268,6 +294,7 @@ int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
/* Cleanup the ring buffer. */ /* Cleanup the ring buffer. */
void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info) void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
{ {
vunmap(ring_info->ring_buffer);
} }
/* Write to the ring buffer. */ /* Write to the ring buffer. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment