hv_balloon.c 25 KB

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