hv_balloon.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. struct balloon_state {
  353. __u32 num_pages;
  354. struct work_struct wrk;
  355. };
  356. static bool hot_add;
  357. static bool do_hot_add;
  358. /*
  359. * Delay reporting memory pressure by
  360. * the specified number of seconds.
  361. */
  362. static uint pressure_report_delay = 30;
  363. module_param(hot_add, bool, (S_IRUGO | S_IWUSR));
  364. MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
  365. module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR));
  366. MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
  367. static atomic_t trans_id = ATOMIC_INIT(0);
  368. static int dm_ring_size = (5 * PAGE_SIZE);
  369. /*
  370. * Driver specific state.
  371. */
  372. enum hv_dm_state {
  373. DM_INITIALIZING = 0,
  374. DM_INITIALIZED,
  375. DM_BALLOON_UP,
  376. DM_BALLOON_DOWN,
  377. DM_HOT_ADD,
  378. DM_INIT_ERROR
  379. };
  380. static __u8 recv_buffer[PAGE_SIZE];
  381. static __u8 *send_buffer;
  382. #define PAGES_IN_2M 512
  383. struct hv_dynmem_device {
  384. struct hv_device *dev;
  385. enum hv_dm_state state;
  386. struct completion host_event;
  387. struct completion config_event;
  388. /*
  389. * Number of pages we have currently ballooned out.
  390. */
  391. unsigned int num_pages_ballooned;
  392. /*
  393. * State to manage the ballooning (up) operation.
  394. */
  395. struct balloon_state balloon_wrk;
  396. /*
  397. * This thread handles hot-add
  398. * requests from the host as well as notifying
  399. * the host with regards to memory pressure in
  400. * the guest.
  401. */
  402. struct task_struct *thread;
  403. /*
  404. * We start with the highest version we can support
  405. * and downgrade based on the host; we save here the
  406. * next version to try.
  407. */
  408. __u32 next_version;
  409. };
  410. static struct hv_dynmem_device dm_device;
  411. static void hot_add_req(struct hv_dynmem_device *dm, struct dm_hot_add *msg)
  412. {
  413. struct dm_hot_add_response resp;
  414. if (do_hot_add) {
  415. pr_info("Memory hot add not supported\n");
  416. /*
  417. * Currently we do not support hot add.
  418. * Just fail the request.
  419. */
  420. }
  421. memset(&resp, 0, sizeof(struct dm_hot_add_response));
  422. resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
  423. resp.hdr.size = sizeof(struct dm_hot_add_response);
  424. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  425. resp.page_count = 0;
  426. resp.result = 0;
  427. dm->state = DM_INITIALIZED;
  428. vmbus_sendpacket(dm->dev->channel, &resp,
  429. sizeof(struct dm_hot_add_response),
  430. (unsigned long)NULL,
  431. VM_PKT_DATA_INBAND, 0);
  432. }
  433. static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
  434. {
  435. struct dm_info_header *info_hdr;
  436. info_hdr = (struct dm_info_header *)msg->info;
  437. switch (info_hdr->type) {
  438. case INFO_TYPE_MAX_PAGE_CNT:
  439. pr_info("Received INFO_TYPE_MAX_PAGE_CNT\n");
  440. pr_info("Data Size is %d\n", info_hdr->data_size);
  441. break;
  442. default:
  443. pr_info("Received Unknown type: %d\n", info_hdr->type);
  444. }
  445. }
  446. unsigned long compute_balloon_floor(void)
  447. {
  448. unsigned long min_pages;
  449. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  450. /* Simple continuous piecewiese linear function:
  451. * max MiB -> min MiB gradient
  452. * 0 0
  453. * 16 16
  454. * 32 24
  455. * 128 72 (1/2)
  456. * 512 168 (1/4)
  457. * 2048 360 (1/8)
  458. * 8192 552 (1/32)
  459. * 32768 1320
  460. * 131072 4392
  461. */
  462. if (totalram_pages < MB2PAGES(128))
  463. min_pages = MB2PAGES(8) + (totalram_pages >> 1);
  464. else if (totalram_pages < MB2PAGES(512))
  465. min_pages = MB2PAGES(40) + (totalram_pages >> 2);
  466. else if (totalram_pages < MB2PAGES(2048))
  467. min_pages = MB2PAGES(104) + (totalram_pages >> 3);
  468. else
  469. min_pages = MB2PAGES(296) + (totalram_pages >> 5);
  470. #undef MB2PAGES
  471. return min_pages;
  472. }
  473. /*
  474. * Post our status as it relates memory pressure to the
  475. * host. Host expects the guests to post this status
  476. * periodically at 1 second intervals.
  477. *
  478. * The metrics specified in this protocol are very Windows
  479. * specific and so we cook up numbers here to convey our memory
  480. * pressure.
  481. */
  482. static void post_status(struct hv_dynmem_device *dm)
  483. {
  484. struct dm_status status;
  485. struct sysinfo val;
  486. if (pressure_report_delay > 0) {
  487. --pressure_report_delay;
  488. return;
  489. }
  490. si_meminfo(&val);
  491. memset(&status, 0, sizeof(struct dm_status));
  492. status.hdr.type = DM_STATUS_REPORT;
  493. status.hdr.size = sizeof(struct dm_status);
  494. status.hdr.trans_id = atomic_inc_return(&trans_id);
  495. /*
  496. * The host expects the guest to report free memory.
  497. * Further, the host expects the pressure information to
  498. * include the ballooned out pages.
  499. * For a given amount of memory that we are managing, we
  500. * need to compute a floor below which we should not balloon.
  501. * Compute this and add it to the pressure report.
  502. */
  503. status.num_avail = val.freeram;
  504. status.num_committed = vm_memory_committed() +
  505. dm->num_pages_ballooned +
  506. compute_balloon_floor();
  507. vmbus_sendpacket(dm->dev->channel, &status,
  508. sizeof(struct dm_status),
  509. (unsigned long)NULL,
  510. VM_PKT_DATA_INBAND, 0);
  511. }
  512. static void free_balloon_pages(struct hv_dynmem_device *dm,
  513. union dm_mem_page_range *range_array)
  514. {
  515. int num_pages = range_array->finfo.page_cnt;
  516. __u64 start_frame = range_array->finfo.start_page;
  517. struct page *pg;
  518. int i;
  519. for (i = 0; i < num_pages; i++) {
  520. pg = pfn_to_page(i + start_frame);
  521. __free_page(pg);
  522. dm->num_pages_ballooned--;
  523. }
  524. }
  525. static int alloc_balloon_pages(struct hv_dynmem_device *dm, int num_pages,
  526. struct dm_balloon_response *bl_resp, int alloc_unit,
  527. bool *alloc_error)
  528. {
  529. int i = 0;
  530. struct page *pg;
  531. if (num_pages < alloc_unit)
  532. return 0;
  533. for (i = 0; (i * alloc_unit) < num_pages; i++) {
  534. if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
  535. PAGE_SIZE)
  536. return i * alloc_unit;
  537. /*
  538. * We execute this code in a thread context. Furthermore,
  539. * we don't want the kernel to try too hard.
  540. */
  541. pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
  542. __GFP_NOMEMALLOC | __GFP_NOWARN,
  543. get_order(alloc_unit << PAGE_SHIFT));
  544. if (!pg) {
  545. *alloc_error = true;
  546. return i * alloc_unit;
  547. }
  548. dm->num_pages_ballooned += alloc_unit;
  549. bl_resp->range_count++;
  550. bl_resp->range_array[i].finfo.start_page =
  551. page_to_pfn(pg);
  552. bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
  553. bl_resp->hdr.size += sizeof(union dm_mem_page_range);
  554. }
  555. return num_pages;
  556. }
  557. static void balloon_up(struct work_struct *dummy)
  558. {
  559. int num_pages = dm_device.balloon_wrk.num_pages;
  560. int num_ballooned = 0;
  561. struct dm_balloon_response *bl_resp;
  562. int alloc_unit;
  563. int ret;
  564. bool alloc_error = false;
  565. bool done = false;
  566. int i;
  567. /*
  568. * Currently, we only support 4k allocations.
  569. */
  570. alloc_unit = 1;
  571. while (!done) {
  572. bl_resp = (struct dm_balloon_response *)send_buffer;
  573. memset(send_buffer, 0, PAGE_SIZE);
  574. bl_resp->hdr.type = DM_BALLOON_RESPONSE;
  575. bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
  576. bl_resp->hdr.size = sizeof(struct dm_balloon_response);
  577. bl_resp->more_pages = 1;
  578. num_pages -= num_ballooned;
  579. num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
  580. bl_resp, alloc_unit,
  581. &alloc_error);
  582. if ((alloc_error) || (num_ballooned == num_pages)) {
  583. bl_resp->more_pages = 0;
  584. done = true;
  585. dm_device.state = DM_INITIALIZED;
  586. }
  587. /*
  588. * We are pushing a lot of data through the channel;
  589. * deal with transient failures caused because of the
  590. * lack of space in the ring buffer.
  591. */
  592. do {
  593. ret = vmbus_sendpacket(dm_device.dev->channel,
  594. bl_resp,
  595. bl_resp->hdr.size,
  596. (unsigned long)NULL,
  597. VM_PKT_DATA_INBAND, 0);
  598. if (ret == -EAGAIN)
  599. msleep(20);
  600. } while (ret == -EAGAIN);
  601. if (ret) {
  602. /*
  603. * Free up the memory we allocatted.
  604. */
  605. pr_info("Balloon response failed\n");
  606. for (i = 0; i < bl_resp->range_count; i++)
  607. free_balloon_pages(&dm_device,
  608. &bl_resp->range_array[i]);
  609. done = true;
  610. }
  611. }
  612. }
  613. static void balloon_down(struct hv_dynmem_device *dm,
  614. struct dm_unballoon_request *req)
  615. {
  616. union dm_mem_page_range *range_array = req->range_array;
  617. int range_count = req->range_count;
  618. struct dm_unballoon_response resp;
  619. int i;
  620. for (i = 0; i < range_count; i++)
  621. free_balloon_pages(dm, &range_array[i]);
  622. if (req->more_pages == 1)
  623. return;
  624. memset(&resp, 0, sizeof(struct dm_unballoon_response));
  625. resp.hdr.type = DM_UNBALLOON_RESPONSE;
  626. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  627. resp.hdr.size = sizeof(struct dm_unballoon_response);
  628. vmbus_sendpacket(dm_device.dev->channel, &resp,
  629. sizeof(struct dm_unballoon_response),
  630. (unsigned long)NULL,
  631. VM_PKT_DATA_INBAND, 0);
  632. dm->state = DM_INITIALIZED;
  633. }
  634. static void balloon_onchannelcallback(void *context);
  635. static int dm_thread_func(void *dm_dev)
  636. {
  637. struct hv_dynmem_device *dm = dm_dev;
  638. int t;
  639. unsigned long scan_start;
  640. while (!kthread_should_stop()) {
  641. t = wait_for_completion_timeout(&dm_device.config_event, 1*HZ);
  642. /*
  643. * The host expects us to post information on the memory
  644. * pressure every second.
  645. */
  646. if (t == 0)
  647. post_status(dm);
  648. scan_start = jiffies;
  649. switch (dm->state) {
  650. case DM_HOT_ADD:
  651. hot_add_req(dm, (struct dm_hot_add *)recv_buffer);
  652. break;
  653. default:
  654. break;
  655. }
  656. if (!time_in_range(jiffies, scan_start, scan_start + HZ))
  657. post_status(dm);
  658. }
  659. return 0;
  660. }
  661. static void version_resp(struct hv_dynmem_device *dm,
  662. struct dm_version_response *vresp)
  663. {
  664. struct dm_version_request version_req;
  665. int ret;
  666. if (vresp->is_accepted) {
  667. /*
  668. * We are done; wakeup the
  669. * context waiting for version
  670. * negotiation.
  671. */
  672. complete(&dm->host_event);
  673. return;
  674. }
  675. /*
  676. * If there are more versions to try, continue
  677. * with negotiations; if not
  678. * shutdown the service since we are not able
  679. * to negotiate a suitable version number
  680. * with the host.
  681. */
  682. if (dm->next_version == 0)
  683. goto version_error;
  684. dm->next_version = 0;
  685. memset(&version_req, 0, sizeof(struct dm_version_request));
  686. version_req.hdr.type = DM_VERSION_REQUEST;
  687. version_req.hdr.size = sizeof(struct dm_version_request);
  688. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  689. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN7;
  690. version_req.is_last_attempt = 1;
  691. ret = vmbus_sendpacket(dm->dev->channel, &version_req,
  692. sizeof(struct dm_version_request),
  693. (unsigned long)NULL,
  694. VM_PKT_DATA_INBAND, 0);
  695. if (ret)
  696. goto version_error;
  697. return;
  698. version_error:
  699. dm->state = DM_INIT_ERROR;
  700. complete(&dm->host_event);
  701. }
  702. static void cap_resp(struct hv_dynmem_device *dm,
  703. struct dm_capabilities_resp_msg *cap_resp)
  704. {
  705. if (!cap_resp->is_accepted) {
  706. pr_info("Capabilities not accepted by host\n");
  707. dm->state = DM_INIT_ERROR;
  708. }
  709. complete(&dm->host_event);
  710. }
  711. static void balloon_onchannelcallback(void *context)
  712. {
  713. struct hv_device *dev = context;
  714. u32 recvlen;
  715. u64 requestid;
  716. struct dm_message *dm_msg;
  717. struct dm_header *dm_hdr;
  718. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  719. struct dm_balloon *bal_msg;
  720. memset(recv_buffer, 0, sizeof(recv_buffer));
  721. vmbus_recvpacket(dev->channel, recv_buffer,
  722. PAGE_SIZE, &recvlen, &requestid);
  723. if (recvlen > 0) {
  724. dm_msg = (struct dm_message *)recv_buffer;
  725. dm_hdr = &dm_msg->hdr;
  726. switch (dm_hdr->type) {
  727. case DM_VERSION_RESPONSE:
  728. version_resp(dm,
  729. (struct dm_version_response *)dm_msg);
  730. break;
  731. case DM_CAPABILITIES_RESPONSE:
  732. cap_resp(dm,
  733. (struct dm_capabilities_resp_msg *)dm_msg);
  734. break;
  735. case DM_BALLOON_REQUEST:
  736. if (dm->state == DM_BALLOON_UP)
  737. pr_warn("Currently ballooning\n");
  738. bal_msg = (struct dm_balloon *)recv_buffer;
  739. dm->state = DM_BALLOON_UP;
  740. dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
  741. schedule_work(&dm_device.balloon_wrk.wrk);
  742. break;
  743. case DM_UNBALLOON_REQUEST:
  744. dm->state = DM_BALLOON_DOWN;
  745. balloon_down(dm,
  746. (struct dm_unballoon_request *)recv_buffer);
  747. break;
  748. case DM_MEM_HOT_ADD_REQUEST:
  749. dm->state = DM_HOT_ADD;
  750. complete(&dm->config_event);
  751. break;
  752. case DM_INFO_MESSAGE:
  753. process_info(dm, (struct dm_info_msg *)dm_msg);
  754. break;
  755. default:
  756. pr_err("Unhandled message: type: %d\n", dm_hdr->type);
  757. }
  758. }
  759. }
  760. static int balloon_probe(struct hv_device *dev,
  761. const struct hv_vmbus_device_id *dev_id)
  762. {
  763. int ret, t;
  764. struct dm_version_request version_req;
  765. struct dm_capabilities cap_msg;
  766. do_hot_add = hot_add;
  767. /*
  768. * First allocate a send buffer.
  769. */
  770. send_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  771. if (!send_buffer)
  772. return -ENOMEM;
  773. ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
  774. balloon_onchannelcallback, dev);
  775. if (ret)
  776. goto probe_error0;
  777. dm_device.dev = dev;
  778. dm_device.state = DM_INITIALIZING;
  779. dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
  780. init_completion(&dm_device.host_event);
  781. init_completion(&dm_device.config_event);
  782. INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
  783. dm_device.thread =
  784. kthread_run(dm_thread_func, &dm_device, "hv_balloon");
  785. if (IS_ERR(dm_device.thread)) {
  786. ret = PTR_ERR(dm_device.thread);
  787. goto probe_error1;
  788. }
  789. hv_set_drvdata(dev, &dm_device);
  790. /*
  791. * Initiate the hand shake with the host and negotiate
  792. * a version that the host can support. We start with the
  793. * highest version number and go down if the host cannot
  794. * support it.
  795. */
  796. memset(&version_req, 0, sizeof(struct dm_version_request));
  797. version_req.hdr.type = DM_VERSION_REQUEST;
  798. version_req.hdr.size = sizeof(struct dm_version_request);
  799. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  800. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN8;
  801. version_req.is_last_attempt = 0;
  802. ret = vmbus_sendpacket(dev->channel, &version_req,
  803. sizeof(struct dm_version_request),
  804. (unsigned long)NULL,
  805. VM_PKT_DATA_INBAND, 0);
  806. if (ret)
  807. goto probe_error2;
  808. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  809. if (t == 0) {
  810. ret = -ETIMEDOUT;
  811. goto probe_error2;
  812. }
  813. /*
  814. * If we could not negotiate a compatible version with the host
  815. * fail the probe function.
  816. */
  817. if (dm_device.state == DM_INIT_ERROR) {
  818. ret = -ETIMEDOUT;
  819. goto probe_error2;
  820. }
  821. /*
  822. * Now submit our capabilities to the host.
  823. */
  824. memset(&cap_msg, 0, sizeof(struct dm_capabilities));
  825. cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
  826. cap_msg.hdr.size = sizeof(struct dm_capabilities);
  827. cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
  828. cap_msg.caps.cap_bits.balloon = 1;
  829. /*
  830. * While we currently don't support hot-add,
  831. * we still advertise this capability since the
  832. * host requires that guests partcipating in the
  833. * dynamic memory protocol support hot add.
  834. */
  835. cap_msg.caps.cap_bits.hot_add = 1;
  836. /*
  837. * Currently the host does not use these
  838. * values and we set them to what is done in the
  839. * Windows driver.
  840. */
  841. cap_msg.min_page_cnt = 0;
  842. cap_msg.max_page_number = -1;
  843. ret = vmbus_sendpacket(dev->channel, &cap_msg,
  844. sizeof(struct dm_capabilities),
  845. (unsigned long)NULL,
  846. VM_PKT_DATA_INBAND, 0);
  847. if (ret)
  848. goto probe_error2;
  849. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  850. if (t == 0) {
  851. ret = -ETIMEDOUT;
  852. goto probe_error2;
  853. }
  854. /*
  855. * If the host does not like our capabilities,
  856. * fail the probe function.
  857. */
  858. if (dm_device.state == DM_INIT_ERROR) {
  859. ret = -ETIMEDOUT;
  860. goto probe_error2;
  861. }
  862. dm_device.state = DM_INITIALIZED;
  863. return 0;
  864. probe_error2:
  865. kthread_stop(dm_device.thread);
  866. probe_error1:
  867. vmbus_close(dev->channel);
  868. probe_error0:
  869. kfree(send_buffer);
  870. return ret;
  871. }
  872. static int balloon_remove(struct hv_device *dev)
  873. {
  874. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  875. if (dm->num_pages_ballooned != 0)
  876. pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
  877. cancel_work_sync(&dm->balloon_wrk.wrk);
  878. vmbus_close(dev->channel);
  879. kthread_stop(dm->thread);
  880. kfree(send_buffer);
  881. return 0;
  882. }
  883. static const struct hv_vmbus_device_id id_table[] = {
  884. /* Dynamic Memory Class ID */
  885. /* 525074DC-8985-46e2-8057-A307DC18A502 */
  886. { HV_DM_GUID, },
  887. { },
  888. };
  889. MODULE_DEVICE_TABLE(vmbus, id_table);
  890. static struct hv_driver balloon_drv = {
  891. .name = "hv_balloon",
  892. .id_table = id_table,
  893. .probe = balloon_probe,
  894. .remove = balloon_remove,
  895. };
  896. static int __init init_balloon_drv(void)
  897. {
  898. return vmbus_driver_register(&balloon_drv);
  899. }
  900. static void exit_balloon_drv(void)
  901. {
  902. vmbus_driver_unregister(&balloon_drv);
  903. }
  904. module_init(init_balloon_drv);
  905. module_exit(exit_balloon_drv);
  906. MODULE_DESCRIPTION("Hyper-V Balloon");
  907. MODULE_VERSION(HV_DRV_VERSION);
  908. MODULE_LICENSE("GPL");