vmci_doorbell.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. schedule_work(&entry->work);
  274. } else {
  275. entry->notify_cb(entry->client_data);
  276. vmci_resource_put(resource);
  277. }
  278. return VMCI_SUCCESS;
  279. }
  280. /*
  281. * Register the notification bitmap with the host.
  282. */
  283. bool vmci_dbell_register_notification_bitmap(u32 bitmap_ppn)
  284. {
  285. int result;
  286. struct vmci_notify_bm_set_msg bitmap_set_msg;
  287. bitmap_set_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  288. VMCI_SET_NOTIFY_BITMAP);
  289. bitmap_set_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
  290. bitmap_set_msg.hdr.payload_size = sizeof(bitmap_set_msg) -
  291. VMCI_DG_HEADERSIZE;
  292. bitmap_set_msg.bitmap_ppn = bitmap_ppn;
  293. result = vmci_send_datagram(&bitmap_set_msg.hdr);
  294. if (result != VMCI_SUCCESS) {
  295. pr_devel("Failed to register (PPN=%u) as notification bitmap (error=%d)\n",
  296. bitmap_ppn, result);
  297. return false;
  298. }
  299. return true;
  300. }
  301. /*
  302. * Executes or schedules the handlers for a given notify index.
  303. */
  304. static void dbell_fire_entries(u32 notify_idx)
  305. {
  306. u32 bucket = VMCI_DOORBELL_HASH(notify_idx);
  307. struct dbell_entry *dbell;
  308. spin_lock_bh(&vmci_doorbell_it.lock);
  309. hlist_for_each_entry(dbell, &vmci_doorbell_it.entries[bucket], node) {
  310. if (dbell->idx == notify_idx &&
  311. atomic_read(&dbell->active) == 1) {
  312. if (dbell->run_delayed) {
  313. vmci_resource_get(&dbell->resource);
  314. schedule_work(&dbell->work);
  315. } else {
  316. dbell->notify_cb(dbell->client_data);
  317. }
  318. }
  319. }
  320. spin_unlock_bh(&vmci_doorbell_it.lock);
  321. }
  322. /*
  323. * Scans the notification bitmap, collects pending notifications,
  324. * resets the bitmap and invokes appropriate callbacks.
  325. */
  326. void vmci_dbell_scan_notification_entries(u8 *bitmap)
  327. {
  328. u32 idx;
  329. for (idx = 0; idx < max_notify_idx; idx++) {
  330. if (bitmap[idx] & 0x1) {
  331. bitmap[idx] &= ~1;
  332. dbell_fire_entries(idx);
  333. }
  334. }
  335. }
  336. /*
  337. * vmci_doorbell_create() - Creates a doorbell
  338. * @handle: A handle used to track the resource. Can be invalid.
  339. * @flags: Flag that determines context of callback.
  340. * @priv_flags: Privileges flags.
  341. * @notify_cb: The callback to be ivoked when the doorbell fires.
  342. * @client_data: A parameter to be passed to the callback.
  343. *
  344. * Creates a doorbell with the given callback. If the handle is
  345. * VMCI_INVALID_HANDLE, a free handle will be assigned, if
  346. * possible. The callback can be run immediately (potentially with
  347. * locks held - the default) or delayed (in a kernel thread) by
  348. * specifying the flag VMCI_FLAG_DELAYED_CB. If delayed execution
  349. * is selected, a given callback may not be run if the kernel is
  350. * unable to allocate memory for the delayed execution (highly
  351. * unlikely).
  352. */
  353. int vmci_doorbell_create(struct vmci_handle *handle,
  354. u32 flags,
  355. u32 priv_flags,
  356. vmci_callback notify_cb, void *client_data)
  357. {
  358. struct dbell_entry *entry;
  359. struct vmci_handle new_handle;
  360. int result;
  361. if (!handle || !notify_cb || flags & ~VMCI_FLAG_DELAYED_CB ||
  362. priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS)
  363. return VMCI_ERROR_INVALID_ARGS;
  364. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  365. if (entry == NULL) {
  366. pr_warn("Failed allocating memory for datagram entry\n");
  367. return VMCI_ERROR_NO_MEM;
  368. }
  369. if (vmci_handle_is_invalid(*handle)) {
  370. u32 context_id = vmci_get_context_id();
  371. /* Let resource code allocate a free ID for us */
  372. new_handle = vmci_make_handle(context_id, VMCI_INVALID_ID);
  373. } else {
  374. bool valid_context = false;
  375. /*
  376. * Validate the handle. We must do both of the checks below
  377. * because we can be acting as both a host and a guest at the
  378. * same time. We always allow the host context ID, since the
  379. * host functionality is in practice always there with the
  380. * unified driver.
  381. */
  382. if (handle->context == VMCI_HOST_CONTEXT_ID ||
  383. (vmci_guest_code_active() &&
  384. vmci_get_context_id() == handle->context)) {
  385. valid_context = true;
  386. }
  387. if (!valid_context || handle->resource == VMCI_INVALID_ID) {
  388. pr_devel("Invalid argument (handle=0x%x:0x%x)\n",
  389. handle->context, handle->resource);
  390. result = VMCI_ERROR_INVALID_ARGS;
  391. goto free_mem;
  392. }
  393. new_handle = *handle;
  394. }
  395. entry->idx = 0;
  396. INIT_HLIST_NODE(&entry->node);
  397. entry->priv_flags = priv_flags;
  398. INIT_WORK(&entry->work, dbell_delayed_dispatch);
  399. entry->run_delayed = flags & VMCI_FLAG_DELAYED_CB;
  400. entry->notify_cb = notify_cb;
  401. entry->client_data = client_data;
  402. atomic_set(&entry->active, 0);
  403. result = vmci_resource_add(&entry->resource,
  404. VMCI_RESOURCE_TYPE_DOORBELL,
  405. new_handle);
  406. if (result != VMCI_SUCCESS) {
  407. pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d\n",
  408. new_handle.context, new_handle.resource, result);
  409. goto free_mem;
  410. }
  411. new_handle = vmci_resource_handle(&entry->resource);
  412. if (vmci_guest_code_active()) {
  413. dbell_index_table_add(entry);
  414. result = dbell_link(new_handle, entry->idx);
  415. if (VMCI_SUCCESS != result)
  416. goto destroy_resource;
  417. atomic_set(&entry->active, 1);
  418. }
  419. *handle = new_handle;
  420. return result;
  421. destroy_resource:
  422. dbell_index_table_remove(entry);
  423. vmci_resource_remove(&entry->resource);
  424. free_mem:
  425. kfree(entry);
  426. return result;
  427. }
  428. EXPORT_SYMBOL_GPL(vmci_doorbell_create);
  429. /*
  430. * vmci_doorbell_destroy() - Destroy a doorbell.
  431. * @handle: The handle tracking the resource.
  432. *
  433. * Destroys a doorbell previously created with vmcii_doorbell_create. This
  434. * operation may block waiting for a callback to finish.
  435. */
  436. int vmci_doorbell_destroy(struct vmci_handle handle)
  437. {
  438. struct dbell_entry *entry;
  439. struct vmci_resource *resource;
  440. if (vmci_handle_is_invalid(handle))
  441. return VMCI_ERROR_INVALID_ARGS;
  442. resource = vmci_resource_by_handle(handle,
  443. VMCI_RESOURCE_TYPE_DOORBELL);
  444. if (!resource) {
  445. pr_devel("Failed to destroy doorbell (handle=0x%x:0x%x)\n",
  446. handle.context, handle.resource);
  447. return VMCI_ERROR_NOT_FOUND;
  448. }
  449. entry = container_of(resource, struct dbell_entry, resource);
  450. if (vmci_guest_code_active()) {
  451. int result;
  452. dbell_index_table_remove(entry);
  453. result = dbell_unlink(handle);
  454. if (VMCI_SUCCESS != result) {
  455. /*
  456. * The only reason this should fail would be
  457. * an inconsistency between guest and
  458. * hypervisor state, where the guest believes
  459. * it has an active registration whereas the
  460. * hypervisor doesn't. One case where this may
  461. * happen is if a doorbell is unregistered
  462. * following a hibernation at a time where the
  463. * doorbell state hasn't been restored on the
  464. * hypervisor side yet. Since the handle has
  465. * now been removed in the guest, we just
  466. * print a warning and return success.
  467. */
  468. pr_devel("Unlink of doorbell (handle=0x%x:0x%x) unknown by hypervisor (error=%d)\n",
  469. handle.context, handle.resource, result);
  470. }
  471. }
  472. /*
  473. * Now remove the resource from the table. It might still be in use
  474. * after this, in a callback or still on the delayed work queue.
  475. */
  476. vmci_resource_put(&entry->resource);
  477. vmci_resource_remove(&entry->resource);
  478. kfree(entry);
  479. return VMCI_SUCCESS;
  480. }
  481. EXPORT_SYMBOL_GPL(vmci_doorbell_destroy);
  482. /*
  483. * vmci_doorbell_notify() - Ring the doorbell (and hide in the bushes).
  484. * @dst: The handlle identifying the doorbell resource
  485. * @priv_flags: Priviledge flags.
  486. *
  487. * Generates a notification on the doorbell identified by the
  488. * handle. For host side generation of notifications, the caller
  489. * can specify what the privilege of the calling side is.
  490. */
  491. int vmci_doorbell_notify(struct vmci_handle dst, u32 priv_flags)
  492. {
  493. int retval;
  494. enum vmci_route route;
  495. struct vmci_handle src;
  496. if (vmci_handle_is_invalid(dst) ||
  497. (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS))
  498. return VMCI_ERROR_INVALID_ARGS;
  499. src = VMCI_INVALID_HANDLE;
  500. retval = vmci_route(&src, &dst, false, &route);
  501. if (retval < VMCI_SUCCESS)
  502. return retval;
  503. if (VMCI_ROUTE_AS_HOST == route)
  504. return vmci_ctx_notify_dbell(VMCI_HOST_CONTEXT_ID,
  505. dst, priv_flags);
  506. if (VMCI_ROUTE_AS_GUEST == route)
  507. return dbell_notify_as_guest(dst, priv_flags);
  508. pr_warn("Unknown route (%d) for doorbell\n", route);
  509. return VMCI_ERROR_DST_UNREACHABLE;
  510. }
  511. EXPORT_SYMBOL_GPL(vmci_doorbell_notify);