hv_balloon.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140
  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 <hyperv/hvhdk.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, secs_to_jiffies(5));
  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. scoped_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. return;
  668. }
  669. }
  670. generic_online_page(pg, order);
  671. }
  672. static int pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
  673. {
  674. struct hv_hotadd_state *has;
  675. struct hv_hotadd_gap *gap;
  676. unsigned long residual;
  677. int ret = 0;
  678. guard(spinlock_irqsave)(&dm_device.ha_lock);
  679. list_for_each_entry(has, &dm_device.ha_region_list, list) {
  680. /*
  681. * If the pfn range we are dealing with is not in the current
  682. * "hot add block", move on.
  683. */
  684. if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
  685. continue;
  686. /*
  687. * If the current start pfn is not where the covered_end
  688. * is, create a gap and update covered_end_pfn.
  689. */
  690. if (has->covered_end_pfn != start_pfn) {
  691. gap = kzalloc_obj(struct hv_hotadd_gap, GFP_ATOMIC);
  692. if (!gap) {
  693. ret = -ENOMEM;
  694. break;
  695. }
  696. INIT_LIST_HEAD(&gap->list);
  697. gap->start_pfn = has->covered_end_pfn;
  698. gap->end_pfn = start_pfn;
  699. list_add_tail(&gap->list, &has->gap_list);
  700. has->covered_end_pfn = start_pfn;
  701. }
  702. /*
  703. * If the current hot add-request extends beyond
  704. * our current limit; extend it.
  705. */
  706. if ((start_pfn + pfn_cnt) > has->end_pfn) {
  707. /* Extend the region by multiples of ha_pages_in_chunk */
  708. residual = (start_pfn + pfn_cnt - has->end_pfn);
  709. has->end_pfn += ALIGN(residual, ha_pages_in_chunk);
  710. }
  711. ret = 1;
  712. break;
  713. }
  714. return ret;
  715. }
  716. static unsigned long handle_pg_range(unsigned long pg_start,
  717. unsigned long pg_count)
  718. {
  719. unsigned long start_pfn = pg_start;
  720. unsigned long pfn_cnt = pg_count;
  721. unsigned long size;
  722. struct hv_hotadd_state *has;
  723. unsigned long pgs_ol = 0;
  724. unsigned long old_covered_state;
  725. unsigned long res = 0, flags;
  726. pr_debug("Hot adding %lu pages starting at pfn 0x%lx.\n", pg_count,
  727. pg_start);
  728. spin_lock_irqsave(&dm_device.ha_lock, flags);
  729. list_for_each_entry(has, &dm_device.ha_region_list, list) {
  730. /*
  731. * If the pfn range we are dealing with is not in the current
  732. * "hot add block", move on.
  733. */
  734. if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
  735. continue;
  736. old_covered_state = has->covered_end_pfn;
  737. if (start_pfn < has->ha_end_pfn) {
  738. /*
  739. * This is the case where we are backing pages
  740. * in an already hot added region. Bring
  741. * these pages online first.
  742. */
  743. pgs_ol = has->ha_end_pfn - start_pfn;
  744. if (pgs_ol > pfn_cnt)
  745. pgs_ol = pfn_cnt;
  746. has->covered_end_pfn += pgs_ol;
  747. pfn_cnt -= pgs_ol;
  748. /*
  749. * Check if the corresponding memory block is already
  750. * online. It is possible to observe struct pages still
  751. * being uninitialized here so check section instead.
  752. * In case the section is online we need to bring the
  753. * rest of pfns (which were not backed previously)
  754. * online too.
  755. */
  756. if (start_pfn > has->start_pfn &&
  757. online_section_nr(pfn_to_section_nr(start_pfn)))
  758. hv_bring_pgs_online(has, start_pfn, pgs_ol);
  759. }
  760. if (has->ha_end_pfn < has->end_pfn && pfn_cnt > 0) {
  761. /*
  762. * We have some residual hot add range
  763. * that needs to be hot added; hot add
  764. * it now. Hot add a multiple of
  765. * ha_pages_in_chunk that fully covers the pages
  766. * we have.
  767. */
  768. size = (has->end_pfn - has->ha_end_pfn);
  769. if (pfn_cnt <= size) {
  770. size = ALIGN(pfn_cnt, ha_pages_in_chunk);
  771. } else {
  772. pfn_cnt = size;
  773. }
  774. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  775. hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
  776. spin_lock_irqsave(&dm_device.ha_lock, flags);
  777. }
  778. /*
  779. * If we managed to online any pages that were given to us,
  780. * we declare success.
  781. */
  782. res = has->covered_end_pfn - old_covered_state;
  783. break;
  784. }
  785. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  786. return res;
  787. }
  788. static unsigned long process_hot_add(unsigned long pg_start,
  789. unsigned long pfn_cnt,
  790. unsigned long rg_start,
  791. unsigned long rg_size)
  792. {
  793. struct hv_hotadd_state *ha_region = NULL;
  794. int covered;
  795. if (pfn_cnt == 0)
  796. return 0;
  797. if (!dm_device.host_specified_ha_region) {
  798. covered = pfn_covered(pg_start, pfn_cnt);
  799. if (covered < 0)
  800. return 0;
  801. if (covered)
  802. goto do_pg_range;
  803. }
  804. /*
  805. * If the host has specified a hot-add range; deal with it first.
  806. */
  807. if (rg_size != 0) {
  808. ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
  809. if (!ha_region)
  810. return 0;
  811. INIT_LIST_HEAD(&ha_region->list);
  812. INIT_LIST_HEAD(&ha_region->gap_list);
  813. ha_region->start_pfn = rg_start;
  814. ha_region->ha_end_pfn = rg_start;
  815. ha_region->covered_start_pfn = pg_start;
  816. ha_region->covered_end_pfn = pg_start;
  817. ha_region->end_pfn = rg_start + rg_size;
  818. scoped_guard(spinlock_irqsave, &dm_device.ha_lock) {
  819. list_add_tail(&ha_region->list, &dm_device.ha_region_list);
  820. }
  821. }
  822. do_pg_range:
  823. /*
  824. * Process the page range specified; bringing them
  825. * online if possible.
  826. */
  827. return handle_pg_range(pg_start, pfn_cnt);
  828. }
  829. #endif
  830. static void hot_add_req(struct work_struct *dummy)
  831. {
  832. struct dm_hot_add_response resp;
  833. #ifdef CONFIG_MEMORY_HOTPLUG
  834. unsigned long pg_start, pfn_cnt;
  835. unsigned long rg_start, rg_sz;
  836. #endif
  837. struct hv_dynmem_device *dm = &dm_device;
  838. memset(&resp, 0, sizeof(struct dm_hot_add_response));
  839. resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
  840. resp.hdr.size = sizeof(struct dm_hot_add_response);
  841. #ifdef CONFIG_MEMORY_HOTPLUG
  842. pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
  843. pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
  844. rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
  845. rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
  846. if (rg_start == 0 && !dm->host_specified_ha_region) {
  847. /*
  848. * The host has not specified the hot-add region.
  849. * Based on the hot-add page range being specified,
  850. * compute a hot-add region that can cover the pages
  851. * that need to be hot-added while ensuring the alignment
  852. * and size requirements of Linux as it relates to hot-add.
  853. */
  854. rg_start = ALIGN_DOWN(pg_start, ha_pages_in_chunk);
  855. rg_sz = ALIGN(pfn_cnt, ha_pages_in_chunk);
  856. }
  857. if (do_hot_add)
  858. resp.page_count = process_hot_add(pg_start, pfn_cnt,
  859. rg_start, rg_sz);
  860. dm->num_pages_added += resp.page_count;
  861. #endif
  862. /*
  863. * The result field of the response structure has the
  864. * following semantics:
  865. *
  866. * 1. If all or some pages hot-added: Guest should return success.
  867. *
  868. * 2. If no pages could be hot-added:
  869. *
  870. * If the guest returns success, then the host
  871. * will not attempt any further hot-add operations. This
  872. * signifies a permanent failure.
  873. *
  874. * If the guest returns failure, then this failure will be
  875. * treated as a transient failure and the host may retry the
  876. * hot-add operation after some delay.
  877. */
  878. if (resp.page_count > 0)
  879. resp.result = 1;
  880. else if (!do_hot_add)
  881. resp.result = 1;
  882. else
  883. resp.result = 0;
  884. if (!do_hot_add || resp.page_count == 0) {
  885. if (!allow_hibernation)
  886. pr_err("Memory hot add failed\n");
  887. else
  888. pr_info("Ignore hot-add request!\n");
  889. }
  890. dm->state = DM_INITIALIZED;
  891. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  892. vmbus_sendpacket(dm->dev->channel, &resp,
  893. sizeof(struct dm_hot_add_response),
  894. (unsigned long)NULL,
  895. VM_PKT_DATA_INBAND, 0);
  896. }
  897. static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
  898. {
  899. struct dm_info_header *info_hdr;
  900. info_hdr = (struct dm_info_header *)msg->info;
  901. switch (info_hdr->type) {
  902. case INFO_TYPE_MAX_PAGE_CNT:
  903. if (info_hdr->data_size == sizeof(__u64)) {
  904. __u64 *max_page_count = (__u64 *)&info_hdr[1];
  905. pr_info("Max. dynamic memory size: %llu MB\n",
  906. (*max_page_count) >> (20 - HV_HYP_PAGE_SHIFT));
  907. dm->max_dynamic_page_count = *max_page_count;
  908. }
  909. break;
  910. default:
  911. pr_warn("Received Unknown type: %d\n", info_hdr->type);
  912. }
  913. }
  914. static unsigned long compute_balloon_floor(void)
  915. {
  916. unsigned long min_pages;
  917. unsigned long nr_pages = totalram_pages();
  918. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  919. /* Simple continuous piecewiese linear function:
  920. * max MiB -> min MiB gradient
  921. * 0 0
  922. * 16 16
  923. * 32 24
  924. * 128 72 (1/2)
  925. * 512 168 (1/4)
  926. * 2048 360 (1/8)
  927. * 8192 744 (1/16)
  928. * 32768 1512 (1/32)
  929. */
  930. if (nr_pages < MB2PAGES(128))
  931. min_pages = MB2PAGES(8) + (nr_pages >> 1);
  932. else if (nr_pages < MB2PAGES(512))
  933. min_pages = MB2PAGES(40) + (nr_pages >> 2);
  934. else if (nr_pages < MB2PAGES(2048))
  935. min_pages = MB2PAGES(104) + (nr_pages >> 3);
  936. else if (nr_pages < MB2PAGES(8192))
  937. min_pages = MB2PAGES(232) + (nr_pages >> 4);
  938. else
  939. min_pages = MB2PAGES(488) + (nr_pages >> 5);
  940. #undef MB2PAGES
  941. return min_pages;
  942. }
  943. /*
  944. * Compute total committed memory pages
  945. */
  946. static unsigned long get_pages_committed(struct hv_dynmem_device *dm)
  947. {
  948. return vm_memory_committed() +
  949. dm->num_pages_ballooned +
  950. (dm->num_pages_added > dm->num_pages_onlined ?
  951. dm->num_pages_added - dm->num_pages_onlined : 0) +
  952. compute_balloon_floor();
  953. }
  954. /*
  955. * Post our status as it relates memory pressure to the
  956. * host. Host expects the guests to post this status
  957. * periodically at 1 second intervals.
  958. *
  959. * The metrics specified in this protocol are very Windows
  960. * specific and so we cook up numbers here to convey our memory
  961. * pressure.
  962. */
  963. static void post_status(struct hv_dynmem_device *dm)
  964. {
  965. struct dm_status status;
  966. unsigned long now = jiffies;
  967. unsigned long last_post = last_post_time;
  968. unsigned long num_pages_avail, num_pages_committed;
  969. if (pressure_report_delay > 0) {
  970. --pressure_report_delay;
  971. return;
  972. }
  973. if (!time_after(now, (last_post_time + HZ)))
  974. return;
  975. memset(&status, 0, sizeof(struct dm_status));
  976. status.hdr.type = DM_STATUS_REPORT;
  977. status.hdr.size = sizeof(struct dm_status);
  978. status.hdr.trans_id = atomic_inc_return(&trans_id);
  979. /*
  980. * The host expects the guest to report free and committed memory.
  981. * Furthermore, the host expects the pressure information to include
  982. * the ballooned out pages. For a given amount of memory that we are
  983. * managing we need to compute a floor below which we should not
  984. * balloon. Compute this and add it to the pressure report.
  985. * We also need to report all offline pages (num_pages_added -
  986. * num_pages_onlined) as committed to the host, otherwise it can try
  987. * asking us to balloon them out.
  988. */
  989. num_pages_avail = si_mem_available();
  990. num_pages_committed = get_pages_committed(dm);
  991. trace_balloon_status(num_pages_avail, num_pages_committed,
  992. vm_memory_committed(), dm->num_pages_ballooned,
  993. dm->num_pages_added, dm->num_pages_onlined);
  994. /* Convert numbers of pages into numbers of HV_HYP_PAGEs. */
  995. status.num_avail = num_pages_avail * NR_HV_HYP_PAGES_IN_PAGE;
  996. status.num_committed = num_pages_committed * NR_HV_HYP_PAGES_IN_PAGE;
  997. /*
  998. * If our transaction ID is no longer current, just don't
  999. * send the status. This can happen if we were interrupted
  1000. * after we picked our transaction ID.
  1001. */
  1002. if (status.hdr.trans_id != atomic_read(&trans_id))
  1003. return;
  1004. /*
  1005. * If the last post time that we sampled has changed,
  1006. * we have raced, don't post the status.
  1007. */
  1008. if (last_post != last_post_time)
  1009. return;
  1010. last_post_time = jiffies;
  1011. vmbus_sendpacket(dm->dev->channel, &status,
  1012. sizeof(struct dm_status),
  1013. (unsigned long)NULL,
  1014. VM_PKT_DATA_INBAND, 0);
  1015. }
  1016. static void free_balloon_pages(struct hv_dynmem_device *dm,
  1017. union dm_mem_page_range *range_array)
  1018. {
  1019. int num_pages = range_array->finfo.page_cnt;
  1020. __u64 start_frame = range_array->finfo.start_page;
  1021. struct page *pg;
  1022. int i;
  1023. for (i = 0; i < num_pages; i++) {
  1024. pg = pfn_to_page(i + start_frame);
  1025. __ClearPageOffline(pg);
  1026. __free_page(pg);
  1027. dm->num_pages_ballooned--;
  1028. mod_node_page_state(page_pgdat(pg), NR_BALLOON_PAGES, -1);
  1029. adjust_managed_page_count(pg, 1);
  1030. }
  1031. }
  1032. static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
  1033. unsigned int num_pages,
  1034. struct dm_balloon_response *bl_resp,
  1035. int alloc_unit)
  1036. {
  1037. unsigned int i, j;
  1038. struct page *pg;
  1039. for (i = 0; i < num_pages / alloc_unit; i++) {
  1040. if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
  1041. HV_HYP_PAGE_SIZE)
  1042. return i * alloc_unit;
  1043. /*
  1044. * We execute this code in a thread context. Furthermore,
  1045. * we don't want the kernel to try too hard.
  1046. */
  1047. pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
  1048. __GFP_NOMEMALLOC | __GFP_NOWARN,
  1049. get_order(alloc_unit << PAGE_SHIFT));
  1050. if (!pg)
  1051. return i * alloc_unit;
  1052. dm->num_pages_ballooned += alloc_unit;
  1053. mod_node_page_state(page_pgdat(pg), NR_BALLOON_PAGES, alloc_unit);
  1054. /*
  1055. * If we allocatted 2M pages; split them so we
  1056. * can free them in any order we get.
  1057. */
  1058. if (alloc_unit != 1)
  1059. split_page(pg, get_order(alloc_unit << PAGE_SHIFT));
  1060. /* mark all pages offline */
  1061. for (j = 0; j < alloc_unit; j++) {
  1062. __SetPageOffline(pg + j);
  1063. adjust_managed_page_count(pg + j, -1);
  1064. }
  1065. bl_resp->range_count++;
  1066. bl_resp->range_array[i].finfo.start_page =
  1067. page_to_pfn(pg);
  1068. bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
  1069. bl_resp->hdr.size += sizeof(union dm_mem_page_range);
  1070. }
  1071. return i * alloc_unit;
  1072. }
  1073. static void balloon_up(struct work_struct *dummy)
  1074. {
  1075. unsigned int num_pages = dm_device.balloon_wrk.num_pages;
  1076. unsigned int num_ballooned = 0;
  1077. struct dm_balloon_response *bl_resp;
  1078. int alloc_unit;
  1079. int ret;
  1080. bool done = false;
  1081. int i;
  1082. long avail_pages;
  1083. unsigned long floor;
  1084. /*
  1085. * We will attempt 2M allocations. However, if we fail to
  1086. * allocate 2M chunks, we will go back to PAGE_SIZE allocations.
  1087. */
  1088. alloc_unit = PAGES_IN_2M;
  1089. avail_pages = si_mem_available();
  1090. floor = compute_balloon_floor();
  1091. /* Refuse to balloon below the floor. */
  1092. if (avail_pages < num_pages || avail_pages - num_pages < floor) {
  1093. pr_info("Balloon request will be partially fulfilled. %s\n",
  1094. avail_pages < num_pages ? "Not enough memory." :
  1095. "Balloon floor reached.");
  1096. num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
  1097. }
  1098. while (!done) {
  1099. memset(balloon_up_send_buffer, 0, HV_HYP_PAGE_SIZE);
  1100. bl_resp = (struct dm_balloon_response *)balloon_up_send_buffer;
  1101. bl_resp->hdr.type = DM_BALLOON_RESPONSE;
  1102. bl_resp->hdr.size = sizeof(struct dm_balloon_response);
  1103. bl_resp->more_pages = 1;
  1104. num_pages -= num_ballooned;
  1105. num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
  1106. bl_resp, alloc_unit);
  1107. if (alloc_unit != 1 && num_ballooned == 0) {
  1108. alloc_unit = 1;
  1109. continue;
  1110. }
  1111. if (num_ballooned == 0 || num_ballooned == num_pages) {
  1112. pr_debug("Ballooned %u out of %u requested pages.\n",
  1113. num_pages, dm_device.balloon_wrk.num_pages);
  1114. bl_resp->more_pages = 0;
  1115. done = true;
  1116. dm_device.state = DM_INITIALIZED;
  1117. }
  1118. /*
  1119. * We are pushing a lot of data through the channel;
  1120. * deal with transient failures caused because of the
  1121. * lack of space in the ring buffer.
  1122. */
  1123. do {
  1124. bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
  1125. ret = vmbus_sendpacket(dm_device.dev->channel,
  1126. bl_resp,
  1127. bl_resp->hdr.size,
  1128. (unsigned long)NULL,
  1129. VM_PKT_DATA_INBAND, 0);
  1130. if (ret == -EAGAIN)
  1131. msleep(20);
  1132. post_status(&dm_device);
  1133. } while (ret == -EAGAIN);
  1134. if (ret) {
  1135. /*
  1136. * Free up the memory we allocatted.
  1137. */
  1138. pr_err("Balloon response failed\n");
  1139. for (i = 0; i < bl_resp->range_count; i++)
  1140. free_balloon_pages(&dm_device,
  1141. &bl_resp->range_array[i]);
  1142. done = true;
  1143. }
  1144. }
  1145. }
  1146. static void balloon_down(struct hv_dynmem_device *dm,
  1147. struct dm_unballoon_request *req)
  1148. {
  1149. union dm_mem_page_range *range_array = req->range_array;
  1150. int range_count = req->range_count;
  1151. struct dm_unballoon_response resp;
  1152. int i;
  1153. unsigned int prev_pages_ballooned = dm->num_pages_ballooned;
  1154. for (i = 0; i < range_count; i++) {
  1155. free_balloon_pages(dm, &range_array[i]);
  1156. complete(&dm_device.config_event);
  1157. }
  1158. pr_debug("Freed %u ballooned pages.\n",
  1159. prev_pages_ballooned - dm->num_pages_ballooned);
  1160. if (req->more_pages == 1)
  1161. return;
  1162. memset(&resp, 0, sizeof(struct dm_unballoon_response));
  1163. resp.hdr.type = DM_UNBALLOON_RESPONSE;
  1164. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  1165. resp.hdr.size = sizeof(struct dm_unballoon_response);
  1166. vmbus_sendpacket(dm_device.dev->channel, &resp,
  1167. sizeof(struct dm_unballoon_response),
  1168. (unsigned long)NULL,
  1169. VM_PKT_DATA_INBAND, 0);
  1170. dm->state = DM_INITIALIZED;
  1171. }
  1172. static void balloon_onchannelcallback(void *context);
  1173. static int dm_thread_func(void *dm_dev)
  1174. {
  1175. struct hv_dynmem_device *dm = dm_dev;
  1176. while (!kthread_should_stop()) {
  1177. wait_for_completion_interruptible_timeout(&dm_device.config_event,
  1178. secs_to_jiffies(1));
  1179. /*
  1180. * The host expects us to post information on the memory
  1181. * pressure every second.
  1182. */
  1183. reinit_completion(&dm_device.config_event);
  1184. post_status(dm);
  1185. /*
  1186. * disable free page reporting if multiple hypercall
  1187. * failure flag set. It is not done in the page_reporting
  1188. * callback context as that causes a deadlock between
  1189. * page_reporting_process() and page_reporting_unregister()
  1190. */
  1191. if (hv_hypercall_multi_failure >= HV_MAX_FAILURES) {
  1192. pr_err("Multiple failures in cold memory discard hypercall, disabling page reporting\n");
  1193. disable_page_reporting();
  1194. /* Reset the flag after disabling reporting */
  1195. hv_hypercall_multi_failure = 0;
  1196. }
  1197. }
  1198. return 0;
  1199. }
  1200. static void version_resp(struct hv_dynmem_device *dm,
  1201. struct dm_version_response *vresp)
  1202. {
  1203. struct dm_version_request version_req;
  1204. int ret;
  1205. if (vresp->is_accepted) {
  1206. /*
  1207. * We are done; wakeup the
  1208. * context waiting for version
  1209. * negotiation.
  1210. */
  1211. complete(&dm->host_event);
  1212. return;
  1213. }
  1214. /*
  1215. * If there are more versions to try, continue
  1216. * with negotiations; if not
  1217. * shutdown the service since we are not able
  1218. * to negotiate a suitable version number
  1219. * with the host.
  1220. */
  1221. if (dm->next_version == 0)
  1222. goto version_error;
  1223. memset(&version_req, 0, sizeof(struct dm_version_request));
  1224. version_req.hdr.type = DM_VERSION_REQUEST;
  1225. version_req.hdr.size = sizeof(struct dm_version_request);
  1226. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1227. version_req.version.version = dm->next_version;
  1228. dm->version = version_req.version.version;
  1229. /*
  1230. * Set the next version to try in case current version fails.
  1231. * Win7 protocol ought to be the last one to try.
  1232. */
  1233. switch (version_req.version.version) {
  1234. case DYNMEM_PROTOCOL_VERSION_WIN8:
  1235. dm->next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
  1236. version_req.is_last_attempt = 0;
  1237. break;
  1238. default:
  1239. dm->next_version = 0;
  1240. version_req.is_last_attempt = 1;
  1241. }
  1242. ret = vmbus_sendpacket(dm->dev->channel, &version_req,
  1243. sizeof(struct dm_version_request),
  1244. (unsigned long)NULL,
  1245. VM_PKT_DATA_INBAND, 0);
  1246. if (ret)
  1247. goto version_error;
  1248. return;
  1249. version_error:
  1250. dm->state = DM_INIT_ERROR;
  1251. complete(&dm->host_event);
  1252. }
  1253. static void cap_resp(struct hv_dynmem_device *dm,
  1254. struct dm_capabilities_resp_msg *cap_resp)
  1255. {
  1256. if (!cap_resp->is_accepted) {
  1257. pr_err("Capabilities not accepted by host\n");
  1258. dm->state = DM_INIT_ERROR;
  1259. }
  1260. complete(&dm->host_event);
  1261. }
  1262. static void balloon_onchannelcallback(void *context)
  1263. {
  1264. struct hv_device *dev = context;
  1265. u32 recvlen;
  1266. u64 requestid;
  1267. struct dm_message *dm_msg;
  1268. struct dm_header *dm_hdr;
  1269. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1270. struct dm_balloon *bal_msg;
  1271. struct dm_hot_add *ha_msg;
  1272. union dm_mem_page_range *ha_pg_range;
  1273. union dm_mem_page_range *ha_region;
  1274. memset(recv_buffer, 0, sizeof(recv_buffer));
  1275. vmbus_recvpacket(dev->channel, recv_buffer,
  1276. HV_HYP_PAGE_SIZE, &recvlen, &requestid);
  1277. if (recvlen > 0) {
  1278. dm_msg = (struct dm_message *)recv_buffer;
  1279. dm_hdr = &dm_msg->hdr;
  1280. switch (dm_hdr->type) {
  1281. case DM_VERSION_RESPONSE:
  1282. version_resp(dm,
  1283. (struct dm_version_response *)dm_msg);
  1284. break;
  1285. case DM_CAPABILITIES_RESPONSE:
  1286. cap_resp(dm,
  1287. (struct dm_capabilities_resp_msg *)dm_msg);
  1288. break;
  1289. case DM_BALLOON_REQUEST:
  1290. if (allow_hibernation) {
  1291. pr_info("Ignore balloon-up request!\n");
  1292. break;
  1293. }
  1294. if (dm->state == DM_BALLOON_UP)
  1295. pr_warn("Currently ballooning\n");
  1296. bal_msg = (struct dm_balloon *)recv_buffer;
  1297. dm->state = DM_BALLOON_UP;
  1298. dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
  1299. schedule_work(&dm_device.balloon_wrk.wrk);
  1300. break;
  1301. case DM_UNBALLOON_REQUEST:
  1302. if (allow_hibernation) {
  1303. pr_info("Ignore balloon-down request!\n");
  1304. break;
  1305. }
  1306. dm->state = DM_BALLOON_DOWN;
  1307. balloon_down(dm,
  1308. (struct dm_unballoon_request *)recv_buffer);
  1309. break;
  1310. case DM_MEM_HOT_ADD_REQUEST:
  1311. if (dm->state == DM_HOT_ADD)
  1312. pr_warn("Currently hot-adding\n");
  1313. dm->state = DM_HOT_ADD;
  1314. ha_msg = (struct dm_hot_add *)recv_buffer;
  1315. if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
  1316. /*
  1317. * This is a normal hot-add request specifying
  1318. * hot-add memory.
  1319. */
  1320. dm->host_specified_ha_region = false;
  1321. ha_pg_range = &ha_msg->range;
  1322. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1323. dm->ha_wrk.ha_region_range.page_range = 0;
  1324. } else {
  1325. /*
  1326. * Host is specifying that we first hot-add
  1327. * a region and then partially populate this
  1328. * region.
  1329. */
  1330. dm->host_specified_ha_region = true;
  1331. ha_pg_range = &ha_msg->range;
  1332. ha_region = &ha_pg_range[1];
  1333. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1334. dm->ha_wrk.ha_region_range = *ha_region;
  1335. }
  1336. schedule_work(&dm_device.ha_wrk.wrk);
  1337. break;
  1338. case DM_INFO_MESSAGE:
  1339. process_info(dm, (struct dm_info_msg *)dm_msg);
  1340. break;
  1341. default:
  1342. pr_warn_ratelimited("Unhandled message: type: %d\n", dm_hdr->type);
  1343. }
  1344. }
  1345. }
  1346. #define HV_LARGE_REPORTING_ORDER 9
  1347. #define HV_LARGE_REPORTING_LEN (HV_HYP_PAGE_SIZE << \
  1348. HV_LARGE_REPORTING_ORDER)
  1349. static int hv_free_page_report(struct page_reporting_dev_info *pr_dev_info,
  1350. struct scatterlist *sgl, unsigned int nents)
  1351. {
  1352. unsigned long flags;
  1353. struct hv_memory_hint *hint;
  1354. int i, order;
  1355. u64 status;
  1356. struct scatterlist *sg;
  1357. WARN_ON_ONCE(nents > HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES);
  1358. WARN_ON_ONCE(sgl->length < (HV_HYP_PAGE_SIZE << page_reporting_order));
  1359. local_irq_save(flags);
  1360. hint = *this_cpu_ptr(hyperv_pcpu_input_arg);
  1361. if (!hint) {
  1362. local_irq_restore(flags);
  1363. return -ENOSPC;
  1364. }
  1365. hint->heat_type = HV_EXTMEM_HEAT_HINT_COLD_DISCARD;
  1366. hint->reserved = 0;
  1367. for_each_sg(sgl, sg, nents, i) {
  1368. union hv_gpa_page_range *range;
  1369. range = &hint->ranges[i];
  1370. range->address_space = 0;
  1371. order = get_order(sg->length);
  1372. /*
  1373. * Hyper-V expects the additional_pages field in the units
  1374. * of one of these 3 sizes, 4Kbytes, 2Mbytes or 1Gbytes.
  1375. * This is dictated by the values of the fields page.largesize
  1376. * and page_size.
  1377. * This code however, only uses 4Kbytes and 2Mbytes units
  1378. * and not 1Gbytes unit.
  1379. */
  1380. /* page reporting for pages 2MB or higher */
  1381. if (order >= HV_LARGE_REPORTING_ORDER) {
  1382. range->page.largepage = 1;
  1383. range->page_size = HV_GPA_PAGE_RANGE_PAGE_SIZE_2MB;
  1384. range->base_large_pfn = page_to_hvpfn(
  1385. sg_page(sg)) >> HV_LARGE_REPORTING_ORDER;
  1386. range->page.additional_pages =
  1387. (sg->length / HV_LARGE_REPORTING_LEN) - 1;
  1388. } else {
  1389. /* Page reporting for pages below 2MB */
  1390. range->page.basepfn = page_to_hvpfn(sg_page(sg));
  1391. range->page.largepage = false;
  1392. range->page.additional_pages =
  1393. (sg->length / HV_HYP_PAGE_SIZE) - 1;
  1394. }
  1395. }
  1396. status = hv_do_rep_hypercall(HV_EXT_CALL_MEMORY_HEAT_HINT, nents, 0,
  1397. hint, NULL);
  1398. local_irq_restore(flags);
  1399. if (!hv_result_success(status)) {
  1400. pr_err("Cold memory discard hypercall failed with status %llx\n",
  1401. status);
  1402. if (hv_hypercall_multi_failure > 0)
  1403. hv_hypercall_multi_failure++;
  1404. if (hv_result(status) == HV_STATUS_INVALID_PARAMETER) {
  1405. pr_err("Underlying Hyper-V does not support order less than 9. Hypercall failed\n");
  1406. pr_err("Defaulting to page_reporting_order %d\n",
  1407. pageblock_order);
  1408. page_reporting_order = pageblock_order;
  1409. hv_hypercall_multi_failure++;
  1410. return -EINVAL;
  1411. }
  1412. return -EINVAL;
  1413. }
  1414. return 0;
  1415. }
  1416. static void enable_page_reporting(void)
  1417. {
  1418. int ret;
  1419. if (!hv_query_ext_cap(HV_EXT_CAPABILITY_MEMORY_COLD_DISCARD_HINT)) {
  1420. pr_debug("Cold memory discard hint not supported by Hyper-V\n");
  1421. return;
  1422. }
  1423. BUILD_BUG_ON(PAGE_REPORTING_CAPACITY > HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES);
  1424. dm_device.pr_dev_info.report = hv_free_page_report;
  1425. /*
  1426. * We let the page_reporting_order parameter decide the order
  1427. * in the page_reporting code
  1428. */
  1429. dm_device.pr_dev_info.order = 0;
  1430. ret = page_reporting_register(&dm_device.pr_dev_info);
  1431. if (ret < 0) {
  1432. dm_device.pr_dev_info.report = NULL;
  1433. pr_err("Failed to enable cold memory discard: %d\n", ret);
  1434. } else {
  1435. pr_info("Cold memory discard hint enabled with order %d\n",
  1436. page_reporting_order);
  1437. }
  1438. }
  1439. static void disable_page_reporting(void)
  1440. {
  1441. if (dm_device.pr_dev_info.report) {
  1442. page_reporting_unregister(&dm_device.pr_dev_info);
  1443. dm_device.pr_dev_info.report = NULL;
  1444. }
  1445. }
  1446. static int ballooning_enabled(void)
  1447. {
  1448. /*
  1449. * Disable ballooning if the page size is not 4k (HV_HYP_PAGE_SIZE),
  1450. * since currently it's unclear to us whether an unballoon request can
  1451. * make sure all page ranges are guest page size aligned.
  1452. */
  1453. if (PAGE_SIZE != HV_HYP_PAGE_SIZE) {
  1454. pr_info("Ballooning disabled because page size is not 4096 bytes\n");
  1455. return 0;
  1456. }
  1457. return 1;
  1458. }
  1459. static int hot_add_enabled(void)
  1460. {
  1461. /*
  1462. * Disable hot add on ARM64, because we currently rely on
  1463. * memory_add_physaddr_to_nid() to get a node id of a hot add range,
  1464. * however ARM64's memory_add_physaddr_to_nid() always return 0 and
  1465. * DM_MEM_HOT_ADD_REQUEST doesn't have the NUMA node information for
  1466. * add_memory().
  1467. */
  1468. if (IS_ENABLED(CONFIG_ARM64)) {
  1469. pr_info("Memory hot add disabled on ARM64\n");
  1470. return 0;
  1471. }
  1472. return 1;
  1473. }
  1474. static int balloon_connect_vsp(struct hv_device *dev)
  1475. {
  1476. struct dm_version_request version_req;
  1477. struct dm_capabilities cap_msg;
  1478. unsigned long t;
  1479. int ret;
  1480. /*
  1481. * max_pkt_size should be large enough for one vmbus packet header plus
  1482. * our receive buffer size. Hyper-V sends messages up to
  1483. * HV_HYP_PAGE_SIZE bytes long on balloon channel.
  1484. */
  1485. dev->channel->max_pkt_size = HV_HYP_PAGE_SIZE * 2;
  1486. ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
  1487. balloon_onchannelcallback, dev);
  1488. if (ret)
  1489. return ret;
  1490. /*
  1491. * Initiate the hand shake with the host and negotiate
  1492. * a version that the host can support. We start with the
  1493. * highest version number and go down if the host cannot
  1494. * support it.
  1495. */
  1496. memset(&version_req, 0, sizeof(struct dm_version_request));
  1497. version_req.hdr.type = DM_VERSION_REQUEST;
  1498. version_req.hdr.size = sizeof(struct dm_version_request);
  1499. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1500. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN10;
  1501. version_req.is_last_attempt = 0;
  1502. dm_device.version = version_req.version.version;
  1503. ret = vmbus_sendpacket(dev->channel, &version_req,
  1504. sizeof(struct dm_version_request),
  1505. (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
  1506. if (ret)
  1507. goto out;
  1508. t = wait_for_completion_timeout(&dm_device.host_event, secs_to_jiffies(5));
  1509. if (t == 0) {
  1510. ret = -ETIMEDOUT;
  1511. goto out;
  1512. }
  1513. /*
  1514. * If we could not negotiate a compatible version with the host
  1515. * fail the probe function.
  1516. */
  1517. if (dm_device.state == DM_INIT_ERROR) {
  1518. ret = -EPROTO;
  1519. goto out;
  1520. }
  1521. pr_info("Using Dynamic Memory protocol version %u.%u\n",
  1522. DYNMEM_MAJOR_VERSION(dm_device.version),
  1523. DYNMEM_MINOR_VERSION(dm_device.version));
  1524. /*
  1525. * Now submit our capabilities to the host.
  1526. */
  1527. memset(&cap_msg, 0, sizeof(struct dm_capabilities));
  1528. cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
  1529. cap_msg.hdr.size = sizeof(struct dm_capabilities);
  1530. cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
  1531. /*
  1532. * When hibernation (i.e. virtual ACPI S4 state) is enabled, the host
  1533. * currently still requires the bits to be set, so we have to add code
  1534. * to fail the host's hot-add and balloon up/down requests, if any.
  1535. */
  1536. cap_msg.caps.cap_bits.balloon = ballooning_enabled();
  1537. cap_msg.caps.cap_bits.hot_add = hot_add_enabled();
  1538. /*
  1539. * Specify our alignment requirements for memory hot-add. The value is
  1540. * the log base 2 of the number of megabytes in a chunk. For example,
  1541. * with 256 MiB chunks, the value is 8. The number of MiB in a chunk
  1542. * must be a power of 2.
  1543. */
  1544. cap_msg.caps.cap_bits.hot_add_alignment =
  1545. ilog2(HA_BYTES_IN_CHUNK / SZ_1M);
  1546. /*
  1547. * Currently the host does not use these
  1548. * values and we set them to what is done in the
  1549. * Windows driver.
  1550. */
  1551. cap_msg.min_page_cnt = 0;
  1552. cap_msg.max_page_number = -1;
  1553. ret = vmbus_sendpacket(dev->channel, &cap_msg,
  1554. sizeof(struct dm_capabilities),
  1555. (unsigned long)NULL, VM_PKT_DATA_INBAND, 0);
  1556. if (ret)
  1557. goto out;
  1558. t = wait_for_completion_timeout(&dm_device.host_event, secs_to_jiffies(5));
  1559. if (t == 0) {
  1560. ret = -ETIMEDOUT;
  1561. goto out;
  1562. }
  1563. /*
  1564. * If the host does not like our capabilities,
  1565. * fail the probe function.
  1566. */
  1567. if (dm_device.state == DM_INIT_ERROR) {
  1568. ret = -EPROTO;
  1569. goto out;
  1570. }
  1571. return 0;
  1572. out:
  1573. vmbus_close(dev->channel);
  1574. return ret;
  1575. }
  1576. /*
  1577. * DEBUGFS Interface
  1578. */
  1579. #ifdef CONFIG_DEBUG_FS
  1580. /**
  1581. * hv_balloon_debug_show - shows statistics of balloon operations.
  1582. * @f: pointer to the &struct seq_file.
  1583. * @offset: ignored.
  1584. *
  1585. * Provides the statistics that can be accessed in hv-balloon in the debugfs.
  1586. *
  1587. * Return: zero on success or an error code.
  1588. */
  1589. static int hv_balloon_debug_show(struct seq_file *f, void *offset)
  1590. {
  1591. struct hv_dynmem_device *dm = f->private;
  1592. char *sname;
  1593. seq_printf(f, "%-22s: %u.%u\n", "host_version",
  1594. DYNMEM_MAJOR_VERSION(dm->version),
  1595. DYNMEM_MINOR_VERSION(dm->version));
  1596. seq_printf(f, "%-22s:", "capabilities");
  1597. if (ballooning_enabled())
  1598. seq_puts(f, " enabled");
  1599. if (hot_add_enabled())
  1600. seq_puts(f, " hot_add");
  1601. seq_puts(f, "\n");
  1602. seq_printf(f, "%-22s: %u", "state", dm->state);
  1603. switch (dm->state) {
  1604. case DM_INITIALIZING:
  1605. sname = "Initializing";
  1606. break;
  1607. case DM_INITIALIZED:
  1608. sname = "Initialized";
  1609. break;
  1610. case DM_BALLOON_UP:
  1611. sname = "Balloon Up";
  1612. break;
  1613. case DM_BALLOON_DOWN:
  1614. sname = "Balloon Down";
  1615. break;
  1616. case DM_HOT_ADD:
  1617. sname = "Hot Add";
  1618. break;
  1619. case DM_INIT_ERROR:
  1620. sname = "Error";
  1621. break;
  1622. default:
  1623. sname = "Unknown";
  1624. }
  1625. seq_printf(f, " (%s)\n", sname);
  1626. /* HV Page Size */
  1627. seq_printf(f, "%-22s: %ld\n", "page_size", HV_HYP_PAGE_SIZE);
  1628. /* Pages added with hot_add */
  1629. seq_printf(f, "%-22s: %u\n", "pages_added", dm->num_pages_added);
  1630. /* pages that are "onlined"/used from pages_added */
  1631. seq_printf(f, "%-22s: %u\n", "pages_onlined", dm->num_pages_onlined);
  1632. /* pages we have given back to host */
  1633. seq_printf(f, "%-22s: %u\n", "pages_ballooned", dm->num_pages_ballooned);
  1634. seq_printf(f, "%-22s: %lu\n", "total_pages_committed",
  1635. get_pages_committed(dm));
  1636. seq_printf(f, "%-22s: %llu\n", "max_dynamic_page_count",
  1637. dm->max_dynamic_page_count);
  1638. return 0;
  1639. }
  1640. DEFINE_SHOW_ATTRIBUTE(hv_balloon_debug);
  1641. static void hv_balloon_debugfs_init(struct hv_dynmem_device *b)
  1642. {
  1643. debugfs_create_file("hv-balloon", 0444, NULL, b,
  1644. &hv_balloon_debug_fops);
  1645. }
  1646. static void hv_balloon_debugfs_exit(struct hv_dynmem_device *b)
  1647. {
  1648. debugfs_lookup_and_remove("hv-balloon", NULL);
  1649. }
  1650. #else
  1651. static inline void hv_balloon_debugfs_init(struct hv_dynmem_device *b)
  1652. {
  1653. }
  1654. static inline void hv_balloon_debugfs_exit(struct hv_dynmem_device *b)
  1655. {
  1656. }
  1657. #endif /* CONFIG_DEBUG_FS */
  1658. static int balloon_probe(struct hv_device *dev,
  1659. const struct hv_vmbus_device_id *dev_id)
  1660. {
  1661. int ret;
  1662. allow_hibernation = hv_is_hibernation_supported();
  1663. if (allow_hibernation)
  1664. hot_add = false;
  1665. #ifdef CONFIG_MEMORY_HOTPLUG
  1666. /*
  1667. * Hot-add must operate in chunks that are of size equal to the
  1668. * memory block size because that's what the core add_memory()
  1669. * interface requires. The Hyper-V interface requires that the memory
  1670. * block size be a power of 2, which is guaranteed by the check in
  1671. * memory_dev_init().
  1672. */
  1673. ha_pages_in_chunk = memory_block_size_bytes() / PAGE_SIZE;
  1674. do_hot_add = hot_add;
  1675. #else
  1676. /*
  1677. * Without MEMORY_HOTPLUG, the guest returns a failure status for all
  1678. * hot add requests from Hyper-V, and the chunk size is used only to
  1679. * specify alignment to Hyper-V as required by the host/guest protocol.
  1680. * Somewhat arbitrarily, use 128 MiB.
  1681. */
  1682. ha_pages_in_chunk = SZ_128M / PAGE_SIZE;
  1683. do_hot_add = false;
  1684. #endif
  1685. dm_device.dev = dev;
  1686. dm_device.state = DM_INITIALIZING;
  1687. dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN8;
  1688. init_completion(&dm_device.host_event);
  1689. init_completion(&dm_device.config_event);
  1690. INIT_LIST_HEAD(&dm_device.ha_region_list);
  1691. spin_lock_init(&dm_device.ha_lock);
  1692. INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
  1693. INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
  1694. dm_device.host_specified_ha_region = false;
  1695. #ifdef CONFIG_MEMORY_HOTPLUG
  1696. set_online_page_callback(&hv_online_page);
  1697. init_completion(&dm_device.ol_waitevent);
  1698. register_memory_notifier(&hv_memory_nb);
  1699. #endif
  1700. hv_set_drvdata(dev, &dm_device);
  1701. ret = balloon_connect_vsp(dev);
  1702. if (ret != 0)
  1703. goto connect_error;
  1704. enable_page_reporting();
  1705. dm_device.state = DM_INITIALIZED;
  1706. dm_device.thread =
  1707. kthread_run(dm_thread_func, &dm_device, "hv_balloon");
  1708. if (IS_ERR(dm_device.thread)) {
  1709. ret = PTR_ERR(dm_device.thread);
  1710. goto probe_error;
  1711. }
  1712. hv_balloon_debugfs_init(&dm_device);
  1713. return 0;
  1714. probe_error:
  1715. dm_device.state = DM_INIT_ERROR;
  1716. dm_device.thread = NULL;
  1717. disable_page_reporting();
  1718. vmbus_close(dev->channel);
  1719. connect_error:
  1720. #ifdef CONFIG_MEMORY_HOTPLUG
  1721. unregister_memory_notifier(&hv_memory_nb);
  1722. restore_online_page_callback(&hv_online_page);
  1723. #endif
  1724. return ret;
  1725. }
  1726. static void balloon_remove(struct hv_device *dev)
  1727. {
  1728. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1729. struct hv_hotadd_state *has, *tmp;
  1730. struct hv_hotadd_gap *gap, *tmp_gap;
  1731. if (dm->num_pages_ballooned != 0)
  1732. pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
  1733. hv_balloon_debugfs_exit(dm);
  1734. cancel_work_sync(&dm->balloon_wrk.wrk);
  1735. cancel_work_sync(&dm->ha_wrk.wrk);
  1736. kthread_stop(dm->thread);
  1737. /*
  1738. * This is to handle the case when balloon_resume()
  1739. * call has failed and some cleanup has been done as
  1740. * a part of the error handling.
  1741. */
  1742. if (dm_device.state != DM_INIT_ERROR) {
  1743. disable_page_reporting();
  1744. vmbus_close(dev->channel);
  1745. #ifdef CONFIG_MEMORY_HOTPLUG
  1746. unregister_memory_notifier(&hv_memory_nb);
  1747. restore_online_page_callback(&hv_online_page);
  1748. #endif
  1749. }
  1750. guard(spinlock_irqsave)(&dm_device.ha_lock);
  1751. list_for_each_entry_safe(has, tmp, &dm->ha_region_list, list) {
  1752. list_for_each_entry_safe(gap, tmp_gap, &has->gap_list, list) {
  1753. list_del(&gap->list);
  1754. kfree(gap);
  1755. }
  1756. list_del(&has->list);
  1757. kfree(has);
  1758. }
  1759. }
  1760. static int balloon_suspend(struct hv_device *hv_dev)
  1761. {
  1762. struct hv_dynmem_device *dm = hv_get_drvdata(hv_dev);
  1763. tasklet_disable(&hv_dev->channel->callback_event);
  1764. cancel_work_sync(&dm->balloon_wrk.wrk);
  1765. cancel_work_sync(&dm->ha_wrk.wrk);
  1766. if (dm->thread) {
  1767. kthread_stop(dm->thread);
  1768. dm->thread = NULL;
  1769. vmbus_close(hv_dev->channel);
  1770. }
  1771. tasklet_enable(&hv_dev->channel->callback_event);
  1772. return 0;
  1773. }
  1774. static int balloon_resume(struct hv_device *dev)
  1775. {
  1776. int ret;
  1777. dm_device.state = DM_INITIALIZING;
  1778. ret = balloon_connect_vsp(dev);
  1779. if (ret != 0)
  1780. goto out;
  1781. dm_device.thread =
  1782. kthread_run(dm_thread_func, &dm_device, "hv_balloon");
  1783. if (IS_ERR(dm_device.thread)) {
  1784. ret = PTR_ERR(dm_device.thread);
  1785. dm_device.thread = NULL;
  1786. goto close_channel;
  1787. }
  1788. dm_device.state = DM_INITIALIZED;
  1789. return 0;
  1790. close_channel:
  1791. vmbus_close(dev->channel);
  1792. out:
  1793. dm_device.state = DM_INIT_ERROR;
  1794. disable_page_reporting();
  1795. #ifdef CONFIG_MEMORY_HOTPLUG
  1796. unregister_memory_notifier(&hv_memory_nb);
  1797. restore_online_page_callback(&hv_online_page);
  1798. #endif
  1799. return ret;
  1800. }
  1801. static const struct hv_vmbus_device_id id_table[] = {
  1802. /* Dynamic Memory Class ID */
  1803. /* 525074DC-8985-46e2-8057-A307DC18A502 */
  1804. { HV_DM_GUID, },
  1805. { },
  1806. };
  1807. MODULE_DEVICE_TABLE(vmbus, id_table);
  1808. static struct hv_driver balloon_drv = {
  1809. .name = "hv_balloon",
  1810. .id_table = id_table,
  1811. .probe = balloon_probe,
  1812. .remove = balloon_remove,
  1813. .suspend = balloon_suspend,
  1814. .resume = balloon_resume,
  1815. .driver = {
  1816. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1817. },
  1818. };
  1819. static int __init init_balloon_drv(void)
  1820. {
  1821. return vmbus_driver_register(&balloon_drv);
  1822. }
  1823. module_init(init_balloon_drv);
  1824. MODULE_DESCRIPTION("Hyper-V Balloon");
  1825. MODULE_LICENSE("GPL");