hv_balloon.c 41 KB

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