hv_balloon.c 52 KB

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