page.c 5.7 KB

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