Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'hyperv-next-signed' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux

Pull Hyper-V updates from Sasha Levin:

- Most of the commits here are work to enable host-initiated
hibernation support by Dexuan Cui.

- Fix for a warning shown when host sends non-aligned balloon requests
by Tianyu Lan.

* tag 'hyperv-next-signed' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux:
hv_utils: Add the support of hibernation
hv_utils: Support host-initiated hibernation request
hv_utils: Support host-initiated restart request
Tools: hv: Reopen the devices if read() or write() returns errors
video: hyperv: hyperv_fb: Use physical memory for fb on HyperV Gen 1 VMs.
Drivers: hv: vmbus: Ignore CHANNELMSG_TL_CONNECT_RESULT(23)
video: hyperv_fb: Fix hibernation for the deferred IO feature
Input: hyperv-keyboard: Add the support of hibernation
hv_balloon: Balloon up according to request page number

+574 -108
+7 -14
drivers/hv/channel_mgmt.c
··· 1351 1351 { CHANNELMSG_19, 0, NULL }, 1352 1352 { CHANNELMSG_20, 0, NULL }, 1353 1353 { CHANNELMSG_TL_CONNECT_REQUEST, 0, NULL }, 1354 + { CHANNELMSG_22, 0, NULL }, 1355 + { CHANNELMSG_TL_CONNECT_RESULT, 0, NULL }, 1354 1356 }; 1355 1357 1356 1358 /* ··· 1364 1362 { 1365 1363 struct hv_message *msg = context; 1366 1364 struct vmbus_channel_message_header *hdr; 1367 - int size; 1368 1365 1369 1366 hdr = (struct vmbus_channel_message_header *)msg->u.payload; 1370 - size = msg->header.payload_size; 1371 1367 1372 1368 trace_vmbus_on_message(hdr); 1373 1369 1374 - if (hdr->msgtype >= CHANNELMSG_COUNT) { 1375 - pr_err("Received invalid channel message type %d size %d\n", 1376 - hdr->msgtype, size); 1377 - print_hex_dump_bytes("", DUMP_PREFIX_NONE, 1378 - (unsigned char *)msg->u.payload, size); 1379 - return; 1380 - } 1381 - 1382 - if (channel_message_table[hdr->msgtype].message_handler) 1383 - channel_message_table[hdr->msgtype].message_handler(hdr); 1384 - else 1385 - pr_err("Unhandled channel message type %d\n", hdr->msgtype); 1370 + /* 1371 + * vmbus_on_msg_dpc() makes sure the hdr->msgtype here can not go 1372 + * out of bound and the message_handler pointer can not be NULL. 1373 + */ 1374 + channel_message_table[hdr->msgtype].message_handler(hdr); 1386 1375 } 1387 1376 1388 1377 /*
+3 -10
drivers/hv/hv_balloon.c
··· 1217 1217 unsigned int i, j; 1218 1218 struct page *pg; 1219 1219 1220 - if (num_pages < alloc_unit) 1221 - return 0; 1222 - 1223 - for (i = 0; (i * alloc_unit) < num_pages; i++) { 1220 + for (i = 0; i < num_pages / alloc_unit; i++) { 1224 1221 if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) > 1225 1222 HV_HYP_PAGE_SIZE) 1226 1223 return i * alloc_unit; ··· 1255 1258 1256 1259 } 1257 1260 1258 - return num_pages; 1261 + return i * alloc_unit; 1259 1262 } 1260 1263 1261 1264 static void balloon_up(struct work_struct *dummy) ··· 1270 1273 long avail_pages; 1271 1274 unsigned long floor; 1272 1275 1273 - /* The host balloons pages in 2M granularity. */ 1274 - WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0); 1275 - 1276 1276 /* 1277 1277 * We will attempt 2M allocations. However, if we fail to 1278 1278 * allocate 2M chunks, we will go back to PAGE_SIZE allocations. ··· 1279 1285 avail_pages = si_mem_available(); 1280 1286 floor = compute_balloon_floor(); 1281 1287 1282 - /* Refuse to balloon below the floor, keep the 2M granularity. */ 1288 + /* Refuse to balloon below the floor. */ 1283 1289 if (avail_pages < num_pages || avail_pages - num_pages < floor) { 1284 1290 pr_warn("Balloon request will be partially fulfilled. %s\n", 1285 1291 avail_pages < num_pages ? "Not enough memory." : 1286 1292 "Balloon floor reached."); 1287 1293 1288 1294 num_pages = avail_pages > floor ? (avail_pages - floor) : 0; 1289 - num_pages -= num_pages % PAGES_IN_2M; 1290 1295 } 1291 1296 1292 1297 while (!done) {
+53 -1
drivers/hv/hv_fcopy.c
··· 346 346 return 0; 347 347 } 348 348 349 + static void hv_fcopy_cancel_work(void) 350 + { 351 + cancel_delayed_work_sync(&fcopy_timeout_work); 352 + cancel_work_sync(&fcopy_send_work); 353 + } 354 + 355 + int hv_fcopy_pre_suspend(void) 356 + { 357 + struct vmbus_channel *channel = fcopy_transaction.recv_channel; 358 + struct hv_fcopy_hdr *fcopy_msg; 359 + 360 + /* 361 + * Fake a CANCEL_FCOPY message for the user space daemon in case the 362 + * daemon is in the middle of copying some file. It doesn't matter if 363 + * there is already a message pending to be delivered to the user 364 + * space since we force fcopy_transaction.state to be HVUTIL_READY, so 365 + * the user space daemon's write() will fail with EINVAL (see 366 + * fcopy_on_msg()), and the daemon will reset the device by closing 367 + * and re-opening it. 368 + */ 369 + fcopy_msg = kzalloc(sizeof(*fcopy_msg), GFP_KERNEL); 370 + if (!fcopy_msg) 371 + return -ENOMEM; 372 + 373 + tasklet_disable(&channel->callback_event); 374 + 375 + fcopy_msg->operation = CANCEL_FCOPY; 376 + 377 + hv_fcopy_cancel_work(); 378 + 379 + /* We don't care about the return value. */ 380 + hvutil_transport_send(hvt, fcopy_msg, sizeof(*fcopy_msg), NULL); 381 + 382 + kfree(fcopy_msg); 383 + 384 + fcopy_transaction.state = HVUTIL_READY; 385 + 386 + /* tasklet_enable() will be called in hv_fcopy_pre_resume(). */ 387 + return 0; 388 + } 389 + 390 + int hv_fcopy_pre_resume(void) 391 + { 392 + struct vmbus_channel *channel = fcopy_transaction.recv_channel; 393 + 394 + tasklet_enable(&channel->callback_event); 395 + 396 + return 0; 397 + } 398 + 349 399 void hv_fcopy_deinit(void) 350 400 { 351 401 fcopy_transaction.state = HVUTIL_DEVICE_DYING; 352 - cancel_delayed_work_sync(&fcopy_timeout_work); 402 + 403 + hv_fcopy_cancel_work(); 404 + 353 405 hvutil_transport_destroy(hvt); 354 406 }
+41 -2
drivers/hv/hv_kvp.c
··· 758 758 return 0; 759 759 } 760 760 761 - void hv_kvp_deinit(void) 761 + static void hv_kvp_cancel_work(void) 762 762 { 763 - kvp_transaction.state = HVUTIL_DEVICE_DYING; 764 763 cancel_delayed_work_sync(&kvp_host_handshake_work); 765 764 cancel_delayed_work_sync(&kvp_timeout_work); 766 765 cancel_work_sync(&kvp_sendkey_work); 766 + } 767 + 768 + int hv_kvp_pre_suspend(void) 769 + { 770 + struct vmbus_channel *channel = kvp_transaction.recv_channel; 771 + 772 + tasklet_disable(&channel->callback_event); 773 + 774 + /* 775 + * If there is a pending transtion, it's unnecessary to tell the host 776 + * that the transaction will fail, because that is implied when 777 + * util_suspend() calls vmbus_close() later. 778 + */ 779 + hv_kvp_cancel_work(); 780 + 781 + /* 782 + * Forece the state to READY to handle the ICMSGTYPE_NEGOTIATE message 783 + * later. The user space daemon may go out of order and its write() 784 + * may fail with EINVAL: this doesn't matter since the daemon will 785 + * reset the device by closing and re-opening it. 786 + */ 787 + kvp_transaction.state = HVUTIL_READY; 788 + return 0; 789 + } 790 + 791 + int hv_kvp_pre_resume(void) 792 + { 793 + struct vmbus_channel *channel = kvp_transaction.recv_channel; 794 + 795 + tasklet_enable(&channel->callback_event); 796 + 797 + return 0; 798 + } 799 + 800 + void hv_kvp_deinit(void) 801 + { 802 + kvp_transaction.state = HVUTIL_DEVICE_DYING; 803 + 804 + hv_kvp_cancel_work(); 805 + 767 806 hvutil_transport_destroy(hvt); 768 807 }
+53 -2
drivers/hv/hv_snapshot.c
··· 379 379 return 0; 380 380 } 381 381 382 + static void hv_vss_cancel_work(void) 383 + { 384 + cancel_delayed_work_sync(&vss_timeout_work); 385 + cancel_work_sync(&vss_handle_request_work); 386 + } 387 + 388 + int hv_vss_pre_suspend(void) 389 + { 390 + struct vmbus_channel *channel = vss_transaction.recv_channel; 391 + struct hv_vss_msg *vss_msg; 392 + 393 + /* 394 + * Fake a THAW message for the user space daemon in case the daemon 395 + * has frozen the file systems. It doesn't matter if there is already 396 + * a message pending to be delivered to the user space since we force 397 + * vss_transaction.state to be HVUTIL_READY, so the user space daemon's 398 + * write() will fail with EINVAL (see vss_on_msg()), and the daemon 399 + * will reset the device by closing and re-opening it. 400 + */ 401 + vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL); 402 + if (!vss_msg) 403 + return -ENOMEM; 404 + 405 + tasklet_disable(&channel->callback_event); 406 + 407 + vss_msg->vss_hdr.operation = VSS_OP_THAW; 408 + 409 + /* Cancel any possible pending work. */ 410 + hv_vss_cancel_work(); 411 + 412 + /* We don't care about the return value. */ 413 + hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg), NULL); 414 + 415 + kfree(vss_msg); 416 + 417 + vss_transaction.state = HVUTIL_READY; 418 + 419 + /* tasklet_enable() will be called in hv_vss_pre_resume(). */ 420 + return 0; 421 + } 422 + 423 + int hv_vss_pre_resume(void) 424 + { 425 + struct vmbus_channel *channel = vss_transaction.recv_channel; 426 + 427 + tasklet_enable(&channel->callback_event); 428 + 429 + return 0; 430 + } 431 + 382 432 void hv_vss_deinit(void) 383 433 { 384 434 vss_transaction.state = HVUTIL_DEVICE_DYING; 385 - cancel_delayed_work_sync(&vss_timeout_work); 386 - cancel_work_sync(&vss_handle_request_work); 435 + 436 + hv_vss_cancel_work(); 437 + 387 438 hvutil_transport_destroy(hvt); 388 439 }
+139 -9
drivers/hv/hv_util.c
··· 24 24 25 25 #define SD_MAJOR 3 26 26 #define SD_MINOR 0 27 + #define SD_MINOR_1 1 28 + #define SD_MINOR_2 2 29 + #define SD_VERSION_3_1 (SD_MAJOR << 16 | SD_MINOR_1) 30 + #define SD_VERSION_3_2 (SD_MAJOR << 16 | SD_MINOR_2) 27 31 #define SD_VERSION (SD_MAJOR << 16 | SD_MINOR) 28 32 29 33 #define SD_MAJOR_1 1 ··· 54 50 static int ts_srv_version; 55 51 static int hb_srv_version; 56 52 57 - #define SD_VER_COUNT 2 53 + #define SD_VER_COUNT 4 58 54 static const int sd_versions[] = { 55 + SD_VERSION_3_2, 56 + SD_VERSION_3_1, 59 57 SD_VERSION, 60 58 SD_VERSION_1 61 59 }; ··· 81 75 UTIL_WS2K8_FW_VERSION 82 76 }; 83 77 78 + /* 79 + * Send the "hibernate" udev event in a thread context. 80 + */ 81 + struct hibernate_work_context { 82 + struct work_struct work; 83 + struct hv_device *dev; 84 + }; 85 + 86 + static struct hibernate_work_context hibernate_context; 87 + static bool hibernation_supported; 88 + 89 + static void send_hibernate_uevent(struct work_struct *work) 90 + { 91 + char *uevent_env[2] = { "EVENT=hibernate", NULL }; 92 + struct hibernate_work_context *ctx; 93 + 94 + ctx = container_of(work, struct hibernate_work_context, work); 95 + 96 + kobject_uevent_env(&ctx->dev->device.kobj, KOBJ_CHANGE, uevent_env); 97 + 98 + pr_info("Sent hibernation uevent\n"); 99 + } 100 + 101 + static int hv_shutdown_init(struct hv_util_service *srv) 102 + { 103 + struct vmbus_channel *channel = srv->channel; 104 + 105 + INIT_WORK(&hibernate_context.work, send_hibernate_uevent); 106 + hibernate_context.dev = channel->device_obj; 107 + 108 + hibernation_supported = hv_is_hibernation_supported(); 109 + 110 + return 0; 111 + } 112 + 84 113 static void shutdown_onchannelcallback(void *context); 85 114 static struct hv_util_service util_shutdown = { 86 115 .util_cb = shutdown_onchannelcallback, 116 + .util_init = hv_shutdown_init, 87 117 }; 88 118 89 119 static int hv_timesync_init(struct hv_util_service *srv); 120 + static int hv_timesync_pre_suspend(void); 90 121 static void hv_timesync_deinit(void); 91 122 92 123 static void timesync_onchannelcallback(void *context); 93 124 static struct hv_util_service util_timesynch = { 94 125 .util_cb = timesync_onchannelcallback, 95 126 .util_init = hv_timesync_init, 127 + .util_pre_suspend = hv_timesync_pre_suspend, 96 128 .util_deinit = hv_timesync_deinit, 97 129 }; 98 130 ··· 142 98 static struct hv_util_service util_kvp = { 143 99 .util_cb = hv_kvp_onchannelcallback, 144 100 .util_init = hv_kvp_init, 101 + .util_pre_suspend = hv_kvp_pre_suspend, 102 + .util_pre_resume = hv_kvp_pre_resume, 145 103 .util_deinit = hv_kvp_deinit, 146 104 }; 147 105 148 106 static struct hv_util_service util_vss = { 149 107 .util_cb = hv_vss_onchannelcallback, 150 108 .util_init = hv_vss_init, 109 + .util_pre_suspend = hv_vss_pre_suspend, 110 + .util_pre_resume = hv_vss_pre_resume, 151 111 .util_deinit = hv_vss_deinit, 152 112 }; 153 113 154 114 static struct hv_util_service util_fcopy = { 155 115 .util_cb = hv_fcopy_onchannelcallback, 156 116 .util_init = hv_fcopy_init, 117 + .util_pre_suspend = hv_fcopy_pre_suspend, 118 + .util_pre_resume = hv_fcopy_pre_resume, 157 119 .util_deinit = hv_fcopy_deinit, 158 120 }; 159 121 ··· 168 118 orderly_poweroff(true); 169 119 } 170 120 121 + static void perform_restart(struct work_struct *dummy) 122 + { 123 + orderly_reboot(); 124 + } 125 + 171 126 /* 172 127 * Perform the shutdown operation in a thread context. 173 128 */ 174 129 static DECLARE_WORK(shutdown_work, perform_shutdown); 175 130 131 + /* 132 + * Perform the restart operation in a thread context. 133 + */ 134 + static DECLARE_WORK(restart_work, perform_restart); 135 + 176 136 static void shutdown_onchannelcallback(void *context) 177 137 { 178 138 struct vmbus_channel *channel = context; 139 + struct work_struct *work = NULL; 179 140 u32 recvlen; 180 141 u64 requestid; 181 - bool execute_shutdown = false; 182 142 u8 *shut_txf_buf = util_shutdown.recv_buffer; 183 143 184 144 struct shutdown_msg_data *shutdown_msg; ··· 217 157 sizeof(struct vmbuspipe_hdr) + 218 158 sizeof(struct icmsg_hdr)]; 219 159 160 + /* 161 + * shutdown_msg->flags can be 0(shut down), 2(reboot), 162 + * or 4(hibernate). It may bitwise-OR 1, which means 163 + * performing the request by force. Linux always tries 164 + * to perform the request by force. 165 + */ 220 166 switch (shutdown_msg->flags) { 221 167 case 0: 222 168 case 1: 223 169 icmsghdrp->status = HV_S_OK; 224 - execute_shutdown = true; 225 - 170 + work = &shutdown_work; 226 171 pr_info("Shutdown request received -" 227 172 " graceful shutdown initiated\n"); 228 173 break; 174 + case 2: 175 + case 3: 176 + icmsghdrp->status = HV_S_OK; 177 + work = &restart_work; 178 + pr_info("Restart request received -" 179 + " graceful restart initiated\n"); 180 + break; 181 + case 4: 182 + case 5: 183 + pr_info("Hibernation request received\n"); 184 + icmsghdrp->status = hibernation_supported ? 185 + HV_S_OK : HV_E_FAIL; 186 + if (hibernation_supported) 187 + work = &hibernate_context.work; 188 + break; 229 189 default: 230 190 icmsghdrp->status = HV_E_FAIL; 231 - execute_shutdown = false; 232 - 233 191 pr_info("Shutdown request received -" 234 192 " Invalid request\n"); 235 193 break; ··· 262 184 VM_PKT_DATA_INBAND, 0); 263 185 } 264 186 265 - if (execute_shutdown == true) 266 - schedule_work(&shutdown_work); 187 + if (work) 188 + schedule_work(work); 267 189 } 268 190 269 191 /* ··· 519 441 return 0; 520 442 } 521 443 444 + /* 445 + * When we're in util_suspend(), all the userspace processes have been frozen 446 + * (refer to hibernate() -> freeze_processes()). The userspace is thawed only 447 + * after the whole resume procedure, including util_resume(), finishes. 448 + */ 449 + static int util_suspend(struct hv_device *dev) 450 + { 451 + struct hv_util_service *srv = hv_get_drvdata(dev); 452 + int ret = 0; 453 + 454 + if (srv->util_pre_suspend) { 455 + ret = srv->util_pre_suspend(); 456 + if (ret) 457 + return ret; 458 + } 459 + 460 + vmbus_close(dev->channel); 461 + 462 + return 0; 463 + } 464 + 465 + static int util_resume(struct hv_device *dev) 466 + { 467 + struct hv_util_service *srv = hv_get_drvdata(dev); 468 + int ret = 0; 469 + 470 + if (srv->util_pre_resume) { 471 + ret = srv->util_pre_resume(); 472 + if (ret) 473 + return ret; 474 + } 475 + 476 + ret = vmbus_open(dev->channel, 4 * HV_HYP_PAGE_SIZE, 477 + 4 * HV_HYP_PAGE_SIZE, NULL, 0, srv->util_cb, 478 + dev->channel); 479 + return ret; 480 + } 481 + 522 482 static const struct hv_vmbus_device_id id_table[] = { 523 483 /* Shutdown guid */ 524 484 { HV_SHUTDOWN_GUID, ··· 593 477 .id_table = id_table, 594 478 .probe = util_probe, 595 479 .remove = util_remove, 480 + .suspend = util_suspend, 481 + .resume = util_resume, 596 482 .driver = { 597 483 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 598 484 }, ··· 664 546 return 0; 665 547 } 666 548 549 + static void hv_timesync_cancel_work(void) 550 + { 551 + cancel_work_sync(&adj_time_work); 552 + } 553 + 554 + static int hv_timesync_pre_suspend(void) 555 + { 556 + hv_timesync_cancel_work(); 557 + return 0; 558 + } 559 + 667 560 static void hv_timesync_deinit(void) 668 561 { 669 562 if (hv_ptp_clock) 670 563 ptp_clock_unregister(hv_ptp_clock); 671 - cancel_work_sync(&adj_time_work); 564 + 565 + hv_timesync_cancel_work(); 672 566 } 673 567 674 568 static int __init init_hyperv_utils(void)
+6
drivers/hv/hyperv_vmbus.h
··· 352 352 353 353 int hv_kvp_init(struct hv_util_service *srv); 354 354 void hv_kvp_deinit(void); 355 + int hv_kvp_pre_suspend(void); 356 + int hv_kvp_pre_resume(void); 355 357 void hv_kvp_onchannelcallback(void *context); 356 358 357 359 int hv_vss_init(struct hv_util_service *srv); 358 360 void hv_vss_deinit(void); 361 + int hv_vss_pre_suspend(void); 362 + int hv_vss_pre_resume(void); 359 363 void hv_vss_onchannelcallback(void *context); 360 364 361 365 int hv_fcopy_init(struct hv_util_service *srv); 362 366 void hv_fcopy_deinit(void); 367 + int hv_fcopy_pre_suspend(void); 368 + int hv_fcopy_pre_resume(void); 363 369 void hv_fcopy_onchannelcallback(void *context); 364 370 void vmbus_initiate_unload(bool crash); 365 371
+4
drivers/hv/vmbus_drv.c
··· 1033 1033 } 1034 1034 1035 1035 entry = &channel_message_table[hdr->msgtype]; 1036 + 1037 + if (!entry->message_handler) 1038 + goto msg_handled; 1039 + 1036 1040 if (entry->handler_type == VMHT_BLOCKING) { 1037 1041 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC); 1038 1042 if (ctx == NULL)
+27
drivers/input/serio/hyperv-keyboard.c
··· 259 259 u32 proto_status; 260 260 int error; 261 261 262 + reinit_completion(&kbd_dev->wait_event); 263 + 262 264 request = &kbd_dev->protocol_req; 263 265 memset(request, 0, sizeof(struct synth_kbd_protocol_request)); 264 266 request->header.type = __cpu_to_le32(SYNTH_KBD_PROTOCOL_REQUEST); ··· 382 380 return 0; 383 381 } 384 382 383 + static int hv_kbd_suspend(struct hv_device *hv_dev) 384 + { 385 + vmbus_close(hv_dev->channel); 386 + 387 + return 0; 388 + } 389 + 390 + static int hv_kbd_resume(struct hv_device *hv_dev) 391 + { 392 + int ret; 393 + 394 + ret = vmbus_open(hv_dev->channel, 395 + KBD_VSC_SEND_RING_BUFFER_SIZE, 396 + KBD_VSC_RECV_RING_BUFFER_SIZE, 397 + NULL, 0, 398 + hv_kbd_on_channel_callback, 399 + hv_dev); 400 + if (ret == 0) 401 + ret = hv_kbd_connect_to_vsp(hv_dev); 402 + 403 + return ret; 404 + } 405 + 385 406 static const struct hv_vmbus_device_id id_table[] = { 386 407 /* Keyboard guid */ 387 408 { HV_KBD_GUID, }, ··· 418 393 .id_table = id_table, 419 394 .probe = hv_kbd_probe, 420 395 .remove = hv_kbd_remove, 396 + .suspend = hv_kbd_suspend, 397 + .resume = hv_kbd_resume, 421 398 .driver = { 422 399 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 423 400 },
+1
drivers/video/fbdev/Kconfig
··· 2215 2215 select FB_CFB_COPYAREA 2216 2216 select FB_CFB_IMAGEBLIT 2217 2217 select FB_DEFERRED_IO 2218 + select DMA_CMA if HAVE_DMA_CONTIGUOUS && CMA 2218 2219 help 2219 2220 This framebuffer driver supports Microsoft Hyper-V Synthetic Video. 2220 2221
+145 -39
drivers/video/fbdev/hyperv_fb.c
··· 31 31 * "set-vmvideo" command. For example 32 32 * set-vmvideo -vmname name -horizontalresolution:1920 \ 33 33 * -verticalresolution:1200 -resolutiontype single 34 + * 35 + * Gen 1 VMs also support direct using VM's physical memory for framebuffer. 36 + * It could improve the efficiency and performance for framebuffer and VM. 37 + * This requires to allocate contiguous physical memory from Linux kernel's 38 + * CMA memory allocator. To enable this, supply a kernel parameter to give 39 + * enough memory space to CMA allocator for framebuffer. For example: 40 + * cma=130m 41 + * This gives 130MB memory to CMA allocator that can be allocated to 42 + * framebuffer. For reference, 8K resolution (7680x4320) takes about 43 + * 127MB memory. 34 44 */ 35 45 36 46 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt ··· 238 228 } __packed; 239 229 240 230 241 - 242 231 /* FB driver definitions and structures */ 243 232 #define HVFB_WIDTH 1152 /* default screen width */ 244 233 #define HVFB_HEIGHT 864 /* default screen height */ ··· 267 258 /* If true, the VSC notifies the VSP on every framebuffer change */ 268 259 bool synchronous_fb; 269 260 261 + /* If true, need to copy from deferred IO mem to framebuffer mem */ 262 + bool need_docopy; 263 + 270 264 struct notifier_block hvfb_panic_nb; 271 265 272 266 /* Memory for deferred IO and frame buffer itself */ 273 267 unsigned char *dio_vp; 274 268 unsigned char *mmio_vp; 275 - unsigned long mmio_pp; 269 + phys_addr_t mmio_pp; 276 270 277 271 /* Dirty rectangle, protected by delayed_refresh_lock */ 278 272 int x1, y1, x2, y2; ··· 446 434 maxy = max_t(int, maxy, y2); 447 435 448 436 /* Copy from dio space to mmio address */ 449 - if (par->fb_ready) 437 + if (par->fb_ready && par->need_docopy) 450 438 hvfb_docopy(par, start, PAGE_SIZE); 451 439 } 452 440 ··· 763 751 return; 764 752 765 753 /* Copy the dirty rectangle to frame buffer memory */ 766 - for (j = y1; j < y2; j++) { 767 - hvfb_docopy(par, 768 - j * info->fix.line_length + 769 - (x1 * screen_depth / 8), 770 - (x2 - x1) * screen_depth / 8); 771 - } 754 + if (par->need_docopy) 755 + for (j = y1; j < y2; j++) 756 + hvfb_docopy(par, 757 + j * info->fix.line_length + 758 + (x1 * screen_depth / 8), 759 + (x2 - x1) * screen_depth / 8); 772 760 773 761 /* Refresh */ 774 762 if (par->fb_ready && par->update) ··· 813 801 par = container_of(nb, struct hvfb_par, hvfb_panic_nb); 814 802 par->synchronous_fb = true; 815 803 info = par->info; 816 - hvfb_docopy(par, 0, dio_fb_size); 804 + if (par->need_docopy) 805 + hvfb_docopy(par, 0, dio_fb_size); 817 806 synthvid_update(info, 0, 0, INT_MAX, INT_MAX); 818 807 819 808 return NOTIFY_DONE; ··· 953 940 return; 954 941 } 955 942 943 + /* 944 + * Allocate enough contiguous physical memory. 945 + * Return physical address if succeeded or -1 if failed. 946 + */ 947 + static phys_addr_t hvfb_get_phymem(struct hv_device *hdev, 948 + unsigned int request_size) 949 + { 950 + struct page *page = NULL; 951 + dma_addr_t dma_handle; 952 + void *vmem; 953 + phys_addr_t paddr = 0; 954 + unsigned int order = get_order(request_size); 955 + 956 + if (request_size == 0) 957 + return -1; 958 + 959 + if (order < MAX_ORDER) { 960 + /* Call alloc_pages if the size is less than 2^MAX_ORDER */ 961 + page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); 962 + if (!page) 963 + return -1; 964 + 965 + paddr = (page_to_pfn(page) << PAGE_SHIFT); 966 + } else { 967 + /* Allocate from CMA */ 968 + hdev->device.coherent_dma_mask = DMA_BIT_MASK(64); 969 + 970 + vmem = dma_alloc_coherent(&hdev->device, 971 + round_up(request_size, PAGE_SIZE), 972 + &dma_handle, 973 + GFP_KERNEL | __GFP_NOWARN); 974 + 975 + if (!vmem) 976 + return -1; 977 + 978 + paddr = virt_to_phys(vmem); 979 + } 980 + 981 + return paddr; 982 + } 983 + 984 + /* Release contiguous physical memory */ 985 + static void hvfb_release_phymem(struct hv_device *hdev, 986 + phys_addr_t paddr, unsigned int size) 987 + { 988 + unsigned int order = get_order(size); 989 + 990 + if (order < MAX_ORDER) 991 + __free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order); 992 + else 993 + dma_free_coherent(&hdev->device, 994 + round_up(size, PAGE_SIZE), 995 + phys_to_virt(paddr), 996 + paddr); 997 + } 998 + 956 999 957 1000 /* Get framebuffer memory from Hyper-V video pci space */ 958 1001 static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) ··· 1018 949 void __iomem *fb_virt; 1019 950 int gen2vm = efi_enabled(EFI_BOOT); 1020 951 resource_size_t pot_start, pot_end; 952 + phys_addr_t paddr; 1021 953 int ret; 1022 954 955 + info->apertures = alloc_apertures(1); 956 + if (!info->apertures) 957 + return -ENOMEM; 958 + 959 + if (!gen2vm) { 960 + pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT, 961 + PCI_DEVICE_ID_HYPERV_VIDEO, NULL); 962 + if (!pdev) { 963 + pr_err("Unable to find PCI Hyper-V video\n"); 964 + kfree(info->apertures); 965 + return -ENODEV; 966 + } 967 + 968 + info->apertures->ranges[0].base = pci_resource_start(pdev, 0); 969 + info->apertures->ranges[0].size = pci_resource_len(pdev, 0); 970 + 971 + /* 972 + * For Gen 1 VM, we can directly use the contiguous memory 973 + * from VM. If we succeed, deferred IO happens directly 974 + * on this allocated framebuffer memory, avoiding extra 975 + * memory copy. 976 + */ 977 + paddr = hvfb_get_phymem(hdev, screen_fb_size); 978 + if (paddr != (phys_addr_t) -1) { 979 + par->mmio_pp = paddr; 980 + par->mmio_vp = par->dio_vp = __va(paddr); 981 + 982 + info->fix.smem_start = paddr; 983 + info->fix.smem_len = screen_fb_size; 984 + info->screen_base = par->mmio_vp; 985 + info->screen_size = screen_fb_size; 986 + 987 + par->need_docopy = false; 988 + goto getmem_done; 989 + } 990 + pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n"); 991 + } else { 992 + info->apertures->ranges[0].base = screen_info.lfb_base; 993 + info->apertures->ranges[0].size = screen_info.lfb_size; 994 + } 995 + 996 + /* 997 + * Cannot use the contiguous physical memory. 998 + * Allocate mmio space for framebuffer. 999 + */ 1023 1000 dio_fb_size = 1024 1001 screen_width * screen_height * screen_depth / 8; 1025 1002 ··· 1073 958 pot_start = 0; 1074 959 pot_end = -1; 1075 960 } else { 1076 - pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT, 1077 - PCI_DEVICE_ID_HYPERV_VIDEO, NULL); 1078 - if (!pdev) { 1079 - pr_err("Unable to find PCI Hyper-V video\n"); 1080 - return -ENODEV; 1081 - } 1082 - 1083 961 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) || 1084 962 pci_resource_len(pdev, 0) < screen_fb_size) { 1085 963 pr_err("Resource not available or (0x%lx < 0x%lx)\n", ··· 1101 993 if (par->dio_vp == NULL) 1102 994 goto err3; 1103 995 1104 - info->apertures = alloc_apertures(1); 1105 - if (!info->apertures) 1106 - goto err4; 1107 - 1108 - if (gen2vm) { 1109 - info->apertures->ranges[0].base = screen_info.lfb_base; 1110 - info->apertures->ranges[0].size = screen_info.lfb_size; 1111 - remove_conflicting_framebuffers(info->apertures, 1112 - KBUILD_MODNAME, false); 1113 - } else { 1114 - info->apertures->ranges[0].base = pci_resource_start(pdev, 0); 1115 - info->apertures->ranges[0].size = pci_resource_len(pdev, 0); 1116 - } 1117 - 1118 996 /* Physical address of FB device */ 1119 997 par->mmio_pp = par->mem->start; 1120 998 /* Virtual address of FB device */ ··· 1111 1017 info->screen_base = par->dio_vp; 1112 1018 info->screen_size = dio_fb_size; 1113 1019 1020 + getmem_done: 1021 + remove_conflicting_framebuffers(info->apertures, 1022 + KBUILD_MODNAME, false); 1114 1023 if (!gen2vm) 1115 1024 pci_dev_put(pdev); 1025 + kfree(info->apertures); 1116 1026 1117 1027 return 0; 1118 1028 1119 - err4: 1120 - vfree(par->dio_vp); 1121 1029 err3: 1122 1030 iounmap(fb_virt); 1123 1031 err2: ··· 1128 1032 err1: 1129 1033 if (!gen2vm) 1130 1034 pci_dev_put(pdev); 1035 + kfree(info->apertures); 1131 1036 1132 1037 return -ENOMEM; 1133 1038 } 1134 1039 1135 1040 /* Release the framebuffer */ 1136 - static void hvfb_putmem(struct fb_info *info) 1041 + static void hvfb_putmem(struct hv_device *hdev, struct fb_info *info) 1137 1042 { 1138 1043 struct hvfb_par *par = info->par; 1139 1044 1140 - vfree(par->dio_vp); 1141 - iounmap(info->screen_base); 1142 - vmbus_free_mmio(par->mem->start, screen_fb_size); 1045 + if (par->need_docopy) { 1046 + vfree(par->dio_vp); 1047 + iounmap(info->screen_base); 1048 + vmbus_free_mmio(par->mem->start, screen_fb_size); 1049 + } else { 1050 + hvfb_release_phymem(hdev, info->fix.smem_start, 1051 + screen_fb_size); 1052 + } 1053 + 1143 1054 par->mem = NULL; 1144 1055 } 1145 1056 ··· 1165 1062 par = info->par; 1166 1063 par->info = info; 1167 1064 par->fb_ready = false; 1065 + par->need_docopy = true; 1168 1066 init_completion(&par->wait); 1169 1067 INIT_DELAYED_WORK(&par->dwork, hvfb_update_work); 1170 1068 ··· 1251 1147 1252 1148 error: 1253 1149 fb_deferred_io_cleanup(info); 1254 - hvfb_putmem(info); 1150 + hvfb_putmem(hdev, info); 1255 1151 error2: 1256 1152 vmbus_close(hdev->channel); 1257 1153 error1: ··· 1281 1177 vmbus_close(hdev->channel); 1282 1178 hv_set_drvdata(hdev, NULL); 1283 1179 1284 - hvfb_putmem(info); 1180 + hvfb_putmem(hdev, info); 1285 1181 framebuffer_release(info); 1286 1182 1287 1183 return 0; ··· 1298 1194 fb_set_suspend(info, 1); 1299 1195 1300 1196 cancel_delayed_work_sync(&par->dwork); 1197 + cancel_delayed_work_sync(&info->deferred_work); 1301 1198 1302 1199 par->update_saved = par->update; 1303 1200 par->update = false; ··· 1332 1227 par->fb_ready = true; 1333 1228 par->update = par->update_saved; 1334 1229 1230 + schedule_delayed_work(&info->deferred_work, info->fbdefio->delay); 1335 1231 schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY); 1336 1232 1337 1233 /* 0 means do resume */
+4
include/linux/hyperv.h
··· 425 425 CHANNELMSG_19 = 19, 426 426 CHANNELMSG_20 = 20, 427 427 CHANNELMSG_TL_CONNECT_REQUEST = 21, 428 + CHANNELMSG_22 = 22, 429 + CHANNELMSG_TL_CONNECT_RESULT = 23, 428 430 CHANNELMSG_COUNT 429 431 }; 430 432 ··· 1435 1433 void (*util_cb)(void *); 1436 1434 int (*util_init)(struct hv_util_service *); 1437 1435 void (*util_deinit)(void); 1436 + int (*util_pre_suspend)(void); 1437 + int (*util_pre_resume)(void); 1438 1438 }; 1439 1439 1440 1440 struct vmbuspipe_hdr {
+32 -5
tools/hv/hv_fcopy_daemon.c
··· 80 80 81 81 error = 0; 82 82 done: 83 + if (error) 84 + target_fname[0] = '\0'; 83 85 return error; 84 86 } 85 87 ··· 110 108 return ret; 111 109 } 112 110 111 + /* 112 + * Reset target_fname to "" in the two below functions for hibernation: if 113 + * the fcopy operation is aborted by hibernation, the daemon should remove the 114 + * partially-copied file; to achieve this, the hv_utils driver always fakes a 115 + * CANCEL_FCOPY message upon suspend, and later when the VM resumes back, 116 + * the daemon calls hv_copy_cancel() to remove the file; if a file is copied 117 + * successfully before suspend, hv_copy_finished() must reset target_fname to 118 + * avoid that the file can be incorrectly removed upon resume, since the faked 119 + * CANCEL_FCOPY message is spurious in this case. 120 + */ 113 121 static int hv_copy_finished(void) 114 122 { 115 123 close(target_fd); 124 + target_fname[0] = '\0'; 116 125 return 0; 117 126 } 118 127 static int hv_copy_cancel(void) 119 128 { 120 129 close(target_fd); 121 - unlink(target_fname); 130 + if (strlen(target_fname) > 0) { 131 + unlink(target_fname); 132 + target_fname[0] = '\0'; 133 + } 122 134 return 0; 123 135 124 136 } ··· 147 131 148 132 int main(int argc, char *argv[]) 149 133 { 150 - int fcopy_fd; 134 + int fcopy_fd = -1; 151 135 int error; 152 136 int daemonize = 1, long_index = 0, opt; 153 137 int version = FCOPY_CURRENT_VERSION; ··· 157 141 struct hv_do_fcopy copy; 158 142 __u32 kernel_modver; 159 143 } buffer = { }; 160 - int in_handshake = 1; 144 + int in_handshake; 161 145 162 146 static struct option long_options[] = { 163 147 {"help", no_argument, 0, 'h' }, ··· 186 170 openlog("HV_FCOPY", 0, LOG_USER); 187 171 syslog(LOG_INFO, "starting; pid is:%d", getpid()); 188 172 173 + reopen_fcopy_fd: 174 + if (fcopy_fd != -1) 175 + close(fcopy_fd); 176 + /* Remove any possible partially-copied file on error */ 177 + hv_copy_cancel(); 178 + in_handshake = 1; 189 179 fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR); 190 180 191 181 if (fcopy_fd < 0) { ··· 218 196 len = pread(fcopy_fd, &buffer, sizeof(buffer), 0); 219 197 if (len < 0) { 220 198 syslog(LOG_ERR, "pread failed: %s", strerror(errno)); 221 - exit(EXIT_FAILURE); 199 + goto reopen_fcopy_fd; 222 200 } 223 201 224 202 if (in_handshake) { ··· 253 231 254 232 } 255 233 234 + /* 235 + * pwrite() may return an error due to the faked CANCEL_FCOPY 236 + * message upon hibernation. Ignore the error by resetting the 237 + * dev file, i.e. closing and re-opening it. 238 + */ 256 239 if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) { 257 240 syslog(LOG_ERR, "pwrite failed: %s", strerror(errno)); 258 - exit(EXIT_FAILURE); 241 + goto reopen_fcopy_fd; 259 242 } 260 243 } 261 244 }
+21 -15
tools/hv/hv_kvp_daemon.c
··· 76 76 DNS 77 77 }; 78 78 79 - static int in_hand_shake = 1; 79 + static int in_hand_shake; 80 80 81 81 static char *os_name = ""; 82 82 static char *os_major = ""; ··· 1360 1360 1361 1361 int main(int argc, char *argv[]) 1362 1362 { 1363 - int kvp_fd, len; 1363 + int kvp_fd = -1, len; 1364 1364 int error; 1365 1365 struct pollfd pfd; 1366 1366 char *p; ··· 1400 1400 openlog("KVP", 0, LOG_USER); 1401 1401 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid()); 1402 1402 1403 - kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC); 1404 - 1405 - if (kvp_fd < 0) { 1406 - syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s", 1407 - errno, strerror(errno)); 1408 - exit(EXIT_FAILURE); 1409 - } 1410 - 1411 1403 /* 1412 1404 * Retrieve OS release information. 1413 1405 */ ··· 1412 1420 1413 1421 if (kvp_file_init()) { 1414 1422 syslog(LOG_ERR, "Failed to initialize the pools"); 1423 + exit(EXIT_FAILURE); 1424 + } 1425 + 1426 + reopen_kvp_fd: 1427 + if (kvp_fd != -1) 1428 + close(kvp_fd); 1429 + in_hand_shake = 1; 1430 + kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC); 1431 + 1432 + if (kvp_fd < 0) { 1433 + syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s", 1434 + errno, strerror(errno)); 1415 1435 exit(EXIT_FAILURE); 1416 1436 } 1417 1437 ··· 1460 1456 if (len != sizeof(struct hv_kvp_msg)) { 1461 1457 syslog(LOG_ERR, "read failed; error:%d %s", 1462 1458 errno, strerror(errno)); 1463 - 1464 - close(kvp_fd); 1465 - return EXIT_FAILURE; 1459 + goto reopen_kvp_fd; 1466 1460 } 1467 1461 1468 1462 /* ··· 1619 1617 break; 1620 1618 } 1621 1619 1622 - /* Send the value back to the kernel. */ 1620 + /* 1621 + * Send the value back to the kernel. Note: the write() may 1622 + * return an error due to hibernation; we can ignore the error 1623 + * by resetting the dev file, i.e. closing and re-opening it. 1624 + */ 1623 1625 kvp_done: 1624 1626 len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg)); 1625 1627 if (len != sizeof(struct hv_kvp_msg)) { 1626 1628 syslog(LOG_ERR, "write failed; error: %d %s", errno, 1627 1629 strerror(errno)); 1628 - exit(EXIT_FAILURE); 1630 + goto reopen_kvp_fd; 1629 1631 } 1630 1632 } 1631 1633
+38 -11
tools/hv/hv_vss_daemon.c
··· 28 28 #include <stdbool.h> 29 29 #include <dirent.h> 30 30 31 + static bool fs_frozen; 32 + 31 33 /* Don't use syslog() in the function since that can cause write to disk */ 32 34 static int vss_do_freeze(char *dir, unsigned int cmd) 33 35 { ··· 157 155 continue; 158 156 } 159 157 error |= vss_do_freeze(ent->mnt_dir, cmd); 160 - if (error && operation == VSS_OP_FREEZE) 161 - goto err; 158 + if (operation == VSS_OP_FREEZE) { 159 + if (error) 160 + goto err; 161 + fs_frozen = true; 162 + } 162 163 } 163 164 164 165 endmntent(mounts); 165 166 166 167 if (root_seen) { 167 168 error |= vss_do_freeze("/", cmd); 168 - if (error && operation == VSS_OP_FREEZE) 169 - goto err; 169 + if (operation == VSS_OP_FREEZE) { 170 + if (error) 171 + goto err; 172 + fs_frozen = true; 173 + } 170 174 } 175 + 176 + if (operation == VSS_OP_THAW && !error) 177 + fs_frozen = false; 171 178 172 179 goto out; 173 180 err: ··· 186 175 endmntent(mounts); 187 176 } 188 177 vss_operate(VSS_OP_THAW); 178 + fs_frozen = false; 189 179 /* Call syslog after we thaw all filesystems */ 190 180 if (ent) 191 181 syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s", ··· 208 196 209 197 int main(int argc, char *argv[]) 210 198 { 211 - int vss_fd, len; 199 + int vss_fd = -1, len; 212 200 int error; 213 201 struct pollfd pfd; 214 202 int op; 215 203 struct hv_vss_msg vss_msg[1]; 216 204 int daemonize = 1, long_index = 0, opt; 217 - int in_handshake = 1; 205 + int in_handshake; 218 206 __u32 kernel_modver; 219 207 220 208 static struct option long_options[] = { ··· 244 232 openlog("Hyper-V VSS", 0, LOG_USER); 245 233 syslog(LOG_INFO, "VSS starting; pid is:%d", getpid()); 246 234 235 + reopen_vss_fd: 236 + if (vss_fd != -1) 237 + close(vss_fd); 238 + if (fs_frozen) { 239 + if (vss_operate(VSS_OP_THAW) || fs_frozen) { 240 + syslog(LOG_ERR, "failed to thaw file system: err=%d", 241 + errno); 242 + exit(EXIT_FAILURE); 243 + } 244 + } 245 + 246 + in_handshake = 1; 247 247 vss_fd = open("/dev/vmbus/hv_vss", O_RDWR); 248 248 if (vss_fd < 0) { 249 249 syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s", ··· 308 284 if (len != sizeof(struct hv_vss_msg)) { 309 285 syslog(LOG_ERR, "read failed; error:%d %s", 310 286 errno, strerror(errno)); 311 - close(vss_fd); 312 - return EXIT_FAILURE; 287 + goto reopen_vss_fd; 313 288 } 314 289 315 290 op = vss_msg->vss_hdr.operation; ··· 335 312 default: 336 313 syslog(LOG_ERR, "Illegal op:%d\n", op); 337 314 } 315 + 316 + /* 317 + * The write() may return an error due to the faked VSS_OP_THAW 318 + * message upon hibernation. Ignore the error by resetting the 319 + * dev file, i.e. closing and re-opening it. 320 + */ 338 321 vss_msg->error = error; 339 322 len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg)); 340 323 if (len != sizeof(struct hv_vss_msg)) { 341 324 syslog(LOG_ERR, "write failed; error: %d %s", errno, 342 325 strerror(errno)); 343 - 344 - if (op == VSS_OP_FREEZE) 345 - vss_operate(VSS_OP_THAW); 326 + goto reopen_vss_fd; 346 327 } 347 328 } 348 329