vmci_queue_pair.c 95 KB

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