hv_balloon.c 36 KB

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