vmci_resource.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VMware VMCI Driver
  4. *
  5. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  6. */
  7. #include <linux/vmw_vmci_defs.h>
  8. #include <linux/hash.h>
  9. #include <linux/types.h>
  10. #include <linux/rculist.h>
  11. #include <linux/completion.h>
  12. #include "vmci_resource.h"
  13. #include "vmci_driver.h"
  14. #define VMCI_RESOURCE_HASH_BITS 7
  15. #define VMCI_RESOURCE_HASH_BUCKETS (1 << VMCI_RESOURCE_HASH_BITS)
  16. struct vmci_hash_table {
  17. spinlock_t lock;
  18. struct hlist_head entries[VMCI_RESOURCE_HASH_BUCKETS];
  19. };
  20. static struct vmci_hash_table vmci_resource_table = {
  21. .lock = __SPIN_LOCK_UNLOCKED(vmci_resource_table.lock),
  22. };
  23. static unsigned int vmci_resource_hash(struct vmci_handle handle)
  24. {
  25. return hash_32(handle.resource, VMCI_RESOURCE_HASH_BITS);
  26. }
  27. /*
  28. * Gets a resource (if one exists) matching given handle from the hash table.
  29. */
  30. static struct vmci_resource *vmci_resource_lookup(struct vmci_handle handle,
  31. enum vmci_resource_type type)
  32. {
  33. struct vmci_resource *r, *resource = NULL;
  34. unsigned int idx = vmci_resource_hash(handle);
  35. rcu_read_lock();
  36. hlist_for_each_entry_rcu(r,
  37. &vmci_resource_table.entries[idx], node) {
  38. u32 cid = r->handle.context;
  39. u32 rid = r->handle.resource;
  40. if (r->type == type &&
  41. rid == handle.resource &&
  42. (cid == handle.context || cid == VMCI_INVALID_ID ||
  43. handle.context == VMCI_INVALID_ID)) {
  44. resource = r;
  45. break;
  46. }
  47. }
  48. rcu_read_unlock();
  49. return resource;
  50. }
  51. /*
  52. * Find an unused resource ID and return it. The first
  53. * VMCI_RESERVED_RESOURCE_ID_MAX are reserved so we start from
  54. * its value + 1.
  55. * Returns VMCI resource id on success, VMCI_INVALID_ID on failure.
  56. */
  57. static u32 vmci_resource_find_id(u32 context_id,
  58. enum vmci_resource_type resource_type)
  59. {
  60. static u32 resource_id = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
  61. u32 old_rid = resource_id;
  62. u32 current_rid;
  63. /*
  64. * Generate a unique resource ID. Keep on trying until we wrap around
  65. * in the RID space.
  66. */
  67. do {
  68. struct vmci_handle handle;
  69. current_rid = resource_id;
  70. resource_id++;
  71. if (unlikely(resource_id == VMCI_INVALID_ID)) {
  72. /* Skip the reserved rids. */
  73. resource_id = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
  74. }
  75. handle = vmci_make_handle(context_id, current_rid);
  76. if (!vmci_resource_lookup(handle, resource_type))
  77. return current_rid;
  78. } while (resource_id != old_rid);
  79. return VMCI_INVALID_ID;
  80. }
  81. int vmci_resource_add(struct vmci_resource *resource,
  82. enum vmci_resource_type resource_type,
  83. struct vmci_handle handle)
  84. {
  85. unsigned int idx;
  86. int result;
  87. spin_lock(&vmci_resource_table.lock);
  88. if (handle.resource == VMCI_INVALID_ID) {
  89. handle.resource = vmci_resource_find_id(handle.context,
  90. resource_type);
  91. if (handle.resource == VMCI_INVALID_ID) {
  92. result = VMCI_ERROR_NO_HANDLE;
  93. goto out;
  94. }
  95. } else if (vmci_resource_lookup(handle, resource_type)) {
  96. result = VMCI_ERROR_ALREADY_EXISTS;
  97. goto out;
  98. }
  99. resource->handle = handle;
  100. resource->type = resource_type;
  101. INIT_HLIST_NODE(&resource->node);
  102. kref_init(&resource->kref);
  103. init_completion(&resource->done);
  104. idx = vmci_resource_hash(resource->handle);
  105. hlist_add_head_rcu(&resource->node, &vmci_resource_table.entries[idx]);
  106. result = VMCI_SUCCESS;
  107. out:
  108. spin_unlock(&vmci_resource_table.lock);
  109. return result;
  110. }
  111. void vmci_resource_remove(struct vmci_resource *resource)
  112. {
  113. struct vmci_handle handle = resource->handle;
  114. unsigned int idx = vmci_resource_hash(handle);
  115. struct vmci_resource *r;
  116. /* Remove resource from hash table. */
  117. spin_lock(&vmci_resource_table.lock);
  118. hlist_for_each_entry(r, &vmci_resource_table.entries[idx], node) {
  119. if (vmci_handle_is_equal(r->handle, resource->handle)) {
  120. hlist_del_init_rcu(&r->node);
  121. break;
  122. }
  123. }
  124. spin_unlock(&vmci_resource_table.lock);
  125. synchronize_rcu();
  126. vmci_resource_put(resource);
  127. wait_for_completion(&resource->done);
  128. }
  129. struct vmci_resource *
  130. vmci_resource_by_handle(struct vmci_handle resource_handle,
  131. enum vmci_resource_type resource_type)
  132. {
  133. struct vmci_resource *r, *resource = NULL;
  134. rcu_read_lock();
  135. r = vmci_resource_lookup(resource_handle, resource_type);
  136. if (r &&
  137. (resource_type == r->type ||
  138. resource_type == VMCI_RESOURCE_TYPE_ANY)) {
  139. resource = vmci_resource_get(r);
  140. }
  141. rcu_read_unlock();
  142. return resource;
  143. }
  144. /*
  145. * Get a reference to given resource.
  146. */
  147. struct vmci_resource *vmci_resource_get(struct vmci_resource *resource)
  148. {
  149. kref_get(&resource->kref);
  150. return resource;
  151. }
  152. static void vmci_release_resource(struct kref *kref)
  153. {
  154. struct vmci_resource *resource =
  155. container_of(kref, struct vmci_resource, kref);
  156. /* Verify the resource has been unlinked from hash table */
  157. WARN_ON(!hlist_unhashed(&resource->node));
  158. /* Signal that container of this resource can now be destroyed */
  159. complete(&resource->done);
  160. }
  161. /*
  162. * Resource's release function will get called if last reference.
  163. * If it is the last reference, then we are sure that nobody else
  164. * can increment the count again (it's gone from the resource hash
  165. * table), so there's no need for locking here.
  166. */
  167. int vmci_resource_put(struct vmci_resource *resource)
  168. {
  169. /*
  170. * We propagate the information back to caller in case it wants to know
  171. * whether entry was freed.
  172. */
  173. return kref_put(&resource->kref, vmci_release_resource) ?
  174. VMCI_SUCCESS_ENTRY_DEAD : VMCI_SUCCESS;
  175. }
  176. struct vmci_handle vmci_resource_handle(struct vmci_resource *resource)
  177. {
  178. return resource->handle;
  179. }