hv_balloon.c 42 KB

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