hv_balloon.c 43 KB

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