hv_balloon.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012, Microsoft Corporation.
  4. *
  5. * Author:
  6. * K. Y. Srinivasan <kys@microsoft.com>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/cleanup.h>
  10. #include <linux/kernel.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/mman.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/kthread.h>
  19. #include <linux/completion.h>
  20. #include <linux/count_zeros.h>
  21. #include <linux/memory_hotplug.h>
  22. #include <linux/memory.h>
  23. #include <linux/notifier.h>
  24. #include <linux/percpu_counter.h>
  25. #include <linux/page_reporting.h>
  26. #include <linux/sizes.h>
  27. #include <linux/hyperv.h>
  28. #include <asm/hyperv-tlfs.h>
  29. #include <asm/mshyperv.h>
  30. #define CREATE_TRACE_POINTS
  31. #include "hv_trace_balloon.h"
  32. /*
  33. * We begin with definitions supporting the Dynamic Memory protocol
  34. * with the host.
  35. *
  36. * Begin protocol definitions.
  37. */
  38. /*
  39. * Protocol versions. The low word is the minor version, the high word the major
  40. * version.
  41. *
  42. * History:
  43. * Initial version 1.0
  44. * Changed to 0.1 on 2009/03/25
  45. * Changes to 0.2 on 2009/05/14
  46. * Changes to 0.3 on 2009/12/03
  47. * Changed to 1.0 on 2011/04/05
  48. */
  49. #define DYNMEM_MAKE_VERSION(Major, Minor) ((__u32)(((Major) << 16) | (Minor)))
  50. #define DYNMEM_MAJOR_VERSION(Version) ((__u32)(Version) >> 16)
  51. #define DYNMEM_MINOR_VERSION(Version) ((__u32)(Version) & 0xff)
  52. enum {
  53. DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
  54. DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
  55. DYNMEM_PROTOCOL_VERSION_3 = DYNMEM_MAKE_VERSION(2, 0),
  56. DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
  57. DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
  58. DYNMEM_PROTOCOL_VERSION_WIN10 = DYNMEM_PROTOCOL_VERSION_3,
  59. DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN10
  60. };
  61. /*
  62. * Message Types
  63. */
  64. enum dm_message_type {
  65. /*
  66. * Version 0.3
  67. */
  68. DM_ERROR = 0,
  69. DM_VERSION_REQUEST = 1,
  70. DM_VERSION_RESPONSE = 2,
  71. DM_CAPABILITIES_REPORT = 3,
  72. DM_CAPABILITIES_RESPONSE = 4,
  73. DM_STATUS_REPORT = 5,
  74. DM_BALLOON_REQUEST = 6,
  75. DM_BALLOON_RESPONSE = 7,
  76. DM_UNBALLOON_REQUEST = 8,
  77. DM_UNBALLOON_RESPONSE = 9,
  78. DM_MEM_HOT_ADD_REQUEST = 10,
  79. DM_MEM_HOT_ADD_RESPONSE = 11,
  80. DM_VERSION_03_MAX = 11,
  81. /*
  82. * Version 1.0.
  83. */
  84. DM_INFO_MESSAGE = 12,
  85. DM_VERSION_1_MAX = 12
  86. };
  87. /*
  88. * Structures defining the dynamic memory management
  89. * protocol.
  90. */
  91. union dm_version {
  92. struct {
  93. __u16 minor_version;
  94. __u16 major_version;
  95. };
  96. __u32 version;
  97. } __packed;
  98. union dm_caps {
  99. struct {
  100. __u64 balloon:1;
  101. __u64 hot_add:1;
  102. /*
  103. * To support guests that may have alignment
  104. * limitations on hot-add, the guest can specify
  105. * its alignment requirements; a value of n
  106. * represents an alignment of 2^n in mega bytes.
  107. */
  108. __u64 hot_add_alignment:4;
  109. __u64 reservedz:58;
  110. } cap_bits;
  111. __u64 caps;
  112. } __packed;
  113. union dm_mem_page_range {
  114. struct {
  115. /*
  116. * The PFN number of the first page in the range.
  117. * 40 bits is the architectural limit of a PFN
  118. * number for AMD64.
  119. */
  120. __u64 start_page:40;
  121. /*
  122. * The number of pages in the range.
  123. */
  124. __u64 page_cnt:24;
  125. } finfo;
  126. __u64 page_range;
  127. } __packed;
  128. /*
  129. * The header for all dynamic memory messages:
  130. *
  131. * type: Type of the message.
  132. * size: Size of the message in bytes; including the header.
  133. * trans_id: The guest is responsible for manufacturing this ID.
  134. */
  135. struct dm_header {
  136. __u16 type;
  137. __u16 size;
  138. __u32 trans_id;
  139. } __packed;
  140. /*
  141. * A generic message format for dynamic memory.
  142. * Specific message formats are defined later in the file.
  143. */
  144. struct dm_message {
  145. struct dm_header hdr;
  146. __u8 data[]; /* enclosed message */
  147. } __packed;
  148. /*
  149. * Specific message types supporting the dynamic memory protocol.
  150. */
  151. /*
  152. * Version negotiation message. Sent from the guest to the host.
  153. * The guest is free to try different versions until the host
  154. * accepts the version.
  155. *
  156. * dm_version: The protocol version requested.
  157. * is_last_attempt: If TRUE, this is the last version guest will request.
  158. * reservedz: Reserved field, set to zero.
  159. */
  160. struct dm_version_request {
  161. struct dm_header hdr;
  162. union dm_version version;
  163. __u32 is_last_attempt:1;
  164. __u32 reservedz:31;
  165. } __packed;
  166. /*
  167. * Version response message; Host to Guest and indicates
  168. * if the host has accepted the version sent by the guest.
  169. *
  170. * is_accepted: If TRUE, host has accepted the version and the guest
  171. * should proceed to the next stage of the protocol. FALSE indicates that
  172. * guest should re-try with a different version.
  173. *
  174. * reservedz: Reserved field, set to zero.
  175. */
  176. struct dm_version_response {
  177. struct dm_header hdr;
  178. __u64 is_accepted:1;
  179. __u64 reservedz:63;
  180. } __packed;
  181. /*
  182. * Message reporting capabilities. This is sent from the guest to the
  183. * host.
  184. */
  185. struct dm_capabilities {
  186. struct dm_header hdr;
  187. union dm_caps caps;
  188. __u64 min_page_cnt;
  189. __u64 max_page_number;
  190. } __packed;
  191. /*
  192. * Response to the capabilities message. This is sent from the host to the
  193. * guest. This message notifies if the host has accepted the guest's
  194. * capabilities. If the host has not accepted, the guest must shutdown
  195. * the service.
  196. *
  197. * is_accepted: Indicates if the host has accepted guest's capabilities.
  198. * reservedz: Must be 0.
  199. */
  200. struct dm_capabilities_resp_msg {
  201. struct dm_header hdr;
  202. __u64 is_accepted:1;
  203. __u64 reservedz:63;
  204. } __packed;
  205. /*
  206. * This message is used to report memory pressure from the guest.
  207. * This message is not part of any transaction and there is no
  208. * response to this message.
  209. *
  210. * num_avail: Available memory in pages.
  211. * num_committed: Committed memory in pages.
  212. * page_file_size: The accumulated size of all page files
  213. * in the system in pages.
  214. * zero_free: The number of zero and free pages.
  215. * page_file_writes: The writes to the page file in pages.
  216. * io_diff: An indicator of file cache efficiency or page file activity,
  217. * calculated as File Cache Page Fault Count - Page Read Count.
  218. * This value is in pages.
  219. *
  220. * Some of these metrics are Windows specific and fortunately
  221. * the algorithm on the host side that computes the guest memory
  222. * pressure only uses num_committed value.
  223. */
  224. struct dm_status {
  225. struct dm_header hdr;
  226. __u64 num_avail;
  227. __u64 num_committed;
  228. __u64 page_file_size;
  229. __u64 zero_free;
  230. __u32 page_file_writes;
  231. __u32 io_diff;
  232. } __packed;
  233. /*
  234. * Message to ask the guest to allocate memory - balloon up message.
  235. * This message is sent from the host to the guest. The guest may not be
  236. * able to allocate as much memory as requested.
  237. *
  238. * num_pages: number of pages to allocate.
  239. */
  240. struct dm_balloon {
  241. struct dm_header hdr;
  242. __u32 num_pages;
  243. __u32 reservedz;
  244. } __packed;
  245. /*
  246. * Balloon response message; this message is sent from the guest
  247. * to the host in response to the balloon message.
  248. *
  249. * reservedz: Reserved; must be set to zero.
  250. * more_pages: If FALSE, this is the last message of the transaction.
  251. * if TRUE there will be at least one more message from the guest.
  252. *
  253. * range_count: The number of ranges in the range array.
  254. *
  255. * range_array: An array of page ranges returned to the host.
  256. *
  257. */
  258. struct dm_balloon_response {
  259. struct dm_header hdr;
  260. __u32 reservedz;
  261. __u32 more_pages:1;
  262. __u32 range_count:31;
  263. union dm_mem_page_range range_array[];
  264. } __packed;
  265. /*
  266. * Un-balloon message; this message is sent from the host
  267. * to the guest to give guest more memory.
  268. *
  269. * more_pages: If FALSE, this is the last message of the transaction.
  270. * if TRUE there will be at least one more message from the guest.
  271. *
  272. * reservedz: Reserved; must be set to zero.
  273. *
  274. * range_count: The number of ranges in the range array.
  275. *
  276. * range_array: An array of page ranges returned to the host.
  277. *
  278. */
  279. struct dm_unballoon_request {
  280. struct dm_header hdr;
  281. __u32 more_pages:1;
  282. __u32 reservedz:31;
  283. __u32 range_count;
  284. union dm_mem_page_range range_array[];
  285. } __packed;
  286. /*
  287. * Un-balloon response message; this message is sent from the guest
  288. * to the host in response to an unballoon request.
  289. *
  290. */
  291. struct dm_unballoon_response {
  292. struct dm_header hdr;
  293. } __packed;
  294. /*
  295. * Hot add request message. Message sent from the host to the guest.
  296. *
  297. * mem_range: Memory range to hot add.
  298. *
  299. */
  300. struct dm_hot_add {
  301. struct dm_header hdr;
  302. union dm_mem_page_range range;
  303. } __packed;
  304. /*
  305. * Hot add response message.
  306. * This message is sent by the guest to report the status of a hot add request.
  307. * If page_count is less than the requested page count, then the host should
  308. * assume all further hot add requests will fail, since this indicates that
  309. * the guest has hit an upper physical memory barrier.
  310. *
  311. * Hot adds may also fail due to low resources; in this case, the guest must
  312. * not complete this message until the hot add can succeed, and the host must
  313. * not send a new hot add request until the response is sent.
  314. * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
  315. * times it fails the request.
  316. *
  317. *
  318. * page_count: number of pages that were successfully hot added.
  319. *
  320. * result: result of the operation 1: success, 0: failure.
  321. *
  322. */
  323. struct dm_hot_add_response {
  324. struct dm_header hdr;
  325. __u32 page_count;
  326. __u32 result;
  327. } __packed;
  328. /*
  329. * Types of information sent from host to the guest.
  330. */
  331. enum dm_info_type {
  332. INFO_TYPE_MAX_PAGE_CNT = 0,
  333. MAX_INFO_TYPE
  334. };
  335. /*
  336. * Header for the information message.
  337. */
  338. struct dm_info_header {
  339. enum dm_info_type type;
  340. __u32 data_size;
  341. } __packed;
  342. /*
  343. * This message is sent from the host to the guest to pass
  344. * some relevant information (win8 addition).
  345. *
  346. * reserved: no used.
  347. * info_size: size of the information blob.
  348. * info: information blob.
  349. */
  350. struct dm_info_msg {
  351. struct dm_header hdr;
  352. __u32 reserved;
  353. __u32 info_size;
  354. __u8 info[];
  355. };
  356. /*
  357. * End protocol definitions.
  358. */
  359. /*
  360. * State to manage hot adding memory into the guest.
  361. * The range start_pfn : end_pfn specifies the range
  362. * that the host has asked us to hot add. The range
  363. * start_pfn : ha_end_pfn specifies the range that we have
  364. * currently hot added. We hot add in chunks equal to the
  365. * memory block size; it is possible that we may not be able
  366. * to bring online all the pages in the region. The range
  367. * covered_start_pfn:covered_end_pfn defines the pages that can
  368. * be brought online.
  369. */
  370. struct hv_hotadd_state {
  371. struct list_head list;
  372. unsigned long start_pfn;
  373. unsigned long covered_start_pfn;
  374. unsigned long covered_end_pfn;
  375. unsigned long ha_end_pfn;
  376. unsigned long end_pfn;
  377. /*
  378. * A list of gaps.
  379. */
  380. struct list_head gap_list;
  381. };
  382. struct hv_hotadd_gap {
  383. struct list_head list;
  384. unsigned long start_pfn;
  385. unsigned long end_pfn;
  386. };
  387. struct balloon_state {
  388. __u32 num_pages;
  389. struct work_struct wrk;
  390. };
  391. struct hot_add_wrk {
  392. union dm_mem_page_range ha_page_range;
  393. union dm_mem_page_range ha_region_range;
  394. struct work_struct wrk;
  395. };
  396. static bool allow_hibernation;
  397. static bool hot_add = true;
  398. static bool do_hot_add;
  399. /*
  400. * Delay reporting memory pressure by
  401. * the specified number of seconds.
  402. */
  403. static uint pressure_report_delay = 45;
  404. extern unsigned int page_reporting_order;
  405. #define HV_MAX_FAILURES 2
  406. /*
  407. * The last time we posted a pressure report to host.
  408. */
  409. static unsigned long last_post_time;
  410. static int hv_hypercall_multi_failure;
  411. module_param(hot_add, bool, 0644);
  412. MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
  413. module_param(pressure_report_delay, uint, 0644);
  414. MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
  415. static atomic_t trans_id = ATOMIC_INIT(0);
  416. static int dm_ring_size = VMBUS_RING_SIZE(16 * 1024);
  417. /*
  418. * Driver specific state.
  419. */
  420. enum hv_dm_state {
  421. DM_INITIALIZING = 0,
  422. DM_INITIALIZED,
  423. DM_BALLOON_UP,
  424. DM_BALLOON_DOWN,
  425. DM_HOT_ADD,
  426. DM_INIT_ERROR
  427. };
  428. static __u8 recv_buffer[HV_HYP_PAGE_SIZE];
  429. static __u8 balloon_up_send_buffer[HV_HYP_PAGE_SIZE];
  430. static unsigned long ha_pages_in_chunk;
  431. #define HA_BYTES_IN_CHUNK (ha_pages_in_chunk << PAGE_SHIFT)
  432. #define PAGES_IN_2M (2 * 1024 * 1024 / PAGE_SIZE)
  433. struct hv_dynmem_device {
  434. struct hv_device *dev;
  435. enum hv_dm_state state;
  436. struct completion host_event;
  437. struct completion config_event;
  438. /*
  439. * Number of pages we have currently ballooned out.
  440. */
  441. unsigned int num_pages_ballooned;
  442. unsigned int num_pages_onlined;
  443. unsigned int num_pages_added;
  444. /*
  445. * State to manage the ballooning (up) operation.
  446. */
  447. struct balloon_state balloon_wrk;
  448. /*
  449. * State to execute the "hot-add" operation.
  450. */
  451. struct hot_add_wrk ha_wrk;
  452. /*
  453. * This state tracks if the host has specified a hot-add
  454. * region.
  455. */
  456. bool host_specified_ha_region;
  457. /*
  458. * State to synchronize hot-add.
  459. */
  460. struct completion ol_waitevent;
  461. /*
  462. * This thread handles hot-add
  463. * requests from the host as well as notifying
  464. * the host with regards to memory pressure in
  465. * the guest.
  466. */
  467. struct task_struct *thread;
  468. /*
  469. * Protects ha_region_list, num_pages_onlined counter and individual
  470. * regions from ha_region_list.
  471. */
  472. spinlock_t ha_lock;
  473. /*
  474. * A list of hot-add regions.
  475. */
  476. struct list_head ha_region_list;
  477. /*
  478. * We start with the highest version we can support
  479. * and downgrade based on the host; we save here the
  480. * next version to try.
  481. */
  482. __u32 next_version;
  483. /*
  484. * The negotiated version agreed by host.
  485. */
  486. __u32 version;
  487. struct page_reporting_dev_info pr_dev_info;
  488. /*
  489. * Maximum number of pages that can be hot_add-ed
  490. */
  491. __u64 max_dynamic_page_count;
  492. };
  493. static struct hv_dynmem_device dm_device;
  494. static void post_status(struct hv_dynmem_device *dm);
  495. static void enable_page_reporting(void);
  496. static void disable_page_reporting(void);
  497. #ifdef CONFIG_MEMORY_HOTPLUG
  498. static inline bool has_pfn_is_backed(struct hv_hotadd_state *has,
  499. unsigned long pfn)
  500. {
  501. struct hv_hotadd_gap *gap;
  502. /* The page is not backed. */
  503. if (pfn < has->covered_start_pfn || pfn >= has->covered_end_pfn)
  504. return false;
  505. /* Check for gaps. */
  506. list_for_each_entry(gap, &has->gap_list, list) {
  507. if (pfn >= gap->start_pfn && pfn < gap->end_pfn)
  508. return false;
  509. }
  510. return true;
  511. }
  512. static unsigned long hv_page_offline_check(unsigned long start_pfn,
  513. unsigned long nr_pages)
  514. {
  515. unsigned long pfn = start_pfn, count = 0;
  516. struct hv_hotadd_state *has;
  517. bool found;
  518. while (pfn < start_pfn + nr_pages) {
  519. /*
  520. * Search for HAS which covers the pfn and when we find one
  521. * count how many consequitive PFNs are covered.
  522. */
  523. found = false;
  524. list_for_each_entry(has, &dm_device.ha_region_list, list) {
  525. while ((pfn >= has->start_pfn) &&
  526. (pfn < has->end_pfn) &&
  527. (pfn < start_pfn + nr_pages)) {
  528. found = true;
  529. if (has_pfn_is_backed(has, pfn))
  530. count++;
  531. pfn++;
  532. }
  533. }
  534. /*
  535. * This PFN is not in any HAS (e.g. we're offlining a region
  536. * which was present at boot), no need to account for it. Go
  537. * to the next one.
  538. */
  539. if (!found)
  540. pfn++;
  541. }
  542. return count;
  543. }
  544. static int hv_memory_notifier(struct notifier_block *nb, unsigned long val,
  545. void *v)
  546. {
  547. struct memory_notify *mem = (struct memory_notify *)v;
  548. unsigned long pfn_count;
  549. switch (val) {
  550. case MEM_ONLINE:
  551. case MEM_CANCEL_ONLINE:
  552. complete(&dm_device.ol_waitevent);
  553. break;
  554. case MEM_OFFLINE:
  555. scoped_guard(spinlock_irqsave, &dm_device.ha_lock) {
  556. pfn_count = hv_page_offline_check(mem->start_pfn,
  557. mem->nr_pages);
  558. if (pfn_count <= dm_device.num_pages_onlined) {
  559. dm_device.num_pages_onlined -= pfn_count;
  560. } else {
  561. /*
  562. * We're offlining more pages than we
  563. * managed to online. This is
  564. * unexpected. In any case don't let
  565. * num_pages_onlined wrap around zero.
  566. */
  567. WARN_ON_ONCE(1);
  568. dm_device.num_pages_onlined = 0;
  569. }
  570. }
  571. break;
  572. case MEM_GOING_ONLINE:
  573. case MEM_GOING_OFFLINE:
  574. case MEM_CANCEL_OFFLINE:
  575. break;
  576. }
  577. return NOTIFY_OK;
  578. }
  579. static struct notifier_block hv_memory_nb = {
  580. .notifier_call = hv_memory_notifier,
  581. .priority = 0
  582. };
  583. /* Check if the particular page is backed and can be onlined and online it. */
  584. static void hv_page_online_one(struct hv_hotadd_state *has, struct page *pg)
  585. {
  586. if (!has_pfn_is_backed(has, page_to_pfn(pg))) {
  587. if (!PageOffline(pg))
  588. __SetPageOffline(pg);
  589. return;
  590. } else if (!PageOffline(pg))
  591. return;
  592. /* This frame is currently backed; online the page. */
  593. generic_online_page(pg, 0);
  594. lockdep_assert_held(&dm_device.ha_lock);
  595. dm_device.num_pages_onlined++;
  596. }
  597. static void hv_bring_pgs_online(struct hv_hotadd_state *has,
  598. unsigned long start_pfn, unsigned long size)
  599. {
  600. int i;
  601. pr_debug("Online %lu pages starting at pfn 0x%lx\n", size, start_pfn);
  602. for (i = 0; i < size; i++)
  603. hv_page_online_one(has, pfn_to_page(start_pfn + i));
  604. }
  605. static void hv_mem_hot_add(unsigned long start, unsigned long size,
  606. unsigned long pfn_count,
  607. struct hv_hotadd_state *has)
  608. {
  609. int ret = 0;
  610. int i, nid;
  611. unsigned long start_pfn;
  612. unsigned long processed_pfn;
  613. unsigned long total_pfn = pfn_count;
  614. for (i = 0; i < (size/ha_pages_in_chunk); i++) {
  615. start_pfn = start + (i * ha_pages_in_chunk);
  616. scoped_guard(spinlock_irqsave, &dm_device.ha_lock) {
  617. has->ha_end_pfn += ha_pages_in_chunk;
  618. processed_pfn = umin(total_pfn, ha_pages_in_chunk);
  619. total_pfn -= processed_pfn;
  620. has->covered_end_pfn += processed_pfn;
  621. }
  622. reinit_completion(&dm_device.ol_waitevent);
  623. nid = memory_add_physaddr_to_nid(PFN_PHYS(start_pfn));
  624. ret = add_memory(nid, PFN_PHYS((start_pfn)),
  625. HA_BYTES_IN_CHUNK, MHP_MERGE_RESOURCE);
  626. if (ret) {
  627. pr_err("hot_add memory failed error is %d\n", ret);
  628. if (ret == -EEXIST) {
  629. /*
  630. * This error indicates that the error
  631. * is not a transient failure. This is the
  632. * case where the guest's physical address map
  633. * precludes hot adding memory. Stop all further
  634. * memory hot-add.
  635. */
  636. do_hot_add = false;
  637. }
  638. scoped_guard(spinlock_irqsave, &dm_device.ha_lock) {
  639. has->ha_end_pfn -= ha_pages_in_chunk;
  640. has->covered_end_pfn -= processed_pfn;
  641. }
  642. break;
  643. }
  644. /*
  645. * Wait for memory to get onlined. If the kernel onlined the
  646. * memory when adding it, this will return directly. Otherwise,
  647. * it will wait for user space to online the memory. This helps
  648. * to avoid adding memory faster than it is getting onlined. As
  649. * adding succeeded, it is ok to proceed even if the memory was
  650. * not onlined in time.
  651. */
  652. wait_for_completion_timeout(&dm_device.ol_waitevent, 5 * HZ);
  653. post_status(&dm_device);
  654. }
  655. }
  656. static void hv_online_page(struct page *pg, unsigned int order)
  657. {
  658. struct hv_hotadd_state *has;
  659. unsigned long pfn = page_to_pfn(pg);
  660. guard(spinlock_irqsave)(&dm_device.ha_lock);
  661. list_for_each_entry(has, &dm_device.ha_region_list, list) {
  662. /* The page belongs to a different HAS. */
  663. if (pfn < has->start_pfn ||
  664. (pfn + (1UL << order) > has->end_pfn))
  665. continue;
  666. hv_bring_pgs_online(has, pfn, 1UL << order);
  667. break;
  668. }
  669. }
  670. static int pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
  671. {
  672. struct hv_hotadd_state *has;
  673. struct hv_hotadd_gap *gap;
  674. unsigned long residual;
  675. int ret = 0;
  676. guard(spinlock_irqsave)(&dm_device.ha_lock);
  677. list_for_each_entry(has, &dm_device.ha_region_list, list) {
  678. /*
  679. * If the pfn range we are dealing with is not in the current
  680. * "hot add block", move on.
  681. */
  682. if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
  683. continue;
  684. /*
  685. * If the current start pfn is not where the covered_end
  686. * is, create a gap and update covered_end_pfn.
  687. */
  688. if (has->covered_end_pfn != start_pfn) {
  689. gap = kzalloc(sizeof(struct hv_hotadd_gap), GFP_ATOMIC);
  690. if (!gap) {
  691. ret = -ENOMEM;
  692. break;
  693. }
  694. INIT_LIST_HEAD(&gap->list);
  695. gap->start_pfn = has->covered_end_pfn;
  696. gap->end_pfn = start_pfn;
  697. list_add_tail(&gap->list, &has->gap_list);
  698. has->covered_end_pfn = start_pfn;
  699. }
  700. /*
  701. * If the current hot add-request extends beyond
  702. * our current limit; extend it.
  703. */
  704. if ((start_pfn + pfn_cnt) > has->end_pfn) {
  705. /* Extend the region by multiples of ha_pages_in_chunk */
  706. residual = (start_pfn + pfn_cnt - has->end_pfn);
  707. has->end_pfn += ALIGN(residual, ha_pages_in_chunk);
  708. }
  709. ret = 1;
  710. break;
  711. }
  712. return ret;
  713. }
  714. static unsigned long handle_pg_range(unsigned long pg_start,
  715. unsigned long pg_count)
  716. {
  717. unsigned long start_pfn = pg_start;
  718. unsigned long pfn_cnt = pg_count;
  719. unsigned long size;
  720. struct hv_hotadd_state *has;
  721. unsigned long pgs_ol = 0;
  722. unsigned long old_covered_state;
  723. unsigned long res = 0, flags;
  724. pr_debug("Hot adding %lu pages starting at pfn 0x%lx.\n", pg_count,
  725. pg_start);
  726. spin_lock_irqsave(&dm_device.ha_lock, flags);
  727. list_for_each_entry(has, &dm_device.ha_region_list, list) {
  728. /*
  729. * If the pfn range we are dealing with is not in the current
  730. * "hot add block", move on.
  731. */
  732. if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
  733. continue;
  734. old_covered_state = has->covered_end_pfn;
  735. if (start_pfn < has->ha_end_pfn) {
  736. /*
  737. * This is the case where we are backing pages
  738. * in an already hot added region. Bring
  739. * these pages online first.
  740. */
  741. pgs_ol = has->ha_end_pfn - start_pfn;
  742. if (pgs_ol > pfn_cnt)
  743. pgs_ol = pfn_cnt;
  744. has->covered_end_pfn += pgs_ol;
  745. pfn_cnt -= pgs_ol;
  746. /*
  747. * Check if the corresponding memory block is already
  748. * online. It is possible to observe struct pages still
  749. * being uninitialized here so check section instead.
  750. * In case the section is online we need to bring the
  751. * rest of pfns (which were not backed previously)
  752. * online too.
  753. */
  754. if (start_pfn > has->start_pfn &&
  755. online_section_nr(pfn_to_section_nr(start_pfn)))
  756. hv_bring_pgs_online(has, start_pfn, pgs_ol);
  757. }
  758. if (has->ha_end_pfn < has->end_pfn && pfn_cnt > 0) {
  759. /*
  760. * We have some residual hot add range
  761. * that needs to be hot added; hot add
  762. * it now. Hot add a multiple of
  763. * ha_pages_in_chunk that fully covers the pages
  764. * we have.
  765. */
  766. size = (has->end_pfn - has->ha_end_pfn);
  767. if (pfn_cnt <= size) {
  768. size = ALIGN(pfn_cnt, ha_pages_in_chunk);
  769. } else {
  770. pfn_cnt = size;
  771. }
  772. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  773. hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
  774. spin_lock_irqsave(&dm_device.ha_lock, flags);
  775. }
  776. /*
  777. * If we managed to online any pages that were given to us,
  778. * we declare success.
  779. */
  780. res = has->covered_end_pfn - old_covered_state;
  781. break;
  782. }
  783. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  784. return res;
  785. }
  786. static unsigned long process_hot_add(unsigned long pg_start,
  787. unsigned long pfn_cnt,
  788. unsigned long rg_start,
  789. unsigned long rg_size)
  790. {
  791. struct hv_hotadd_state *ha_region = NULL;
  792. int covered;
  793. if (pfn_cnt == 0)
  794. return 0;
  795. if (!dm_device.host_specified_ha_region) {
  796. covered = pfn_covered(pg_start, pfn_cnt);
  797. if (covered < 0)
  798. return 0;
  799. if (covered)
  800. goto do_pg_range;
  801. }
  802. /*
  803. * If the host has specified a hot-add range; deal with it first.
  804. */
  805. if (rg_size != 0) {
  806. ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
  807. if (!ha_region)
  808. return 0;
  809. INIT_LIST_HEAD(&ha_region->list);
  810. INIT_LIST_HEAD(&ha_region->gap_list);
  811. ha_region->start_pfn = rg_start;
  812. ha_region->ha_end_pfn = rg_start;
  813. ha_region->covered_start_pfn = pg_start;
  814. ha_region->covered_end_pfn = pg_start;
  815. ha_region->end_pfn = rg_start + rg_size;
  816. scoped_guard(spinlock_irqsave, &dm_device.ha_lock) {
  817. list_add_tail(&ha_region->list, &dm_device.ha_region_list);
  818. }
  819. }
  820. do_pg_range:
  821. /*
  822. * Process the page range specified; bringing them
  823. * online if possible.
  824. */
  825. return handle_pg_range(pg_start, pfn_cnt);
  826. }
  827. #endif
  828. static void hot_add_req(struct work_struct *dummy)
  829. {
  830. struct dm_hot_add_response resp;
  831. #ifdef CONFIG_MEMORY_HOTPLUG
  832. unsigned long pg_start, pfn_cnt;
  833. unsigned long rg_start, rg_sz;
  834. #endif
  835. struct hv_dynmem_device *dm = &dm_device;
  836. memset(&resp, 0, sizeof(struct dm_hot_add_response));
  837. resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
  838. resp.hdr.size = sizeof(struct dm_hot_add_response);
  839. #ifdef CONFIG_MEMORY_HOTPLUG
  840. pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
  841. pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
  842. rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
  843. rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
  844. if (rg_start == 0 && !dm->host_specified_ha_region) {
  845. /*
  846. * The host has not specified the hot-add region.
  847. * Based on the hot-add page range being specified,
  848. * compute a hot-add region that can cover the pages
  849. * that need to be hot-added while ensuring the alignment
  850. * and size requirements of Linux as it relates to hot-add.
  851. */
  852. rg_start = ALIGN_DOWN(pg_start, ha_pages_in_chunk);
  853. rg_sz = ALIGN(pfn_cnt, ha_pages_in_chunk);
  854. }
  855. if (do_hot_add)
  856. resp.page_count = process_hot_add(pg_start, pfn_cnt,
  857. rg_start, rg_sz);
  858. dm->num_pages_added += resp.page_count;
  859. #endif
  860. /*
  861. * The result field of the response structure has the
  862. * following semantics:
  863. *
  864. * 1. If all or some pages hot-added: Guest should return success.
  865. *
  866. * 2. If no pages could be hot-added:
  867. *
  868. * If the guest returns success, then the host
  869. * will not attempt any further hot-add operations. This
  870. * signifies a permanent failure.
  871. *
  872. * If the guest returns failure, then this failure will be
  873. * treated as a transient failure and the host may retry the
  874. * hot-add operation after some delay.
  875. */
  876. if (resp.page_count > 0)
  877. resp.result = 1;
  878. else if (!do_hot_add)
  879. resp.result = 1;
  880. else
  881. resp.result = 0;
  882. if (!do_hot_add || resp.page_count == 0) {
  883. if (!allow_hibernation)
  884. pr_err("Memory hot add failed\n");
  885. else
  886. pr_info("Ignore hot-add request!\n");
  887. }
  888. dm->state = DM_INITIALIZED;
  889. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  890. vmbus_sendpacket(dm->dev->channel, &resp,
  891. sizeof(struct dm_hot_add_response),
  892. (unsigned long)NULL,
  893. VM_PKT_DATA_INBAND, 0);
  894. }
  895. static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
  896. {
  897. struct dm_info_header *info_hdr;
  898. info_hdr = (struct dm_info_header *)msg->info;
  899. switch (info_hdr->type) {
  900. case INFO_TYPE_MAX_PAGE_CNT:
  901. if (info_hdr->data_size == sizeof(__u64)) {
  902. __u64 *max_page_count = (__u64 *)&info_hdr[1];
  903. pr_info("Max. dynamic memory size: %llu MB\n",
  904. (*max_page_count) >> (20 - HV_HYP_PAGE_SHIFT));
  905. dm->max_dynamic_page_count = *max_page_count;
  906. }
  907. break;
  908. default:
  909. pr_warn("Received Unknown type: %d\n", info_hdr->type);
  910. }
  911. }
  912. static unsigned long compute_balloon_floor(void)
  913. {
  914. unsigned long min_pages;
  915. unsigned long nr_pages = totalram_pages();
  916. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  917. /* Simple continuous piecewiese linear function:
  918. * max MiB -> min MiB gradient
  919. * 0 0
  920. * 16 16
  921. * 32 24
  922. * 128 72 (1/2)
  923. * 512 168 (1/4)
  924. * 2048 360 (1/8)
  925. * 8192 744 (1/16)
  926. * 32768 1512 (1/32)
  927. */
  928. if (nr_pages < MB2PAGES(128))
  929. min_pages = MB2PAGES(8) + (nr_pages >> 1);
  930. else if (nr_pages < MB2PAGES(512))
  931. min_pages = MB2PAGES(40) + (nr_pages >> 2);
  932. else if (nr_pages < MB2PAGES(2048))
  933. min_pages = MB2PAGES(104) + (nr_pages >> 3);
  934. else if (nr_pages < MB2PAGES(8192))
  935. min_pages = MB2PAGES(232) + (nr_pages >> 4);
  936. else
  937. min_pages = MB2PAGES(488) + (nr_pages >> 5);
  938. #undef MB2PAGES
  939. return min_pages;
  940. }
  941. /*
  942. * Compute total committed memory pages
  943. */
  944. static unsigned long get_pages_committed(struct hv_dynmem_device *dm)
  945. {
  946. return vm_memory_committed() +
  947. dm->num_pages_ballooned +
  948. (dm->num_pages_added > dm->num_pages_onlined ?
  949. dm->num_pages_added - dm->num_pages_onlined : 0) +
  950. compute_balloon_floor();
  951. }
  952. /*
  953. * Post our status as it relates memory pressure to the
  954. * host. Host expects the guests to post this status
  955. * periodically at 1 second intervals.
  956. *
  957. * The metrics specified in this protocol are very Windows
  958. * specific and so we cook up numbers here to convey our memory
  959. * pressure.
  960. */
  961. static void post_status(struct hv_dynmem_device *dm)
  962. {
  963. struct dm_status status;
  964. unsigned long now = jiffies;
  965. unsigned long last_post = last_post_time;
  966. unsigned long num_pages_avail, num_pages_committed;
  967. if (pressure_report_delay > 0) {
  968. --pressure_report_delay;
  969. return;
  970. }
  971. if (!time_after(now, (last_post_time + HZ)))
  972. return;
  973. memset(&status, 0, sizeof(struct dm_status));
  974. status.hdr.type = DM_STATUS_REPORT;
  975. status.hdr.size = sizeof(struct dm_status);
  976. status.hdr.trans_id = atomic_inc_return(&trans_id);
  977. /*
  978. * The host expects the guest to report free and committed memory.
  979. * Furthermore, the host expects the pressure information to include
  980. * the ballooned out pages. For a given amount of memory that we are
  981. * managing we need to compute a floor below which we should not
  982. * balloon. Compute this and add it to the pressure report.
  983. * We also need to report all offline pages (num_pages_added -
  984. * num_pages_onlined) as committed to the host, otherwise it can try
  985. * asking us to balloon them out.
  986. */
  987. num_pages_avail = si_mem_available();
  988. num_pages_committed = get_pages_committed(dm);
  989. trace_balloon_status(num_pages_avail, num_pages_committed,
  990. vm_memory_committed(), dm->num_pages_ballooned,
  991. dm->num_pages_added, dm->num_pages_onlined);
  992. /* Convert numbers of pages into numbers of HV_HYP_PAGEs. */
  993. status.num_avail = num_pages_avail * NR_HV_HYP_PAGES_IN_PAGE;
  994. status.num_committed = num_pages_committed * NR_HV_HYP_PAGES_IN_PAGE;
  995. /*
  996. * If our transaction ID is no longer current, just don't
  997. * send the status. This can happen if we were interrupted
  998. * after we picked our transaction ID.
  999. */
  1000. if (status.hdr.trans_id != atomic_read(&trans_id))
  1001. return;
  1002. /*
  1003. * If the last post time that we sampled has changed,
  1004. * we have raced, don't post the status.
  1005. */
  1006. if (last_post != last_post_time)
  1007. return;
  1008. last_post_time = jiffies;
  1009. vmbus_sendpacket(dm->dev->channel, &status,
  1010. sizeof(struct dm_status),
  1011. (unsigned long)NULL,
  1012. VM_PKT_DATA_INBAND, 0);
  1013. }
  1014. static void free_balloon_pages(struct hv_dynmem_device *dm,
  1015. union dm_mem_page_range *range_array)
  1016. {
  1017. int num_pages = range_array->finfo.page_cnt;
  1018. __u64 start_frame = range_array->finfo.start_page;
  1019. struct page *pg;
  1020. int i;
  1021. for (i = 0; i < num_pages; i++) {
  1022. pg = pfn_to_page(i + start_frame);
  1023. __ClearPageOffline(pg);
  1024. __free_page(pg);
  1025. dm->num_pages_ballooned--;
  1026. adjust_managed_page_count(pg, 1);
  1027. }
  1028. }
  1029. static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
  1030. unsigned int num_pages,
  1031. struct dm_balloon_response *bl_resp,
  1032. int alloc_unit)
  1033. {
  1034. unsigned int i, j;
  1035. struct page *pg;
  1036. for (i = 0; i < num_pages / alloc_unit; i++) {
  1037. if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
  1038. HV_HYP_PAGE_SIZE)
  1039. return i * alloc_unit;
  1040. /*
  1041. * We execute this code in a thread context. Furthermore,
  1042. * we don't want the kernel to try too hard.
  1043. */
  1044. pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
  1045. __GFP_NOMEMALLOC | __GFP_NOWARN,
  1046. get_order(alloc_unit << PAGE_SHIFT));
  1047. if (!pg)
  1048. return i * alloc_unit;
  1049. dm->num_pages_ballooned += alloc_unit;
  1050. /*
  1051. * If we allocatted 2M pages; split them so we
  1052. * can free them in any order we get.
  1053. */
  1054. if (alloc_unit != 1)
  1055. split_page(pg, get_order(alloc_unit << PAGE_SHIFT));
  1056. /* mark all pages offline */
  1057. for (j = 0; j < alloc_unit; j++) {
  1058. __SetPageOffline(pg + j);
  1059. adjust_managed_page_count(pg + j, -1);
  1060. }
  1061. bl_resp->range_count++;
  1062. bl_resp->range_array[i].finfo.start_page =
  1063. page_to_pfn(pg);
  1064. bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
  1065. bl_resp->hdr.size += sizeof(union dm_mem_page_range);
  1066. }
  1067. return i * alloc_unit;
  1068. }
  1069. static void balloon_up(struct work_struct *dummy)
  1070. {
  1071. unsigned int num_pages = dm_device.balloon_wrk.num_pages;
  1072. unsigned int num_ballooned = 0;
  1073. struct dm_balloon_response *bl_resp;
  1074. int alloc_unit;
  1075. int ret;
  1076. bool done = false;
  1077. int i;
  1078. long avail_pages;
  1079. unsigned long floor;
  1080. /*
  1081. * We will attempt 2M allocations. However, if we fail to
  1082. * allocate 2M chunks, we will go back to PAGE_SIZE allocations.
  1083. */
  1084. alloc_unit = PAGES_IN_2M;
  1085. avail_pages = si_mem_available();
  1086. floor = compute_balloon_floor();
  1087. /* Refuse to balloon below the floor. */
  1088. if (avail_pages < num_pages || avail_pages - num_pages < floor) {
  1089. pr_info("Balloon request will be partially fulfilled. %s\n",
  1090. avail_pages < num_pages ? "Not enough memory." :
  1091. "Balloon floor reached.");
  1092. num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
  1093. }
  1094. while (!done) {
  1095. memset(balloon_up_send_buffer, 0, HV_HYP_PAGE_SIZE);
  1096. bl_resp = (struct dm_balloon_response *)balloon_up_send_buffer;
  1097. bl_resp->hdr.type = DM_BALLOON_RESPONSE;
  1098. bl_resp->hdr.size = sizeof(struct dm_balloon_response);
  1099. bl_resp->more_pages = 1;
  1100. num_pages -= num_ballooned;
  1101. num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
  1102. bl_resp, alloc_unit);
  1103. if (alloc_unit != 1 && num_ballooned == 0) {
  1104. alloc_unit = 1;
  1105. continue;
  1106. }
  1107. if (num_ballooned == 0 || num_ballooned == num_pages) {
  1108. pr_debug("Ballooned %u out of %u requested pages.\n",
  1109. num_pages, dm_device.balloon_wrk.num_pages);
  1110. bl_resp->more_pages = 0;
  1111. done = true;
  1112. dm_device.state = DM_INITIALIZED;
  1113. }
  1114. /*
  1115. * We are pushing a lot of data through the channel;
  1116. * deal with transient failures caused because of the
  1117. * lack of space in the ring buffer.
  1118. */
  1119. do {
  1120. bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
  1121. ret = vmbus_sendpacket(dm_device.dev->channel,
  1122. bl_resp,
  1123. bl_resp->hdr.size,
  1124. (unsigned long)NULL,
  1125. VM_PKT_DATA_INBAND, 0);
  1126. if (ret == -EAGAIN)
  1127. msleep(20);
  1128. post_status(&dm_device);
  1129. } while (ret == -EAGAIN);
  1130. if (ret) {
  1131. /*
  1132. * Free up the memory we allocatted.
  1133. */
  1134. pr_err("Balloon response failed\n");
  1135. for (i = 0; i < bl_resp->range_count; i++)
  1136. free_balloon_pages(&dm_device,
  1137. &bl_resp->range_array[i]);
  1138. done = true;
  1139. }
  1140. }
  1141. }
  1142. static void balloon_down(struct hv_dynmem_device *dm,
  1143. struct dm_unballoon_request *req)
  1144. {
  1145. union dm_mem_page_range *range_array = req->range_array;
  1146. int range_count = req->range_count;
  1147. struct dm_unballoon_response resp;
  1148. int i;
  1149. unsigned int prev_pages_ballooned = dm->num_pages_ballooned;
  1150. for (i = 0; i < range_count; i++) {
  1151. free_balloon_pages(dm, &range_array[i]);
  1152. complete(&dm_device.config_event);
  1153. }
  1154. pr_debug("Freed %u ballooned pages.\n",
  1155. prev_pages_ballooned - dm->num_pages_ballooned);
  1156. if (req->more_pages == 1)
  1157. return;
  1158. memset(&resp, 0, sizeof(struct dm_unballoon_response));
  1159. resp.hdr.type = DM_UNBALLOON_RESPONSE;
  1160. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  1161. resp.hdr.size = sizeof(struct dm_unballoon_response);
  1162. vmbus_sendpacket(dm_device.dev->channel, &resp,
  1163. sizeof(struct dm_unballoon_response),
  1164. (unsigned long)NULL,
  1165. VM_PKT_DATA_INBAND, 0);
  1166. dm->state = DM_INITIALIZED;
  1167. }
  1168. static void balloon_onchannelcallback(void *context);
  1169. static int dm_thread_func(void *dm_dev)
  1170. {
  1171. struct hv_dynmem_device *dm = dm_dev;
  1172. while (!kthread_should_stop()) {
  1173. wait_for_completion_interruptible_timeout(&dm_device.config_event, 1 * HZ);
  1174. /*
  1175. * The host expects us to post information on the memory
  1176. * pressure every second.
  1177. */
  1178. reinit_completion(&dm_device.config_event);
  1179. post_status(dm);
  1180. /*
  1181. * disable free page reporting if multiple hypercall
  1182. * failure flag set. It is not done in the page_reporting
  1183. * callback context as that causes a deadlock between
  1184. * page_reporting_process() and page_reporting_unregister()
  1185. */
  1186. if (hv_hypercall_multi_failure >= HV_MAX_FAILURES) {
  1187. pr_err("Multiple failures in cold memory discard hypercall, disabling page reporting\n");
  1188. disable_page_reporting();
  1189. /* Reset the flag after disabling reporting */
  1190. hv_hypercall_multi_failure = 0;
  1191. }
  1192. }
  1193. return 0;
  1194. }
  1195. static void version_resp(struct hv_dynmem_device *dm,
  1196. struct dm_version_response *vresp)
  1197. {
  1198. struct dm_version_request version_req;
  1199. int ret;
  1200. if (vresp->is_accepted) {
  1201. /*
  1202. * We are done; wakeup the
  1203. * context waiting for version
  1204. * negotiation.
  1205. */
  1206. complete(&dm->host_event);
  1207. return;
  1208. }
  1209. /*
  1210. * If there are more versions to try, continue
  1211. * with negotiations; if not
  1212. * shutdown the service since we are not able
  1213. * to negotiate a suitable version number
  1214. * with the host.
  1215. */
  1216. if (dm->next_version == 0)
  1217. goto version_error;
  1218. memset(&version_req, 0, sizeof(struct dm_version_request));
  1219. version_req.hdr.type = DM_VERSION_REQUEST;
  1220. version_req.hdr.size = sizeof(struct dm_version_request);
  1221. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1222. version_req.version.version = dm->next_version;
  1223. dm->version = version_req.version.version;
  1224. /*
  1225. * Set the next version to try in case current version fails.
  1226. * Win7 protocol ought to be the last one to try.
  1227. */
  1228. switch (version_req.version.version) {
  1229. case DYNMEM_PROTOCOL_VERSION_WIN8:
  1230. dm->next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
  1231. version_req.is_last_attempt = 0;
  1232. break;
  1233. default:
  1234. dm->next_version = 0;
  1235. version_req.is_last_attempt = 1;
  1236. }
  1237. ret = vmbus_sendpacket(dm->dev->channel, &version_req,
  1238. sizeof(struct dm_version_request),
  1239. (unsigned long)NULL,
  1240. VM_PKT_DATA_INBAND, 0);
  1241. if (ret)
  1242. goto version_error;
  1243. return;
  1244. version_error:
  1245. dm->state = DM_INIT_ERROR;
  1246. complete(&dm->host_event);
  1247. }
  1248. static void cap_resp(struct hv_dynmem_device *dm,
  1249. struct dm_capabilities_resp_msg *cap_resp)
  1250. {
  1251. if (!cap_resp->is_accepted) {
  1252. pr_err("Capabilities not accepted by host\n");
  1253. dm->state = DM_INIT_ERROR;
  1254. }
  1255. complete(&dm->host_event);
  1256. }
  1257. static void balloon_onchannelcallback(void *context)
  1258. {
  1259. struct hv_device *dev = context;
  1260. u32 recvlen;
  1261. u64 requestid;
  1262. struct dm_message *dm_msg;
  1263. struct dm_header *dm_hdr;
  1264. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1265. struct dm_balloon *bal_msg;
  1266. struct dm_hot_add *ha_msg;
  1267. union dm_mem_page_range *ha_pg_range;
  1268. union dm_mem_page_range *ha_region;
  1269. memset(recv_buffer, 0, sizeof(recv_buffer));
  1270. vmbus_recvpacket(dev->channel, recv_buffer,
  1271. HV_HYP_PAGE_SIZE, &recvlen, &requestid);
  1272. if (recvlen > 0) {
  1273. dm_msg = (struct dm_message *)recv_buffer;
  1274. dm_hdr = &dm_msg->hdr;
  1275. switch (dm_hdr->type) {
  1276. case DM_VERSION_RESPONSE:
  1277. version_resp(dm,
  1278. (struct dm_version_response *)dm_msg);
  1279. break;
  1280. case DM_CAPABILITIES_RESPONSE:
  1281. cap_resp(dm,
  1282. (struct dm_capabilities_resp_msg *)dm_msg);
  1283. break;
  1284. case DM_BALLOON_REQUEST:
  1285. if (allow_hibernation) {
  1286. pr_info("Ignore balloon-up request!\n");
  1287. break;
  1288. }
  1289. if (dm->state == DM_BALLOON_UP)
  1290. pr_warn("Currently ballooning\n");
  1291. bal_msg = (struct dm_balloon *)recv_buffer;
  1292. dm->state = DM_BALLOON_UP;
  1293. dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
  1294. schedule_work(&dm_device.balloon_wrk.wrk);
  1295. break;
  1296. case DM_UNBALLOON_REQUEST:
  1297. if (allow_hibernation) {
  1298. pr_info("Ignore balloon-down request!\n");
  1299. break;
  1300. }
  1301. dm->state = DM_BALLOON_DOWN;
  1302. balloon_down(dm,
  1303. (struct dm_unballoon_request *)recv_buffer);
  1304. break;
  1305. case DM_MEM_HOT_ADD_REQUEST:
  1306. if (dm->state == DM_HOT_ADD)
  1307. pr_warn("Currently hot-adding\n");
  1308. dm->state = DM_HOT_ADD;
  1309. ha_msg = (struct dm_hot_add *)recv_buffer;
  1310. if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
  1311. /*
  1312. * This is a normal hot-add request specifying
  1313. * hot-add memory.
  1314. */
  1315. dm->host_specified_ha_region = false;
  1316. ha_pg_range = &ha_msg->range;
  1317. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1318. dm->ha_wrk.ha_region_range.page_range = 0;
  1319. } else {
  1320. /*
  1321. * Host is specifying that we first hot-add
  1322. * a region and then partially populate this
  1323. * region.
  1324. */
  1325. dm->host_specified_ha_region = true;
  1326. ha_pg_range = &ha_msg->range;
  1327. ha_region = &ha_pg_range[1];
  1328. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1329. dm->ha_wrk.ha_region_range = *ha_region;
  1330. }
  1331. schedule_work(&dm_device.ha_wrk.wrk);
  1332. break;
  1333. case DM_INFO_MESSAGE:
  1334. process_info(dm, (struct dm_info_msg *)dm_msg);
  1335. break;
  1336. default:
  1337. pr_warn_ratelimited("Unhandled message: type: %d\n", dm_hdr->type);
  1338. }
  1339. }
  1340. }
  1341. #define HV_LARGE_REPORTING_ORDER 9
  1342. #define HV_LARGE_REPORTING_LEN (HV_HYP_PAGE_SIZE << \
  1343. HV_LARGE_REPORTING_ORDER)
  1344. static int hv_free_page_report(struct page_reporting_dev_info *pr_dev_info,
  1345. struct scatterlist *sgl, unsigned int nents)
  1346. {
  1347. unsigned long flags;
  1348. struct hv_memory_hint *hint;
  1349. int i, order;
  1350. u64 status;
  1351. struct scatterlist *sg;
  1352. WARN_ON_ONCE(nents > HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES);
  1353. WARN_ON_ONCE(sgl->length < (HV_HYP_PAGE_SIZE << page_reporting_order));
  1354. local_irq_save(flags);
  1355. hint = *this_cpu_ptr(hyperv_pcpu_input_arg);
  1356. if (!hint) {
  1357. local_irq_restore(flags);
  1358. return -ENOSPC;
  1359. }
  1360. hint->type = HV_EXT_MEMORY_HEAT_HINT_TYPE_COLD_DISCARD;
  1361. hint->reserved = 0;
  1362. for_each_sg(sgl, sg, nents, i) {
  1363. union hv_gpa_page_range *range;
  1364. range = &hint->ranges[i];
  1365. range->address_space = 0;
  1366. order = get_order(sg->length);
  1367. /*
  1368. * Hyper-V expects the additional_pages field in the units
  1369. * of one of these 3 sizes, 4Kbytes, 2Mbytes or 1Gbytes.
  1370. * This is dictated by the values of the fields page.largesize
  1371. * and page_size.
  1372. * This code however, only uses 4Kbytes and 2Mbytes units
  1373. * and not 1Gbytes unit.
  1374. */
  1375. /* page reporting for pages 2MB or higher */
  1376. if (order >= HV_LARGE_REPORTING_ORDER) {
  1377. range->page.largepage = 1;
  1378. range->page_size = HV_GPA_PAGE_RANGE_PAGE_SIZE_2MB;
  1379. range->base_large_pfn = page_to_hvpfn(
  1380. sg_page(sg)) >> HV_LARGE_REPORTING_ORDER;
  1381. range->page.additional_pages =
  1382. (sg->length / HV_LARGE_REPORTING_LEN) - 1;
  1383. } else {
  1384. /* Page reporting for pages below 2MB */
  1385. range->page.basepfn = page_to_hvpfn(sg_page(sg));
  1386. range->page.largepage = false;
  1387. range->page.additional_pages =
  1388. (sg->length / HV_HYP_PAGE_SIZE) - 1;
  1389. }
  1390. }
  1391. status = hv_do_rep_hypercall(HV_EXT_CALL_MEMORY_HEAT_HINT, nents, 0,
  1392. hint, NULL);
  1393. local_irq_restore(flags);
  1394. if (!hv_result_success(status)) {
  1395. pr_err("Cold memory discard hypercall failed with status %llx\n",
  1396. status);
  1397. if (hv_hypercall_multi_failure > 0)
  1398. hv_hypercall_multi_failure++;
  1399. if (hv_result(status) == HV_STATUS_INVALID_PARAMETER) {
  1400. pr_err("Underlying Hyper-V does not support order less than 9. Hypercall failed\n");
  1401. pr_err("Defaulting to page_reporting_order %d\n",
  1402. pageblock_order);
  1403. page_reporting_order = pageblock_order;
  1404. hv_hypercall_multi_failure++;
  1405. return -EINVAL;
  1406. }
  1407. return -EINVAL;
  1408. }
  1409. return 0;
  1410. }
  1411. static void enable_page_reporting(void)
  1412. {
  1413. int ret;
  1414. if (!hv_query_ext_cap(HV_EXT_CAPABILITY_MEMORY_COLD_DISCARD_HINT)) {
  1415. pr_debug("Cold memory discard hint not supported by Hyper-V\n");
  1416. return;
  1417. }
  1418. BUILD_BUG_ON(PAGE_REPORTING_CAPACITY > HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES);
  1419. dm_device.pr_dev_info.report = hv_free_page_report;
  1420. /*
  1421. * We let the page_reporting_order parameter decide the order
  1422. * in the page_reporting code
  1423. */
  1424. dm_device.pr_dev_info.order = 0;
  1425. ret = page_reporting_register(&dm_device.pr_dev_info);
  1426. if (ret < 0) {
  1427. dm_device.pr_dev_info.report = NULL;
  1428. pr_err("Failed to enable cold memory discard: %d\n", ret);
  1429. } else {
  1430. pr_info("Cold memory discard hint enabled with order %d\n",
  1431. page_reporting_order);
  1432. }
  1433. }
  1434. static void disable_page_reporting(void)
  1435. {
  1436. if (dm_device.pr_dev_info.report) {
  1437. page_reporting_unregister(&dm_device.pr_dev_info);
  1438. dm_device.pr_dev_info.report = NULL;
  1439. }
  1440. }
  1441. static int ballooning_enabled(void)
  1442. {
  1443. /*
  1444. * Disable ballooning if the page size is not 4k (HV_HYP_PAGE_SIZE),
  1445. * since currently it's unclear to us whether an unballoon request can
  1446. * make sure all page ranges are guest page size aligned.
  1447. */
  1448. if (PAGE_SIZE != HV_HYP_PAGE_SIZE) {
  1449. pr_info("Ballooning disabled because page size is not 4096 bytes\n");
  1450. return 0;
  1451. }
  1452. return 1;
  1453. }
  1454. static int hot_add_enabled(void)
  1455. {
  1456. /*
  1457. * Disable hot add on ARM64, because we currently rely on
  1458. * memory_add_physaddr_to_nid() to get a node id of a hot add range,
  1459. * however ARM64's memory_add_physaddr_to_nid() always return 0 and
  1460. * DM_MEM_HOT_ADD_REQUEST doesn't have the NUMA node information for
  1461. * add_memory().
  1462. */
  1463. if (IS_ENABLED(CONFIG_ARM64)) {
  1464. pr_info("Memory hot add disabled on ARM64\n");
  1465. return 0;
  1466. }
  1467. return 1;
  1468. }
  1469. static int balloon_connect_vsp(struct hv_device *dev)
  1470. {
  1471. struct dm_version_request version_req;
  1472. struct dm_capabilities cap_msg;
  1473. unsigned long t;
  1474. int ret;
  1475. /*
  1476. * max_pkt_size should be large enough for one vmbus packet header plus
  1477. * our receive buffer size. Hyper-V sends messages up to
  1478. * HV_HYP_PAGE_SIZE bytes long on balloon channel.
  1479. */
  1480. dev->channel->max_pkt_size = HV_HYP_PAGE_SIZE * 2;
  1481. ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
  1482. balloon_onchannelcallback, dev);
  1483. if (ret)
  1484. return ret;
  1485. /*
  1486. * Initiate the hand shake with the host and negotiate
  1487. * a version that the host can support. We start with the
  1488. * highest version number and go down if the host cannot
  1489. * support it.
  1490. */
  1491. memset(&version_req, 0, sizeof(struct dm_version_request));
  1492. version_req.hdr.type = DM_VERSION_REQUEST;
  1493. version_req.hdr.size = sizeof(struct dm_version_request);
  1494. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1495. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN10;
  1496. version_req.is_last_attempt = 0;
  1497. dm_device.version = version_req.version.version;
  1498. ret = vmbus_sendpacket(dev->channel, &version_req,
  1499. sizeof(struct dm_version_request),
  1500. (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
  1501. if (ret)
  1502. goto out;
  1503. t = wait_for_completion_timeout(&dm_device.host_event, 5 * HZ);
  1504. if (t == 0) {
  1505. ret = -ETIMEDOUT;
  1506. goto out;
  1507. }
  1508. /*
  1509. * If we could not negotiate a compatible version with the host
  1510. * fail the probe function.
  1511. */
  1512. if (dm_device.state == DM_INIT_ERROR) {
  1513. ret = -EPROTO;
  1514. goto out;
  1515. }
  1516. pr_info("Using Dynamic Memory protocol version %u.%u\n",
  1517. DYNMEM_MAJOR_VERSION(dm_device.version),
  1518. DYNMEM_MINOR_VERSION(dm_device.version));
  1519. /*
  1520. * Now submit our capabilities to the host.
  1521. */
  1522. memset(&cap_msg, 0, sizeof(struct dm_capabilities));
  1523. cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
  1524. cap_msg.hdr.size = sizeof(struct dm_capabilities);
  1525. cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
  1526. /*
  1527. * When hibernation (i.e. virtual ACPI S4 state) is enabled, the host
  1528. * currently still requires the bits to be set, so we have to add code
  1529. * to fail the host's hot-add and balloon up/down requests, if any.
  1530. */
  1531. cap_msg.caps.cap_bits.balloon = ballooning_enabled();
  1532. cap_msg.caps.cap_bits.hot_add = hot_add_enabled();
  1533. /*
  1534. * Specify our alignment requirements for memory hot-add. The value is
  1535. * the log base 2 of the number of megabytes in a chunk. For example,
  1536. * with 256 MiB chunks, the value is 8. The number of MiB in a chunk
  1537. * must be a power of 2.
  1538. */
  1539. cap_msg.caps.cap_bits.hot_add_alignment =
  1540. ilog2(HA_BYTES_IN_CHUNK / SZ_1M);
  1541. /*
  1542. * Currently the host does not use these
  1543. * values and we set them to what is done in the
  1544. * Windows driver.
  1545. */
  1546. cap_msg.min_page_cnt = 0;
  1547. cap_msg.max_page_number = -1;
  1548. ret = vmbus_sendpacket(dev->channel, &cap_msg,
  1549. sizeof(struct dm_capabilities),
  1550. (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
  1551. if (ret)
  1552. goto out;
  1553. t = wait_for_completion_timeout(&dm_device.host_event, 5 * HZ);
  1554. if (t == 0) {
  1555. ret = -ETIMEDOUT;
  1556. goto out;
  1557. }
  1558. /*
  1559. * If the host does not like our capabilities,
  1560. * fail the probe function.
  1561. */
  1562. if (dm_device.state == DM_INIT_ERROR) {
  1563. ret = -EPROTO;
  1564. goto out;
  1565. }
  1566. return 0;
  1567. out:
  1568. vmbus_close(dev->channel);
  1569. return ret;
  1570. }
  1571. /*
  1572. * DEBUGFS Interface
  1573. */
  1574. #ifdef CONFIG_DEBUG_FS
  1575. /**
  1576. * hv_balloon_debug_show - shows statistics of balloon operations.
  1577. * @f: pointer to the &struct seq_file.
  1578. * @offset: ignored.
  1579. *
  1580. * Provides the statistics that can be accessed in hv-balloon in the debugfs.
  1581. *
  1582. * Return: zero on success or an error code.
  1583. */
  1584. static int hv_balloon_debug_show(struct seq_file *f, void *offset)
  1585. {
  1586. struct hv_dynmem_device *dm = f->private;
  1587. char *sname;
  1588. seq_printf(f, "%-22s: %u.%u\n", "host_version",
  1589. DYNMEM_MAJOR_VERSION(dm->version),
  1590. DYNMEM_MINOR_VERSION(dm->version));
  1591. seq_printf(f, "%-22s:", "capabilities");
  1592. if (ballooning_enabled())
  1593. seq_puts(f, " enabled");
  1594. if (hot_add_enabled())
  1595. seq_puts(f, " hot_add");
  1596. seq_puts(f, "\n");
  1597. seq_printf(f, "%-22s: %u", "state", dm->state);
  1598. switch (dm->state) {
  1599. case DM_INITIALIZING:
  1600. sname = "Initializing";
  1601. break;
  1602. case DM_INITIALIZED:
  1603. sname = "Initialized";
  1604. break;
  1605. case DM_BALLOON_UP:
  1606. sname = "Balloon Up";
  1607. break;
  1608. case DM_BALLOON_DOWN:
  1609. sname = "Balloon Down";
  1610. break;
  1611. case DM_HOT_ADD:
  1612. sname = "Hot Add";
  1613. break;
  1614. case DM_INIT_ERROR:
  1615. sname = "Error";
  1616. break;
  1617. default:
  1618. sname = "Unknown";
  1619. }
  1620. seq_printf(f, " (%s)\n", sname);
  1621. /* HV Page Size */
  1622. seq_printf(f, "%-22s: %ld\n", "page_size", HV_HYP_PAGE_SIZE);
  1623. /* Pages added with hot_add */
  1624. seq_printf(f, "%-22s: %u\n", "pages_added", dm->num_pages_added);
  1625. /* pages that are "onlined"/used from pages_added */
  1626. seq_printf(f, "%-22s: %u\n", "pages_onlined", dm->num_pages_onlined);
  1627. /* pages we have given back to host */
  1628. seq_printf(f, "%-22s: %u\n", "pages_ballooned", dm->num_pages_ballooned);
  1629. seq_printf(f, "%-22s: %lu\n", "total_pages_committed",
  1630. get_pages_committed(dm));
  1631. seq_printf(f, "%-22s: %llu\n", "max_dynamic_page_count",
  1632. dm->max_dynamic_page_count);
  1633. return 0;
  1634. }
  1635. DEFINE_SHOW_ATTRIBUTE(hv_balloon_debug);
  1636. static void hv_balloon_debugfs_init(struct hv_dynmem_device *b)
  1637. {
  1638. debugfs_create_file("hv-balloon", 0444, NULL, b,
  1639. &hv_balloon_debug_fops);
  1640. }
  1641. static void hv_balloon_debugfs_exit(struct hv_dynmem_device *b)
  1642. {
  1643. debugfs_lookup_and_remove("hv-balloon", NULL);
  1644. }
  1645. #else
  1646. static inline void hv_balloon_debugfs_init(struct hv_dynmem_device *b)
  1647. {
  1648. }
  1649. static inline void hv_balloon_debugfs_exit(struct hv_dynmem_device *b)
  1650. {
  1651. }
  1652. #endif /* CONFIG_DEBUG_FS */
  1653. static int balloon_probe(struct hv_device *dev,
  1654. const struct hv_vmbus_device_id *dev_id)
  1655. {
  1656. int ret;
  1657. allow_hibernation = hv_is_hibernation_supported();
  1658. if (allow_hibernation)
  1659. hot_add = false;
  1660. #ifdef CONFIG_MEMORY_HOTPLUG
  1661. /*
  1662. * Hot-add must operate in chunks that are of size equal to the
  1663. * memory block size because that's what the core add_memory()
  1664. * interface requires. The Hyper-V interface requires that the memory
  1665. * block size be a power of 2, which is guaranteed by the check in
  1666. * memory_dev_init().
  1667. */
  1668. ha_pages_in_chunk = memory_block_size_bytes() / PAGE_SIZE;
  1669. do_hot_add = hot_add;
  1670. #else
  1671. /*
  1672. * Without MEMORY_HOTPLUG, the guest returns a failure status for all
  1673. * hot add requests from Hyper-V, and the chunk size is used only to
  1674. * specify alignment to Hyper-V as required by the host/guest protocol.
  1675. * Somewhat arbitrarily, use 128 MiB.
  1676. */
  1677. ha_pages_in_chunk = SZ_128M / PAGE_SIZE;
  1678. do_hot_add = false;
  1679. #endif
  1680. dm_device.dev = dev;
  1681. dm_device.state = DM_INITIALIZING;
  1682. dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN8;
  1683. init_completion(&dm_device.host_event);
  1684. init_completion(&dm_device.config_event);
  1685. INIT_LIST_HEAD(&dm_device.ha_region_list);
  1686. spin_lock_init(&dm_device.ha_lock);
  1687. INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
  1688. INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
  1689. dm_device.host_specified_ha_region = false;
  1690. #ifdef CONFIG_MEMORY_HOTPLUG
  1691. set_online_page_callback(&hv_online_page);
  1692. init_completion(&dm_device.ol_waitevent);
  1693. register_memory_notifier(&hv_memory_nb);
  1694. #endif
  1695. hv_set_drvdata(dev, &dm_device);
  1696. ret = balloon_connect_vsp(dev);
  1697. if (ret != 0)
  1698. goto connect_error;
  1699. enable_page_reporting();
  1700. dm_device.state = DM_INITIALIZED;
  1701. dm_device.thread =
  1702. kthread_run(dm_thread_func, &dm_device, "hv_balloon");
  1703. if (IS_ERR(dm_device.thread)) {
  1704. ret = PTR_ERR(dm_device.thread);
  1705. goto probe_error;
  1706. }
  1707. hv_balloon_debugfs_init(&dm_device);
  1708. return 0;
  1709. probe_error:
  1710. dm_device.state = DM_INIT_ERROR;
  1711. dm_device.thread = NULL;
  1712. disable_page_reporting();
  1713. vmbus_close(dev->channel);
  1714. connect_error:
  1715. #ifdef CONFIG_MEMORY_HOTPLUG
  1716. unregister_memory_notifier(&hv_memory_nb);
  1717. restore_online_page_callback(&hv_online_page);
  1718. #endif
  1719. return ret;
  1720. }
  1721. static void balloon_remove(struct hv_device *dev)
  1722. {
  1723. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1724. struct hv_hotadd_state *has, *tmp;
  1725. struct hv_hotadd_gap *gap, *tmp_gap;
  1726. if (dm->num_pages_ballooned != 0)
  1727. pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
  1728. hv_balloon_debugfs_exit(dm);
  1729. cancel_work_sync(&dm->balloon_wrk.wrk);
  1730. cancel_work_sync(&dm->ha_wrk.wrk);
  1731. kthread_stop(dm->thread);
  1732. /*
  1733. * This is to handle the case when balloon_resume()
  1734. * call has failed and some cleanup has been done as
  1735. * a part of the error handling.
  1736. */
  1737. if (dm_device.state != DM_INIT_ERROR) {
  1738. disable_page_reporting();
  1739. vmbus_close(dev->channel);
  1740. #ifdef CONFIG_MEMORY_HOTPLUG
  1741. unregister_memory_notifier(&hv_memory_nb);
  1742. restore_online_page_callback(&hv_online_page);
  1743. #endif
  1744. }
  1745. guard(spinlock_irqsave)(&dm_device.ha_lock);
  1746. list_for_each_entry_safe(has, tmp, &dm->ha_region_list, list) {
  1747. list_for_each_entry_safe(gap, tmp_gap, &has->gap_list, list) {
  1748. list_del(&gap->list);
  1749. kfree(gap);
  1750. }
  1751. list_del(&has->list);
  1752. kfree(has);
  1753. }
  1754. }
  1755. static int balloon_suspend(struct hv_device *hv_dev)
  1756. {
  1757. struct hv_dynmem_device *dm = hv_get_drvdata(hv_dev);
  1758. tasklet_disable(&hv_dev->channel->callback_event);
  1759. cancel_work_sync(&dm->balloon_wrk.wrk);
  1760. cancel_work_sync(&dm->ha_wrk.wrk);
  1761. if (dm->thread) {
  1762. kthread_stop(dm->thread);
  1763. dm->thread = NULL;
  1764. vmbus_close(hv_dev->channel);
  1765. }
  1766. tasklet_enable(&hv_dev->channel->callback_event);
  1767. return 0;
  1768. }
  1769. static int balloon_resume(struct hv_device *dev)
  1770. {
  1771. int ret;
  1772. dm_device.state = DM_INITIALIZING;
  1773. ret = balloon_connect_vsp(dev);
  1774. if (ret != 0)
  1775. goto out;
  1776. dm_device.thread =
  1777. kthread_run(dm_thread_func, &dm_device, "hv_balloon");
  1778. if (IS_ERR(dm_device.thread)) {
  1779. ret = PTR_ERR(dm_device.thread);
  1780. dm_device.thread = NULL;
  1781. goto close_channel;
  1782. }
  1783. dm_device.state = DM_INITIALIZED;
  1784. return 0;
  1785. close_channel:
  1786. vmbus_close(dev->channel);
  1787. out:
  1788. dm_device.state = DM_INIT_ERROR;
  1789. disable_page_reporting();
  1790. #ifdef CONFIG_MEMORY_HOTPLUG
  1791. unregister_memory_notifier(&hv_memory_nb);
  1792. restore_online_page_callback(&hv_online_page);
  1793. #endif
  1794. return ret;
  1795. }
  1796. static const struct hv_vmbus_device_id id_table[] = {
  1797. /* Dynamic Memory Class ID */
  1798. /* 525074DC-8985-46e2-8057-A307DC18A502 */
  1799. { HV_DM_GUID, },
  1800. { },
  1801. };
  1802. MODULE_DEVICE_TABLE(vmbus, id_table);
  1803. static struct hv_driver balloon_drv = {
  1804. .name = "hv_balloon",
  1805. .id_table = id_table,
  1806. .probe = balloon_probe,
  1807. .remove = balloon_remove,
  1808. .suspend = balloon_suspend,
  1809. .resume = balloon_resume,
  1810. .driver = {
  1811. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1812. },
  1813. };
  1814. static int __init init_balloon_drv(void)
  1815. {
  1816. return vmbus_driver_register(&balloon_drv);
  1817. }
  1818. module_init(init_balloon_drv);
  1819. MODULE_DESCRIPTION("Hyper-V Balloon");
  1820. MODULE_LICENSE("GPL");