hv_balloon.c 23 KB

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