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
46DEFINE_PER_CPU_SHARED_ALIGNED(struct rds_page_remainder, rds_page_remainders);
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68int rds_page_remainder_alloc(struct scatterlist *scat, unsigned long bytes,
69 gfp_t gfp)
70{
71 struct rds_page_remainder *rem;
72 unsigned long flags;
73 struct page *page;
74 int ret;
75
76 gfp |= __GFP_HIGHMEM;
77
78
79 if (bytes >= PAGE_SIZE) {
80 page = alloc_page(gfp);
81 if (!page) {
82 ret = -ENOMEM;
83 } else {
84 sg_set_page(scat, page, PAGE_SIZE, 0);
85 ret = 0;
86 }
87 goto out;
88 }
89
90 rem = &per_cpu(rds_page_remainders, get_cpu());
91 local_irq_save(flags);
92
93 while (1) {
94
95 if (rem->r_page && bytes > (PAGE_SIZE - rem->r_offset)) {
96 rds_stats_inc(s_page_remainder_miss);
97 __free_page(rem->r_page);
98 rem->r_page = NULL;
99 }
100
101
102 if (rem->r_page && bytes <= (PAGE_SIZE - rem->r_offset)) {
103 sg_set_page(scat, rem->r_page, bytes, rem->r_offset);
104 get_page(sg_page(scat));
105
106 if (rem->r_offset != 0)
107 rds_stats_inc(s_page_remainder_hit);
108
109 rem->r_offset += ALIGN(bytes, 8);
110 if (rem->r_offset >= PAGE_SIZE) {
111 __free_page(rem->r_page);
112 rem->r_page = NULL;
113 }
114 ret = 0;
115 break;
116 }
117
118
119 local_irq_restore(flags);
120 put_cpu();
121
122 page = alloc_page(gfp);
123
124 rem = &per_cpu(rds_page_remainders, get_cpu());
125 local_irq_save(flags);
126
127 if (!page) {
128 ret = -ENOMEM;
129 break;
130 }
131
132
133 if (rem->r_page) {
134 __free_page(page);
135 continue;
136 }
137
138
139 rem->r_page = page;
140 rem->r_offset = 0;
141 }
142
143 local_irq_restore(flags);
144 put_cpu();
145out:
146 rdsdebug("bytes %lu ret %d %p %u %u\n", bytes, ret,
147 ret ? NULL : sg_page(scat), ret ? 0 : scat->offset,
148 ret ? 0 : scat->length);
149 return ret;
150}
151EXPORT_SYMBOL_GPL(rds_page_remainder_alloc);
152
153void rds_page_exit(void)
154{
155 unsigned int cpu;
156
157 for_each_possible_cpu(cpu) {
158 struct rds_page_remainder *rem;
159
160 rem = &per_cpu(rds_page_remainders, cpu);
161 rdsdebug("cpu %u\n", cpu);
162
163 if (rem->r_page)
164 __free_page(rem->r_page);
165 rem->r_page = NULL;
166 }
167}
168