hv_balloon.c 34 KB

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