connection.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (c) 2009, Microsoft Corporation.
  5. *
  6. * Authors:
  7. * Haiyang Zhang <haiyangz@microsoft.com>
  8. * Hank Janssen <hjanssen@microsoft.com>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/wait.h>
  14. #include <linux/delay.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/hyperv.h>
  20. #include <linux/export.h>
  21. #include <linux/io.h>
  22. #include <linux/set_memory.h>
  23. #include <asm/mshyperv.h>
  24. #include "hyperv_vmbus.h"
  25. struct vmbus_connection vmbus_connection = {
  26. .conn_state = DISCONNECTED,
  27. .unload_event = COMPLETION_INITIALIZER(
  28. vmbus_connection.unload_event),
  29. .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
  30. .ready_for_suspend_event = COMPLETION_INITIALIZER(
  31. vmbus_connection.ready_for_suspend_event),
  32. .ready_for_resume_event = COMPLETION_INITIALIZER(
  33. vmbus_connection.ready_for_resume_event),
  34. };
  35. EXPORT_SYMBOL_GPL(vmbus_connection);
  36. /*
  37. * Negotiated protocol version with the host.
  38. */
  39. __u32 vmbus_proto_version;
  40. EXPORT_SYMBOL_GPL(vmbus_proto_version);
  41. /*
  42. * Table of VMBus versions listed from newest to oldest.
  43. * VERSION_WIN7 and VERSION_WS2008 are no longer supported in
  44. * Linux guests and are not listed.
  45. */
  46. static __u32 vmbus_versions[] = {
  47. VERSION_WIN10_V5_3,
  48. VERSION_WIN10_V5_2,
  49. VERSION_WIN10_V5_1,
  50. VERSION_WIN10_V5,
  51. VERSION_WIN10_V4_1,
  52. VERSION_WIN10,
  53. VERSION_WIN8_1,
  54. VERSION_WIN8
  55. };
  56. /*
  57. * Maximal VMBus protocol version guests can negotiate. Useful to cap the
  58. * VMBus version for testing and debugging purpose.
  59. */
  60. static uint max_version = VERSION_WIN10_V5_3;
  61. module_param(max_version, uint, S_IRUGO);
  62. MODULE_PARM_DESC(max_version,
  63. "Maximal VMBus protocol version which can be negotiated");
  64. int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version)
  65. {
  66. int ret = 0;
  67. struct vmbus_channel_initiate_contact *msg;
  68. unsigned long flags;
  69. init_completion(&msginfo->waitevent);
  70. msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
  71. memset(msg, 0, sizeof(*msg));
  72. msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
  73. msg->vmbus_version_requested = version;
  74. /*
  75. * VMBus protocol 5.0 (VERSION_WIN10_V5) and higher require that we must
  76. * use VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
  77. * and for subsequent messages, we must use the Message Connection ID
  78. * field in the host-returned Version Response Message. And, with
  79. * VERSION_WIN10_V5 and higher, we don't use msg->interrupt_page, but we
  80. * tell the host explicitly that we still use VMBUS_MESSAGE_SINT(2) for
  81. * compatibility.
  82. *
  83. * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
  84. */
  85. if (version >= VERSION_WIN10_V5) {
  86. msg->msg_sint = VMBUS_MESSAGE_SINT;
  87. msg->msg_vtl = ms_hyperv.vtl;
  88. vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
  89. } else {
  90. msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
  91. vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
  92. }
  93. /*
  94. * shared_gpa_boundary is zero in non-SNP VMs, so it's safe to always
  95. * bitwise OR it
  96. */
  97. msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]) |
  98. ms_hyperv.shared_gpa_boundary;
  99. msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]) |
  100. ms_hyperv.shared_gpa_boundary;
  101. msg->target_vcpu = hv_cpu_number_to_vp_number(VMBUS_CONNECT_CPU);
  102. /*
  103. * Add to list before we send the request since we may
  104. * receive the response before returning from this routine
  105. */
  106. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  107. list_add_tail(&msginfo->msglistentry,
  108. &vmbus_connection.chn_msg_list);
  109. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  110. ret = vmbus_post_msg(msg,
  111. sizeof(struct vmbus_channel_initiate_contact),
  112. true);
  113. trace_vmbus_negotiate_version(msg, ret);
  114. if (ret != 0) {
  115. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  116. list_del(&msginfo->msglistentry);
  117. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  118. flags);
  119. return ret;
  120. }
  121. /* Wait for the connection response */
  122. wait_for_completion(&msginfo->waitevent);
  123. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  124. list_del(&msginfo->msglistentry);
  125. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  126. /* Check if successful */
  127. if (msginfo->response.version_response.version_supported) {
  128. vmbus_connection.conn_state = CONNECTED;
  129. if (version >= VERSION_WIN10_V5)
  130. vmbus_connection.msg_conn_id =
  131. msginfo->response.version_response.msg_conn_id;
  132. } else {
  133. return -ECONNREFUSED;
  134. }
  135. return ret;
  136. }
  137. /*
  138. * vmbus_connect - Sends a connect request on the partition service connection
  139. */
  140. int vmbus_connect(void)
  141. {
  142. struct vmbus_channel_msginfo *msginfo = NULL;
  143. int i, ret = 0;
  144. __u32 version;
  145. /* Initialize the vmbus connection */
  146. vmbus_connection.conn_state = CONNECTING;
  147. vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
  148. if (!vmbus_connection.work_queue) {
  149. ret = -ENOMEM;
  150. goto cleanup;
  151. }
  152. vmbus_connection.rescind_work_queue =
  153. create_workqueue("hv_vmbus_rescind");
  154. if (!vmbus_connection.rescind_work_queue) {
  155. ret = -ENOMEM;
  156. goto cleanup;
  157. }
  158. vmbus_connection.ignore_any_offer_msg = false;
  159. vmbus_connection.handle_primary_chan_wq =
  160. create_workqueue("hv_pri_chan");
  161. if (!vmbus_connection.handle_primary_chan_wq) {
  162. ret = -ENOMEM;
  163. goto cleanup;
  164. }
  165. vmbus_connection.handle_sub_chan_wq =
  166. create_workqueue("hv_sub_chan");
  167. if (!vmbus_connection.handle_sub_chan_wq) {
  168. ret = -ENOMEM;
  169. goto cleanup;
  170. }
  171. INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
  172. spin_lock_init(&vmbus_connection.channelmsg_lock);
  173. INIT_LIST_HEAD(&vmbus_connection.chn_list);
  174. mutex_init(&vmbus_connection.channel_mutex);
  175. /*
  176. * Setup the vmbus event connection for channel interrupt
  177. * abstraction stuff
  178. */
  179. vmbus_connection.int_page = hv_alloc_hyperv_zeroed_page();
  180. if (vmbus_connection.int_page == NULL) {
  181. ret = -ENOMEM;
  182. goto cleanup;
  183. }
  184. vmbus_connection.recv_int_page = vmbus_connection.int_page;
  185. vmbus_connection.send_int_page =
  186. (void *)((unsigned long)vmbus_connection.int_page +
  187. (HV_HYP_PAGE_SIZE >> 1));
  188. /*
  189. * Setup the monitor notification facility. The 1st page for
  190. * parent->child and the 2nd page for child->parent
  191. */
  192. vmbus_connection.monitor_pages[0] = hv_alloc_hyperv_page();
  193. vmbus_connection.monitor_pages[1] = hv_alloc_hyperv_page();
  194. if ((vmbus_connection.monitor_pages[0] == NULL) ||
  195. (vmbus_connection.monitor_pages[1] == NULL)) {
  196. ret = -ENOMEM;
  197. goto cleanup;
  198. }
  199. ret = set_memory_decrypted((unsigned long)
  200. vmbus_connection.monitor_pages[0], 1);
  201. ret |= set_memory_decrypted((unsigned long)
  202. vmbus_connection.monitor_pages[1], 1);
  203. if (ret) {
  204. /*
  205. * If set_memory_decrypted() fails, the encryption state
  206. * of the memory is unknown. So leak the memory instead
  207. * of risking returning decrypted memory to the free list.
  208. * For simplicity, always handle both pages the same.
  209. */
  210. vmbus_connection.monitor_pages[0] = NULL;
  211. vmbus_connection.monitor_pages[1] = NULL;
  212. goto cleanup;
  213. }
  214. /*
  215. * Set_memory_decrypted() will change the memory contents if
  216. * decryption occurs, so zero monitor pages here.
  217. */
  218. memset(vmbus_connection.monitor_pages[0], 0x00, HV_HYP_PAGE_SIZE);
  219. memset(vmbus_connection.monitor_pages[1], 0x00, HV_HYP_PAGE_SIZE);
  220. msginfo = kzalloc(sizeof(*msginfo) +
  221. sizeof(struct vmbus_channel_initiate_contact),
  222. GFP_KERNEL);
  223. if (msginfo == NULL) {
  224. ret = -ENOMEM;
  225. goto cleanup;
  226. }
  227. /*
  228. * Negotiate a compatible VMBUS version number with the
  229. * host. We start with the highest number we can support
  230. * and work our way down until we negotiate a compatible
  231. * version.
  232. */
  233. for (i = 0; ; i++) {
  234. if (i == ARRAY_SIZE(vmbus_versions)) {
  235. ret = -EDOM;
  236. goto cleanup;
  237. }
  238. version = vmbus_versions[i];
  239. if (version > max_version)
  240. continue;
  241. ret = vmbus_negotiate_version(msginfo, version);
  242. if (ret == -ETIMEDOUT)
  243. goto cleanup;
  244. if (vmbus_connection.conn_state == CONNECTED)
  245. break;
  246. }
  247. if (hv_is_isolation_supported() && version < VERSION_WIN10_V5_2) {
  248. pr_err("Invalid VMBus version %d.%d (expected >= %d.%d) from the host supporting isolation\n",
  249. version >> 16, version & 0xFFFF, VERSION_WIN10_V5_2 >> 16, VERSION_WIN10_V5_2 & 0xFFFF);
  250. ret = -EINVAL;
  251. goto cleanup;
  252. }
  253. vmbus_proto_version = version;
  254. pr_info("Vmbus version:%d.%d\n",
  255. version >> 16, version & 0xFFFF);
  256. vmbus_connection.channels = kcalloc(MAX_CHANNEL_RELIDS,
  257. sizeof(struct vmbus_channel *),
  258. GFP_KERNEL);
  259. if (vmbus_connection.channels == NULL) {
  260. ret = -ENOMEM;
  261. goto cleanup;
  262. }
  263. kfree(msginfo);
  264. return 0;
  265. cleanup:
  266. pr_err("Unable to connect to host\n");
  267. vmbus_connection.conn_state = DISCONNECTED;
  268. vmbus_disconnect();
  269. kfree(msginfo);
  270. return ret;
  271. }
  272. void vmbus_disconnect(void)
  273. {
  274. /*
  275. * First send the unload request to the host.
  276. */
  277. vmbus_initiate_unload(false);
  278. if (vmbus_connection.handle_sub_chan_wq)
  279. destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
  280. if (vmbus_connection.handle_primary_chan_wq)
  281. destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
  282. if (vmbus_connection.rescind_work_queue)
  283. destroy_workqueue(vmbus_connection.rescind_work_queue);
  284. if (vmbus_connection.work_queue)
  285. destroy_workqueue(vmbus_connection.work_queue);
  286. if (vmbus_connection.int_page) {
  287. hv_free_hyperv_page(vmbus_connection.int_page);
  288. vmbus_connection.int_page = NULL;
  289. }
  290. if (vmbus_connection.monitor_pages[0]) {
  291. if (!set_memory_encrypted(
  292. (unsigned long)vmbus_connection.monitor_pages[0], 1))
  293. hv_free_hyperv_page(vmbus_connection.monitor_pages[0]);
  294. vmbus_connection.monitor_pages[0] = NULL;
  295. }
  296. if (vmbus_connection.monitor_pages[1]) {
  297. if (!set_memory_encrypted(
  298. (unsigned long)vmbus_connection.monitor_pages[1], 1))
  299. hv_free_hyperv_page(vmbus_connection.monitor_pages[1]);
  300. vmbus_connection.monitor_pages[1] = NULL;
  301. }
  302. }
  303. /*
  304. * relid2channel - Get the channel object given its
  305. * child relative id (ie channel id)
  306. */
  307. struct vmbus_channel *relid2channel(u32 relid)
  308. {
  309. if (vmbus_connection.channels == NULL) {
  310. pr_warn_once("relid2channel: relid=%d: No channels mapped!\n", relid);
  311. return NULL;
  312. }
  313. if (WARN_ON(relid >= MAX_CHANNEL_RELIDS))
  314. return NULL;
  315. return READ_ONCE(vmbus_connection.channels[relid]);
  316. }
  317. /*
  318. * vmbus_on_event - Process a channel event notification
  319. *
  320. * For batched channels (default) optimize host to guest signaling
  321. * by ensuring:
  322. * 1. While reading the channel, we disable interrupts from host.
  323. * 2. Ensure that we process all posted messages from the host
  324. * before returning from this callback.
  325. * 3. Once we return, enable signaling from the host. Once this
  326. * state is set we check to see if additional packets are
  327. * available to read. In this case we repeat the process.
  328. * If this tasklet has been running for a long time
  329. * then reschedule ourselves.
  330. */
  331. void vmbus_on_event(unsigned long data)
  332. {
  333. struct vmbus_channel *channel = (void *) data;
  334. void (*callback_fn)(void *context);
  335. trace_vmbus_on_event(channel);
  336. hv_debug_delay_test(channel, INTERRUPT_DELAY);
  337. /* A channel once created is persistent even when
  338. * there is no driver handling the device. An
  339. * unloading driver sets the onchannel_callback to NULL.
  340. */
  341. callback_fn = READ_ONCE(channel->onchannel_callback);
  342. if (unlikely(!callback_fn))
  343. return;
  344. (*callback_fn)(channel->channel_callback_context);
  345. if (channel->callback_mode != HV_CALL_BATCHED)
  346. return;
  347. if (likely(hv_end_read(&channel->inbound) == 0))
  348. return;
  349. hv_begin_read(&channel->inbound);
  350. tasklet_schedule(&channel->callback_event);
  351. }
  352. /*
  353. * vmbus_post_msg - Send a msg on the vmbus's message connection
  354. */
  355. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
  356. {
  357. struct vmbus_channel_message_header *hdr;
  358. union hv_connection_id conn_id;
  359. int ret = 0;
  360. int retries = 0;
  361. u32 usec = 1;
  362. conn_id.asu32 = 0;
  363. conn_id.u.id = vmbus_connection.msg_conn_id;
  364. /*
  365. * hv_post_message() can have transient failures because of
  366. * insufficient resources. Retry the operation a couple of
  367. * times before giving up.
  368. */
  369. while (retries < 100) {
  370. ret = hv_post_message(conn_id, 1, buffer, buflen);
  371. switch (ret) {
  372. case HV_STATUS_INVALID_CONNECTION_ID:
  373. /*
  374. * See vmbus_negotiate_version(): VMBus protocol 5.0
  375. * and higher require that we must use
  376. * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
  377. * Contact message, but on old hosts that only
  378. * support VMBus protocol 4.0 or lower, here we get
  379. * HV_STATUS_INVALID_CONNECTION_ID and we should
  380. * return an error immediately without retrying.
  381. */
  382. hdr = buffer;
  383. if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
  384. return -EINVAL;
  385. /*
  386. * We could get this if we send messages too
  387. * frequently.
  388. */
  389. ret = -EAGAIN;
  390. break;
  391. case HV_STATUS_INSUFFICIENT_MEMORY:
  392. case HV_STATUS_INSUFFICIENT_BUFFERS:
  393. ret = -ENOBUFS;
  394. break;
  395. case HV_STATUS_SUCCESS:
  396. return ret;
  397. default:
  398. pr_err("hv_post_msg() failed; error code:%d\n", ret);
  399. return -EINVAL;
  400. }
  401. retries++;
  402. if (can_sleep && usec > 1000)
  403. msleep(usec / 1000);
  404. else if (usec < MAX_UDELAY_MS * 1000)
  405. udelay(usec);
  406. else
  407. mdelay(usec / 1000);
  408. if (retries < 22)
  409. usec *= 2;
  410. }
  411. return ret;
  412. }
  413. /*
  414. * vmbus_set_event - Send an event notification to the parent
  415. */
  416. void vmbus_set_event(struct vmbus_channel *channel)
  417. {
  418. u32 child_relid = channel->offermsg.child_relid;
  419. if (!channel->is_dedicated_interrupt)
  420. vmbus_send_interrupt(child_relid);
  421. ++channel->sig_events;
  422. if (ms_hyperv.paravisor_present) {
  423. if (hv_isolation_type_snp())
  424. hv_ghcb_hypercall(HVCALL_SIGNAL_EVENT, &channel->sig_event,
  425. NULL, sizeof(channel->sig_event));
  426. else if (hv_isolation_type_tdx())
  427. hv_tdx_hypercall(HVCALL_SIGNAL_EVENT | HV_HYPERCALL_FAST_BIT,
  428. channel->sig_event, 0);
  429. else
  430. WARN_ON_ONCE(1);
  431. } else {
  432. hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
  433. }
  434. }
  435. EXPORT_SYMBOL_GPL(vmbus_set_event);