162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Virtio balloon implementation, inspired by Dor Laor and Marcelo 462306a36Sopenharmony_ci * Tosatti's implementations. 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Copyright 2008 Rusty Russell IBM Corporation 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/virtio.h> 1062306a36Sopenharmony_ci#include <linux/virtio_balloon.h> 1162306a36Sopenharmony_ci#include <linux/swap.h> 1262306a36Sopenharmony_ci#include <linux/workqueue.h> 1362306a36Sopenharmony_ci#include <linux/delay.h> 1462306a36Sopenharmony_ci#include <linux/slab.h> 1562306a36Sopenharmony_ci#include <linux/module.h> 1662306a36Sopenharmony_ci#include <linux/balloon_compaction.h> 1762306a36Sopenharmony_ci#include <linux/oom.h> 1862306a36Sopenharmony_ci#include <linux/wait.h> 1962306a36Sopenharmony_ci#include <linux/mm.h> 2062306a36Sopenharmony_ci#include <linux/page_reporting.h> 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci/* 2362306a36Sopenharmony_ci * Balloon device works in 4K page units. So each page is pointed to by 2462306a36Sopenharmony_ci * multiple balloon pages. All memory counters in this driver are in balloon 2562306a36Sopenharmony_ci * page units. 2662306a36Sopenharmony_ci */ 2762306a36Sopenharmony_ci#define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned int)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT) 2862306a36Sopenharmony_ci#define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256 2962306a36Sopenharmony_ci/* Maximum number of (4k) pages to deflate on OOM notifications. */ 3062306a36Sopenharmony_ci#define VIRTIO_BALLOON_OOM_NR_PAGES 256 3162306a36Sopenharmony_ci#define VIRTIO_BALLOON_OOM_NOTIFY_PRIORITY 80 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci#define VIRTIO_BALLOON_FREE_PAGE_ALLOC_FLAG (__GFP_NORETRY | __GFP_NOWARN | \ 3462306a36Sopenharmony_ci __GFP_NOMEMALLOC) 3562306a36Sopenharmony_ci/* The order of free page blocks to report to host */ 3662306a36Sopenharmony_ci#define VIRTIO_BALLOON_HINT_BLOCK_ORDER MAX_ORDER 3762306a36Sopenharmony_ci/* The size of a free page block in bytes */ 3862306a36Sopenharmony_ci#define VIRTIO_BALLOON_HINT_BLOCK_BYTES \ 3962306a36Sopenharmony_ci (1 << (VIRTIO_BALLOON_HINT_BLOCK_ORDER + PAGE_SHIFT)) 4062306a36Sopenharmony_ci#define VIRTIO_BALLOON_HINT_BLOCK_PAGES (1 << VIRTIO_BALLOON_HINT_BLOCK_ORDER) 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_cienum virtio_balloon_vq { 4362306a36Sopenharmony_ci VIRTIO_BALLOON_VQ_INFLATE, 4462306a36Sopenharmony_ci VIRTIO_BALLOON_VQ_DEFLATE, 4562306a36Sopenharmony_ci VIRTIO_BALLOON_VQ_STATS, 4662306a36Sopenharmony_ci VIRTIO_BALLOON_VQ_FREE_PAGE, 4762306a36Sopenharmony_ci VIRTIO_BALLOON_VQ_REPORTING, 4862306a36Sopenharmony_ci VIRTIO_BALLOON_VQ_MAX 4962306a36Sopenharmony_ci}; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_cienum virtio_balloon_config_read { 5262306a36Sopenharmony_ci VIRTIO_BALLOON_CONFIG_READ_CMD_ID = 0, 5362306a36Sopenharmony_ci}; 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_cistruct virtio_balloon { 5662306a36Sopenharmony_ci struct virtio_device *vdev; 5762306a36Sopenharmony_ci struct virtqueue *inflate_vq, *deflate_vq, *stats_vq, *free_page_vq; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci /* Balloon's own wq for cpu-intensive work items */ 6062306a36Sopenharmony_ci struct workqueue_struct *balloon_wq; 6162306a36Sopenharmony_ci /* The free page reporting work item submitted to the balloon wq */ 6262306a36Sopenharmony_ci struct work_struct report_free_page_work; 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci /* The balloon servicing is delegated to a freezable workqueue. */ 6562306a36Sopenharmony_ci struct work_struct update_balloon_stats_work; 6662306a36Sopenharmony_ci struct work_struct update_balloon_size_work; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci /* Prevent updating balloon when it is being canceled. */ 6962306a36Sopenharmony_ci spinlock_t stop_update_lock; 7062306a36Sopenharmony_ci bool stop_update; 7162306a36Sopenharmony_ci /* Bitmap to indicate if reading the related config fields are needed */ 7262306a36Sopenharmony_ci unsigned long config_read_bitmap; 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci /* The list of allocated free pages, waiting to be given back to mm */ 7562306a36Sopenharmony_ci struct list_head free_page_list; 7662306a36Sopenharmony_ci spinlock_t free_page_list_lock; 7762306a36Sopenharmony_ci /* The number of free page blocks on the above list */ 7862306a36Sopenharmony_ci unsigned long num_free_page_blocks; 7962306a36Sopenharmony_ci /* 8062306a36Sopenharmony_ci * The cmd id received from host. 8162306a36Sopenharmony_ci * Read it via virtio_balloon_cmd_id_received to get the latest value 8262306a36Sopenharmony_ci * sent from host. 8362306a36Sopenharmony_ci */ 8462306a36Sopenharmony_ci u32 cmd_id_received_cache; 8562306a36Sopenharmony_ci /* The cmd id that is actively in use */ 8662306a36Sopenharmony_ci __virtio32 cmd_id_active; 8762306a36Sopenharmony_ci /* Buffer to store the stop sign */ 8862306a36Sopenharmony_ci __virtio32 cmd_id_stop; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci /* Waiting for host to ack the pages we released. */ 9162306a36Sopenharmony_ci wait_queue_head_t acked; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci /* Number of balloon pages we've told the Host we're not using. */ 9462306a36Sopenharmony_ci unsigned int num_pages; 9562306a36Sopenharmony_ci /* 9662306a36Sopenharmony_ci * The pages we've told the Host we're not using are enqueued 9762306a36Sopenharmony_ci * at vb_dev_info->pages list. 9862306a36Sopenharmony_ci * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE 9962306a36Sopenharmony_ci * to num_pages above. 10062306a36Sopenharmony_ci */ 10162306a36Sopenharmony_ci struct balloon_dev_info vb_dev_info; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci /* Synchronize access/update to this struct virtio_balloon elements */ 10462306a36Sopenharmony_ci struct mutex balloon_lock; 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci /* The array of pfns we tell the Host about. */ 10762306a36Sopenharmony_ci unsigned int num_pfns; 10862306a36Sopenharmony_ci __virtio32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX]; 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci /* Memory statistics */ 11162306a36Sopenharmony_ci struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR]; 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci /* Shrinker to return free pages - VIRTIO_BALLOON_F_FREE_PAGE_HINT */ 11462306a36Sopenharmony_ci struct shrinker shrinker; 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci /* OOM notifier to deflate on OOM - VIRTIO_BALLOON_F_DEFLATE_ON_OOM */ 11762306a36Sopenharmony_ci struct notifier_block oom_nb; 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci /* Free page reporting device */ 12062306a36Sopenharmony_ci struct virtqueue *reporting_vq; 12162306a36Sopenharmony_ci struct page_reporting_dev_info pr_dev_info; 12262306a36Sopenharmony_ci}; 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_cistatic const struct virtio_device_id id_table[] = { 12562306a36Sopenharmony_ci { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID }, 12662306a36Sopenharmony_ci { 0 }, 12762306a36Sopenharmony_ci}; 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_cistatic u32 page_to_balloon_pfn(struct page *page) 13062306a36Sopenharmony_ci{ 13162306a36Sopenharmony_ci unsigned long pfn = page_to_pfn(page); 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT); 13462306a36Sopenharmony_ci /* Convert pfn from Linux page size to balloon page size. */ 13562306a36Sopenharmony_ci return pfn * VIRTIO_BALLOON_PAGES_PER_PAGE; 13662306a36Sopenharmony_ci} 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_cistatic void balloon_ack(struct virtqueue *vq) 13962306a36Sopenharmony_ci{ 14062306a36Sopenharmony_ci struct virtio_balloon *vb = vq->vdev->priv; 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci wake_up(&vb->acked); 14362306a36Sopenharmony_ci} 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_cistatic void tell_host(struct virtio_balloon *vb, struct virtqueue *vq) 14662306a36Sopenharmony_ci{ 14762306a36Sopenharmony_ci struct scatterlist sg; 14862306a36Sopenharmony_ci unsigned int len; 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns); 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci /* We should always be able to add one buffer to an empty queue. */ 15362306a36Sopenharmony_ci virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL); 15462306a36Sopenharmony_ci virtqueue_kick(vq); 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci /* When host has read buffer, this completes via balloon_ack */ 15762306a36Sopenharmony_ci wait_event(vb->acked, virtqueue_get_buf(vq, &len)); 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci} 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_cistatic int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_info, 16262306a36Sopenharmony_ci struct scatterlist *sg, unsigned int nents) 16362306a36Sopenharmony_ci{ 16462306a36Sopenharmony_ci struct virtio_balloon *vb = 16562306a36Sopenharmony_ci container_of(pr_dev_info, struct virtio_balloon, pr_dev_info); 16662306a36Sopenharmony_ci struct virtqueue *vq = vb->reporting_vq; 16762306a36Sopenharmony_ci unsigned int unused, err; 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci /* We should always be able to add these buffers to an empty queue. */ 17062306a36Sopenharmony_ci err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT | __GFP_NOWARN); 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci /* 17362306a36Sopenharmony_ci * In the extremely unlikely case that something has occurred and we 17462306a36Sopenharmony_ci * are able to trigger an error we will simply display a warning 17562306a36Sopenharmony_ci * and exit without actually processing the pages. 17662306a36Sopenharmony_ci */ 17762306a36Sopenharmony_ci if (WARN_ON_ONCE(err)) 17862306a36Sopenharmony_ci return err; 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci virtqueue_kick(vq); 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci /* When host has read buffer, this completes via balloon_ack */ 18362306a36Sopenharmony_ci wait_event(vb->acked, virtqueue_get_buf(vq, &unused)); 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci return 0; 18662306a36Sopenharmony_ci} 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_cistatic void set_page_pfns(struct virtio_balloon *vb, 18962306a36Sopenharmony_ci __virtio32 pfns[], struct page *page) 19062306a36Sopenharmony_ci{ 19162306a36Sopenharmony_ci unsigned int i; 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci BUILD_BUG_ON(VIRTIO_BALLOON_PAGES_PER_PAGE > VIRTIO_BALLOON_ARRAY_PFNS_MAX); 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci /* 19662306a36Sopenharmony_ci * Set balloon pfns pointing at this page. 19762306a36Sopenharmony_ci * Note that the first pfn points at start of the page. 19862306a36Sopenharmony_ci */ 19962306a36Sopenharmony_ci for (i = 0; i < VIRTIO_BALLOON_PAGES_PER_PAGE; i++) 20062306a36Sopenharmony_ci pfns[i] = cpu_to_virtio32(vb->vdev, 20162306a36Sopenharmony_ci page_to_balloon_pfn(page) + i); 20262306a36Sopenharmony_ci} 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_cistatic unsigned int fill_balloon(struct virtio_balloon *vb, size_t num) 20562306a36Sopenharmony_ci{ 20662306a36Sopenharmony_ci unsigned int num_allocated_pages; 20762306a36Sopenharmony_ci unsigned int num_pfns; 20862306a36Sopenharmony_ci struct page *page; 20962306a36Sopenharmony_ci LIST_HEAD(pages); 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci /* We can only do one array worth at a time. */ 21262306a36Sopenharmony_ci num = min(num, ARRAY_SIZE(vb->pfns)); 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci for (num_pfns = 0; num_pfns < num; 21562306a36Sopenharmony_ci num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) { 21662306a36Sopenharmony_ci struct page *page = balloon_page_alloc(); 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci if (!page) { 21962306a36Sopenharmony_ci dev_info_ratelimited(&vb->vdev->dev, 22062306a36Sopenharmony_ci "Out of puff! Can't get %u pages\n", 22162306a36Sopenharmony_ci VIRTIO_BALLOON_PAGES_PER_PAGE); 22262306a36Sopenharmony_ci /* Sleep for at least 1/5 of a second before retry. */ 22362306a36Sopenharmony_ci msleep(200); 22462306a36Sopenharmony_ci break; 22562306a36Sopenharmony_ci } 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci balloon_page_push(&pages, page); 22862306a36Sopenharmony_ci } 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ci mutex_lock(&vb->balloon_lock); 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci vb->num_pfns = 0; 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ci while ((page = balloon_page_pop(&pages))) { 23562306a36Sopenharmony_ci balloon_page_enqueue(&vb->vb_dev_info, page); 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ci set_page_pfns(vb, vb->pfns + vb->num_pfns, page); 23862306a36Sopenharmony_ci vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE; 23962306a36Sopenharmony_ci if (!virtio_has_feature(vb->vdev, 24062306a36Sopenharmony_ci VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) 24162306a36Sopenharmony_ci adjust_managed_page_count(page, -1); 24262306a36Sopenharmony_ci vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE; 24362306a36Sopenharmony_ci } 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci num_allocated_pages = vb->num_pfns; 24662306a36Sopenharmony_ci /* Did we get any? */ 24762306a36Sopenharmony_ci if (vb->num_pfns != 0) 24862306a36Sopenharmony_ci tell_host(vb, vb->inflate_vq); 24962306a36Sopenharmony_ci mutex_unlock(&vb->balloon_lock); 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ci return num_allocated_pages; 25262306a36Sopenharmony_ci} 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_cistatic void release_pages_balloon(struct virtio_balloon *vb, 25562306a36Sopenharmony_ci struct list_head *pages) 25662306a36Sopenharmony_ci{ 25762306a36Sopenharmony_ci struct page *page, *next; 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ci list_for_each_entry_safe(page, next, pages, lru) { 26062306a36Sopenharmony_ci if (!virtio_has_feature(vb->vdev, 26162306a36Sopenharmony_ci VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) 26262306a36Sopenharmony_ci adjust_managed_page_count(page, 1); 26362306a36Sopenharmony_ci list_del(&page->lru); 26462306a36Sopenharmony_ci put_page(page); /* balloon reference */ 26562306a36Sopenharmony_ci } 26662306a36Sopenharmony_ci} 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_cistatic unsigned int leak_balloon(struct virtio_balloon *vb, size_t num) 26962306a36Sopenharmony_ci{ 27062306a36Sopenharmony_ci unsigned int num_freed_pages; 27162306a36Sopenharmony_ci struct page *page; 27262306a36Sopenharmony_ci struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info; 27362306a36Sopenharmony_ci LIST_HEAD(pages); 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci /* We can only do one array worth at a time. */ 27662306a36Sopenharmony_ci num = min(num, ARRAY_SIZE(vb->pfns)); 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_ci mutex_lock(&vb->balloon_lock); 27962306a36Sopenharmony_ci /* We can't release more pages than taken */ 28062306a36Sopenharmony_ci num = min(num, (size_t)vb->num_pages); 28162306a36Sopenharmony_ci for (vb->num_pfns = 0; vb->num_pfns < num; 28262306a36Sopenharmony_ci vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) { 28362306a36Sopenharmony_ci page = balloon_page_dequeue(vb_dev_info); 28462306a36Sopenharmony_ci if (!page) 28562306a36Sopenharmony_ci break; 28662306a36Sopenharmony_ci set_page_pfns(vb, vb->pfns + vb->num_pfns, page); 28762306a36Sopenharmony_ci list_add(&page->lru, &pages); 28862306a36Sopenharmony_ci vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE; 28962306a36Sopenharmony_ci } 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci num_freed_pages = vb->num_pfns; 29262306a36Sopenharmony_ci /* 29362306a36Sopenharmony_ci * Note that if 29462306a36Sopenharmony_ci * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST); 29562306a36Sopenharmony_ci * is true, we *have* to do it in this order 29662306a36Sopenharmony_ci */ 29762306a36Sopenharmony_ci if (vb->num_pfns != 0) 29862306a36Sopenharmony_ci tell_host(vb, vb->deflate_vq); 29962306a36Sopenharmony_ci release_pages_balloon(vb, &pages); 30062306a36Sopenharmony_ci mutex_unlock(&vb->balloon_lock); 30162306a36Sopenharmony_ci return num_freed_pages; 30262306a36Sopenharmony_ci} 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_cistatic inline void update_stat(struct virtio_balloon *vb, int idx, 30562306a36Sopenharmony_ci u16 tag, u64 val) 30662306a36Sopenharmony_ci{ 30762306a36Sopenharmony_ci BUG_ON(idx >= VIRTIO_BALLOON_S_NR); 30862306a36Sopenharmony_ci vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag); 30962306a36Sopenharmony_ci vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val); 31062306a36Sopenharmony_ci} 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_ci#define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT) 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_cistatic unsigned int update_balloon_stats(struct virtio_balloon *vb) 31562306a36Sopenharmony_ci{ 31662306a36Sopenharmony_ci unsigned long events[NR_VM_EVENT_ITEMS]; 31762306a36Sopenharmony_ci struct sysinfo i; 31862306a36Sopenharmony_ci unsigned int idx = 0; 31962306a36Sopenharmony_ci long available; 32062306a36Sopenharmony_ci unsigned long caches; 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ci all_vm_events(events); 32362306a36Sopenharmony_ci si_meminfo(&i); 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ci available = si_mem_available(); 32662306a36Sopenharmony_ci caches = global_node_page_state(NR_FILE_PAGES); 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ci#ifdef CONFIG_VM_EVENT_COUNTERS 32962306a36Sopenharmony_ci update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN, 33062306a36Sopenharmony_ci pages_to_bytes(events[PSWPIN])); 33162306a36Sopenharmony_ci update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT, 33262306a36Sopenharmony_ci pages_to_bytes(events[PSWPOUT])); 33362306a36Sopenharmony_ci update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]); 33462306a36Sopenharmony_ci update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]); 33562306a36Sopenharmony_ci#ifdef CONFIG_HUGETLB_PAGE 33662306a36Sopenharmony_ci update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGALLOC, 33762306a36Sopenharmony_ci events[HTLB_BUDDY_PGALLOC]); 33862306a36Sopenharmony_ci update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGFAIL, 33962306a36Sopenharmony_ci events[HTLB_BUDDY_PGALLOC_FAIL]); 34062306a36Sopenharmony_ci#endif 34162306a36Sopenharmony_ci#endif 34262306a36Sopenharmony_ci update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE, 34362306a36Sopenharmony_ci pages_to_bytes(i.freeram)); 34462306a36Sopenharmony_ci update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT, 34562306a36Sopenharmony_ci pages_to_bytes(i.totalram)); 34662306a36Sopenharmony_ci update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL, 34762306a36Sopenharmony_ci pages_to_bytes(available)); 34862306a36Sopenharmony_ci update_stat(vb, idx++, VIRTIO_BALLOON_S_CACHES, 34962306a36Sopenharmony_ci pages_to_bytes(caches)); 35062306a36Sopenharmony_ci 35162306a36Sopenharmony_ci return idx; 35262306a36Sopenharmony_ci} 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_ci/* 35562306a36Sopenharmony_ci * While most virtqueues communicate guest-initiated requests to the hypervisor, 35662306a36Sopenharmony_ci * the stats queue operates in reverse. The driver initializes the virtqueue 35762306a36Sopenharmony_ci * with a single buffer. From that point forward, all conversations consist of 35862306a36Sopenharmony_ci * a hypervisor request (a call to this function) which directs us to refill 35962306a36Sopenharmony_ci * the virtqueue with a fresh stats buffer. Since stats collection can sleep, 36062306a36Sopenharmony_ci * we delegate the job to a freezable workqueue that will do the actual work via 36162306a36Sopenharmony_ci * stats_handle_request(). 36262306a36Sopenharmony_ci */ 36362306a36Sopenharmony_cistatic void stats_request(struct virtqueue *vq) 36462306a36Sopenharmony_ci{ 36562306a36Sopenharmony_ci struct virtio_balloon *vb = vq->vdev->priv; 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ci spin_lock(&vb->stop_update_lock); 36862306a36Sopenharmony_ci if (!vb->stop_update) 36962306a36Sopenharmony_ci queue_work(system_freezable_wq, &vb->update_balloon_stats_work); 37062306a36Sopenharmony_ci spin_unlock(&vb->stop_update_lock); 37162306a36Sopenharmony_ci} 37262306a36Sopenharmony_ci 37362306a36Sopenharmony_cistatic void stats_handle_request(struct virtio_balloon *vb) 37462306a36Sopenharmony_ci{ 37562306a36Sopenharmony_ci struct virtqueue *vq; 37662306a36Sopenharmony_ci struct scatterlist sg; 37762306a36Sopenharmony_ci unsigned int len, num_stats; 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_ci num_stats = update_balloon_stats(vb); 38062306a36Sopenharmony_ci 38162306a36Sopenharmony_ci vq = vb->stats_vq; 38262306a36Sopenharmony_ci if (!virtqueue_get_buf(vq, &len)) 38362306a36Sopenharmony_ci return; 38462306a36Sopenharmony_ci sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats); 38562306a36Sopenharmony_ci virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL); 38662306a36Sopenharmony_ci virtqueue_kick(vq); 38762306a36Sopenharmony_ci} 38862306a36Sopenharmony_ci 38962306a36Sopenharmony_cistatic inline s64 towards_target(struct virtio_balloon *vb) 39062306a36Sopenharmony_ci{ 39162306a36Sopenharmony_ci s64 target; 39262306a36Sopenharmony_ci u32 num_pages; 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_ci /* Legacy balloon config space is LE, unlike all other devices. */ 39562306a36Sopenharmony_ci virtio_cread_le(vb->vdev, struct virtio_balloon_config, num_pages, 39662306a36Sopenharmony_ci &num_pages); 39762306a36Sopenharmony_ci 39862306a36Sopenharmony_ci /* 39962306a36Sopenharmony_ci * Aligned up to guest page size to avoid inflating and deflating 40062306a36Sopenharmony_ci * balloon endlessly. 40162306a36Sopenharmony_ci */ 40262306a36Sopenharmony_ci target = ALIGN(num_pages, VIRTIO_BALLOON_PAGES_PER_PAGE); 40362306a36Sopenharmony_ci return target - vb->num_pages; 40462306a36Sopenharmony_ci} 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ci/* Gives back @num_to_return blocks of free pages to mm. */ 40762306a36Sopenharmony_cistatic unsigned long return_free_pages_to_mm(struct virtio_balloon *vb, 40862306a36Sopenharmony_ci unsigned long num_to_return) 40962306a36Sopenharmony_ci{ 41062306a36Sopenharmony_ci struct page *page; 41162306a36Sopenharmony_ci unsigned long num_returned; 41262306a36Sopenharmony_ci 41362306a36Sopenharmony_ci spin_lock_irq(&vb->free_page_list_lock); 41462306a36Sopenharmony_ci for (num_returned = 0; num_returned < num_to_return; num_returned++) { 41562306a36Sopenharmony_ci page = balloon_page_pop(&vb->free_page_list); 41662306a36Sopenharmony_ci if (!page) 41762306a36Sopenharmony_ci break; 41862306a36Sopenharmony_ci free_pages((unsigned long)page_address(page), 41962306a36Sopenharmony_ci VIRTIO_BALLOON_HINT_BLOCK_ORDER); 42062306a36Sopenharmony_ci } 42162306a36Sopenharmony_ci vb->num_free_page_blocks -= num_returned; 42262306a36Sopenharmony_ci spin_unlock_irq(&vb->free_page_list_lock); 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_ci return num_returned; 42562306a36Sopenharmony_ci} 42662306a36Sopenharmony_ci 42762306a36Sopenharmony_cistatic void virtio_balloon_queue_free_page_work(struct virtio_balloon *vb) 42862306a36Sopenharmony_ci{ 42962306a36Sopenharmony_ci if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) 43062306a36Sopenharmony_ci return; 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci /* No need to queue the work if the bit was already set. */ 43362306a36Sopenharmony_ci if (test_and_set_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID, 43462306a36Sopenharmony_ci &vb->config_read_bitmap)) 43562306a36Sopenharmony_ci return; 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci queue_work(vb->balloon_wq, &vb->report_free_page_work); 43862306a36Sopenharmony_ci} 43962306a36Sopenharmony_ci 44062306a36Sopenharmony_cistatic void virtballoon_changed(struct virtio_device *vdev) 44162306a36Sopenharmony_ci{ 44262306a36Sopenharmony_ci struct virtio_balloon *vb = vdev->priv; 44362306a36Sopenharmony_ci unsigned long flags; 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_ci spin_lock_irqsave(&vb->stop_update_lock, flags); 44662306a36Sopenharmony_ci if (!vb->stop_update) { 44762306a36Sopenharmony_ci queue_work(system_freezable_wq, 44862306a36Sopenharmony_ci &vb->update_balloon_size_work); 44962306a36Sopenharmony_ci virtio_balloon_queue_free_page_work(vb); 45062306a36Sopenharmony_ci } 45162306a36Sopenharmony_ci spin_unlock_irqrestore(&vb->stop_update_lock, flags); 45262306a36Sopenharmony_ci} 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_cistatic void update_balloon_size(struct virtio_balloon *vb) 45562306a36Sopenharmony_ci{ 45662306a36Sopenharmony_ci u32 actual = vb->num_pages; 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ci /* Legacy balloon config space is LE, unlike all other devices. */ 45962306a36Sopenharmony_ci virtio_cwrite_le(vb->vdev, struct virtio_balloon_config, actual, 46062306a36Sopenharmony_ci &actual); 46162306a36Sopenharmony_ci} 46262306a36Sopenharmony_ci 46362306a36Sopenharmony_cistatic void update_balloon_stats_func(struct work_struct *work) 46462306a36Sopenharmony_ci{ 46562306a36Sopenharmony_ci struct virtio_balloon *vb; 46662306a36Sopenharmony_ci 46762306a36Sopenharmony_ci vb = container_of(work, struct virtio_balloon, 46862306a36Sopenharmony_ci update_balloon_stats_work); 46962306a36Sopenharmony_ci stats_handle_request(vb); 47062306a36Sopenharmony_ci} 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_cistatic void update_balloon_size_func(struct work_struct *work) 47362306a36Sopenharmony_ci{ 47462306a36Sopenharmony_ci struct virtio_balloon *vb; 47562306a36Sopenharmony_ci s64 diff; 47662306a36Sopenharmony_ci 47762306a36Sopenharmony_ci vb = container_of(work, struct virtio_balloon, 47862306a36Sopenharmony_ci update_balloon_size_work); 47962306a36Sopenharmony_ci diff = towards_target(vb); 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_ci if (!diff) 48262306a36Sopenharmony_ci return; 48362306a36Sopenharmony_ci 48462306a36Sopenharmony_ci if (diff > 0) 48562306a36Sopenharmony_ci diff -= fill_balloon(vb, diff); 48662306a36Sopenharmony_ci else 48762306a36Sopenharmony_ci diff += leak_balloon(vb, -diff); 48862306a36Sopenharmony_ci update_balloon_size(vb); 48962306a36Sopenharmony_ci 49062306a36Sopenharmony_ci if (diff) 49162306a36Sopenharmony_ci queue_work(system_freezable_wq, work); 49262306a36Sopenharmony_ci} 49362306a36Sopenharmony_ci 49462306a36Sopenharmony_cistatic int init_vqs(struct virtio_balloon *vb) 49562306a36Sopenharmony_ci{ 49662306a36Sopenharmony_ci struct virtqueue *vqs[VIRTIO_BALLOON_VQ_MAX]; 49762306a36Sopenharmony_ci vq_callback_t *callbacks[VIRTIO_BALLOON_VQ_MAX]; 49862306a36Sopenharmony_ci const char *names[VIRTIO_BALLOON_VQ_MAX]; 49962306a36Sopenharmony_ci int err; 50062306a36Sopenharmony_ci 50162306a36Sopenharmony_ci /* 50262306a36Sopenharmony_ci * Inflateq and deflateq are used unconditionally. The names[] 50362306a36Sopenharmony_ci * will be NULL if the related feature is not enabled, which will 50462306a36Sopenharmony_ci * cause no allocation for the corresponding virtqueue in find_vqs. 50562306a36Sopenharmony_ci */ 50662306a36Sopenharmony_ci callbacks[VIRTIO_BALLOON_VQ_INFLATE] = balloon_ack; 50762306a36Sopenharmony_ci names[VIRTIO_BALLOON_VQ_INFLATE] = "inflate"; 50862306a36Sopenharmony_ci callbacks[VIRTIO_BALLOON_VQ_DEFLATE] = balloon_ack; 50962306a36Sopenharmony_ci names[VIRTIO_BALLOON_VQ_DEFLATE] = "deflate"; 51062306a36Sopenharmony_ci callbacks[VIRTIO_BALLOON_VQ_STATS] = NULL; 51162306a36Sopenharmony_ci names[VIRTIO_BALLOON_VQ_STATS] = NULL; 51262306a36Sopenharmony_ci callbacks[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL; 51362306a36Sopenharmony_ci names[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL; 51462306a36Sopenharmony_ci names[VIRTIO_BALLOON_VQ_REPORTING] = NULL; 51562306a36Sopenharmony_ci 51662306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { 51762306a36Sopenharmony_ci names[VIRTIO_BALLOON_VQ_STATS] = "stats"; 51862306a36Sopenharmony_ci callbacks[VIRTIO_BALLOON_VQ_STATS] = stats_request; 51962306a36Sopenharmony_ci } 52062306a36Sopenharmony_ci 52162306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) { 52262306a36Sopenharmony_ci names[VIRTIO_BALLOON_VQ_FREE_PAGE] = "free_page_vq"; 52362306a36Sopenharmony_ci callbacks[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL; 52462306a36Sopenharmony_ci } 52562306a36Sopenharmony_ci 52662306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) { 52762306a36Sopenharmony_ci names[VIRTIO_BALLOON_VQ_REPORTING] = "reporting_vq"; 52862306a36Sopenharmony_ci callbacks[VIRTIO_BALLOON_VQ_REPORTING] = balloon_ack; 52962306a36Sopenharmony_ci } 53062306a36Sopenharmony_ci 53162306a36Sopenharmony_ci err = virtio_find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX, vqs, 53262306a36Sopenharmony_ci callbacks, names, NULL); 53362306a36Sopenharmony_ci if (err) 53462306a36Sopenharmony_ci return err; 53562306a36Sopenharmony_ci 53662306a36Sopenharmony_ci vb->inflate_vq = vqs[VIRTIO_BALLOON_VQ_INFLATE]; 53762306a36Sopenharmony_ci vb->deflate_vq = vqs[VIRTIO_BALLOON_VQ_DEFLATE]; 53862306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { 53962306a36Sopenharmony_ci struct scatterlist sg; 54062306a36Sopenharmony_ci unsigned int num_stats; 54162306a36Sopenharmony_ci vb->stats_vq = vqs[VIRTIO_BALLOON_VQ_STATS]; 54262306a36Sopenharmony_ci 54362306a36Sopenharmony_ci /* 54462306a36Sopenharmony_ci * Prime this virtqueue with one buffer so the hypervisor can 54562306a36Sopenharmony_ci * use it to signal us later (it can't be broken yet!). 54662306a36Sopenharmony_ci */ 54762306a36Sopenharmony_ci num_stats = update_balloon_stats(vb); 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats); 55062306a36Sopenharmony_ci err = virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb, 55162306a36Sopenharmony_ci GFP_KERNEL); 55262306a36Sopenharmony_ci if (err) { 55362306a36Sopenharmony_ci dev_warn(&vb->vdev->dev, "%s: add stat_vq failed\n", 55462306a36Sopenharmony_ci __func__); 55562306a36Sopenharmony_ci return err; 55662306a36Sopenharmony_ci } 55762306a36Sopenharmony_ci virtqueue_kick(vb->stats_vq); 55862306a36Sopenharmony_ci } 55962306a36Sopenharmony_ci 56062306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) 56162306a36Sopenharmony_ci vb->free_page_vq = vqs[VIRTIO_BALLOON_VQ_FREE_PAGE]; 56262306a36Sopenharmony_ci 56362306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) 56462306a36Sopenharmony_ci vb->reporting_vq = vqs[VIRTIO_BALLOON_VQ_REPORTING]; 56562306a36Sopenharmony_ci 56662306a36Sopenharmony_ci return 0; 56762306a36Sopenharmony_ci} 56862306a36Sopenharmony_ci 56962306a36Sopenharmony_cistatic u32 virtio_balloon_cmd_id_received(struct virtio_balloon *vb) 57062306a36Sopenharmony_ci{ 57162306a36Sopenharmony_ci if (test_and_clear_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID, 57262306a36Sopenharmony_ci &vb->config_read_bitmap)) { 57362306a36Sopenharmony_ci /* Legacy balloon config space is LE, unlike all other devices. */ 57462306a36Sopenharmony_ci virtio_cread_le(vb->vdev, struct virtio_balloon_config, 57562306a36Sopenharmony_ci free_page_hint_cmd_id, 57662306a36Sopenharmony_ci &vb->cmd_id_received_cache); 57762306a36Sopenharmony_ci } 57862306a36Sopenharmony_ci 57962306a36Sopenharmony_ci return vb->cmd_id_received_cache; 58062306a36Sopenharmony_ci} 58162306a36Sopenharmony_ci 58262306a36Sopenharmony_cistatic int send_cmd_id_start(struct virtio_balloon *vb) 58362306a36Sopenharmony_ci{ 58462306a36Sopenharmony_ci struct scatterlist sg; 58562306a36Sopenharmony_ci struct virtqueue *vq = vb->free_page_vq; 58662306a36Sopenharmony_ci int err, unused; 58762306a36Sopenharmony_ci 58862306a36Sopenharmony_ci /* Detach all the used buffers from the vq */ 58962306a36Sopenharmony_ci while (virtqueue_get_buf(vq, &unused)) 59062306a36Sopenharmony_ci ; 59162306a36Sopenharmony_ci 59262306a36Sopenharmony_ci vb->cmd_id_active = cpu_to_virtio32(vb->vdev, 59362306a36Sopenharmony_ci virtio_balloon_cmd_id_received(vb)); 59462306a36Sopenharmony_ci sg_init_one(&sg, &vb->cmd_id_active, sizeof(vb->cmd_id_active)); 59562306a36Sopenharmony_ci err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_active, GFP_KERNEL); 59662306a36Sopenharmony_ci if (!err) 59762306a36Sopenharmony_ci virtqueue_kick(vq); 59862306a36Sopenharmony_ci return err; 59962306a36Sopenharmony_ci} 60062306a36Sopenharmony_ci 60162306a36Sopenharmony_cistatic int send_cmd_id_stop(struct virtio_balloon *vb) 60262306a36Sopenharmony_ci{ 60362306a36Sopenharmony_ci struct scatterlist sg; 60462306a36Sopenharmony_ci struct virtqueue *vq = vb->free_page_vq; 60562306a36Sopenharmony_ci int err, unused; 60662306a36Sopenharmony_ci 60762306a36Sopenharmony_ci /* Detach all the used buffers from the vq */ 60862306a36Sopenharmony_ci while (virtqueue_get_buf(vq, &unused)) 60962306a36Sopenharmony_ci ; 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci sg_init_one(&sg, &vb->cmd_id_stop, sizeof(vb->cmd_id_stop)); 61262306a36Sopenharmony_ci err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_stop, GFP_KERNEL); 61362306a36Sopenharmony_ci if (!err) 61462306a36Sopenharmony_ci virtqueue_kick(vq); 61562306a36Sopenharmony_ci return err; 61662306a36Sopenharmony_ci} 61762306a36Sopenharmony_ci 61862306a36Sopenharmony_cistatic int get_free_page_and_send(struct virtio_balloon *vb) 61962306a36Sopenharmony_ci{ 62062306a36Sopenharmony_ci struct virtqueue *vq = vb->free_page_vq; 62162306a36Sopenharmony_ci struct page *page; 62262306a36Sopenharmony_ci struct scatterlist sg; 62362306a36Sopenharmony_ci int err, unused; 62462306a36Sopenharmony_ci void *p; 62562306a36Sopenharmony_ci 62662306a36Sopenharmony_ci /* Detach all the used buffers from the vq */ 62762306a36Sopenharmony_ci while (virtqueue_get_buf(vq, &unused)) 62862306a36Sopenharmony_ci ; 62962306a36Sopenharmony_ci 63062306a36Sopenharmony_ci page = alloc_pages(VIRTIO_BALLOON_FREE_PAGE_ALLOC_FLAG, 63162306a36Sopenharmony_ci VIRTIO_BALLOON_HINT_BLOCK_ORDER); 63262306a36Sopenharmony_ci /* 63362306a36Sopenharmony_ci * When the allocation returns NULL, it indicates that we have got all 63462306a36Sopenharmony_ci * the possible free pages, so return -EINTR to stop. 63562306a36Sopenharmony_ci */ 63662306a36Sopenharmony_ci if (!page) 63762306a36Sopenharmony_ci return -EINTR; 63862306a36Sopenharmony_ci 63962306a36Sopenharmony_ci p = page_address(page); 64062306a36Sopenharmony_ci sg_init_one(&sg, p, VIRTIO_BALLOON_HINT_BLOCK_BYTES); 64162306a36Sopenharmony_ci /* There is always 1 entry reserved for the cmd id to use. */ 64262306a36Sopenharmony_ci if (vq->num_free > 1) { 64362306a36Sopenharmony_ci err = virtqueue_add_inbuf(vq, &sg, 1, p, GFP_KERNEL); 64462306a36Sopenharmony_ci if (unlikely(err)) { 64562306a36Sopenharmony_ci free_pages((unsigned long)p, 64662306a36Sopenharmony_ci VIRTIO_BALLOON_HINT_BLOCK_ORDER); 64762306a36Sopenharmony_ci return err; 64862306a36Sopenharmony_ci } 64962306a36Sopenharmony_ci virtqueue_kick(vq); 65062306a36Sopenharmony_ci spin_lock_irq(&vb->free_page_list_lock); 65162306a36Sopenharmony_ci balloon_page_push(&vb->free_page_list, page); 65262306a36Sopenharmony_ci vb->num_free_page_blocks++; 65362306a36Sopenharmony_ci spin_unlock_irq(&vb->free_page_list_lock); 65462306a36Sopenharmony_ci } else { 65562306a36Sopenharmony_ci /* 65662306a36Sopenharmony_ci * The vq has no available entry to add this page block, so 65762306a36Sopenharmony_ci * just free it. 65862306a36Sopenharmony_ci */ 65962306a36Sopenharmony_ci free_pages((unsigned long)p, VIRTIO_BALLOON_HINT_BLOCK_ORDER); 66062306a36Sopenharmony_ci } 66162306a36Sopenharmony_ci 66262306a36Sopenharmony_ci return 0; 66362306a36Sopenharmony_ci} 66462306a36Sopenharmony_ci 66562306a36Sopenharmony_cistatic int send_free_pages(struct virtio_balloon *vb) 66662306a36Sopenharmony_ci{ 66762306a36Sopenharmony_ci int err; 66862306a36Sopenharmony_ci u32 cmd_id_active; 66962306a36Sopenharmony_ci 67062306a36Sopenharmony_ci while (1) { 67162306a36Sopenharmony_ci /* 67262306a36Sopenharmony_ci * If a stop id or a new cmd id was just received from host, 67362306a36Sopenharmony_ci * stop the reporting. 67462306a36Sopenharmony_ci */ 67562306a36Sopenharmony_ci cmd_id_active = virtio32_to_cpu(vb->vdev, vb->cmd_id_active); 67662306a36Sopenharmony_ci if (unlikely(cmd_id_active != 67762306a36Sopenharmony_ci virtio_balloon_cmd_id_received(vb))) 67862306a36Sopenharmony_ci break; 67962306a36Sopenharmony_ci 68062306a36Sopenharmony_ci /* 68162306a36Sopenharmony_ci * The free page blocks are allocated and sent to host one by 68262306a36Sopenharmony_ci * one. 68362306a36Sopenharmony_ci */ 68462306a36Sopenharmony_ci err = get_free_page_and_send(vb); 68562306a36Sopenharmony_ci if (err == -EINTR) 68662306a36Sopenharmony_ci break; 68762306a36Sopenharmony_ci else if (unlikely(err)) 68862306a36Sopenharmony_ci return err; 68962306a36Sopenharmony_ci } 69062306a36Sopenharmony_ci 69162306a36Sopenharmony_ci return 0; 69262306a36Sopenharmony_ci} 69362306a36Sopenharmony_ci 69462306a36Sopenharmony_cistatic void virtio_balloon_report_free_page(struct virtio_balloon *vb) 69562306a36Sopenharmony_ci{ 69662306a36Sopenharmony_ci int err; 69762306a36Sopenharmony_ci struct device *dev = &vb->vdev->dev; 69862306a36Sopenharmony_ci 69962306a36Sopenharmony_ci /* Start by sending the received cmd id to host with an outbuf. */ 70062306a36Sopenharmony_ci err = send_cmd_id_start(vb); 70162306a36Sopenharmony_ci if (unlikely(err)) 70262306a36Sopenharmony_ci dev_err(dev, "Failed to send a start id, err = %d\n", err); 70362306a36Sopenharmony_ci 70462306a36Sopenharmony_ci err = send_free_pages(vb); 70562306a36Sopenharmony_ci if (unlikely(err)) 70662306a36Sopenharmony_ci dev_err(dev, "Failed to send a free page, err = %d\n", err); 70762306a36Sopenharmony_ci 70862306a36Sopenharmony_ci /* End by sending a stop id to host with an outbuf. */ 70962306a36Sopenharmony_ci err = send_cmd_id_stop(vb); 71062306a36Sopenharmony_ci if (unlikely(err)) 71162306a36Sopenharmony_ci dev_err(dev, "Failed to send a stop id, err = %d\n", err); 71262306a36Sopenharmony_ci} 71362306a36Sopenharmony_ci 71462306a36Sopenharmony_cistatic void report_free_page_func(struct work_struct *work) 71562306a36Sopenharmony_ci{ 71662306a36Sopenharmony_ci struct virtio_balloon *vb = container_of(work, struct virtio_balloon, 71762306a36Sopenharmony_ci report_free_page_work); 71862306a36Sopenharmony_ci u32 cmd_id_received; 71962306a36Sopenharmony_ci 72062306a36Sopenharmony_ci cmd_id_received = virtio_balloon_cmd_id_received(vb); 72162306a36Sopenharmony_ci if (cmd_id_received == VIRTIO_BALLOON_CMD_ID_DONE) { 72262306a36Sopenharmony_ci /* Pass ULONG_MAX to give back all the free pages */ 72362306a36Sopenharmony_ci return_free_pages_to_mm(vb, ULONG_MAX); 72462306a36Sopenharmony_ci } else if (cmd_id_received != VIRTIO_BALLOON_CMD_ID_STOP && 72562306a36Sopenharmony_ci cmd_id_received != 72662306a36Sopenharmony_ci virtio32_to_cpu(vb->vdev, vb->cmd_id_active)) { 72762306a36Sopenharmony_ci virtio_balloon_report_free_page(vb); 72862306a36Sopenharmony_ci } 72962306a36Sopenharmony_ci} 73062306a36Sopenharmony_ci 73162306a36Sopenharmony_ci#ifdef CONFIG_BALLOON_COMPACTION 73262306a36Sopenharmony_ci/* 73362306a36Sopenharmony_ci * virtballoon_migratepage - perform the balloon page migration on behalf of 73462306a36Sopenharmony_ci * a compaction thread. (called under page lock) 73562306a36Sopenharmony_ci * @vb_dev_info: the balloon device 73662306a36Sopenharmony_ci * @newpage: page that will replace the isolated page after migration finishes. 73762306a36Sopenharmony_ci * @page : the isolated (old) page that is about to be migrated to newpage. 73862306a36Sopenharmony_ci * @mode : compaction mode -- not used for balloon page migration. 73962306a36Sopenharmony_ci * 74062306a36Sopenharmony_ci * After a ballooned page gets isolated by compaction procedures, this is the 74162306a36Sopenharmony_ci * function that performs the page migration on behalf of a compaction thread 74262306a36Sopenharmony_ci * The page migration for virtio balloon is done in a simple swap fashion which 74362306a36Sopenharmony_ci * follows these two macro steps: 74462306a36Sopenharmony_ci * 1) insert newpage into vb->pages list and update the host about it; 74562306a36Sopenharmony_ci * 2) update the host about the old page removed from vb->pages list; 74662306a36Sopenharmony_ci * 74762306a36Sopenharmony_ci * This function preforms the balloon page migration task. 74862306a36Sopenharmony_ci * Called through balloon_mapping->a_ops->migratepage 74962306a36Sopenharmony_ci */ 75062306a36Sopenharmony_cistatic int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info, 75162306a36Sopenharmony_ci struct page *newpage, struct page *page, enum migrate_mode mode) 75262306a36Sopenharmony_ci{ 75362306a36Sopenharmony_ci struct virtio_balloon *vb = container_of(vb_dev_info, 75462306a36Sopenharmony_ci struct virtio_balloon, vb_dev_info); 75562306a36Sopenharmony_ci unsigned long flags; 75662306a36Sopenharmony_ci 75762306a36Sopenharmony_ci /* 75862306a36Sopenharmony_ci * In order to avoid lock contention while migrating pages concurrently 75962306a36Sopenharmony_ci * to leak_balloon() or fill_balloon() we just give up the balloon_lock 76062306a36Sopenharmony_ci * this turn, as it is easier to retry the page migration later. 76162306a36Sopenharmony_ci * This also prevents fill_balloon() getting stuck into a mutex 76262306a36Sopenharmony_ci * recursion in the case it ends up triggering memory compaction 76362306a36Sopenharmony_ci * while it is attempting to inflate the ballon. 76462306a36Sopenharmony_ci */ 76562306a36Sopenharmony_ci if (!mutex_trylock(&vb->balloon_lock)) 76662306a36Sopenharmony_ci return -EAGAIN; 76762306a36Sopenharmony_ci 76862306a36Sopenharmony_ci get_page(newpage); /* balloon reference */ 76962306a36Sopenharmony_ci 77062306a36Sopenharmony_ci /* 77162306a36Sopenharmony_ci * When we migrate a page to a different zone and adjusted the 77262306a36Sopenharmony_ci * managed page count when inflating, we have to fixup the count of 77362306a36Sopenharmony_ci * both involved zones. 77462306a36Sopenharmony_ci */ 77562306a36Sopenharmony_ci if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM) && 77662306a36Sopenharmony_ci page_zone(page) != page_zone(newpage)) { 77762306a36Sopenharmony_ci adjust_managed_page_count(page, 1); 77862306a36Sopenharmony_ci adjust_managed_page_count(newpage, -1); 77962306a36Sopenharmony_ci } 78062306a36Sopenharmony_ci 78162306a36Sopenharmony_ci /* balloon's page migration 1st step -- inflate "newpage" */ 78262306a36Sopenharmony_ci spin_lock_irqsave(&vb_dev_info->pages_lock, flags); 78362306a36Sopenharmony_ci balloon_page_insert(vb_dev_info, newpage); 78462306a36Sopenharmony_ci vb_dev_info->isolated_pages--; 78562306a36Sopenharmony_ci __count_vm_event(BALLOON_MIGRATE); 78662306a36Sopenharmony_ci spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags); 78762306a36Sopenharmony_ci vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE; 78862306a36Sopenharmony_ci set_page_pfns(vb, vb->pfns, newpage); 78962306a36Sopenharmony_ci tell_host(vb, vb->inflate_vq); 79062306a36Sopenharmony_ci 79162306a36Sopenharmony_ci /* balloon's page migration 2nd step -- deflate "page" */ 79262306a36Sopenharmony_ci spin_lock_irqsave(&vb_dev_info->pages_lock, flags); 79362306a36Sopenharmony_ci balloon_page_delete(page); 79462306a36Sopenharmony_ci spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags); 79562306a36Sopenharmony_ci vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE; 79662306a36Sopenharmony_ci set_page_pfns(vb, vb->pfns, page); 79762306a36Sopenharmony_ci tell_host(vb, vb->deflate_vq); 79862306a36Sopenharmony_ci 79962306a36Sopenharmony_ci mutex_unlock(&vb->balloon_lock); 80062306a36Sopenharmony_ci 80162306a36Sopenharmony_ci put_page(page); /* balloon reference */ 80262306a36Sopenharmony_ci 80362306a36Sopenharmony_ci return MIGRATEPAGE_SUCCESS; 80462306a36Sopenharmony_ci} 80562306a36Sopenharmony_ci#endif /* CONFIG_BALLOON_COMPACTION */ 80662306a36Sopenharmony_ci 80762306a36Sopenharmony_cistatic unsigned long shrink_free_pages(struct virtio_balloon *vb, 80862306a36Sopenharmony_ci unsigned long pages_to_free) 80962306a36Sopenharmony_ci{ 81062306a36Sopenharmony_ci unsigned long blocks_to_free, blocks_freed; 81162306a36Sopenharmony_ci 81262306a36Sopenharmony_ci pages_to_free = round_up(pages_to_free, 81362306a36Sopenharmony_ci VIRTIO_BALLOON_HINT_BLOCK_PAGES); 81462306a36Sopenharmony_ci blocks_to_free = pages_to_free / VIRTIO_BALLOON_HINT_BLOCK_PAGES; 81562306a36Sopenharmony_ci blocks_freed = return_free_pages_to_mm(vb, blocks_to_free); 81662306a36Sopenharmony_ci 81762306a36Sopenharmony_ci return blocks_freed * VIRTIO_BALLOON_HINT_BLOCK_PAGES; 81862306a36Sopenharmony_ci} 81962306a36Sopenharmony_ci 82062306a36Sopenharmony_cistatic unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, 82162306a36Sopenharmony_ci struct shrink_control *sc) 82262306a36Sopenharmony_ci{ 82362306a36Sopenharmony_ci struct virtio_balloon *vb = container_of(shrinker, 82462306a36Sopenharmony_ci struct virtio_balloon, shrinker); 82562306a36Sopenharmony_ci 82662306a36Sopenharmony_ci return shrink_free_pages(vb, sc->nr_to_scan); 82762306a36Sopenharmony_ci} 82862306a36Sopenharmony_ci 82962306a36Sopenharmony_cistatic unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker, 83062306a36Sopenharmony_ci struct shrink_control *sc) 83162306a36Sopenharmony_ci{ 83262306a36Sopenharmony_ci struct virtio_balloon *vb = container_of(shrinker, 83362306a36Sopenharmony_ci struct virtio_balloon, shrinker); 83462306a36Sopenharmony_ci 83562306a36Sopenharmony_ci return vb->num_free_page_blocks * VIRTIO_BALLOON_HINT_BLOCK_PAGES; 83662306a36Sopenharmony_ci} 83762306a36Sopenharmony_ci 83862306a36Sopenharmony_cistatic int virtio_balloon_oom_notify(struct notifier_block *nb, 83962306a36Sopenharmony_ci unsigned long dummy, void *parm) 84062306a36Sopenharmony_ci{ 84162306a36Sopenharmony_ci struct virtio_balloon *vb = container_of(nb, 84262306a36Sopenharmony_ci struct virtio_balloon, oom_nb); 84362306a36Sopenharmony_ci unsigned long *freed = parm; 84462306a36Sopenharmony_ci 84562306a36Sopenharmony_ci *freed += leak_balloon(vb, VIRTIO_BALLOON_OOM_NR_PAGES) / 84662306a36Sopenharmony_ci VIRTIO_BALLOON_PAGES_PER_PAGE; 84762306a36Sopenharmony_ci update_balloon_size(vb); 84862306a36Sopenharmony_ci 84962306a36Sopenharmony_ci return NOTIFY_OK; 85062306a36Sopenharmony_ci} 85162306a36Sopenharmony_ci 85262306a36Sopenharmony_cistatic void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb) 85362306a36Sopenharmony_ci{ 85462306a36Sopenharmony_ci unregister_shrinker(&vb->shrinker); 85562306a36Sopenharmony_ci} 85662306a36Sopenharmony_ci 85762306a36Sopenharmony_cistatic int virtio_balloon_register_shrinker(struct virtio_balloon *vb) 85862306a36Sopenharmony_ci{ 85962306a36Sopenharmony_ci vb->shrinker.scan_objects = virtio_balloon_shrinker_scan; 86062306a36Sopenharmony_ci vb->shrinker.count_objects = virtio_balloon_shrinker_count; 86162306a36Sopenharmony_ci vb->shrinker.seeks = DEFAULT_SEEKS; 86262306a36Sopenharmony_ci 86362306a36Sopenharmony_ci return register_shrinker(&vb->shrinker, "virtio-balloon"); 86462306a36Sopenharmony_ci} 86562306a36Sopenharmony_ci 86662306a36Sopenharmony_cistatic int virtballoon_probe(struct virtio_device *vdev) 86762306a36Sopenharmony_ci{ 86862306a36Sopenharmony_ci struct virtio_balloon *vb; 86962306a36Sopenharmony_ci int err; 87062306a36Sopenharmony_ci 87162306a36Sopenharmony_ci if (!vdev->config->get) { 87262306a36Sopenharmony_ci dev_err(&vdev->dev, "%s failure: config access disabled\n", 87362306a36Sopenharmony_ci __func__); 87462306a36Sopenharmony_ci return -EINVAL; 87562306a36Sopenharmony_ci } 87662306a36Sopenharmony_ci 87762306a36Sopenharmony_ci vdev->priv = vb = kzalloc(sizeof(*vb), GFP_KERNEL); 87862306a36Sopenharmony_ci if (!vb) { 87962306a36Sopenharmony_ci err = -ENOMEM; 88062306a36Sopenharmony_ci goto out; 88162306a36Sopenharmony_ci } 88262306a36Sopenharmony_ci 88362306a36Sopenharmony_ci INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func); 88462306a36Sopenharmony_ci INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func); 88562306a36Sopenharmony_ci spin_lock_init(&vb->stop_update_lock); 88662306a36Sopenharmony_ci mutex_init(&vb->balloon_lock); 88762306a36Sopenharmony_ci init_waitqueue_head(&vb->acked); 88862306a36Sopenharmony_ci vb->vdev = vdev; 88962306a36Sopenharmony_ci 89062306a36Sopenharmony_ci balloon_devinfo_init(&vb->vb_dev_info); 89162306a36Sopenharmony_ci 89262306a36Sopenharmony_ci err = init_vqs(vb); 89362306a36Sopenharmony_ci if (err) 89462306a36Sopenharmony_ci goto out_free_vb; 89562306a36Sopenharmony_ci 89662306a36Sopenharmony_ci#ifdef CONFIG_BALLOON_COMPACTION 89762306a36Sopenharmony_ci vb->vb_dev_info.migratepage = virtballoon_migratepage; 89862306a36Sopenharmony_ci#endif 89962306a36Sopenharmony_ci if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) { 90062306a36Sopenharmony_ci /* 90162306a36Sopenharmony_ci * There is always one entry reserved for cmd id, so the ring 90262306a36Sopenharmony_ci * size needs to be at least two to report free page hints. 90362306a36Sopenharmony_ci */ 90462306a36Sopenharmony_ci if (virtqueue_get_vring_size(vb->free_page_vq) < 2) { 90562306a36Sopenharmony_ci err = -ENOSPC; 90662306a36Sopenharmony_ci goto out_del_vqs; 90762306a36Sopenharmony_ci } 90862306a36Sopenharmony_ci vb->balloon_wq = alloc_workqueue("balloon-wq", 90962306a36Sopenharmony_ci WQ_FREEZABLE | WQ_CPU_INTENSIVE, 0); 91062306a36Sopenharmony_ci if (!vb->balloon_wq) { 91162306a36Sopenharmony_ci err = -ENOMEM; 91262306a36Sopenharmony_ci goto out_del_vqs; 91362306a36Sopenharmony_ci } 91462306a36Sopenharmony_ci INIT_WORK(&vb->report_free_page_work, report_free_page_func); 91562306a36Sopenharmony_ci vb->cmd_id_received_cache = VIRTIO_BALLOON_CMD_ID_STOP; 91662306a36Sopenharmony_ci vb->cmd_id_active = cpu_to_virtio32(vb->vdev, 91762306a36Sopenharmony_ci VIRTIO_BALLOON_CMD_ID_STOP); 91862306a36Sopenharmony_ci vb->cmd_id_stop = cpu_to_virtio32(vb->vdev, 91962306a36Sopenharmony_ci VIRTIO_BALLOON_CMD_ID_STOP); 92062306a36Sopenharmony_ci spin_lock_init(&vb->free_page_list_lock); 92162306a36Sopenharmony_ci INIT_LIST_HEAD(&vb->free_page_list); 92262306a36Sopenharmony_ci /* 92362306a36Sopenharmony_ci * We're allowed to reuse any free pages, even if they are 92462306a36Sopenharmony_ci * still to be processed by the host. 92562306a36Sopenharmony_ci */ 92662306a36Sopenharmony_ci err = virtio_balloon_register_shrinker(vb); 92762306a36Sopenharmony_ci if (err) 92862306a36Sopenharmony_ci goto out_del_balloon_wq; 92962306a36Sopenharmony_ci } 93062306a36Sopenharmony_ci 93162306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) { 93262306a36Sopenharmony_ci vb->oom_nb.notifier_call = virtio_balloon_oom_notify; 93362306a36Sopenharmony_ci vb->oom_nb.priority = VIRTIO_BALLOON_OOM_NOTIFY_PRIORITY; 93462306a36Sopenharmony_ci err = register_oom_notifier(&vb->oom_nb); 93562306a36Sopenharmony_ci if (err < 0) 93662306a36Sopenharmony_ci goto out_unregister_shrinker; 93762306a36Sopenharmony_ci } 93862306a36Sopenharmony_ci 93962306a36Sopenharmony_ci if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) { 94062306a36Sopenharmony_ci /* Start with poison val of 0 representing general init */ 94162306a36Sopenharmony_ci __u32 poison_val = 0; 94262306a36Sopenharmony_ci 94362306a36Sopenharmony_ci /* 94462306a36Sopenharmony_ci * Let the hypervisor know that we are expecting a 94562306a36Sopenharmony_ci * specific value to be written back in balloon pages. 94662306a36Sopenharmony_ci * 94762306a36Sopenharmony_ci * If the PAGE_POISON value was larger than a byte we would 94862306a36Sopenharmony_ci * need to byte swap poison_val here to guarantee it is 94962306a36Sopenharmony_ci * little-endian. However for now it is a single byte so we 95062306a36Sopenharmony_ci * can pass it as-is. 95162306a36Sopenharmony_ci */ 95262306a36Sopenharmony_ci if (!want_init_on_free()) 95362306a36Sopenharmony_ci memset(&poison_val, PAGE_POISON, sizeof(poison_val)); 95462306a36Sopenharmony_ci 95562306a36Sopenharmony_ci virtio_cwrite_le(vb->vdev, struct virtio_balloon_config, 95662306a36Sopenharmony_ci poison_val, &poison_val); 95762306a36Sopenharmony_ci } 95862306a36Sopenharmony_ci 95962306a36Sopenharmony_ci vb->pr_dev_info.report = virtballoon_free_page_report; 96062306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) { 96162306a36Sopenharmony_ci unsigned int capacity; 96262306a36Sopenharmony_ci 96362306a36Sopenharmony_ci capacity = virtqueue_get_vring_size(vb->reporting_vq); 96462306a36Sopenharmony_ci if (capacity < PAGE_REPORTING_CAPACITY) { 96562306a36Sopenharmony_ci err = -ENOSPC; 96662306a36Sopenharmony_ci goto out_unregister_oom; 96762306a36Sopenharmony_ci } 96862306a36Sopenharmony_ci 96962306a36Sopenharmony_ci /* 97062306a36Sopenharmony_ci * The default page reporting order is @pageblock_order, which 97162306a36Sopenharmony_ci * corresponds to 512MB in size on ARM64 when 64KB base page 97262306a36Sopenharmony_ci * size is used. The page reporting won't be triggered if the 97362306a36Sopenharmony_ci * freeing page can't come up with a free area like that huge. 97462306a36Sopenharmony_ci * So we specify the page reporting order to 5, corresponding 97562306a36Sopenharmony_ci * to 2MB. It helps to avoid THP splitting if 4KB base page 97662306a36Sopenharmony_ci * size is used by host. 97762306a36Sopenharmony_ci * 97862306a36Sopenharmony_ci * Ideally, the page reporting order is selected based on the 97962306a36Sopenharmony_ci * host's base page size. However, it needs more work to report 98062306a36Sopenharmony_ci * that value. The hard-coded order would be fine currently. 98162306a36Sopenharmony_ci */ 98262306a36Sopenharmony_ci#if defined(CONFIG_ARM64) && defined(CONFIG_ARM64_64K_PAGES) 98362306a36Sopenharmony_ci vb->pr_dev_info.order = 5; 98462306a36Sopenharmony_ci#endif 98562306a36Sopenharmony_ci 98662306a36Sopenharmony_ci err = page_reporting_register(&vb->pr_dev_info); 98762306a36Sopenharmony_ci if (err) 98862306a36Sopenharmony_ci goto out_unregister_oom; 98962306a36Sopenharmony_ci } 99062306a36Sopenharmony_ci 99162306a36Sopenharmony_ci virtio_device_ready(vdev); 99262306a36Sopenharmony_ci 99362306a36Sopenharmony_ci if (towards_target(vb)) 99462306a36Sopenharmony_ci virtballoon_changed(vdev); 99562306a36Sopenharmony_ci return 0; 99662306a36Sopenharmony_ci 99762306a36Sopenharmony_ciout_unregister_oom: 99862306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) 99962306a36Sopenharmony_ci unregister_oom_notifier(&vb->oom_nb); 100062306a36Sopenharmony_ciout_unregister_shrinker: 100162306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) 100262306a36Sopenharmony_ci virtio_balloon_unregister_shrinker(vb); 100362306a36Sopenharmony_ciout_del_balloon_wq: 100462306a36Sopenharmony_ci if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) 100562306a36Sopenharmony_ci destroy_workqueue(vb->balloon_wq); 100662306a36Sopenharmony_ciout_del_vqs: 100762306a36Sopenharmony_ci vdev->config->del_vqs(vdev); 100862306a36Sopenharmony_ciout_free_vb: 100962306a36Sopenharmony_ci kfree(vb); 101062306a36Sopenharmony_ciout: 101162306a36Sopenharmony_ci return err; 101262306a36Sopenharmony_ci} 101362306a36Sopenharmony_ci 101462306a36Sopenharmony_cistatic void remove_common(struct virtio_balloon *vb) 101562306a36Sopenharmony_ci{ 101662306a36Sopenharmony_ci /* There might be pages left in the balloon: free them. */ 101762306a36Sopenharmony_ci while (vb->num_pages) 101862306a36Sopenharmony_ci leak_balloon(vb, vb->num_pages); 101962306a36Sopenharmony_ci update_balloon_size(vb); 102062306a36Sopenharmony_ci 102162306a36Sopenharmony_ci /* There might be free pages that are being reported: release them. */ 102262306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) 102362306a36Sopenharmony_ci return_free_pages_to_mm(vb, ULONG_MAX); 102462306a36Sopenharmony_ci 102562306a36Sopenharmony_ci /* Now we reset the device so we can clean up the queues. */ 102662306a36Sopenharmony_ci virtio_reset_device(vb->vdev); 102762306a36Sopenharmony_ci 102862306a36Sopenharmony_ci vb->vdev->config->del_vqs(vb->vdev); 102962306a36Sopenharmony_ci} 103062306a36Sopenharmony_ci 103162306a36Sopenharmony_cistatic void virtballoon_remove(struct virtio_device *vdev) 103262306a36Sopenharmony_ci{ 103362306a36Sopenharmony_ci struct virtio_balloon *vb = vdev->priv; 103462306a36Sopenharmony_ci 103562306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) 103662306a36Sopenharmony_ci page_reporting_unregister(&vb->pr_dev_info); 103762306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) 103862306a36Sopenharmony_ci unregister_oom_notifier(&vb->oom_nb); 103962306a36Sopenharmony_ci if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) 104062306a36Sopenharmony_ci virtio_balloon_unregister_shrinker(vb); 104162306a36Sopenharmony_ci spin_lock_irq(&vb->stop_update_lock); 104262306a36Sopenharmony_ci vb->stop_update = true; 104362306a36Sopenharmony_ci spin_unlock_irq(&vb->stop_update_lock); 104462306a36Sopenharmony_ci cancel_work_sync(&vb->update_balloon_size_work); 104562306a36Sopenharmony_ci cancel_work_sync(&vb->update_balloon_stats_work); 104662306a36Sopenharmony_ci 104762306a36Sopenharmony_ci if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) { 104862306a36Sopenharmony_ci cancel_work_sync(&vb->report_free_page_work); 104962306a36Sopenharmony_ci destroy_workqueue(vb->balloon_wq); 105062306a36Sopenharmony_ci } 105162306a36Sopenharmony_ci 105262306a36Sopenharmony_ci remove_common(vb); 105362306a36Sopenharmony_ci kfree(vb); 105462306a36Sopenharmony_ci} 105562306a36Sopenharmony_ci 105662306a36Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 105762306a36Sopenharmony_cistatic int virtballoon_freeze(struct virtio_device *vdev) 105862306a36Sopenharmony_ci{ 105962306a36Sopenharmony_ci struct virtio_balloon *vb = vdev->priv; 106062306a36Sopenharmony_ci 106162306a36Sopenharmony_ci /* 106262306a36Sopenharmony_ci * The workqueue is already frozen by the PM core before this 106362306a36Sopenharmony_ci * function is called. 106462306a36Sopenharmony_ci */ 106562306a36Sopenharmony_ci remove_common(vb); 106662306a36Sopenharmony_ci return 0; 106762306a36Sopenharmony_ci} 106862306a36Sopenharmony_ci 106962306a36Sopenharmony_cistatic int virtballoon_restore(struct virtio_device *vdev) 107062306a36Sopenharmony_ci{ 107162306a36Sopenharmony_ci struct virtio_balloon *vb = vdev->priv; 107262306a36Sopenharmony_ci int ret; 107362306a36Sopenharmony_ci 107462306a36Sopenharmony_ci ret = init_vqs(vdev->priv); 107562306a36Sopenharmony_ci if (ret) 107662306a36Sopenharmony_ci return ret; 107762306a36Sopenharmony_ci 107862306a36Sopenharmony_ci virtio_device_ready(vdev); 107962306a36Sopenharmony_ci 108062306a36Sopenharmony_ci if (towards_target(vb)) 108162306a36Sopenharmony_ci virtballoon_changed(vdev); 108262306a36Sopenharmony_ci update_balloon_size(vb); 108362306a36Sopenharmony_ci return 0; 108462306a36Sopenharmony_ci} 108562306a36Sopenharmony_ci#endif 108662306a36Sopenharmony_ci 108762306a36Sopenharmony_cistatic int virtballoon_validate(struct virtio_device *vdev) 108862306a36Sopenharmony_ci{ 108962306a36Sopenharmony_ci /* 109062306a36Sopenharmony_ci * Inform the hypervisor that our pages are poisoned or 109162306a36Sopenharmony_ci * initialized. If we cannot do that then we should disable 109262306a36Sopenharmony_ci * page reporting as it could potentially change the contents 109362306a36Sopenharmony_ci * of our free pages. 109462306a36Sopenharmony_ci */ 109562306a36Sopenharmony_ci if (!want_init_on_free() && !page_poisoning_enabled_static()) 109662306a36Sopenharmony_ci __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_PAGE_POISON); 109762306a36Sopenharmony_ci else if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) 109862306a36Sopenharmony_ci __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_REPORTING); 109962306a36Sopenharmony_ci 110062306a36Sopenharmony_ci __virtio_clear_bit(vdev, VIRTIO_F_ACCESS_PLATFORM); 110162306a36Sopenharmony_ci return 0; 110262306a36Sopenharmony_ci} 110362306a36Sopenharmony_ci 110462306a36Sopenharmony_cistatic unsigned int features[] = { 110562306a36Sopenharmony_ci VIRTIO_BALLOON_F_MUST_TELL_HOST, 110662306a36Sopenharmony_ci VIRTIO_BALLOON_F_STATS_VQ, 110762306a36Sopenharmony_ci VIRTIO_BALLOON_F_DEFLATE_ON_OOM, 110862306a36Sopenharmony_ci VIRTIO_BALLOON_F_FREE_PAGE_HINT, 110962306a36Sopenharmony_ci VIRTIO_BALLOON_F_PAGE_POISON, 111062306a36Sopenharmony_ci VIRTIO_BALLOON_F_REPORTING, 111162306a36Sopenharmony_ci}; 111262306a36Sopenharmony_ci 111362306a36Sopenharmony_cistatic struct virtio_driver virtio_balloon_driver = { 111462306a36Sopenharmony_ci .feature_table = features, 111562306a36Sopenharmony_ci .feature_table_size = ARRAY_SIZE(features), 111662306a36Sopenharmony_ci .driver.name = KBUILD_MODNAME, 111762306a36Sopenharmony_ci .driver.owner = THIS_MODULE, 111862306a36Sopenharmony_ci .id_table = id_table, 111962306a36Sopenharmony_ci .validate = virtballoon_validate, 112062306a36Sopenharmony_ci .probe = virtballoon_probe, 112162306a36Sopenharmony_ci .remove = virtballoon_remove, 112262306a36Sopenharmony_ci .config_changed = virtballoon_changed, 112362306a36Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 112462306a36Sopenharmony_ci .freeze = virtballoon_freeze, 112562306a36Sopenharmony_ci .restore = virtballoon_restore, 112662306a36Sopenharmony_ci#endif 112762306a36Sopenharmony_ci}; 112862306a36Sopenharmony_ci 112962306a36Sopenharmony_cimodule_virtio_driver(virtio_balloon_driver); 113062306a36Sopenharmony_ciMODULE_DEVICE_TABLE(virtio, id_table); 113162306a36Sopenharmony_ciMODULE_DESCRIPTION("Virtio balloon driver"); 113262306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 1133