vmci_context.c 31 KB

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