void vmbus_on_msg_dpc()

in vmbus_drv.c [1072:1203]


void vmbus_on_msg_dpc(unsigned long data)
{
	struct hv_per_cpu_context *hv_cpu = (void *)data;
	void *page_addr = hv_cpu->synic_message_page;
	struct hv_message msg_copy, *msg = (struct hv_message *)page_addr +
				  VMBUS_MESSAGE_SINT;
	struct vmbus_channel_message_header *hdr;
	enum vmbus_channel_message_type msgtype;
	const struct vmbus_channel_message_table_entry *entry;
	struct onmessage_work_context *ctx;
	__u8 payload_size;
	u32 message_type;

	/*
	 * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as
	 * it is being used in 'struct vmbus_channel_message_header' definition
	 * which is supposed to match hypervisor ABI.
	 */
	BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32));

	/*
	 * Since the message is in memory shared with the host, an erroneous or
	 * malicious Hyper-V could modify the message while vmbus_on_msg_dpc()
	 * or individual message handlers are executing; to prevent this, copy
	 * the message into private memory.
	 */
	memcpy(&msg_copy, msg, sizeof(struct hv_message));

	message_type = msg_copy.header.message_type;
	if (message_type == HVMSG_NONE)
		/* no msg */
		return;

	hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload;
	msgtype = hdr->msgtype;

	trace_vmbus_on_msg_dpc(hdr);

	if (msgtype >= CHANNELMSG_COUNT) {
		WARN_ONCE(1, "unknown msgtype=%d\n", msgtype);
		goto msg_handled;
	}

	payload_size = msg_copy.header.payload_size;
	if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) {
		WARN_ONCE(1, "payload size is too large (%d)\n", payload_size);
		goto msg_handled;
	}

	entry = &channel_message_table[msgtype];

	if (!entry->message_handler)
		goto msg_handled;

	if (payload_size < entry->min_payload_len) {
		WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size);
		goto msg_handled;
	}

	if (entry->handler_type	== VMHT_BLOCKING) {
		ctx = kmalloc(sizeof(*ctx) + payload_size, GFP_ATOMIC);
		if (ctx == NULL)
			return;

		INIT_WORK(&ctx->work, vmbus_onmessage_work);
		memcpy(&ctx->msg, &msg_copy, sizeof(msg->header) + payload_size);

		/*
		 * The host can generate a rescind message while we
		 * may still be handling the original offer. We deal with
		 * this condition by relying on the synchronization provided
		 * by offer_in_progress and by channel_mutex.  See also the
		 * inline comments in vmbus_onoffer_rescind().
		 */
		switch (msgtype) {
		case CHANNELMSG_RESCIND_CHANNELOFFER:
			/*
			 * If we are handling the rescind message;
			 * schedule the work on the global work queue.
			 *
			 * The OFFER message and the RESCIND message should
			 * not be handled by the same serialized work queue,
			 * because the OFFER handler may call vmbus_open(),
			 * which tries to open the channel by sending an
			 * OPEN_CHANNEL message to the host and waits for
			 * the host's response; however, if the host has
			 * rescinded the channel before it receives the
			 * OPEN_CHANNEL message, the host just silently
			 * ignores the OPEN_CHANNEL message; as a result,
			 * the guest's OFFER handler hangs for ever, if we
			 * handle the RESCIND message in the same serialized
			 * work queue: the RESCIND handler can not start to
			 * run before the OFFER handler finishes.
			 */
			schedule_work(&ctx->work);
			break;

		case CHANNELMSG_OFFERCHANNEL:
			/*
			 * The host sends the offer message of a given channel
			 * before sending the rescind message of the same
			 * channel.  These messages are sent to the guest's
			 * connect CPU; the guest then starts processing them
			 * in the tasklet handler on this CPU:
			 *
			 * VMBUS_CONNECT_CPU
			 *
			 * [vmbus_on_msg_dpc()]
			 * atomic_inc()  // CHANNELMSG_OFFERCHANNEL
			 * queue_work()
			 * ...
			 * [vmbus_on_msg_dpc()]
			 * schedule_work()  // CHANNELMSG_RESCIND_CHANNELOFFER
			 *
			 * We rely on the memory-ordering properties of the
			 * queue_work() and schedule_work() primitives, which
			 * guarantee that the atomic increment will be visible
			 * to the CPUs which will execute the offer & rescind
			 * works by the time these works will start execution.
			 */
			atomic_inc(&vmbus_connection.offer_in_progress);
			fallthrough;

		default:
			queue_work(vmbus_connection.work_queue, &ctx->work);
		}
	} else
		entry->message_handler(hdr);

msg_handled:
	vmbus_signal_eom(msg, message_type);
}