hv_balloon.c 23 KB

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