ccid.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * net/dccp/ccid.c
  3. *
  4. * An implementation of the DCCP protocol
  5. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * CCID infrastructure
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include "ccid.h"
  14. static struct ccid_operations *ccids[CCID_MAX];
  15. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  16. static atomic_t ccids_lockct = ATOMIC_INIT(0);
  17. static DEFINE_SPINLOCK(ccids_lock);
  18. /*
  19. * The strategy is: modifications ccids vector are short, do not sleep and
  20. * veeery rare, but read access should be free of any exclusive locks.
  21. */
  22. static void ccids_write_lock(void)
  23. {
  24. spin_lock(&ccids_lock);
  25. while (atomic_read(&ccids_lockct) != 0) {
  26. spin_unlock(&ccids_lock);
  27. yield();
  28. spin_lock(&ccids_lock);
  29. }
  30. }
  31. static inline void ccids_write_unlock(void)
  32. {
  33. spin_unlock(&ccids_lock);
  34. }
  35. static inline void ccids_read_lock(void)
  36. {
  37. atomic_inc(&ccids_lockct);
  38. smp_mb__after_atomic_inc();
  39. spin_unlock_wait(&ccids_lock);
  40. }
  41. static inline void ccids_read_unlock(void)
  42. {
  43. atomic_dec(&ccids_lockct);
  44. }
  45. #else
  46. #define ccids_write_lock() do { } while(0)
  47. #define ccids_write_unlock() do { } while(0)
  48. #define ccids_read_lock() do { } while(0)
  49. #define ccids_read_unlock() do { } while(0)
  50. #endif
  51. static struct kmem_cache *ccid_kmem_cache_create(int obj_size, const char *fmt,...)
  52. {
  53. struct kmem_cache *slab;
  54. char slab_name_fmt[32], *slab_name;
  55. va_list args;
  56. va_start(args, fmt);
  57. vsnprintf(slab_name_fmt, sizeof(slab_name_fmt), fmt, args);
  58. va_end(args);
  59. slab_name = kstrdup(slab_name_fmt, GFP_KERNEL);
  60. if (slab_name == NULL)
  61. return NULL;
  62. slab = kmem_cache_create(slab_name, sizeof(struct ccid) + obj_size, 0,
  63. SLAB_HWCACHE_ALIGN, NULL);
  64. if (slab == NULL)
  65. kfree(slab_name);
  66. return slab;
  67. }
  68. static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
  69. {
  70. if (slab != NULL) {
  71. const char *name = kmem_cache_name(slab);
  72. kmem_cache_destroy(slab);
  73. kfree(name);
  74. }
  75. }
  76. int ccid_register(struct ccid_operations *ccid_ops)
  77. {
  78. int err = -ENOBUFS;
  79. ccid_ops->ccid_hc_rx_slab =
  80. ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
  81. "ccid%u_hc_rx_sock",
  82. ccid_ops->ccid_id);
  83. if (ccid_ops->ccid_hc_rx_slab == NULL)
  84. goto out;
  85. ccid_ops->ccid_hc_tx_slab =
  86. ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
  87. "ccid%u_hc_tx_sock",
  88. ccid_ops->ccid_id);
  89. if (ccid_ops->ccid_hc_tx_slab == NULL)
  90. goto out_free_rx_slab;
  91. ccids_write_lock();
  92. err = -EEXIST;
  93. if (ccids[ccid_ops->ccid_id] == NULL) {
  94. ccids[ccid_ops->ccid_id] = ccid_ops;
  95. err = 0;
  96. }
  97. ccids_write_unlock();
  98. if (err != 0)
  99. goto out_free_tx_slab;
  100. pr_info("CCID: Registered CCID %d (%s)\n",
  101. ccid_ops->ccid_id, ccid_ops->ccid_name);
  102. out:
  103. return err;
  104. out_free_tx_slab:
  105. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
  106. ccid_ops->ccid_hc_tx_slab = NULL;
  107. goto out;
  108. out_free_rx_slab:
  109. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  110. ccid_ops->ccid_hc_rx_slab = NULL;
  111. goto out;
  112. }
  113. EXPORT_SYMBOL_GPL(ccid_register);
  114. int ccid_unregister(struct ccid_operations *ccid_ops)
  115. {
  116. ccids_write_lock();
  117. ccids[ccid_ops->ccid_id] = NULL;
  118. ccids_write_unlock();
  119. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
  120. ccid_ops->ccid_hc_tx_slab = NULL;
  121. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  122. ccid_ops->ccid_hc_rx_slab = NULL;
  123. pr_info("CCID: Unregistered CCID %d (%s)\n",
  124. ccid_ops->ccid_id, ccid_ops->ccid_name);
  125. return 0;
  126. }
  127. EXPORT_SYMBOL_GPL(ccid_unregister);
  128. struct ccid *ccid_new(unsigned char id, struct sock *sk, int rx, gfp_t gfp)
  129. {
  130. struct ccid_operations *ccid_ops;
  131. struct ccid *ccid = NULL;
  132. ccids_read_lock();
  133. #ifdef CONFIG_KMOD
  134. if (ccids[id] == NULL) {
  135. /* We only try to load if in process context */
  136. ccids_read_unlock();
  137. if (gfp & GFP_ATOMIC)
  138. goto out;
  139. request_module("net-dccp-ccid-%d", id);
  140. ccids_read_lock();
  141. }
  142. #endif
  143. ccid_ops = ccids[id];
  144. if (ccid_ops == NULL)
  145. goto out_unlock;
  146. if (!try_module_get(ccid_ops->ccid_owner))
  147. goto out_unlock;
  148. ccids_read_unlock();
  149. ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
  150. ccid_ops->ccid_hc_tx_slab, gfp);
  151. if (ccid == NULL)
  152. goto out_module_put;
  153. ccid->ccid_ops = ccid_ops;
  154. if (rx) {
  155. memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
  156. if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
  157. ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
  158. goto out_free_ccid;
  159. } else {
  160. memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
  161. if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
  162. ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
  163. goto out_free_ccid;
  164. }
  165. out:
  166. return ccid;
  167. out_unlock:
  168. ccids_read_unlock();
  169. goto out;
  170. out_free_ccid:
  171. kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
  172. ccid_ops->ccid_hc_tx_slab, ccid);
  173. ccid = NULL;
  174. out_module_put:
  175. module_put(ccid_ops->ccid_owner);
  176. goto out;
  177. }
  178. EXPORT_SYMBOL_GPL(ccid_new);
  179. struct ccid *ccid_hc_rx_new(unsigned char id, struct sock *sk, gfp_t gfp)
  180. {
  181. return ccid_new(id, sk, 1, gfp);
  182. }
  183. EXPORT_SYMBOL_GPL(ccid_hc_rx_new);
  184. struct ccid *ccid_hc_tx_new(unsigned char id,struct sock *sk, gfp_t gfp)
  185. {
  186. return ccid_new(id, sk, 0, gfp);
  187. }
  188. EXPORT_SYMBOL_GPL(ccid_hc_tx_new);
  189. static void ccid_delete(struct ccid *ccid, struct sock *sk, int rx)
  190. {
  191. struct ccid_operations *ccid_ops;
  192. if (ccid == NULL)
  193. return;
  194. ccid_ops = ccid->ccid_ops;
  195. if (rx) {
  196. if (ccid_ops->ccid_hc_rx_exit != NULL)
  197. ccid_ops->ccid_hc_rx_exit(sk);
  198. kmem_cache_free(ccid_ops->ccid_hc_rx_slab, ccid);
  199. } else {
  200. if (ccid_ops->ccid_hc_tx_exit != NULL)
  201. ccid_ops->ccid_hc_tx_exit(sk);
  202. kmem_cache_free(ccid_ops->ccid_hc_tx_slab, ccid);
  203. }
  204. ccids_read_lock();
  205. if (ccids[ccid_ops->ccid_id] != NULL)
  206. module_put(ccid_ops->ccid_owner);
  207. ccids_read_unlock();
  208. }
  209. void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
  210. {
  211. ccid_delete(ccid, sk, 1);
  212. }
  213. EXPORT_SYMBOL_GPL(ccid_hc_rx_delete);
  214. void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
  215. {
  216. ccid_delete(ccid, sk, 0);
  217. }
  218. EXPORT_SYMBOL_GPL(ccid_hc_tx_delete);