hv_balloon.c 37 KB

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