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