virtio-balloon: tweak config_changed implementation

virtio-ccw has deadlock issues with reading the config space inside the
interrupt context, so we tweak the virtballoon_changed implementation
by moving the config read operations into the related workqueue contexts.
The config_read_bitmap is used as a flag to the workqueue callbacks
about the related config fields that need to be read.

The cmd_id_received is also renamed to cmd_id_received_cache, and
the value should be obtained via virtio_balloon_cmd_id_received.

Reported-by: Christian Borntraeger <borntraeger@de.ibm.com>
Signed-off-by: Wei Wang <wei.w.wang@intel.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Halil Pasic <pasic@linux.ibm.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Cc: stable@vger.kernel.org
Fixes: 86a559787e6f ("virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT")
Tested-by: Christian Borntraeger <borntraeger@de.ibm.com>

authored by Wei Wang and committed by Michael S. Tsirkin bf4dc0b2 a229989d

+65 -33
+65 -33
drivers/virtio/virtio_balloon.c
··· 61 VIRTIO_BALLOON_VQ_MAX 62 }; 63 64 struct virtio_balloon { 65 struct virtio_device *vdev; 66 struct virtqueue *inflate_vq, *deflate_vq, *stats_vq, *free_page_vq; ··· 81 /* Prevent updating balloon when it is being canceled. */ 82 spinlock_t stop_update_lock; 83 bool stop_update; 84 85 /* The list of allocated free pages, waiting to be given back to mm */ 86 struct list_head free_page_list; 87 spinlock_t free_page_list_lock; 88 /* The number of free page blocks on the above list */ 89 unsigned long num_free_page_blocks; 90 - /* The cmd id received from host */ 91 - u32 cmd_id_received; 92 /* The cmd id that is actively in use */ 93 __virtio32 cmd_id_active; 94 /* Buffer to store the stop sign */ ··· 400 return num_returned; 401 } 402 403 static void virtballoon_changed(struct virtio_device *vdev) 404 { 405 struct virtio_balloon *vb = vdev->priv; 406 unsigned long flags; 407 - s64 diff = towards_target(vb); 408 409 - if (diff) { 410 - spin_lock_irqsave(&vb->stop_update_lock, flags); 411 - if (!vb->stop_update) 412 - queue_work(system_freezable_wq, 413 - &vb->update_balloon_size_work); 414 - spin_unlock_irqrestore(&vb->stop_update_lock, flags); 415 } 416 - 417 - if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) { 418 - virtio_cread(vdev, struct virtio_balloon_config, 419 - free_page_report_cmd_id, &vb->cmd_id_received); 420 - if (vb->cmd_id_received == VIRTIO_BALLOON_CMD_ID_DONE) { 421 - /* Pass ULONG_MAX to give back all the free pages */ 422 - return_free_pages_to_mm(vb, ULONG_MAX); 423 - } else if (vb->cmd_id_received != VIRTIO_BALLOON_CMD_ID_STOP && 424 - vb->cmd_id_received != 425 - virtio32_to_cpu(vdev, vb->cmd_id_active)) { 426 - spin_lock_irqsave(&vb->stop_update_lock, flags); 427 - if (!vb->stop_update) { 428 - queue_work(vb->balloon_wq, 429 - &vb->report_free_page_work); 430 - } 431 - spin_unlock_irqrestore(&vb->stop_update_lock, flags); 432 - } 433 - } 434 } 435 436 static void update_balloon_size(struct virtio_balloon *vb) ··· 531 return 0; 532 } 533 534 static int send_cmd_id_start(struct virtio_balloon *vb) 535 { 536 struct scatterlist sg; ··· 552 while (virtqueue_get_buf(vq, &unused)) 553 ; 554 555 - vb->cmd_id_active = cpu_to_virtio32(vb->vdev, vb->cmd_id_received); 556 sg_init_one(&sg, &vb->cmd_id_active, sizeof(vb->cmd_id_active)); 557 err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_active, GFP_KERNEL); 558 if (!err) ··· 636 * stop the reporting. 637 */ 638 cmd_id_active = virtio32_to_cpu(vb->vdev, vb->cmd_id_active); 639 - if (cmd_id_active != vb->cmd_id_received) 640 break; 641 642 /* ··· 654 return 0; 655 } 656 657 - static void report_free_page_func(struct work_struct *work) 658 { 659 int err; 660 - struct virtio_balloon *vb = container_of(work, struct virtio_balloon, 661 - report_free_page_work); 662 struct device *dev = &vb->vdev->dev; 663 664 /* Start by sending the received cmd id to host with an outbuf. */ ··· 672 err = send_cmd_id_stop(vb); 673 if (unlikely(err)) 674 dev_err(dev, "Failed to send a stop id, err = %d\n", err); 675 } 676 677 #ifdef CONFIG_BALLOON_COMPACTION ··· 917 goto out_del_vqs; 918 } 919 INIT_WORK(&vb->report_free_page_work, report_free_page_func); 920 - vb->cmd_id_received = VIRTIO_BALLOON_CMD_ID_STOP; 921 vb->cmd_id_active = cpu_to_virtio32(vb->vdev, 922 VIRTIO_BALLOON_CMD_ID_STOP); 923 vb->cmd_id_stop = cpu_to_virtio32(vb->vdev,
··· 61 VIRTIO_BALLOON_VQ_MAX 62 }; 63 64 + enum virtio_balloon_config_read { 65 + VIRTIO_BALLOON_CONFIG_READ_CMD_ID = 0, 66 + }; 67 + 68 struct virtio_balloon { 69 struct virtio_device *vdev; 70 struct virtqueue *inflate_vq, *deflate_vq, *stats_vq, *free_page_vq; ··· 77 /* Prevent updating balloon when it is being canceled. */ 78 spinlock_t stop_update_lock; 79 bool stop_update; 80 + /* Bitmap to indicate if reading the related config fields are needed */ 81 + unsigned long config_read_bitmap; 82 83 /* The list of allocated free pages, waiting to be given back to mm */ 84 struct list_head free_page_list; 85 spinlock_t free_page_list_lock; 86 /* The number of free page blocks on the above list */ 87 unsigned long num_free_page_blocks; 88 + /* 89 + * The cmd id received from host. 90 + * Read it via virtio_balloon_cmd_id_received to get the latest value 91 + * sent from host. 92 + */ 93 + u32 cmd_id_received_cache; 94 /* The cmd id that is actively in use */ 95 __virtio32 cmd_id_active; 96 /* Buffer to store the stop sign */ ··· 390 return num_returned; 391 } 392 393 + static void virtio_balloon_queue_free_page_work(struct virtio_balloon *vb) 394 + { 395 + if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) 396 + return; 397 + 398 + /* No need to queue the work if the bit was already set. */ 399 + if (test_and_set_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID, 400 + &vb->config_read_bitmap)) 401 + return; 402 + 403 + queue_work(vb->balloon_wq, &vb->report_free_page_work); 404 + } 405 + 406 static void virtballoon_changed(struct virtio_device *vdev) 407 { 408 struct virtio_balloon *vb = vdev->priv; 409 unsigned long flags; 410 411 + spin_lock_irqsave(&vb->stop_update_lock, flags); 412 + if (!vb->stop_update) { 413 + queue_work(system_freezable_wq, 414 + &vb->update_balloon_size_work); 415 + virtio_balloon_queue_free_page_work(vb); 416 } 417 + spin_unlock_irqrestore(&vb->stop_update_lock, flags); 418 } 419 420 static void update_balloon_size(struct virtio_balloon *vb) ··· 527 return 0; 528 } 529 530 + static u32 virtio_balloon_cmd_id_received(struct virtio_balloon *vb) 531 + { 532 + if (test_and_clear_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID, 533 + &vb->config_read_bitmap)) 534 + virtio_cread(vb->vdev, struct virtio_balloon_config, 535 + free_page_report_cmd_id, 536 + &vb->cmd_id_received_cache); 537 + 538 + return vb->cmd_id_received_cache; 539 + } 540 + 541 static int send_cmd_id_start(struct virtio_balloon *vb) 542 { 543 struct scatterlist sg; ··· 537 while (virtqueue_get_buf(vq, &unused)) 538 ; 539 540 + vb->cmd_id_active = virtio32_to_cpu(vb->vdev, 541 + virtio_balloon_cmd_id_received(vb)); 542 sg_init_one(&sg, &vb->cmd_id_active, sizeof(vb->cmd_id_active)); 543 err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_active, GFP_KERNEL); 544 if (!err) ··· 620 * stop the reporting. 621 */ 622 cmd_id_active = virtio32_to_cpu(vb->vdev, vb->cmd_id_active); 623 + if (unlikely(cmd_id_active != 624 + virtio_balloon_cmd_id_received(vb))) 625 break; 626 627 /* ··· 637 return 0; 638 } 639 640 + static void virtio_balloon_report_free_page(struct virtio_balloon *vb) 641 { 642 int err; 643 struct device *dev = &vb->vdev->dev; 644 645 /* Start by sending the received cmd id to host with an outbuf. */ ··· 657 err = send_cmd_id_stop(vb); 658 if (unlikely(err)) 659 dev_err(dev, "Failed to send a stop id, err = %d\n", err); 660 + } 661 + 662 + static void report_free_page_func(struct work_struct *work) 663 + { 664 + struct virtio_balloon *vb = container_of(work, struct virtio_balloon, 665 + report_free_page_work); 666 + u32 cmd_id_received; 667 + 668 + cmd_id_received = virtio_balloon_cmd_id_received(vb); 669 + if (cmd_id_received == VIRTIO_BALLOON_CMD_ID_DONE) { 670 + /* Pass ULONG_MAX to give back all the free pages */ 671 + return_free_pages_to_mm(vb, ULONG_MAX); 672 + } else if (cmd_id_received != VIRTIO_BALLOON_CMD_ID_STOP && 673 + cmd_id_received != 674 + virtio32_to_cpu(vb->vdev, vb->cmd_id_active)) { 675 + virtio_balloon_report_free_page(vb); 676 + } 677 } 678 679 #ifdef CONFIG_BALLOON_COMPACTION ··· 885 goto out_del_vqs; 886 } 887 INIT_WORK(&vb->report_free_page_work, report_free_page_func); 888 + vb->cmd_id_received_cache = VIRTIO_BALLOON_CMD_ID_STOP; 889 vb->cmd_id_active = cpu_to_virtio32(vb->vdev, 890 VIRTIO_BALLOON_CMD_ID_STOP); 891 vb->cmd_id_stop = cpu_to_virtio32(vb->vdev,