vmci_resource.c 5.7 KB

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