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