message.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 <linux/slab.h>
  35. #include <linux/export.h>
  36. #include "rds.h"
  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. [RDS_EXTHDR_NPATHS] = sizeof(u16),
  43. };
  44. void rds_message_addref(struct rds_message *rm)
  45. {
  46. rdsdebug("addref rm %p ref %d\n", rm, atomic_read(&rm->m_refcount));
  47. atomic_inc(&rm->m_refcount);
  48. }
  49. EXPORT_SYMBOL_GPL(rds_message_addref);
  50. /*
  51. * This relies on dma_map_sg() not touching sg[].page during merging.
  52. */
  53. static void rds_message_purge(struct rds_message *rm)
  54. {
  55. unsigned long i;
  56. if (unlikely(test_bit(RDS_MSG_PAGEVEC, &rm->m_flags)))
  57. return;
  58. for (i = 0; i < rm->data.op_nents; i++) {
  59. rdsdebug("putting data page %p\n", (void *)sg_page(&rm->data.op_sg[i]));
  60. /* XXX will have to put_page for page refs */
  61. __free_page(sg_page(&rm->data.op_sg[i]));
  62. }
  63. rm->data.op_nents = 0;
  64. if (rm->rdma.op_active)
  65. rds_rdma_free_op(&rm->rdma);
  66. if (rm->rdma.op_rdma_mr)
  67. rds_mr_put(rm->rdma.op_rdma_mr);
  68. if (rm->atomic.op_active)
  69. rds_atomic_free_op(&rm->atomic);
  70. if (rm->atomic.op_rdma_mr)
  71. rds_mr_put(rm->atomic.op_rdma_mr);
  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. WARN(!atomic_read(&rm->m_refcount), "danger refcount zero on %p\n", rm);
  77. if (atomic_dec_and_test(&rm->m_refcount)) {
  78. BUG_ON(!list_empty(&rm->m_sock_item));
  79. BUG_ON(!list_empty(&rm->m_conn_item));
  80. rds_message_purge(rm);
  81. kfree(rm);
  82. }
  83. }
  84. EXPORT_SYMBOL_GPL(rds_message_put);
  85. void rds_message_populate_header(struct rds_header *hdr, __be16 sport,
  86. __be16 dport, u64 seq)
  87. {
  88. hdr->h_flags = 0;
  89. hdr->h_sport = sport;
  90. hdr->h_dport = dport;
  91. hdr->h_sequence = cpu_to_be64(seq);
  92. hdr->h_exthdr[0] = RDS_EXTHDR_NONE;
  93. }
  94. EXPORT_SYMBOL_GPL(rds_message_populate_header);
  95. int rds_message_add_extension(struct rds_header *hdr, unsigned int type,
  96. const void *data, unsigned int len)
  97. {
  98. unsigned int ext_len = sizeof(u8) + len;
  99. unsigned char *dst;
  100. /* For now, refuse to add more than one extension header */
  101. if (hdr->h_exthdr[0] != RDS_EXTHDR_NONE)
  102. return 0;
  103. if (type >= __RDS_EXTHDR_MAX || len != rds_exthdr_size[type])
  104. return 0;
  105. if (ext_len >= RDS_HEADER_EXT_SPACE)
  106. return 0;
  107. dst = hdr->h_exthdr;
  108. *dst++ = type;
  109. memcpy(dst, data, len);
  110. dst[len] = RDS_EXTHDR_NONE;
  111. return 1;
  112. }
  113. EXPORT_SYMBOL_GPL(rds_message_add_extension);
  114. /*
  115. * If a message has extension headers, retrieve them here.
  116. * Call like this:
  117. *
  118. * unsigned int pos = 0;
  119. *
  120. * while (1) {
  121. * buflen = sizeof(buffer);
  122. * type = rds_message_next_extension(hdr, &pos, buffer, &buflen);
  123. * if (type == RDS_EXTHDR_NONE)
  124. * break;
  125. * ...
  126. * }
  127. */
  128. int rds_message_next_extension(struct rds_header *hdr,
  129. unsigned int *pos, void *buf, unsigned int *buflen)
  130. {
  131. unsigned int offset, ext_type, ext_len;
  132. u8 *src = hdr->h_exthdr;
  133. offset = *pos;
  134. if (offset >= RDS_HEADER_EXT_SPACE)
  135. goto none;
  136. /* Get the extension type and length. For now, the
  137. * length is implied by the extension type. */
  138. ext_type = src[offset++];
  139. if (ext_type == RDS_EXTHDR_NONE || ext_type >= __RDS_EXTHDR_MAX)
  140. goto none;
  141. ext_len = rds_exthdr_size[ext_type];
  142. if (offset + ext_len > RDS_HEADER_EXT_SPACE)
  143. goto none;
  144. *pos = offset + ext_len;
  145. if (ext_len < *buflen)
  146. *buflen = ext_len;
  147. memcpy(buf, src + offset, *buflen);
  148. return ext_type;
  149. none:
  150. *pos = RDS_HEADER_EXT_SPACE;
  151. *buflen = 0;
  152. return RDS_EXTHDR_NONE;
  153. }
  154. int rds_message_add_rdma_dest_extension(struct rds_header *hdr, u32 r_key, u32 offset)
  155. {
  156. struct rds_ext_header_rdma_dest ext_hdr;
  157. ext_hdr.h_rdma_rkey = cpu_to_be32(r_key);
  158. ext_hdr.h_rdma_offset = cpu_to_be32(offset);
  159. return rds_message_add_extension(hdr, RDS_EXTHDR_RDMA_DEST, &ext_hdr, sizeof(ext_hdr));
  160. }
  161. EXPORT_SYMBOL_GPL(rds_message_add_rdma_dest_extension);
  162. /*
  163. * Each rds_message is allocated with extra space for the scatterlist entries
  164. * rds ops will need. This is to minimize memory allocation count. Then, each rds op
  165. * can grab SGs when initializing its part of the rds_message.
  166. */
  167. struct rds_message *rds_message_alloc(unsigned int extra_len, gfp_t gfp)
  168. {
  169. struct rds_message *rm;
  170. if (extra_len > KMALLOC_MAX_SIZE - sizeof(struct rds_message))
  171. return NULL;
  172. rm = kzalloc(sizeof(struct rds_message) + extra_len, gfp);
  173. if (!rm)
  174. goto out;
  175. rm->m_used_sgs = 0;
  176. rm->m_total_sgs = extra_len / sizeof(struct scatterlist);
  177. atomic_set(&rm->m_refcount, 1);
  178. INIT_LIST_HEAD(&rm->m_sock_item);
  179. INIT_LIST_HEAD(&rm->m_conn_item);
  180. spin_lock_init(&rm->m_rs_lock);
  181. init_waitqueue_head(&rm->m_flush_wait);
  182. out:
  183. return rm;
  184. }
  185. /*
  186. * RDS ops use this to grab SG entries from the rm's sg pool.
  187. */
  188. struct scatterlist *rds_message_alloc_sgs(struct rds_message *rm, int nents)
  189. {
  190. struct scatterlist *sg_first = (struct scatterlist *) &rm[1];
  191. struct scatterlist *sg_ret;
  192. WARN_ON(rm->m_used_sgs + nents > rm->m_total_sgs);
  193. WARN_ON(!nents);
  194. if (rm->m_used_sgs + nents > rm->m_total_sgs)
  195. return NULL;
  196. sg_ret = &sg_first[rm->m_used_sgs];
  197. sg_init_table(sg_ret, nents);
  198. rm->m_used_sgs += nents;
  199. return sg_ret;
  200. }
  201. struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len)
  202. {
  203. struct rds_message *rm;
  204. unsigned int i;
  205. int num_sgs = ceil(total_len, PAGE_SIZE);
  206. int extra_bytes = num_sgs * sizeof(struct scatterlist);
  207. rm = rds_message_alloc(extra_bytes, GFP_NOWAIT);
  208. if (!rm)
  209. return ERR_PTR(-ENOMEM);
  210. set_bit(RDS_MSG_PAGEVEC, &rm->m_flags);
  211. rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len);
  212. rm->data.op_nents = ceil(total_len, PAGE_SIZE);
  213. rm->data.op_sg = rds_message_alloc_sgs(rm, num_sgs);
  214. if (!rm->data.op_sg) {
  215. rds_message_put(rm);
  216. return ERR_PTR(-ENOMEM);
  217. }
  218. for (i = 0; i < rm->data.op_nents; ++i) {
  219. sg_set_page(&rm->data.op_sg[i],
  220. virt_to_page(page_addrs[i]),
  221. PAGE_SIZE, 0);
  222. }
  223. return rm;
  224. }
  225. int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from)
  226. {
  227. unsigned long to_copy, nbytes;
  228. unsigned long sg_off;
  229. struct scatterlist *sg;
  230. int ret = 0;
  231. rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from));
  232. /*
  233. * now allocate and copy in the data payload.
  234. */
  235. sg = rm->data.op_sg;
  236. sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */
  237. while (iov_iter_count(from)) {
  238. if (!sg_page(sg)) {
  239. ret = rds_page_remainder_alloc(sg, iov_iter_count(from),
  240. GFP_HIGHUSER);
  241. if (ret)
  242. return ret;
  243. rm->data.op_nents++;
  244. sg_off = 0;
  245. }
  246. to_copy = min_t(unsigned long, iov_iter_count(from),
  247. sg->length - sg_off);
  248. rds_stats_add(s_copy_from_user, to_copy);
  249. nbytes = copy_page_from_iter(sg_page(sg), sg->offset + sg_off,
  250. to_copy, from);
  251. if (nbytes != to_copy)
  252. return -EFAULT;
  253. sg_off += to_copy;
  254. if (sg_off == sg->length)
  255. sg++;
  256. }
  257. return ret;
  258. }
  259. int rds_message_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to)
  260. {
  261. struct rds_message *rm;
  262. struct scatterlist *sg;
  263. unsigned long to_copy;
  264. unsigned long vec_off;
  265. int copied;
  266. int ret;
  267. u32 len;
  268. rm = container_of(inc, struct rds_message, m_inc);
  269. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  270. sg = rm->data.op_sg;
  271. vec_off = 0;
  272. copied = 0;
  273. while (iov_iter_count(to) && copied < len) {
  274. to_copy = min_t(unsigned long, iov_iter_count(to),
  275. sg->length - vec_off);
  276. to_copy = min_t(unsigned long, to_copy, len - copied);
  277. rds_stats_add(s_copy_to_user, to_copy);
  278. ret = copy_page_to_iter(sg_page(sg), sg->offset + vec_off,
  279. to_copy, to);
  280. if (ret != to_copy)
  281. return -EFAULT;
  282. vec_off += to_copy;
  283. copied += to_copy;
  284. if (vec_off == sg->length) {
  285. vec_off = 0;
  286. sg++;
  287. }
  288. }
  289. return copied;
  290. }
  291. /*
  292. * If the message is still on the send queue, wait until the transport
  293. * is done with it. This is particularly important for RDMA operations.
  294. */
  295. void rds_message_wait(struct rds_message *rm)
  296. {
  297. wait_event_interruptible(rm->m_flush_wait,
  298. !test_bit(RDS_MSG_MAPPED, &rm->m_flags));
  299. }
  300. void rds_message_unmapped(struct rds_message *rm)
  301. {
  302. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  303. wake_up_interruptible(&rm->m_flush_wait);
  304. }
  305. EXPORT_SYMBOL_GPL(rds_message_unmapped);