hv_balloon.c 35 KB

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