vmci_queue_pair.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361
  1. /*
  2. * VMware VMCI Driver
  3. *
  4. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation version 2 and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <linux/vmw_vmci_defs.h>
  16. #include <linux/vmw_vmci_api.h>
  17. #include <linux/highmem.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/pci.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/uio.h>
  27. #include <linux/wait.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/skbuff.h>
  30. #include "vmci_handle_array.h"
  31. #include "vmci_queue_pair.h"
  32. #include "vmci_datagram.h"
  33. #include "vmci_resource.h"
  34. #include "vmci_context.h"
  35. #include "vmci_driver.h"
  36. #include "vmci_event.h"
  37. #include "vmci_route.h"
  38. /*
  39. * In the following, we will distinguish between two kinds of VMX processes -
  40. * the ones with versions lower than VMCI_VERSION_NOVMVM that use specialized
  41. * VMCI page files in the VMX and supporting VM to VM communication and the
  42. * newer ones that use the guest memory directly. We will in the following
  43. * refer to the older VMX versions as old-style VMX'en, and the newer ones as
  44. * new-style VMX'en.
  45. *
  46. * The state transition datagram is as follows (the VMCIQPB_ prefix has been
  47. * removed for readability) - see below for more details on the transtions:
  48. *
  49. * -------------- NEW -------------
  50. * | |
  51. * \_/ \_/
  52. * CREATED_NO_MEM <-----------------> CREATED_MEM
  53. * | | |
  54. * | o-----------------------o |
  55. * | | |
  56. * \_/ \_/ \_/
  57. * ATTACHED_NO_MEM <----------------> ATTACHED_MEM
  58. * | | |
  59. * | o----------------------o |
  60. * | | |
  61. * \_/ \_/ \_/
  62. * SHUTDOWN_NO_MEM <----------------> SHUTDOWN_MEM
  63. * | |
  64. * | |
  65. * -------------> gone <-------------
  66. *
  67. * In more detail. When a VMCI queue pair is first created, it will be in the
  68. * VMCIQPB_NEW state. It will then move into one of the following states:
  69. *
  70. * - VMCIQPB_CREATED_NO_MEM: this state indicates that either:
  71. *
  72. * - the created was performed by a host endpoint, in which case there is
  73. * no backing memory yet.
  74. *
  75. * - the create was initiated by an old-style VMX, that uses
  76. * vmci_qp_broker_set_page_store to specify the UVAs of the queue pair at
  77. * a later point in time. This state can be distinguished from the one
  78. * above by the context ID of the creator. A host side is not allowed to
  79. * attach until the page store has been set.
  80. *
  81. * - VMCIQPB_CREATED_MEM: this state is the result when the queue pair
  82. * is created by a VMX using the queue pair device backend that
  83. * sets the UVAs of the queue pair immediately and stores the
  84. * information for later attachers. At this point, it is ready for
  85. * the host side to attach to it.
  86. *
  87. * Once the queue pair is in one of the created states (with the exception of
  88. * the case mentioned for older VMX'en above), it is possible to attach to the
  89. * queue pair. Again we have two new states possible:
  90. *
  91. * - VMCIQPB_ATTACHED_MEM: this state can be reached through the following
  92. * paths:
  93. *
  94. * - from VMCIQPB_CREATED_NO_MEM when a new-style VMX allocates a queue
  95. * pair, and attaches to a queue pair previously created by the host side.
  96. *
  97. * - from VMCIQPB_CREATED_MEM when the host side attaches to a queue pair
  98. * already created by a guest.
  99. *
  100. * - from VMCIQPB_ATTACHED_NO_MEM, when an old-style VMX calls
  101. * vmci_qp_broker_set_page_store (see below).
  102. *
  103. * - VMCIQPB_ATTACHED_NO_MEM: If the queue pair already was in the
  104. * VMCIQPB_CREATED_NO_MEM due to a host side create, an old-style VMX will
  105. * bring the queue pair into this state. Once vmci_qp_broker_set_page_store
  106. * is called to register the user memory, the VMCIQPB_ATTACH_MEM state
  107. * will be entered.
  108. *
  109. * From the attached queue pair, the queue pair can enter the shutdown states
  110. * when either side of the queue pair detaches. If the guest side detaches
  111. * first, the queue pair will enter the VMCIQPB_SHUTDOWN_NO_MEM state, where
  112. * the content of the queue pair will no longer be available. If the host
  113. * side detaches first, the queue pair will either enter the
  114. * VMCIQPB_SHUTDOWN_MEM, if the guest memory is currently mapped, or
  115. * VMCIQPB_SHUTDOWN_NO_MEM, if the guest memory is not mapped
  116. * (e.g., the host detaches while a guest is stunned).
  117. *
  118. * New-style VMX'en will also unmap guest memory, if the guest is
  119. * quiesced, e.g., during a snapshot operation. In that case, the guest
  120. * memory will no longer be available, and the queue pair will transition from
  121. * *_MEM state to a *_NO_MEM state. The VMX may later map the memory once more,
  122. * in which case the queue pair will transition from the *_NO_MEM state at that
  123. * point back to the *_MEM state. Note that the *_NO_MEM state may have changed,
  124. * since the peer may have either attached or detached in the meantime. The
  125. * values are laid out such that ++ on a state will move from a *_NO_MEM to a
  126. * *_MEM state, and vice versa.
  127. */
  128. /*
  129. * VMCIMemcpy{To,From}QueueFunc() prototypes. Functions of these
  130. * types are passed around to enqueue and dequeue routines. Note that
  131. * often the functions passed are simply wrappers around memcpy
  132. * itself.
  133. *
  134. * Note: In order for the memcpy typedefs to be compatible with the VMKernel,
  135. * there's an unused last parameter for the hosted side. In
  136. * ESX, that parameter holds a buffer type.
  137. */
  138. typedef int vmci_memcpy_to_queue_func(struct vmci_queue *queue,
  139. u64 queue_offset, const void *src,
  140. size_t src_offset, size_t size);
  141. typedef int vmci_memcpy_from_queue_func(void *dest, size_t dest_offset,
  142. const struct vmci_queue *queue,
  143. u64 queue_offset, size_t size);
  144. /* The Kernel specific component of the struct vmci_queue structure. */
  145. struct vmci_queue_kern_if {
  146. struct mutex __mutex; /* Protects the queue. */
  147. struct mutex *mutex; /* Shared by producer and consumer queues. */
  148. size_t num_pages; /* Number of pages incl. header. */
  149. bool host; /* Host or guest? */
  150. union {
  151. struct {
  152. dma_addr_t *pas;
  153. void **vas;
  154. } g; /* Used by the guest. */
  155. struct {
  156. struct page **page;
  157. struct page **header_page;
  158. } h; /* Used by the host. */
  159. } u;
  160. };
  161. /*
  162. * This structure is opaque to the clients.
  163. */
  164. struct vmci_qp {
  165. struct vmci_handle handle;
  166. struct vmci_queue *produce_q;
  167. struct vmci_queue *consume_q;
  168. u64 produce_q_size;
  169. u64 consume_q_size;
  170. u32 peer;
  171. u32 flags;
  172. u32 priv_flags;
  173. bool guest_endpoint;
  174. unsigned int blocked;
  175. unsigned int generation;
  176. wait_queue_head_t event;
  177. };
  178. enum qp_broker_state {
  179. VMCIQPB_NEW,
  180. VMCIQPB_CREATED_NO_MEM,
  181. VMCIQPB_CREATED_MEM,
  182. VMCIQPB_ATTACHED_NO_MEM,
  183. VMCIQPB_ATTACHED_MEM,
  184. VMCIQPB_SHUTDOWN_NO_MEM,
  185. VMCIQPB_SHUTDOWN_MEM,
  186. VMCIQPB_GONE
  187. };
  188. #define QPBROKERSTATE_HAS_MEM(_qpb) (_qpb->state == VMCIQPB_CREATED_MEM || \
  189. _qpb->state == VMCIQPB_ATTACHED_MEM || \
  190. _qpb->state == VMCIQPB_SHUTDOWN_MEM)
  191. /*
  192. * In the queue pair broker, we always use the guest point of view for
  193. * the produce and consume queue values and references, e.g., the
  194. * produce queue size stored is the guests produce queue size. The
  195. * host endpoint will need to swap these around. The only exception is
  196. * the local queue pairs on the host, in which case the host endpoint
  197. * that creates the queue pair will have the right orientation, and
  198. * the attaching host endpoint will need to swap.
  199. */
  200. struct qp_entry {
  201. struct list_head list_item;
  202. struct vmci_handle handle;
  203. u32 peer;
  204. u32 flags;
  205. u64 produce_size;
  206. u64 consume_size;
  207. u32 ref_count;
  208. };
  209. struct qp_broker_entry {
  210. struct vmci_resource resource;
  211. struct qp_entry qp;
  212. u32 create_id;
  213. u32 attach_id;
  214. enum qp_broker_state state;
  215. bool require_trusted_attach;
  216. bool created_by_trusted;
  217. bool vmci_page_files; /* Created by VMX using VMCI page files */
  218. struct vmci_queue *produce_q;
  219. struct vmci_queue *consume_q;
  220. struct vmci_queue_header saved_produce_q;
  221. struct vmci_queue_header saved_consume_q;
  222. vmci_event_release_cb wakeup_cb;
  223. void *client_data;
  224. void *local_mem; /* Kernel memory for local queue pair */
  225. };
  226. struct qp_guest_endpoint {
  227. struct vmci_resource resource;
  228. struct qp_entry qp;
  229. u64 num_ppns;
  230. void *produce_q;
  231. void *consume_q;
  232. struct ppn_set ppn_set;
  233. };
  234. struct qp_list {
  235. struct list_head head;
  236. struct mutex mutex; /* Protect queue list. */
  237. };
  238. static struct qp_list qp_broker_list = {
  239. .head = LIST_HEAD_INIT(qp_broker_list.head),
  240. .mutex = __MUTEX_INITIALIZER(qp_broker_list.mutex),
  241. };
  242. static struct qp_list qp_guest_endpoints = {
  243. .head = LIST_HEAD_INIT(qp_guest_endpoints.head),
  244. .mutex = __MUTEX_INITIALIZER(qp_guest_endpoints.mutex),
  245. };
  246. #define INVALID_VMCI_GUEST_MEM_ID 0
  247. #define QPE_NUM_PAGES(_QPE) ((u32) \
  248. (DIV_ROUND_UP(_QPE.produce_size, PAGE_SIZE) + \
  249. DIV_ROUND_UP(_QPE.consume_size, PAGE_SIZE) + 2))
  250. /*
  251. * Frees kernel VA space for a given queue and its queue header, and
  252. * frees physical data pages.
  253. */
  254. static void qp_free_queue(void *q, u64 size)
  255. {
  256. struct vmci_queue *queue = q;
  257. if (queue) {
  258. u64 i;
  259. /* Given size does not include header, so add in a page here. */
  260. for (i = 0; i < DIV_ROUND_UP(size, PAGE_SIZE) + 1; i++) {
  261. dma_free_coherent(&vmci_pdev->dev, PAGE_SIZE,
  262. queue->kernel_if->u.g.vas[i],
  263. queue->kernel_if->u.g.pas[i]);
  264. }
  265. vfree(queue);
  266. }
  267. }
  268. /*
  269. * Allocates kernel queue pages of specified size with IOMMU mappings,
  270. * plus space for the queue structure/kernel interface and the queue
  271. * header.
  272. */
  273. static void *qp_alloc_queue(u64 size, u32 flags)
  274. {
  275. u64 i;
  276. struct vmci_queue *queue;
  277. size_t pas_size;
  278. size_t vas_size;
  279. size_t queue_size = sizeof(*queue) + sizeof(*queue->kernel_if);
  280. u64 num_pages;
  281. if (size > SIZE_MAX - PAGE_SIZE)
  282. return NULL;
  283. num_pages = DIV_ROUND_UP(size, PAGE_SIZE) + 1;
  284. if (num_pages >
  285. (SIZE_MAX - queue_size) /
  286. (sizeof(*queue->kernel_if->u.g.pas) +
  287. sizeof(*queue->kernel_if->u.g.vas)))
  288. return NULL;
  289. pas_size = num_pages * sizeof(*queue->kernel_if->u.g.pas);
  290. vas_size = num_pages * sizeof(*queue->kernel_if->u.g.vas);
  291. queue_size += pas_size + vas_size;
  292. queue = vmalloc(queue_size);
  293. if (!queue)
  294. return NULL;
  295. queue->q_header = NULL;
  296. queue->saved_header = NULL;
  297. queue->kernel_if = (struct vmci_queue_kern_if *)(queue + 1);
  298. queue->kernel_if->mutex = NULL;
  299. queue->kernel_if->num_pages = num_pages;
  300. queue->kernel_if->u.g.pas = (dma_addr_t *)(queue->kernel_if + 1);
  301. queue->kernel_if->u.g.vas =
  302. (void **)((u8 *)queue->kernel_if->u.g.pas + pas_size);
  303. queue->kernel_if->host = false;
  304. for (i = 0; i < num_pages; i++) {
  305. queue->kernel_if->u.g.vas[i] =
  306. dma_alloc_coherent(&vmci_pdev->dev, PAGE_SIZE,
  307. &queue->kernel_if->u.g.pas[i],
  308. GFP_KERNEL);
  309. if (!queue->kernel_if->u.g.vas[i]) {
  310. /* Size excl. the header. */
  311. qp_free_queue(queue, i * PAGE_SIZE);
  312. return NULL;
  313. }
  314. }
  315. /* Queue header is the first page. */
  316. queue->q_header = queue->kernel_if->u.g.vas[0];
  317. return queue;
  318. }
  319. /*
  320. * Copies from a given buffer or iovector to a VMCI Queue. Uses
  321. * kmap()/kunmap() to dynamically map/unmap required portions of the queue
  322. * by traversing the offset -> page translation structure for the queue.
  323. * Assumes that offset + size does not wrap around in the queue.
  324. */
  325. static int __qp_memcpy_to_queue(struct vmci_queue *queue,
  326. u64 queue_offset,
  327. const void *src,
  328. size_t size,
  329. bool is_iovec)
  330. {
  331. struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
  332. size_t bytes_copied = 0;
  333. while (bytes_copied < size) {
  334. const u64 page_index =
  335. (queue_offset + bytes_copied) / PAGE_SIZE;
  336. const size_t page_offset =
  337. (queue_offset + bytes_copied) & (PAGE_SIZE - 1);
  338. void *va;
  339. size_t to_copy;
  340. if (kernel_if->host)
  341. va = kmap(kernel_if->u.h.page[page_index]);
  342. else
  343. va = kernel_if->u.g.vas[page_index + 1];
  344. /* Skip header. */
  345. if (size - bytes_copied > PAGE_SIZE - page_offset)
  346. /* Enough payload to fill up from this page. */
  347. to_copy = PAGE_SIZE - page_offset;
  348. else
  349. to_copy = size - bytes_copied;
  350. if (is_iovec) {
  351. struct msghdr *msg = (struct msghdr *)src;
  352. int err;
  353. /* The iovec will track bytes_copied internally. */
  354. err = memcpy_from_msg((u8 *)va + page_offset,
  355. msg, to_copy);
  356. if (err != 0) {
  357. if (kernel_if->host)
  358. kunmap(kernel_if->u.h.page[page_index]);
  359. return VMCI_ERROR_INVALID_ARGS;
  360. }
  361. } else {
  362. memcpy((u8 *)va + page_offset,
  363. (u8 *)src + bytes_copied, to_copy);
  364. }
  365. bytes_copied += to_copy;
  366. if (kernel_if->host)
  367. kunmap(kernel_if->u.h.page[page_index]);
  368. }
  369. return VMCI_SUCCESS;
  370. }
  371. /*
  372. * Copies to a given buffer or iovector from a VMCI Queue. Uses
  373. * kmap()/kunmap() to dynamically map/unmap required portions of the queue
  374. * by traversing the offset -> page translation structure for the queue.
  375. * Assumes that offset + size does not wrap around in the queue.
  376. */
  377. static int __qp_memcpy_from_queue(void *dest,
  378. const struct vmci_queue *queue,
  379. u64 queue_offset,
  380. size_t size,
  381. bool is_iovec)
  382. {
  383. struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
  384. size_t bytes_copied = 0;
  385. while (bytes_copied < size) {
  386. const u64 page_index =
  387. (queue_offset + bytes_copied) / PAGE_SIZE;
  388. const size_t page_offset =
  389. (queue_offset + bytes_copied) & (PAGE_SIZE - 1);
  390. void *va;
  391. size_t to_copy;
  392. if (kernel_if->host)
  393. va = kmap(kernel_if->u.h.page[page_index]);
  394. else
  395. va = kernel_if->u.g.vas[page_index + 1];
  396. /* Skip header. */
  397. if (size - bytes_copied > PAGE_SIZE - page_offset)
  398. /* Enough payload to fill up this page. */
  399. to_copy = PAGE_SIZE - page_offset;
  400. else
  401. to_copy = size - bytes_copied;
  402. if (is_iovec) {
  403. struct msghdr *msg = dest;
  404. int err;
  405. /* The iovec will track bytes_copied internally. */
  406. err = memcpy_to_msg(msg, (u8 *)va + page_offset,
  407. to_copy);
  408. if (err != 0) {
  409. if (kernel_if->host)
  410. kunmap(kernel_if->u.h.page[page_index]);
  411. return VMCI_ERROR_INVALID_ARGS;
  412. }
  413. } else {
  414. memcpy((u8 *)dest + bytes_copied,
  415. (u8 *)va + page_offset, to_copy);
  416. }
  417. bytes_copied += to_copy;
  418. if (kernel_if->host)
  419. kunmap(kernel_if->u.h.page[page_index]);
  420. }
  421. return VMCI_SUCCESS;
  422. }
  423. /*
  424. * Allocates two list of PPNs --- one for the pages in the produce queue,
  425. * and the other for the pages in the consume queue. Intializes the list
  426. * of PPNs with the page frame numbers of the KVA for the two queues (and
  427. * the queue headers).
  428. */
  429. static int qp_alloc_ppn_set(void *prod_q,
  430. u64 num_produce_pages,
  431. void *cons_q,
  432. u64 num_consume_pages, struct ppn_set *ppn_set)
  433. {
  434. u32 *produce_ppns;
  435. u32 *consume_ppns;
  436. struct vmci_queue *produce_q = prod_q;
  437. struct vmci_queue *consume_q = cons_q;
  438. u64 i;
  439. if (!produce_q || !num_produce_pages || !consume_q ||
  440. !num_consume_pages || !ppn_set)
  441. return VMCI_ERROR_INVALID_ARGS;
  442. if (ppn_set->initialized)
  443. return VMCI_ERROR_ALREADY_EXISTS;
  444. produce_ppns =
  445. kmalloc(num_produce_pages * sizeof(*produce_ppns), GFP_KERNEL);
  446. if (!produce_ppns)
  447. return VMCI_ERROR_NO_MEM;
  448. consume_ppns =
  449. kmalloc(num_consume_pages * sizeof(*consume_ppns), GFP_KERNEL);
  450. if (!consume_ppns) {
  451. kfree(produce_ppns);
  452. return VMCI_ERROR_NO_MEM;
  453. }
  454. for (i = 0; i < num_produce_pages; i++) {
  455. unsigned long pfn;
  456. produce_ppns[i] =
  457. produce_q->kernel_if->u.g.pas[i] >> PAGE_SHIFT;
  458. pfn = produce_ppns[i];
  459. /* Fail allocation if PFN isn't supported by hypervisor. */
  460. if (sizeof(pfn) > sizeof(*produce_ppns)
  461. && pfn != produce_ppns[i])
  462. goto ppn_error;
  463. }
  464. for (i = 0; i < num_consume_pages; i++) {
  465. unsigned long pfn;
  466. consume_ppns[i] =
  467. consume_q->kernel_if->u.g.pas[i] >> PAGE_SHIFT;
  468. pfn = consume_ppns[i];
  469. /* Fail allocation if PFN isn't supported by hypervisor. */
  470. if (sizeof(pfn) > sizeof(*consume_ppns)
  471. && pfn != consume_ppns[i])
  472. goto ppn_error;
  473. }
  474. ppn_set->num_produce_pages = num_produce_pages;
  475. ppn_set->num_consume_pages = num_consume_pages;
  476. ppn_set->produce_ppns = produce_ppns;
  477. ppn_set->consume_ppns = consume_ppns;
  478. ppn_set->initialized = true;
  479. return VMCI_SUCCESS;
  480. ppn_error:
  481. kfree(produce_ppns);
  482. kfree(consume_ppns);
  483. return VMCI_ERROR_INVALID_ARGS;
  484. }
  485. /*
  486. * Frees the two list of PPNs for a queue pair.
  487. */
  488. static void qp_free_ppn_set(struct ppn_set *ppn_set)
  489. {
  490. if (ppn_set->initialized) {
  491. /* Do not call these functions on NULL inputs. */
  492. kfree(ppn_set->produce_ppns);
  493. kfree(ppn_set->consume_ppns);
  494. }
  495. memset(ppn_set, 0, sizeof(*ppn_set));
  496. }
  497. /*
  498. * Populates the list of PPNs in the hypercall structure with the PPNS
  499. * of the produce queue and the consume queue.
  500. */
  501. static int qp_populate_ppn_set(u8 *call_buf, const struct ppn_set *ppn_set)
  502. {
  503. memcpy(call_buf, ppn_set->produce_ppns,
  504. ppn_set->num_produce_pages * sizeof(*ppn_set->produce_ppns));
  505. memcpy(call_buf +
  506. ppn_set->num_produce_pages * sizeof(*ppn_set->produce_ppns),
  507. ppn_set->consume_ppns,
  508. ppn_set->num_consume_pages * sizeof(*ppn_set->consume_ppns));
  509. return VMCI_SUCCESS;
  510. }
  511. static int qp_memcpy_to_queue(struct vmci_queue *queue,
  512. u64 queue_offset,
  513. const void *src, size_t src_offset, size_t size)
  514. {
  515. return __qp_memcpy_to_queue(queue, queue_offset,
  516. (u8 *)src + src_offset, size, false);
  517. }
  518. static int qp_memcpy_from_queue(void *dest,
  519. size_t dest_offset,
  520. const struct vmci_queue *queue,
  521. u64 queue_offset, size_t size)
  522. {
  523. return __qp_memcpy_from_queue((u8 *)dest + dest_offset,
  524. queue, queue_offset, size, false);
  525. }
  526. /*
  527. * Copies from a given iovec from a VMCI Queue.
  528. */
  529. static int qp_memcpy_to_queue_iov(struct vmci_queue *queue,
  530. u64 queue_offset,
  531. const void *msg,
  532. size_t src_offset, size_t size)
  533. {
  534. /*
  535. * We ignore src_offset because src is really a struct iovec * and will
  536. * maintain offset internally.
  537. */
  538. return __qp_memcpy_to_queue(queue, queue_offset, msg, size, true);
  539. }
  540. /*
  541. * Copies to a given iovec from a VMCI Queue.
  542. */
  543. static int qp_memcpy_from_queue_iov(void *dest,
  544. size_t dest_offset,
  545. const struct vmci_queue *queue,
  546. u64 queue_offset, size_t size)
  547. {
  548. /*
  549. * We ignore dest_offset because dest is really a struct iovec * and
  550. * will maintain offset internally.
  551. */
  552. return __qp_memcpy_from_queue(dest, queue, queue_offset, size, true);
  553. }
  554. /*
  555. * Allocates kernel VA space of specified size plus space for the queue
  556. * and kernel interface. This is different from the guest queue allocator,
  557. * because we do not allocate our own queue header/data pages here but
  558. * share those of the guest.
  559. */
  560. static struct vmci_queue *qp_host_alloc_queue(u64 size)
  561. {
  562. struct vmci_queue *queue;
  563. size_t queue_page_size;
  564. u64 num_pages;
  565. const size_t queue_size = sizeof(*queue) + sizeof(*(queue->kernel_if));
  566. if (size > SIZE_MAX - PAGE_SIZE)
  567. return NULL;
  568. num_pages = DIV_ROUND_UP(size, PAGE_SIZE) + 1;
  569. if (num_pages > (SIZE_MAX - queue_size) /
  570. sizeof(*queue->kernel_if->u.h.page))
  571. return NULL;
  572. queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
  573. queue = kzalloc(queue_size + queue_page_size, GFP_KERNEL);
  574. if (queue) {
  575. queue->q_header = NULL;
  576. queue->saved_header = NULL;
  577. queue->kernel_if = (struct vmci_queue_kern_if *)(queue + 1);
  578. queue->kernel_if->host = true;
  579. queue->kernel_if->mutex = NULL;
  580. queue->kernel_if->num_pages = num_pages;
  581. queue->kernel_if->u.h.header_page =
  582. (struct page **)((u8 *)queue + queue_size);
  583. queue->kernel_if->u.h.page =
  584. &queue->kernel_if->u.h.header_page[1];
  585. }
  586. return queue;
  587. }
  588. /*
  589. * Frees kernel memory for a given queue (header plus translation
  590. * structure).
  591. */
  592. static void qp_host_free_queue(struct vmci_queue *queue, u64 queue_size)
  593. {
  594. kfree(queue);
  595. }
  596. /*
  597. * Initialize the mutex for the pair of queues. This mutex is used to
  598. * protect the q_header and the buffer from changing out from under any
  599. * users of either queue. Of course, it's only any good if the mutexes
  600. * are actually acquired. Queue structure must lie on non-paged memory
  601. * or we cannot guarantee access to the mutex.
  602. */
  603. static void qp_init_queue_mutex(struct vmci_queue *produce_q,
  604. struct vmci_queue *consume_q)
  605. {
  606. /*
  607. * Only the host queue has shared state - the guest queues do not
  608. * need to synchronize access using a queue mutex.
  609. */
  610. if (produce_q->kernel_if->host) {
  611. produce_q->kernel_if->mutex = &produce_q->kernel_if->__mutex;
  612. consume_q->kernel_if->mutex = &produce_q->kernel_if->__mutex;
  613. mutex_init(produce_q->kernel_if->mutex);
  614. }
  615. }
  616. /*
  617. * Cleans up the mutex for the pair of queues.
  618. */
  619. static void qp_cleanup_queue_mutex(struct vmci_queue *produce_q,
  620. struct vmci_queue *consume_q)
  621. {
  622. if (produce_q->kernel_if->host) {
  623. produce_q->kernel_if->mutex = NULL;
  624. consume_q->kernel_if->mutex = NULL;
  625. }
  626. }
  627. /*
  628. * Acquire the mutex for the queue. Note that the produce_q and
  629. * the consume_q share a mutex. So, only one of the two need to
  630. * be passed in to this routine. Either will work just fine.
  631. */
  632. static void qp_acquire_queue_mutex(struct vmci_queue *queue)
  633. {
  634. if (queue->kernel_if->host)
  635. mutex_lock(queue->kernel_if->mutex);
  636. }
  637. /*
  638. * Release the mutex for the queue. Note that the produce_q and
  639. * the consume_q share a mutex. So, only one of the two need to
  640. * be passed in to this routine. Either will work just fine.
  641. */
  642. static void qp_release_queue_mutex(struct vmci_queue *queue)
  643. {
  644. if (queue->kernel_if->host)
  645. mutex_unlock(queue->kernel_if->mutex);
  646. }
  647. /*
  648. * Helper function to release pages in the PageStoreAttachInfo
  649. * previously obtained using get_user_pages.
  650. */
  651. static void qp_release_pages(struct page **pages,
  652. u64 num_pages, bool dirty)
  653. {
  654. int i;
  655. for (i = 0; i < num_pages; i++) {
  656. if (dirty)
  657. set_page_dirty(pages[i]);
  658. page_cache_release(pages[i]);
  659. pages[i] = NULL;
  660. }
  661. }
  662. /*
  663. * Lock the user pages referenced by the {produce,consume}Buffer
  664. * struct into memory and populate the {produce,consume}Pages
  665. * arrays in the attach structure with them.
  666. */
  667. static int qp_host_get_user_memory(u64 produce_uva,
  668. u64 consume_uva,
  669. struct vmci_queue *produce_q,
  670. struct vmci_queue *consume_q)
  671. {
  672. int retval;
  673. int err = VMCI_SUCCESS;
  674. retval = get_user_pages_fast((uintptr_t) produce_uva,
  675. produce_q->kernel_if->num_pages, 1,
  676. produce_q->kernel_if->u.h.header_page);
  677. if (retval < (int)produce_q->kernel_if->num_pages) {
  678. pr_debug("get_user_pages_fast(produce) failed (retval=%d)",
  679. retval);
  680. qp_release_pages(produce_q->kernel_if->u.h.header_page,
  681. retval, false);
  682. err = VMCI_ERROR_NO_MEM;
  683. goto out;
  684. }
  685. retval = get_user_pages_fast((uintptr_t) consume_uva,
  686. consume_q->kernel_if->num_pages, 1,
  687. consume_q->kernel_if->u.h.header_page);
  688. if (retval < (int)consume_q->kernel_if->num_pages) {
  689. pr_debug("get_user_pages_fast(consume) failed (retval=%d)",
  690. retval);
  691. qp_release_pages(consume_q->kernel_if->u.h.header_page,
  692. retval, false);
  693. qp_release_pages(produce_q->kernel_if->u.h.header_page,
  694. produce_q->kernel_if->num_pages, false);
  695. err = VMCI_ERROR_NO_MEM;
  696. }
  697. out:
  698. return err;
  699. }
  700. /*
  701. * Registers the specification of the user pages used for backing a queue
  702. * pair. Enough information to map in pages is stored in the OS specific
  703. * part of the struct vmci_queue structure.
  704. */
  705. static int qp_host_register_user_memory(struct vmci_qp_page_store *page_store,
  706. struct vmci_queue *produce_q,
  707. struct vmci_queue *consume_q)
  708. {
  709. u64 produce_uva;
  710. u64 consume_uva;
  711. /*
  712. * The new style and the old style mapping only differs in
  713. * that we either get a single or two UVAs, so we split the
  714. * single UVA range at the appropriate spot.
  715. */
  716. produce_uva = page_store->pages;
  717. consume_uva = page_store->pages +
  718. produce_q->kernel_if->num_pages * PAGE_SIZE;
  719. return qp_host_get_user_memory(produce_uva, consume_uva, produce_q,
  720. consume_q);
  721. }
  722. /*
  723. * Releases and removes the references to user pages stored in the attach
  724. * struct. Pages are released from the page cache and may become
  725. * swappable again.
  726. */
  727. static void qp_host_unregister_user_memory(struct vmci_queue *produce_q,
  728. struct vmci_queue *consume_q)
  729. {
  730. qp_release_pages(produce_q->kernel_if->u.h.header_page,
  731. produce_q->kernel_if->num_pages, true);
  732. memset(produce_q->kernel_if->u.h.header_page, 0,
  733. sizeof(*produce_q->kernel_if->u.h.header_page) *
  734. produce_q->kernel_if->num_pages);
  735. qp_release_pages(consume_q->kernel_if->u.h.header_page,
  736. consume_q->kernel_if->num_pages, true);
  737. memset(consume_q->kernel_if->u.h.header_page, 0,
  738. sizeof(*consume_q->kernel_if->u.h.header_page) *
  739. consume_q->kernel_if->num_pages);
  740. }
  741. /*
  742. * Once qp_host_register_user_memory has been performed on a
  743. * queue, the queue pair headers can be mapped into the
  744. * kernel. Once mapped, they must be unmapped with
  745. * qp_host_unmap_queues prior to calling
  746. * qp_host_unregister_user_memory.
  747. * Pages are pinned.
  748. */
  749. static int qp_host_map_queues(struct vmci_queue *produce_q,
  750. struct vmci_queue *consume_q)
  751. {
  752. int result;
  753. if (!produce_q->q_header || !consume_q->q_header) {
  754. struct page *headers[2];
  755. if (produce_q->q_header != consume_q->q_header)
  756. return VMCI_ERROR_QUEUEPAIR_MISMATCH;
  757. if (produce_q->kernel_if->u.h.header_page == NULL ||
  758. *produce_q->kernel_if->u.h.header_page == NULL)
  759. return VMCI_ERROR_UNAVAILABLE;
  760. headers[0] = *produce_q->kernel_if->u.h.header_page;
  761. headers[1] = *consume_q->kernel_if->u.h.header_page;
  762. produce_q->q_header = vmap(headers, 2, VM_MAP, PAGE_KERNEL);
  763. if (produce_q->q_header != NULL) {
  764. consume_q->q_header =
  765. (struct vmci_queue_header *)((u8 *)
  766. produce_q->q_header +
  767. PAGE_SIZE);
  768. result = VMCI_SUCCESS;
  769. } else {
  770. pr_warn("vmap failed\n");
  771. result = VMCI_ERROR_NO_MEM;
  772. }
  773. } else {
  774. result = VMCI_SUCCESS;
  775. }
  776. return result;
  777. }
  778. /*
  779. * Unmaps previously mapped queue pair headers from the kernel.
  780. * Pages are unpinned.
  781. */
  782. static int qp_host_unmap_queues(u32 gid,
  783. struct vmci_queue *produce_q,
  784. struct vmci_queue *consume_q)
  785. {
  786. if (produce_q->q_header) {
  787. if (produce_q->q_header < consume_q->q_header)
  788. vunmap(produce_q->q_header);
  789. else
  790. vunmap(consume_q->q_header);
  791. produce_q->q_header = NULL;
  792. consume_q->q_header = NULL;
  793. }
  794. return VMCI_SUCCESS;
  795. }
  796. /*
  797. * Finds the entry in the list corresponding to a given handle. Assumes
  798. * that the list is locked.
  799. */
  800. static struct qp_entry *qp_list_find(struct qp_list *qp_list,
  801. struct vmci_handle handle)
  802. {
  803. struct qp_entry *entry;
  804. if (vmci_handle_is_invalid(handle))
  805. return NULL;
  806. list_for_each_entry(entry, &qp_list->head, list_item) {
  807. if (vmci_handle_is_equal(entry->handle, handle))
  808. return entry;
  809. }
  810. return NULL;
  811. }
  812. /*
  813. * Finds the entry in the list corresponding to a given handle.
  814. */
  815. static struct qp_guest_endpoint *
  816. qp_guest_handle_to_entry(struct vmci_handle handle)
  817. {
  818. struct qp_guest_endpoint *entry;
  819. struct qp_entry *qp = qp_list_find(&qp_guest_endpoints, handle);
  820. entry = qp ? container_of(
  821. qp, struct qp_guest_endpoint, qp) : NULL;
  822. return entry;
  823. }
  824. /*
  825. * Finds the entry in the list corresponding to a given handle.
  826. */
  827. static struct qp_broker_entry *
  828. qp_broker_handle_to_entry(struct vmci_handle handle)
  829. {
  830. struct qp_broker_entry *entry;
  831. struct qp_entry *qp = qp_list_find(&qp_broker_list, handle);
  832. entry = qp ? container_of(
  833. qp, struct qp_broker_entry, qp) : NULL;
  834. return entry;
  835. }
  836. /*
  837. * Dispatches a queue pair event message directly into the local event
  838. * queue.
  839. */
  840. static int qp_notify_peer_local(bool attach, struct vmci_handle handle)
  841. {
  842. u32 context_id = vmci_get_context_id();
  843. struct vmci_event_qp ev;
  844. ev.msg.hdr.dst = vmci_make_handle(context_id, VMCI_EVENT_HANDLER);
  845. ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  846. VMCI_CONTEXT_RESOURCE_ID);
  847. ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
  848. ev.msg.event_data.event =
  849. attach ? VMCI_EVENT_QP_PEER_ATTACH : VMCI_EVENT_QP_PEER_DETACH;
  850. ev.payload.peer_id = context_id;
  851. ev.payload.handle = handle;
  852. return vmci_event_dispatch(&ev.msg.hdr);
  853. }
  854. /*
  855. * Allocates and initializes a qp_guest_endpoint structure.
  856. * Allocates a queue_pair rid (and handle) iff the given entry has
  857. * an invalid handle. 0 through VMCI_RESERVED_RESOURCE_ID_MAX
  858. * are reserved handles. Assumes that the QP list mutex is held
  859. * by the caller.
  860. */
  861. static struct qp_guest_endpoint *
  862. qp_guest_endpoint_create(struct vmci_handle handle,
  863. u32 peer,
  864. u32 flags,
  865. u64 produce_size,
  866. u64 consume_size,
  867. void *produce_q,
  868. void *consume_q)
  869. {
  870. int result;
  871. struct qp_guest_endpoint *entry;
  872. /* One page each for the queue headers. */
  873. const u64 num_ppns = DIV_ROUND_UP(produce_size, PAGE_SIZE) +
  874. DIV_ROUND_UP(consume_size, PAGE_SIZE) + 2;
  875. if (vmci_handle_is_invalid(handle)) {
  876. u32 context_id = vmci_get_context_id();
  877. handle = vmci_make_handle(context_id, VMCI_INVALID_ID);
  878. }
  879. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  880. if (entry) {
  881. entry->qp.peer = peer;
  882. entry->qp.flags = flags;
  883. entry->qp.produce_size = produce_size;
  884. entry->qp.consume_size = consume_size;
  885. entry->qp.ref_count = 0;
  886. entry->num_ppns = num_ppns;
  887. entry->produce_q = produce_q;
  888. entry->consume_q = consume_q;
  889. INIT_LIST_HEAD(&entry->qp.list_item);
  890. /* Add resource obj */
  891. result = vmci_resource_add(&entry->resource,
  892. VMCI_RESOURCE_TYPE_QPAIR_GUEST,
  893. handle);
  894. entry->qp.handle = vmci_resource_handle(&entry->resource);
  895. if ((result != VMCI_SUCCESS) ||
  896. qp_list_find(&qp_guest_endpoints, entry->qp.handle)) {
  897. pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
  898. handle.context, handle.resource, result);
  899. kfree(entry);
  900. entry = NULL;
  901. }
  902. }
  903. return entry;
  904. }
  905. /*
  906. * Frees a qp_guest_endpoint structure.
  907. */
  908. static void qp_guest_endpoint_destroy(struct qp_guest_endpoint *entry)
  909. {
  910. qp_free_ppn_set(&entry->ppn_set);
  911. qp_cleanup_queue_mutex(entry->produce_q, entry->consume_q);
  912. qp_free_queue(entry->produce_q, entry->qp.produce_size);
  913. qp_free_queue(entry->consume_q, entry->qp.consume_size);
  914. /* Unlink from resource hash table and free callback */
  915. vmci_resource_remove(&entry->resource);
  916. kfree(entry);
  917. }
  918. /*
  919. * Helper to make a queue_pairAlloc hypercall when the driver is
  920. * supporting a guest device.
  921. */
  922. static int qp_alloc_hypercall(const struct qp_guest_endpoint *entry)
  923. {
  924. struct vmci_qp_alloc_msg *alloc_msg;
  925. size_t msg_size;
  926. int result;
  927. if (!entry || entry->num_ppns <= 2)
  928. return VMCI_ERROR_INVALID_ARGS;
  929. msg_size = sizeof(*alloc_msg) +
  930. (size_t) entry->num_ppns * sizeof(u32);
  931. alloc_msg = kmalloc(msg_size, GFP_KERNEL);
  932. if (!alloc_msg)
  933. return VMCI_ERROR_NO_MEM;
  934. alloc_msg->hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  935. VMCI_QUEUEPAIR_ALLOC);
  936. alloc_msg->hdr.src = VMCI_ANON_SRC_HANDLE;
  937. alloc_msg->hdr.payload_size = msg_size - VMCI_DG_HEADERSIZE;
  938. alloc_msg->handle = entry->qp.handle;
  939. alloc_msg->peer = entry->qp.peer;
  940. alloc_msg->flags = entry->qp.flags;
  941. alloc_msg->produce_size = entry->qp.produce_size;
  942. alloc_msg->consume_size = entry->qp.consume_size;
  943. alloc_msg->num_ppns = entry->num_ppns;
  944. result = qp_populate_ppn_set((u8 *)alloc_msg + sizeof(*alloc_msg),
  945. &entry->ppn_set);
  946. if (result == VMCI_SUCCESS)
  947. result = vmci_send_datagram(&alloc_msg->hdr);
  948. kfree(alloc_msg);
  949. return result;
  950. }
  951. /*
  952. * Helper to make a queue_pairDetach hypercall when the driver is
  953. * supporting a guest device.
  954. */
  955. static int qp_detatch_hypercall(struct vmci_handle handle)
  956. {
  957. struct vmci_qp_detach_msg detach_msg;
  958. detach_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  959. VMCI_QUEUEPAIR_DETACH);
  960. detach_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
  961. detach_msg.hdr.payload_size = sizeof(handle);
  962. detach_msg.handle = handle;
  963. return vmci_send_datagram(&detach_msg.hdr);
  964. }
  965. /*
  966. * Adds the given entry to the list. Assumes that the list is locked.
  967. */
  968. static void qp_list_add_entry(struct qp_list *qp_list, struct qp_entry *entry)
  969. {
  970. if (entry)
  971. list_add(&entry->list_item, &qp_list->head);
  972. }
  973. /*
  974. * Removes the given entry from the list. Assumes that the list is locked.
  975. */
  976. static void qp_list_remove_entry(struct qp_list *qp_list,
  977. struct qp_entry *entry)
  978. {
  979. if (entry)
  980. list_del(&entry->list_item);
  981. }
  982. /*
  983. * Helper for VMCI queue_pair detach interface. Frees the physical
  984. * pages for the queue pair.
  985. */
  986. static int qp_detatch_guest_work(struct vmci_handle handle)
  987. {
  988. int result;
  989. struct qp_guest_endpoint *entry;
  990. u32 ref_count = ~0; /* To avoid compiler warning below */
  991. mutex_lock(&qp_guest_endpoints.mutex);
  992. entry = qp_guest_handle_to_entry(handle);
  993. if (!entry) {
  994. mutex_unlock(&qp_guest_endpoints.mutex);
  995. return VMCI_ERROR_NOT_FOUND;
  996. }
  997. if (entry->qp.flags & VMCI_QPFLAG_LOCAL) {
  998. result = VMCI_SUCCESS;
  999. if (entry->qp.ref_count > 1) {
  1000. result = qp_notify_peer_local(false, handle);
  1001. /*
  1002. * We can fail to notify a local queuepair
  1003. * because we can't allocate. We still want
  1004. * to release the entry if that happens, so
  1005. * don't bail out yet.
  1006. */
  1007. }
  1008. } else {
  1009. result = qp_detatch_hypercall(handle);
  1010. if (result < VMCI_SUCCESS) {
  1011. /*
  1012. * We failed to notify a non-local queuepair.
  1013. * That other queuepair might still be
  1014. * accessing the shared memory, so don't
  1015. * release the entry yet. It will get cleaned
  1016. * up by VMCIqueue_pair_Exit() if necessary
  1017. * (assuming we are going away, otherwise why
  1018. * did this fail?).
  1019. */
  1020. mutex_unlock(&qp_guest_endpoints.mutex);
  1021. return result;
  1022. }
  1023. }
  1024. /*
  1025. * If we get here then we either failed to notify a local queuepair, or
  1026. * we succeeded in all cases. Release the entry if required.
  1027. */
  1028. entry->qp.ref_count--;
  1029. if (entry->qp.ref_count == 0)
  1030. qp_list_remove_entry(&qp_guest_endpoints, &entry->qp);
  1031. /* If we didn't remove the entry, this could change once we unlock. */
  1032. if (entry)
  1033. ref_count = entry->qp.ref_count;
  1034. mutex_unlock(&qp_guest_endpoints.mutex);
  1035. if (ref_count == 0)
  1036. qp_guest_endpoint_destroy(entry);
  1037. return result;
  1038. }
  1039. /*
  1040. * This functions handles the actual allocation of a VMCI queue
  1041. * pair guest endpoint. Allocates physical pages for the queue
  1042. * pair. It makes OS dependent calls through generic wrappers.
  1043. */
  1044. static int qp_alloc_guest_work(struct vmci_handle *handle,
  1045. struct vmci_queue **produce_q,
  1046. u64 produce_size,
  1047. struct vmci_queue **consume_q,
  1048. u64 consume_size,
  1049. u32 peer,
  1050. u32 flags,
  1051. u32 priv_flags)
  1052. {
  1053. const u64 num_produce_pages =
  1054. DIV_ROUND_UP(produce_size, PAGE_SIZE) + 1;
  1055. const u64 num_consume_pages =
  1056. DIV_ROUND_UP(consume_size, PAGE_SIZE) + 1;
  1057. void *my_produce_q = NULL;
  1058. void *my_consume_q = NULL;
  1059. int result;
  1060. struct qp_guest_endpoint *queue_pair_entry = NULL;
  1061. if (priv_flags != VMCI_NO_PRIVILEGE_FLAGS)
  1062. return VMCI_ERROR_NO_ACCESS;
  1063. mutex_lock(&qp_guest_endpoints.mutex);
  1064. queue_pair_entry = qp_guest_handle_to_entry(*handle);
  1065. if (queue_pair_entry) {
  1066. if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) {
  1067. /* Local attach case. */
  1068. if (queue_pair_entry->qp.ref_count > 1) {
  1069. pr_devel("Error attempting to attach more than once\n");
  1070. result = VMCI_ERROR_UNAVAILABLE;
  1071. goto error_keep_entry;
  1072. }
  1073. if (queue_pair_entry->qp.produce_size != consume_size ||
  1074. queue_pair_entry->qp.consume_size !=
  1075. produce_size ||
  1076. queue_pair_entry->qp.flags !=
  1077. (flags & ~VMCI_QPFLAG_ATTACH_ONLY)) {
  1078. pr_devel("Error mismatched queue pair in local attach\n");
  1079. result = VMCI_ERROR_QUEUEPAIR_MISMATCH;
  1080. goto error_keep_entry;
  1081. }
  1082. /*
  1083. * Do a local attach. We swap the consume and
  1084. * produce queues for the attacher and deliver
  1085. * an attach event.
  1086. */
  1087. result = qp_notify_peer_local(true, *handle);
  1088. if (result < VMCI_SUCCESS)
  1089. goto error_keep_entry;
  1090. my_produce_q = queue_pair_entry->consume_q;
  1091. my_consume_q = queue_pair_entry->produce_q;
  1092. goto out;
  1093. }
  1094. result = VMCI_ERROR_ALREADY_EXISTS;
  1095. goto error_keep_entry;
  1096. }
  1097. my_produce_q = qp_alloc_queue(produce_size, flags);
  1098. if (!my_produce_q) {
  1099. pr_warn("Error allocating pages for produce queue\n");
  1100. result = VMCI_ERROR_NO_MEM;
  1101. goto error;
  1102. }
  1103. my_consume_q = qp_alloc_queue(consume_size, flags);
  1104. if (!my_consume_q) {
  1105. pr_warn("Error allocating pages for consume queue\n");
  1106. result = VMCI_ERROR_NO_MEM;
  1107. goto error;
  1108. }
  1109. queue_pair_entry = qp_guest_endpoint_create(*handle, peer, flags,
  1110. produce_size, consume_size,
  1111. my_produce_q, my_consume_q);
  1112. if (!queue_pair_entry) {
  1113. pr_warn("Error allocating memory in %s\n", __func__);
  1114. result = VMCI_ERROR_NO_MEM;
  1115. goto error;
  1116. }
  1117. result = qp_alloc_ppn_set(my_produce_q, num_produce_pages, my_consume_q,
  1118. num_consume_pages,
  1119. &queue_pair_entry->ppn_set);
  1120. if (result < VMCI_SUCCESS) {
  1121. pr_warn("qp_alloc_ppn_set failed\n");
  1122. goto error;
  1123. }
  1124. /*
  1125. * It's only necessary to notify the host if this queue pair will be
  1126. * attached to from another context.
  1127. */
  1128. if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) {
  1129. /* Local create case. */
  1130. u32 context_id = vmci_get_context_id();
  1131. /*
  1132. * Enforce similar checks on local queue pairs as we
  1133. * do for regular ones. The handle's context must
  1134. * match the creator or attacher context id (here they
  1135. * are both the current context id) and the
  1136. * attach-only flag cannot exist during create. We
  1137. * also ensure specified peer is this context or an
  1138. * invalid one.
  1139. */
  1140. if (queue_pair_entry->qp.handle.context != context_id ||
  1141. (queue_pair_entry->qp.peer != VMCI_INVALID_ID &&
  1142. queue_pair_entry->qp.peer != context_id)) {
  1143. result = VMCI_ERROR_NO_ACCESS;
  1144. goto error;
  1145. }
  1146. if (queue_pair_entry->qp.flags & VMCI_QPFLAG_ATTACH_ONLY) {
  1147. result = VMCI_ERROR_NOT_FOUND;
  1148. goto error;
  1149. }
  1150. } else {
  1151. result = qp_alloc_hypercall(queue_pair_entry);
  1152. if (result < VMCI_SUCCESS) {
  1153. pr_warn("qp_alloc_hypercall result = %d\n", result);
  1154. goto error;
  1155. }
  1156. }
  1157. qp_init_queue_mutex((struct vmci_queue *)my_produce_q,
  1158. (struct vmci_queue *)my_consume_q);
  1159. qp_list_add_entry(&qp_guest_endpoints, &queue_pair_entry->qp);
  1160. out:
  1161. queue_pair_entry->qp.ref_count++;
  1162. *handle = queue_pair_entry->qp.handle;
  1163. *produce_q = (struct vmci_queue *)my_produce_q;
  1164. *consume_q = (struct vmci_queue *)my_consume_q;
  1165. /*
  1166. * We should initialize the queue pair header pages on a local
  1167. * queue pair create. For non-local queue pairs, the
  1168. * hypervisor initializes the header pages in the create step.
  1169. */
  1170. if ((queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) &&
  1171. queue_pair_entry->qp.ref_count == 1) {
  1172. vmci_q_header_init((*produce_q)->q_header, *handle);
  1173. vmci_q_header_init((*consume_q)->q_header, *handle);
  1174. }
  1175. mutex_unlock(&qp_guest_endpoints.mutex);
  1176. return VMCI_SUCCESS;
  1177. error:
  1178. mutex_unlock(&qp_guest_endpoints.mutex);
  1179. if (queue_pair_entry) {
  1180. /* The queues will be freed inside the destroy routine. */
  1181. qp_guest_endpoint_destroy(queue_pair_entry);
  1182. } else {
  1183. qp_free_queue(my_produce_q, produce_size);
  1184. qp_free_queue(my_consume_q, consume_size);
  1185. }
  1186. return result;
  1187. error_keep_entry:
  1188. /* This path should only be used when an existing entry was found. */
  1189. mutex_unlock(&qp_guest_endpoints.mutex);
  1190. return result;
  1191. }
  1192. /*
  1193. * The first endpoint issuing a queue pair allocation will create the state
  1194. * of the queue pair in the queue pair broker.
  1195. *
  1196. * If the creator is a guest, it will associate a VMX virtual address range
  1197. * with the queue pair as specified by the page_store. For compatibility with
  1198. * older VMX'en, that would use a separate step to set the VMX virtual
  1199. * address range, the virtual address range can be registered later using
  1200. * vmci_qp_broker_set_page_store. In that case, a page_store of NULL should be
  1201. * used.
  1202. *
  1203. * If the creator is the host, a page_store of NULL should be used as well,
  1204. * since the host is not able to supply a page store for the queue pair.
  1205. *
  1206. * For older VMX and host callers, the queue pair will be created in the
  1207. * VMCIQPB_CREATED_NO_MEM state, and for current VMX callers, it will be
  1208. * created in VMCOQPB_CREATED_MEM state.
  1209. */
  1210. static int qp_broker_create(struct vmci_handle handle,
  1211. u32 peer,
  1212. u32 flags,
  1213. u32 priv_flags,
  1214. u64 produce_size,
  1215. u64 consume_size,
  1216. struct vmci_qp_page_store *page_store,
  1217. struct vmci_ctx *context,
  1218. vmci_event_release_cb wakeup_cb,
  1219. void *client_data, struct qp_broker_entry **ent)
  1220. {
  1221. struct qp_broker_entry *entry = NULL;
  1222. const u32 context_id = vmci_ctx_get_id(context);
  1223. bool is_local = flags & VMCI_QPFLAG_LOCAL;
  1224. int result;
  1225. u64 guest_produce_size;
  1226. u64 guest_consume_size;
  1227. /* Do not create if the caller asked not to. */
  1228. if (flags & VMCI_QPFLAG_ATTACH_ONLY)
  1229. return VMCI_ERROR_NOT_FOUND;
  1230. /*
  1231. * Creator's context ID should match handle's context ID or the creator
  1232. * must allow the context in handle's context ID as the "peer".
  1233. */
  1234. if (handle.context != context_id && handle.context != peer)
  1235. return VMCI_ERROR_NO_ACCESS;
  1236. if (VMCI_CONTEXT_IS_VM(context_id) && VMCI_CONTEXT_IS_VM(peer))
  1237. return VMCI_ERROR_DST_UNREACHABLE;
  1238. /*
  1239. * Creator's context ID for local queue pairs should match the
  1240. * peer, if a peer is specified.
  1241. */
  1242. if (is_local && peer != VMCI_INVALID_ID && context_id != peer)
  1243. return VMCI_ERROR_NO_ACCESS;
  1244. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  1245. if (!entry)
  1246. return VMCI_ERROR_NO_MEM;
  1247. if (vmci_ctx_get_id(context) == VMCI_HOST_CONTEXT_ID && !is_local) {
  1248. /*
  1249. * The queue pair broker entry stores values from the guest
  1250. * point of view, so a creating host side endpoint should swap
  1251. * produce and consume values -- unless it is a local queue
  1252. * pair, in which case no swapping is necessary, since the local
  1253. * attacher will swap queues.
  1254. */
  1255. guest_produce_size = consume_size;
  1256. guest_consume_size = produce_size;
  1257. } else {
  1258. guest_produce_size = produce_size;
  1259. guest_consume_size = consume_size;
  1260. }
  1261. entry->qp.handle = handle;
  1262. entry->qp.peer = peer;
  1263. entry->qp.flags = flags;
  1264. entry->qp.produce_size = guest_produce_size;
  1265. entry->qp.consume_size = guest_consume_size;
  1266. entry->qp.ref_count = 1;
  1267. entry->create_id = context_id;
  1268. entry->attach_id = VMCI_INVALID_ID;
  1269. entry->state = VMCIQPB_NEW;
  1270. entry->require_trusted_attach =
  1271. !!(context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED);
  1272. entry->created_by_trusted =
  1273. !!(priv_flags & VMCI_PRIVILEGE_FLAG_TRUSTED);
  1274. entry->vmci_page_files = false;
  1275. entry->wakeup_cb = wakeup_cb;
  1276. entry->client_data = client_data;
  1277. entry->produce_q = qp_host_alloc_queue(guest_produce_size);
  1278. if (entry->produce_q == NULL) {
  1279. result = VMCI_ERROR_NO_MEM;
  1280. goto error;
  1281. }
  1282. entry->consume_q = qp_host_alloc_queue(guest_consume_size);
  1283. if (entry->consume_q == NULL) {
  1284. result = VMCI_ERROR_NO_MEM;
  1285. goto error;
  1286. }
  1287. qp_init_queue_mutex(entry->produce_q, entry->consume_q);
  1288. INIT_LIST_HEAD(&entry->qp.list_item);
  1289. if (is_local) {
  1290. u8 *tmp;
  1291. entry->local_mem = kcalloc(QPE_NUM_PAGES(entry->qp),
  1292. PAGE_SIZE, GFP_KERNEL);
  1293. if (entry->local_mem == NULL) {
  1294. result = VMCI_ERROR_NO_MEM;
  1295. goto error;
  1296. }
  1297. entry->state = VMCIQPB_CREATED_MEM;
  1298. entry->produce_q->q_header = entry->local_mem;
  1299. tmp = (u8 *)entry->local_mem + PAGE_SIZE *
  1300. (DIV_ROUND_UP(entry->qp.produce_size, PAGE_SIZE) + 1);
  1301. entry->consume_q->q_header = (struct vmci_queue_header *)tmp;
  1302. } else if (page_store) {
  1303. /*
  1304. * The VMX already initialized the queue pair headers, so no
  1305. * need for the kernel side to do that.
  1306. */
  1307. result = qp_host_register_user_memory(page_store,
  1308. entry->produce_q,
  1309. entry->consume_q);
  1310. if (result < VMCI_SUCCESS)
  1311. goto error;
  1312. entry->state = VMCIQPB_CREATED_MEM;
  1313. } else {
  1314. /*
  1315. * A create without a page_store may be either a host
  1316. * side create (in which case we are waiting for the
  1317. * guest side to supply the memory) or an old style
  1318. * queue pair create (in which case we will expect a
  1319. * set page store call as the next step).
  1320. */
  1321. entry->state = VMCIQPB_CREATED_NO_MEM;
  1322. }
  1323. qp_list_add_entry(&qp_broker_list, &entry->qp);
  1324. if (ent != NULL)
  1325. *ent = entry;
  1326. /* Add to resource obj */
  1327. result = vmci_resource_add(&entry->resource,
  1328. VMCI_RESOURCE_TYPE_QPAIR_HOST,
  1329. handle);
  1330. if (result != VMCI_SUCCESS) {
  1331. pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
  1332. handle.context, handle.resource, result);
  1333. goto error;
  1334. }
  1335. entry->qp.handle = vmci_resource_handle(&entry->resource);
  1336. if (is_local) {
  1337. vmci_q_header_init(entry->produce_q->q_header,
  1338. entry->qp.handle);
  1339. vmci_q_header_init(entry->consume_q->q_header,
  1340. entry->qp.handle);
  1341. }
  1342. vmci_ctx_qp_create(context, entry->qp.handle);
  1343. return VMCI_SUCCESS;
  1344. error:
  1345. if (entry != NULL) {
  1346. qp_host_free_queue(entry->produce_q, guest_produce_size);
  1347. qp_host_free_queue(entry->consume_q, guest_consume_size);
  1348. kfree(entry);
  1349. }
  1350. return result;
  1351. }
  1352. /*
  1353. * Enqueues an event datagram to notify the peer VM attached to
  1354. * the given queue pair handle about attach/detach event by the
  1355. * given VM. Returns Payload size of datagram enqueued on
  1356. * success, error code otherwise.
  1357. */
  1358. static int qp_notify_peer(bool attach,
  1359. struct vmci_handle handle,
  1360. u32 my_id,
  1361. u32 peer_id)
  1362. {
  1363. int rv;
  1364. struct vmci_event_qp ev;
  1365. if (vmci_handle_is_invalid(handle) || my_id == VMCI_INVALID_ID ||
  1366. peer_id == VMCI_INVALID_ID)
  1367. return VMCI_ERROR_INVALID_ARGS;
  1368. /*
  1369. * In vmci_ctx_enqueue_datagram() we enforce the upper limit on
  1370. * number of pending events from the hypervisor to a given VM
  1371. * otherwise a rogue VM could do an arbitrary number of attach
  1372. * and detach operations causing memory pressure in the host
  1373. * kernel.
  1374. */
  1375. ev.msg.hdr.dst = vmci_make_handle(peer_id, VMCI_EVENT_HANDLER);
  1376. ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  1377. VMCI_CONTEXT_RESOURCE_ID);
  1378. ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
  1379. ev.msg.event_data.event = attach ?
  1380. VMCI_EVENT_QP_PEER_ATTACH : VMCI_EVENT_QP_PEER_DETACH;
  1381. ev.payload.handle = handle;
  1382. ev.payload.peer_id = my_id;
  1383. rv = vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID,
  1384. &ev.msg.hdr, false);
  1385. if (rv < VMCI_SUCCESS)
  1386. pr_warn("Failed to enqueue queue_pair %s event datagram for context (ID=0x%x)\n",
  1387. attach ? "ATTACH" : "DETACH", peer_id);
  1388. return rv;
  1389. }
  1390. /*
  1391. * The second endpoint issuing a queue pair allocation will attach to
  1392. * the queue pair registered with the queue pair broker.
  1393. *
  1394. * If the attacher is a guest, it will associate a VMX virtual address
  1395. * range with the queue pair as specified by the page_store. At this
  1396. * point, the already attach host endpoint may start using the queue
  1397. * pair, and an attach event is sent to it. For compatibility with
  1398. * older VMX'en, that used a separate step to set the VMX virtual
  1399. * address range, the virtual address range can be registered later
  1400. * using vmci_qp_broker_set_page_store. In that case, a page_store of
  1401. * NULL should be used, and the attach event will be generated once
  1402. * the actual page store has been set.
  1403. *
  1404. * If the attacher is the host, a page_store of NULL should be used as
  1405. * well, since the page store information is already set by the guest.
  1406. *
  1407. * For new VMX and host callers, the queue pair will be moved to the
  1408. * VMCIQPB_ATTACHED_MEM state, and for older VMX callers, it will be
  1409. * moved to the VMCOQPB_ATTACHED_NO_MEM state.
  1410. */
  1411. static int qp_broker_attach(struct qp_broker_entry *entry,
  1412. u32 peer,
  1413. u32 flags,
  1414. u32 priv_flags,
  1415. u64 produce_size,
  1416. u64 consume_size,
  1417. struct vmci_qp_page_store *page_store,
  1418. struct vmci_ctx *context,
  1419. vmci_event_release_cb wakeup_cb,
  1420. void *client_data,
  1421. struct qp_broker_entry **ent)
  1422. {
  1423. const u32 context_id = vmci_ctx_get_id(context);
  1424. bool is_local = flags & VMCI_QPFLAG_LOCAL;
  1425. int result;
  1426. if (entry->state != VMCIQPB_CREATED_NO_MEM &&
  1427. entry->state != VMCIQPB_CREATED_MEM)
  1428. return VMCI_ERROR_UNAVAILABLE;
  1429. if (is_local) {
  1430. if (!(entry->qp.flags & VMCI_QPFLAG_LOCAL) ||
  1431. context_id != entry->create_id) {
  1432. return VMCI_ERROR_INVALID_ARGS;
  1433. }
  1434. } else if (context_id == entry->create_id ||
  1435. context_id == entry->attach_id) {
  1436. return VMCI_ERROR_ALREADY_EXISTS;
  1437. }
  1438. if (VMCI_CONTEXT_IS_VM(context_id) &&
  1439. VMCI_CONTEXT_IS_VM(entry->create_id))
  1440. return VMCI_ERROR_DST_UNREACHABLE;
  1441. /*
  1442. * If we are attaching from a restricted context then the queuepair
  1443. * must have been created by a trusted endpoint.
  1444. */
  1445. if ((context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
  1446. !entry->created_by_trusted)
  1447. return VMCI_ERROR_NO_ACCESS;
  1448. /*
  1449. * If we are attaching to a queuepair that was created by a restricted
  1450. * context then we must be trusted.
  1451. */
  1452. if (entry->require_trusted_attach &&
  1453. (!(priv_flags & VMCI_PRIVILEGE_FLAG_TRUSTED)))
  1454. return VMCI_ERROR_NO_ACCESS;
  1455. /*
  1456. * If the creator specifies VMCI_INVALID_ID in "peer" field, access
  1457. * control check is not performed.
  1458. */
  1459. if (entry->qp.peer != VMCI_INVALID_ID && entry->qp.peer != context_id)
  1460. return VMCI_ERROR_NO_ACCESS;
  1461. if (entry->create_id == VMCI_HOST_CONTEXT_ID) {
  1462. /*
  1463. * Do not attach if the caller doesn't support Host Queue Pairs
  1464. * and a host created this queue pair.
  1465. */
  1466. if (!vmci_ctx_supports_host_qp(context))
  1467. return VMCI_ERROR_INVALID_RESOURCE;
  1468. } else if (context_id == VMCI_HOST_CONTEXT_ID) {
  1469. struct vmci_ctx *create_context;
  1470. bool supports_host_qp;
  1471. /*
  1472. * Do not attach a host to a user created queue pair if that
  1473. * user doesn't support host queue pair end points.
  1474. */
  1475. create_context = vmci_ctx_get(entry->create_id);
  1476. supports_host_qp = vmci_ctx_supports_host_qp(create_context);
  1477. vmci_ctx_put(create_context);
  1478. if (!supports_host_qp)
  1479. return VMCI_ERROR_INVALID_RESOURCE;
  1480. }
  1481. if ((entry->qp.flags & ~VMCI_QP_ASYMM) != (flags & ~VMCI_QP_ASYMM_PEER))
  1482. return VMCI_ERROR_QUEUEPAIR_MISMATCH;
  1483. if (context_id != VMCI_HOST_CONTEXT_ID) {
  1484. /*
  1485. * The queue pair broker entry stores values from the guest
  1486. * point of view, so an attaching guest should match the values
  1487. * stored in the entry.
  1488. */
  1489. if (entry->qp.produce_size != produce_size ||
  1490. entry->qp.consume_size != consume_size) {
  1491. return VMCI_ERROR_QUEUEPAIR_MISMATCH;
  1492. }
  1493. } else if (entry->qp.produce_size != consume_size ||
  1494. entry->qp.consume_size != produce_size) {
  1495. return VMCI_ERROR_QUEUEPAIR_MISMATCH;
  1496. }
  1497. if (context_id != VMCI_HOST_CONTEXT_ID) {
  1498. /*
  1499. * If a guest attached to a queue pair, it will supply
  1500. * the backing memory. If this is a pre NOVMVM vmx,
  1501. * the backing memory will be supplied by calling
  1502. * vmci_qp_broker_set_page_store() following the
  1503. * return of the vmci_qp_broker_alloc() call. If it is
  1504. * a vmx of version NOVMVM or later, the page store
  1505. * must be supplied as part of the
  1506. * vmci_qp_broker_alloc call. Under all circumstances
  1507. * must the initially created queue pair not have any
  1508. * memory associated with it already.
  1509. */
  1510. if (entry->state != VMCIQPB_CREATED_NO_MEM)
  1511. return VMCI_ERROR_INVALID_ARGS;
  1512. if (page_store != NULL) {
  1513. /*
  1514. * Patch up host state to point to guest
  1515. * supplied memory. The VMX already
  1516. * initialized the queue pair headers, so no
  1517. * need for the kernel side to do that.
  1518. */
  1519. result = qp_host_register_user_memory(page_store,
  1520. entry->produce_q,
  1521. entry->consume_q);
  1522. if (result < VMCI_SUCCESS)
  1523. return result;
  1524. entry->state = VMCIQPB_ATTACHED_MEM;
  1525. } else {
  1526. entry->state = VMCIQPB_ATTACHED_NO_MEM;
  1527. }
  1528. } else if (entry->state == VMCIQPB_CREATED_NO_MEM) {
  1529. /*
  1530. * The host side is attempting to attach to a queue
  1531. * pair that doesn't have any memory associated with
  1532. * it. This must be a pre NOVMVM vmx that hasn't set
  1533. * the page store information yet, or a quiesced VM.
  1534. */
  1535. return VMCI_ERROR_UNAVAILABLE;
  1536. } else {
  1537. /* The host side has successfully attached to a queue pair. */
  1538. entry->state = VMCIQPB_ATTACHED_MEM;
  1539. }
  1540. if (entry->state == VMCIQPB_ATTACHED_MEM) {
  1541. result =
  1542. qp_notify_peer(true, entry->qp.handle, context_id,
  1543. entry->create_id);
  1544. if (result < VMCI_SUCCESS)
  1545. pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
  1546. entry->create_id, entry->qp.handle.context,
  1547. entry->qp.handle.resource);
  1548. }
  1549. entry->attach_id = context_id;
  1550. entry->qp.ref_count++;
  1551. if (wakeup_cb) {
  1552. entry->wakeup_cb = wakeup_cb;
  1553. entry->client_data = client_data;
  1554. }
  1555. /*
  1556. * When attaching to local queue pairs, the context already has
  1557. * an entry tracking the queue pair, so don't add another one.
  1558. */
  1559. if (!is_local)
  1560. vmci_ctx_qp_create(context, entry->qp.handle);
  1561. if (ent != NULL)
  1562. *ent = entry;
  1563. return VMCI_SUCCESS;
  1564. }
  1565. /*
  1566. * queue_pair_Alloc for use when setting up queue pair endpoints
  1567. * on the host.
  1568. */
  1569. static int qp_broker_alloc(struct vmci_handle handle,
  1570. u32 peer,
  1571. u32 flags,
  1572. u32 priv_flags,
  1573. u64 produce_size,
  1574. u64 consume_size,
  1575. struct vmci_qp_page_store *page_store,
  1576. struct vmci_ctx *context,
  1577. vmci_event_release_cb wakeup_cb,
  1578. void *client_data,
  1579. struct qp_broker_entry **ent,
  1580. bool *swap)
  1581. {
  1582. const u32 context_id = vmci_ctx_get_id(context);
  1583. bool create;
  1584. struct qp_broker_entry *entry = NULL;
  1585. bool is_local = flags & VMCI_QPFLAG_LOCAL;
  1586. int result;
  1587. if (vmci_handle_is_invalid(handle) ||
  1588. (flags & ~VMCI_QP_ALL_FLAGS) || is_local ||
  1589. !(produce_size || consume_size) ||
  1590. !context || context_id == VMCI_INVALID_ID ||
  1591. handle.context == VMCI_INVALID_ID) {
  1592. return VMCI_ERROR_INVALID_ARGS;
  1593. }
  1594. if (page_store && !VMCI_QP_PAGESTORE_IS_WELLFORMED(page_store))
  1595. return VMCI_ERROR_INVALID_ARGS;
  1596. /*
  1597. * In the initial argument check, we ensure that non-vmkernel hosts
  1598. * are not allowed to create local queue pairs.
  1599. */
  1600. mutex_lock(&qp_broker_list.mutex);
  1601. if (!is_local && vmci_ctx_qp_exists(context, handle)) {
  1602. pr_devel("Context (ID=0x%x) already attached to queue pair (handle=0x%x:0x%x)\n",
  1603. context_id, handle.context, handle.resource);
  1604. mutex_unlock(&qp_broker_list.mutex);
  1605. return VMCI_ERROR_ALREADY_EXISTS;
  1606. }
  1607. if (handle.resource != VMCI_INVALID_ID)
  1608. entry = qp_broker_handle_to_entry(handle);
  1609. if (!entry) {
  1610. create = true;
  1611. result =
  1612. qp_broker_create(handle, peer, flags, priv_flags,
  1613. produce_size, consume_size, page_store,
  1614. context, wakeup_cb, client_data, ent);
  1615. } else {
  1616. create = false;
  1617. result =
  1618. qp_broker_attach(entry, peer, flags, priv_flags,
  1619. produce_size, consume_size, page_store,
  1620. context, wakeup_cb, client_data, ent);
  1621. }
  1622. mutex_unlock(&qp_broker_list.mutex);
  1623. if (swap)
  1624. *swap = (context_id == VMCI_HOST_CONTEXT_ID) &&
  1625. !(create && is_local);
  1626. return result;
  1627. }
  1628. /*
  1629. * This function implements the kernel API for allocating a queue
  1630. * pair.
  1631. */
  1632. static int qp_alloc_host_work(struct vmci_handle *handle,
  1633. struct vmci_queue **produce_q,
  1634. u64 produce_size,
  1635. struct vmci_queue **consume_q,
  1636. u64 consume_size,
  1637. u32 peer,
  1638. u32 flags,
  1639. u32 priv_flags,
  1640. vmci_event_release_cb wakeup_cb,
  1641. void *client_data)
  1642. {
  1643. struct vmci_handle new_handle;
  1644. struct vmci_ctx *context;
  1645. struct qp_broker_entry *entry;
  1646. int result;
  1647. bool swap;
  1648. if (vmci_handle_is_invalid(*handle)) {
  1649. new_handle = vmci_make_handle(
  1650. VMCI_HOST_CONTEXT_ID, VMCI_INVALID_ID);
  1651. } else
  1652. new_handle = *handle;
  1653. context = vmci_ctx_get(VMCI_HOST_CONTEXT_ID);
  1654. entry = NULL;
  1655. result =
  1656. qp_broker_alloc(new_handle, peer, flags, priv_flags,
  1657. produce_size, consume_size, NULL, context,
  1658. wakeup_cb, client_data, &entry, &swap);
  1659. if (result == VMCI_SUCCESS) {
  1660. if (swap) {
  1661. /*
  1662. * If this is a local queue pair, the attacher
  1663. * will swap around produce and consume
  1664. * queues.
  1665. */
  1666. *produce_q = entry->consume_q;
  1667. *consume_q = entry->produce_q;
  1668. } else {
  1669. *produce_q = entry->produce_q;
  1670. *consume_q = entry->consume_q;
  1671. }
  1672. *handle = vmci_resource_handle(&entry->resource);
  1673. } else {
  1674. *handle = VMCI_INVALID_HANDLE;
  1675. pr_devel("queue pair broker failed to alloc (result=%d)\n",
  1676. result);
  1677. }
  1678. vmci_ctx_put(context);
  1679. return result;
  1680. }
  1681. /*
  1682. * Allocates a VMCI queue_pair. Only checks validity of input
  1683. * arguments. The real work is done in the host or guest
  1684. * specific function.
  1685. */
  1686. int vmci_qp_alloc(struct vmci_handle *handle,
  1687. struct vmci_queue **produce_q,
  1688. u64 produce_size,
  1689. struct vmci_queue **consume_q,
  1690. u64 consume_size,
  1691. u32 peer,
  1692. u32 flags,
  1693. u32 priv_flags,
  1694. bool guest_endpoint,
  1695. vmci_event_release_cb wakeup_cb,
  1696. void *client_data)
  1697. {
  1698. if (!handle || !produce_q || !consume_q ||
  1699. (!produce_size && !consume_size) || (flags & ~VMCI_QP_ALL_FLAGS))
  1700. return VMCI_ERROR_INVALID_ARGS;
  1701. if (guest_endpoint) {
  1702. return qp_alloc_guest_work(handle, produce_q,
  1703. produce_size, consume_q,
  1704. consume_size, peer,
  1705. flags, priv_flags);
  1706. } else {
  1707. return qp_alloc_host_work(handle, produce_q,
  1708. produce_size, consume_q,
  1709. consume_size, peer, flags,
  1710. priv_flags, wakeup_cb, client_data);
  1711. }
  1712. }
  1713. /*
  1714. * This function implements the host kernel API for detaching from
  1715. * a queue pair.
  1716. */
  1717. static int qp_detatch_host_work(struct vmci_handle handle)
  1718. {
  1719. int result;
  1720. struct vmci_ctx *context;
  1721. context = vmci_ctx_get(VMCI_HOST_CONTEXT_ID);
  1722. result = vmci_qp_broker_detach(handle, context);
  1723. vmci_ctx_put(context);
  1724. return result;
  1725. }
  1726. /*
  1727. * Detaches from a VMCI queue_pair. Only checks validity of input argument.
  1728. * Real work is done in the host or guest specific function.
  1729. */
  1730. static int qp_detatch(struct vmci_handle handle, bool guest_endpoint)
  1731. {
  1732. if (vmci_handle_is_invalid(handle))
  1733. return VMCI_ERROR_INVALID_ARGS;
  1734. if (guest_endpoint)
  1735. return qp_detatch_guest_work(handle);
  1736. else
  1737. return qp_detatch_host_work(handle);
  1738. }
  1739. /*
  1740. * Returns the entry from the head of the list. Assumes that the list is
  1741. * locked.
  1742. */
  1743. static struct qp_entry *qp_list_get_head(struct qp_list *qp_list)
  1744. {
  1745. if (!list_empty(&qp_list->head)) {
  1746. struct qp_entry *entry =
  1747. list_first_entry(&qp_list->head, struct qp_entry,
  1748. list_item);
  1749. return entry;
  1750. }
  1751. return NULL;
  1752. }
  1753. void vmci_qp_broker_exit(void)
  1754. {
  1755. struct qp_entry *entry;
  1756. struct qp_broker_entry *be;
  1757. mutex_lock(&qp_broker_list.mutex);
  1758. while ((entry = qp_list_get_head(&qp_broker_list))) {
  1759. be = (struct qp_broker_entry *)entry;
  1760. qp_list_remove_entry(&qp_broker_list, entry);
  1761. kfree(be);
  1762. }
  1763. mutex_unlock(&qp_broker_list.mutex);
  1764. }
  1765. /*
  1766. * Requests that a queue pair be allocated with the VMCI queue
  1767. * pair broker. Allocates a queue pair entry if one does not
  1768. * exist. Attaches to one if it exists, and retrieves the page
  1769. * files backing that queue_pair. Assumes that the queue pair
  1770. * broker lock is held.
  1771. */
  1772. int vmci_qp_broker_alloc(struct vmci_handle handle,
  1773. u32 peer,
  1774. u32 flags,
  1775. u32 priv_flags,
  1776. u64 produce_size,
  1777. u64 consume_size,
  1778. struct vmci_qp_page_store *page_store,
  1779. struct vmci_ctx *context)
  1780. {
  1781. return qp_broker_alloc(handle, peer, flags, priv_flags,
  1782. produce_size, consume_size,
  1783. page_store, context, NULL, NULL, NULL, NULL);
  1784. }
  1785. /*
  1786. * VMX'en with versions lower than VMCI_VERSION_NOVMVM use a separate
  1787. * step to add the UVAs of the VMX mapping of the queue pair. This function
  1788. * provides backwards compatibility with such VMX'en, and takes care of
  1789. * registering the page store for a queue pair previously allocated by the
  1790. * VMX during create or attach. This function will move the queue pair state
  1791. * to either from VMCIQBP_CREATED_NO_MEM to VMCIQBP_CREATED_MEM or
  1792. * VMCIQBP_ATTACHED_NO_MEM to VMCIQBP_ATTACHED_MEM. If moving to the
  1793. * attached state with memory, the queue pair is ready to be used by the
  1794. * host peer, and an attached event will be generated.
  1795. *
  1796. * Assumes that the queue pair broker lock is held.
  1797. *
  1798. * This function is only used by the hosted platform, since there is no
  1799. * issue with backwards compatibility for vmkernel.
  1800. */
  1801. int vmci_qp_broker_set_page_store(struct vmci_handle handle,
  1802. u64 produce_uva,
  1803. u64 consume_uva,
  1804. struct vmci_ctx *context)
  1805. {
  1806. struct qp_broker_entry *entry;
  1807. int result;
  1808. const u32 context_id = vmci_ctx_get_id(context);
  1809. if (vmci_handle_is_invalid(handle) || !context ||
  1810. context_id == VMCI_INVALID_ID)
  1811. return VMCI_ERROR_INVALID_ARGS;
  1812. /*
  1813. * We only support guest to host queue pairs, so the VMX must
  1814. * supply UVAs for the mapped page files.
  1815. */
  1816. if (produce_uva == 0 || consume_uva == 0)
  1817. return VMCI_ERROR_INVALID_ARGS;
  1818. mutex_lock(&qp_broker_list.mutex);
  1819. if (!vmci_ctx_qp_exists(context, handle)) {
  1820. pr_warn("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
  1821. context_id, handle.context, handle.resource);
  1822. result = VMCI_ERROR_NOT_FOUND;
  1823. goto out;
  1824. }
  1825. entry = qp_broker_handle_to_entry(handle);
  1826. if (!entry) {
  1827. result = VMCI_ERROR_NOT_FOUND;
  1828. goto out;
  1829. }
  1830. /*
  1831. * If I'm the owner then I can set the page store.
  1832. *
  1833. * Or, if a host created the queue_pair and I'm the attached peer
  1834. * then I can set the page store.
  1835. */
  1836. if (entry->create_id != context_id &&
  1837. (entry->create_id != VMCI_HOST_CONTEXT_ID ||
  1838. entry->attach_id != context_id)) {
  1839. result = VMCI_ERROR_QUEUEPAIR_NOTOWNER;
  1840. goto out;
  1841. }
  1842. if (entry->state != VMCIQPB_CREATED_NO_MEM &&
  1843. entry->state != VMCIQPB_ATTACHED_NO_MEM) {
  1844. result = VMCI_ERROR_UNAVAILABLE;
  1845. goto out;
  1846. }
  1847. result = qp_host_get_user_memory(produce_uva, consume_uva,
  1848. entry->produce_q, entry->consume_q);
  1849. if (result < VMCI_SUCCESS)
  1850. goto out;
  1851. result = qp_host_map_queues(entry->produce_q, entry->consume_q);
  1852. if (result < VMCI_SUCCESS) {
  1853. qp_host_unregister_user_memory(entry->produce_q,
  1854. entry->consume_q);
  1855. goto out;
  1856. }
  1857. if (entry->state == VMCIQPB_CREATED_NO_MEM)
  1858. entry->state = VMCIQPB_CREATED_MEM;
  1859. else
  1860. entry->state = VMCIQPB_ATTACHED_MEM;
  1861. entry->vmci_page_files = true;
  1862. if (entry->state == VMCIQPB_ATTACHED_MEM) {
  1863. result =
  1864. qp_notify_peer(true, handle, context_id, entry->create_id);
  1865. if (result < VMCI_SUCCESS) {
  1866. pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
  1867. entry->create_id, entry->qp.handle.context,
  1868. entry->qp.handle.resource);
  1869. }
  1870. }
  1871. result = VMCI_SUCCESS;
  1872. out:
  1873. mutex_unlock(&qp_broker_list.mutex);
  1874. return result;
  1875. }
  1876. /*
  1877. * Resets saved queue headers for the given QP broker
  1878. * entry. Should be used when guest memory becomes available
  1879. * again, or the guest detaches.
  1880. */
  1881. static void qp_reset_saved_headers(struct qp_broker_entry *entry)
  1882. {
  1883. entry->produce_q->saved_header = NULL;
  1884. entry->consume_q->saved_header = NULL;
  1885. }
  1886. /*
  1887. * The main entry point for detaching from a queue pair registered with the
  1888. * queue pair broker. If more than one endpoint is attached to the queue
  1889. * pair, the first endpoint will mainly decrement a reference count and
  1890. * generate a notification to its peer. The last endpoint will clean up
  1891. * the queue pair state registered with the broker.
  1892. *
  1893. * When a guest endpoint detaches, it will unmap and unregister the guest
  1894. * memory backing the queue pair. If the host is still attached, it will
  1895. * no longer be able to access the queue pair content.
  1896. *
  1897. * If the queue pair is already in a state where there is no memory
  1898. * registered for the queue pair (any *_NO_MEM state), it will transition to
  1899. * the VMCIQPB_SHUTDOWN_NO_MEM state. This will also happen, if a guest
  1900. * endpoint is the first of two endpoints to detach. If the host endpoint is
  1901. * the first out of two to detach, the queue pair will move to the
  1902. * VMCIQPB_SHUTDOWN_MEM state.
  1903. */
  1904. int vmci_qp_broker_detach(struct vmci_handle handle, struct vmci_ctx *context)
  1905. {
  1906. struct qp_broker_entry *entry;
  1907. const u32 context_id = vmci_ctx_get_id(context);
  1908. u32 peer_id;
  1909. bool is_local = false;
  1910. int result;
  1911. if (vmci_handle_is_invalid(handle) || !context ||
  1912. context_id == VMCI_INVALID_ID) {
  1913. return VMCI_ERROR_INVALID_ARGS;
  1914. }
  1915. mutex_lock(&qp_broker_list.mutex);
  1916. if (!vmci_ctx_qp_exists(context, handle)) {
  1917. pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
  1918. context_id, handle.context, handle.resource);
  1919. result = VMCI_ERROR_NOT_FOUND;
  1920. goto out;
  1921. }
  1922. entry = qp_broker_handle_to_entry(handle);
  1923. if (!entry) {
  1924. pr_devel("Context (ID=0x%x) reports being attached to queue pair(handle=0x%x:0x%x) that isn't present in broker\n",
  1925. context_id, handle.context, handle.resource);
  1926. result = VMCI_ERROR_NOT_FOUND;
  1927. goto out;
  1928. }
  1929. if (context_id != entry->create_id && context_id != entry->attach_id) {
  1930. result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
  1931. goto out;
  1932. }
  1933. if (context_id == entry->create_id) {
  1934. peer_id = entry->attach_id;
  1935. entry->create_id = VMCI_INVALID_ID;
  1936. } else {
  1937. peer_id = entry->create_id;
  1938. entry->attach_id = VMCI_INVALID_ID;
  1939. }
  1940. entry->qp.ref_count--;
  1941. is_local = entry->qp.flags & VMCI_QPFLAG_LOCAL;
  1942. if (context_id != VMCI_HOST_CONTEXT_ID) {
  1943. bool headers_mapped;
  1944. /*
  1945. * Pre NOVMVM vmx'en may detach from a queue pair
  1946. * before setting the page store, and in that case
  1947. * there is no user memory to detach from. Also, more
  1948. * recent VMX'en may detach from a queue pair in the
  1949. * quiesced state.
  1950. */
  1951. qp_acquire_queue_mutex(entry->produce_q);
  1952. headers_mapped = entry->produce_q->q_header ||
  1953. entry->consume_q->q_header;
  1954. if (QPBROKERSTATE_HAS_MEM(entry)) {
  1955. result =
  1956. qp_host_unmap_queues(INVALID_VMCI_GUEST_MEM_ID,
  1957. entry->produce_q,
  1958. entry->consume_q);
  1959. if (result < VMCI_SUCCESS)
  1960. pr_warn("Failed to unmap queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
  1961. handle.context, handle.resource,
  1962. result);
  1963. if (entry->vmci_page_files)
  1964. qp_host_unregister_user_memory(entry->produce_q,
  1965. entry->
  1966. consume_q);
  1967. else
  1968. qp_host_unregister_user_memory(entry->produce_q,
  1969. entry->
  1970. consume_q);
  1971. }
  1972. if (!headers_mapped)
  1973. qp_reset_saved_headers(entry);
  1974. qp_release_queue_mutex(entry->produce_q);
  1975. if (!headers_mapped && entry->wakeup_cb)
  1976. entry->wakeup_cb(entry->client_data);
  1977. } else {
  1978. if (entry->wakeup_cb) {
  1979. entry->wakeup_cb = NULL;
  1980. entry->client_data = NULL;
  1981. }
  1982. }
  1983. if (entry->qp.ref_count == 0) {
  1984. qp_list_remove_entry(&qp_broker_list, &entry->qp);
  1985. if (is_local)
  1986. kfree(entry->local_mem);
  1987. qp_cleanup_queue_mutex(entry->produce_q, entry->consume_q);
  1988. qp_host_free_queue(entry->produce_q, entry->qp.produce_size);
  1989. qp_host_free_queue(entry->consume_q, entry->qp.consume_size);
  1990. /* Unlink from resource hash table and free callback */
  1991. vmci_resource_remove(&entry->resource);
  1992. kfree(entry);
  1993. vmci_ctx_qp_destroy(context, handle);
  1994. } else {
  1995. qp_notify_peer(false, handle, context_id, peer_id);
  1996. if (context_id == VMCI_HOST_CONTEXT_ID &&
  1997. QPBROKERSTATE_HAS_MEM(entry)) {
  1998. entry->state = VMCIQPB_SHUTDOWN_MEM;
  1999. } else {
  2000. entry->state = VMCIQPB_SHUTDOWN_NO_MEM;
  2001. }
  2002. if (!is_local)
  2003. vmci_ctx_qp_destroy(context, handle);
  2004. }
  2005. result = VMCI_SUCCESS;
  2006. out:
  2007. mutex_unlock(&qp_broker_list.mutex);
  2008. return result;
  2009. }
  2010. /*
  2011. * Establishes the necessary mappings for a queue pair given a
  2012. * reference to the queue pair guest memory. This is usually
  2013. * called when a guest is unquiesced and the VMX is allowed to
  2014. * map guest memory once again.
  2015. */
  2016. int vmci_qp_broker_map(struct vmci_handle handle,
  2017. struct vmci_ctx *context,
  2018. u64 guest_mem)
  2019. {
  2020. struct qp_broker_entry *entry;
  2021. const u32 context_id = vmci_ctx_get_id(context);
  2022. bool is_local = false;
  2023. int result;
  2024. if (vmci_handle_is_invalid(handle) || !context ||
  2025. context_id == VMCI_INVALID_ID)
  2026. return VMCI_ERROR_INVALID_ARGS;
  2027. mutex_lock(&qp_broker_list.mutex);
  2028. if (!vmci_ctx_qp_exists(context, handle)) {
  2029. pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
  2030. context_id, handle.context, handle.resource);
  2031. result = VMCI_ERROR_NOT_FOUND;
  2032. goto out;
  2033. }
  2034. entry = qp_broker_handle_to_entry(handle);
  2035. if (!entry) {
  2036. pr_devel("Context (ID=0x%x) reports being attached to queue pair (handle=0x%x:0x%x) that isn't present in broker\n",
  2037. context_id, handle.context, handle.resource);
  2038. result = VMCI_ERROR_NOT_FOUND;
  2039. goto out;
  2040. }
  2041. if (context_id != entry->create_id && context_id != entry->attach_id) {
  2042. result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
  2043. goto out;
  2044. }
  2045. is_local = entry->qp.flags & VMCI_QPFLAG_LOCAL;
  2046. result = VMCI_SUCCESS;
  2047. if (context_id != VMCI_HOST_CONTEXT_ID) {
  2048. struct vmci_qp_page_store page_store;
  2049. page_store.pages = guest_mem;
  2050. page_store.len = QPE_NUM_PAGES(entry->qp);
  2051. qp_acquire_queue_mutex(entry->produce_q);
  2052. qp_reset_saved_headers(entry);
  2053. result =
  2054. qp_host_register_user_memory(&page_store,
  2055. entry->produce_q,
  2056. entry->consume_q);
  2057. qp_release_queue_mutex(entry->produce_q);
  2058. if (result == VMCI_SUCCESS) {
  2059. /* Move state from *_NO_MEM to *_MEM */
  2060. entry->state++;
  2061. if (entry->wakeup_cb)
  2062. entry->wakeup_cb(entry->client_data);
  2063. }
  2064. }
  2065. out:
  2066. mutex_unlock(&qp_broker_list.mutex);
  2067. return result;
  2068. }
  2069. /*
  2070. * Saves a snapshot of the queue headers for the given QP broker
  2071. * entry. Should be used when guest memory is unmapped.
  2072. * Results:
  2073. * VMCI_SUCCESS on success, appropriate error code if guest memory
  2074. * can't be accessed..
  2075. */
  2076. static int qp_save_headers(struct qp_broker_entry *entry)
  2077. {
  2078. int result;
  2079. if (entry->produce_q->saved_header != NULL &&
  2080. entry->consume_q->saved_header != NULL) {
  2081. /*
  2082. * If the headers have already been saved, we don't need to do
  2083. * it again, and we don't want to map in the headers
  2084. * unnecessarily.
  2085. */
  2086. return VMCI_SUCCESS;
  2087. }
  2088. if (NULL == entry->produce_q->q_header ||
  2089. NULL == entry->consume_q->q_header) {
  2090. result = qp_host_map_queues(entry->produce_q, entry->consume_q);
  2091. if (result < VMCI_SUCCESS)
  2092. return result;
  2093. }
  2094. memcpy(&entry->saved_produce_q, entry->produce_q->q_header,
  2095. sizeof(entry->saved_produce_q));
  2096. entry->produce_q->saved_header = &entry->saved_produce_q;
  2097. memcpy(&entry->saved_consume_q, entry->consume_q->q_header,
  2098. sizeof(entry->saved_consume_q));
  2099. entry->consume_q->saved_header = &entry->saved_consume_q;
  2100. return VMCI_SUCCESS;
  2101. }
  2102. /*
  2103. * Removes all references to the guest memory of a given queue pair, and
  2104. * will move the queue pair from state *_MEM to *_NO_MEM. It is usually
  2105. * called when a VM is being quiesced where access to guest memory should
  2106. * avoided.
  2107. */
  2108. int vmci_qp_broker_unmap(struct vmci_handle handle,
  2109. struct vmci_ctx *context,
  2110. u32 gid)
  2111. {
  2112. struct qp_broker_entry *entry;
  2113. const u32 context_id = vmci_ctx_get_id(context);
  2114. bool is_local = false;
  2115. int result;
  2116. if (vmci_handle_is_invalid(handle) || !context ||
  2117. context_id == VMCI_INVALID_ID)
  2118. return VMCI_ERROR_INVALID_ARGS;
  2119. mutex_lock(&qp_broker_list.mutex);
  2120. if (!vmci_ctx_qp_exists(context, handle)) {
  2121. pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
  2122. context_id, handle.context, handle.resource);
  2123. result = VMCI_ERROR_NOT_FOUND;
  2124. goto out;
  2125. }
  2126. entry = qp_broker_handle_to_entry(handle);
  2127. if (!entry) {
  2128. pr_devel("Context (ID=0x%x) reports being attached to queue pair (handle=0x%x:0x%x) that isn't present in broker\n",
  2129. context_id, handle.context, handle.resource);
  2130. result = VMCI_ERROR_NOT_FOUND;
  2131. goto out;
  2132. }
  2133. if (context_id != entry->create_id && context_id != entry->attach_id) {
  2134. result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
  2135. goto out;
  2136. }
  2137. is_local = entry->qp.flags & VMCI_QPFLAG_LOCAL;
  2138. if (context_id != VMCI_HOST_CONTEXT_ID) {
  2139. qp_acquire_queue_mutex(entry->produce_q);
  2140. result = qp_save_headers(entry);
  2141. if (result < VMCI_SUCCESS)
  2142. pr_warn("Failed to save queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
  2143. handle.context, handle.resource, result);
  2144. qp_host_unmap_queues(gid, entry->produce_q, entry->consume_q);
  2145. /*
  2146. * On hosted, when we unmap queue pairs, the VMX will also
  2147. * unmap the guest memory, so we invalidate the previously
  2148. * registered memory. If the queue pair is mapped again at a
  2149. * later point in time, we will need to reregister the user
  2150. * memory with a possibly new user VA.
  2151. */
  2152. qp_host_unregister_user_memory(entry->produce_q,
  2153. entry->consume_q);
  2154. /*
  2155. * Move state from *_MEM to *_NO_MEM.
  2156. */
  2157. entry->state--;
  2158. qp_release_queue_mutex(entry->produce_q);
  2159. }
  2160. result = VMCI_SUCCESS;
  2161. out:
  2162. mutex_unlock(&qp_broker_list.mutex);
  2163. return result;
  2164. }
  2165. /*
  2166. * Destroys all guest queue pair endpoints. If active guest queue
  2167. * pairs still exist, hypercalls to attempt detach from these
  2168. * queue pairs will be made. Any failure to detach is silently
  2169. * ignored.
  2170. */
  2171. void vmci_qp_guest_endpoints_exit(void)
  2172. {
  2173. struct qp_entry *entry;
  2174. struct qp_guest_endpoint *ep;
  2175. mutex_lock(&qp_guest_endpoints.mutex);
  2176. while ((entry = qp_list_get_head(&qp_guest_endpoints))) {
  2177. ep = (struct qp_guest_endpoint *)entry;
  2178. /* Don't make a hypercall for local queue_pairs. */
  2179. if (!(entry->flags & VMCI_QPFLAG_LOCAL))
  2180. qp_detatch_hypercall(entry->handle);
  2181. /* We cannot fail the exit, so let's reset ref_count. */
  2182. entry->ref_count = 0;
  2183. qp_list_remove_entry(&qp_guest_endpoints, entry);
  2184. qp_guest_endpoint_destroy(ep);
  2185. }
  2186. mutex_unlock(&qp_guest_endpoints.mutex);
  2187. }
  2188. /*
  2189. * Helper routine that will lock the queue pair before subsequent
  2190. * operations.
  2191. * Note: Non-blocking on the host side is currently only implemented in ESX.
  2192. * Since non-blocking isn't yet implemented on the host personality we
  2193. * have no reason to acquire a spin lock. So to avoid the use of an
  2194. * unnecessary lock only acquire the mutex if we can block.
  2195. */
  2196. static void qp_lock(const struct vmci_qp *qpair)
  2197. {
  2198. qp_acquire_queue_mutex(qpair->produce_q);
  2199. }
  2200. /*
  2201. * Helper routine that unlocks the queue pair after calling
  2202. * qp_lock.
  2203. */
  2204. static void qp_unlock(const struct vmci_qp *qpair)
  2205. {
  2206. qp_release_queue_mutex(qpair->produce_q);
  2207. }
  2208. /*
  2209. * The queue headers may not be mapped at all times. If a queue is
  2210. * currently not mapped, it will be attempted to do so.
  2211. */
  2212. static int qp_map_queue_headers(struct vmci_queue *produce_q,
  2213. struct vmci_queue *consume_q)
  2214. {
  2215. int result;
  2216. if (NULL == produce_q->q_header || NULL == consume_q->q_header) {
  2217. result = qp_host_map_queues(produce_q, consume_q);
  2218. if (result < VMCI_SUCCESS)
  2219. return (produce_q->saved_header &&
  2220. consume_q->saved_header) ?
  2221. VMCI_ERROR_QUEUEPAIR_NOT_READY :
  2222. VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
  2223. }
  2224. return VMCI_SUCCESS;
  2225. }
  2226. /*
  2227. * Helper routine that will retrieve the produce and consume
  2228. * headers of a given queue pair. If the guest memory of the
  2229. * queue pair is currently not available, the saved queue headers
  2230. * will be returned, if these are available.
  2231. */
  2232. static int qp_get_queue_headers(const struct vmci_qp *qpair,
  2233. struct vmci_queue_header **produce_q_header,
  2234. struct vmci_queue_header **consume_q_header)
  2235. {
  2236. int result;
  2237. result = qp_map_queue_headers(qpair->produce_q, qpair->consume_q);
  2238. if (result == VMCI_SUCCESS) {
  2239. *produce_q_header = qpair->produce_q->q_header;
  2240. *consume_q_header = qpair->consume_q->q_header;
  2241. } else if (qpair->produce_q->saved_header &&
  2242. qpair->consume_q->saved_header) {
  2243. *produce_q_header = qpair->produce_q->saved_header;
  2244. *consume_q_header = qpair->consume_q->saved_header;
  2245. result = VMCI_SUCCESS;
  2246. }
  2247. return result;
  2248. }
  2249. /*
  2250. * Callback from VMCI queue pair broker indicating that a queue
  2251. * pair that was previously not ready, now either is ready or
  2252. * gone forever.
  2253. */
  2254. static int qp_wakeup_cb(void *client_data)
  2255. {
  2256. struct vmci_qp *qpair = (struct vmci_qp *)client_data;
  2257. qp_lock(qpair);
  2258. while (qpair->blocked > 0) {
  2259. qpair->blocked--;
  2260. qpair->generation++;
  2261. wake_up(&qpair->event);
  2262. }
  2263. qp_unlock(qpair);
  2264. return VMCI_SUCCESS;
  2265. }
  2266. /*
  2267. * Makes the calling thread wait for the queue pair to become
  2268. * ready for host side access. Returns true when thread is
  2269. * woken up after queue pair state change, false otherwise.
  2270. */
  2271. static bool qp_wait_for_ready_queue(struct vmci_qp *qpair)
  2272. {
  2273. unsigned int generation;
  2274. qpair->blocked++;
  2275. generation = qpair->generation;
  2276. qp_unlock(qpair);
  2277. wait_event(qpair->event, generation != qpair->generation);
  2278. qp_lock(qpair);
  2279. return true;
  2280. }
  2281. /*
  2282. * Enqueues a given buffer to the produce queue using the provided
  2283. * function. As many bytes as possible (space available in the queue)
  2284. * are enqueued. Assumes the queue->mutex has been acquired. Returns
  2285. * VMCI_ERROR_QUEUEPAIR_NOSPACE if no space was available to enqueue
  2286. * data, VMCI_ERROR_INVALID_SIZE, if any queue pointer is outside the
  2287. * queue (as defined by the queue size), VMCI_ERROR_INVALID_ARGS, if
  2288. * an error occured when accessing the buffer,
  2289. * VMCI_ERROR_QUEUEPAIR_NOTATTACHED, if the queue pair pages aren't
  2290. * available. Otherwise, the number of bytes written to the queue is
  2291. * returned. Updates the tail pointer of the produce queue.
  2292. */
  2293. static ssize_t qp_enqueue_locked(struct vmci_queue *produce_q,
  2294. struct vmci_queue *consume_q,
  2295. const u64 produce_q_size,
  2296. const void *buf,
  2297. size_t buf_size,
  2298. vmci_memcpy_to_queue_func memcpy_to_queue)
  2299. {
  2300. s64 free_space;
  2301. u64 tail;
  2302. size_t written;
  2303. ssize_t result;
  2304. result = qp_map_queue_headers(produce_q, consume_q);
  2305. if (unlikely(result != VMCI_SUCCESS))
  2306. return result;
  2307. free_space = vmci_q_header_free_space(produce_q->q_header,
  2308. consume_q->q_header,
  2309. produce_q_size);
  2310. if (free_space == 0)
  2311. return VMCI_ERROR_QUEUEPAIR_NOSPACE;
  2312. if (free_space < VMCI_SUCCESS)
  2313. return (ssize_t) free_space;
  2314. written = (size_t) (free_space > buf_size ? buf_size : free_space);
  2315. tail = vmci_q_header_producer_tail(produce_q->q_header);
  2316. if (likely(tail + written < produce_q_size)) {
  2317. result = memcpy_to_queue(produce_q, tail, buf, 0, written);
  2318. } else {
  2319. /* Tail pointer wraps around. */
  2320. const size_t tmp = (size_t) (produce_q_size - tail);
  2321. result = memcpy_to_queue(produce_q, tail, buf, 0, tmp);
  2322. if (result >= VMCI_SUCCESS)
  2323. result = memcpy_to_queue(produce_q, 0, buf, tmp,
  2324. written - tmp);
  2325. }
  2326. if (result < VMCI_SUCCESS)
  2327. return result;
  2328. vmci_q_header_add_producer_tail(produce_q->q_header, written,
  2329. produce_q_size);
  2330. return written;
  2331. }
  2332. /*
  2333. * Dequeues data (if available) from the given consume queue. Writes data
  2334. * to the user provided buffer using the provided function.
  2335. * Assumes the queue->mutex has been acquired.
  2336. * Results:
  2337. * VMCI_ERROR_QUEUEPAIR_NODATA if no data was available to dequeue.
  2338. * VMCI_ERROR_INVALID_SIZE, if any queue pointer is outside the queue
  2339. * (as defined by the queue size).
  2340. * VMCI_ERROR_INVALID_ARGS, if an error occured when accessing the buffer.
  2341. * Otherwise the number of bytes dequeued is returned.
  2342. * Side effects:
  2343. * Updates the head pointer of the consume queue.
  2344. */
  2345. static ssize_t qp_dequeue_locked(struct vmci_queue *produce_q,
  2346. struct vmci_queue *consume_q,
  2347. const u64 consume_q_size,
  2348. void *buf,
  2349. size_t buf_size,
  2350. vmci_memcpy_from_queue_func memcpy_from_queue,
  2351. bool update_consumer)
  2352. {
  2353. s64 buf_ready;
  2354. u64 head;
  2355. size_t read;
  2356. ssize_t result;
  2357. result = qp_map_queue_headers(produce_q, consume_q);
  2358. if (unlikely(result != VMCI_SUCCESS))
  2359. return result;
  2360. buf_ready = vmci_q_header_buf_ready(consume_q->q_header,
  2361. produce_q->q_header,
  2362. consume_q_size);
  2363. if (buf_ready == 0)
  2364. return VMCI_ERROR_QUEUEPAIR_NODATA;
  2365. if (buf_ready < VMCI_SUCCESS)
  2366. return (ssize_t) buf_ready;
  2367. read = (size_t) (buf_ready > buf_size ? buf_size : buf_ready);
  2368. head = vmci_q_header_consumer_head(produce_q->q_header);
  2369. if (likely(head + read < consume_q_size)) {
  2370. result = memcpy_from_queue(buf, 0, consume_q, head, read);
  2371. } else {
  2372. /* Head pointer wraps around. */
  2373. const size_t tmp = (size_t) (consume_q_size - head);
  2374. result = memcpy_from_queue(buf, 0, consume_q, head, tmp);
  2375. if (result >= VMCI_SUCCESS)
  2376. result = memcpy_from_queue(buf, tmp, consume_q, 0,
  2377. read - tmp);
  2378. }
  2379. if (result < VMCI_SUCCESS)
  2380. return result;
  2381. if (update_consumer)
  2382. vmci_q_header_add_consumer_head(produce_q->q_header,
  2383. read, consume_q_size);
  2384. return read;
  2385. }
  2386. /*
  2387. * vmci_qpair_alloc() - Allocates a queue pair.
  2388. * @qpair: Pointer for the new vmci_qp struct.
  2389. * @handle: Handle to track the resource.
  2390. * @produce_qsize: Desired size of the producer queue.
  2391. * @consume_qsize: Desired size of the consumer queue.
  2392. * @peer: ContextID of the peer.
  2393. * @flags: VMCI flags.
  2394. * @priv_flags: VMCI priviledge flags.
  2395. *
  2396. * This is the client interface for allocating the memory for a
  2397. * vmci_qp structure and then attaching to the underlying
  2398. * queue. If an error occurs allocating the memory for the
  2399. * vmci_qp structure no attempt is made to attach. If an
  2400. * error occurs attaching, then the structure is freed.
  2401. */
  2402. int vmci_qpair_alloc(struct vmci_qp **qpair,
  2403. struct vmci_handle *handle,
  2404. u64 produce_qsize,
  2405. u64 consume_qsize,
  2406. u32 peer,
  2407. u32 flags,
  2408. u32 priv_flags)
  2409. {
  2410. struct vmci_qp *my_qpair;
  2411. int retval;
  2412. struct vmci_handle src = VMCI_INVALID_HANDLE;
  2413. struct vmci_handle dst = vmci_make_handle(peer, VMCI_INVALID_ID);
  2414. enum vmci_route route;
  2415. vmci_event_release_cb wakeup_cb;
  2416. void *client_data;
  2417. /*
  2418. * Restrict the size of a queuepair. The device already
  2419. * enforces a limit on the total amount of memory that can be
  2420. * allocated to queuepairs for a guest. However, we try to
  2421. * allocate this memory before we make the queuepair
  2422. * allocation hypercall. On Linux, we allocate each page
  2423. * separately, which means rather than fail, the guest will
  2424. * thrash while it tries to allocate, and will become
  2425. * increasingly unresponsive to the point where it appears to
  2426. * be hung. So we place a limit on the size of an individual
  2427. * queuepair here, and leave the device to enforce the
  2428. * restriction on total queuepair memory. (Note that this
  2429. * doesn't prevent all cases; a user with only this much
  2430. * physical memory could still get into trouble.) The error
  2431. * used by the device is NO_RESOURCES, so use that here too.
  2432. */
  2433. if (produce_qsize + consume_qsize < max(produce_qsize, consume_qsize) ||
  2434. produce_qsize + consume_qsize > VMCI_MAX_GUEST_QP_MEMORY)
  2435. return VMCI_ERROR_NO_RESOURCES;
  2436. retval = vmci_route(&src, &dst, false, &route);
  2437. if (retval < VMCI_SUCCESS)
  2438. route = vmci_guest_code_active() ?
  2439. VMCI_ROUTE_AS_GUEST : VMCI_ROUTE_AS_HOST;
  2440. if (flags & (VMCI_QPFLAG_NONBLOCK | VMCI_QPFLAG_PINNED)) {
  2441. pr_devel("NONBLOCK OR PINNED set");
  2442. return VMCI_ERROR_INVALID_ARGS;
  2443. }
  2444. my_qpair = kzalloc(sizeof(*my_qpair), GFP_KERNEL);
  2445. if (!my_qpair)
  2446. return VMCI_ERROR_NO_MEM;
  2447. my_qpair->produce_q_size = produce_qsize;
  2448. my_qpair->consume_q_size = consume_qsize;
  2449. my_qpair->peer = peer;
  2450. my_qpair->flags = flags;
  2451. my_qpair->priv_flags = priv_flags;
  2452. wakeup_cb = NULL;
  2453. client_data = NULL;
  2454. if (VMCI_ROUTE_AS_HOST == route) {
  2455. my_qpair->guest_endpoint = false;
  2456. if (!(flags & VMCI_QPFLAG_LOCAL)) {
  2457. my_qpair->blocked = 0;
  2458. my_qpair->generation = 0;
  2459. init_waitqueue_head(&my_qpair->event);
  2460. wakeup_cb = qp_wakeup_cb;
  2461. client_data = (void *)my_qpair;
  2462. }
  2463. } else {
  2464. my_qpair->guest_endpoint = true;
  2465. }
  2466. retval = vmci_qp_alloc(handle,
  2467. &my_qpair->produce_q,
  2468. my_qpair->produce_q_size,
  2469. &my_qpair->consume_q,
  2470. my_qpair->consume_q_size,
  2471. my_qpair->peer,
  2472. my_qpair->flags,
  2473. my_qpair->priv_flags,
  2474. my_qpair->guest_endpoint,
  2475. wakeup_cb, client_data);
  2476. if (retval < VMCI_SUCCESS) {
  2477. kfree(my_qpair);
  2478. return retval;
  2479. }
  2480. *qpair = my_qpair;
  2481. my_qpair->handle = *handle;
  2482. return retval;
  2483. }
  2484. EXPORT_SYMBOL_GPL(vmci_qpair_alloc);
  2485. /*
  2486. * vmci_qpair_detach() - Detatches the client from a queue pair.
  2487. * @qpair: Reference of a pointer to the qpair struct.
  2488. *
  2489. * This is the client interface for detaching from a VMCIQPair.
  2490. * Note that this routine will free the memory allocated for the
  2491. * vmci_qp structure too.
  2492. */
  2493. int vmci_qpair_detach(struct vmci_qp **qpair)
  2494. {
  2495. int result;
  2496. struct vmci_qp *old_qpair;
  2497. if (!qpair || !(*qpair))
  2498. return VMCI_ERROR_INVALID_ARGS;
  2499. old_qpair = *qpair;
  2500. result = qp_detatch(old_qpair->handle, old_qpair->guest_endpoint);
  2501. /*
  2502. * The guest can fail to detach for a number of reasons, and
  2503. * if it does so, it will cleanup the entry (if there is one).
  2504. * The host can fail too, but it won't cleanup the entry
  2505. * immediately, it will do that later when the context is
  2506. * freed. Either way, we need to release the qpair struct
  2507. * here; there isn't much the caller can do, and we don't want
  2508. * to leak.
  2509. */
  2510. memset(old_qpair, 0, sizeof(*old_qpair));
  2511. old_qpair->handle = VMCI_INVALID_HANDLE;
  2512. old_qpair->peer = VMCI_INVALID_ID;
  2513. kfree(old_qpair);
  2514. *qpair = NULL;
  2515. return result;
  2516. }
  2517. EXPORT_SYMBOL_GPL(vmci_qpair_detach);
  2518. /*
  2519. * vmci_qpair_get_produce_indexes() - Retrieves the indexes of the producer.
  2520. * @qpair: Pointer to the queue pair struct.
  2521. * @producer_tail: Reference used for storing producer tail index.
  2522. * @consumer_head: Reference used for storing the consumer head index.
  2523. *
  2524. * This is the client interface for getting the current indexes of the
  2525. * QPair from the point of the view of the caller as the producer.
  2526. */
  2527. int vmci_qpair_get_produce_indexes(const struct vmci_qp *qpair,
  2528. u64 *producer_tail,
  2529. u64 *consumer_head)
  2530. {
  2531. struct vmci_queue_header *produce_q_header;
  2532. struct vmci_queue_header *consume_q_header;
  2533. int result;
  2534. if (!qpair)
  2535. return VMCI_ERROR_INVALID_ARGS;
  2536. qp_lock(qpair);
  2537. result =
  2538. qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
  2539. if (result == VMCI_SUCCESS)
  2540. vmci_q_header_get_pointers(produce_q_header, consume_q_header,
  2541. producer_tail, consumer_head);
  2542. qp_unlock(qpair);
  2543. if (result == VMCI_SUCCESS &&
  2544. ((producer_tail && *producer_tail >= qpair->produce_q_size) ||
  2545. (consumer_head && *consumer_head >= qpair->produce_q_size)))
  2546. return VMCI_ERROR_INVALID_SIZE;
  2547. return result;
  2548. }
  2549. EXPORT_SYMBOL_GPL(vmci_qpair_get_produce_indexes);
  2550. /*
  2551. * vmci_qpair_get_consume_indexes() - Retrieves the indexes of the comsumer.
  2552. * @qpair: Pointer to the queue pair struct.
  2553. * @consumer_tail: Reference used for storing consumer tail index.
  2554. * @producer_head: Reference used for storing the producer head index.
  2555. *
  2556. * This is the client interface for getting the current indexes of the
  2557. * QPair from the point of the view of the caller as the consumer.
  2558. */
  2559. int vmci_qpair_get_consume_indexes(const struct vmci_qp *qpair,
  2560. u64 *consumer_tail,
  2561. u64 *producer_head)
  2562. {
  2563. struct vmci_queue_header *produce_q_header;
  2564. struct vmci_queue_header *consume_q_header;
  2565. int result;
  2566. if (!qpair)
  2567. return VMCI_ERROR_INVALID_ARGS;
  2568. qp_lock(qpair);
  2569. result =
  2570. qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
  2571. if (result == VMCI_SUCCESS)
  2572. vmci_q_header_get_pointers(consume_q_header, produce_q_header,
  2573. consumer_tail, producer_head);
  2574. qp_unlock(qpair);
  2575. if (result == VMCI_SUCCESS &&
  2576. ((consumer_tail && *consumer_tail >= qpair->consume_q_size) ||
  2577. (producer_head && *producer_head >= qpair->consume_q_size)))
  2578. return VMCI_ERROR_INVALID_SIZE;
  2579. return result;
  2580. }
  2581. EXPORT_SYMBOL_GPL(vmci_qpair_get_consume_indexes);
  2582. /*
  2583. * vmci_qpair_produce_free_space() - Retrieves free space in producer queue.
  2584. * @qpair: Pointer to the queue pair struct.
  2585. *
  2586. * This is the client interface for getting the amount of free
  2587. * space in the QPair from the point of the view of the caller as
  2588. * the producer which is the common case. Returns < 0 if err, else
  2589. * available bytes into which data can be enqueued if > 0.
  2590. */
  2591. s64 vmci_qpair_produce_free_space(const struct vmci_qp *qpair)
  2592. {
  2593. struct vmci_queue_header *produce_q_header;
  2594. struct vmci_queue_header *consume_q_header;
  2595. s64 result;
  2596. if (!qpair)
  2597. return VMCI_ERROR_INVALID_ARGS;
  2598. qp_lock(qpair);
  2599. result =
  2600. qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
  2601. if (result == VMCI_SUCCESS)
  2602. result = vmci_q_header_free_space(produce_q_header,
  2603. consume_q_header,
  2604. qpair->produce_q_size);
  2605. else
  2606. result = 0;
  2607. qp_unlock(qpair);
  2608. return result;
  2609. }
  2610. EXPORT_SYMBOL_GPL(vmci_qpair_produce_free_space);
  2611. /*
  2612. * vmci_qpair_consume_free_space() - Retrieves free space in consumer queue.
  2613. * @qpair: Pointer to the queue pair struct.
  2614. *
  2615. * This is the client interface for getting the amount of free
  2616. * space in the QPair from the point of the view of the caller as
  2617. * the consumer which is not the common case. Returns < 0 if err, else
  2618. * available bytes into which data can be enqueued if > 0.
  2619. */
  2620. s64 vmci_qpair_consume_free_space(const struct vmci_qp *qpair)
  2621. {
  2622. struct vmci_queue_header *produce_q_header;
  2623. struct vmci_queue_header *consume_q_header;
  2624. s64 result;
  2625. if (!qpair)
  2626. return VMCI_ERROR_INVALID_ARGS;
  2627. qp_lock(qpair);
  2628. result =
  2629. qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
  2630. if (result == VMCI_SUCCESS)
  2631. result = vmci_q_header_free_space(consume_q_header,
  2632. produce_q_header,
  2633. qpair->consume_q_size);
  2634. else
  2635. result = 0;
  2636. qp_unlock(qpair);
  2637. return result;
  2638. }
  2639. EXPORT_SYMBOL_GPL(vmci_qpair_consume_free_space);
  2640. /*
  2641. * vmci_qpair_produce_buf_ready() - Gets bytes ready to read from
  2642. * producer queue.
  2643. * @qpair: Pointer to the queue pair struct.
  2644. *
  2645. * This is the client interface for getting the amount of
  2646. * enqueued data in the QPair from the point of the view of the
  2647. * caller as the producer which is not the common case. Returns < 0 if err,
  2648. * else available bytes that may be read.
  2649. */
  2650. s64 vmci_qpair_produce_buf_ready(const struct vmci_qp *qpair)
  2651. {
  2652. struct vmci_queue_header *produce_q_header;
  2653. struct vmci_queue_header *consume_q_header;
  2654. s64 result;
  2655. if (!qpair)
  2656. return VMCI_ERROR_INVALID_ARGS;
  2657. qp_lock(qpair);
  2658. result =
  2659. qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
  2660. if (result == VMCI_SUCCESS)
  2661. result = vmci_q_header_buf_ready(produce_q_header,
  2662. consume_q_header,
  2663. qpair->produce_q_size);
  2664. else
  2665. result = 0;
  2666. qp_unlock(qpair);
  2667. return result;
  2668. }
  2669. EXPORT_SYMBOL_GPL(vmci_qpair_produce_buf_ready);
  2670. /*
  2671. * vmci_qpair_consume_buf_ready() - Gets bytes ready to read from
  2672. * consumer queue.
  2673. * @qpair: Pointer to the queue pair struct.
  2674. *
  2675. * This is the client interface for getting the amount of
  2676. * enqueued data in the QPair from the point of the view of the
  2677. * caller as the consumer which is the normal case. Returns < 0 if err,
  2678. * else available bytes that may be read.
  2679. */
  2680. s64 vmci_qpair_consume_buf_ready(const struct vmci_qp *qpair)
  2681. {
  2682. struct vmci_queue_header *produce_q_header;
  2683. struct vmci_queue_header *consume_q_header;
  2684. s64 result;
  2685. if (!qpair)
  2686. return VMCI_ERROR_INVALID_ARGS;
  2687. qp_lock(qpair);
  2688. result =
  2689. qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
  2690. if (result == VMCI_SUCCESS)
  2691. result = vmci_q_header_buf_ready(consume_q_header,
  2692. produce_q_header,
  2693. qpair->consume_q_size);
  2694. else
  2695. result = 0;
  2696. qp_unlock(qpair);
  2697. return result;
  2698. }
  2699. EXPORT_SYMBOL_GPL(vmci_qpair_consume_buf_ready);
  2700. /*
  2701. * vmci_qpair_enqueue() - Throw data on the queue.
  2702. * @qpair: Pointer to the queue pair struct.
  2703. * @buf: Pointer to buffer containing data
  2704. * @buf_size: Length of buffer.
  2705. * @buf_type: Buffer type (Unused).
  2706. *
  2707. * This is the client interface for enqueueing data into the queue.
  2708. * Returns number of bytes enqueued or < 0 on error.
  2709. */
  2710. ssize_t vmci_qpair_enqueue(struct vmci_qp *qpair,
  2711. const void *buf,
  2712. size_t buf_size,
  2713. int buf_type)
  2714. {
  2715. ssize_t result;
  2716. if (!qpair || !buf)
  2717. return VMCI_ERROR_INVALID_ARGS;
  2718. qp_lock(qpair);
  2719. do {
  2720. result = qp_enqueue_locked(qpair->produce_q,
  2721. qpair->consume_q,
  2722. qpair->produce_q_size,
  2723. buf, buf_size,
  2724. qp_memcpy_to_queue);
  2725. if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
  2726. !qp_wait_for_ready_queue(qpair))
  2727. result = VMCI_ERROR_WOULD_BLOCK;
  2728. } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
  2729. qp_unlock(qpair);
  2730. return result;
  2731. }
  2732. EXPORT_SYMBOL_GPL(vmci_qpair_enqueue);
  2733. /*
  2734. * vmci_qpair_dequeue() - Get data from the queue.
  2735. * @qpair: Pointer to the queue pair struct.
  2736. * @buf: Pointer to buffer for the data
  2737. * @buf_size: Length of buffer.
  2738. * @buf_type: Buffer type (Unused).
  2739. *
  2740. * This is the client interface for dequeueing data from the queue.
  2741. * Returns number of bytes dequeued or < 0 on error.
  2742. */
  2743. ssize_t vmci_qpair_dequeue(struct vmci_qp *qpair,
  2744. void *buf,
  2745. size_t buf_size,
  2746. int buf_type)
  2747. {
  2748. ssize_t result;
  2749. if (!qpair || !buf)
  2750. return VMCI_ERROR_INVALID_ARGS;
  2751. qp_lock(qpair);
  2752. do {
  2753. result = qp_dequeue_locked(qpair->produce_q,
  2754. qpair->consume_q,
  2755. qpair->consume_q_size,
  2756. buf, buf_size,
  2757. qp_memcpy_from_queue, true);
  2758. if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
  2759. !qp_wait_for_ready_queue(qpair))
  2760. result = VMCI_ERROR_WOULD_BLOCK;
  2761. } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
  2762. qp_unlock(qpair);
  2763. return result;
  2764. }
  2765. EXPORT_SYMBOL_GPL(vmci_qpair_dequeue);
  2766. /*
  2767. * vmci_qpair_peek() - Peek at the data in the queue.
  2768. * @qpair: Pointer to the queue pair struct.
  2769. * @buf: Pointer to buffer for the data
  2770. * @buf_size: Length of buffer.
  2771. * @buf_type: Buffer type (Unused on Linux).
  2772. *
  2773. * This is the client interface for peeking into a queue. (I.e.,
  2774. * copy data from the queue without updating the head pointer.)
  2775. * Returns number of bytes dequeued or < 0 on error.
  2776. */
  2777. ssize_t vmci_qpair_peek(struct vmci_qp *qpair,
  2778. void *buf,
  2779. size_t buf_size,
  2780. int buf_type)
  2781. {
  2782. ssize_t result;
  2783. if (!qpair || !buf)
  2784. return VMCI_ERROR_INVALID_ARGS;
  2785. qp_lock(qpair);
  2786. do {
  2787. result = qp_dequeue_locked(qpair->produce_q,
  2788. qpair->consume_q,
  2789. qpair->consume_q_size,
  2790. buf, buf_size,
  2791. qp_memcpy_from_queue, false);
  2792. if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
  2793. !qp_wait_for_ready_queue(qpair))
  2794. result = VMCI_ERROR_WOULD_BLOCK;
  2795. } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
  2796. qp_unlock(qpair);
  2797. return result;
  2798. }
  2799. EXPORT_SYMBOL_GPL(vmci_qpair_peek);
  2800. /*
  2801. * vmci_qpair_enquev() - Throw data on the queue using iov.
  2802. * @qpair: Pointer to the queue pair struct.
  2803. * @iov: Pointer to buffer containing data
  2804. * @iov_size: Length of buffer.
  2805. * @buf_type: Buffer type (Unused).
  2806. *
  2807. * This is the client interface for enqueueing data into the queue.
  2808. * This function uses IO vectors to handle the work. Returns number
  2809. * of bytes enqueued or < 0 on error.
  2810. */
  2811. ssize_t vmci_qpair_enquev(struct vmci_qp *qpair,
  2812. struct msghdr *msg,
  2813. size_t iov_size,
  2814. int buf_type)
  2815. {
  2816. ssize_t result;
  2817. if (!qpair)
  2818. return VMCI_ERROR_INVALID_ARGS;
  2819. qp_lock(qpair);
  2820. do {
  2821. result = qp_enqueue_locked(qpair->produce_q,
  2822. qpair->consume_q,
  2823. qpair->produce_q_size,
  2824. msg, iov_size,
  2825. qp_memcpy_to_queue_iov);
  2826. if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
  2827. !qp_wait_for_ready_queue(qpair))
  2828. result = VMCI_ERROR_WOULD_BLOCK;
  2829. } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
  2830. qp_unlock(qpair);
  2831. return result;
  2832. }
  2833. EXPORT_SYMBOL_GPL(vmci_qpair_enquev);
  2834. /*
  2835. * vmci_qpair_dequev() - Get data from the queue using iov.
  2836. * @qpair: Pointer to the queue pair struct.
  2837. * @iov: Pointer to buffer for the data
  2838. * @iov_size: Length of buffer.
  2839. * @buf_type: Buffer type (Unused).
  2840. *
  2841. * This is the client interface for dequeueing data from the queue.
  2842. * This function uses IO vectors to handle the work. Returns number
  2843. * of bytes dequeued or < 0 on error.
  2844. */
  2845. ssize_t vmci_qpair_dequev(struct vmci_qp *qpair,
  2846. struct msghdr *msg,
  2847. size_t iov_size,
  2848. int buf_type)
  2849. {
  2850. ssize_t result;
  2851. if (!qpair)
  2852. return VMCI_ERROR_INVALID_ARGS;
  2853. qp_lock(qpair);
  2854. do {
  2855. result = qp_dequeue_locked(qpair->produce_q,
  2856. qpair->consume_q,
  2857. qpair->consume_q_size,
  2858. msg, iov_size,
  2859. qp_memcpy_from_queue_iov,
  2860. true);
  2861. if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
  2862. !qp_wait_for_ready_queue(qpair))
  2863. result = VMCI_ERROR_WOULD_BLOCK;
  2864. } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
  2865. qp_unlock(qpair);
  2866. return result;
  2867. }
  2868. EXPORT_SYMBOL_GPL(vmci_qpair_dequev);
  2869. /*
  2870. * vmci_qpair_peekv() - Peek at the data in the queue using iov.
  2871. * @qpair: Pointer to the queue pair struct.
  2872. * @iov: Pointer to buffer for the data
  2873. * @iov_size: Length of buffer.
  2874. * @buf_type: Buffer type (Unused on Linux).
  2875. *
  2876. * This is the client interface for peeking into a queue. (I.e.,
  2877. * copy data from the queue without updating the head pointer.)
  2878. * This function uses IO vectors to handle the work. Returns number
  2879. * of bytes peeked or < 0 on error.
  2880. */
  2881. ssize_t vmci_qpair_peekv(struct vmci_qp *qpair,
  2882. struct msghdr *msg,
  2883. size_t iov_size,
  2884. int buf_type)
  2885. {
  2886. ssize_t result;
  2887. if (!qpair)
  2888. return VMCI_ERROR_INVALID_ARGS;
  2889. qp_lock(qpair);
  2890. do {
  2891. result = qp_dequeue_locked(qpair->produce_q,
  2892. qpair->consume_q,
  2893. qpair->consume_q_size,
  2894. msg, iov_size,
  2895. qp_memcpy_from_queue_iov,
  2896. false);
  2897. if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
  2898. !qp_wait_for_ready_queue(qpair))
  2899. result = VMCI_ERROR_WOULD_BLOCK;
  2900. } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
  2901. qp_unlock(qpair);
  2902. return result;
  2903. }
  2904. EXPORT_SYMBOL_GPL(vmci_qpair_peekv);