vmci_doorbell.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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/completion.h>
  18. #include <linux/hash.h>
  19. #include <linux/kernel.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include "vmci_datagram.h"
  25. #include "vmci_doorbell.h"
  26. #include "vmci_resource.h"
  27. #include "vmci_driver.h"
  28. #include "vmci_route.h"
  29. #define VMCI_DOORBELL_INDEX_BITS 6
  30. #define VMCI_DOORBELL_INDEX_TABLE_SIZE (1 << VMCI_DOORBELL_INDEX_BITS)
  31. #define VMCI_DOORBELL_HASH(_idx) hash_32(_idx, VMCI_DOORBELL_INDEX_BITS)
  32. /*
  33. * DoorbellEntry describes the a doorbell notification handle allocated by the
  34. * host.
  35. */
  36. struct dbell_entry {
  37. struct vmci_resource resource;
  38. struct hlist_node node;
  39. struct work_struct work;
  40. vmci_callback notify_cb;
  41. void *client_data;
  42. u32 idx;
  43. u32 priv_flags;
  44. bool run_delayed;
  45. atomic_t active; /* Only used by guest personality */
  46. };
  47. /* The VMCI index table keeps track of currently registered doorbells. */
  48. struct dbell_index_table {
  49. spinlock_t lock; /* Index table lock */
  50. struct hlist_head entries[VMCI_DOORBELL_INDEX_TABLE_SIZE];
  51. };
  52. static struct dbell_index_table vmci_doorbell_it = {
  53. .lock = __SPIN_LOCK_UNLOCKED(vmci_doorbell_it.lock),
  54. };
  55. /*
  56. * The max_notify_idx is one larger than the currently known bitmap index in
  57. * use, and is used to determine how much of the bitmap needs to be scanned.
  58. */
  59. static u32 max_notify_idx;
  60. /*
  61. * The notify_idx_count is used for determining whether there are free entries
  62. * within the bitmap (if notify_idx_count + 1 < max_notify_idx).
  63. */
  64. static u32 notify_idx_count;
  65. /*
  66. * The last_notify_idx_reserved is used to track the last index handed out - in
  67. * the case where multiple handles share a notification index, we hand out
  68. * indexes round robin based on last_notify_idx_reserved.
  69. */
  70. static u32 last_notify_idx_reserved;
  71. /* This is a one entry cache used to by the index allocation. */
  72. static u32 last_notify_idx_released = PAGE_SIZE;
  73. /*
  74. * Utility function that retrieves the privilege flags associated
  75. * with a given doorbell handle. For guest endpoints, the
  76. * privileges are determined by the context ID, but for host
  77. * endpoints privileges are associated with the complete
  78. * handle. Hypervisor endpoints are not yet supported.
  79. */
  80. int vmci_dbell_get_priv_flags(struct vmci_handle handle, u32 *priv_flags)
  81. {
  82. if (priv_flags == NULL || handle.context == VMCI_INVALID_ID)
  83. return VMCI_ERROR_INVALID_ARGS;
  84. if (handle.context == VMCI_HOST_CONTEXT_ID) {
  85. struct dbell_entry *entry;
  86. struct vmci_resource *resource;
  87. resource = vmci_resource_by_handle(handle,
  88. VMCI_RESOURCE_TYPE_DOORBELL);
  89. if (!resource)
  90. return VMCI_ERROR_NOT_FOUND;
  91. entry = container_of(resource, struct dbell_entry, resource);
  92. *priv_flags = entry->priv_flags;
  93. vmci_resource_put(resource);
  94. } else if (handle.context == VMCI_HYPERVISOR_CONTEXT_ID) {
  95. /*
  96. * Hypervisor endpoints for notifications are not
  97. * supported (yet).
  98. */
  99. return VMCI_ERROR_INVALID_ARGS;
  100. } else {
  101. *priv_flags = vmci_context_get_priv_flags(handle.context);
  102. }
  103. return VMCI_SUCCESS;
  104. }
  105. /*
  106. * Find doorbell entry by bitmap index.
  107. */
  108. static struct dbell_entry *dbell_index_table_find(u32 idx)
  109. {
  110. u32 bucket = VMCI_DOORBELL_HASH(idx);
  111. struct dbell_entry *dbell;
  112. hlist_for_each_entry(dbell, &vmci_doorbell_it.entries[bucket],
  113. node) {
  114. if (idx == dbell->idx)
  115. return dbell;
  116. }
  117. return NULL;
  118. }
  119. /*
  120. * Add the given entry to the index table. This willi take a reference to the
  121. * entry's resource so that the entry is not deleted before it is removed from
  122. * the * table.
  123. */
  124. static void dbell_index_table_add(struct dbell_entry *entry)
  125. {
  126. u32 bucket;
  127. u32 new_notify_idx;
  128. vmci_resource_get(&entry->resource);
  129. spin_lock_bh(&vmci_doorbell_it.lock);
  130. /*
  131. * Below we try to allocate an index in the notification
  132. * bitmap with "not too much" sharing between resources. If we
  133. * use less that the full bitmap, we either add to the end if
  134. * there are no unused flags within the currently used area,
  135. * or we search for unused ones. If we use the full bitmap, we
  136. * allocate the index round robin.
  137. */
  138. if (max_notify_idx < PAGE_SIZE || notify_idx_count < PAGE_SIZE) {
  139. if (last_notify_idx_released < max_notify_idx &&
  140. !dbell_index_table_find(last_notify_idx_released)) {
  141. new_notify_idx = last_notify_idx_released;
  142. last_notify_idx_released = PAGE_SIZE;
  143. } else {
  144. bool reused = false;
  145. new_notify_idx = last_notify_idx_reserved;
  146. if (notify_idx_count + 1 < max_notify_idx) {
  147. do {
  148. if (!dbell_index_table_find
  149. (new_notify_idx)) {
  150. reused = true;
  151. break;
  152. }
  153. new_notify_idx = (new_notify_idx + 1) %
  154. max_notify_idx;
  155. } while (new_notify_idx !=
  156. last_notify_idx_released);
  157. }
  158. if (!reused) {
  159. new_notify_idx = max_notify_idx;
  160. max_notify_idx++;
  161. }
  162. }
  163. } else {
  164. new_notify_idx = (last_notify_idx_reserved + 1) % PAGE_SIZE;
  165. }
  166. last_notify_idx_reserved = new_notify_idx;
  167. notify_idx_count++;
  168. entry->idx = new_notify_idx;
  169. bucket = VMCI_DOORBELL_HASH(entry->idx);
  170. hlist_add_head(&entry->node, &vmci_doorbell_it.entries[bucket]);
  171. spin_unlock_bh(&vmci_doorbell_it.lock);
  172. }
  173. /*
  174. * Remove the given entry from the index table. This will release() the
  175. * entry's resource.
  176. */
  177. static void dbell_index_table_remove(struct dbell_entry *entry)
  178. {
  179. spin_lock_bh(&vmci_doorbell_it.lock);
  180. hlist_del_init(&entry->node);
  181. notify_idx_count--;
  182. if (entry->idx == max_notify_idx - 1) {
  183. /*
  184. * If we delete an entry with the maximum known
  185. * notification index, we take the opportunity to
  186. * prune the current max. As there might be other
  187. * unused indices immediately below, we lower the
  188. * maximum until we hit an index in use.
  189. */
  190. while (max_notify_idx > 0 &&
  191. !dbell_index_table_find(max_notify_idx - 1))
  192. max_notify_idx--;
  193. }
  194. last_notify_idx_released = entry->idx;
  195. spin_unlock_bh(&vmci_doorbell_it.lock);
  196. vmci_resource_put(&entry->resource);
  197. }
  198. /*
  199. * Creates a link between the given doorbell handle and the given
  200. * index in the bitmap in the device backend. A notification state
  201. * is created in hypervisor.
  202. */
  203. static int dbell_link(struct vmci_handle handle, u32 notify_idx)
  204. {
  205. struct vmci_doorbell_link_msg link_msg;
  206. link_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  207. VMCI_DOORBELL_LINK);
  208. link_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
  209. link_msg.hdr.payload_size = sizeof(link_msg) - VMCI_DG_HEADERSIZE;
  210. link_msg.handle = handle;
  211. link_msg.notify_idx = notify_idx;
  212. return vmci_send_datagram(&link_msg.hdr);
  213. }
  214. /*
  215. * Unlinks the given doorbell handle from an index in the bitmap in
  216. * the device backend. The notification state is destroyed in hypervisor.
  217. */
  218. static int dbell_unlink(struct vmci_handle handle)
  219. {
  220. struct vmci_doorbell_unlink_msg unlink_msg;
  221. unlink_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  222. VMCI_DOORBELL_UNLINK);
  223. unlink_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
  224. unlink_msg.hdr.payload_size = sizeof(unlink_msg) - VMCI_DG_HEADERSIZE;
  225. unlink_msg.handle = handle;
  226. return vmci_send_datagram(&unlink_msg.hdr);
  227. }
  228. /*
  229. * Notify another guest or the host. We send a datagram down to the
  230. * host via the hypervisor with the notification info.
  231. */
  232. static int dbell_notify_as_guest(struct vmci_handle handle, u32 priv_flags)
  233. {
  234. struct vmci_doorbell_notify_msg notify_msg;
  235. notify_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  236. VMCI_DOORBELL_NOTIFY);
  237. notify_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
  238. notify_msg.hdr.payload_size = sizeof(notify_msg) - VMCI_DG_HEADERSIZE;
  239. notify_msg.handle = handle;
  240. return vmci_send_datagram(&notify_msg.hdr);
  241. }
  242. /*
  243. * Calls the specified callback in a delayed context.
  244. */
  245. static void dbell_delayed_dispatch(struct work_struct *work)
  246. {
  247. struct dbell_entry *entry = container_of(work,
  248. struct dbell_entry, work);
  249. entry->notify_cb(entry->client_data);
  250. vmci_resource_put(&entry->resource);
  251. }
  252. /*
  253. * Dispatches a doorbell notification to the host context.
  254. */
  255. int vmci_dbell_host_context_notify(u32 src_cid, struct vmci_handle handle)
  256. {
  257. struct dbell_entry *entry;
  258. struct vmci_resource *resource;
  259. if (vmci_handle_is_invalid(handle)) {
  260. pr_devel("Notifying an invalid doorbell (handle=0x%x:0x%x)\n",
  261. handle.context, handle.resource);
  262. return VMCI_ERROR_INVALID_ARGS;
  263. }
  264. resource = vmci_resource_by_handle(handle,
  265. VMCI_RESOURCE_TYPE_DOORBELL);
  266. if (!resource) {
  267. pr_devel("Notifying an unknown doorbell (handle=0x%x:0x%x)\n",
  268. handle.context, handle.resource);
  269. return VMCI_ERROR_NOT_FOUND;
  270. }
  271. entry = container_of(resource, struct dbell_entry, resource);
  272. if (entry->run_delayed) {
  273. if (!schedule_work(&entry->work))
  274. vmci_resource_put(resource);
  275. } else {
  276. entry->notify_cb(entry->client_data);
  277. vmci_resource_put(resource);
  278. }
  279. return VMCI_SUCCESS;
  280. }
  281. /*
  282. * Register the notification bitmap with the host.
  283. */
  284. bool vmci_dbell_register_notification_bitmap(u32 bitmap_ppn)
  285. {
  286. int result;
  287. struct vmci_notify_bm_set_msg bitmap_set_msg;
  288. bitmap_set_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  289. VMCI_SET_NOTIFY_BITMAP);
  290. bitmap_set_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
  291. bitmap_set_msg.hdr.payload_size = sizeof(bitmap_set_msg) -
  292. VMCI_DG_HEADERSIZE;
  293. bitmap_set_msg.bitmap_ppn = bitmap_ppn;
  294. result = vmci_send_datagram(&bitmap_set_msg.hdr);
  295. if (result != VMCI_SUCCESS) {
  296. pr_devel("Failed to register (PPN=%u) as notification bitmap (error=%d)\n",
  297. bitmap_ppn, result);
  298. return false;
  299. }
  300. return true;
  301. }
  302. /*
  303. * Executes or schedules the handlers for a given notify index.
  304. */
  305. static void dbell_fire_entries(u32 notify_idx)
  306. {
  307. u32 bucket = VMCI_DOORBELL_HASH(notify_idx);
  308. struct dbell_entry *dbell;
  309. spin_lock_bh(&vmci_doorbell_it.lock);
  310. hlist_for_each_entry(dbell, &vmci_doorbell_it.entries[bucket], node) {
  311. if (dbell->idx == notify_idx &&
  312. atomic_read(&dbell->active) == 1) {
  313. if (dbell->run_delayed) {
  314. vmci_resource_get(&dbell->resource);
  315. if (!schedule_work(&dbell->work))
  316. vmci_resource_put(&dbell->resource);
  317. } else {
  318. dbell->notify_cb(dbell->client_data);
  319. }
  320. }
  321. }
  322. spin_unlock_bh(&vmci_doorbell_it.lock);
  323. }
  324. /*
  325. * Scans the notification bitmap, collects pending notifications,
  326. * resets the bitmap and invokes appropriate callbacks.
  327. */
  328. void vmci_dbell_scan_notification_entries(u8 *bitmap)
  329. {
  330. u32 idx;
  331. for (idx = 0; idx < max_notify_idx; idx++) {
  332. if (bitmap[idx] & 0x1) {
  333. bitmap[idx] &= ~1;
  334. dbell_fire_entries(idx);
  335. }
  336. }
  337. }
  338. /*
  339. * vmci_doorbell_create() - Creates a doorbell
  340. * @handle: A handle used to track the resource. Can be invalid.
  341. * @flags: Flag that determines context of callback.
  342. * @priv_flags: Privileges flags.
  343. * @notify_cb: The callback to be ivoked when the doorbell fires.
  344. * @client_data: A parameter to be passed to the callback.
  345. *
  346. * Creates a doorbell with the given callback. If the handle is
  347. * VMCI_INVALID_HANDLE, a free handle will be assigned, if
  348. * possible. The callback can be run immediately (potentially with
  349. * locks held - the default) or delayed (in a kernel thread) by
  350. * specifying the flag VMCI_FLAG_DELAYED_CB. If delayed execution
  351. * is selected, a given callback may not be run if the kernel is
  352. * unable to allocate memory for the delayed execution (highly
  353. * unlikely).
  354. */
  355. int vmci_doorbell_create(struct vmci_handle *handle,
  356. u32 flags,
  357. u32 priv_flags,
  358. vmci_callback notify_cb, void *client_data)
  359. {
  360. struct dbell_entry *entry;
  361. struct vmci_handle new_handle;
  362. int result;
  363. if (!handle || !notify_cb || flags & ~VMCI_FLAG_DELAYED_CB ||
  364. priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS)
  365. return VMCI_ERROR_INVALID_ARGS;
  366. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  367. if (entry == NULL) {
  368. pr_warn("Failed allocating memory for datagram entry\n");
  369. return VMCI_ERROR_NO_MEM;
  370. }
  371. if (vmci_handle_is_invalid(*handle)) {
  372. u32 context_id = vmci_get_context_id();
  373. if (context_id == VMCI_INVALID_ID) {
  374. pr_warn("Failed to get context ID\n");
  375. result = VMCI_ERROR_NO_RESOURCES;
  376. goto free_mem;
  377. }
  378. /* Let resource code allocate a free ID for us */
  379. new_handle = vmci_make_handle(context_id, VMCI_INVALID_ID);
  380. } else {
  381. bool valid_context = false;
  382. /*
  383. * Validate the handle. We must do both of the checks below
  384. * because we can be acting as both a host and a guest at the
  385. * same time. We always allow the host context ID, since the
  386. * host functionality is in practice always there with the
  387. * unified driver.
  388. */
  389. if (handle->context == VMCI_HOST_CONTEXT_ID ||
  390. (vmci_guest_code_active() &&
  391. vmci_get_context_id() == handle->context)) {
  392. valid_context = true;
  393. }
  394. if (!valid_context || handle->resource == VMCI_INVALID_ID) {
  395. pr_devel("Invalid argument (handle=0x%x:0x%x)\n",
  396. handle->context, handle->resource);
  397. result = VMCI_ERROR_INVALID_ARGS;
  398. goto free_mem;
  399. }
  400. new_handle = *handle;
  401. }
  402. entry->idx = 0;
  403. INIT_HLIST_NODE(&entry->node);
  404. entry->priv_flags = priv_flags;
  405. INIT_WORK(&entry->work, dbell_delayed_dispatch);
  406. entry->run_delayed = flags & VMCI_FLAG_DELAYED_CB;
  407. entry->notify_cb = notify_cb;
  408. entry->client_data = client_data;
  409. atomic_set(&entry->active, 0);
  410. result = vmci_resource_add(&entry->resource,
  411. VMCI_RESOURCE_TYPE_DOORBELL,
  412. new_handle);
  413. if (result != VMCI_SUCCESS) {
  414. pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d\n",
  415. new_handle.context, new_handle.resource, result);
  416. goto free_mem;
  417. }
  418. new_handle = vmci_resource_handle(&entry->resource);
  419. if (vmci_guest_code_active()) {
  420. dbell_index_table_add(entry);
  421. result = dbell_link(new_handle, entry->idx);
  422. if (VMCI_SUCCESS != result)
  423. goto destroy_resource;
  424. atomic_set(&entry->active, 1);
  425. }
  426. *handle = new_handle;
  427. return result;
  428. destroy_resource:
  429. dbell_index_table_remove(entry);
  430. vmci_resource_remove(&entry->resource);
  431. free_mem:
  432. kfree(entry);
  433. return result;
  434. }
  435. EXPORT_SYMBOL_GPL(vmci_doorbell_create);
  436. /*
  437. * vmci_doorbell_destroy() - Destroy a doorbell.
  438. * @handle: The handle tracking the resource.
  439. *
  440. * Destroys a doorbell previously created with vmcii_doorbell_create. This
  441. * operation may block waiting for a callback to finish.
  442. */
  443. int vmci_doorbell_destroy(struct vmci_handle handle)
  444. {
  445. struct dbell_entry *entry;
  446. struct vmci_resource *resource;
  447. if (vmci_handle_is_invalid(handle))
  448. return VMCI_ERROR_INVALID_ARGS;
  449. resource = vmci_resource_by_handle(handle,
  450. VMCI_RESOURCE_TYPE_DOORBELL);
  451. if (!resource) {
  452. pr_devel("Failed to destroy doorbell (handle=0x%x:0x%x)\n",
  453. handle.context, handle.resource);
  454. return VMCI_ERROR_NOT_FOUND;
  455. }
  456. entry = container_of(resource, struct dbell_entry, resource);
  457. if (!hlist_unhashed(&entry->node)) {
  458. int result;
  459. dbell_index_table_remove(entry);
  460. result = dbell_unlink(handle);
  461. if (VMCI_SUCCESS != result) {
  462. /*
  463. * The only reason this should fail would be
  464. * an inconsistency between guest and
  465. * hypervisor state, where the guest believes
  466. * it has an active registration whereas the
  467. * hypervisor doesn't. One case where this may
  468. * happen is if a doorbell is unregistered
  469. * following a hibernation at a time where the
  470. * doorbell state hasn't been restored on the
  471. * hypervisor side yet. Since the handle has
  472. * now been removed in the guest, we just
  473. * print a warning and return success.
  474. */
  475. pr_devel("Unlink of doorbell (handle=0x%x:0x%x) unknown by hypervisor (error=%d)\n",
  476. handle.context, handle.resource, result);
  477. }
  478. }
  479. /*
  480. * Now remove the resource from the table. It might still be in use
  481. * after this, in a callback or still on the delayed work queue.
  482. */
  483. vmci_resource_put(&entry->resource);
  484. vmci_resource_remove(&entry->resource);
  485. kfree(entry);
  486. return VMCI_SUCCESS;
  487. }
  488. EXPORT_SYMBOL_GPL(vmci_doorbell_destroy);
  489. /*
  490. * vmci_doorbell_notify() - Ring the doorbell (and hide in the bushes).
  491. * @dst: The handlle identifying the doorbell resource
  492. * @priv_flags: Priviledge flags.
  493. *
  494. * Generates a notification on the doorbell identified by the
  495. * handle. For host side generation of notifications, the caller
  496. * can specify what the privilege of the calling side is.
  497. */
  498. int vmci_doorbell_notify(struct vmci_handle dst, u32 priv_flags)
  499. {
  500. int retval;
  501. enum vmci_route route;
  502. struct vmci_handle src;
  503. if (vmci_handle_is_invalid(dst) ||
  504. (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS))
  505. return VMCI_ERROR_INVALID_ARGS;
  506. src = VMCI_INVALID_HANDLE;
  507. retval = vmci_route(&src, &dst, false, &route);
  508. if (retval < VMCI_SUCCESS)
  509. return retval;
  510. if (VMCI_ROUTE_AS_HOST == route)
  511. return vmci_ctx_notify_dbell(VMCI_HOST_CONTEXT_ID,
  512. dst, priv_flags);
  513. if (VMCI_ROUTE_AS_GUEST == route)
  514. return dbell_notify_as_guest(dst, priv_flags);
  515. pr_warn("Unknown route (%d) for doorbell\n", route);
  516. return VMCI_ERROR_DST_UNREACHABLE;
  517. }
  518. EXPORT_SYMBOL_GPL(vmci_doorbell_notify);