12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356 |
- #include <linux/vmw_vmci_defs.h>
- #include <linux/vmw_vmci_api.h>
- #include <linux/highmem.h>
- #include <linux/kernel.h>
- #include <linux/mm.h>
- #include <linux/module.h>
- #include <linux/mutex.h>
- #include <linux/pagemap.h>
- #include <linux/pci.h>
- #include <linux/sched.h>
- #include <linux/slab.h>
- #include <linux/uio.h>
- #include <linux/wait.h>
- #include <linux/vmalloc.h>
- #include <linux/skbuff.h>
- #include "vmci_handle_array.h"
- #include "vmci_queue_pair.h"
- #include "vmci_datagram.h"
- #include "vmci_resource.h"
- #include "vmci_context.h"
- #include "vmci_driver.h"
- #include "vmci_event.h"
- #include "vmci_route.h"
- typedef int vmci_memcpy_to_queue_func(struct vmci_queue *queue,
- u64 queue_offset, const void *src,
- size_t src_offset, size_t size);
- typedef int vmci_memcpy_from_queue_func(void *dest, size_t dest_offset,
- const struct vmci_queue *queue,
- u64 queue_offset, size_t size);
- struct vmci_queue_kern_if {
- struct mutex __mutex;
- struct mutex *mutex;
- size_t num_pages;
- bool host;
- union {
- struct {
- dma_addr_t *pas;
- void **vas;
- } g;
- struct {
- struct page **page;
- struct page **header_page;
- } h;
- } u;
- };
- struct vmci_qp {
- struct vmci_handle handle;
- struct vmci_queue *produce_q;
- struct vmci_queue *consume_q;
- u64 produce_q_size;
- u64 consume_q_size;
- u32 peer;
- u32 flags;
- u32 priv_flags;
- bool guest_endpoint;
- unsigned int blocked;
- unsigned int generation;
- wait_queue_head_t event;
- };
- enum qp_broker_state {
- VMCIQPB_NEW,
- VMCIQPB_CREATED_NO_MEM,
- VMCIQPB_CREATED_MEM,
- VMCIQPB_ATTACHED_NO_MEM,
- VMCIQPB_ATTACHED_MEM,
- VMCIQPB_SHUTDOWN_NO_MEM,
- VMCIQPB_SHUTDOWN_MEM,
- VMCIQPB_GONE
- };
- #define QPBROKERSTATE_HAS_MEM(_qpb) (_qpb->state == VMCIQPB_CREATED_MEM || \
- _qpb->state == VMCIQPB_ATTACHED_MEM || \
- _qpb->state == VMCIQPB_SHUTDOWN_MEM)
- struct qp_entry {
- struct list_head list_item;
- struct vmci_handle handle;
- u32 peer;
- u32 flags;
- u64 produce_size;
- u64 consume_size;
- u32 ref_count;
- };
- struct qp_broker_entry {
- struct vmci_resource resource;
- struct qp_entry qp;
- u32 create_id;
- u32 attach_id;
- enum qp_broker_state state;
- bool require_trusted_attach;
- bool created_by_trusted;
- bool vmci_page_files;
- struct vmci_queue *produce_q;
- struct vmci_queue *consume_q;
- struct vmci_queue_header saved_produce_q;
- struct vmci_queue_header saved_consume_q;
- vmci_event_release_cb wakeup_cb;
- void *client_data;
- void *local_mem;
- };
- struct qp_guest_endpoint {
- struct vmci_resource resource;
- struct qp_entry qp;
- u64 num_ppns;
- void *produce_q;
- void *consume_q;
- struct ppn_set ppn_set;
- };
- struct qp_list {
- struct list_head head;
- struct mutex mutex;
- };
- static struct qp_list qp_broker_list = {
- .head = LIST_HEAD_INIT(qp_broker_list.head),
- .mutex = __MUTEX_INITIALIZER(qp_broker_list.mutex),
- };
- static struct qp_list qp_guest_endpoints = {
- .head = LIST_HEAD_INIT(qp_guest_endpoints.head),
- .mutex = __MUTEX_INITIALIZER(qp_guest_endpoints.mutex),
- };
- #define INVALID_VMCI_GUEST_MEM_ID 0
- #define QPE_NUM_PAGES(_QPE) ((u32) \
- (DIV_ROUND_UP(_QPE.produce_size, PAGE_SIZE) + \
- DIV_ROUND_UP(_QPE.consume_size, PAGE_SIZE) + 2))
- static void qp_free_queue(void *q, u64 size)
- {
- struct vmci_queue *queue = q;
- if (queue) {
- u64 i;
-
- for (i = 0; i < DIV_ROUND_UP(size, PAGE_SIZE) + 1; i++) {
- dma_free_coherent(&vmci_pdev->dev, PAGE_SIZE,
- queue->kernel_if->u.g.vas[i],
- queue->kernel_if->u.g.pas[i]);
- }
- vfree(queue);
- }
- }
- static void *qp_alloc_queue(u64 size, u32 flags)
- {
- u64 i;
- struct vmci_queue *queue;
- size_t pas_size;
- size_t vas_size;
- size_t queue_size = sizeof(*queue) + sizeof(*queue->kernel_if);
- const u64 num_pages = DIV_ROUND_UP(size, PAGE_SIZE) + 1;
- if (num_pages >
- (SIZE_MAX - queue_size) /
- (sizeof(*queue->kernel_if->u.g.pas) +
- sizeof(*queue->kernel_if->u.g.vas)))
- return NULL;
- pas_size = num_pages * sizeof(*queue->kernel_if->u.g.pas);
- vas_size = num_pages * sizeof(*queue->kernel_if->u.g.vas);
- queue_size += pas_size + vas_size;
- queue = vmalloc(queue_size);
- if (!queue)
- return NULL;
- queue->q_header = NULL;
- queue->saved_header = NULL;
- queue->kernel_if = (struct vmci_queue_kern_if *)(queue + 1);
- queue->kernel_if->mutex = NULL;
- queue->kernel_if->num_pages = num_pages;
- queue->kernel_if->u.g.pas = (dma_addr_t *)(queue->kernel_if + 1);
- queue->kernel_if->u.g.vas =
- (void **)((u8 *)queue->kernel_if->u.g.pas + pas_size);
- queue->kernel_if->host = false;
- for (i = 0; i < num_pages; i++) {
- queue->kernel_if->u.g.vas[i] =
- dma_alloc_coherent(&vmci_pdev->dev, PAGE_SIZE,
- &queue->kernel_if->u.g.pas[i],
- GFP_KERNEL);
- if (!queue->kernel_if->u.g.vas[i]) {
-
- qp_free_queue(queue, i * PAGE_SIZE);
- return NULL;
- }
- }
-
- queue->q_header = queue->kernel_if->u.g.vas[0];
- return queue;
- }
- static int __qp_memcpy_to_queue(struct vmci_queue *queue,
- u64 queue_offset,
- const void *src,
- size_t size,
- bool is_iovec)
- {
- struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
- size_t bytes_copied = 0;
- while (bytes_copied < size) {
- const u64 page_index =
- (queue_offset + bytes_copied) / PAGE_SIZE;
- const size_t page_offset =
- (queue_offset + bytes_copied) & (PAGE_SIZE - 1);
- void *va;
- size_t to_copy;
- if (kernel_if->host)
- va = kmap(kernel_if->u.h.page[page_index]);
- else
- va = kernel_if->u.g.vas[page_index + 1];
-
- if (size - bytes_copied > PAGE_SIZE - page_offset)
-
- to_copy = PAGE_SIZE - page_offset;
- else
- to_copy = size - bytes_copied;
- if (is_iovec) {
- struct msghdr *msg = (struct msghdr *)src;
- int err;
-
- err = memcpy_from_msg((u8 *)va + page_offset,
- msg, to_copy);
- if (err != 0) {
- if (kernel_if->host)
- kunmap(kernel_if->u.h.page[page_index]);
- return VMCI_ERROR_INVALID_ARGS;
- }
- } else {
- memcpy((u8 *)va + page_offset,
- (u8 *)src + bytes_copied, to_copy);
- }
- bytes_copied += to_copy;
- if (kernel_if->host)
- kunmap(kernel_if->u.h.page[page_index]);
- }
- return VMCI_SUCCESS;
- }
- static int __qp_memcpy_from_queue(void *dest,
- const struct vmci_queue *queue,
- u64 queue_offset,
- size_t size,
- bool is_iovec)
- {
- struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
- size_t bytes_copied = 0;
- while (bytes_copied < size) {
- const u64 page_index =
- (queue_offset + bytes_copied) / PAGE_SIZE;
- const size_t page_offset =
- (queue_offset + bytes_copied) & (PAGE_SIZE - 1);
- void *va;
- size_t to_copy;
- if (kernel_if->host)
- va = kmap(kernel_if->u.h.page[page_index]);
- else
- va = kernel_if->u.g.vas[page_index + 1];
-
- if (size - bytes_copied > PAGE_SIZE - page_offset)
-
- to_copy = PAGE_SIZE - page_offset;
- else
- to_copy = size - bytes_copied;
- if (is_iovec) {
- struct msghdr *msg = dest;
- int err;
-
- err = memcpy_to_msg(msg, (u8 *)va + page_offset,
- to_copy);
- if (err != 0) {
- if (kernel_if->host)
- kunmap(kernel_if->u.h.page[page_index]);
- return VMCI_ERROR_INVALID_ARGS;
- }
- } else {
- memcpy((u8 *)dest + bytes_copied,
- (u8 *)va + page_offset, to_copy);
- }
- bytes_copied += to_copy;
- if (kernel_if->host)
- kunmap(kernel_if->u.h.page[page_index]);
- }
- return VMCI_SUCCESS;
- }
- static int qp_alloc_ppn_set(void *prod_q,
- u64 num_produce_pages,
- void *cons_q,
- u64 num_consume_pages, struct ppn_set *ppn_set)
- {
- u32 *produce_ppns;
- u32 *consume_ppns;
- struct vmci_queue *produce_q = prod_q;
- struct vmci_queue *consume_q = cons_q;
- u64 i;
- if (!produce_q || !num_produce_pages || !consume_q ||
- !num_consume_pages || !ppn_set)
- return VMCI_ERROR_INVALID_ARGS;
- if (ppn_set->initialized)
- return VMCI_ERROR_ALREADY_EXISTS;
- produce_ppns =
- kmalloc(num_produce_pages * sizeof(*produce_ppns), GFP_KERNEL);
- if (!produce_ppns)
- return VMCI_ERROR_NO_MEM;
- consume_ppns =
- kmalloc(num_consume_pages * sizeof(*consume_ppns), GFP_KERNEL);
- if (!consume_ppns) {
- kfree(produce_ppns);
- return VMCI_ERROR_NO_MEM;
- }
- for (i = 0; i < num_produce_pages; i++) {
- unsigned long pfn;
- produce_ppns[i] =
- produce_q->kernel_if->u.g.pas[i] >> PAGE_SHIFT;
- pfn = produce_ppns[i];
-
- if (sizeof(pfn) > sizeof(*produce_ppns)
- && pfn != produce_ppns[i])
- goto ppn_error;
- }
- for (i = 0; i < num_consume_pages; i++) {
- unsigned long pfn;
- consume_ppns[i] =
- consume_q->kernel_if->u.g.pas[i] >> PAGE_SHIFT;
- pfn = consume_ppns[i];
-
- if (sizeof(pfn) > sizeof(*consume_ppns)
- && pfn != consume_ppns[i])
- goto ppn_error;
- }
- ppn_set->num_produce_pages = num_produce_pages;
- ppn_set->num_consume_pages = num_consume_pages;
- ppn_set->produce_ppns = produce_ppns;
- ppn_set->consume_ppns = consume_ppns;
- ppn_set->initialized = true;
- return VMCI_SUCCESS;
- ppn_error:
- kfree(produce_ppns);
- kfree(consume_ppns);
- return VMCI_ERROR_INVALID_ARGS;
- }
- static void qp_free_ppn_set(struct ppn_set *ppn_set)
- {
- if (ppn_set->initialized) {
-
- kfree(ppn_set->produce_ppns);
- kfree(ppn_set->consume_ppns);
- }
- memset(ppn_set, 0, sizeof(*ppn_set));
- }
- static int qp_populate_ppn_set(u8 *call_buf, const struct ppn_set *ppn_set)
- {
- memcpy(call_buf, ppn_set->produce_ppns,
- ppn_set->num_produce_pages * sizeof(*ppn_set->produce_ppns));
- memcpy(call_buf +
- ppn_set->num_produce_pages * sizeof(*ppn_set->produce_ppns),
- ppn_set->consume_ppns,
- ppn_set->num_consume_pages * sizeof(*ppn_set->consume_ppns));
- return VMCI_SUCCESS;
- }
- static int qp_memcpy_to_queue(struct vmci_queue *queue,
- u64 queue_offset,
- const void *src, size_t src_offset, size_t size)
- {
- return __qp_memcpy_to_queue(queue, queue_offset,
- (u8 *)src + src_offset, size, false);
- }
- static int qp_memcpy_from_queue(void *dest,
- size_t dest_offset,
- const struct vmci_queue *queue,
- u64 queue_offset, size_t size)
- {
- return __qp_memcpy_from_queue((u8 *)dest + dest_offset,
- queue, queue_offset, size, false);
- }
- static int qp_memcpy_to_queue_iov(struct vmci_queue *queue,
- u64 queue_offset,
- const void *msg,
- size_t src_offset, size_t size)
- {
-
- return __qp_memcpy_to_queue(queue, queue_offset, msg, size, true);
- }
- static int qp_memcpy_from_queue_iov(void *dest,
- size_t dest_offset,
- const struct vmci_queue *queue,
- u64 queue_offset, size_t size)
- {
-
- return __qp_memcpy_from_queue(dest, queue, queue_offset, size, true);
- }
- static struct vmci_queue *qp_host_alloc_queue(u64 size)
- {
- struct vmci_queue *queue;
- size_t queue_page_size;
- const u64 num_pages = DIV_ROUND_UP(size, PAGE_SIZE) + 1;
- const size_t queue_size = sizeof(*queue) + sizeof(*(queue->kernel_if));
- if (num_pages > (SIZE_MAX - queue_size) /
- sizeof(*queue->kernel_if->u.h.page))
- return NULL;
- queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
- queue = kzalloc(queue_size + queue_page_size, GFP_KERNEL);
- if (queue) {
- queue->q_header = NULL;
- queue->saved_header = NULL;
- queue->kernel_if = (struct vmci_queue_kern_if *)(queue + 1);
- queue->kernel_if->host = true;
- queue->kernel_if->mutex = NULL;
- queue->kernel_if->num_pages = num_pages;
- queue->kernel_if->u.h.header_page =
- (struct page **)((u8 *)queue + queue_size);
- queue->kernel_if->u.h.page =
- &queue->kernel_if->u.h.header_page[1];
- }
- return queue;
- }
- static void qp_host_free_queue(struct vmci_queue *queue, u64 queue_size)
- {
- kfree(queue);
- }
- static void qp_init_queue_mutex(struct vmci_queue *produce_q,
- struct vmci_queue *consume_q)
- {
-
- if (produce_q->kernel_if->host) {
- produce_q->kernel_if->mutex = &produce_q->kernel_if->__mutex;
- consume_q->kernel_if->mutex = &produce_q->kernel_if->__mutex;
- mutex_init(produce_q->kernel_if->mutex);
- }
- }
- static void qp_cleanup_queue_mutex(struct vmci_queue *produce_q,
- struct vmci_queue *consume_q)
- {
- if (produce_q->kernel_if->host) {
- produce_q->kernel_if->mutex = NULL;
- consume_q->kernel_if->mutex = NULL;
- }
- }
- static void qp_acquire_queue_mutex(struct vmci_queue *queue)
- {
- if (queue->kernel_if->host)
- mutex_lock(queue->kernel_if->mutex);
- }
- static void qp_release_queue_mutex(struct vmci_queue *queue)
- {
- if (queue->kernel_if->host)
- mutex_unlock(queue->kernel_if->mutex);
- }
- static void qp_release_pages(struct page **pages,
- u64 num_pages, bool dirty)
- {
- int i;
- for (i = 0; i < num_pages; i++) {
- if (dirty)
- set_page_dirty(pages[i]);
- page_cache_release(pages[i]);
- pages[i] = NULL;
- }
- }
- static int qp_host_get_user_memory(u64 produce_uva,
- u64 consume_uva,
- struct vmci_queue *produce_q,
- struct vmci_queue *consume_q)
- {
- int retval;
- int err = VMCI_SUCCESS;
- retval = get_user_pages_fast((uintptr_t) produce_uva,
- produce_q->kernel_if->num_pages, 1,
- produce_q->kernel_if->u.h.header_page);
- if (retval < produce_q->kernel_if->num_pages) {
- pr_debug("get_user_pages_fast(produce) failed (retval=%d)",
- retval);
- qp_release_pages(produce_q->kernel_if->u.h.header_page,
- retval, false);
- err = VMCI_ERROR_NO_MEM;
- goto out;
- }
- retval = get_user_pages_fast((uintptr_t) consume_uva,
- consume_q->kernel_if->num_pages, 1,
- consume_q->kernel_if->u.h.header_page);
- if (retval < consume_q->kernel_if->num_pages) {
- pr_debug("get_user_pages_fast(consume) failed (retval=%d)",
- retval);
- qp_release_pages(consume_q->kernel_if->u.h.header_page,
- retval, false);
- qp_release_pages(produce_q->kernel_if->u.h.header_page,
- produce_q->kernel_if->num_pages, false);
- err = VMCI_ERROR_NO_MEM;
- }
- out:
- return err;
- }
- static int qp_host_register_user_memory(struct vmci_qp_page_store *page_store,
- struct vmci_queue *produce_q,
- struct vmci_queue *consume_q)
- {
- u64 produce_uva;
- u64 consume_uva;
-
- produce_uva = page_store->pages;
- consume_uva = page_store->pages +
- produce_q->kernel_if->num_pages * PAGE_SIZE;
- return qp_host_get_user_memory(produce_uva, consume_uva, produce_q,
- consume_q);
- }
- static void qp_host_unregister_user_memory(struct vmci_queue *produce_q,
- struct vmci_queue *consume_q)
- {
- qp_release_pages(produce_q->kernel_if->u.h.header_page,
- produce_q->kernel_if->num_pages, true);
- memset(produce_q->kernel_if->u.h.header_page, 0,
- sizeof(*produce_q->kernel_if->u.h.header_page) *
- produce_q->kernel_if->num_pages);
- qp_release_pages(consume_q->kernel_if->u.h.header_page,
- consume_q->kernel_if->num_pages, true);
- memset(consume_q->kernel_if->u.h.header_page, 0,
- sizeof(*consume_q->kernel_if->u.h.header_page) *
- consume_q->kernel_if->num_pages);
- }
- static int qp_host_map_queues(struct vmci_queue *produce_q,
- struct vmci_queue *consume_q)
- {
- int result;
- if (!produce_q->q_header || !consume_q->q_header) {
- struct page *headers[2];
- if (produce_q->q_header != consume_q->q_header)
- return VMCI_ERROR_QUEUEPAIR_MISMATCH;
- if (produce_q->kernel_if->u.h.header_page == NULL ||
- *produce_q->kernel_if->u.h.header_page == NULL)
- return VMCI_ERROR_UNAVAILABLE;
- headers[0] = *produce_q->kernel_if->u.h.header_page;
- headers[1] = *consume_q->kernel_if->u.h.header_page;
- produce_q->q_header = vmap(headers, 2, VM_MAP, PAGE_KERNEL);
- if (produce_q->q_header != NULL) {
- consume_q->q_header =
- (struct vmci_queue_header *)((u8 *)
- produce_q->q_header +
- PAGE_SIZE);
- result = VMCI_SUCCESS;
- } else {
- pr_warn("vmap failed\n");
- result = VMCI_ERROR_NO_MEM;
- }
- } else {
- result = VMCI_SUCCESS;
- }
- return result;
- }
- static int qp_host_unmap_queues(u32 gid,
- struct vmci_queue *produce_q,
- struct vmci_queue *consume_q)
- {
- if (produce_q->q_header) {
- if (produce_q->q_header < consume_q->q_header)
- vunmap(produce_q->q_header);
- else
- vunmap(consume_q->q_header);
- produce_q->q_header = NULL;
- consume_q->q_header = NULL;
- }
- return VMCI_SUCCESS;
- }
- static struct qp_entry *qp_list_find(struct qp_list *qp_list,
- struct vmci_handle handle)
- {
- struct qp_entry *entry;
- if (vmci_handle_is_invalid(handle))
- return NULL;
- list_for_each_entry(entry, &qp_list->head, list_item) {
- if (vmci_handle_is_equal(entry->handle, handle))
- return entry;
- }
- return NULL;
- }
- static struct qp_guest_endpoint *
- qp_guest_handle_to_entry(struct vmci_handle handle)
- {
- struct qp_guest_endpoint *entry;
- struct qp_entry *qp = qp_list_find(&qp_guest_endpoints, handle);
- entry = qp ? container_of(
- qp, struct qp_guest_endpoint, qp) : NULL;
- return entry;
- }
- static struct qp_broker_entry *
- qp_broker_handle_to_entry(struct vmci_handle handle)
- {
- struct qp_broker_entry *entry;
- struct qp_entry *qp = qp_list_find(&qp_broker_list, handle);
- entry = qp ? container_of(
- qp, struct qp_broker_entry, qp) : NULL;
- return entry;
- }
- static int qp_notify_peer_local(bool attach, struct vmci_handle handle)
- {
- u32 context_id = vmci_get_context_id();
- struct vmci_event_qp ev;
- ev.msg.hdr.dst = vmci_make_handle(context_id, VMCI_EVENT_HANDLER);
- ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
- VMCI_CONTEXT_RESOURCE_ID);
- ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
- ev.msg.event_data.event =
- attach ? VMCI_EVENT_QP_PEER_ATTACH : VMCI_EVENT_QP_PEER_DETACH;
- ev.payload.peer_id = context_id;
- ev.payload.handle = handle;
- return vmci_event_dispatch(&ev.msg.hdr);
- }
- static struct qp_guest_endpoint *
- qp_guest_endpoint_create(struct vmci_handle handle,
- u32 peer,
- u32 flags,
- u64 produce_size,
- u64 consume_size,
- void *produce_q,
- void *consume_q)
- {
- int result;
- struct qp_guest_endpoint *entry;
-
- const u64 num_ppns = DIV_ROUND_UP(produce_size, PAGE_SIZE) +
- DIV_ROUND_UP(consume_size, PAGE_SIZE) + 2;
- if (vmci_handle_is_invalid(handle)) {
- u32 context_id = vmci_get_context_id();
- handle = vmci_make_handle(context_id, VMCI_INVALID_ID);
- }
- entry = kzalloc(sizeof(*entry), GFP_KERNEL);
- if (entry) {
- entry->qp.peer = peer;
- entry->qp.flags = flags;
- entry->qp.produce_size = produce_size;
- entry->qp.consume_size = consume_size;
- entry->qp.ref_count = 0;
- entry->num_ppns = num_ppns;
- entry->produce_q = produce_q;
- entry->consume_q = consume_q;
- INIT_LIST_HEAD(&entry->qp.list_item);
-
- result = vmci_resource_add(&entry->resource,
- VMCI_RESOURCE_TYPE_QPAIR_GUEST,
- handle);
- entry->qp.handle = vmci_resource_handle(&entry->resource);
- if ((result != VMCI_SUCCESS) ||
- qp_list_find(&qp_guest_endpoints, entry->qp.handle)) {
- pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
- handle.context, handle.resource, result);
- kfree(entry);
- entry = NULL;
- }
- }
- return entry;
- }
- static void qp_guest_endpoint_destroy(struct qp_guest_endpoint *entry)
- {
- qp_free_ppn_set(&entry->ppn_set);
- qp_cleanup_queue_mutex(entry->produce_q, entry->consume_q);
- qp_free_queue(entry->produce_q, entry->qp.produce_size);
- qp_free_queue(entry->consume_q, entry->qp.consume_size);
-
- vmci_resource_remove(&entry->resource);
- kfree(entry);
- }
- static int qp_alloc_hypercall(const struct qp_guest_endpoint *entry)
- {
- struct vmci_qp_alloc_msg *alloc_msg;
- size_t msg_size;
- int result;
- if (!entry || entry->num_ppns <= 2)
- return VMCI_ERROR_INVALID_ARGS;
- msg_size = sizeof(*alloc_msg) +
- (size_t) entry->num_ppns * sizeof(u32);
- alloc_msg = kmalloc(msg_size, GFP_KERNEL);
- if (!alloc_msg)
- return VMCI_ERROR_NO_MEM;
- alloc_msg->hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
- VMCI_QUEUEPAIR_ALLOC);
- alloc_msg->hdr.src = VMCI_ANON_SRC_HANDLE;
- alloc_msg->hdr.payload_size = msg_size - VMCI_DG_HEADERSIZE;
- alloc_msg->handle = entry->qp.handle;
- alloc_msg->peer = entry->qp.peer;
- alloc_msg->flags = entry->qp.flags;
- alloc_msg->produce_size = entry->qp.produce_size;
- alloc_msg->consume_size = entry->qp.consume_size;
- alloc_msg->num_ppns = entry->num_ppns;
- result = qp_populate_ppn_set((u8 *)alloc_msg + sizeof(*alloc_msg),
- &entry->ppn_set);
- if (result == VMCI_SUCCESS)
- result = vmci_send_datagram(&alloc_msg->hdr);
- kfree(alloc_msg);
- return result;
- }
- static int qp_detatch_hypercall(struct vmci_handle handle)
- {
- struct vmci_qp_detach_msg detach_msg;
- detach_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
- VMCI_QUEUEPAIR_DETACH);
- detach_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
- detach_msg.hdr.payload_size = sizeof(handle);
- detach_msg.handle = handle;
- return vmci_send_datagram(&detach_msg.hdr);
- }
- static void qp_list_add_entry(struct qp_list *qp_list, struct qp_entry *entry)
- {
- if (entry)
- list_add(&entry->list_item, &qp_list->head);
- }
- static void qp_list_remove_entry(struct qp_list *qp_list,
- struct qp_entry *entry)
- {
- if (entry)
- list_del(&entry->list_item);
- }
- static int qp_detatch_guest_work(struct vmci_handle handle)
- {
- int result;
- struct qp_guest_endpoint *entry;
- u32 ref_count = ~0;
- mutex_lock(&qp_guest_endpoints.mutex);
- entry = qp_guest_handle_to_entry(handle);
- if (!entry) {
- mutex_unlock(&qp_guest_endpoints.mutex);
- return VMCI_ERROR_NOT_FOUND;
- }
- if (entry->qp.flags & VMCI_QPFLAG_LOCAL) {
- result = VMCI_SUCCESS;
- if (entry->qp.ref_count > 1) {
- result = qp_notify_peer_local(false, handle);
-
- }
- } else {
- result = qp_detatch_hypercall(handle);
- if (result < VMCI_SUCCESS) {
-
- mutex_unlock(&qp_guest_endpoints.mutex);
- return result;
- }
- }
-
- entry->qp.ref_count--;
- if (entry->qp.ref_count == 0)
- qp_list_remove_entry(&qp_guest_endpoints, &entry->qp);
-
- if (entry)
- ref_count = entry->qp.ref_count;
- mutex_unlock(&qp_guest_endpoints.mutex);
- if (ref_count == 0)
- qp_guest_endpoint_destroy(entry);
- return result;
- }
- static int qp_alloc_guest_work(struct vmci_handle *handle,
- struct vmci_queue **produce_q,
- u64 produce_size,
- struct vmci_queue **consume_q,
- u64 consume_size,
- u32 peer,
- u32 flags,
- u32 priv_flags)
- {
- const u64 num_produce_pages =
- DIV_ROUND_UP(produce_size, PAGE_SIZE) + 1;
- const u64 num_consume_pages =
- DIV_ROUND_UP(consume_size, PAGE_SIZE) + 1;
- void *my_produce_q = NULL;
- void *my_consume_q = NULL;
- int result;
- struct qp_guest_endpoint *queue_pair_entry = NULL;
- if (priv_flags != VMCI_NO_PRIVILEGE_FLAGS)
- return VMCI_ERROR_NO_ACCESS;
- mutex_lock(&qp_guest_endpoints.mutex);
- queue_pair_entry = qp_guest_handle_to_entry(*handle);
- if (queue_pair_entry) {
- if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) {
-
- if (queue_pair_entry->qp.ref_count > 1) {
- pr_devel("Error attempting to attach more than once\n");
- result = VMCI_ERROR_UNAVAILABLE;
- goto error_keep_entry;
- }
- if (queue_pair_entry->qp.produce_size != consume_size ||
- queue_pair_entry->qp.consume_size !=
- produce_size ||
- queue_pair_entry->qp.flags !=
- (flags & ~VMCI_QPFLAG_ATTACH_ONLY)) {
- pr_devel("Error mismatched queue pair in local attach\n");
- result = VMCI_ERROR_QUEUEPAIR_MISMATCH;
- goto error_keep_entry;
- }
-
- result = qp_notify_peer_local(true, *handle);
- if (result < VMCI_SUCCESS)
- goto error_keep_entry;
- my_produce_q = queue_pair_entry->consume_q;
- my_consume_q = queue_pair_entry->produce_q;
- goto out;
- }
- result = VMCI_ERROR_ALREADY_EXISTS;
- goto error_keep_entry;
- }
- my_produce_q = qp_alloc_queue(produce_size, flags);
- if (!my_produce_q) {
- pr_warn("Error allocating pages for produce queue\n");
- result = VMCI_ERROR_NO_MEM;
- goto error;
- }
- my_consume_q = qp_alloc_queue(consume_size, flags);
- if (!my_consume_q) {
- pr_warn("Error allocating pages for consume queue\n");
- result = VMCI_ERROR_NO_MEM;
- goto error;
- }
- queue_pair_entry = qp_guest_endpoint_create(*handle, peer, flags,
- produce_size, consume_size,
- my_produce_q, my_consume_q);
- if (!queue_pair_entry) {
- pr_warn("Error allocating memory in %s\n", __func__);
- result = VMCI_ERROR_NO_MEM;
- goto error;
- }
- result = qp_alloc_ppn_set(my_produce_q, num_produce_pages, my_consume_q,
- num_consume_pages,
- &queue_pair_entry->ppn_set);
- if (result < VMCI_SUCCESS) {
- pr_warn("qp_alloc_ppn_set failed\n");
- goto error;
- }
-
- if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) {
-
- u32 context_id = vmci_get_context_id();
-
- if (queue_pair_entry->qp.handle.context != context_id ||
- (queue_pair_entry->qp.peer != VMCI_INVALID_ID &&
- queue_pair_entry->qp.peer != context_id)) {
- result = VMCI_ERROR_NO_ACCESS;
- goto error;
- }
- if (queue_pair_entry->qp.flags & VMCI_QPFLAG_ATTACH_ONLY) {
- result = VMCI_ERROR_NOT_FOUND;
- goto error;
- }
- } else {
- result = qp_alloc_hypercall(queue_pair_entry);
- if (result < VMCI_SUCCESS) {
- pr_warn("qp_alloc_hypercall result = %d\n", result);
- goto error;
- }
- }
- qp_init_queue_mutex((struct vmci_queue *)my_produce_q,
- (struct vmci_queue *)my_consume_q);
- qp_list_add_entry(&qp_guest_endpoints, &queue_pair_entry->qp);
- out:
- queue_pair_entry->qp.ref_count++;
- *handle = queue_pair_entry->qp.handle;
- *produce_q = (struct vmci_queue *)my_produce_q;
- *consume_q = (struct vmci_queue *)my_consume_q;
-
- if ((queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) &&
- queue_pair_entry->qp.ref_count == 1) {
- vmci_q_header_init((*produce_q)->q_header, *handle);
- vmci_q_header_init((*consume_q)->q_header, *handle);
- }
- mutex_unlock(&qp_guest_endpoints.mutex);
- return VMCI_SUCCESS;
- error:
- mutex_unlock(&qp_guest_endpoints.mutex);
- if (queue_pair_entry) {
-
- qp_guest_endpoint_destroy(queue_pair_entry);
- } else {
- qp_free_queue(my_produce_q, produce_size);
- qp_free_queue(my_consume_q, consume_size);
- }
- return result;
- error_keep_entry:
-
- mutex_unlock(&qp_guest_endpoints.mutex);
- return result;
- }
- static int qp_broker_create(struct vmci_handle handle,
- u32 peer,
- u32 flags,
- u32 priv_flags,
- u64 produce_size,
- u64 consume_size,
- struct vmci_qp_page_store *page_store,
- struct vmci_ctx *context,
- vmci_event_release_cb wakeup_cb,
- void *client_data, struct qp_broker_entry **ent)
- {
- struct qp_broker_entry *entry = NULL;
- const u32 context_id = vmci_ctx_get_id(context);
- bool is_local = flags & VMCI_QPFLAG_LOCAL;
- int result;
- u64 guest_produce_size;
- u64 guest_consume_size;
-
- if (flags & VMCI_QPFLAG_ATTACH_ONLY)
- return VMCI_ERROR_NOT_FOUND;
-
- if (handle.context != context_id && handle.context != peer)
- return VMCI_ERROR_NO_ACCESS;
- if (VMCI_CONTEXT_IS_VM(context_id) && VMCI_CONTEXT_IS_VM(peer))
- return VMCI_ERROR_DST_UNREACHABLE;
-
- if (is_local && peer != VMCI_INVALID_ID && context_id != peer)
- return VMCI_ERROR_NO_ACCESS;
- entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
- if (!entry)
- return VMCI_ERROR_NO_MEM;
- if (vmci_ctx_get_id(context) == VMCI_HOST_CONTEXT_ID && !is_local) {
-
- guest_produce_size = consume_size;
- guest_consume_size = produce_size;
- } else {
- guest_produce_size = produce_size;
- guest_consume_size = consume_size;
- }
- entry->qp.handle = handle;
- entry->qp.peer = peer;
- entry->qp.flags = flags;
- entry->qp.produce_size = guest_produce_size;
- entry->qp.consume_size = guest_consume_size;
- entry->qp.ref_count = 1;
- entry->create_id = context_id;
- entry->attach_id = VMCI_INVALID_ID;
- entry->state = VMCIQPB_NEW;
- entry->require_trusted_attach =
- !!(context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED);
- entry->created_by_trusted =
- !!(priv_flags & VMCI_PRIVILEGE_FLAG_TRUSTED);
- entry->vmci_page_files = false;
- entry->wakeup_cb = wakeup_cb;
- entry->client_data = client_data;
- entry->produce_q = qp_host_alloc_queue(guest_produce_size);
- if (entry->produce_q == NULL) {
- result = VMCI_ERROR_NO_MEM;
- goto error;
- }
- entry->consume_q = qp_host_alloc_queue(guest_consume_size);
- if (entry->consume_q == NULL) {
- result = VMCI_ERROR_NO_MEM;
- goto error;
- }
- qp_init_queue_mutex(entry->produce_q, entry->consume_q);
- INIT_LIST_HEAD(&entry->qp.list_item);
- if (is_local) {
- u8 *tmp;
- entry->local_mem = kcalloc(QPE_NUM_PAGES(entry->qp),
- PAGE_SIZE, GFP_KERNEL);
- if (entry->local_mem == NULL) {
- result = VMCI_ERROR_NO_MEM;
- goto error;
- }
- entry->state = VMCIQPB_CREATED_MEM;
- entry->produce_q->q_header = entry->local_mem;
- tmp = (u8 *)entry->local_mem + PAGE_SIZE *
- (DIV_ROUND_UP(entry->qp.produce_size, PAGE_SIZE) + 1);
- entry->consume_q->q_header = (struct vmci_queue_header *)tmp;
- } else if (page_store) {
-
- result = qp_host_register_user_memory(page_store,
- entry->produce_q,
- entry->consume_q);
- if (result < VMCI_SUCCESS)
- goto error;
- entry->state = VMCIQPB_CREATED_MEM;
- } else {
-
- entry->state = VMCIQPB_CREATED_NO_MEM;
- }
- qp_list_add_entry(&qp_broker_list, &entry->qp);
- if (ent != NULL)
- *ent = entry;
-
- result = vmci_resource_add(&entry->resource,
- VMCI_RESOURCE_TYPE_QPAIR_HOST,
- handle);
- if (result != VMCI_SUCCESS) {
- pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
- handle.context, handle.resource, result);
- goto error;
- }
- entry->qp.handle = vmci_resource_handle(&entry->resource);
- if (is_local) {
- vmci_q_header_init(entry->produce_q->q_header,
- entry->qp.handle);
- vmci_q_header_init(entry->consume_q->q_header,
- entry->qp.handle);
- }
- vmci_ctx_qp_create(context, entry->qp.handle);
- return VMCI_SUCCESS;
- error:
- if (entry != NULL) {
- qp_host_free_queue(entry->produce_q, guest_produce_size);
- qp_host_free_queue(entry->consume_q, guest_consume_size);
- kfree(entry);
- }
- return result;
- }
- static int qp_notify_peer(bool attach,
- struct vmci_handle handle,
- u32 my_id,
- u32 peer_id)
- {
- int rv;
- struct vmci_event_qp ev;
- if (vmci_handle_is_invalid(handle) || my_id == VMCI_INVALID_ID ||
- peer_id == VMCI_INVALID_ID)
- return VMCI_ERROR_INVALID_ARGS;
-
- ev.msg.hdr.dst = vmci_make_handle(peer_id, VMCI_EVENT_HANDLER);
- ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
- VMCI_CONTEXT_RESOURCE_ID);
- ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
- ev.msg.event_data.event = attach ?
- VMCI_EVENT_QP_PEER_ATTACH : VMCI_EVENT_QP_PEER_DETACH;
- ev.payload.handle = handle;
- ev.payload.peer_id = my_id;
- rv = vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID,
- &ev.msg.hdr, false);
- if (rv < VMCI_SUCCESS)
- pr_warn("Failed to enqueue queue_pair %s event datagram for context (ID=0x%x)\n",
- attach ? "ATTACH" : "DETACH", peer_id);
- return rv;
- }
- static int qp_broker_attach(struct qp_broker_entry *entry,
- u32 peer,
- u32 flags,
- u32 priv_flags,
- u64 produce_size,
- u64 consume_size,
- struct vmci_qp_page_store *page_store,
- struct vmci_ctx *context,
- vmci_event_release_cb wakeup_cb,
- void *client_data,
- struct qp_broker_entry **ent)
- {
- const u32 context_id = vmci_ctx_get_id(context);
- bool is_local = flags & VMCI_QPFLAG_LOCAL;
- int result;
- if (entry->state != VMCIQPB_CREATED_NO_MEM &&
- entry->state != VMCIQPB_CREATED_MEM)
- return VMCI_ERROR_UNAVAILABLE;
- if (is_local) {
- if (!(entry->qp.flags & VMCI_QPFLAG_LOCAL) ||
- context_id != entry->create_id) {
- return VMCI_ERROR_INVALID_ARGS;
- }
- } else if (context_id == entry->create_id ||
- context_id == entry->attach_id) {
- return VMCI_ERROR_ALREADY_EXISTS;
- }
- if (VMCI_CONTEXT_IS_VM(context_id) &&
- VMCI_CONTEXT_IS_VM(entry->create_id))
- return VMCI_ERROR_DST_UNREACHABLE;
-
- if ((context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
- !entry->created_by_trusted)
- return VMCI_ERROR_NO_ACCESS;
-
- if (entry->require_trusted_attach &&
- (!(priv_flags & VMCI_PRIVILEGE_FLAG_TRUSTED)))
- return VMCI_ERROR_NO_ACCESS;
-
- if (entry->qp.peer != VMCI_INVALID_ID && entry->qp.peer != context_id)
- return VMCI_ERROR_NO_ACCESS;
- if (entry->create_id == VMCI_HOST_CONTEXT_ID) {
-
- if (!vmci_ctx_supports_host_qp(context))
- return VMCI_ERROR_INVALID_RESOURCE;
- } else if (context_id == VMCI_HOST_CONTEXT_ID) {
- struct vmci_ctx *create_context;
- bool supports_host_qp;
-
- create_context = vmci_ctx_get(entry->create_id);
- supports_host_qp = vmci_ctx_supports_host_qp(create_context);
- vmci_ctx_put(create_context);
- if (!supports_host_qp)
- return VMCI_ERROR_INVALID_RESOURCE;
- }
- if ((entry->qp.flags & ~VMCI_QP_ASYMM) != (flags & ~VMCI_QP_ASYMM_PEER))
- return VMCI_ERROR_QUEUEPAIR_MISMATCH;
- if (context_id != VMCI_HOST_CONTEXT_ID) {
-
- if (entry->qp.produce_size != produce_size ||
- entry->qp.consume_size != consume_size) {
- return VMCI_ERROR_QUEUEPAIR_MISMATCH;
- }
- } else if (entry->qp.produce_size != consume_size ||
- entry->qp.consume_size != produce_size) {
- return VMCI_ERROR_QUEUEPAIR_MISMATCH;
- }
- if (context_id != VMCI_HOST_CONTEXT_ID) {
-
- if (entry->state != VMCIQPB_CREATED_NO_MEM)
- return VMCI_ERROR_INVALID_ARGS;
- if (page_store != NULL) {
-
- result = qp_host_register_user_memory(page_store,
- entry->produce_q,
- entry->consume_q);
- if (result < VMCI_SUCCESS)
- return result;
- entry->state = VMCIQPB_ATTACHED_MEM;
- } else {
- entry->state = VMCIQPB_ATTACHED_NO_MEM;
- }
- } else if (entry->state == VMCIQPB_CREATED_NO_MEM) {
-
- return VMCI_ERROR_UNAVAILABLE;
- } else {
-
- entry->state = VMCIQPB_ATTACHED_MEM;
- }
- if (entry->state == VMCIQPB_ATTACHED_MEM) {
- result =
- qp_notify_peer(true, entry->qp.handle, context_id,
- entry->create_id);
- if (result < VMCI_SUCCESS)
- pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
- entry->create_id, entry->qp.handle.context,
- entry->qp.handle.resource);
- }
- entry->attach_id = context_id;
- entry->qp.ref_count++;
- if (wakeup_cb) {
- entry->wakeup_cb = wakeup_cb;
- entry->client_data = client_data;
- }
-
- if (!is_local)
- vmci_ctx_qp_create(context, entry->qp.handle);
- if (ent != NULL)
- *ent = entry;
- return VMCI_SUCCESS;
- }
- static int qp_broker_alloc(struct vmci_handle handle,
- u32 peer,
- u32 flags,
- u32 priv_flags,
- u64 produce_size,
- u64 consume_size,
- struct vmci_qp_page_store *page_store,
- struct vmci_ctx *context,
- vmci_event_release_cb wakeup_cb,
- void *client_data,
- struct qp_broker_entry **ent,
- bool *swap)
- {
- const u32 context_id = vmci_ctx_get_id(context);
- bool create;
- struct qp_broker_entry *entry = NULL;
- bool is_local = flags & VMCI_QPFLAG_LOCAL;
- int result;
- if (vmci_handle_is_invalid(handle) ||
- (flags & ~VMCI_QP_ALL_FLAGS) || is_local ||
- !(produce_size || consume_size) ||
- !context || context_id == VMCI_INVALID_ID ||
- handle.context == VMCI_INVALID_ID) {
- return VMCI_ERROR_INVALID_ARGS;
- }
- if (page_store && !VMCI_QP_PAGESTORE_IS_WELLFORMED(page_store))
- return VMCI_ERROR_INVALID_ARGS;
-
- mutex_lock(&qp_broker_list.mutex);
- if (!is_local && vmci_ctx_qp_exists(context, handle)) {
- pr_devel("Context (ID=0x%x) already attached to queue pair (handle=0x%x:0x%x)\n",
- context_id, handle.context, handle.resource);
- mutex_unlock(&qp_broker_list.mutex);
- return VMCI_ERROR_ALREADY_EXISTS;
- }
- if (handle.resource != VMCI_INVALID_ID)
- entry = qp_broker_handle_to_entry(handle);
- if (!entry) {
- create = true;
- result =
- qp_broker_create(handle, peer, flags, priv_flags,
- produce_size, consume_size, page_store,
- context, wakeup_cb, client_data, ent);
- } else {
- create = false;
- result =
- qp_broker_attach(entry, peer, flags, priv_flags,
- produce_size, consume_size, page_store,
- context, wakeup_cb, client_data, ent);
- }
- mutex_unlock(&qp_broker_list.mutex);
- if (swap)
- *swap = (context_id == VMCI_HOST_CONTEXT_ID) &&
- !(create && is_local);
- return result;
- }
- static int qp_alloc_host_work(struct vmci_handle *handle,
- struct vmci_queue **produce_q,
- u64 produce_size,
- struct vmci_queue **consume_q,
- u64 consume_size,
- u32 peer,
- u32 flags,
- u32 priv_flags,
- vmci_event_release_cb wakeup_cb,
- void *client_data)
- {
- struct vmci_handle new_handle;
- struct vmci_ctx *context;
- struct qp_broker_entry *entry;
- int result;
- bool swap;
- if (vmci_handle_is_invalid(*handle)) {
- new_handle = vmci_make_handle(
- VMCI_HOST_CONTEXT_ID, VMCI_INVALID_ID);
- } else
- new_handle = *handle;
- context = vmci_ctx_get(VMCI_HOST_CONTEXT_ID);
- entry = NULL;
- result =
- qp_broker_alloc(new_handle, peer, flags, priv_flags,
- produce_size, consume_size, NULL, context,
- wakeup_cb, client_data, &entry, &swap);
- if (result == VMCI_SUCCESS) {
- if (swap) {
-
- *produce_q = entry->consume_q;
- *consume_q = entry->produce_q;
- } else {
- *produce_q = entry->produce_q;
- *consume_q = entry->consume_q;
- }
- *handle = vmci_resource_handle(&entry->resource);
- } else {
- *handle = VMCI_INVALID_HANDLE;
- pr_devel("queue pair broker failed to alloc (result=%d)\n",
- result);
- }
- vmci_ctx_put(context);
- return result;
- }
- int vmci_qp_alloc(struct vmci_handle *handle,
- struct vmci_queue **produce_q,
- u64 produce_size,
- struct vmci_queue **consume_q,
- u64 consume_size,
- u32 peer,
- u32 flags,
- u32 priv_flags,
- bool guest_endpoint,
- vmci_event_release_cb wakeup_cb,
- void *client_data)
- {
- if (!handle || !produce_q || !consume_q ||
- (!produce_size && !consume_size) || (flags & ~VMCI_QP_ALL_FLAGS))
- return VMCI_ERROR_INVALID_ARGS;
- if (guest_endpoint) {
- return qp_alloc_guest_work(handle, produce_q,
- produce_size, consume_q,
- consume_size, peer,
- flags, priv_flags);
- } else {
- return qp_alloc_host_work(handle, produce_q,
- produce_size, consume_q,
- consume_size, peer, flags,
- priv_flags, wakeup_cb, client_data);
- }
- }
- static int qp_detatch_host_work(struct vmci_handle handle)
- {
- int result;
- struct vmci_ctx *context;
- context = vmci_ctx_get(VMCI_HOST_CONTEXT_ID);
- result = vmci_qp_broker_detach(handle, context);
- vmci_ctx_put(context);
- return result;
- }
- static int qp_detatch(struct vmci_handle handle, bool guest_endpoint)
- {
- if (vmci_handle_is_invalid(handle))
- return VMCI_ERROR_INVALID_ARGS;
- if (guest_endpoint)
- return qp_detatch_guest_work(handle);
- else
- return qp_detatch_host_work(handle);
- }
- static struct qp_entry *qp_list_get_head(struct qp_list *qp_list)
- {
- if (!list_empty(&qp_list->head)) {
- struct qp_entry *entry =
- list_first_entry(&qp_list->head, struct qp_entry,
- list_item);
- return entry;
- }
- return NULL;
- }
- void vmci_qp_broker_exit(void)
- {
- struct qp_entry *entry;
- struct qp_broker_entry *be;
- mutex_lock(&qp_broker_list.mutex);
- while ((entry = qp_list_get_head(&qp_broker_list))) {
- be = (struct qp_broker_entry *)entry;
- qp_list_remove_entry(&qp_broker_list, entry);
- kfree(be);
- }
- mutex_unlock(&qp_broker_list.mutex);
- }
- int vmci_qp_broker_alloc(struct vmci_handle handle,
- u32 peer,
- u32 flags,
- u32 priv_flags,
- u64 produce_size,
- u64 consume_size,
- struct vmci_qp_page_store *page_store,
- struct vmci_ctx *context)
- {
- return qp_broker_alloc(handle, peer, flags, priv_flags,
- produce_size, consume_size,
- page_store, context, NULL, NULL, NULL, NULL);
- }
- int vmci_qp_broker_set_page_store(struct vmci_handle handle,
- u64 produce_uva,
- u64 consume_uva,
- struct vmci_ctx *context)
- {
- struct qp_broker_entry *entry;
- int result;
- const u32 context_id = vmci_ctx_get_id(context);
- if (vmci_handle_is_invalid(handle) || !context ||
- context_id == VMCI_INVALID_ID)
- return VMCI_ERROR_INVALID_ARGS;
-
- if (produce_uva == 0 || consume_uva == 0)
- return VMCI_ERROR_INVALID_ARGS;
- mutex_lock(&qp_broker_list.mutex);
- if (!vmci_ctx_qp_exists(context, handle)) {
- pr_warn("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
- context_id, handle.context, handle.resource);
- result = VMCI_ERROR_NOT_FOUND;
- goto out;
- }
- entry = qp_broker_handle_to_entry(handle);
- if (!entry) {
- result = VMCI_ERROR_NOT_FOUND;
- goto out;
- }
-
- if (entry->create_id != context_id &&
- (entry->create_id != VMCI_HOST_CONTEXT_ID ||
- entry->attach_id != context_id)) {
- result = VMCI_ERROR_QUEUEPAIR_NOTOWNER;
- goto out;
- }
- if (entry->state != VMCIQPB_CREATED_NO_MEM &&
- entry->state != VMCIQPB_ATTACHED_NO_MEM) {
- result = VMCI_ERROR_UNAVAILABLE;
- goto out;
- }
- result = qp_host_get_user_memory(produce_uva, consume_uva,
- entry->produce_q, entry->consume_q);
- if (result < VMCI_SUCCESS)
- goto out;
- result = qp_host_map_queues(entry->produce_q, entry->consume_q);
- if (result < VMCI_SUCCESS) {
- qp_host_unregister_user_memory(entry->produce_q,
- entry->consume_q);
- goto out;
- }
- if (entry->state == VMCIQPB_CREATED_NO_MEM)
- entry->state = VMCIQPB_CREATED_MEM;
- else
- entry->state = VMCIQPB_ATTACHED_MEM;
- entry->vmci_page_files = true;
- if (entry->state == VMCIQPB_ATTACHED_MEM) {
- result =
- qp_notify_peer(true, handle, context_id, entry->create_id);
- if (result < VMCI_SUCCESS) {
- pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
- entry->create_id, entry->qp.handle.context,
- entry->qp.handle.resource);
- }
- }
- result = VMCI_SUCCESS;
- out:
- mutex_unlock(&qp_broker_list.mutex);
- return result;
- }
- static void qp_reset_saved_headers(struct qp_broker_entry *entry)
- {
- entry->produce_q->saved_header = NULL;
- entry->consume_q->saved_header = NULL;
- }
- int vmci_qp_broker_detach(struct vmci_handle handle, struct vmci_ctx *context)
- {
- struct qp_broker_entry *entry;
- const u32 context_id = vmci_ctx_get_id(context);
- u32 peer_id;
- bool is_local = false;
- int result;
- if (vmci_handle_is_invalid(handle) || !context ||
- context_id == VMCI_INVALID_ID) {
- return VMCI_ERROR_INVALID_ARGS;
- }
- mutex_lock(&qp_broker_list.mutex);
- if (!vmci_ctx_qp_exists(context, handle)) {
- pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
- context_id, handle.context, handle.resource);
- result = VMCI_ERROR_NOT_FOUND;
- goto out;
- }
- entry = qp_broker_handle_to_entry(handle);
- if (!entry) {
- pr_devel("Context (ID=0x%x) reports being attached to queue pair(handle=0x%x:0x%x) that isn't present in broker\n",
- context_id, handle.context, handle.resource);
- result = VMCI_ERROR_NOT_FOUND;
- goto out;
- }
- if (context_id != entry->create_id && context_id != entry->attach_id) {
- result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
- goto out;
- }
- if (context_id == entry->create_id) {
- peer_id = entry->attach_id;
- entry->create_id = VMCI_INVALID_ID;
- } else {
- peer_id = entry->create_id;
- entry->attach_id = VMCI_INVALID_ID;
- }
- entry->qp.ref_count--;
- is_local = entry->qp.flags & VMCI_QPFLAG_LOCAL;
- if (context_id != VMCI_HOST_CONTEXT_ID) {
- bool headers_mapped;
-
- qp_acquire_queue_mutex(entry->produce_q);
- headers_mapped = entry->produce_q->q_header ||
- entry->consume_q->q_header;
- if (QPBROKERSTATE_HAS_MEM(entry)) {
- result =
- qp_host_unmap_queues(INVALID_VMCI_GUEST_MEM_ID,
- entry->produce_q,
- entry->consume_q);
- if (result < VMCI_SUCCESS)
- pr_warn("Failed to unmap queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
- handle.context, handle.resource,
- result);
- if (entry->vmci_page_files)
- qp_host_unregister_user_memory(entry->produce_q,
- entry->
- consume_q);
- else
- qp_host_unregister_user_memory(entry->produce_q,
- entry->
- consume_q);
- }
- if (!headers_mapped)
- qp_reset_saved_headers(entry);
- qp_release_queue_mutex(entry->produce_q);
- if (!headers_mapped && entry->wakeup_cb)
- entry->wakeup_cb(entry->client_data);
- } else {
- if (entry->wakeup_cb) {
- entry->wakeup_cb = NULL;
- entry->client_data = NULL;
- }
- }
- if (entry->qp.ref_count == 0) {
- qp_list_remove_entry(&qp_broker_list, &entry->qp);
- if (is_local)
- kfree(entry->local_mem);
- qp_cleanup_queue_mutex(entry->produce_q, entry->consume_q);
- qp_host_free_queue(entry->produce_q, entry->qp.produce_size);
- qp_host_free_queue(entry->consume_q, entry->qp.consume_size);
-
- vmci_resource_remove(&entry->resource);
- kfree(entry);
- vmci_ctx_qp_destroy(context, handle);
- } else {
- qp_notify_peer(false, handle, context_id, peer_id);
- if (context_id == VMCI_HOST_CONTEXT_ID &&
- QPBROKERSTATE_HAS_MEM(entry)) {
- entry->state = VMCIQPB_SHUTDOWN_MEM;
- } else {
- entry->state = VMCIQPB_SHUTDOWN_NO_MEM;
- }
- if (!is_local)
- vmci_ctx_qp_destroy(context, handle);
- }
- result = VMCI_SUCCESS;
- out:
- mutex_unlock(&qp_broker_list.mutex);
- return result;
- }
- int vmci_qp_broker_map(struct vmci_handle handle,
- struct vmci_ctx *context,
- u64 guest_mem)
- {
- struct qp_broker_entry *entry;
- const u32 context_id = vmci_ctx_get_id(context);
- bool is_local = false;
- int result;
- if (vmci_handle_is_invalid(handle) || !context ||
- context_id == VMCI_INVALID_ID)
- return VMCI_ERROR_INVALID_ARGS;
- mutex_lock(&qp_broker_list.mutex);
- if (!vmci_ctx_qp_exists(context, handle)) {
- pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
- context_id, handle.context, handle.resource);
- result = VMCI_ERROR_NOT_FOUND;
- goto out;
- }
- entry = qp_broker_handle_to_entry(handle);
- if (!entry) {
- pr_devel("Context (ID=0x%x) reports being attached to queue pair (handle=0x%x:0x%x) that isn't present in broker\n",
- context_id, handle.context, handle.resource);
- result = VMCI_ERROR_NOT_FOUND;
- goto out;
- }
- if (context_id != entry->create_id && context_id != entry->attach_id) {
- result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
- goto out;
- }
- is_local = entry->qp.flags & VMCI_QPFLAG_LOCAL;
- result = VMCI_SUCCESS;
- if (context_id != VMCI_HOST_CONTEXT_ID) {
- struct vmci_qp_page_store page_store;
- page_store.pages = guest_mem;
- page_store.len = QPE_NUM_PAGES(entry->qp);
- qp_acquire_queue_mutex(entry->produce_q);
- qp_reset_saved_headers(entry);
- result =
- qp_host_register_user_memory(&page_store,
- entry->produce_q,
- entry->consume_q);
- qp_release_queue_mutex(entry->produce_q);
- if (result == VMCI_SUCCESS) {
-
- entry->state++;
- if (entry->wakeup_cb)
- entry->wakeup_cb(entry->client_data);
- }
- }
- out:
- mutex_unlock(&qp_broker_list.mutex);
- return result;
- }
- static int qp_save_headers(struct qp_broker_entry *entry)
- {
- int result;
- if (entry->produce_q->saved_header != NULL &&
- entry->consume_q->saved_header != NULL) {
-
- return VMCI_SUCCESS;
- }
- if (NULL == entry->produce_q->q_header ||
- NULL == entry->consume_q->q_header) {
- result = qp_host_map_queues(entry->produce_q, entry->consume_q);
- if (result < VMCI_SUCCESS)
- return result;
- }
- memcpy(&entry->saved_produce_q, entry->produce_q->q_header,
- sizeof(entry->saved_produce_q));
- entry->produce_q->saved_header = &entry->saved_produce_q;
- memcpy(&entry->saved_consume_q, entry->consume_q->q_header,
- sizeof(entry->saved_consume_q));
- entry->consume_q->saved_header = &entry->saved_consume_q;
- return VMCI_SUCCESS;
- }
- int vmci_qp_broker_unmap(struct vmci_handle handle,
- struct vmci_ctx *context,
- u32 gid)
- {
- struct qp_broker_entry *entry;
- const u32 context_id = vmci_ctx_get_id(context);
- bool is_local = false;
- int result;
- if (vmci_handle_is_invalid(handle) || !context ||
- context_id == VMCI_INVALID_ID)
- return VMCI_ERROR_INVALID_ARGS;
- mutex_lock(&qp_broker_list.mutex);
- if (!vmci_ctx_qp_exists(context, handle)) {
- pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
- context_id, handle.context, handle.resource);
- result = VMCI_ERROR_NOT_FOUND;
- goto out;
- }
- entry = qp_broker_handle_to_entry(handle);
- if (!entry) {
- pr_devel("Context (ID=0x%x) reports being attached to queue pair (handle=0x%x:0x%x) that isn't present in broker\n",
- context_id, handle.context, handle.resource);
- result = VMCI_ERROR_NOT_FOUND;
- goto out;
- }
- if (context_id != entry->create_id && context_id != entry->attach_id) {
- result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
- goto out;
- }
- is_local = entry->qp.flags & VMCI_QPFLAG_LOCAL;
- if (context_id != VMCI_HOST_CONTEXT_ID) {
- qp_acquire_queue_mutex(entry->produce_q);
- result = qp_save_headers(entry);
- if (result < VMCI_SUCCESS)
- pr_warn("Failed to save queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
- handle.context, handle.resource, result);
- qp_host_unmap_queues(gid, entry->produce_q, entry->consume_q);
-
- qp_host_unregister_user_memory(entry->produce_q,
- entry->consume_q);
-
- entry->state--;
- qp_release_queue_mutex(entry->produce_q);
- }
- result = VMCI_SUCCESS;
- out:
- mutex_unlock(&qp_broker_list.mutex);
- return result;
- }
- void vmci_qp_guest_endpoints_exit(void)
- {
- struct qp_entry *entry;
- struct qp_guest_endpoint *ep;
- mutex_lock(&qp_guest_endpoints.mutex);
- while ((entry = qp_list_get_head(&qp_guest_endpoints))) {
- ep = (struct qp_guest_endpoint *)entry;
-
- if (!(entry->flags & VMCI_QPFLAG_LOCAL))
- qp_detatch_hypercall(entry->handle);
-
- entry->ref_count = 0;
- qp_list_remove_entry(&qp_guest_endpoints, entry);
- qp_guest_endpoint_destroy(ep);
- }
- mutex_unlock(&qp_guest_endpoints.mutex);
- }
- static void qp_lock(const struct vmci_qp *qpair)
- {
- qp_acquire_queue_mutex(qpair->produce_q);
- }
- static void qp_unlock(const struct vmci_qp *qpair)
- {
- qp_release_queue_mutex(qpair->produce_q);
- }
- static int qp_map_queue_headers(struct vmci_queue *produce_q,
- struct vmci_queue *consume_q)
- {
- int result;
- if (NULL == produce_q->q_header || NULL == consume_q->q_header) {
- result = qp_host_map_queues(produce_q, consume_q);
- if (result < VMCI_SUCCESS)
- return (produce_q->saved_header &&
- consume_q->saved_header) ?
- VMCI_ERROR_QUEUEPAIR_NOT_READY :
- VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
- }
- return VMCI_SUCCESS;
- }
- static int qp_get_queue_headers(const struct vmci_qp *qpair,
- struct vmci_queue_header **produce_q_header,
- struct vmci_queue_header **consume_q_header)
- {
- int result;
- result = qp_map_queue_headers(qpair->produce_q, qpair->consume_q);
- if (result == VMCI_SUCCESS) {
- *produce_q_header = qpair->produce_q->q_header;
- *consume_q_header = qpair->consume_q->q_header;
- } else if (qpair->produce_q->saved_header &&
- qpair->consume_q->saved_header) {
- *produce_q_header = qpair->produce_q->saved_header;
- *consume_q_header = qpair->consume_q->saved_header;
- result = VMCI_SUCCESS;
- }
- return result;
- }
- static int qp_wakeup_cb(void *client_data)
- {
- struct vmci_qp *qpair = (struct vmci_qp *)client_data;
- qp_lock(qpair);
- while (qpair->blocked > 0) {
- qpair->blocked--;
- qpair->generation++;
- wake_up(&qpair->event);
- }
- qp_unlock(qpair);
- return VMCI_SUCCESS;
- }
- static bool qp_wait_for_ready_queue(struct vmci_qp *qpair)
- {
- unsigned int generation;
- qpair->blocked++;
- generation = qpair->generation;
- qp_unlock(qpair);
- wait_event(qpair->event, generation != qpair->generation);
- qp_lock(qpair);
- return true;
- }
- static ssize_t qp_enqueue_locked(struct vmci_queue *produce_q,
- struct vmci_queue *consume_q,
- const u64 produce_q_size,
- const void *buf,
- size_t buf_size,
- vmci_memcpy_to_queue_func memcpy_to_queue)
- {
- s64 free_space;
- u64 tail;
- size_t written;
- ssize_t result;
- result = qp_map_queue_headers(produce_q, consume_q);
- if (unlikely(result != VMCI_SUCCESS))
- return result;
- free_space = vmci_q_header_free_space(produce_q->q_header,
- consume_q->q_header,
- produce_q_size);
- if (free_space == 0)
- return VMCI_ERROR_QUEUEPAIR_NOSPACE;
- if (free_space < VMCI_SUCCESS)
- return (ssize_t) free_space;
- written = (size_t) (free_space > buf_size ? buf_size : free_space);
- tail = vmci_q_header_producer_tail(produce_q->q_header);
- if (likely(tail + written < produce_q_size)) {
- result = memcpy_to_queue(produce_q, tail, buf, 0, written);
- } else {
-
- const size_t tmp = (size_t) (produce_q_size - tail);
- result = memcpy_to_queue(produce_q, tail, buf, 0, tmp);
- if (result >= VMCI_SUCCESS)
- result = memcpy_to_queue(produce_q, 0, buf, tmp,
- written - tmp);
- }
- if (result < VMCI_SUCCESS)
- return result;
- vmci_q_header_add_producer_tail(produce_q->q_header, written,
- produce_q_size);
- return written;
- }
- static ssize_t qp_dequeue_locked(struct vmci_queue *produce_q,
- struct vmci_queue *consume_q,
- const u64 consume_q_size,
- void *buf,
- size_t buf_size,
- vmci_memcpy_from_queue_func memcpy_from_queue,
- bool update_consumer)
- {
- s64 buf_ready;
- u64 head;
- size_t read;
- ssize_t result;
- result = qp_map_queue_headers(produce_q, consume_q);
- if (unlikely(result != VMCI_SUCCESS))
- return result;
- buf_ready = vmci_q_header_buf_ready(consume_q->q_header,
- produce_q->q_header,
- consume_q_size);
- if (buf_ready == 0)
- return VMCI_ERROR_QUEUEPAIR_NODATA;
- if (buf_ready < VMCI_SUCCESS)
- return (ssize_t) buf_ready;
- read = (size_t) (buf_ready > buf_size ? buf_size : buf_ready);
- head = vmci_q_header_consumer_head(produce_q->q_header);
- if (likely(head + read < consume_q_size)) {
- result = memcpy_from_queue(buf, 0, consume_q, head, read);
- } else {
-
- const size_t tmp = (size_t) (consume_q_size - head);
- result = memcpy_from_queue(buf, 0, consume_q, head, tmp);
- if (result >= VMCI_SUCCESS)
- result = memcpy_from_queue(buf, tmp, consume_q, 0,
- read - tmp);
- }
- if (result < VMCI_SUCCESS)
- return result;
- if (update_consumer)
- vmci_q_header_add_consumer_head(produce_q->q_header,
- read, consume_q_size);
- return read;
- }
- int vmci_qpair_alloc(struct vmci_qp **qpair,
- struct vmci_handle *handle,
- u64 produce_qsize,
- u64 consume_qsize,
- u32 peer,
- u32 flags,
- u32 priv_flags)
- {
- struct vmci_qp *my_qpair;
- int retval;
- struct vmci_handle src = VMCI_INVALID_HANDLE;
- struct vmci_handle dst = vmci_make_handle(peer, VMCI_INVALID_ID);
- enum vmci_route route;
- vmci_event_release_cb wakeup_cb;
- void *client_data;
-
- if (produce_qsize + consume_qsize < max(produce_qsize, consume_qsize) ||
- produce_qsize + consume_qsize > VMCI_MAX_GUEST_QP_MEMORY)
- return VMCI_ERROR_NO_RESOURCES;
- retval = vmci_route(&src, &dst, false, &route);
- if (retval < VMCI_SUCCESS)
- route = vmci_guest_code_active() ?
- VMCI_ROUTE_AS_GUEST : VMCI_ROUTE_AS_HOST;
- if (flags & (VMCI_QPFLAG_NONBLOCK | VMCI_QPFLAG_PINNED)) {
- pr_devel("NONBLOCK OR PINNED set");
- return VMCI_ERROR_INVALID_ARGS;
- }
- my_qpair = kzalloc(sizeof(*my_qpair), GFP_KERNEL);
- if (!my_qpair)
- return VMCI_ERROR_NO_MEM;
- my_qpair->produce_q_size = produce_qsize;
- my_qpair->consume_q_size = consume_qsize;
- my_qpair->peer = peer;
- my_qpair->flags = flags;
- my_qpair->priv_flags = priv_flags;
- wakeup_cb = NULL;
- client_data = NULL;
- if (VMCI_ROUTE_AS_HOST == route) {
- my_qpair->guest_endpoint = false;
- if (!(flags & VMCI_QPFLAG_LOCAL)) {
- my_qpair->blocked = 0;
- my_qpair->generation = 0;
- init_waitqueue_head(&my_qpair->event);
- wakeup_cb = qp_wakeup_cb;
- client_data = (void *)my_qpair;
- }
- } else {
- my_qpair->guest_endpoint = true;
- }
- retval = vmci_qp_alloc(handle,
- &my_qpair->produce_q,
- my_qpair->produce_q_size,
- &my_qpair->consume_q,
- my_qpair->consume_q_size,
- my_qpair->peer,
- my_qpair->flags,
- my_qpair->priv_flags,
- my_qpair->guest_endpoint,
- wakeup_cb, client_data);
- if (retval < VMCI_SUCCESS) {
- kfree(my_qpair);
- return retval;
- }
- *qpair = my_qpair;
- my_qpair->handle = *handle;
- return retval;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_alloc);
- int vmci_qpair_detach(struct vmci_qp **qpair)
- {
- int result;
- struct vmci_qp *old_qpair;
- if (!qpair || !(*qpair))
- return VMCI_ERROR_INVALID_ARGS;
- old_qpair = *qpair;
- result = qp_detatch(old_qpair->handle, old_qpair->guest_endpoint);
-
- memset(old_qpair, 0, sizeof(*old_qpair));
- old_qpair->handle = VMCI_INVALID_HANDLE;
- old_qpair->peer = VMCI_INVALID_ID;
- kfree(old_qpair);
- *qpair = NULL;
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_detach);
- int vmci_qpair_get_produce_indexes(const struct vmci_qp *qpair,
- u64 *producer_tail,
- u64 *consumer_head)
- {
- struct vmci_queue_header *produce_q_header;
- struct vmci_queue_header *consume_q_header;
- int result;
- if (!qpair)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- result =
- qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
- if (result == VMCI_SUCCESS)
- vmci_q_header_get_pointers(produce_q_header, consume_q_header,
- producer_tail, consumer_head);
- qp_unlock(qpair);
- if (result == VMCI_SUCCESS &&
- ((producer_tail && *producer_tail >= qpair->produce_q_size) ||
- (consumer_head && *consumer_head >= qpair->produce_q_size)))
- return VMCI_ERROR_INVALID_SIZE;
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_get_produce_indexes);
- int vmci_qpair_get_consume_indexes(const struct vmci_qp *qpair,
- u64 *consumer_tail,
- u64 *producer_head)
- {
- struct vmci_queue_header *produce_q_header;
- struct vmci_queue_header *consume_q_header;
- int result;
- if (!qpair)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- result =
- qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
- if (result == VMCI_SUCCESS)
- vmci_q_header_get_pointers(consume_q_header, produce_q_header,
- consumer_tail, producer_head);
- qp_unlock(qpair);
- if (result == VMCI_SUCCESS &&
- ((consumer_tail && *consumer_tail >= qpair->consume_q_size) ||
- (producer_head && *producer_head >= qpair->consume_q_size)))
- return VMCI_ERROR_INVALID_SIZE;
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_get_consume_indexes);
- s64 vmci_qpair_produce_free_space(const struct vmci_qp *qpair)
- {
- struct vmci_queue_header *produce_q_header;
- struct vmci_queue_header *consume_q_header;
- s64 result;
- if (!qpair)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- result =
- qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
- if (result == VMCI_SUCCESS)
- result = vmci_q_header_free_space(produce_q_header,
- consume_q_header,
- qpair->produce_q_size);
- else
- result = 0;
- qp_unlock(qpair);
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_produce_free_space);
- s64 vmci_qpair_consume_free_space(const struct vmci_qp *qpair)
- {
- struct vmci_queue_header *produce_q_header;
- struct vmci_queue_header *consume_q_header;
- s64 result;
- if (!qpair)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- result =
- qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
- if (result == VMCI_SUCCESS)
- result = vmci_q_header_free_space(consume_q_header,
- produce_q_header,
- qpair->consume_q_size);
- else
- result = 0;
- qp_unlock(qpair);
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_consume_free_space);
- s64 vmci_qpair_produce_buf_ready(const struct vmci_qp *qpair)
- {
- struct vmci_queue_header *produce_q_header;
- struct vmci_queue_header *consume_q_header;
- s64 result;
- if (!qpair)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- result =
- qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
- if (result == VMCI_SUCCESS)
- result = vmci_q_header_buf_ready(produce_q_header,
- consume_q_header,
- qpair->produce_q_size);
- else
- result = 0;
- qp_unlock(qpair);
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_produce_buf_ready);
- s64 vmci_qpair_consume_buf_ready(const struct vmci_qp *qpair)
- {
- struct vmci_queue_header *produce_q_header;
- struct vmci_queue_header *consume_q_header;
- s64 result;
- if (!qpair)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- result =
- qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
- if (result == VMCI_SUCCESS)
- result = vmci_q_header_buf_ready(consume_q_header,
- produce_q_header,
- qpair->consume_q_size);
- else
- result = 0;
- qp_unlock(qpair);
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_consume_buf_ready);
- ssize_t vmci_qpair_enqueue(struct vmci_qp *qpair,
- const void *buf,
- size_t buf_size,
- int buf_type)
- {
- ssize_t result;
- if (!qpair || !buf)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- do {
- result = qp_enqueue_locked(qpair->produce_q,
- qpair->consume_q,
- qpair->produce_q_size,
- buf, buf_size,
- qp_memcpy_to_queue);
- if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
- !qp_wait_for_ready_queue(qpair))
- result = VMCI_ERROR_WOULD_BLOCK;
- } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
- qp_unlock(qpair);
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_enqueue);
- ssize_t vmci_qpair_dequeue(struct vmci_qp *qpair,
- void *buf,
- size_t buf_size,
- int buf_type)
- {
- ssize_t result;
- if (!qpair || !buf)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- do {
- result = qp_dequeue_locked(qpair->produce_q,
- qpair->consume_q,
- qpair->consume_q_size,
- buf, buf_size,
- qp_memcpy_from_queue, true);
- if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
- !qp_wait_for_ready_queue(qpair))
- result = VMCI_ERROR_WOULD_BLOCK;
- } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
- qp_unlock(qpair);
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_dequeue);
- ssize_t vmci_qpair_peek(struct vmci_qp *qpair,
- void *buf,
- size_t buf_size,
- int buf_type)
- {
- ssize_t result;
- if (!qpair || !buf)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- do {
- result = qp_dequeue_locked(qpair->produce_q,
- qpair->consume_q,
- qpair->consume_q_size,
- buf, buf_size,
- qp_memcpy_from_queue, false);
- if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
- !qp_wait_for_ready_queue(qpair))
- result = VMCI_ERROR_WOULD_BLOCK;
- } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
- qp_unlock(qpair);
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_peek);
- ssize_t vmci_qpair_enquev(struct vmci_qp *qpair,
- struct msghdr *msg,
- size_t iov_size,
- int buf_type)
- {
- ssize_t result;
- if (!qpair)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- do {
- result = qp_enqueue_locked(qpair->produce_q,
- qpair->consume_q,
- qpair->produce_q_size,
- msg, iov_size,
- qp_memcpy_to_queue_iov);
- if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
- !qp_wait_for_ready_queue(qpair))
- result = VMCI_ERROR_WOULD_BLOCK;
- } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
- qp_unlock(qpair);
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_enquev);
- ssize_t vmci_qpair_dequev(struct vmci_qp *qpair,
- struct msghdr *msg,
- size_t iov_size,
- int buf_type)
- {
- ssize_t result;
- if (!qpair)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- do {
- result = qp_dequeue_locked(qpair->produce_q,
- qpair->consume_q,
- qpair->consume_q_size,
- msg, iov_size,
- qp_memcpy_from_queue_iov,
- true);
- if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
- !qp_wait_for_ready_queue(qpair))
- result = VMCI_ERROR_WOULD_BLOCK;
- } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
- qp_unlock(qpair);
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_dequev);
- ssize_t vmci_qpair_peekv(struct vmci_qp *qpair,
- struct msghdr *msg,
- size_t iov_size,
- int buf_type)
- {
- ssize_t result;
- if (!qpair)
- return VMCI_ERROR_INVALID_ARGS;
- qp_lock(qpair);
- do {
- result = qp_dequeue_locked(qpair->produce_q,
- qpair->consume_q,
- qpair->consume_q_size,
- msg, iov_size,
- qp_memcpy_from_queue_iov,
- false);
- if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
- !qp_wait_for_ready_queue(qpair))
- result = VMCI_ERROR_WOULD_BLOCK;
- } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
- qp_unlock(qpair);
- return result;
- }
- EXPORT_SYMBOL_GPL(vmci_qpair_peekv);
|