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