vmci_resource.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. 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. /* Remove resource from hash table. */
  124. spin_lock(&vmci_resource_table.lock);
  125. hlist_for_each_entry(r, &vmci_resource_table.entries[idx], node) {
  126. if (vmci_handle_is_equal(r->handle, resource->handle)) {
  127. hlist_del_init_rcu(&r->node);
  128. break;
  129. }
  130. }
  131. spin_unlock(&vmci_resource_table.lock);
  132. synchronize_rcu();
  133. vmci_resource_put(resource);
  134. wait_for_completion(&resource->done);
  135. }
  136. struct vmci_resource *
  137. vmci_resource_by_handle(struct vmci_handle resource_handle,
  138. enum vmci_resource_type resource_type)
  139. {
  140. struct vmci_resource *r, *resource = NULL;
  141. rcu_read_lock();
  142. r = vmci_resource_lookup(resource_handle, resource_type);
  143. if (r &&
  144. (resource_type == r->type ||
  145. resource_type == VMCI_RESOURCE_TYPE_ANY)) {
  146. resource = vmci_resource_get(r);
  147. }
  148. rcu_read_unlock();
  149. return resource;
  150. }
  151. /*
  152. * Get a reference to given resource.
  153. */
  154. struct vmci_resource *vmci_resource_get(struct vmci_resource *resource)
  155. {
  156. kref_get(&resource->kref);
  157. return resource;
  158. }
  159. static void vmci_release_resource(struct kref *kref)
  160. {
  161. struct vmci_resource *resource =
  162. container_of(kref, struct vmci_resource, kref);
  163. /* Verify the resource has been unlinked from hash table */
  164. WARN_ON(!hlist_unhashed(&resource->node));
  165. /* Signal that container of this resource can now be destroyed */
  166. complete(&resource->done);
  167. }
  168. /*
  169. * Resource's release function will get called if last reference.
  170. * If it is the last reference, then we are sure that nobody else
  171. * can increment the count again (it's gone from the resource hash
  172. * table), so there's no need for locking here.
  173. */
  174. int vmci_resource_put(struct vmci_resource *resource)
  175. {
  176. /*
  177. * We propagate the information back to caller in case it wants to know
  178. * whether entry was freed.
  179. */
  180. return kref_put(&resource->kref, vmci_release_resource) ?
  181. VMCI_SUCCESS_ENTRY_DEAD : VMCI_SUCCESS;
  182. }
  183. struct vmci_handle vmci_resource_handle(struct vmci_resource *resource)
  184. {
  185. return resource->handle;
  186. }