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