vmci_queue_pair.c 98 KB

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