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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include <linux/slab.h>
49#include <linux/vmalloc.h>
50#include <linux/mm.h>
51#include <asm/pgtable.h>
52#include "mmap.h"
53
54
55
56
57
58void rvt_mmap_init(struct rvt_dev_info *rdi)
59{
60 INIT_LIST_HEAD(&rdi->pending_mmaps);
61 spin_lock_init(&rdi->pending_lock);
62 rdi->mmap_offset = PAGE_SIZE;
63 spin_lock_init(&rdi->mmap_offset_lock);
64}
65
66
67
68
69
70void rvt_release_mmap_info(struct kref *ref)
71{
72 struct rvt_mmap_info *ip =
73 container_of(ref, struct rvt_mmap_info, ref);
74 struct rvt_dev_info *rdi = ib_to_rvt(ip->context->device);
75
76 spin_lock_irq(&rdi->pending_lock);
77 list_del(&ip->pending_mmaps);
78 spin_unlock_irq(&rdi->pending_lock);
79
80 vfree(ip->obj);
81 kfree(ip);
82}
83
84static void rvt_vma_open(struct vm_area_struct *vma)
85{
86 struct rvt_mmap_info *ip = vma->vm_private_data;
87
88 kref_get(&ip->ref);
89}
90
91static void rvt_vma_close(struct vm_area_struct *vma)
92{
93 struct rvt_mmap_info *ip = vma->vm_private_data;
94
95 kref_put(&ip->ref, rvt_release_mmap_info);
96}
97
98static const struct vm_operations_struct rvt_vm_ops = {
99 .open = rvt_vma_open,
100 .close = rvt_vma_close,
101};
102
103
104
105
106
107
108
109
110int rvt_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
111{
112 struct rvt_dev_info *rdi = ib_to_rvt(context->device);
113 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
114 unsigned long size = vma->vm_end - vma->vm_start;
115 struct rvt_mmap_info *ip, *pp;
116 int ret = -EINVAL;
117
118
119
120
121
122
123 spin_lock_irq(&rdi->pending_lock);
124 list_for_each_entry_safe(ip, pp, &rdi->pending_mmaps,
125 pending_mmaps) {
126
127 if (context != ip->context || (__u64)offset != ip->offset)
128 continue;
129
130 if (size > ip->size)
131 break;
132
133 list_del_init(&ip->pending_mmaps);
134 spin_unlock_irq(&rdi->pending_lock);
135
136 ret = remap_vmalloc_range(vma, ip->obj, 0);
137 if (ret)
138 goto done;
139 vma->vm_ops = &rvt_vm_ops;
140 vma->vm_private_data = ip;
141 rvt_vma_open(vma);
142 goto done;
143 }
144 spin_unlock_irq(&rdi->pending_lock);
145done:
146 return ret;
147}
148
149
150
151
152
153
154
155
156
157
158struct rvt_mmap_info *rvt_create_mmap_info(struct rvt_dev_info *rdi,
159 u32 size,
160 struct ib_ucontext *context,
161 void *obj)
162{
163 struct rvt_mmap_info *ip;
164
165 ip = kmalloc_node(sizeof(*ip), GFP_KERNEL, rdi->dparms.node);
166 if (!ip)
167 return ip;
168
169 size = PAGE_ALIGN(size);
170
171 spin_lock_irq(&rdi->mmap_offset_lock);
172 if (rdi->mmap_offset == 0)
173 rdi->mmap_offset = ALIGN(PAGE_SIZE, SHMLBA);
174 ip->offset = rdi->mmap_offset;
175 rdi->mmap_offset += ALIGN(size, SHMLBA);
176 spin_unlock_irq(&rdi->mmap_offset_lock);
177
178 INIT_LIST_HEAD(&ip->pending_mmaps);
179 ip->size = size;
180 ip->context = context;
181 ip->obj = obj;
182 kref_init(&ip->ref);
183
184 return ip;
185}
186
187
188
189
190
191
192
193
194void rvt_update_mmap_info(struct rvt_dev_info *rdi, struct rvt_mmap_info *ip,
195 u32 size, void *obj)
196{
197 size = PAGE_ALIGN(size);
198
199 spin_lock_irq(&rdi->mmap_offset_lock);
200 if (rdi->mmap_offset == 0)
201 rdi->mmap_offset = PAGE_SIZE;
202 ip->offset = rdi->mmap_offset;
203 rdi->mmap_offset += size;
204 spin_unlock_irq(&rdi->mmap_offset_lock);
205
206 ip->size = size;
207 ip->obj = obj;
208}
209