vmci_queue_pair.c 90 KB

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