page.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/highmem.h>
  34. #include <linux/gfp.h>
  35. #include "rds.h"
  36. struct rds_page_remainder {
  37. struct page *r_page;
  38. unsigned long r_offset;
  39. };
  40. DEFINE_PER_CPU_SHARED_ALIGNED(struct rds_page_remainder, rds_page_remainders);
  41. /*
  42. * returns 0 on success or -errno on failure.
  43. *
  44. * We don't have to worry about flush_dcache_page() as this only works
  45. * with private pages. If, say, we were to do directed receive to pinned
  46. * user pages we'd have to worry more about cache coherence. (Though
  47. * the flush_dcache_page() in get_user_pages() would probably be enough).
  48. */
  49. int rds_page_copy_user(struct page *page, unsigned long offset,
  50. void __user *ptr, unsigned long bytes,
  51. int to_user)
  52. {
  53. unsigned long ret;
  54. void *addr;
  55. if (to_user)
  56. rds_stats_add(s_copy_to_user, bytes);
  57. else
  58. rds_stats_add(s_copy_from_user, bytes);
  59. addr = kmap_atomic(page, KM_USER0);
  60. if (to_user)
  61. ret = __copy_to_user_inatomic(ptr, addr + offset, bytes);
  62. else
  63. ret = __copy_from_user_inatomic(addr + offset, ptr, bytes);
  64. kunmap_atomic(addr, KM_USER0);
  65. if (ret) {
  66. addr = kmap(page);
  67. if (to_user)
  68. ret = copy_to_user(ptr, addr + offset, bytes);
  69. else
  70. ret = copy_from_user(addr + offset, ptr, bytes);
  71. kunmap(page);
  72. if (ret)
  73. return -EFAULT;
  74. }
  75. return 0;
  76. }
  77. EXPORT_SYMBOL_GPL(rds_page_copy_user);
  78. /*
  79. * Message allocation uses this to build up regions of a message.
  80. *
  81. * @bytes - the number of bytes needed.
  82. * @gfp - the waiting behaviour of the allocation
  83. *
  84. * @gfp is always ored with __GFP_HIGHMEM. Callers must be prepared to
  85. * kmap the pages, etc.
  86. *
  87. * If @bytes is at least a full page then this just returns a page from
  88. * alloc_page().
  89. *
  90. * If @bytes is a partial page then this stores the unused region of the
  91. * page in a per-cpu structure. Future partial-page allocations may be
  92. * satisfied from that cached region. This lets us waste less memory on
  93. * small allocations with minimal complexity. It works because the transmit
  94. * path passes read-only page regions down to devices. They hold a page
  95. * reference until they are done with the region.
  96. */
  97. int rds_page_remainder_alloc(struct scatterlist *scat, unsigned long bytes,
  98. gfp_t gfp)
  99. {
  100. struct rds_page_remainder *rem;
  101. unsigned long flags;
  102. struct page *page;
  103. int ret;
  104. gfp |= __GFP_HIGHMEM;
  105. /* jump straight to allocation if we're trying for a huge page */
  106. if (bytes >= PAGE_SIZE) {
  107. page = alloc_page(gfp);
  108. if (page == NULL) {
  109. ret = -ENOMEM;
  110. } else {
  111. sg_set_page(scat, page, PAGE_SIZE, 0);
  112. ret = 0;
  113. }
  114. goto out;
  115. }
  116. rem = &per_cpu(rds_page_remainders, get_cpu());
  117. local_irq_save(flags);
  118. while (1) {
  119. /* avoid a tiny region getting stuck by tossing it */
  120. if (rem->r_page && bytes > (PAGE_SIZE - rem->r_offset)) {
  121. rds_stats_inc(s_page_remainder_miss);
  122. __free_page(rem->r_page);
  123. rem->r_page = NULL;
  124. }
  125. /* hand out a fragment from the cached page */
  126. if (rem->r_page && bytes <= (PAGE_SIZE - rem->r_offset)) {
  127. sg_set_page(scat, rem->r_page, bytes, rem->r_offset);
  128. get_page(sg_page(scat));
  129. if (rem->r_offset != 0)
  130. rds_stats_inc(s_page_remainder_hit);
  131. rem->r_offset += bytes;
  132. if (rem->r_offset == PAGE_SIZE) {
  133. __free_page(rem->r_page);
  134. rem->r_page = NULL;
  135. }
  136. ret = 0;
  137. break;
  138. }
  139. /* alloc if there is nothing for us to use */
  140. local_irq_restore(flags);
  141. put_cpu();
  142. page = alloc_page(gfp);
  143. rem = &per_cpu(rds_page_remainders, get_cpu());
  144. local_irq_save(flags);
  145. if (page == NULL) {
  146. ret = -ENOMEM;
  147. break;
  148. }
  149. /* did someone race to fill the remainder before us? */
  150. if (rem->r_page) {
  151. __free_page(page);
  152. continue;
  153. }
  154. /* otherwise install our page and loop around to alloc */
  155. rem->r_page = page;
  156. rem->r_offset = 0;
  157. }
  158. local_irq_restore(flags);
  159. put_cpu();
  160. out:
  161. rdsdebug("bytes %lu ret %d %p %u %u\n", bytes, ret,
  162. ret ? NULL : sg_page(scat), ret ? 0 : scat->offset,
  163. ret ? 0 : scat->length);
  164. return ret;
  165. }
  166. static int rds_page_remainder_cpu_notify(struct notifier_block *self,
  167. unsigned long action, void *hcpu)
  168. {
  169. struct rds_page_remainder *rem;
  170. long cpu = (long)hcpu;
  171. rem = &per_cpu(rds_page_remainders, cpu);
  172. rdsdebug("cpu %ld action 0x%lx\n", cpu, action);
  173. switch (action) {
  174. case CPU_DEAD:
  175. if (rem->r_page)
  176. __free_page(rem->r_page);
  177. rem->r_page = NULL;
  178. break;
  179. }
  180. return 0;
  181. }
  182. static struct notifier_block rds_page_remainder_nb = {
  183. .notifier_call = rds_page_remainder_cpu_notify,
  184. };
  185. void rds_page_exit(void)
  186. {
  187. int i;
  188. for_each_possible_cpu(i)
  189. rds_page_remainder_cpu_notify(&rds_page_remainder_nb,
  190. (unsigned long)CPU_DEAD,
  191. (void *)(long)i);
  192. }