hv_balloon.c 37 KB

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