blob: b155d005298139950dddebbc1b0124714a41cbe6 [file] [log] [blame]
Thomas Gleixner43aa3132019-05-29 07:17:54 -07001// SPDX-License-Identifier: GPL-2.0-only
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08002/*
3 * Copyright (c) 2012, Microsoft Corporation.
4 *
5 * Author:
6 * K. Y. Srinivasan <kys@microsoft.com>
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08007 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11#include <linux/kernel.h>
K. Y. Srinivasanae339332014-04-23 13:53:39 -070012#include <linux/jiffies.h>
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080013#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>
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080023#include <linux/percpu_counter.h>
24
25#include <linux/hyperv.h>
Himadri Pandya2af5e7b2019-08-17 04:08:50 +000026#include <asm/hyperv-tlfs.h>
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080027
Dexuan Cui25bd2b22019-11-19 23:16:05 -080028#include <asm/mshyperv.h>
29
Vitaly Kuznetsovcf21be92018-03-04 22:17:22 -070030#define CREATE_TRACE_POINTS
31#include "hv_trace_balloon.h"
32
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080033/*
34 * We begin with definitions supporting the Dynamic Memory protocol
35 * with the host.
36 *
37 * Begin protocol definitions.
38 */
39
40
41
42/*
43 * Protocol versions. The low word is the minor version, the high word the major
44 * version.
45 *
46 * History:
47 * Initial version 1.0
48 * Changed to 0.1 on 2009/03/25
49 * Changes to 0.2 on 2009/05/14
50 * Changes to 0.3 on 2009/12/03
51 * Changed to 1.0 on 2011/04/05
52 */
53
54#define DYNMEM_MAKE_VERSION(Major, Minor) ((__u32)(((Major) << 16) | (Minor)))
55#define DYNMEM_MAJOR_VERSION(Version) ((__u32)(Version) >> 16)
56#define DYNMEM_MINOR_VERSION(Version) ((__u32)(Version) & 0xff)
57
58enum {
59 DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
60 DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
Alex Ngb6ddeae2015-08-01 16:08:13 -070061 DYNMEM_PROTOCOL_VERSION_3 = DYNMEM_MAKE_VERSION(2, 0),
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080062
63 DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
64 DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
Alex Ngb6ddeae2015-08-01 16:08:13 -070065 DYNMEM_PROTOCOL_VERSION_WIN10 = DYNMEM_PROTOCOL_VERSION_3,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080066
Alex Ngb6ddeae2015-08-01 16:08:13 -070067 DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN10
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080068};
69
70
71
72/*
73 * Message Types
74 */
75
76enum dm_message_type {
77 /*
78 * Version 0.3
79 */
80 DM_ERROR = 0,
81 DM_VERSION_REQUEST = 1,
82 DM_VERSION_RESPONSE = 2,
83 DM_CAPABILITIES_REPORT = 3,
84 DM_CAPABILITIES_RESPONSE = 4,
85 DM_STATUS_REPORT = 5,
86 DM_BALLOON_REQUEST = 6,
87 DM_BALLOON_RESPONSE = 7,
88 DM_UNBALLOON_REQUEST = 8,
89 DM_UNBALLOON_RESPONSE = 9,
90 DM_MEM_HOT_ADD_REQUEST = 10,
91 DM_MEM_HOT_ADD_RESPONSE = 11,
92 DM_VERSION_03_MAX = 11,
93 /*
94 * Version 1.0.
95 */
96 DM_INFO_MESSAGE = 12,
97 DM_VERSION_1_MAX = 12
98};
99
100
101/*
102 * Structures defining the dynamic memory management
103 * protocol.
104 */
105
106union dm_version {
107 struct {
108 __u16 minor_version;
109 __u16 major_version;
110 };
111 __u32 version;
112} __packed;
113
114
115union dm_caps {
116 struct {
117 __u64 balloon:1;
118 __u64 hot_add:1;
K. Y. Srinivasan647965a2013-03-29 07:36:11 -0700119 /*
120 * To support guests that may have alignment
121 * limitations on hot-add, the guest can specify
122 * its alignment requirements; a value of n
123 * represents an alignment of 2^n in mega bytes.
124 */
125 __u64 hot_add_alignment:4;
126 __u64 reservedz:58;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800127 } cap_bits;
128 __u64 caps;
129} __packed;
130
131union dm_mem_page_range {
132 struct {
133 /*
134 * The PFN number of the first page in the range.
135 * 40 bits is the architectural limit of a PFN
136 * number for AMD64.
137 */
138 __u64 start_page:40;
139 /*
140 * The number of pages in the range.
141 */
142 __u64 page_cnt:24;
143 } finfo;
144 __u64 page_range;
145} __packed;
146
147
148
149/*
150 * The header for all dynamic memory messages:
151 *
152 * type: Type of the message.
153 * size: Size of the message in bytes; including the header.
154 * trans_id: The guest is responsible for manufacturing this ID.
155 */
156
157struct dm_header {
158 __u16 type;
159 __u16 size;
160 __u32 trans_id;
161} __packed;
162
163/*
164 * A generic message format for dynamic memory.
165 * Specific message formats are defined later in the file.
166 */
167
168struct dm_message {
169 struct dm_header hdr;
170 __u8 data[]; /* enclosed message */
171} __packed;
172
173
174/*
175 * Specific message types supporting the dynamic memory protocol.
176 */
177
178/*
179 * Version negotiation message. Sent from the guest to the host.
180 * The guest is free to try different versions until the host
181 * accepts the version.
182 *
183 * dm_version: The protocol version requested.
184 * is_last_attempt: If TRUE, this is the last version guest will request.
185 * reservedz: Reserved field, set to zero.
186 */
187
188struct dm_version_request {
189 struct dm_header hdr;
190 union dm_version version;
191 __u32 is_last_attempt:1;
192 __u32 reservedz:31;
193} __packed;
194
195/*
196 * Version response message; Host to Guest and indicates
197 * if the host has accepted the version sent by the guest.
198 *
199 * is_accepted: If TRUE, host has accepted the version and the guest
200 * should proceed to the next stage of the protocol. FALSE indicates that
201 * guest should re-try with a different version.
202 *
203 * reservedz: Reserved field, set to zero.
204 */
205
206struct dm_version_response {
207 struct dm_header hdr;
208 __u64 is_accepted:1;
209 __u64 reservedz:63;
210} __packed;
211
212/*
213 * Message reporting capabilities. This is sent from the guest to the
214 * host.
215 */
216
217struct dm_capabilities {
218 struct dm_header hdr;
219 union dm_caps caps;
220 __u64 min_page_cnt;
221 __u64 max_page_number;
222} __packed;
223
224/*
225 * Response to the capabilities message. This is sent from the host to the
226 * guest. This message notifies if the host has accepted the guest's
227 * capabilities. If the host has not accepted, the guest must shutdown
228 * the service.
229 *
230 * is_accepted: Indicates if the host has accepted guest's capabilities.
231 * reservedz: Must be 0.
232 */
233
234struct dm_capabilities_resp_msg {
235 struct dm_header hdr;
236 __u64 is_accepted:1;
237 __u64 reservedz:63;
238} __packed;
239
240/*
241 * This message is used to report memory pressure from the guest.
242 * This message is not part of any transaction and there is no
243 * response to this message.
244 *
245 * num_avail: Available memory in pages.
246 * num_committed: Committed memory in pages.
247 * page_file_size: The accumulated size of all page files
248 * in the system in pages.
249 * zero_free: The nunber of zero and free pages.
250 * page_file_writes: The writes to the page file in pages.
251 * io_diff: An indicator of file cache efficiency or page file activity,
252 * calculated as File Cache Page Fault Count - Page Read Count.
253 * This value is in pages.
254 *
255 * Some of these metrics are Windows specific and fortunately
256 * the algorithm on the host side that computes the guest memory
257 * pressure only uses num_committed value.
258 */
259
260struct dm_status {
261 struct dm_header hdr;
262 __u64 num_avail;
263 __u64 num_committed;
264 __u64 page_file_size;
265 __u64 zero_free;
266 __u32 page_file_writes;
267 __u32 io_diff;
268} __packed;
269
270
271/*
272 * Message to ask the guest to allocate memory - balloon up message.
273 * This message is sent from the host to the guest. The guest may not be
274 * able to allocate as much memory as requested.
275 *
276 * num_pages: number of pages to allocate.
277 */
278
279struct dm_balloon {
280 struct dm_header hdr;
281 __u32 num_pages;
282 __u32 reservedz;
283} __packed;
284
285
286/*
287 * Balloon response message; this message is sent from the guest
288 * to the host in response to the balloon message.
289 *
290 * reservedz: Reserved; must be set to zero.
291 * more_pages: If FALSE, this is the last message of the transaction.
292 * if TRUE there will atleast one more message from the guest.
293 *
294 * range_count: The number of ranges in the range array.
295 *
296 * range_array: An array of page ranges returned to the host.
297 *
298 */
299
300struct dm_balloon_response {
301 struct dm_header hdr;
302 __u32 reservedz;
303 __u32 more_pages:1;
304 __u32 range_count:31;
305 union dm_mem_page_range range_array[];
306} __packed;
307
308/*
309 * Un-balloon message; this message is sent from the host
310 * to the guest to give guest more memory.
311 *
312 * more_pages: If FALSE, this is the last message of the transaction.
313 * if TRUE there will atleast one more message from the guest.
314 *
315 * reservedz: Reserved; must be set to zero.
316 *
317 * range_count: The number of ranges in the range array.
318 *
319 * range_array: An array of page ranges returned to the host.
320 *
321 */
322
323struct dm_unballoon_request {
324 struct dm_header hdr;
325 __u32 more_pages:1;
326 __u32 reservedz:31;
327 __u32 range_count;
328 union dm_mem_page_range range_array[];
329} __packed;
330
331/*
332 * Un-balloon response message; this message is sent from the guest
333 * to the host in response to an unballoon request.
334 *
335 */
336
337struct dm_unballoon_response {
338 struct dm_header hdr;
339} __packed;
340
341
342/*
343 * Hot add request message. Message sent from the host to the guest.
344 *
345 * mem_range: Memory range to hot add.
346 *
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800347 */
348
349struct dm_hot_add {
350 struct dm_header hdr;
351 union dm_mem_page_range range;
352} __packed;
353
354/*
355 * Hot add response message.
356 * This message is sent by the guest to report the status of a hot add request.
357 * If page_count is less than the requested page count, then the host should
358 * assume all further hot add requests will fail, since this indicates that
359 * the guest has hit an upper physical memory barrier.
360 *
361 * Hot adds may also fail due to low resources; in this case, the guest must
362 * not complete this message until the hot add can succeed, and the host must
363 * not send a new hot add request until the response is sent.
364 * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
365 * times it fails the request.
366 *
367 *
368 * page_count: number of pages that were successfully hot added.
369 *
370 * result: result of the operation 1: success, 0: failure.
371 *
372 */
373
374struct dm_hot_add_response {
375 struct dm_header hdr;
376 __u32 page_count;
377 __u32 result;
378} __packed;
379
380/*
381 * Types of information sent from host to the guest.
382 */
383
384enum dm_info_type {
385 INFO_TYPE_MAX_PAGE_CNT = 0,
386 MAX_INFO_TYPE
387};
388
389
390/*
391 * Header for the information message.
392 */
393
394struct dm_info_header {
395 enum dm_info_type type;
396 __u32 data_size;
397} __packed;
398
399/*
400 * This message is sent from the host to the guest to pass
401 * some relevant information (win8 addition).
402 *
403 * reserved: no used.
404 * info_size: size of the information blob.
405 * info: information blob.
406 */
407
408struct dm_info_msg {
K. Y. Srinivasan6427a0d72012-12-06 11:06:54 -0800409 struct dm_header hdr;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800410 __u32 reserved;
411 __u32 info_size;
412 __u8 info[];
413};
414
415/*
416 * End protocol definitions.
417 */
418
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700419/*
420 * State to manage hot adding memory into the guest.
421 * The range start_pfn : end_pfn specifies the range
422 * that the host has asked us to hot add. The range
423 * start_pfn : ha_end_pfn specifies the range that we have
424 * currently hot added. We hot add in multiples of 128M
425 * chunks; it is possible that we may not be able to bring
426 * online all the pages in the region. The range
Vitaly Kuznetsov7cf3b792016-08-24 16:23:09 -0700427 * covered_start_pfn:covered_end_pfn defines the pages that can
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700428 * be brough online.
429 */
430
431struct hv_hotadd_state {
432 struct list_head list;
433 unsigned long start_pfn;
Vitaly Kuznetsov7cf3b792016-08-24 16:23:09 -0700434 unsigned long covered_start_pfn;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700435 unsigned long covered_end_pfn;
436 unsigned long ha_end_pfn;
437 unsigned long end_pfn;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700438 /*
439 * A list of gaps.
440 */
441 struct list_head gap_list;
442};
443
444struct hv_hotadd_gap {
445 struct list_head list;
446 unsigned long start_pfn;
447 unsigned long end_pfn;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700448};
449
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -0700450struct balloon_state {
451 __u32 num_pages;
452 struct work_struct wrk;
453};
454
K. Y. Srinivasanc51af822013-03-15 12:25:41 -0700455struct hot_add_wrk {
456 union dm_mem_page_range ha_page_range;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700457 union dm_mem_page_range ha_region_range;
K. Y. Srinivasanc51af822013-03-15 12:25:41 -0700458 struct work_struct wrk;
459};
460
Dexuan Cui25bd2b22019-11-19 23:16:05 -0800461static bool allow_hibernation;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700462static bool hot_add = true;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800463static bool do_hot_add;
K. Y. Srinivasane500d152013-02-08 15:57:15 -0800464/*
465 * Delay reporting memory pressure by
466 * the specified number of seconds.
467 */
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700468static uint pressure_report_delay = 45;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800469
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700470/*
471 * The last time we posted a pressure report to host.
472 */
473static unsigned long last_post_time;
474
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800475module_param(hot_add, bool, (S_IRUGO | S_IWUSR));
476MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
477
K. Y. Srinivasane500d152013-02-08 15:57:15 -0800478module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR));
479MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800480static atomic_t trans_id = ATOMIC_INIT(0);
481
Himadri Pandya2af5e7b2019-08-17 04:08:50 +0000482static int dm_ring_size = 20 * 1024;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800483
484/*
485 * Driver specific state.
486 */
487
488enum hv_dm_state {
489 DM_INITIALIZING = 0,
490 DM_INITIALIZED,
491 DM_BALLOON_UP,
492 DM_BALLOON_DOWN,
493 DM_HOT_ADD,
494 DM_INIT_ERROR
495};
496
497
Himadri Pandya2af5e7b2019-08-17 04:08:50 +0000498static __u8 recv_buffer[HV_HYP_PAGE_SIZE];
499static __u8 balloon_up_send_buffer[HV_HYP_PAGE_SIZE];
500#define PAGES_IN_2M (2 * 1024 * 1024 / PAGE_SIZE)
501#define HA_CHUNK (128 * 1024 * 1024 / PAGE_SIZE)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800502
503struct hv_dynmem_device {
504 struct hv_device *dev;
505 enum hv_dm_state state;
506 struct completion host_event;
507 struct completion config_event;
508
509 /*
510 * Number of pages we have currently ballooned out.
511 */
512 unsigned int num_pages_ballooned;
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -0800513 unsigned int num_pages_onlined;
514 unsigned int num_pages_added;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800515
516 /*
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -0700517 * State to manage the ballooning (up) operation.
518 */
519 struct balloon_state balloon_wrk;
520
521 /*
K. Y. Srinivasanc51af822013-03-15 12:25:41 -0700522 * State to execute the "hot-add" operation.
523 */
524 struct hot_add_wrk ha_wrk;
525
526 /*
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700527 * This state tracks if the host has specified a hot-add
528 * region.
529 */
530 bool host_specified_ha_region;
531
532 /*
533 * State to synchronize hot-add.
534 */
535 struct completion ol_waitevent;
536 bool ha_waiting;
537 /*
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -0700538 * This thread handles hot-add
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800539 * 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
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700545 /*
546 * Protects ha_region_list, num_pages_onlined counter and individual
547 * regions from ha_region_list.
548 */
549 spinlock_t ha_lock;
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800550
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800551 /*
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700552 * A list of hot-add regions.
553 */
554 struct list_head ha_region_list;
555
556 /*
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800557 * 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;
Alex Ngb3bb97b2016-11-06 13:14:09 -0800562
563 /*
564 * The negotiated version agreed by host.
565 */
566 __u32 version;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800567};
568
569static struct hv_dynmem_device dm_device;
570
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700571static void post_status(struct hv_dynmem_device *dm);
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800572
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700573#ifdef CONFIG_MEMORY_HOTPLUG
Vitaly Kuznetsovbba072d12018-03-04 22:17:21 -0700574static inline bool has_pfn_is_backed(struct hv_hotadd_state *has,
575 unsigned long pfn)
576{
577 struct hv_hotadd_gap *gap;
578
579 /* The page is not backed. */
580 if ((pfn < has->covered_start_pfn) || (pfn >= has->covered_end_pfn))
581 return false;
582
583 /* Check for gaps. */
584 list_for_each_entry(gap, &has->gap_list, list) {
585 if ((pfn >= gap->start_pfn) && (pfn < gap->end_pfn))
586 return false;
587 }
588
589 return true;
590}
591
592static unsigned long hv_page_offline_check(unsigned long start_pfn,
593 unsigned long nr_pages)
594{
595 unsigned long pfn = start_pfn, count = 0;
596 struct hv_hotadd_state *has;
597 bool found;
598
599 while (pfn < start_pfn + nr_pages) {
600 /*
601 * Search for HAS which covers the pfn and when we find one
602 * count how many consequitive PFNs are covered.
603 */
604 found = false;
605 list_for_each_entry(has, &dm_device.ha_region_list, list) {
606 while ((pfn >= has->start_pfn) &&
607 (pfn < has->end_pfn) &&
608 (pfn < start_pfn + nr_pages)) {
609 found = true;
610 if (has_pfn_is_backed(has, pfn))
611 count++;
612 pfn++;
613 }
614 }
615
616 /*
617 * This PFN is not in any HAS (e.g. we're offlining a region
618 * which was present at boot), no need to account for it. Go
619 * to the next one.
620 */
621 if (!found)
622 pfn++;
623 }
624
625 return count;
626}
627
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800628static int hv_memory_notifier(struct notifier_block *nb, unsigned long val,
629 void *v)
630{
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -0800631 struct memory_notify *mem = (struct memory_notify *)v;
Vitaly Kuznetsovbba072d12018-03-04 22:17:21 -0700632 unsigned long flags, pfn_count;
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -0800633
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800634 switch (val) {
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800635 case MEM_ONLINE:
636 case MEM_CANCEL_ONLINE:
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800637 if (dm_device.ha_waiting) {
638 dm_device.ha_waiting = false;
639 complete(&dm_device.ol_waitevent);
640 }
641 break;
642
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800643 case MEM_OFFLINE:
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700644 spin_lock_irqsave(&dm_device.ha_lock, flags);
Vitaly Kuznetsovbba072d12018-03-04 22:17:21 -0700645 pfn_count = hv_page_offline_check(mem->start_pfn,
646 mem->nr_pages);
647 if (pfn_count <= dm_device.num_pages_onlined) {
648 dm_device.num_pages_onlined -= pfn_count;
649 } else {
650 /*
651 * We're offlining more pages than we managed to online.
652 * This is unexpected. In any case don't let
653 * num_pages_onlined wrap around zero.
654 */
655 WARN_ON_ONCE(1);
656 dm_device.num_pages_onlined = 0;
657 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700658 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -0800659 break;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700660 case MEM_GOING_ONLINE:
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -0800661 case MEM_GOING_OFFLINE:
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800662 case MEM_CANCEL_OFFLINE:
663 break;
664 }
665 return NOTIFY_OK;
666}
667
668static struct notifier_block hv_memory_nb = {
669 .notifier_call = hv_memory_notifier,
670 .priority = 0
671};
672
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700673/* Check if the particular page is backed and can be onlined and online it. */
674static void hv_page_online_one(struct hv_hotadd_state *has, struct page *pg)
675{
David Hildenbrandfae42c42019-03-05 15:42:36 -0800676 if (!has_pfn_is_backed(has, page_to_pfn(pg))) {
677 if (!PageOffline(pg))
678 __SetPageOffline(pg);
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700679 return;
David Hildenbrandfae42c42019-03-05 15:42:36 -0800680 }
681 if (PageOffline(pg))
682 __ClearPageOffline(pg);
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700683
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700684 /* This frame is currently backed; online the page. */
David Hildenbrand30a9c242019-11-30 17:53:55 -0800685 generic_online_page(pg, 0);
Alex Ng6df8d9a2017-08-06 13:12:53 -0700686
Lance Roy1c87dc82018-10-02 22:38:48 -0700687 lockdep_assert_held(&dm_device.ha_lock);
Alex Ng6df8d9a2017-08-06 13:12:53 -0700688 dm_device.num_pages_onlined++;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700689}
690
691static void hv_bring_pgs_online(struct hv_hotadd_state *has,
692 unsigned long start_pfn, unsigned long size)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800693{
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700694 int i;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800695
Alex Ngb3bb97b2016-11-06 13:14:09 -0800696 pr_debug("Online %lu pages starting at pfn 0x%lx\n", size, start_pfn);
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700697 for (i = 0; i < size; i++)
698 hv_page_online_one(has, pfn_to_page(start_pfn + i));
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700699}
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800700
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700701static void hv_mem_hot_add(unsigned long start, unsigned long size,
702 unsigned long pfn_count,
703 struct hv_hotadd_state *has)
704{
705 int ret = 0;
K. Y. Srinivasaned07ec92013-07-14 22:38:11 -0700706 int i, nid;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700707 unsigned long start_pfn;
708 unsigned long processed_pfn;
709 unsigned long total_pfn = pfn_count;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700710 unsigned long flags;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800711
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700712 for (i = 0; i < (size/HA_CHUNK); i++) {
713 start_pfn = start + (i * HA_CHUNK);
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700714
715 spin_lock_irqsave(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700716 has->ha_end_pfn += HA_CHUNK;
717
718 if (total_pfn > HA_CHUNK) {
719 processed_pfn = HA_CHUNK;
720 total_pfn -= HA_CHUNK;
721 } else {
722 processed_pfn = total_pfn;
723 total_pfn = 0;
724 }
725
726 has->covered_end_pfn += processed_pfn;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700727 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700728
729 init_completion(&dm_device.ol_waitevent);
Vitaly Kuznetsova132c542016-08-24 16:23:11 -0700730 dm_device.ha_waiting = !memhp_auto_online;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700731
732 nid = memory_add_physaddr_to_nid(PFN_PHYS(start_pfn));
733 ret = add_memory(nid, PFN_PHYS((start_pfn)),
734 (HA_CHUNK << PAGE_SHIFT));
735
736 if (ret) {
Vitaly Kuznetsov223e1e4d2018-03-04 22:17:19 -0700737 pr_err("hot_add memory failed error is %d\n", ret);
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -0700738 if (ret == -EEXIST) {
739 /*
740 * This error indicates that the error
741 * is not a transient failure. This is the
742 * case where the guest's physical address map
743 * precludes hot adding memory. Stop all further
744 * memory hot-add.
745 */
746 do_hot_add = false;
747 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700748 spin_lock_irqsave(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700749 has->ha_end_pfn -= HA_CHUNK;
750 has->covered_end_pfn -= processed_pfn;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700751 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700752 break;
753 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800754
755 /*
Vitaly Kuznetsova132c542016-08-24 16:23:11 -0700756 * Wait for the memory block to be onlined when memory onlining
757 * is done outside of kernel (memhp_auto_online). Since the hot
758 * add has succeeded, it is ok to proceed even if the pages in
759 * the hot added region have not been "onlined" within the
760 * allowed time.
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800761 */
Vitaly Kuznetsova132c542016-08-24 16:23:11 -0700762 if (dm_device.ha_waiting)
763 wait_for_completion_timeout(&dm_device.ol_waitevent,
764 5*HZ);
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700765 post_status(&dm_device);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800766 }
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700767}
768
Arun KSa9cd4102019-03-05 15:42:14 -0800769static void hv_online_page(struct page *pg, unsigned int order)
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700770{
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700771 struct hv_hotadd_state *has;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700772 unsigned long flags;
Vitaly Kuznetsov4f098af2018-03-04 22:17:20 -0700773 unsigned long pfn = page_to_pfn(pg);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700774
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700775 spin_lock_irqsave(&dm_device.ha_lock, flags);
776 list_for_each_entry(has, &dm_device.ha_region_list, list) {
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700777 /* The page belongs to a different HAS. */
Arun KSa9cd4102019-03-05 15:42:14 -0800778 if ((pfn < has->start_pfn) ||
779 (pfn + (1UL << order) > has->end_pfn))
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700780 continue;
781
Arun KSa9cd4102019-03-05 15:42:14 -0800782 hv_bring_pgs_online(has, pfn, 1UL << order);
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700783 break;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700784 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700785 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700786}
787
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700788static int pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700789{
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700790 struct hv_hotadd_state *has;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700791 struct hv_hotadd_gap *gap;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700792 unsigned long residual, new_inc;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700793 int ret = 0;
794 unsigned long flags;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700795
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700796 spin_lock_irqsave(&dm_device.ha_lock, flags);
797 list_for_each_entry(has, &dm_device.ha_region_list, list) {
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700798 /*
799 * If the pfn range we are dealing with is not in the current
800 * "hot add block", move on.
801 */
Vitaly Kuznetsov77c0c972016-04-30 19:21:35 -0700802 if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700803 continue;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700804
805 /*
806 * If the current start pfn is not where the covered_end
807 * is, create a gap and update covered_end_pfn.
808 */
809 if (has->covered_end_pfn != start_pfn) {
810 gap = kzalloc(sizeof(struct hv_hotadd_gap), GFP_ATOMIC);
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700811 if (!gap) {
812 ret = -ENOMEM;
813 break;
814 }
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700815
816 INIT_LIST_HEAD(&gap->list);
817 gap->start_pfn = has->covered_end_pfn;
818 gap->end_pfn = start_pfn;
819 list_add_tail(&gap->list, &has->gap_list);
820
821 has->covered_end_pfn = start_pfn;
822 }
823
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700824 /*
825 * If the current hot add-request extends beyond
826 * our current limit; extend it.
827 */
828 if ((start_pfn + pfn_cnt) > has->end_pfn) {
829 residual = (start_pfn + pfn_cnt - has->end_pfn);
830 /*
831 * Extend the region by multiples of HA_CHUNK.
832 */
833 new_inc = (residual / HA_CHUNK) * HA_CHUNK;
834 if (residual % HA_CHUNK)
835 new_inc += HA_CHUNK;
836
837 has->end_pfn += new_inc;
838 }
839
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700840 ret = 1;
841 break;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700842 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700843 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700844
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700845 return ret;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700846}
847
848static unsigned long handle_pg_range(unsigned long pg_start,
849 unsigned long pg_count)
850{
851 unsigned long start_pfn = pg_start;
852 unsigned long pfn_cnt = pg_count;
853 unsigned long size;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700854 struct hv_hotadd_state *has;
855 unsigned long pgs_ol = 0;
856 unsigned long old_covered_state;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700857 unsigned long res = 0, flags;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700858
Alex Ngb3bb97b2016-11-06 13:14:09 -0800859 pr_debug("Hot adding %lu pages starting at pfn 0x%lx.\n", pg_count,
860 pg_start);
861
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700862 spin_lock_irqsave(&dm_device.ha_lock, flags);
863 list_for_each_entry(has, &dm_device.ha_region_list, list) {
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700864 /*
865 * If the pfn range we are dealing with is not in the current
866 * "hot add block", move on.
867 */
Vitaly Kuznetsov77c0c972016-04-30 19:21:35 -0700868 if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700869 continue;
870
871 old_covered_state = has->covered_end_pfn;
872
873 if (start_pfn < has->ha_end_pfn) {
874 /*
875 * This is the case where we are backing pages
876 * in an already hot added region. Bring
877 * these pages online first.
878 */
879 pgs_ol = has->ha_end_pfn - start_pfn;
880 if (pgs_ol > pfn_cnt)
881 pgs_ol = pfn_cnt;
Vitaly Kuznetsovd6cbd2c2015-03-27 09:10:11 -0700882
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700883 has->covered_end_pfn += pgs_ol;
884 pfn_cnt -= pgs_ol;
Vitaly Kuznetsovd6cbd2c2015-03-27 09:10:11 -0700885 /*
886 * Check if the corresponding memory block is already
Vitaly Kuznetsovda8ced32019-01-04 15:19:42 +0100887 * online. It is possible to observe struct pages still
888 * being uninitialized here so check section instead.
889 * In case the section is online we need to bring the
890 * rest of pfns (which were not backed previously)
891 * online too.
Vitaly Kuznetsovd6cbd2c2015-03-27 09:10:11 -0700892 */
893 if (start_pfn > has->start_pfn &&
Vitaly Kuznetsovda8ced32019-01-04 15:19:42 +0100894 online_section_nr(pfn_to_section_nr(start_pfn)))
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700895 hv_bring_pgs_online(has, start_pfn, pgs_ol);
Vitaly Kuznetsovd6cbd2c2015-03-27 09:10:11 -0700896
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700897 }
898
899 if ((has->ha_end_pfn < has->end_pfn) && (pfn_cnt > 0)) {
900 /*
901 * We have some residual hot add range
902 * that needs to be hot added; hot add
903 * it now. Hot add a multiple of
904 * of HA_CHUNK that fully covers the pages
905 * we have.
906 */
907 size = (has->end_pfn - has->ha_end_pfn);
908 if (pfn_cnt <= size) {
909 size = ((pfn_cnt / HA_CHUNK) * HA_CHUNK);
910 if (pfn_cnt % HA_CHUNK)
911 size += HA_CHUNK;
912 } else {
913 pfn_cnt = size;
914 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700915 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700916 hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700917 spin_lock_irqsave(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700918 }
919 /*
920 * If we managed to online any pages that were given to us,
921 * we declare success.
922 */
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700923 res = has->covered_end_pfn - old_covered_state;
924 break;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700925 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700926 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700927
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700928 return res;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700929}
930
931static unsigned long process_hot_add(unsigned long pg_start,
932 unsigned long pfn_cnt,
933 unsigned long rg_start,
934 unsigned long rg_size)
935{
936 struct hv_hotadd_state *ha_region = NULL;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700937 int covered;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700938 unsigned long flags;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700939
940 if (pfn_cnt == 0)
941 return 0;
942
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700943 if (!dm_device.host_specified_ha_region) {
944 covered = pfn_covered(pg_start, pfn_cnt);
945 if (covered < 0)
946 return 0;
947
948 if (covered)
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700949 goto do_pg_range;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700950 }
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700951
952 /*
953 * If the host has specified a hot-add range; deal with it first.
954 */
955
K. Y. Srinivasan647965a2013-03-29 07:36:11 -0700956 if (rg_size != 0) {
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700957 ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
958 if (!ha_region)
959 return 0;
960
961 INIT_LIST_HEAD(&ha_region->list);
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700962 INIT_LIST_HEAD(&ha_region->gap_list);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700963
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700964 ha_region->start_pfn = rg_start;
965 ha_region->ha_end_pfn = rg_start;
Vitaly Kuznetsov7cf3b792016-08-24 16:23:09 -0700966 ha_region->covered_start_pfn = pg_start;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700967 ha_region->covered_end_pfn = pg_start;
968 ha_region->end_pfn = rg_start + rg_size;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700969
970 spin_lock_irqsave(&dm_device.ha_lock, flags);
971 list_add_tail(&ha_region->list, &dm_device.ha_region_list);
972 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700973 }
974
975do_pg_range:
976 /*
977 * Process the page range specified; bringing them
978 * online if possible.
979 */
980 return handle_pg_range(pg_start, pfn_cnt);
981}
982
983#endif
984
985static void hot_add_req(struct work_struct *dummy)
986{
987 struct dm_hot_add_response resp;
988#ifdef CONFIG_MEMORY_HOTPLUG
989 unsigned long pg_start, pfn_cnt;
990 unsigned long rg_start, rg_sz;
991#endif
992 struct hv_dynmem_device *dm = &dm_device;
993
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800994 memset(&resp, 0, sizeof(struct dm_hot_add_response));
995 resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
996 resp.hdr.size = sizeof(struct dm_hot_add_response);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800997
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700998#ifdef CONFIG_MEMORY_HOTPLUG
999 pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
1000 pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001001
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001002 rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
1003 rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
1004
1005 if ((rg_start == 0) && (!dm->host_specified_ha_region)) {
1006 unsigned long region_size;
1007 unsigned long region_start;
1008
1009 /*
1010 * The host has not specified the hot-add region.
1011 * Based on the hot-add page range being specified,
1012 * compute a hot-add region that can cover the pages
1013 * that need to be hot-added while ensuring the alignment
1014 * and size requirements of Linux as it relates to hot-add.
1015 */
1016 region_start = pg_start;
1017 region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK;
1018 if (pfn_cnt % HA_CHUNK)
1019 region_size += HA_CHUNK;
1020
1021 region_start = (pg_start / HA_CHUNK) * HA_CHUNK;
1022
1023 rg_start = region_start;
1024 rg_sz = region_size;
1025 }
1026
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -07001027 if (do_hot_add)
1028 resp.page_count = process_hot_add(pg_start, pfn_cnt,
1029 rg_start, rg_sz);
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -08001030
1031 dm->num_pages_added += resp.page_count;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001032#endif
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -07001033 /*
1034 * The result field of the response structure has the
1035 * following semantics:
1036 *
1037 * 1. If all or some pages hot-added: Guest should return success.
1038 *
1039 * 2. If no pages could be hot-added:
1040 *
1041 * If the guest returns success, then the host
1042 * will not attempt any further hot-add operations. This
1043 * signifies a permanent failure.
1044 *
1045 * If the guest returns failure, then this failure will be
1046 * treated as a transient failure and the host may retry the
1047 * hot-add operation after some delay.
1048 */
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001049 if (resp.page_count > 0)
1050 resp.result = 1;
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -07001051 else if (!do_hot_add)
1052 resp.result = 1;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001053 else
1054 resp.result = 0;
1055
Dexuan Cui25bd2b22019-11-19 23:16:05 -08001056 if (!do_hot_add || resp.page_count == 0) {
1057 if (!allow_hibernation)
1058 pr_err("Memory hot add failed\n");
1059 else
1060 pr_info("Ignore hot-add request!\n");
1061 }
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001062
1063 dm->state = DM_INITIALIZED;
K. Y. Srinivasan20138d62013-07-17 17:27:27 -07001064 resp.hdr.trans_id = atomic_inc_return(&trans_id);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001065 vmbus_sendpacket(dm->dev->channel, &resp,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001066 sizeof(struct dm_hot_add_response),
1067 (unsigned long)NULL,
1068 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001069}
1070
1071static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
1072{
K. Y. Srinivasan6427a0d72012-12-06 11:06:54 -08001073 struct dm_info_header *info_hdr;
1074
1075 info_hdr = (struct dm_info_header *)msg->info;
1076
1077 switch (info_hdr->type) {
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001078 case INFO_TYPE_MAX_PAGE_CNT:
Alex Ng85000962016-11-06 13:14:12 -08001079 if (info_hdr->data_size == sizeof(__u64)) {
1080 __u64 *max_page_count = (__u64 *)&info_hdr[1];
1081
Alex Ng7b6e54b52017-08-06 13:12:54 -07001082 pr_info("Max. dynamic memory size: %llu MB\n",
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001083 (*max_page_count) >> (20 - HV_HYP_PAGE_SHIFT));
Alex Ng85000962016-11-06 13:14:12 -08001084 }
1085
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001086 break;
1087 default:
Vitaly Kuznetsov223e1e4d2018-03-04 22:17:19 -07001088 pr_warn("Received Unknown type: %d\n", info_hdr->type);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001089 }
1090}
1091
Wei Yongjuna6025a22013-03-20 23:25:59 +08001092static unsigned long compute_balloon_floor(void)
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001093{
1094 unsigned long min_pages;
Arun KSca79b0c2018-12-28 00:34:29 -08001095 unsigned long nr_pages = totalram_pages();
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001096#define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
1097 /* Simple continuous piecewiese linear function:
1098 * max MiB -> min MiB gradient
1099 * 0 0
1100 * 16 16
1101 * 32 24
1102 * 128 72 (1/2)
1103 * 512 168 (1/4)
1104 * 2048 360 (1/8)
Vitaly Kuznetsov7fb0e1a2015-03-27 09:10:12 -07001105 * 8192 744 (1/16)
1106 * 32768 1512 (1/32)
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001107 */
Arun KS3d6357d2018-12-28 00:34:20 -08001108 if (nr_pages < MB2PAGES(128))
1109 min_pages = MB2PAGES(8) + (nr_pages >> 1);
1110 else if (nr_pages < MB2PAGES(512))
1111 min_pages = MB2PAGES(40) + (nr_pages >> 2);
1112 else if (nr_pages < MB2PAGES(2048))
1113 min_pages = MB2PAGES(104) + (nr_pages >> 3);
1114 else if (nr_pages < MB2PAGES(8192))
1115 min_pages = MB2PAGES(232) + (nr_pages >> 4);
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001116 else
Arun KS3d6357d2018-12-28 00:34:20 -08001117 min_pages = MB2PAGES(488) + (nr_pages >> 5);
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001118#undef MB2PAGES
1119 return min_pages;
1120}
1121
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001122/*
1123 * Post our status as it relates memory pressure to the
1124 * host. Host expects the guests to post this status
1125 * periodically at 1 second intervals.
1126 *
1127 * The metrics specified in this protocol are very Windows
1128 * specific and so we cook up numbers here to convey our memory
1129 * pressure.
1130 */
1131
1132static void post_status(struct hv_dynmem_device *dm)
1133{
1134 struct dm_status status;
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001135 unsigned long now = jiffies;
1136 unsigned long last_post = last_post_time;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001137
K. Y. Srinivasane500d152013-02-08 15:57:15 -08001138 if (pressure_report_delay > 0) {
1139 --pressure_report_delay;
1140 return;
1141 }
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001142
1143 if (!time_after(now, (last_post_time + HZ)))
1144 return;
1145
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001146 memset(&status, 0, sizeof(struct dm_status));
1147 status.hdr.type = DM_STATUS_REPORT;
1148 status.hdr.size = sizeof(struct dm_status);
1149 status.hdr.trans_id = atomic_inc_return(&trans_id);
1150
K. Y. Srinivasan07315722013-01-25 16:18:47 -08001151 /*
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -08001152 * The host expects the guest to report free and committed memory.
1153 * Furthermore, the host expects the pressure information to include
1154 * the ballooned out pages. For a given amount of memory that we are
1155 * managing we need to compute a floor below which we should not
1156 * balloon. Compute this and add it to the pressure report.
1157 * We also need to report all offline pages (num_pages_added -
1158 * num_pages_onlined) as committed to the host, otherwise it can try
1159 * asking us to balloon them out.
K. Y. Srinivasan07315722013-01-25 16:18:47 -08001160 */
Alex Ngb605c2d2016-08-24 16:23:13 -07001161 status.num_avail = si_mem_available();
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001162 status.num_committed = vm_memory_committed() +
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -08001163 dm->num_pages_ballooned +
1164 (dm->num_pages_added > dm->num_pages_onlined ?
1165 dm->num_pages_added - dm->num_pages_onlined : 0) +
1166 compute_balloon_floor();
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001167
Vitaly Kuznetsovcf21be92018-03-04 22:17:22 -07001168 trace_balloon_status(status.num_avail, status.num_committed,
1169 vm_memory_committed(), dm->num_pages_ballooned,
1170 dm->num_pages_added, dm->num_pages_onlined);
K. Y. Srinivasanc5e22542013-07-14 22:38:12 -07001171 /*
1172 * If our transaction ID is no longer current, just don't
1173 * send the status. This can happen if we were interrupted
1174 * after we picked our transaction ID.
1175 */
1176 if (status.hdr.trans_id != atomic_read(&trans_id))
1177 return;
1178
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001179 /*
1180 * If the last post time that we sampled has changed,
1181 * we have raced, don't post the status.
1182 */
1183 if (last_post != last_post_time)
1184 return;
1185
1186 last_post_time = jiffies;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001187 vmbus_sendpacket(dm->dev->channel, &status,
1188 sizeof(struct dm_status),
1189 (unsigned long)NULL,
1190 VM_PKT_DATA_INBAND, 0);
1191
1192}
1193
Greg Kroah-Hartman989623c2012-11-21 12:46:40 -08001194static void free_balloon_pages(struct hv_dynmem_device *dm,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001195 union dm_mem_page_range *range_array)
1196{
1197 int num_pages = range_array->finfo.page_cnt;
1198 __u64 start_frame = range_array->finfo.start_page;
1199 struct page *pg;
1200 int i;
1201
1202 for (i = 0; i < num_pages; i++) {
1203 pg = pfn_to_page(i + start_frame);
David Hildenbrandfae42c42019-03-05 15:42:36 -08001204 __ClearPageOffline(pg);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001205 __free_page(pg);
1206 dm->num_pages_ballooned--;
1207 }
1208}
1209
1210
1211
Vitaly Kuznetsov797f88c92015-03-31 11:16:41 -07001212static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
1213 unsigned int num_pages,
1214 struct dm_balloon_response *bl_resp,
1215 int alloc_unit)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001216{
David Hildenbrandfae42c42019-03-05 15:42:36 -08001217 unsigned int i, j;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001218 struct page *pg;
1219
1220 if (num_pages < alloc_unit)
1221 return 0;
1222
1223 for (i = 0; (i * alloc_unit) < num_pages; i++) {
1224 if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001225 HV_HYP_PAGE_SIZE)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001226 return i * alloc_unit;
1227
1228 /*
1229 * We execute this code in a thread context. Furthermore,
1230 * we don't want the kernel to try too hard.
1231 */
1232 pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
1233 __GFP_NOMEMALLOC | __GFP_NOWARN,
1234 get_order(alloc_unit << PAGE_SHIFT));
1235
Vitaly Kuznetsov0a1a86a2015-03-27 09:10:13 -07001236 if (!pg)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001237 return i * alloc_unit;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001238
1239 dm->num_pages_ballooned += alloc_unit;
1240
K. Y. Srinivasanf766dc12013-03-18 13:51:37 -07001241 /*
1242 * If we allocatted 2M pages; split them so we
1243 * can free them in any order we get.
1244 */
1245
1246 if (alloc_unit != 1)
1247 split_page(pg, get_order(alloc_unit << PAGE_SHIFT));
1248
David Hildenbrandfae42c42019-03-05 15:42:36 -08001249 /* mark all pages offline */
1250 for (j = 0; j < (1 << get_order(alloc_unit << PAGE_SHIFT)); j++)
1251 __SetPageOffline(pg + j);
1252
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001253 bl_resp->range_count++;
1254 bl_resp->range_array[i].finfo.start_page =
1255 page_to_pfn(pg);
1256 bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
1257 bl_resp->hdr.size += sizeof(union dm_mem_page_range);
1258
1259 }
1260
1261 return num_pages;
1262}
1263
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001264static void balloon_up(struct work_struct *dummy)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001265{
Vitaly Kuznetsov797f88c92015-03-31 11:16:41 -07001266 unsigned int num_pages = dm_device.balloon_wrk.num_pages;
1267 unsigned int num_ballooned = 0;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001268 struct dm_balloon_response *bl_resp;
1269 int alloc_unit;
1270 int ret;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001271 bool done = false;
1272 int i;
Alex Ngb605c2d2016-08-24 16:23:13 -07001273 long avail_pages;
Vitaly Kuznetsov530d15b2015-02-28 11:39:00 -08001274 unsigned long floor;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001275
Dexuan Cuif6712232014-11-24 20:32:43 -08001276 /* The host balloons pages in 2M granularity. */
1277 WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001278
1279 /*
K. Y. Srinivasanf766dc12013-03-18 13:51:37 -07001280 * We will attempt 2M allocations. However, if we fail to
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001281 * allocate 2M chunks, we will go back to PAGE_SIZE allocations.
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001282 */
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001283 alloc_unit = PAGES_IN_2M;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001284
Alex Ngb605c2d2016-08-24 16:23:13 -07001285 avail_pages = si_mem_available();
Vitaly Kuznetsov530d15b2015-02-28 11:39:00 -08001286 floor = compute_balloon_floor();
1287
1288 /* Refuse to balloon below the floor, keep the 2M granularity. */
Alex Ngb605c2d2016-08-24 16:23:13 -07001289 if (avail_pages < num_pages || avail_pages - num_pages < floor) {
Alex Ngb3bb97b2016-11-06 13:14:09 -08001290 pr_warn("Balloon request will be partially fulfilled. %s\n",
1291 avail_pages < num_pages ? "Not enough memory." :
1292 "Balloon floor reached.");
1293
Alex Ngb605c2d2016-08-24 16:23:13 -07001294 num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
Vitaly Kuznetsov530d15b2015-02-28 11:39:00 -08001295 num_pages -= num_pages % PAGES_IN_2M;
1296 }
1297
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001298 while (!done) {
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001299 memset(balloon_up_send_buffer, 0, HV_HYP_PAGE_SIZE);
Dexuan Cui1fed17d2019-06-14 18:42:17 +00001300 bl_resp = (struct dm_balloon_response *)balloon_up_send_buffer;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001301 bl_resp->hdr.type = DM_BALLOON_RESPONSE;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001302 bl_resp->hdr.size = sizeof(struct dm_balloon_response);
1303 bl_resp->more_pages = 1;
1304
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001305 num_pages -= num_ballooned;
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001306 num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
Vitaly Kuznetsov0a1a86a2015-03-27 09:10:13 -07001307 bl_resp, alloc_unit);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001308
Dexuan Cuif6712232014-11-24 20:32:43 -08001309 if (alloc_unit != 1 && num_ballooned == 0) {
K. Y. Srinivasanf766dc12013-03-18 13:51:37 -07001310 alloc_unit = 1;
1311 continue;
1312 }
1313
Vitaly Kuznetsov0a1a86a2015-03-27 09:10:13 -07001314 if (num_ballooned == 0 || num_ballooned == num_pages) {
Alex Ngb3bb97b2016-11-06 13:14:09 -08001315 pr_debug("Ballooned %u out of %u requested pages.\n",
1316 num_pages, dm_device.balloon_wrk.num_pages);
1317
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001318 bl_resp->more_pages = 0;
1319 done = true;
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001320 dm_device.state = DM_INITIALIZED;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001321 }
1322
1323 /*
1324 * We are pushing a lot of data through the channel;
1325 * deal with transient failures caused because of the
1326 * lack of space in the ring buffer.
1327 */
1328
1329 do {
K. Y. Srinivasan20138d62013-07-17 17:27:27 -07001330 bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001331 ret = vmbus_sendpacket(dm_device.dev->channel,
1332 bl_resp,
1333 bl_resp->hdr.size,
1334 (unsigned long)NULL,
1335 VM_PKT_DATA_INBAND, 0);
1336
1337 if (ret == -EAGAIN)
1338 msleep(20);
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001339 post_status(&dm_device);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001340 } while (ret == -EAGAIN);
1341
1342 if (ret) {
1343 /*
1344 * Free up the memory we allocatted.
1345 */
Vitaly Kuznetsov223e1e4d2018-03-04 22:17:19 -07001346 pr_err("Balloon response failed\n");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001347
1348 for (i = 0; i < bl_resp->range_count; i++)
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001349 free_balloon_pages(&dm_device,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001350 &bl_resp->range_array[i]);
1351
1352 done = true;
1353 }
1354 }
1355
1356}
1357
1358static void balloon_down(struct hv_dynmem_device *dm,
1359 struct dm_unballoon_request *req)
1360{
1361 union dm_mem_page_range *range_array = req->range_array;
1362 int range_count = req->range_count;
1363 struct dm_unballoon_response resp;
1364 int i;
Alex Ngb3bb97b2016-11-06 13:14:09 -08001365 unsigned int prev_pages_ballooned = dm->num_pages_ballooned;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001366
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001367 for (i = 0; i < range_count; i++) {
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001368 free_balloon_pages(dm, &range_array[i]);
K. Y. Srinivasanab3de222015-01-09 23:54:31 -08001369 complete(&dm_device.config_event);
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001370 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001371
Alex Ngb3bb97b2016-11-06 13:14:09 -08001372 pr_debug("Freed %u ballooned pages.\n",
1373 prev_pages_ballooned - dm->num_pages_ballooned);
1374
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001375 if (req->more_pages == 1)
1376 return;
1377
1378 memset(&resp, 0, sizeof(struct dm_unballoon_response));
1379 resp.hdr.type = DM_UNBALLOON_RESPONSE;
1380 resp.hdr.trans_id = atomic_inc_return(&trans_id);
1381 resp.hdr.size = sizeof(struct dm_unballoon_response);
1382
1383 vmbus_sendpacket(dm_device.dev->channel, &resp,
1384 sizeof(struct dm_unballoon_response),
1385 (unsigned long)NULL,
1386 VM_PKT_DATA_INBAND, 0);
1387
1388 dm->state = DM_INITIALIZED;
1389}
1390
1391static void balloon_onchannelcallback(void *context);
1392
1393static int dm_thread_func(void *dm_dev)
1394{
1395 struct hv_dynmem_device *dm = dm_dev;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001396
1397 while (!kthread_should_stop()) {
K. Y. Srinivasanab3de222015-01-09 23:54:31 -08001398 wait_for_completion_interruptible_timeout(
K. Y. Srinivasan5dba4c52014-02-13 16:24:33 -08001399 &dm_device.config_event, 1*HZ);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001400 /*
1401 * The host expects us to post information on the memory
1402 * pressure every second.
1403 */
K. Y. Srinivasanab3de222015-01-09 23:54:31 -08001404 reinit_completion(&dm_device.config_event);
1405 post_status(dm);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001406 }
1407
1408 return 0;
1409}
1410
1411
1412static void version_resp(struct hv_dynmem_device *dm,
1413 struct dm_version_response *vresp)
1414{
1415 struct dm_version_request version_req;
1416 int ret;
1417
1418 if (vresp->is_accepted) {
1419 /*
1420 * We are done; wakeup the
1421 * context waiting for version
1422 * negotiation.
1423 */
1424 complete(&dm->host_event);
1425 return;
1426 }
1427 /*
1428 * If there are more versions to try, continue
1429 * with negotiations; if not
1430 * shutdown the service since we are not able
1431 * to negotiate a suitable version number
1432 * with the host.
1433 */
1434 if (dm->next_version == 0)
1435 goto version_error;
1436
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001437 memset(&version_req, 0, sizeof(struct dm_version_request));
1438 version_req.hdr.type = DM_VERSION_REQUEST;
1439 version_req.hdr.size = sizeof(struct dm_version_request);
1440 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
Alex Ngb6ddeae2015-08-01 16:08:13 -07001441 version_req.version.version = dm->next_version;
Alex Ngb3bb97b2016-11-06 13:14:09 -08001442 dm->version = version_req.version.version;
Alex Ngb6ddeae2015-08-01 16:08:13 -07001443
1444 /*
1445 * Set the next version to try in case current version fails.
1446 * Win7 protocol ought to be the last one to try.
1447 */
1448 switch (version_req.version.version) {
1449 case DYNMEM_PROTOCOL_VERSION_WIN8:
1450 dm->next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
1451 version_req.is_last_attempt = 0;
1452 break;
1453 default:
1454 dm->next_version = 0;
1455 version_req.is_last_attempt = 1;
1456 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001457
1458 ret = vmbus_sendpacket(dm->dev->channel, &version_req,
1459 sizeof(struct dm_version_request),
1460 (unsigned long)NULL,
1461 VM_PKT_DATA_INBAND, 0);
1462
1463 if (ret)
1464 goto version_error;
1465
1466 return;
1467
1468version_error:
1469 dm->state = DM_INIT_ERROR;
1470 complete(&dm->host_event);
1471}
1472
1473static void cap_resp(struct hv_dynmem_device *dm,
1474 struct dm_capabilities_resp_msg *cap_resp)
1475{
1476 if (!cap_resp->is_accepted) {
Vitaly Kuznetsov223e1e4d2018-03-04 22:17:19 -07001477 pr_err("Capabilities not accepted by host\n");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001478 dm->state = DM_INIT_ERROR;
1479 }
1480 complete(&dm->host_event);
1481}
1482
1483static void balloon_onchannelcallback(void *context)
1484{
1485 struct hv_device *dev = context;
1486 u32 recvlen;
1487 u64 requestid;
1488 struct dm_message *dm_msg;
1489 struct dm_header *dm_hdr;
1490 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001491 struct dm_balloon *bal_msg;
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001492 struct dm_hot_add *ha_msg;
1493 union dm_mem_page_range *ha_pg_range;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001494 union dm_mem_page_range *ha_region;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001495
1496 memset(recv_buffer, 0, sizeof(recv_buffer));
1497 vmbus_recvpacket(dev->channel, recv_buffer,
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001498 HV_HYP_PAGE_SIZE, &recvlen, &requestid);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001499
1500 if (recvlen > 0) {
1501 dm_msg = (struct dm_message *)recv_buffer;
1502 dm_hdr = &dm_msg->hdr;
1503
1504 switch (dm_hdr->type) {
1505 case DM_VERSION_RESPONSE:
1506 version_resp(dm,
1507 (struct dm_version_response *)dm_msg);
1508 break;
1509
1510 case DM_CAPABILITIES_RESPONSE:
1511 cap_resp(dm,
1512 (struct dm_capabilities_resp_msg *)dm_msg);
1513 break;
1514
1515 case DM_BALLOON_REQUEST:
Dexuan Cui25bd2b22019-11-19 23:16:05 -08001516 if (allow_hibernation) {
1517 pr_info("Ignore balloon-up request!\n");
1518 break;
1519 }
1520
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001521 if (dm->state == DM_BALLOON_UP)
1522 pr_warn("Currently ballooning\n");
1523 bal_msg = (struct dm_balloon *)recv_buffer;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001524 dm->state = DM_BALLOON_UP;
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001525 dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
1526 schedule_work(&dm_device.balloon_wrk.wrk);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001527 break;
1528
1529 case DM_UNBALLOON_REQUEST:
Dexuan Cui25bd2b22019-11-19 23:16:05 -08001530 if (allow_hibernation) {
1531 pr_info("Ignore balloon-down request!\n");
1532 break;
1533 }
1534
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001535 dm->state = DM_BALLOON_DOWN;
1536 balloon_down(dm,
1537 (struct dm_unballoon_request *)recv_buffer);
1538 break;
1539
1540 case DM_MEM_HOT_ADD_REQUEST:
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001541 if (dm->state == DM_HOT_ADD)
1542 pr_warn("Currently hot-adding\n");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001543 dm->state = DM_HOT_ADD;
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001544 ha_msg = (struct dm_hot_add *)recv_buffer;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001545 if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
1546 /*
1547 * This is a normal hot-add request specifying
1548 * hot-add memory.
1549 */
Vitaly Kuznetsovd19a55d2016-04-30 19:21:36 -07001550 dm->host_specified_ha_region = false;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001551 ha_pg_range = &ha_msg->range;
1552 dm->ha_wrk.ha_page_range = *ha_pg_range;
1553 dm->ha_wrk.ha_region_range.page_range = 0;
1554 } else {
1555 /*
1556 * Host is specifying that we first hot-add
1557 * a region and then partially populate this
1558 * region.
1559 */
1560 dm->host_specified_ha_region = true;
1561 ha_pg_range = &ha_msg->range;
1562 ha_region = &ha_pg_range[1];
1563 dm->ha_wrk.ha_page_range = *ha_pg_range;
1564 dm->ha_wrk.ha_region_range = *ha_region;
1565 }
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001566 schedule_work(&dm_device.ha_wrk.wrk);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001567 break;
1568
1569 case DM_INFO_MESSAGE:
1570 process_info(dm, (struct dm_info_msg *)dm_msg);
1571 break;
1572
1573 default:
Vitaly Kuznetsov223e1e4d2018-03-04 22:17:19 -07001574 pr_warn("Unhandled message: type: %d\n", dm_hdr->type);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001575
1576 }
1577 }
1578
1579}
1580
Dexuan Cui221f6df2019-06-14 18:42:30 +00001581static int balloon_connect_vsp(struct hv_device *dev)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001582{
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001583 struct dm_version_request version_req;
1584 struct dm_capabilities cap_msg;
Dexuan Cui221f6df2019-06-14 18:42:30 +00001585 unsigned long t;
1586 int ret;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001587
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001588 ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
Dexuan Cui221f6df2019-06-14 18:42:30 +00001589 balloon_onchannelcallback, dev);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001590 if (ret)
Dexuan Cui1fed17d2019-06-14 18:42:17 +00001591 return ret;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001592
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001593 /*
1594 * Initiate the hand shake with the host and negotiate
1595 * a version that the host can support. We start with the
1596 * highest version number and go down if the host cannot
1597 * support it.
1598 */
1599 memset(&version_req, 0, sizeof(struct dm_version_request));
1600 version_req.hdr.type = DM_VERSION_REQUEST;
1601 version_req.hdr.size = sizeof(struct dm_version_request);
1602 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
Alex Ngb6ddeae2015-08-01 16:08:13 -07001603 version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN10;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001604 version_req.is_last_attempt = 0;
Alex Ngb3bb97b2016-11-06 13:14:09 -08001605 dm_device.version = version_req.version.version;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001606
1607 ret = vmbus_sendpacket(dev->channel, &version_req,
Dexuan Cui221f6df2019-06-14 18:42:30 +00001608 sizeof(struct dm_version_request),
1609 (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001610 if (ret)
Dexuan Cui221f6df2019-06-14 18:42:30 +00001611 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001612
1613 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1614 if (t == 0) {
1615 ret = -ETIMEDOUT;
Dexuan Cui221f6df2019-06-14 18:42:30 +00001616 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001617 }
1618
1619 /*
1620 * If we could not negotiate a compatible version with the host
1621 * fail the probe function.
1622 */
1623 if (dm_device.state == DM_INIT_ERROR) {
Dexuan Cui221f6df2019-06-14 18:42:30 +00001624 ret = -EPROTO;
1625 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001626 }
Alex Ngb3bb97b2016-11-06 13:14:09 -08001627
1628 pr_info("Using Dynamic Memory protocol version %u.%u\n",
1629 DYNMEM_MAJOR_VERSION(dm_device.version),
1630 DYNMEM_MINOR_VERSION(dm_device.version));
1631
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001632 /*
1633 * Now submit our capabilities to the host.
1634 */
1635 memset(&cap_msg, 0, sizeof(struct dm_capabilities));
1636 cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
1637 cap_msg.hdr.size = sizeof(struct dm_capabilities);
1638 cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
1639
Dexuan Cui25bd2b22019-11-19 23:16:05 -08001640 /*
1641 * When hibernation (i.e. virtual ACPI S4 state) is enabled, the host
1642 * currently still requires the bits to be set, so we have to add code
1643 * to fail the host's hot-add and balloon up/down requests, if any.
1644 */
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001645 cap_msg.caps.cap_bits.balloon = 1;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001646 cap_msg.caps.cap_bits.hot_add = 1;
1647
1648 /*
K. Y. Srinivasan647965a2013-03-29 07:36:11 -07001649 * Specify our alignment requirements as it relates
1650 * memory hot-add. Specify 128MB alignment.
1651 */
1652 cap_msg.caps.cap_bits.hot_add_alignment = 7;
1653
1654 /*
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001655 * Currently the host does not use these
1656 * values and we set them to what is done in the
1657 * Windows driver.
1658 */
1659 cap_msg.min_page_cnt = 0;
1660 cap_msg.max_page_number = -1;
1661
1662 ret = vmbus_sendpacket(dev->channel, &cap_msg,
Dexuan Cui221f6df2019-06-14 18:42:30 +00001663 sizeof(struct dm_capabilities),
1664 (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001665 if (ret)
Dexuan Cui221f6df2019-06-14 18:42:30 +00001666 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001667
1668 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1669 if (t == 0) {
1670 ret = -ETIMEDOUT;
Dexuan Cui221f6df2019-06-14 18:42:30 +00001671 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001672 }
1673
1674 /*
1675 * If the host does not like our capabilities,
1676 * fail the probe function.
1677 */
1678 if (dm_device.state == DM_INIT_ERROR) {
Dexuan Cui221f6df2019-06-14 18:42:30 +00001679 ret = -EPROTO;
1680 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001681 }
1682
Dexuan Cui221f6df2019-06-14 18:42:30 +00001683 return 0;
1684out:
1685 vmbus_close(dev->channel);
1686 return ret;
1687}
1688
1689static int balloon_probe(struct hv_device *dev,
1690 const struct hv_vmbus_device_id *dev_id)
1691{
1692 int ret;
1693
Dexuan Cui25bd2b22019-11-19 23:16:05 -08001694 allow_hibernation = hv_is_hibernation_supported();
1695 if (allow_hibernation)
1696 hot_add = false;
1697
Dexuan Cui221f6df2019-06-14 18:42:30 +00001698#ifdef CONFIG_MEMORY_HOTPLUG
1699 do_hot_add = hot_add;
1700#else
1701 do_hot_add = false;
1702#endif
1703 dm_device.dev = dev;
1704 dm_device.state = DM_INITIALIZING;
1705 dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN8;
1706 init_completion(&dm_device.host_event);
1707 init_completion(&dm_device.config_event);
1708 INIT_LIST_HEAD(&dm_device.ha_region_list);
1709 spin_lock_init(&dm_device.ha_lock);
1710 INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
1711 INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
1712 dm_device.host_specified_ha_region = false;
1713
1714#ifdef CONFIG_MEMORY_HOTPLUG
1715 set_online_page_callback(&hv_online_page);
1716 register_memory_notifier(&hv_memory_nb);
1717#endif
1718
1719 hv_set_drvdata(dev, &dm_device);
1720
1721 ret = balloon_connect_vsp(dev);
1722 if (ret != 0)
1723 return ret;
1724
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001725 dm_device.state = DM_INITIALIZED;
Dexuan Cui221f6df2019-06-14 18:42:30 +00001726
1727 dm_device.thread =
1728 kthread_run(dm_thread_func, &dm_device, "hv_balloon");
1729 if (IS_ERR(dm_device.thread)) {
1730 ret = PTR_ERR(dm_device.thread);
1731 goto probe_error;
1732 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001733
1734 return 0;
1735
Dexuan Cui221f6df2019-06-14 18:42:30 +00001736probe_error:
Dexuan Cui25bd2b22019-11-19 23:16:05 -08001737 dm_device.state = DM_INIT_ERROR;
1738 dm_device.thread = NULL;
Dexuan Cui221f6df2019-06-14 18:42:30 +00001739 vmbus_close(dev->channel);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001740#ifdef CONFIG_MEMORY_HOTPLUG
Dexuan Cui221f6df2019-06-14 18:42:30 +00001741 unregister_memory_notifier(&hv_memory_nb);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001742 restore_online_page_callback(&hv_online_page);
1743#endif
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001744 return ret;
1745}
1746
1747static int balloon_remove(struct hv_device *dev)
1748{
1749 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -07001750 struct hv_hotadd_state *has, *tmp;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -07001751 struct hv_hotadd_gap *gap, *tmp_gap;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -07001752 unsigned long flags;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001753
1754 if (dm->num_pages_ballooned != 0)
1755 pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
1756
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001757 cancel_work_sync(&dm->balloon_wrk.wrk);
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001758 cancel_work_sync(&dm->ha_wrk.wrk);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001759
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001760 kthread_stop(dm->thread);
Dexuan Cui221f6df2019-06-14 18:42:30 +00001761 vmbus_close(dev->channel);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001762#ifdef CONFIG_MEMORY_HOTPLUG
K. Y. Srinivasan22f88472015-01-09 23:54:30 -08001763 unregister_memory_notifier(&hv_memory_nb);
Dexuan Cui221f6df2019-06-14 18:42:30 +00001764 restore_online_page_callback(&hv_online_page);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001765#endif
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -07001766 spin_lock_irqsave(&dm_device.ha_lock, flags);
1767 list_for_each_entry_safe(has, tmp, &dm->ha_region_list, list) {
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -07001768 list_for_each_entry_safe(gap, tmp_gap, &has->gap_list, list) {
1769 list_del(&gap->list);
1770 kfree(gap);
1771 }
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001772 list_del(&has->list);
1773 kfree(has);
1774 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -07001775 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001776
1777 return 0;
1778}
1779
Dexuan Cui25bd2b22019-11-19 23:16:05 -08001780static int balloon_suspend(struct hv_device *hv_dev)
1781{
1782 struct hv_dynmem_device *dm = hv_get_drvdata(hv_dev);
1783
1784 tasklet_disable(&hv_dev->channel->callback_event);
1785
1786 cancel_work_sync(&dm->balloon_wrk.wrk);
1787 cancel_work_sync(&dm->ha_wrk.wrk);
1788
1789 if (dm->thread) {
1790 kthread_stop(dm->thread);
1791 dm->thread = NULL;
1792 vmbus_close(hv_dev->channel);
1793 }
1794
1795 tasklet_enable(&hv_dev->channel->callback_event);
1796
1797 return 0;
1798
1799}
1800
1801static int balloon_resume(struct hv_device *dev)
1802{
1803 int ret;
1804
1805 dm_device.state = DM_INITIALIZING;
1806
1807 ret = balloon_connect_vsp(dev);
1808
1809 if (ret != 0)
1810 goto out;
1811
1812 dm_device.thread =
1813 kthread_run(dm_thread_func, &dm_device, "hv_balloon");
1814 if (IS_ERR(dm_device.thread)) {
1815 ret = PTR_ERR(dm_device.thread);
1816 dm_device.thread = NULL;
1817 goto close_channel;
1818 }
1819
1820 dm_device.state = DM_INITIALIZED;
1821 return 0;
1822close_channel:
1823 vmbus_close(dev->channel);
1824out:
1825 dm_device.state = DM_INIT_ERROR;
1826#ifdef CONFIG_MEMORY_HOTPLUG
1827 unregister_memory_notifier(&hv_memory_nb);
1828 restore_online_page_callback(&hv_online_page);
1829#endif
1830 return ret;
1831}
1832
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001833static const struct hv_vmbus_device_id id_table[] = {
1834 /* Dynamic Memory Class ID */
1835 /* 525074DC-8985-46e2-8057-A307DC18A502 */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -08001836 { HV_DM_GUID, },
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001837 { },
1838};
1839
1840MODULE_DEVICE_TABLE(vmbus, id_table);
1841
1842static struct hv_driver balloon_drv = {
1843 .name = "hv_balloon",
1844 .id_table = id_table,
1845 .probe = balloon_probe,
1846 .remove = balloon_remove,
Dexuan Cui25bd2b22019-11-19 23:16:05 -08001847 .suspend = balloon_suspend,
1848 .resume = balloon_resume,
Arjan van de Venaf0a5642018-06-05 13:37:49 -07001849 .driver = {
1850 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1851 },
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001852};
1853
1854static int __init init_balloon_drv(void)
1855{
1856
1857 return vmbus_driver_register(&balloon_drv);
1858}
1859
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001860module_init(init_balloon_drv);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001861
1862MODULE_DESCRIPTION("Hyper-V Balloon");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001863MODULE_LICENSE("GPL");