vmci_queue_pair.c 95 KB

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