Fix possible race in segdev_get and segdev_destroy segment
[archipelago] / xseg / sys / kernel / segdev.c
1 /*
2  * Copyright 2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *   2. Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and
30  * documentation are those of the authors and should not be
31  * interpreted as representing official policies, either expressed
32  * or implied, of GRNET S.A.
33  */
34
35 /*
36  *
37  */
38
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/moduleparam.h>
42 #include <linux/init.h>
43 #include <linux/mm.h>
44 #include <linux/fs.h>
45 #include <linux/init.h>
46 #include <linux/list.h>
47 #include <linux/cdev.h>
48 #include <linux/poll.h>
49 #include <linux/slab.h>
50 #include <linux/sched.h>
51 #include <linux/ioctl.h>
52 #include <linux/types.h>
53 #include <linux/module.h>
54 #include <linux/mmzone.h>
55 #include <linux/vmalloc.h>
56 #include <linux/spinlock.h>
57 #include <linux/wait.h>
58 #include <sys/util.h>
59 #include "segdev.h"
60
61 static struct segdev segdev;
62
63 int segdev_create_segment(struct segdev *dev, u64 segsize, char reserved)
64 {
65         void *segment;
66         int ret = mutex_lock_interruptible(&dev->mutex);
67         if (ret)
68                 goto out;
69
70         ret = -EBUSY;
71         if (dev->segment)
72                 goto out_unlock;
73
74         /* vmalloc can handle large sizes */
75         ret = -ENOMEM;
76         XSEGLOG("creating segment of size %llu\n", segsize);
77         segment = vmalloc(segsize);
78         if (!segment)
79                 goto out_unlock;
80
81         dev->segsize = segsize;
82         dev->segment = segment;
83         memset(dev->segment, 0, segsize);
84         set_bit(SEGDEV_READY, &dev->flags);
85         if (reserved)
86                 set_bit(SEGDEV_RESERVED, &dev->flags);
87         ret = 0;
88
89 out_unlock:
90         mutex_unlock(&dev->mutex);
91 out:
92         return ret;
93 }
94
95 EXPORT_SYMBOL(segdev_create_segment);
96
97 int segdev_destroy_segment(struct segdev *dev)
98 {
99         int ret = mutex_lock_interruptible(&dev->mutex);
100         if (ret)
101                 goto out;
102
103         /* VERIFY:
104          * The segment trully dies when everyone in userspace has unmapped it.
105          * However, the kernel mapping is immediately destroyed.
106          * Kernel users are notified to abort via switching of SEGDEV_READY.
107          * The mapping deallocation is performed when all kernel users
108          * have stopped using the segment as reported by usercount.
109         */
110
111         ret = -EINVAL;
112         if (!dev->segment)
113                 goto out_unlock;
114
115         ret = -EBUSY;
116         if (test_bit(SEGDEV_RESERVED, &dev->flags))
117                 goto out_unlock;
118
119         clear_bit(SEGDEV_READY, &dev->flags);
120         ret = wait_event_interruptible(dev->wq, atomic_read(&dev->usercount) <= 1);
121         if (ret)
122                 goto out_unlock;
123
124         vfree(dev->segment);
125         dev->segment = NULL;
126         dev->segsize = 0;
127         ret = 0;
128
129 out_unlock:
130         mutex_unlock(&dev->mutex);
131         set_bit(SEGDEV_READY, &dev->flags);
132 out:
133         return ret;
134 }
135
136 EXPORT_SYMBOL(segdev_destroy_segment);
137
138 struct segdev *segdev_get(int minor)
139 {
140         struct segdev *dev = ERR_PTR(-ENODEV);
141         if (minor)
142                 goto out;
143
144         dev = &segdev;
145         atomic_inc(&dev->usercount);
146         if (!test_bit(SEGDEV_READY, &dev->flags))
147                 goto fail_busy;
148 out:
149         return dev;
150
151 fail_busy:
152         segdev_put(dev)
153         dev = ERR_PTR(-EBUSY);
154         goto out;
155 }
156
157 EXPORT_SYMBOL(segdev_get);
158
159 void segdev_put(struct segdev *dev)
160 {
161         atomic_dec(&dev->usercount);
162         wake_up(&dev->wq);
163         /* ain't all this too heavy ? */
164 }
165
166 EXPORT_SYMBOL(segdev_put);
167
168 /* ********************* */
169 /* ** File Operations ** */
170 /* ********************* */
171
172 struct segdev_file {
173         int minor;
174 };
175
176 static int segdev_open(struct inode *inode, struct file *file)
177 {
178         struct segdev_file *vf = kmalloc(sizeof(struct segdev_file), GFP_KERNEL);
179         if (!vf)
180                 return -ENOMEM;
181         vf->minor = 0;
182         file->private_data = vf;
183         return 0;
184 }
185
186 static int segdev_release(struct inode *inode, struct file *file)
187 {
188         struct segdev_file *vf = file->private_data;
189         kfree(vf);
190         return 0;
191 }
192
193 static long segdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
194 {
195         struct segdev *dev;
196         char *seg;
197         long size;
198         int ret = -EINVAL;
199
200         switch (cmd) {
201
202         case SEGDEV_IOC_CREATESEG:
203                 dev = segdev_get(0);
204                 ret = IS_ERR(dev) ? PTR_ERR(dev) : 0;
205                 if (ret)
206                         goto out;
207
208                 ret = segdev_create_segment(dev, (u64)arg, 0);
209                 segdev_put(dev);
210                 goto out;
211
212         case SEGDEV_IOC_DESTROYSEG:
213                 dev = segdev_get(0);
214                 ret = segdev_destroy_segment(&segdev);
215                 segdev_put(dev);
216                 goto out;
217
218         case SEGDEV_IOC_SEGSIZE:
219                 dev = segdev_get(0);
220
221                 ret = IS_ERR(dev) ? PTR_ERR(dev) : 0;
222                 if (ret)
223                         goto out;
224
225                 size = dev->segsize;
226                 seg = dev->segment;
227                 segdev_put(dev);
228
229                 ret = -ENODEV;
230                 if (!seg)
231                         goto out;
232
233                 return size;
234         }
235
236 out:
237         return ret;
238 }
239
240 static ssize_t segdev_read(struct file *file, char __user *buf,
241                            size_t count, loff_t *f_pos)
242 {
243         return 0;
244 }
245
246 static ssize_t segdev_write(struct file *file, const char __user *buf,
247                             size_t count, loff_t *f_pos)
248 {
249         struct segdev_file *vf = file->private_data;
250         struct segdev *dev = segdev_get(vf->minor);
251         uint32_t portno;
252         int ret = -ENODEV;
253         if (!dev)
254                 goto out;
255
256         if (count != sizeof(uint32_t))
257                 goto out;
258
259         ret = copy_from_user(&portno, buf, sizeof(uint32_t));
260         if (ret < 0)
261                 goto out;
262
263         if((count - ret) != sizeof(uint32_t))
264                 goto out;
265
266         ret = 0;
267         if (dev->callback)
268                 dev->callback(dev, portno);
269         else
270                 ret = -ENOSYS;
271
272         dev->buffer_index = 0;
273         segdev_put(dev);
274 out:
275         return ret;
276 }
277
278 static int segdev_mmap(struct file *file, struct vm_area_struct *vma)
279 {
280         struct segdev_file *vf = file->private_data;
281         struct segdev *dev;
282         size_t size = vma->vm_end - vma->vm_start;
283         unsigned long start = vma->vm_start, end = start + size;
284         char *ptr;
285         int ret = -ENODEV;
286
287         dev = segdev_get(vf->minor);
288         if (IS_ERR(dev))
289                 goto out;
290
291         ptr = dev->segment;
292         if (!ptr)
293                 goto out_put;
294
295         ret = -EINVAL;
296
297         /* do not allow offset mappings, for now */
298         if (vma->vm_pgoff || size > dev->segsize)
299                 goto out_put;
300
301         /* allow only shared, read-write mappings */
302         if (!(vma->vm_flags & VM_SHARED))
303                 goto out_put;
304
305         /* the segment is vmalloc() so we have to iterate through
306          * all pages and laboriously map them one by one. */
307         for (; start < end; start += PAGE_SIZE, ptr += PAGE_SIZE) {
308                 ret = remap_pfn_range(vma, start, vmalloc_to_pfn(ptr),
309                                       PAGE_SIZE, vma->vm_page_prot);
310                 if (ret)
311                         goto out_put; /* mmap syscall should clean up, right? */
312         }
313
314         ret = 0;
315
316 out_put:
317         segdev_put(dev);
318 out:
319         return ret;
320 }
321
322 static struct file_operations segdev_ops = 
323 {
324         .owner          = THIS_MODULE,
325         .open           = segdev_open,
326         .release        = segdev_release,
327         .read           = segdev_read,
328         .write          = segdev_write,
329         .mmap           = segdev_mmap,
330         .unlocked_ioctl = segdev_ioctl,
331 };
332
333
334 /* *************************** */
335 /* ** Module Initialization ** */
336 /* *************************** */
337
338 static void segdev_init(struct segdev *dev, int minor)
339 {
340         dev->minor = 0;
341         dev->segment = NULL;
342         dev->segsize = 0;
343         dev->flags = 0;
344         atomic_set(&dev->usercount, 0);
345         init_waitqueue_head(&dev->wq);
346         cdev_init(&dev->cdev, &segdev_ops);
347         mutex_init(&dev->mutex);
348         spin_lock_init(&dev->lock);
349         dev->cdev.owner = THIS_MODULE;
350         set_bit(SEGDEV_READY, &dev->flags);
351 }
352
353 int __init segdev_mod_init(void)
354 {
355         int ret;
356         dev_t dev_no = MKDEV(SEGDEV_MAJOR, 0);
357         ret = register_chrdev_region(dev_no, 1, "segdev");
358         if (ret < 0)
359                 goto out;
360
361         segdev_init(&segdev, 0);
362         ret = cdev_add(&segdev.cdev, dev_no, 1);
363         if (ret < 0)
364                 goto out_unregister;
365
366         return ret;
367
368 out_unregister:
369         unregister_chrdev_region(dev_no, 1);
370 out:
371         return ret;
372 }
373
374 void __exit segdev_mod_exit(void)
375 {
376         dev_t dev_no = MKDEV(SEGDEV_MAJOR, 0);
377         segdev_destroy_segment(&segdev);
378         cdev_del(&segdev.cdev);
379         unregister_chrdev_region(dev_no, 1);
380 }
381
382 module_init(segdev_mod_init);
383 module_exit(segdev_mod_exit);
384
385 MODULE_DESCRIPTION("segdev");
386 MODULE_AUTHOR("XSEG");
387 MODULE_LICENSE("BSD");
388