Lines Matching refs:vb
147 struct virtio_balloon *vb = vq->vdev->priv;
149 wake_up(&vb->acked);
152 static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
157 sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
160 virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
164 wait_event(vb->acked, virtqueue_get_buf(vq, &len));
171 struct virtio_balloon *vb =
173 struct virtqueue *vq = vb->reporting_vq;
177 err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT | __GFP_NOWARN);
190 wait_event(vb->acked, virtqueue_get_buf(vq, &unused));
195 static void set_page_pfns(struct virtio_balloon *vb,
207 pfns[i] = cpu_to_virtio32(vb->vdev,
211 static unsigned fill_balloon(struct virtio_balloon *vb, size_t num)
219 num = min(num, ARRAY_SIZE(vb->pfns));
226 dev_info_ratelimited(&vb->vdev->dev,
237 mutex_lock(&vb->balloon_lock);
239 vb->num_pfns = 0;
242 balloon_page_enqueue(&vb->vb_dev_info, page);
244 set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
245 vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
246 if (!virtio_has_feature(vb->vdev,
249 vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE;
252 num_allocated_pages = vb->num_pfns;
254 if (vb->num_pfns != 0)
255 tell_host(vb, vb->inflate_vq);
256 mutex_unlock(&vb->balloon_lock);
261 static void release_pages_balloon(struct virtio_balloon *vb,
267 if (!virtio_has_feature(vb->vdev,
275 static unsigned leak_balloon(struct virtio_balloon *vb, size_t num)
279 struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
283 num = min(num, ARRAY_SIZE(vb->pfns));
285 mutex_lock(&vb->balloon_lock);
287 num = min(num, (size_t)vb->num_pages);
288 for (vb->num_pfns = 0; vb->num_pfns < num;
289 vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
293 set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
295 vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
298 num_freed_pages = vb->num_pfns;
304 if (vb->num_pfns != 0)
305 tell_host(vb, vb->deflate_vq);
306 release_pages_balloon(vb, &pages);
307 mutex_unlock(&vb->balloon_lock);
311 static inline void update_stat(struct virtio_balloon *vb, int idx,
315 vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag);
316 vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val);
321 static unsigned int update_balloon_stats(struct virtio_balloon *vb)
336 update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
338 update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
340 update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
341 update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
343 update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGALLOC,
345 update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGFAIL,
349 update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
351 update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
353 update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
355 update_stat(vb, idx++, VIRTIO_BALLOON_S_CACHES,
372 struct virtio_balloon *vb = vq->vdev->priv;
374 spin_lock(&vb->stop_update_lock);
375 if (!vb->stop_update)
376 queue_work(system_freezable_wq, &vb->update_balloon_stats_work);
377 spin_unlock(&vb->stop_update_lock);
380 static void stats_handle_request(struct virtio_balloon *vb)
386 num_stats = update_balloon_stats(vb);
388 vq = vb->stats_vq;
391 sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
392 virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
396 static inline s64 towards_target(struct virtio_balloon *vb)
402 virtio_cread_le(vb->vdev, struct virtio_balloon_config, num_pages,
410 return target - vb->num_pages;
414 static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
420 spin_lock_irq(&vb->free_page_list_lock);
422 page = balloon_page_pop(&vb->free_page_list);
428 vb->num_free_page_blocks -= num_returned;
429 spin_unlock_irq(&vb->free_page_list_lock);
434 static void virtio_balloon_queue_free_page_work(struct virtio_balloon *vb)
436 if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
441 &vb->config_read_bitmap))
444 queue_work(vb->balloon_wq, &vb->report_free_page_work);
449 struct virtio_balloon *vb = vdev->priv;
452 spin_lock_irqsave(&vb->stop_update_lock, flags);
453 if (!vb->stop_update) {
455 &vb->update_balloon_size_work);
456 virtio_balloon_queue_free_page_work(vb);
458 spin_unlock_irqrestore(&vb->stop_update_lock, flags);
461 static void update_balloon_size(struct virtio_balloon *vb)
463 u32 actual = vb->num_pages;
466 virtio_cwrite_le(vb->vdev, struct virtio_balloon_config, actual,
472 struct virtio_balloon *vb;
474 vb = container_of(work, struct virtio_balloon,
476 stats_handle_request(vb);
481 struct virtio_balloon *vb;
484 vb = container_of(work, struct virtio_balloon,
486 diff = towards_target(vb);
492 diff -= fill_balloon(vb, diff);
494 diff += leak_balloon(vb, -diff);
495 update_balloon_size(vb);
501 static int init_vqs(struct virtio_balloon *vb)
523 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
528 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
533 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
538 err = vb->vdev->config->find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX,
543 vb->inflate_vq = vqs[VIRTIO_BALLOON_VQ_INFLATE];
544 vb->deflate_vq = vqs[VIRTIO_BALLOON_VQ_DEFLATE];
545 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
548 vb->stats_vq = vqs[VIRTIO_BALLOON_VQ_STATS];
554 num_stats = update_balloon_stats(vb);
556 sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
557 err = virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb,
560 dev_warn(&vb->vdev->dev, "%s: add stat_vq failed\n",
564 virtqueue_kick(vb->stats_vq);
567 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
568 vb->free_page_vq = vqs[VIRTIO_BALLOON_VQ_FREE_PAGE];
570 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
571 vb->reporting_vq = vqs[VIRTIO_BALLOON_VQ_REPORTING];
576 static u32 virtio_balloon_cmd_id_received(struct virtio_balloon *vb)
579 &vb->config_read_bitmap)) {
581 virtio_cread_le(vb->vdev, struct virtio_balloon_config,
583 &vb->cmd_id_received_cache);
586 return vb->cmd_id_received_cache;
589 static int send_cmd_id_start(struct virtio_balloon *vb)
592 struct virtqueue *vq = vb->free_page_vq;
599 vb->cmd_id_active = cpu_to_virtio32(vb->vdev,
600 virtio_balloon_cmd_id_received(vb));
601 sg_init_one(&sg, &vb->cmd_id_active, sizeof(vb->cmd_id_active));
602 err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_active, GFP_KERNEL);
608 static int send_cmd_id_stop(struct virtio_balloon *vb)
611 struct virtqueue *vq = vb->free_page_vq;
618 sg_init_one(&sg, &vb->cmd_id_stop, sizeof(vb->cmd_id_stop));
619 err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_stop, GFP_KERNEL);
625 static int get_free_page_and_send(struct virtio_balloon *vb)
627 struct virtqueue *vq = vb->free_page_vq;
657 spin_lock_irq(&vb->free_page_list_lock);
658 balloon_page_push(&vb->free_page_list, page);
659 vb->num_free_page_blocks++;
660 spin_unlock_irq(&vb->free_page_list_lock);
672 static int send_free_pages(struct virtio_balloon *vb)
682 cmd_id_active = virtio32_to_cpu(vb->vdev, vb->cmd_id_active);
684 virtio_balloon_cmd_id_received(vb)))
691 err = get_free_page_and_send(vb);
701 static void virtio_balloon_report_free_page(struct virtio_balloon *vb)
704 struct device *dev = &vb->vdev->dev;
707 err = send_cmd_id_start(vb);
711 err = send_free_pages(vb);
716 err = send_cmd_id_stop(vb);
723 struct virtio_balloon *vb = container_of(work, struct virtio_balloon,
727 cmd_id_received = virtio_balloon_cmd_id_received(vb);
730 return_free_pages_to_mm(vb, ULONG_MAX);
733 virtio32_to_cpu(vb->vdev, vb->cmd_id_active)) {
734 virtio_balloon_report_free_page(vb);
751 * 1) insert newpage into vb->pages list and update the host about it;
752 * 2) update the host about the old page removed from vb->pages list;
760 struct virtio_balloon *vb = container_of(vb_dev_info,
772 if (!mutex_trylock(&vb->balloon_lock))
782 if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM) &&
794 vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
795 set_page_pfns(vb, vb->pfns, newpage);
796 tell_host(vb, vb->inflate_vq);
802 vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
803 set_page_pfns(vb, vb->pfns, page);
804 tell_host(vb, vb->deflate_vq);
806 mutex_unlock(&vb->balloon_lock);
826 static unsigned long shrink_free_pages(struct virtio_balloon *vb,
834 blocks_freed = return_free_pages_to_mm(vb, blocks_to_free);
842 struct virtio_balloon *vb = container_of(shrinker,
845 return shrink_free_pages(vb, sc->nr_to_scan);
851 struct virtio_balloon *vb = container_of(shrinker,
854 return vb->num_free_page_blocks * VIRTIO_BALLOON_HINT_BLOCK_PAGES;
860 struct virtio_balloon *vb = container_of(nb,
864 *freed += leak_balloon(vb, VIRTIO_BALLOON_OOM_NR_PAGES) /
866 update_balloon_size(vb);
871 static void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb)
873 unregister_shrinker(&vb->shrinker);
876 static int virtio_balloon_register_shrinker(struct virtio_balloon *vb)
878 vb->shrinker.scan_objects = virtio_balloon_shrinker_scan;
879 vb->shrinker.count_objects = virtio_balloon_shrinker_count;
880 vb->shrinker.seeks = DEFAULT_SEEKS;
882 return register_shrinker(&vb->shrinker);
887 struct virtio_balloon *vb;
896 vdev->priv = vb = kzalloc(sizeof(*vb), GFP_KERNEL);
897 if (!vb) {
902 INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
903 INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
904 spin_lock_init(&vb->stop_update_lock);
905 mutex_init(&vb->balloon_lock);
906 init_waitqueue_head(&vb->acked);
907 vb->vdev = vdev;
909 balloon_devinfo_init(&vb->vb_dev_info);
911 err = init_vqs(vb);
922 vb->vb_dev_info.migratepage = virtballoon_migratepage;
923 vb->vb_dev_info.inode = alloc_anon_inode(balloon_mnt->mnt_sb);
924 if (IS_ERR(vb->vb_dev_info.inode)) {
925 err = PTR_ERR(vb->vb_dev_info.inode);
928 vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops;
935 if (virtqueue_get_vring_size(vb->free_page_vq) < 2) {
939 vb->balloon_wq = alloc_workqueue("balloon-wq",
941 if (!vb->balloon_wq) {
945 INIT_WORK(&vb->report_free_page_work, report_free_page_func);
946 vb->cmd_id_received_cache = VIRTIO_BALLOON_CMD_ID_STOP;
947 vb->cmd_id_active = cpu_to_virtio32(vb->vdev,
949 vb->cmd_id_stop = cpu_to_virtio32(vb->vdev,
951 spin_lock_init(&vb->free_page_list_lock);
952 INIT_LIST_HEAD(&vb->free_page_list);
957 err = virtio_balloon_register_shrinker(vb);
962 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) {
963 vb->oom_nb.notifier_call = virtio_balloon_oom_notify;
964 vb->oom_nb.priority = VIRTIO_BALLOON_OOM_NOTIFY_PRIORITY;
965 err = register_oom_notifier(&vb->oom_nb);
986 virtio_cwrite_le(vb->vdev, struct virtio_balloon_config,
990 vb->pr_dev_info.report = virtballoon_free_page_report;
991 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
994 capacity = virtqueue_get_vring_size(vb->reporting_vq);
1000 err = page_reporting_register(&vb->pr_dev_info);
1007 if (towards_target(vb))
1012 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
1013 unregister_oom_notifier(&vb->oom_nb);
1015 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
1016 virtio_balloon_unregister_shrinker(vb);
1019 destroy_workqueue(vb->balloon_wq);
1022 iput(vb->vb_dev_info.inode);
1029 kfree(vb);
1034 static void remove_common(struct virtio_balloon *vb)
1037 while (vb->num_pages)
1038 leak_balloon(vb, vb->num_pages);
1039 update_balloon_size(vb);
1042 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
1043 return_free_pages_to_mm(vb, ULONG_MAX);
1046 vb->vdev->config->reset(vb->vdev);
1048 vb->vdev->config->del_vqs(vb->vdev);
1053 struct virtio_balloon *vb = vdev->priv;
1055 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
1056 page_reporting_unregister(&vb->pr_dev_info);
1057 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
1058 unregister_oom_notifier(&vb->oom_nb);
1059 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
1060 virtio_balloon_unregister_shrinker(vb);
1061 spin_lock_irq(&vb->stop_update_lock);
1062 vb->stop_update = true;
1063 spin_unlock_irq(&vb->stop_update_lock);
1064 cancel_work_sync(&vb->update_balloon_size_work);
1065 cancel_work_sync(&vb->update_balloon_stats_work);
1068 cancel_work_sync(&vb->report_free_page_work);
1069 destroy_workqueue(vb->balloon_wq);
1072 remove_common(vb);
1074 if (vb->vb_dev_info.inode)
1075 iput(vb->vb_dev_info.inode);
1079 kfree(vb);
1085 struct virtio_balloon *vb = vdev->priv;
1091 remove_common(vb);
1097 struct virtio_balloon *vb = vdev->priv;
1106 if (towards_target(vb))
1108 update_balloon_size(vb);