vmci_resource.c 5.7 KB

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