1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012, Microsoft Corporation.
6 * K. Y. Srinivasan <kys@microsoft.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/jiffies.h>
13 #include <linux/mman.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/kthread.h>
19 #include <linux/completion.h>
20 #include <linux/count_zeros.h>
21 #include <linux/memory_hotplug.h>
22 #include <linux/memory.h>
23 #include <linux/notifier.h>
24 #include <linux/percpu_counter.h>
25 #include <linux/page_reporting.h>
27 #include <linux/hyperv.h>
28 #include <asm/hyperv-tlfs.h>
30 #include <asm/mshyperv.h>
32 #define CREATE_TRACE_POINTS
33 #include "hv_trace_balloon.h"
36 * We begin with definitions supporting the Dynamic Memory protocol
39 * Begin protocol definitions.
45 * Protocol versions. The low word is the minor version, the high word the major
50 * Changed to 0.1 on 2009/03/25
51 * Changes to 0.2 on 2009/05/14
52 * Changes to 0.3 on 2009/12/03
53 * Changed to 1.0 on 2011/04/05
56 #define DYNMEM_MAKE_VERSION(Major, Minor) ((__u32)(((Major) << 16) | (Minor)))
57 #define DYNMEM_MAJOR_VERSION(Version) ((__u32)(Version) >> 16)
58 #define DYNMEM_MINOR_VERSION(Version) ((__u32)(Version) & 0xff)
61 DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
62 DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
63 DYNMEM_PROTOCOL_VERSION_3 = DYNMEM_MAKE_VERSION(2, 0),
65 DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
66 DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
67 DYNMEM_PROTOCOL_VERSION_WIN10 = DYNMEM_PROTOCOL_VERSION_3,
69 DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN10
78 enum dm_message_type {
83 DM_VERSION_REQUEST = 1,
84 DM_VERSION_RESPONSE = 2,
85 DM_CAPABILITIES_REPORT = 3,
86 DM_CAPABILITIES_RESPONSE = 4,
88 DM_BALLOON_REQUEST = 6,
89 DM_BALLOON_RESPONSE = 7,
90 DM_UNBALLOON_REQUEST = 8,
91 DM_UNBALLOON_RESPONSE = 9,
92 DM_MEM_HOT_ADD_REQUEST = 10,
93 DM_MEM_HOT_ADD_RESPONSE = 11,
94 DM_VERSION_03_MAX = 11,
104 * Structures defining the dynamic memory management
122 * To support guests that may have alignment
123 * limitations on hot-add, the guest can specify
124 * its alignment requirements; a value of n
125 * represents an alignment of 2^n in mega bytes.
127 __u64 hot_add_alignment:4;
133 union dm_mem_page_range {
136 * The PFN number of the first page in the range.
137 * 40 bits is the architectural limit of a PFN
142 * The number of pages in the range.
152 * The header for all dynamic memory messages:
154 * type: Type of the message.
155 * size: Size of the message in bytes; including the header.
156 * trans_id: The guest is responsible for manufacturing this ID.
166 * A generic message format for dynamic memory.
167 * Specific message formats are defined later in the file.
171 struct dm_header hdr;
172 __u8 data[]; /* enclosed message */
177 * Specific message types supporting the dynamic memory protocol.
181 * Version negotiation message. Sent from the guest to the host.
182 * The guest is free to try different versions until the host
183 * accepts the version.
185 * dm_version: The protocol version requested.
186 * is_last_attempt: If TRUE, this is the last version guest will request.
187 * reservedz: Reserved field, set to zero.
190 struct dm_version_request {
191 struct dm_header hdr;
192 union dm_version version;
193 __u32 is_last_attempt:1;
198 * Version response message; Host to Guest and indicates
199 * if the host has accepted the version sent by the guest.
201 * is_accepted: If TRUE, host has accepted the version and the guest
202 * should proceed to the next stage of the protocol. FALSE indicates that
203 * guest should re-try with a different version.
205 * reservedz: Reserved field, set to zero.
208 struct dm_version_response {
209 struct dm_header hdr;
215 * Message reporting capabilities. This is sent from the guest to the
219 struct dm_capabilities {
220 struct dm_header hdr;
223 __u64 max_page_number;
227 * Response to the capabilities message. This is sent from the host to the
228 * guest. This message notifies if the host has accepted the guest's
229 * capabilities. If the host has not accepted, the guest must shutdown
232 * is_accepted: Indicates if the host has accepted guest's capabilities.
233 * reservedz: Must be 0.
236 struct dm_capabilities_resp_msg {
237 struct dm_header hdr;
243 * This message is used to report memory pressure from the guest.
244 * This message is not part of any transaction and there is no
245 * response to this message.
247 * num_avail: Available memory in pages.
248 * num_committed: Committed memory in pages.
249 * page_file_size: The accumulated size of all page files
250 * in the system in pages.
251 * zero_free: The nunber of zero and free pages.
252 * page_file_writes: The writes to the page file in pages.
253 * io_diff: An indicator of file cache efficiency or page file activity,
254 * calculated as File Cache Page Fault Count - Page Read Count.
255 * This value is in pages.
257 * Some of these metrics are Windows specific and fortunately
258 * the algorithm on the host side that computes the guest memory
259 * pressure only uses num_committed value.
263 struct dm_header hdr;
266 __u64 page_file_size;
268 __u32 page_file_writes;
274 * Message to ask the guest to allocate memory - balloon up message.
275 * This message is sent from the host to the guest. The guest may not be
276 * able to allocate as much memory as requested.
278 * num_pages: number of pages to allocate.
282 struct dm_header hdr;
289 * Balloon response message; this message is sent from the guest
290 * to the host in response to the balloon message.
292 * reservedz: Reserved; must be set to zero.
293 * more_pages: If FALSE, this is the last message of the transaction.
294 * if TRUE there will atleast one more message from the guest.
296 * range_count: The number of ranges in the range array.
298 * range_array: An array of page ranges returned to the host.
302 struct dm_balloon_response {
303 struct dm_header hdr;
306 __u32 range_count:31;
307 union dm_mem_page_range range_array[];
311 * Un-balloon message; this message is sent from the host
312 * to the guest to give guest more memory.
314 * more_pages: If FALSE, this is the last message of the transaction.
315 * if TRUE there will atleast one more message from the guest.
317 * reservedz: Reserved; must be set to zero.
319 * range_count: The number of ranges in the range array.
321 * range_array: An array of page ranges returned to the host.
325 struct dm_unballoon_request {
326 struct dm_header hdr;
330 union dm_mem_page_range range_array[];
334 * Un-balloon response message; this message is sent from the guest
335 * to the host in response to an unballoon request.
339 struct dm_unballoon_response {
340 struct dm_header hdr;
345 * Hot add request message. Message sent from the host to the guest.
347 * mem_range: Memory range to hot add.
352 struct dm_header hdr;
353 union dm_mem_page_range range;
357 * Hot add response message.
358 * This message is sent by the guest to report the status of a hot add request.
359 * If page_count is less than the requested page count, then the host should
360 * assume all further hot add requests will fail, since this indicates that
361 * the guest has hit an upper physical memory barrier.
363 * Hot adds may also fail due to low resources; in this case, the guest must
364 * not complete this message until the hot add can succeed, and the host must
365 * not send a new hot add request until the response is sent.
366 * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
367 * times it fails the request.
370 * page_count: number of pages that were successfully hot added.
372 * result: result of the operation 1: success, 0: failure.
376 struct dm_hot_add_response {
377 struct dm_header hdr;
383 * Types of information sent from host to the guest.
387 INFO_TYPE_MAX_PAGE_CNT = 0,
393 * Header for the information message.
396 struct dm_info_header {
397 enum dm_info_type type;
402 * This message is sent from the host to the guest to pass
403 * some relevant information (win8 addition).
406 * info_size: size of the information blob.
407 * info: information blob.
411 struct dm_header hdr;
418 * End protocol definitions.
422 * State to manage hot adding memory into the guest.
423 * The range start_pfn : end_pfn specifies the range
424 * that the host has asked us to hot add. The range
425 * start_pfn : ha_end_pfn specifies the range that we have
426 * currently hot added. We hot add in multiples of 128M
427 * chunks; it is possible that we may not be able to bring
428 * online all the pages in the region. The range
429 * covered_start_pfn:covered_end_pfn defines the pages that can
433 struct hv_hotadd_state {
434 struct list_head list;
435 unsigned long start_pfn;
436 unsigned long covered_start_pfn;
437 unsigned long covered_end_pfn;
438 unsigned long ha_end_pfn;
439 unsigned long end_pfn;
443 struct list_head gap_list;
446 struct hv_hotadd_gap {
447 struct list_head list;
448 unsigned long start_pfn;
449 unsigned long end_pfn;
452 struct balloon_state {
454 struct work_struct wrk;
458 union dm_mem_page_range ha_page_range;
459 union dm_mem_page_range ha_region_range;
460 struct work_struct wrk;
463 static bool allow_hibernation;
464 static bool hot_add = true;
465 static bool do_hot_add;
467 * Delay reporting memory pressure by
468 * the specified number of seconds.
470 static uint pressure_report_delay = 45;
473 * The last time we posted a pressure report to host.
475 static unsigned long last_post_time;
477 module_param(hot_add, bool, (S_IRUGO | S_IWUSR));
478 MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
480 module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR));
481 MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
482 static atomic_t trans_id = ATOMIC_INIT(0);
484 static int dm_ring_size = VMBUS_RING_SIZE(16 * 1024);
487 * Driver specific state.
500 static __u8 recv_buffer[HV_HYP_PAGE_SIZE];
501 static __u8 balloon_up_send_buffer[HV_HYP_PAGE_SIZE];
502 #define PAGES_IN_2M (2 * 1024 * 1024 / PAGE_SIZE)
503 #define HA_CHUNK (128 * 1024 * 1024 / PAGE_SIZE)
505 struct hv_dynmem_device {
506 struct hv_device *dev;
507 enum hv_dm_state state;
508 struct completion host_event;
509 struct completion config_event;
512 * Number of pages we have currently ballooned out.
514 unsigned int num_pages_ballooned;
515 unsigned int num_pages_onlined;
516 unsigned int num_pages_added;
519 * State to manage the ballooning (up) operation.
521 struct balloon_state balloon_wrk;
524 * State to execute the "hot-add" operation.
526 struct hot_add_wrk ha_wrk;
529 * This state tracks if the host has specified a hot-add
532 bool host_specified_ha_region;
535 * State to synchronize hot-add.
537 struct completion ol_waitevent;
539 * This thread handles hot-add
540 * requests from the host as well as notifying
541 * the host with regards to memory pressure in
544 struct task_struct *thread;
547 * Protects ha_region_list, num_pages_onlined counter and individual
548 * regions from ha_region_list.
553 * A list of hot-add regions.
555 struct list_head ha_region_list;
558 * We start with the highest version we can support
559 * and downgrade based on the host; we save here the
560 * next version to try.
565 * The negotiated version agreed by host.
569 struct page_reporting_dev_info pr_dev_info;
572 static struct hv_dynmem_device dm_device;
574 static void post_status(struct hv_dynmem_device *dm);
576 #ifdef CONFIG_MEMORY_HOTPLUG
577 static inline bool has_pfn_is_backed(struct hv_hotadd_state *has,
580 struct hv_hotadd_gap *gap;
582 /* The page is not backed. */
583 if ((pfn < has->covered_start_pfn) || (pfn >= has->covered_end_pfn))
586 /* Check for gaps. */
587 list_for_each_entry(gap, &has->gap_list, list) {
588 if ((pfn >= gap->start_pfn) && (pfn < gap->end_pfn))
595 static unsigned long hv_page_offline_check(unsigned long start_pfn,
596 unsigned long nr_pages)
598 unsigned long pfn = start_pfn, count = 0;
599 struct hv_hotadd_state *has;
602 while (pfn < start_pfn + nr_pages) {
604 * Search for HAS which covers the pfn and when we find one
605 * count how many consequitive PFNs are covered.
608 list_for_each_entry(has, &dm_device.ha_region_list, list) {
609 while ((pfn >= has->start_pfn) &&
610 (pfn < has->end_pfn) &&
611 (pfn < start_pfn + nr_pages)) {
613 if (has_pfn_is_backed(has, pfn))
620 * This PFN is not in any HAS (e.g. we're offlining a region
621 * which was present at boot), no need to account for it. Go
631 static int hv_memory_notifier(struct notifier_block *nb, unsigned long val,
634 struct memory_notify *mem = (struct memory_notify *)v;
635 unsigned long flags, pfn_count;
639 case MEM_CANCEL_ONLINE:
640 complete(&dm_device.ol_waitevent);
644 spin_lock_irqsave(&dm_device.ha_lock, flags);
645 pfn_count = hv_page_offline_check(mem->start_pfn,
647 if (pfn_count <= dm_device.num_pages_onlined) {
648 dm_device.num_pages_onlined -= pfn_count;
651 * We're offlining more pages than we managed to online.
652 * This is unexpected. In any case don't let
653 * num_pages_onlined wrap around zero.
656 dm_device.num_pages_onlined = 0;
658 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
660 case MEM_GOING_ONLINE:
661 case MEM_GOING_OFFLINE:
662 case MEM_CANCEL_OFFLINE:
668 static struct notifier_block hv_memory_nb = {
669 .notifier_call = hv_memory_notifier,
673 /* Check if the particular page is backed and can be onlined and online it. */
674 static void hv_page_online_one(struct hv_hotadd_state *has, struct page *pg)
676 if (!has_pfn_is_backed(has, page_to_pfn(pg))) {
677 if (!PageOffline(pg))
678 __SetPageOffline(pg);
682 __ClearPageOffline(pg);
684 /* This frame is currently backed; online the page. */
685 generic_online_page(pg, 0);
687 lockdep_assert_held(&dm_device.ha_lock);
688 dm_device.num_pages_onlined++;
691 static void hv_bring_pgs_online(struct hv_hotadd_state *has,
692 unsigned long start_pfn, unsigned long size)
696 pr_debug("Online %lu pages starting at pfn 0x%lx\n", size, start_pfn);
697 for (i = 0; i < size; i++)
698 hv_page_online_one(has, pfn_to_page(start_pfn + i));
701 static void hv_mem_hot_add(unsigned long start, unsigned long size,
702 unsigned long pfn_count,
703 struct hv_hotadd_state *has)
707 unsigned long start_pfn;
708 unsigned long processed_pfn;
709 unsigned long total_pfn = pfn_count;
712 for (i = 0; i < (size/HA_CHUNK); i++) {
713 start_pfn = start + (i * HA_CHUNK);
715 spin_lock_irqsave(&dm_device.ha_lock, flags);
716 has->ha_end_pfn += HA_CHUNK;
718 if (total_pfn > HA_CHUNK) {
719 processed_pfn = HA_CHUNK;
720 total_pfn -= HA_CHUNK;
722 processed_pfn = total_pfn;
726 has->covered_end_pfn += processed_pfn;
727 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
729 reinit_completion(&dm_device.ol_waitevent);
731 nid = memory_add_physaddr_to_nid(PFN_PHYS(start_pfn));
732 ret = add_memory(nid, PFN_PHYS((start_pfn)),
733 (HA_CHUNK << PAGE_SHIFT), MHP_MERGE_RESOURCE);
736 pr_err("hot_add memory failed error is %d\n", ret);
737 if (ret == -EEXIST) {
739 * This error indicates that the error
740 * is not a transient failure. This is the
741 * case where the guest's physical address map
742 * precludes hot adding memory. Stop all further
747 spin_lock_irqsave(&dm_device.ha_lock, flags);
748 has->ha_end_pfn -= HA_CHUNK;
749 has->covered_end_pfn -= processed_pfn;
750 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
755 * Wait for memory to get onlined. If the kernel onlined the
756 * memory when adding it, this will return directly. Otherwise,
757 * it will wait for user space to online the memory. This helps
758 * to avoid adding memory faster than it is getting onlined. As
759 * adding succeeded, it is ok to proceed even if the memory was
760 * not onlined in time.
762 wait_for_completion_timeout(&dm_device.ol_waitevent, 5 * HZ);
763 post_status(&dm_device);
767 static void hv_online_page(struct page *pg, unsigned int order)
769 struct hv_hotadd_state *has;
771 unsigned long pfn = page_to_pfn(pg);
773 spin_lock_irqsave(&dm_device.ha_lock, flags);
774 list_for_each_entry(has, &dm_device.ha_region_list, list) {
775 /* The page belongs to a different HAS. */
776 if ((pfn < has->start_pfn) ||
777 (pfn + (1UL << order) > has->end_pfn))
780 hv_bring_pgs_online(has, pfn, 1UL << order);
783 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
786 static int pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
788 struct hv_hotadd_state *has;
789 struct hv_hotadd_gap *gap;
790 unsigned long residual, new_inc;
794 spin_lock_irqsave(&dm_device.ha_lock, flags);
795 list_for_each_entry(has, &dm_device.ha_region_list, list) {
797 * If the pfn range we are dealing with is not in the current
798 * "hot add block", move on.
800 if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
804 * If the current start pfn is not where the covered_end
805 * is, create a gap and update covered_end_pfn.
807 if (has->covered_end_pfn != start_pfn) {
808 gap = kzalloc(sizeof(struct hv_hotadd_gap), GFP_ATOMIC);
814 INIT_LIST_HEAD(&gap->list);
815 gap->start_pfn = has->covered_end_pfn;
816 gap->end_pfn = start_pfn;
817 list_add_tail(&gap->list, &has->gap_list);
819 has->covered_end_pfn = start_pfn;
823 * If the current hot add-request extends beyond
824 * our current limit; extend it.
826 if ((start_pfn + pfn_cnt) > has->end_pfn) {
827 residual = (start_pfn + pfn_cnt - has->end_pfn);
829 * Extend the region by multiples of HA_CHUNK.
831 new_inc = (residual / HA_CHUNK) * HA_CHUNK;
832 if (residual % HA_CHUNK)
835 has->end_pfn += new_inc;
841 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
846 static unsigned long handle_pg_range(unsigned long pg_start,
847 unsigned long pg_count)
849 unsigned long start_pfn = pg_start;
850 unsigned long pfn_cnt = pg_count;
852 struct hv_hotadd_state *has;
853 unsigned long pgs_ol = 0;
854 unsigned long old_covered_state;
855 unsigned long res = 0, flags;
857 pr_debug("Hot adding %lu pages starting at pfn 0x%lx.\n", pg_count,
860 spin_lock_irqsave(&dm_device.ha_lock, flags);
861 list_for_each_entry(has, &dm_device.ha_region_list, list) {
863 * If the pfn range we are dealing with is not in the current
864 * "hot add block", move on.
866 if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
869 old_covered_state = has->covered_end_pfn;
871 if (start_pfn < has->ha_end_pfn) {
873 * This is the case where we are backing pages
874 * in an already hot added region. Bring
875 * these pages online first.
877 pgs_ol = has->ha_end_pfn - start_pfn;
878 if (pgs_ol > pfn_cnt)
881 has->covered_end_pfn += pgs_ol;
884 * Check if the corresponding memory block is already
885 * online. It is possible to observe struct pages still
886 * being uninitialized here so check section instead.
887 * In case the section is online we need to bring the
888 * rest of pfns (which were not backed previously)
891 if (start_pfn > has->start_pfn &&
892 online_section_nr(pfn_to_section_nr(start_pfn)))
893 hv_bring_pgs_online(has, start_pfn, pgs_ol);
897 if ((has->ha_end_pfn < has->end_pfn) && (pfn_cnt > 0)) {
899 * We have some residual hot add range
900 * that needs to be hot added; hot add
901 * it now. Hot add a multiple of
902 * of HA_CHUNK that fully covers the pages
905 size = (has->end_pfn - has->ha_end_pfn);
906 if (pfn_cnt <= size) {
907 size = ((pfn_cnt / HA_CHUNK) * HA_CHUNK);
908 if (pfn_cnt % HA_CHUNK)
913 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
914 hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
915 spin_lock_irqsave(&dm_device.ha_lock, flags);
918 * If we managed to online any pages that were given to us,
919 * we declare success.
921 res = has->covered_end_pfn - old_covered_state;
924 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
929 static unsigned long process_hot_add(unsigned long pg_start,
930 unsigned long pfn_cnt,
931 unsigned long rg_start,
932 unsigned long rg_size)
934 struct hv_hotadd_state *ha_region = NULL;
941 if (!dm_device.host_specified_ha_region) {
942 covered = pfn_covered(pg_start, pfn_cnt);
951 * If the host has specified a hot-add range; deal with it first.
955 ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
959 INIT_LIST_HEAD(&ha_region->list);
960 INIT_LIST_HEAD(&ha_region->gap_list);
962 ha_region->start_pfn = rg_start;
963 ha_region->ha_end_pfn = rg_start;
964 ha_region->covered_start_pfn = pg_start;
965 ha_region->covered_end_pfn = pg_start;
966 ha_region->end_pfn = rg_start + rg_size;
968 spin_lock_irqsave(&dm_device.ha_lock, flags);
969 list_add_tail(&ha_region->list, &dm_device.ha_region_list);
970 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
975 * Process the page range specified; bringing them
976 * online if possible.
978 return handle_pg_range(pg_start, pfn_cnt);
983 static void hot_add_req(struct work_struct *dummy)
985 struct dm_hot_add_response resp;
986 #ifdef CONFIG_MEMORY_HOTPLUG
987 unsigned long pg_start, pfn_cnt;
988 unsigned long rg_start, rg_sz;
990 struct hv_dynmem_device *dm = &dm_device;
992 memset(&resp, 0, sizeof(struct dm_hot_add_response));
993 resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
994 resp.hdr.size = sizeof(struct dm_hot_add_response);
996 #ifdef CONFIG_MEMORY_HOTPLUG
997 pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
998 pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
1000 rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
1001 rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
1003 if ((rg_start == 0) && (!dm->host_specified_ha_region)) {
1004 unsigned long region_size;
1005 unsigned long region_start;
1008 * The host has not specified the hot-add region.
1009 * Based on the hot-add page range being specified,
1010 * compute a hot-add region that can cover the pages
1011 * that need to be hot-added while ensuring the alignment
1012 * and size requirements of Linux as it relates to hot-add.
1014 region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK;
1015 if (pfn_cnt % HA_CHUNK)
1016 region_size += HA_CHUNK;
1018 region_start = (pg_start / HA_CHUNK) * HA_CHUNK;
1020 rg_start = region_start;
1021 rg_sz = region_size;
1025 resp.page_count = process_hot_add(pg_start, pfn_cnt,
1028 dm->num_pages_added += resp.page_count;
1031 * The result field of the response structure has the
1032 * following semantics:
1034 * 1. If all or some pages hot-added: Guest should return success.
1036 * 2. If no pages could be hot-added:
1038 * If the guest returns success, then the host
1039 * will not attempt any further hot-add operations. This
1040 * signifies a permanent failure.
1042 * If the guest returns failure, then this failure will be
1043 * treated as a transient failure and the host may retry the
1044 * hot-add operation after some delay.
1046 if (resp.page_count > 0)
1048 else if (!do_hot_add)
1053 if (!do_hot_add || resp.page_count == 0) {
1054 if (!allow_hibernation)
1055 pr_err("Memory hot add failed\n");
1057 pr_info("Ignore hot-add request!\n");
1060 dm->state = DM_INITIALIZED;
1061 resp.hdr.trans_id = atomic_inc_return(&trans_id);
1062 vmbus_sendpacket(dm->dev->channel, &resp,
1063 sizeof(struct dm_hot_add_response),
1064 (unsigned long)NULL,
1065 VM_PKT_DATA_INBAND, 0);
1068 static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
1070 struct dm_info_header *info_hdr;
1072 info_hdr = (struct dm_info_header *)msg->info;
1074 switch (info_hdr->type) {
1075 case INFO_TYPE_MAX_PAGE_CNT:
1076 if (info_hdr->data_size == sizeof(__u64)) {
1077 __u64 *max_page_count = (__u64 *)&info_hdr[1];
1079 pr_info("Max. dynamic memory size: %llu MB\n",
1080 (*max_page_count) >> (20 - HV_HYP_PAGE_SHIFT));
1085 pr_warn("Received Unknown type: %d\n", info_hdr->type);
1089 static unsigned long compute_balloon_floor(void)
1091 unsigned long min_pages;
1092 unsigned long nr_pages = totalram_pages();
1093 #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
1094 /* Simple continuous piecewiese linear function:
1095 * max MiB -> min MiB gradient
1105 if (nr_pages < MB2PAGES(128))
1106 min_pages = MB2PAGES(8) + (nr_pages >> 1);
1107 else if (nr_pages < MB2PAGES(512))
1108 min_pages = MB2PAGES(40) + (nr_pages >> 2);
1109 else if (nr_pages < MB2PAGES(2048))
1110 min_pages = MB2PAGES(104) + (nr_pages >> 3);
1111 else if (nr_pages < MB2PAGES(8192))
1112 min_pages = MB2PAGES(232) + (nr_pages >> 4);
1114 min_pages = MB2PAGES(488) + (nr_pages >> 5);
1120 * Post our status as it relates memory pressure to the
1121 * host. Host expects the guests to post this status
1122 * periodically at 1 second intervals.
1124 * The metrics specified in this protocol are very Windows
1125 * specific and so we cook up numbers here to convey our memory
1129 static void post_status(struct hv_dynmem_device *dm)
1131 struct dm_status status;
1132 unsigned long now = jiffies;
1133 unsigned long last_post = last_post_time;
1134 unsigned long num_pages_avail, num_pages_committed;
1136 if (pressure_report_delay > 0) {
1137 --pressure_report_delay;
1141 if (!time_after(now, (last_post_time + HZ)))
1144 memset(&status, 0, sizeof(struct dm_status));
1145 status.hdr.type = DM_STATUS_REPORT;
1146 status.hdr.size = sizeof(struct dm_status);
1147 status.hdr.trans_id = atomic_inc_return(&trans_id);
1150 * The host expects the guest to report free and committed memory.
1151 * Furthermore, the host expects the pressure information to include
1152 * the ballooned out pages. For a given amount of memory that we are
1153 * managing we need to compute a floor below which we should not
1154 * balloon. Compute this and add it to the pressure report.
1155 * We also need to report all offline pages (num_pages_added -
1156 * num_pages_onlined) as committed to the host, otherwise it can try
1157 * asking us to balloon them out.
1159 num_pages_avail = si_mem_available();
1160 num_pages_committed = vm_memory_committed() +
1161 dm->num_pages_ballooned +
1162 (dm->num_pages_added > dm->num_pages_onlined ?
1163 dm->num_pages_added - dm->num_pages_onlined : 0) +
1164 compute_balloon_floor();
1166 trace_balloon_status(num_pages_avail, num_pages_committed,
1167 vm_memory_committed(), dm->num_pages_ballooned,
1168 dm->num_pages_added, dm->num_pages_onlined);
1170 /* Convert numbers of pages into numbers of HV_HYP_PAGEs. */
1171 status.num_avail = num_pages_avail * NR_HV_HYP_PAGES_IN_PAGE;
1172 status.num_committed = num_pages_committed * NR_HV_HYP_PAGES_IN_PAGE;
1175 * If our transaction ID is no longer current, just don't
1176 * send the status. This can happen if we were interrupted
1177 * after we picked our transaction ID.
1179 if (status.hdr.trans_id != atomic_read(&trans_id))
1183 * If the last post time that we sampled has changed,
1184 * we have raced, don't post the status.
1186 if (last_post != last_post_time)
1189 last_post_time = jiffies;
1190 vmbus_sendpacket(dm->dev->channel, &status,
1191 sizeof(struct dm_status),
1192 (unsigned long)NULL,
1193 VM_PKT_DATA_INBAND, 0);
1197 static void free_balloon_pages(struct hv_dynmem_device *dm,
1198 union dm_mem_page_range *range_array)
1200 int num_pages = range_array->finfo.page_cnt;
1201 __u64 start_frame = range_array->finfo.start_page;
1205 for (i = 0; i < num_pages; i++) {
1206 pg = pfn_to_page(i + start_frame);
1207 __ClearPageOffline(pg);
1209 dm->num_pages_ballooned--;
1210 adjust_managed_page_count(pg, 1);
1216 static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
1217 unsigned int num_pages,
1218 struct dm_balloon_response *bl_resp,
1224 for (i = 0; i < num_pages / alloc_unit; i++) {
1225 if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
1227 return i * alloc_unit;
1230 * We execute this code in a thread context. Furthermore,
1231 * we don't want the kernel to try too hard.
1233 pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
1234 __GFP_NOMEMALLOC | __GFP_NOWARN,
1235 get_order(alloc_unit << PAGE_SHIFT));
1238 return i * alloc_unit;
1240 dm->num_pages_ballooned += alloc_unit;
1243 * If we allocatted 2M pages; split them so we
1244 * can free them in any order we get.
1247 if (alloc_unit != 1)
1248 split_page(pg, get_order(alloc_unit << PAGE_SHIFT));
1250 /* mark all pages offline */
1251 for (j = 0; j < alloc_unit; j++) {
1252 __SetPageOffline(pg + j);
1253 adjust_managed_page_count(pg + j, -1);
1256 bl_resp->range_count++;
1257 bl_resp->range_array[i].finfo.start_page =
1259 bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
1260 bl_resp->hdr.size += sizeof(union dm_mem_page_range);
1264 return i * alloc_unit;
1267 static void balloon_up(struct work_struct *dummy)
1269 unsigned int num_pages = dm_device.balloon_wrk.num_pages;
1270 unsigned int num_ballooned = 0;
1271 struct dm_balloon_response *bl_resp;
1277 unsigned long floor;
1280 * We will attempt 2M allocations. However, if we fail to
1281 * allocate 2M chunks, we will go back to PAGE_SIZE allocations.
1283 alloc_unit = PAGES_IN_2M;
1285 avail_pages = si_mem_available();
1286 floor = compute_balloon_floor();
1288 /* Refuse to balloon below the floor. */
1289 if (avail_pages < num_pages || avail_pages - num_pages < floor) {
1290 pr_info("Balloon request will be partially fulfilled. %s\n",
1291 avail_pages < num_pages ? "Not enough memory." :
1292 "Balloon floor reached.");
1294 num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
1298 memset(balloon_up_send_buffer, 0, HV_HYP_PAGE_SIZE);
1299 bl_resp = (struct dm_balloon_response *)balloon_up_send_buffer;
1300 bl_resp->hdr.type = DM_BALLOON_RESPONSE;
1301 bl_resp->hdr.size = sizeof(struct dm_balloon_response);
1302 bl_resp->more_pages = 1;
1304 num_pages -= num_ballooned;
1305 num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
1306 bl_resp, alloc_unit);
1308 if (alloc_unit != 1 && num_ballooned == 0) {
1313 if (num_ballooned == 0 || num_ballooned == num_pages) {
1314 pr_debug("Ballooned %u out of %u requested pages.\n",
1315 num_pages, dm_device.balloon_wrk.num_pages);
1317 bl_resp->more_pages = 0;
1319 dm_device.state = DM_INITIALIZED;
1323 * We are pushing a lot of data through the channel;
1324 * deal with transient failures caused because of the
1325 * lack of space in the ring buffer.
1329 bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
1330 ret = vmbus_sendpacket(dm_device.dev->channel,
1333 (unsigned long)NULL,
1334 VM_PKT_DATA_INBAND, 0);
1338 post_status(&dm_device);
1339 } while (ret == -EAGAIN);
1343 * Free up the memory we allocatted.
1345 pr_err("Balloon response failed\n");
1347 for (i = 0; i < bl_resp->range_count; i++)
1348 free_balloon_pages(&dm_device,
1349 &bl_resp->range_array[i]);
1357 static void balloon_down(struct hv_dynmem_device *dm,
1358 struct dm_unballoon_request *req)
1360 union dm_mem_page_range *range_array = req->range_array;
1361 int range_count = req->range_count;
1362 struct dm_unballoon_response resp;
1364 unsigned int prev_pages_ballooned = dm->num_pages_ballooned;
1366 for (i = 0; i < range_count; i++) {
1367 free_balloon_pages(dm, &range_array[i]);
1368 complete(&dm_device.config_event);
1371 pr_debug("Freed %u ballooned pages.\n",
1372 prev_pages_ballooned - dm->num_pages_ballooned);
1374 if (req->more_pages == 1)
1377 memset(&resp, 0, sizeof(struct dm_unballoon_response));
1378 resp.hdr.type = DM_UNBALLOON_RESPONSE;
1379 resp.hdr.trans_id = atomic_inc_return(&trans_id);
1380 resp.hdr.size = sizeof(struct dm_unballoon_response);
1382 vmbus_sendpacket(dm_device.dev->channel, &resp,
1383 sizeof(struct dm_unballoon_response),
1384 (unsigned long)NULL,
1385 VM_PKT_DATA_INBAND, 0);
1387 dm->state = DM_INITIALIZED;
1390 static void balloon_onchannelcallback(void *context);
1392 static int dm_thread_func(void *dm_dev)
1394 struct hv_dynmem_device *dm = dm_dev;
1396 while (!kthread_should_stop()) {
1397 wait_for_completion_interruptible_timeout(
1398 &dm_device.config_event, 1*HZ);
1400 * The host expects us to post information on the memory
1401 * pressure every second.
1403 reinit_completion(&dm_device.config_event);
1411 static void version_resp(struct hv_dynmem_device *dm,
1412 struct dm_version_response *vresp)
1414 struct dm_version_request version_req;
1417 if (vresp->is_accepted) {
1419 * We are done; wakeup the
1420 * context waiting for version
1423 complete(&dm->host_event);
1427 * If there are more versions to try, continue
1428 * with negotiations; if not
1429 * shutdown the service since we are not able
1430 * to negotiate a suitable version number
1433 if (dm->next_version == 0)
1436 memset(&version_req, 0, sizeof(struct dm_version_request));
1437 version_req.hdr.type = DM_VERSION_REQUEST;
1438 version_req.hdr.size = sizeof(struct dm_version_request);
1439 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
1440 version_req.version.version = dm->next_version;
1441 dm->version = version_req.version.version;
1444 * Set the next version to try in case current version fails.
1445 * Win7 protocol ought to be the last one to try.
1447 switch (version_req.version.version) {
1448 case DYNMEM_PROTOCOL_VERSION_WIN8:
1449 dm->next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
1450 version_req.is_last_attempt = 0;
1453 dm->next_version = 0;
1454 version_req.is_last_attempt = 1;
1457 ret = vmbus_sendpacket(dm->dev->channel, &version_req,
1458 sizeof(struct dm_version_request),
1459 (unsigned long)NULL,
1460 VM_PKT_DATA_INBAND, 0);
1468 dm->state = DM_INIT_ERROR;
1469 complete(&dm->host_event);
1472 static void cap_resp(struct hv_dynmem_device *dm,
1473 struct dm_capabilities_resp_msg *cap_resp)
1475 if (!cap_resp->is_accepted) {
1476 pr_err("Capabilities not accepted by host\n");
1477 dm->state = DM_INIT_ERROR;
1479 complete(&dm->host_event);
1482 static void balloon_onchannelcallback(void *context)
1484 struct hv_device *dev = context;
1487 struct dm_message *dm_msg;
1488 struct dm_header *dm_hdr;
1489 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
1490 struct dm_balloon *bal_msg;
1491 struct dm_hot_add *ha_msg;
1492 union dm_mem_page_range *ha_pg_range;
1493 union dm_mem_page_range *ha_region;
1495 memset(recv_buffer, 0, sizeof(recv_buffer));
1496 vmbus_recvpacket(dev->channel, recv_buffer,
1497 HV_HYP_PAGE_SIZE, &recvlen, &requestid);
1500 dm_msg = (struct dm_message *)recv_buffer;
1501 dm_hdr = &dm_msg->hdr;
1503 switch (dm_hdr->type) {
1504 case DM_VERSION_RESPONSE:
1506 (struct dm_version_response *)dm_msg);
1509 case DM_CAPABILITIES_RESPONSE:
1511 (struct dm_capabilities_resp_msg *)dm_msg);
1514 case DM_BALLOON_REQUEST:
1515 if (allow_hibernation) {
1516 pr_info("Ignore balloon-up request!\n");
1520 if (dm->state == DM_BALLOON_UP)
1521 pr_warn("Currently ballooning\n");
1522 bal_msg = (struct dm_balloon *)recv_buffer;
1523 dm->state = DM_BALLOON_UP;
1524 dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
1525 schedule_work(&dm_device.balloon_wrk.wrk);
1528 case DM_UNBALLOON_REQUEST:
1529 if (allow_hibernation) {
1530 pr_info("Ignore balloon-down request!\n");
1534 dm->state = DM_BALLOON_DOWN;
1536 (struct dm_unballoon_request *)recv_buffer);
1539 case DM_MEM_HOT_ADD_REQUEST:
1540 if (dm->state == DM_HOT_ADD)
1541 pr_warn("Currently hot-adding\n");
1542 dm->state = DM_HOT_ADD;
1543 ha_msg = (struct dm_hot_add *)recv_buffer;
1544 if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
1546 * This is a normal hot-add request specifying
1549 dm->host_specified_ha_region = false;
1550 ha_pg_range = &ha_msg->range;
1551 dm->ha_wrk.ha_page_range = *ha_pg_range;
1552 dm->ha_wrk.ha_region_range.page_range = 0;
1555 * Host is specifying that we first hot-add
1556 * a region and then partially populate this
1559 dm->host_specified_ha_region = true;
1560 ha_pg_range = &ha_msg->range;
1561 ha_region = &ha_pg_range[1];
1562 dm->ha_wrk.ha_page_range = *ha_pg_range;
1563 dm->ha_wrk.ha_region_range = *ha_region;
1565 schedule_work(&dm_device.ha_wrk.wrk);
1568 case DM_INFO_MESSAGE:
1569 process_info(dm, (struct dm_info_msg *)dm_msg);
1573 pr_warn_ratelimited("Unhandled message: type: %d\n", dm_hdr->type);
1580 /* Hyper-V only supports reporting 2MB pages or higher */
1581 #define HV_MIN_PAGE_REPORTING_ORDER 9
1582 #define HV_MIN_PAGE_REPORTING_LEN (HV_HYP_PAGE_SIZE << HV_MIN_PAGE_REPORTING_ORDER)
1583 static int hv_free_page_report(struct page_reporting_dev_info *pr_dev_info,
1584 struct scatterlist *sgl, unsigned int nents)
1586 unsigned long flags;
1587 struct hv_memory_hint *hint;
1590 struct scatterlist *sg;
1592 WARN_ON_ONCE(nents > HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES);
1593 WARN_ON_ONCE(sgl->length < HV_MIN_PAGE_REPORTING_LEN);
1594 local_irq_save(flags);
1595 hint = *(struct hv_memory_hint **)this_cpu_ptr(hyperv_pcpu_input_arg);
1597 local_irq_restore(flags);
1601 hint->type = HV_EXT_MEMORY_HEAT_HINT_TYPE_COLD_DISCARD;
1603 for_each_sg(sgl, sg, nents, i) {
1604 union hv_gpa_page_range *range;
1606 range = &hint->ranges[i];
1607 range->address_space = 0;
1608 /* page reporting only reports 2MB pages or higher */
1609 range->page.largepage = 1;
1610 range->page.additional_pages =
1611 (sg->length / HV_MIN_PAGE_REPORTING_LEN) - 1;
1612 range->page_size = HV_GPA_PAGE_RANGE_PAGE_SIZE_2MB;
1613 range->base_large_pfn =
1614 page_to_hvpfn(sg_page(sg)) >> HV_MIN_PAGE_REPORTING_ORDER;
1617 status = hv_do_rep_hypercall(HV_EXT_CALL_MEMORY_HEAT_HINT, nents, 0,
1619 local_irq_restore(flags);
1620 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) {
1621 pr_err("Cold memory discard hypercall failed with status %llx\n",
1629 static void enable_page_reporting(void)
1633 /* Essentially, validating 'PAGE_REPORTING_MIN_ORDER' is big enough. */
1634 if (pageblock_order < HV_MIN_PAGE_REPORTING_ORDER) {
1635 pr_debug("Cold memory discard is only supported on 2MB pages and above\n");
1639 if (!hv_query_ext_cap(HV_EXT_CAPABILITY_MEMORY_COLD_DISCARD_HINT)) {
1640 pr_debug("Cold memory discard hint not supported by Hyper-V\n");
1644 BUILD_BUG_ON(PAGE_REPORTING_CAPACITY > HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES);
1645 dm_device.pr_dev_info.report = hv_free_page_report;
1646 ret = page_reporting_register(&dm_device.pr_dev_info);
1648 dm_device.pr_dev_info.report = NULL;
1649 pr_err("Failed to enable cold memory discard: %d\n", ret);
1651 pr_info("Cold memory discard hint enabled\n");
1655 static void disable_page_reporting(void)
1657 if (dm_device.pr_dev_info.report) {
1658 page_reporting_unregister(&dm_device.pr_dev_info);
1659 dm_device.pr_dev_info.report = NULL;
1663 static int ballooning_enabled(void)
1666 * Disable ballooning if the page size is not 4k (HV_HYP_PAGE_SIZE),
1667 * since currently it's unclear to us whether an unballoon request can
1668 * make sure all page ranges are guest page size aligned.
1670 if (PAGE_SIZE != HV_HYP_PAGE_SIZE) {
1671 pr_info("Ballooning disabled because page size is not 4096 bytes\n");
1678 static int hot_add_enabled(void)
1681 * Disable hot add on ARM64, because we currently rely on
1682 * memory_add_physaddr_to_nid() to get a node id of a hot add range,
1683 * however ARM64's memory_add_physaddr_to_nid() always return 0 and
1684 * DM_MEM_HOT_ADD_REQUEST doesn't have the NUMA node information for
1687 if (IS_ENABLED(CONFIG_ARM64)) {
1688 pr_info("Memory hot add disabled on ARM64\n");
1695 static int balloon_connect_vsp(struct hv_device *dev)
1697 struct dm_version_request version_req;
1698 struct dm_capabilities cap_msg;
1703 * max_pkt_size should be large enough for one vmbus packet header plus
1704 * our receive buffer size. Hyper-V sends messages up to
1705 * HV_HYP_PAGE_SIZE bytes long on balloon channel.
1707 dev->channel->max_pkt_size = HV_HYP_PAGE_SIZE * 2;
1709 ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
1710 balloon_onchannelcallback, dev);
1715 * Initiate the hand shake with the host and negotiate
1716 * a version that the host can support. We start with the
1717 * highest version number and go down if the host cannot
1720 memset(&version_req, 0, sizeof(struct dm_version_request));
1721 version_req.hdr.type = DM_VERSION_REQUEST;
1722 version_req.hdr.size = sizeof(struct dm_version_request);
1723 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
1724 version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN10;
1725 version_req.is_last_attempt = 0;
1726 dm_device.version = version_req.version.version;
1728 ret = vmbus_sendpacket(dev->channel, &version_req,
1729 sizeof(struct dm_version_request),
1730 (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
1734 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1741 * If we could not negotiate a compatible version with the host
1742 * fail the probe function.
1744 if (dm_device.state == DM_INIT_ERROR) {
1749 pr_info("Using Dynamic Memory protocol version %u.%u\n",
1750 DYNMEM_MAJOR_VERSION(dm_device.version),
1751 DYNMEM_MINOR_VERSION(dm_device.version));
1754 * Now submit our capabilities to the host.
1756 memset(&cap_msg, 0, sizeof(struct dm_capabilities));
1757 cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
1758 cap_msg.hdr.size = sizeof(struct dm_capabilities);
1759 cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
1762 * When hibernation (i.e. virtual ACPI S4 state) is enabled, the host
1763 * currently still requires the bits to be set, so we have to add code
1764 * to fail the host's hot-add and balloon up/down requests, if any.
1766 cap_msg.caps.cap_bits.balloon = ballooning_enabled();
1767 cap_msg.caps.cap_bits.hot_add = hot_add_enabled();
1770 * Specify our alignment requirements as it relates
1771 * memory hot-add. Specify 128MB alignment.
1773 cap_msg.caps.cap_bits.hot_add_alignment = 7;
1776 * Currently the host does not use these
1777 * values and we set them to what is done in the
1780 cap_msg.min_page_cnt = 0;
1781 cap_msg.max_page_number = -1;
1783 ret = vmbus_sendpacket(dev->channel, &cap_msg,
1784 sizeof(struct dm_capabilities),
1785 (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
1789 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1796 * If the host does not like our capabilities,
1797 * fail the probe function.
1799 if (dm_device.state == DM_INIT_ERROR) {
1806 vmbus_close(dev->channel);
1810 static int balloon_probe(struct hv_device *dev,
1811 const struct hv_vmbus_device_id *dev_id)
1815 allow_hibernation = hv_is_hibernation_supported();
1816 if (allow_hibernation)
1819 #ifdef CONFIG_MEMORY_HOTPLUG
1820 do_hot_add = hot_add;
1824 dm_device.dev = dev;
1825 dm_device.state = DM_INITIALIZING;
1826 dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN8;
1827 init_completion(&dm_device.host_event);
1828 init_completion(&dm_device.config_event);
1829 INIT_LIST_HEAD(&dm_device.ha_region_list);
1830 spin_lock_init(&dm_device.ha_lock);
1831 INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
1832 INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
1833 dm_device.host_specified_ha_region = false;
1835 #ifdef CONFIG_MEMORY_HOTPLUG
1836 set_online_page_callback(&hv_online_page);
1837 init_completion(&dm_device.ol_waitevent);
1838 register_memory_notifier(&hv_memory_nb);
1841 hv_set_drvdata(dev, &dm_device);
1843 ret = balloon_connect_vsp(dev);
1847 enable_page_reporting();
1848 dm_device.state = DM_INITIALIZED;
1851 kthread_run(dm_thread_func, &dm_device, "hv_balloon");
1852 if (IS_ERR(dm_device.thread)) {
1853 ret = PTR_ERR(dm_device.thread);
1860 dm_device.state = DM_INIT_ERROR;
1861 dm_device.thread = NULL;
1862 disable_page_reporting();
1863 vmbus_close(dev->channel);
1865 #ifdef CONFIG_MEMORY_HOTPLUG
1866 unregister_memory_notifier(&hv_memory_nb);
1867 restore_online_page_callback(&hv_online_page);
1872 static int balloon_remove(struct hv_device *dev)
1874 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
1875 struct hv_hotadd_state *has, *tmp;
1876 struct hv_hotadd_gap *gap, *tmp_gap;
1877 unsigned long flags;
1879 if (dm->num_pages_ballooned != 0)
1880 pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
1882 cancel_work_sync(&dm->balloon_wrk.wrk);
1883 cancel_work_sync(&dm->ha_wrk.wrk);
1885 kthread_stop(dm->thread);
1888 * This is to handle the case when balloon_resume()
1889 * call has failed and some cleanup has been done as
1890 * a part of the error handling.
1892 if (dm_device.state != DM_INIT_ERROR) {
1893 disable_page_reporting();
1894 vmbus_close(dev->channel);
1895 #ifdef CONFIG_MEMORY_HOTPLUG
1896 unregister_memory_notifier(&hv_memory_nb);
1897 restore_online_page_callback(&hv_online_page);
1901 spin_lock_irqsave(&dm_device.ha_lock, flags);
1902 list_for_each_entry_safe(has, tmp, &dm->ha_region_list, list) {
1903 list_for_each_entry_safe(gap, tmp_gap, &has->gap_list, list) {
1904 list_del(&gap->list);
1907 list_del(&has->list);
1910 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
1915 static int balloon_suspend(struct hv_device *hv_dev)
1917 struct hv_dynmem_device *dm = hv_get_drvdata(hv_dev);
1919 tasklet_disable(&hv_dev->channel->callback_event);
1921 cancel_work_sync(&dm->balloon_wrk.wrk);
1922 cancel_work_sync(&dm->ha_wrk.wrk);
1925 kthread_stop(dm->thread);
1927 vmbus_close(hv_dev->channel);
1930 tasklet_enable(&hv_dev->channel->callback_event);
1936 static int balloon_resume(struct hv_device *dev)
1940 dm_device.state = DM_INITIALIZING;
1942 ret = balloon_connect_vsp(dev);
1948 kthread_run(dm_thread_func, &dm_device, "hv_balloon");
1949 if (IS_ERR(dm_device.thread)) {
1950 ret = PTR_ERR(dm_device.thread);
1951 dm_device.thread = NULL;
1955 dm_device.state = DM_INITIALIZED;
1958 vmbus_close(dev->channel);
1960 dm_device.state = DM_INIT_ERROR;
1961 disable_page_reporting();
1962 #ifdef CONFIG_MEMORY_HOTPLUG
1963 unregister_memory_notifier(&hv_memory_nb);
1964 restore_online_page_callback(&hv_online_page);
1969 static const struct hv_vmbus_device_id id_table[] = {
1970 /* Dynamic Memory Class ID */
1971 /* 525074DC-8985-46e2-8057-A307DC18A502 */
1976 MODULE_DEVICE_TABLE(vmbus, id_table);
1978 static struct hv_driver balloon_drv = {
1979 .name = "hv_balloon",
1980 .id_table = id_table,
1981 .probe = balloon_probe,
1982 .remove = balloon_remove,
1983 .suspend = balloon_suspend,
1984 .resume = balloon_resume,
1986 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1990 static int __init init_balloon_drv(void)
1993 return vmbus_driver_register(&balloon_drv);
1996 module_init(init_balloon_drv);
1998 MODULE_DESCRIPTION("Hyper-V Balloon");
1999 MODULE_LICENSE("GPL");