Drivers: hv: balloon: Make the balloon driver not unloadable
[deliverable/linux.git] / drivers / hv / hv_balloon.c
CommitLineData
9aa8b50b
S
1/*
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>
22#include <linux/mman.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/kthread.h>
28#include <linux/completion.h>
29#include <linux/memory_hotplug.h>
30#include <linux/memory.h>
31#include <linux/notifier.h>
9aa8b50b
S
32#include <linux/percpu_counter.h>
33
34#include <linux/hyperv.h>
35
36/*
37 * We begin with definitions supporting the Dynamic Memory protocol
38 * with the host.
39 *
40 * Begin protocol definitions.
41 */
42
43
44
45/*
46 * Protocol versions. The low word is the minor version, the high word the major
47 * version.
48 *
49 * History:
50 * Initial version 1.0
51 * Changed to 0.1 on 2009/03/25
52 * Changes to 0.2 on 2009/05/14
53 * Changes to 0.3 on 2009/12/03
54 * Changed to 1.0 on 2011/04/05
55 */
56
57#define DYNMEM_MAKE_VERSION(Major, Minor) ((__u32)(((Major) << 16) | (Minor)))
58#define DYNMEM_MAJOR_VERSION(Version) ((__u32)(Version) >> 16)
59#define DYNMEM_MINOR_VERSION(Version) ((__u32)(Version) & 0xff)
60
61enum {
62 DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
63 DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
64
65 DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
66 DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
67
68 DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN8
69};
70
71
72
73/*
74 * Message Types
75 */
76
77enum 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
107union dm_version {
108 struct {
109 __u16 minor_version;
110 __u16 major_version;
111 };
112 __u32 version;
113} __packed;
114
115
116union dm_caps {
117 struct {
118 __u64 balloon:1;
119 __u64 hot_add:1;
120 __u64 reservedz:62;
121 } cap_bits;
122 __u64 caps;
123} __packed;
124
125union dm_mem_page_range {
126 struct {
127 /*
128 * The PFN number of the first page in the range.
129 * 40 bits is the architectural limit of a PFN
130 * number for AMD64.
131 */
132 __u64 start_page:40;
133 /*
134 * The number of pages in the range.
135 */
136 __u64 page_cnt:24;
137 } finfo;
138 __u64 page_range;
139} __packed;
140
141
142
143/*
144 * The header for all dynamic memory messages:
145 *
146 * type: Type of the message.
147 * size: Size of the message in bytes; including the header.
148 * trans_id: The guest is responsible for manufacturing this ID.
149 */
150
151struct dm_header {
152 __u16 type;
153 __u16 size;
154 __u32 trans_id;
155} __packed;
156
157/*
158 * A generic message format for dynamic memory.
159 * Specific message formats are defined later in the file.
160 */
161
162struct dm_message {
163 struct dm_header hdr;
164 __u8 data[]; /* enclosed message */
165} __packed;
166
167
168/*
169 * Specific message types supporting the dynamic memory protocol.
170 */
171
172/*
173 * Version negotiation message. Sent from the guest to the host.
174 * The guest is free to try different versions until the host
175 * accepts the version.
176 *
177 * dm_version: The protocol version requested.
178 * is_last_attempt: If TRUE, this is the last version guest will request.
179 * reservedz: Reserved field, set to zero.
180 */
181
182struct dm_version_request {
183 struct dm_header hdr;
184 union dm_version version;
185 __u32 is_last_attempt:1;
186 __u32 reservedz:31;
187} __packed;
188
189/*
190 * Version response message; Host to Guest and indicates
191 * if the host has accepted the version sent by the guest.
192 *
193 * is_accepted: If TRUE, host has accepted the version and the guest
194 * should proceed to the next stage of the protocol. FALSE indicates that
195 * guest should re-try with a different version.
196 *
197 * reservedz: Reserved field, set to zero.
198 */
199
200struct dm_version_response {
201 struct dm_header hdr;
202 __u64 is_accepted:1;
203 __u64 reservedz:63;
204} __packed;
205
206/*
207 * Message reporting capabilities. This is sent from the guest to the
208 * host.
209 */
210
211struct dm_capabilities {
212 struct dm_header hdr;
213 union dm_caps caps;
214 __u64 min_page_cnt;
215 __u64 max_page_number;
216} __packed;
217
218/*
219 * Response to the capabilities message. This is sent from the host to the
220 * guest. This message notifies if the host has accepted the guest's
221 * capabilities. If the host has not accepted, the guest must shutdown
222 * the service.
223 *
224 * is_accepted: Indicates if the host has accepted guest's capabilities.
225 * reservedz: Must be 0.
226 */
227
228struct dm_capabilities_resp_msg {
229 struct dm_header hdr;
230 __u64 is_accepted:1;
231 __u64 reservedz:63;
232} __packed;
233
234/*
235 * This message is used to report memory pressure from the guest.
236 * This message is not part of any transaction and there is no
237 * response to this message.
238 *
239 * num_avail: Available memory in pages.
240 * num_committed: Committed memory in pages.
241 * page_file_size: The accumulated size of all page files
242 * in the system in pages.
243 * zero_free: The nunber of zero and free pages.
244 * page_file_writes: The writes to the page file in pages.
245 * io_diff: An indicator of file cache efficiency or page file activity,
246 * calculated as File Cache Page Fault Count - Page Read Count.
247 * This value is in pages.
248 *
249 * Some of these metrics are Windows specific and fortunately
250 * the algorithm on the host side that computes the guest memory
251 * pressure only uses num_committed value.
252 */
253
254struct dm_status {
255 struct dm_header hdr;
256 __u64 num_avail;
257 __u64 num_committed;
258 __u64 page_file_size;
259 __u64 zero_free;
260 __u32 page_file_writes;
261 __u32 io_diff;
262} __packed;
263
264
265/*
266 * Message to ask the guest to allocate memory - balloon up message.
267 * This message is sent from the host to the guest. The guest may not be
268 * able to allocate as much memory as requested.
269 *
270 * num_pages: number of pages to allocate.
271 */
272
273struct dm_balloon {
274 struct dm_header hdr;
275 __u32 num_pages;
276 __u32 reservedz;
277} __packed;
278
279
280/*
281 * Balloon response message; this message is sent from the guest
282 * to the host in response to the balloon message.
283 *
284 * reservedz: Reserved; must be set to zero.
285 * more_pages: If FALSE, this is the last message of the transaction.
286 * if TRUE there will atleast one more message from the guest.
287 *
288 * range_count: The number of ranges in the range array.
289 *
290 * range_array: An array of page ranges returned to the host.
291 *
292 */
293
294struct dm_balloon_response {
295 struct dm_header hdr;
296 __u32 reservedz;
297 __u32 more_pages:1;
298 __u32 range_count:31;
299 union dm_mem_page_range range_array[];
300} __packed;
301
302/*
303 * Un-balloon message; this message is sent from the host
304 * to the guest to give guest more memory.
305 *
306 * more_pages: If FALSE, this is the last message of the transaction.
307 * if TRUE there will atleast one more message from the guest.
308 *
309 * reservedz: Reserved; must be set to zero.
310 *
311 * range_count: The number of ranges in the range array.
312 *
313 * range_array: An array of page ranges returned to the host.
314 *
315 */
316
317struct dm_unballoon_request {
318 struct dm_header hdr;
319 __u32 more_pages:1;
320 __u32 reservedz:31;
321 __u32 range_count;
322 union dm_mem_page_range range_array[];
323} __packed;
324
325/*
326 * Un-balloon response message; this message is sent from the guest
327 * to the host in response to an unballoon request.
328 *
329 */
330
331struct dm_unballoon_response {
332 struct dm_header hdr;
333} __packed;
334
335
336/*
337 * Hot add request message. Message sent from the host to the guest.
338 *
339 * mem_range: Memory range to hot add.
340 *
341 * On Linux we currently don't support this since we cannot hot add
342 * arbitrary granularity of memory.
343 */
344
345struct dm_hot_add {
346 struct dm_header hdr;
347 union dm_mem_page_range range;
348} __packed;
349
350/*
351 * Hot add response message.
352 * This message is sent by the guest to report the status of a hot add request.
353 * If page_count is less than the requested page count, then the host should
354 * assume all further hot add requests will fail, since this indicates that
355 * the guest has hit an upper physical memory barrier.
356 *
357 * Hot adds may also fail due to low resources; in this case, the guest must
358 * not complete this message until the hot add can succeed, and the host must
359 * not send a new hot add request until the response is sent.
360 * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
361 * times it fails the request.
362 *
363 *
364 * page_count: number of pages that were successfully hot added.
365 *
366 * result: result of the operation 1: success, 0: failure.
367 *
368 */
369
370struct dm_hot_add_response {
371 struct dm_header hdr;
372 __u32 page_count;
373 __u32 result;
374} __packed;
375
376/*
377 * Types of information sent from host to the guest.
378 */
379
380enum dm_info_type {
381 INFO_TYPE_MAX_PAGE_CNT = 0,
382 MAX_INFO_TYPE
383};
384
385
386/*
387 * Header for the information message.
388 */
389
390struct dm_info_header {
391 enum dm_info_type type;
392 __u32 data_size;
393} __packed;
394
395/*
396 * This message is sent from the host to the guest to pass
397 * some relevant information (win8 addition).
398 *
399 * reserved: no used.
400 * info_size: size of the information blob.
401 * info: information blob.
402 */
403
404struct dm_info_msg {
6427a0d7 405 struct dm_header hdr;
9aa8b50b
S
406 __u32 reserved;
407 __u32 info_size;
408 __u8 info[];
409};
410
411/*
412 * End protocol definitions.
413 */
414
6571b2da
S
415struct balloon_state {
416 __u32 num_pages;
417 struct work_struct wrk;
418};
419
c51af826
S
420struct hot_add_wrk {
421 union dm_mem_page_range ha_page_range;
422 struct work_struct wrk;
423};
424
9aa8b50b
S
425static bool hot_add;
426static bool do_hot_add;
e500d158
S
427/*
428 * Delay reporting memory pressure by
429 * the specified number of seconds.
430 */
431static uint pressure_report_delay = 30;
9aa8b50b
S
432
433module_param(hot_add, bool, (S_IRUGO | S_IWUSR));
434MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
435
e500d158
S
436module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR));
437MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
9aa8b50b
S
438static atomic_t trans_id = ATOMIC_INIT(0);
439
440static int dm_ring_size = (5 * PAGE_SIZE);
441
442/*
443 * Driver specific state.
444 */
445
446enum hv_dm_state {
447 DM_INITIALIZING = 0,
448 DM_INITIALIZED,
449 DM_BALLOON_UP,
450 DM_BALLOON_DOWN,
451 DM_HOT_ADD,
452 DM_INIT_ERROR
453};
454
455
456static __u8 recv_buffer[PAGE_SIZE];
457static __u8 *send_buffer;
458#define PAGES_IN_2M 512
459
460struct hv_dynmem_device {
461 struct hv_device *dev;
462 enum hv_dm_state state;
463 struct completion host_event;
464 struct completion config_event;
465
466 /*
467 * Number of pages we have currently ballooned out.
468 */
469 unsigned int num_pages_ballooned;
470
471 /*
6571b2da
S
472 * State to manage the ballooning (up) operation.
473 */
474 struct balloon_state balloon_wrk;
475
c51af826
S
476 /*
477 * State to execute the "hot-add" operation.
478 */
479 struct hot_add_wrk ha_wrk;
480
6571b2da
S
481 /*
482 * This thread handles hot-add
9aa8b50b
S
483 * requests from the host as well as notifying
484 * the host with regards to memory pressure in
485 * the guest.
486 */
487 struct task_struct *thread;
488
489 /*
490 * We start with the highest version we can support
491 * and downgrade based on the host; we save here the
492 * next version to try.
493 */
494 __u32 next_version;
495};
496
497static struct hv_dynmem_device dm_device;
498
c51af826 499static void hot_add_req(struct work_struct *dummy)
9aa8b50b
S
500{
501
502 struct dm_hot_add_response resp;
503
504 if (do_hot_add) {
505
506 pr_info("Memory hot add not supported\n");
507
508 /*
509 * Currently we do not support hot add.
510 * Just fail the request.
511 */
512 }
513
514 memset(&resp, 0, sizeof(struct dm_hot_add_response));
515 resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
516 resp.hdr.size = sizeof(struct dm_hot_add_response);
517 resp.hdr.trans_id = atomic_inc_return(&trans_id);
518
519 resp.page_count = 0;
520 resp.result = 0;
521
c51af826
S
522 dm_device.state = DM_INITIALIZED;
523 vmbus_sendpacket(dm_device.dev->channel, &resp,
9aa8b50b
S
524 sizeof(struct dm_hot_add_response),
525 (unsigned long)NULL,
526 VM_PKT_DATA_INBAND, 0);
527
528}
529
530static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
531{
6427a0d7
S
532 struct dm_info_header *info_hdr;
533
534 info_hdr = (struct dm_info_header *)msg->info;
535
536 switch (info_hdr->type) {
9aa8b50b
S
537 case INFO_TYPE_MAX_PAGE_CNT:
538 pr_info("Received INFO_TYPE_MAX_PAGE_CNT\n");
6427a0d7 539 pr_info("Data Size is %d\n", info_hdr->data_size);
9aa8b50b
S
540 break;
541 default:
6427a0d7 542 pr_info("Received Unknown type: %d\n", info_hdr->type);
9aa8b50b
S
543 }
544}
545
1c7db96f
S
546unsigned long compute_balloon_floor(void)
547{
548 unsigned long min_pages;
549#define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
550 /* Simple continuous piecewiese linear function:
551 * max MiB -> min MiB gradient
552 * 0 0
553 * 16 16
554 * 32 24
555 * 128 72 (1/2)
556 * 512 168 (1/4)
557 * 2048 360 (1/8)
558 * 8192 552 (1/32)
559 * 32768 1320
560 * 131072 4392
561 */
562 if (totalram_pages < MB2PAGES(128))
563 min_pages = MB2PAGES(8) + (totalram_pages >> 1);
564 else if (totalram_pages < MB2PAGES(512))
565 min_pages = MB2PAGES(40) + (totalram_pages >> 2);
566 else if (totalram_pages < MB2PAGES(2048))
567 min_pages = MB2PAGES(104) + (totalram_pages >> 3);
568 else
569 min_pages = MB2PAGES(296) + (totalram_pages >> 5);
570#undef MB2PAGES
571 return min_pages;
572}
573
9aa8b50b
S
574/*
575 * Post our status as it relates memory pressure to the
576 * host. Host expects the guests to post this status
577 * periodically at 1 second intervals.
578 *
579 * The metrics specified in this protocol are very Windows
580 * specific and so we cook up numbers here to convey our memory
581 * pressure.
582 */
583
584static void post_status(struct hv_dynmem_device *dm)
585{
586 struct dm_status status;
0731572b 587 struct sysinfo val;
9aa8b50b 588
e500d158
S
589 if (pressure_report_delay > 0) {
590 --pressure_report_delay;
591 return;
592 }
0731572b 593 si_meminfo(&val);
9aa8b50b
S
594 memset(&status, 0, sizeof(struct dm_status));
595 status.hdr.type = DM_STATUS_REPORT;
596 status.hdr.size = sizeof(struct dm_status);
597 status.hdr.trans_id = atomic_inc_return(&trans_id);
598
0731572b
S
599 /*
600 * The host expects the guest to report free memory.
601 * Further, the host expects the pressure information to
602 * include the ballooned out pages.
1c7db96f
S
603 * For a given amount of memory that we are managing, we
604 * need to compute a floor below which we should not balloon.
605 * Compute this and add it to the pressure report.
0731572b
S
606 */
607 status.num_avail = val.freeram;
1c7db96f
S
608 status.num_committed = vm_memory_committed() +
609 dm->num_pages_ballooned +
610 compute_balloon_floor();
9aa8b50b
S
611
612 vmbus_sendpacket(dm->dev->channel, &status,
613 sizeof(struct dm_status),
614 (unsigned long)NULL,
615 VM_PKT_DATA_INBAND, 0);
616
617}
618
989623c7 619static void free_balloon_pages(struct hv_dynmem_device *dm,
9aa8b50b
S
620 union dm_mem_page_range *range_array)
621{
622 int num_pages = range_array->finfo.page_cnt;
623 __u64 start_frame = range_array->finfo.start_page;
624 struct page *pg;
625 int i;
626
627 for (i = 0; i < num_pages; i++) {
628 pg = pfn_to_page(i + start_frame);
629 __free_page(pg);
630 dm->num_pages_ballooned--;
631 }
632}
633
634
635
636static int alloc_balloon_pages(struct hv_dynmem_device *dm, int num_pages,
637 struct dm_balloon_response *bl_resp, int alloc_unit,
638 bool *alloc_error)
639{
640 int i = 0;
641 struct page *pg;
642
643 if (num_pages < alloc_unit)
644 return 0;
645
646 for (i = 0; (i * alloc_unit) < num_pages; i++) {
647 if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
648 PAGE_SIZE)
649 return i * alloc_unit;
650
651 /*
652 * We execute this code in a thread context. Furthermore,
653 * we don't want the kernel to try too hard.
654 */
655 pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
656 __GFP_NOMEMALLOC | __GFP_NOWARN,
657 get_order(alloc_unit << PAGE_SHIFT));
658
659 if (!pg) {
660 *alloc_error = true;
661 return i * alloc_unit;
662 }
663
664
665 dm->num_pages_ballooned += alloc_unit;
666
667 bl_resp->range_count++;
668 bl_resp->range_array[i].finfo.start_page =
669 page_to_pfn(pg);
670 bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
671 bl_resp->hdr.size += sizeof(union dm_mem_page_range);
672
673 }
674
675 return num_pages;
676}
677
678
679
6571b2da 680static void balloon_up(struct work_struct *dummy)
9aa8b50b 681{
6571b2da 682 int num_pages = dm_device.balloon_wrk.num_pages;
9aa8b50b
S
683 int num_ballooned = 0;
684 struct dm_balloon_response *bl_resp;
685 int alloc_unit;
686 int ret;
687 bool alloc_error = false;
688 bool done = false;
689 int i;
690
691
692 /*
693 * Currently, we only support 4k allocations.
694 */
695 alloc_unit = 1;
696
697 while (!done) {
698 bl_resp = (struct dm_balloon_response *)send_buffer;
699 memset(send_buffer, 0, PAGE_SIZE);
700 bl_resp->hdr.type = DM_BALLOON_RESPONSE;
701 bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
702 bl_resp->hdr.size = sizeof(struct dm_balloon_response);
703 bl_resp->more_pages = 1;
704
705
706 num_pages -= num_ballooned;
6571b2da 707 num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
9aa8b50b
S
708 bl_resp, alloc_unit,
709 &alloc_error);
710
711 if ((alloc_error) || (num_ballooned == num_pages)) {
712 bl_resp->more_pages = 0;
713 done = true;
6571b2da 714 dm_device.state = DM_INITIALIZED;
9aa8b50b
S
715 }
716
717 /*
718 * We are pushing a lot of data through the channel;
719 * deal with transient failures caused because of the
720 * lack of space in the ring buffer.
721 */
722
723 do {
724 ret = vmbus_sendpacket(dm_device.dev->channel,
725 bl_resp,
726 bl_resp->hdr.size,
727 (unsigned long)NULL,
728 VM_PKT_DATA_INBAND, 0);
729
730 if (ret == -EAGAIN)
731 msleep(20);
732
733 } while (ret == -EAGAIN);
734
735 if (ret) {
736 /*
737 * Free up the memory we allocatted.
738 */
739 pr_info("Balloon response failed\n");
740
741 for (i = 0; i < bl_resp->range_count; i++)
6571b2da 742 free_balloon_pages(&dm_device,
9aa8b50b
S
743 &bl_resp->range_array[i]);
744
745 done = true;
746 }
747 }
748
749}
750
751static void balloon_down(struct hv_dynmem_device *dm,
752 struct dm_unballoon_request *req)
753{
754 union dm_mem_page_range *range_array = req->range_array;
755 int range_count = req->range_count;
756 struct dm_unballoon_response resp;
757 int i;
758
759 for (i = 0; i < range_count; i++)
760 free_balloon_pages(dm, &range_array[i]);
761
762 if (req->more_pages == 1)
763 return;
764
765 memset(&resp, 0, sizeof(struct dm_unballoon_response));
766 resp.hdr.type = DM_UNBALLOON_RESPONSE;
767 resp.hdr.trans_id = atomic_inc_return(&trans_id);
768 resp.hdr.size = sizeof(struct dm_unballoon_response);
769
770 vmbus_sendpacket(dm_device.dev->channel, &resp,
771 sizeof(struct dm_unballoon_response),
772 (unsigned long)NULL,
773 VM_PKT_DATA_INBAND, 0);
774
775 dm->state = DM_INITIALIZED;
776}
777
778static void balloon_onchannelcallback(void *context);
779
780static int dm_thread_func(void *dm_dev)
781{
782 struct hv_dynmem_device *dm = dm_dev;
783 int t;
9aa8b50b
S
784
785 while (!kthread_should_stop()) {
786 t = wait_for_completion_timeout(&dm_device.config_event, 1*HZ);
787 /*
788 * The host expects us to post information on the memory
789 * pressure every second.
790 */
791
792 if (t == 0)
793 post_status(dm);
794
9aa8b50b
S
795 }
796
797 return 0;
798}
799
800
801static void version_resp(struct hv_dynmem_device *dm,
802 struct dm_version_response *vresp)
803{
804 struct dm_version_request version_req;
805 int ret;
806
807 if (vresp->is_accepted) {
808 /*
809 * We are done; wakeup the
810 * context waiting for version
811 * negotiation.
812 */
813 complete(&dm->host_event);
814 return;
815 }
816 /*
817 * If there are more versions to try, continue
818 * with negotiations; if not
819 * shutdown the service since we are not able
820 * to negotiate a suitable version number
821 * with the host.
822 */
823 if (dm->next_version == 0)
824 goto version_error;
825
826 dm->next_version = 0;
827 memset(&version_req, 0, sizeof(struct dm_version_request));
828 version_req.hdr.type = DM_VERSION_REQUEST;
829 version_req.hdr.size = sizeof(struct dm_version_request);
830 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
831 version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN7;
832 version_req.is_last_attempt = 1;
833
834 ret = vmbus_sendpacket(dm->dev->channel, &version_req,
835 sizeof(struct dm_version_request),
836 (unsigned long)NULL,
837 VM_PKT_DATA_INBAND, 0);
838
839 if (ret)
840 goto version_error;
841
842 return;
843
844version_error:
845 dm->state = DM_INIT_ERROR;
846 complete(&dm->host_event);
847}
848
849static void cap_resp(struct hv_dynmem_device *dm,
850 struct dm_capabilities_resp_msg *cap_resp)
851{
852 if (!cap_resp->is_accepted) {
853 pr_info("Capabilities not accepted by host\n");
854 dm->state = DM_INIT_ERROR;
855 }
856 complete(&dm->host_event);
857}
858
859static void balloon_onchannelcallback(void *context)
860{
861 struct hv_device *dev = context;
862 u32 recvlen;
863 u64 requestid;
864 struct dm_message *dm_msg;
865 struct dm_header *dm_hdr;
866 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
6571b2da 867 struct dm_balloon *bal_msg;
c51af826
S
868 struct dm_hot_add *ha_msg;
869 union dm_mem_page_range *ha_pg_range;
9aa8b50b
S
870
871 memset(recv_buffer, 0, sizeof(recv_buffer));
872 vmbus_recvpacket(dev->channel, recv_buffer,
873 PAGE_SIZE, &recvlen, &requestid);
874
875 if (recvlen > 0) {
876 dm_msg = (struct dm_message *)recv_buffer;
877 dm_hdr = &dm_msg->hdr;
878
879 switch (dm_hdr->type) {
880 case DM_VERSION_RESPONSE:
881 version_resp(dm,
882 (struct dm_version_response *)dm_msg);
883 break;
884
885 case DM_CAPABILITIES_RESPONSE:
886 cap_resp(dm,
887 (struct dm_capabilities_resp_msg *)dm_msg);
888 break;
889
890 case DM_BALLOON_REQUEST:
6571b2da
S
891 if (dm->state == DM_BALLOON_UP)
892 pr_warn("Currently ballooning\n");
893 bal_msg = (struct dm_balloon *)recv_buffer;
9aa8b50b 894 dm->state = DM_BALLOON_UP;
6571b2da
S
895 dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
896 schedule_work(&dm_device.balloon_wrk.wrk);
9aa8b50b
S
897 break;
898
899 case DM_UNBALLOON_REQUEST:
900 dm->state = DM_BALLOON_DOWN;
901 balloon_down(dm,
902 (struct dm_unballoon_request *)recv_buffer);
903 break;
904
905 case DM_MEM_HOT_ADD_REQUEST:
c51af826
S
906 if (dm->state == DM_HOT_ADD)
907 pr_warn("Currently hot-adding\n");
9aa8b50b 908 dm->state = DM_HOT_ADD;
c51af826
S
909 ha_msg = (struct dm_hot_add *)recv_buffer;
910 ha_pg_range = &ha_msg->range;
911 dm_device.ha_wrk.ha_page_range = *ha_pg_range;
912 schedule_work(&dm_device.ha_wrk.wrk);
9aa8b50b
S
913 break;
914
915 case DM_INFO_MESSAGE:
916 process_info(dm, (struct dm_info_msg *)dm_msg);
917 break;
918
919 default:
920 pr_err("Unhandled message: type: %d\n", dm_hdr->type);
921
922 }
923 }
924
925}
926
927static int balloon_probe(struct hv_device *dev,
928 const struct hv_vmbus_device_id *dev_id)
929{
930 int ret, t;
931 struct dm_version_request version_req;
932 struct dm_capabilities cap_msg;
933
934 do_hot_add = hot_add;
935
936 /*
937 * First allocate a send buffer.
938 */
939
940 send_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
941 if (!send_buffer)
942 return -ENOMEM;
943
944 ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
945 balloon_onchannelcallback, dev);
946
947 if (ret)
33080c1c 948 goto probe_error0;
9aa8b50b
S
949
950 dm_device.dev = dev;
951 dm_device.state = DM_INITIALIZING;
952 dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
953 init_completion(&dm_device.host_event);
954 init_completion(&dm_device.config_event);
6571b2da 955 INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
c51af826 956 INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
9aa8b50b
S
957
958 dm_device.thread =
959 kthread_run(dm_thread_func, &dm_device, "hv_balloon");
960 if (IS_ERR(dm_device.thread)) {
961 ret = PTR_ERR(dm_device.thread);
33080c1c 962 goto probe_error1;
9aa8b50b
S
963 }
964
965 hv_set_drvdata(dev, &dm_device);
966 /*
967 * Initiate the hand shake with the host and negotiate
968 * a version that the host can support. We start with the
969 * highest version number and go down if the host cannot
970 * support it.
971 */
972 memset(&version_req, 0, sizeof(struct dm_version_request));
973 version_req.hdr.type = DM_VERSION_REQUEST;
974 version_req.hdr.size = sizeof(struct dm_version_request);
975 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
976 version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN8;
977 version_req.is_last_attempt = 0;
978
979 ret = vmbus_sendpacket(dev->channel, &version_req,
980 sizeof(struct dm_version_request),
981 (unsigned long)NULL,
7a64b864 982 VM_PKT_DATA_INBAND, 0);
9aa8b50b 983 if (ret)
33080c1c 984 goto probe_error2;
9aa8b50b
S
985
986 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
987 if (t == 0) {
988 ret = -ETIMEDOUT;
33080c1c 989 goto probe_error2;
9aa8b50b
S
990 }
991
992 /*
993 * If we could not negotiate a compatible version with the host
994 * fail the probe function.
995 */
996 if (dm_device.state == DM_INIT_ERROR) {
997 ret = -ETIMEDOUT;
33080c1c 998 goto probe_error2;
9aa8b50b
S
999 }
1000 /*
1001 * Now submit our capabilities to the host.
1002 */
1003 memset(&cap_msg, 0, sizeof(struct dm_capabilities));
1004 cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
1005 cap_msg.hdr.size = sizeof(struct dm_capabilities);
1006 cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
1007
1008 cap_msg.caps.cap_bits.balloon = 1;
1009 /*
1010 * While we currently don't support hot-add,
1011 * we still advertise this capability since the
1012 * host requires that guests partcipating in the
1013 * dynamic memory protocol support hot add.
1014 */
1015 cap_msg.caps.cap_bits.hot_add = 1;
1016
1017 /*
1018 * Currently the host does not use these
1019 * values and we set them to what is done in the
1020 * Windows driver.
1021 */
1022 cap_msg.min_page_cnt = 0;
1023 cap_msg.max_page_number = -1;
1024
1025 ret = vmbus_sendpacket(dev->channel, &cap_msg,
1026 sizeof(struct dm_capabilities),
1027 (unsigned long)NULL,
7a64b864 1028 VM_PKT_DATA_INBAND, 0);
9aa8b50b 1029 if (ret)
33080c1c 1030 goto probe_error2;
9aa8b50b
S
1031
1032 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1033 if (t == 0) {
1034 ret = -ETIMEDOUT;
33080c1c 1035 goto probe_error2;
9aa8b50b
S
1036 }
1037
1038 /*
1039 * If the host does not like our capabilities,
1040 * fail the probe function.
1041 */
1042 if (dm_device.state == DM_INIT_ERROR) {
1043 ret = -ETIMEDOUT;
33080c1c 1044 goto probe_error2;
9aa8b50b
S
1045 }
1046
1047 dm_device.state = DM_INITIALIZED;
1048
1049 return 0;
1050
33080c1c 1051probe_error2:
9aa8b50b
S
1052 kthread_stop(dm_device.thread);
1053
33080c1c 1054probe_error1:
9aa8b50b 1055 vmbus_close(dev->channel);
33080c1c
S
1056probe_error0:
1057 kfree(send_buffer);
9aa8b50b
S
1058 return ret;
1059}
1060
1061static int balloon_remove(struct hv_device *dev)
1062{
1063 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
1064
1065 if (dm->num_pages_ballooned != 0)
1066 pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
1067
6571b2da 1068 cancel_work_sync(&dm->balloon_wrk.wrk);
c51af826 1069 cancel_work_sync(&dm->ha_wrk.wrk);
9aa8b50b
S
1070 vmbus_close(dev->channel);
1071 kthread_stop(dm->thread);
33080c1c 1072 kfree(send_buffer);
9aa8b50b
S
1073
1074 return 0;
1075}
1076
1077static const struct hv_vmbus_device_id id_table[] = {
1078 /* Dynamic Memory Class ID */
1079 /* 525074DC-8985-46e2-8057-A307DC18A502 */
d13984e5 1080 { HV_DM_GUID, },
9aa8b50b
S
1081 { },
1082};
1083
1084MODULE_DEVICE_TABLE(vmbus, id_table);
1085
1086static struct hv_driver balloon_drv = {
1087 .name = "hv_balloon",
1088 .id_table = id_table,
1089 .probe = balloon_probe,
1090 .remove = balloon_remove,
1091};
1092
1093static int __init init_balloon_drv(void)
1094{
1095
1096 return vmbus_driver_register(&balloon_drv);
1097}
1098
9aa8b50b 1099module_init(init_balloon_drv);
9aa8b50b
S
1100
1101MODULE_DESCRIPTION("Hyper-V Balloon");
1102MODULE_VERSION(HV_DRV_VERSION);
1103MODULE_LICENSE("GPL");
This page took 0.085354 seconds and 5 git commands to generate.