vmci_queue_pair.c 93 KB

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