vmci_doorbell.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. struct hlist_node *node;
  113. hlist_for_each_entry(dbell, node, &vmci_doorbell_it.entries[bucket],
  114. node) {
  115. if (idx == dbell->idx)
  116. return dbell;
  117. }
  118. return NULL;
  119. }
  120. /*
  121. * Add the given entry to the index table. This willi take a reference to the
  122. * entry's resource so that the entry is not deleted before it is removed from
  123. * the * table.
  124. */
  125. static void dbell_index_table_add(struct dbell_entry *entry)
  126. {
  127. u32 bucket;
  128. u32 new_notify_idx;
  129. vmci_resource_get(&entry->resource);
  130. spin_lock_bh(&vmci_doorbell_it.lock);
  131. /*
  132. * Below we try to allocate an index in the notification
  133. * bitmap with "not too much" sharing between resources. If we
  134. * use less that the full bitmap, we either add to the end if
  135. * there are no unused flags within the currently used area,
  136. * or we search for unused ones. If we use the full bitmap, we
  137. * allocate the index round robin.
  138. */
  139. if (max_notify_idx < PAGE_SIZE || notify_idx_count < PAGE_SIZE) {
  140. if (last_notify_idx_released < max_notify_idx &&
  141. !dbell_index_table_find(last_notify_idx_released)) {
  142. new_notify_idx = last_notify_idx_released;
  143. last_notify_idx_released = PAGE_SIZE;
  144. } else {
  145. bool reused = false;
  146. new_notify_idx = last_notify_idx_reserved;
  147. if (notify_idx_count + 1 < max_notify_idx) {
  148. do {
  149. if (!dbell_index_table_find
  150. (new_notify_idx)) {
  151. reused = true;
  152. break;
  153. }
  154. new_notify_idx = (new_notify_idx + 1) %
  155. max_notify_idx;
  156. } while (new_notify_idx !=
  157. last_notify_idx_released);
  158. }
  159. if (!reused) {
  160. new_notify_idx = max_notify_idx;
  161. max_notify_idx++;
  162. }
  163. }
  164. } else {
  165. new_notify_idx = (last_notify_idx_reserved + 1) % PAGE_SIZE;
  166. }
  167. last_notify_idx_reserved = new_notify_idx;
  168. notify_idx_count++;
  169. entry->idx = new_notify_idx;
  170. bucket = VMCI_DOORBELL_HASH(entry->idx);
  171. hlist_add_head(&entry->node, &vmci_doorbell_it.entries[bucket]);
  172. spin_unlock_bh(&vmci_doorbell_it.lock);
  173. }
  174. /*
  175. * Remove the given entry from the index table. This will release() the
  176. * entry's resource.
  177. */
  178. static void dbell_index_table_remove(struct dbell_entry *entry)
  179. {
  180. spin_lock_bh(&vmci_doorbell_it.lock);
  181. hlist_del_init(&entry->node);
  182. notify_idx_count--;
  183. if (entry->idx == max_notify_idx - 1) {
  184. /*
  185. * If we delete an entry with the maximum known
  186. * notification index, we take the opportunity to
  187. * prune the current max. As there might be other
  188. * unused indices immediately below, we lower the
  189. * maximum until we hit an index in use.
  190. */
  191. while (max_notify_idx > 0 &&
  192. !dbell_index_table_find(max_notify_idx - 1))
  193. max_notify_idx--;
  194. }
  195. last_notify_idx_released = entry->idx;
  196. spin_unlock_bh(&vmci_doorbell_it.lock);
  197. vmci_resource_put(&entry->resource);
  198. }
  199. /*
  200. * Creates a link between the given doorbell handle and the given
  201. * index in the bitmap in the device backend. A notification state
  202. * is created in hypervisor.
  203. */
  204. static int dbell_link(struct vmci_handle handle, u32 notify_idx)
  205. {
  206. struct vmci_doorbell_link_msg link_msg;
  207. link_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  208. VMCI_DOORBELL_LINK);
  209. link_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
  210. link_msg.hdr.payload_size = sizeof(link_msg) - VMCI_DG_HEADERSIZE;
  211. link_msg.handle = handle;
  212. link_msg.notify_idx = notify_idx;
  213. return vmci_send_datagram(&link_msg.hdr);
  214. }
  215. /*
  216. * Unlinks the given doorbell handle from an index in the bitmap in
  217. * the device backend. The notification state is destroyed in hypervisor.
  218. */
  219. static int dbell_unlink(struct vmci_handle handle)
  220. {
  221. struct vmci_doorbell_unlink_msg unlink_msg;
  222. unlink_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  223. VMCI_DOORBELL_UNLINK);
  224. unlink_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
  225. unlink_msg.hdr.payload_size = sizeof(unlink_msg) - VMCI_DG_HEADERSIZE;
  226. unlink_msg.handle = handle;
  227. return vmci_send_datagram(&unlink_msg.hdr);
  228. }
  229. /*
  230. * Notify another guest or the host. We send a datagram down to the
  231. * host via the hypervisor with the notification info.
  232. */
  233. static int dbell_notify_as_guest(struct vmci_handle handle, u32 priv_flags)
  234. {
  235. struct vmci_doorbell_notify_msg notify_msg;
  236. notify_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  237. VMCI_DOORBELL_NOTIFY);
  238. notify_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
  239. notify_msg.hdr.payload_size = sizeof(notify_msg) - VMCI_DG_HEADERSIZE;
  240. notify_msg.handle = handle;
  241. return vmci_send_datagram(&notify_msg.hdr);
  242. }
  243. /*
  244. * Calls the specified callback in a delayed context.
  245. */
  246. static void dbell_delayed_dispatch(struct work_struct *work)
  247. {
  248. struct dbell_entry *entry = container_of(work,
  249. struct dbell_entry, work);
  250. entry->notify_cb(entry->client_data);
  251. vmci_resource_put(&entry->resource);
  252. }
  253. /*
  254. * Dispatches a doorbell notification to the host context.
  255. */
  256. int vmci_dbell_host_context_notify(u32 src_cid, struct vmci_handle handle)
  257. {
  258. struct dbell_entry *entry;
  259. struct vmci_resource *resource;
  260. if (vmci_handle_is_invalid(handle)) {
  261. pr_devel("Notifying an invalid doorbell (handle=0x%x:0x%x)\n",
  262. handle.context, handle.resource);
  263. return VMCI_ERROR_INVALID_ARGS;
  264. }
  265. resource = vmci_resource_by_handle(handle,
  266. VMCI_RESOURCE_TYPE_DOORBELL);
  267. if (!resource) {
  268. pr_devel("Notifying an unknown doorbell (handle=0x%x:0x%x)\n",
  269. handle.context, handle.resource);
  270. return VMCI_ERROR_NOT_FOUND;
  271. }
  272. entry = container_of(resource, struct dbell_entry, resource);
  273. if (entry->run_delayed) {
  274. schedule_work(&entry->work);
  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. struct hlist_node *node;
  310. spin_lock_bh(&vmci_doorbell_it.lock);
  311. hlist_for_each_entry(dbell, node,
  312. &vmci_doorbell_it.entries[bucket], node) {
  313. if (dbell->idx == notify_idx &&
  314. atomic_read(&dbell->active) == 1) {
  315. if (dbell->run_delayed) {
  316. vmci_resource_get(&dbell->resource);
  317. schedule_work(&dbell->work);
  318. } else {
  319. dbell->notify_cb(dbell->client_data);
  320. }
  321. }
  322. }
  323. spin_unlock_bh(&vmci_doorbell_it.lock);
  324. }
  325. /*
  326. * Scans the notification bitmap, collects pending notifications,
  327. * resets the bitmap and invokes appropriate callbacks.
  328. */
  329. void vmci_dbell_scan_notification_entries(u8 *bitmap)
  330. {
  331. u32 idx;
  332. for (idx = 0; idx < max_notify_idx; idx++) {
  333. if (bitmap[idx] & 0x1) {
  334. bitmap[idx] &= ~1;
  335. dbell_fire_entries(idx);
  336. }
  337. }
  338. }
  339. /*
  340. * vmci_doorbell_create() - Creates a doorbell
  341. * @handle: A handle used to track the resource. Can be invalid.
  342. * @flags: Flag that determines context of callback.
  343. * @priv_flags: Privileges flags.
  344. * @notify_cb: The callback to be ivoked when the doorbell fires.
  345. * @client_data: A parameter to be passed to the callback.
  346. *
  347. * Creates a doorbell with the given callback. If the handle is
  348. * VMCI_INVALID_HANDLE, a free handle will be assigned, if
  349. * possible. The callback can be run immediately (potentially with
  350. * locks held - the default) or delayed (in a kernel thread) by
  351. * specifying the flag VMCI_FLAG_DELAYED_CB. If delayed execution
  352. * is selected, a given callback may not be run if the kernel is
  353. * unable to allocate memory for the delayed execution (highly
  354. * unlikely).
  355. */
  356. int vmci_doorbell_create(struct vmci_handle *handle,
  357. u32 flags,
  358. u32 priv_flags,
  359. vmci_callback notify_cb, void *client_data)
  360. {
  361. struct dbell_entry *entry;
  362. struct vmci_handle new_handle;
  363. int result;
  364. if (!handle || !notify_cb || flags & ~VMCI_FLAG_DELAYED_CB ||
  365. priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS)
  366. return VMCI_ERROR_INVALID_ARGS;
  367. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  368. if (entry == NULL) {
  369. pr_warn("Failed allocating memory for datagram entry\n");
  370. return VMCI_ERROR_NO_MEM;
  371. }
  372. if (vmci_handle_is_invalid(*handle)) {
  373. u32 context_id = vmci_get_context_id();
  374. /* Let resource code allocate a free ID for us */
  375. new_handle = vmci_make_handle(context_id, VMCI_INVALID_ID);
  376. } else {
  377. bool valid_context = false;
  378. /*
  379. * Validate the handle. We must do both of the checks below
  380. * because we can be acting as both a host and a guest at the
  381. * same time. We always allow the host context ID, since the
  382. * host functionality is in practice always there with the
  383. * unified driver.
  384. */
  385. if (handle->context == VMCI_HOST_CONTEXT_ID ||
  386. (vmci_guest_code_active() &&
  387. vmci_get_context_id() == handle->context)) {
  388. valid_context = true;
  389. }
  390. if (!valid_context || handle->resource == VMCI_INVALID_ID) {
  391. pr_devel("Invalid argument (handle=0x%x:0x%x)\n",
  392. handle->context, handle->resource);
  393. result = VMCI_ERROR_INVALID_ARGS;
  394. goto free_mem;
  395. }
  396. new_handle = *handle;
  397. }
  398. entry->idx = 0;
  399. INIT_HLIST_NODE(&entry->node);
  400. entry->priv_flags = priv_flags;
  401. INIT_WORK(&entry->work, dbell_delayed_dispatch);
  402. entry->run_delayed = flags & VMCI_FLAG_DELAYED_CB;
  403. entry->notify_cb = notify_cb;
  404. entry->client_data = client_data;
  405. atomic_set(&entry->active, 0);
  406. result = vmci_resource_add(&entry->resource,
  407. VMCI_RESOURCE_TYPE_DOORBELL,
  408. new_handle);
  409. if (result != VMCI_SUCCESS) {
  410. pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d\n",
  411. new_handle.context, new_handle.resource, result);
  412. goto free_mem;
  413. }
  414. new_handle = vmci_resource_handle(&entry->resource);
  415. if (vmci_guest_code_active()) {
  416. dbell_index_table_add(entry);
  417. result = dbell_link(new_handle, entry->idx);
  418. if (VMCI_SUCCESS != result)
  419. goto destroy_resource;
  420. atomic_set(&entry->active, 1);
  421. }
  422. *handle = new_handle;
  423. return result;
  424. destroy_resource:
  425. dbell_index_table_remove(entry);
  426. vmci_resource_remove(&entry->resource);
  427. free_mem:
  428. kfree(entry);
  429. return result;
  430. }
  431. EXPORT_SYMBOL_GPL(vmci_doorbell_create);
  432. /*
  433. * vmci_doorbell_destroy() - Destroy a doorbell.
  434. * @handle: The handle tracking the resource.
  435. *
  436. * Destroys a doorbell previously created with vmcii_doorbell_create. This
  437. * operation may block waiting for a callback to finish.
  438. */
  439. int vmci_doorbell_destroy(struct vmci_handle handle)
  440. {
  441. struct dbell_entry *entry;
  442. struct vmci_resource *resource;
  443. if (vmci_handle_is_invalid(handle))
  444. return VMCI_ERROR_INVALID_ARGS;
  445. resource = vmci_resource_by_handle(handle,
  446. VMCI_RESOURCE_TYPE_DOORBELL);
  447. if (!resource) {
  448. pr_devel("Failed to destroy doorbell (handle=0x%x:0x%x)\n",
  449. handle.context, handle.resource);
  450. return VMCI_ERROR_NOT_FOUND;
  451. }
  452. entry = container_of(resource, struct dbell_entry, resource);
  453. if (vmci_guest_code_active()) {
  454. int result;
  455. dbell_index_table_remove(entry);
  456. result = dbell_unlink(handle);
  457. if (VMCI_SUCCESS != result) {
  458. /*
  459. * The only reason this should fail would be
  460. * an inconsistency between guest and
  461. * hypervisor state, where the guest believes
  462. * it has an active registration whereas the
  463. * hypervisor doesn't. One case where this may
  464. * happen is if a doorbell is unregistered
  465. * following a hibernation at a time where the
  466. * doorbell state hasn't been restored on the
  467. * hypervisor side yet. Since the handle has
  468. * now been removed in the guest, we just
  469. * print a warning and return success.
  470. */
  471. pr_devel("Unlink of doorbell (handle=0x%x:0x%x) unknown by hypervisor (error=%d)\n",
  472. handle.context, handle.resource, result);
  473. }
  474. }
  475. /*
  476. * Now remove the resource from the table. It might still be in use
  477. * after this, in a callback or still on the delayed work queue.
  478. */
  479. vmci_resource_put(&entry->resource);
  480. vmci_resource_remove(&entry->resource);
  481. kfree(entry);
  482. return VMCI_SUCCESS;
  483. }
  484. EXPORT_SYMBOL_GPL(vmci_doorbell_destroy);
  485. /*
  486. * vmci_doorbell_notify() - Ring the doorbell (and hide in the bushes).
  487. * @dst: The handlle identifying the doorbell resource
  488. * @priv_flags: Priviledge flags.
  489. *
  490. * Generates a notification on the doorbell identified by the
  491. * handle. For host side generation of notifications, the caller
  492. * can specify what the privilege of the calling side is.
  493. */
  494. int vmci_doorbell_notify(struct vmci_handle dst, u32 priv_flags)
  495. {
  496. int retval;
  497. enum vmci_route route;
  498. struct vmci_handle src;
  499. if (vmci_handle_is_invalid(dst) ||
  500. (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS))
  501. return VMCI_ERROR_INVALID_ARGS;
  502. src = VMCI_INVALID_HANDLE;
  503. retval = vmci_route(&src, &dst, false, &route);
  504. if (retval < VMCI_SUCCESS)
  505. return retval;
  506. if (VMCI_ROUTE_AS_HOST == route)
  507. return vmci_ctx_notify_dbell(VMCI_HOST_CONTEXT_ID,
  508. dst, priv_flags);
  509. if (VMCI_ROUTE_AS_GUEST == route)
  510. return dbell_notify_as_guest(dst, priv_flags);
  511. pr_warn("Unknown route (%d) for doorbell\n", route);
  512. return VMCI_ERROR_DST_UNREACHABLE;
  513. }
  514. EXPORT_SYMBOL_GPL(vmci_doorbell_notify);