hv_balloon.c 25 KB

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