Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 45241e50 authored by Hank Janssen's avatar Hank Janssen Committed by Greg Kroah-Hartman
Browse files

Staging: hv: Use only one txf buffer per channel and kmalloc/GFP_KERNEL on initialize



Correct issue with not checking kmalloc return value.
This fix now only uses one receive buffer for all hv_utils
channels, and will do only one kmalloc on init and will return
with a -ENOMEM if kmalloc fails on initialize.

And properly clean up memory on failure.

Thanks to Evgeniy Polyakov <zbr@ioremap.net> for pointing this out.
And thanks to Jesper Juhl <jj@chaosbits.net> and Ky Srinivasan
<ksrinivasan@novell.com> for suggesting a better implementation of
my original patch.

Signed-off-by: default avatarHaiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: default avatarHank Janssen <hjanssen@microsoft.com>
Reviewed-by: default avatarJesper Juhl <jj@chaosbits.net>
Cc: Evgeniy Polyakov <zbr@ioremap.net>
Cc: Jesper Juhl <jj@chaosbits.net>
Cc: Ky Srinivasan <ksrinivasan@novell.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 244ba856
Loading
Loading
Loading
Loading
+47 −40
Original line number Original line Diff line number Diff line
@@ -38,12 +38,14 @@
#include "vmbus_api.h"
#include "vmbus_api.h"
#include "utils.h"
#include "utils.h"


static u8 *shut_txf_buf;
static u8 *time_txf_buf;
static u8 *hbeat_txf_buf;


static void shutdown_onchannelcallback(void *context)
static void shutdown_onchannelcallback(void *context)
{
{
	struct vmbus_channel *channel = context;
	struct vmbus_channel *channel = context;
	u8 *buf;
	u32 recvlen;
	u32 buflen, recvlen;
	u64 requestid;
	u64 requestid;
	u8  execute_shutdown = false;
	u8  execute_shutdown = false;


@@ -52,22 +54,21 @@ static void shutdown_onchannelcallback(void *context)
	struct icmsg_hdr *icmsghdrp;
	struct icmsg_hdr *icmsghdrp;
	struct icmsg_negotiate *negop = NULL;
	struct icmsg_negotiate *negop = NULL;


	buflen = PAGE_SIZE;
	vmbus_recvpacket(channel, shut_txf_buf,
	buf = kmalloc(buflen, GFP_ATOMIC);
			 PAGE_SIZE, &recvlen, &requestid);

	vmbus_recvpacket(channel, buf, buflen, &recvlen, &requestid);


	if (recvlen > 0) {
	if (recvlen > 0) {
		DPRINT_DBG(VMBUS, "shutdown packet: len=%d, requestid=%lld",
		DPRINT_DBG(VMBUS, "shutdown packet: len=%d, requestid=%lld",
			   recvlen, requestid);
			   recvlen, requestid);


		icmsghdrp = (struct icmsg_hdr *)&buf[
		icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
			sizeof(struct vmbuspipe_hdr)];
			sizeof(struct vmbuspipe_hdr)];


		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
			prep_negotiate_resp(icmsghdrp, negop, buf);
			prep_negotiate_resp(icmsghdrp, negop, shut_txf_buf);
		} else {
		} else {
			shutdown_msg = (struct shutdown_msg_data *)&buf[
			shutdown_msg =
				(struct shutdown_msg_data *)&shut_txf_buf[
					sizeof(struct vmbuspipe_hdr) +
					sizeof(struct vmbuspipe_hdr) +
					sizeof(struct icmsg_hdr)];
					sizeof(struct icmsg_hdr)];


@@ -93,13 +94,11 @@ static void shutdown_onchannelcallback(void *context)
		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
			| ICMSGHDRFLAG_RESPONSE;
			| ICMSGHDRFLAG_RESPONSE;


		vmbus_sendpacket(channel, buf,
		vmbus_sendpacket(channel, shut_txf_buf,
				       recvlen, requestid,
				       recvlen, requestid,
				       VmbusPacketTypeDataInBand, 0);
				       VmbusPacketTypeDataInBand, 0);
	}
	}


	kfree(buf);

	if (execute_shutdown == true)
	if (execute_shutdown == true)
		orderly_poweroff(false);
		orderly_poweroff(false);
}
}
@@ -150,28 +149,25 @@ static inline void adj_guesttime(u64 hosttime, u8 flags)
static void timesync_onchannelcallback(void *context)
static void timesync_onchannelcallback(void *context)
{
{
	struct vmbus_channel *channel = context;
	struct vmbus_channel *channel = context;
	u8 *buf;
	u32 recvlen;
	u32 buflen, recvlen;
	u64 requestid;
	u64 requestid;
	struct icmsg_hdr *icmsghdrp;
	struct icmsg_hdr *icmsghdrp;
	struct ictimesync_data *timedatap;
	struct ictimesync_data *timedatap;


	buflen = PAGE_SIZE;
	vmbus_recvpacket(channel, time_txf_buf,
	buf = kmalloc(buflen, GFP_ATOMIC);
			 PAGE_SIZE, &recvlen, &requestid);

	vmbus_recvpacket(channel, buf, buflen, &recvlen, &requestid);


	if (recvlen > 0) {
	if (recvlen > 0) {
		DPRINT_DBG(VMBUS, "timesync packet: recvlen=%d, requestid=%lld",
		DPRINT_DBG(VMBUS, "timesync packet: recvlen=%d, requestid=%lld",
			recvlen, requestid);
			recvlen, requestid);


		icmsghdrp = (struct icmsg_hdr *)&buf[
		icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
				sizeof(struct vmbuspipe_hdr)];
				sizeof(struct vmbuspipe_hdr)];


		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
			prep_negotiate_resp(icmsghdrp, NULL, buf);
			prep_negotiate_resp(icmsghdrp, NULL, time_txf_buf);
		} else {
		} else {
			timedatap = (struct ictimesync_data *)&buf[
			timedatap = (struct ictimesync_data *)&time_txf_buf[
				sizeof(struct vmbuspipe_hdr) +
				sizeof(struct vmbuspipe_hdr) +
				sizeof(struct icmsg_hdr)];
				sizeof(struct icmsg_hdr)];
			adj_guesttime(timedatap->parenttime, timedatap->flags);
			adj_guesttime(timedatap->parenttime, timedatap->flags);
@@ -180,12 +176,10 @@ static void timesync_onchannelcallback(void *context)
		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
			| ICMSGHDRFLAG_RESPONSE;
			| ICMSGHDRFLAG_RESPONSE;


		vmbus_sendpacket(channel, buf,
		vmbus_sendpacket(channel, time_txf_buf,
				recvlen, requestid,
				recvlen, requestid,
				VmbusPacketTypeDataInBand, 0);
				VmbusPacketTypeDataInBand, 0);
	}
	}

	kfree(buf);
}
}


/*
/*
@@ -196,28 +190,26 @@ static void timesync_onchannelcallback(void *context)
static void heartbeat_onchannelcallback(void *context)
static void heartbeat_onchannelcallback(void *context)
{
{
	struct vmbus_channel *channel = context;
	struct vmbus_channel *channel = context;
	u8 *buf;
	u32 recvlen;
	u32 buflen, recvlen;
	u64 requestid;
	u64 requestid;
	struct icmsg_hdr *icmsghdrp;
	struct icmsg_hdr *icmsghdrp;
	struct heartbeat_msg_data *heartbeat_msg;
	struct heartbeat_msg_data *heartbeat_msg;


	buflen = PAGE_SIZE;
	vmbus_recvpacket(channel, hbeat_txf_buf,
	buf = kmalloc(buflen, GFP_ATOMIC);
			 PAGE_SIZE, &recvlen, &requestid);

	vmbus_recvpacket(channel, buf, buflen, &recvlen, &requestid);


	if (recvlen > 0) {
	if (recvlen > 0) {
		DPRINT_DBG(VMBUS, "heartbeat packet: len=%d, requestid=%lld",
		DPRINT_DBG(VMBUS, "heartbeat packet: len=%d, requestid=%lld",
			   recvlen, requestid);
			   recvlen, requestid);


		icmsghdrp = (struct icmsg_hdr *)&buf[
		icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
				sizeof(struct vmbuspipe_hdr)];
				sizeof(struct vmbuspipe_hdr)];


		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
			prep_negotiate_resp(icmsghdrp, NULL, buf);
			prep_negotiate_resp(icmsghdrp, NULL, hbeat_txf_buf);
		} else {
		} else {
			heartbeat_msg = (struct heartbeat_msg_data *)&buf[
			heartbeat_msg =
				(struct heartbeat_msg_data *)&hbeat_txf_buf[
					sizeof(struct vmbuspipe_hdr) +
					sizeof(struct vmbuspipe_hdr) +
					sizeof(struct icmsg_hdr)];
					sizeof(struct icmsg_hdr)];


@@ -230,12 +222,10 @@ static void heartbeat_onchannelcallback(void *context)
		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
			| ICMSGHDRFLAG_RESPONSE;
			| ICMSGHDRFLAG_RESPONSE;


		vmbus_sendpacket(channel, buf,
		vmbus_sendpacket(channel, hbeat_txf_buf,
				       recvlen, requestid,
				       recvlen, requestid,
				       VmbusPacketTypeDataInBand, 0);
				       VmbusPacketTypeDataInBand, 0);
	}
	}

	kfree(buf);
}
}


static const struct pci_device_id __initconst
static const struct pci_device_id __initconst
@@ -268,6 +258,19 @@ static int __init init_hyperv_utils(void)
	if (!dmi_check_system(hv_utils_dmi_table))
	if (!dmi_check_system(hv_utils_dmi_table))
		return -ENODEV;
		return -ENODEV;


	shut_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
	time_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
	hbeat_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);

	if (!shut_txf_buf || !time_txf_buf || !hbeat_txf_buf) {
		printk(KERN_INFO
		       "Unable to allocate memory for receive buffer\n");
		kfree(shut_txf_buf);
		kfree(time_txf_buf);
		kfree(hbeat_txf_buf);
		return -ENOMEM;
	}

	hv_cb_utils[HV_SHUTDOWN_MSG].channel->onchannel_callback =
	hv_cb_utils[HV_SHUTDOWN_MSG].channel->onchannel_callback =
		&shutdown_onchannelcallback;
		&shutdown_onchannelcallback;
	hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback;
	hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback;
@@ -298,6 +301,10 @@ static void exit_hyperv_utils(void)
	hv_cb_utils[HV_HEARTBEAT_MSG].channel->onchannel_callback =
	hv_cb_utils[HV_HEARTBEAT_MSG].channel->onchannel_callback =
		&chn_cb_negotiate;
		&chn_cb_negotiate;
	hv_cb_utils[HV_HEARTBEAT_MSG].callback = &chn_cb_negotiate;
	hv_cb_utils[HV_HEARTBEAT_MSG].callback = &chn_cb_negotiate;

	kfree(shut_txf_buf);
	kfree(time_txf_buf);
	kfree(hbeat_txf_buf);
}
}


module_init(init_hyperv_utils);
module_init(init_hyperv_utils);