xref: /linux/drivers/hv/hv_balloon.c (revision b8321ed4a40c02054f930ca59d3570caa27bc86c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012, Microsoft Corporation.
4  *
5  * Author:
6  *   K. Y. Srinivasan <kys@microsoft.com>
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
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/memory_hotplug.h>
21 #include <linux/memory.h>
22 #include <linux/notifier.h>
23 #include <linux/percpu_counter.h>
24 #include <linux/page_reporting.h>
25 
26 #include <linux/hyperv.h>
27 #include <asm/hyperv-tlfs.h>
28 
29 #include <asm/mshyperv.h>
30 
31 #define CREATE_TRACE_POINTS
32 #include "hv_trace_balloon.h"
33 
34 /*
35  * We begin with definitions supporting the Dynamic Memory protocol
36  * with the host.
37  *
38  * Begin protocol definitions.
39  */
40 
41 
42 
43 /*
44  * Protocol versions. The low word is the minor version, the high word the major
45  * version.
46  *
47  * History:
48  * Initial version 1.0
49  * Changed to 0.1 on 2009/03/25
50  * Changes to 0.2 on 2009/05/14
51  * Changes to 0.3 on 2009/12/03
52  * Changed to 1.0 on 2011/04/05
53  */
54 
55 #define DYNMEM_MAKE_VERSION(Major, Minor) ((__u32)(((Major) << 16) | (Minor)))
56 #define DYNMEM_MAJOR_VERSION(Version) ((__u32)(Version) >> 16)
57 #define DYNMEM_MINOR_VERSION(Version) ((__u32)(Version) & 0xff)
58 
59 enum {
60 	DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
61 	DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
62 	DYNMEM_PROTOCOL_VERSION_3 = DYNMEM_MAKE_VERSION(2, 0),
63 
64 	DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
65 	DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
66 	DYNMEM_PROTOCOL_VERSION_WIN10 = DYNMEM_PROTOCOL_VERSION_3,
67 
68 	DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN10
69 };
70 
71 
72 
73 /*
74  * Message Types
75  */
76 
77 enum dm_message_type {
78 	/*
79 	 * Version 0.3
80 	 */
81 	DM_ERROR			= 0,
82 	DM_VERSION_REQUEST		= 1,
83 	DM_VERSION_RESPONSE		= 2,
84 	DM_CAPABILITIES_REPORT		= 3,
85 	DM_CAPABILITIES_RESPONSE	= 4,
86 	DM_STATUS_REPORT		= 5,
87 	DM_BALLOON_REQUEST		= 6,
88 	DM_BALLOON_RESPONSE		= 7,
89 	DM_UNBALLOON_REQUEST		= 8,
90 	DM_UNBALLOON_RESPONSE		= 9,
91 	DM_MEM_HOT_ADD_REQUEST		= 10,
92 	DM_MEM_HOT_ADD_RESPONSE		= 11,
93 	DM_VERSION_03_MAX		= 11,
94 	/*
95 	 * Version 1.0.
96 	 */
97 	DM_INFO_MESSAGE			= 12,
98 	DM_VERSION_1_MAX		= 12
99 };
100 
101 
102 /*
103  * Structures defining the dynamic memory management
104  * protocol.
105  */
106 
107 union dm_version {
108 	struct {
109 		__u16 minor_version;
110 		__u16 major_version;
111 	};
112 	__u32 version;
113 } __packed;
114 
115 
116 union dm_caps {
117 	struct {
118 		__u64 balloon:1;
119 		__u64 hot_add:1;
120 		/*
121 		 * To support guests that may have alignment
122 		 * limitations on hot-add, the guest can specify
123 		 * its alignment requirements; a value of n
124 		 * represents an alignment of 2^n in mega bytes.
125 		 */
126 		__u64 hot_add_alignment:4;
127 		__u64 reservedz:58;
128 	} cap_bits;
129 	__u64 caps;
130 } __packed;
131 
132 union dm_mem_page_range {
133 	struct  {
134 		/*
135 		 * The PFN number of the first page in the range.
136 		 * 40 bits is the architectural limit of a PFN
137 		 * number for AMD64.
138 		 */
139 		__u64 start_page:40;
140 		/*
141 		 * The number of pages in the range.
142 		 */
143 		__u64 page_cnt:24;
144 	} finfo;
145 	__u64  page_range;
146 } __packed;
147 
148 
149 
150 /*
151  * The header for all dynamic memory messages:
152  *
153  * type: Type of the message.
154  * size: Size of the message in bytes; including the header.
155  * trans_id: The guest is responsible for manufacturing this ID.
156  */
157 
158 struct dm_header {
159 	__u16 type;
160 	__u16 size;
161 	__u32 trans_id;
162 } __packed;
163 
164 /*
165  * A generic message format for dynamic memory.
166  * Specific message formats are defined later in the file.
167  */
168 
169 struct dm_message {
170 	struct dm_header hdr;
171 	__u8 data[]; /* enclosed message */
172 } __packed;
173 
174 
175 /*
176  * Specific message types supporting the dynamic memory protocol.
177  */
178 
179 /*
180  * Version negotiation message. Sent from the guest to the host.
181  * The guest is free to try different versions until the host
182  * accepts the version.
183  *
184  * dm_version: The protocol version requested.
185  * is_last_attempt: If TRUE, this is the last version guest will request.
186  * reservedz: Reserved field, set to zero.
187  */
188 
189 struct dm_version_request {
190 	struct dm_header hdr;
191 	union dm_version version;
192 	__u32 is_last_attempt:1;
193 	__u32 reservedz:31;
194 } __packed;
195 
196 /*
197  * Version response message; Host to Guest and indicates
198  * if the host has accepted the version sent by the guest.
199  *
200  * is_accepted: If TRUE, host has accepted the version and the guest
201  * should proceed to the next stage of the protocol. FALSE indicates that
202  * guest should re-try with a different version.
203  *
204  * reservedz: Reserved field, set to zero.
205  */
206 
207 struct dm_version_response {
208 	struct dm_header hdr;
209 	__u64 is_accepted:1;
210 	__u64 reservedz:63;
211 } __packed;
212 
213 /*
214  * Message reporting capabilities. This is sent from the guest to the
215  * host.
216  */
217 
218 struct dm_capabilities {
219 	struct dm_header hdr;
220 	union dm_caps caps;
221 	__u64 min_page_cnt;
222 	__u64 max_page_number;
223 } __packed;
224 
225 /*
226  * Response to the capabilities message. This is sent from the host to the
227  * guest. This message notifies if the host has accepted the guest's
228  * capabilities. If the host has not accepted, the guest must shutdown
229  * the service.
230  *
231  * is_accepted: Indicates if the host has accepted guest's capabilities.
232  * reservedz: Must be 0.
233  */
234 
235 struct dm_capabilities_resp_msg {
236 	struct dm_header hdr;
237 	__u64 is_accepted:1;
238 	__u64 reservedz:63;
239 } __packed;
240 
241 /*
242  * This message is used to report memory pressure from the guest.
243  * This message is not part of any transaction and there is no
244  * response to this message.
245  *
246  * num_avail: Available memory in pages.
247  * num_committed: Committed memory in pages.
248  * page_file_size: The accumulated size of all page files
249  *		   in the system in pages.
250  * zero_free: The nunber of zero and free pages.
251  * page_file_writes: The writes to the page file in pages.
252  * io_diff: An indicator of file cache efficiency or page file activity,
253  *	    calculated as File Cache Page Fault Count - Page Read Count.
254  *	    This value is in pages.
255  *
256  * Some of these metrics are Windows specific and fortunately
257  * the algorithm on the host side that computes the guest memory
258  * pressure only uses num_committed value.
259  */
260 
261 struct dm_status {
262 	struct dm_header hdr;
263 	__u64 num_avail;
264 	__u64 num_committed;
265 	__u64 page_file_size;
266 	__u64 zero_free;
267 	__u32 page_file_writes;
268 	__u32 io_diff;
269 } __packed;
270 
271 
272 /*
273  * Message to ask the guest to allocate memory - balloon up message.
274  * This message is sent from the host to the guest. The guest may not be
275  * able to allocate as much memory as requested.
276  *
277  * num_pages: number of pages to allocate.
278  */
279 
280 struct dm_balloon {
281 	struct dm_header hdr;
282 	__u32 num_pages;
283 	__u32 reservedz;
284 } __packed;
285 
286 
287 /*
288  * Balloon response message; this message is sent from the guest
289  * to the host in response to the balloon message.
290  *
291  * reservedz: Reserved; must be set to zero.
292  * more_pages: If FALSE, this is the last message of the transaction.
293  * if TRUE there will atleast one more message from the guest.
294  *
295  * range_count: The number of ranges in the range array.
296  *
297  * range_array: An array of page ranges returned to the host.
298  *
299  */
300 
301 struct dm_balloon_response {
302 	struct dm_header hdr;
303 	__u32 reservedz;
304 	__u32 more_pages:1;
305 	__u32 range_count:31;
306 	union dm_mem_page_range range_array[];
307 } __packed;
308 
309 /*
310  * Un-balloon message; this message is sent from the host
311  * to the guest to give guest more memory.
312  *
313  * more_pages: If FALSE, this is the last message of the transaction.
314  * if TRUE there will atleast one more message from the guest.
315  *
316  * reservedz: Reserved; must be set to zero.
317  *
318  * range_count: The number of ranges in the range array.
319  *
320  * range_array: An array of page ranges returned to the host.
321  *
322  */
323 
324 struct dm_unballoon_request {
325 	struct dm_header hdr;
326 	__u32 more_pages:1;
327 	__u32 reservedz:31;
328 	__u32 range_count;
329 	union dm_mem_page_range range_array[];
330 } __packed;
331 
332 /*
333  * Un-balloon response message; this message is sent from the guest
334  * to the host in response to an unballoon request.
335  *
336  */
337 
338 struct dm_unballoon_response {
339 	struct dm_header hdr;
340 } __packed;
341 
342 
343 /*
344  * Hot add request message. Message sent from the host to the guest.
345  *
346  * mem_range: Memory range to hot add.
347  *
348  */
349 
350 struct dm_hot_add {
351 	struct dm_header hdr;
352 	union dm_mem_page_range range;
353 } __packed;
354 
355 /*
356  * Hot add response message.
357  * This message is sent by the guest to report the status of a hot add request.
358  * If page_count is less than the requested page count, then the host should
359  * assume all further hot add requests will fail, since this indicates that
360  * the guest has hit an upper physical memory barrier.
361  *
362  * Hot adds may also fail due to low resources; in this case, the guest must
363  * not complete this message until the hot add can succeed, and the host must
364  * not send a new hot add request until the response is sent.
365  * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
366  * times it fails the request.
367  *
368  *
369  * page_count: number of pages that were successfully hot added.
370  *
371  * result: result of the operation 1: success, 0: failure.
372  *
373  */
374 
375 struct dm_hot_add_response {
376 	struct dm_header hdr;
377 	__u32 page_count;
378 	__u32 result;
379 } __packed;
380 
381 /*
382  * Types of information sent from host to the guest.
383  */
384 
385 enum dm_info_type {
386 	INFO_TYPE_MAX_PAGE_CNT = 0,
387 	MAX_INFO_TYPE
388 };
389 
390 
391 /*
392  * Header for the information message.
393  */
394 
395 struct dm_info_header {
396 	enum dm_info_type type;
397 	__u32 data_size;
398 } __packed;
399 
400 /*
401  * This message is sent from the host to the guest to pass
402  * some relevant information (win8 addition).
403  *
404  * reserved: no used.
405  * info_size: size of the information blob.
406  * info: information blob.
407  */
408 
409 struct dm_info_msg {
410 	struct dm_header hdr;
411 	__u32 reserved;
412 	__u32 info_size;
413 	__u8  info[];
414 };
415 
416 /*
417  * End protocol definitions.
418  */
419 
420 /*
421  * State to manage hot adding memory into the guest.
422  * The range start_pfn : end_pfn specifies the range
423  * that the host has asked us to hot add. The range
424  * start_pfn : ha_end_pfn specifies the range that we have
425  * currently hot added. We hot add in multiples of 128M
426  * chunks; it is possible that we may not be able to bring
427  * online all the pages in the region. The range
428  * covered_start_pfn:covered_end_pfn defines the pages that can
429  * be brough online.
430  */
431 
432 struct hv_hotadd_state {
433 	struct list_head list;
434 	unsigned long start_pfn;
435 	unsigned long covered_start_pfn;
436 	unsigned long covered_end_pfn;
437 	unsigned long ha_end_pfn;
438 	unsigned long end_pfn;
439 	/*
440 	 * A list of gaps.
441 	 */
442 	struct list_head gap_list;
443 };
444 
445 struct hv_hotadd_gap {
446 	struct list_head list;
447 	unsigned long start_pfn;
448 	unsigned long end_pfn;
449 };
450 
451 struct balloon_state {
452 	__u32 num_pages;
453 	struct work_struct wrk;
454 };
455 
456 struct hot_add_wrk {
457 	union dm_mem_page_range ha_page_range;
458 	union dm_mem_page_range ha_region_range;
459 	struct work_struct wrk;
460 };
461 
462 static bool allow_hibernation;
463 static bool hot_add = true;
464 static bool do_hot_add;
465 /*
466  * Delay reporting memory pressure by
467  * the specified number of seconds.
468  */
469 static uint pressure_report_delay = 45;
470 
471 /*
472  * The last time we posted a pressure report to host.
473  */
474 static unsigned long last_post_time;
475 
476 module_param(hot_add, bool, (S_IRUGO | S_IWUSR));
477 MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
478 
479 module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR));
480 MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
481 static atomic_t trans_id = ATOMIC_INIT(0);
482 
483 static int dm_ring_size = VMBUS_RING_SIZE(16 * 1024);
484 
485 /*
486  * Driver specific state.
487  */
488 
489 enum hv_dm_state {
490 	DM_INITIALIZING = 0,
491 	DM_INITIALIZED,
492 	DM_BALLOON_UP,
493 	DM_BALLOON_DOWN,
494 	DM_HOT_ADD,
495 	DM_INIT_ERROR
496 };
497 
498 
499 static __u8 recv_buffer[HV_HYP_PAGE_SIZE];
500 static __u8 balloon_up_send_buffer[HV_HYP_PAGE_SIZE];
501 #define PAGES_IN_2M (2 * 1024 * 1024 / PAGE_SIZE)
502 #define HA_CHUNK (128 * 1024 * 1024 / PAGE_SIZE)
503 
504 struct hv_dynmem_device {
505 	struct hv_device *dev;
506 	enum hv_dm_state state;
507 	struct completion host_event;
508 	struct completion config_event;
509 
510 	/*
511 	 * Number of pages we have currently ballooned out.
512 	 */
513 	unsigned int num_pages_ballooned;
514 	unsigned int num_pages_onlined;
515 	unsigned int num_pages_added;
516 
517 	/*
518 	 * State to manage the ballooning (up) operation.
519 	 */
520 	struct balloon_state balloon_wrk;
521 
522 	/*
523 	 * State to execute the "hot-add" operation.
524 	 */
525 	struct hot_add_wrk ha_wrk;
526 
527 	/*
528 	 * This state tracks if the host has specified a hot-add
529 	 * region.
530 	 */
531 	bool host_specified_ha_region;
532 
533 	/*
534 	 * State to synchronize hot-add.
535 	 */
536 	struct completion  ol_waitevent;
537 	/*
538 	 * This thread handles hot-add
539 	 * requests from the host as well as notifying
540 	 * the host with regards to memory pressure in
541 	 * the guest.
542 	 */
543 	struct task_struct *thread;
544 
545 	/*
546 	 * Protects ha_region_list, num_pages_onlined counter and individual
547 	 * regions from ha_region_list.
548 	 */
549 	spinlock_t ha_lock;
550 
551 	/*
552 	 * A list of hot-add regions.
553 	 */
554 	struct list_head ha_region_list;
555 
556 	/*
557 	 * We start with the highest version we can support
558 	 * and downgrade based on the host; we save here the
559 	 * next version to try.
560 	 */
561 	__u32 next_version;
562 
563 	/*
564 	 * The negotiated version agreed by host.
565 	 */
566 	__u32 version;
567 
568 	struct page_reporting_dev_info pr_dev_info;
569 };
570 
571 static struct hv_dynmem_device dm_device;
572 
573 static void post_status(struct hv_dynmem_device *dm);
574 
575 #ifdef CONFIG_MEMORY_HOTPLUG
576 static inline bool has_pfn_is_backed(struct hv_hotadd_state *has,
577 				     unsigned long pfn)
578 {
579 	struct hv_hotadd_gap *gap;
580 
581 	/* The page is not backed. */
582 	if ((pfn < has->covered_start_pfn) || (pfn >= has->covered_end_pfn))
583 		return false;
584 
585 	/* Check for gaps. */
586 	list_for_each_entry(gap, &has->gap_list, list) {
587 		if ((pfn >= gap->start_pfn) && (pfn < gap->end_pfn))
588 			return false;
589 	}
590 
591 	return true;
592 }
593 
594 static unsigned long hv_page_offline_check(unsigned long start_pfn,
595 					   unsigned long nr_pages)
596 {
597 	unsigned long pfn = start_pfn, count = 0;
598 	struct hv_hotadd_state *has;
599 	bool found;
600 
601 	while (pfn < start_pfn + nr_pages) {
602 		/*
603 		 * Search for HAS which covers the pfn and when we find one
604 		 * count how many consequitive PFNs are covered.
605 		 */
606 		found = false;
607 		list_for_each_entry(has, &dm_device.ha_region_list, list) {
608 			while ((pfn >= has->start_pfn) &&
609 			       (pfn < has->end_pfn) &&
610 			       (pfn < start_pfn + nr_pages)) {
611 				found = true;
612 				if (has_pfn_is_backed(has, pfn))
613 					count++;
614 				pfn++;
615 			}
616 		}
617 
618 		/*
619 		 * This PFN is not in any HAS (e.g. we're offlining a region
620 		 * which was present at boot), no need to account for it. Go
621 		 * to the next one.
622 		 */
623 		if (!found)
624 			pfn++;
625 	}
626 
627 	return count;
628 }
629 
630 static int hv_memory_notifier(struct notifier_block *nb, unsigned long val,
631 			      void *v)
632 {
633 	struct memory_notify *mem = (struct memory_notify *)v;
634 	unsigned long flags, pfn_count;
635 
636 	switch (val) {
637 	case MEM_ONLINE:
638 	case MEM_CANCEL_ONLINE:
639 		complete(&dm_device.ol_waitevent);
640 		break;
641 
642 	case MEM_OFFLINE:
643 		spin_lock_irqsave(&dm_device.ha_lock, flags);
644 		pfn_count = hv_page_offline_check(mem->start_pfn,
645 						  mem->nr_pages);
646 		if (pfn_count <= dm_device.num_pages_onlined) {
647 			dm_device.num_pages_onlined -= pfn_count;
648 		} else {
649 			/*
650 			 * We're offlining more pages than we managed to online.
651 			 * This is unexpected. In any case don't let
652 			 * num_pages_onlined wrap around zero.
653 			 */
654 			WARN_ON_ONCE(1);
655 			dm_device.num_pages_onlined = 0;
656 		}
657 		spin_unlock_irqrestore(&dm_device.ha_lock, flags);
658 		break;
659 	case MEM_GOING_ONLINE:
660 	case MEM_GOING_OFFLINE:
661 	case MEM_CANCEL_OFFLINE:
662 		break;
663 	}
664 	return NOTIFY_OK;
665 }
666 
667 static struct notifier_block hv_memory_nb = {
668 	.notifier_call = hv_memory_notifier,
669 	.priority = 0
670 };
671 
672 /* Check if the particular page is backed and can be onlined and online it. */
673 static void hv_page_online_one(struct hv_hotadd_state *has, struct page *pg)
674 {
675 	if (!has_pfn_is_backed(has, page_to_pfn(pg))) {
676 		if (!PageOffline(pg))
677 			__SetPageOffline(pg);
678 		return;
679 	}
680 	if (PageOffline(pg))
681 		__ClearPageOffline(pg);
682 
683 	/* This frame is currently backed; online the page. */
684 	generic_online_page(pg, 0);
685 
686 	lockdep_assert_held(&dm_device.ha_lock);
687 	dm_device.num_pages_onlined++;
688 }
689 
690 static void hv_bring_pgs_online(struct hv_hotadd_state *has,
691 				unsigned long start_pfn, unsigned long size)
692 {
693 	int i;
694 
695 	pr_debug("Online %lu pages starting at pfn 0x%lx\n", size, start_pfn);
696 	for (i = 0; i < size; i++)
697 		hv_page_online_one(has, pfn_to_page(start_pfn + i));
698 }
699 
700 static void hv_mem_hot_add(unsigned long start, unsigned long size,
701 				unsigned long pfn_count,
702 				struct hv_hotadd_state *has)
703 {
704 	int ret = 0;
705 	int i, nid;
706 	unsigned long start_pfn;
707 	unsigned long processed_pfn;
708 	unsigned long total_pfn = pfn_count;
709 	unsigned long flags;
710 
711 	for (i = 0; i < (size/HA_CHUNK); i++) {
712 		start_pfn = start + (i * HA_CHUNK);
713 
714 		spin_lock_irqsave(&dm_device.ha_lock, flags);
715 		has->ha_end_pfn +=  HA_CHUNK;
716 
717 		if (total_pfn > HA_CHUNK) {
718 			processed_pfn = HA_CHUNK;
719 			total_pfn -= HA_CHUNK;
720 		} else {
721 			processed_pfn = total_pfn;
722 			total_pfn = 0;
723 		}
724 
725 		has->covered_end_pfn +=  processed_pfn;
726 		spin_unlock_irqrestore(&dm_device.ha_lock, flags);
727 
728 		reinit_completion(&dm_device.ol_waitevent);
729 
730 		nid = memory_add_physaddr_to_nid(PFN_PHYS(start_pfn));
731 		ret = add_memory(nid, PFN_PHYS((start_pfn)),
732 				(HA_CHUNK << PAGE_SHIFT), MHP_MERGE_RESOURCE);
733 
734 		if (ret) {
735 			pr_err("hot_add memory failed error is %d\n", ret);
736 			if (ret == -EEXIST) {
737 				/*
738 				 * This error indicates that the error
739 				 * is not a transient failure. This is the
740 				 * case where the guest's physical address map
741 				 * precludes hot adding memory. Stop all further
742 				 * memory hot-add.
743 				 */
744 				do_hot_add = false;
745 			}
746 			spin_lock_irqsave(&dm_device.ha_lock, flags);
747 			has->ha_end_pfn -= HA_CHUNK;
748 			has->covered_end_pfn -=  processed_pfn;
749 			spin_unlock_irqrestore(&dm_device.ha_lock, flags);
750 			break;
751 		}
752 
753 		/*
754 		 * Wait for memory to get onlined. If the kernel onlined the
755 		 * memory when adding it, this will return directly. Otherwise,
756 		 * it will wait for user space to online the memory. This helps
757 		 * to avoid adding memory faster than it is getting onlined. As
758 		 * adding succeeded, it is ok to proceed even if the memory was
759 		 * not onlined in time.
760 		 */
761 		wait_for_completion_timeout(&dm_device.ol_waitevent, 5 * HZ);
762 		post_status(&dm_device);
763 	}
764 }
765 
766 static void hv_online_page(struct page *pg, unsigned int order)
767 {
768 	struct hv_hotadd_state *has;
769 	unsigned long flags;
770 	unsigned long pfn = page_to_pfn(pg);
771 
772 	spin_lock_irqsave(&dm_device.ha_lock, flags);
773 	list_for_each_entry(has, &dm_device.ha_region_list, list) {
774 		/* The page belongs to a different HAS. */
775 		if ((pfn < has->start_pfn) ||
776 				(pfn + (1UL << order) > has->end_pfn))
777 			continue;
778 
779 		hv_bring_pgs_online(has, pfn, 1UL << order);
780 		break;
781 	}
782 	spin_unlock_irqrestore(&dm_device.ha_lock, flags);
783 }
784 
785 static int pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
786 {
787 	struct hv_hotadd_state *has;
788 	struct hv_hotadd_gap *gap;
789 	unsigned long residual, new_inc;
790 	int ret = 0;
791 	unsigned long flags;
792 
793 	spin_lock_irqsave(&dm_device.ha_lock, flags);
794 	list_for_each_entry(has, &dm_device.ha_region_list, list) {
795 		/*
796 		 * If the pfn range we are dealing with is not in the current
797 		 * "hot add block", move on.
798 		 */
799 		if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
800 			continue;
801 
802 		/*
803 		 * If the current start pfn is not where the covered_end
804 		 * is, create a gap and update covered_end_pfn.
805 		 */
806 		if (has->covered_end_pfn != start_pfn) {
807 			gap = kzalloc(sizeof(struct hv_hotadd_gap), GFP_ATOMIC);
808 			if (!gap) {
809 				ret = -ENOMEM;
810 				break;
811 			}
812 
813 			INIT_LIST_HEAD(&gap->list);
814 			gap->start_pfn = has->covered_end_pfn;
815 			gap->end_pfn = start_pfn;
816 			list_add_tail(&gap->list, &has->gap_list);
817 
818 			has->covered_end_pfn = start_pfn;
819 		}
820 
821 		/*
822 		 * If the current hot add-request extends beyond
823 		 * our current limit; extend it.
824 		 */
825 		if ((start_pfn + pfn_cnt) > has->end_pfn) {
826 			residual = (start_pfn + pfn_cnt - has->end_pfn);
827 			/*
828 			 * Extend the region by multiples of HA_CHUNK.
829 			 */
830 			new_inc = (residual / HA_CHUNK) * HA_CHUNK;
831 			if (residual % HA_CHUNK)
832 				new_inc += HA_CHUNK;
833 
834 			has->end_pfn += new_inc;
835 		}
836 
837 		ret = 1;
838 		break;
839 	}
840 	spin_unlock_irqrestore(&dm_device.ha_lock, flags);
841 
842 	return ret;
843 }
844 
845 static unsigned long handle_pg_range(unsigned long pg_start,
846 					unsigned long pg_count)
847 {
848 	unsigned long start_pfn = pg_start;
849 	unsigned long pfn_cnt = pg_count;
850 	unsigned long size;
851 	struct hv_hotadd_state *has;
852 	unsigned long pgs_ol = 0;
853 	unsigned long old_covered_state;
854 	unsigned long res = 0, flags;
855 
856 	pr_debug("Hot adding %lu pages starting at pfn 0x%lx.\n", pg_count,
857 		pg_start);
858 
859 	spin_lock_irqsave(&dm_device.ha_lock, flags);
860 	list_for_each_entry(has, &dm_device.ha_region_list, list) {
861 		/*
862 		 * If the pfn range we are dealing with is not in the current
863 		 * "hot add block", move on.
864 		 */
865 		if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
866 			continue;
867 
868 		old_covered_state = has->covered_end_pfn;
869 
870 		if (start_pfn < has->ha_end_pfn) {
871 			/*
872 			 * This is the case where we are backing pages
873 			 * in an already hot added region. Bring
874 			 * these pages online first.
875 			 */
876 			pgs_ol = has->ha_end_pfn - start_pfn;
877 			if (pgs_ol > pfn_cnt)
878 				pgs_ol = pfn_cnt;
879 
880 			has->covered_end_pfn +=  pgs_ol;
881 			pfn_cnt -= pgs_ol;
882 			/*
883 			 * Check if the corresponding memory block is already
884 			 * online. It is possible to observe struct pages still
885 			 * being uninitialized here so check section instead.
886 			 * In case the section is online we need to bring the
887 			 * rest of pfns (which were not backed previously)
888 			 * online too.
889 			 */
890 			if (start_pfn > has->start_pfn &&
891 			    online_section_nr(pfn_to_section_nr(start_pfn)))
892 				hv_bring_pgs_online(has, start_pfn, pgs_ol);
893 
894 		}
895 
896 		if ((has->ha_end_pfn < has->end_pfn) && (pfn_cnt > 0)) {
897 			/*
898 			 * We have some residual hot add range
899 			 * that needs to be hot added; hot add
900 			 * it now. Hot add a multiple of
901 			 * of HA_CHUNK that fully covers the pages
902 			 * we have.
903 			 */
904 			size = (has->end_pfn - has->ha_end_pfn);
905 			if (pfn_cnt <= size) {
906 				size = ((pfn_cnt / HA_CHUNK) * HA_CHUNK);
907 				if (pfn_cnt % HA_CHUNK)
908 					size += HA_CHUNK;
909 			} else {
910 				pfn_cnt = size;
911 			}
912 			spin_unlock_irqrestore(&dm_device.ha_lock, flags);
913 			hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
914 			spin_lock_irqsave(&dm_device.ha_lock, flags);
915 		}
916 		/*
917 		 * If we managed to online any pages that were given to us,
918 		 * we declare success.
919 		 */
920 		res = has->covered_end_pfn - old_covered_state;
921 		break;
922 	}
923 	spin_unlock_irqrestore(&dm_device.ha_lock, flags);
924 
925 	return res;
926 }
927 
928 static unsigned long process_hot_add(unsigned long pg_start,
929 					unsigned long pfn_cnt,
930 					unsigned long rg_start,
931 					unsigned long rg_size)
932 {
933 	struct hv_hotadd_state *ha_region = NULL;
934 	int covered;
935 	unsigned long flags;
936 
937 	if (pfn_cnt == 0)
938 		return 0;
939 
940 	if (!dm_device.host_specified_ha_region) {
941 		covered = pfn_covered(pg_start, pfn_cnt);
942 		if (covered < 0)
943 			return 0;
944 
945 		if (covered)
946 			goto do_pg_range;
947 	}
948 
949 	/*
950 	 * If the host has specified a hot-add range; deal with it first.
951 	 */
952 
953 	if (rg_size != 0) {
954 		ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
955 		if (!ha_region)
956 			return 0;
957 
958 		INIT_LIST_HEAD(&ha_region->list);
959 		INIT_LIST_HEAD(&ha_region->gap_list);
960 
961 		ha_region->start_pfn = rg_start;
962 		ha_region->ha_end_pfn = rg_start;
963 		ha_region->covered_start_pfn = pg_start;
964 		ha_region->covered_end_pfn = pg_start;
965 		ha_region->end_pfn = rg_start + rg_size;
966 
967 		spin_lock_irqsave(&dm_device.ha_lock, flags);
968 		list_add_tail(&ha_region->list, &dm_device.ha_region_list);
969 		spin_unlock_irqrestore(&dm_device.ha_lock, flags);
970 	}
971 
972 do_pg_range:
973 	/*
974 	 * Process the page range specified; bringing them
975 	 * online if possible.
976 	 */
977 	return handle_pg_range(pg_start, pfn_cnt);
978 }
979 
980 #endif
981 
982 static void hot_add_req(struct work_struct *dummy)
983 {
984 	struct dm_hot_add_response resp;
985 #ifdef CONFIG_MEMORY_HOTPLUG
986 	unsigned long pg_start, pfn_cnt;
987 	unsigned long rg_start, rg_sz;
988 #endif
989 	struct hv_dynmem_device *dm = &dm_device;
990 
991 	memset(&resp, 0, sizeof(struct dm_hot_add_response));
992 	resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
993 	resp.hdr.size = sizeof(struct dm_hot_add_response);
994 
995 #ifdef CONFIG_MEMORY_HOTPLUG
996 	pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
997 	pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
998 
999 	rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
1000 	rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
1001 
1002 	if ((rg_start == 0) && (!dm->host_specified_ha_region)) {
1003 		unsigned long region_size;
1004 		unsigned long region_start;
1005 
1006 		/*
1007 		 * The host has not specified the hot-add region.
1008 		 * Based on the hot-add page range being specified,
1009 		 * compute a hot-add region that can cover the pages
1010 		 * that need to be hot-added while ensuring the alignment
1011 		 * and size requirements of Linux as it relates to hot-add.
1012 		 */
1013 		region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK;
1014 		if (pfn_cnt % HA_CHUNK)
1015 			region_size += HA_CHUNK;
1016 
1017 		region_start = (pg_start / HA_CHUNK) * HA_CHUNK;
1018 
1019 		rg_start = region_start;
1020 		rg_sz = region_size;
1021 	}
1022 
1023 	if (do_hot_add)
1024 		resp.page_count = process_hot_add(pg_start, pfn_cnt,
1025 						rg_start, rg_sz);
1026 
1027 	dm->num_pages_added += resp.page_count;
1028 #endif
1029 	/*
1030 	 * The result field of the response structure has the
1031 	 * following semantics:
1032 	 *
1033 	 * 1. If all or some pages hot-added: Guest should return success.
1034 	 *
1035 	 * 2. If no pages could be hot-added:
1036 	 *
1037 	 * If the guest returns success, then the host
1038 	 * will not attempt any further hot-add operations. This
1039 	 * signifies a permanent failure.
1040 	 *
1041 	 * If the guest returns failure, then this failure will be
1042 	 * treated as a transient failure and the host may retry the
1043 	 * hot-add operation after some delay.
1044 	 */
1045 	if (resp.page_count > 0)
1046 		resp.result = 1;
1047 	else if (!do_hot_add)
1048 		resp.result = 1;
1049 	else
1050 		resp.result = 0;
1051 
1052 	if (!do_hot_add || resp.page_count == 0) {
1053 		if (!allow_hibernation)
1054 			pr_err("Memory hot add failed\n");
1055 		else
1056 			pr_info("Ignore hot-add request!\n");
1057 	}
1058 
1059 	dm->state = DM_INITIALIZED;
1060 	resp.hdr.trans_id = atomic_inc_return(&trans_id);
1061 	vmbus_sendpacket(dm->dev->channel, &resp,
1062 			sizeof(struct dm_hot_add_response),
1063 			(unsigned long)NULL,
1064 			VM_PKT_DATA_INBAND, 0);
1065 }
1066 
1067 static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
1068 {
1069 	struct dm_info_header *info_hdr;
1070 
1071 	info_hdr = (struct dm_info_header *)msg->info;
1072 
1073 	switch (info_hdr->type) {
1074 	case INFO_TYPE_MAX_PAGE_CNT:
1075 		if (info_hdr->data_size == sizeof(__u64)) {
1076 			__u64 *max_page_count = (__u64 *)&info_hdr[1];
1077 
1078 			pr_info("Max. dynamic memory size: %llu MB\n",
1079 				(*max_page_count) >> (20 - HV_HYP_PAGE_SHIFT));
1080 		}
1081 
1082 		break;
1083 	default:
1084 		pr_warn("Received Unknown type: %d\n", info_hdr->type);
1085 	}
1086 }
1087 
1088 static unsigned long compute_balloon_floor(void)
1089 {
1090 	unsigned long min_pages;
1091 	unsigned long nr_pages = totalram_pages();
1092 #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
1093 	/* Simple continuous piecewiese linear function:
1094 	 *  max MiB -> min MiB  gradient
1095 	 *       0         0
1096 	 *      16        16
1097 	 *      32        24
1098 	 *     128        72    (1/2)
1099 	 *     512       168    (1/4)
1100 	 *    2048       360    (1/8)
1101 	 *    8192       744    (1/16)
1102 	 *   32768      1512	(1/32)
1103 	 */
1104 	if (nr_pages < MB2PAGES(128))
1105 		min_pages = MB2PAGES(8) + (nr_pages >> 1);
1106 	else if (nr_pages < MB2PAGES(512))
1107 		min_pages = MB2PAGES(40) + (nr_pages >> 2);
1108 	else if (nr_pages < MB2PAGES(2048))
1109 		min_pages = MB2PAGES(104) + (nr_pages >> 3);
1110 	else if (nr_pages < MB2PAGES(8192))
1111 		min_pages = MB2PAGES(232) + (nr_pages >> 4);
1112 	else
1113 		min_pages = MB2PAGES(488) + (nr_pages >> 5);
1114 #undef MB2PAGES
1115 	return min_pages;
1116 }
1117 
1118 /*
1119  * Post our status as it relates memory pressure to the
1120  * host. Host expects the guests to post this status
1121  * periodically at 1 second intervals.
1122  *
1123  * The metrics specified in this protocol are very Windows
1124  * specific and so we cook up numbers here to convey our memory
1125  * pressure.
1126  */
1127 
1128 static void post_status(struct hv_dynmem_device *dm)
1129 {
1130 	struct dm_status status;
1131 	unsigned long now = jiffies;
1132 	unsigned long last_post = last_post_time;
1133 
1134 	if (pressure_report_delay > 0) {
1135 		--pressure_report_delay;
1136 		return;
1137 	}
1138 
1139 	if (!time_after(now, (last_post_time + HZ)))
1140 		return;
1141 
1142 	memset(&status, 0, sizeof(struct dm_status));
1143 	status.hdr.type = DM_STATUS_REPORT;
1144 	status.hdr.size = sizeof(struct dm_status);
1145 	status.hdr.trans_id = atomic_inc_return(&trans_id);
1146 
1147 	/*
1148 	 * The host expects the guest to report free and committed memory.
1149 	 * Furthermore, the host expects the pressure information to include
1150 	 * the ballooned out pages. For a given amount of memory that we are
1151 	 * managing we need to compute a floor below which we should not
1152 	 * balloon. Compute this and add it to the pressure report.
1153 	 * We also need to report all offline pages (num_pages_added -
1154 	 * num_pages_onlined) as committed to the host, otherwise it can try
1155 	 * asking us to balloon them out.
1156 	 */
1157 	status.num_avail = si_mem_available();
1158 	status.num_committed = vm_memory_committed() +
1159 		dm->num_pages_ballooned +
1160 		(dm->num_pages_added > dm->num_pages_onlined ?
1161 		 dm->num_pages_added - dm->num_pages_onlined : 0) +
1162 		compute_balloon_floor();
1163 
1164 	trace_balloon_status(status.num_avail, status.num_committed,
1165 			     vm_memory_committed(), dm->num_pages_ballooned,
1166 			     dm->num_pages_added, dm->num_pages_onlined);
1167 	/*
1168 	 * If our transaction ID is no longer current, just don't
1169 	 * send the status. This can happen if we were interrupted
1170 	 * after we picked our transaction ID.
1171 	 */
1172 	if (status.hdr.trans_id != atomic_read(&trans_id))
1173 		return;
1174 
1175 	/*
1176 	 * If the last post time that we sampled has changed,
1177 	 * we have raced, don't post the status.
1178 	 */
1179 	if (last_post != last_post_time)
1180 		return;
1181 
1182 	last_post_time = jiffies;
1183 	vmbus_sendpacket(dm->dev->channel, &status,
1184 				sizeof(struct dm_status),
1185 				(unsigned long)NULL,
1186 				VM_PKT_DATA_INBAND, 0);
1187 
1188 }
1189 
1190 static void free_balloon_pages(struct hv_dynmem_device *dm,
1191 			 union dm_mem_page_range *range_array)
1192 {
1193 	int num_pages = range_array->finfo.page_cnt;
1194 	__u64 start_frame = range_array->finfo.start_page;
1195 	struct page *pg;
1196 	int i;
1197 
1198 	for (i = 0; i < num_pages; i++) {
1199 		pg = pfn_to_page(i + start_frame);
1200 		__ClearPageOffline(pg);
1201 		__free_page(pg);
1202 		dm->num_pages_ballooned--;
1203 		adjust_managed_page_count(pg, 1);
1204 	}
1205 }
1206 
1207 
1208 
1209 static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
1210 					unsigned int num_pages,
1211 					struct dm_balloon_response *bl_resp,
1212 					int alloc_unit)
1213 {
1214 	unsigned int i, j;
1215 	struct page *pg;
1216 
1217 	for (i = 0; i < num_pages / alloc_unit; i++) {
1218 		if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
1219 			HV_HYP_PAGE_SIZE)
1220 			return i * alloc_unit;
1221 
1222 		/*
1223 		 * We execute this code in a thread context. Furthermore,
1224 		 * we don't want the kernel to try too hard.
1225 		 */
1226 		pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
1227 				__GFP_NOMEMALLOC | __GFP_NOWARN,
1228 				get_order(alloc_unit << PAGE_SHIFT));
1229 
1230 		if (!pg)
1231 			return i * alloc_unit;
1232 
1233 		dm->num_pages_ballooned += alloc_unit;
1234 
1235 		/*
1236 		 * If we allocatted 2M pages; split them so we
1237 		 * can free them in any order we get.
1238 		 */
1239 
1240 		if (alloc_unit != 1)
1241 			split_page(pg, get_order(alloc_unit << PAGE_SHIFT));
1242 
1243 		/* mark all pages offline */
1244 		for (j = 0; j < alloc_unit; j++) {
1245 			__SetPageOffline(pg + j);
1246 			adjust_managed_page_count(pg + j, -1);
1247 		}
1248 
1249 		bl_resp->range_count++;
1250 		bl_resp->range_array[i].finfo.start_page =
1251 			page_to_pfn(pg);
1252 		bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
1253 		bl_resp->hdr.size += sizeof(union dm_mem_page_range);
1254 
1255 	}
1256 
1257 	return i * alloc_unit;
1258 }
1259 
1260 static void balloon_up(struct work_struct *dummy)
1261 {
1262 	unsigned int num_pages = dm_device.balloon_wrk.num_pages;
1263 	unsigned int num_ballooned = 0;
1264 	struct dm_balloon_response *bl_resp;
1265 	int alloc_unit;
1266 	int ret;
1267 	bool done = false;
1268 	int i;
1269 	long avail_pages;
1270 	unsigned long floor;
1271 
1272 	/*
1273 	 * We will attempt 2M allocations. However, if we fail to
1274 	 * allocate 2M chunks, we will go back to PAGE_SIZE allocations.
1275 	 */
1276 	alloc_unit = PAGES_IN_2M;
1277 
1278 	avail_pages = si_mem_available();
1279 	floor = compute_balloon_floor();
1280 
1281 	/* Refuse to balloon below the floor. */
1282 	if (avail_pages < num_pages || avail_pages - num_pages < floor) {
1283 		pr_info("Balloon request will be partially fulfilled. %s\n",
1284 			avail_pages < num_pages ? "Not enough memory." :
1285 			"Balloon floor reached.");
1286 
1287 		num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
1288 	}
1289 
1290 	while (!done) {
1291 		memset(balloon_up_send_buffer, 0, HV_HYP_PAGE_SIZE);
1292 		bl_resp = (struct dm_balloon_response *)balloon_up_send_buffer;
1293 		bl_resp->hdr.type = DM_BALLOON_RESPONSE;
1294 		bl_resp->hdr.size = sizeof(struct dm_balloon_response);
1295 		bl_resp->more_pages = 1;
1296 
1297 		num_pages -= num_ballooned;
1298 		num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
1299 						    bl_resp, alloc_unit);
1300 
1301 		if (alloc_unit != 1 && num_ballooned == 0) {
1302 			alloc_unit = 1;
1303 			continue;
1304 		}
1305 
1306 		if (num_ballooned == 0 || num_ballooned == num_pages) {
1307 			pr_debug("Ballooned %u out of %u requested pages.\n",
1308 				num_pages, dm_device.balloon_wrk.num_pages);
1309 
1310 			bl_resp->more_pages = 0;
1311 			done = true;
1312 			dm_device.state = DM_INITIALIZED;
1313 		}
1314 
1315 		/*
1316 		 * We are pushing a lot of data through the channel;
1317 		 * deal with transient failures caused because of the
1318 		 * lack of space in the ring buffer.
1319 		 */
1320 
1321 		do {
1322 			bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
1323 			ret = vmbus_sendpacket(dm_device.dev->channel,
1324 						bl_resp,
1325 						bl_resp->hdr.size,
1326 						(unsigned long)NULL,
1327 						VM_PKT_DATA_INBAND, 0);
1328 
1329 			if (ret == -EAGAIN)
1330 				msleep(20);
1331 			post_status(&dm_device);
1332 		} while (ret == -EAGAIN);
1333 
1334 		if (ret) {
1335 			/*
1336 			 * Free up the memory we allocatted.
1337 			 */
1338 			pr_err("Balloon response failed\n");
1339 
1340 			for (i = 0; i < bl_resp->range_count; i++)
1341 				free_balloon_pages(&dm_device,
1342 						 &bl_resp->range_array[i]);
1343 
1344 			done = true;
1345 		}
1346 	}
1347 
1348 }
1349 
1350 static void balloon_down(struct hv_dynmem_device *dm,
1351 			struct dm_unballoon_request *req)
1352 {
1353 	union dm_mem_page_range *range_array = req->range_array;
1354 	int range_count = req->range_count;
1355 	struct dm_unballoon_response resp;
1356 	int i;
1357 	unsigned int prev_pages_ballooned = dm->num_pages_ballooned;
1358 
1359 	for (i = 0; i < range_count; i++) {
1360 		free_balloon_pages(dm, &range_array[i]);
1361 		complete(&dm_device.config_event);
1362 	}
1363 
1364 	pr_debug("Freed %u ballooned pages.\n",
1365 		prev_pages_ballooned - dm->num_pages_ballooned);
1366 
1367 	if (req->more_pages == 1)
1368 		return;
1369 
1370 	memset(&resp, 0, sizeof(struct dm_unballoon_response));
1371 	resp.hdr.type = DM_UNBALLOON_RESPONSE;
1372 	resp.hdr.trans_id = atomic_inc_return(&trans_id);
1373 	resp.hdr.size = sizeof(struct dm_unballoon_response);
1374 
1375 	vmbus_sendpacket(dm_device.dev->channel, &resp,
1376 				sizeof(struct dm_unballoon_response),
1377 				(unsigned long)NULL,
1378 				VM_PKT_DATA_INBAND, 0);
1379 
1380 	dm->state = DM_INITIALIZED;
1381 }
1382 
1383 static void balloon_onchannelcallback(void *context);
1384 
1385 static int dm_thread_func(void *dm_dev)
1386 {
1387 	struct hv_dynmem_device *dm = dm_dev;
1388 
1389 	while (!kthread_should_stop()) {
1390 		wait_for_completion_interruptible_timeout(
1391 						&dm_device.config_event, 1*HZ);
1392 		/*
1393 		 * The host expects us to post information on the memory
1394 		 * pressure every second.
1395 		 */
1396 		reinit_completion(&dm_device.config_event);
1397 		post_status(dm);
1398 	}
1399 
1400 	return 0;
1401 }
1402 
1403 
1404 static void version_resp(struct hv_dynmem_device *dm,
1405 			struct dm_version_response *vresp)
1406 {
1407 	struct dm_version_request version_req;
1408 	int ret;
1409 
1410 	if (vresp->is_accepted) {
1411 		/*
1412 		 * We are done; wakeup the
1413 		 * context waiting for version
1414 		 * negotiation.
1415 		 */
1416 		complete(&dm->host_event);
1417 		return;
1418 	}
1419 	/*
1420 	 * If there are more versions to try, continue
1421 	 * with negotiations; if not
1422 	 * shutdown the service since we are not able
1423 	 * to negotiate a suitable version number
1424 	 * with the host.
1425 	 */
1426 	if (dm->next_version == 0)
1427 		goto version_error;
1428 
1429 	memset(&version_req, 0, sizeof(struct dm_version_request));
1430 	version_req.hdr.type = DM_VERSION_REQUEST;
1431 	version_req.hdr.size = sizeof(struct dm_version_request);
1432 	version_req.hdr.trans_id = atomic_inc_return(&trans_id);
1433 	version_req.version.version = dm->next_version;
1434 	dm->version = version_req.version.version;
1435 
1436 	/*
1437 	 * Set the next version to try in case current version fails.
1438 	 * Win7 protocol ought to be the last one to try.
1439 	 */
1440 	switch (version_req.version.version) {
1441 	case DYNMEM_PROTOCOL_VERSION_WIN8:
1442 		dm->next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
1443 		version_req.is_last_attempt = 0;
1444 		break;
1445 	default:
1446 		dm->next_version = 0;
1447 		version_req.is_last_attempt = 1;
1448 	}
1449 
1450 	ret = vmbus_sendpacket(dm->dev->channel, &version_req,
1451 				sizeof(struct dm_version_request),
1452 				(unsigned long)NULL,
1453 				VM_PKT_DATA_INBAND, 0);
1454 
1455 	if (ret)
1456 		goto version_error;
1457 
1458 	return;
1459 
1460 version_error:
1461 	dm->state = DM_INIT_ERROR;
1462 	complete(&dm->host_event);
1463 }
1464 
1465 static void cap_resp(struct hv_dynmem_device *dm,
1466 			struct dm_capabilities_resp_msg *cap_resp)
1467 {
1468 	if (!cap_resp->is_accepted) {
1469 		pr_err("Capabilities not accepted by host\n");
1470 		dm->state = DM_INIT_ERROR;
1471 	}
1472 	complete(&dm->host_event);
1473 }
1474 
1475 static void balloon_onchannelcallback(void *context)
1476 {
1477 	struct hv_device *dev = context;
1478 	u32 recvlen;
1479 	u64 requestid;
1480 	struct dm_message *dm_msg;
1481 	struct dm_header *dm_hdr;
1482 	struct hv_dynmem_device *dm = hv_get_drvdata(dev);
1483 	struct dm_balloon *bal_msg;
1484 	struct dm_hot_add *ha_msg;
1485 	union dm_mem_page_range *ha_pg_range;
1486 	union dm_mem_page_range *ha_region;
1487 
1488 	memset(recv_buffer, 0, sizeof(recv_buffer));
1489 	vmbus_recvpacket(dev->channel, recv_buffer,
1490 			 HV_HYP_PAGE_SIZE, &recvlen, &requestid);
1491 
1492 	if (recvlen > 0) {
1493 		dm_msg = (struct dm_message *)recv_buffer;
1494 		dm_hdr = &dm_msg->hdr;
1495 
1496 		switch (dm_hdr->type) {
1497 		case DM_VERSION_RESPONSE:
1498 			version_resp(dm,
1499 				 (struct dm_version_response *)dm_msg);
1500 			break;
1501 
1502 		case DM_CAPABILITIES_RESPONSE:
1503 			cap_resp(dm,
1504 				 (struct dm_capabilities_resp_msg *)dm_msg);
1505 			break;
1506 
1507 		case DM_BALLOON_REQUEST:
1508 			if (allow_hibernation) {
1509 				pr_info("Ignore balloon-up request!\n");
1510 				break;
1511 			}
1512 
1513 			if (dm->state == DM_BALLOON_UP)
1514 				pr_warn("Currently ballooning\n");
1515 			bal_msg = (struct dm_balloon *)recv_buffer;
1516 			dm->state = DM_BALLOON_UP;
1517 			dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
1518 			schedule_work(&dm_device.balloon_wrk.wrk);
1519 			break;
1520 
1521 		case DM_UNBALLOON_REQUEST:
1522 			if (allow_hibernation) {
1523 				pr_info("Ignore balloon-down request!\n");
1524 				break;
1525 			}
1526 
1527 			dm->state = DM_BALLOON_DOWN;
1528 			balloon_down(dm,
1529 				 (struct dm_unballoon_request *)recv_buffer);
1530 			break;
1531 
1532 		case DM_MEM_HOT_ADD_REQUEST:
1533 			if (dm->state == DM_HOT_ADD)
1534 				pr_warn("Currently hot-adding\n");
1535 			dm->state = DM_HOT_ADD;
1536 			ha_msg = (struct dm_hot_add *)recv_buffer;
1537 			if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
1538 				/*
1539 				 * This is a normal hot-add request specifying
1540 				 * hot-add memory.
1541 				 */
1542 				dm->host_specified_ha_region = false;
1543 				ha_pg_range = &ha_msg->range;
1544 				dm->ha_wrk.ha_page_range = *ha_pg_range;
1545 				dm->ha_wrk.ha_region_range.page_range = 0;
1546 			} else {
1547 				/*
1548 				 * Host is specifying that we first hot-add
1549 				 * a region and then partially populate this
1550 				 * region.
1551 				 */
1552 				dm->host_specified_ha_region = true;
1553 				ha_pg_range = &ha_msg->range;
1554 				ha_region = &ha_pg_range[1];
1555 				dm->ha_wrk.ha_page_range = *ha_pg_range;
1556 				dm->ha_wrk.ha_region_range = *ha_region;
1557 			}
1558 			schedule_work(&dm_device.ha_wrk.wrk);
1559 			break;
1560 
1561 		case DM_INFO_MESSAGE:
1562 			process_info(dm, (struct dm_info_msg *)dm_msg);
1563 			break;
1564 
1565 		default:
1566 			pr_warn_ratelimited("Unhandled message: type: %d\n", dm_hdr->type);
1567 
1568 		}
1569 	}
1570 
1571 }
1572 
1573 /* Hyper-V only supports reporting 2MB pages or higher */
1574 #define HV_MIN_PAGE_REPORTING_ORDER	9
1575 #define HV_MIN_PAGE_REPORTING_LEN (HV_HYP_PAGE_SIZE << HV_MIN_PAGE_REPORTING_ORDER)
1576 static int hv_free_page_report(struct page_reporting_dev_info *pr_dev_info,
1577 		    struct scatterlist *sgl, unsigned int nents)
1578 {
1579 	unsigned long flags;
1580 	struct hv_memory_hint *hint;
1581 	int i;
1582 	u64 status;
1583 	struct scatterlist *sg;
1584 
1585 	WARN_ON_ONCE(nents > HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES);
1586 	WARN_ON_ONCE(sgl->length < HV_MIN_PAGE_REPORTING_LEN);
1587 	local_irq_save(flags);
1588 	hint = *(struct hv_memory_hint **)this_cpu_ptr(hyperv_pcpu_input_arg);
1589 	if (!hint) {
1590 		local_irq_restore(flags);
1591 		return -ENOSPC;
1592 	}
1593 
1594 	hint->type = HV_EXT_MEMORY_HEAT_HINT_TYPE_COLD_DISCARD;
1595 	hint->reserved = 0;
1596 	for_each_sg(sgl, sg, nents, i) {
1597 		union hv_gpa_page_range *range;
1598 
1599 		range = &hint->ranges[i];
1600 		range->address_space = 0;
1601 		/* page reporting only reports 2MB pages or higher */
1602 		range->page.largepage = 1;
1603 		range->page.additional_pages =
1604 			(sg->length / HV_MIN_PAGE_REPORTING_LEN) - 1;
1605 		range->page_size = HV_GPA_PAGE_RANGE_PAGE_SIZE_2MB;
1606 		range->base_large_pfn =
1607 			page_to_hvpfn(sg_page(sg)) >> HV_MIN_PAGE_REPORTING_ORDER;
1608 	}
1609 
1610 	status = hv_do_rep_hypercall(HV_EXT_CALL_MEMORY_HEAT_HINT, nents, 0,
1611 				     hint, NULL);
1612 	local_irq_restore(flags);
1613 	if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) {
1614 		pr_err("Cold memory discard hypercall failed with status %llx\n",
1615 			status);
1616 		return -EINVAL;
1617 	}
1618 
1619 	return 0;
1620 }
1621 
1622 static void enable_page_reporting(void)
1623 {
1624 	int ret;
1625 
1626 	/* Essentially, validating 'PAGE_REPORTING_MIN_ORDER' is big enough. */
1627 	if (pageblock_order < HV_MIN_PAGE_REPORTING_ORDER) {
1628 		pr_debug("Cold memory discard is only supported on 2MB pages and above\n");
1629 		return;
1630 	}
1631 
1632 	if (!hv_query_ext_cap(HV_EXT_CAPABILITY_MEMORY_COLD_DISCARD_HINT)) {
1633 		pr_debug("Cold memory discard hint not supported by Hyper-V\n");
1634 		return;
1635 	}
1636 
1637 	BUILD_BUG_ON(PAGE_REPORTING_CAPACITY > HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES);
1638 	dm_device.pr_dev_info.report = hv_free_page_report;
1639 	ret = page_reporting_register(&dm_device.pr_dev_info);
1640 	if (ret < 0) {
1641 		dm_device.pr_dev_info.report = NULL;
1642 		pr_err("Failed to enable cold memory discard: %d\n", ret);
1643 	} else {
1644 		pr_info("Cold memory discard hint enabled\n");
1645 	}
1646 }
1647 
1648 static void disable_page_reporting(void)
1649 {
1650 	if (dm_device.pr_dev_info.report) {
1651 		page_reporting_unregister(&dm_device.pr_dev_info);
1652 		dm_device.pr_dev_info.report = NULL;
1653 	}
1654 }
1655 
1656 static int balloon_connect_vsp(struct hv_device *dev)
1657 {
1658 	struct dm_version_request version_req;
1659 	struct dm_capabilities cap_msg;
1660 	unsigned long t;
1661 	int ret;
1662 
1663 	/*
1664 	 * max_pkt_size should be large enough for one vmbus packet header plus
1665 	 * our receive buffer size. Hyper-V sends messages up to
1666 	 * HV_HYP_PAGE_SIZE bytes long on balloon channel.
1667 	 */
1668 	dev->channel->max_pkt_size = HV_HYP_PAGE_SIZE * 2;
1669 
1670 	ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
1671 			 balloon_onchannelcallback, dev);
1672 	if (ret)
1673 		return ret;
1674 
1675 	/*
1676 	 * Initiate the hand shake with the host and negotiate
1677 	 * a version that the host can support. We start with the
1678 	 * highest version number and go down if the host cannot
1679 	 * support it.
1680 	 */
1681 	memset(&version_req, 0, sizeof(struct dm_version_request));
1682 	version_req.hdr.type = DM_VERSION_REQUEST;
1683 	version_req.hdr.size = sizeof(struct dm_version_request);
1684 	version_req.hdr.trans_id = atomic_inc_return(&trans_id);
1685 	version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN10;
1686 	version_req.is_last_attempt = 0;
1687 	dm_device.version = version_req.version.version;
1688 
1689 	ret = vmbus_sendpacket(dev->channel, &version_req,
1690 			       sizeof(struct dm_version_request),
1691 			       (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
1692 	if (ret)
1693 		goto out;
1694 
1695 	t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1696 	if (t == 0) {
1697 		ret = -ETIMEDOUT;
1698 		goto out;
1699 	}
1700 
1701 	/*
1702 	 * If we could not negotiate a compatible version with the host
1703 	 * fail the probe function.
1704 	 */
1705 	if (dm_device.state == DM_INIT_ERROR) {
1706 		ret = -EPROTO;
1707 		goto out;
1708 	}
1709 
1710 	pr_info("Using Dynamic Memory protocol version %u.%u\n",
1711 		DYNMEM_MAJOR_VERSION(dm_device.version),
1712 		DYNMEM_MINOR_VERSION(dm_device.version));
1713 
1714 	/*
1715 	 * Now submit our capabilities to the host.
1716 	 */
1717 	memset(&cap_msg, 0, sizeof(struct dm_capabilities));
1718 	cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
1719 	cap_msg.hdr.size = sizeof(struct dm_capabilities);
1720 	cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
1721 
1722 	/*
1723 	 * When hibernation (i.e. virtual ACPI S4 state) is enabled, the host
1724 	 * currently still requires the bits to be set, so we have to add code
1725 	 * to fail the host's hot-add and balloon up/down requests, if any.
1726 	 */
1727 	cap_msg.caps.cap_bits.balloon = 1;
1728 	cap_msg.caps.cap_bits.hot_add = 1;
1729 
1730 	/*
1731 	 * Specify our alignment requirements as it relates
1732 	 * memory hot-add. Specify 128MB alignment.
1733 	 */
1734 	cap_msg.caps.cap_bits.hot_add_alignment = 7;
1735 
1736 	/*
1737 	 * Currently the host does not use these
1738 	 * values and we set them to what is done in the
1739 	 * Windows driver.
1740 	 */
1741 	cap_msg.min_page_cnt = 0;
1742 	cap_msg.max_page_number = -1;
1743 
1744 	ret = vmbus_sendpacket(dev->channel, &cap_msg,
1745 			       sizeof(struct dm_capabilities),
1746 			       (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
1747 	if (ret)
1748 		goto out;
1749 
1750 	t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1751 	if (t == 0) {
1752 		ret = -ETIMEDOUT;
1753 		goto out;
1754 	}
1755 
1756 	/*
1757 	 * If the host does not like our capabilities,
1758 	 * fail the probe function.
1759 	 */
1760 	if (dm_device.state == DM_INIT_ERROR) {
1761 		ret = -EPROTO;
1762 		goto out;
1763 	}
1764 
1765 	return 0;
1766 out:
1767 	vmbus_close(dev->channel);
1768 	return ret;
1769 }
1770 
1771 static int balloon_probe(struct hv_device *dev,
1772 			 const struct hv_vmbus_device_id *dev_id)
1773 {
1774 	int ret;
1775 
1776 	allow_hibernation = hv_is_hibernation_supported();
1777 	if (allow_hibernation)
1778 		hot_add = false;
1779 
1780 #ifdef CONFIG_MEMORY_HOTPLUG
1781 	do_hot_add = hot_add;
1782 #else
1783 	do_hot_add = false;
1784 #endif
1785 	dm_device.dev = dev;
1786 	dm_device.state = DM_INITIALIZING;
1787 	dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN8;
1788 	init_completion(&dm_device.host_event);
1789 	init_completion(&dm_device.config_event);
1790 	INIT_LIST_HEAD(&dm_device.ha_region_list);
1791 	spin_lock_init(&dm_device.ha_lock);
1792 	INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
1793 	INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
1794 	dm_device.host_specified_ha_region = false;
1795 
1796 #ifdef CONFIG_MEMORY_HOTPLUG
1797 	set_online_page_callback(&hv_online_page);
1798 	init_completion(&dm_device.ol_waitevent);
1799 	register_memory_notifier(&hv_memory_nb);
1800 #endif
1801 
1802 	hv_set_drvdata(dev, &dm_device);
1803 
1804 	ret = balloon_connect_vsp(dev);
1805 	if (ret != 0)
1806 		return ret;
1807 
1808 	enable_page_reporting();
1809 	dm_device.state = DM_INITIALIZED;
1810 
1811 	dm_device.thread =
1812 		 kthread_run(dm_thread_func, &dm_device, "hv_balloon");
1813 	if (IS_ERR(dm_device.thread)) {
1814 		ret = PTR_ERR(dm_device.thread);
1815 		goto probe_error;
1816 	}
1817 
1818 	return 0;
1819 
1820 probe_error:
1821 	dm_device.state = DM_INIT_ERROR;
1822 	dm_device.thread  = NULL;
1823 	disable_page_reporting();
1824 	vmbus_close(dev->channel);
1825 #ifdef CONFIG_MEMORY_HOTPLUG
1826 	unregister_memory_notifier(&hv_memory_nb);
1827 	restore_online_page_callback(&hv_online_page);
1828 #endif
1829 	return ret;
1830 }
1831 
1832 static int balloon_remove(struct hv_device *dev)
1833 {
1834 	struct hv_dynmem_device *dm = hv_get_drvdata(dev);
1835 	struct hv_hotadd_state *has, *tmp;
1836 	struct hv_hotadd_gap *gap, *tmp_gap;
1837 	unsigned long flags;
1838 
1839 	if (dm->num_pages_ballooned != 0)
1840 		pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
1841 
1842 	cancel_work_sync(&dm->balloon_wrk.wrk);
1843 	cancel_work_sync(&dm->ha_wrk.wrk);
1844 
1845 	kthread_stop(dm->thread);
1846 	disable_page_reporting();
1847 	vmbus_close(dev->channel);
1848 #ifdef CONFIG_MEMORY_HOTPLUG
1849 	unregister_memory_notifier(&hv_memory_nb);
1850 	restore_online_page_callback(&hv_online_page);
1851 #endif
1852 	spin_lock_irqsave(&dm_device.ha_lock, flags);
1853 	list_for_each_entry_safe(has, tmp, &dm->ha_region_list, list) {
1854 		list_for_each_entry_safe(gap, tmp_gap, &has->gap_list, list) {
1855 			list_del(&gap->list);
1856 			kfree(gap);
1857 		}
1858 		list_del(&has->list);
1859 		kfree(has);
1860 	}
1861 	spin_unlock_irqrestore(&dm_device.ha_lock, flags);
1862 
1863 	return 0;
1864 }
1865 
1866 static int balloon_suspend(struct hv_device *hv_dev)
1867 {
1868 	struct hv_dynmem_device *dm = hv_get_drvdata(hv_dev);
1869 
1870 	tasklet_disable(&hv_dev->channel->callback_event);
1871 
1872 	cancel_work_sync(&dm->balloon_wrk.wrk);
1873 	cancel_work_sync(&dm->ha_wrk.wrk);
1874 
1875 	if (dm->thread) {
1876 		kthread_stop(dm->thread);
1877 		dm->thread = NULL;
1878 		vmbus_close(hv_dev->channel);
1879 	}
1880 
1881 	tasklet_enable(&hv_dev->channel->callback_event);
1882 
1883 	return 0;
1884 
1885 }
1886 
1887 static int balloon_resume(struct hv_device *dev)
1888 {
1889 	int ret;
1890 
1891 	dm_device.state = DM_INITIALIZING;
1892 
1893 	ret = balloon_connect_vsp(dev);
1894 
1895 	if (ret != 0)
1896 		goto out;
1897 
1898 	dm_device.thread =
1899 		 kthread_run(dm_thread_func, &dm_device, "hv_balloon");
1900 	if (IS_ERR(dm_device.thread)) {
1901 		ret = PTR_ERR(dm_device.thread);
1902 		dm_device.thread = NULL;
1903 		goto close_channel;
1904 	}
1905 
1906 	dm_device.state = DM_INITIALIZED;
1907 	return 0;
1908 close_channel:
1909 	vmbus_close(dev->channel);
1910 out:
1911 	dm_device.state = DM_INIT_ERROR;
1912 #ifdef CONFIG_MEMORY_HOTPLUG
1913 	unregister_memory_notifier(&hv_memory_nb);
1914 	restore_online_page_callback(&hv_online_page);
1915 #endif
1916 	return ret;
1917 }
1918 
1919 static const struct hv_vmbus_device_id id_table[] = {
1920 	/* Dynamic Memory Class ID */
1921 	/* 525074DC-8985-46e2-8057-A307DC18A502 */
1922 	{ HV_DM_GUID, },
1923 	{ },
1924 };
1925 
1926 MODULE_DEVICE_TABLE(vmbus, id_table);
1927 
1928 static  struct hv_driver balloon_drv = {
1929 	.name = "hv_balloon",
1930 	.id_table = id_table,
1931 	.probe =  balloon_probe,
1932 	.remove =  balloon_remove,
1933 	.suspend = balloon_suspend,
1934 	.resume = balloon_resume,
1935 	.driver = {
1936 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1937 	},
1938 };
1939 
1940 static int __init init_balloon_drv(void)
1941 {
1942 
1943 	return vmbus_driver_register(&balloon_drv);
1944 }
1945 
1946 module_init(init_balloon_drv);
1947 
1948 MODULE_DESCRIPTION("Hyper-V Balloon");
1949 MODULE_LICENSE("GPL");
1950