vmci_queue_pair.c 97 KB

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