Thomas Gleixner | 43aa313 | 2019-05-29 07:17:54 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, Microsoft Corporation. |
| 4 | * |
| 5 | * Author: |
| 6 | * K. Y. Srinivasan <kys@microsoft.com> |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
| 11 | #include <linux/kernel.h> |
K. Y. Srinivasan | ae33933 | 2014-04-23 13:53:39 -0700 | [diff] [blame] | 12 | #include <linux/jiffies.h> |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 13 | #include <linux/mman.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/kthread.h> |
| 19 | #include <linux/completion.h> |
| 20 | #include <linux/memory_hotplug.h> |
| 21 | #include <linux/memory.h> |
| 22 | #include <linux/notifier.h> |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 23 | #include <linux/percpu_counter.h> |
| 24 | |
| 25 | #include <linux/hyperv.h> |
Himadri Pandya | 2af5e7b | 2019-08-17 04:08:50 +0000 | [diff] [blame^] | 26 | #include <asm/hyperv-tlfs.h> |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 27 | |
Vitaly Kuznetsov | cf21be9 | 2018-03-04 22:17:22 -0700 | [diff] [blame] | 28 | #define CREATE_TRACE_POINTS |
| 29 | #include "hv_trace_balloon.h" |
| 30 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 31 | /* |
| 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 | |
| 56 | enum { |
| 57 | DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3), |
| 58 | DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0), |
Alex Ng | b6ddeae | 2015-08-01 16:08:13 -0700 | [diff] [blame] | 59 | DYNMEM_PROTOCOL_VERSION_3 = DYNMEM_MAKE_VERSION(2, 0), |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 60 | |
| 61 | DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1, |
| 62 | DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2, |
Alex Ng | b6ddeae | 2015-08-01 16:08:13 -0700 | [diff] [blame] | 63 | DYNMEM_PROTOCOL_VERSION_WIN10 = DYNMEM_PROTOCOL_VERSION_3, |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 64 | |
Alex Ng | b6ddeae | 2015-08-01 16:08:13 -0700 | [diff] [blame] | 65 | DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN10 |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * Message Types |
| 72 | */ |
| 73 | |
| 74 | enum 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 | |
| 104 | union dm_version { |
| 105 | struct { |
| 106 | __u16 minor_version; |
| 107 | __u16 major_version; |
| 108 | }; |
| 109 | __u32 version; |
| 110 | } __packed; |
| 111 | |
| 112 | |
| 113 | union dm_caps { |
| 114 | struct { |
| 115 | __u64 balloon:1; |
| 116 | __u64 hot_add:1; |
K. Y. Srinivasan | 647965a | 2013-03-29 07:36:11 -0700 | [diff] [blame] | 117 | /* |
| 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 125 | } cap_bits; |
| 126 | __u64 caps; |
| 127 | } __packed; |
| 128 | |
| 129 | union 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 | |
| 155 | struct 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 | |
| 166 | struct 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 | |
| 186 | struct 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 | |
| 204 | struct 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 | |
| 215 | struct 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 | |
| 232 | struct 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 | |
| 258 | struct 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 | |
| 277 | struct 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 | |
| 298 | struct 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 | |
| 321 | struct 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 | |
| 335 | struct 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 345 | */ |
| 346 | |
| 347 | struct 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 | |
| 372 | struct 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 | |
| 382 | enum 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 | |
| 392 | struct 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 | |
| 406 | struct dm_info_msg { |
K. Y. Srinivasan | 6427a0d | 2012-12-06 11:06:54 -0800 | [diff] [blame] | 407 | struct dm_header hdr; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 408 | __u32 reserved; |
| 409 | __u32 info_size; |
| 410 | __u8 info[]; |
| 411 | }; |
| 412 | |
| 413 | /* |
| 414 | * End protocol definitions. |
| 415 | */ |
| 416 | |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 417 | /* |
| 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 Kuznetsov | 7cf3b79 | 2016-08-24 16:23:09 -0700 | [diff] [blame] | 425 | * covered_start_pfn:covered_end_pfn defines the pages that can |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 426 | * be brough online. |
| 427 | */ |
| 428 | |
| 429 | struct hv_hotadd_state { |
| 430 | struct list_head list; |
| 431 | unsigned long start_pfn; |
Vitaly Kuznetsov | 7cf3b79 | 2016-08-24 16:23:09 -0700 | [diff] [blame] | 432 | unsigned long covered_start_pfn; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 433 | unsigned long covered_end_pfn; |
| 434 | unsigned long ha_end_pfn; |
| 435 | unsigned long end_pfn; |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 436 | /* |
| 437 | * A list of gaps. |
| 438 | */ |
| 439 | struct list_head gap_list; |
| 440 | }; |
| 441 | |
| 442 | struct hv_hotadd_gap { |
| 443 | struct list_head list; |
| 444 | unsigned long start_pfn; |
| 445 | unsigned long end_pfn; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 446 | }; |
| 447 | |
K. Y. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 448 | struct balloon_state { |
| 449 | __u32 num_pages; |
| 450 | struct work_struct wrk; |
| 451 | }; |
| 452 | |
K. Y. Srinivasan | c51af82 | 2013-03-15 12:25:41 -0700 | [diff] [blame] | 453 | struct hot_add_wrk { |
| 454 | union dm_mem_page_range ha_page_range; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 455 | union dm_mem_page_range ha_region_range; |
K. Y. Srinivasan | c51af82 | 2013-03-15 12:25:41 -0700 | [diff] [blame] | 456 | struct work_struct wrk; |
| 457 | }; |
| 458 | |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 459 | static bool hot_add = true; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 460 | static bool do_hot_add; |
K. Y. Srinivasan | e500d15 | 2013-02-08 15:57:15 -0800 | [diff] [blame] | 461 | /* |
| 462 | * Delay reporting memory pressure by |
| 463 | * the specified number of seconds. |
| 464 | */ |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 465 | static uint pressure_report_delay = 45; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 466 | |
K. Y. Srinivasan | ae33933 | 2014-04-23 13:53:39 -0700 | [diff] [blame] | 467 | /* |
| 468 | * The last time we posted a pressure report to host. |
| 469 | */ |
| 470 | static unsigned long last_post_time; |
| 471 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 472 | module_param(hot_add, bool, (S_IRUGO | S_IWUSR)); |
| 473 | MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add"); |
| 474 | |
K. Y. Srinivasan | e500d15 | 2013-02-08 15:57:15 -0800 | [diff] [blame] | 475 | module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR)); |
| 476 | MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure"); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 477 | static atomic_t trans_id = ATOMIC_INIT(0); |
| 478 | |
Himadri Pandya | 2af5e7b | 2019-08-17 04:08:50 +0000 | [diff] [blame^] | 479 | static int dm_ring_size = 20 * 1024; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * Driver specific state. |
| 483 | */ |
| 484 | |
| 485 | enum 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 Pandya | 2af5e7b | 2019-08-17 04:08:50 +0000 | [diff] [blame^] | 495 | static __u8 recv_buffer[HV_HYP_PAGE_SIZE]; |
| 496 | static __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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 499 | |
| 500 | struct 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 Kuznetsov | 549fd28 | 2015-02-28 11:38:59 -0800 | [diff] [blame] | 510 | unsigned int num_pages_onlined; |
| 511 | unsigned int num_pages_added; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 512 | |
| 513 | /* |
K. Y. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 514 | * State to manage the ballooning (up) operation. |
| 515 | */ |
| 516 | struct balloon_state balloon_wrk; |
| 517 | |
| 518 | /* |
K. Y. Srinivasan | c51af82 | 2013-03-15 12:25:41 -0700 | [diff] [blame] | 519 | * State to execute the "hot-add" operation. |
| 520 | */ |
| 521 | struct hot_add_wrk ha_wrk; |
| 522 | |
| 523 | /* |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 524 | * 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. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 535 | * This thread handles hot-add |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 536 | * 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 Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 542 | /* |
| 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. Srinivasan | 22f8847 | 2015-01-09 23:54:30 -0800 | [diff] [blame] | 547 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 548 | /* |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 549 | * A list of hot-add regions. |
| 550 | */ |
| 551 | struct list_head ha_region_list; |
| 552 | |
| 553 | /* |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 554 | * 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 Ng | b3bb97b | 2016-11-06 13:14:09 -0800 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | * The negotiated version agreed by host. |
| 562 | */ |
| 563 | __u32 version; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 564 | }; |
| 565 | |
| 566 | static struct hv_dynmem_device dm_device; |
| 567 | |
K. Y. Srinivasan | ae33933 | 2014-04-23 13:53:39 -0700 | [diff] [blame] | 568 | static void post_status(struct hv_dynmem_device *dm); |
K. Y. Srinivasan | 22f8847 | 2015-01-09 23:54:30 -0800 | [diff] [blame] | 569 | |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 570 | #ifdef CONFIG_MEMORY_HOTPLUG |
Vitaly Kuznetsov | bba072d1 | 2018-03-04 22:17:21 -0700 | [diff] [blame] | 571 | static 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 | |
| 589 | static 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. Srinivasan | 22f8847 | 2015-01-09 23:54:30 -0800 | [diff] [blame] | 625 | static int hv_memory_notifier(struct notifier_block *nb, unsigned long val, |
| 626 | void *v) |
| 627 | { |
Vitaly Kuznetsov | 549fd28 | 2015-02-28 11:38:59 -0800 | [diff] [blame] | 628 | struct memory_notify *mem = (struct memory_notify *)v; |
Vitaly Kuznetsov | bba072d1 | 2018-03-04 22:17:21 -0700 | [diff] [blame] | 629 | unsigned long flags, pfn_count; |
Vitaly Kuznetsov | 549fd28 | 2015-02-28 11:38:59 -0800 | [diff] [blame] | 630 | |
K. Y. Srinivasan | 22f8847 | 2015-01-09 23:54:30 -0800 | [diff] [blame] | 631 | switch (val) { |
K. Y. Srinivasan | 22f8847 | 2015-01-09 23:54:30 -0800 | [diff] [blame] | 632 | case MEM_ONLINE: |
| 633 | case MEM_CANCEL_ONLINE: |
K. Y. Srinivasan | 22f8847 | 2015-01-09 23:54:30 -0800 | [diff] [blame] | 634 | if (dm_device.ha_waiting) { |
| 635 | dm_device.ha_waiting = false; |
| 636 | complete(&dm_device.ol_waitevent); |
| 637 | } |
| 638 | break; |
| 639 | |
K. Y. Srinivasan | 22f8847 | 2015-01-09 23:54:30 -0800 | [diff] [blame] | 640 | case MEM_OFFLINE: |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 641 | spin_lock_irqsave(&dm_device.ha_lock, flags); |
Vitaly Kuznetsov | bba072d1 | 2018-03-04 22:17:21 -0700 | [diff] [blame] | 642 | 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 Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 655 | spin_unlock_irqrestore(&dm_device.ha_lock, flags); |
Vitaly Kuznetsov | 549fd28 | 2015-02-28 11:38:59 -0800 | [diff] [blame] | 656 | break; |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 657 | case MEM_GOING_ONLINE: |
Vitaly Kuznetsov | 549fd28 | 2015-02-28 11:38:59 -0800 | [diff] [blame] | 658 | case MEM_GOING_OFFLINE: |
K. Y. Srinivasan | 22f8847 | 2015-01-09 23:54:30 -0800 | [diff] [blame] | 659 | case MEM_CANCEL_OFFLINE: |
| 660 | break; |
| 661 | } |
| 662 | return NOTIFY_OK; |
| 663 | } |
| 664 | |
| 665 | static struct notifier_block hv_memory_nb = { |
| 666 | .notifier_call = hv_memory_notifier, |
| 667 | .priority = 0 |
| 668 | }; |
| 669 | |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 670 | /* Check if the particular page is backed and can be onlined and online it. */ |
| 671 | static void hv_page_online_one(struct hv_hotadd_state *has, struct page *pg) |
| 672 | { |
David Hildenbrand | fae42c4 | 2019-03-05 15:42:36 -0800 | [diff] [blame] | 673 | if (!has_pfn_is_backed(has, page_to_pfn(pg))) { |
| 674 | if (!PageOffline(pg)) |
| 675 | __SetPageOffline(pg); |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 676 | return; |
David Hildenbrand | fae42c4 | 2019-03-05 15:42:36 -0800 | [diff] [blame] | 677 | } |
| 678 | if (PageOffline(pg)) |
| 679 | __ClearPageOffline(pg); |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 680 | |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 681 | /* 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 Ng | 6df8d9a | 2017-08-06 13:12:53 -0700 | [diff] [blame] | 685 | |
Lance Roy | 1c87dc8 | 2018-10-02 22:38:48 -0700 | [diff] [blame] | 686 | lockdep_assert_held(&dm_device.ha_lock); |
Alex Ng | 6df8d9a | 2017-08-06 13:12:53 -0700 | [diff] [blame] | 687 | dm_device.num_pages_onlined++; |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static void hv_bring_pgs_online(struct hv_hotadd_state *has, |
| 691 | unsigned long start_pfn, unsigned long size) |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 692 | { |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 693 | int i; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 694 | |
Alex Ng | b3bb97b | 2016-11-06 13:14:09 -0800 | [diff] [blame] | 695 | pr_debug("Online %lu pages starting at pfn 0x%lx\n", size, start_pfn); |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 696 | for (i = 0; i < size; i++) |
| 697 | hv_page_online_one(has, pfn_to_page(start_pfn + i)); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 698 | } |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 699 | |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 700 | static void hv_mem_hot_add(unsigned long start, unsigned long size, |
| 701 | unsigned long pfn_count, |
| 702 | struct hv_hotadd_state *has) |
| 703 | { |
| 704 | int ret = 0; |
K. Y. Srinivasan | ed07ec9 | 2013-07-14 22:38:11 -0700 | [diff] [blame] | 705 | int i, nid; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 706 | unsigned long start_pfn; |
| 707 | unsigned long processed_pfn; |
| 708 | unsigned long total_pfn = pfn_count; |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 709 | unsigned long flags; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 710 | |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 711 | for (i = 0; i < (size/HA_CHUNK); i++) { |
| 712 | start_pfn = start + (i * HA_CHUNK); |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 713 | |
| 714 | spin_lock_irqsave(&dm_device.ha_lock, flags); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 715 | has->ha_end_pfn += HA_CHUNK; |
| 716 | |
| 717 | if (total_pfn > HA_CHUNK) { |
| 718 | processed_pfn = HA_CHUNK; |
| 719 | total_pfn -= HA_CHUNK; |
| 720 | } else { |
| 721 | processed_pfn = total_pfn; |
| 722 | total_pfn = 0; |
| 723 | } |
| 724 | |
| 725 | has->covered_end_pfn += processed_pfn; |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 726 | spin_unlock_irqrestore(&dm_device.ha_lock, flags); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 727 | |
| 728 | init_completion(&dm_device.ol_waitevent); |
Vitaly Kuznetsov | a132c54 | 2016-08-24 16:23:11 -0700 | [diff] [blame] | 729 | dm_device.ha_waiting = !memhp_auto_online; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 730 | |
| 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 Kuznetsov | 223e1e4 | 2018-03-04 22:17:19 -0700 | [diff] [blame] | 736 | pr_err("hot_add memory failed error is %d\n", ret); |
K. Y. Srinivasan | 7f4f230 | 2013-03-18 13:51:38 -0700 | [diff] [blame] | 737 | 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 Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 747 | spin_lock_irqsave(&dm_device.ha_lock, flags); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 748 | has->ha_end_pfn -= HA_CHUNK; |
| 749 | has->covered_end_pfn -= processed_pfn; |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 750 | spin_unlock_irqrestore(&dm_device.ha_lock, flags); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 751 | break; |
| 752 | } |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 753 | |
| 754 | /* |
Vitaly Kuznetsov | a132c54 | 2016-08-24 16:23:11 -0700 | [diff] [blame] | 755 | * 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 760 | */ |
Vitaly Kuznetsov | a132c54 | 2016-08-24 16:23:11 -0700 | [diff] [blame] | 761 | if (dm_device.ha_waiting) |
| 762 | wait_for_completion_timeout(&dm_device.ol_waitevent, |
| 763 | 5*HZ); |
K. Y. Srinivasan | ae33933 | 2014-04-23 13:53:39 -0700 | [diff] [blame] | 764 | post_status(&dm_device); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 765 | } |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Arun KS | a9cd410 | 2019-03-05 15:42:14 -0800 | [diff] [blame] | 768 | static void hv_online_page(struct page *pg, unsigned int order) |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 769 | { |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 770 | struct hv_hotadd_state *has; |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 771 | unsigned long flags; |
Vitaly Kuznetsov | 4f098af | 2018-03-04 22:17:20 -0700 | [diff] [blame] | 772 | unsigned long pfn = page_to_pfn(pg); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 773 | |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 774 | spin_lock_irqsave(&dm_device.ha_lock, flags); |
| 775 | list_for_each_entry(has, &dm_device.ha_region_list, list) { |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 776 | /* The page belongs to a different HAS. */ |
Arun KS | a9cd410 | 2019-03-05 15:42:14 -0800 | [diff] [blame] | 777 | if ((pfn < has->start_pfn) || |
| 778 | (pfn + (1UL << order) > has->end_pfn)) |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 779 | continue; |
| 780 | |
Arun KS | a9cd410 | 2019-03-05 15:42:14 -0800 | [diff] [blame] | 781 | hv_bring_pgs_online(has, pfn, 1UL << order); |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 782 | break; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 783 | } |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 784 | spin_unlock_irqrestore(&dm_device.ha_lock, flags); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 787 | static int pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt) |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 788 | { |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 789 | struct hv_hotadd_state *has; |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 790 | struct hv_hotadd_gap *gap; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 791 | unsigned long residual, new_inc; |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 792 | int ret = 0; |
| 793 | unsigned long flags; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 794 | |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 795 | spin_lock_irqsave(&dm_device.ha_lock, flags); |
| 796 | list_for_each_entry(has, &dm_device.ha_region_list, list) { |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 797 | /* |
| 798 | * If the pfn range we are dealing with is not in the current |
| 799 | * "hot add block", move on. |
| 800 | */ |
Vitaly Kuznetsov | 77c0c97 | 2016-04-30 19:21:35 -0700 | [diff] [blame] | 801 | if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn) |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 802 | continue; |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 803 | |
| 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 Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 810 | if (!gap) { |
| 811 | ret = -ENOMEM; |
| 812 | break; |
| 813 | } |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 814 | |
| 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. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 823 | /* |
| 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 Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 839 | ret = 1; |
| 840 | break; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 841 | } |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 842 | spin_unlock_irqrestore(&dm_device.ha_lock, flags); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 843 | |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 844 | return ret; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static 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. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 853 | struct hv_hotadd_state *has; |
| 854 | unsigned long pgs_ol = 0; |
| 855 | unsigned long old_covered_state; |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 856 | unsigned long res = 0, flags; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 857 | |
Alex Ng | b3bb97b | 2016-11-06 13:14:09 -0800 | [diff] [blame] | 858 | pr_debug("Hot adding %lu pages starting at pfn 0x%lx.\n", pg_count, |
| 859 | pg_start); |
| 860 | |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 861 | spin_lock_irqsave(&dm_device.ha_lock, flags); |
| 862 | list_for_each_entry(has, &dm_device.ha_region_list, list) { |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 863 | /* |
| 864 | * If the pfn range we are dealing with is not in the current |
| 865 | * "hot add block", move on. |
| 866 | */ |
Vitaly Kuznetsov | 77c0c97 | 2016-04-30 19:21:35 -0700 | [diff] [blame] | 867 | if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn) |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 868 | 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 Kuznetsov | d6cbd2c | 2015-03-27 09:10:11 -0700 | [diff] [blame] | 881 | |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 882 | has->covered_end_pfn += pgs_ol; |
| 883 | pfn_cnt -= pgs_ol; |
Vitaly Kuznetsov | d6cbd2c | 2015-03-27 09:10:11 -0700 | [diff] [blame] | 884 | /* |
| 885 | * Check if the corresponding memory block is already |
Vitaly Kuznetsov | da8ced3 | 2019-01-04 15:19:42 +0100 | [diff] [blame] | 886 | * 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 Kuznetsov | d6cbd2c | 2015-03-27 09:10:11 -0700 | [diff] [blame] | 891 | */ |
| 892 | if (start_pfn > has->start_pfn && |
Vitaly Kuznetsov | da8ced3 | 2019-01-04 15:19:42 +0100 | [diff] [blame] | 893 | online_section_nr(pfn_to_section_nr(start_pfn))) |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 894 | hv_bring_pgs_online(has, start_pfn, pgs_ol); |
Vitaly Kuznetsov | d6cbd2c | 2015-03-27 09:10:11 -0700 | [diff] [blame] | 895 | |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 896 | } |
| 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 Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 914 | spin_unlock_irqrestore(&dm_device.ha_lock, flags); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 915 | hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has); |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 916 | spin_lock_irqsave(&dm_device.ha_lock, flags); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 917 | } |
| 918 | /* |
| 919 | * If we managed to online any pages that were given to us, |
| 920 | * we declare success. |
| 921 | */ |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 922 | res = has->covered_end_pfn - old_covered_state; |
| 923 | break; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 924 | } |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 925 | spin_unlock_irqrestore(&dm_device.ha_lock, flags); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 926 | |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 927 | return res; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static 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 Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 936 | int covered; |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 937 | unsigned long flags; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 938 | |
| 939 | if (pfn_cnt == 0) |
| 940 | return 0; |
| 941 | |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 942 | 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. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 948 | goto do_pg_range; |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 949 | } |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 950 | |
| 951 | /* |
| 952 | * If the host has specified a hot-add range; deal with it first. |
| 953 | */ |
| 954 | |
K. Y. Srinivasan | 647965a | 2013-03-29 07:36:11 -0700 | [diff] [blame] | 955 | if (rg_size != 0) { |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 956 | 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 Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 961 | INIT_LIST_HEAD(&ha_region->gap_list); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 962 | |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 963 | ha_region->start_pfn = rg_start; |
| 964 | ha_region->ha_end_pfn = rg_start; |
Vitaly Kuznetsov | 7cf3b79 | 2016-08-24 16:23:09 -0700 | [diff] [blame] | 965 | ha_region->covered_start_pfn = pg_start; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 966 | ha_region->covered_end_pfn = pg_start; |
| 967 | ha_region->end_pfn = rg_start + rg_size; |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 968 | |
| 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. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | do_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 | |
| 984 | static 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 993 | 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 996 | |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 997 | #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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1000 | |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1001 | 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. Srinivasan | 7f4f230 | 2013-03-18 13:51:38 -0700 | [diff] [blame] | 1026 | if (do_hot_add) |
| 1027 | resp.page_count = process_hot_add(pg_start, pfn_cnt, |
| 1028 | rg_start, rg_sz); |
Vitaly Kuznetsov | 549fd28 | 2015-02-28 11:38:59 -0800 | [diff] [blame] | 1029 | |
| 1030 | dm->num_pages_added += resp.page_count; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1031 | #endif |
K. Y. Srinivasan | 7f4f230 | 2013-03-18 13:51:38 -0700 | [diff] [blame] | 1032 | /* |
| 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. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1048 | if (resp.page_count > 0) |
| 1049 | resp.result = 1; |
K. Y. Srinivasan | 7f4f230 | 2013-03-18 13:51:38 -0700 | [diff] [blame] | 1050 | else if (!do_hot_add) |
| 1051 | resp.result = 1; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1052 | else |
| 1053 | resp.result = 0; |
| 1054 | |
| 1055 | if (!do_hot_add || (resp.page_count == 0)) |
Vitaly Kuznetsov | 223e1e4 | 2018-03-04 22:17:19 -0700 | [diff] [blame] | 1056 | pr_err("Memory hot add failed\n"); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1057 | |
| 1058 | dm->state = DM_INITIALIZED; |
K. Y. Srinivasan | 20138d6 | 2013-07-17 17:27:27 -0700 | [diff] [blame] | 1059 | resp.hdr.trans_id = atomic_inc_return(&trans_id); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1060 | vmbus_sendpacket(dm->dev->channel, &resp, |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1061 | sizeof(struct dm_hot_add_response), |
| 1062 | (unsigned long)NULL, |
| 1063 | VM_PKT_DATA_INBAND, 0); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg) |
| 1067 | { |
K. Y. Srinivasan | 6427a0d | 2012-12-06 11:06:54 -0800 | [diff] [blame] | 1068 | struct dm_info_header *info_hdr; |
| 1069 | |
| 1070 | info_hdr = (struct dm_info_header *)msg->info; |
| 1071 | |
| 1072 | switch (info_hdr->type) { |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1073 | case INFO_TYPE_MAX_PAGE_CNT: |
Alex Ng | 8500096 | 2016-11-06 13:14:12 -0800 | [diff] [blame] | 1074 | if (info_hdr->data_size == sizeof(__u64)) { |
| 1075 | __u64 *max_page_count = (__u64 *)&info_hdr[1]; |
| 1076 | |
Alex Ng | 7b6e54b | 2017-08-06 13:12:54 -0700 | [diff] [blame] | 1077 | pr_info("Max. dynamic memory size: %llu MB\n", |
Himadri Pandya | 2af5e7b | 2019-08-17 04:08:50 +0000 | [diff] [blame^] | 1078 | (*max_page_count) >> (20 - HV_HYP_PAGE_SHIFT)); |
Alex Ng | 8500096 | 2016-11-06 13:14:12 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1081 | break; |
| 1082 | default: |
Vitaly Kuznetsov | 223e1e4 | 2018-03-04 22:17:19 -0700 | [diff] [blame] | 1083 | pr_warn("Received Unknown type: %d\n", info_hdr->type); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
Wei Yongjun | a6025a2 | 2013-03-20 23:25:59 +0800 | [diff] [blame] | 1087 | static unsigned long compute_balloon_floor(void) |
K. Y. Srinivasan | 1c7db96 | 2013-02-08 15:57:16 -0800 | [diff] [blame] | 1088 | { |
| 1089 | unsigned long min_pages; |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 1090 | unsigned long nr_pages = totalram_pages(); |
K. Y. Srinivasan | 1c7db96 | 2013-02-08 15:57:16 -0800 | [diff] [blame] | 1091 | #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 Kuznetsov | 7fb0e1a | 2015-03-27 09:10:12 -0700 | [diff] [blame] | 1100 | * 8192 744 (1/16) |
| 1101 | * 32768 1512 (1/32) |
K. Y. Srinivasan | 1c7db96 | 2013-02-08 15:57:16 -0800 | [diff] [blame] | 1102 | */ |
Arun KS | 3d6357d | 2018-12-28 00:34:20 -0800 | [diff] [blame] | 1103 | 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. Srinivasan | 1c7db96 | 2013-02-08 15:57:16 -0800 | [diff] [blame] | 1111 | else |
Arun KS | 3d6357d | 2018-12-28 00:34:20 -0800 | [diff] [blame] | 1112 | min_pages = MB2PAGES(488) + (nr_pages >> 5); |
K. Y. Srinivasan | 1c7db96 | 2013-02-08 15:57:16 -0800 | [diff] [blame] | 1113 | #undef MB2PAGES |
| 1114 | return min_pages; |
| 1115 | } |
| 1116 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1117 | /* |
| 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 | |
| 1127 | static void post_status(struct hv_dynmem_device *dm) |
| 1128 | { |
| 1129 | struct dm_status status; |
K. Y. Srinivasan | ae33933 | 2014-04-23 13:53:39 -0700 | [diff] [blame] | 1130 | unsigned long now = jiffies; |
| 1131 | unsigned long last_post = last_post_time; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1132 | |
K. Y. Srinivasan | e500d15 | 2013-02-08 15:57:15 -0800 | [diff] [blame] | 1133 | if (pressure_report_delay > 0) { |
| 1134 | --pressure_report_delay; |
| 1135 | return; |
| 1136 | } |
K. Y. Srinivasan | ae33933 | 2014-04-23 13:53:39 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (!time_after(now, (last_post_time + HZ))) |
| 1139 | return; |
| 1140 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1141 | 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. Srinivasan | 0731572 | 2013-01-25 16:18:47 -0800 | [diff] [blame] | 1146 | /* |
Vitaly Kuznetsov | 549fd28 | 2015-02-28 11:38:59 -0800 | [diff] [blame] | 1147 | * 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. Srinivasan | 0731572 | 2013-01-25 16:18:47 -0800 | [diff] [blame] | 1155 | */ |
Alex Ng | b605c2d | 2016-08-24 16:23:13 -0700 | [diff] [blame] | 1156 | status.num_avail = si_mem_available(); |
K. Y. Srinivasan | 1c7db96 | 2013-02-08 15:57:16 -0800 | [diff] [blame] | 1157 | status.num_committed = vm_memory_committed() + |
Vitaly Kuznetsov | 549fd28 | 2015-02-28 11:38:59 -0800 | [diff] [blame] | 1158 | 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1162 | |
Vitaly Kuznetsov | cf21be9 | 2018-03-04 22:17:22 -0700 | [diff] [blame] | 1163 | 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. Srinivasan | c5e2254 | 2013-07-14 22:38:12 -0700 | [diff] [blame] | 1166 | /* |
| 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. Srinivasan | ae33933 | 2014-04-23 13:53:39 -0700 | [diff] [blame] | 1174 | /* |
| 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1182 | 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-Hartman | 989623c | 2012-11-21 12:46:40 -0800 | [diff] [blame] | 1189 | static void free_balloon_pages(struct hv_dynmem_device *dm, |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1190 | 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 Hildenbrand | fae42c4 | 2019-03-05 15:42:36 -0800 | [diff] [blame] | 1199 | __ClearPageOffline(pg); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1200 | __free_page(pg); |
| 1201 | dm->num_pages_ballooned--; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | |
| 1206 | |
Vitaly Kuznetsov | 797f88c9 | 2015-03-31 11:16:41 -0700 | [diff] [blame] | 1207 | static 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1211 | { |
David Hildenbrand | fae42c4 | 2019-03-05 15:42:36 -0800 | [diff] [blame] | 1212 | unsigned int i, j; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1213 | 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 Pandya | 2af5e7b | 2019-08-17 04:08:50 +0000 | [diff] [blame^] | 1220 | HV_HYP_PAGE_SIZE) |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1221 | 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 Kuznetsov | 0a1a86a | 2015-03-27 09:10:13 -0700 | [diff] [blame] | 1231 | if (!pg) |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1232 | return i * alloc_unit; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1233 | |
| 1234 | dm->num_pages_ballooned += alloc_unit; |
| 1235 | |
K. Y. Srinivasan | f766dc1 | 2013-03-18 13:51:37 -0700 | [diff] [blame] | 1236 | /* |
| 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 Hildenbrand | fae42c4 | 2019-03-05 15:42:36 -0800 | [diff] [blame] | 1244 | /* mark all pages offline */ |
| 1245 | for (j = 0; j < (1 << get_order(alloc_unit << PAGE_SHIFT)); j++) |
| 1246 | __SetPageOffline(pg + j); |
| 1247 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1248 | 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. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 1259 | static void balloon_up(struct work_struct *dummy) |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1260 | { |
Vitaly Kuznetsov | 797f88c9 | 2015-03-31 11:16:41 -0700 | [diff] [blame] | 1261 | unsigned int num_pages = dm_device.balloon_wrk.num_pages; |
| 1262 | unsigned int num_ballooned = 0; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1263 | struct dm_balloon_response *bl_resp; |
| 1264 | int alloc_unit; |
| 1265 | int ret; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1266 | bool done = false; |
| 1267 | int i; |
Alex Ng | b605c2d | 2016-08-24 16:23:13 -0700 | [diff] [blame] | 1268 | long avail_pages; |
Vitaly Kuznetsov | 530d15b | 2015-02-28 11:39:00 -0800 | [diff] [blame] | 1269 | unsigned long floor; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1270 | |
Dexuan Cui | f671223 | 2014-11-24 20:32:43 -0800 | [diff] [blame] | 1271 | /* The host balloons pages in 2M granularity. */ |
| 1272 | WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1273 | |
| 1274 | /* |
K. Y. Srinivasan | f766dc1 | 2013-03-18 13:51:37 -0700 | [diff] [blame] | 1275 | * We will attempt 2M allocations. However, if we fail to |
Himadri Pandya | 2af5e7b | 2019-08-17 04:08:50 +0000 | [diff] [blame^] | 1276 | * allocate 2M chunks, we will go back to PAGE_SIZE allocations. |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1277 | */ |
Himadri Pandya | 2af5e7b | 2019-08-17 04:08:50 +0000 | [diff] [blame^] | 1278 | alloc_unit = PAGES_IN_2M; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1279 | |
Alex Ng | b605c2d | 2016-08-24 16:23:13 -0700 | [diff] [blame] | 1280 | avail_pages = si_mem_available(); |
Vitaly Kuznetsov | 530d15b | 2015-02-28 11:39:00 -0800 | [diff] [blame] | 1281 | floor = compute_balloon_floor(); |
| 1282 | |
| 1283 | /* Refuse to balloon below the floor, keep the 2M granularity. */ |
Alex Ng | b605c2d | 2016-08-24 16:23:13 -0700 | [diff] [blame] | 1284 | if (avail_pages < num_pages || avail_pages - num_pages < floor) { |
Alex Ng | b3bb97b | 2016-11-06 13:14:09 -0800 | [diff] [blame] | 1285 | pr_warn("Balloon request will be partially fulfilled. %s\n", |
| 1286 | avail_pages < num_pages ? "Not enough memory." : |
| 1287 | "Balloon floor reached."); |
| 1288 | |
Alex Ng | b605c2d | 2016-08-24 16:23:13 -0700 | [diff] [blame] | 1289 | num_pages = avail_pages > floor ? (avail_pages - floor) : 0; |
Vitaly Kuznetsov | 530d15b | 2015-02-28 11:39:00 -0800 | [diff] [blame] | 1290 | num_pages -= num_pages % PAGES_IN_2M; |
| 1291 | } |
| 1292 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1293 | while (!done) { |
Himadri Pandya | 2af5e7b | 2019-08-17 04:08:50 +0000 | [diff] [blame^] | 1294 | memset(balloon_up_send_buffer, 0, HV_HYP_PAGE_SIZE); |
Dexuan Cui | 1fed17d | 2019-06-14 18:42:17 +0000 | [diff] [blame] | 1295 | bl_resp = (struct dm_balloon_response *)balloon_up_send_buffer; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1296 | bl_resp->hdr.type = DM_BALLOON_RESPONSE; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1297 | bl_resp->hdr.size = sizeof(struct dm_balloon_response); |
| 1298 | bl_resp->more_pages = 1; |
| 1299 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1300 | num_pages -= num_ballooned; |
K. Y. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 1301 | num_ballooned = alloc_balloon_pages(&dm_device, num_pages, |
Vitaly Kuznetsov | 0a1a86a | 2015-03-27 09:10:13 -0700 | [diff] [blame] | 1302 | bl_resp, alloc_unit); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1303 | |
Dexuan Cui | f671223 | 2014-11-24 20:32:43 -0800 | [diff] [blame] | 1304 | if (alloc_unit != 1 && num_ballooned == 0) { |
K. Y. Srinivasan | f766dc1 | 2013-03-18 13:51:37 -0700 | [diff] [blame] | 1305 | alloc_unit = 1; |
| 1306 | continue; |
| 1307 | } |
| 1308 | |
Vitaly Kuznetsov | 0a1a86a | 2015-03-27 09:10:13 -0700 | [diff] [blame] | 1309 | if (num_ballooned == 0 || num_ballooned == num_pages) { |
Alex Ng | b3bb97b | 2016-11-06 13:14:09 -0800 | [diff] [blame] | 1310 | pr_debug("Ballooned %u out of %u requested pages.\n", |
| 1311 | num_pages, dm_device.balloon_wrk.num_pages); |
| 1312 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1313 | bl_resp->more_pages = 0; |
| 1314 | done = true; |
K. Y. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 1315 | dm_device.state = DM_INITIALIZED; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1316 | } |
| 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. Srinivasan | 20138d6 | 2013-07-17 17:27:27 -0700 | [diff] [blame] | 1325 | bl_resp->hdr.trans_id = atomic_inc_return(&trans_id); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1326 | 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. Srinivasan | ae33933 | 2014-04-23 13:53:39 -0700 | [diff] [blame] | 1334 | post_status(&dm_device); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1335 | } while (ret == -EAGAIN); |
| 1336 | |
| 1337 | if (ret) { |
| 1338 | /* |
| 1339 | * Free up the memory we allocatted. |
| 1340 | */ |
Vitaly Kuznetsov | 223e1e4 | 2018-03-04 22:17:19 -0700 | [diff] [blame] | 1341 | pr_err("Balloon response failed\n"); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1342 | |
| 1343 | for (i = 0; i < bl_resp->range_count; i++) |
K. Y. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 1344 | free_balloon_pages(&dm_device, |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1345 | &bl_resp->range_array[i]); |
| 1346 | |
| 1347 | done = true; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | } |
| 1352 | |
| 1353 | static 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 Ng | b3bb97b | 2016-11-06 13:14:09 -0800 | [diff] [blame] | 1360 | unsigned int prev_pages_ballooned = dm->num_pages_ballooned; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1361 | |
K. Y. Srinivasan | ae33933 | 2014-04-23 13:53:39 -0700 | [diff] [blame] | 1362 | for (i = 0; i < range_count; i++) { |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1363 | free_balloon_pages(dm, &range_array[i]); |
K. Y. Srinivasan | ab3de22 | 2015-01-09 23:54:31 -0800 | [diff] [blame] | 1364 | complete(&dm_device.config_event); |
K. Y. Srinivasan | ae33933 | 2014-04-23 13:53:39 -0700 | [diff] [blame] | 1365 | } |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1366 | |
Alex Ng | b3bb97b | 2016-11-06 13:14:09 -0800 | [diff] [blame] | 1367 | pr_debug("Freed %u ballooned pages.\n", |
| 1368 | prev_pages_ballooned - dm->num_pages_ballooned); |
| 1369 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1370 | 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 | |
| 1386 | static void balloon_onchannelcallback(void *context); |
| 1387 | |
| 1388 | static int dm_thread_func(void *dm_dev) |
| 1389 | { |
| 1390 | struct hv_dynmem_device *dm = dm_dev; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1391 | |
| 1392 | while (!kthread_should_stop()) { |
K. Y. Srinivasan | ab3de22 | 2015-01-09 23:54:31 -0800 | [diff] [blame] | 1393 | wait_for_completion_interruptible_timeout( |
K. Y. Srinivasan | 5dba4c5 | 2014-02-13 16:24:33 -0800 | [diff] [blame] | 1394 | &dm_device.config_event, 1*HZ); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1395 | /* |
| 1396 | * The host expects us to post information on the memory |
| 1397 | * pressure every second. |
| 1398 | */ |
K. Y. Srinivasan | ab3de22 | 2015-01-09 23:54:31 -0800 | [diff] [blame] | 1399 | reinit_completion(&dm_device.config_event); |
| 1400 | post_status(dm); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | |
| 1407 | static 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1432 | 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 Ng | b6ddeae | 2015-08-01 16:08:13 -0700 | [diff] [blame] | 1436 | version_req.version.version = dm->next_version; |
Alex Ng | b3bb97b | 2016-11-06 13:14:09 -0800 | [diff] [blame] | 1437 | dm->version = version_req.version.version; |
Alex Ng | b6ddeae | 2015-08-01 16:08:13 -0700 | [diff] [blame] | 1438 | |
| 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1452 | |
| 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 | |
| 1463 | version_error: |
| 1464 | dm->state = DM_INIT_ERROR; |
| 1465 | complete(&dm->host_event); |
| 1466 | } |
| 1467 | |
| 1468 | static void cap_resp(struct hv_dynmem_device *dm, |
| 1469 | struct dm_capabilities_resp_msg *cap_resp) |
| 1470 | { |
| 1471 | if (!cap_resp->is_accepted) { |
Vitaly Kuznetsov | 223e1e4 | 2018-03-04 22:17:19 -0700 | [diff] [blame] | 1472 | pr_err("Capabilities not accepted by host\n"); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1473 | dm->state = DM_INIT_ERROR; |
| 1474 | } |
| 1475 | complete(&dm->host_event); |
| 1476 | } |
| 1477 | |
| 1478 | static 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. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 1486 | struct dm_balloon *bal_msg; |
K. Y. Srinivasan | c51af82 | 2013-03-15 12:25:41 -0700 | [diff] [blame] | 1487 | struct dm_hot_add *ha_msg; |
| 1488 | union dm_mem_page_range *ha_pg_range; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1489 | union dm_mem_page_range *ha_region; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1490 | |
| 1491 | memset(recv_buffer, 0, sizeof(recv_buffer)); |
| 1492 | vmbus_recvpacket(dev->channel, recv_buffer, |
Himadri Pandya | 2af5e7b | 2019-08-17 04:08:50 +0000 | [diff] [blame^] | 1493 | HV_HYP_PAGE_SIZE, &recvlen, &requestid); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1494 | |
| 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. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 1511 | if (dm->state == DM_BALLOON_UP) |
| 1512 | pr_warn("Currently ballooning\n"); |
| 1513 | bal_msg = (struct dm_balloon *)recv_buffer; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1514 | dm->state = DM_BALLOON_UP; |
K. Y. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 1515 | dm_device.balloon_wrk.num_pages = bal_msg->num_pages; |
| 1516 | schedule_work(&dm_device.balloon_wrk.wrk); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1517 | 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. Srinivasan | c51af82 | 2013-03-15 12:25:41 -0700 | [diff] [blame] | 1526 | if (dm->state == DM_HOT_ADD) |
| 1527 | pr_warn("Currently hot-adding\n"); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1528 | dm->state = DM_HOT_ADD; |
K. Y. Srinivasan | c51af82 | 2013-03-15 12:25:41 -0700 | [diff] [blame] | 1529 | ha_msg = (struct dm_hot_add *)recv_buffer; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1530 | 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 Kuznetsov | d19a55d | 2016-04-30 19:21:36 -0700 | [diff] [blame] | 1535 | dm->host_specified_ha_region = false; |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1536 | 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. Srinivasan | c51af82 | 2013-03-15 12:25:41 -0700 | [diff] [blame] | 1551 | schedule_work(&dm_device.ha_wrk.wrk); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1552 | break; |
| 1553 | |
| 1554 | case DM_INFO_MESSAGE: |
| 1555 | process_info(dm, (struct dm_info_msg *)dm_msg); |
| 1556 | break; |
| 1557 | |
| 1558 | default: |
Vitaly Kuznetsov | 223e1e4 | 2018-03-04 22:17:19 -0700 | [diff] [blame] | 1559 | pr_warn("Unhandled message: type: %d\n", dm_hdr->type); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1560 | |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | } |
| 1565 | |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1566 | static int balloon_connect_vsp(struct hv_device *dev) |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1567 | { |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1568 | struct dm_version_request version_req; |
| 1569 | struct dm_capabilities cap_msg; |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1570 | unsigned long t; |
| 1571 | int ret; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1572 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1573 | ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0, |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1574 | balloon_onchannelcallback, dev); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1575 | if (ret) |
Dexuan Cui | 1fed17d | 2019-06-14 18:42:17 +0000 | [diff] [blame] | 1576 | return ret; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1577 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1578 | /* |
| 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 Ng | b6ddeae | 2015-08-01 16:08:13 -0700 | [diff] [blame] | 1588 | version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN10; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1589 | version_req.is_last_attempt = 0; |
Alex Ng | b3bb97b | 2016-11-06 13:14:09 -0800 | [diff] [blame] | 1590 | dm_device.version = version_req.version.version; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1591 | |
| 1592 | ret = vmbus_sendpacket(dev->channel, &version_req, |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1593 | sizeof(struct dm_version_request), |
| 1594 | (unsigned long)NULL, VM_PKT_DATA_INBAND, 0); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1595 | if (ret) |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1596 | goto out; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1597 | |
| 1598 | t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ); |
| 1599 | if (t == 0) { |
| 1600 | ret = -ETIMEDOUT; |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1601 | goto out; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1602 | } |
| 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 Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1609 | ret = -EPROTO; |
| 1610 | goto out; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1611 | } |
Alex Ng | b3bb97b | 2016-11-06 13:14:09 -0800 | [diff] [blame] | 1612 | |
| 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1617 | /* |
| 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1626 | cap_msg.caps.cap_bits.hot_add = 1; |
| 1627 | |
| 1628 | /* |
K. Y. Srinivasan | 647965a | 2013-03-29 07:36:11 -0700 | [diff] [blame] | 1629 | * 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1635 | * 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 Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1643 | sizeof(struct dm_capabilities), |
| 1644 | (unsigned long)NULL, VM_PKT_DATA_INBAND, 0); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1645 | if (ret) |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1646 | goto out; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1647 | |
| 1648 | t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ); |
| 1649 | if (t == 0) { |
| 1650 | ret = -ETIMEDOUT; |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1651 | goto out; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1652 | } |
| 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 Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1659 | ret = -EPROTO; |
| 1660 | goto out; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1661 | } |
| 1662 | |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1663 | return 0; |
| 1664 | out: |
| 1665 | vmbus_close(dev->channel); |
| 1666 | return ret; |
| 1667 | } |
| 1668 | |
| 1669 | static 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1701 | dm_device.state = DM_INITIALIZED; |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1702 | |
| 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. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1709 | |
| 1710 | return 0; |
| 1711 | |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1712 | probe_error: |
| 1713 | vmbus_close(dev->channel); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1714 | #ifdef CONFIG_MEMORY_HOTPLUG |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1715 | unregister_memory_notifier(&hv_memory_nb); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1716 | restore_online_page_callback(&hv_online_page); |
| 1717 | #endif |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1718 | return ret; |
| 1719 | } |
| 1720 | |
| 1721 | static int balloon_remove(struct hv_device *dev) |
| 1722 | { |
| 1723 | struct hv_dynmem_device *dm = hv_get_drvdata(dev); |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 1724 | struct hv_hotadd_state *has, *tmp; |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 1725 | struct hv_hotadd_gap *gap, *tmp_gap; |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 1726 | unsigned long flags; |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1727 | |
| 1728 | if (dm->num_pages_ballooned != 0) |
| 1729 | pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned); |
| 1730 | |
K. Y. Srinivasan | 6571b2d | 2013-03-15 12:25:40 -0700 | [diff] [blame] | 1731 | cancel_work_sync(&dm->balloon_wrk.wrk); |
K. Y. Srinivasan | c51af82 | 2013-03-15 12:25:41 -0700 | [diff] [blame] | 1732 | cancel_work_sync(&dm->ha_wrk.wrk); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1733 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1734 | kthread_stop(dm->thread); |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1735 | vmbus_close(dev->channel); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1736 | #ifdef CONFIG_MEMORY_HOTPLUG |
K. Y. Srinivasan | 22f8847 | 2015-01-09 23:54:30 -0800 | [diff] [blame] | 1737 | unregister_memory_notifier(&hv_memory_nb); |
Dexuan Cui | 221f6df | 2019-06-14 18:42:30 +0000 | [diff] [blame] | 1738 | restore_online_page_callback(&hv_online_page); |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1739 | #endif |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 1740 | spin_lock_irqsave(&dm_device.ha_lock, flags); |
| 1741 | list_for_each_entry_safe(has, tmp, &dm->ha_region_list, list) { |
Vitaly Kuznetsov | cb7a572 | 2016-08-24 16:23:10 -0700 | [diff] [blame] | 1742 | list_for_each_entry_safe(gap, tmp_gap, &has->gap_list, list) { |
| 1743 | list_del(&gap->list); |
| 1744 | kfree(gap); |
| 1745 | } |
K. Y. Srinivasan | 1cac8cd | 2013-03-15 12:25:43 -0700 | [diff] [blame] | 1746 | list_del(&has->list); |
| 1747 | kfree(has); |
| 1748 | } |
Vitaly Kuznetsov | eece30b | 2016-08-24 16:23:12 -0700 | [diff] [blame] | 1749 | spin_unlock_irqrestore(&dm_device.ha_lock, flags); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1750 | |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
| 1754 | static const struct hv_vmbus_device_id id_table[] = { |
| 1755 | /* Dynamic Memory Class ID */ |
| 1756 | /* 525074DC-8985-46e2-8057-A307DC18A502 */ |
K. Y. Srinivasan | d13984e | 2013-01-23 17:42:41 -0800 | [diff] [blame] | 1757 | { HV_DM_GUID, }, |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1758 | { }, |
| 1759 | }; |
| 1760 | |
| 1761 | MODULE_DEVICE_TABLE(vmbus, id_table); |
| 1762 | |
| 1763 | static 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 Ven | af0a564 | 2018-06-05 13:37:49 -0700 | [diff] [blame] | 1768 | .driver = { |
| 1769 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 1770 | }, |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1771 | }; |
| 1772 | |
| 1773 | static int __init init_balloon_drv(void) |
| 1774 | { |
| 1775 | |
| 1776 | return vmbus_driver_register(&balloon_drv); |
| 1777 | } |
| 1778 | |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1779 | module_init(init_balloon_drv); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1780 | |
| 1781 | MODULE_DESCRIPTION("Hyper-V Balloon"); |
K. Y. Srinivasan | 9aa8b50 | 2012-11-14 01:09:02 -0800 | [diff] [blame] | 1782 | MODULE_LICENSE("GPL"); |