vmci_context.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VMware VMCI Driver
  4. *
  5. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  6. */
  7. #include <linux/vmw_vmci_defs.h>
  8. #include <linux/vmw_vmci_api.h>
  9. #include <linux/highmem.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/cred.h>
  14. #include <linux/slab.h>
  15. #include "vmci_queue_pair.h"
  16. #include "vmci_datagram.h"
  17. #include "vmci_doorbell.h"
  18. #include "vmci_context.h"
  19. #include "vmci_driver.h"
  20. #include "vmci_event.h"
  21. /* Use a wide upper bound for the maximum contexts. */
  22. #define VMCI_MAX_CONTEXTS 2000
  23. /*
  24. * List of current VMCI contexts. Contexts can be added by
  25. * vmci_ctx_create() and removed via vmci_ctx_destroy().
  26. * These, along with context lookup, are protected by the
  27. * list structure's lock.
  28. */
  29. static struct {
  30. struct list_head head;
  31. spinlock_t lock; /* Spinlock for context list operations */
  32. } ctx_list = {
  33. .head = LIST_HEAD_INIT(ctx_list.head),
  34. .lock = __SPIN_LOCK_UNLOCKED(ctx_list.lock),
  35. };
  36. /* Used by contexts that did not set up notify flag pointers */
  37. static bool ctx_dummy_notify;
  38. static void ctx_signal_notify(struct vmci_ctx *context)
  39. {
  40. *context->notify = true;
  41. }
  42. static void ctx_clear_notify(struct vmci_ctx *context)
  43. {
  44. *context->notify = false;
  45. }
  46. /*
  47. * If nothing requires the attention of the guest, clears both
  48. * notify flag and call.
  49. */
  50. static void ctx_clear_notify_call(struct vmci_ctx *context)
  51. {
  52. if (context->pending_datagrams == 0 &&
  53. vmci_handle_arr_get_size(context->pending_doorbell_array) == 0)
  54. ctx_clear_notify(context);
  55. }
  56. /*
  57. * Sets the context's notify flag iff datagrams are pending for this
  58. * context. Called from vmci_setup_notify().
  59. */
  60. void vmci_ctx_check_signal_notify(struct vmci_ctx *context)
  61. {
  62. spin_lock(&context->lock);
  63. if (context->pending_datagrams)
  64. ctx_signal_notify(context);
  65. spin_unlock(&context->lock);
  66. }
  67. /*
  68. * Allocates and initializes a VMCI context.
  69. */
  70. struct vmci_ctx *vmci_ctx_create(u32 cid, u32 priv_flags,
  71. uintptr_t event_hnd,
  72. int user_version,
  73. const struct cred *cred)
  74. {
  75. struct vmci_ctx *context;
  76. int error;
  77. if (cid == VMCI_INVALID_ID) {
  78. pr_devel("Invalid context ID for VMCI context\n");
  79. error = -EINVAL;
  80. goto err_out;
  81. }
  82. if (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS) {
  83. pr_devel("Invalid flag (flags=0x%x) for VMCI context\n",
  84. priv_flags);
  85. error = -EINVAL;
  86. goto err_out;
  87. }
  88. if (user_version == 0) {
  89. pr_devel("Invalid suer_version %d\n", user_version);
  90. error = -EINVAL;
  91. goto err_out;
  92. }
  93. context = kzalloc_obj(*context);
  94. if (!context) {
  95. pr_warn("Failed to allocate memory for VMCI context\n");
  96. error = -ENOMEM;
  97. goto err_out;
  98. }
  99. kref_init(&context->kref);
  100. spin_lock_init(&context->lock);
  101. INIT_LIST_HEAD(&context->list_item);
  102. INIT_LIST_HEAD(&context->datagram_queue);
  103. INIT_LIST_HEAD(&context->notifier_list);
  104. /* Initialize host-specific VMCI context. */
  105. init_waitqueue_head(&context->host_context.wait_queue);
  106. context->queue_pair_array =
  107. vmci_handle_arr_create(0, VMCI_MAX_GUEST_QP_COUNT);
  108. if (!context->queue_pair_array) {
  109. error = -ENOMEM;
  110. goto err_free_ctx;
  111. }
  112. context->doorbell_array =
  113. vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT);
  114. if (!context->doorbell_array) {
  115. error = -ENOMEM;
  116. goto err_free_qp_array;
  117. }
  118. context->pending_doorbell_array =
  119. vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT);
  120. if (!context->pending_doorbell_array) {
  121. error = -ENOMEM;
  122. goto err_free_db_array;
  123. }
  124. context->user_version = user_version;
  125. context->priv_flags = priv_flags;
  126. if (cred)
  127. context->cred = get_cred(cred);
  128. context->notify = &ctx_dummy_notify;
  129. context->notify_page = NULL;
  130. /*
  131. * If we collide with an existing context we generate a new
  132. * and use it instead. The VMX will determine if regeneration
  133. * is okay. Since there isn't 4B - 16 VMs running on a given
  134. * host, the below loop will terminate.
  135. */
  136. spin_lock(&ctx_list.lock);
  137. while (vmci_ctx_exists(cid)) {
  138. /* We reserve the lowest 16 ids for fixed contexts. */
  139. cid = max(cid, VMCI_RESERVED_CID_LIMIT - 1) + 1;
  140. if (cid == VMCI_INVALID_ID)
  141. cid = VMCI_RESERVED_CID_LIMIT;
  142. }
  143. context->cid = cid;
  144. list_add_tail_rcu(&context->list_item, &ctx_list.head);
  145. spin_unlock(&ctx_list.lock);
  146. return context;
  147. err_free_db_array:
  148. vmci_handle_arr_destroy(context->doorbell_array);
  149. err_free_qp_array:
  150. vmci_handle_arr_destroy(context->queue_pair_array);
  151. err_free_ctx:
  152. kfree(context);
  153. err_out:
  154. return ERR_PTR(error);
  155. }
  156. /*
  157. * Destroy VMCI context.
  158. */
  159. void vmci_ctx_destroy(struct vmci_ctx *context)
  160. {
  161. spin_lock(&ctx_list.lock);
  162. list_del_rcu(&context->list_item);
  163. spin_unlock(&ctx_list.lock);
  164. synchronize_rcu();
  165. vmci_ctx_put(context);
  166. }
  167. /*
  168. * Fire notification for all contexts interested in given cid.
  169. */
  170. static int ctx_fire_notification(u32 context_id, u32 priv_flags)
  171. {
  172. u32 i, array_size;
  173. struct vmci_ctx *sub_ctx;
  174. struct vmci_handle_arr *subscriber_array;
  175. struct vmci_handle context_handle =
  176. vmci_make_handle(context_id, VMCI_EVENT_HANDLER);
  177. /*
  178. * We create an array to hold the subscribers we find when
  179. * scanning through all contexts.
  180. */
  181. subscriber_array = vmci_handle_arr_create(0, VMCI_MAX_CONTEXTS);
  182. if (subscriber_array == NULL)
  183. return VMCI_ERROR_NO_MEM;
  184. /*
  185. * Scan all contexts to find who is interested in being
  186. * notified about given contextID.
  187. */
  188. rcu_read_lock();
  189. list_for_each_entry_rcu(sub_ctx, &ctx_list.head, list_item) {
  190. struct vmci_handle_list *node;
  191. /*
  192. * We only deliver notifications of the removal of
  193. * contexts, if the two contexts are allowed to
  194. * interact.
  195. */
  196. if (vmci_deny_interaction(priv_flags, sub_ctx->priv_flags))
  197. continue;
  198. list_for_each_entry_rcu(node, &sub_ctx->notifier_list, node) {
  199. if (!vmci_handle_is_equal(node->handle, context_handle))
  200. continue;
  201. vmci_handle_arr_append_entry(&subscriber_array,
  202. vmci_make_handle(sub_ctx->cid,
  203. VMCI_EVENT_HANDLER));
  204. }
  205. }
  206. rcu_read_unlock();
  207. /* Fire event to all subscribers. */
  208. array_size = vmci_handle_arr_get_size(subscriber_array);
  209. for (i = 0; i < array_size; i++) {
  210. int result;
  211. struct vmci_event_ctx ev;
  212. ev.msg.hdr.dst = vmci_handle_arr_get_entry(subscriber_array, i);
  213. ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  214. VMCI_CONTEXT_RESOURCE_ID);
  215. ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
  216. memset((char*)&ev + sizeof(ev.msg.hdr), 0,
  217. ev.msg.hdr.payload_size);
  218. ev.msg.event_data.event = VMCI_EVENT_CTX_REMOVED;
  219. ev.payload.context_id = context_id;
  220. result = vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID,
  221. &ev.msg.hdr, false);
  222. if (result < VMCI_SUCCESS) {
  223. pr_devel("Failed to enqueue event datagram (type=%d) for context (ID=0x%x)\n",
  224. ev.msg.event_data.event,
  225. ev.msg.hdr.dst.context);
  226. /* We continue to enqueue on next subscriber. */
  227. }
  228. }
  229. vmci_handle_arr_destroy(subscriber_array);
  230. return VMCI_SUCCESS;
  231. }
  232. /*
  233. * Queues a VMCI datagram for the appropriate target VM context.
  234. */
  235. int vmci_ctx_enqueue_datagram(u32 cid, struct vmci_datagram *dg)
  236. {
  237. struct vmci_datagram_queue_entry *dq_entry;
  238. struct vmci_ctx *context;
  239. struct vmci_handle dg_src;
  240. size_t vmci_dg_size;
  241. vmci_dg_size = VMCI_DG_SIZE(dg);
  242. if (vmci_dg_size > VMCI_MAX_DG_SIZE) {
  243. pr_devel("Datagram too large (bytes=%zu)\n", vmci_dg_size);
  244. return VMCI_ERROR_INVALID_ARGS;
  245. }
  246. /* Get the target VM's VMCI context. */
  247. context = vmci_ctx_get(cid);
  248. if (!context) {
  249. pr_devel("Invalid context (ID=0x%x)\n", cid);
  250. return VMCI_ERROR_INVALID_ARGS;
  251. }
  252. /* Allocate guest call entry and add it to the target VM's queue. */
  253. dq_entry = kmalloc_obj(*dq_entry);
  254. if (dq_entry == NULL) {
  255. pr_warn("Failed to allocate memory for datagram\n");
  256. vmci_ctx_put(context);
  257. return VMCI_ERROR_NO_MEM;
  258. }
  259. dq_entry->dg = dg;
  260. dq_entry->dg_size = vmci_dg_size;
  261. dg_src = dg->src;
  262. INIT_LIST_HEAD(&dq_entry->list_item);
  263. spin_lock(&context->lock);
  264. /*
  265. * We put a higher limit on datagrams from the hypervisor. If
  266. * the pending datagram is not from hypervisor, then we check
  267. * if enqueueing it would exceed the
  268. * VMCI_MAX_DATAGRAM_QUEUE_SIZE limit on the destination. If
  269. * the pending datagram is from hypervisor, we allow it to be
  270. * queued at the destination side provided we don't reach the
  271. * VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE limit.
  272. */
  273. if (context->datagram_queue_size + vmci_dg_size >=
  274. VMCI_MAX_DATAGRAM_QUEUE_SIZE &&
  275. (!vmci_handle_is_equal(dg_src,
  276. vmci_make_handle
  277. (VMCI_HYPERVISOR_CONTEXT_ID,
  278. VMCI_CONTEXT_RESOURCE_ID)) ||
  279. context->datagram_queue_size + vmci_dg_size >=
  280. VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE)) {
  281. spin_unlock(&context->lock);
  282. vmci_ctx_put(context);
  283. kfree(dq_entry);
  284. pr_devel("Context (ID=0x%x) receive queue is full\n", cid);
  285. return VMCI_ERROR_NO_RESOURCES;
  286. }
  287. list_add(&dq_entry->list_item, &context->datagram_queue);
  288. context->pending_datagrams++;
  289. context->datagram_queue_size += vmci_dg_size;
  290. ctx_signal_notify(context);
  291. wake_up(&context->host_context.wait_queue);
  292. spin_unlock(&context->lock);
  293. vmci_ctx_put(context);
  294. return vmci_dg_size;
  295. }
  296. /*
  297. * Verifies whether a context with the specified context ID exists.
  298. * FIXME: utility is dubious as no decisions can be reliably made
  299. * using this data as context can appear and disappear at any time.
  300. */
  301. bool vmci_ctx_exists(u32 cid)
  302. {
  303. struct vmci_ctx *context;
  304. bool exists = false;
  305. rcu_read_lock();
  306. list_for_each_entry_rcu(context, &ctx_list.head, list_item) {
  307. if (context->cid == cid) {
  308. exists = true;
  309. break;
  310. }
  311. }
  312. rcu_read_unlock();
  313. return exists;
  314. }
  315. /*
  316. * Retrieves VMCI context corresponding to the given cid.
  317. */
  318. struct vmci_ctx *vmci_ctx_get(u32 cid)
  319. {
  320. struct vmci_ctx *c, *context = NULL;
  321. if (cid == VMCI_INVALID_ID)
  322. return NULL;
  323. rcu_read_lock();
  324. list_for_each_entry_rcu(c, &ctx_list.head, list_item) {
  325. if (c->cid == cid) {
  326. /*
  327. * The context owner drops its own reference to the
  328. * context only after removing it from the list and
  329. * waiting for RCU grace period to expire. This
  330. * means that we are not about to increase the
  331. * reference count of something that is in the
  332. * process of being destroyed.
  333. */
  334. context = c;
  335. kref_get(&context->kref);
  336. break;
  337. }
  338. }
  339. rcu_read_unlock();
  340. return context;
  341. }
  342. /*
  343. * Deallocates all parts of a context data structure. This
  344. * function doesn't lock the context, because it assumes that
  345. * the caller was holding the last reference to context.
  346. */
  347. static void ctx_free_ctx(struct kref *kref)
  348. {
  349. struct vmci_ctx *context = container_of(kref, struct vmci_ctx, kref);
  350. struct vmci_datagram_queue_entry *dq_entry, *dq_entry_tmp;
  351. struct vmci_handle temp_handle;
  352. struct vmci_handle_list *notifier, *tmp;
  353. /*
  354. * Fire event to all contexts interested in knowing this
  355. * context is dying.
  356. */
  357. ctx_fire_notification(context->cid, context->priv_flags);
  358. /*
  359. * Cleanup all queue pair resources attached to context. If
  360. * the VM dies without cleaning up, this code will make sure
  361. * that no resources are leaked.
  362. */
  363. temp_handle = vmci_handle_arr_get_entry(context->queue_pair_array, 0);
  364. while (!vmci_handle_is_equal(temp_handle, VMCI_INVALID_HANDLE)) {
  365. if (vmci_qp_broker_detach(temp_handle,
  366. context) < VMCI_SUCCESS) {
  367. /*
  368. * When vmci_qp_broker_detach() succeeds it
  369. * removes the handle from the array. If
  370. * detach fails, we must remove the handle
  371. * ourselves.
  372. */
  373. vmci_handle_arr_remove_entry(context->queue_pair_array,
  374. temp_handle);
  375. }
  376. temp_handle =
  377. vmci_handle_arr_get_entry(context->queue_pair_array, 0);
  378. }
  379. /*
  380. * It is fine to destroy this without locking the callQueue, as
  381. * this is the only thread having a reference to the context.
  382. */
  383. list_for_each_entry_safe(dq_entry, dq_entry_tmp,
  384. &context->datagram_queue, list_item) {
  385. WARN_ON(dq_entry->dg_size != VMCI_DG_SIZE(dq_entry->dg));
  386. list_del(&dq_entry->list_item);
  387. kfree(dq_entry->dg);
  388. kfree(dq_entry);
  389. }
  390. list_for_each_entry_safe(notifier, tmp,
  391. &context->notifier_list, node) {
  392. list_del(&notifier->node);
  393. kfree(notifier);
  394. }
  395. vmci_handle_arr_destroy(context->queue_pair_array);
  396. vmci_handle_arr_destroy(context->doorbell_array);
  397. vmci_handle_arr_destroy(context->pending_doorbell_array);
  398. vmci_ctx_unset_notify(context);
  399. if (context->cred)
  400. put_cred(context->cred);
  401. kfree(context);
  402. }
  403. /*
  404. * Drops reference to VMCI context. If this is the last reference to
  405. * the context it will be deallocated. A context is created with
  406. * a reference count of one, and on destroy, it is removed from
  407. * the context list before its reference count is decremented. Thus,
  408. * if we reach zero, we are sure that nobody else are about to increment
  409. * it (they need the entry in the context list for that), and so there
  410. * is no need for locking.
  411. */
  412. void vmci_ctx_put(struct vmci_ctx *context)
  413. {
  414. kref_put(&context->kref, ctx_free_ctx);
  415. }
  416. /*
  417. * Dequeues the next datagram and returns it to caller.
  418. * The caller passes in a pointer to the max size datagram
  419. * it can handle and the datagram is only unqueued if the
  420. * size is less than max_size. If larger max_size is set to
  421. * the size of the datagram to give the caller a chance to
  422. * set up a larger buffer for the guestcall.
  423. */
  424. int vmci_ctx_dequeue_datagram(struct vmci_ctx *context,
  425. size_t *max_size,
  426. struct vmci_datagram **dg)
  427. {
  428. struct vmci_datagram_queue_entry *dq_entry;
  429. struct list_head *list_item;
  430. int rv;
  431. /* Dequeue the next datagram entry. */
  432. spin_lock(&context->lock);
  433. if (context->pending_datagrams == 0) {
  434. ctx_clear_notify_call(context);
  435. spin_unlock(&context->lock);
  436. pr_devel("No datagrams pending\n");
  437. return VMCI_ERROR_NO_MORE_DATAGRAMS;
  438. }
  439. list_item = context->datagram_queue.next;
  440. dq_entry =
  441. list_entry(list_item, struct vmci_datagram_queue_entry, list_item);
  442. /* Check size of caller's buffer. */
  443. if (*max_size < dq_entry->dg_size) {
  444. *max_size = dq_entry->dg_size;
  445. spin_unlock(&context->lock);
  446. pr_devel("Caller's buffer should be at least (size=%u bytes)\n",
  447. (u32) *max_size);
  448. return VMCI_ERROR_NO_MEM;
  449. }
  450. list_del(list_item);
  451. context->pending_datagrams--;
  452. context->datagram_queue_size -= dq_entry->dg_size;
  453. if (context->pending_datagrams == 0) {
  454. ctx_clear_notify_call(context);
  455. rv = VMCI_SUCCESS;
  456. } else {
  457. /*
  458. * Return the size of the next datagram.
  459. */
  460. struct vmci_datagram_queue_entry *next_entry;
  461. list_item = context->datagram_queue.next;
  462. next_entry =
  463. list_entry(list_item, struct vmci_datagram_queue_entry,
  464. list_item);
  465. /*
  466. * The following size_t -> int truncation is fine as
  467. * the maximum size of a (routable) datagram is 68KB.
  468. */
  469. rv = (int)next_entry->dg_size;
  470. }
  471. spin_unlock(&context->lock);
  472. /* Caller must free datagram. */
  473. *dg = dq_entry->dg;
  474. dq_entry->dg = NULL;
  475. kfree(dq_entry);
  476. return rv;
  477. }
  478. /*
  479. * Reverts actions set up by vmci_setup_notify(). Unmaps and unlocks the
  480. * page mapped/locked by vmci_setup_notify().
  481. */
  482. void vmci_ctx_unset_notify(struct vmci_ctx *context)
  483. {
  484. struct page *notify_page;
  485. spin_lock(&context->lock);
  486. notify_page = context->notify_page;
  487. context->notify = &ctx_dummy_notify;
  488. context->notify_page = NULL;
  489. spin_unlock(&context->lock);
  490. if (notify_page) {
  491. kunmap(notify_page);
  492. put_page(notify_page);
  493. }
  494. }
  495. /*
  496. * Add remote_cid to list of contexts current contexts wants
  497. * notifications from/about.
  498. */
  499. int vmci_ctx_add_notification(u32 context_id, u32 remote_cid)
  500. {
  501. struct vmci_ctx *context;
  502. struct vmci_handle_list *notifier, *n;
  503. int result;
  504. bool exists = false;
  505. context = vmci_ctx_get(context_id);
  506. if (!context)
  507. return VMCI_ERROR_NOT_FOUND;
  508. if (VMCI_CONTEXT_IS_VM(context_id) && VMCI_CONTEXT_IS_VM(remote_cid)) {
  509. pr_devel("Context removed notifications for other VMs not supported (src=0x%x, remote=0x%x)\n",
  510. context_id, remote_cid);
  511. result = VMCI_ERROR_DST_UNREACHABLE;
  512. goto out;
  513. }
  514. if (context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED) {
  515. result = VMCI_ERROR_NO_ACCESS;
  516. goto out;
  517. }
  518. notifier = kmalloc_obj(struct vmci_handle_list);
  519. if (!notifier) {
  520. result = VMCI_ERROR_NO_MEM;
  521. goto out;
  522. }
  523. INIT_LIST_HEAD(&notifier->node);
  524. notifier->handle = vmci_make_handle(remote_cid, VMCI_EVENT_HANDLER);
  525. spin_lock(&context->lock);
  526. if (context->n_notifiers < VMCI_MAX_CONTEXTS) {
  527. list_for_each_entry(n, &context->notifier_list, node) {
  528. if (vmci_handle_is_equal(n->handle, notifier->handle)) {
  529. exists = true;
  530. break;
  531. }
  532. }
  533. if (exists) {
  534. kfree(notifier);
  535. result = VMCI_ERROR_ALREADY_EXISTS;
  536. } else {
  537. list_add_tail_rcu(&notifier->node,
  538. &context->notifier_list);
  539. context->n_notifiers++;
  540. result = VMCI_SUCCESS;
  541. }
  542. } else {
  543. kfree(notifier);
  544. result = VMCI_ERROR_NO_MEM;
  545. }
  546. spin_unlock(&context->lock);
  547. out:
  548. vmci_ctx_put(context);
  549. return result;
  550. }
  551. /*
  552. * Remove remote_cid from current context's list of contexts it is
  553. * interested in getting notifications from/about.
  554. */
  555. int vmci_ctx_remove_notification(u32 context_id, u32 remote_cid)
  556. {
  557. struct vmci_ctx *context;
  558. struct vmci_handle_list *notifier = NULL, *iter, *tmp;
  559. struct vmci_handle handle;
  560. context = vmci_ctx_get(context_id);
  561. if (!context)
  562. return VMCI_ERROR_NOT_FOUND;
  563. handle = vmci_make_handle(remote_cid, VMCI_EVENT_HANDLER);
  564. spin_lock(&context->lock);
  565. list_for_each_entry_safe(iter, tmp,
  566. &context->notifier_list, node) {
  567. if (vmci_handle_is_equal(iter->handle, handle)) {
  568. list_del_rcu(&iter->node);
  569. context->n_notifiers--;
  570. notifier = iter;
  571. break;
  572. }
  573. }
  574. spin_unlock(&context->lock);
  575. if (notifier)
  576. kvfree_rcu_mightsleep(notifier);
  577. vmci_ctx_put(context);
  578. return notifier ? VMCI_SUCCESS : VMCI_ERROR_NOT_FOUND;
  579. }
  580. static int vmci_ctx_get_chkpt_notifiers(struct vmci_ctx *context,
  581. u32 *buf_size, void **pbuf)
  582. {
  583. u32 *notifiers;
  584. size_t data_size;
  585. struct vmci_handle_list *entry;
  586. int i = 0;
  587. if (context->n_notifiers == 0) {
  588. *buf_size = 0;
  589. *pbuf = NULL;
  590. return VMCI_SUCCESS;
  591. }
  592. data_size = context->n_notifiers * sizeof(*notifiers);
  593. if (*buf_size < data_size) {
  594. *buf_size = data_size;
  595. return VMCI_ERROR_MORE_DATA;
  596. }
  597. notifiers = kmalloc(data_size, GFP_ATOMIC); /* FIXME: want GFP_KERNEL */
  598. if (!notifiers)
  599. return VMCI_ERROR_NO_MEM;
  600. list_for_each_entry(entry, &context->notifier_list, node)
  601. notifiers[i++] = entry->handle.context;
  602. *buf_size = data_size;
  603. *pbuf = notifiers;
  604. return VMCI_SUCCESS;
  605. }
  606. static int vmci_ctx_get_chkpt_doorbells(struct vmci_ctx *context,
  607. u32 *buf_size, void **pbuf)
  608. {
  609. struct dbell_cpt_state *dbells;
  610. u32 i, n_doorbells;
  611. n_doorbells = vmci_handle_arr_get_size(context->doorbell_array);
  612. if (n_doorbells > 0) {
  613. size_t data_size = n_doorbells * sizeof(*dbells);
  614. if (*buf_size < data_size) {
  615. *buf_size = data_size;
  616. return VMCI_ERROR_MORE_DATA;
  617. }
  618. dbells = kzalloc(data_size, GFP_ATOMIC);
  619. if (!dbells)
  620. return VMCI_ERROR_NO_MEM;
  621. for (i = 0; i < n_doorbells; i++)
  622. dbells[i].handle = vmci_handle_arr_get_entry(
  623. context->doorbell_array, i);
  624. *buf_size = data_size;
  625. *pbuf = dbells;
  626. } else {
  627. *buf_size = 0;
  628. *pbuf = NULL;
  629. }
  630. return VMCI_SUCCESS;
  631. }
  632. /*
  633. * Get current context's checkpoint state of given type.
  634. */
  635. int vmci_ctx_get_chkpt_state(u32 context_id,
  636. u32 cpt_type,
  637. u32 *buf_size,
  638. void **pbuf)
  639. {
  640. struct vmci_ctx *context;
  641. int result;
  642. context = vmci_ctx_get(context_id);
  643. if (!context)
  644. return VMCI_ERROR_NOT_FOUND;
  645. spin_lock(&context->lock);
  646. switch (cpt_type) {
  647. case VMCI_NOTIFICATION_CPT_STATE:
  648. result = vmci_ctx_get_chkpt_notifiers(context, buf_size, pbuf);
  649. break;
  650. case VMCI_WELLKNOWN_CPT_STATE:
  651. /*
  652. * For compatibility with VMX'en with VM to VM communication, we
  653. * always return zero wellknown handles.
  654. */
  655. *buf_size = 0;
  656. *pbuf = NULL;
  657. result = VMCI_SUCCESS;
  658. break;
  659. case VMCI_DOORBELL_CPT_STATE:
  660. result = vmci_ctx_get_chkpt_doorbells(context, buf_size, pbuf);
  661. break;
  662. default:
  663. pr_devel("Invalid cpt state (type=%d)\n", cpt_type);
  664. result = VMCI_ERROR_INVALID_ARGS;
  665. break;
  666. }
  667. spin_unlock(&context->lock);
  668. vmci_ctx_put(context);
  669. return result;
  670. }
  671. /*
  672. * Set current context's checkpoint state of given type.
  673. */
  674. int vmci_ctx_set_chkpt_state(u32 context_id,
  675. u32 cpt_type,
  676. u32 buf_size,
  677. void *cpt_buf)
  678. {
  679. u32 i;
  680. u32 current_id;
  681. int result = VMCI_SUCCESS;
  682. u32 num_ids = buf_size / sizeof(u32);
  683. if (cpt_type == VMCI_WELLKNOWN_CPT_STATE && num_ids > 0) {
  684. /*
  685. * We would end up here if VMX with VM to VM communication
  686. * attempts to restore a checkpoint with wellknown handles.
  687. */
  688. pr_warn("Attempt to restore checkpoint with obsolete wellknown handles\n");
  689. return VMCI_ERROR_OBSOLETE;
  690. }
  691. if (cpt_type != VMCI_NOTIFICATION_CPT_STATE) {
  692. pr_devel("Invalid cpt state (type=%d)\n", cpt_type);
  693. return VMCI_ERROR_INVALID_ARGS;
  694. }
  695. for (i = 0; i < num_ids && result == VMCI_SUCCESS; i++) {
  696. current_id = ((u32 *)cpt_buf)[i];
  697. result = vmci_ctx_add_notification(context_id, current_id);
  698. if (result != VMCI_SUCCESS)
  699. break;
  700. }
  701. if (result != VMCI_SUCCESS)
  702. pr_devel("Failed to set cpt state (type=%d) (error=%d)\n",
  703. cpt_type, result);
  704. return result;
  705. }
  706. /*
  707. * Retrieves the specified context's pending notifications in the
  708. * form of a handle array. The handle arrays returned are the
  709. * actual data - not a copy and should not be modified by the
  710. * caller. They must be released using
  711. * vmci_ctx_rcv_notifications_release.
  712. */
  713. int vmci_ctx_rcv_notifications_get(u32 context_id,
  714. struct vmci_handle_arr **db_handle_array,
  715. struct vmci_handle_arr **qp_handle_array)
  716. {
  717. struct vmci_ctx *context;
  718. int result = VMCI_SUCCESS;
  719. context = vmci_ctx_get(context_id);
  720. if (context == NULL)
  721. return VMCI_ERROR_NOT_FOUND;
  722. spin_lock(&context->lock);
  723. *db_handle_array = context->pending_doorbell_array;
  724. context->pending_doorbell_array =
  725. vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT);
  726. if (!context->pending_doorbell_array) {
  727. context->pending_doorbell_array = *db_handle_array;
  728. *db_handle_array = NULL;
  729. result = VMCI_ERROR_NO_MEM;
  730. }
  731. *qp_handle_array = NULL;
  732. spin_unlock(&context->lock);
  733. vmci_ctx_put(context);
  734. return result;
  735. }
  736. /*
  737. * Releases handle arrays with pending notifications previously
  738. * retrieved using vmci_ctx_rcv_notifications_get. If the
  739. * notifications were not successfully handed over to the guest,
  740. * success must be false.
  741. */
  742. void vmci_ctx_rcv_notifications_release(u32 context_id,
  743. struct vmci_handle_arr *db_handle_array,
  744. struct vmci_handle_arr *qp_handle_array,
  745. bool success)
  746. {
  747. struct vmci_ctx *context = vmci_ctx_get(context_id);
  748. spin_lock(&context->lock);
  749. if (!success) {
  750. struct vmci_handle handle;
  751. /*
  752. * New notifications may have been added while we were not
  753. * holding the context lock, so we transfer any new pending
  754. * doorbell notifications to the old array, and reinstate the
  755. * old array.
  756. */
  757. handle = vmci_handle_arr_remove_tail(
  758. context->pending_doorbell_array);
  759. while (!vmci_handle_is_invalid(handle)) {
  760. if (!vmci_handle_arr_has_entry(db_handle_array,
  761. handle)) {
  762. vmci_handle_arr_append_entry(
  763. &db_handle_array, handle);
  764. }
  765. handle = vmci_handle_arr_remove_tail(
  766. context->pending_doorbell_array);
  767. }
  768. vmci_handle_arr_destroy(context->pending_doorbell_array);
  769. context->pending_doorbell_array = db_handle_array;
  770. db_handle_array = NULL;
  771. } else {
  772. ctx_clear_notify_call(context);
  773. }
  774. spin_unlock(&context->lock);
  775. vmci_ctx_put(context);
  776. if (db_handle_array)
  777. vmci_handle_arr_destroy(db_handle_array);
  778. if (qp_handle_array)
  779. vmci_handle_arr_destroy(qp_handle_array);
  780. }
  781. /*
  782. * Registers that a new doorbell handle has been allocated by the
  783. * context. Only doorbell handles registered can be notified.
  784. */
  785. int vmci_ctx_dbell_create(u32 context_id, struct vmci_handle handle)
  786. {
  787. struct vmci_ctx *context;
  788. int result;
  789. if (context_id == VMCI_INVALID_ID || vmci_handle_is_invalid(handle))
  790. return VMCI_ERROR_INVALID_ARGS;
  791. context = vmci_ctx_get(context_id);
  792. if (context == NULL)
  793. return VMCI_ERROR_NOT_FOUND;
  794. spin_lock(&context->lock);
  795. if (!vmci_handle_arr_has_entry(context->doorbell_array, handle))
  796. result = vmci_handle_arr_append_entry(&context->doorbell_array,
  797. handle);
  798. else
  799. result = VMCI_ERROR_DUPLICATE_ENTRY;
  800. spin_unlock(&context->lock);
  801. vmci_ctx_put(context);
  802. return result;
  803. }
  804. /*
  805. * Unregisters a doorbell handle that was previously registered
  806. * with vmci_ctx_dbell_create.
  807. */
  808. int vmci_ctx_dbell_destroy(u32 context_id, struct vmci_handle handle)
  809. {
  810. struct vmci_ctx *context;
  811. struct vmci_handle removed_handle;
  812. if (context_id == VMCI_INVALID_ID || vmci_handle_is_invalid(handle))
  813. return VMCI_ERROR_INVALID_ARGS;
  814. context = vmci_ctx_get(context_id);
  815. if (context == NULL)
  816. return VMCI_ERROR_NOT_FOUND;
  817. spin_lock(&context->lock);
  818. removed_handle =
  819. vmci_handle_arr_remove_entry(context->doorbell_array, handle);
  820. vmci_handle_arr_remove_entry(context->pending_doorbell_array, handle);
  821. spin_unlock(&context->lock);
  822. vmci_ctx_put(context);
  823. return vmci_handle_is_invalid(removed_handle) ?
  824. VMCI_ERROR_NOT_FOUND : VMCI_SUCCESS;
  825. }
  826. /*
  827. * Registers a notification of a doorbell handle initiated by the
  828. * specified source context. The notification of doorbells are
  829. * subject to the same isolation rules as datagram delivery. To
  830. * allow host side senders of notifications a finer granularity
  831. * of sender rights than those assigned to the sending context
  832. * itself, the host context is required to specify a different
  833. * set of privilege flags that will override the privileges of
  834. * the source context.
  835. */
  836. int vmci_ctx_notify_dbell(u32 src_cid,
  837. struct vmci_handle handle,
  838. u32 src_priv_flags)
  839. {
  840. struct vmci_ctx *dst_context;
  841. int result;
  842. if (vmci_handle_is_invalid(handle))
  843. return VMCI_ERROR_INVALID_ARGS;
  844. /* Get the target VM's VMCI context. */
  845. dst_context = vmci_ctx_get(handle.context);
  846. if (!dst_context) {
  847. pr_devel("Invalid context (ID=0x%x)\n", handle.context);
  848. return VMCI_ERROR_NOT_FOUND;
  849. }
  850. if (src_cid != handle.context) {
  851. u32 dst_priv_flags;
  852. if (VMCI_CONTEXT_IS_VM(src_cid) &&
  853. VMCI_CONTEXT_IS_VM(handle.context)) {
  854. pr_devel("Doorbell notification from VM to VM not supported (src=0x%x, dst=0x%x)\n",
  855. src_cid, handle.context);
  856. result = VMCI_ERROR_DST_UNREACHABLE;
  857. goto out;
  858. }
  859. result = vmci_dbell_get_priv_flags(handle, &dst_priv_flags);
  860. if (result < VMCI_SUCCESS) {
  861. pr_warn("Failed to get privilege flags for destination (handle=0x%x:0x%x)\n",
  862. handle.context, handle.resource);
  863. goto out;
  864. }
  865. if (src_cid != VMCI_HOST_CONTEXT_ID ||
  866. src_priv_flags == VMCI_NO_PRIVILEGE_FLAGS) {
  867. src_priv_flags = vmci_context_get_priv_flags(src_cid);
  868. }
  869. if (vmci_deny_interaction(src_priv_flags, dst_priv_flags)) {
  870. result = VMCI_ERROR_NO_ACCESS;
  871. goto out;
  872. }
  873. }
  874. if (handle.context == VMCI_HOST_CONTEXT_ID) {
  875. result = vmci_dbell_host_context_notify(src_cid, handle);
  876. } else {
  877. spin_lock(&dst_context->lock);
  878. if (!vmci_handle_arr_has_entry(dst_context->doorbell_array,
  879. handle)) {
  880. result = VMCI_ERROR_NOT_FOUND;
  881. } else {
  882. if (!vmci_handle_arr_has_entry(
  883. dst_context->pending_doorbell_array,
  884. handle)) {
  885. result = vmci_handle_arr_append_entry(
  886. &dst_context->pending_doorbell_array,
  887. handle);
  888. if (result == VMCI_SUCCESS) {
  889. ctx_signal_notify(dst_context);
  890. wake_up(&dst_context->host_context.wait_queue);
  891. }
  892. } else {
  893. result = VMCI_SUCCESS;
  894. }
  895. }
  896. spin_unlock(&dst_context->lock);
  897. }
  898. out:
  899. vmci_ctx_put(dst_context);
  900. return result;
  901. }
  902. bool vmci_ctx_supports_host_qp(struct vmci_ctx *context)
  903. {
  904. return context && context->user_version >= VMCI_VERSION_HOSTQP;
  905. }
  906. /*
  907. * Registers that a new queue pair handle has been allocated by
  908. * the context.
  909. */
  910. int vmci_ctx_qp_create(struct vmci_ctx *context, struct vmci_handle handle)
  911. {
  912. int result;
  913. if (context == NULL || vmci_handle_is_invalid(handle))
  914. return VMCI_ERROR_INVALID_ARGS;
  915. if (!vmci_handle_arr_has_entry(context->queue_pair_array, handle))
  916. result = vmci_handle_arr_append_entry(
  917. &context->queue_pair_array, handle);
  918. else
  919. result = VMCI_ERROR_DUPLICATE_ENTRY;
  920. return result;
  921. }
  922. /*
  923. * Unregisters a queue pair handle that was previously registered
  924. * with vmci_ctx_qp_create.
  925. */
  926. int vmci_ctx_qp_destroy(struct vmci_ctx *context, struct vmci_handle handle)
  927. {
  928. struct vmci_handle hndl;
  929. if (context == NULL || vmci_handle_is_invalid(handle))
  930. return VMCI_ERROR_INVALID_ARGS;
  931. hndl = vmci_handle_arr_remove_entry(context->queue_pair_array, handle);
  932. return vmci_handle_is_invalid(hndl) ?
  933. VMCI_ERROR_NOT_FOUND : VMCI_SUCCESS;
  934. }
  935. /*
  936. * Determines whether a given queue pair handle is registered
  937. * with the given context.
  938. */
  939. bool vmci_ctx_qp_exists(struct vmci_ctx *context, struct vmci_handle handle)
  940. {
  941. if (context == NULL || vmci_handle_is_invalid(handle))
  942. return false;
  943. return vmci_handle_arr_has_entry(context->queue_pair_array, handle);
  944. }
  945. /*
  946. * vmci_context_get_priv_flags() - Retrieve privilege flags.
  947. * @context_id: The context ID of the VMCI context.
  948. *
  949. * Retrieves privilege flags of the given VMCI context ID.
  950. */
  951. u32 vmci_context_get_priv_flags(u32 context_id)
  952. {
  953. if (vmci_host_code_active()) {
  954. u32 flags;
  955. struct vmci_ctx *context;
  956. context = vmci_ctx_get(context_id);
  957. if (!context)
  958. return VMCI_LEAST_PRIVILEGE_FLAGS;
  959. flags = context->priv_flags;
  960. vmci_ctx_put(context);
  961. return flags;
  962. }
  963. return VMCI_NO_PRIVILEGE_FLAGS;
  964. }
  965. EXPORT_SYMBOL_GPL(vmci_context_get_priv_flags);
  966. /*
  967. * vmci_is_context_owner() - Determimnes if user is the context owner
  968. * @context_id: The context ID of the VMCI context.
  969. * @uid: The host user id (real kernel value).
  970. *
  971. * Determines whether a given UID is the owner of given VMCI context.
  972. */
  973. bool vmci_is_context_owner(u32 context_id, kuid_t uid)
  974. {
  975. bool is_owner = false;
  976. if (vmci_host_code_active()) {
  977. struct vmci_ctx *context = vmci_ctx_get(context_id);
  978. if (context) {
  979. if (context->cred)
  980. is_owner = uid_eq(context->cred->uid, uid);
  981. vmci_ctx_put(context);
  982. }
  983. }
  984. return is_owner;
  985. }
  986. EXPORT_SYMBOL_GPL(vmci_is_context_owner);