virtio_mem.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio-mem device driver.
  4. *
  5. * Copyright Red Hat, Inc. 2020
  6. *
  7. * Author(s): David Hildenbrand <david@redhat.com>
  8. */
  9. #include <linux/virtio.h>
  10. #include <linux/virtio_mem.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/memory_hotplug.h>
  16. #include <linux/memory.h>
  17. #include <linux/hrtimer.h>
  18. #include <linux/crash_dump.h>
  19. #include <linux/mutex.h>
  20. #include <linux/bitmap.h>
  21. #include <linux/lockdep.h>
  22. #include <linux/log2.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/suspend.h>
  25. #include <acpi/acpi_numa.h>
  26. static bool unplug_online = true;
  27. module_param(unplug_online, bool, 0644);
  28. MODULE_PARM_DESC(unplug_online, "Try to unplug online memory");
  29. static bool force_bbm;
  30. module_param(force_bbm, bool, 0444);
  31. MODULE_PARM_DESC(force_bbm,
  32. "Force Big Block Mode. Default is 0 (auto-selection)");
  33. static unsigned long bbm_block_size;
  34. module_param(bbm_block_size, ulong, 0444);
  35. MODULE_PARM_DESC(bbm_block_size,
  36. "Big Block size in bytes. Default is 0 (auto-detection).");
  37. /*
  38. * virtio-mem currently supports the following modes of operation:
  39. *
  40. * * Sub Block Mode (SBM): A Linux memory block spans 2..X subblocks (SB). The
  41. * size of a Sub Block (SB) is determined based on the device block size, the
  42. * pageblock size, and the maximum allocation granularity of the buddy.
  43. * Subblocks within a Linux memory block might either be plugged or unplugged.
  44. * Memory is added/removed to Linux MM in Linux memory block granularity.
  45. *
  46. * * Big Block Mode (BBM): A Big Block (BB) spans 1..X Linux memory blocks.
  47. * Memory is added/removed to Linux MM in Big Block granularity.
  48. *
  49. * The mode is determined automatically based on the Linux memory block size
  50. * and the device block size.
  51. *
  52. * User space / core MM (auto onlining) is responsible for onlining added
  53. * Linux memory blocks - and for selecting a zone. Linux Memory Blocks are
  54. * always onlined separately, and all memory within a Linux memory block is
  55. * onlined to the same zone - virtio-mem relies on this behavior.
  56. */
  57. /*
  58. * State of a Linux memory block in SBM.
  59. */
  60. enum virtio_mem_sbm_mb_state {
  61. /* Unplugged, not added to Linux. Can be reused later. */
  62. VIRTIO_MEM_SBM_MB_UNUSED = 0,
  63. /* (Partially) plugged, not added to Linux. Error on add_memory(). */
  64. VIRTIO_MEM_SBM_MB_PLUGGED,
  65. /* Fully plugged, fully added to Linux, offline. */
  66. VIRTIO_MEM_SBM_MB_OFFLINE,
  67. /* Partially plugged, fully added to Linux, offline. */
  68. VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL,
  69. /* Fully plugged, fully added to Linux, onlined to a kernel zone. */
  70. VIRTIO_MEM_SBM_MB_KERNEL,
  71. /* Partially plugged, fully added to Linux, online to a kernel zone */
  72. VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL,
  73. /* Fully plugged, fully added to Linux, onlined to ZONE_MOVABLE. */
  74. VIRTIO_MEM_SBM_MB_MOVABLE,
  75. /* Partially plugged, fully added to Linux, onlined to ZONE_MOVABLE. */
  76. VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL,
  77. VIRTIO_MEM_SBM_MB_COUNT
  78. };
  79. /*
  80. * State of a Big Block (BB) in BBM, covering 1..X Linux memory blocks.
  81. */
  82. enum virtio_mem_bbm_bb_state {
  83. /* Unplugged, not added to Linux. Can be reused later. */
  84. VIRTIO_MEM_BBM_BB_UNUSED = 0,
  85. /* Plugged, not added to Linux. Error on add_memory(). */
  86. VIRTIO_MEM_BBM_BB_PLUGGED,
  87. /* Plugged and added to Linux. */
  88. VIRTIO_MEM_BBM_BB_ADDED,
  89. /* All online parts are fake-offline, ready to remove. */
  90. VIRTIO_MEM_BBM_BB_FAKE_OFFLINE,
  91. VIRTIO_MEM_BBM_BB_COUNT
  92. };
  93. struct virtio_mem {
  94. struct virtio_device *vdev;
  95. /* We might first have to unplug all memory when starting up. */
  96. bool unplug_all_required;
  97. /* Workqueue that processes the plug/unplug requests. */
  98. struct work_struct wq;
  99. atomic_t wq_active;
  100. atomic_t config_changed;
  101. /* Virtqueue for guest->host requests. */
  102. struct virtqueue *vq;
  103. /* Wait for a host response to a guest request. */
  104. wait_queue_head_t host_resp;
  105. /* Space for one guest request and the host response. */
  106. struct virtio_mem_req req;
  107. struct virtio_mem_resp resp;
  108. /* The current size of the device. */
  109. uint64_t plugged_size;
  110. /* The requested size of the device. */
  111. uint64_t requested_size;
  112. /* The device block size (for communicating with the device). */
  113. uint64_t device_block_size;
  114. /* The determined node id for all memory of the device. */
  115. int nid;
  116. /* Physical start address of the memory region. */
  117. uint64_t addr;
  118. /* Maximum region size in bytes. */
  119. uint64_t region_size;
  120. /* The parent resource for all memory added via this device. */
  121. struct resource *parent_resource;
  122. /*
  123. * Copy of "System RAM (virtio_mem)" to be used for
  124. * add_memory_driver_managed().
  125. */
  126. const char *resource_name;
  127. /* Memory group identification. */
  128. int mgid;
  129. /*
  130. * We don't want to add too much memory if it's not getting onlined,
  131. * to avoid running OOM. Besides this threshold, we allow to have at
  132. * least two offline blocks at a time (whatever is bigger).
  133. */
  134. #define VIRTIO_MEM_DEFAULT_OFFLINE_THRESHOLD (1024 * 1024 * 1024)
  135. atomic64_t offline_size;
  136. uint64_t offline_threshold;
  137. /* If set, the driver is in SBM, otherwise in BBM. */
  138. bool in_sbm;
  139. union {
  140. struct {
  141. /* Id of the first memory block of this device. */
  142. unsigned long first_mb_id;
  143. /* Id of the last usable memory block of this device. */
  144. unsigned long last_usable_mb_id;
  145. /* Id of the next memory bock to prepare when needed. */
  146. unsigned long next_mb_id;
  147. /* The subblock size. */
  148. uint64_t sb_size;
  149. /* The number of subblocks per Linux memory block. */
  150. uint32_t sbs_per_mb;
  151. /*
  152. * Some of the Linux memory blocks tracked as "partially
  153. * plugged" are completely unplugged and can be offlined
  154. * and removed -- which previously failed.
  155. */
  156. bool have_unplugged_mb;
  157. /* Summary of all memory block states. */
  158. unsigned long mb_count[VIRTIO_MEM_SBM_MB_COUNT];
  159. /*
  160. * One byte state per memory block. Allocated via
  161. * vmalloc(). Resized (alloc+copy+free) on demand.
  162. *
  163. * With 128 MiB memory blocks, we have states for 512
  164. * GiB of memory in one 4 KiB page.
  165. */
  166. uint8_t *mb_states;
  167. /*
  168. * Bitmap: one bit per subblock. Allocated similar to
  169. * sbm.mb_states.
  170. *
  171. * A set bit means the corresponding subblock is
  172. * plugged, otherwise it's unblocked.
  173. *
  174. * With 4 MiB subblocks, we manage 128 GiB of memory
  175. * in one 4 KiB page.
  176. */
  177. unsigned long *sb_states;
  178. } sbm;
  179. struct {
  180. /* Id of the first big block of this device. */
  181. unsigned long first_bb_id;
  182. /* Id of the last usable big block of this device. */
  183. unsigned long last_usable_bb_id;
  184. /* Id of the next device bock to prepare when needed. */
  185. unsigned long next_bb_id;
  186. /* Summary of all big block states. */
  187. unsigned long bb_count[VIRTIO_MEM_BBM_BB_COUNT];
  188. /* One byte state per big block. See sbm.mb_states. */
  189. uint8_t *bb_states;
  190. /* The block size used for plugging/adding/removing. */
  191. uint64_t bb_size;
  192. } bbm;
  193. };
  194. /*
  195. * Mutex that protects the sbm.mb_count, sbm.mb_states,
  196. * sbm.sb_states, bbm.bb_count, and bbm.bb_states
  197. *
  198. * When this lock is held the pointers can't change, ONLINE and
  199. * OFFLINE blocks can't change the state and no subblocks will get
  200. * plugged/unplugged.
  201. *
  202. * In kdump mode, used to serialize requests, last_block_addr and
  203. * last_block_plugged.
  204. */
  205. struct mutex hotplug_mutex;
  206. bool hotplug_active;
  207. /* An error occurred we cannot handle - stop processing requests. */
  208. bool broken;
  209. /* Cached valued of is_kdump_kernel() when the device was probed. */
  210. bool in_kdump;
  211. /* The driver is being removed. */
  212. spinlock_t removal_lock;
  213. bool removing;
  214. /* Timer for retrying to plug/unplug memory. */
  215. struct hrtimer retry_timer;
  216. unsigned int retry_timer_ms;
  217. #define VIRTIO_MEM_RETRY_TIMER_MIN_MS 50000
  218. #define VIRTIO_MEM_RETRY_TIMER_MAX_MS 300000
  219. /* Memory notifier (online/offline events). */
  220. struct notifier_block memory_notifier;
  221. /* Notifier to block hibernation image storing/reloading. */
  222. struct notifier_block pm_notifier;
  223. #ifdef CONFIG_PROC_VMCORE
  224. /* vmcore callback for /proc/vmcore handling in kdump mode */
  225. struct vmcore_cb vmcore_cb;
  226. uint64_t last_block_addr;
  227. bool last_block_plugged;
  228. #endif /* CONFIG_PROC_VMCORE */
  229. /* Next device in the list of virtio-mem devices. */
  230. struct list_head next;
  231. };
  232. /*
  233. * We have to share a single online_page callback among all virtio-mem
  234. * devices. We use RCU to iterate the list in the callback.
  235. */
  236. static DEFINE_MUTEX(virtio_mem_mutex);
  237. static LIST_HEAD(virtio_mem_devices);
  238. static void virtio_mem_online_page_cb(struct page *page, unsigned int order);
  239. static void virtio_mem_fake_offline_going_offline(unsigned long pfn,
  240. unsigned long nr_pages);
  241. static void virtio_mem_fake_offline_cancel_offline(unsigned long pfn,
  242. unsigned long nr_pages);
  243. static void virtio_mem_retry(struct virtio_mem *vm);
  244. static int virtio_mem_create_resource(struct virtio_mem *vm);
  245. static void virtio_mem_delete_resource(struct virtio_mem *vm);
  246. /*
  247. * Register a virtio-mem device so it will be considered for the online_page
  248. * callback.
  249. */
  250. static int register_virtio_mem_device(struct virtio_mem *vm)
  251. {
  252. int rc = 0;
  253. /* First device registers the callback. */
  254. mutex_lock(&virtio_mem_mutex);
  255. if (list_empty(&virtio_mem_devices))
  256. rc = set_online_page_callback(&virtio_mem_online_page_cb);
  257. if (!rc)
  258. list_add_rcu(&vm->next, &virtio_mem_devices);
  259. mutex_unlock(&virtio_mem_mutex);
  260. return rc;
  261. }
  262. /*
  263. * Unregister a virtio-mem device so it will no longer be considered for the
  264. * online_page callback.
  265. */
  266. static void unregister_virtio_mem_device(struct virtio_mem *vm)
  267. {
  268. /* Last device unregisters the callback. */
  269. mutex_lock(&virtio_mem_mutex);
  270. list_del_rcu(&vm->next);
  271. if (list_empty(&virtio_mem_devices))
  272. restore_online_page_callback(&virtio_mem_online_page_cb);
  273. mutex_unlock(&virtio_mem_mutex);
  274. synchronize_rcu();
  275. }
  276. /*
  277. * Calculate the memory block id of a given address.
  278. */
  279. static unsigned long virtio_mem_phys_to_mb_id(unsigned long addr)
  280. {
  281. return addr / memory_block_size_bytes();
  282. }
  283. /*
  284. * Calculate the physical start address of a given memory block id.
  285. */
  286. static unsigned long virtio_mem_mb_id_to_phys(unsigned long mb_id)
  287. {
  288. return mb_id * memory_block_size_bytes();
  289. }
  290. /*
  291. * Calculate the big block id of a given address.
  292. */
  293. static unsigned long virtio_mem_phys_to_bb_id(struct virtio_mem *vm,
  294. uint64_t addr)
  295. {
  296. return addr / vm->bbm.bb_size;
  297. }
  298. /*
  299. * Calculate the physical start address of a given big block id.
  300. */
  301. static uint64_t virtio_mem_bb_id_to_phys(struct virtio_mem *vm,
  302. unsigned long bb_id)
  303. {
  304. return bb_id * vm->bbm.bb_size;
  305. }
  306. /*
  307. * Calculate the subblock id of a given address.
  308. */
  309. static unsigned long virtio_mem_phys_to_sb_id(struct virtio_mem *vm,
  310. unsigned long addr)
  311. {
  312. const unsigned long mb_id = virtio_mem_phys_to_mb_id(addr);
  313. const unsigned long mb_addr = virtio_mem_mb_id_to_phys(mb_id);
  314. return (addr - mb_addr) / vm->sbm.sb_size;
  315. }
  316. /*
  317. * Set the state of a big block, taking care of the state counter.
  318. */
  319. static void virtio_mem_bbm_set_bb_state(struct virtio_mem *vm,
  320. unsigned long bb_id,
  321. enum virtio_mem_bbm_bb_state state)
  322. {
  323. const unsigned long idx = bb_id - vm->bbm.first_bb_id;
  324. enum virtio_mem_bbm_bb_state old_state;
  325. old_state = vm->bbm.bb_states[idx];
  326. vm->bbm.bb_states[idx] = state;
  327. BUG_ON(vm->bbm.bb_count[old_state] == 0);
  328. vm->bbm.bb_count[old_state]--;
  329. vm->bbm.bb_count[state]++;
  330. }
  331. /*
  332. * Get the state of a big block.
  333. */
  334. static enum virtio_mem_bbm_bb_state virtio_mem_bbm_get_bb_state(struct virtio_mem *vm,
  335. unsigned long bb_id)
  336. {
  337. return vm->bbm.bb_states[bb_id - vm->bbm.first_bb_id];
  338. }
  339. /*
  340. * Prepare the big block state array for the next big block.
  341. */
  342. static int virtio_mem_bbm_bb_states_prepare_next_bb(struct virtio_mem *vm)
  343. {
  344. unsigned long old_bytes = vm->bbm.next_bb_id - vm->bbm.first_bb_id;
  345. unsigned long new_bytes = old_bytes + 1;
  346. int old_pages = PFN_UP(old_bytes);
  347. int new_pages = PFN_UP(new_bytes);
  348. uint8_t *new_array;
  349. if (vm->bbm.bb_states && old_pages == new_pages)
  350. return 0;
  351. new_array = vzalloc(new_pages * PAGE_SIZE);
  352. if (!new_array)
  353. return -ENOMEM;
  354. mutex_lock(&vm->hotplug_mutex);
  355. if (vm->bbm.bb_states)
  356. memcpy(new_array, vm->bbm.bb_states, old_pages * PAGE_SIZE);
  357. vfree(vm->bbm.bb_states);
  358. vm->bbm.bb_states = new_array;
  359. mutex_unlock(&vm->hotplug_mutex);
  360. return 0;
  361. }
  362. #define virtio_mem_bbm_for_each_bb(_vm, _bb_id, _state) \
  363. for (_bb_id = vm->bbm.first_bb_id; \
  364. _bb_id < vm->bbm.next_bb_id && _vm->bbm.bb_count[_state]; \
  365. _bb_id++) \
  366. if (virtio_mem_bbm_get_bb_state(_vm, _bb_id) == _state)
  367. #define virtio_mem_bbm_for_each_bb_rev(_vm, _bb_id, _state) \
  368. for (_bb_id = vm->bbm.next_bb_id - 1; \
  369. _bb_id >= vm->bbm.first_bb_id && _vm->bbm.bb_count[_state]; \
  370. _bb_id--) \
  371. if (virtio_mem_bbm_get_bb_state(_vm, _bb_id) == _state)
  372. /*
  373. * Set the state of a memory block, taking care of the state counter.
  374. */
  375. static void virtio_mem_sbm_set_mb_state(struct virtio_mem *vm,
  376. unsigned long mb_id, uint8_t state)
  377. {
  378. const unsigned long idx = mb_id - vm->sbm.first_mb_id;
  379. uint8_t old_state;
  380. old_state = vm->sbm.mb_states[idx];
  381. vm->sbm.mb_states[idx] = state;
  382. BUG_ON(vm->sbm.mb_count[old_state] == 0);
  383. vm->sbm.mb_count[old_state]--;
  384. vm->sbm.mb_count[state]++;
  385. }
  386. /*
  387. * Get the state of a memory block.
  388. */
  389. static uint8_t virtio_mem_sbm_get_mb_state(struct virtio_mem *vm,
  390. unsigned long mb_id)
  391. {
  392. const unsigned long idx = mb_id - vm->sbm.first_mb_id;
  393. return vm->sbm.mb_states[idx];
  394. }
  395. /*
  396. * Prepare the state array for the next memory block.
  397. */
  398. static int virtio_mem_sbm_mb_states_prepare_next_mb(struct virtio_mem *vm)
  399. {
  400. int old_pages = PFN_UP(vm->sbm.next_mb_id - vm->sbm.first_mb_id);
  401. int new_pages = PFN_UP(vm->sbm.next_mb_id - vm->sbm.first_mb_id + 1);
  402. uint8_t *new_array;
  403. if (vm->sbm.mb_states && old_pages == new_pages)
  404. return 0;
  405. new_array = vzalloc(new_pages * PAGE_SIZE);
  406. if (!new_array)
  407. return -ENOMEM;
  408. mutex_lock(&vm->hotplug_mutex);
  409. if (vm->sbm.mb_states)
  410. memcpy(new_array, vm->sbm.mb_states, old_pages * PAGE_SIZE);
  411. vfree(vm->sbm.mb_states);
  412. vm->sbm.mb_states = new_array;
  413. mutex_unlock(&vm->hotplug_mutex);
  414. return 0;
  415. }
  416. #define virtio_mem_sbm_for_each_mb(_vm, _mb_id, _state) \
  417. for (_mb_id = _vm->sbm.first_mb_id; \
  418. _mb_id < _vm->sbm.next_mb_id && _vm->sbm.mb_count[_state]; \
  419. _mb_id++) \
  420. if (virtio_mem_sbm_get_mb_state(_vm, _mb_id) == _state)
  421. #define virtio_mem_sbm_for_each_mb_rev(_vm, _mb_id, _state) \
  422. for (_mb_id = _vm->sbm.next_mb_id - 1; \
  423. _mb_id >= _vm->sbm.first_mb_id && _vm->sbm.mb_count[_state]; \
  424. _mb_id--) \
  425. if (virtio_mem_sbm_get_mb_state(_vm, _mb_id) == _state)
  426. /*
  427. * Calculate the bit number in the subblock bitmap for the given subblock
  428. * inside the given memory block.
  429. */
  430. static int virtio_mem_sbm_sb_state_bit_nr(struct virtio_mem *vm,
  431. unsigned long mb_id, int sb_id)
  432. {
  433. return (mb_id - vm->sbm.first_mb_id) * vm->sbm.sbs_per_mb + sb_id;
  434. }
  435. /*
  436. * Mark all selected subblocks plugged.
  437. *
  438. * Will not modify the state of the memory block.
  439. */
  440. static void virtio_mem_sbm_set_sb_plugged(struct virtio_mem *vm,
  441. unsigned long mb_id, int sb_id,
  442. int count)
  443. {
  444. const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id);
  445. __bitmap_set(vm->sbm.sb_states, bit, count);
  446. }
  447. /*
  448. * Mark all selected subblocks unplugged.
  449. *
  450. * Will not modify the state of the memory block.
  451. */
  452. static void virtio_mem_sbm_set_sb_unplugged(struct virtio_mem *vm,
  453. unsigned long mb_id, int sb_id,
  454. int count)
  455. {
  456. const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id);
  457. __bitmap_clear(vm->sbm.sb_states, bit, count);
  458. }
  459. /*
  460. * Test if all selected subblocks are plugged.
  461. */
  462. static bool virtio_mem_sbm_test_sb_plugged(struct virtio_mem *vm,
  463. unsigned long mb_id, int sb_id,
  464. int count)
  465. {
  466. const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id);
  467. if (count == 1)
  468. return test_bit(bit, vm->sbm.sb_states);
  469. /* TODO: Helper similar to bitmap_set() */
  470. return find_next_zero_bit(vm->sbm.sb_states, bit + count, bit) >=
  471. bit + count;
  472. }
  473. /*
  474. * Test if all selected subblocks are unplugged.
  475. */
  476. static bool virtio_mem_sbm_test_sb_unplugged(struct virtio_mem *vm,
  477. unsigned long mb_id, int sb_id,
  478. int count)
  479. {
  480. const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id);
  481. /* TODO: Helper similar to bitmap_set() */
  482. return find_next_bit(vm->sbm.sb_states, bit + count, bit) >=
  483. bit + count;
  484. }
  485. /*
  486. * Find the first unplugged subblock. Returns vm->sbm.sbs_per_mb in case there is
  487. * none.
  488. */
  489. static int virtio_mem_sbm_first_unplugged_sb(struct virtio_mem *vm,
  490. unsigned long mb_id)
  491. {
  492. const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, 0);
  493. return find_next_zero_bit(vm->sbm.sb_states,
  494. bit + vm->sbm.sbs_per_mb, bit) - bit;
  495. }
  496. /*
  497. * Prepare the subblock bitmap for the next memory block.
  498. */
  499. static int virtio_mem_sbm_sb_states_prepare_next_mb(struct virtio_mem *vm)
  500. {
  501. const unsigned long old_nb_mb = vm->sbm.next_mb_id - vm->sbm.first_mb_id;
  502. const unsigned long old_nb_bits = old_nb_mb * vm->sbm.sbs_per_mb;
  503. const unsigned long new_nb_bits = (old_nb_mb + 1) * vm->sbm.sbs_per_mb;
  504. int old_pages = PFN_UP(BITS_TO_LONGS(old_nb_bits) * sizeof(long));
  505. int new_pages = PFN_UP(BITS_TO_LONGS(new_nb_bits) * sizeof(long));
  506. unsigned long *new_bitmap, *old_bitmap;
  507. if (vm->sbm.sb_states && old_pages == new_pages)
  508. return 0;
  509. new_bitmap = vzalloc(new_pages * PAGE_SIZE);
  510. if (!new_bitmap)
  511. return -ENOMEM;
  512. mutex_lock(&vm->hotplug_mutex);
  513. if (vm->sbm.sb_states)
  514. memcpy(new_bitmap, vm->sbm.sb_states, old_pages * PAGE_SIZE);
  515. old_bitmap = vm->sbm.sb_states;
  516. vm->sbm.sb_states = new_bitmap;
  517. mutex_unlock(&vm->hotplug_mutex);
  518. vfree(old_bitmap);
  519. return 0;
  520. }
  521. /*
  522. * Test if we could add memory without creating too much offline memory -
  523. * to avoid running OOM if memory is getting onlined deferred.
  524. */
  525. static bool virtio_mem_could_add_memory(struct virtio_mem *vm, uint64_t size)
  526. {
  527. if (WARN_ON_ONCE(size > vm->offline_threshold))
  528. return false;
  529. return atomic64_read(&vm->offline_size) + size <= vm->offline_threshold;
  530. }
  531. /*
  532. * Try adding memory to Linux. Will usually only fail if out of memory.
  533. *
  534. * Must not be called with the vm->hotplug_mutex held (possible deadlock with
  535. * onlining code).
  536. *
  537. * Will not modify the state of memory blocks in virtio-mem.
  538. */
  539. static int virtio_mem_add_memory(struct virtio_mem *vm, uint64_t addr,
  540. uint64_t size)
  541. {
  542. int rc;
  543. /*
  544. * When force-unloading the driver and we still have memory added to
  545. * Linux, the resource name has to stay.
  546. */
  547. if (!vm->resource_name) {
  548. vm->resource_name = kstrdup_const("System RAM (virtio_mem)",
  549. GFP_KERNEL);
  550. if (!vm->resource_name)
  551. return -ENOMEM;
  552. }
  553. dev_dbg(&vm->vdev->dev, "adding memory: 0x%llx - 0x%llx\n", addr,
  554. addr + size - 1);
  555. /* Memory might get onlined immediately. */
  556. atomic64_add(size, &vm->offline_size);
  557. rc = add_memory_driver_managed(vm->mgid, addr, size, vm->resource_name,
  558. MHP_MERGE_RESOURCE | MHP_NID_IS_MGID);
  559. if (rc) {
  560. atomic64_sub(size, &vm->offline_size);
  561. dev_warn(&vm->vdev->dev, "adding memory failed: %d\n", rc);
  562. /*
  563. * TODO: Linux MM does not properly clean up yet in all cases
  564. * where adding of memory failed - especially on -ENOMEM.
  565. */
  566. }
  567. return rc;
  568. }
  569. /*
  570. * See virtio_mem_add_memory(): Try adding a single Linux memory block.
  571. */
  572. static int virtio_mem_sbm_add_mb(struct virtio_mem *vm, unsigned long mb_id)
  573. {
  574. const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
  575. const uint64_t size = memory_block_size_bytes();
  576. return virtio_mem_add_memory(vm, addr, size);
  577. }
  578. /*
  579. * See virtio_mem_add_memory(): Try adding a big block.
  580. */
  581. static int virtio_mem_bbm_add_bb(struct virtio_mem *vm, unsigned long bb_id)
  582. {
  583. const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id);
  584. const uint64_t size = vm->bbm.bb_size;
  585. return virtio_mem_add_memory(vm, addr, size);
  586. }
  587. /*
  588. * Try removing memory from Linux. Will only fail if memory blocks aren't
  589. * offline.
  590. *
  591. * Must not be called with the vm->hotplug_mutex held (possible deadlock with
  592. * onlining code).
  593. *
  594. * Will not modify the state of memory blocks in virtio-mem.
  595. */
  596. static int virtio_mem_remove_memory(struct virtio_mem *vm, uint64_t addr,
  597. uint64_t size)
  598. {
  599. int rc;
  600. dev_dbg(&vm->vdev->dev, "removing memory: 0x%llx - 0x%llx\n", addr,
  601. addr + size - 1);
  602. rc = remove_memory(addr, size);
  603. if (!rc) {
  604. atomic64_sub(size, &vm->offline_size);
  605. /*
  606. * We might have freed up memory we can now unplug, retry
  607. * immediately instead of waiting.
  608. */
  609. virtio_mem_retry(vm);
  610. } else {
  611. dev_dbg(&vm->vdev->dev, "removing memory failed: %d\n", rc);
  612. }
  613. return rc;
  614. }
  615. /*
  616. * See virtio_mem_remove_memory(): Try removing a single Linux memory block.
  617. */
  618. static int virtio_mem_sbm_remove_mb(struct virtio_mem *vm, unsigned long mb_id)
  619. {
  620. const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
  621. const uint64_t size = memory_block_size_bytes();
  622. return virtio_mem_remove_memory(vm, addr, size);
  623. }
  624. /*
  625. * Try offlining and removing memory from Linux.
  626. *
  627. * Must not be called with the vm->hotplug_mutex held (possible deadlock with
  628. * onlining code).
  629. *
  630. * Will not modify the state of memory blocks in virtio-mem.
  631. */
  632. static int virtio_mem_offline_and_remove_memory(struct virtio_mem *vm,
  633. uint64_t addr,
  634. uint64_t size)
  635. {
  636. int rc;
  637. dev_dbg(&vm->vdev->dev,
  638. "offlining and removing memory: 0x%llx - 0x%llx\n", addr,
  639. addr + size - 1);
  640. rc = offline_and_remove_memory(addr, size);
  641. if (!rc) {
  642. atomic64_sub(size, &vm->offline_size);
  643. /*
  644. * We might have freed up memory we can now unplug, retry
  645. * immediately instead of waiting.
  646. */
  647. virtio_mem_retry(vm);
  648. return 0;
  649. }
  650. dev_dbg(&vm->vdev->dev, "offlining and removing memory failed: %d\n", rc);
  651. /*
  652. * We don't really expect this to fail, because we fake-offlined all
  653. * memory already. But it could fail in corner cases.
  654. */
  655. WARN_ON_ONCE(rc != -ENOMEM && rc != -EBUSY);
  656. return rc == -ENOMEM ? -ENOMEM : -EBUSY;
  657. }
  658. /*
  659. * See virtio_mem_offline_and_remove_memory(): Try offlining and removing
  660. * a single Linux memory block.
  661. */
  662. static int virtio_mem_sbm_offline_and_remove_mb(struct virtio_mem *vm,
  663. unsigned long mb_id)
  664. {
  665. const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
  666. const uint64_t size = memory_block_size_bytes();
  667. return virtio_mem_offline_and_remove_memory(vm, addr, size);
  668. }
  669. /*
  670. * Try (offlining and) removing memory from Linux in case all subblocks are
  671. * unplugged. Can be called on online and offline memory blocks.
  672. *
  673. * May modify the state of memory blocks in virtio-mem.
  674. */
  675. static int virtio_mem_sbm_try_remove_unplugged_mb(struct virtio_mem *vm,
  676. unsigned long mb_id)
  677. {
  678. int rc;
  679. /*
  680. * Once all subblocks of a memory block were unplugged, offline and
  681. * remove it.
  682. */
  683. if (!virtio_mem_sbm_test_sb_unplugged(vm, mb_id, 0, vm->sbm.sbs_per_mb))
  684. return 0;
  685. /* offline_and_remove_memory() works for online and offline memory. */
  686. mutex_unlock(&vm->hotplug_mutex);
  687. rc = virtio_mem_sbm_offline_and_remove_mb(vm, mb_id);
  688. mutex_lock(&vm->hotplug_mutex);
  689. if (!rc)
  690. virtio_mem_sbm_set_mb_state(vm, mb_id,
  691. VIRTIO_MEM_SBM_MB_UNUSED);
  692. return rc;
  693. }
  694. /*
  695. * See virtio_mem_offline_and_remove_memory(): Try to offline and remove a
  696. * all Linux memory blocks covered by the big block.
  697. */
  698. static int virtio_mem_bbm_offline_and_remove_bb(struct virtio_mem *vm,
  699. unsigned long bb_id)
  700. {
  701. const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id);
  702. const uint64_t size = vm->bbm.bb_size;
  703. return virtio_mem_offline_and_remove_memory(vm, addr, size);
  704. }
  705. /*
  706. * Trigger the workqueue so the device can perform its magic.
  707. */
  708. static void virtio_mem_retry(struct virtio_mem *vm)
  709. {
  710. unsigned long flags;
  711. spin_lock_irqsave(&vm->removal_lock, flags);
  712. if (!vm->removing)
  713. queue_work(system_freezable_wq, &vm->wq);
  714. spin_unlock_irqrestore(&vm->removal_lock, flags);
  715. }
  716. static int virtio_mem_translate_node_id(struct virtio_mem *vm, uint16_t node_id)
  717. {
  718. int node = NUMA_NO_NODE;
  719. #if defined(CONFIG_ACPI_NUMA)
  720. if (virtio_has_feature(vm->vdev, VIRTIO_MEM_F_ACPI_PXM))
  721. node = pxm_to_node(node_id);
  722. #endif
  723. return node;
  724. }
  725. /*
  726. * Test if a virtio-mem device overlaps with the given range. Can be called
  727. * from (notifier) callbacks lockless.
  728. */
  729. static bool virtio_mem_overlaps_range(struct virtio_mem *vm, uint64_t start,
  730. uint64_t size)
  731. {
  732. return start < vm->addr + vm->region_size && vm->addr < start + size;
  733. }
  734. /*
  735. * Test if a virtio-mem device contains a given range. Can be called from
  736. * (notifier) callbacks lockless.
  737. */
  738. static bool virtio_mem_contains_range(struct virtio_mem *vm, uint64_t start,
  739. uint64_t size)
  740. {
  741. return start >= vm->addr && start + size <= vm->addr + vm->region_size;
  742. }
  743. static int virtio_mem_sbm_notify_going_online(struct virtio_mem *vm,
  744. unsigned long mb_id)
  745. {
  746. switch (virtio_mem_sbm_get_mb_state(vm, mb_id)) {
  747. case VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL:
  748. case VIRTIO_MEM_SBM_MB_OFFLINE:
  749. return NOTIFY_OK;
  750. default:
  751. break;
  752. }
  753. dev_warn_ratelimited(&vm->vdev->dev,
  754. "memory block onlining denied\n");
  755. return NOTIFY_BAD;
  756. }
  757. static void virtio_mem_sbm_notify_offline(struct virtio_mem *vm,
  758. unsigned long mb_id)
  759. {
  760. switch (virtio_mem_sbm_get_mb_state(vm, mb_id)) {
  761. case VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL:
  762. case VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL:
  763. virtio_mem_sbm_set_mb_state(vm, mb_id,
  764. VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL);
  765. break;
  766. case VIRTIO_MEM_SBM_MB_KERNEL:
  767. case VIRTIO_MEM_SBM_MB_MOVABLE:
  768. virtio_mem_sbm_set_mb_state(vm, mb_id,
  769. VIRTIO_MEM_SBM_MB_OFFLINE);
  770. break;
  771. default:
  772. BUG();
  773. break;
  774. }
  775. }
  776. static void virtio_mem_sbm_notify_online(struct virtio_mem *vm,
  777. unsigned long mb_id,
  778. unsigned long start_pfn)
  779. {
  780. const bool is_movable = is_zone_movable_page(pfn_to_page(start_pfn));
  781. int new_state;
  782. switch (virtio_mem_sbm_get_mb_state(vm, mb_id)) {
  783. case VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL:
  784. new_state = VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL;
  785. if (is_movable)
  786. new_state = VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL;
  787. break;
  788. case VIRTIO_MEM_SBM_MB_OFFLINE:
  789. new_state = VIRTIO_MEM_SBM_MB_KERNEL;
  790. if (is_movable)
  791. new_state = VIRTIO_MEM_SBM_MB_MOVABLE;
  792. break;
  793. default:
  794. BUG();
  795. break;
  796. }
  797. virtio_mem_sbm_set_mb_state(vm, mb_id, new_state);
  798. }
  799. static void virtio_mem_sbm_notify_going_offline(struct virtio_mem *vm,
  800. unsigned long mb_id)
  801. {
  802. const unsigned long nr_pages = PFN_DOWN(vm->sbm.sb_size);
  803. unsigned long pfn;
  804. int sb_id;
  805. for (sb_id = 0; sb_id < vm->sbm.sbs_per_mb; sb_id++) {
  806. if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id, 1))
  807. continue;
  808. pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
  809. sb_id * vm->sbm.sb_size);
  810. virtio_mem_fake_offline_going_offline(pfn, nr_pages);
  811. }
  812. }
  813. static void virtio_mem_sbm_notify_cancel_offline(struct virtio_mem *vm,
  814. unsigned long mb_id)
  815. {
  816. const unsigned long nr_pages = PFN_DOWN(vm->sbm.sb_size);
  817. unsigned long pfn;
  818. int sb_id;
  819. for (sb_id = 0; sb_id < vm->sbm.sbs_per_mb; sb_id++) {
  820. if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id, 1))
  821. continue;
  822. pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
  823. sb_id * vm->sbm.sb_size);
  824. virtio_mem_fake_offline_cancel_offline(pfn, nr_pages);
  825. }
  826. }
  827. static void virtio_mem_bbm_notify_going_offline(struct virtio_mem *vm,
  828. unsigned long bb_id,
  829. unsigned long pfn,
  830. unsigned long nr_pages)
  831. {
  832. /*
  833. * When marked as "fake-offline", all online memory of this device block
  834. * is allocated by us. Otherwise, we don't have any memory allocated.
  835. */
  836. if (virtio_mem_bbm_get_bb_state(vm, bb_id) !=
  837. VIRTIO_MEM_BBM_BB_FAKE_OFFLINE)
  838. return;
  839. virtio_mem_fake_offline_going_offline(pfn, nr_pages);
  840. }
  841. static void virtio_mem_bbm_notify_cancel_offline(struct virtio_mem *vm,
  842. unsigned long bb_id,
  843. unsigned long pfn,
  844. unsigned long nr_pages)
  845. {
  846. if (virtio_mem_bbm_get_bb_state(vm, bb_id) !=
  847. VIRTIO_MEM_BBM_BB_FAKE_OFFLINE)
  848. return;
  849. virtio_mem_fake_offline_cancel_offline(pfn, nr_pages);
  850. }
  851. /*
  852. * This callback will either be called synchronously from add_memory() or
  853. * asynchronously (e.g., triggered via user space). We have to be careful
  854. * with locking when calling add_memory().
  855. */
  856. static int virtio_mem_memory_notifier_cb(struct notifier_block *nb,
  857. unsigned long action, void *arg)
  858. {
  859. struct virtio_mem *vm = container_of(nb, struct virtio_mem,
  860. memory_notifier);
  861. struct memory_notify *mhp = arg;
  862. const unsigned long start = PFN_PHYS(mhp->start_pfn);
  863. const unsigned long size = PFN_PHYS(mhp->nr_pages);
  864. int rc = NOTIFY_OK;
  865. unsigned long id;
  866. if (!virtio_mem_overlaps_range(vm, start, size))
  867. return NOTIFY_DONE;
  868. if (vm->in_sbm) {
  869. id = virtio_mem_phys_to_mb_id(start);
  870. /*
  871. * In SBM, we add memory in separate memory blocks - we expect
  872. * it to be onlined/offlined in the same granularity. Bail out
  873. * if this ever changes.
  874. */
  875. if (WARN_ON_ONCE(size != memory_block_size_bytes() ||
  876. !IS_ALIGNED(start, memory_block_size_bytes())))
  877. return NOTIFY_BAD;
  878. } else {
  879. id = virtio_mem_phys_to_bb_id(vm, start);
  880. /*
  881. * In BBM, we only care about onlining/offlining happening
  882. * within a single big block, we don't care about the
  883. * actual granularity as we don't track individual Linux
  884. * memory blocks.
  885. */
  886. if (WARN_ON_ONCE(id != virtio_mem_phys_to_bb_id(vm, start + size - 1)))
  887. return NOTIFY_BAD;
  888. }
  889. /*
  890. * Avoid circular locking lockdep warnings. We lock the mutex
  891. * e.g., in MEM_GOING_ONLINE and unlock it in MEM_ONLINE. The
  892. * blocking_notifier_call_chain() has it's own lock, which gets unlocked
  893. * between both notifier calls and will bail out. False positive.
  894. */
  895. lockdep_off();
  896. switch (action) {
  897. case MEM_GOING_OFFLINE:
  898. mutex_lock(&vm->hotplug_mutex);
  899. if (vm->removing) {
  900. rc = notifier_from_errno(-EBUSY);
  901. mutex_unlock(&vm->hotplug_mutex);
  902. break;
  903. }
  904. vm->hotplug_active = true;
  905. if (vm->in_sbm)
  906. virtio_mem_sbm_notify_going_offline(vm, id);
  907. else
  908. virtio_mem_bbm_notify_going_offline(vm, id,
  909. mhp->start_pfn,
  910. mhp->nr_pages);
  911. break;
  912. case MEM_GOING_ONLINE:
  913. mutex_lock(&vm->hotplug_mutex);
  914. if (vm->removing) {
  915. rc = notifier_from_errno(-EBUSY);
  916. mutex_unlock(&vm->hotplug_mutex);
  917. break;
  918. }
  919. vm->hotplug_active = true;
  920. if (vm->in_sbm)
  921. rc = virtio_mem_sbm_notify_going_online(vm, id);
  922. break;
  923. case MEM_OFFLINE:
  924. if (vm->in_sbm)
  925. virtio_mem_sbm_notify_offline(vm, id);
  926. atomic64_add(size, &vm->offline_size);
  927. /*
  928. * Trigger the workqueue. Now that we have some offline memory,
  929. * maybe we can handle pending unplug requests.
  930. */
  931. if (!unplug_online)
  932. virtio_mem_retry(vm);
  933. vm->hotplug_active = false;
  934. mutex_unlock(&vm->hotplug_mutex);
  935. break;
  936. case MEM_ONLINE:
  937. if (vm->in_sbm)
  938. virtio_mem_sbm_notify_online(vm, id, mhp->start_pfn);
  939. atomic64_sub(size, &vm->offline_size);
  940. /*
  941. * Start adding more memory once we onlined half of our
  942. * threshold. Don't trigger if it's possibly due to our actipn
  943. * (e.g., us adding memory which gets onlined immediately from
  944. * the core).
  945. */
  946. if (!atomic_read(&vm->wq_active) &&
  947. virtio_mem_could_add_memory(vm, vm->offline_threshold / 2))
  948. virtio_mem_retry(vm);
  949. vm->hotplug_active = false;
  950. mutex_unlock(&vm->hotplug_mutex);
  951. break;
  952. case MEM_CANCEL_OFFLINE:
  953. if (!vm->hotplug_active)
  954. break;
  955. if (vm->in_sbm)
  956. virtio_mem_sbm_notify_cancel_offline(vm, id);
  957. else
  958. virtio_mem_bbm_notify_cancel_offline(vm, id,
  959. mhp->start_pfn,
  960. mhp->nr_pages);
  961. vm->hotplug_active = false;
  962. mutex_unlock(&vm->hotplug_mutex);
  963. break;
  964. case MEM_CANCEL_ONLINE:
  965. if (!vm->hotplug_active)
  966. break;
  967. vm->hotplug_active = false;
  968. mutex_unlock(&vm->hotplug_mutex);
  969. break;
  970. default:
  971. break;
  972. }
  973. lockdep_on();
  974. return rc;
  975. }
  976. static int virtio_mem_pm_notifier_cb(struct notifier_block *nb,
  977. unsigned long action, void *arg)
  978. {
  979. struct virtio_mem *vm = container_of(nb, struct virtio_mem,
  980. pm_notifier);
  981. switch (action) {
  982. case PM_HIBERNATION_PREPARE:
  983. case PM_RESTORE_PREPARE:
  984. /*
  985. * When restarting the VM, all memory is unplugged. Don't
  986. * allow to hibernate and restore from an image.
  987. */
  988. dev_err(&vm->vdev->dev, "hibernation is not supported.\n");
  989. return NOTIFY_BAD;
  990. default:
  991. return NOTIFY_OK;
  992. }
  993. }
  994. /*
  995. * Set a range of pages PG_offline. Remember pages that were never onlined
  996. * (via generic_online_page()) using PageDirty().
  997. */
  998. static void virtio_mem_set_fake_offline(unsigned long pfn,
  999. unsigned long nr_pages, bool onlined)
  1000. {
  1001. page_offline_begin();
  1002. for (; nr_pages--; pfn++) {
  1003. struct page *page = pfn_to_page(pfn);
  1004. if (!onlined)
  1005. /*
  1006. * Pages that have not been onlined yet were initialized
  1007. * to PageOffline(). Remember that we have to route them
  1008. * through generic_online_page().
  1009. */
  1010. SetPageDirty(page);
  1011. else
  1012. __SetPageOffline(page);
  1013. VM_WARN_ON_ONCE(!PageOffline(page));
  1014. }
  1015. page_offline_end();
  1016. }
  1017. /*
  1018. * Clear PG_offline from a range of pages. If the pages were never onlined,
  1019. * (via generic_online_page()), clear PageDirty().
  1020. */
  1021. static void virtio_mem_clear_fake_offline(unsigned long pfn,
  1022. unsigned long nr_pages, bool onlined)
  1023. {
  1024. for (; nr_pages--; pfn++) {
  1025. struct page *page = pfn_to_page(pfn);
  1026. if (!onlined)
  1027. /* generic_online_page() will clear PageOffline(). */
  1028. ClearPageDirty(page);
  1029. else
  1030. __ClearPageOffline(page);
  1031. }
  1032. }
  1033. /*
  1034. * Release a range of fake-offline pages to the buddy, effectively
  1035. * fake-onlining them.
  1036. */
  1037. static void virtio_mem_fake_online(unsigned long pfn, unsigned long nr_pages)
  1038. {
  1039. unsigned long order = MAX_PAGE_ORDER;
  1040. unsigned long i;
  1041. /*
  1042. * We might get called for ranges that don't cover properly aligned
  1043. * MAX_PAGE_ORDER pages; however, we can only online properly aligned
  1044. * pages with an order of MAX_PAGE_ORDER at maximum.
  1045. */
  1046. while (!IS_ALIGNED(pfn | nr_pages, 1 << order))
  1047. order--;
  1048. for (i = 0; i < nr_pages; i += 1 << order) {
  1049. struct page *page = pfn_to_page(pfn + i);
  1050. /*
  1051. * If the page is PageDirty(), it was kept fake-offline when
  1052. * onlining the memory block. Otherwise, it was allocated
  1053. * using alloc_contig_range(). All pages in a subblock are
  1054. * alike.
  1055. */
  1056. if (PageDirty(page)) {
  1057. virtio_mem_clear_fake_offline(pfn + i, 1 << order, false);
  1058. generic_online_page(page, order);
  1059. } else {
  1060. virtio_mem_clear_fake_offline(pfn + i, 1 << order, true);
  1061. free_contig_range(pfn + i, 1 << order);
  1062. adjust_managed_page_count(page, 1 << order);
  1063. }
  1064. }
  1065. }
  1066. /*
  1067. * Try to allocate a range, marking pages fake-offline, effectively
  1068. * fake-offlining them.
  1069. */
  1070. static int virtio_mem_fake_offline(struct virtio_mem *vm, unsigned long pfn,
  1071. unsigned long nr_pages)
  1072. {
  1073. const bool is_movable = is_zone_movable_page(pfn_to_page(pfn));
  1074. int rc, retry_count;
  1075. /*
  1076. * TODO: We want an alloc_contig_range() mode that tries to allocate
  1077. * harder (e.g., dealing with temporarily pinned pages, PCP), especially
  1078. * with ZONE_MOVABLE. So for now, retry a couple of times with
  1079. * ZONE_MOVABLE before giving up - because that zone is supposed to give
  1080. * some guarantees.
  1081. */
  1082. for (retry_count = 0; retry_count < 5; retry_count++) {
  1083. /*
  1084. * If the config changed, stop immediately and go back to the
  1085. * main loop: avoid trying to keep unplugging if the device
  1086. * might have decided to not remove any more memory.
  1087. */
  1088. if (atomic_read(&vm->config_changed))
  1089. return -EAGAIN;
  1090. rc = alloc_contig_range(pfn, pfn + nr_pages, MIGRATE_MOVABLE,
  1091. GFP_KERNEL);
  1092. if (rc == -ENOMEM)
  1093. /* whoops, out of memory */
  1094. return rc;
  1095. else if (rc && !is_movable)
  1096. break;
  1097. else if (rc)
  1098. continue;
  1099. virtio_mem_set_fake_offline(pfn, nr_pages, true);
  1100. adjust_managed_page_count(pfn_to_page(pfn), -nr_pages);
  1101. return 0;
  1102. }
  1103. return -EBUSY;
  1104. }
  1105. /*
  1106. * Handle fake-offline pages when memory is going offline - such that the
  1107. * pages can be skipped by mm-core when offlining.
  1108. */
  1109. static void virtio_mem_fake_offline_going_offline(unsigned long pfn,
  1110. unsigned long nr_pages)
  1111. {
  1112. struct page *page;
  1113. unsigned long i;
  1114. /* Drop our reference to the pages so the memory can get offlined. */
  1115. for (i = 0; i < nr_pages; i++) {
  1116. page = pfn_to_page(pfn + i);
  1117. if (WARN_ON(!page_ref_dec_and_test(page)))
  1118. dump_page(page, "fake-offline page referenced");
  1119. }
  1120. }
  1121. /*
  1122. * Handle fake-offline pages when memory offlining is canceled - to undo
  1123. * what we did in virtio_mem_fake_offline_going_offline().
  1124. */
  1125. static void virtio_mem_fake_offline_cancel_offline(unsigned long pfn,
  1126. unsigned long nr_pages)
  1127. {
  1128. unsigned long i;
  1129. /*
  1130. * Get the reference again that we dropped via page_ref_dec_and_test()
  1131. * when going offline.
  1132. */
  1133. for (i = 0; i < nr_pages; i++)
  1134. page_ref_inc(pfn_to_page(pfn + i));
  1135. }
  1136. static void virtio_mem_online_page(struct virtio_mem *vm,
  1137. struct page *page, unsigned int order)
  1138. {
  1139. const unsigned long start = page_to_phys(page);
  1140. const unsigned long end = start + PFN_PHYS(1 << order);
  1141. unsigned long addr, next, id, sb_id, count;
  1142. bool do_online;
  1143. /*
  1144. * We can get called with any order up to MAX_PAGE_ORDER. If our subblock
  1145. * size is smaller than that and we have a mixture of plugged and
  1146. * unplugged subblocks within such a page, we have to process in
  1147. * smaller granularity. In that case we'll adjust the order exactly once
  1148. * within the loop.
  1149. */
  1150. for (addr = start; addr < end; ) {
  1151. next = addr + PFN_PHYS(1 << order);
  1152. if (vm->in_sbm) {
  1153. id = virtio_mem_phys_to_mb_id(addr);
  1154. sb_id = virtio_mem_phys_to_sb_id(vm, addr);
  1155. count = virtio_mem_phys_to_sb_id(vm, next - 1) - sb_id + 1;
  1156. if (virtio_mem_sbm_test_sb_plugged(vm, id, sb_id, count)) {
  1157. /* Fully plugged. */
  1158. do_online = true;
  1159. } else if (count == 1 ||
  1160. virtio_mem_sbm_test_sb_unplugged(vm, id, sb_id, count)) {
  1161. /* Fully unplugged. */
  1162. do_online = false;
  1163. } else {
  1164. /*
  1165. * Mixture, process sub-blocks instead. This
  1166. * will be at least the size of a pageblock.
  1167. * We'll run into this case exactly once.
  1168. */
  1169. order = ilog2(vm->sbm.sb_size) - PAGE_SHIFT;
  1170. do_online = virtio_mem_sbm_test_sb_plugged(vm, id, sb_id, 1);
  1171. continue;
  1172. }
  1173. } else {
  1174. /*
  1175. * If the whole block is marked fake offline, keep
  1176. * everything that way.
  1177. */
  1178. id = virtio_mem_phys_to_bb_id(vm, addr);
  1179. do_online = virtio_mem_bbm_get_bb_state(vm, id) !=
  1180. VIRTIO_MEM_BBM_BB_FAKE_OFFLINE;
  1181. }
  1182. if (do_online)
  1183. generic_online_page(pfn_to_page(PFN_DOWN(addr)), order);
  1184. else
  1185. virtio_mem_set_fake_offline(PFN_DOWN(addr), 1 << order,
  1186. false);
  1187. addr = next;
  1188. }
  1189. }
  1190. static void virtio_mem_online_page_cb(struct page *page, unsigned int order)
  1191. {
  1192. const unsigned long addr = page_to_phys(page);
  1193. struct virtio_mem *vm;
  1194. rcu_read_lock();
  1195. list_for_each_entry_rcu(vm, &virtio_mem_devices, next) {
  1196. /*
  1197. * Pages we're onlining will never cross memory blocks and,
  1198. * therefore, not virtio-mem devices.
  1199. */
  1200. if (!virtio_mem_contains_range(vm, addr, PFN_PHYS(1 << order)))
  1201. continue;
  1202. /*
  1203. * virtio_mem_set_fake_offline() might sleep. We can safely
  1204. * drop the RCU lock at this point because the device
  1205. * cannot go away. See virtio_mem_remove() how races
  1206. * between memory onlining and device removal are handled.
  1207. */
  1208. rcu_read_unlock();
  1209. virtio_mem_online_page(vm, page, order);
  1210. return;
  1211. }
  1212. rcu_read_unlock();
  1213. /* not virtio-mem memory, but e.g., a DIMM. online it */
  1214. generic_online_page(page, order);
  1215. }
  1216. static uint64_t virtio_mem_send_request(struct virtio_mem *vm,
  1217. const struct virtio_mem_req *req)
  1218. {
  1219. struct scatterlist *sgs[2], sg_req, sg_resp;
  1220. unsigned int len;
  1221. int rc;
  1222. /* don't use the request residing on the stack (vaddr) */
  1223. vm->req = *req;
  1224. /* out: buffer for request */
  1225. sg_init_one(&sg_req, &vm->req, sizeof(vm->req));
  1226. sgs[0] = &sg_req;
  1227. /* in: buffer for response */
  1228. sg_init_one(&sg_resp, &vm->resp, sizeof(vm->resp));
  1229. sgs[1] = &sg_resp;
  1230. rc = virtqueue_add_sgs(vm->vq, sgs, 1, 1, vm, GFP_KERNEL);
  1231. if (rc < 0)
  1232. return rc;
  1233. virtqueue_kick(vm->vq);
  1234. /* wait for a response */
  1235. wait_event(vm->host_resp, virtqueue_get_buf(vm->vq, &len));
  1236. return virtio16_to_cpu(vm->vdev, vm->resp.type);
  1237. }
  1238. static int virtio_mem_send_plug_request(struct virtio_mem *vm, uint64_t addr,
  1239. uint64_t size)
  1240. {
  1241. const uint64_t nb_vm_blocks = size / vm->device_block_size;
  1242. const struct virtio_mem_req req = {
  1243. .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_PLUG),
  1244. .u.plug.addr = cpu_to_virtio64(vm->vdev, addr),
  1245. .u.plug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
  1246. };
  1247. int rc = -ENOMEM;
  1248. if (atomic_read(&vm->config_changed))
  1249. return -EAGAIN;
  1250. dev_dbg(&vm->vdev->dev, "plugging memory: 0x%llx - 0x%llx\n", addr,
  1251. addr + size - 1);
  1252. switch (virtio_mem_send_request(vm, &req)) {
  1253. case VIRTIO_MEM_RESP_ACK:
  1254. vm->plugged_size += size;
  1255. return 0;
  1256. case VIRTIO_MEM_RESP_NACK:
  1257. rc = -EAGAIN;
  1258. break;
  1259. case VIRTIO_MEM_RESP_BUSY:
  1260. rc = -ETXTBSY;
  1261. break;
  1262. case VIRTIO_MEM_RESP_ERROR:
  1263. rc = -EINVAL;
  1264. break;
  1265. default:
  1266. break;
  1267. }
  1268. dev_dbg(&vm->vdev->dev, "plugging memory failed: %d\n", rc);
  1269. return rc;
  1270. }
  1271. static int virtio_mem_send_unplug_request(struct virtio_mem *vm, uint64_t addr,
  1272. uint64_t size)
  1273. {
  1274. const uint64_t nb_vm_blocks = size / vm->device_block_size;
  1275. const struct virtio_mem_req req = {
  1276. .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG),
  1277. .u.unplug.addr = cpu_to_virtio64(vm->vdev, addr),
  1278. .u.unplug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
  1279. };
  1280. int rc = -ENOMEM;
  1281. if (atomic_read(&vm->config_changed))
  1282. return -EAGAIN;
  1283. dev_dbg(&vm->vdev->dev, "unplugging memory: 0x%llx - 0x%llx\n", addr,
  1284. addr + size - 1);
  1285. switch (virtio_mem_send_request(vm, &req)) {
  1286. case VIRTIO_MEM_RESP_ACK:
  1287. vm->plugged_size -= size;
  1288. return 0;
  1289. case VIRTIO_MEM_RESP_BUSY:
  1290. rc = -ETXTBSY;
  1291. break;
  1292. case VIRTIO_MEM_RESP_ERROR:
  1293. rc = -EINVAL;
  1294. break;
  1295. default:
  1296. break;
  1297. }
  1298. dev_dbg(&vm->vdev->dev, "unplugging memory failed: %d\n", rc);
  1299. return rc;
  1300. }
  1301. static int virtio_mem_send_unplug_all_request(struct virtio_mem *vm)
  1302. {
  1303. const struct virtio_mem_req req = {
  1304. .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG_ALL),
  1305. };
  1306. int rc = -ENOMEM;
  1307. dev_dbg(&vm->vdev->dev, "unplugging all memory");
  1308. switch (virtio_mem_send_request(vm, &req)) {
  1309. case VIRTIO_MEM_RESP_ACK:
  1310. vm->unplug_all_required = false;
  1311. vm->plugged_size = 0;
  1312. /* usable region might have shrunk */
  1313. atomic_set(&vm->config_changed, 1);
  1314. return 0;
  1315. case VIRTIO_MEM_RESP_BUSY:
  1316. rc = -ETXTBSY;
  1317. break;
  1318. default:
  1319. break;
  1320. }
  1321. dev_dbg(&vm->vdev->dev, "unplugging all memory failed: %d\n", rc);
  1322. return rc;
  1323. }
  1324. /*
  1325. * Plug selected subblocks. Updates the plugged state, but not the state
  1326. * of the memory block.
  1327. */
  1328. static int virtio_mem_sbm_plug_sb(struct virtio_mem *vm, unsigned long mb_id,
  1329. int sb_id, int count)
  1330. {
  1331. const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id) +
  1332. sb_id * vm->sbm.sb_size;
  1333. const uint64_t size = count * vm->sbm.sb_size;
  1334. int rc;
  1335. rc = virtio_mem_send_plug_request(vm, addr, size);
  1336. if (!rc)
  1337. virtio_mem_sbm_set_sb_plugged(vm, mb_id, sb_id, count);
  1338. return rc;
  1339. }
  1340. /*
  1341. * Unplug selected subblocks. Updates the plugged state, but not the state
  1342. * of the memory block.
  1343. */
  1344. static int virtio_mem_sbm_unplug_sb(struct virtio_mem *vm, unsigned long mb_id,
  1345. int sb_id, int count)
  1346. {
  1347. const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id) +
  1348. sb_id * vm->sbm.sb_size;
  1349. const uint64_t size = count * vm->sbm.sb_size;
  1350. int rc;
  1351. rc = virtio_mem_send_unplug_request(vm, addr, size);
  1352. if (!rc)
  1353. virtio_mem_sbm_set_sb_unplugged(vm, mb_id, sb_id, count);
  1354. return rc;
  1355. }
  1356. /*
  1357. * Request to unplug a big block.
  1358. *
  1359. * Will not modify the state of the big block.
  1360. */
  1361. static int virtio_mem_bbm_unplug_bb(struct virtio_mem *vm, unsigned long bb_id)
  1362. {
  1363. const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id);
  1364. const uint64_t size = vm->bbm.bb_size;
  1365. return virtio_mem_send_unplug_request(vm, addr, size);
  1366. }
  1367. /*
  1368. * Request to plug a big block.
  1369. *
  1370. * Will not modify the state of the big block.
  1371. */
  1372. static int virtio_mem_bbm_plug_bb(struct virtio_mem *vm, unsigned long bb_id)
  1373. {
  1374. const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id);
  1375. const uint64_t size = vm->bbm.bb_size;
  1376. return virtio_mem_send_plug_request(vm, addr, size);
  1377. }
  1378. /*
  1379. * Unplug the desired number of plugged subblocks of a offline or not-added
  1380. * memory block. Will fail if any subblock cannot get unplugged (instead of
  1381. * skipping it).
  1382. *
  1383. * Will not modify the state of the memory block.
  1384. *
  1385. * Note: can fail after some subblocks were unplugged.
  1386. */
  1387. static int virtio_mem_sbm_unplug_any_sb_raw(struct virtio_mem *vm,
  1388. unsigned long mb_id, uint64_t *nb_sb)
  1389. {
  1390. int sb_id, count;
  1391. int rc;
  1392. sb_id = vm->sbm.sbs_per_mb - 1;
  1393. while (*nb_sb) {
  1394. /* Find the next candidate subblock */
  1395. while (sb_id >= 0 &&
  1396. virtio_mem_sbm_test_sb_unplugged(vm, mb_id, sb_id, 1))
  1397. sb_id--;
  1398. if (sb_id < 0)
  1399. break;
  1400. /* Try to unplug multiple subblocks at a time */
  1401. count = 1;
  1402. while (count < *nb_sb && sb_id > 0 &&
  1403. virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id - 1, 1)) {
  1404. count++;
  1405. sb_id--;
  1406. }
  1407. rc = virtio_mem_sbm_unplug_sb(vm, mb_id, sb_id, count);
  1408. if (rc)
  1409. return rc;
  1410. *nb_sb -= count;
  1411. sb_id--;
  1412. }
  1413. return 0;
  1414. }
  1415. /*
  1416. * Unplug all plugged subblocks of an offline or not-added memory block.
  1417. *
  1418. * Will not modify the state of the memory block.
  1419. *
  1420. * Note: can fail after some subblocks were unplugged.
  1421. */
  1422. static int virtio_mem_sbm_unplug_mb(struct virtio_mem *vm, unsigned long mb_id)
  1423. {
  1424. uint64_t nb_sb = vm->sbm.sbs_per_mb;
  1425. return virtio_mem_sbm_unplug_any_sb_raw(vm, mb_id, &nb_sb);
  1426. }
  1427. /*
  1428. * Prepare tracking data for the next memory block.
  1429. */
  1430. static int virtio_mem_sbm_prepare_next_mb(struct virtio_mem *vm,
  1431. unsigned long *mb_id)
  1432. {
  1433. int rc;
  1434. if (vm->sbm.next_mb_id > vm->sbm.last_usable_mb_id)
  1435. return -ENOSPC;
  1436. /* Resize the state array if required. */
  1437. rc = virtio_mem_sbm_mb_states_prepare_next_mb(vm);
  1438. if (rc)
  1439. return rc;
  1440. /* Resize the subblock bitmap if required. */
  1441. rc = virtio_mem_sbm_sb_states_prepare_next_mb(vm);
  1442. if (rc)
  1443. return rc;
  1444. vm->sbm.mb_count[VIRTIO_MEM_SBM_MB_UNUSED]++;
  1445. *mb_id = vm->sbm.next_mb_id++;
  1446. return 0;
  1447. }
  1448. /*
  1449. * Try to plug the desired number of subblocks and add the memory block
  1450. * to Linux.
  1451. *
  1452. * Will modify the state of the memory block.
  1453. */
  1454. static int virtio_mem_sbm_plug_and_add_mb(struct virtio_mem *vm,
  1455. unsigned long mb_id, uint64_t *nb_sb)
  1456. {
  1457. const int count = min_t(int, *nb_sb, vm->sbm.sbs_per_mb);
  1458. int rc;
  1459. if (WARN_ON_ONCE(!count))
  1460. return -EINVAL;
  1461. /*
  1462. * Plug the requested number of subblocks before adding it to linux,
  1463. * so that onlining will directly online all plugged subblocks.
  1464. */
  1465. rc = virtio_mem_sbm_plug_sb(vm, mb_id, 0, count);
  1466. if (rc)
  1467. return rc;
  1468. /*
  1469. * Mark the block properly offline before adding it to Linux,
  1470. * so the memory notifiers will find the block in the right state.
  1471. */
  1472. if (count == vm->sbm.sbs_per_mb)
  1473. virtio_mem_sbm_set_mb_state(vm, mb_id,
  1474. VIRTIO_MEM_SBM_MB_OFFLINE);
  1475. else
  1476. virtio_mem_sbm_set_mb_state(vm, mb_id,
  1477. VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL);
  1478. /* Add the memory block to linux - if that fails, try to unplug. */
  1479. rc = virtio_mem_sbm_add_mb(vm, mb_id);
  1480. if (rc) {
  1481. int new_state = VIRTIO_MEM_SBM_MB_UNUSED;
  1482. if (virtio_mem_sbm_unplug_sb(vm, mb_id, 0, count))
  1483. new_state = VIRTIO_MEM_SBM_MB_PLUGGED;
  1484. virtio_mem_sbm_set_mb_state(vm, mb_id, new_state);
  1485. return rc;
  1486. }
  1487. *nb_sb -= count;
  1488. return 0;
  1489. }
  1490. /*
  1491. * Try to plug the desired number of subblocks of a memory block that
  1492. * is already added to Linux.
  1493. *
  1494. * Will modify the state of the memory block.
  1495. *
  1496. * Note: Can fail after some subblocks were successfully plugged.
  1497. */
  1498. static int virtio_mem_sbm_plug_any_sb(struct virtio_mem *vm,
  1499. unsigned long mb_id, uint64_t *nb_sb)
  1500. {
  1501. const int old_state = virtio_mem_sbm_get_mb_state(vm, mb_id);
  1502. unsigned long pfn, nr_pages;
  1503. int sb_id, count;
  1504. int rc;
  1505. if (WARN_ON_ONCE(!*nb_sb))
  1506. return -EINVAL;
  1507. while (*nb_sb) {
  1508. sb_id = virtio_mem_sbm_first_unplugged_sb(vm, mb_id);
  1509. if (sb_id >= vm->sbm.sbs_per_mb)
  1510. break;
  1511. count = 1;
  1512. while (count < *nb_sb &&
  1513. sb_id + count < vm->sbm.sbs_per_mb &&
  1514. !virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id + count, 1))
  1515. count++;
  1516. rc = virtio_mem_sbm_plug_sb(vm, mb_id, sb_id, count);
  1517. if (rc)
  1518. return rc;
  1519. *nb_sb -= count;
  1520. if (old_state == VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL)
  1521. continue;
  1522. /* fake-online the pages if the memory block is online */
  1523. pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
  1524. sb_id * vm->sbm.sb_size);
  1525. nr_pages = PFN_DOWN(count * vm->sbm.sb_size);
  1526. virtio_mem_fake_online(pfn, nr_pages);
  1527. }
  1528. if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb))
  1529. virtio_mem_sbm_set_mb_state(vm, mb_id, old_state - 1);
  1530. return 0;
  1531. }
  1532. static int virtio_mem_sbm_plug_request(struct virtio_mem *vm, uint64_t diff)
  1533. {
  1534. const int mb_states[] = {
  1535. VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL,
  1536. VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL,
  1537. VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL,
  1538. };
  1539. uint64_t nb_sb = diff / vm->sbm.sb_size;
  1540. unsigned long mb_id;
  1541. int rc, i;
  1542. if (!nb_sb)
  1543. return 0;
  1544. /* Don't race with onlining/offlining */
  1545. mutex_lock(&vm->hotplug_mutex);
  1546. for (i = 0; i < ARRAY_SIZE(mb_states); i++) {
  1547. virtio_mem_sbm_for_each_mb(vm, mb_id, mb_states[i]) {
  1548. rc = virtio_mem_sbm_plug_any_sb(vm, mb_id, &nb_sb);
  1549. if (rc || !nb_sb)
  1550. goto out_unlock;
  1551. cond_resched();
  1552. }
  1553. }
  1554. /*
  1555. * We won't be working on online/offline memory blocks from this point,
  1556. * so we can't race with memory onlining/offlining. Drop the mutex.
  1557. */
  1558. mutex_unlock(&vm->hotplug_mutex);
  1559. /* Try to plug and add unused blocks */
  1560. virtio_mem_sbm_for_each_mb(vm, mb_id, VIRTIO_MEM_SBM_MB_UNUSED) {
  1561. if (!virtio_mem_could_add_memory(vm, memory_block_size_bytes()))
  1562. return -ENOSPC;
  1563. rc = virtio_mem_sbm_plug_and_add_mb(vm, mb_id, &nb_sb);
  1564. if (rc || !nb_sb)
  1565. return rc;
  1566. cond_resched();
  1567. }
  1568. /* Try to prepare, plug and add new blocks */
  1569. while (nb_sb) {
  1570. if (!virtio_mem_could_add_memory(vm, memory_block_size_bytes()))
  1571. return -ENOSPC;
  1572. rc = virtio_mem_sbm_prepare_next_mb(vm, &mb_id);
  1573. if (rc)
  1574. return rc;
  1575. rc = virtio_mem_sbm_plug_and_add_mb(vm, mb_id, &nb_sb);
  1576. if (rc)
  1577. return rc;
  1578. cond_resched();
  1579. }
  1580. return 0;
  1581. out_unlock:
  1582. mutex_unlock(&vm->hotplug_mutex);
  1583. return rc;
  1584. }
  1585. /*
  1586. * Plug a big block and add it to Linux.
  1587. *
  1588. * Will modify the state of the big block.
  1589. */
  1590. static int virtio_mem_bbm_plug_and_add_bb(struct virtio_mem *vm,
  1591. unsigned long bb_id)
  1592. {
  1593. int rc;
  1594. if (WARN_ON_ONCE(virtio_mem_bbm_get_bb_state(vm, bb_id) !=
  1595. VIRTIO_MEM_BBM_BB_UNUSED))
  1596. return -EINVAL;
  1597. rc = virtio_mem_bbm_plug_bb(vm, bb_id);
  1598. if (rc)
  1599. return rc;
  1600. virtio_mem_bbm_set_bb_state(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED);
  1601. rc = virtio_mem_bbm_add_bb(vm, bb_id);
  1602. if (rc) {
  1603. if (!virtio_mem_bbm_unplug_bb(vm, bb_id))
  1604. virtio_mem_bbm_set_bb_state(vm, bb_id,
  1605. VIRTIO_MEM_BBM_BB_UNUSED);
  1606. else
  1607. /* Retry from the main loop. */
  1608. virtio_mem_bbm_set_bb_state(vm, bb_id,
  1609. VIRTIO_MEM_BBM_BB_PLUGGED);
  1610. return rc;
  1611. }
  1612. return 0;
  1613. }
  1614. /*
  1615. * Prepare tracking data for the next big block.
  1616. */
  1617. static int virtio_mem_bbm_prepare_next_bb(struct virtio_mem *vm,
  1618. unsigned long *bb_id)
  1619. {
  1620. int rc;
  1621. if (vm->bbm.next_bb_id > vm->bbm.last_usable_bb_id)
  1622. return -ENOSPC;
  1623. /* Resize the big block state array if required. */
  1624. rc = virtio_mem_bbm_bb_states_prepare_next_bb(vm);
  1625. if (rc)
  1626. return rc;
  1627. vm->bbm.bb_count[VIRTIO_MEM_BBM_BB_UNUSED]++;
  1628. *bb_id = vm->bbm.next_bb_id;
  1629. vm->bbm.next_bb_id++;
  1630. return 0;
  1631. }
  1632. static int virtio_mem_bbm_plug_request(struct virtio_mem *vm, uint64_t diff)
  1633. {
  1634. uint64_t nb_bb = diff / vm->bbm.bb_size;
  1635. unsigned long bb_id;
  1636. int rc;
  1637. if (!nb_bb)
  1638. return 0;
  1639. /* Try to plug and add unused big blocks */
  1640. virtio_mem_bbm_for_each_bb(vm, bb_id, VIRTIO_MEM_BBM_BB_UNUSED) {
  1641. if (!virtio_mem_could_add_memory(vm, vm->bbm.bb_size))
  1642. return -ENOSPC;
  1643. rc = virtio_mem_bbm_plug_and_add_bb(vm, bb_id);
  1644. if (!rc)
  1645. nb_bb--;
  1646. if (rc || !nb_bb)
  1647. return rc;
  1648. cond_resched();
  1649. }
  1650. /* Try to prepare, plug and add new big blocks */
  1651. while (nb_bb) {
  1652. if (!virtio_mem_could_add_memory(vm, vm->bbm.bb_size))
  1653. return -ENOSPC;
  1654. rc = virtio_mem_bbm_prepare_next_bb(vm, &bb_id);
  1655. if (rc)
  1656. return rc;
  1657. rc = virtio_mem_bbm_plug_and_add_bb(vm, bb_id);
  1658. if (!rc)
  1659. nb_bb--;
  1660. if (rc)
  1661. return rc;
  1662. cond_resched();
  1663. }
  1664. return 0;
  1665. }
  1666. /*
  1667. * Try to plug the requested amount of memory.
  1668. */
  1669. static int virtio_mem_plug_request(struct virtio_mem *vm, uint64_t diff)
  1670. {
  1671. if (vm->in_sbm)
  1672. return virtio_mem_sbm_plug_request(vm, diff);
  1673. return virtio_mem_bbm_plug_request(vm, diff);
  1674. }
  1675. /*
  1676. * Unplug the desired number of plugged subblocks of an offline memory block.
  1677. * Will fail if any subblock cannot get unplugged (instead of skipping it).
  1678. *
  1679. * Will modify the state of the memory block. Might temporarily drop the
  1680. * hotplug_mutex.
  1681. *
  1682. * Note: Can fail after some subblocks were successfully unplugged.
  1683. */
  1684. static int virtio_mem_sbm_unplug_any_sb_offline(struct virtio_mem *vm,
  1685. unsigned long mb_id,
  1686. uint64_t *nb_sb)
  1687. {
  1688. int rc;
  1689. rc = virtio_mem_sbm_unplug_any_sb_raw(vm, mb_id, nb_sb);
  1690. /* some subblocks might have been unplugged even on failure */
  1691. if (!virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb))
  1692. virtio_mem_sbm_set_mb_state(vm, mb_id,
  1693. VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL);
  1694. if (rc)
  1695. return rc;
  1696. if (virtio_mem_sbm_test_sb_unplugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) {
  1697. /*
  1698. * Remove the block from Linux - this should never fail.
  1699. * Hinder the block from getting onlined by marking it
  1700. * unplugged. Temporarily drop the mutex, so
  1701. * any pending GOING_ONLINE requests can be serviced/rejected.
  1702. */
  1703. virtio_mem_sbm_set_mb_state(vm, mb_id,
  1704. VIRTIO_MEM_SBM_MB_UNUSED);
  1705. mutex_unlock(&vm->hotplug_mutex);
  1706. rc = virtio_mem_sbm_remove_mb(vm, mb_id);
  1707. BUG_ON(rc);
  1708. mutex_lock(&vm->hotplug_mutex);
  1709. }
  1710. return 0;
  1711. }
  1712. /*
  1713. * Unplug the given plugged subblocks of an online memory block.
  1714. *
  1715. * Will modify the state of the memory block.
  1716. */
  1717. static int virtio_mem_sbm_unplug_sb_online(struct virtio_mem *vm,
  1718. unsigned long mb_id, int sb_id,
  1719. int count)
  1720. {
  1721. const unsigned long nr_pages = PFN_DOWN(vm->sbm.sb_size) * count;
  1722. const int old_state = virtio_mem_sbm_get_mb_state(vm, mb_id);
  1723. unsigned long start_pfn;
  1724. int rc;
  1725. start_pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
  1726. sb_id * vm->sbm.sb_size);
  1727. rc = virtio_mem_fake_offline(vm, start_pfn, nr_pages);
  1728. if (rc)
  1729. return rc;
  1730. /* Try to unplug the allocated memory */
  1731. rc = virtio_mem_sbm_unplug_sb(vm, mb_id, sb_id, count);
  1732. if (rc) {
  1733. /* Return the memory to the buddy. */
  1734. virtio_mem_fake_online(start_pfn, nr_pages);
  1735. return rc;
  1736. }
  1737. switch (old_state) {
  1738. case VIRTIO_MEM_SBM_MB_KERNEL:
  1739. virtio_mem_sbm_set_mb_state(vm, mb_id,
  1740. VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL);
  1741. break;
  1742. case VIRTIO_MEM_SBM_MB_MOVABLE:
  1743. virtio_mem_sbm_set_mb_state(vm, mb_id,
  1744. VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL);
  1745. break;
  1746. }
  1747. return 0;
  1748. }
  1749. /*
  1750. * Unplug the desired number of plugged subblocks of an online memory block.
  1751. * Will skip subblock that are busy.
  1752. *
  1753. * Will modify the state of the memory block. Might temporarily drop the
  1754. * hotplug_mutex.
  1755. *
  1756. * Note: Can fail after some subblocks were successfully unplugged. Can
  1757. * return 0 even if subblocks were busy and could not get unplugged.
  1758. */
  1759. static int virtio_mem_sbm_unplug_any_sb_online(struct virtio_mem *vm,
  1760. unsigned long mb_id,
  1761. uint64_t *nb_sb)
  1762. {
  1763. int rc, sb_id;
  1764. /* If possible, try to unplug the complete block in one shot. */
  1765. if (*nb_sb >= vm->sbm.sbs_per_mb &&
  1766. virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) {
  1767. rc = virtio_mem_sbm_unplug_sb_online(vm, mb_id, 0,
  1768. vm->sbm.sbs_per_mb);
  1769. if (!rc) {
  1770. *nb_sb -= vm->sbm.sbs_per_mb;
  1771. goto unplugged;
  1772. } else if (rc != -EBUSY)
  1773. return rc;
  1774. }
  1775. /* Fallback to single subblocks. */
  1776. for (sb_id = vm->sbm.sbs_per_mb - 1; sb_id >= 0 && *nb_sb; sb_id--) {
  1777. /* Find the next candidate subblock */
  1778. while (sb_id >= 0 &&
  1779. !virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id, 1))
  1780. sb_id--;
  1781. if (sb_id < 0)
  1782. break;
  1783. rc = virtio_mem_sbm_unplug_sb_online(vm, mb_id, sb_id, 1);
  1784. if (rc == -EBUSY)
  1785. continue;
  1786. else if (rc)
  1787. return rc;
  1788. *nb_sb -= 1;
  1789. }
  1790. unplugged:
  1791. rc = virtio_mem_sbm_try_remove_unplugged_mb(vm, mb_id);
  1792. if (rc)
  1793. vm->sbm.have_unplugged_mb = 1;
  1794. /* Ignore errors, this is not critical. We'll retry later. */
  1795. return 0;
  1796. }
  1797. /*
  1798. * Unplug the desired number of plugged subblocks of a memory block that is
  1799. * already added to Linux. Will skip subblock of online memory blocks that are
  1800. * busy (by the OS). Will fail if any subblock that's not busy cannot get
  1801. * unplugged.
  1802. *
  1803. * Will modify the state of the memory block. Might temporarily drop the
  1804. * hotplug_mutex.
  1805. *
  1806. * Note: Can fail after some subblocks were successfully unplugged. Can
  1807. * return 0 even if subblocks were busy and could not get unplugged.
  1808. */
  1809. static int virtio_mem_sbm_unplug_any_sb(struct virtio_mem *vm,
  1810. unsigned long mb_id,
  1811. uint64_t *nb_sb)
  1812. {
  1813. const int old_state = virtio_mem_sbm_get_mb_state(vm, mb_id);
  1814. switch (old_state) {
  1815. case VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL:
  1816. case VIRTIO_MEM_SBM_MB_KERNEL:
  1817. case VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL:
  1818. case VIRTIO_MEM_SBM_MB_MOVABLE:
  1819. return virtio_mem_sbm_unplug_any_sb_online(vm, mb_id, nb_sb);
  1820. case VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL:
  1821. case VIRTIO_MEM_SBM_MB_OFFLINE:
  1822. return virtio_mem_sbm_unplug_any_sb_offline(vm, mb_id, nb_sb);
  1823. }
  1824. return -EINVAL;
  1825. }
  1826. static int virtio_mem_sbm_unplug_request(struct virtio_mem *vm, uint64_t diff)
  1827. {
  1828. const int mb_states[] = {
  1829. VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL,
  1830. VIRTIO_MEM_SBM_MB_OFFLINE,
  1831. VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL,
  1832. VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL,
  1833. VIRTIO_MEM_SBM_MB_MOVABLE,
  1834. VIRTIO_MEM_SBM_MB_KERNEL,
  1835. };
  1836. uint64_t nb_sb = diff / vm->sbm.sb_size;
  1837. unsigned long mb_id;
  1838. int rc, i;
  1839. if (!nb_sb)
  1840. return 0;
  1841. /*
  1842. * We'll drop the mutex a couple of times when it is safe to do so.
  1843. * This might result in some blocks switching the state (online/offline)
  1844. * and we could miss them in this run - we will retry again later.
  1845. */
  1846. mutex_lock(&vm->hotplug_mutex);
  1847. /*
  1848. * We try unplug from partially plugged blocks first, to try removing
  1849. * whole memory blocks along with metadata. We prioritize ZONE_MOVABLE
  1850. * as it's more reliable to unplug memory and remove whole memory
  1851. * blocks, and we don't want to trigger a zone imbalances by
  1852. * accidentially removing too much kernel memory.
  1853. */
  1854. for (i = 0; i < ARRAY_SIZE(mb_states); i++) {
  1855. virtio_mem_sbm_for_each_mb_rev(vm, mb_id, mb_states[i]) {
  1856. rc = virtio_mem_sbm_unplug_any_sb(vm, mb_id, &nb_sb);
  1857. if (rc || !nb_sb)
  1858. goto out_unlock;
  1859. mutex_unlock(&vm->hotplug_mutex);
  1860. cond_resched();
  1861. mutex_lock(&vm->hotplug_mutex);
  1862. }
  1863. if (!unplug_online && i == 1) {
  1864. mutex_unlock(&vm->hotplug_mutex);
  1865. return 0;
  1866. }
  1867. }
  1868. mutex_unlock(&vm->hotplug_mutex);
  1869. return nb_sb ? -EBUSY : 0;
  1870. out_unlock:
  1871. mutex_unlock(&vm->hotplug_mutex);
  1872. return rc;
  1873. }
  1874. /*
  1875. * Try to offline and remove a big block from Linux and unplug it. Will fail
  1876. * with -EBUSY if some memory is busy and cannot get unplugged.
  1877. *
  1878. * Will modify the state of the memory block. Might temporarily drop the
  1879. * hotplug_mutex.
  1880. */
  1881. static int virtio_mem_bbm_offline_remove_and_unplug_bb(struct virtio_mem *vm,
  1882. unsigned long bb_id)
  1883. {
  1884. const unsigned long start_pfn = PFN_DOWN(virtio_mem_bb_id_to_phys(vm, bb_id));
  1885. const unsigned long nr_pages = PFN_DOWN(vm->bbm.bb_size);
  1886. unsigned long end_pfn = start_pfn + nr_pages;
  1887. unsigned long pfn;
  1888. struct page *page;
  1889. int rc;
  1890. if (WARN_ON_ONCE(virtio_mem_bbm_get_bb_state(vm, bb_id) !=
  1891. VIRTIO_MEM_BBM_BB_ADDED))
  1892. return -EINVAL;
  1893. /*
  1894. * Start by fake-offlining all memory. Once we marked the device
  1895. * block as fake-offline, all newly onlined memory will
  1896. * automatically be kept fake-offline. Protect from concurrent
  1897. * onlining/offlining until we have a consistent state.
  1898. */
  1899. mutex_lock(&vm->hotplug_mutex);
  1900. virtio_mem_bbm_set_bb_state(vm, bb_id, VIRTIO_MEM_BBM_BB_FAKE_OFFLINE);
  1901. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1902. page = pfn_to_online_page(pfn);
  1903. if (!page)
  1904. continue;
  1905. rc = virtio_mem_fake_offline(vm, pfn, PAGES_PER_SECTION);
  1906. if (rc) {
  1907. end_pfn = pfn;
  1908. goto rollback;
  1909. }
  1910. }
  1911. mutex_unlock(&vm->hotplug_mutex);
  1912. rc = virtio_mem_bbm_offline_and_remove_bb(vm, bb_id);
  1913. if (rc) {
  1914. mutex_lock(&vm->hotplug_mutex);
  1915. goto rollback;
  1916. }
  1917. rc = virtio_mem_bbm_unplug_bb(vm, bb_id);
  1918. if (rc)
  1919. virtio_mem_bbm_set_bb_state(vm, bb_id,
  1920. VIRTIO_MEM_BBM_BB_PLUGGED);
  1921. else
  1922. virtio_mem_bbm_set_bb_state(vm, bb_id,
  1923. VIRTIO_MEM_BBM_BB_UNUSED);
  1924. return rc;
  1925. rollback:
  1926. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1927. page = pfn_to_online_page(pfn);
  1928. if (!page)
  1929. continue;
  1930. virtio_mem_fake_online(pfn, PAGES_PER_SECTION);
  1931. }
  1932. virtio_mem_bbm_set_bb_state(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED);
  1933. mutex_unlock(&vm->hotplug_mutex);
  1934. return rc;
  1935. }
  1936. /*
  1937. * Test if a big block is completely offline.
  1938. */
  1939. static bool virtio_mem_bbm_bb_is_offline(struct virtio_mem *vm,
  1940. unsigned long bb_id)
  1941. {
  1942. const unsigned long start_pfn = PFN_DOWN(virtio_mem_bb_id_to_phys(vm, bb_id));
  1943. const unsigned long nr_pages = PFN_DOWN(vm->bbm.bb_size);
  1944. unsigned long pfn;
  1945. for (pfn = start_pfn; pfn < start_pfn + nr_pages;
  1946. pfn += PAGES_PER_SECTION) {
  1947. if (pfn_to_online_page(pfn))
  1948. return false;
  1949. }
  1950. return true;
  1951. }
  1952. /*
  1953. * Test if a big block is completely onlined to ZONE_MOVABLE (or offline).
  1954. */
  1955. static bool virtio_mem_bbm_bb_is_movable(struct virtio_mem *vm,
  1956. unsigned long bb_id)
  1957. {
  1958. const unsigned long start_pfn = PFN_DOWN(virtio_mem_bb_id_to_phys(vm, bb_id));
  1959. const unsigned long nr_pages = PFN_DOWN(vm->bbm.bb_size);
  1960. struct page *page;
  1961. unsigned long pfn;
  1962. for (pfn = start_pfn; pfn < start_pfn + nr_pages;
  1963. pfn += PAGES_PER_SECTION) {
  1964. page = pfn_to_online_page(pfn);
  1965. if (!page)
  1966. continue;
  1967. if (page_zonenum(page) != ZONE_MOVABLE)
  1968. return false;
  1969. }
  1970. return true;
  1971. }
  1972. static int virtio_mem_bbm_unplug_request(struct virtio_mem *vm, uint64_t diff)
  1973. {
  1974. uint64_t nb_bb = diff / vm->bbm.bb_size;
  1975. uint64_t bb_id;
  1976. int rc, i;
  1977. if (!nb_bb)
  1978. return 0;
  1979. /*
  1980. * Try to unplug big blocks. Similar to SBM, start with offline
  1981. * big blocks.
  1982. */
  1983. for (i = 0; i < 3; i++) {
  1984. virtio_mem_bbm_for_each_bb_rev(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED) {
  1985. cond_resched();
  1986. /*
  1987. * As we're holding no locks, these checks are racy,
  1988. * but we don't care.
  1989. */
  1990. if (i == 0 && !virtio_mem_bbm_bb_is_offline(vm, bb_id))
  1991. continue;
  1992. if (i == 1 && !virtio_mem_bbm_bb_is_movable(vm, bb_id))
  1993. continue;
  1994. rc = virtio_mem_bbm_offline_remove_and_unplug_bb(vm, bb_id);
  1995. if (rc == -EBUSY)
  1996. continue;
  1997. if (!rc)
  1998. nb_bb--;
  1999. if (rc || !nb_bb)
  2000. return rc;
  2001. }
  2002. if (i == 0 && !unplug_online)
  2003. return 0;
  2004. }
  2005. return nb_bb ? -EBUSY : 0;
  2006. }
  2007. /*
  2008. * Try to unplug the requested amount of memory.
  2009. */
  2010. static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff)
  2011. {
  2012. if (vm->in_sbm)
  2013. return virtio_mem_sbm_unplug_request(vm, diff);
  2014. return virtio_mem_bbm_unplug_request(vm, diff);
  2015. }
  2016. /*
  2017. * Try to unplug all blocks that couldn't be unplugged before, for example,
  2018. * because the hypervisor was busy. Further, offline and remove any memory
  2019. * blocks where we previously failed.
  2020. */
  2021. static int virtio_mem_cleanup_pending_mb(struct virtio_mem *vm)
  2022. {
  2023. unsigned long id;
  2024. int rc = 0;
  2025. if (!vm->in_sbm) {
  2026. virtio_mem_bbm_for_each_bb(vm, id,
  2027. VIRTIO_MEM_BBM_BB_PLUGGED) {
  2028. rc = virtio_mem_bbm_unplug_bb(vm, id);
  2029. if (rc)
  2030. return rc;
  2031. virtio_mem_bbm_set_bb_state(vm, id,
  2032. VIRTIO_MEM_BBM_BB_UNUSED);
  2033. }
  2034. return 0;
  2035. }
  2036. virtio_mem_sbm_for_each_mb(vm, id, VIRTIO_MEM_SBM_MB_PLUGGED) {
  2037. rc = virtio_mem_sbm_unplug_mb(vm, id);
  2038. if (rc)
  2039. return rc;
  2040. virtio_mem_sbm_set_mb_state(vm, id,
  2041. VIRTIO_MEM_SBM_MB_UNUSED);
  2042. }
  2043. if (!vm->sbm.have_unplugged_mb)
  2044. return 0;
  2045. /*
  2046. * Let's retry (offlining and) removing completely unplugged Linux
  2047. * memory blocks.
  2048. */
  2049. vm->sbm.have_unplugged_mb = false;
  2050. mutex_lock(&vm->hotplug_mutex);
  2051. virtio_mem_sbm_for_each_mb(vm, id, VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL)
  2052. rc |= virtio_mem_sbm_try_remove_unplugged_mb(vm, id);
  2053. virtio_mem_sbm_for_each_mb(vm, id, VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL)
  2054. rc |= virtio_mem_sbm_try_remove_unplugged_mb(vm, id);
  2055. virtio_mem_sbm_for_each_mb(vm, id, VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL)
  2056. rc |= virtio_mem_sbm_try_remove_unplugged_mb(vm, id);
  2057. mutex_unlock(&vm->hotplug_mutex);
  2058. if (rc)
  2059. vm->sbm.have_unplugged_mb = true;
  2060. /* Ignore errors, this is not critical. We'll retry later. */
  2061. return 0;
  2062. }
  2063. /*
  2064. * Update all parts of the config that could have changed.
  2065. */
  2066. static void virtio_mem_refresh_config(struct virtio_mem *vm)
  2067. {
  2068. const struct range pluggable_range = mhp_get_pluggable_range(true);
  2069. uint64_t new_plugged_size, usable_region_size, end_addr;
  2070. /* the plugged_size is just a reflection of what _we_ did previously */
  2071. virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
  2072. &new_plugged_size);
  2073. if (WARN_ON_ONCE(new_plugged_size != vm->plugged_size))
  2074. vm->plugged_size = new_plugged_size;
  2075. /* calculate the last usable memory block id */
  2076. virtio_cread_le(vm->vdev, struct virtio_mem_config,
  2077. usable_region_size, &usable_region_size);
  2078. end_addr = min(vm->addr + usable_region_size - 1,
  2079. pluggable_range.end);
  2080. if (vm->in_sbm) {
  2081. vm->sbm.last_usable_mb_id = virtio_mem_phys_to_mb_id(end_addr);
  2082. if (!IS_ALIGNED(end_addr + 1, memory_block_size_bytes()))
  2083. vm->sbm.last_usable_mb_id--;
  2084. } else {
  2085. vm->bbm.last_usable_bb_id = virtio_mem_phys_to_bb_id(vm,
  2086. end_addr);
  2087. if (!IS_ALIGNED(end_addr + 1, vm->bbm.bb_size))
  2088. vm->bbm.last_usable_bb_id--;
  2089. }
  2090. /*
  2091. * If we cannot plug any of our device memory (e.g., nothing in the
  2092. * usable region is addressable), the last usable memory block id will
  2093. * be smaller than the first usable memory block id. We'll stop
  2094. * attempting to add memory with -ENOSPC from our main loop.
  2095. */
  2096. /* see if there is a request to change the size */
  2097. virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size,
  2098. &vm->requested_size);
  2099. dev_info(&vm->vdev->dev, "plugged size: 0x%llx", vm->plugged_size);
  2100. dev_info(&vm->vdev->dev, "requested size: 0x%llx", vm->requested_size);
  2101. }
  2102. /*
  2103. * Workqueue function for handling plug/unplug requests and config updates.
  2104. */
  2105. static void virtio_mem_run_wq(struct work_struct *work)
  2106. {
  2107. struct virtio_mem *vm = container_of(work, struct virtio_mem, wq);
  2108. uint64_t diff;
  2109. int rc;
  2110. if (unlikely(vm->in_kdump)) {
  2111. dev_warn_once(&vm->vdev->dev,
  2112. "unexpected workqueue run in kdump kernel\n");
  2113. return;
  2114. }
  2115. hrtimer_cancel(&vm->retry_timer);
  2116. if (vm->broken)
  2117. return;
  2118. atomic_set(&vm->wq_active, 1);
  2119. retry:
  2120. rc = 0;
  2121. /* Make sure we start with a clean state if there are leftovers. */
  2122. if (unlikely(vm->unplug_all_required))
  2123. rc = virtio_mem_send_unplug_all_request(vm);
  2124. if (atomic_read(&vm->config_changed)) {
  2125. atomic_set(&vm->config_changed, 0);
  2126. virtio_mem_refresh_config(vm);
  2127. }
  2128. /* Cleanup any leftovers from previous runs */
  2129. if (!rc)
  2130. rc = virtio_mem_cleanup_pending_mb(vm);
  2131. if (!rc && vm->requested_size != vm->plugged_size) {
  2132. if (vm->requested_size > vm->plugged_size) {
  2133. diff = vm->requested_size - vm->plugged_size;
  2134. rc = virtio_mem_plug_request(vm, diff);
  2135. } else {
  2136. diff = vm->plugged_size - vm->requested_size;
  2137. rc = virtio_mem_unplug_request(vm, diff);
  2138. }
  2139. }
  2140. /*
  2141. * Keep retrying to offline and remove completely unplugged Linux
  2142. * memory blocks.
  2143. */
  2144. if (!rc && vm->in_sbm && vm->sbm.have_unplugged_mb)
  2145. rc = -EBUSY;
  2146. switch (rc) {
  2147. case 0:
  2148. vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS;
  2149. break;
  2150. case -ENOSPC:
  2151. /*
  2152. * We cannot add any more memory (alignment, physical limit)
  2153. * or we have too many offline memory blocks.
  2154. */
  2155. break;
  2156. case -ETXTBSY:
  2157. /*
  2158. * The hypervisor cannot process our request right now
  2159. * (e.g., out of memory, migrating);
  2160. */
  2161. case -EBUSY:
  2162. /*
  2163. * We cannot free up any memory to unplug it (all plugged memory
  2164. * is busy).
  2165. */
  2166. case -ENOMEM:
  2167. /* Out of memory, try again later. */
  2168. hrtimer_start(&vm->retry_timer, ms_to_ktime(vm->retry_timer_ms),
  2169. HRTIMER_MODE_REL);
  2170. break;
  2171. case -EAGAIN:
  2172. /* Retry immediately (e.g., the config changed). */
  2173. goto retry;
  2174. default:
  2175. /* Unknown error, mark as broken */
  2176. dev_err(&vm->vdev->dev,
  2177. "unknown error, marking device broken: %d\n", rc);
  2178. vm->broken = true;
  2179. }
  2180. atomic_set(&vm->wq_active, 0);
  2181. }
  2182. static enum hrtimer_restart virtio_mem_timer_expired(struct hrtimer *timer)
  2183. {
  2184. struct virtio_mem *vm = container_of(timer, struct virtio_mem,
  2185. retry_timer);
  2186. virtio_mem_retry(vm);
  2187. vm->retry_timer_ms = min_t(unsigned int, vm->retry_timer_ms * 2,
  2188. VIRTIO_MEM_RETRY_TIMER_MAX_MS);
  2189. return HRTIMER_NORESTART;
  2190. }
  2191. static void virtio_mem_handle_response(struct virtqueue *vq)
  2192. {
  2193. struct virtio_mem *vm = vq->vdev->priv;
  2194. wake_up(&vm->host_resp);
  2195. }
  2196. static int virtio_mem_init_vq(struct virtio_mem *vm)
  2197. {
  2198. struct virtqueue *vq;
  2199. vq = virtio_find_single_vq(vm->vdev, virtio_mem_handle_response,
  2200. "guest-request");
  2201. if (IS_ERR(vq))
  2202. return PTR_ERR(vq);
  2203. vm->vq = vq;
  2204. return 0;
  2205. }
  2206. static int virtio_mem_init_hotplug(struct virtio_mem *vm)
  2207. {
  2208. const struct range pluggable_range = mhp_get_pluggable_range(true);
  2209. uint64_t unit_pages, sb_size, addr;
  2210. int rc;
  2211. /* bad device setup - warn only */
  2212. if (!IS_ALIGNED(vm->addr, memory_block_size_bytes()))
  2213. dev_warn(&vm->vdev->dev,
  2214. "The alignment of the physical start address can make some memory unusable.\n");
  2215. if (!IS_ALIGNED(vm->addr + vm->region_size, memory_block_size_bytes()))
  2216. dev_warn(&vm->vdev->dev,
  2217. "The alignment of the physical end address can make some memory unusable.\n");
  2218. if (vm->addr < pluggable_range.start ||
  2219. vm->addr + vm->region_size - 1 > pluggable_range.end)
  2220. dev_warn(&vm->vdev->dev,
  2221. "Some device memory is not addressable/pluggable. This can make some memory unusable.\n");
  2222. /* Prepare the offline threshold - make sure we can add two blocks. */
  2223. vm->offline_threshold = max_t(uint64_t, 2 * memory_block_size_bytes(),
  2224. VIRTIO_MEM_DEFAULT_OFFLINE_THRESHOLD);
  2225. /*
  2226. * alloc_contig_range() works reliably with pageblock
  2227. * granularity on ZONE_NORMAL, use pageblock_nr_pages.
  2228. */
  2229. sb_size = PAGE_SIZE * pageblock_nr_pages;
  2230. sb_size = max_t(uint64_t, vm->device_block_size, sb_size);
  2231. if (sb_size < memory_block_size_bytes() && !force_bbm) {
  2232. /* SBM: At least two subblocks per Linux memory block. */
  2233. vm->in_sbm = true;
  2234. vm->sbm.sb_size = sb_size;
  2235. vm->sbm.sbs_per_mb = memory_block_size_bytes() /
  2236. vm->sbm.sb_size;
  2237. /* Round up to the next full memory block */
  2238. addr = max_t(uint64_t, vm->addr, pluggable_range.start) +
  2239. memory_block_size_bytes() - 1;
  2240. vm->sbm.first_mb_id = virtio_mem_phys_to_mb_id(addr);
  2241. vm->sbm.next_mb_id = vm->sbm.first_mb_id;
  2242. } else {
  2243. /* BBM: At least one Linux memory block. */
  2244. vm->bbm.bb_size = max_t(uint64_t, vm->device_block_size,
  2245. memory_block_size_bytes());
  2246. if (bbm_block_size) {
  2247. if (!is_power_of_2(bbm_block_size)) {
  2248. dev_warn(&vm->vdev->dev,
  2249. "bbm_block_size is not a power of 2");
  2250. } else if (bbm_block_size < vm->bbm.bb_size) {
  2251. dev_warn(&vm->vdev->dev,
  2252. "bbm_block_size is too small");
  2253. } else {
  2254. vm->bbm.bb_size = bbm_block_size;
  2255. }
  2256. }
  2257. /* Round up to the next aligned big block */
  2258. addr = max_t(uint64_t, vm->addr, pluggable_range.start) +
  2259. vm->bbm.bb_size - 1;
  2260. vm->bbm.first_bb_id = virtio_mem_phys_to_bb_id(vm, addr);
  2261. vm->bbm.next_bb_id = vm->bbm.first_bb_id;
  2262. /* Make sure we can add two big blocks. */
  2263. vm->offline_threshold = max_t(uint64_t, 2 * vm->bbm.bb_size,
  2264. vm->offline_threshold);
  2265. }
  2266. dev_info(&vm->vdev->dev, "memory block size: 0x%lx",
  2267. memory_block_size_bytes());
  2268. if (vm->in_sbm)
  2269. dev_info(&vm->vdev->dev, "subblock size: 0x%llx",
  2270. (unsigned long long)vm->sbm.sb_size);
  2271. else
  2272. dev_info(&vm->vdev->dev, "big block size: 0x%llx",
  2273. (unsigned long long)vm->bbm.bb_size);
  2274. /* create the parent resource for all memory */
  2275. rc = virtio_mem_create_resource(vm);
  2276. if (rc)
  2277. return rc;
  2278. /* use a single dynamic memory group to cover the whole memory device */
  2279. if (vm->in_sbm)
  2280. unit_pages = PHYS_PFN(memory_block_size_bytes());
  2281. else
  2282. unit_pages = PHYS_PFN(vm->bbm.bb_size);
  2283. rc = memory_group_register_dynamic(vm->nid, unit_pages);
  2284. if (rc < 0)
  2285. goto out_del_resource;
  2286. vm->mgid = rc;
  2287. /*
  2288. * If we still have memory plugged, we have to unplug all memory first.
  2289. * Registering our parent resource makes sure that this memory isn't
  2290. * actually in use (e.g., trying to reload the driver).
  2291. */
  2292. if (vm->plugged_size) {
  2293. vm->unplug_all_required = true;
  2294. dev_info(&vm->vdev->dev, "unplugging all memory is required\n");
  2295. }
  2296. /* register callbacks */
  2297. vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb;
  2298. rc = register_memory_notifier(&vm->memory_notifier);
  2299. if (rc)
  2300. goto out_unreg_group;
  2301. /* Block hibernation as early as possible. */
  2302. vm->pm_notifier.priority = INT_MAX;
  2303. vm->pm_notifier.notifier_call = virtio_mem_pm_notifier_cb;
  2304. rc = register_pm_notifier(&vm->pm_notifier);
  2305. if (rc)
  2306. goto out_unreg_mem;
  2307. rc = register_virtio_mem_device(vm);
  2308. if (rc)
  2309. goto out_unreg_pm;
  2310. return 0;
  2311. out_unreg_pm:
  2312. unregister_pm_notifier(&vm->pm_notifier);
  2313. out_unreg_mem:
  2314. unregister_memory_notifier(&vm->memory_notifier);
  2315. out_unreg_group:
  2316. memory_group_unregister(vm->mgid);
  2317. out_del_resource:
  2318. virtio_mem_delete_resource(vm);
  2319. return rc;
  2320. }
  2321. #ifdef CONFIG_PROC_VMCORE
  2322. static int virtio_mem_send_state_request(struct virtio_mem *vm, uint64_t addr,
  2323. uint64_t size)
  2324. {
  2325. const uint64_t nb_vm_blocks = size / vm->device_block_size;
  2326. const struct virtio_mem_req req = {
  2327. .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_STATE),
  2328. .u.state.addr = cpu_to_virtio64(vm->vdev, addr),
  2329. .u.state.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
  2330. };
  2331. int rc = -ENOMEM;
  2332. dev_dbg(&vm->vdev->dev, "requesting state: 0x%llx - 0x%llx\n", addr,
  2333. addr + size - 1);
  2334. switch (virtio_mem_send_request(vm, &req)) {
  2335. case VIRTIO_MEM_RESP_ACK:
  2336. return virtio16_to_cpu(vm->vdev, vm->resp.u.state.state);
  2337. case VIRTIO_MEM_RESP_ERROR:
  2338. rc = -EINVAL;
  2339. break;
  2340. default:
  2341. break;
  2342. }
  2343. dev_dbg(&vm->vdev->dev, "requesting state failed: %d\n", rc);
  2344. return rc;
  2345. }
  2346. static bool virtio_mem_vmcore_pfn_is_ram(struct vmcore_cb *cb,
  2347. unsigned long pfn)
  2348. {
  2349. struct virtio_mem *vm = container_of(cb, struct virtio_mem,
  2350. vmcore_cb);
  2351. uint64_t addr = PFN_PHYS(pfn);
  2352. bool is_ram;
  2353. int rc;
  2354. if (!virtio_mem_contains_range(vm, addr, PAGE_SIZE))
  2355. return true;
  2356. if (!vm->plugged_size)
  2357. return false;
  2358. /*
  2359. * We have to serialize device requests and access to the information
  2360. * about the block queried last.
  2361. */
  2362. mutex_lock(&vm->hotplug_mutex);
  2363. addr = ALIGN_DOWN(addr, vm->device_block_size);
  2364. if (addr != vm->last_block_addr) {
  2365. rc = virtio_mem_send_state_request(vm, addr,
  2366. vm->device_block_size);
  2367. /* On any kind of error, we're going to signal !ram. */
  2368. if (rc == VIRTIO_MEM_STATE_PLUGGED)
  2369. vm->last_block_plugged = true;
  2370. else
  2371. vm->last_block_plugged = false;
  2372. vm->last_block_addr = addr;
  2373. }
  2374. is_ram = vm->last_block_plugged;
  2375. mutex_unlock(&vm->hotplug_mutex);
  2376. return is_ram;
  2377. }
  2378. #endif /* CONFIG_PROC_VMCORE */
  2379. static int virtio_mem_init_kdump(struct virtio_mem *vm)
  2380. {
  2381. #ifdef CONFIG_PROC_VMCORE
  2382. dev_info(&vm->vdev->dev, "memory hot(un)plug disabled in kdump kernel\n");
  2383. vm->vmcore_cb.pfn_is_ram = virtio_mem_vmcore_pfn_is_ram;
  2384. register_vmcore_cb(&vm->vmcore_cb);
  2385. return 0;
  2386. #else /* CONFIG_PROC_VMCORE */
  2387. dev_warn(&vm->vdev->dev, "disabled in kdump kernel without vmcore\n");
  2388. return -EBUSY;
  2389. #endif /* CONFIG_PROC_VMCORE */
  2390. }
  2391. static int virtio_mem_init(struct virtio_mem *vm)
  2392. {
  2393. uint16_t node_id;
  2394. if (!vm->vdev->config->get) {
  2395. dev_err(&vm->vdev->dev, "config access disabled\n");
  2396. return -EINVAL;
  2397. }
  2398. /* Fetch all properties that can't change. */
  2399. virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
  2400. &vm->plugged_size);
  2401. virtio_cread_le(vm->vdev, struct virtio_mem_config, block_size,
  2402. &vm->device_block_size);
  2403. virtio_cread_le(vm->vdev, struct virtio_mem_config, node_id,
  2404. &node_id);
  2405. vm->nid = virtio_mem_translate_node_id(vm, node_id);
  2406. virtio_cread_le(vm->vdev, struct virtio_mem_config, addr, &vm->addr);
  2407. virtio_cread_le(vm->vdev, struct virtio_mem_config, region_size,
  2408. &vm->region_size);
  2409. /* Determine the nid for the device based on the lowest address. */
  2410. if (vm->nid == NUMA_NO_NODE)
  2411. vm->nid = memory_add_physaddr_to_nid(vm->addr);
  2412. dev_info(&vm->vdev->dev, "start address: 0x%llx", vm->addr);
  2413. dev_info(&vm->vdev->dev, "region size: 0x%llx", vm->region_size);
  2414. dev_info(&vm->vdev->dev, "device block size: 0x%llx",
  2415. (unsigned long long)vm->device_block_size);
  2416. if (vm->nid != NUMA_NO_NODE && IS_ENABLED(CONFIG_NUMA))
  2417. dev_info(&vm->vdev->dev, "nid: %d", vm->nid);
  2418. /*
  2419. * We don't want to (un)plug or reuse any memory when in kdump. The
  2420. * memory is still accessible (but not exposed to Linux).
  2421. */
  2422. if (vm->in_kdump)
  2423. return virtio_mem_init_kdump(vm);
  2424. return virtio_mem_init_hotplug(vm);
  2425. }
  2426. static int virtio_mem_create_resource(struct virtio_mem *vm)
  2427. {
  2428. /*
  2429. * When force-unloading the driver and removing the device, we
  2430. * could have a garbage pointer. Duplicate the string.
  2431. */
  2432. const char *name = kstrdup(dev_name(&vm->vdev->dev), GFP_KERNEL);
  2433. if (!name)
  2434. return -ENOMEM;
  2435. /* Disallow mapping device memory via /dev/mem completely. */
  2436. vm->parent_resource = __request_mem_region(vm->addr, vm->region_size,
  2437. name, IORESOURCE_SYSTEM_RAM |
  2438. IORESOURCE_EXCLUSIVE);
  2439. if (!vm->parent_resource) {
  2440. kfree(name);
  2441. dev_warn(&vm->vdev->dev, "could not reserve device region\n");
  2442. dev_info(&vm->vdev->dev,
  2443. "reloading the driver is not supported\n");
  2444. return -EBUSY;
  2445. }
  2446. /* The memory is not actually busy - make add_memory() work. */
  2447. vm->parent_resource->flags &= ~IORESOURCE_BUSY;
  2448. return 0;
  2449. }
  2450. static void virtio_mem_delete_resource(struct virtio_mem *vm)
  2451. {
  2452. const char *name;
  2453. if (!vm->parent_resource)
  2454. return;
  2455. name = vm->parent_resource->name;
  2456. release_resource(vm->parent_resource);
  2457. kfree(vm->parent_resource);
  2458. kfree(name);
  2459. vm->parent_resource = NULL;
  2460. }
  2461. static int virtio_mem_range_has_system_ram(struct resource *res, void *arg)
  2462. {
  2463. return 1;
  2464. }
  2465. static bool virtio_mem_has_memory_added(struct virtio_mem *vm)
  2466. {
  2467. const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  2468. return walk_iomem_res_desc(IORES_DESC_NONE, flags, vm->addr,
  2469. vm->addr + vm->region_size, NULL,
  2470. virtio_mem_range_has_system_ram) == 1;
  2471. }
  2472. static int virtio_mem_probe(struct virtio_device *vdev)
  2473. {
  2474. struct virtio_mem *vm;
  2475. int rc;
  2476. BUILD_BUG_ON(sizeof(struct virtio_mem_req) != 24);
  2477. BUILD_BUG_ON(sizeof(struct virtio_mem_resp) != 10);
  2478. vdev->priv = vm = kzalloc(sizeof(*vm), GFP_KERNEL);
  2479. if (!vm)
  2480. return -ENOMEM;
  2481. init_waitqueue_head(&vm->host_resp);
  2482. vm->vdev = vdev;
  2483. INIT_WORK(&vm->wq, virtio_mem_run_wq);
  2484. mutex_init(&vm->hotplug_mutex);
  2485. INIT_LIST_HEAD(&vm->next);
  2486. spin_lock_init(&vm->removal_lock);
  2487. hrtimer_init(&vm->retry_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2488. vm->retry_timer.function = virtio_mem_timer_expired;
  2489. vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS;
  2490. vm->in_kdump = is_kdump_kernel();
  2491. /* register the virtqueue */
  2492. rc = virtio_mem_init_vq(vm);
  2493. if (rc)
  2494. goto out_free_vm;
  2495. /* initialize the device by querying the config */
  2496. rc = virtio_mem_init(vm);
  2497. if (rc)
  2498. goto out_del_vq;
  2499. virtio_device_ready(vdev);
  2500. /* trigger a config update to start processing the requested_size */
  2501. if (!vm->in_kdump) {
  2502. atomic_set(&vm->config_changed, 1);
  2503. queue_work(system_freezable_wq, &vm->wq);
  2504. }
  2505. return 0;
  2506. out_del_vq:
  2507. vdev->config->del_vqs(vdev);
  2508. out_free_vm:
  2509. kfree(vm);
  2510. vdev->priv = NULL;
  2511. return rc;
  2512. }
  2513. static void virtio_mem_deinit_hotplug(struct virtio_mem *vm)
  2514. {
  2515. unsigned long mb_id;
  2516. int rc;
  2517. /*
  2518. * Make sure the workqueue won't be triggered anymore and no memory
  2519. * blocks can be onlined/offlined until we're finished here.
  2520. */
  2521. mutex_lock(&vm->hotplug_mutex);
  2522. spin_lock_irq(&vm->removal_lock);
  2523. vm->removing = true;
  2524. spin_unlock_irq(&vm->removal_lock);
  2525. mutex_unlock(&vm->hotplug_mutex);
  2526. /* wait until the workqueue stopped */
  2527. cancel_work_sync(&vm->wq);
  2528. hrtimer_cancel(&vm->retry_timer);
  2529. if (vm->in_sbm) {
  2530. /*
  2531. * After we unregistered our callbacks, user space can online
  2532. * partially plugged offline blocks. Make sure to remove them.
  2533. */
  2534. virtio_mem_sbm_for_each_mb(vm, mb_id,
  2535. VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL) {
  2536. rc = virtio_mem_sbm_remove_mb(vm, mb_id);
  2537. BUG_ON(rc);
  2538. virtio_mem_sbm_set_mb_state(vm, mb_id,
  2539. VIRTIO_MEM_SBM_MB_UNUSED);
  2540. }
  2541. /*
  2542. * After we unregistered our callbacks, user space can no longer
  2543. * offline partially plugged online memory blocks. No need to
  2544. * worry about them.
  2545. */
  2546. }
  2547. /* unregister callbacks */
  2548. unregister_virtio_mem_device(vm);
  2549. unregister_pm_notifier(&vm->pm_notifier);
  2550. unregister_memory_notifier(&vm->memory_notifier);
  2551. /*
  2552. * There is no way we could reliably remove all memory we have added to
  2553. * the system. And there is no way to stop the driver/device from going
  2554. * away. Warn at least.
  2555. */
  2556. if (virtio_mem_has_memory_added(vm)) {
  2557. dev_warn(&vm->vdev->dev,
  2558. "device still has system memory added\n");
  2559. } else {
  2560. virtio_mem_delete_resource(vm);
  2561. kfree_const(vm->resource_name);
  2562. memory_group_unregister(vm->mgid);
  2563. }
  2564. /* remove all tracking data - no locking needed */
  2565. if (vm->in_sbm) {
  2566. vfree(vm->sbm.mb_states);
  2567. vfree(vm->sbm.sb_states);
  2568. } else {
  2569. vfree(vm->bbm.bb_states);
  2570. }
  2571. }
  2572. static void virtio_mem_deinit_kdump(struct virtio_mem *vm)
  2573. {
  2574. #ifdef CONFIG_PROC_VMCORE
  2575. unregister_vmcore_cb(&vm->vmcore_cb);
  2576. #endif /* CONFIG_PROC_VMCORE */
  2577. }
  2578. static void virtio_mem_remove(struct virtio_device *vdev)
  2579. {
  2580. struct virtio_mem *vm = vdev->priv;
  2581. if (vm->in_kdump)
  2582. virtio_mem_deinit_kdump(vm);
  2583. else
  2584. virtio_mem_deinit_hotplug(vm);
  2585. /* reset the device and cleanup the queues */
  2586. virtio_reset_device(vdev);
  2587. vdev->config->del_vqs(vdev);
  2588. kfree(vm);
  2589. vdev->priv = NULL;
  2590. }
  2591. static void virtio_mem_config_changed(struct virtio_device *vdev)
  2592. {
  2593. struct virtio_mem *vm = vdev->priv;
  2594. if (unlikely(vm->in_kdump))
  2595. return;
  2596. atomic_set(&vm->config_changed, 1);
  2597. virtio_mem_retry(vm);
  2598. }
  2599. #ifdef CONFIG_PM_SLEEP
  2600. static int virtio_mem_freeze(struct virtio_device *vdev)
  2601. {
  2602. struct virtio_mem *vm = vdev->priv;
  2603. /*
  2604. * We block hibernation using the PM notifier completely. The workqueue
  2605. * is already frozen by the PM core at this point, so we simply
  2606. * reset the device and cleanup the queues.
  2607. */
  2608. if (pm_suspend_target_state != PM_SUSPEND_TO_IDLE &&
  2609. vm->plugged_size &&
  2610. !virtio_has_feature(vm->vdev, VIRTIO_MEM_F_PERSISTENT_SUSPEND)) {
  2611. dev_err(&vm->vdev->dev,
  2612. "suspending with plugged memory is not supported\n");
  2613. return -EPERM;
  2614. }
  2615. virtio_reset_device(vdev);
  2616. vdev->config->del_vqs(vdev);
  2617. vm->vq = NULL;
  2618. return 0;
  2619. }
  2620. static int virtio_mem_restore(struct virtio_device *vdev)
  2621. {
  2622. struct virtio_mem *vm = vdev->priv;
  2623. int ret;
  2624. ret = virtio_mem_init_vq(vm);
  2625. if (ret)
  2626. return ret;
  2627. virtio_device_ready(vdev);
  2628. /* Let's check if anything changed. */
  2629. virtio_mem_config_changed(vdev);
  2630. return 0;
  2631. }
  2632. #endif
  2633. static unsigned int virtio_mem_features[] = {
  2634. #if defined(CONFIG_NUMA) && defined(CONFIG_ACPI_NUMA)
  2635. VIRTIO_MEM_F_ACPI_PXM,
  2636. #endif
  2637. VIRTIO_MEM_F_UNPLUGGED_INACCESSIBLE,
  2638. VIRTIO_MEM_F_PERSISTENT_SUSPEND,
  2639. };
  2640. static const struct virtio_device_id virtio_mem_id_table[] = {
  2641. { VIRTIO_ID_MEM, VIRTIO_DEV_ANY_ID },
  2642. { 0 },
  2643. };
  2644. static struct virtio_driver virtio_mem_driver = {
  2645. .feature_table = virtio_mem_features,
  2646. .feature_table_size = ARRAY_SIZE(virtio_mem_features),
  2647. .driver.name = KBUILD_MODNAME,
  2648. .id_table = virtio_mem_id_table,
  2649. .probe = virtio_mem_probe,
  2650. .remove = virtio_mem_remove,
  2651. .config_changed = virtio_mem_config_changed,
  2652. #ifdef CONFIG_PM_SLEEP
  2653. .freeze = virtio_mem_freeze,
  2654. .restore = virtio_mem_restore,
  2655. #endif
  2656. };
  2657. module_virtio_driver(virtio_mem_driver);
  2658. MODULE_DEVICE_TABLE(virtio, virtio_mem_id_table);
  2659. MODULE_AUTHOR("David Hildenbrand <david@redhat.com>");
  2660. MODULE_DESCRIPTION("Virtio-mem driver");
  2661. MODULE_LICENSE("GPL");