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 <linux/completion.h>
  20. #include "vmci_resource.h"
  21. #include "vmci_driver.h"
  22. #define VMCI_RESOURCE_HASH_BITS 7
  23. #define VMCI_RESOURCE_HASH_BUCKETS (1 << VMCI_RESOURCE_HASH_BITS)
  24. struct vmci_hash_table {
  25. spinlock_t lock;
  26. struct hlist_head entries[VMCI_RESOURCE_HASH_BUCKETS];
  27. };
  28. static struct vmci_hash_table vmci_resource_table = {
  29. .lock = __SPIN_LOCK_UNLOCKED(vmci_resource_table.lock),
  30. };
  31. static unsigned int vmci_resource_hash(struct vmci_handle handle)
  32. {
  33. return hash_32(handle.resource, VMCI_RESOURCE_HASH_BITS);
  34. }
  35. /*
  36. * Gets a resource (if one exists) matching given handle from the hash table.
  37. */
  38. static struct vmci_resource *vmci_resource_lookup(struct vmci_handle handle,
  39. enum vmci_resource_type type)
  40. {
  41. struct vmci_resource *r, *resource = NULL;
  42. unsigned int idx = vmci_resource_hash(handle);
  43. rcu_read_lock();
  44. hlist_for_each_entry_rcu(r,
  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. handle.context == VMCI_INVALID_ID)) {
  52. resource = r;
  53. break;
  54. }
  55. }
  56. rcu_read_unlock();
  57. return resource;
  58. }
  59. /*
  60. * Find an unused resource ID and return it. The first
  61. * VMCI_RESERVED_RESOURCE_ID_MAX are reserved so we start from
  62. * its value + 1.
  63. * Returns VMCI resource id on success, VMCI_INVALID_ID on failure.
  64. */
  65. static u32 vmci_resource_find_id(u32 context_id,
  66. enum vmci_resource_type resource_type)
  67. {
  68. static u32 resource_id = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
  69. u32 old_rid = resource_id;
  70. u32 current_rid;
  71. /*
  72. * Generate a unique resource ID. Keep on trying until we wrap around
  73. * in the RID space.
  74. */
  75. do {
  76. struct vmci_handle handle;
  77. current_rid = resource_id;
  78. resource_id++;
  79. if (unlikely(resource_id == VMCI_INVALID_ID)) {
  80. /* Skip the reserved rids. */
  81. resource_id = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
  82. }
  83. handle = vmci_make_handle(context_id, current_rid);
  84. if (!vmci_resource_lookup(handle, resource_type))
  85. return current_rid;
  86. } while (resource_id != old_rid);
  87. return VMCI_INVALID_ID;
  88. }
  89. int vmci_resource_add(struct vmci_resource *resource,
  90. enum vmci_resource_type resource_type,
  91. struct vmci_handle handle)
  92. {
  93. unsigned int idx;
  94. int result;
  95. spin_lock(&vmci_resource_table.lock);
  96. if (handle.resource == VMCI_INVALID_ID) {
  97. handle.resource = vmci_resource_find_id(handle.context,
  98. resource_type);
  99. if (handle.resource == VMCI_INVALID_ID) {
  100. result = VMCI_ERROR_NO_HANDLE;
  101. goto out;
  102. }
  103. } else if (vmci_resource_lookup(handle, resource_type)) {
  104. result = VMCI_ERROR_ALREADY_EXISTS;
  105. goto out;
  106. }
  107. resource->handle = handle;
  108. resource->type = resource_type;
  109. INIT_HLIST_NODE(&resource->node);
  110. kref_init(&resource->kref);
  111. init_completion(&resource->done);
  112. idx = vmci_resource_hash(resource->handle);
  113. hlist_add_head_rcu(&resource->node, &vmci_resource_table.entries[idx]);
  114. result = VMCI_SUCCESS;
  115. out:
  116. spin_unlock(&vmci_resource_table.lock);
  117. return result;
  118. }
  119. void vmci_resource_remove(struct vmci_resource *resource)
  120. {
  121. struct vmci_handle handle = resource->handle;
  122. unsigned int idx = vmci_resource_hash(handle);
  123. struct vmci_resource *r;
  124. /* Remove resource from hash table. */
  125. spin_lock(&vmci_resource_table.lock);
  126. hlist_for_each_entry(r, &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. }