hv_balloon.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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. if (ret == -EEXIST) {
  501. /*
  502. * This error indicates that the error
  503. * is not a transient failure. This is the
  504. * case where the guest's physical address map
  505. * precludes hot adding memory. Stop all further
  506. * memory hot-add.
  507. */
  508. do_hot_add = false;
  509. }
  510. has->ha_end_pfn -= HA_CHUNK;
  511. has->covered_end_pfn -= processed_pfn;
  512. break;
  513. }
  514. /*
  515. * Wait for the memory block to be onlined.
  516. */
  517. t = wait_for_completion_timeout(&dm_device.ol_waitevent, 5*HZ);
  518. if (t == 0) {
  519. pr_info("hot_add memory timedout\n");
  520. has->ha_end_pfn -= HA_CHUNK;
  521. has->covered_end_pfn -= processed_pfn;
  522. break;
  523. }
  524. }
  525. return;
  526. }
  527. static void hv_online_page(struct page *pg)
  528. {
  529. struct list_head *cur;
  530. struct hv_hotadd_state *has;
  531. unsigned long cur_start_pgp;
  532. unsigned long cur_end_pgp;
  533. if (dm_device.ha_waiting) {
  534. dm_device.ha_waiting = false;
  535. complete(&dm_device.ol_waitevent);
  536. }
  537. list_for_each(cur, &dm_device.ha_region_list) {
  538. has = list_entry(cur, struct hv_hotadd_state, list);
  539. cur_start_pgp = (unsigned long)
  540. pfn_to_page(has->covered_start_pfn);
  541. cur_end_pgp = (unsigned long)pfn_to_page(has->covered_end_pfn);
  542. if (((unsigned long)pg >= cur_start_pgp) &&
  543. ((unsigned long)pg < cur_end_pgp)) {
  544. /*
  545. * This frame is currently backed; online the
  546. * page.
  547. */
  548. __online_page_set_limits(pg);
  549. __online_page_increment_counters(pg);
  550. __online_page_free(pg);
  551. has->covered_start_pfn++;
  552. }
  553. }
  554. }
  555. static bool pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
  556. {
  557. struct list_head *cur;
  558. struct hv_hotadd_state *has;
  559. unsigned long residual, new_inc;
  560. if (list_empty(&dm_device.ha_region_list))
  561. return false;
  562. list_for_each(cur, &dm_device.ha_region_list) {
  563. has = list_entry(cur, struct hv_hotadd_state, list);
  564. /*
  565. * If the pfn range we are dealing with is not in the current
  566. * "hot add block", move on.
  567. */
  568. if ((start_pfn >= has->end_pfn))
  569. continue;
  570. /*
  571. * If the current hot add-request extends beyond
  572. * our current limit; extend it.
  573. */
  574. if ((start_pfn + pfn_cnt) > has->end_pfn) {
  575. residual = (start_pfn + pfn_cnt - has->end_pfn);
  576. /*
  577. * Extend the region by multiples of HA_CHUNK.
  578. */
  579. new_inc = (residual / HA_CHUNK) * HA_CHUNK;
  580. if (residual % HA_CHUNK)
  581. new_inc += HA_CHUNK;
  582. has->end_pfn += new_inc;
  583. }
  584. /*
  585. * If the current start pfn is not where the covered_end
  586. * is, update it.
  587. */
  588. if (has->covered_end_pfn != start_pfn) {
  589. has->covered_end_pfn = start_pfn;
  590. has->covered_start_pfn = start_pfn;
  591. }
  592. return true;
  593. }
  594. return false;
  595. }
  596. static unsigned long handle_pg_range(unsigned long pg_start,
  597. unsigned long pg_count)
  598. {
  599. unsigned long start_pfn = pg_start;
  600. unsigned long pfn_cnt = pg_count;
  601. unsigned long size;
  602. struct list_head *cur;
  603. struct hv_hotadd_state *has;
  604. unsigned long pgs_ol = 0;
  605. unsigned long old_covered_state;
  606. if (list_empty(&dm_device.ha_region_list))
  607. return 0;
  608. list_for_each(cur, &dm_device.ha_region_list) {
  609. has = list_entry(cur, struct hv_hotadd_state, list);
  610. /*
  611. * If the pfn range we are dealing with is not in the current
  612. * "hot add block", move on.
  613. */
  614. if ((start_pfn >= has->end_pfn))
  615. continue;
  616. old_covered_state = has->covered_end_pfn;
  617. if (start_pfn < has->ha_end_pfn) {
  618. /*
  619. * This is the case where we are backing pages
  620. * in an already hot added region. Bring
  621. * these pages online first.
  622. */
  623. pgs_ol = has->ha_end_pfn - start_pfn;
  624. if (pgs_ol > pfn_cnt)
  625. pgs_ol = pfn_cnt;
  626. hv_bring_pgs_online(start_pfn, pgs_ol);
  627. has->covered_end_pfn += pgs_ol;
  628. has->covered_start_pfn += pgs_ol;
  629. pfn_cnt -= pgs_ol;
  630. }
  631. if ((has->ha_end_pfn < has->end_pfn) && (pfn_cnt > 0)) {
  632. /*
  633. * We have some residual hot add range
  634. * that needs to be hot added; hot add
  635. * it now. Hot add a multiple of
  636. * of HA_CHUNK that fully covers the pages
  637. * we have.
  638. */
  639. size = (has->end_pfn - has->ha_end_pfn);
  640. if (pfn_cnt <= size) {
  641. size = ((pfn_cnt / HA_CHUNK) * HA_CHUNK);
  642. if (pfn_cnt % HA_CHUNK)
  643. size += HA_CHUNK;
  644. } else {
  645. pfn_cnt = size;
  646. }
  647. hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
  648. }
  649. /*
  650. * If we managed to online any pages that were given to us,
  651. * we declare success.
  652. */
  653. return has->covered_end_pfn - old_covered_state;
  654. }
  655. return 0;
  656. }
  657. static unsigned long process_hot_add(unsigned long pg_start,
  658. unsigned long pfn_cnt,
  659. unsigned long rg_start,
  660. unsigned long rg_size)
  661. {
  662. struct hv_hotadd_state *ha_region = NULL;
  663. if (pfn_cnt == 0)
  664. return 0;
  665. if (!dm_device.host_specified_ha_region)
  666. if (pfn_covered(pg_start, pfn_cnt))
  667. goto do_pg_range;
  668. /*
  669. * If the host has specified a hot-add range; deal with it first.
  670. */
  671. if (rg_size != 0) {
  672. ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
  673. if (!ha_region)
  674. return 0;
  675. INIT_LIST_HEAD(&ha_region->list);
  676. list_add_tail(&ha_region->list, &dm_device.ha_region_list);
  677. ha_region->start_pfn = rg_start;
  678. ha_region->ha_end_pfn = rg_start;
  679. ha_region->covered_start_pfn = pg_start;
  680. ha_region->covered_end_pfn = pg_start;
  681. ha_region->end_pfn = rg_start + rg_size;
  682. }
  683. do_pg_range:
  684. /*
  685. * Process the page range specified; bringing them
  686. * online if possible.
  687. */
  688. return handle_pg_range(pg_start, pfn_cnt);
  689. }
  690. #endif
  691. static void hot_add_req(struct work_struct *dummy)
  692. {
  693. struct dm_hot_add_response resp;
  694. #ifdef CONFIG_MEMORY_HOTPLUG
  695. unsigned long pg_start, pfn_cnt;
  696. unsigned long rg_start, rg_sz;
  697. #endif
  698. struct hv_dynmem_device *dm = &dm_device;
  699. memset(&resp, 0, sizeof(struct dm_hot_add_response));
  700. resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
  701. resp.hdr.size = sizeof(struct dm_hot_add_response);
  702. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  703. #ifdef CONFIG_MEMORY_HOTPLUG
  704. pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
  705. pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
  706. rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
  707. rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
  708. if ((rg_start == 0) && (!dm->host_specified_ha_region)) {
  709. unsigned long region_size;
  710. unsigned long region_start;
  711. /*
  712. * The host has not specified the hot-add region.
  713. * Based on the hot-add page range being specified,
  714. * compute a hot-add region that can cover the pages
  715. * that need to be hot-added while ensuring the alignment
  716. * and size requirements of Linux as it relates to hot-add.
  717. */
  718. region_start = pg_start;
  719. region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK;
  720. if (pfn_cnt % HA_CHUNK)
  721. region_size += HA_CHUNK;
  722. region_start = (pg_start / HA_CHUNK) * HA_CHUNK;
  723. rg_start = region_start;
  724. rg_sz = region_size;
  725. }
  726. if (do_hot_add)
  727. resp.page_count = process_hot_add(pg_start, pfn_cnt,
  728. rg_start, rg_sz);
  729. #endif
  730. /*
  731. * The result field of the response structure has the
  732. * following semantics:
  733. *
  734. * 1. If all or some pages hot-added: Guest should return success.
  735. *
  736. * 2. If no pages could be hot-added:
  737. *
  738. * If the guest returns success, then the host
  739. * will not attempt any further hot-add operations. This
  740. * signifies a permanent failure.
  741. *
  742. * If the guest returns failure, then this failure will be
  743. * treated as a transient failure and the host may retry the
  744. * hot-add operation after some delay.
  745. */
  746. if (resp.page_count > 0)
  747. resp.result = 1;
  748. else if (!do_hot_add)
  749. resp.result = 1;
  750. else
  751. resp.result = 0;
  752. if (!do_hot_add || (resp.page_count == 0))
  753. pr_info("Memory hot add failed\n");
  754. dm->state = DM_INITIALIZED;
  755. vmbus_sendpacket(dm->dev->channel, &resp,
  756. sizeof(struct dm_hot_add_response),
  757. (unsigned long)NULL,
  758. VM_PKT_DATA_INBAND, 0);
  759. }
  760. static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
  761. {
  762. struct dm_info_header *info_hdr;
  763. info_hdr = (struct dm_info_header *)msg->info;
  764. switch (info_hdr->type) {
  765. case INFO_TYPE_MAX_PAGE_CNT:
  766. pr_info("Received INFO_TYPE_MAX_PAGE_CNT\n");
  767. pr_info("Data Size is %d\n", info_hdr->data_size);
  768. break;
  769. default:
  770. pr_info("Received Unknown type: %d\n", info_hdr->type);
  771. }
  772. }
  773. static unsigned long compute_balloon_floor(void)
  774. {
  775. unsigned long min_pages;
  776. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  777. /* Simple continuous piecewiese linear function:
  778. * max MiB -> min MiB gradient
  779. * 0 0
  780. * 16 16
  781. * 32 24
  782. * 128 72 (1/2)
  783. * 512 168 (1/4)
  784. * 2048 360 (1/8)
  785. * 8192 552 (1/32)
  786. * 32768 1320
  787. * 131072 4392
  788. */
  789. if (totalram_pages < MB2PAGES(128))
  790. min_pages = MB2PAGES(8) + (totalram_pages >> 1);
  791. else if (totalram_pages < MB2PAGES(512))
  792. min_pages = MB2PAGES(40) + (totalram_pages >> 2);
  793. else if (totalram_pages < MB2PAGES(2048))
  794. min_pages = MB2PAGES(104) + (totalram_pages >> 3);
  795. else
  796. min_pages = MB2PAGES(296) + (totalram_pages >> 5);
  797. #undef MB2PAGES
  798. return min_pages;
  799. }
  800. /*
  801. * Post our status as it relates memory pressure to the
  802. * host. Host expects the guests to post this status
  803. * periodically at 1 second intervals.
  804. *
  805. * The metrics specified in this protocol are very Windows
  806. * specific and so we cook up numbers here to convey our memory
  807. * pressure.
  808. */
  809. static void post_status(struct hv_dynmem_device *dm)
  810. {
  811. struct dm_status status;
  812. struct sysinfo val;
  813. if (pressure_report_delay > 0) {
  814. --pressure_report_delay;
  815. return;
  816. }
  817. si_meminfo(&val);
  818. memset(&status, 0, sizeof(struct dm_status));
  819. status.hdr.type = DM_STATUS_REPORT;
  820. status.hdr.size = sizeof(struct dm_status);
  821. status.hdr.trans_id = atomic_inc_return(&trans_id);
  822. /*
  823. * The host expects the guest to report free memory.
  824. * Further, the host expects the pressure information to
  825. * include the ballooned out pages.
  826. * For a given amount of memory that we are managing, we
  827. * need to compute a floor below which we should not balloon.
  828. * Compute this and add it to the pressure report.
  829. */
  830. status.num_avail = val.freeram;
  831. status.num_committed = vm_memory_committed() +
  832. dm->num_pages_ballooned +
  833. compute_balloon_floor();
  834. vmbus_sendpacket(dm->dev->channel, &status,
  835. sizeof(struct dm_status),
  836. (unsigned long)NULL,
  837. VM_PKT_DATA_INBAND, 0);
  838. }
  839. static void free_balloon_pages(struct hv_dynmem_device *dm,
  840. union dm_mem_page_range *range_array)
  841. {
  842. int num_pages = range_array->finfo.page_cnt;
  843. __u64 start_frame = range_array->finfo.start_page;
  844. struct page *pg;
  845. int i;
  846. for (i = 0; i < num_pages; i++) {
  847. pg = pfn_to_page(i + start_frame);
  848. __free_page(pg);
  849. dm->num_pages_ballooned--;
  850. }
  851. }
  852. static int alloc_balloon_pages(struct hv_dynmem_device *dm, int num_pages,
  853. struct dm_balloon_response *bl_resp, int alloc_unit,
  854. bool *alloc_error)
  855. {
  856. int i = 0;
  857. struct page *pg;
  858. if (num_pages < alloc_unit)
  859. return 0;
  860. for (i = 0; (i * alloc_unit) < num_pages; i++) {
  861. if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
  862. PAGE_SIZE)
  863. return i * alloc_unit;
  864. /*
  865. * We execute this code in a thread context. Furthermore,
  866. * we don't want the kernel to try too hard.
  867. */
  868. pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
  869. __GFP_NOMEMALLOC | __GFP_NOWARN,
  870. get_order(alloc_unit << PAGE_SHIFT));
  871. if (!pg) {
  872. *alloc_error = true;
  873. return i * alloc_unit;
  874. }
  875. dm->num_pages_ballooned += alloc_unit;
  876. /*
  877. * If we allocatted 2M pages; split them so we
  878. * can free them in any order we get.
  879. */
  880. if (alloc_unit != 1)
  881. split_page(pg, get_order(alloc_unit << PAGE_SHIFT));
  882. bl_resp->range_count++;
  883. bl_resp->range_array[i].finfo.start_page =
  884. page_to_pfn(pg);
  885. bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
  886. bl_resp->hdr.size += sizeof(union dm_mem_page_range);
  887. }
  888. return num_pages;
  889. }
  890. static void balloon_up(struct work_struct *dummy)
  891. {
  892. int num_pages = dm_device.balloon_wrk.num_pages;
  893. int num_ballooned = 0;
  894. struct dm_balloon_response *bl_resp;
  895. int alloc_unit;
  896. int ret;
  897. bool alloc_error = false;
  898. bool done = false;
  899. int i;
  900. /*
  901. * We will attempt 2M allocations. However, if we fail to
  902. * allocate 2M chunks, we will go back to 4k allocations.
  903. */
  904. alloc_unit = 512;
  905. while (!done) {
  906. bl_resp = (struct dm_balloon_response *)send_buffer;
  907. memset(send_buffer, 0, PAGE_SIZE);
  908. bl_resp->hdr.type = DM_BALLOON_RESPONSE;
  909. bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
  910. bl_resp->hdr.size = sizeof(struct dm_balloon_response);
  911. bl_resp->more_pages = 1;
  912. num_pages -= num_ballooned;
  913. num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
  914. bl_resp, alloc_unit,
  915. &alloc_error);
  916. if ((alloc_error) && (alloc_unit != 1)) {
  917. alloc_unit = 1;
  918. continue;
  919. }
  920. if ((alloc_error) || (num_ballooned == num_pages)) {
  921. bl_resp->more_pages = 0;
  922. done = true;
  923. dm_device.state = DM_INITIALIZED;
  924. }
  925. /*
  926. * We are pushing a lot of data through the channel;
  927. * deal with transient failures caused because of the
  928. * lack of space in the ring buffer.
  929. */
  930. do {
  931. ret = vmbus_sendpacket(dm_device.dev->channel,
  932. bl_resp,
  933. bl_resp->hdr.size,
  934. (unsigned long)NULL,
  935. VM_PKT_DATA_INBAND, 0);
  936. if (ret == -EAGAIN)
  937. msleep(20);
  938. } while (ret == -EAGAIN);
  939. if (ret) {
  940. /*
  941. * Free up the memory we allocatted.
  942. */
  943. pr_info("Balloon response failed\n");
  944. for (i = 0; i < bl_resp->range_count; i++)
  945. free_balloon_pages(&dm_device,
  946. &bl_resp->range_array[i]);
  947. done = true;
  948. }
  949. }
  950. }
  951. static void balloon_down(struct hv_dynmem_device *dm,
  952. struct dm_unballoon_request *req)
  953. {
  954. union dm_mem_page_range *range_array = req->range_array;
  955. int range_count = req->range_count;
  956. struct dm_unballoon_response resp;
  957. int i;
  958. for (i = 0; i < range_count; i++)
  959. free_balloon_pages(dm, &range_array[i]);
  960. if (req->more_pages == 1)
  961. return;
  962. memset(&resp, 0, sizeof(struct dm_unballoon_response));
  963. resp.hdr.type = DM_UNBALLOON_RESPONSE;
  964. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  965. resp.hdr.size = sizeof(struct dm_unballoon_response);
  966. vmbus_sendpacket(dm_device.dev->channel, &resp,
  967. sizeof(struct dm_unballoon_response),
  968. (unsigned long)NULL,
  969. VM_PKT_DATA_INBAND, 0);
  970. dm->state = DM_INITIALIZED;
  971. }
  972. static void balloon_onchannelcallback(void *context);
  973. static int dm_thread_func(void *dm_dev)
  974. {
  975. struct hv_dynmem_device *dm = dm_dev;
  976. int t;
  977. while (!kthread_should_stop()) {
  978. t = wait_for_completion_timeout(&dm_device.config_event, 1*HZ);
  979. /*
  980. * The host expects us to post information on the memory
  981. * pressure every second.
  982. */
  983. if (t == 0)
  984. post_status(dm);
  985. }
  986. return 0;
  987. }
  988. static void version_resp(struct hv_dynmem_device *dm,
  989. struct dm_version_response *vresp)
  990. {
  991. struct dm_version_request version_req;
  992. int ret;
  993. if (vresp->is_accepted) {
  994. /*
  995. * We are done; wakeup the
  996. * context waiting for version
  997. * negotiation.
  998. */
  999. complete(&dm->host_event);
  1000. return;
  1001. }
  1002. /*
  1003. * If there are more versions to try, continue
  1004. * with negotiations; if not
  1005. * shutdown the service since we are not able
  1006. * to negotiate a suitable version number
  1007. * with the host.
  1008. */
  1009. if (dm->next_version == 0)
  1010. goto version_error;
  1011. dm->next_version = 0;
  1012. memset(&version_req, 0, sizeof(struct dm_version_request));
  1013. version_req.hdr.type = DM_VERSION_REQUEST;
  1014. version_req.hdr.size = sizeof(struct dm_version_request);
  1015. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1016. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN7;
  1017. version_req.is_last_attempt = 1;
  1018. ret = vmbus_sendpacket(dm->dev->channel, &version_req,
  1019. sizeof(struct dm_version_request),
  1020. (unsigned long)NULL,
  1021. VM_PKT_DATA_INBAND, 0);
  1022. if (ret)
  1023. goto version_error;
  1024. return;
  1025. version_error:
  1026. dm->state = DM_INIT_ERROR;
  1027. complete(&dm->host_event);
  1028. }
  1029. static void cap_resp(struct hv_dynmem_device *dm,
  1030. struct dm_capabilities_resp_msg *cap_resp)
  1031. {
  1032. if (!cap_resp->is_accepted) {
  1033. pr_info("Capabilities not accepted by host\n");
  1034. dm->state = DM_INIT_ERROR;
  1035. }
  1036. complete(&dm->host_event);
  1037. }
  1038. static void balloon_onchannelcallback(void *context)
  1039. {
  1040. struct hv_device *dev = context;
  1041. u32 recvlen;
  1042. u64 requestid;
  1043. struct dm_message *dm_msg;
  1044. struct dm_header *dm_hdr;
  1045. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1046. struct dm_balloon *bal_msg;
  1047. struct dm_hot_add *ha_msg;
  1048. union dm_mem_page_range *ha_pg_range;
  1049. union dm_mem_page_range *ha_region;
  1050. memset(recv_buffer, 0, sizeof(recv_buffer));
  1051. vmbus_recvpacket(dev->channel, recv_buffer,
  1052. PAGE_SIZE, &recvlen, &requestid);
  1053. if (recvlen > 0) {
  1054. dm_msg = (struct dm_message *)recv_buffer;
  1055. dm_hdr = &dm_msg->hdr;
  1056. switch (dm_hdr->type) {
  1057. case DM_VERSION_RESPONSE:
  1058. version_resp(dm,
  1059. (struct dm_version_response *)dm_msg);
  1060. break;
  1061. case DM_CAPABILITIES_RESPONSE:
  1062. cap_resp(dm,
  1063. (struct dm_capabilities_resp_msg *)dm_msg);
  1064. break;
  1065. case DM_BALLOON_REQUEST:
  1066. if (dm->state == DM_BALLOON_UP)
  1067. pr_warn("Currently ballooning\n");
  1068. bal_msg = (struct dm_balloon *)recv_buffer;
  1069. dm->state = DM_BALLOON_UP;
  1070. dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
  1071. schedule_work(&dm_device.balloon_wrk.wrk);
  1072. break;
  1073. case DM_UNBALLOON_REQUEST:
  1074. dm->state = DM_BALLOON_DOWN;
  1075. balloon_down(dm,
  1076. (struct dm_unballoon_request *)recv_buffer);
  1077. break;
  1078. case DM_MEM_HOT_ADD_REQUEST:
  1079. if (dm->state == DM_HOT_ADD)
  1080. pr_warn("Currently hot-adding\n");
  1081. dm->state = DM_HOT_ADD;
  1082. ha_msg = (struct dm_hot_add *)recv_buffer;
  1083. if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
  1084. /*
  1085. * This is a normal hot-add request specifying
  1086. * hot-add memory.
  1087. */
  1088. ha_pg_range = &ha_msg->range;
  1089. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1090. dm->ha_wrk.ha_region_range.page_range = 0;
  1091. } else {
  1092. /*
  1093. * Host is specifying that we first hot-add
  1094. * a region and then partially populate this
  1095. * region.
  1096. */
  1097. dm->host_specified_ha_region = true;
  1098. ha_pg_range = &ha_msg->range;
  1099. ha_region = &ha_pg_range[1];
  1100. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1101. dm->ha_wrk.ha_region_range = *ha_region;
  1102. }
  1103. schedule_work(&dm_device.ha_wrk.wrk);
  1104. break;
  1105. case DM_INFO_MESSAGE:
  1106. process_info(dm, (struct dm_info_msg *)dm_msg);
  1107. break;
  1108. default:
  1109. pr_err("Unhandled message: type: %d\n", dm_hdr->type);
  1110. }
  1111. }
  1112. }
  1113. static int balloon_probe(struct hv_device *dev,
  1114. const struct hv_vmbus_device_id *dev_id)
  1115. {
  1116. int ret, t;
  1117. struct dm_version_request version_req;
  1118. struct dm_capabilities cap_msg;
  1119. do_hot_add = hot_add;
  1120. /*
  1121. * First allocate a send buffer.
  1122. */
  1123. send_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1124. if (!send_buffer)
  1125. return -ENOMEM;
  1126. ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
  1127. balloon_onchannelcallback, dev);
  1128. if (ret)
  1129. goto probe_error0;
  1130. dm_device.dev = dev;
  1131. dm_device.state = DM_INITIALIZING;
  1132. dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
  1133. init_completion(&dm_device.host_event);
  1134. init_completion(&dm_device.config_event);
  1135. INIT_LIST_HEAD(&dm_device.ha_region_list);
  1136. INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
  1137. INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
  1138. dm_device.host_specified_ha_region = false;
  1139. dm_device.thread =
  1140. kthread_run(dm_thread_func, &dm_device, "hv_balloon");
  1141. if (IS_ERR(dm_device.thread)) {
  1142. ret = PTR_ERR(dm_device.thread);
  1143. goto probe_error1;
  1144. }
  1145. #ifdef CONFIG_MEMORY_HOTPLUG
  1146. set_online_page_callback(&hv_online_page);
  1147. #endif
  1148. hv_set_drvdata(dev, &dm_device);
  1149. /*
  1150. * Initiate the hand shake with the host and negotiate
  1151. * a version that the host can support. We start with the
  1152. * highest version number and go down if the host cannot
  1153. * support it.
  1154. */
  1155. memset(&version_req, 0, sizeof(struct dm_version_request));
  1156. version_req.hdr.type = DM_VERSION_REQUEST;
  1157. version_req.hdr.size = sizeof(struct dm_version_request);
  1158. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1159. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN8;
  1160. version_req.is_last_attempt = 0;
  1161. ret = vmbus_sendpacket(dev->channel, &version_req,
  1162. sizeof(struct dm_version_request),
  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 we could not negotiate a compatible version with the host
  1174. * fail the probe function.
  1175. */
  1176. if (dm_device.state == DM_INIT_ERROR) {
  1177. ret = -ETIMEDOUT;
  1178. goto probe_error2;
  1179. }
  1180. /*
  1181. * Now submit our capabilities to the host.
  1182. */
  1183. memset(&cap_msg, 0, sizeof(struct dm_capabilities));
  1184. cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
  1185. cap_msg.hdr.size = sizeof(struct dm_capabilities);
  1186. cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
  1187. cap_msg.caps.cap_bits.balloon = 1;
  1188. cap_msg.caps.cap_bits.hot_add = 1;
  1189. /*
  1190. * Specify our alignment requirements as it relates
  1191. * memory hot-add. Specify 128MB alignment.
  1192. */
  1193. cap_msg.caps.cap_bits.hot_add_alignment = 7;
  1194. /*
  1195. * Currently the host does not use these
  1196. * values and we set them to what is done in the
  1197. * Windows driver.
  1198. */
  1199. cap_msg.min_page_cnt = 0;
  1200. cap_msg.max_page_number = -1;
  1201. ret = vmbus_sendpacket(dev->channel, &cap_msg,
  1202. sizeof(struct dm_capabilities),
  1203. (unsigned long)NULL,
  1204. VM_PKT_DATA_INBAND, 0);
  1205. if (ret)
  1206. goto probe_error2;
  1207. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  1208. if (t == 0) {
  1209. ret = -ETIMEDOUT;
  1210. goto probe_error2;
  1211. }
  1212. /*
  1213. * If the host does not like our capabilities,
  1214. * fail the probe function.
  1215. */
  1216. if (dm_device.state == DM_INIT_ERROR) {
  1217. ret = -ETIMEDOUT;
  1218. goto probe_error2;
  1219. }
  1220. dm_device.state = DM_INITIALIZED;
  1221. return 0;
  1222. probe_error2:
  1223. #ifdef CONFIG_MEMORY_HOTPLUG
  1224. restore_online_page_callback(&hv_online_page);
  1225. #endif
  1226. kthread_stop(dm_device.thread);
  1227. probe_error1:
  1228. vmbus_close(dev->channel);
  1229. probe_error0:
  1230. kfree(send_buffer);
  1231. return ret;
  1232. }
  1233. static int balloon_remove(struct hv_device *dev)
  1234. {
  1235. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1236. struct list_head *cur, *tmp;
  1237. struct hv_hotadd_state *has;
  1238. if (dm->num_pages_ballooned != 0)
  1239. pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
  1240. cancel_work_sync(&dm->balloon_wrk.wrk);
  1241. cancel_work_sync(&dm->ha_wrk.wrk);
  1242. vmbus_close(dev->channel);
  1243. kthread_stop(dm->thread);
  1244. kfree(send_buffer);
  1245. #ifdef CONFIG_MEMORY_HOTPLUG
  1246. restore_online_page_callback(&hv_online_page);
  1247. #endif
  1248. list_for_each_safe(cur, tmp, &dm->ha_region_list) {
  1249. has = list_entry(cur, struct hv_hotadd_state, list);
  1250. list_del(&has->list);
  1251. kfree(has);
  1252. }
  1253. return 0;
  1254. }
  1255. static const struct hv_vmbus_device_id id_table[] = {
  1256. /* Dynamic Memory Class ID */
  1257. /* 525074DC-8985-46e2-8057-A307DC18A502 */
  1258. { HV_DM_GUID, },
  1259. { },
  1260. };
  1261. MODULE_DEVICE_TABLE(vmbus, id_table);
  1262. static struct hv_driver balloon_drv = {
  1263. .name = "hv_balloon",
  1264. .id_table = id_table,
  1265. .probe = balloon_probe,
  1266. .remove = balloon_remove,
  1267. };
  1268. static int __init init_balloon_drv(void)
  1269. {
  1270. return vmbus_driver_register(&balloon_drv);
  1271. }
  1272. module_init(init_balloon_drv);
  1273. MODULE_DESCRIPTION("Hyper-V Balloon");
  1274. MODULE_VERSION(HV_DRV_VERSION);
  1275. MODULE_LICENSE("GPL");