hv_balloon.c 23 KB

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