blob: 935904830d428646a75dcd3f45c684fa5c420696 [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
Vitaly Kuznetsovcf21be92018-03-04 22:17:22 -070028#define CREATE_TRACE_POINTS
29#include "hv_trace_balloon.h"
30
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080031/*
32 * We begin with definitions supporting the Dynamic Memory protocol
33 * with the host.
34 *
35 * Begin protocol definitions.
36 */
37
38
39
40/*
41 * Protocol versions. The low word is the minor version, the high word the major
42 * version.
43 *
44 * History:
45 * Initial version 1.0
46 * Changed to 0.1 on 2009/03/25
47 * Changes to 0.2 on 2009/05/14
48 * Changes to 0.3 on 2009/12/03
49 * Changed to 1.0 on 2011/04/05
50 */
51
52#define DYNMEM_MAKE_VERSION(Major, Minor) ((__u32)(((Major) << 16) | (Minor)))
53#define DYNMEM_MAJOR_VERSION(Version) ((__u32)(Version) >> 16)
54#define DYNMEM_MINOR_VERSION(Version) ((__u32)(Version) & 0xff)
55
56enum {
57 DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
58 DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
Alex Ngb6ddeae2015-08-01 16:08:13 -070059 DYNMEM_PROTOCOL_VERSION_3 = DYNMEM_MAKE_VERSION(2, 0),
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080060
61 DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
62 DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
Alex Ngb6ddeae2015-08-01 16:08:13 -070063 DYNMEM_PROTOCOL_VERSION_WIN10 = DYNMEM_PROTOCOL_VERSION_3,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080064
Alex Ngb6ddeae2015-08-01 16:08:13 -070065 DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN10
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080066};
67
68
69
70/*
71 * Message Types
72 */
73
74enum dm_message_type {
75 /*
76 * Version 0.3
77 */
78 DM_ERROR = 0,
79 DM_VERSION_REQUEST = 1,
80 DM_VERSION_RESPONSE = 2,
81 DM_CAPABILITIES_REPORT = 3,
82 DM_CAPABILITIES_RESPONSE = 4,
83 DM_STATUS_REPORT = 5,
84 DM_BALLOON_REQUEST = 6,
85 DM_BALLOON_RESPONSE = 7,
86 DM_UNBALLOON_REQUEST = 8,
87 DM_UNBALLOON_RESPONSE = 9,
88 DM_MEM_HOT_ADD_REQUEST = 10,
89 DM_MEM_HOT_ADD_RESPONSE = 11,
90 DM_VERSION_03_MAX = 11,
91 /*
92 * Version 1.0.
93 */
94 DM_INFO_MESSAGE = 12,
95 DM_VERSION_1_MAX = 12
96};
97
98
99/*
100 * Structures defining the dynamic memory management
101 * protocol.
102 */
103
104union dm_version {
105 struct {
106 __u16 minor_version;
107 __u16 major_version;
108 };
109 __u32 version;
110} __packed;
111
112
113union dm_caps {
114 struct {
115 __u64 balloon:1;
116 __u64 hot_add:1;
K. Y. Srinivasan647965a2013-03-29 07:36:11 -0700117 /*
118 * To support guests that may have alignment
119 * limitations on hot-add, the guest can specify
120 * its alignment requirements; a value of n
121 * represents an alignment of 2^n in mega bytes.
122 */
123 __u64 hot_add_alignment:4;
124 __u64 reservedz:58;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800125 } cap_bits;
126 __u64 caps;
127} __packed;
128
129union dm_mem_page_range {
130 struct {
131 /*
132 * The PFN number of the first page in the range.
133 * 40 bits is the architectural limit of a PFN
134 * number for AMD64.
135 */
136 __u64 start_page:40;
137 /*
138 * The number of pages in the range.
139 */
140 __u64 page_cnt:24;
141 } finfo;
142 __u64 page_range;
143} __packed;
144
145
146
147/*
148 * The header for all dynamic memory messages:
149 *
150 * type: Type of the message.
151 * size: Size of the message in bytes; including the header.
152 * trans_id: The guest is responsible for manufacturing this ID.
153 */
154
155struct dm_header {
156 __u16 type;
157 __u16 size;
158 __u32 trans_id;
159} __packed;
160
161/*
162 * A generic message format for dynamic memory.
163 * Specific message formats are defined later in the file.
164 */
165
166struct dm_message {
167 struct dm_header hdr;
168 __u8 data[]; /* enclosed message */
169} __packed;
170
171
172/*
173 * Specific message types supporting the dynamic memory protocol.
174 */
175
176/*
177 * Version negotiation message. Sent from the guest to the host.
178 * The guest is free to try different versions until the host
179 * accepts the version.
180 *
181 * dm_version: The protocol version requested.
182 * is_last_attempt: If TRUE, this is the last version guest will request.
183 * reservedz: Reserved field, set to zero.
184 */
185
186struct dm_version_request {
187 struct dm_header hdr;
188 union dm_version version;
189 __u32 is_last_attempt:1;
190 __u32 reservedz:31;
191} __packed;
192
193/*
194 * Version response message; Host to Guest and indicates
195 * if the host has accepted the version sent by the guest.
196 *
197 * is_accepted: If TRUE, host has accepted the version and the guest
198 * should proceed to the next stage of the protocol. FALSE indicates that
199 * guest should re-try with a different version.
200 *
201 * reservedz: Reserved field, set to zero.
202 */
203
204struct dm_version_response {
205 struct dm_header hdr;
206 __u64 is_accepted:1;
207 __u64 reservedz:63;
208} __packed;
209
210/*
211 * Message reporting capabilities. This is sent from the guest to the
212 * host.
213 */
214
215struct dm_capabilities {
216 struct dm_header hdr;
217 union dm_caps caps;
218 __u64 min_page_cnt;
219 __u64 max_page_number;
220} __packed;
221
222/*
223 * Response to the capabilities message. This is sent from the host to the
224 * guest. This message notifies if the host has accepted the guest's
225 * capabilities. If the host has not accepted, the guest must shutdown
226 * the service.
227 *
228 * is_accepted: Indicates if the host has accepted guest's capabilities.
229 * reservedz: Must be 0.
230 */
231
232struct dm_capabilities_resp_msg {
233 struct dm_header hdr;
234 __u64 is_accepted:1;
235 __u64 reservedz:63;
236} __packed;
237
238/*
239 * This message is used to report memory pressure from the guest.
240 * This message is not part of any transaction and there is no
241 * response to this message.
242 *
243 * num_avail: Available memory in pages.
244 * num_committed: Committed memory in pages.
245 * page_file_size: The accumulated size of all page files
246 * in the system in pages.
247 * zero_free: The nunber of zero and free pages.
248 * page_file_writes: The writes to the page file in pages.
249 * io_diff: An indicator of file cache efficiency or page file activity,
250 * calculated as File Cache Page Fault Count - Page Read Count.
251 * This value is in pages.
252 *
253 * Some of these metrics are Windows specific and fortunately
254 * the algorithm on the host side that computes the guest memory
255 * pressure only uses num_committed value.
256 */
257
258struct dm_status {
259 struct dm_header hdr;
260 __u64 num_avail;
261 __u64 num_committed;
262 __u64 page_file_size;
263 __u64 zero_free;
264 __u32 page_file_writes;
265 __u32 io_diff;
266} __packed;
267
268
269/*
270 * Message to ask the guest to allocate memory - balloon up message.
271 * This message is sent from the host to the guest. The guest may not be
272 * able to allocate as much memory as requested.
273 *
274 * num_pages: number of pages to allocate.
275 */
276
277struct dm_balloon {
278 struct dm_header hdr;
279 __u32 num_pages;
280 __u32 reservedz;
281} __packed;
282
283
284/*
285 * Balloon response message; this message is sent from the guest
286 * to the host in response to the balloon message.
287 *
288 * reservedz: Reserved; must be set to zero.
289 * more_pages: If FALSE, this is the last message of the transaction.
290 * if TRUE there will atleast one more message from the guest.
291 *
292 * range_count: The number of ranges in the range array.
293 *
294 * range_array: An array of page ranges returned to the host.
295 *
296 */
297
298struct dm_balloon_response {
299 struct dm_header hdr;
300 __u32 reservedz;
301 __u32 more_pages:1;
302 __u32 range_count:31;
303 union dm_mem_page_range range_array[];
304} __packed;
305
306/*
307 * Un-balloon message; this message is sent from the host
308 * to the guest to give guest more memory.
309 *
310 * more_pages: If FALSE, this is the last message of the transaction.
311 * if TRUE there will atleast one more message from the guest.
312 *
313 * reservedz: Reserved; must be set to zero.
314 *
315 * range_count: The number of ranges in the range array.
316 *
317 * range_array: An array of page ranges returned to the host.
318 *
319 */
320
321struct dm_unballoon_request {
322 struct dm_header hdr;
323 __u32 more_pages:1;
324 __u32 reservedz:31;
325 __u32 range_count;
326 union dm_mem_page_range range_array[];
327} __packed;
328
329/*
330 * Un-balloon response message; this message is sent from the guest
331 * to the host in response to an unballoon request.
332 *
333 */
334
335struct dm_unballoon_response {
336 struct dm_header hdr;
337} __packed;
338
339
340/*
341 * Hot add request message. Message sent from the host to the guest.
342 *
343 * mem_range: Memory range to hot add.
344 *
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800345 */
346
347struct dm_hot_add {
348 struct dm_header hdr;
349 union dm_mem_page_range range;
350} __packed;
351
352/*
353 * Hot add response message.
354 * This message is sent by the guest to report the status of a hot add request.
355 * If page_count is less than the requested page count, then the host should
356 * assume all further hot add requests will fail, since this indicates that
357 * the guest has hit an upper physical memory barrier.
358 *
359 * Hot adds may also fail due to low resources; in this case, the guest must
360 * not complete this message until the hot add can succeed, and the host must
361 * not send a new hot add request until the response is sent.
362 * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
363 * times it fails the request.
364 *
365 *
366 * page_count: number of pages that were successfully hot added.
367 *
368 * result: result of the operation 1: success, 0: failure.
369 *
370 */
371
372struct dm_hot_add_response {
373 struct dm_header hdr;
374 __u32 page_count;
375 __u32 result;
376} __packed;
377
378/*
379 * Types of information sent from host to the guest.
380 */
381
382enum dm_info_type {
383 INFO_TYPE_MAX_PAGE_CNT = 0,
384 MAX_INFO_TYPE
385};
386
387
388/*
389 * Header for the information message.
390 */
391
392struct dm_info_header {
393 enum dm_info_type type;
394 __u32 data_size;
395} __packed;
396
397/*
398 * This message is sent from the host to the guest to pass
399 * some relevant information (win8 addition).
400 *
401 * reserved: no used.
402 * info_size: size of the information blob.
403 * info: information blob.
404 */
405
406struct dm_info_msg {
K. Y. Srinivasan6427a0d2012-12-06 11:06:54 -0800407 struct dm_header hdr;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800408 __u32 reserved;
409 __u32 info_size;
410 __u8 info[];
411};
412
413/*
414 * End protocol definitions.
415 */
416
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700417/*
418 * State to manage hot adding memory into the guest.
419 * The range start_pfn : end_pfn specifies the range
420 * that the host has asked us to hot add. The range
421 * start_pfn : ha_end_pfn specifies the range that we have
422 * currently hot added. We hot add in multiples of 128M
423 * chunks; it is possible that we may not be able to bring
424 * online all the pages in the region. The range
Vitaly Kuznetsov7cf3b792016-08-24 16:23:09 -0700425 * covered_start_pfn:covered_end_pfn defines the pages that can
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700426 * be brough online.
427 */
428
429struct hv_hotadd_state {
430 struct list_head list;
431 unsigned long start_pfn;
Vitaly Kuznetsov7cf3b792016-08-24 16:23:09 -0700432 unsigned long covered_start_pfn;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700433 unsigned long covered_end_pfn;
434 unsigned long ha_end_pfn;
435 unsigned long end_pfn;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700436 /*
437 * A list of gaps.
438 */
439 struct list_head gap_list;
440};
441
442struct hv_hotadd_gap {
443 struct list_head list;
444 unsigned long start_pfn;
445 unsigned long end_pfn;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700446};
447
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -0700448struct balloon_state {
449 __u32 num_pages;
450 struct work_struct wrk;
451};
452
K. Y. Srinivasanc51af822013-03-15 12:25:41 -0700453struct hot_add_wrk {
454 union dm_mem_page_range ha_page_range;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700455 union dm_mem_page_range ha_region_range;
K. Y. Srinivasanc51af822013-03-15 12:25:41 -0700456 struct work_struct wrk;
457};
458
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700459static bool hot_add = true;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800460static bool do_hot_add;
K. Y. Srinivasane500d152013-02-08 15:57:15 -0800461/*
462 * Delay reporting memory pressure by
463 * the specified number of seconds.
464 */
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700465static uint pressure_report_delay = 45;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800466
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700467/*
468 * The last time we posted a pressure report to host.
469 */
470static unsigned long last_post_time;
471
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800472module_param(hot_add, bool, (S_IRUGO | S_IWUSR));
473MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
474
K. Y. Srinivasane500d152013-02-08 15:57:15 -0800475module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR));
476MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800477static atomic_t trans_id = ATOMIC_INIT(0);
478
Himadri Pandya2af5e7b2019-08-17 04:08:50 +0000479static int dm_ring_size = 20 * 1024;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800480
481/*
482 * Driver specific state.
483 */
484
485enum hv_dm_state {
486 DM_INITIALIZING = 0,
487 DM_INITIALIZED,
488 DM_BALLOON_UP,
489 DM_BALLOON_DOWN,
490 DM_HOT_ADD,
491 DM_INIT_ERROR
492};
493
494
Himadri Pandya2af5e7b2019-08-17 04:08:50 +0000495static __u8 recv_buffer[HV_HYP_PAGE_SIZE];
496static __u8 balloon_up_send_buffer[HV_HYP_PAGE_SIZE];
497#define PAGES_IN_2M (2 * 1024 * 1024 / PAGE_SIZE)
498#define HA_CHUNK (128 * 1024 * 1024 / PAGE_SIZE)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800499
500struct hv_dynmem_device {
501 struct hv_device *dev;
502 enum hv_dm_state state;
503 struct completion host_event;
504 struct completion config_event;
505
506 /*
507 * Number of pages we have currently ballooned out.
508 */
509 unsigned int num_pages_ballooned;
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -0800510 unsigned int num_pages_onlined;
511 unsigned int num_pages_added;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800512
513 /*
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -0700514 * State to manage the ballooning (up) operation.
515 */
516 struct balloon_state balloon_wrk;
517
518 /*
K. Y. Srinivasanc51af822013-03-15 12:25:41 -0700519 * State to execute the "hot-add" operation.
520 */
521 struct hot_add_wrk ha_wrk;
522
523 /*
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700524 * This state tracks if the host has specified a hot-add
525 * region.
526 */
527 bool host_specified_ha_region;
528
529 /*
530 * State to synchronize hot-add.
531 */
532 struct completion ol_waitevent;
533 bool ha_waiting;
534 /*
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -0700535 * This thread handles hot-add
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800536 * requests from the host as well as notifying
537 * the host with regards to memory pressure in
538 * the guest.
539 */
540 struct task_struct *thread;
541
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700542 /*
543 * Protects ha_region_list, num_pages_onlined counter and individual
544 * regions from ha_region_list.
545 */
546 spinlock_t ha_lock;
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800547
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800548 /*
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700549 * A list of hot-add regions.
550 */
551 struct list_head ha_region_list;
552
553 /*
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800554 * We start with the highest version we can support
555 * and downgrade based on the host; we save here the
556 * next version to try.
557 */
558 __u32 next_version;
Alex Ngb3bb97b2016-11-06 13:14:09 -0800559
560 /*
561 * The negotiated version agreed by host.
562 */
563 __u32 version;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800564};
565
566static struct hv_dynmem_device dm_device;
567
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700568static void post_status(struct hv_dynmem_device *dm);
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800569
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700570#ifdef CONFIG_MEMORY_HOTPLUG
Vitaly Kuznetsovbba072d12018-03-04 22:17:21 -0700571static inline bool has_pfn_is_backed(struct hv_hotadd_state *has,
572 unsigned long pfn)
573{
574 struct hv_hotadd_gap *gap;
575
576 /* The page is not backed. */
577 if ((pfn < has->covered_start_pfn) || (pfn >= has->covered_end_pfn))
578 return false;
579
580 /* Check for gaps. */
581 list_for_each_entry(gap, &has->gap_list, list) {
582 if ((pfn >= gap->start_pfn) && (pfn < gap->end_pfn))
583 return false;
584 }
585
586 return true;
587}
588
589static unsigned long hv_page_offline_check(unsigned long start_pfn,
590 unsigned long nr_pages)
591{
592 unsigned long pfn = start_pfn, count = 0;
593 struct hv_hotadd_state *has;
594 bool found;
595
596 while (pfn < start_pfn + nr_pages) {
597 /*
598 * Search for HAS which covers the pfn and when we find one
599 * count how many consequitive PFNs are covered.
600 */
601 found = false;
602 list_for_each_entry(has, &dm_device.ha_region_list, list) {
603 while ((pfn >= has->start_pfn) &&
604 (pfn < has->end_pfn) &&
605 (pfn < start_pfn + nr_pages)) {
606 found = true;
607 if (has_pfn_is_backed(has, pfn))
608 count++;
609 pfn++;
610 }
611 }
612
613 /*
614 * This PFN is not in any HAS (e.g. we're offlining a region
615 * which was present at boot), no need to account for it. Go
616 * to the next one.
617 */
618 if (!found)
619 pfn++;
620 }
621
622 return count;
623}
624
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800625static int hv_memory_notifier(struct notifier_block *nb, unsigned long val,
626 void *v)
627{
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -0800628 struct memory_notify *mem = (struct memory_notify *)v;
Vitaly Kuznetsovbba072d12018-03-04 22:17:21 -0700629 unsigned long flags, pfn_count;
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -0800630
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800631 switch (val) {
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800632 case MEM_ONLINE:
633 case MEM_CANCEL_ONLINE:
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800634 if (dm_device.ha_waiting) {
635 dm_device.ha_waiting = false;
636 complete(&dm_device.ol_waitevent);
637 }
638 break;
639
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800640 case MEM_OFFLINE:
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700641 spin_lock_irqsave(&dm_device.ha_lock, flags);
Vitaly Kuznetsovbba072d12018-03-04 22:17:21 -0700642 pfn_count = hv_page_offline_check(mem->start_pfn,
643 mem->nr_pages);
644 if (pfn_count <= dm_device.num_pages_onlined) {
645 dm_device.num_pages_onlined -= pfn_count;
646 } else {
647 /*
648 * We're offlining more pages than we managed to online.
649 * This is unexpected. In any case don't let
650 * num_pages_onlined wrap around zero.
651 */
652 WARN_ON_ONCE(1);
653 dm_device.num_pages_onlined = 0;
654 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700655 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -0800656 break;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700657 case MEM_GOING_ONLINE:
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -0800658 case MEM_GOING_OFFLINE:
K. Y. Srinivasan22f88472015-01-09 23:54:30 -0800659 case MEM_CANCEL_OFFLINE:
660 break;
661 }
662 return NOTIFY_OK;
663}
664
665static struct notifier_block hv_memory_nb = {
666 .notifier_call = hv_memory_notifier,
667 .priority = 0
668};
669
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700670/* Check if the particular page is backed and can be onlined and online it. */
671static void hv_page_online_one(struct hv_hotadd_state *has, struct page *pg)
672{
David Hildenbrandfae42c42019-03-05 15:42:36 -0800673 if (!has_pfn_is_backed(has, page_to_pfn(pg))) {
674 if (!PageOffline(pg))
675 __SetPageOffline(pg);
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700676 return;
David Hildenbrandfae42c42019-03-05 15:42:36 -0800677 }
678 if (PageOffline(pg))
679 __ClearPageOffline(pg);
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700680
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700681 /* This frame is currently backed; online the page. */
682 __online_page_set_limits(pg);
683 __online_page_increment_counters(pg);
684 __online_page_free(pg);
Alex Ng6df8d9a2017-08-06 13:12:53 -0700685
Lance Roy1c87dc82018-10-02 22:38:48 -0700686 lockdep_assert_held(&dm_device.ha_lock);
Alex Ng6df8d9a2017-08-06 13:12:53 -0700687 dm_device.num_pages_onlined++;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700688}
689
690static void hv_bring_pgs_online(struct hv_hotadd_state *has,
691 unsigned long start_pfn, unsigned long size)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800692{
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700693 int i;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800694
Alex Ngb3bb97b2016-11-06 13:14:09 -0800695 pr_debug("Online %lu pages starting at pfn 0x%lx\n", size, start_pfn);
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700696 for (i = 0; i < size; i++)
697 hv_page_online_one(has, pfn_to_page(start_pfn + i));
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700698}
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800699
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700700static void hv_mem_hot_add(unsigned long start, unsigned long size,
701 unsigned long pfn_count,
702 struct hv_hotadd_state *has)
703{
704 int ret = 0;
K. Y. Srinivasaned07ec92013-07-14 22:38:11 -0700705 int i, nid;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700706 unsigned long start_pfn;
707 unsigned long processed_pfn;
708 unsigned long total_pfn = pfn_count;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700709 unsigned long flags;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800710
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700711 for (i = 0; i < (size/HA_CHUNK); i++) {
712 start_pfn = start + (i * HA_CHUNK);
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700713
714 spin_lock_irqsave(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700715 has->ha_end_pfn += HA_CHUNK;
716
717 if (total_pfn > HA_CHUNK) {
718 processed_pfn = HA_CHUNK;
719 total_pfn -= HA_CHUNK;
720 } else {
721 processed_pfn = total_pfn;
722 total_pfn = 0;
723 }
724
725 has->covered_end_pfn += processed_pfn;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700726 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700727
728 init_completion(&dm_device.ol_waitevent);
Vitaly Kuznetsova132c542016-08-24 16:23:11 -0700729 dm_device.ha_waiting = !memhp_auto_online;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700730
731 nid = memory_add_physaddr_to_nid(PFN_PHYS(start_pfn));
732 ret = add_memory(nid, PFN_PHYS((start_pfn)),
733 (HA_CHUNK << PAGE_SHIFT));
734
735 if (ret) {
Vitaly Kuznetsov223e1e42018-03-04 22:17:19 -0700736 pr_err("hot_add memory failed error is %d\n", ret);
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -0700737 if (ret == -EEXIST) {
738 /*
739 * This error indicates that the error
740 * is not a transient failure. This is the
741 * case where the guest's physical address map
742 * precludes hot adding memory. Stop all further
743 * memory hot-add.
744 */
745 do_hot_add = false;
746 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700747 spin_lock_irqsave(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700748 has->ha_end_pfn -= HA_CHUNK;
749 has->covered_end_pfn -= processed_pfn;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700750 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700751 break;
752 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800753
754 /*
Vitaly Kuznetsova132c542016-08-24 16:23:11 -0700755 * Wait for the memory block to be onlined when memory onlining
756 * is done outside of kernel (memhp_auto_online). Since the hot
757 * add has succeeded, it is ok to proceed even if the pages in
758 * the hot added region have not been "onlined" within the
759 * allowed time.
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800760 */
Vitaly Kuznetsova132c542016-08-24 16:23:11 -0700761 if (dm_device.ha_waiting)
762 wait_for_completion_timeout(&dm_device.ol_waitevent,
763 5*HZ);
K. Y. Srinivasanae339332014-04-23 13:53:39 -0700764 post_status(&dm_device);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800765 }
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700766}
767
Arun KSa9cd4102019-03-05 15:42:14 -0800768static void hv_online_page(struct page *pg, unsigned int order)
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700769{
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700770 struct hv_hotadd_state *has;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700771 unsigned long flags;
Vitaly Kuznetsov4f098af2018-03-04 22:17:20 -0700772 unsigned long pfn = page_to_pfn(pg);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700773
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700774 spin_lock_irqsave(&dm_device.ha_lock, flags);
775 list_for_each_entry(has, &dm_device.ha_region_list, list) {
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700776 /* The page belongs to a different HAS. */
Arun KSa9cd4102019-03-05 15:42:14 -0800777 if ((pfn < has->start_pfn) ||
778 (pfn + (1UL << order) > has->end_pfn))
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700779 continue;
780
Arun KSa9cd4102019-03-05 15:42:14 -0800781 hv_bring_pgs_online(has, pfn, 1UL << order);
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700782 break;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700783 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700784 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700785}
786
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700787static int pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700788{
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700789 struct hv_hotadd_state *has;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700790 struct hv_hotadd_gap *gap;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700791 unsigned long residual, new_inc;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700792 int ret = 0;
793 unsigned long flags;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700794
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700795 spin_lock_irqsave(&dm_device.ha_lock, flags);
796 list_for_each_entry(has, &dm_device.ha_region_list, list) {
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700797 /*
798 * If the pfn range we are dealing with is not in the current
799 * "hot add block", move on.
800 */
Vitaly Kuznetsov77c0c972016-04-30 19:21:35 -0700801 if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700802 continue;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700803
804 /*
805 * If the current start pfn is not where the covered_end
806 * is, create a gap and update covered_end_pfn.
807 */
808 if (has->covered_end_pfn != start_pfn) {
809 gap = kzalloc(sizeof(struct hv_hotadd_gap), GFP_ATOMIC);
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700810 if (!gap) {
811 ret = -ENOMEM;
812 break;
813 }
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700814
815 INIT_LIST_HEAD(&gap->list);
816 gap->start_pfn = has->covered_end_pfn;
817 gap->end_pfn = start_pfn;
818 list_add_tail(&gap->list, &has->gap_list);
819
820 has->covered_end_pfn = start_pfn;
821 }
822
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700823 /*
824 * If the current hot add-request extends beyond
825 * our current limit; extend it.
826 */
827 if ((start_pfn + pfn_cnt) > has->end_pfn) {
828 residual = (start_pfn + pfn_cnt - has->end_pfn);
829 /*
830 * Extend the region by multiples of HA_CHUNK.
831 */
832 new_inc = (residual / HA_CHUNK) * HA_CHUNK;
833 if (residual % HA_CHUNK)
834 new_inc += HA_CHUNK;
835
836 has->end_pfn += new_inc;
837 }
838
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700839 ret = 1;
840 break;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700841 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700842 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700843
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700844 return ret;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700845}
846
847static unsigned long handle_pg_range(unsigned long pg_start,
848 unsigned long pg_count)
849{
850 unsigned long start_pfn = pg_start;
851 unsigned long pfn_cnt = pg_count;
852 unsigned long size;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700853 struct hv_hotadd_state *has;
854 unsigned long pgs_ol = 0;
855 unsigned long old_covered_state;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700856 unsigned long res = 0, flags;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700857
Alex Ngb3bb97b2016-11-06 13:14:09 -0800858 pr_debug("Hot adding %lu pages starting at pfn 0x%lx.\n", pg_count,
859 pg_start);
860
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700861 spin_lock_irqsave(&dm_device.ha_lock, flags);
862 list_for_each_entry(has, &dm_device.ha_region_list, list) {
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700863 /*
864 * If the pfn range we are dealing with is not in the current
865 * "hot add block", move on.
866 */
Vitaly Kuznetsov77c0c972016-04-30 19:21:35 -0700867 if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700868 continue;
869
870 old_covered_state = has->covered_end_pfn;
871
872 if (start_pfn < has->ha_end_pfn) {
873 /*
874 * This is the case where we are backing pages
875 * in an already hot added region. Bring
876 * these pages online first.
877 */
878 pgs_ol = has->ha_end_pfn - start_pfn;
879 if (pgs_ol > pfn_cnt)
880 pgs_ol = pfn_cnt;
Vitaly Kuznetsovd6cbd2c2015-03-27 09:10:11 -0700881
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700882 has->covered_end_pfn += pgs_ol;
883 pfn_cnt -= pgs_ol;
Vitaly Kuznetsovd6cbd2c2015-03-27 09:10:11 -0700884 /*
885 * Check if the corresponding memory block is already
Vitaly Kuznetsovda8ced32019-01-04 15:19:42 +0100886 * online. It is possible to observe struct pages still
887 * being uninitialized here so check section instead.
888 * In case the section is online we need to bring the
889 * rest of pfns (which were not backed previously)
890 * online too.
Vitaly Kuznetsovd6cbd2c2015-03-27 09:10:11 -0700891 */
892 if (start_pfn > has->start_pfn &&
Vitaly Kuznetsovda8ced32019-01-04 15:19:42 +0100893 online_section_nr(pfn_to_section_nr(start_pfn)))
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700894 hv_bring_pgs_online(has, start_pfn, pgs_ol);
Vitaly Kuznetsovd6cbd2c2015-03-27 09:10:11 -0700895
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700896 }
897
898 if ((has->ha_end_pfn < has->end_pfn) && (pfn_cnt > 0)) {
899 /*
900 * We have some residual hot add range
901 * that needs to be hot added; hot add
902 * it now. Hot add a multiple of
903 * of HA_CHUNK that fully covers the pages
904 * we have.
905 */
906 size = (has->end_pfn - has->ha_end_pfn);
907 if (pfn_cnt <= size) {
908 size = ((pfn_cnt / HA_CHUNK) * HA_CHUNK);
909 if (pfn_cnt % HA_CHUNK)
910 size += HA_CHUNK;
911 } else {
912 pfn_cnt = size;
913 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700914 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700915 hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700916 spin_lock_irqsave(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700917 }
918 /*
919 * If we managed to online any pages that were given to us,
920 * we declare success.
921 */
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700922 res = has->covered_end_pfn - old_covered_state;
923 break;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700924 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700925 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700926
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700927 return res;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700928}
929
930static unsigned long process_hot_add(unsigned long pg_start,
931 unsigned long pfn_cnt,
932 unsigned long rg_start,
933 unsigned long rg_size)
934{
935 struct hv_hotadd_state *ha_region = NULL;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700936 int covered;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700937 unsigned long flags;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700938
939 if (pfn_cnt == 0)
940 return 0;
941
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700942 if (!dm_device.host_specified_ha_region) {
943 covered = pfn_covered(pg_start, pfn_cnt);
944 if (covered < 0)
945 return 0;
946
947 if (covered)
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700948 goto do_pg_range;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700949 }
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700950
951 /*
952 * If the host has specified a hot-add range; deal with it first.
953 */
954
K. Y. Srinivasan647965a2013-03-29 07:36:11 -0700955 if (rg_size != 0) {
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700956 ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
957 if (!ha_region)
958 return 0;
959
960 INIT_LIST_HEAD(&ha_region->list);
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -0700961 INIT_LIST_HEAD(&ha_region->gap_list);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700962
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700963 ha_region->start_pfn = rg_start;
964 ha_region->ha_end_pfn = rg_start;
Vitaly Kuznetsov7cf3b792016-08-24 16:23:09 -0700965 ha_region->covered_start_pfn = pg_start;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700966 ha_region->covered_end_pfn = pg_start;
967 ha_region->end_pfn = rg_start + rg_size;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -0700968
969 spin_lock_irqsave(&dm_device.ha_lock, flags);
970 list_add_tail(&ha_region->list, &dm_device.ha_region_list);
971 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700972 }
973
974do_pg_range:
975 /*
976 * Process the page range specified; bringing them
977 * online if possible.
978 */
979 return handle_pg_range(pg_start, pfn_cnt);
980}
981
982#endif
983
984static void hot_add_req(struct work_struct *dummy)
985{
986 struct dm_hot_add_response resp;
987#ifdef CONFIG_MEMORY_HOTPLUG
988 unsigned long pg_start, pfn_cnt;
989 unsigned long rg_start, rg_sz;
990#endif
991 struct hv_dynmem_device *dm = &dm_device;
992
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800993 memset(&resp, 0, sizeof(struct dm_hot_add_response));
994 resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
995 resp.hdr.size = sizeof(struct dm_hot_add_response);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800996
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -0700997#ifdef CONFIG_MEMORY_HOTPLUG
998 pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
999 pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001000
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001001 rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
1002 rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
1003
1004 if ((rg_start == 0) && (!dm->host_specified_ha_region)) {
1005 unsigned long region_size;
1006 unsigned long region_start;
1007
1008 /*
1009 * The host has not specified the hot-add region.
1010 * Based on the hot-add page range being specified,
1011 * compute a hot-add region that can cover the pages
1012 * that need to be hot-added while ensuring the alignment
1013 * and size requirements of Linux as it relates to hot-add.
1014 */
1015 region_start = pg_start;
1016 region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK;
1017 if (pfn_cnt % HA_CHUNK)
1018 region_size += HA_CHUNK;
1019
1020 region_start = (pg_start / HA_CHUNK) * HA_CHUNK;
1021
1022 rg_start = region_start;
1023 rg_sz = region_size;
1024 }
1025
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -07001026 if (do_hot_add)
1027 resp.page_count = process_hot_add(pg_start, pfn_cnt,
1028 rg_start, rg_sz);
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -08001029
1030 dm->num_pages_added += resp.page_count;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001031#endif
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -07001032 /*
1033 * The result field of the response structure has the
1034 * following semantics:
1035 *
1036 * 1. If all or some pages hot-added: Guest should return success.
1037 *
1038 * 2. If no pages could be hot-added:
1039 *
1040 * If the guest returns success, then the host
1041 * will not attempt any further hot-add operations. This
1042 * signifies a permanent failure.
1043 *
1044 * If the guest returns failure, then this failure will be
1045 * treated as a transient failure and the host may retry the
1046 * hot-add operation after some delay.
1047 */
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001048 if (resp.page_count > 0)
1049 resp.result = 1;
K. Y. Srinivasan7f4f2302013-03-18 13:51:38 -07001050 else if (!do_hot_add)
1051 resp.result = 1;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001052 else
1053 resp.result = 0;
1054
1055 if (!do_hot_add || (resp.page_count == 0))
Vitaly Kuznetsov223e1e42018-03-04 22:17:19 -07001056 pr_err("Memory hot add failed\n");
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001057
1058 dm->state = DM_INITIALIZED;
K. Y. Srinivasan20138d62013-07-17 17:27:27 -07001059 resp.hdr.trans_id = atomic_inc_return(&trans_id);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001060 vmbus_sendpacket(dm->dev->channel, &resp,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001061 sizeof(struct dm_hot_add_response),
1062 (unsigned long)NULL,
1063 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001064}
1065
1066static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
1067{
K. Y. Srinivasan6427a0d2012-12-06 11:06:54 -08001068 struct dm_info_header *info_hdr;
1069
1070 info_hdr = (struct dm_info_header *)msg->info;
1071
1072 switch (info_hdr->type) {
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001073 case INFO_TYPE_MAX_PAGE_CNT:
Alex Ng85000962016-11-06 13:14:12 -08001074 if (info_hdr->data_size == sizeof(__u64)) {
1075 __u64 *max_page_count = (__u64 *)&info_hdr[1];
1076
Alex Ng7b6e54b2017-08-06 13:12:54 -07001077 pr_info("Max. dynamic memory size: %llu MB\n",
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001078 (*max_page_count) >> (20 - HV_HYP_PAGE_SHIFT));
Alex Ng85000962016-11-06 13:14:12 -08001079 }
1080
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001081 break;
1082 default:
Vitaly Kuznetsov223e1e42018-03-04 22:17:19 -07001083 pr_warn("Received Unknown type: %d\n", info_hdr->type);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001084 }
1085}
1086
Wei Yongjuna6025a22013-03-20 23:25:59 +08001087static unsigned long compute_balloon_floor(void)
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001088{
1089 unsigned long min_pages;
Arun KSca79b0c2018-12-28 00:34:29 -08001090 unsigned long nr_pages = totalram_pages();
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001091#define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
1092 /* Simple continuous piecewiese linear function:
1093 * max MiB -> min MiB gradient
1094 * 0 0
1095 * 16 16
1096 * 32 24
1097 * 128 72 (1/2)
1098 * 512 168 (1/4)
1099 * 2048 360 (1/8)
Vitaly Kuznetsov7fb0e1a2015-03-27 09:10:12 -07001100 * 8192 744 (1/16)
1101 * 32768 1512 (1/32)
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001102 */
Arun KS3d6357d2018-12-28 00:34:20 -08001103 if (nr_pages < MB2PAGES(128))
1104 min_pages = MB2PAGES(8) + (nr_pages >> 1);
1105 else if (nr_pages < MB2PAGES(512))
1106 min_pages = MB2PAGES(40) + (nr_pages >> 2);
1107 else if (nr_pages < MB2PAGES(2048))
1108 min_pages = MB2PAGES(104) + (nr_pages >> 3);
1109 else if (nr_pages < MB2PAGES(8192))
1110 min_pages = MB2PAGES(232) + (nr_pages >> 4);
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001111 else
Arun KS3d6357d2018-12-28 00:34:20 -08001112 min_pages = MB2PAGES(488) + (nr_pages >> 5);
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001113#undef MB2PAGES
1114 return min_pages;
1115}
1116
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001117/*
1118 * Post our status as it relates memory pressure to the
1119 * host. Host expects the guests to post this status
1120 * periodically at 1 second intervals.
1121 *
1122 * The metrics specified in this protocol are very Windows
1123 * specific and so we cook up numbers here to convey our memory
1124 * pressure.
1125 */
1126
1127static void post_status(struct hv_dynmem_device *dm)
1128{
1129 struct dm_status status;
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001130 unsigned long now = jiffies;
1131 unsigned long last_post = last_post_time;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001132
K. Y. Srinivasane500d152013-02-08 15:57:15 -08001133 if (pressure_report_delay > 0) {
1134 --pressure_report_delay;
1135 return;
1136 }
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001137
1138 if (!time_after(now, (last_post_time + HZ)))
1139 return;
1140
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001141 memset(&status, 0, sizeof(struct dm_status));
1142 status.hdr.type = DM_STATUS_REPORT;
1143 status.hdr.size = sizeof(struct dm_status);
1144 status.hdr.trans_id = atomic_inc_return(&trans_id);
1145
K. Y. Srinivasan07315722013-01-25 16:18:47 -08001146 /*
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -08001147 * The host expects the guest to report free and committed memory.
1148 * Furthermore, the host expects the pressure information to include
1149 * the ballooned out pages. For a given amount of memory that we are
1150 * managing we need to compute a floor below which we should not
1151 * balloon. Compute this and add it to the pressure report.
1152 * We also need to report all offline pages (num_pages_added -
1153 * num_pages_onlined) as committed to the host, otherwise it can try
1154 * asking us to balloon them out.
K. Y. Srinivasan07315722013-01-25 16:18:47 -08001155 */
Alex Ngb605c2d2016-08-24 16:23:13 -07001156 status.num_avail = si_mem_available();
K. Y. Srinivasan1c7db962013-02-08 15:57:16 -08001157 status.num_committed = vm_memory_committed() +
Vitaly Kuznetsov549fd282015-02-28 11:38:59 -08001158 dm->num_pages_ballooned +
1159 (dm->num_pages_added > dm->num_pages_onlined ?
1160 dm->num_pages_added - dm->num_pages_onlined : 0) +
1161 compute_balloon_floor();
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001162
Vitaly Kuznetsovcf21be92018-03-04 22:17:22 -07001163 trace_balloon_status(status.num_avail, status.num_committed,
1164 vm_memory_committed(), dm->num_pages_ballooned,
1165 dm->num_pages_added, dm->num_pages_onlined);
K. Y. Srinivasanc5e22542013-07-14 22:38:12 -07001166 /*
1167 * If our transaction ID is no longer current, just don't
1168 * send the status. This can happen if we were interrupted
1169 * after we picked our transaction ID.
1170 */
1171 if (status.hdr.trans_id != atomic_read(&trans_id))
1172 return;
1173
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001174 /*
1175 * If the last post time that we sampled has changed,
1176 * we have raced, don't post the status.
1177 */
1178 if (last_post != last_post_time)
1179 return;
1180
1181 last_post_time = jiffies;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001182 vmbus_sendpacket(dm->dev->channel, &status,
1183 sizeof(struct dm_status),
1184 (unsigned long)NULL,
1185 VM_PKT_DATA_INBAND, 0);
1186
1187}
1188
Greg Kroah-Hartman989623c2012-11-21 12:46:40 -08001189static void free_balloon_pages(struct hv_dynmem_device *dm,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001190 union dm_mem_page_range *range_array)
1191{
1192 int num_pages = range_array->finfo.page_cnt;
1193 __u64 start_frame = range_array->finfo.start_page;
1194 struct page *pg;
1195 int i;
1196
1197 for (i = 0; i < num_pages; i++) {
1198 pg = pfn_to_page(i + start_frame);
David Hildenbrandfae42c42019-03-05 15:42:36 -08001199 __ClearPageOffline(pg);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001200 __free_page(pg);
1201 dm->num_pages_ballooned--;
1202 }
1203}
1204
1205
1206
Vitaly Kuznetsov797f88c92015-03-31 11:16:41 -07001207static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
1208 unsigned int num_pages,
1209 struct dm_balloon_response *bl_resp,
1210 int alloc_unit)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001211{
David Hildenbrandfae42c42019-03-05 15:42:36 -08001212 unsigned int i, j;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001213 struct page *pg;
1214
1215 if (num_pages < alloc_unit)
1216 return 0;
1217
1218 for (i = 0; (i * alloc_unit) < num_pages; i++) {
1219 if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001220 HV_HYP_PAGE_SIZE)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001221 return i * alloc_unit;
1222
1223 /*
1224 * We execute this code in a thread context. Furthermore,
1225 * we don't want the kernel to try too hard.
1226 */
1227 pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
1228 __GFP_NOMEMALLOC | __GFP_NOWARN,
1229 get_order(alloc_unit << PAGE_SHIFT));
1230
Vitaly Kuznetsov0a1a86a2015-03-27 09:10:13 -07001231 if (!pg)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001232 return i * alloc_unit;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001233
1234 dm->num_pages_ballooned += alloc_unit;
1235
K. Y. Srinivasanf766dc12013-03-18 13:51:37 -07001236 /*
1237 * If we allocatted 2M pages; split them so we
1238 * can free them in any order we get.
1239 */
1240
1241 if (alloc_unit != 1)
1242 split_page(pg, get_order(alloc_unit << PAGE_SHIFT));
1243
David Hildenbrandfae42c42019-03-05 15:42:36 -08001244 /* mark all pages offline */
1245 for (j = 0; j < (1 << get_order(alloc_unit << PAGE_SHIFT)); j++)
1246 __SetPageOffline(pg + j);
1247
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001248 bl_resp->range_count++;
1249 bl_resp->range_array[i].finfo.start_page =
1250 page_to_pfn(pg);
1251 bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
1252 bl_resp->hdr.size += sizeof(union dm_mem_page_range);
1253
1254 }
1255
1256 return num_pages;
1257}
1258
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001259static void balloon_up(struct work_struct *dummy)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001260{
Vitaly Kuznetsov797f88c92015-03-31 11:16:41 -07001261 unsigned int num_pages = dm_device.balloon_wrk.num_pages;
1262 unsigned int num_ballooned = 0;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001263 struct dm_balloon_response *bl_resp;
1264 int alloc_unit;
1265 int ret;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001266 bool done = false;
1267 int i;
Alex Ngb605c2d2016-08-24 16:23:13 -07001268 long avail_pages;
Vitaly Kuznetsov530d15b2015-02-28 11:39:00 -08001269 unsigned long floor;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001270
Dexuan Cuif6712232014-11-24 20:32:43 -08001271 /* The host balloons pages in 2M granularity. */
1272 WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001273
1274 /*
K. Y. Srinivasanf766dc12013-03-18 13:51:37 -07001275 * We will attempt 2M allocations. However, if we fail to
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001276 * allocate 2M chunks, we will go back to PAGE_SIZE allocations.
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001277 */
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001278 alloc_unit = PAGES_IN_2M;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001279
Alex Ngb605c2d2016-08-24 16:23:13 -07001280 avail_pages = si_mem_available();
Vitaly Kuznetsov530d15b2015-02-28 11:39:00 -08001281 floor = compute_balloon_floor();
1282
1283 /* Refuse to balloon below the floor, keep the 2M granularity. */
Alex Ngb605c2d2016-08-24 16:23:13 -07001284 if (avail_pages < num_pages || avail_pages - num_pages < floor) {
Alex Ngb3bb97b2016-11-06 13:14:09 -08001285 pr_warn("Balloon request will be partially fulfilled. %s\n",
1286 avail_pages < num_pages ? "Not enough memory." :
1287 "Balloon floor reached.");
1288
Alex Ngb605c2d2016-08-24 16:23:13 -07001289 num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
Vitaly Kuznetsov530d15b2015-02-28 11:39:00 -08001290 num_pages -= num_pages % PAGES_IN_2M;
1291 }
1292
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001293 while (!done) {
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001294 memset(balloon_up_send_buffer, 0, HV_HYP_PAGE_SIZE);
Dexuan Cui1fed17d2019-06-14 18:42:17 +00001295 bl_resp = (struct dm_balloon_response *)balloon_up_send_buffer;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001296 bl_resp->hdr.type = DM_BALLOON_RESPONSE;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001297 bl_resp->hdr.size = sizeof(struct dm_balloon_response);
1298 bl_resp->more_pages = 1;
1299
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001300 num_pages -= num_ballooned;
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001301 num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
Vitaly Kuznetsov0a1a86a2015-03-27 09:10:13 -07001302 bl_resp, alloc_unit);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001303
Dexuan Cuif6712232014-11-24 20:32:43 -08001304 if (alloc_unit != 1 && num_ballooned == 0) {
K. Y. Srinivasanf766dc12013-03-18 13:51:37 -07001305 alloc_unit = 1;
1306 continue;
1307 }
1308
Vitaly Kuznetsov0a1a86a2015-03-27 09:10:13 -07001309 if (num_ballooned == 0 || num_ballooned == num_pages) {
Alex Ngb3bb97b2016-11-06 13:14:09 -08001310 pr_debug("Ballooned %u out of %u requested pages.\n",
1311 num_pages, dm_device.balloon_wrk.num_pages);
1312
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001313 bl_resp->more_pages = 0;
1314 done = true;
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001315 dm_device.state = DM_INITIALIZED;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001316 }
1317
1318 /*
1319 * We are pushing a lot of data through the channel;
1320 * deal with transient failures caused because of the
1321 * lack of space in the ring buffer.
1322 */
1323
1324 do {
K. Y. Srinivasan20138d62013-07-17 17:27:27 -07001325 bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001326 ret = vmbus_sendpacket(dm_device.dev->channel,
1327 bl_resp,
1328 bl_resp->hdr.size,
1329 (unsigned long)NULL,
1330 VM_PKT_DATA_INBAND, 0);
1331
1332 if (ret == -EAGAIN)
1333 msleep(20);
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001334 post_status(&dm_device);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001335 } while (ret == -EAGAIN);
1336
1337 if (ret) {
1338 /*
1339 * Free up the memory we allocatted.
1340 */
Vitaly Kuznetsov223e1e42018-03-04 22:17:19 -07001341 pr_err("Balloon response failed\n");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001342
1343 for (i = 0; i < bl_resp->range_count; i++)
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001344 free_balloon_pages(&dm_device,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001345 &bl_resp->range_array[i]);
1346
1347 done = true;
1348 }
1349 }
1350
1351}
1352
1353static void balloon_down(struct hv_dynmem_device *dm,
1354 struct dm_unballoon_request *req)
1355{
1356 union dm_mem_page_range *range_array = req->range_array;
1357 int range_count = req->range_count;
1358 struct dm_unballoon_response resp;
1359 int i;
Alex Ngb3bb97b2016-11-06 13:14:09 -08001360 unsigned int prev_pages_ballooned = dm->num_pages_ballooned;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001361
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001362 for (i = 0; i < range_count; i++) {
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001363 free_balloon_pages(dm, &range_array[i]);
K. Y. Srinivasanab3de222015-01-09 23:54:31 -08001364 complete(&dm_device.config_event);
K. Y. Srinivasanae339332014-04-23 13:53:39 -07001365 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001366
Alex Ngb3bb97b2016-11-06 13:14:09 -08001367 pr_debug("Freed %u ballooned pages.\n",
1368 prev_pages_ballooned - dm->num_pages_ballooned);
1369
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001370 if (req->more_pages == 1)
1371 return;
1372
1373 memset(&resp, 0, sizeof(struct dm_unballoon_response));
1374 resp.hdr.type = DM_UNBALLOON_RESPONSE;
1375 resp.hdr.trans_id = atomic_inc_return(&trans_id);
1376 resp.hdr.size = sizeof(struct dm_unballoon_response);
1377
1378 vmbus_sendpacket(dm_device.dev->channel, &resp,
1379 sizeof(struct dm_unballoon_response),
1380 (unsigned long)NULL,
1381 VM_PKT_DATA_INBAND, 0);
1382
1383 dm->state = DM_INITIALIZED;
1384}
1385
1386static void balloon_onchannelcallback(void *context);
1387
1388static int dm_thread_func(void *dm_dev)
1389{
1390 struct hv_dynmem_device *dm = dm_dev;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001391
1392 while (!kthread_should_stop()) {
K. Y. Srinivasanab3de222015-01-09 23:54:31 -08001393 wait_for_completion_interruptible_timeout(
K. Y. Srinivasan5dba4c52014-02-13 16:24:33 -08001394 &dm_device.config_event, 1*HZ);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001395 /*
1396 * The host expects us to post information on the memory
1397 * pressure every second.
1398 */
K. Y. Srinivasanab3de222015-01-09 23:54:31 -08001399 reinit_completion(&dm_device.config_event);
1400 post_status(dm);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001401 }
1402
1403 return 0;
1404}
1405
1406
1407static void version_resp(struct hv_dynmem_device *dm,
1408 struct dm_version_response *vresp)
1409{
1410 struct dm_version_request version_req;
1411 int ret;
1412
1413 if (vresp->is_accepted) {
1414 /*
1415 * We are done; wakeup the
1416 * context waiting for version
1417 * negotiation.
1418 */
1419 complete(&dm->host_event);
1420 return;
1421 }
1422 /*
1423 * If there are more versions to try, continue
1424 * with negotiations; if not
1425 * shutdown the service since we are not able
1426 * to negotiate a suitable version number
1427 * with the host.
1428 */
1429 if (dm->next_version == 0)
1430 goto version_error;
1431
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001432 memset(&version_req, 0, sizeof(struct dm_version_request));
1433 version_req.hdr.type = DM_VERSION_REQUEST;
1434 version_req.hdr.size = sizeof(struct dm_version_request);
1435 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
Alex Ngb6ddeae2015-08-01 16:08:13 -07001436 version_req.version.version = dm->next_version;
Alex Ngb3bb97b2016-11-06 13:14:09 -08001437 dm->version = version_req.version.version;
Alex Ngb6ddeae2015-08-01 16:08:13 -07001438
1439 /*
1440 * Set the next version to try in case current version fails.
1441 * Win7 protocol ought to be the last one to try.
1442 */
1443 switch (version_req.version.version) {
1444 case DYNMEM_PROTOCOL_VERSION_WIN8:
1445 dm->next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
1446 version_req.is_last_attempt = 0;
1447 break;
1448 default:
1449 dm->next_version = 0;
1450 version_req.is_last_attempt = 1;
1451 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001452
1453 ret = vmbus_sendpacket(dm->dev->channel, &version_req,
1454 sizeof(struct dm_version_request),
1455 (unsigned long)NULL,
1456 VM_PKT_DATA_INBAND, 0);
1457
1458 if (ret)
1459 goto version_error;
1460
1461 return;
1462
1463version_error:
1464 dm->state = DM_INIT_ERROR;
1465 complete(&dm->host_event);
1466}
1467
1468static void cap_resp(struct hv_dynmem_device *dm,
1469 struct dm_capabilities_resp_msg *cap_resp)
1470{
1471 if (!cap_resp->is_accepted) {
Vitaly Kuznetsov223e1e42018-03-04 22:17:19 -07001472 pr_err("Capabilities not accepted by host\n");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001473 dm->state = DM_INIT_ERROR;
1474 }
1475 complete(&dm->host_event);
1476}
1477
1478static void balloon_onchannelcallback(void *context)
1479{
1480 struct hv_device *dev = context;
1481 u32 recvlen;
1482 u64 requestid;
1483 struct dm_message *dm_msg;
1484 struct dm_header *dm_hdr;
1485 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001486 struct dm_balloon *bal_msg;
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001487 struct dm_hot_add *ha_msg;
1488 union dm_mem_page_range *ha_pg_range;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001489 union dm_mem_page_range *ha_region;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001490
1491 memset(recv_buffer, 0, sizeof(recv_buffer));
1492 vmbus_recvpacket(dev->channel, recv_buffer,
Himadri Pandya2af5e7b2019-08-17 04:08:50 +00001493 HV_HYP_PAGE_SIZE, &recvlen, &requestid);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001494
1495 if (recvlen > 0) {
1496 dm_msg = (struct dm_message *)recv_buffer;
1497 dm_hdr = &dm_msg->hdr;
1498
1499 switch (dm_hdr->type) {
1500 case DM_VERSION_RESPONSE:
1501 version_resp(dm,
1502 (struct dm_version_response *)dm_msg);
1503 break;
1504
1505 case DM_CAPABILITIES_RESPONSE:
1506 cap_resp(dm,
1507 (struct dm_capabilities_resp_msg *)dm_msg);
1508 break;
1509
1510 case DM_BALLOON_REQUEST:
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001511 if (dm->state == DM_BALLOON_UP)
1512 pr_warn("Currently ballooning\n");
1513 bal_msg = (struct dm_balloon *)recv_buffer;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001514 dm->state = DM_BALLOON_UP;
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001515 dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
1516 schedule_work(&dm_device.balloon_wrk.wrk);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001517 break;
1518
1519 case DM_UNBALLOON_REQUEST:
1520 dm->state = DM_BALLOON_DOWN;
1521 balloon_down(dm,
1522 (struct dm_unballoon_request *)recv_buffer);
1523 break;
1524
1525 case DM_MEM_HOT_ADD_REQUEST:
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001526 if (dm->state == DM_HOT_ADD)
1527 pr_warn("Currently hot-adding\n");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001528 dm->state = DM_HOT_ADD;
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001529 ha_msg = (struct dm_hot_add *)recv_buffer;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001530 if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
1531 /*
1532 * This is a normal hot-add request specifying
1533 * hot-add memory.
1534 */
Vitaly Kuznetsovd19a55d2016-04-30 19:21:36 -07001535 dm->host_specified_ha_region = false;
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001536 ha_pg_range = &ha_msg->range;
1537 dm->ha_wrk.ha_page_range = *ha_pg_range;
1538 dm->ha_wrk.ha_region_range.page_range = 0;
1539 } else {
1540 /*
1541 * Host is specifying that we first hot-add
1542 * a region and then partially populate this
1543 * region.
1544 */
1545 dm->host_specified_ha_region = true;
1546 ha_pg_range = &ha_msg->range;
1547 ha_region = &ha_pg_range[1];
1548 dm->ha_wrk.ha_page_range = *ha_pg_range;
1549 dm->ha_wrk.ha_region_range = *ha_region;
1550 }
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001551 schedule_work(&dm_device.ha_wrk.wrk);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001552 break;
1553
1554 case DM_INFO_MESSAGE:
1555 process_info(dm, (struct dm_info_msg *)dm_msg);
1556 break;
1557
1558 default:
Vitaly Kuznetsov223e1e42018-03-04 22:17:19 -07001559 pr_warn("Unhandled message: type: %d\n", dm_hdr->type);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001560
1561 }
1562 }
1563
1564}
1565
Dexuan Cui221f6df2019-06-14 18:42:30 +00001566static int balloon_connect_vsp(struct hv_device *dev)
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001567{
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001568 struct dm_version_request version_req;
1569 struct dm_capabilities cap_msg;
Dexuan Cui221f6df2019-06-14 18:42:30 +00001570 unsigned long t;
1571 int ret;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001572
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001573 ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
Dexuan Cui221f6df2019-06-14 18:42:30 +00001574 balloon_onchannelcallback, dev);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001575 if (ret)
Dexuan Cui1fed17d2019-06-14 18:42:17 +00001576 return ret;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001577
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001578 /*
1579 * Initiate the hand shake with the host and negotiate
1580 * a version that the host can support. We start with the
1581 * highest version number and go down if the host cannot
1582 * support it.
1583 */
1584 memset(&version_req, 0, sizeof(struct dm_version_request));
1585 version_req.hdr.type = DM_VERSION_REQUEST;
1586 version_req.hdr.size = sizeof(struct dm_version_request);
1587 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
Alex Ngb6ddeae2015-08-01 16:08:13 -07001588 version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN10;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001589 version_req.is_last_attempt = 0;
Alex Ngb3bb97b2016-11-06 13:14:09 -08001590 dm_device.version = version_req.version.version;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001591
1592 ret = vmbus_sendpacket(dev->channel, &version_req,
Dexuan Cui221f6df2019-06-14 18:42:30 +00001593 sizeof(struct dm_version_request),
1594 (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001595 if (ret)
Dexuan Cui221f6df2019-06-14 18:42:30 +00001596 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001597
1598 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1599 if (t == 0) {
1600 ret = -ETIMEDOUT;
Dexuan Cui221f6df2019-06-14 18:42:30 +00001601 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001602 }
1603
1604 /*
1605 * If we could not negotiate a compatible version with the host
1606 * fail the probe function.
1607 */
1608 if (dm_device.state == DM_INIT_ERROR) {
Dexuan Cui221f6df2019-06-14 18:42:30 +00001609 ret = -EPROTO;
1610 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001611 }
Alex Ngb3bb97b2016-11-06 13:14:09 -08001612
1613 pr_info("Using Dynamic Memory protocol version %u.%u\n",
1614 DYNMEM_MAJOR_VERSION(dm_device.version),
1615 DYNMEM_MINOR_VERSION(dm_device.version));
1616
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001617 /*
1618 * Now submit our capabilities to the host.
1619 */
1620 memset(&cap_msg, 0, sizeof(struct dm_capabilities));
1621 cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
1622 cap_msg.hdr.size = sizeof(struct dm_capabilities);
1623 cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
1624
1625 cap_msg.caps.cap_bits.balloon = 1;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001626 cap_msg.caps.cap_bits.hot_add = 1;
1627
1628 /*
K. Y. Srinivasan647965a2013-03-29 07:36:11 -07001629 * Specify our alignment requirements as it relates
1630 * memory hot-add. Specify 128MB alignment.
1631 */
1632 cap_msg.caps.cap_bits.hot_add_alignment = 7;
1633
1634 /*
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001635 * Currently the host does not use these
1636 * values and we set them to what is done in the
1637 * Windows driver.
1638 */
1639 cap_msg.min_page_cnt = 0;
1640 cap_msg.max_page_number = -1;
1641
1642 ret = vmbus_sendpacket(dev->channel, &cap_msg,
Dexuan Cui221f6df2019-06-14 18:42:30 +00001643 sizeof(struct dm_capabilities),
1644 (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001645 if (ret)
Dexuan Cui221f6df2019-06-14 18:42:30 +00001646 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001647
1648 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
1649 if (t == 0) {
1650 ret = -ETIMEDOUT;
Dexuan Cui221f6df2019-06-14 18:42:30 +00001651 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001652 }
1653
1654 /*
1655 * If the host does not like our capabilities,
1656 * fail the probe function.
1657 */
1658 if (dm_device.state == DM_INIT_ERROR) {
Dexuan Cui221f6df2019-06-14 18:42:30 +00001659 ret = -EPROTO;
1660 goto out;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001661 }
1662
Dexuan Cui221f6df2019-06-14 18:42:30 +00001663 return 0;
1664out:
1665 vmbus_close(dev->channel);
1666 return ret;
1667}
1668
1669static int balloon_probe(struct hv_device *dev,
1670 const struct hv_vmbus_device_id *dev_id)
1671{
1672 int ret;
1673
1674#ifdef CONFIG_MEMORY_HOTPLUG
1675 do_hot_add = hot_add;
1676#else
1677 do_hot_add = false;
1678#endif
1679 dm_device.dev = dev;
1680 dm_device.state = DM_INITIALIZING;
1681 dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN8;
1682 init_completion(&dm_device.host_event);
1683 init_completion(&dm_device.config_event);
1684 INIT_LIST_HEAD(&dm_device.ha_region_list);
1685 spin_lock_init(&dm_device.ha_lock);
1686 INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
1687 INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
1688 dm_device.host_specified_ha_region = false;
1689
1690#ifdef CONFIG_MEMORY_HOTPLUG
1691 set_online_page_callback(&hv_online_page);
1692 register_memory_notifier(&hv_memory_nb);
1693#endif
1694
1695 hv_set_drvdata(dev, &dm_device);
1696
1697 ret = balloon_connect_vsp(dev);
1698 if (ret != 0)
1699 return ret;
1700
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001701 dm_device.state = DM_INITIALIZED;
Dexuan Cui221f6df2019-06-14 18:42:30 +00001702
1703 dm_device.thread =
1704 kthread_run(dm_thread_func, &dm_device, "hv_balloon");
1705 if (IS_ERR(dm_device.thread)) {
1706 ret = PTR_ERR(dm_device.thread);
1707 goto probe_error;
1708 }
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001709
1710 return 0;
1711
Dexuan Cui221f6df2019-06-14 18:42:30 +00001712probe_error:
1713 vmbus_close(dev->channel);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001714#ifdef CONFIG_MEMORY_HOTPLUG
Dexuan Cui221f6df2019-06-14 18:42:30 +00001715 unregister_memory_notifier(&hv_memory_nb);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001716 restore_online_page_callback(&hv_online_page);
1717#endif
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001718 return ret;
1719}
1720
1721static int balloon_remove(struct hv_device *dev)
1722{
1723 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -07001724 struct hv_hotadd_state *has, *tmp;
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -07001725 struct hv_hotadd_gap *gap, *tmp_gap;
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -07001726 unsigned long flags;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001727
1728 if (dm->num_pages_ballooned != 0)
1729 pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
1730
K. Y. Srinivasan6571b2d2013-03-15 12:25:40 -07001731 cancel_work_sync(&dm->balloon_wrk.wrk);
K. Y. Srinivasanc51af822013-03-15 12:25:41 -07001732 cancel_work_sync(&dm->ha_wrk.wrk);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001733
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001734 kthread_stop(dm->thread);
Dexuan Cui221f6df2019-06-14 18:42:30 +00001735 vmbus_close(dev->channel);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001736#ifdef CONFIG_MEMORY_HOTPLUG
K. Y. Srinivasan22f88472015-01-09 23:54:30 -08001737 unregister_memory_notifier(&hv_memory_nb);
Dexuan Cui221f6df2019-06-14 18:42:30 +00001738 restore_online_page_callback(&hv_online_page);
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001739#endif
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -07001740 spin_lock_irqsave(&dm_device.ha_lock, flags);
1741 list_for_each_entry_safe(has, tmp, &dm->ha_region_list, list) {
Vitaly Kuznetsovcb7a5722016-08-24 16:23:10 -07001742 list_for_each_entry_safe(gap, tmp_gap, &has->gap_list, list) {
1743 list_del(&gap->list);
1744 kfree(gap);
1745 }
K. Y. Srinivasan1cac8cd2013-03-15 12:25:43 -07001746 list_del(&has->list);
1747 kfree(has);
1748 }
Vitaly Kuznetsoveece30b2016-08-24 16:23:12 -07001749 spin_unlock_irqrestore(&dm_device.ha_lock, flags);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001750
1751 return 0;
1752}
1753
1754static const struct hv_vmbus_device_id id_table[] = {
1755 /* Dynamic Memory Class ID */
1756 /* 525074DC-8985-46e2-8057-A307DC18A502 */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -08001757 { HV_DM_GUID, },
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001758 { },
1759};
1760
1761MODULE_DEVICE_TABLE(vmbus, id_table);
1762
1763static struct hv_driver balloon_drv = {
1764 .name = "hv_balloon",
1765 .id_table = id_table,
1766 .probe = balloon_probe,
1767 .remove = balloon_remove,
Arjan van de Venaf0a5642018-06-05 13:37:49 -07001768 .driver = {
1769 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1770 },
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001771};
1772
1773static int __init init_balloon_drv(void)
1774{
1775
1776 return vmbus_driver_register(&balloon_drv);
1777}
1778
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001779module_init(init_balloon_drv);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001780
1781MODULE_DESCRIPTION("Hyper-V Balloon");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001782MODULE_LICENSE("GPL");