1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/highmem.h>
34#include <linux/gfp.h>
35#include <linux/cpu.h>
36#include <linux/export.h>
37
38#include "rds.h"
39
40struct rds_page_remainder {
41 struct page *r_page;
42 unsigned long r_offset;
43};
44
45static DEFINE_PER_CPU_SHARED_ALIGNED(struct rds_page_remainder,
46 rds_page_remainders);
47
48
49
50
51
52
53
54
55
56int rds_page_copy_user(struct page *page, unsigned long offset,
57 void __user *ptr, unsigned long bytes,
58 int to_user)
59{
60 unsigned long ret;
61 void *addr;
62
63 addr = kmap(page);
64 if (to_user) {
65 rds_stats_add(s_copy_to_user, bytes);
66 ret = copy_to_user(ptr, addr + offset, bytes);
67 } else {
68 rds_stats_add(s_copy_from_user, bytes);
69 ret = copy_from_user(addr + offset, ptr, bytes);
70 }
71 kunmap(page);
72
73 return ret ? -EFAULT : 0;
74}
75EXPORT_SYMBOL_GPL(rds_page_copy_user);
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97int rds_page_remainder_alloc(struct scatterlist *scat, unsigned long bytes,
98 gfp_t gfp)
99{
100 struct rds_page_remainder *rem;
101 unsigned long flags;
102 struct page *page;
103 int ret;
104
105 gfp |= __GFP_HIGHMEM;
106
107
108 if (bytes >= PAGE_SIZE) {
109 page = alloc_page(gfp);
110 if (!page) {
111 ret = -ENOMEM;
112 } else {
113 sg_set_page(scat, page, PAGE_SIZE, 0);
114 ret = 0;
115 }
116 goto out;
117 }
118
119 rem = &per_cpu(rds_page_remainders, get_cpu());
120 local_irq_save(flags);
121
122 while (1) {
123
124 if (rem->r_page && bytes > (PAGE_SIZE - rem->r_offset)) {
125 rds_stats_inc(s_page_remainder_miss);
126 __free_page(rem->r_page);
127 rem->r_page = NULL;
128 }
129
130
131 if (rem->r_page && bytes <= (PAGE_SIZE - rem->r_offset)) {
132 sg_set_page(scat, rem->r_page, bytes, rem->r_offset);
133 get_page(sg_page(scat));
134
135 if (rem->r_offset != 0)
136 rds_stats_inc(s_page_remainder_hit);
137
138 rem->r_offset += ALIGN(bytes, 8);
139 if (rem->r_offset >= PAGE_SIZE) {
140 __free_page(rem->r_page);
141 rem->r_page = NULL;
142 }
143 ret = 0;
144 break;
145 }
146
147
148 local_irq_restore(flags);
149 put_cpu();
150
151 page = alloc_page(gfp);
152
153 rem = &per_cpu(rds_page_remainders, get_cpu());
154 local_irq_save(flags);
155
156 if (!page) {
157 ret = -ENOMEM;
158 break;
159 }
160
161
162 if (rem->r_page) {
163 __free_page(page);
164 continue;
165 }
166
167
168 rem->r_page = page;
169 rem->r_offset = 0;
170 }
171
172 local_irq_restore(flags);
173 put_cpu();
174out:
175 rdsdebug("bytes %lu ret %d %p %u %u\n", bytes, ret,
176 ret ? NULL : sg_page(scat), ret ? 0 : scat->offset,
177 ret ? 0 : scat->length);
178 return ret;
179}
180EXPORT_SYMBOL_GPL(rds_page_remainder_alloc);
181
182void rds_page_exit(void)
183{
184 unsigned int cpu;
185
186 for_each_possible_cpu(cpu) {
187 struct rds_page_remainder *rem;
188
189 rem = &per_cpu(rds_page_remainders, cpu);
190 rdsdebug("cpu %u\n", cpu);
191
192 if (rem->r_page)
193 __free_page(rem->r_page);
194 rem->r_page = NULL;
195 }
196}
197