message.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2006 Oracle. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include "rds.h"
  35. #include "rdma.h"
  36. static DECLARE_WAIT_QUEUE_HEAD(rds_message_flush_waitq);
  37. static unsigned int rds_exthdr_size[__RDS_EXTHDR_MAX] = {
  38. [RDS_EXTHDR_NONE] = 0,
  39. [RDS_EXTHDR_VERSION] = sizeof(struct rds_ext_header_version),
  40. [RDS_EXTHDR_RDMA] = sizeof(struct rds_ext_header_rdma),
  41. [RDS_EXTHDR_RDMA_DEST] = sizeof(struct rds_ext_header_rdma_dest),
  42. };
  43. void rds_message_addref(struct rds_message *rm)
  44. {
  45. rdsdebug("addref rm %p ref %d\n", rm, atomic_read(&rm->m_refcount));
  46. atomic_inc(&rm->m_refcount);
  47. }
  48. EXPORT_SYMBOL_GPL(rds_message_addref);
  49. /*
  50. * This relies on dma_map_sg() not touching sg[].page during merging.
  51. */
  52. static void rds_message_purge(struct rds_message *rm)
  53. {
  54. unsigned long i;
  55. if (unlikely(test_bit(RDS_MSG_PAGEVEC, &rm->m_flags)))
  56. return;
  57. for (i = 0; i < rm->m_nents; i++) {
  58. rdsdebug("putting data page %p\n", (void *)sg_page(&rm->m_sg[i]));
  59. /* XXX will have to put_page for page refs */
  60. __free_page(sg_page(&rm->m_sg[i]));
  61. }
  62. rm->m_nents = 0;
  63. if (rm->m_rdma_op)
  64. rds_rdma_free_op(rm->m_rdma_op);
  65. if (rm->m_rdma_mr)
  66. rds_mr_put(rm->m_rdma_mr);
  67. }
  68. void rds_message_inc_purge(struct rds_incoming *inc)
  69. {
  70. struct rds_message *rm = container_of(inc, struct rds_message, m_inc);
  71. rds_message_purge(rm);
  72. }
  73. void rds_message_put(struct rds_message *rm)
  74. {
  75. rdsdebug("put rm %p ref %d\n", rm, atomic_read(&rm->m_refcount));
  76. if (atomic_dec_and_test(&rm->m_refcount)) {
  77. BUG_ON(!list_empty(&rm->m_sock_item));
  78. BUG_ON(!list_empty(&rm->m_conn_item));
  79. rds_message_purge(rm);
  80. kfree(rm);
  81. }
  82. }
  83. EXPORT_SYMBOL_GPL(rds_message_put);
  84. void rds_message_inc_free(struct rds_incoming *inc)
  85. {
  86. struct rds_message *rm = container_of(inc, struct rds_message, m_inc);
  87. rds_message_put(rm);
  88. }
  89. void rds_message_populate_header(struct rds_header *hdr, __be16 sport,
  90. __be16 dport, u64 seq)
  91. {
  92. hdr->h_flags = 0;
  93. hdr->h_sport = sport;
  94. hdr->h_dport = dport;
  95. hdr->h_sequence = cpu_to_be64(seq);
  96. hdr->h_exthdr[0] = RDS_EXTHDR_NONE;
  97. }
  98. EXPORT_SYMBOL_GPL(rds_message_populate_header);
  99. int rds_message_add_extension(struct rds_header *hdr,
  100. unsigned int type, const void *data, unsigned int len)
  101. {
  102. unsigned int ext_len = sizeof(u8) + len;
  103. unsigned char *dst;
  104. /* For now, refuse to add more than one extension header */
  105. if (hdr->h_exthdr[0] != RDS_EXTHDR_NONE)
  106. return 0;
  107. if (type >= __RDS_EXTHDR_MAX || len != rds_exthdr_size[type])
  108. return 0;
  109. if (ext_len >= RDS_HEADER_EXT_SPACE)
  110. return 0;
  111. dst = hdr->h_exthdr;
  112. *dst++ = type;
  113. memcpy(dst, data, len);
  114. dst[len] = RDS_EXTHDR_NONE;
  115. return 1;
  116. }
  117. EXPORT_SYMBOL_GPL(rds_message_add_extension);
  118. /*
  119. * If a message has extension headers, retrieve them here.
  120. * Call like this:
  121. *
  122. * unsigned int pos = 0;
  123. *
  124. * while (1) {
  125. * buflen = sizeof(buffer);
  126. * type = rds_message_next_extension(hdr, &pos, buffer, &buflen);
  127. * if (type == RDS_EXTHDR_NONE)
  128. * break;
  129. * ...
  130. * }
  131. */
  132. int rds_message_next_extension(struct rds_header *hdr,
  133. unsigned int *pos, void *buf, unsigned int *buflen)
  134. {
  135. unsigned int offset, ext_type, ext_len;
  136. u8 *src = hdr->h_exthdr;
  137. offset = *pos;
  138. if (offset >= RDS_HEADER_EXT_SPACE)
  139. goto none;
  140. /* Get the extension type and length. For now, the
  141. * length is implied by the extension type. */
  142. ext_type = src[offset++];
  143. if (ext_type == RDS_EXTHDR_NONE || ext_type >= __RDS_EXTHDR_MAX)
  144. goto none;
  145. ext_len = rds_exthdr_size[ext_type];
  146. if (offset + ext_len > RDS_HEADER_EXT_SPACE)
  147. goto none;
  148. *pos = offset + ext_len;
  149. if (ext_len < *buflen)
  150. *buflen = ext_len;
  151. memcpy(buf, src + offset, *buflen);
  152. return ext_type;
  153. none:
  154. *pos = RDS_HEADER_EXT_SPACE;
  155. *buflen = 0;
  156. return RDS_EXTHDR_NONE;
  157. }
  158. int rds_message_add_version_extension(struct rds_header *hdr, unsigned int version)
  159. {
  160. struct rds_ext_header_version ext_hdr;
  161. ext_hdr.h_version = cpu_to_be32(version);
  162. return rds_message_add_extension(hdr, RDS_EXTHDR_VERSION, &ext_hdr, sizeof(ext_hdr));
  163. }
  164. int rds_message_get_version_extension(struct rds_header *hdr, unsigned int *version)
  165. {
  166. struct rds_ext_header_version ext_hdr;
  167. unsigned int pos = 0, len = sizeof(ext_hdr);
  168. /* We assume the version extension is the only one present */
  169. if (rds_message_next_extension(hdr, &pos, &ext_hdr, &len) != RDS_EXTHDR_VERSION)
  170. return 0;
  171. *version = be32_to_cpu(ext_hdr.h_version);
  172. return 1;
  173. }
  174. int rds_message_add_rdma_dest_extension(struct rds_header *hdr, u32 r_key, u32 offset)
  175. {
  176. struct rds_ext_header_rdma_dest ext_hdr;
  177. ext_hdr.h_rdma_rkey = cpu_to_be32(r_key);
  178. ext_hdr.h_rdma_offset = cpu_to_be32(offset);
  179. return rds_message_add_extension(hdr, RDS_EXTHDR_RDMA_DEST, &ext_hdr, sizeof(ext_hdr));
  180. }
  181. EXPORT_SYMBOL_GPL(rds_message_add_rdma_dest_extension);
  182. struct rds_message *rds_message_alloc(unsigned int nents, gfp_t gfp)
  183. {
  184. struct rds_message *rm;
  185. rm = kzalloc(sizeof(struct rds_message) +
  186. (nents * sizeof(struct scatterlist)), gfp);
  187. if (!rm)
  188. goto out;
  189. if (nents)
  190. sg_init_table(rm->m_sg, nents);
  191. atomic_set(&rm->m_refcount, 1);
  192. INIT_LIST_HEAD(&rm->m_sock_item);
  193. INIT_LIST_HEAD(&rm->m_conn_item);
  194. spin_lock_init(&rm->m_rs_lock);
  195. out:
  196. return rm;
  197. }
  198. struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len)
  199. {
  200. struct rds_message *rm;
  201. unsigned int i;
  202. rm = rds_message_alloc(ceil(total_len, PAGE_SIZE), GFP_KERNEL);
  203. if (rm == NULL)
  204. return ERR_PTR(-ENOMEM);
  205. set_bit(RDS_MSG_PAGEVEC, &rm->m_flags);
  206. rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len);
  207. rm->m_nents = ceil(total_len, PAGE_SIZE);
  208. for (i = 0; i < rm->m_nents; ++i) {
  209. sg_set_page(&rm->m_sg[i],
  210. virt_to_page(page_addrs[i]),
  211. PAGE_SIZE, 0);
  212. }
  213. return rm;
  214. }
  215. struct rds_message *rds_message_copy_from_user(struct iovec *first_iov,
  216. size_t total_len)
  217. {
  218. unsigned long to_copy;
  219. unsigned long iov_off;
  220. unsigned long sg_off;
  221. struct rds_message *rm;
  222. struct iovec *iov;
  223. struct scatterlist *sg;
  224. int ret;
  225. rm = rds_message_alloc(ceil(total_len, PAGE_SIZE), GFP_KERNEL);
  226. if (rm == NULL) {
  227. ret = -ENOMEM;
  228. goto out;
  229. }
  230. rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len);
  231. /*
  232. * now allocate and copy in the data payload.
  233. */
  234. sg = rm->m_sg;
  235. iov = first_iov;
  236. iov_off = 0;
  237. sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */
  238. while (total_len) {
  239. if (sg_page(sg) == NULL) {
  240. ret = rds_page_remainder_alloc(sg, total_len,
  241. GFP_HIGHUSER);
  242. if (ret)
  243. goto out;
  244. rm->m_nents++;
  245. sg_off = 0;
  246. }
  247. while (iov_off == iov->iov_len) {
  248. iov_off = 0;
  249. iov++;
  250. }
  251. to_copy = min(iov->iov_len - iov_off, sg->length - sg_off);
  252. to_copy = min_t(size_t, to_copy, total_len);
  253. rdsdebug("copying %lu bytes from user iov [%p, %zu] + %lu to "
  254. "sg [%p, %u, %u] + %lu\n",
  255. to_copy, iov->iov_base, iov->iov_len, iov_off,
  256. (void *)sg_page(sg), sg->offset, sg->length, sg_off);
  257. ret = rds_page_copy_from_user(sg_page(sg), sg->offset + sg_off,
  258. iov->iov_base + iov_off,
  259. to_copy);
  260. if (ret)
  261. goto out;
  262. iov_off += to_copy;
  263. total_len -= to_copy;
  264. sg_off += to_copy;
  265. if (sg_off == sg->length)
  266. sg++;
  267. }
  268. ret = 0;
  269. out:
  270. if (ret) {
  271. if (rm)
  272. rds_message_put(rm);
  273. rm = ERR_PTR(ret);
  274. }
  275. return rm;
  276. }
  277. int rds_message_inc_copy_to_user(struct rds_incoming *inc,
  278. struct iovec *first_iov, size_t size)
  279. {
  280. struct rds_message *rm;
  281. struct iovec *iov;
  282. struct scatterlist *sg;
  283. unsigned long to_copy;
  284. unsigned long iov_off;
  285. unsigned long vec_off;
  286. int copied;
  287. int ret;
  288. u32 len;
  289. rm = container_of(inc, struct rds_message, m_inc);
  290. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  291. iov = first_iov;
  292. iov_off = 0;
  293. sg = rm->m_sg;
  294. vec_off = 0;
  295. copied = 0;
  296. while (copied < size && copied < len) {
  297. while (iov_off == iov->iov_len) {
  298. iov_off = 0;
  299. iov++;
  300. }
  301. to_copy = min(iov->iov_len - iov_off, sg->length - vec_off);
  302. to_copy = min_t(size_t, to_copy, size - copied);
  303. to_copy = min_t(unsigned long, to_copy, len - copied);
  304. rdsdebug("copying %lu bytes to user iov [%p, %zu] + %lu to "
  305. "sg [%p, %u, %u] + %lu\n",
  306. to_copy, iov->iov_base, iov->iov_len, iov_off,
  307. sg_page(sg), sg->offset, sg->length, vec_off);
  308. ret = rds_page_copy_to_user(sg_page(sg), sg->offset + vec_off,
  309. iov->iov_base + iov_off,
  310. to_copy);
  311. if (ret) {
  312. copied = ret;
  313. break;
  314. }
  315. iov_off += to_copy;
  316. vec_off += to_copy;
  317. copied += to_copy;
  318. if (vec_off == sg->length) {
  319. vec_off = 0;
  320. sg++;
  321. }
  322. }
  323. return copied;
  324. }
  325. /*
  326. * If the message is still on the send queue, wait until the transport
  327. * is done with it. This is particularly important for RDMA operations.
  328. */
  329. void rds_message_wait(struct rds_message *rm)
  330. {
  331. wait_event(rds_message_flush_waitq,
  332. !test_bit(RDS_MSG_MAPPED, &rm->m_flags));
  333. }
  334. void rds_message_unmapped(struct rds_message *rm)
  335. {
  336. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  337. if (waitqueue_active(&rds_message_flush_waitq))
  338. wake_up(&rds_message_flush_waitq);
  339. }
  340. EXPORT_SYMBOL_GPL(rds_message_unmapped);