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