vmci_queue_pair.c 95 KB

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