vmci_queue_pair.c 98 KB

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