vmci_queue_pair.c 95 KB

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