virtio_mem.c 85 KB

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