hv_balloon.c 41 KB

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