hv_balloon.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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. /*
  101. * To support guests that may have alignment
  102. * limitations on hot-add, the guest can specify
  103. * its alignment requirements; a value of n
  104. * represents an alignment of 2^n in mega bytes.
  105. */
  106. __u64 hot_add_alignment:4;
  107. __u64 reservedz:58;
  108. } cap_bits;
  109. __u64 caps;
  110. } __packed;
  111. union dm_mem_page_range {
  112. struct {
  113. /*
  114. * The PFN number of the first page in the range.
  115. * 40 bits is the architectural limit of a PFN
  116. * number for AMD64.
  117. */
  118. __u64 start_page:40;
  119. /*
  120. * The number of pages in the range.
  121. */
  122. __u64 page_cnt:24;
  123. } finfo;
  124. __u64 page_range;
  125. } __packed;
  126. /*
  127. * The header for all dynamic memory messages:
  128. *
  129. * type: Type of the message.
  130. * size: Size of the message in bytes; including the header.
  131. * trans_id: The guest is responsible for manufacturing this ID.
  132. */
  133. struct dm_header {
  134. __u16 type;
  135. __u16 size;
  136. __u32 trans_id;
  137. } __packed;
  138. /*
  139. * A generic message format for dynamic memory.
  140. * Specific message formats are defined later in the file.
  141. */
  142. struct dm_message {
  143. struct dm_header hdr;
  144. __u8 data[]; /* enclosed message */
  145. } __packed;
  146. /*
  147. * Specific message types supporting the dynamic memory protocol.
  148. */
  149. /*
  150. * Version negotiation message. Sent from the guest to the host.
  151. * The guest is free to try different versions until the host
  152. * accepts the version.
  153. *
  154. * dm_version: The protocol version requested.
  155. * is_last_attempt: If TRUE, this is the last version guest will request.
  156. * reservedz: Reserved field, set to zero.
  157. */
  158. struct dm_version_request {
  159. struct dm_header hdr;
  160. union dm_version version;
  161. __u32 is_last_attempt:1;
  162. __u32 reservedz:31;
  163. } __packed;
  164. /*
  165. * Version response message; Host to Guest and indicates
  166. * if the host has accepted the version sent by the guest.
  167. *
  168. * is_accepted: If TRUE, host has accepted the version and the guest
  169. * should proceed to the next stage of the protocol. FALSE indicates that
  170. * guest should re-try with a different version.
  171. *
  172. * reservedz: Reserved field, set to zero.
  173. */
  174. struct dm_version_response {
  175. struct dm_header hdr;
  176. __u64 is_accepted:1;
  177. __u64 reservedz:63;
  178. } __packed;
  179. /*
  180. * Message reporting capabilities. This is sent from the guest to the
  181. * host.
  182. */
  183. struct dm_capabilities {
  184. struct dm_header hdr;
  185. union dm_caps caps;
  186. __u64 min_page_cnt;
  187. __u64 max_page_number;
  188. } __packed;
  189. /*
  190. * Response to the capabilities message. This is sent from the host to the
  191. * guest. This message notifies if the host has accepted the guest's
  192. * capabilities. If the host has not accepted, the guest must shutdown
  193. * the service.
  194. *
  195. * is_accepted: Indicates if the host has accepted guest's capabilities.
  196. * reservedz: Must be 0.
  197. */
  198. struct dm_capabilities_resp_msg {
  199. struct dm_header hdr;
  200. __u64 is_accepted:1;
  201. __u64 reservedz:63;
  202. } __packed;
  203. /*
  204. * This message is used to report memory pressure from the guest.
  205. * This message is not part of any transaction and there is no
  206. * response to this message.
  207. *
  208. * num_avail: Available memory in pages.
  209. * num_committed: Committed memory in pages.
  210. * page_file_size: The accumulated size of all page files
  211. * in the system in pages.
  212. * zero_free: The nunber of zero and free pages.
  213. * page_file_writes: The writes to the page file in pages.
  214. * io_diff: An indicator of file cache efficiency or page file activity,
  215. * calculated as File Cache Page Fault Count - Page Read Count.
  216. * This value is in pages.
  217. *
  218. * Some of these metrics are Windows specific and fortunately
  219. * the algorithm on the host side that computes the guest memory
  220. * pressure only uses num_committed value.
  221. */
  222. struct dm_status {
  223. struct dm_header hdr;
  224. __u64 num_avail;
  225. __u64 num_committed;
  226. __u64 page_file_size;
  227. __u64 zero_free;
  228. __u32 page_file_writes;
  229. __u32 io_diff;
  230. } __packed;
  231. /*
  232. * Message to ask the guest to allocate memory - balloon up message.
  233. * This message is sent from the host to the guest. The guest may not be
  234. * able to allocate as much memory as requested.
  235. *
  236. * num_pages: number of pages to allocate.
  237. */
  238. struct dm_balloon {
  239. struct dm_header hdr;
  240. __u32 num_pages;
  241. __u32 reservedz;
  242. } __packed;
  243. /*
  244. * Balloon response message; this message is sent from the guest
  245. * to the host in response to the balloon message.
  246. *
  247. * reservedz: Reserved; must be set to zero.
  248. * more_pages: If FALSE, this is the last message of the transaction.
  249. * if TRUE there will atleast one more message from the guest.
  250. *
  251. * range_count: The number of ranges in the range array.
  252. *
  253. * range_array: An array of page ranges returned to the host.
  254. *
  255. */
  256. struct dm_balloon_response {
  257. struct dm_header hdr;
  258. __u32 reservedz;
  259. __u32 more_pages:1;
  260. __u32 range_count:31;
  261. union dm_mem_page_range range_array[];
  262. } __packed;
  263. /*
  264. * Un-balloon message; this message is sent from the host
  265. * to the guest to give guest more memory.
  266. *
  267. * more_pages: If FALSE, this is the last message of the transaction.
  268. * if TRUE there will atleast one more message from the guest.
  269. *
  270. * reservedz: Reserved; must be set to zero.
  271. *
  272. * range_count: The number of ranges in the range array.
  273. *
  274. * range_array: An array of page ranges returned to the host.
  275. *
  276. */
  277. struct dm_unballoon_request {
  278. struct dm_header hdr;
  279. __u32 more_pages:1;
  280. __u32 reservedz:31;
  281. __u32 range_count;
  282. union dm_mem_page_range range_array[];
  283. } __packed;
  284. /*
  285. * Un-balloon response message; this message is sent from the guest
  286. * to the host in response to an unballoon request.
  287. *
  288. */
  289. struct dm_unballoon_response {
  290. struct dm_header hdr;
  291. } __packed;
  292. /*
  293. * Hot add request message. Message sent from the host to the guest.
  294. *
  295. * mem_range: Memory range to hot add.
  296. *
  297. * On Linux we currently don't support this since we cannot hot add
  298. * arbitrary granularity of memory.
  299. */
  300. struct dm_hot_add {
  301. struct dm_header hdr;
  302. union dm_mem_page_range range;
  303. } __packed;
  304. /*
  305. * Hot add response message.
  306. * This message is sent by the guest to report the status of a hot add request.
  307. * If page_count is less than the requested page count, then the host should
  308. * assume all further hot add requests will fail, since this indicates that
  309. * the guest has hit an upper physical memory barrier.
  310. *
  311. * Hot adds may also fail due to low resources; in this case, the guest must
  312. * not complete this message until the hot add can succeed, and the host must
  313. * not send a new hot add request until the response is sent.
  314. * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
  315. * times it fails the request.
  316. *
  317. *
  318. * page_count: number of pages that were successfully hot added.
  319. *
  320. * result: result of the operation 1: success, 0: failure.
  321. *
  322. */
  323. struct dm_hot_add_response {
  324. struct dm_header hdr;
  325. __u32 page_count;
  326. __u32 result;
  327. } __packed;
  328. /*
  329. * Types of information sent from host to the guest.
  330. */
  331. enum dm_info_type {
  332. INFO_TYPE_MAX_PAGE_CNT = 0,
  333. MAX_INFO_TYPE
  334. };
  335. /*
  336. * Header for the information message.
  337. */
  338. struct dm_info_header {
  339. enum dm_info_type type;
  340. __u32 data_size;
  341. } __packed;
  342. /*
  343. * This message is sent from the host to the guest to pass
  344. * some relevant information (win8 addition).
  345. *
  346. * reserved: no used.
  347. * info_size: size of the information blob.
  348. * info: information blob.
  349. */
  350. struct dm_info_msg {
  351. struct dm_header hdr;
  352. __u32 reserved;
  353. __u32 info_size;
  354. __u8 info[];
  355. };
  356. /*
  357. * End protocol definitions.
  358. */
  359. /*
  360. * State to manage hot adding memory into the guest.
  361. * The range start_pfn : end_pfn specifies the range
  362. * that the host has asked us to hot add. The range
  363. * start_pfn : ha_end_pfn specifies the range that we have
  364. * currently hot added. We hot add in multiples of 128M
  365. * chunks; it is possible that we may not be able to bring
  366. * online all the pages in the region. The range
  367. * covered_start_pfn : covered_end_pfn defines the pages that can
  368. * be brough online.
  369. */
  370. struct hv_hotadd_state {
  371. struct list_head list;
  372. unsigned long start_pfn;
  373. unsigned long covered_start_pfn;
  374. unsigned long covered_end_pfn;
  375. unsigned long ha_end_pfn;
  376. unsigned long end_pfn;
  377. };
  378. struct balloon_state {
  379. __u32 num_pages;
  380. struct work_struct wrk;
  381. };
  382. struct hot_add_wrk {
  383. union dm_mem_page_range ha_page_range;
  384. union dm_mem_page_range ha_region_range;
  385. struct work_struct wrk;
  386. };
  387. static bool hot_add = true;
  388. static bool do_hot_add;
  389. /*
  390. * Delay reporting memory pressure by
  391. * the specified number of seconds.
  392. */
  393. static uint pressure_report_delay = 45;
  394. module_param(hot_add, bool, (S_IRUGO | S_IWUSR));
  395. MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
  396. module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR));
  397. MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
  398. static atomic_t trans_id = ATOMIC_INIT(0);
  399. static int dm_ring_size = (5 * PAGE_SIZE);
  400. /*
  401. * Driver specific state.
  402. */
  403. enum hv_dm_state {
  404. DM_INITIALIZING = 0,
  405. DM_INITIALIZED,
  406. DM_BALLOON_UP,
  407. DM_BALLOON_DOWN,
  408. DM_HOT_ADD,
  409. DM_INIT_ERROR
  410. };
  411. static __u8 recv_buffer[PAGE_SIZE];
  412. static __u8 *send_buffer;
  413. #define PAGES_IN_2M 512
  414. #define HA_CHUNK (32 * 1024)
  415. struct hv_dynmem_device {
  416. struct hv_device *dev;
  417. enum hv_dm_state state;
  418. struct completion host_event;
  419. struct completion config_event;
  420. /*
  421. * Number of pages we have currently ballooned out.
  422. */
  423. unsigned int num_pages_ballooned;
  424. /*
  425. * State to manage the ballooning (up) operation.
  426. */
  427. struct balloon_state balloon_wrk;
  428. /*
  429. * State to execute the "hot-add" operation.
  430. */
  431. struct hot_add_wrk ha_wrk;
  432. /*
  433. * This state tracks if the host has specified a hot-add
  434. * region.
  435. */
  436. bool host_specified_ha_region;
  437. /*
  438. * State to synchronize hot-add.
  439. */
  440. struct completion ol_waitevent;
  441. bool ha_waiting;
  442. /*
  443. * This thread handles hot-add
  444. * requests from the host as well as notifying
  445. * the host with regards to memory pressure in
  446. * the guest.
  447. */
  448. struct task_struct *thread;
  449. /*
  450. * A list of hot-add regions.
  451. */
  452. struct list_head ha_region_list;
  453. /*
  454. * We start with the highest version we can support
  455. * and downgrade based on the host; we save here the
  456. * next version to try.
  457. */
  458. __u32 next_version;
  459. };
  460. static struct hv_dynmem_device dm_device;
  461. #ifdef CONFIG_MEMORY_HOTPLUG
  462. static void hv_bring_pgs_online(unsigned long start_pfn, unsigned long size)
  463. {
  464. int i;
  465. for (i = 0; i < size; i++) {
  466. struct page *pg;
  467. pg = pfn_to_page(start_pfn + i);
  468. __online_page_set_limits(pg);
  469. __online_page_increment_counters(pg);
  470. __online_page_free(pg);
  471. }
  472. }
  473. static void hv_mem_hot_add(unsigned long start, unsigned long size,
  474. unsigned long pfn_count,
  475. struct hv_hotadd_state *has)
  476. {
  477. int ret = 0;
  478. int i, nid, t;
  479. unsigned long start_pfn;
  480. unsigned long processed_pfn;
  481. unsigned long total_pfn = pfn_count;
  482. for (i = 0; i < (size/HA_CHUNK); i++) {
  483. start_pfn = start + (i * HA_CHUNK);
  484. has->ha_end_pfn += HA_CHUNK;
  485. if (total_pfn > HA_CHUNK) {
  486. processed_pfn = HA_CHUNK;
  487. total_pfn -= HA_CHUNK;
  488. } else {
  489. processed_pfn = total_pfn;
  490. total_pfn = 0;
  491. }
  492. has->covered_end_pfn += processed_pfn;
  493. init_completion(&dm_device.ol_waitevent);
  494. dm_device.ha_waiting = true;
  495. nid = memory_add_physaddr_to_nid(PFN_PHYS(start_pfn));
  496. ret = add_memory(nid, PFN_PHYS((start_pfn)),
  497. (HA_CHUNK << PAGE_SHIFT));
  498. if (ret) {
  499. pr_info("hot_add memory failed error is %d\n", ret);
  500. has->ha_end_pfn -= HA_CHUNK;
  501. has->covered_end_pfn -= processed_pfn;
  502. break;
  503. }
  504. /*
  505. * Wait for the memory block to be onlined.
  506. */
  507. t = wait_for_completion_timeout(&dm_device.ol_waitevent, 5*HZ);
  508. if (t == 0) {
  509. pr_info("hot_add memory timedout\n");
  510. has->ha_end_pfn -= HA_CHUNK;
  511. has->covered_end_pfn -= processed_pfn;
  512. break;
  513. }
  514. }
  515. return;
  516. }
  517. static void hv_online_page(struct page *pg)
  518. {
  519. struct list_head *cur;
  520. struct hv_hotadd_state *has;
  521. unsigned long cur_start_pgp;
  522. unsigned long cur_end_pgp;
  523. if (dm_device.ha_waiting) {
  524. dm_device.ha_waiting = false;
  525. complete(&dm_device.ol_waitevent);
  526. }
  527. list_for_each(cur, &dm_device.ha_region_list) {
  528. has = list_entry(cur, struct hv_hotadd_state, list);
  529. cur_start_pgp = (unsigned long)
  530. pfn_to_page(has->covered_start_pfn);
  531. cur_end_pgp = (unsigned long)pfn_to_page(has->covered_end_pfn);
  532. if (((unsigned long)pg >= cur_start_pgp) &&
  533. ((unsigned long)pg < cur_end_pgp)) {
  534. /*
  535. * This frame is currently backed; online the
  536. * page.
  537. */
  538. __online_page_set_limits(pg);
  539. __online_page_increment_counters(pg);
  540. __online_page_free(pg);
  541. has->covered_start_pfn++;
  542. }
  543. }
  544. }
  545. static bool pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
  546. {
  547. struct list_head *cur;
  548. struct hv_hotadd_state *has;
  549. unsigned long residual, new_inc;
  550. if (list_empty(&dm_device.ha_region_list))
  551. return false;
  552. list_for_each(cur, &dm_device.ha_region_list) {
  553. has = list_entry(cur, struct hv_hotadd_state, list);
  554. /*
  555. * If the pfn range we are dealing with is not in the current
  556. * "hot add block", move on.
  557. */
  558. if ((start_pfn >= has->end_pfn))
  559. continue;
  560. /*
  561. * If the current hot add-request extends beyond
  562. * our current limit; extend it.
  563. */
  564. if ((start_pfn + pfn_cnt) > has->end_pfn) {
  565. residual = (start_pfn + pfn_cnt - has->end_pfn);
  566. /*
  567. * Extend the region by multiples of HA_CHUNK.
  568. */
  569. new_inc = (residual / HA_CHUNK) * HA_CHUNK;
  570. if (residual % HA_CHUNK)
  571. new_inc += HA_CHUNK;
  572. has->end_pfn += new_inc;
  573. }
  574. /*
  575. * If the current start pfn is not where the covered_end
  576. * is, update it.
  577. */
  578. if (has->covered_end_pfn != start_pfn) {
  579. has->covered_end_pfn = start_pfn;
  580. has->covered_start_pfn = start_pfn;
  581. }
  582. return true;
  583. }
  584. return false;
  585. }
  586. static unsigned long handle_pg_range(unsigned long pg_start,
  587. unsigned long pg_count)
  588. {
  589. unsigned long start_pfn = pg_start;
  590. unsigned long pfn_cnt = pg_count;
  591. unsigned long size;
  592. struct list_head *cur;
  593. struct hv_hotadd_state *has;
  594. unsigned long pgs_ol = 0;
  595. unsigned long old_covered_state;
  596. if (list_empty(&dm_device.ha_region_list))
  597. return 0;
  598. list_for_each(cur, &dm_device.ha_region_list) {
  599. has = list_entry(cur, struct hv_hotadd_state, list);
  600. /*
  601. * If the pfn range we are dealing with is not in the current
  602. * "hot add block", move on.
  603. */
  604. if ((start_pfn >= has->end_pfn))
  605. continue;
  606. old_covered_state = has->covered_end_pfn;
  607. if (start_pfn < has->ha_end_pfn) {
  608. /*
  609. * This is the case where we are backing pages
  610. * in an already hot added region. Bring
  611. * these pages online first.
  612. */
  613. pgs_ol = has->ha_end_pfn - start_pfn;
  614. if (pgs_ol > pfn_cnt)
  615. pgs_ol = pfn_cnt;
  616. hv_bring_pgs_online(start_pfn, pgs_ol);
  617. has->covered_end_pfn += pgs_ol;
  618. has->covered_start_pfn += pgs_ol;
  619. pfn_cnt -= pgs_ol;
  620. }
  621. if ((has->ha_end_pfn < has->end_pfn) && (pfn_cnt > 0)) {
  622. /*
  623. * We have some residual hot add range
  624. * that needs to be hot added; hot add
  625. * it now. Hot add a multiple of
  626. * of HA_CHUNK that fully covers the pages
  627. * we have.
  628. */
  629. size = (has->end_pfn - has->ha_end_pfn);
  630. if (pfn_cnt <= size) {
  631. size = ((pfn_cnt / HA_CHUNK) * HA_CHUNK);
  632. if (pfn_cnt % HA_CHUNK)
  633. size += HA_CHUNK;
  634. } else {
  635. pfn_cnt = size;
  636. }
  637. hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
  638. }
  639. /*
  640. * If we managed to online any pages that were given to us,
  641. * we declare success.
  642. */
  643. return has->covered_end_pfn - old_covered_state;
  644. }
  645. return 0;
  646. }
  647. static unsigned long process_hot_add(unsigned long pg_start,
  648. unsigned long pfn_cnt,
  649. unsigned long rg_start,
  650. unsigned long rg_size)
  651. {
  652. struct hv_hotadd_state *ha_region = NULL;
  653. if (pfn_cnt == 0)
  654. return 0;
  655. if (!dm_device.host_specified_ha_region)
  656. if (pfn_covered(pg_start, pfn_cnt))
  657. goto do_pg_range;
  658. /*
  659. * If the host has specified a hot-add range; deal with it first.
  660. */
  661. if (rg_size != 0) {
  662. ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
  663. if (!ha_region)
  664. return 0;
  665. INIT_LIST_HEAD(&ha_region->list);
  666. list_add_tail(&ha_region->list, &dm_device.ha_region_list);
  667. ha_region->start_pfn = rg_start;
  668. ha_region->ha_end_pfn = rg_start;
  669. ha_region->covered_start_pfn = pg_start;
  670. ha_region->covered_end_pfn = pg_start;
  671. ha_region->end_pfn = rg_start + rg_size;
  672. }
  673. do_pg_range:
  674. /*
  675. * Process the page range specified; bringing them
  676. * online if possible.
  677. */
  678. return handle_pg_range(pg_start, pfn_cnt);
  679. }
  680. #endif
  681. static void hot_add_req(struct work_struct *dummy)
  682. {
  683. struct dm_hot_add_response resp;
  684. #ifdef CONFIG_MEMORY_HOTPLUG
  685. unsigned long pg_start, pfn_cnt;
  686. unsigned long rg_start, rg_sz;
  687. #endif
  688. struct hv_dynmem_device *dm = &dm_device;
  689. memset(&resp, 0, sizeof(struct dm_hot_add_response));
  690. resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
  691. resp.hdr.size = sizeof(struct dm_hot_add_response);
  692. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  693. #ifdef CONFIG_MEMORY_HOTPLUG
  694. pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
  695. pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
  696. rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
  697. rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
  698. if ((rg_start == 0) && (!dm->host_specified_ha_region)) {
  699. unsigned long region_size;
  700. unsigned long region_start;
  701. /*
  702. * The host has not specified the hot-add region.
  703. * Based on the hot-add page range being specified,
  704. * compute a hot-add region that can cover the pages
  705. * that need to be hot-added while ensuring the alignment
  706. * and size requirements of Linux as it relates to hot-add.
  707. */
  708. region_start = pg_start;
  709. region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK;
  710. if (pfn_cnt % HA_CHUNK)
  711. region_size += HA_CHUNK;
  712. region_start = (pg_start / HA_CHUNK) * HA_CHUNK;
  713. rg_start = region_start;
  714. rg_sz = region_size;
  715. }
  716. resp.page_count = process_hot_add(pg_start, pfn_cnt,
  717. rg_start, rg_sz);
  718. #endif
  719. if (resp.page_count > 0)
  720. resp.result = 1;
  721. else
  722. resp.result = 0;
  723. if (!do_hot_add || (resp.page_count == 0))
  724. pr_info("Memory hot add failed\n");
  725. dm->state = DM_INITIALIZED;
  726. vmbus_sendpacket(dm->dev->channel, &resp,
  727. sizeof(struct dm_hot_add_response),
  728. (unsigned long)NULL,
  729. VM_PKT_DATA_INBAND, 0);
  730. }
  731. static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
  732. {
  733. struct dm_info_header *info_hdr;
  734. info_hdr = (struct dm_info_header *)msg->info;
  735. switch (info_hdr->type) {
  736. case INFO_TYPE_MAX_PAGE_CNT:
  737. pr_info("Received INFO_TYPE_MAX_PAGE_CNT\n");
  738. pr_info("Data Size is %d\n", info_hdr->data_size);
  739. break;
  740. default:
  741. pr_info("Received Unknown type: %d\n", info_hdr->type);
  742. }
  743. }
  744. static unsigned long compute_balloon_floor(void)
  745. {
  746. unsigned long min_pages;
  747. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  748. /* Simple continuous piecewiese linear function:
  749. * max MiB -> min MiB gradient
  750. * 0 0
  751. * 16 16
  752. * 32 24
  753. * 128 72 (1/2)
  754. * 512 168 (1/4)
  755. * 2048 360 (1/8)
  756. * 8192 552 (1/32)
  757. * 32768 1320
  758. * 131072 4392
  759. */
  760. if (totalram_pages < MB2PAGES(128))
  761. min_pages = MB2PAGES(8) + (totalram_pages >> 1);
  762. else if (totalram_pages < MB2PAGES(512))
  763. min_pages = MB2PAGES(40) + (totalram_pages >> 2);
  764. else if (totalram_pages < MB2PAGES(2048))
  765. min_pages = MB2PAGES(104) + (totalram_pages >> 3);
  766. else
  767. min_pages = MB2PAGES(296) + (totalram_pages >> 5);
  768. #undef MB2PAGES
  769. return min_pages;
  770. }
  771. /*
  772. * Post our status as it relates memory pressure to the
  773. * host. Host expects the guests to post this status
  774. * periodically at 1 second intervals.
  775. *
  776. * The metrics specified in this protocol are very Windows
  777. * specific and so we cook up numbers here to convey our memory
  778. * pressure.
  779. */
  780. static void post_status(struct hv_dynmem_device *dm)
  781. {
  782. struct dm_status status;
  783. struct sysinfo val;
  784. if (pressure_report_delay > 0) {
  785. --pressure_report_delay;
  786. return;
  787. }
  788. si_meminfo(&val);
  789. memset(&status, 0, sizeof(struct dm_status));
  790. status.hdr.type = DM_STATUS_REPORT;
  791. status.hdr.size = sizeof(struct dm_status);
  792. status.hdr.trans_id = atomic_inc_return(&trans_id);
  793. /*
  794. * The host expects the guest to report free memory.
  795. * Further, the host expects the pressure information to
  796. * include the ballooned out pages.
  797. * For a given amount of memory that we are managing, we
  798. * need to compute a floor below which we should not balloon.
  799. * Compute this and add it to the pressure report.
  800. */
  801. status.num_avail = val.freeram;
  802. status.num_committed = vm_memory_committed() +
  803. dm->num_pages_ballooned +
  804. compute_balloon_floor();
  805. vmbus_sendpacket(dm->dev->channel, &status,
  806. sizeof(struct dm_status),
  807. (unsigned long)NULL,
  808. VM_PKT_DATA_INBAND, 0);
  809. }
  810. static void free_balloon_pages(struct hv_dynmem_device *dm,
  811. union dm_mem_page_range *range_array)
  812. {
  813. int num_pages = range_array->finfo.page_cnt;
  814. __u64 start_frame = range_array->finfo.start_page;
  815. struct page *pg;
  816. int i;
  817. for (i = 0; i < num_pages; i++) {
  818. pg = pfn_to_page(i + start_frame);
  819. __free_page(pg);
  820. dm->num_pages_ballooned--;
  821. }
  822. }
  823. static int alloc_balloon_pages(struct hv_dynmem_device *dm, int num_pages,
  824. struct dm_balloon_response *bl_resp, int alloc_unit,
  825. bool *alloc_error)
  826. {
  827. int i = 0;
  828. struct page *pg;
  829. if (num_pages < alloc_unit)
  830. return 0;
  831. for (i = 0; (i * alloc_unit) < num_pages; i++) {
  832. if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
  833. PAGE_SIZE)
  834. return i * alloc_unit;
  835. /*
  836. * We execute this code in a thread context. Furthermore,
  837. * we don't want the kernel to try too hard.
  838. */
  839. pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
  840. __GFP_NOMEMALLOC | __GFP_NOWARN,
  841. get_order(alloc_unit << PAGE_SHIFT));
  842. if (!pg) {
  843. *alloc_error = true;
  844. return i * alloc_unit;
  845. }
  846. dm->num_pages_ballooned += alloc_unit;
  847. bl_resp->range_count++;
  848. bl_resp->range_array[i].finfo.start_page =
  849. page_to_pfn(pg);
  850. bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
  851. bl_resp->hdr.size += sizeof(union dm_mem_page_range);
  852. }
  853. return num_pages;
  854. }
  855. static void balloon_up(struct work_struct *dummy)
  856. {
  857. int num_pages = dm_device.balloon_wrk.num_pages;
  858. int num_ballooned = 0;
  859. struct dm_balloon_response *bl_resp;
  860. int alloc_unit;
  861. int ret;
  862. bool alloc_error = false;
  863. bool done = false;
  864. int i;
  865. /*
  866. * Currently, we only support 4k allocations.
  867. */
  868. alloc_unit = 1;
  869. while (!done) {
  870. bl_resp = (struct dm_balloon_response *)send_buffer;
  871. memset(send_buffer, 0, PAGE_SIZE);
  872. bl_resp->hdr.type = DM_BALLOON_RESPONSE;
  873. bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
  874. bl_resp->hdr.size = sizeof(struct dm_balloon_response);
  875. bl_resp->more_pages = 1;
  876. num_pages -= num_ballooned;
  877. num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
  878. bl_resp, alloc_unit,
  879. &alloc_error);
  880. if ((alloc_error) || (num_ballooned == num_pages)) {
  881. bl_resp->more_pages = 0;
  882. done = true;
  883. dm_device.state = DM_INITIALIZED;
  884. }
  885. /*
  886. * We are pushing a lot of data through the channel;
  887. * deal with transient failures caused because of the
  888. * lack of space in the ring buffer.
  889. */
  890. do {
  891. ret = vmbus_sendpacket(dm_device.dev->channel,
  892. bl_resp,
  893. bl_resp->hdr.size,
  894. (unsigned long)NULL,
  895. VM_PKT_DATA_INBAND, 0);
  896. if (ret == -EAGAIN)
  897. msleep(20);
  898. } while (ret == -EAGAIN);
  899. if (ret) {
  900. /*
  901. * Free up the memory we allocatted.
  902. */
  903. pr_info("Balloon response failed\n");
  904. for (i = 0; i < bl_resp->range_count; i++)
  905. free_balloon_pages(&dm_device,
  906. &bl_resp->range_array[i]);
  907. done = true;
  908. }
  909. }
  910. }
  911. static void balloon_down(struct hv_dynmem_device *dm,
  912. struct dm_unballoon_request *req)
  913. {
  914. union dm_mem_page_range *range_array = req->range_array;
  915. int range_count = req->range_count;
  916. struct dm_unballoon_response resp;
  917. int i;
  918. for (i = 0; i < range_count; i++)
  919. free_balloon_pages(dm, &range_array[i]);
  920. if (req->more_pages == 1)
  921. return;
  922. memset(&resp, 0, sizeof(struct dm_unballoon_response));
  923. resp.hdr.type = DM_UNBALLOON_RESPONSE;
  924. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  925. resp.hdr.size = sizeof(struct dm_unballoon_response);
  926. vmbus_sendpacket(dm_device.dev->channel, &resp,
  927. sizeof(struct dm_unballoon_response),
  928. (unsigned long)NULL,
  929. VM_PKT_DATA_INBAND, 0);
  930. dm->state = DM_INITIALIZED;
  931. }
  932. static void balloon_onchannelcallback(void *context);
  933. static int dm_thread_func(void *dm_dev)
  934. {
  935. struct hv_dynmem_device *dm = dm_dev;
  936. int t;
  937. while (!kthread_should_stop()) {
  938. t = wait_for_completion_timeout(&dm_device.config_event, 1*HZ);
  939. /*
  940. * The host expects us to post information on the memory
  941. * pressure every second.
  942. */
  943. if (t == 0)
  944. post_status(dm);
  945. }
  946. return 0;
  947. }
  948. static void version_resp(struct hv_dynmem_device *dm,
  949. struct dm_version_response *vresp)
  950. {
  951. struct dm_version_request version_req;
  952. int ret;
  953. if (vresp->is_accepted) {
  954. /*
  955. * We are done; wakeup the
  956. * context waiting for version
  957. * negotiation.
  958. */
  959. complete(&dm->host_event);
  960. return;
  961. }
  962. /*
  963. * If there are more versions to try, continue
  964. * with negotiations; if not
  965. * shutdown the service since we are not able
  966. * to negotiate a suitable version number
  967. * with the host.
  968. */
  969. if (dm->next_version == 0)
  970. goto version_error;
  971. dm->next_version = 0;
  972. memset(&version_req, 0, sizeof(struct dm_version_request));
  973. version_req.hdr.type = DM_VERSION_REQUEST;
  974. version_req.hdr.size = sizeof(struct dm_version_request);
  975. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  976. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN7;
  977. version_req.is_last_attempt = 1;
  978. ret = vmbus_sendpacket(dm->dev->channel, &version_req,
  979. sizeof(struct dm_version_request),
  980. (unsigned long)NULL,
  981. VM_PKT_DATA_INBAND, 0);
  982. if (ret)
  983. goto version_error;
  984. return;
  985. version_error:
  986. dm->state = DM_INIT_ERROR;
  987. complete(&dm->host_event);
  988. }
  989. static void cap_resp(struct hv_dynmem_device *dm,
  990. struct dm_capabilities_resp_msg *cap_resp)
  991. {
  992. if (!cap_resp->is_accepted) {
  993. pr_info("Capabilities not accepted by host\n");
  994. dm->state = DM_INIT_ERROR;
  995. }
  996. complete(&dm->host_event);
  997. }
  998. static void balloon_onchannelcallback(void *context)
  999. {
  1000. struct hv_device *dev = context;
  1001. u32 recvlen;
  1002. u64 requestid;
  1003. struct dm_message *dm_msg;
  1004. struct dm_header *dm_hdr;
  1005. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1006. struct dm_balloon *bal_msg;
  1007. struct dm_hot_add *ha_msg;
  1008. union dm_mem_page_range *ha_pg_range;
  1009. union dm_mem_page_range *ha_region;
  1010. memset(recv_buffer, 0, sizeof(recv_buffer));
  1011. vmbus_recvpacket(dev->channel, recv_buffer,
  1012. PAGE_SIZE, &recvlen, &requestid);
  1013. if (recvlen > 0) {
  1014. dm_msg = (struct dm_message *)recv_buffer;
  1015. dm_hdr = &dm_msg->hdr;
  1016. switch (dm_hdr->type) {
  1017. case DM_VERSION_RESPONSE:
  1018. version_resp(dm,
  1019. (struct dm_version_response *)dm_msg);
  1020. break;
  1021. case DM_CAPABILITIES_RESPONSE:
  1022. cap_resp(dm,
  1023. (struct dm_capabilities_resp_msg *)dm_msg);
  1024. break;
  1025. case DM_BALLOON_REQUEST:
  1026. if (dm->state == DM_BALLOON_UP)
  1027. pr_warn("Currently ballooning\n");
  1028. bal_msg = (struct dm_balloon *)recv_buffer;
  1029. dm->state = DM_BALLOON_UP;
  1030. dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
  1031. schedule_work(&dm_device.balloon_wrk.wrk);
  1032. break;
  1033. case DM_UNBALLOON_REQUEST:
  1034. dm->state = DM_BALLOON_DOWN;
  1035. balloon_down(dm,
  1036. (struct dm_unballoon_request *)recv_buffer);
  1037. break;
  1038. case DM_MEM_HOT_ADD_REQUEST:
  1039. if (dm->state == DM_HOT_ADD)
  1040. pr_warn("Currently hot-adding\n");
  1041. dm->state = DM_HOT_ADD;
  1042. ha_msg = (struct dm_hot_add *)recv_buffer;
  1043. if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
  1044. /*
  1045. * This is a normal hot-add request specifying
  1046. * hot-add memory.
  1047. */
  1048. ha_pg_range = &ha_msg->range;
  1049. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1050. dm->ha_wrk.ha_region_range.page_range = 0;
  1051. } else {
  1052. /*
  1053. * Host is specifying that we first hot-add
  1054. * a region and then partially populate this
  1055. * region.
  1056. */
  1057. dm->host_specified_ha_region = true;
  1058. ha_pg_range = &ha_msg->range;
  1059. ha_region = &ha_pg_range[1];
  1060. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1061. dm->ha_wrk.ha_region_range = *ha_region;
  1062. }
  1063. schedule_work(&dm_device.ha_wrk.wrk);
  1064. break;
  1065. case DM_INFO_MESSAGE:
  1066. process_info(dm, (struct dm_info_msg *)dm_msg);
  1067. break;
  1068. default:
  1069. pr_err("Unhandled message: type: %d\n", dm_hdr->type);
  1070. }
  1071. }
  1072. }
  1073. static int balloon_probe(struct hv_device *dev,
  1074. const struct hv_vmbus_device_id *dev_id)
  1075. {
  1076. int ret, t;
  1077. struct dm_version_request version_req;
  1078. struct dm_capabilities cap_msg;
  1079. do_hot_add = hot_add;
  1080. /*
  1081. * First allocate a send buffer.
  1082. */
  1083. send_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1084. if (!send_buffer)
  1085. return -ENOMEM;
  1086. ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
  1087. balloon_onchannelcallback, dev);
  1088. if (ret)
  1089. goto probe_error0;
  1090. dm_device.dev = dev;
  1091. dm_device.state = DM_INITIALIZING;
  1092. dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
  1093. init_completion(&dm_device.host_event);
  1094. init_completion(&dm_device.config_event);
  1095. INIT_LIST_HEAD(&dm_device.ha_region_list);
  1096. INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
  1097. INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
  1098. dm_device.host_specified_ha_region = false;
  1099. dm_device.thread =
  1100. kthread_run(dm_thread_func, &dm_device, "hv_balloon");
  1101. if (IS_ERR(dm_device.thread)) {
  1102. ret = PTR_ERR(dm_device.thread);
  1103. goto probe_error1;
  1104. }
  1105. #ifdef CONFIG_MEMORY_HOTPLUG
  1106. set_online_page_callback(&hv_online_page);
  1107. #endif
  1108. hv_set_drvdata(dev, &dm_device);
  1109. /*
  1110. * Initiate the hand shake with the host and negotiate
  1111. * a version that the host can support. We start with the
  1112. * highest version number and go down if the host cannot
  1113. * support it.
  1114. */
  1115. memset(&version_req, 0, sizeof(struct dm_version_request));
  1116. version_req.hdr.type = DM_VERSION_REQUEST;
  1117. version_req.hdr.size = sizeof(struct dm_version_request);
  1118. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1119. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN8;
  1120. version_req.is_last_attempt = 0;
  1121. ret = vmbus_sendpacket(dev->channel, &version_req,
  1122. sizeof(struct dm_version_request),
  1123. (unsigned long)NULL,
  1124. VM_PKT_DATA_INBAND, 0);
  1125. if (ret)
  1126. goto probe_error2;
  1127. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  1128. if (t == 0) {
  1129. ret = -ETIMEDOUT;
  1130. goto probe_error2;
  1131. }
  1132. /*
  1133. * If we could not negotiate a compatible version with the host
  1134. * fail the probe function.
  1135. */
  1136. if (dm_device.state == DM_INIT_ERROR) {
  1137. ret = -ETIMEDOUT;
  1138. goto probe_error2;
  1139. }
  1140. /*
  1141. * Now submit our capabilities to the host.
  1142. */
  1143. memset(&cap_msg, 0, sizeof(struct dm_capabilities));
  1144. cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
  1145. cap_msg.hdr.size = sizeof(struct dm_capabilities);
  1146. cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
  1147. cap_msg.caps.cap_bits.balloon = 1;
  1148. cap_msg.caps.cap_bits.hot_add = 1;
  1149. /*
  1150. * Specify our alignment requirements as it relates
  1151. * memory hot-add. Specify 128MB alignment.
  1152. */
  1153. cap_msg.caps.cap_bits.hot_add_alignment = 7;
  1154. /*
  1155. * Currently the host does not use these
  1156. * values and we set them to what is done in the
  1157. * Windows driver.
  1158. */
  1159. cap_msg.min_page_cnt = 0;
  1160. cap_msg.max_page_number = -1;
  1161. ret = vmbus_sendpacket(dev->channel, &cap_msg,
  1162. sizeof(struct dm_capabilities),
  1163. (unsigned long)NULL,
  1164. VM_PKT_DATA_INBAND, 0);
  1165. if (ret)
  1166. goto probe_error2;
  1167. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  1168. if (t == 0) {
  1169. ret = -ETIMEDOUT;
  1170. goto probe_error2;
  1171. }
  1172. /*
  1173. * If the host does not like our capabilities,
  1174. * fail the probe function.
  1175. */
  1176. if (dm_device.state == DM_INIT_ERROR) {
  1177. ret = -ETIMEDOUT;
  1178. goto probe_error2;
  1179. }
  1180. dm_device.state = DM_INITIALIZED;
  1181. return 0;
  1182. probe_error2:
  1183. #ifdef CONFIG_MEMORY_HOTPLUG
  1184. restore_online_page_callback(&hv_online_page);
  1185. #endif
  1186. kthread_stop(dm_device.thread);
  1187. probe_error1:
  1188. vmbus_close(dev->channel);
  1189. probe_error0:
  1190. kfree(send_buffer);
  1191. return ret;
  1192. }
  1193. static int balloon_remove(struct hv_device *dev)
  1194. {
  1195. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1196. struct list_head *cur, *tmp;
  1197. struct hv_hotadd_state *has;
  1198. if (dm->num_pages_ballooned != 0)
  1199. pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
  1200. cancel_work_sync(&dm->balloon_wrk.wrk);
  1201. cancel_work_sync(&dm->ha_wrk.wrk);
  1202. vmbus_close(dev->channel);
  1203. kthread_stop(dm->thread);
  1204. kfree(send_buffer);
  1205. #ifdef CONFIG_MEMORY_HOTPLUG
  1206. restore_online_page_callback(&hv_online_page);
  1207. #endif
  1208. list_for_each_safe(cur, tmp, &dm->ha_region_list) {
  1209. has = list_entry(cur, struct hv_hotadd_state, list);
  1210. list_del(&has->list);
  1211. kfree(has);
  1212. }
  1213. return 0;
  1214. }
  1215. static const struct hv_vmbus_device_id id_table[] = {
  1216. /* Dynamic Memory Class ID */
  1217. /* 525074DC-8985-46e2-8057-A307DC18A502 */
  1218. { HV_DM_GUID, },
  1219. { },
  1220. };
  1221. MODULE_DEVICE_TABLE(vmbus, id_table);
  1222. static struct hv_driver balloon_drv = {
  1223. .name = "hv_balloon",
  1224. .id_table = id_table,
  1225. .probe = balloon_probe,
  1226. .remove = balloon_remove,
  1227. };
  1228. static int __init init_balloon_drv(void)
  1229. {
  1230. return vmbus_driver_register(&balloon_drv);
  1231. }
  1232. module_init(init_balloon_drv);
  1233. MODULE_DESCRIPTION("Hyper-V Balloon");
  1234. MODULE_VERSION(HV_DRV_VERSION);
  1235. MODULE_LICENSE("GPL");