PageRenderTime 116ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/hv/hv_balloon.c

https://gitlab.com/CadeLaRen/linux
C | 1633 lines | 901 code | 263 blank | 469 comment | 121 complexity | 41a5f2b4d8bf2311d6848283ceeae718 MD5 | raw file
  1. /*
  2. * Copyright (c) 2012, Microsoft Corporation.
  3. *
  4. * Author:
  5. * K. Y. Srinivasan <kys@microsoft.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  14. * NON INFRINGEMENT. See the GNU General Public License for more
  15. * details.
  16. *
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/mman.h>
  22. #include <linux/delay.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/kthread.h>
  27. #include <linux/completion.h>
  28. #include <linux/memory_hotplug.h>
  29. #include <linux/memory.h>
  30. #include <linux/notifier.h>
  31. #include <linux/percpu_counter.h>
  32. #include <linux/hyperv.h>
  33. /*
  34. * We begin with definitions supporting the Dynamic Memory protocol
  35. * with the host.
  36. *
  37. * Begin protocol definitions.
  38. */
  39. /*
  40. * Protocol versions. The low word is the minor version, the high word the major
  41. * version.
  42. *
  43. * History:
  44. * Initial version 1.0
  45. * Changed to 0.1 on 2009/03/25
  46. * Changes to 0.2 on 2009/05/14
  47. * Changes to 0.3 on 2009/12/03
  48. * Changed to 1.0 on 2011/04/05
  49. */
  50. #define DYNMEM_MAKE_VERSION(Major, Minor) ((__u32)(((Major) << 16) | (Minor)))
  51. #define DYNMEM_MAJOR_VERSION(Version) ((__u32)(Version) >> 16)
  52. #define DYNMEM_MINOR_VERSION(Version) ((__u32)(Version) & 0xff)
  53. enum {
  54. DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
  55. DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
  56. DYNMEM_PROTOCOL_VERSION_3 = DYNMEM_MAKE_VERSION(2, 0),
  57. DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
  58. DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
  59. DYNMEM_PROTOCOL_VERSION_WIN10 = DYNMEM_PROTOCOL_VERSION_3,
  60. DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN10
  61. };
  62. /*
  63. * Message Types
  64. */
  65. enum dm_message_type {
  66. /*
  67. * Version 0.3
  68. */
  69. DM_ERROR = 0,
  70. DM_VERSION_REQUEST = 1,
  71. DM_VERSION_RESPONSE = 2,
  72. DM_CAPABILITIES_REPORT = 3,
  73. DM_CAPABILITIES_RESPONSE = 4,
  74. DM_STATUS_REPORT = 5,
  75. DM_BALLOON_REQUEST = 6,
  76. DM_BALLOON_RESPONSE = 7,
  77. DM_UNBALLOON_REQUEST = 8,
  78. DM_UNBALLOON_RESPONSE = 9,
  79. DM_MEM_HOT_ADD_REQUEST = 10,
  80. DM_MEM_HOT_ADD_RESPONSE = 11,
  81. DM_VERSION_03_MAX = 11,
  82. /*
  83. * Version 1.0.
  84. */
  85. DM_INFO_MESSAGE = 12,
  86. DM_VERSION_1_MAX = 12
  87. };
  88. /*
  89. * Structures defining the dynamic memory management
  90. * protocol.
  91. */
  92. union dm_version {
  93. struct {
  94. __u16 minor_version;
  95. __u16 major_version;
  96. };
  97. __u32 version;
  98. } __packed;
  99. union dm_caps {
  100. struct {
  101. __u64 balloon:1;
  102. __u64 hot_add:1;
  103. /*
  104. * To support guests that may have alignment
  105. * limitations on hot-add, the guest can specify
  106. * its alignment requirements; a value of n
  107. * represents an alignment of 2^n in mega bytes.
  108. */
  109. __u64 hot_add_alignment:4;
  110. __u64 reservedz:58;
  111. } cap_bits;
  112. __u64 caps;
  113. } __packed;
  114. union dm_mem_page_range {
  115. struct {
  116. /*
  117. * The PFN number of the first page in the range.
  118. * 40 bits is the architectural limit of a PFN
  119. * number for AMD64.
  120. */
  121. __u64 start_page:40;
  122. /*
  123. * The number of pages in the range.
  124. */
  125. __u64 page_cnt:24;
  126. } finfo;
  127. __u64 page_range;
  128. } __packed;
  129. /*
  130. * The header for all dynamic memory messages:
  131. *
  132. * type: Type of the message.
  133. * size: Size of the message in bytes; including the header.
  134. * trans_id: The guest is responsible for manufacturing this ID.
  135. */
  136. struct dm_header {
  137. __u16 type;
  138. __u16 size;
  139. __u32 trans_id;
  140. } __packed;
  141. /*
  142. * A generic message format for dynamic memory.
  143. * Specific message formats are defined later in the file.
  144. */
  145. struct dm_message {
  146. struct dm_header hdr;
  147. __u8 data[]; /* enclosed message */
  148. } __packed;
  149. /*
  150. * Specific message types supporting the dynamic memory protocol.
  151. */
  152. /*
  153. * Version negotiation message. Sent from the guest to the host.
  154. * The guest is free to try different versions until the host
  155. * accepts the version.
  156. *
  157. * dm_version: The protocol version requested.
  158. * is_last_attempt: If TRUE, this is the last version guest will request.
  159. * reservedz: Reserved field, set to zero.
  160. */
  161. struct dm_version_request {
  162. struct dm_header hdr;
  163. union dm_version version;
  164. __u32 is_last_attempt:1;
  165. __u32 reservedz:31;
  166. } __packed;
  167. /*
  168. * Version response message; Host to Guest and indicates
  169. * if the host has accepted the version sent by the guest.
  170. *
  171. * is_accepted: If TRUE, host has accepted the version and the guest
  172. * should proceed to the next stage of the protocol. FALSE indicates that
  173. * guest should re-try with a different version.
  174. *
  175. * reservedz: Reserved field, set to zero.
  176. */
  177. struct dm_version_response {
  178. struct dm_header hdr;
  179. __u64 is_accepted:1;
  180. __u64 reservedz:63;
  181. } __packed;
  182. /*
  183. * Message reporting capabilities. This is sent from the guest to the
  184. * host.
  185. */
  186. struct dm_capabilities {
  187. struct dm_header hdr;
  188. union dm_caps caps;
  189. __u64 min_page_cnt;
  190. __u64 max_page_number;
  191. } __packed;
  192. /*
  193. * Response to the capabilities message. This is sent from the host to the
  194. * guest. This message notifies if the host has accepted the guest's
  195. * capabilities. If the host has not accepted, the guest must shutdown
  196. * the service.
  197. *
  198. * is_accepted: Indicates if the host has accepted guest's capabilities.
  199. * reservedz: Must be 0.
  200. */
  201. struct dm_capabilities_resp_msg {
  202. struct dm_header hdr;
  203. __u64 is_accepted:1;
  204. __u64 reservedz:63;
  205. } __packed;
  206. /*
  207. * This message is used to report memory pressure from the guest.
  208. * This message is not part of any transaction and there is no
  209. * response to this message.
  210. *
  211. * num_avail: Available memory in pages.
  212. * num_committed: Committed memory in pages.
  213. * page_file_size: The accumulated size of all page files
  214. * in the system in pages.
  215. * zero_free: The nunber of zero and free pages.
  216. * page_file_writes: The writes to the page file in pages.
  217. * io_diff: An indicator of file cache efficiency or page file activity,
  218. * calculated as File Cache Page Fault Count - Page Read Count.
  219. * This value is in pages.
  220. *
  221. * Some of these metrics are Windows specific and fortunately
  222. * the algorithm on the host side that computes the guest memory
  223. * pressure only uses num_committed value.
  224. */
  225. struct dm_status {
  226. struct dm_header hdr;
  227. __u64 num_avail;
  228. __u64 num_committed;
  229. __u64 page_file_size;
  230. __u64 zero_free;
  231. __u32 page_file_writes;
  232. __u32 io_diff;
  233. } __packed;
  234. /*
  235. * Message to ask the guest to allocate memory - balloon up message.
  236. * This message is sent from the host to the guest. The guest may not be
  237. * able to allocate as much memory as requested.
  238. *
  239. * num_pages: number of pages to allocate.
  240. */
  241. struct dm_balloon {
  242. struct dm_header hdr;
  243. __u32 num_pages;
  244. __u32 reservedz;
  245. } __packed;
  246. /*
  247. * Balloon response message; this message is sent from the guest
  248. * to the host in response to the balloon message.
  249. *
  250. * reservedz: Reserved; must be set to zero.
  251. * more_pages: If FALSE, this is the last message of the transaction.
  252. * if TRUE there will atleast one more message from the guest.
  253. *
  254. * range_count: The number of ranges in the range array.
  255. *
  256. * range_array: An array of page ranges returned to the host.
  257. *
  258. */
  259. struct dm_balloon_response {
  260. struct dm_header hdr;
  261. __u32 reservedz;
  262. __u32 more_pages:1;
  263. __u32 range_count:31;
  264. union dm_mem_page_range range_array[];
  265. } __packed;
  266. /*
  267. * Un-balloon message; this message is sent from the host
  268. * to the guest to give guest more memory.
  269. *
  270. * more_pages: If FALSE, this is the last message of the transaction.
  271. * if TRUE there will atleast one more message from the guest.
  272. *
  273. * reservedz: Reserved; must be set to zero.
  274. *
  275. * range_count: The number of ranges in the range array.
  276. *
  277. * range_array: An array of page ranges returned to the host.
  278. *
  279. */
  280. struct dm_unballoon_request {
  281. struct dm_header hdr;
  282. __u32 more_pages:1;
  283. __u32 reservedz:31;
  284. __u32 range_count;
  285. union dm_mem_page_range range_array[];
  286. } __packed;
  287. /*
  288. * Un-balloon response message; this message is sent from the guest
  289. * to the host in response to an unballoon request.
  290. *
  291. */
  292. struct dm_unballoon_response {
  293. struct dm_header hdr;
  294. } __packed;
  295. /*
  296. * Hot add request message. Message sent from the host to the guest.
  297. *
  298. * mem_range: Memory range to hot add.
  299. *
  300. * On Linux we currently don't support this since we cannot hot add
  301. * arbitrary granularity of memory.
  302. */
  303. struct dm_hot_add {
  304. struct dm_header hdr;
  305. union dm_mem_page_range range;
  306. } __packed;
  307. /*
  308. * Hot add response message.
  309. * This message is sent by the guest to report the status of a hot add request.
  310. * If page_count is less than the requested page count, then the host should
  311. * assume all further hot add requests will fail, since this indicates that
  312. * the guest has hit an upper physical memory barrier.
  313. *
  314. * Hot adds may also fail due to low resources; in this case, the guest must
  315. * not complete this message until the hot add can succeed, and the host must
  316. * not send a new hot add request until the response is sent.
  317. * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
  318. * times it fails the request.
  319. *
  320. *
  321. * page_count: number of pages that were successfully hot added.
  322. *
  323. * result: result of the operation 1: success, 0: failure.
  324. *
  325. */
  326. struct dm_hot_add_response {
  327. struct dm_header hdr;
  328. __u32 page_count;
  329. __u32 result;
  330. } __packed;
  331. /*
  332. * Types of information sent from host to the guest.
  333. */
  334. enum dm_info_type {
  335. INFO_TYPE_MAX_PAGE_CNT = 0,
  336. MAX_INFO_TYPE
  337. };
  338. /*
  339. * Header for the information message.
  340. */
  341. struct dm_info_header {
  342. enum dm_info_type type;
  343. __u32 data_size;
  344. } __packed;
  345. /*
  346. * This message is sent from the host to the guest to pass
  347. * some relevant information (win8 addition).
  348. *
  349. * reserved: no used.
  350. * info_size: size of the information blob.
  351. * info: information blob.
  352. */
  353. struct dm_info_msg {
  354. struct dm_header hdr;
  355. __u32 reserved;
  356. __u32 info_size;
  357. __u8 info[];
  358. };
  359. /*
  360. * End protocol definitions.
  361. */
  362. /*
  363. * State to manage hot adding memory into the guest.
  364. * The range start_pfn : end_pfn specifies the range
  365. * that the host has asked us to hot add. The range
  366. * start_pfn : ha_end_pfn specifies the range that we have
  367. * currently hot added. We hot add in multiples of 128M
  368. * chunks; it is possible that we may not be able to bring
  369. * online all the pages in the region. The range
  370. * covered_end_pfn defines the pages that can
  371. * be brough online.
  372. */
  373. struct hv_hotadd_state {
  374. struct list_head list;
  375. unsigned long start_pfn;
  376. unsigned long covered_end_pfn;
  377. unsigned long ha_end_pfn;
  378. unsigned long end_pfn;
  379. };
  380. struct balloon_state {
  381. __u32 num_pages;
  382. struct work_struct wrk;
  383. };
  384. struct hot_add_wrk {
  385. union dm_mem_page_range ha_page_range;
  386. union dm_mem_page_range ha_region_range;
  387. struct work_struct wrk;
  388. };
  389. static bool hot_add = true;
  390. static bool do_hot_add;
  391. /*
  392. * Delay reporting memory pressure by
  393. * the specified number of seconds.
  394. */
  395. static uint pressure_report_delay = 45;
  396. /*
  397. * The last time we posted a pressure report to host.
  398. */
  399. static unsigned long last_post_time;
  400. module_param(hot_add, bool, (S_IRUGO | S_IWUSR));
  401. MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
  402. module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR));
  403. MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
  404. static atomic_t trans_id = ATOMIC_INIT(0);
  405. static int dm_ring_size = (5 * PAGE_SIZE);
  406. /*
  407. * Driver specific state.
  408. */
  409. enum hv_dm_state {
  410. DM_INITIALIZING = 0,
  411. DM_INITIALIZED,
  412. DM_BALLOON_UP,
  413. DM_BALLOON_DOWN,
  414. DM_HOT_ADD,
  415. DM_INIT_ERROR
  416. };
  417. static __u8 recv_buffer[PAGE_SIZE];
  418. static __u8 *send_buffer;
  419. #define PAGES_IN_2M 512
  420. #define HA_CHUNK (32 * 1024)
  421. struct hv_dynmem_device {
  422. struct hv_device *dev;
  423. enum hv_dm_state state;
  424. struct completion host_event;
  425. struct completion config_event;
  426. /*
  427. * Number of pages we have currently ballooned out.
  428. */
  429. unsigned int num_pages_ballooned;
  430. unsigned int num_pages_onlined;
  431. unsigned int num_pages_added;
  432. /*
  433. * State to manage the ballooning (up) operation.
  434. */
  435. struct balloon_state balloon_wrk;
  436. /*
  437. * State to execute the "hot-add" operation.
  438. */
  439. struct hot_add_wrk ha_wrk;
  440. /*
  441. * This state tracks if the host has specified a hot-add
  442. * region.
  443. */
  444. bool host_specified_ha_region;
  445. /*
  446. * State to synchronize hot-add.
  447. */
  448. struct completion ol_waitevent;
  449. bool ha_waiting;
  450. /*
  451. * This thread handles hot-add
  452. * requests from the host as well as notifying
  453. * the host with regards to memory pressure in
  454. * the guest.
  455. */
  456. struct task_struct *thread;
  457. struct mutex ha_region_mutex;
  458. /*
  459. * A list of hot-add regions.
  460. */
  461. struct list_head ha_region_list;
  462. /*
  463. * We start with the highest version we can support
  464. * and downgrade based on the host; we save here the
  465. * next version to try.
  466. */
  467. __u32 next_version;
  468. };
  469. static struct hv_dynmem_device dm_device;
  470. static void post_status(struct hv_dynmem_device *dm);
  471. #ifdef CONFIG_MEMORY_HOTPLUG
  472. static int hv_memory_notifier(struct notifier_block *nb, unsigned long val,
  473. void *v)
  474. {
  475. struct memory_notify *mem = (struct memory_notify *)v;
  476. switch (val) {
  477. case MEM_GOING_ONLINE:
  478. mutex_lock(&dm_device.ha_region_mutex);
  479. break;
  480. case MEM_ONLINE:
  481. dm_device.num_pages_onlined += mem->nr_pages;
  482. case MEM_CANCEL_ONLINE:
  483. if (val == MEM_ONLINE ||
  484. mutex_is_locked(&dm_device.ha_region_mutex))
  485. mutex_unlock(&dm_device.ha_region_mutex);
  486. if (dm_device.ha_waiting) {
  487. dm_device.ha_waiting = false;
  488. complete(&dm_device.ol_waitevent);
  489. }
  490. break;
  491. case MEM_OFFLINE:
  492. mutex_lock(&dm_device.ha_region_mutex);
  493. dm_device.num_pages_onlined -= mem->nr_pages;
  494. mutex_unlock(&dm_device.ha_region_mutex);
  495. break;
  496. case MEM_GOING_OFFLINE:
  497. case MEM_CANCEL_OFFLINE:
  498. break;
  499. }
  500. return NOTIFY_OK;
  501. }
  502. static struct notifier_block hv_memory_nb = {
  503. .notifier_call = hv_memory_notifier,
  504. .priority = 0
  505. };
  506. static void hv_bring_pgs_online(unsigned long start_pfn, unsigned long size)
  507. {
  508. int i;
  509. for (i = 0; i < size; i++) {
  510. struct page *pg;
  511. pg = pfn_to_page(start_pfn + i);
  512. __online_page_set_limits(pg);
  513. __online_page_increment_counters(pg);
  514. __online_page_free(pg);
  515. }
  516. }
  517. static void hv_mem_hot_add(unsigned long start, unsigned long size,
  518. unsigned long pfn_count,
  519. struct hv_hotadd_state *has)
  520. {
  521. int ret = 0;
  522. int i, nid;
  523. unsigned long start_pfn;
  524. unsigned long processed_pfn;
  525. unsigned long total_pfn = pfn_count;
  526. for (i = 0; i < (size/HA_CHUNK); i++) {
  527. start_pfn = start + (i * HA_CHUNK);
  528. has->ha_end_pfn += HA_CHUNK;
  529. if (total_pfn > HA_CHUNK) {
  530. processed_pfn = HA_CHUNK;
  531. total_pfn -= HA_CHUNK;
  532. } else {
  533. processed_pfn = total_pfn;
  534. total_pfn = 0;
  535. }
  536. has->covered_end_pfn += processed_pfn;
  537. init_completion(&dm_device.ol_waitevent);
  538. dm_device.ha_waiting = true;
  539. mutex_unlock(&dm_device.ha_region_mutex);
  540. nid = memory_add_physaddr_to_nid(PFN_PHYS(start_pfn));
  541. ret = add_memory(nid, PFN_PHYS((start_pfn)),
  542. (HA_CHUNK << PAGE_SHIFT));
  543. if (ret) {
  544. pr_info("hot_add memory failed error is %d\n", ret);
  545. if (ret == -EEXIST) {
  546. /*
  547. * This error indicates that the error
  548. * is not a transient failure. This is the
  549. * case where the guest's physical address map
  550. * precludes hot adding memory. Stop all further
  551. * memory hot-add.
  552. */
  553. do_hot_add = false;
  554. }
  555. has->ha_end_pfn -= HA_CHUNK;
  556. has->covered_end_pfn -= processed_pfn;
  557. mutex_lock(&dm_device.ha_region_mutex);
  558. break;
  559. }
  560. /*
  561. * Wait for the memory block to be onlined.
  562. * Since the hot add has succeeded, it is ok to
  563. * proceed even if the pages in the hot added region
  564. * have not been "onlined" within the allowed time.
  565. */
  566. wait_for_completion_timeout(&dm_device.ol_waitevent, 5*HZ);
  567. mutex_lock(&dm_device.ha_region_mutex);
  568. post_status(&dm_device);
  569. }
  570. return;
  571. }
  572. static void hv_online_page(struct page *pg)
  573. {
  574. struct list_head *cur;
  575. struct hv_hotadd_state *has;
  576. unsigned long cur_start_pgp;
  577. unsigned long cur_end_pgp;
  578. list_for_each(cur, &dm_device.ha_region_list) {
  579. has = list_entry(cur, struct hv_hotadd_state, list);
  580. cur_start_pgp = (unsigned long)pfn_to_page(has->start_pfn);
  581. cur_end_pgp = (unsigned long)pfn_to_page(has->covered_end_pfn);
  582. if (((unsigned long)pg >= cur_start_pgp) &&
  583. ((unsigned long)pg < cur_end_pgp)) {
  584. /*
  585. * This frame is currently backed; online the
  586. * page.
  587. */
  588. __online_page_set_limits(pg);
  589. __online_page_increment_counters(pg);
  590. __online_page_free(pg);
  591. }
  592. }
  593. }
  594. static bool pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
  595. {
  596. struct list_head *cur;
  597. struct hv_hotadd_state *has;
  598. unsigned long residual, new_inc;
  599. if (list_empty(&dm_device.ha_region_list))
  600. return false;
  601. list_for_each(cur, &dm_device.ha_region_list) {
  602. has = list_entry(cur, struct hv_hotadd_state, list);
  603. /*
  604. * If the pfn range we are dealing with is not in the current
  605. * "hot add block", move on.
  606. */
  607. if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
  608. continue;
  609. /*
  610. * If the current hot add-request extends beyond
  611. * our current limit; extend it.
  612. */
  613. if ((start_pfn + pfn_cnt) > has->end_pfn) {
  614. residual = (start_pfn + pfn_cnt - has->end_pfn);
  615. /*
  616. * Extend the region by multiples of HA_CHUNK.
  617. */
  618. new_inc = (residual / HA_CHUNK) * HA_CHUNK;
  619. if (residual % HA_CHUNK)
  620. new_inc += HA_CHUNK;
  621. has->end_pfn += new_inc;
  622. }
  623. /*
  624. * If the current start pfn is not where the covered_end
  625. * is, update it.
  626. */
  627. if (has->covered_end_pfn != start_pfn)
  628. has->covered_end_pfn = start_pfn;
  629. return true;
  630. }
  631. return false;
  632. }
  633. static unsigned long handle_pg_range(unsigned long pg_start,
  634. unsigned long pg_count)
  635. {
  636. unsigned long start_pfn = pg_start;
  637. unsigned long pfn_cnt = pg_count;
  638. unsigned long size;
  639. struct list_head *cur;
  640. struct hv_hotadd_state *has;
  641. unsigned long pgs_ol = 0;
  642. unsigned long old_covered_state;
  643. if (list_empty(&dm_device.ha_region_list))
  644. return 0;
  645. list_for_each(cur, &dm_device.ha_region_list) {
  646. has = list_entry(cur, struct hv_hotadd_state, list);
  647. /*
  648. * If the pfn range we are dealing with is not in the current
  649. * "hot add block", move on.
  650. */
  651. if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
  652. continue;
  653. old_covered_state = has->covered_end_pfn;
  654. if (start_pfn < has->ha_end_pfn) {
  655. /*
  656. * This is the case where we are backing pages
  657. * in an already hot added region. Bring
  658. * these pages online first.
  659. */
  660. pgs_ol = has->ha_end_pfn - start_pfn;
  661. if (pgs_ol > pfn_cnt)
  662. pgs_ol = pfn_cnt;
  663. /*
  664. * Check if the corresponding memory block is already
  665. * online by checking its last previously backed page.
  666. * In case it is we need to bring rest (which was not
  667. * backed previously) online too.
  668. */
  669. if (start_pfn > has->start_pfn &&
  670. !PageReserved(pfn_to_page(start_pfn - 1)))
  671. hv_bring_pgs_online(start_pfn, pgs_ol);
  672. has->covered_end_pfn += pgs_ol;
  673. pfn_cnt -= pgs_ol;
  674. }
  675. if ((has->ha_end_pfn < has->end_pfn) && (pfn_cnt > 0)) {
  676. /*
  677. * We have some residual hot add range
  678. * that needs to be hot added; hot add
  679. * it now. Hot add a multiple of
  680. * of HA_CHUNK that fully covers the pages
  681. * we have.
  682. */
  683. size = (has->end_pfn - has->ha_end_pfn);
  684. if (pfn_cnt <= size) {
  685. size = ((pfn_cnt / HA_CHUNK) * HA_CHUNK);
  686. if (pfn_cnt % HA_CHUNK)
  687. size += HA_CHUNK;
  688. } else {
  689. pfn_cnt = size;
  690. }
  691. hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
  692. }
  693. /*
  694. * If we managed to online any pages that were given to us,
  695. * we declare success.
  696. */
  697. return has->covered_end_pfn - old_covered_state;
  698. }
  699. return 0;
  700. }
  701. static unsigned long process_hot_add(unsigned long pg_start,
  702. unsigned long pfn_cnt,
  703. unsigned long rg_start,
  704. unsigned long rg_size)
  705. {
  706. struct hv_hotadd_state *ha_region = NULL;
  707. if (pfn_cnt == 0)
  708. return 0;
  709. if (!dm_device.host_specified_ha_region)
  710. if (pfn_covered(pg_start, pfn_cnt))
  711. goto do_pg_range;
  712. /*
  713. * If the host has specified a hot-add range; deal with it first.
  714. */
  715. if (rg_size != 0) {
  716. ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
  717. if (!ha_region)
  718. return 0;
  719. INIT_LIST_HEAD(&ha_region->list);
  720. list_add_tail(&ha_region->list, &dm_device.ha_region_list);
  721. ha_region->start_pfn = rg_start;
  722. ha_region->ha_end_pfn = rg_start;
  723. ha_region->covered_end_pfn = pg_start;
  724. ha_region->end_pfn = rg_start + rg_size;
  725. }
  726. do_pg_range:
  727. /*
  728. * Process the page range specified; bringing them
  729. * online if possible.
  730. */
  731. return handle_pg_range(pg_start, pfn_cnt);
  732. }
  733. #endif
  734. static void hot_add_req(struct work_struct *dummy)
  735. {
  736. struct dm_hot_add_response resp;
  737. #ifdef CONFIG_MEMORY_HOTPLUG
  738. unsigned long pg_start, pfn_cnt;
  739. unsigned long rg_start, rg_sz;
  740. #endif
  741. struct hv_dynmem_device *dm = &dm_device;
  742. memset(&resp, 0, sizeof(struct dm_hot_add_response));
  743. resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
  744. resp.hdr.size = sizeof(struct dm_hot_add_response);
  745. #ifdef CONFIG_MEMORY_HOTPLUG
  746. mutex_lock(&dm_device.ha_region_mutex);
  747. pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
  748. pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
  749. rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
  750. rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
  751. if ((rg_start == 0) && (!dm->host_specified_ha_region)) {
  752. unsigned long region_size;
  753. unsigned long region_start;
  754. /*
  755. * The host has not specified the hot-add region.
  756. * Based on the hot-add page range being specified,
  757. * compute a hot-add region that can cover the pages
  758. * that need to be hot-added while ensuring the alignment
  759. * and size requirements of Linux as it relates to hot-add.
  760. */
  761. region_start = pg_start;
  762. region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK;
  763. if (pfn_cnt % HA_CHUNK)
  764. region_size += HA_CHUNK;
  765. region_start = (pg_start / HA_CHUNK) * HA_CHUNK;
  766. rg_start = region_start;
  767. rg_sz = region_size;
  768. }
  769. if (do_hot_add)
  770. resp.page_count = process_hot_add(pg_start, pfn_cnt,
  771. rg_start, rg_sz);
  772. dm->num_pages_added += resp.page_count;
  773. mutex_unlock(&dm_device.ha_region_mutex);
  774. #endif
  775. /*
  776. * The result field of the response structure has the
  777. * following semantics:
  778. *
  779. * 1. If all or some pages hot-added: Guest should return success.
  780. *
  781. * 2. If no pages could be hot-added:
  782. *
  783. * If the guest returns success, then the host
  784. * will not attempt any further hot-add operations. This
  785. * signifies a permanent failure.
  786. *
  787. * If the guest returns failure, then this failure will be
  788. * treated as a transient failure and the host may retry the
  789. * hot-add operation after some delay.
  790. */
  791. if (resp.page_count > 0)
  792. resp.result = 1;
  793. else if (!do_hot_add)
  794. resp.result = 1;
  795. else
  796. resp.result = 0;
  797. if (!do_hot_add || (resp.page_count == 0))
  798. pr_info("Memory hot add failed\n");
  799. dm->state = DM_INITIALIZED;
  800. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  801. vmbus_sendpacket(dm->dev->channel, &resp,
  802. sizeof(struct dm_hot_add_response),
  803. (unsigned long)NULL,
  804. VM_PKT_DATA_INBAND, 0);
  805. }
  806. static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
  807. {
  808. struct dm_info_header *info_hdr;
  809. info_hdr = (struct dm_info_header *)msg->info;
  810. switch (info_hdr->type) {
  811. case INFO_TYPE_MAX_PAGE_CNT:
  812. pr_info("Received INFO_TYPE_MAX_PAGE_CNT\n");
  813. pr_info("Data Size is %d\n", info_hdr->data_size);
  814. break;
  815. default:
  816. pr_info("Received Unknown type: %d\n", info_hdr->type);
  817. }
  818. }
  819. static unsigned long compute_balloon_floor(void)
  820. {
  821. unsigned long min_pages;
  822. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  823. /* Simple continuous piecewiese linear function:
  824. * max MiB -> min MiB gradient
  825. * 0 0
  826. * 16 16
  827. * 32 24
  828. * 128 72 (1/2)
  829. * 512 168 (1/4)
  830. * 2048 360 (1/8)
  831. * 8192 744 (1/16)
  832. * 32768 1512 (1/32)
  833. */
  834. if (totalram_pages < MB2PAGES(128))
  835. min_pages = MB2PAGES(8) + (totalram_pages >> 1);
  836. else if (totalram_pages < MB2PAGES(512))
  837. min_pages = MB2PAGES(40) + (totalram_pages >> 2);
  838. else if (totalram_pages < MB2PAGES(2048))
  839. min_pages = MB2PAGES(104) + (totalram_pages >> 3);
  840. else if (totalram_pages < MB2PAGES(8192))
  841. min_pages = MB2PAGES(232) + (totalram_pages >> 4);
  842. else
  843. min_pages = MB2PAGES(488) + (totalram_pages >> 5);
  844. #undef MB2PAGES
  845. return min_pages;
  846. }
  847. /*
  848. * Post our status as it relates memory pressure to the
  849. * host. Host expects the guests to post this status
  850. * periodically at 1 second intervals.
  851. *
  852. * The metrics specified in this protocol are very Windows
  853. * specific and so we cook up numbers here to convey our memory
  854. * pressure.
  855. */
  856. static void post_status(struct hv_dynmem_device *dm)
  857. {
  858. struct dm_status status;
  859. struct sysinfo val;
  860. unsigned long now = jiffies;
  861. unsigned long last_post = last_post_time;
  862. if (pressure_report_delay > 0) {
  863. --pressure_report_delay;
  864. return;
  865. }
  866. if (!time_after(now, (last_post_time + HZ)))
  867. return;
  868. si_meminfo(&val);
  869. memset(&status, 0, sizeof(struct dm_status));
  870. status.hdr.type = DM_STATUS_REPORT;
  871. status.hdr.size = sizeof(struct dm_status);
  872. status.hdr.trans_id = atomic_inc_return(&trans_id);
  873. /*
  874. * The host expects the guest to report free and committed memory.
  875. * Furthermore, the host expects the pressure information to include
  876. * the ballooned out pages. For a given amount of memory that we are
  877. * managing we need to compute a floor below which we should not
  878. * balloon. Compute this and add it to the pressure report.
  879. * We also need to report all offline pages (num_pages_added -
  880. * num_pages_onlined) as committed to the host, otherwise it can try
  881. * asking us to balloon them out.
  882. */
  883. status.num_avail = val.freeram;
  884. status.num_committed = vm_memory_committed() +
  885. dm->num_pages_ballooned +
  886. (dm->num_pages_added > dm->num_pages_onlined ?
  887. dm->num_pages_added - dm->num_pages_onlined : 0) +
  888. compute_balloon_floor();
  889. /*
  890. * If our transaction ID is no longer current, just don't
  891. * send the status. This can happen if we were interrupted
  892. * after we picked our transaction ID.
  893. */
  894. if (status.hdr.trans_id != atomic_read(&trans_id))
  895. return;
  896. /*
  897. * If the last post time that we sampled has changed,
  898. * we have raced, don't post the status.
  899. */
  900. if (last_post != last_post_time)
  901. return;
  902. last_post_time = jiffies;
  903. vmbus_sendpacket(dm->dev->channel, &status,
  904. sizeof(struct dm_status),
  905. (unsigned long)NULL,
  906. VM_PKT_DATA_INBAND, 0);
  907. }
  908. static void free_balloon_pages(struct hv_dynmem_device *dm,
  909. union dm_mem_page_range *range_array)
  910. {
  911. int num_pages = range_array->finfo.page_cnt;
  912. __u64 start_frame = range_array->finfo.start_page;
  913. struct page *pg;
  914. int i;
  915. for (i = 0; i < num_pages; i++) {
  916. pg = pfn_to_page(i + start_frame);
  917. __free_page(pg);
  918. dm->num_pages_ballooned--;
  919. }
  920. }
  921. static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
  922. unsigned int num_pages,
  923. struct dm_balloon_response *bl_resp,
  924. int alloc_unit)
  925. {
  926. unsigned int i = 0;
  927. struct page *pg;
  928. if (num_pages < alloc_unit)
  929. return 0;
  930. for (i = 0; (i * alloc_unit) < num_pages; i++) {
  931. if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
  932. PAGE_SIZE)
  933. return i * alloc_unit;
  934. /*
  935. * We execute this code in a thread context. Furthermore,
  936. * we don't want the kernel to try too hard.
  937. */
  938. pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
  939. __GFP_NOMEMALLOC | __GFP_NOWARN,
  940. get_order(alloc_unit << PAGE_SHIFT));
  941. if (!pg)
  942. return i * alloc_unit;
  943. dm->num_pages_ballooned += alloc_unit;
  944. /*
  945. * If we allocatted 2M pages; split them so we
  946. * can free them in any order we get.
  947. */
  948. if (alloc_unit != 1)
  949. split_page(pg, get_order(alloc_unit << PAGE_SHIFT));
  950. bl_resp->range_count++;
  951. bl_resp->range_array[i].finfo.start_page =
  952. page_to_pfn(pg);
  953. bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
  954. bl_resp->hdr.size += sizeof(union dm_mem_page_range);
  955. }
  956. return num_pages;
  957. }
  958. static void balloon_up(struct work_struct *dummy)
  959. {
  960. unsigned int num_pages = dm_device.balloon_wrk.num_pages;
  961. unsigned int num_ballooned = 0;
  962. struct dm_balloon_response *bl_resp;
  963. int alloc_unit;
  964. int ret;
  965. bool done = false;
  966. int i;
  967. struct sysinfo val;
  968. unsigned long floor;
  969. /* The host balloons pages in 2M granularity. */
  970. WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
  971. /*
  972. * We will attempt 2M allocations. However, if we fail to
  973. * allocate 2M chunks, we will go back to 4k allocations.
  974. */
  975. alloc_unit = 512;
  976. si_meminfo(&val);
  977. floor = compute_balloon_floor();
  978. /* Refuse to balloon below the floor, keep the 2M granularity. */
  979. if (val.freeram < num_pages || val.freeram - num_pages < floor) {
  980. num_pages = val.freeram > floor ? (val.freeram - floor) : 0;
  981. num_pages -= num_pages % PAGES_IN_2M;
  982. }
  983. while (!done) {
  984. bl_resp = (struct dm_balloon_response *)send_buffer;
  985. memset(send_buffer, 0, PAGE_SIZE);
  986. bl_resp->hdr.type = DM_BALLOON_RESPONSE;
  987. bl_resp->hdr.size = sizeof(struct dm_balloon_response);
  988. bl_resp->more_pages = 1;
  989. num_pages -= num_ballooned;
  990. num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
  991. bl_resp, alloc_unit);
  992. if (alloc_unit != 1 && num_ballooned == 0) {
  993. alloc_unit = 1;
  994. continue;
  995. }
  996. if (num_ballooned == 0 || num_ballooned == num_pages) {
  997. bl_resp->more_pages = 0;
  998. done = true;
  999. dm_device.state = DM_INITIALIZED;
  1000. }
  1001. /*
  1002. * We are pushing a lot of data through the channel;
  1003. * deal with transient failures caused because of the
  1004. * lack of space in the ring buffer.
  1005. */
  1006. do {
  1007. bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
  1008. ret = vmbus_sendpacket(dm_device.dev->channel,
  1009. bl_resp,
  1010. bl_resp->hdr.size,
  1011. (unsigned long)NULL,
  1012. VM_PKT_DATA_INBAND, 0);
  1013. if (ret == -EAGAIN)
  1014. msleep(20);
  1015. post_status(&dm_device);
  1016. } while (ret == -EAGAIN);
  1017. if (ret) {
  1018. /*
  1019. * Free up the memory we allocatted.
  1020. */
  1021. pr_info("Balloon response failed\n");
  1022. for (i = 0; i < bl_resp->range_count; i++)
  1023. free_balloon_pages(&dm_device,
  1024. &bl_resp->range_array[i]);
  1025. done = true;
  1026. }
  1027. }
  1028. }
  1029. static void balloon_down(struct hv_dynmem_device *dm,
  1030. struct dm_unballoon_request *req)
  1031. {
  1032. union dm_mem_page_range *range_array = req->range_array;
  1033. int range_count = req->range_count;
  1034. struct dm_unballoon_response resp;
  1035. int i;
  1036. for (i = 0; i < range_count; i++) {
  1037. free_balloon_pages(dm, &range_array[i]);
  1038. complete(&dm_device.config_event);
  1039. }
  1040. if (req->more_pages == 1)
  1041. return;
  1042. memset(&resp, 0, sizeof(struct dm_unballoon_response));
  1043. resp.hdr.type = DM_UNBALLOON_RESPONSE;
  1044. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  1045. resp.hdr.size = sizeof(struct dm_unballoon_response);
  1046. vmbus_sendpacket(dm_device.dev->channel, &resp,
  1047. sizeof(struct dm_unballoon_response),
  1048. (unsigned long)NULL,
  1049. VM_PKT_DATA_INBAND, 0);
  1050. dm->state = DM_INITIALIZED;
  1051. }
  1052. static void balloon_onchannelcallback(void *context);
  1053. static int dm_thread_func(void *dm_dev)
  1054. {
  1055. struct hv_dynmem_device *dm = dm_dev;
  1056. while (!kthread_should_stop()) {
  1057. wait_for_completion_interruptible_timeout(
  1058. &dm_device.config_event, 1*HZ);
  1059. /*
  1060. * The host expects us to post information on the memory
  1061. * pressure every second.
  1062. */
  1063. reinit_completion(&dm_device.config_event);
  1064. post_status(dm);
  1065. }
  1066. return 0;
  1067. }
  1068. static void version_resp(struct hv_dynmem_device *dm,
  1069. struct dm_version_response *vresp)
  1070. {
  1071. struct dm_version_request version_req;
  1072. int ret;
  1073. if (vresp->is_accepted) {
  1074. /*
  1075. * We are done; wakeup the
  1076. * context waiting for version
  1077. * negotiation.
  1078. */
  1079. complete(&dm->host_event);
  1080. return;
  1081. }
  1082. /*
  1083. * If there are more versions to try, continue
  1084. * with negotiations; if not
  1085. * shutdown the service since we are not able
  1086. * to negotiate a suitable version number
  1087. * with the host.
  1088. */
  1089. if (dm->next_version == 0)
  1090. goto version_error;
  1091. memset(&version_req, 0, sizeof(struct dm_version_request));
  1092. version_req.hdr.type = DM_VERSION_REQUEST;
  1093. version_req.hdr.size = sizeof(struct dm_version_request);
  1094. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1095. version_req.version.version = dm->next_version;
  1096. /*
  1097. * Set the next version to try in case current version fails.
  1098. * Win7 protocol ought to be the last one to try.
  1099. */
  1100. switch (version_req.version.version) {
  1101. case DYNMEM_PROTOCOL_VERSION_WIN8:
  1102. dm->next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
  1103. version_req.is_last_attempt = 0;
  1104. break;
  1105. default:
  1106. dm->next_version = 0;
  1107. version_req.is_last_attempt = 1;
  1108. }
  1109. ret = vmbus_sendpacket(dm->dev->channel, &version_req,
  1110. sizeof(struct dm_version_request),
  1111. (unsigned long)NULL,
  1112. VM_PKT_DATA_INBAND, 0);
  1113. if (ret)
  1114. goto version_error;
  1115. return;
  1116. version_error:
  1117. dm->state = DM_INIT_ERROR;
  1118. complete(&dm->host_event);
  1119. }
  1120. static void cap_resp(struct hv_dynmem_device *dm,
  1121. struct dm_capabilities_resp_msg *cap_resp)
  1122. {
  1123. if (!cap_resp->is_accepted) {
  1124. pr_info("Capabilities not accepted by host\n");
  1125. dm->state = DM_INIT_ERROR;
  1126. }
  1127. complete(&dm->host_event);
  1128. }
  1129. static void balloon_onchannelcallback(void *context)
  1130. {
  1131. struct hv_device *dev = context;
  1132. u32 recvlen;
  1133. u64 requestid;
  1134. struct dm_message *dm_msg;
  1135. struct dm_header *dm_hdr;
  1136. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1137. struct dm_balloon *bal_msg;
  1138. struct dm_hot_add *ha_msg;
  1139. union dm_mem_page_range *ha_pg_range;
  1140. union dm_mem_page_range *ha_region;
  1141. memset(recv_buffer, 0, sizeof(recv_buffer));
  1142. vmbus_recvpacket(dev->channel, recv_buffer,
  1143. PAGE_SIZE, &recvlen, &requestid);
  1144. if (recvlen > 0) {
  1145. dm_msg = (struct dm_message *)recv_buffer;
  1146. dm_hdr = &dm_msg->hdr;
  1147. switch (dm_hdr->type) {
  1148. case DM_VERSION_RESPONSE:
  1149. version_resp(dm,
  1150. (struct dm_version_response *)dm_msg);
  1151. break;
  1152. case DM_CAPABILITIES_RESPONSE:
  1153. cap_resp(dm,
  1154. (struct dm_capabilities_resp_msg *)dm_msg);
  1155. break;
  1156. case DM_BALLOON_REQUEST:
  1157. if (dm->state == DM_BALLOON_UP)
  1158. pr_warn("Currently ballooning\n");
  1159. bal_msg = (struct dm_balloon *)recv_buffer;
  1160. dm->state = DM_BALLOON_UP;
  1161. dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
  1162. schedule_work(&dm_device.balloon_wrk.wrk);
  1163. break;
  1164. case DM_UNBALLOON_REQUEST:
  1165. dm->state = DM_BALLOON_DOWN;
  1166. balloon_down(dm,
  1167. (struct dm_unballoon_request *)recv_buffer);
  1168. break;
  1169. case DM_MEM_HOT_ADD_REQUEST:
  1170. if (dm->state == DM_HOT_ADD)
  1171. pr_warn("Currently hot-adding\n");
  1172. dm->state = DM_HOT_ADD;
  1173. ha_msg = (struct dm_hot_add *)recv_buffer;
  1174. if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
  1175. /*
  1176. * This is a normal hot-add request specifying
  1177. * hot-add memory.
  1178. */
  1179. dm->host_specified_ha_region = false;
  1180. ha_pg_range = &ha_msg->range;
  1181. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1182. dm->ha_wrk.ha_region_range.page_range = 0;
  1183. } else {
  1184. /*
  1185. * Host is specifying that we first hot-add
  1186. * a region and then partially populate this
  1187. * region.
  1188. */
  1189. dm->host_specified_ha_region = true;
  1190. ha_pg_range = &ha_msg->range;
  1191. ha_region = &ha_pg_range[1];
  1192. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1193. dm->ha_wrk.ha_region_range = *ha_region;
  1194. }
  1195. schedule_work(&dm_device.ha_wrk.wrk);
  1196. break;
  1197. case DM_INFO_MESSAGE:
  1198. process_info(dm, (struct dm_info_msg *)dm_msg);
  1199. break;
  1200. default:
  1201. pr_err("Unhandled message: type: %d\n", dm_hdr->type);
  1202. }
  1203. }
  1204. }
  1205. static int balloon_probe(struct hv_device *dev,
  1206. const struct hv_vmbus_device_id *dev_id)
  1207. {
  1208. int ret;
  1209. unsigned long t;
  1210. struct dm_version_request version_req;
  1211. struct dm_capabilities cap_msg;
  1212. do_hot_add = hot_add;
  1213. /*
  1214. * First allocate a send buffer.
  1215. */
  1216. send_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1217. if (!send_buffer)
  1218. return -ENOMEM;
  1219. ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
  1220. balloon_onchannelcallback, dev);
  1221. if (ret)
  1222. goto probe_error0;
  1223. dm_device.dev = dev;
  1224. dm_device.state = DM_INITIALIZING;
  1225. dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN8;
  1226. init_completion(&dm_device.host_event);
  1227. init_completion(&dm_device.config_event);
  1228. INIT_LIST_HEAD(&dm_device.ha_region_list);
  1229. mutex_init(&dm_device.ha_region_mutex);
  1230. INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
  1231. INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
  1232. dm_device.host_specified_ha_region = false;
  1233. dm_device.thread =
  1234. kthread_run(dm_thread_func, &dm_device, "hv_balloon");
  1235. if (IS_ERR(dm_device.thread)) {
  1236. ret = PTR_ERR(dm_device.thread);
  1237. goto probe_error1;
  1238. }
  1239. #ifdef CONFIG_MEMORY_HOTPLUG
  1240. set_online_page_callback(&hv_online_page);
  1241. register_memory_notifier(&hv_memory_nb);
  1242. #endif
  1243. hv_set_drvdata(dev, &dm_device);
  1244. /*
  1245. * Initiate the hand shake with the host and negotiate
  1246. * a version that the host can support. We start with the
  1247. * highest version number and go down if the host cannot
  1248. * support it.
  1249. */
  1250. memset(&version_req, 0, sizeof(struct dm_version_request));
  1251. version_req.hdr.type = DM_VERSION_REQUEST;
  1252. version_req.hdr.size = sizeof(struct dm_version_request);
  1253. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1254. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN10;
  1255. version_req.is_last_attempt = 0;
  1256. ret = vmbus_sendpacket(dev->channel, &version_req,
  1257. sizeof(struct dm_version_request),
  1258. (unsigned long)NULL,
  1259. VM_PKT_DATA_INBAND, 0);
  1260. if (ret)
  1261. goto probe_error2;
  1262. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  1263. if (t == 0) {
  1264. ret = -ETIMEDOUT;
  1265. goto probe_error2;
  1266. }
  1267. /*
  1268. * If we could not negotiate a compatible version with the host
  1269. * fail the probe function.
  1270. */
  1271. if (dm_device.state == DM_INIT_ERROR) {
  1272. ret = -ETIMEDOUT;
  1273. goto probe_error2;
  1274. }
  1275. /*
  1276. * Now submit our capabilities to the host.
  1277. */
  1278. memset(&cap_msg, 0, sizeof(struct dm_capabilities));
  1279. cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
  1280. cap_msg.hdr.size = sizeof(struct dm_capabilities);
  1281. cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
  1282. cap_msg.caps.cap_bits.balloon = 1;
  1283. cap_msg.caps.cap_bits.hot_add = 1;
  1284. /*
  1285. * Specify our alignment requirements as it relates
  1286. * memory hot-add. Specify 128MB alignment.
  1287. */
  1288. cap_msg.caps.cap_bits.hot_add_alignment = 7;
  1289. /*
  1290. * Currently the host does not use these
  1291. * values and we set them to what is done in the
  1292. * Windows driver.
  1293. */
  1294. cap_msg.min_page_cnt = 0;
  1295. cap_msg.max_page_number = -1;
  1296. ret = vmbus_sendpacket(dev->channel, &cap_msg,
  1297. sizeof(struct dm_capabilities),
  1298. (unsigned long)NULL,
  1299. VM_PKT_DATA_INBAND, 0);
  1300. if (ret)
  1301. goto probe_error2;
  1302. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  1303. if (t == 0) {
  1304. ret = -ETIMEDOUT;
  1305. goto probe_error2;
  1306. }
  1307. /*
  1308. * If the host does not like our capabilities,
  1309. * fail the probe function.
  1310. */
  1311. if (dm_device.state == DM_INIT_ERROR) {
  1312. ret = -ETIMEDOUT;
  1313. goto probe_error2;
  1314. }
  1315. dm_device.state = DM_INITIALIZED;
  1316. return 0;
  1317. probe_error2:
  1318. #ifdef CONFIG_MEMORY_HOTPLUG
  1319. restore_online_page_callback(&hv_online_page);
  1320. #endif
  1321. kthread_stop(dm_device.thread);
  1322. probe_error1:
  1323. vmbus_close(dev->channel);
  1324. probe_error0:
  1325. kfree(send_buffer);
  1326. return ret;
  1327. }
  1328. static int balloon_remove(struct hv_device *dev)
  1329. {
  1330. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1331. struct list_head *cur, *tmp;
  1332. struct hv_hotadd_state *has;
  1333. if (dm->num_pages_ballooned != 0)
  1334. pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
  1335. cancel_work_sync(&dm->balloon_wrk.wrk);
  1336. cancel_work_sync(&dm->ha_wrk.wrk);
  1337. vmbus_close(dev->channel);
  1338. kthread_stop(dm->thread);
  1339. kfree(send_buffer);
  1340. #ifdef CONFIG_MEMORY_HOTPLUG
  1341. restore_online_page_callback(&hv_online_page);
  1342. unregister_memory_notifier(&hv_memory_nb);
  1343. #endif
  1344. list_for_each_safe(cur, tmp, &dm->ha_region_list) {
  1345. has = list_entry(cur, struct hv_hotadd_state, list);
  1346. list_del(&has->list);
  1347. kfree(has);
  1348. }
  1349. return 0;
  1350. }
  1351. static const struct hv_vmbus_device_id id_table[] = {
  1352. /* Dynamic Memory Class ID */
  1353. /* 525074DC-8985-46e2-8057-A307DC18A502 */
  1354. { HV_DM_GUID, },
  1355. { },
  1356. };
  1357. MODULE_DEVICE_TABLE(vmbus, id_table);
  1358. static struct hv_driver balloon_drv = {
  1359. .name = "hv_balloon",
  1360. .id_table = id_table,
  1361. .probe = balloon_probe,
  1362. .remove = balloon_remove,
  1363. };
  1364. static int __init init_balloon_drv(void)
  1365. {
  1366. return vmbus_driver_register(&balloon_drv);
  1367. }
  1368. module_init(init_balloon_drv);
  1369. MODULE_DESCRIPTION("Hyper-V Balloon");
  1370. MODULE_LICENSE("GPL");