virtio_mem.c 84 KB

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