hv_balloon.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  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. lockdep_assert_held(&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 by checking its last previously backed page.
  759. * In case it is we need to bring rest (which was not
  760. * backed previously) online too.
  761. */
  762. if (start_pfn > has->start_pfn &&
  763. !PageReserved(pfn_to_page(start_pfn - 1)))
  764. hv_bring_pgs_online(has, start_pfn, pgs_ol);
  765. }
  766. if ((has->ha_end_pfn < has->end_pfn) && (pfn_cnt > 0)) {
  767. /*
  768. * We have some residual hot add range
  769. * that needs to be hot added; hot add
  770. * it now. Hot add a multiple of
  771. * of HA_CHUNK that fully covers the pages
  772. * we have.
  773. */
  774. size = (has->end_pfn - has->ha_end_pfn);
  775. if (pfn_cnt <= size) {
  776. size = ((pfn_cnt / HA_CHUNK) * HA_CHUNK);
  777. if (pfn_cnt % HA_CHUNK)
  778. size += HA_CHUNK;
  779. } else {
  780. pfn_cnt = size;
  781. }
  782. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  783. hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
  784. spin_lock_irqsave(&dm_device.ha_lock, flags);
  785. }
  786. /*
  787. * If we managed to online any pages that were given to us,
  788. * we declare success.
  789. */
  790. res = has->covered_end_pfn - old_covered_state;
  791. break;
  792. }
  793. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  794. return res;
  795. }
  796. static unsigned long process_hot_add(unsigned long pg_start,
  797. unsigned long pfn_cnt,
  798. unsigned long rg_start,
  799. unsigned long rg_size)
  800. {
  801. struct hv_hotadd_state *ha_region = NULL;
  802. int covered;
  803. unsigned long flags;
  804. if (pfn_cnt == 0)
  805. return 0;
  806. if (!dm_device.host_specified_ha_region) {
  807. covered = pfn_covered(pg_start, pfn_cnt);
  808. if (covered < 0)
  809. return 0;
  810. if (covered)
  811. goto do_pg_range;
  812. }
  813. /*
  814. * If the host has specified a hot-add range; deal with it first.
  815. */
  816. if (rg_size != 0) {
  817. ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
  818. if (!ha_region)
  819. return 0;
  820. INIT_LIST_HEAD(&ha_region->list);
  821. INIT_LIST_HEAD(&ha_region->gap_list);
  822. ha_region->start_pfn = rg_start;
  823. ha_region->ha_end_pfn = rg_start;
  824. ha_region->covered_start_pfn = pg_start;
  825. ha_region->covered_end_pfn = pg_start;
  826. ha_region->end_pfn = rg_start + rg_size;
  827. spin_lock_irqsave(&dm_device.ha_lock, flags);
  828. list_add_tail(&ha_region->list, &dm_device.ha_region_list);
  829. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  830. }
  831. do_pg_range:
  832. /*
  833. * Process the page range specified; bringing them
  834. * online if possible.
  835. */
  836. return handle_pg_range(pg_start, pfn_cnt);
  837. }
  838. #endif
  839. static void hot_add_req(struct work_struct *dummy)
  840. {
  841. struct dm_hot_add_response resp;
  842. #ifdef CONFIG_MEMORY_HOTPLUG
  843. unsigned long pg_start, pfn_cnt;
  844. unsigned long rg_start, rg_sz;
  845. #endif
  846. struct hv_dynmem_device *dm = &dm_device;
  847. memset(&resp, 0, sizeof(struct dm_hot_add_response));
  848. resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
  849. resp.hdr.size = sizeof(struct dm_hot_add_response);
  850. #ifdef CONFIG_MEMORY_HOTPLUG
  851. pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
  852. pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
  853. rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
  854. rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
  855. if ((rg_start == 0) && (!dm->host_specified_ha_region)) {
  856. unsigned long region_size;
  857. unsigned long region_start;
  858. /*
  859. * The host has not specified the hot-add region.
  860. * Based on the hot-add page range being specified,
  861. * compute a hot-add region that can cover the pages
  862. * that need to be hot-added while ensuring the alignment
  863. * and size requirements of Linux as it relates to hot-add.
  864. */
  865. region_start = pg_start;
  866. region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK;
  867. if (pfn_cnt % HA_CHUNK)
  868. region_size += HA_CHUNK;
  869. region_start = (pg_start / HA_CHUNK) * HA_CHUNK;
  870. rg_start = region_start;
  871. rg_sz = region_size;
  872. }
  873. if (do_hot_add)
  874. resp.page_count = process_hot_add(pg_start, pfn_cnt,
  875. rg_start, rg_sz);
  876. dm->num_pages_added += resp.page_count;
  877. #endif
  878. /*
  879. * The result field of the response structure has the
  880. * following semantics:
  881. *
  882. * 1. If all or some pages hot-added: Guest should return success.
  883. *
  884. * 2. If no pages could be hot-added:
  885. *
  886. * If the guest returns success, then the host
  887. * will not attempt any further hot-add operations. This
  888. * signifies a permanent failure.
  889. *
  890. * If the guest returns failure, then this failure will be
  891. * treated as a transient failure and the host may retry the
  892. * hot-add operation after some delay.
  893. */
  894. if (resp.page_count > 0)
  895. resp.result = 1;
  896. else if (!do_hot_add)
  897. resp.result = 1;
  898. else
  899. resp.result = 0;
  900. if (!do_hot_add || (resp.page_count == 0))
  901. pr_err("Memory hot add failed\n");
  902. dm->state = DM_INITIALIZED;
  903. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  904. vmbus_sendpacket(dm->dev->channel, &resp,
  905. sizeof(struct dm_hot_add_response),
  906. (unsigned long)NULL,
  907. VM_PKT_DATA_INBAND, 0);
  908. }
  909. static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
  910. {
  911. struct dm_info_header *info_hdr;
  912. info_hdr = (struct dm_info_header *)msg->info;
  913. switch (info_hdr->type) {
  914. case INFO_TYPE_MAX_PAGE_CNT:
  915. if (info_hdr->data_size == sizeof(__u64)) {
  916. __u64 *max_page_count = (__u64 *)&info_hdr[1];
  917. pr_info("Max. dynamic memory size: %llu MB\n",
  918. (*max_page_count) >> (20 - PAGE_SHIFT));
  919. }
  920. break;
  921. default:
  922. pr_warn("Received Unknown type: %d\n", info_hdr->type);
  923. }
  924. }
  925. static unsigned long compute_balloon_floor(void)
  926. {
  927. unsigned long min_pages;
  928. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  929. /* Simple continuous piecewiese linear function:
  930. * max MiB -> min MiB gradient
  931. * 0 0
  932. * 16 16
  933. * 32 24
  934. * 128 72 (1/2)
  935. * 512 168 (1/4)
  936. * 2048 360 (1/8)
  937. * 8192 744 (1/16)
  938. * 32768 1512 (1/32)
  939. */
  940. if (totalram_pages < MB2PAGES(128))
  941. min_pages = MB2PAGES(8) + (totalram_pages >> 1);
  942. else if (totalram_pages < MB2PAGES(512))
  943. min_pages = MB2PAGES(40) + (totalram_pages >> 2);
  944. else if (totalram_pages < MB2PAGES(2048))
  945. min_pages = MB2PAGES(104) + (totalram_pages >> 3);
  946. else if (totalram_pages < MB2PAGES(8192))
  947. min_pages = MB2PAGES(232) + (totalram_pages >> 4);
  948. else
  949. min_pages = MB2PAGES(488) + (totalram_pages >> 5);
  950. #undef MB2PAGES
  951. return min_pages;
  952. }
  953. /*
  954. * Post our status as it relates memory pressure to the
  955. * host. Host expects the guests to post this status
  956. * periodically at 1 second intervals.
  957. *
  958. * The metrics specified in this protocol are very Windows
  959. * specific and so we cook up numbers here to convey our memory
  960. * pressure.
  961. */
  962. static void post_status(struct hv_dynmem_device *dm)
  963. {
  964. struct dm_status status;
  965. unsigned long now = jiffies;
  966. unsigned long last_post = last_post_time;
  967. if (pressure_report_delay > 0) {
  968. --pressure_report_delay;
  969. return;
  970. }
  971. if (!time_after(now, (last_post_time + HZ)))
  972. return;
  973. memset(&status, 0, sizeof(struct dm_status));
  974. status.hdr.type = DM_STATUS_REPORT;
  975. status.hdr.size = sizeof(struct dm_status);
  976. status.hdr.trans_id = atomic_inc_return(&trans_id);
  977. /*
  978. * The host expects the guest to report free and committed memory.
  979. * Furthermore, the host expects the pressure information to include
  980. * the ballooned out pages. For a given amount of memory that we are
  981. * managing we need to compute a floor below which we should not
  982. * balloon. Compute this and add it to the pressure report.
  983. * We also need to report all offline pages (num_pages_added -
  984. * num_pages_onlined) as committed to the host, otherwise it can try
  985. * asking us to balloon them out.
  986. */
  987. status.num_avail = si_mem_available();
  988. status.num_committed = vm_memory_committed() +
  989. dm->num_pages_ballooned +
  990. (dm->num_pages_added > dm->num_pages_onlined ?
  991. dm->num_pages_added - dm->num_pages_onlined : 0) +
  992. compute_balloon_floor();
  993. trace_balloon_status(status.num_avail, status.num_committed,
  994. vm_memory_committed(), dm->num_pages_ballooned,
  995. dm->num_pages_added, dm->num_pages_onlined);
  996. /*
  997. * If our transaction ID is no longer current, just don't
  998. * send the status. This can happen if we were interrupted
  999. * after we picked our transaction ID.
  1000. */
  1001. if (status.hdr.trans_id != atomic_read(&trans_id))
  1002. return;
  1003. /*
  1004. * If the last post time that we sampled has changed,
  1005. * we have raced, don't post the status.
  1006. */
  1007. if (last_post != last_post_time)
  1008. return;
  1009. last_post_time = jiffies;
  1010. vmbus_sendpacket(dm->dev->channel, &status,
  1011. sizeof(struct dm_status),
  1012. (unsigned long)NULL,
  1013. VM_PKT_DATA_INBAND, 0);
  1014. }
  1015. static void free_balloon_pages(struct hv_dynmem_device *dm,
  1016. union dm_mem_page_range *range_array)
  1017. {
  1018. int num_pages = range_array->finfo.page_cnt;
  1019. __u64 start_frame = range_array->finfo.start_page;
  1020. struct page *pg;
  1021. int i;
  1022. for (i = 0; i < num_pages; i++) {
  1023. pg = pfn_to_page(i + start_frame);
  1024. __free_page(pg);
  1025. dm->num_pages_ballooned--;
  1026. }
  1027. }
  1028. static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
  1029. unsigned int num_pages,
  1030. struct dm_balloon_response *bl_resp,
  1031. int alloc_unit)
  1032. {
  1033. unsigned int i = 0;
  1034. struct page *pg;
  1035. if (num_pages < alloc_unit)
  1036. return 0;
  1037. for (i = 0; (i * alloc_unit) < num_pages; 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 num_pages;
  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. /* The host balloons pages in 2M granularity. */
  1077. WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
  1078. /*
  1079. * We will attempt 2M allocations. However, if we fail to
  1080. * allocate 2M chunks, we will go back to 4k allocations.
  1081. */
  1082. alloc_unit = 512;
  1083. avail_pages = si_mem_available();
  1084. floor = compute_balloon_floor();
  1085. /* Refuse to balloon below the floor, keep the 2M granularity. */
  1086. if (avail_pages < num_pages || avail_pages - num_pages < floor) {
  1087. pr_warn("Balloon request will be partially fulfilled. %s\n",
  1088. avail_pages < num_pages ? "Not enough memory." :
  1089. "Balloon floor reached.");
  1090. num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
  1091. num_pages -= num_pages % PAGES_IN_2M;
  1092. }
  1093. while (!done) {
  1094. bl_resp = (struct dm_balloon_response *)send_buffer;
  1095. memset(send_buffer, 0, PAGE_SIZE);
  1096. bl_resp->hdr.type = DM_BALLOON_RESPONSE;
  1097. bl_resp->hdr.size = sizeof(struct dm_balloon_response);
  1098. bl_resp->more_pages = 1;
  1099. num_pages -= num_ballooned;
  1100. num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
  1101. bl_resp, alloc_unit);
  1102. if (alloc_unit != 1 && num_ballooned == 0) {
  1103. alloc_unit = 1;
  1104. continue;
  1105. }
  1106. if (num_ballooned == 0 || num_ballooned == num_pages) {
  1107. pr_debug("Ballooned %u out of %u requested pages.\n",
  1108. num_pages, dm_device.balloon_wrk.num_pages);
  1109. bl_resp->more_pages = 0;
  1110. done = true;
  1111. dm_device.state = DM_INITIALIZED;
  1112. }
  1113. /*
  1114. * We are pushing a lot of data through the channel;
  1115. * deal with transient failures caused because of the
  1116. * lack of space in the ring buffer.
  1117. */
  1118. do {
  1119. bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
  1120. ret = vmbus_sendpacket(dm_device.dev->channel,
  1121. bl_resp,
  1122. bl_resp->hdr.size,
  1123. (unsigned long)NULL,
  1124. VM_PKT_DATA_INBAND, 0);
  1125. if (ret == -EAGAIN)
  1126. msleep(20);
  1127. post_status(&dm_device);
  1128. } while (ret == -EAGAIN);
  1129. if (ret) {
  1130. /*
  1131. * Free up the memory we allocatted.
  1132. */
  1133. pr_err("Balloon response failed\n");
  1134. for (i = 0; i < bl_resp->range_count; i++)
  1135. free_balloon_pages(&dm_device,
  1136. &bl_resp->range_array[i]);
  1137. done = true;
  1138. }
  1139. }
  1140. }
  1141. static void balloon_down(struct hv_dynmem_device *dm,
  1142. struct dm_unballoon_request *req)
  1143. {
  1144. union dm_mem_page_range *range_array = req->range_array;
  1145. int range_count = req->range_count;
  1146. struct dm_unballoon_response resp;
  1147. int i;
  1148. unsigned int prev_pages_ballooned = dm->num_pages_ballooned;
  1149. for (i = 0; i < range_count; i++) {
  1150. free_balloon_pages(dm, &range_array[i]);
  1151. complete(&dm_device.config_event);
  1152. }
  1153. pr_debug("Freed %u ballooned pages.\n",
  1154. prev_pages_ballooned - dm->num_pages_ballooned);
  1155. if (req->more_pages == 1)
  1156. return;
  1157. memset(&resp, 0, sizeof(struct dm_unballoon_response));
  1158. resp.hdr.type = DM_UNBALLOON_RESPONSE;
  1159. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  1160. resp.hdr.size = sizeof(struct dm_unballoon_response);
  1161. vmbus_sendpacket(dm_device.dev->channel, &resp,
  1162. sizeof(struct dm_unballoon_response),
  1163. (unsigned long)NULL,
  1164. VM_PKT_DATA_INBAND, 0);
  1165. dm->state = DM_INITIALIZED;
  1166. }
  1167. static void balloon_onchannelcallback(void *context);
  1168. static int dm_thread_func(void *dm_dev)
  1169. {
  1170. struct hv_dynmem_device *dm = dm_dev;
  1171. while (!kthread_should_stop()) {
  1172. wait_for_completion_interruptible_timeout(
  1173. &dm_device.config_event, 1*HZ);
  1174. /*
  1175. * The host expects us to post information on the memory
  1176. * pressure every second.
  1177. */
  1178. reinit_completion(&dm_device.config_event);
  1179. post_status(dm);
  1180. }
  1181. return 0;
  1182. }
  1183. static void version_resp(struct hv_dynmem_device *dm,
  1184. struct dm_version_response *vresp)
  1185. {
  1186. struct dm_version_request version_req;
  1187. int ret;
  1188. if (vresp->is_accepted) {
  1189. /*
  1190. * We are done; wakeup the
  1191. * context waiting for version
  1192. * negotiation.
  1193. */
  1194. complete(&dm->host_event);
  1195. return;
  1196. }
  1197. /*
  1198. * If there are more versions to try, continue
  1199. * with negotiations; if not
  1200. * shutdown the service since we are not able
  1201. * to negotiate a suitable version number
  1202. * with the host.
  1203. */
  1204. if (dm->next_version == 0)
  1205. goto version_error;
  1206. memset(&version_req, 0, sizeof(struct dm_version_request));
  1207. version_req.hdr.type = DM_VERSION_REQUEST;
  1208. version_req.hdr.size = sizeof(struct dm_version_request);
  1209. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1210. version_req.version.version = dm->next_version;
  1211. dm->version = version_req.version.version;
  1212. /*
  1213. * Set the next version to try in case current version fails.
  1214. * Win7 protocol ought to be the last one to try.
  1215. */
  1216. switch (version_req.version.version) {
  1217. case DYNMEM_PROTOCOL_VERSION_WIN8:
  1218. dm->next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
  1219. version_req.is_last_attempt = 0;
  1220. break;
  1221. default:
  1222. dm->next_version = 0;
  1223. version_req.is_last_attempt = 1;
  1224. }
  1225. ret = vmbus_sendpacket(dm->dev->channel, &version_req,
  1226. sizeof(struct dm_version_request),
  1227. (unsigned long)NULL,
  1228. VM_PKT_DATA_INBAND, 0);
  1229. if (ret)
  1230. goto version_error;
  1231. return;
  1232. version_error:
  1233. dm->state = DM_INIT_ERROR;
  1234. complete(&dm->host_event);
  1235. }
  1236. static void cap_resp(struct hv_dynmem_device *dm,
  1237. struct dm_capabilities_resp_msg *cap_resp)
  1238. {
  1239. if (!cap_resp->is_accepted) {
  1240. pr_err("Capabilities not accepted by host\n");
  1241. dm->state = DM_INIT_ERROR;
  1242. }
  1243. complete(&dm->host_event);
  1244. }
  1245. static void balloon_onchannelcallback(void *context)
  1246. {
  1247. struct hv_device *dev = context;
  1248. u32 recvlen;
  1249. u64 requestid;
  1250. struct dm_message *dm_msg;
  1251. struct dm_header *dm_hdr;
  1252. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1253. struct dm_balloon *bal_msg;
  1254. struct dm_hot_add *ha_msg;
  1255. union dm_mem_page_range *ha_pg_range;
  1256. union dm_mem_page_range *ha_region;
  1257. memset(recv_buffer, 0, sizeof(recv_buffer));
  1258. vmbus_recvpacket(dev->channel, recv_buffer,
  1259. PAGE_SIZE, &recvlen, &requestid);
  1260. if (recvlen > 0) {
  1261. dm_msg = (struct dm_message *)recv_buffer;
  1262. dm_hdr = &dm_msg->hdr;
  1263. switch (dm_hdr->type) {
  1264. case DM_VERSION_RESPONSE:
  1265. version_resp(dm,
  1266. (struct dm_version_response *)dm_msg);
  1267. break;
  1268. case DM_CAPABILITIES_RESPONSE:
  1269. cap_resp(dm,
  1270. (struct dm_capabilities_resp_msg *)dm_msg);
  1271. break;
  1272. case DM_BALLOON_REQUEST:
  1273. if (dm->state == DM_BALLOON_UP)
  1274. pr_warn("Currently ballooning\n");
  1275. bal_msg = (struct dm_balloon *)recv_buffer;
  1276. dm->state = DM_BALLOON_UP;
  1277. dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
  1278. schedule_work(&dm_device.balloon_wrk.wrk);
  1279. break;
  1280. case DM_UNBALLOON_REQUEST:
  1281. dm->state = DM_BALLOON_DOWN;
  1282. balloon_down(dm,
  1283. (struct dm_unballoon_request *)recv_buffer);
  1284. break;
  1285. case DM_MEM_HOT_ADD_REQUEST:
  1286. if (dm->state == DM_HOT_ADD)
  1287. pr_warn("Currently hot-adding\n");
  1288. dm->state = DM_HOT_ADD;
  1289. ha_msg = (struct dm_hot_add *)recv_buffer;
  1290. if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
  1291. /*
  1292. * This is a normal hot-add request specifying
  1293. * hot-add memory.
  1294. */
  1295. dm->host_specified_ha_region = false;
  1296. ha_pg_range = &ha_msg->range;
  1297. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1298. dm->ha_wrk.ha_region_range.page_range = 0;
  1299. } else {
  1300. /*
  1301. * Host is specifying that we first hot-add
  1302. * a region and then partially populate this
  1303. * region.
  1304. */
  1305. dm->host_specified_ha_region = true;
  1306. ha_pg_range = &ha_msg->range;
  1307. ha_region = &ha_pg_range[1];
  1308. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1309. dm->ha_wrk.ha_region_range = *ha_region;
  1310. }
  1311. schedule_work(&dm_device.ha_wrk.wrk);
  1312. break;
  1313. case DM_INFO_MESSAGE:
  1314. process_info(dm, (struct dm_info_msg *)dm_msg);
  1315. break;
  1316. default:
  1317. pr_warn("Unhandled message: type: %d\n", dm_hdr->type);
  1318. }
  1319. }
  1320. }
  1321. static int balloon_probe(struct hv_device *dev,
  1322. const struct hv_vmbus_device_id *dev_id)
  1323. {
  1324. int ret;
  1325. unsigned long t;
  1326. struct dm_version_request version_req;
  1327. struct dm_capabilities cap_msg;
  1328. #ifdef CONFIG_MEMORY_HOTPLUG
  1329. do_hot_add = hot_add;
  1330. #else
  1331. do_hot_add = false;
  1332. #endif
  1333. /*
  1334. * First allocate a send buffer.
  1335. */
  1336. send_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1337. if (!send_buffer)
  1338. return -ENOMEM;
  1339. ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
  1340. balloon_onchannelcallback, dev);
  1341. if (ret)
  1342. goto probe_error0;
  1343. dm_device.dev = dev;
  1344. dm_device.state = DM_INITIALIZING;
  1345. dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN8;
  1346. init_completion(&dm_device.host_event);
  1347. init_completion(&dm_device.config_event);
  1348. INIT_LIST_HEAD(&dm_device.ha_region_list);
  1349. spin_lock_init(&dm_device.ha_lock);
  1350. INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
  1351. INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
  1352. dm_device.host_specified_ha_region = false;
  1353. dm_device.thread =
  1354. kthread_run(dm_thread_func, &dm_device, "hv_balloon");
  1355. if (IS_ERR(dm_device.thread)) {
  1356. ret = PTR_ERR(dm_device.thread);
  1357. goto probe_error1;
  1358. }
  1359. #ifdef CONFIG_MEMORY_HOTPLUG
  1360. set_online_page_callback(&hv_online_page);
  1361. register_memory_notifier(&hv_memory_nb);
  1362. #endif
  1363. hv_set_drvdata(dev, &dm_device);
  1364. /*
  1365. * Initiate the hand shake with the host and negotiate
  1366. * a version that the host can support. We start with the
  1367. * highest version number and go down if the host cannot
  1368. * support it.
  1369. */
  1370. memset(&version_req, 0, sizeof(struct dm_version_request));
  1371. version_req.hdr.type = DM_VERSION_REQUEST;
  1372. version_req.hdr.size = sizeof(struct dm_version_request);
  1373. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1374. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN10;
  1375. version_req.is_last_attempt = 0;
  1376. dm_device.version = version_req.version.version;
  1377. ret = vmbus_sendpacket(dev->channel, &version_req,
  1378. sizeof(struct dm_version_request),
  1379. (unsigned long)NULL,
  1380. VM_PKT_DATA_INBAND, 0);
  1381. if (ret)
  1382. goto probe_error2;
  1383. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  1384. if (t == 0) {
  1385. ret = -ETIMEDOUT;
  1386. goto probe_error2;
  1387. }
  1388. /*
  1389. * If we could not negotiate a compatible version with the host
  1390. * fail the probe function.
  1391. */
  1392. if (dm_device.state == DM_INIT_ERROR) {
  1393. ret = -ETIMEDOUT;
  1394. goto probe_error2;
  1395. }
  1396. pr_info("Using Dynamic Memory protocol version %u.%u\n",
  1397. DYNMEM_MAJOR_VERSION(dm_device.version),
  1398. DYNMEM_MINOR_VERSION(dm_device.version));
  1399. /*
  1400. * Now submit our capabilities to the host.
  1401. */
  1402. memset(&cap_msg, 0, sizeof(struct dm_capabilities));
  1403. cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
  1404. cap_msg.hdr.size = sizeof(struct dm_capabilities);
  1405. cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
  1406. cap_msg.caps.cap_bits.balloon = 1;
  1407. cap_msg.caps.cap_bits.hot_add = 1;
  1408. /*
  1409. * Specify our alignment requirements as it relates
  1410. * memory hot-add. Specify 128MB alignment.
  1411. */
  1412. cap_msg.caps.cap_bits.hot_add_alignment = 7;
  1413. /*
  1414. * Currently the host does not use these
  1415. * values and we set them to what is done in the
  1416. * Windows driver.
  1417. */
  1418. cap_msg.min_page_cnt = 0;
  1419. cap_msg.max_page_number = -1;
  1420. ret = vmbus_sendpacket(dev->channel, &cap_msg,
  1421. sizeof(struct dm_capabilities),
  1422. (unsigned long)NULL,
  1423. VM_PKT_DATA_INBAND, 0);
  1424. if (ret)
  1425. goto probe_error2;
  1426. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  1427. if (t == 0) {
  1428. ret = -ETIMEDOUT;
  1429. goto probe_error2;
  1430. }
  1431. /*
  1432. * If the host does not like our capabilities,
  1433. * fail the probe function.
  1434. */
  1435. if (dm_device.state == DM_INIT_ERROR) {
  1436. ret = -ETIMEDOUT;
  1437. goto probe_error2;
  1438. }
  1439. dm_device.state = DM_INITIALIZED;
  1440. last_post_time = jiffies;
  1441. return 0;
  1442. probe_error2:
  1443. #ifdef CONFIG_MEMORY_HOTPLUG
  1444. restore_online_page_callback(&hv_online_page);
  1445. #endif
  1446. kthread_stop(dm_device.thread);
  1447. probe_error1:
  1448. vmbus_close(dev->channel);
  1449. probe_error0:
  1450. kfree(send_buffer);
  1451. return ret;
  1452. }
  1453. static int balloon_remove(struct hv_device *dev)
  1454. {
  1455. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1456. struct hv_hotadd_state *has, *tmp;
  1457. struct hv_hotadd_gap *gap, *tmp_gap;
  1458. unsigned long flags;
  1459. if (dm->num_pages_ballooned != 0)
  1460. pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
  1461. cancel_work_sync(&dm->balloon_wrk.wrk);
  1462. cancel_work_sync(&dm->ha_wrk.wrk);
  1463. vmbus_close(dev->channel);
  1464. kthread_stop(dm->thread);
  1465. kfree(send_buffer);
  1466. #ifdef CONFIG_MEMORY_HOTPLUG
  1467. restore_online_page_callback(&hv_online_page);
  1468. unregister_memory_notifier(&hv_memory_nb);
  1469. #endif
  1470. spin_lock_irqsave(&dm_device.ha_lock, flags);
  1471. list_for_each_entry_safe(has, tmp, &dm->ha_region_list, list) {
  1472. list_for_each_entry_safe(gap, tmp_gap, &has->gap_list, list) {
  1473. list_del(&gap->list);
  1474. kfree(gap);
  1475. }
  1476. list_del(&has->list);
  1477. kfree(has);
  1478. }
  1479. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  1480. return 0;
  1481. }
  1482. static const struct hv_vmbus_device_id id_table[] = {
  1483. /* Dynamic Memory Class ID */
  1484. /* 525074DC-8985-46e2-8057-A307DC18A502 */
  1485. { HV_DM_GUID, },
  1486. { },
  1487. };
  1488. MODULE_DEVICE_TABLE(vmbus, id_table);
  1489. static struct hv_driver balloon_drv = {
  1490. .name = "hv_balloon",
  1491. .id_table = id_table,
  1492. .probe = balloon_probe,
  1493. .remove = balloon_remove,
  1494. .driver = {
  1495. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1496. },
  1497. };
  1498. static int __init init_balloon_drv(void)
  1499. {
  1500. return vmbus_driver_register(&balloon_drv);
  1501. }
  1502. module_init(init_balloon_drv);
  1503. MODULE_DESCRIPTION("Hyper-V Balloon");
  1504. MODULE_LICENSE("GPL");