connection.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. * The following Hyper-V interrupt and monitor pages can be used by
  177. * UIO for mapping to user-space, so they should always be allocated on
  178. * system page boundaries. The system page size must be >= the Hyper-V
  179. * page size.
  180. */
  181. BUILD_BUG_ON(PAGE_SIZE < HV_HYP_PAGE_SIZE);
  182. /*
  183. * Setup the vmbus event connection for channel interrupt
  184. * abstraction stuff
  185. */
  186. vmbus_connection.int_page =
  187. (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  188. if (vmbus_connection.int_page == NULL) {
  189. ret = -ENOMEM;
  190. goto cleanup;
  191. }
  192. vmbus_connection.recv_int_page = vmbus_connection.int_page;
  193. vmbus_connection.send_int_page =
  194. (void *)((unsigned long)vmbus_connection.int_page +
  195. (HV_HYP_PAGE_SIZE >> 1));
  196. /*
  197. * Setup the monitor notification facility. The 1st page for
  198. * parent->child and the 2nd page for child->parent
  199. */
  200. vmbus_connection.monitor_pages[0] = (void *)__get_free_page(GFP_KERNEL);
  201. vmbus_connection.monitor_pages[1] = (void *)__get_free_page(GFP_KERNEL);
  202. if ((vmbus_connection.monitor_pages[0] == NULL) ||
  203. (vmbus_connection.monitor_pages[1] == NULL)) {
  204. ret = -ENOMEM;
  205. goto cleanup;
  206. }
  207. ret = set_memory_decrypted((unsigned long)
  208. vmbus_connection.monitor_pages[0], 1);
  209. ret |= set_memory_decrypted((unsigned long)
  210. vmbus_connection.monitor_pages[1], 1);
  211. if (ret) {
  212. /*
  213. * If set_memory_decrypted() fails, the encryption state
  214. * of the memory is unknown. So leak the memory instead
  215. * of risking returning decrypted memory to the free list.
  216. * For simplicity, always handle both pages the same.
  217. */
  218. vmbus_connection.monitor_pages[0] = NULL;
  219. vmbus_connection.monitor_pages[1] = NULL;
  220. goto cleanup;
  221. }
  222. /*
  223. * Set_memory_decrypted() will change the memory contents if
  224. * decryption occurs, so zero monitor pages here.
  225. */
  226. memset(vmbus_connection.monitor_pages[0], 0x00, HV_HYP_PAGE_SIZE);
  227. memset(vmbus_connection.monitor_pages[1], 0x00, HV_HYP_PAGE_SIZE);
  228. msginfo = kzalloc(sizeof(*msginfo) +
  229. sizeof(struct vmbus_channel_initiate_contact),
  230. GFP_KERNEL);
  231. if (msginfo == NULL) {
  232. ret = -ENOMEM;
  233. goto cleanup;
  234. }
  235. /*
  236. * Negotiate a compatible VMBUS version number with the
  237. * host. We start with the highest number we can support
  238. * and work our way down until we negotiate a compatible
  239. * version.
  240. */
  241. for (i = 0; ; i++) {
  242. if (i == ARRAY_SIZE(vmbus_versions)) {
  243. ret = -EDOM;
  244. goto cleanup;
  245. }
  246. version = vmbus_versions[i];
  247. if (version > max_version)
  248. continue;
  249. ret = vmbus_negotiate_version(msginfo, version);
  250. if (ret == -ETIMEDOUT)
  251. goto cleanup;
  252. if (vmbus_connection.conn_state == CONNECTED)
  253. break;
  254. }
  255. if (hv_is_isolation_supported() && version < VERSION_WIN10_V5_2) {
  256. pr_err("Invalid VMBus version %d.%d (expected >= %d.%d) from the host supporting isolation\n",
  257. version >> 16, version & 0xFFFF, VERSION_WIN10_V5_2 >> 16, VERSION_WIN10_V5_2 & 0xFFFF);
  258. ret = -EINVAL;
  259. goto cleanup;
  260. }
  261. vmbus_proto_version = version;
  262. pr_info("Vmbus version:%d.%d\n",
  263. version >> 16, version & 0xFFFF);
  264. vmbus_connection.channels = kcalloc(MAX_CHANNEL_RELIDS,
  265. sizeof(struct vmbus_channel *),
  266. GFP_KERNEL);
  267. if (vmbus_connection.channels == NULL) {
  268. ret = -ENOMEM;
  269. goto cleanup;
  270. }
  271. kfree(msginfo);
  272. return 0;
  273. cleanup:
  274. pr_err("Unable to connect to host\n");
  275. vmbus_connection.conn_state = DISCONNECTED;
  276. vmbus_disconnect();
  277. kfree(msginfo);
  278. return ret;
  279. }
  280. void vmbus_disconnect(void)
  281. {
  282. /*
  283. * First send the unload request to the host.
  284. */
  285. vmbus_initiate_unload(false);
  286. if (vmbus_connection.handle_sub_chan_wq)
  287. destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
  288. if (vmbus_connection.handle_primary_chan_wq)
  289. destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
  290. if (vmbus_connection.rescind_work_queue)
  291. destroy_workqueue(vmbus_connection.rescind_work_queue);
  292. if (vmbus_connection.work_queue)
  293. destroy_workqueue(vmbus_connection.work_queue);
  294. if (vmbus_connection.int_page) {
  295. free_page((unsigned long)vmbus_connection.int_page);
  296. vmbus_connection.int_page = NULL;
  297. }
  298. if (vmbus_connection.monitor_pages[0]) {
  299. if (!set_memory_encrypted(
  300. (unsigned long)vmbus_connection.monitor_pages[0], 1))
  301. free_page((unsigned long)
  302. vmbus_connection.monitor_pages[0]);
  303. vmbus_connection.monitor_pages[0] = NULL;
  304. }
  305. if (vmbus_connection.monitor_pages[1]) {
  306. if (!set_memory_encrypted(
  307. (unsigned long)vmbus_connection.monitor_pages[1], 1))
  308. free_page((unsigned long)
  309. vmbus_connection.monitor_pages[1]);
  310. vmbus_connection.monitor_pages[1] = NULL;
  311. }
  312. }
  313. /*
  314. * relid2channel - Get the channel object given its
  315. * child relative id (ie channel id)
  316. */
  317. struct vmbus_channel *relid2channel(u32 relid)
  318. {
  319. if (vmbus_connection.channels == NULL) {
  320. pr_warn_once("relid2channel: relid=%d: No channels mapped!\n", relid);
  321. return NULL;
  322. }
  323. if (WARN_ON(relid >= MAX_CHANNEL_RELIDS))
  324. return NULL;
  325. return READ_ONCE(vmbus_connection.channels[relid]);
  326. }
  327. /*
  328. * vmbus_on_event - Process a channel event notification
  329. *
  330. * For batched channels (default) optimize host to guest signaling
  331. * by ensuring:
  332. * 1. While reading the channel, we disable interrupts from host.
  333. * 2. Ensure that we process all posted messages from the host
  334. * before returning from this callback.
  335. * 3. Once we return, enable signaling from the host. Once this
  336. * state is set we check to see if additional packets are
  337. * available to read. In this case we repeat the process.
  338. * If this tasklet has been running for a long time
  339. * then reschedule ourselves.
  340. */
  341. void vmbus_on_event(unsigned long data)
  342. {
  343. struct vmbus_channel *channel = (void *) data;
  344. void (*callback_fn)(void *context);
  345. trace_vmbus_on_event(channel);
  346. hv_debug_delay_test(channel, INTERRUPT_DELAY);
  347. /* A channel once created is persistent even when
  348. * there is no driver handling the device. An
  349. * unloading driver sets the onchannel_callback to NULL.
  350. */
  351. callback_fn = READ_ONCE(channel->onchannel_callback);
  352. if (unlikely(!callback_fn))
  353. return;
  354. (*callback_fn)(channel->channel_callback_context);
  355. if (channel->callback_mode != HV_CALL_BATCHED)
  356. return;
  357. if (likely(hv_end_read(&channel->inbound) == 0))
  358. return;
  359. hv_begin_read(&channel->inbound);
  360. tasklet_schedule(&channel->callback_event);
  361. }
  362. /*
  363. * vmbus_post_msg - Send a msg on the vmbus's message connection
  364. */
  365. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
  366. {
  367. struct vmbus_channel_message_header *hdr;
  368. union hv_connection_id conn_id;
  369. int ret = 0;
  370. int retries = 0;
  371. u32 usec = 1;
  372. conn_id.asu32 = 0;
  373. conn_id.u.id = vmbus_connection.msg_conn_id;
  374. /*
  375. * hv_post_message() can have transient failures because of
  376. * insufficient resources. Retry the operation a couple of
  377. * times before giving up.
  378. */
  379. while (retries < 100) {
  380. ret = hv_post_message(conn_id, 1, buffer, buflen);
  381. switch (ret) {
  382. case HV_STATUS_INVALID_CONNECTION_ID:
  383. /*
  384. * See vmbus_negotiate_version(): VMBus protocol 5.0
  385. * and higher require that we must use
  386. * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
  387. * Contact message, but on old hosts that only
  388. * support VMBus protocol 4.0 or lower, here we get
  389. * HV_STATUS_INVALID_CONNECTION_ID and we should
  390. * return an error immediately without retrying.
  391. */
  392. hdr = buffer;
  393. if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
  394. return -EINVAL;
  395. /*
  396. * We could get this if we send messages too
  397. * frequently.
  398. */
  399. ret = -EAGAIN;
  400. break;
  401. case HV_STATUS_INSUFFICIENT_MEMORY:
  402. case HV_STATUS_INSUFFICIENT_BUFFERS:
  403. ret = -ENOBUFS;
  404. break;
  405. case HV_STATUS_SUCCESS:
  406. return ret;
  407. default:
  408. pr_err("hv_post_msg() failed; error code:%d\n", ret);
  409. return -EINVAL;
  410. }
  411. retries++;
  412. if (can_sleep && usec > 1000)
  413. msleep(usec / 1000);
  414. else if (usec < MAX_UDELAY_MS * 1000)
  415. udelay(usec);
  416. else
  417. mdelay(usec / 1000);
  418. if (retries < 22)
  419. usec *= 2;
  420. }
  421. return ret;
  422. }
  423. /*
  424. * vmbus_set_event - Send an event notification to the parent
  425. */
  426. void vmbus_set_event(struct vmbus_channel *channel)
  427. {
  428. u32 child_relid = channel->offermsg.child_relid;
  429. if (!channel->is_dedicated_interrupt)
  430. vmbus_send_interrupt(child_relid);
  431. ++channel->sig_events;
  432. if (ms_hyperv.paravisor_present) {
  433. if (hv_isolation_type_snp())
  434. hv_ghcb_hypercall(HVCALL_SIGNAL_EVENT, &channel->sig_event,
  435. NULL, sizeof(channel->sig_event));
  436. else if (hv_isolation_type_tdx())
  437. hv_tdx_hypercall(HVCALL_SIGNAL_EVENT | HV_HYPERCALL_FAST_BIT,
  438. channel->sig_event, 0);
  439. else
  440. WARN_ON_ONCE(1);
  441. } else {
  442. hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
  443. }
  444. }
  445. EXPORT_SYMBOL_GPL(vmbus_set_event);