hv_balloon.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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. * The negotiated version agreed by host.
  484. */
  485. __u32 version;
  486. };
  487. static struct hv_dynmem_device dm_device;
  488. static void post_status(struct hv_dynmem_device *dm);
  489. #ifdef CONFIG_MEMORY_HOTPLUG
  490. static int hv_memory_notifier(struct notifier_block *nb, unsigned long val,
  491. void *v)
  492. {
  493. struct memory_notify *mem = (struct memory_notify *)v;
  494. unsigned long flags;
  495. switch (val) {
  496. case MEM_ONLINE:
  497. case MEM_CANCEL_ONLINE:
  498. if (dm_device.ha_waiting) {
  499. dm_device.ha_waiting = false;
  500. complete(&dm_device.ol_waitevent);
  501. }
  502. break;
  503. case MEM_OFFLINE:
  504. spin_lock_irqsave(&dm_device.ha_lock, flags);
  505. dm_device.num_pages_onlined -= mem->nr_pages;
  506. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  507. break;
  508. case MEM_GOING_ONLINE:
  509. case MEM_GOING_OFFLINE:
  510. case MEM_CANCEL_OFFLINE:
  511. break;
  512. }
  513. return NOTIFY_OK;
  514. }
  515. static struct notifier_block hv_memory_nb = {
  516. .notifier_call = hv_memory_notifier,
  517. .priority = 0
  518. };
  519. /* Check if the particular page is backed and can be onlined and online it. */
  520. static void hv_page_online_one(struct hv_hotadd_state *has, struct page *pg)
  521. {
  522. unsigned long cur_start_pgp;
  523. unsigned long cur_end_pgp;
  524. struct hv_hotadd_gap *gap;
  525. cur_start_pgp = (unsigned long)pfn_to_page(has->covered_start_pfn);
  526. cur_end_pgp = (unsigned long)pfn_to_page(has->covered_end_pfn);
  527. /* The page is not backed. */
  528. if (((unsigned long)pg < cur_start_pgp) ||
  529. ((unsigned long)pg >= cur_end_pgp))
  530. return;
  531. /* Check for gaps. */
  532. list_for_each_entry(gap, &has->gap_list, list) {
  533. cur_start_pgp = (unsigned long)
  534. pfn_to_page(gap->start_pfn);
  535. cur_end_pgp = (unsigned long)
  536. pfn_to_page(gap->end_pfn);
  537. if (((unsigned long)pg >= cur_start_pgp) &&
  538. ((unsigned long)pg < cur_end_pgp)) {
  539. return;
  540. }
  541. }
  542. /* This frame is currently backed; online the page. */
  543. __online_page_set_limits(pg);
  544. __online_page_increment_counters(pg);
  545. __online_page_free(pg);
  546. WARN_ON_ONCE(!spin_is_locked(&dm_device.ha_lock));
  547. dm_device.num_pages_onlined++;
  548. }
  549. static void hv_bring_pgs_online(struct hv_hotadd_state *has,
  550. unsigned long start_pfn, unsigned long size)
  551. {
  552. int i;
  553. pr_debug("Online %lu pages starting at pfn 0x%lx\n", size, start_pfn);
  554. for (i = 0; i < size; i++)
  555. hv_page_online_one(has, pfn_to_page(start_pfn + i));
  556. }
  557. static void hv_mem_hot_add(unsigned long start, unsigned long size,
  558. unsigned long pfn_count,
  559. struct hv_hotadd_state *has)
  560. {
  561. int ret = 0;
  562. int i, nid;
  563. unsigned long start_pfn;
  564. unsigned long processed_pfn;
  565. unsigned long total_pfn = pfn_count;
  566. unsigned long flags;
  567. for (i = 0; i < (size/HA_CHUNK); i++) {
  568. start_pfn = start + (i * HA_CHUNK);
  569. spin_lock_irqsave(&dm_device.ha_lock, flags);
  570. has->ha_end_pfn += HA_CHUNK;
  571. if (total_pfn > HA_CHUNK) {
  572. processed_pfn = HA_CHUNK;
  573. total_pfn -= HA_CHUNK;
  574. } else {
  575. processed_pfn = total_pfn;
  576. total_pfn = 0;
  577. }
  578. has->covered_end_pfn += processed_pfn;
  579. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  580. init_completion(&dm_device.ol_waitevent);
  581. dm_device.ha_waiting = !memhp_auto_online;
  582. nid = memory_add_physaddr_to_nid(PFN_PHYS(start_pfn));
  583. ret = add_memory(nid, PFN_PHYS((start_pfn)),
  584. (HA_CHUNK << PAGE_SHIFT));
  585. if (ret) {
  586. pr_warn("hot_add memory failed error is %d\n", ret);
  587. if (ret == -EEXIST) {
  588. /*
  589. * This error indicates that the error
  590. * is not a transient failure. This is the
  591. * case where the guest's physical address map
  592. * precludes hot adding memory. Stop all further
  593. * memory hot-add.
  594. */
  595. do_hot_add = false;
  596. }
  597. spin_lock_irqsave(&dm_device.ha_lock, flags);
  598. has->ha_end_pfn -= HA_CHUNK;
  599. has->covered_end_pfn -= processed_pfn;
  600. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  601. break;
  602. }
  603. /*
  604. * Wait for the memory block to be onlined when memory onlining
  605. * is done outside of kernel (memhp_auto_online). Since the hot
  606. * add has succeeded, it is ok to proceed even if the pages in
  607. * the hot added region have not been "onlined" within the
  608. * allowed time.
  609. */
  610. if (dm_device.ha_waiting)
  611. wait_for_completion_timeout(&dm_device.ol_waitevent,
  612. 5*HZ);
  613. post_status(&dm_device);
  614. }
  615. }
  616. static void hv_online_page(struct page *pg)
  617. {
  618. struct hv_hotadd_state *has;
  619. unsigned long cur_start_pgp;
  620. unsigned long cur_end_pgp;
  621. unsigned long flags;
  622. spin_lock_irqsave(&dm_device.ha_lock, flags);
  623. list_for_each_entry(has, &dm_device.ha_region_list, list) {
  624. cur_start_pgp = (unsigned long)
  625. pfn_to_page(has->start_pfn);
  626. cur_end_pgp = (unsigned long)pfn_to_page(has->end_pfn);
  627. /* The page belongs to a different HAS. */
  628. if (((unsigned long)pg < cur_start_pgp) ||
  629. ((unsigned long)pg >= cur_end_pgp))
  630. continue;
  631. hv_page_online_one(has, pg);
  632. break;
  633. }
  634. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  635. }
  636. static int pfn_covered(unsigned long start_pfn, unsigned long pfn_cnt)
  637. {
  638. struct hv_hotadd_state *has;
  639. struct hv_hotadd_gap *gap;
  640. unsigned long residual, new_inc;
  641. int ret = 0;
  642. unsigned long flags;
  643. spin_lock_irqsave(&dm_device.ha_lock, flags);
  644. list_for_each_entry(has, &dm_device.ha_region_list, list) {
  645. /*
  646. * If the pfn range we are dealing with is not in the current
  647. * "hot add block", move on.
  648. */
  649. if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
  650. continue;
  651. /*
  652. * If the current start pfn is not where the covered_end
  653. * is, create a gap and update covered_end_pfn.
  654. */
  655. if (has->covered_end_pfn != start_pfn) {
  656. gap = kzalloc(sizeof(struct hv_hotadd_gap), GFP_ATOMIC);
  657. if (!gap) {
  658. ret = -ENOMEM;
  659. break;
  660. }
  661. INIT_LIST_HEAD(&gap->list);
  662. gap->start_pfn = has->covered_end_pfn;
  663. gap->end_pfn = start_pfn;
  664. list_add_tail(&gap->list, &has->gap_list);
  665. has->covered_end_pfn = start_pfn;
  666. }
  667. /*
  668. * If the current hot add-request extends beyond
  669. * our current limit; extend it.
  670. */
  671. if ((start_pfn + pfn_cnt) > has->end_pfn) {
  672. residual = (start_pfn + pfn_cnt - has->end_pfn);
  673. /*
  674. * Extend the region by multiples of HA_CHUNK.
  675. */
  676. new_inc = (residual / HA_CHUNK) * HA_CHUNK;
  677. if (residual % HA_CHUNK)
  678. new_inc += HA_CHUNK;
  679. has->end_pfn += new_inc;
  680. }
  681. ret = 1;
  682. break;
  683. }
  684. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  685. return ret;
  686. }
  687. static unsigned long handle_pg_range(unsigned long pg_start,
  688. unsigned long pg_count)
  689. {
  690. unsigned long start_pfn = pg_start;
  691. unsigned long pfn_cnt = pg_count;
  692. unsigned long size;
  693. struct hv_hotadd_state *has;
  694. unsigned long pgs_ol = 0;
  695. unsigned long old_covered_state;
  696. unsigned long res = 0, flags;
  697. pr_debug("Hot adding %lu pages starting at pfn 0x%lx.\n", pg_count,
  698. pg_start);
  699. spin_lock_irqsave(&dm_device.ha_lock, flags);
  700. list_for_each_entry(has, &dm_device.ha_region_list, list) {
  701. /*
  702. * If the pfn range we are dealing with is not in the current
  703. * "hot add block", move on.
  704. */
  705. if (start_pfn < has->start_pfn || start_pfn >= has->end_pfn)
  706. continue;
  707. old_covered_state = has->covered_end_pfn;
  708. if (start_pfn < has->ha_end_pfn) {
  709. /*
  710. * This is the case where we are backing pages
  711. * in an already hot added region. Bring
  712. * these pages online first.
  713. */
  714. pgs_ol = has->ha_end_pfn - start_pfn;
  715. if (pgs_ol > pfn_cnt)
  716. pgs_ol = pfn_cnt;
  717. has->covered_end_pfn += pgs_ol;
  718. pfn_cnt -= pgs_ol;
  719. /*
  720. * Check if the corresponding memory block is already
  721. * online by checking its last previously backed page.
  722. * In case it is we need to bring rest (which was not
  723. * backed previously) online too.
  724. */
  725. if (start_pfn > has->start_pfn &&
  726. !PageReserved(pfn_to_page(start_pfn - 1)))
  727. hv_bring_pgs_online(has, start_pfn, pgs_ol);
  728. }
  729. if ((has->ha_end_pfn < has->end_pfn) && (pfn_cnt > 0)) {
  730. /*
  731. * We have some residual hot add range
  732. * that needs to be hot added; hot add
  733. * it now. Hot add a multiple of
  734. * of HA_CHUNK that fully covers the pages
  735. * we have.
  736. */
  737. size = (has->end_pfn - has->ha_end_pfn);
  738. if (pfn_cnt <= size) {
  739. size = ((pfn_cnt / HA_CHUNK) * HA_CHUNK);
  740. if (pfn_cnt % HA_CHUNK)
  741. size += HA_CHUNK;
  742. } else {
  743. pfn_cnt = size;
  744. }
  745. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  746. hv_mem_hot_add(has->ha_end_pfn, size, pfn_cnt, has);
  747. spin_lock_irqsave(&dm_device.ha_lock, flags);
  748. }
  749. /*
  750. * If we managed to online any pages that were given to us,
  751. * we declare success.
  752. */
  753. res = has->covered_end_pfn - old_covered_state;
  754. break;
  755. }
  756. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  757. return res;
  758. }
  759. static unsigned long process_hot_add(unsigned long pg_start,
  760. unsigned long pfn_cnt,
  761. unsigned long rg_start,
  762. unsigned long rg_size)
  763. {
  764. struct hv_hotadd_state *ha_region = NULL;
  765. int covered;
  766. unsigned long flags;
  767. if (pfn_cnt == 0)
  768. return 0;
  769. if (!dm_device.host_specified_ha_region) {
  770. covered = pfn_covered(pg_start, pfn_cnt);
  771. if (covered < 0)
  772. return 0;
  773. if (covered)
  774. goto do_pg_range;
  775. }
  776. /*
  777. * If the host has specified a hot-add range; deal with it first.
  778. */
  779. if (rg_size != 0) {
  780. ha_region = kzalloc(sizeof(struct hv_hotadd_state), GFP_KERNEL);
  781. if (!ha_region)
  782. return 0;
  783. INIT_LIST_HEAD(&ha_region->list);
  784. INIT_LIST_HEAD(&ha_region->gap_list);
  785. ha_region->start_pfn = rg_start;
  786. ha_region->ha_end_pfn = rg_start;
  787. ha_region->covered_start_pfn = pg_start;
  788. ha_region->covered_end_pfn = pg_start;
  789. ha_region->end_pfn = rg_start + rg_size;
  790. spin_lock_irqsave(&dm_device.ha_lock, flags);
  791. list_add_tail(&ha_region->list, &dm_device.ha_region_list);
  792. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  793. }
  794. do_pg_range:
  795. /*
  796. * Process the page range specified; bringing them
  797. * online if possible.
  798. */
  799. return handle_pg_range(pg_start, pfn_cnt);
  800. }
  801. #endif
  802. static void hot_add_req(struct work_struct *dummy)
  803. {
  804. struct dm_hot_add_response resp;
  805. #ifdef CONFIG_MEMORY_HOTPLUG
  806. unsigned long pg_start, pfn_cnt;
  807. unsigned long rg_start, rg_sz;
  808. #endif
  809. struct hv_dynmem_device *dm = &dm_device;
  810. memset(&resp, 0, sizeof(struct dm_hot_add_response));
  811. resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
  812. resp.hdr.size = sizeof(struct dm_hot_add_response);
  813. #ifdef CONFIG_MEMORY_HOTPLUG
  814. pg_start = dm->ha_wrk.ha_page_range.finfo.start_page;
  815. pfn_cnt = dm->ha_wrk.ha_page_range.finfo.page_cnt;
  816. rg_start = dm->ha_wrk.ha_region_range.finfo.start_page;
  817. rg_sz = dm->ha_wrk.ha_region_range.finfo.page_cnt;
  818. if ((rg_start == 0) && (!dm->host_specified_ha_region)) {
  819. unsigned long region_size;
  820. unsigned long region_start;
  821. /*
  822. * The host has not specified the hot-add region.
  823. * Based on the hot-add page range being specified,
  824. * compute a hot-add region that can cover the pages
  825. * that need to be hot-added while ensuring the alignment
  826. * and size requirements of Linux as it relates to hot-add.
  827. */
  828. region_start = pg_start;
  829. region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK;
  830. if (pfn_cnt % HA_CHUNK)
  831. region_size += HA_CHUNK;
  832. region_start = (pg_start / HA_CHUNK) * HA_CHUNK;
  833. rg_start = region_start;
  834. rg_sz = region_size;
  835. }
  836. if (do_hot_add)
  837. resp.page_count = process_hot_add(pg_start, pfn_cnt,
  838. rg_start, rg_sz);
  839. dm->num_pages_added += resp.page_count;
  840. #endif
  841. /*
  842. * The result field of the response structure has the
  843. * following semantics:
  844. *
  845. * 1. If all or some pages hot-added: Guest should return success.
  846. *
  847. * 2. If no pages could be hot-added:
  848. *
  849. * If the guest returns success, then the host
  850. * will not attempt any further hot-add operations. This
  851. * signifies a permanent failure.
  852. *
  853. * If the guest returns failure, then this failure will be
  854. * treated as a transient failure and the host may retry the
  855. * hot-add operation after some delay.
  856. */
  857. if (resp.page_count > 0)
  858. resp.result = 1;
  859. else if (!do_hot_add)
  860. resp.result = 1;
  861. else
  862. resp.result = 0;
  863. if (!do_hot_add || (resp.page_count == 0))
  864. pr_info("Memory hot add failed\n");
  865. dm->state = DM_INITIALIZED;
  866. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  867. vmbus_sendpacket(dm->dev->channel, &resp,
  868. sizeof(struct dm_hot_add_response),
  869. (unsigned long)NULL,
  870. VM_PKT_DATA_INBAND, 0);
  871. }
  872. static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
  873. {
  874. struct dm_info_header *info_hdr;
  875. info_hdr = (struct dm_info_header *)msg->info;
  876. switch (info_hdr->type) {
  877. case INFO_TYPE_MAX_PAGE_CNT:
  878. if (info_hdr->data_size == sizeof(__u64)) {
  879. __u64 *max_page_count = (__u64 *)&info_hdr[1];
  880. pr_info("Max. dynamic memory size: %llu MB\n",
  881. (*max_page_count) >> (20 - PAGE_SHIFT));
  882. }
  883. break;
  884. default:
  885. pr_info("Received Unknown type: %d\n", info_hdr->type);
  886. }
  887. }
  888. static unsigned long compute_balloon_floor(void)
  889. {
  890. unsigned long min_pages;
  891. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  892. /* Simple continuous piecewiese linear function:
  893. * max MiB -> min MiB gradient
  894. * 0 0
  895. * 16 16
  896. * 32 24
  897. * 128 72 (1/2)
  898. * 512 168 (1/4)
  899. * 2048 360 (1/8)
  900. * 8192 744 (1/16)
  901. * 32768 1512 (1/32)
  902. */
  903. if (totalram_pages < MB2PAGES(128))
  904. min_pages = MB2PAGES(8) + (totalram_pages >> 1);
  905. else if (totalram_pages < MB2PAGES(512))
  906. min_pages = MB2PAGES(40) + (totalram_pages >> 2);
  907. else if (totalram_pages < MB2PAGES(2048))
  908. min_pages = MB2PAGES(104) + (totalram_pages >> 3);
  909. else if (totalram_pages < MB2PAGES(8192))
  910. min_pages = MB2PAGES(232) + (totalram_pages >> 4);
  911. else
  912. min_pages = MB2PAGES(488) + (totalram_pages >> 5);
  913. #undef MB2PAGES
  914. return min_pages;
  915. }
  916. /*
  917. * Post our status as it relates memory pressure to the
  918. * host. Host expects the guests to post this status
  919. * periodically at 1 second intervals.
  920. *
  921. * The metrics specified in this protocol are very Windows
  922. * specific and so we cook up numbers here to convey our memory
  923. * pressure.
  924. */
  925. static void post_status(struct hv_dynmem_device *dm)
  926. {
  927. struct dm_status status;
  928. unsigned long now = jiffies;
  929. unsigned long last_post = last_post_time;
  930. if (pressure_report_delay > 0) {
  931. --pressure_report_delay;
  932. return;
  933. }
  934. if (!time_after(now, (last_post_time + HZ)))
  935. return;
  936. memset(&status, 0, sizeof(struct dm_status));
  937. status.hdr.type = DM_STATUS_REPORT;
  938. status.hdr.size = sizeof(struct dm_status);
  939. status.hdr.trans_id = atomic_inc_return(&trans_id);
  940. /*
  941. * The host expects the guest to report free and committed memory.
  942. * Furthermore, the host expects the pressure information to include
  943. * the ballooned out pages. For a given amount of memory that we are
  944. * managing we need to compute a floor below which we should not
  945. * balloon. Compute this and add it to the pressure report.
  946. * We also need to report all offline pages (num_pages_added -
  947. * num_pages_onlined) as committed to the host, otherwise it can try
  948. * asking us to balloon them out.
  949. */
  950. status.num_avail = si_mem_available();
  951. status.num_committed = vm_memory_committed() +
  952. dm->num_pages_ballooned +
  953. (dm->num_pages_added > dm->num_pages_onlined ?
  954. dm->num_pages_added - dm->num_pages_onlined : 0) +
  955. compute_balloon_floor();
  956. /*
  957. * If our transaction ID is no longer current, just don't
  958. * send the status. This can happen if we were interrupted
  959. * after we picked our transaction ID.
  960. */
  961. if (status.hdr.trans_id != atomic_read(&trans_id))
  962. return;
  963. /*
  964. * If the last post time that we sampled has changed,
  965. * we have raced, don't post the status.
  966. */
  967. if (last_post != last_post_time)
  968. return;
  969. last_post_time = jiffies;
  970. vmbus_sendpacket(dm->dev->channel, &status,
  971. sizeof(struct dm_status),
  972. (unsigned long)NULL,
  973. VM_PKT_DATA_INBAND, 0);
  974. }
  975. static void free_balloon_pages(struct hv_dynmem_device *dm,
  976. union dm_mem_page_range *range_array)
  977. {
  978. int num_pages = range_array->finfo.page_cnt;
  979. __u64 start_frame = range_array->finfo.start_page;
  980. struct page *pg;
  981. int i;
  982. for (i = 0; i < num_pages; i++) {
  983. pg = pfn_to_page(i + start_frame);
  984. __free_page(pg);
  985. dm->num_pages_ballooned--;
  986. }
  987. }
  988. static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
  989. unsigned int num_pages,
  990. struct dm_balloon_response *bl_resp,
  991. int alloc_unit)
  992. {
  993. unsigned int i = 0;
  994. struct page *pg;
  995. if (num_pages < alloc_unit)
  996. return 0;
  997. for (i = 0; (i * alloc_unit) < num_pages; i++) {
  998. if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
  999. PAGE_SIZE)
  1000. return i * alloc_unit;
  1001. /*
  1002. * We execute this code in a thread context. Furthermore,
  1003. * we don't want the kernel to try too hard.
  1004. */
  1005. pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
  1006. __GFP_NOMEMALLOC | __GFP_NOWARN,
  1007. get_order(alloc_unit << PAGE_SHIFT));
  1008. if (!pg)
  1009. return i * alloc_unit;
  1010. dm->num_pages_ballooned += alloc_unit;
  1011. /*
  1012. * If we allocatted 2M pages; split them so we
  1013. * can free them in any order we get.
  1014. */
  1015. if (alloc_unit != 1)
  1016. split_page(pg, get_order(alloc_unit << PAGE_SHIFT));
  1017. bl_resp->range_count++;
  1018. bl_resp->range_array[i].finfo.start_page =
  1019. page_to_pfn(pg);
  1020. bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
  1021. bl_resp->hdr.size += sizeof(union dm_mem_page_range);
  1022. }
  1023. return num_pages;
  1024. }
  1025. static void balloon_up(struct work_struct *dummy)
  1026. {
  1027. unsigned int num_pages = dm_device.balloon_wrk.num_pages;
  1028. unsigned int num_ballooned = 0;
  1029. struct dm_balloon_response *bl_resp;
  1030. int alloc_unit;
  1031. int ret;
  1032. bool done = false;
  1033. int i;
  1034. long avail_pages;
  1035. unsigned long floor;
  1036. /* The host balloons pages in 2M granularity. */
  1037. WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
  1038. /*
  1039. * We will attempt 2M allocations. However, if we fail to
  1040. * allocate 2M chunks, we will go back to 4k allocations.
  1041. */
  1042. alloc_unit = 512;
  1043. avail_pages = si_mem_available();
  1044. floor = compute_balloon_floor();
  1045. /* Refuse to balloon below the floor, keep the 2M granularity. */
  1046. if (avail_pages < num_pages || avail_pages - num_pages < floor) {
  1047. pr_warn("Balloon request will be partially fulfilled. %s\n",
  1048. avail_pages < num_pages ? "Not enough memory." :
  1049. "Balloon floor reached.");
  1050. num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
  1051. num_pages -= num_pages % PAGES_IN_2M;
  1052. }
  1053. while (!done) {
  1054. bl_resp = (struct dm_balloon_response *)send_buffer;
  1055. memset(send_buffer, 0, PAGE_SIZE);
  1056. bl_resp->hdr.type = DM_BALLOON_RESPONSE;
  1057. bl_resp->hdr.size = sizeof(struct dm_balloon_response);
  1058. bl_resp->more_pages = 1;
  1059. num_pages -= num_ballooned;
  1060. num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
  1061. bl_resp, alloc_unit);
  1062. if (alloc_unit != 1 && num_ballooned == 0) {
  1063. alloc_unit = 1;
  1064. continue;
  1065. }
  1066. if (num_ballooned == 0 || num_ballooned == num_pages) {
  1067. pr_debug("Ballooned %u out of %u requested pages.\n",
  1068. num_pages, dm_device.balloon_wrk.num_pages);
  1069. bl_resp->more_pages = 0;
  1070. done = true;
  1071. dm_device.state = DM_INITIALIZED;
  1072. }
  1073. /*
  1074. * We are pushing a lot of data through the channel;
  1075. * deal with transient failures caused because of the
  1076. * lack of space in the ring buffer.
  1077. */
  1078. do {
  1079. bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
  1080. ret = vmbus_sendpacket(dm_device.dev->channel,
  1081. bl_resp,
  1082. bl_resp->hdr.size,
  1083. (unsigned long)NULL,
  1084. VM_PKT_DATA_INBAND, 0);
  1085. if (ret == -EAGAIN)
  1086. msleep(20);
  1087. post_status(&dm_device);
  1088. } while (ret == -EAGAIN);
  1089. if (ret) {
  1090. /*
  1091. * Free up the memory we allocatted.
  1092. */
  1093. pr_info("Balloon response failed\n");
  1094. for (i = 0; i < bl_resp->range_count; i++)
  1095. free_balloon_pages(&dm_device,
  1096. &bl_resp->range_array[i]);
  1097. done = true;
  1098. }
  1099. }
  1100. }
  1101. static void balloon_down(struct hv_dynmem_device *dm,
  1102. struct dm_unballoon_request *req)
  1103. {
  1104. union dm_mem_page_range *range_array = req->range_array;
  1105. int range_count = req->range_count;
  1106. struct dm_unballoon_response resp;
  1107. int i;
  1108. unsigned int prev_pages_ballooned = dm->num_pages_ballooned;
  1109. for (i = 0; i < range_count; i++) {
  1110. free_balloon_pages(dm, &range_array[i]);
  1111. complete(&dm_device.config_event);
  1112. }
  1113. pr_debug("Freed %u ballooned pages.\n",
  1114. prev_pages_ballooned - dm->num_pages_ballooned);
  1115. if (req->more_pages == 1)
  1116. return;
  1117. memset(&resp, 0, sizeof(struct dm_unballoon_response));
  1118. resp.hdr.type = DM_UNBALLOON_RESPONSE;
  1119. resp.hdr.trans_id = atomic_inc_return(&trans_id);
  1120. resp.hdr.size = sizeof(struct dm_unballoon_response);
  1121. vmbus_sendpacket(dm_device.dev->channel, &resp,
  1122. sizeof(struct dm_unballoon_response),
  1123. (unsigned long)NULL,
  1124. VM_PKT_DATA_INBAND, 0);
  1125. dm->state = DM_INITIALIZED;
  1126. }
  1127. static void balloon_onchannelcallback(void *context);
  1128. static int dm_thread_func(void *dm_dev)
  1129. {
  1130. struct hv_dynmem_device *dm = dm_dev;
  1131. while (!kthread_should_stop()) {
  1132. wait_for_completion_interruptible_timeout(
  1133. &dm_device.config_event, 1*HZ);
  1134. /*
  1135. * The host expects us to post information on the memory
  1136. * pressure every second.
  1137. */
  1138. reinit_completion(&dm_device.config_event);
  1139. post_status(dm);
  1140. }
  1141. return 0;
  1142. }
  1143. static void version_resp(struct hv_dynmem_device *dm,
  1144. struct dm_version_response *vresp)
  1145. {
  1146. struct dm_version_request version_req;
  1147. int ret;
  1148. if (vresp->is_accepted) {
  1149. /*
  1150. * We are done; wakeup the
  1151. * context waiting for version
  1152. * negotiation.
  1153. */
  1154. complete(&dm->host_event);
  1155. return;
  1156. }
  1157. /*
  1158. * If there are more versions to try, continue
  1159. * with negotiations; if not
  1160. * shutdown the service since we are not able
  1161. * to negotiate a suitable version number
  1162. * with the host.
  1163. */
  1164. if (dm->next_version == 0)
  1165. goto version_error;
  1166. memset(&version_req, 0, sizeof(struct dm_version_request));
  1167. version_req.hdr.type = DM_VERSION_REQUEST;
  1168. version_req.hdr.size = sizeof(struct dm_version_request);
  1169. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1170. version_req.version.version = dm->next_version;
  1171. dm->version = version_req.version.version;
  1172. /*
  1173. * Set the next version to try in case current version fails.
  1174. * Win7 protocol ought to be the last one to try.
  1175. */
  1176. switch (version_req.version.version) {
  1177. case DYNMEM_PROTOCOL_VERSION_WIN8:
  1178. dm->next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
  1179. version_req.is_last_attempt = 0;
  1180. break;
  1181. default:
  1182. dm->next_version = 0;
  1183. version_req.is_last_attempt = 1;
  1184. }
  1185. ret = vmbus_sendpacket(dm->dev->channel, &version_req,
  1186. sizeof(struct dm_version_request),
  1187. (unsigned long)NULL,
  1188. VM_PKT_DATA_INBAND, 0);
  1189. if (ret)
  1190. goto version_error;
  1191. return;
  1192. version_error:
  1193. dm->state = DM_INIT_ERROR;
  1194. complete(&dm->host_event);
  1195. }
  1196. static void cap_resp(struct hv_dynmem_device *dm,
  1197. struct dm_capabilities_resp_msg *cap_resp)
  1198. {
  1199. if (!cap_resp->is_accepted) {
  1200. pr_info("Capabilities not accepted by host\n");
  1201. dm->state = DM_INIT_ERROR;
  1202. }
  1203. complete(&dm->host_event);
  1204. }
  1205. static void balloon_onchannelcallback(void *context)
  1206. {
  1207. struct hv_device *dev = context;
  1208. u32 recvlen;
  1209. u64 requestid;
  1210. struct dm_message *dm_msg;
  1211. struct dm_header *dm_hdr;
  1212. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1213. struct dm_balloon *bal_msg;
  1214. struct dm_hot_add *ha_msg;
  1215. union dm_mem_page_range *ha_pg_range;
  1216. union dm_mem_page_range *ha_region;
  1217. memset(recv_buffer, 0, sizeof(recv_buffer));
  1218. vmbus_recvpacket(dev->channel, recv_buffer,
  1219. PAGE_SIZE, &recvlen, &requestid);
  1220. if (recvlen > 0) {
  1221. dm_msg = (struct dm_message *)recv_buffer;
  1222. dm_hdr = &dm_msg->hdr;
  1223. switch (dm_hdr->type) {
  1224. case DM_VERSION_RESPONSE:
  1225. version_resp(dm,
  1226. (struct dm_version_response *)dm_msg);
  1227. break;
  1228. case DM_CAPABILITIES_RESPONSE:
  1229. cap_resp(dm,
  1230. (struct dm_capabilities_resp_msg *)dm_msg);
  1231. break;
  1232. case DM_BALLOON_REQUEST:
  1233. if (dm->state == DM_BALLOON_UP)
  1234. pr_warn("Currently ballooning\n");
  1235. bal_msg = (struct dm_balloon *)recv_buffer;
  1236. dm->state = DM_BALLOON_UP;
  1237. dm_device.balloon_wrk.num_pages = bal_msg->num_pages;
  1238. schedule_work(&dm_device.balloon_wrk.wrk);
  1239. break;
  1240. case DM_UNBALLOON_REQUEST:
  1241. dm->state = DM_BALLOON_DOWN;
  1242. balloon_down(dm,
  1243. (struct dm_unballoon_request *)recv_buffer);
  1244. break;
  1245. case DM_MEM_HOT_ADD_REQUEST:
  1246. if (dm->state == DM_HOT_ADD)
  1247. pr_warn("Currently hot-adding\n");
  1248. dm->state = DM_HOT_ADD;
  1249. ha_msg = (struct dm_hot_add *)recv_buffer;
  1250. if (ha_msg->hdr.size == sizeof(struct dm_hot_add)) {
  1251. /*
  1252. * This is a normal hot-add request specifying
  1253. * hot-add memory.
  1254. */
  1255. dm->host_specified_ha_region = false;
  1256. ha_pg_range = &ha_msg->range;
  1257. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1258. dm->ha_wrk.ha_region_range.page_range = 0;
  1259. } else {
  1260. /*
  1261. * Host is specifying that we first hot-add
  1262. * a region and then partially populate this
  1263. * region.
  1264. */
  1265. dm->host_specified_ha_region = true;
  1266. ha_pg_range = &ha_msg->range;
  1267. ha_region = &ha_pg_range[1];
  1268. dm->ha_wrk.ha_page_range = *ha_pg_range;
  1269. dm->ha_wrk.ha_region_range = *ha_region;
  1270. }
  1271. schedule_work(&dm_device.ha_wrk.wrk);
  1272. break;
  1273. case DM_INFO_MESSAGE:
  1274. process_info(dm, (struct dm_info_msg *)dm_msg);
  1275. break;
  1276. default:
  1277. pr_err("Unhandled message: type: %d\n", dm_hdr->type);
  1278. }
  1279. }
  1280. }
  1281. static int balloon_probe(struct hv_device *dev,
  1282. const struct hv_vmbus_device_id *dev_id)
  1283. {
  1284. int ret;
  1285. unsigned long t;
  1286. struct dm_version_request version_req;
  1287. struct dm_capabilities cap_msg;
  1288. #ifdef CONFIG_MEMORY_HOTPLUG
  1289. do_hot_add = hot_add;
  1290. #else
  1291. do_hot_add = false;
  1292. #endif
  1293. /*
  1294. * First allocate a send buffer.
  1295. */
  1296. send_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1297. if (!send_buffer)
  1298. return -ENOMEM;
  1299. ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
  1300. balloon_onchannelcallback, dev);
  1301. if (ret)
  1302. goto probe_error0;
  1303. dm_device.dev = dev;
  1304. dm_device.state = DM_INITIALIZING;
  1305. dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN8;
  1306. init_completion(&dm_device.host_event);
  1307. init_completion(&dm_device.config_event);
  1308. INIT_LIST_HEAD(&dm_device.ha_region_list);
  1309. spin_lock_init(&dm_device.ha_lock);
  1310. INIT_WORK(&dm_device.balloon_wrk.wrk, balloon_up);
  1311. INIT_WORK(&dm_device.ha_wrk.wrk, hot_add_req);
  1312. dm_device.host_specified_ha_region = false;
  1313. dm_device.thread =
  1314. kthread_run(dm_thread_func, &dm_device, "hv_balloon");
  1315. if (IS_ERR(dm_device.thread)) {
  1316. ret = PTR_ERR(dm_device.thread);
  1317. goto probe_error1;
  1318. }
  1319. #ifdef CONFIG_MEMORY_HOTPLUG
  1320. set_online_page_callback(&hv_online_page);
  1321. register_memory_notifier(&hv_memory_nb);
  1322. #endif
  1323. hv_set_drvdata(dev, &dm_device);
  1324. /*
  1325. * Initiate the hand shake with the host and negotiate
  1326. * a version that the host can support. We start with the
  1327. * highest version number and go down if the host cannot
  1328. * support it.
  1329. */
  1330. memset(&version_req, 0, sizeof(struct dm_version_request));
  1331. version_req.hdr.type = DM_VERSION_REQUEST;
  1332. version_req.hdr.size = sizeof(struct dm_version_request);
  1333. version_req.hdr.trans_id = atomic_inc_return(&trans_id);
  1334. version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN10;
  1335. version_req.is_last_attempt = 0;
  1336. dm_device.version = version_req.version.version;
  1337. ret = vmbus_sendpacket(dev->channel, &version_req,
  1338. sizeof(struct dm_version_request),
  1339. (unsigned long)NULL,
  1340. VM_PKT_DATA_INBAND, 0);
  1341. if (ret)
  1342. goto probe_error2;
  1343. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  1344. if (t == 0) {
  1345. ret = -ETIMEDOUT;
  1346. goto probe_error2;
  1347. }
  1348. /*
  1349. * If we could not negotiate a compatible version with the host
  1350. * fail the probe function.
  1351. */
  1352. if (dm_device.state == DM_INIT_ERROR) {
  1353. ret = -ETIMEDOUT;
  1354. goto probe_error2;
  1355. }
  1356. pr_info("Using Dynamic Memory protocol version %u.%u\n",
  1357. DYNMEM_MAJOR_VERSION(dm_device.version),
  1358. DYNMEM_MINOR_VERSION(dm_device.version));
  1359. /*
  1360. * Now submit our capabilities to the host.
  1361. */
  1362. memset(&cap_msg, 0, sizeof(struct dm_capabilities));
  1363. cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
  1364. cap_msg.hdr.size = sizeof(struct dm_capabilities);
  1365. cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
  1366. cap_msg.caps.cap_bits.balloon = 1;
  1367. cap_msg.caps.cap_bits.hot_add = 1;
  1368. /*
  1369. * Specify our alignment requirements as it relates
  1370. * memory hot-add. Specify 128MB alignment.
  1371. */
  1372. cap_msg.caps.cap_bits.hot_add_alignment = 7;
  1373. /*
  1374. * Currently the host does not use these
  1375. * values and we set them to what is done in the
  1376. * Windows driver.
  1377. */
  1378. cap_msg.min_page_cnt = 0;
  1379. cap_msg.max_page_number = -1;
  1380. ret = vmbus_sendpacket(dev->channel, &cap_msg,
  1381. sizeof(struct dm_capabilities),
  1382. (unsigned long)NULL,
  1383. VM_PKT_DATA_INBAND, 0);
  1384. if (ret)
  1385. goto probe_error2;
  1386. t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
  1387. if (t == 0) {
  1388. ret = -ETIMEDOUT;
  1389. goto probe_error2;
  1390. }
  1391. /*
  1392. * If the host does not like our capabilities,
  1393. * fail the probe function.
  1394. */
  1395. if (dm_device.state == DM_INIT_ERROR) {
  1396. ret = -ETIMEDOUT;
  1397. goto probe_error2;
  1398. }
  1399. dm_device.state = DM_INITIALIZED;
  1400. last_post_time = jiffies;
  1401. return 0;
  1402. probe_error2:
  1403. #ifdef CONFIG_MEMORY_HOTPLUG
  1404. restore_online_page_callback(&hv_online_page);
  1405. #endif
  1406. kthread_stop(dm_device.thread);
  1407. probe_error1:
  1408. vmbus_close(dev->channel);
  1409. probe_error0:
  1410. kfree(send_buffer);
  1411. return ret;
  1412. }
  1413. static int balloon_remove(struct hv_device *dev)
  1414. {
  1415. struct hv_dynmem_device *dm = hv_get_drvdata(dev);
  1416. struct hv_hotadd_state *has, *tmp;
  1417. struct hv_hotadd_gap *gap, *tmp_gap;
  1418. unsigned long flags;
  1419. if (dm->num_pages_ballooned != 0)
  1420. pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
  1421. cancel_work_sync(&dm->balloon_wrk.wrk);
  1422. cancel_work_sync(&dm->ha_wrk.wrk);
  1423. vmbus_close(dev->channel);
  1424. kthread_stop(dm->thread);
  1425. kfree(send_buffer);
  1426. #ifdef CONFIG_MEMORY_HOTPLUG
  1427. restore_online_page_callback(&hv_online_page);
  1428. unregister_memory_notifier(&hv_memory_nb);
  1429. #endif
  1430. spin_lock_irqsave(&dm_device.ha_lock, flags);
  1431. list_for_each_entry_safe(has, tmp, &dm->ha_region_list, list) {
  1432. list_for_each_entry_safe(gap, tmp_gap, &has->gap_list, list) {
  1433. list_del(&gap->list);
  1434. kfree(gap);
  1435. }
  1436. list_del(&has->list);
  1437. kfree(has);
  1438. }
  1439. spin_unlock_irqrestore(&dm_device.ha_lock, flags);
  1440. return 0;
  1441. }
  1442. static const struct hv_vmbus_device_id id_table[] = {
  1443. /* Dynamic Memory Class ID */
  1444. /* 525074DC-8985-46e2-8057-A307DC18A502 */
  1445. { HV_DM_GUID, },
  1446. { },
  1447. };
  1448. MODULE_DEVICE_TABLE(vmbus, id_table);
  1449. static struct hv_driver balloon_drv = {
  1450. .name = "hv_balloon",
  1451. .id_table = id_table,
  1452. .probe = balloon_probe,
  1453. .remove = balloon_remove,
  1454. };
  1455. static int __init init_balloon_drv(void)
  1456. {
  1457. return vmbus_driver_register(&balloon_drv);
  1458. }
  1459. module_init(init_balloon_drv);
  1460. MODULE_DESCRIPTION("Hyper-V Balloon");
  1461. MODULE_LICENSE("GPL");