blob: b958ded8ac7e9ffef85dbc3b8547746a5cf26367 [file] [log] [blame]
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001/*
2 * Copyright (c) 2012, Microsoft Corporation.
3 *
4 * Author:
5 * K. Y. Srinivasan <kys@microsoft.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
15 * details.
16 *
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/kernel.h>
K. Y. Srinivasanae339332014-04-23 13:53:39 -070022#include <linux/jiffies.h>
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080023#include <linux/mman.h>
24#include <linux/delay.h>
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/kthread.h>
29#include <linux/completion.h>
30#include <linux/memory_hotplug.h>
31#include <linux/memory.h>
32#include <linux/notifier.h>
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080033#include <linux/percpu_counter.h>
34
35#include <linux/hyperv.h>
36
37/*
38 * We begin with definitions supporting the Dynamic Memory protocol
39 * with the host.
40 *
41 * Begin protocol definitions.
42 */
43
44
45
46/*
47 * Protocol versions. The low word is the minor version, the high word the major
48 * version.
49 *
50 * History:
51 * Initial version 1.0
52 * Changed to 0.1 on 2009/03/25
53 * Changes to 0.2 on 2009/05/14
54 * Changes to 0.3 on 2009/12/03
55 * Changed to 1.0 on 2011/04/05
56 */
57
58#define DYNMEM_MAKE_VERSION(Major, Minor) ((__u32)(((Major) << 16) | (Minor)))
59#define DYNMEM_MAJOR_VERSION(Version) ((__u32)(Version) >> 16)
60#define DYNMEM_MINOR_VERSION(Version) ((__u32)(Version) & 0xff)
61
62enum {
63 DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
64 DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
65
66 DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
67 DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
68
69 DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN8
70};
71
72
73
74/*
75 * Message Types
76 */
77
78enum dm_message_type {
79 /*
80 * Version 0.3
81 */
82 DM_ERROR = 0,
83 DM_VERSION_REQUEST = 1,
84 DM_VERSION_RESPONSE = 2,
85 DM_CAPABILITIES_REPORT = 3,
86 DM_CAPABILITIES_RESPONSE = 4,
87 DM_STATUS_REPORT = 5,
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,
95 /*
96 * Version 1.0.
97 */
98 DM_INFO_MESSAGE = 12,
99 DM_VERSION_1_MAX = 12
100};
101
102
103/*
104 * Structures defining the dynamic memory management
105 * protocol.
106 */
107
108union dm_version {
109 struct {
110 __u16 minor_version;
111 __u16 major_version;
112 };
113 __u32 version;
114} __packed;
115
116
117union dm_caps {
118 struct {
119 __u64 balloon:1;
120 __u64 hot_add:1;
K. Y. Srinivasan647965a2013-03-29 07:36:11 -0700121 /*
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.
126 */
127 __u64 hot_add_alignment:4;
128 __u64 reservedz:58;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800129 } cap_bits;
130 __u64 caps;
131} __packed;
132
133union dm_mem_page_range {
134 struct {
135 /*
136 * The PFN number of the first page in the range.
137 * 40 bits is the architectural limit of a PFN
138 * number for AMD64.
139 */
140 __u64 start_page:40;
141 /*
142 * The number of pages in the range.
143 */
144 __u64 page_cnt:24;
145 } finfo;
146 __u64 page_range;
147} __packed;
148
149
150
151/*
152 * The header for all dynamic memory messages:
153 *
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.
157 */
158
159struct dm_header {
160 __u16 type;
161 __u16 size;
162 __u32 trans_id;
163} __packed;
164
165/*
166 * A generic message format for dynamic memory.
167 * Specific message formats are defined later in the file.
168 */
169
170struct dm_message {
171 struct dm_header hdr;
172 __u8 data[]; /* enclosed message */
173} __packed;
174
175
176/*
177 * Specific message types supporting the dynamic memory protocol.
178 */
179
180/*
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.
184 *
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.
188 */
189
190struct dm_version_request {
191 struct dm_header hdr;
192 union dm_version version;
193 __u32 is_last_attempt:1;
194 __u32 reservedz:31;
195} __packed;
196
197/*
198 * Version response message; Host to Guest and indicates
199 * if the host has accepted the version sent by the guest.
200 *
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.
204 *
205 * reservedz: Reserved field, set to zero.
206 */
207
208struct dm_version_response {
209 struct dm_header hdr;
210 __u64 is_accepted:1;
211 __u64 reservedz:63;
212} __packed;
213
214/*
215 * Message reporting capabilities. This is sent from the guest to the
216 * host.
217 */
218
219struct dm_capabilities {
220 struct dm_header hdr;
221 union dm_caps caps;
222 __u64 min_page_cnt;
223 __u64 max_page_number;
224} __packed;
225
226/*
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
230 * the service.
231 *
232 * is_accepted: Indicates if the host has accepted guest's capabilities.
233 * reservedz: Must be 0.
234 */
235
236struct dm_capabilities_resp_msg {
237 struct dm_header hdr;
238 __u64 is_accepted:1;
239 __u64 reservedz:63;
240} __packed;
241
242/*
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.
246 *
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.
256 *
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.
260 */
261
262struct dm_status {
263 struct dm_header hdr;
264 __u64 num_avail;
265 __u64 num_committed;
266 __u64 page_file_size;
267 __u64 zero_free;
268 __u32 page_file_writes;
269 __u32 io_diff;
270} __packed;
271
272
273/*
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.
277 *
278 * num_pages: number of pages to allocate.
279 */
280
281struct dm_balloon {
282 struct dm_header hdr;
283 __u32 num_pages;
284 __u32 reservedz;
285} __packed;
286
287
288/*
289 * Balloon response message; this message is sent from the guest
290 * to the host in response to the balloon message.
291 *
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.
295 *
296 * range_count: The number of ranges in the range array.
297 *
298 * range_array: An array of page ranges returned to the host.
299 *
300 */
301
302struct dm_balloon_response {
303 struct dm_header hdr;
304 __u32 reservedz;
305 __u32 more_pages:1;
306 __u32 range_count:31;
307 union dm_mem_page_range range_array[];
308} __packed;
309
310/*
311 * Un-balloon message; this message is sent from the host
312 * to the guest to give guest more memory.
313 *
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.
316 *
317 * reservedz: Reserved; must be set to zero.
318 *
319 * range_count: The number of ranges in the range array.
320 *
321 * range_array: An array of page ranges returned to the host.
322 *
323 */
324
325struct dm_unballoon_request {
326 struct dm_header hdr;
327 __u32 more_pages:1;
328 __u32 reservedz:31;
329 __u32 range_count;
330 union dm_mem_page_range range_array[];
331} __packed;
332
333/*
334 * Un-balloon response message; this message is sent from the guest
335 * to the host in response to an unballoon request.
336 *
337 */
338
339struct dm_unballoon_response {
340 struct dm_header hdr;
341} __packed;
342
343
344/*
345 * Hot add request message. Message sent from the host to the guest.
346 *
347 * mem_range: Memory range to hot add.
348 *
349 * On Linux we currently don't support this since we cannot hot add
350 * arbitrary granularity of memory.
351 */
352
353struct dm_hot_add {
354 struct dm_header hdr;
355 union dm_mem_page_range range;
356} __packed;
357
358/*
359 * Hot add response message.
360 * This message is sent by the guest to report the status of a hot add request.
361 * If page_count is less than the requested page count, then the host should
362 * assume all further hot add requests will fail, since this indicates that
363 * the guest has hit an upper physical memory barrier.
364 *
365 * Hot adds may also fail due to low resources; in this case, the guest must
366 * not complete this message until the hot add can succeed, and the host must
367 * not send a new hot add request until the response is sent.
368 * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
369 * times it fails the request.
370 *
371 *
372 * page_count: number of pages that were successfully hot added.
373 *
374 * result: result of the operation 1: success, 0: failure.
375 *
376 */
377
378struct dm_hot_add_response {
379 struct dm_header hdr;
380 __u32 page_count;
381 __u32 result;
382} __packed;
383
384/*
385 * Types of information sent from host to the guest.
386 */
387
388enum dm_info_type {
389 INFO_TYPE_MAX_PAGE_CNT = 0,
390 MAX_INFO_TYPE
391};
392
393
394/*
395 * Header for the information message.
396 */
397
398struct dm_info_header {
399 enum dm_info_type type;
400 __u32 data_size;
401} __packed;
402
403/*
404 * This message is sent from the host to the guest to pass
405 * some relevant information (win8 addition).
406 *
407 * reserved: no used.
408 * info_size: size of the information blob.
409 * info: information blob.
410 */
411
412struct dm_info_msg {
K. Y. Srinivasan6427a0d2012-12-06 11:06:54 -0800413 struct dm_header hdr;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800414 __u32 reserved;
415 __u32 info_size;
416 __u8 info[];
417};
418
419/*
420 * End protocol definitions.
421 */
422
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700423/*
424 * State to manage hot adding memory into the guest.
425 * The range start_pfn : end_pfn specifies the range
426 * that the host has asked us to hot add. The range
427 * start_pfn : ha_end_pfn specifies the range that we have
428 * currently hot added. We hot add in multiples of 128M
429 * chunks; it is possible that we may not be able to bring
430 * online all the pages in the region. The range
431 * covered_start_pfn : covered_end_pfn defines the pages that can
432 * be brough online.
433 */
434
435struct hv_hotadd_state {
436 struct list_head list;
437 unsigned long start_pfn;
438 unsigned long covered_start_pfn;
439 unsigned long covered_end_pfn;
440 unsigned long ha_end_pfn;
441 unsigned long end_pfn;
442};
443
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -0700444struct balloon_state {
445 __u32 num_pages;
446 struct work_struct wrk;
447};
448
K. Y. Srinivasanc51af822013-03-15 12:25:41 -0700449struct hot_add_wrk {
450 union dm_mem_page_range ha_page_range;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700451 union dm_mem_page_range ha_region_range;
K. Y. Srinivasanc51af822013-03-15 12:25:41 -0700452 struct work_struct wrk;
453};
454
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700455static bool hot_add = true;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800456static bool do_hot_add;
K. Y. Srinivasane500d152013-02-08 15:57:15 -0800457/*
458 * Delay reporting memory pressure by
459 * the specified number of seconds.
460 */
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700461static uint pressure_report_delay = 45;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800462
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700463/*
464 * The last time we posted a pressure report to host.
465 */
466static unsigned long last_post_time;
467
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800468module_param(hot_add, bool, (S_IRUGO | S_IWUSR));
469MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
470
K. Y. Srinivasane500d152013-02-08 15:57:15 -0800471module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR));
472MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800473static atomic_t trans_id = ATOMIC_INIT(0);
474
475static int dm_ring_size = (5 * PAGE_SIZE);
476
477/*
478 * Driver specific state.
479 */
480
481enum hv_dm_state {
482 DM_INITIALIZING = 0,
483 DM_INITIALIZED,
484 DM_BALLOON_UP,
485 DM_BALLOON_DOWN,
486 DM_HOT_ADD,
487 DM_INIT_ERROR
488};
489
490
491static __u8 recv_buffer[PAGE_SIZE];
492static __u8 *send_buffer;
493#define PAGES_IN_2M 512
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700494#define HA_CHUNK (32 * 1024)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800495
496struct hv_dynmem_device {
497 struct hv_device *dev;
498 enum hv_dm_state state;
499 struct completion host_event;
500 struct completion config_event;
501
502 /*
503 * Number of pages we have currently ballooned out.
504 */
505 unsigned int num_pages_ballooned;
506
507 /*
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -0700508 * State to manage the ballooning (up) operation.
509 */
510 struct balloon_state balloon_wrk;
511
512 /*
K. Y. Srinivasanc51af822013-03-15 12:25:41 -0700513 * State to execute the "hot-add" operation.
514 */
515 struct hot_add_wrk ha_wrk;
516
517 /*
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700518 * This state tracks if the host has specified a hot-add
519 * region.
520 */
521 bool host_specified_ha_region;
522
523 /*
524 * State to synchronize hot-add.
525 */
526 struct completion ol_waitevent;
527 bool ha_waiting;
528 /*
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -0700529 * This thread handles hot-add
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800530 * requests from the host as well as notifying
531 * the host with regards to memory pressure in
532 * the guest.
533 */
534 struct task_struct *thread;
535
536 /*
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700537 * A list of hot-add regions.
538 */
539 struct list_head ha_region_list;
540
541 /*
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800542 * We start with the highest version we can support
543 * and downgrade based on the host; we save here the
544 * next version to try.
545 */
546 __u32 next_version;
547};
548
549static struct hv_dynmem_device dm_device;
550
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700551static void post_status(struct hv_dynmem_device *dm);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700552#ifdef CONFIG_MEMORY_HOTPLUG
553
Wei Yongjuna6025a22013-03-20 23:25:59 +0800554static void hv_bring_pgs_online(unsigned long start_pfn, unsigned long size)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800555{
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700556 int i;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800557
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700558 for (i = 0; i < size; i++) {
559 struct page *pg;
560 pg = pfn_to_page(start_pfn + i);
561 __online_page_set_limits(pg);
562 __online_page_increment_counters(pg);
563 __online_page_free(pg);
564 }
565}
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800566
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700567static void hv_mem_hot_add(unsigned long start, unsigned long size,
568 unsigned long pfn_count,
569 struct hv_hotadd_state *has)
570{
571 int ret = 0;
K. Y. Srinivasaned07ec92013-07-14 22:38:11 -0700572 int i, nid;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700573 unsigned long start_pfn;
574 unsigned long processed_pfn;
575 unsigned long total_pfn = pfn_count;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800576
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700577 for (i = 0; i < (size/HA_CHUNK); i++) {
578 start_pfn = start + (i * HA_CHUNK);
579 has->ha_end_pfn += HA_CHUNK;
580
581 if (total_pfn > HA_CHUNK) {
582 processed_pfn = HA_CHUNK;
583 total_pfn -= HA_CHUNK;
584 } else {
585 processed_pfn = total_pfn;
586 total_pfn = 0;
587 }
588
589 has->covered_end_pfn += processed_pfn;
590
591 init_completion(&dm_device.ol_waitevent);
592 dm_device.ha_waiting = true;
593
594 nid = memory_add_physaddr_to_nid(PFN_PHYS(start_pfn));
595 ret = add_memory(nid, PFN_PHYS((start_pfn)),
596 (HA_CHUNK << PAGE_SHIFT));
597
598 if (ret) {
599 pr_info("hot_add memory failed error is %d\n", ret);
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -0700600 if (ret == -EEXIST) {
601 /*
602 * This error indicates that the error
603 * is not a transient failure. This is the
604 * case where the guest's physical address map
605 * precludes hot adding memory. Stop all further
606 * memory hot-add.
607 */
608 do_hot_add = false;
609 }
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700610 has->ha_end_pfn -= HA_CHUNK;
611 has->covered_end_pfn -= processed_pfn;
612 break;
613 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800614
615 /*
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700616 * Wait for the memory block to be onlined.
K. Y. Srinivasaned07ec92013-07-14 22:38:11 -0700617 * Since the hot add has succeeded, it is ok to
618 * proceed even if the pages in the hot added region
619 * have not been "onlined" within the allowed time.
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800620 */
K. Y. Srinivasaned07ec92013-07-14 22:38:11 -0700621 wait_for_completion_timeout(&dm_device.ol_waitevent, 5*HZ);
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700622 post_status(&dm_device);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800623 }
624
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700625 return;
626}
627
628static void hv_online_page(struct page *pg)
629{
630 struct list_head *cur;
631 struct hv_hotadd_state *has;
632 unsigned long cur_start_pgp;
633 unsigned long cur_end_pgp;
634
635 if (dm_device.ha_waiting) {
636 dm_device.ha_waiting = false;
637 complete(&dm_device.ol_waitevent);
638 }
639
640 list_for_each(cur, &dm_device.ha_region_list) {
641 has = list_entry(cur, struct hv_hotadd_state, list);
642 cur_start_pgp = (unsigned long)
643 pfn_to_page(has->covered_start_pfn);
644 cur_end_pgp = (unsigned long)pfn_to_page(has->covered_end_pfn);
645
646 if (((unsigned long)pg >= cur_start_pgp) &&
647 ((unsigned long)pg < cur_end_pgp)) {
648 /*
649 * This frame is currently backed; online the
650 * page.
651 */
652 __online_page_set_limits(pg);
653 __online_page_increment_counters(pg);
654 __online_page_free(pg);
655 has->covered_start_pfn++;
656 }
657 }
658}
659
660static bool pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
661{
662 struct list_head *cur;
663 struct hv_hotadd_state *has;
664 unsigned long residual, new_inc;
665
666 if (list_empty(&dm_device.ha_region_list))
667 return false;
668
669 list_for_each(cur, &dm_device.ha_region_list) {
670 has = list_entry(cur, struct hv_hotadd_state, list);
671
672 /*
673 * If the pfn range we are dealing with is not in the current
674 * "hot add block", move on.
675 */
676 if ((start_pfn >= has->end_pfn))
677 continue;
678 /*
679 * If the current hot add-request extends beyond
680 * our current limit; extend it.
681 */
682 if ((start_pfn + pfn_cnt) > has->end_pfn) {
683 residual = (start_pfn + pfn_cnt - has->end_pfn);
684 /*
685 * Extend the region by multiples of HA_CHUNK.
686 */
687 new_inc = (residual / HA_CHUNK) * HA_CHUNK;
688 if (residual % HA_CHUNK)
689 new_inc += HA_CHUNK;
690
691 has->end_pfn += new_inc;
692 }
693
694 /*
695 * If the current start pfn is not where the covered_end
696 * is, update it.
697 */
698
699 if (has->covered_end_pfn != start_pfn) {
700 has->covered_end_pfn = start_pfn;
701 has->covered_start_pfn = start_pfn;
702 }
703 return true;
704
705 }
706
707 return false;
708}
709
710static unsigned long handle_pg_range(unsigned long pg_start,
711 unsigned long pg_count)
712{
713 unsigned long start_pfn = pg_start;
714 unsigned long pfn_cnt = pg_count;
715 unsigned long size;
716 struct list_head *cur;
717 struct hv_hotadd_state *has;
718 unsigned long pgs_ol = 0;
719 unsigned long old_covered_state;
720
721 if (list_empty(&dm_device.ha_region_list))
722 return 0;
723
724 list_for_each(cur, &dm_device.ha_region_list) {
725 has = list_entry(cur, struct hv_hotadd_state, list);
726
727 /*
728 * If the pfn range we are dealing with is not in the current
729 * "hot add block", move on.
730 */
731 if ((start_pfn >= has->end_pfn))
732 continue;
733
734 old_covered_state = has->covered_end_pfn;
735
736 if (start_pfn < has->ha_end_pfn) {
737 /*
738 * This is the case where we are backing pages
739 * in an already hot added region. Bring
740 * these pages online first.
741 */
742 pgs_ol = has->ha_end_pfn - start_pfn;
743 if (pgs_ol > pfn_cnt)
744 pgs_ol = pfn_cnt;
745 hv_bring_pgs_online(start_pfn, pgs_ol);
746 has->covered_end_pfn += pgs_ol;
747 has->covered_start_pfn += pgs_ol;
748 pfn_cnt -= pgs_ol;
749 }
750
751 if ((has->ha_end_pfn < has->end_pfn) && (pfn_cnt > 0)) {
752 /*
753 * We have some residual hot add range
754 * that needs to be hot added; hot add
755 * it now. Hot add a multiple of
756 * of HA_CHUNK that fully covers the pages
757 * we have.
758 */
759 size = (has->end_pfn - has->ha_end_pfn);
760 if (pfn_cnt <= size) {
761 size = ((pfn_cnt / HA_CHUNK) * HA_CHUNK);
762 if (pfn_cnt % HA_CHUNK)
763 size += HA_CHUNK;
764 } else {
765 pfn_cnt = size;
766 }
767 hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
768 }
769 /*
770 * If we managed to online any pages that were given to us,
771 * we declare success.
772 */
773 return has->covered_end_pfn - old_covered_state;
774
775 }
776
777 return 0;
778}
779
780static unsigned long process_hot_add(unsigned long pg_start,
781 unsigned long pfn_cnt,
782 unsigned long rg_start,
783 unsigned long rg_size)
784{
785 struct hv_hotadd_state *ha_region = NULL;
786
787 if (pfn_cnt == 0)
788 return 0;
789
790 if (!dm_device.host_specified_ha_region)
791 if (pfn_covered(pg_start, pfn_cnt))
792 goto do_pg_range;
793
794 /*
795 * If the host has specified a hot-add range; deal with it first.
796 */
797
K. Y. Srinivasan647965a2013-03-29 07:36:11 -0700798 if (rg_size != 0) {
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700799 ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
800 if (!ha_region)
801 return 0;
802
803 INIT_LIST_HEAD(&ha_region->list);
804
805 list_add_tail(&ha_region->list, &dm_device.ha_region_list);
806 ha_region->start_pfn = rg_start;
807 ha_region->ha_end_pfn = rg_start;
808 ha_region->covered_start_pfn = pg_start;
809 ha_region->covered_end_pfn = pg_start;
810 ha_region->end_pfn = rg_start + rg_size;
811 }
812
813do_pg_range:
814 /*
815 * Process the page range specified; bringing them
816 * online if possible.
817 */
818 return handle_pg_range(pg_start, pfn_cnt);
819}
820
821#endif
822
823static void hot_add_req(struct work_struct *dummy)
824{
825 struct dm_hot_add_response resp;
826#ifdef CONFIG_MEMORY_HOTPLUG
827 unsigned long pg_start, pfn_cnt;
828 unsigned long rg_start, rg_sz;
829#endif
830 struct hv_dynmem_device *dm = &dm_device;
831
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800832 memset(&resp, 0, sizeof(struct dm_hot_add_response));
833 resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
834 resp.hdr.size = sizeof(struct dm_hot_add_response);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800835
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700836#ifdef CONFIG_MEMORY_HOTPLUG
837 pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
838 pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800839
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700840 rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
841 rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
842
843 if ((rg_start == 0) && (!dm->host_specified_ha_region)) {
844 unsigned long region_size;
845 unsigned long region_start;
846
847 /*
848 * The host has not specified the hot-add region.
849 * Based on the hot-add page range being specified,
850 * compute a hot-add region that can cover the pages
851 * that need to be hot-added while ensuring the alignment
852 * and size requirements of Linux as it relates to hot-add.
853 */
854 region_start = pg_start;
855 region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK;
856 if (pfn_cnt % HA_CHUNK)
857 region_size += HA_CHUNK;
858
859 region_start = (pg_start / HA_CHUNK) * HA_CHUNK;
860
861 rg_start = region_start;
862 rg_sz = region_size;
863 }
864
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -0700865 if (do_hot_add)
866 resp.page_count = process_hot_add(pg_start, pfn_cnt,
867 rg_start, rg_sz);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700868#endif
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -0700869 /*
870 * The result field of the response structure has the
871 * following semantics:
872 *
873 * 1. If all or some pages hot-added: Guest should return success.
874 *
875 * 2. If no pages could be hot-added:
876 *
877 * If the guest returns success, then the host
878 * will not attempt any further hot-add operations. This
879 * signifies a permanent failure.
880 *
881 * If the guest returns failure, then this failure will be
882 * treated as a transient failure and the host may retry the
883 * hot-add operation after some delay.
884 */
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700885 if (resp.page_count > 0)
886 resp.result = 1;
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -0700887 else if (!do_hot_add)
888 resp.result = 1;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700889 else
890 resp.result = 0;
891
892 if (!do_hot_add || (resp.page_count == 0))
893 pr_info("Memory hot add failed\n");
894
895 dm->state = DM_INITIALIZED;
K. Y. Srinivasan20138d6c2013-07-17 17:27:27 -0700896 resp.hdr.trans_id = atomic_inc_return(&trans_id);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700897 vmbus_sendpacket(dm->dev->channel, &resp,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800898 sizeof(struct dm_hot_add_response),
899 (unsigned long)NULL,
900 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800901}
902
903static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
904{
K. Y. Srinivasan6427a0d2012-12-06 11:06:54 -0800905 struct dm_info_header *info_hdr;
906
907 info_hdr = (struct dm_info_header *)msg->info;
908
909 switch (info_hdr->type) {
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800910 case INFO_TYPE_MAX_PAGE_CNT:
911 pr_info("Received INFO_TYPE_MAX_PAGE_CNT\n");
K. Y. Srinivasan6427a0d2012-12-06 11:06:54 -0800912 pr_info("Data Size is %d\n", info_hdr->data_size);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800913 break;
914 default:
K. Y. Srinivasan6427a0d2012-12-06 11:06:54 -0800915 pr_info("Received Unknown type: %d\n", info_hdr->type);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800916 }
917}
918
Wei Yongjuna6025a22013-03-20 23:25:59 +0800919static unsigned long compute_balloon_floor(void)
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -0800920{
921 unsigned long min_pages;
922#define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
923 /* Simple continuous piecewiese linear function:
924 * max MiB -> min MiB gradient
925 * 0 0
926 * 16 16
927 * 32 24
928 * 128 72 (1/2)
929 * 512 168 (1/4)
930 * 2048 360 (1/8)
931 * 8192 552 (1/32)
932 * 32768 1320
933 * 131072 4392
934 */
935 if (totalram_pages < MB2PAGES(128))
936 min_pages = MB2PAGES(8) + (totalram_pages >> 1);
937 else if (totalram_pages < MB2PAGES(512))
938 min_pages = MB2PAGES(40) + (totalram_pages >> 2);
939 else if (totalram_pages < MB2PAGES(2048))
940 min_pages = MB2PAGES(104) + (totalram_pages >> 3);
941 else
942 min_pages = MB2PAGES(296) + (totalram_pages >> 5);
943#undef MB2PAGES
944 return min_pages;
945}
946
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800947/*
948 * Post our status as it relates memory pressure to the
949 * host. Host expects the guests to post this status
950 * periodically at 1 second intervals.
951 *
952 * The metrics specified in this protocol are very Windows
953 * specific and so we cook up numbers here to convey our memory
954 * pressure.
955 */
956
957static void post_status(struct hv_dynmem_device *dm)
958{
959 struct dm_status status;
K. Y. Srinivasan07315722013-01-25 16:18:47 -0800960 struct sysinfo val;
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700961 unsigned long now = jiffies;
962 unsigned long last_post = last_post_time;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800963
K. Y. Srinivasane500d152013-02-08 15:57:15 -0800964 if (pressure_report_delay > 0) {
965 --pressure_report_delay;
966 return;
967 }
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700968
969 if (!time_after(now, (last_post_time + HZ)))
970 return;
971
K. Y. Srinivasan07315722013-01-25 16:18:47 -0800972 si_meminfo(&val);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800973 memset(&status, 0, sizeof(struct dm_status));
974 status.hdr.type = DM_STATUS_REPORT;
975 status.hdr.size = sizeof(struct dm_status);
976 status.hdr.trans_id = atomic_inc_return(&trans_id);
977
K. Y. Srinivasan07315722013-01-25 16:18:47 -0800978 /*
979 * The host expects the guest to report free memory.
980 * Further, the host expects the pressure information to
981 * include the ballooned out pages.
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -0800982 * For a given amount of memory that we are managing, we
983 * need to compute a floor below which we should not balloon.
984 * Compute this and add it to the pressure report.
K. Y. Srinivasan07315722013-01-25 16:18:47 -0800985 */
986 status.num_avail = val.freeram;
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -0800987 status.num_committed = vm_memory_committed() +
988 dm->num_pages_ballooned +
989 compute_balloon_floor();
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800990
K. Y. Srinivasanc5e22542013-07-14 22:38:12 -0700991 /*
992 * If our transaction ID is no longer current, just don't
993 * send the status. This can happen if we were interrupted
994 * after we picked our transaction ID.
995 */
996 if (status.hdr.trans_id != atomic_read(&trans_id))
997 return;
998
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700999 /*
1000 * If the last post time that we sampled has changed,
1001 * we have raced, don't post the status.
1002 */
1003 if (last_post != last_post_time)
1004 return;
1005
1006 last_post_time = jiffies;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001007 vmbus_sendpacket(dm->dev->channel, &status,
1008 sizeof(struct dm_status),
1009 (unsigned long)NULL,
1010 VM_PKT_DATA_INBAND, 0);
1011
1012}
1013
Greg Kroah-Hartman989623c2012-11-21 12:46:40 -08001014static void free_balloon_pages(struct hv_dynmem_device *dm,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001015 union dm_mem_page_range *range_array)
1016{
1017 int num_pages = range_array->finfo.page_cnt;
1018 __u64 start_frame = range_array->finfo.start_page;
1019 struct page *pg;
1020 int i;
1021
1022 for (i = 0; i < num_pages; i++) {
1023 pg = pfn_to_page(i + start_frame);
1024 __free_page(pg);
1025 dm->num_pages_ballooned--;
1026 }
1027}
1028
1029
1030
1031static int alloc_balloon_pages(struct hv_dynmem_device *dm, int num_pages,
1032 struct dm_balloon_response *bl_resp, int alloc_unit,
1033 bool *alloc_error)
1034{
1035 int i = 0;
1036 struct page *pg;
1037
1038 if (num_pages < alloc_unit)
1039 return 0;
1040
1041 for (i = 0; (i * alloc_unit) < num_pages; i++) {
1042 if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
1043 PAGE_SIZE)
1044 return i * alloc_unit;
1045
1046 /*
1047 * We execute this code in a thread context. Furthermore,
1048 * we don't want the kernel to try too hard.
1049 */
1050 pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
1051 __GFP_NOMEMALLOC | __GFP_NOWARN,
1052 get_order(alloc_unit << PAGE_SHIFT));
1053
1054 if (!pg) {
1055 *alloc_error = true;
1056 return i * alloc_unit;
1057 }
1058
1059
1060 dm->num_pages_ballooned += alloc_unit;
1061
K. Y. Srinivasanf766dc12013-03-18 13:51:37 -07001062 /*
1063 * If we allocatted 2M pages; split them so we
1064 * can free them in any order we get.
1065 */
1066
1067 if (alloc_unit != 1)
1068 split_page(pg, get_order(alloc_unit << PAGE_SHIFT));
1069
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001070 bl_resp->range_count++;
1071 bl_resp->range_array[i].finfo.start_page =
1072 page_to_pfn(pg);
1073 bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
1074 bl_resp->hdr.size += sizeof(union dm_mem_page_range);
1075
1076 }
1077
1078 return num_pages;
1079}
1080
1081
1082
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001083static void balloon_up(struct work_struct *dummy)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001084{
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001085 int num_pages = dm_device.balloon_wrk.num_pages;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001086 int num_ballooned = 0;
1087 struct dm_balloon_response *bl_resp;
1088 int alloc_unit;
1089 int ret;
Dexuan Cuif6712232014-11-24 20:32:43 -08001090 bool alloc_error;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001091 bool done = false;
1092 int i;
1093
Dexuan Cuif6712232014-11-24 20:32:43 -08001094 /* The host balloons pages in 2M granularity. */
1095 WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001096
1097 /*
K. Y. Srinivasanf766dc12013-03-18 13:51:37 -07001098 * We will attempt 2M allocations. However, if we fail to
1099 * allocate 2M chunks, we will go back to 4k allocations.
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001100 */
K. Y. Srinivasanf766dc12013-03-18 13:51:37 -07001101 alloc_unit = 512;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001102
1103 while (!done) {
1104 bl_resp = (struct dm_balloon_response *)send_buffer;
1105 memset(send_buffer, 0, PAGE_SIZE);
1106 bl_resp->hdr.type = DM_BALLOON_RESPONSE;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001107 bl_resp->hdr.size = sizeof(struct dm_balloon_response);
1108 bl_resp->more_pages = 1;
1109
1110
1111 num_pages -= num_ballooned;
Dexuan Cuif6712232014-11-24 20:32:43 -08001112 alloc_error = false;
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001113 num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001114 bl_resp, alloc_unit,
1115 &alloc_error);
1116
Dexuan Cuif6712232014-11-24 20:32:43 -08001117 if (alloc_unit != 1 && num_ballooned == 0) {
K. Y. Srinivasanf766dc12013-03-18 13:51:37 -07001118 alloc_unit = 1;
1119 continue;
1120 }
1121
Dexuan Cuif6712232014-11-24 20:32:43 -08001122 if ((alloc_unit == 1 && alloc_error) ||
1123 (num_ballooned == num_pages)) {
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001124 bl_resp->more_pages = 0;
1125 done = true;
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001126 dm_device.state = DM_INITIALIZED;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001127 }
1128
1129 /*
1130 * We are pushing a lot of data through the channel;
1131 * deal with transient failures caused because of the
1132 * lack of space in the ring buffer.
1133 */
1134
1135 do {
K. Y. Srinivasan20138d6c2013-07-17 17:27:27 -07001136 bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001137 ret = vmbus_sendpacket(dm_device.dev->channel,
1138 bl_resp,
1139 bl_resp->hdr.size,
1140 (unsigned long)NULL,
1141 VM_PKT_DATA_INBAND, 0);
1142
1143 if (ret == -EAGAIN)
1144 msleep(20);
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001145 post_status(&dm_device);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001146 } while (ret == -EAGAIN);
1147
1148 if (ret) {
1149 /*
1150 * Free up the memory we allocatted.
1151 */
1152 pr_info("Balloon response failed\n");
1153
1154 for (i = 0; i < bl_resp->range_count; i++)
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001155 free_balloon_pages(&dm_device,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001156 &bl_resp->range_array[i]);
1157
1158 done = true;
1159 }
1160 }
1161
1162}
1163
1164static void balloon_down(struct hv_dynmem_device *dm,
1165 struct dm_unballoon_request *req)
1166{
1167 union dm_mem_page_range *range_array = req->range_array;
1168 int range_count = req->range_count;
1169 struct dm_unballoon_response resp;
1170 int i;
1171
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001172 for (i = 0; i < range_count; i++) {
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001173 free_balloon_pages(dm, &range_array[i]);
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001174 post_status(&dm_device);
1175 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001176
1177 if (req->more_pages == 1)
1178 return;
1179
1180 memset(&resp, 0, sizeof(struct dm_unballoon_response));
1181 resp.hdr.type = DM_UNBALLOON_RESPONSE;
1182 resp.hdr.trans_id = atomic_inc_return(&trans_id);
1183 resp.hdr.size = sizeof(struct dm_unballoon_response);
1184
1185 vmbus_sendpacket(dm_device.dev->channel, &resp,
1186 sizeof(struct dm_unballoon_response),
1187 (unsigned long)NULL,
1188 VM_PKT_DATA_INBAND, 0);
1189
1190 dm->state = DM_INITIALIZED;
1191}
1192
1193static void balloon_onchannelcallback(void *context);
1194
1195static int dm_thread_func(void *dm_dev)
1196{
1197 struct hv_dynmem_device *dm = dm_dev;
1198 int t;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001199
1200 while (!kthread_should_stop()) {
K. Y. Srinivasan5dba4c52014-02-13 16:24:33 -08001201 t = wait_for_completion_interruptible_timeout(
1202 &dm_device.config_event, 1*HZ);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001203 /*
1204 * The host expects us to post information on the memory
1205 * pressure every second.
1206 */
1207
1208 if (t == 0)
1209 post_status(dm);
1210
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001211 }
1212
1213 return 0;
1214}
1215
1216
1217static void version_resp(struct hv_dynmem_device *dm,
1218 struct dm_version_response *vresp)
1219{
1220 struct dm_version_request version_req;
1221 int ret;
1222
1223 if (vresp->is_accepted) {
1224 /*
1225 * We are done; wakeup the
1226 * context waiting for version
1227 * negotiation.
1228 */
1229 complete(&dm->host_event);
1230 return;
1231 }
1232 /*
1233 * If there are more versions to try, continue
1234 * with negotiations; if not
1235 * shutdown the service since we are not able
1236 * to negotiate a suitable version number
1237 * with the host.
1238 */
1239 if (dm->next_version == 0)
1240 goto version_error;
1241
1242 dm->next_version = 0;
1243 memset(&version_req, 0, sizeof(struct dm_version_request));
1244 version_req.hdr.type = DM_VERSION_REQUEST;
1245 version_req.hdr.size = sizeof(struct dm_version_request);
1246 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
1247 version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN7;
1248 version_req.is_last_attempt = 1;
1249
1250 ret = vmbus_sendpacket(dm->dev->channel, &version_req,
1251 sizeof(struct dm_version_request),
1252 (unsigned long)NULL,
1253 VM_PKT_DATA_INBAND, 0);
1254
1255 if (ret)
1256 goto version_error;
1257
1258 return;
1259
1260version_error:
1261 dm->state = DM_INIT_ERROR;
1262 complete(&dm->host_event);
1263}
1264
1265static void cap_resp(struct hv_dynmem_device *dm,
1266 struct dm_capabilities_resp_msg *cap_resp)
1267{
1268 if (!cap_resp->is_accepted) {
1269 pr_info("Capabilities not accepted by host\n");
1270 dm->state = DM_INIT_ERROR;
1271 }
1272 complete(&dm->host_event);
1273}
1274
1275static void balloon_onchannelcallback(void *context)
1276{
1277 struct hv_device *dev = context;
1278 u32 recvlen;
1279 u64 requestid;
1280 struct dm_message *dm_msg;
1281 struct dm_header *dm_hdr;
1282 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001283 struct dm_balloon *bal_msg;
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001284 struct dm_hot_add *ha_msg;
1285 union dm_mem_page_range *ha_pg_range;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001286 union dm_mem_page_range *ha_region;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001287
1288 memset(recv_buffer, 0, sizeof(recv_buffer));
1289 vmbus_recvpacket(dev->channel, recv_buffer,
1290 PAGE_SIZE, &recvlen, &requestid);
1291
1292 if (recvlen > 0) {
1293 dm_msg = (struct dm_message *)recv_buffer;
1294 dm_hdr = &dm_msg->hdr;
1295
1296 switch (dm_hdr->type) {
1297 case DM_VERSION_RESPONSE:
1298 version_resp(dm,
1299 (struct dm_version_response *)dm_msg);
1300 break;
1301
1302 case DM_CAPABILITIES_RESPONSE:
1303 cap_resp(dm,
1304 (struct dm_capabilities_resp_msg *)dm_msg);
1305 break;
1306
1307 case DM_BALLOON_REQUEST:
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001308 if (dm->state == DM_BALLOON_UP)
1309 pr_warn("Currently ballooning\n");
1310 bal_msg = (struct dm_balloon *)recv_buffer;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001311 dm->state = DM_BALLOON_UP;
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001312 dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
1313 schedule_work(&dm_device.balloon_wrk.wrk);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001314 break;
1315
1316 case DM_UNBALLOON_REQUEST:
1317 dm->state = DM_BALLOON_DOWN;
1318 balloon_down(dm,
1319 (struct dm_unballoon_request *)recv_buffer);
1320 break;
1321
1322 case DM_MEM_HOT_ADD_REQUEST:
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001323 if (dm->state == DM_HOT_ADD)
1324 pr_warn("Currently hot-adding\n");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001325 dm->state = DM_HOT_ADD;
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001326 ha_msg = (struct dm_hot_add *)recv_buffer;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001327 if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
1328 /*
1329 * This is a normal hot-add request specifying
1330 * hot-add memory.
1331 */
1332 ha_pg_range = &ha_msg->range;
1333 dm->ha_wrk.ha_page_range = *ha_pg_range;
1334 dm->ha_wrk.ha_region_range.page_range = 0;
1335 } else {
1336 /*
1337 * Host is specifying that we first hot-add
1338 * a region and then partially populate this
1339 * region.
1340 */
1341 dm->host_specified_ha_region = true;
1342 ha_pg_range = &ha_msg->range;
1343 ha_region = &ha_pg_range[1];
1344 dm->ha_wrk.ha_page_range = *ha_pg_range;
1345 dm->ha_wrk.ha_region_range = *ha_region;
1346 }
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001347 schedule_work(&dm_device.ha_wrk.wrk);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001348 break;
1349
1350 case DM_INFO_MESSAGE:
1351 process_info(dm, (struct dm_info_msg *)dm_msg);
1352 break;
1353
1354 default:
1355 pr_err("Unhandled message: type: %d\n", dm_hdr->type);
1356
1357 }
1358 }
1359
1360}
1361
1362static int balloon_probe(struct hv_device *dev,
1363 const struct hv_vmbus_device_id *dev_id)
1364{
1365 int ret, t;
1366 struct dm_version_request version_req;
1367 struct dm_capabilities cap_msg;
1368
1369 do_hot_add = hot_add;
1370
1371 /*
1372 * First allocate a send buffer.
1373 */
1374
1375 send_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
1376 if (!send_buffer)
1377 return -ENOMEM;
1378
1379 ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
1380 balloon_onchannelcallback, dev);
1381
1382 if (ret)
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001383 goto probe_error0;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001384
1385 dm_device.dev = dev;
1386 dm_device.state = DM_INITIALIZING;
1387 dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
1388 init_completion(&dm_device.host_event);
1389 init_completion(&dm_device.config_event);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001390 INIT_LIST_HEAD(&dm_device.ha_region_list);
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001391 INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001392 INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001393 dm_device.host_specified_ha_region = false;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001394
1395 dm_device.thread =
1396 kthread_run(dm_thread_func, &dm_device, "hv_balloon");
1397 if (IS_ERR(dm_device.thread)) {
1398 ret = PTR_ERR(dm_device.thread);
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001399 goto probe_error1;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001400 }
1401
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001402#ifdef CONFIG_MEMORY_HOTPLUG
1403 set_online_page_callback(&hv_online_page);
1404#endif
1405
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001406 hv_set_drvdata(dev, &dm_device);
1407 /*
1408 * Initiate the hand shake with the host and negotiate
1409 * a version that the host can support. We start with the
1410 * highest version number and go down if the host cannot
1411 * support it.
1412 */
1413 memset(&version_req, 0, sizeof(struct dm_version_request));
1414 version_req.hdr.type = DM_VERSION_REQUEST;
1415 version_req.hdr.size = sizeof(struct dm_version_request);
1416 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
1417 version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN8;
1418 version_req.is_last_attempt = 0;
1419
1420 ret = vmbus_sendpacket(dev->channel, &version_req,
1421 sizeof(struct dm_version_request),
1422 (unsigned long)NULL,
K. Y. Srinivasan7a64b862013-03-15 12:25:39 -07001423 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001424 if (ret)
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001425 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001426
1427 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1428 if (t == 0) {
1429 ret = -ETIMEDOUT;
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001430 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001431 }
1432
1433 /*
1434 * If we could not negotiate a compatible version with the host
1435 * fail the probe function.
1436 */
1437 if (dm_device.state == DM_INIT_ERROR) {
1438 ret = -ETIMEDOUT;
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001439 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001440 }
1441 /*
1442 * Now submit our capabilities to the host.
1443 */
1444 memset(&cap_msg, 0, sizeof(struct dm_capabilities));
1445 cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
1446 cap_msg.hdr.size = sizeof(struct dm_capabilities);
1447 cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
1448
1449 cap_msg.caps.cap_bits.balloon = 1;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001450 cap_msg.caps.cap_bits.hot_add = 1;
1451
1452 /*
K. Y. Srinivasan647965a2013-03-29 07:36:11 -07001453 * Specify our alignment requirements as it relates
1454 * memory hot-add. Specify 128MB alignment.
1455 */
1456 cap_msg.caps.cap_bits.hot_add_alignment = 7;
1457
1458 /*
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001459 * Currently the host does not use these
1460 * values and we set them to what is done in the
1461 * Windows driver.
1462 */
1463 cap_msg.min_page_cnt = 0;
1464 cap_msg.max_page_number = -1;
1465
1466 ret = vmbus_sendpacket(dev->channel, &cap_msg,
1467 sizeof(struct dm_capabilities),
1468 (unsigned long)NULL,
K. Y. Srinivasan7a64b862013-03-15 12:25:39 -07001469 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001470 if (ret)
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001471 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001472
1473 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1474 if (t == 0) {
1475 ret = -ETIMEDOUT;
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001476 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001477 }
1478
1479 /*
1480 * If the host does not like our capabilities,
1481 * fail the probe function.
1482 */
1483 if (dm_device.state == DM_INIT_ERROR) {
1484 ret = -ETIMEDOUT;
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001485 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001486 }
1487
1488 dm_device.state = DM_INITIALIZED;
1489
1490 return 0;
1491
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001492probe_error2:
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001493#ifdef CONFIG_MEMORY_HOTPLUG
1494 restore_online_page_callback(&hv_online_page);
1495#endif
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001496 kthread_stop(dm_device.thread);
1497
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001498probe_error1:
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001499 vmbus_close(dev->channel);
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001500probe_error0:
1501 kfree(send_buffer);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001502 return ret;
1503}
1504
1505static int balloon_remove(struct hv_device *dev)
1506{
1507 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001508 struct list_head *cur, *tmp;
1509 struct hv_hotadd_state *has;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001510
1511 if (dm->num_pages_ballooned != 0)
1512 pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
1513
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001514 cancel_work_sync(&dm->balloon_wrk.wrk);
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001515 cancel_work_sync(&dm->ha_wrk.wrk);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001516
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001517 vmbus_close(dev->channel);
1518 kthread_stop(dm->thread);
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001519 kfree(send_buffer);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001520#ifdef CONFIG_MEMORY_HOTPLUG
1521 restore_online_page_callback(&hv_online_page);
1522#endif
1523 list_for_each_safe(cur, tmp, &dm->ha_region_list) {
1524 has = list_entry(cur, struct hv_hotadd_state, list);
1525 list_del(&has->list);
1526 kfree(has);
1527 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001528
1529 return 0;
1530}
1531
1532static const struct hv_vmbus_device_id id_table[] = {
1533 /* Dynamic Memory Class ID */
1534 /* 525074DC-8985-46e2-8057-A307DC18A502 */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -08001535 { HV_DM_GUID, },
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001536 { },
1537};
1538
1539MODULE_DEVICE_TABLE(vmbus, id_table);
1540
1541static struct hv_driver balloon_drv = {
1542 .name = "hv_balloon",
1543 .id_table = id_table,
1544 .probe = balloon_probe,
1545 .remove = balloon_remove,
1546};
1547
1548static int __init init_balloon_drv(void)
1549{
1550
1551 return vmbus_driver_register(&balloon_drv);
1552}
1553
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001554module_init(init_balloon_drv);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001555
1556MODULE_DESCRIPTION("Hyper-V Balloon");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001557MODULE_LICENSE("GPL");