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#include <linux/module.h>
35#include <linux/vmalloc.h>
36#include <linux/mm.h>
37#include <linux/errno.h>
38#include <asm/pgtable.h>
39#include <linux/shm.h>
40
41#include "rxe.h"
42#include "rxe_loc.h"
43#include "rxe_queue.h"
44
45void rxe_mmap_release(struct kref *ref)
46{
47 struct rxe_mmap_info *ip = container_of(ref,
48 struct rxe_mmap_info, ref);
49 struct rxe_dev *rxe = to_rdev(ip->context->device);
50
51 spin_lock_bh(&rxe->pending_lock);
52
53 if (!list_empty(&ip->pending_mmaps))
54 list_del(&ip->pending_mmaps);
55
56 spin_unlock_bh(&rxe->pending_lock);
57
58 vfree(ip->obj);
59 kfree(ip);
60}
61
62
63
64
65
66static void rxe_vma_open(struct vm_area_struct *vma)
67{
68 struct rxe_mmap_info *ip = vma->vm_private_data;
69
70 kref_get(&ip->ref);
71}
72
73static void rxe_vma_close(struct vm_area_struct *vma)
74{
75 struct rxe_mmap_info *ip = vma->vm_private_data;
76
77 kref_put(&ip->ref, rxe_mmap_release);
78}
79
80static const struct vm_operations_struct rxe_vm_ops = {
81 .open = rxe_vma_open,
82 .close = rxe_vma_close,
83};
84
85
86
87
88
89
90
91int rxe_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
92{
93 struct rxe_dev *rxe = to_rdev(context->device);
94 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
95 unsigned long size = vma->vm_end - vma->vm_start;
96 struct rxe_mmap_info *ip, *pp;
97 int ret;
98
99
100
101
102
103
104 spin_lock_bh(&rxe->pending_lock);
105 list_for_each_entry_safe(ip, pp, &rxe->pending_mmaps, pending_mmaps) {
106 if (context != ip->context || (__u64)offset != ip->info.offset)
107 continue;
108
109
110 if (size > ip->info.size) {
111 pr_err("mmap region is larger than the object!\n");
112 spin_unlock_bh(&rxe->pending_lock);
113 ret = -EINVAL;
114 goto done;
115 }
116
117 goto found_it;
118 }
119 pr_warn("unable to find pending mmap info\n");
120 spin_unlock_bh(&rxe->pending_lock);
121 ret = -EINVAL;
122 goto done;
123
124found_it:
125 list_del_init(&ip->pending_mmaps);
126 spin_unlock_bh(&rxe->pending_lock);
127
128 ret = remap_vmalloc_range(vma, ip->obj, 0);
129 if (ret) {
130 pr_err("err %d from remap_vmalloc_range\n", ret);
131 goto done;
132 }
133
134 vma->vm_ops = &rxe_vm_ops;
135 vma->vm_private_data = ip;
136 rxe_vma_open(vma);
137done:
138 return ret;
139}
140
141
142
143
144struct rxe_mmap_info *rxe_create_mmap_info(struct rxe_dev *rxe,
145 u32 size,
146 struct ib_ucontext *context,
147 void *obj)
148{
149 struct rxe_mmap_info *ip;
150
151 ip = kmalloc(sizeof(*ip), GFP_KERNEL);
152 if (!ip)
153 return NULL;
154
155 size = PAGE_ALIGN(size);
156
157 spin_lock_bh(&rxe->mmap_offset_lock);
158
159 if (rxe->mmap_offset == 0)
160 rxe->mmap_offset = ALIGN(PAGE_SIZE, SHMLBA);
161
162 ip->info.offset = rxe->mmap_offset;
163 rxe->mmap_offset += ALIGN(size, SHMLBA);
164
165 spin_unlock_bh(&rxe->mmap_offset_lock);
166
167 INIT_LIST_HEAD(&ip->pending_mmaps);
168 ip->info.size = size;
169 ip->context = context;
170 ip->obj = obj;
171 kref_init(&ip->ref);
172
173 return ip;
174}
175