GNU Linux-libre 4.9.333-gnu1
[releases.git] / drivers / vhost / vringh.c
1 /*
2  * Helpers for the host side of a virtio ring.
3  *
4  * Since these may be in userspace, we use (inline) accessors.
5  */
6 #include <linux/module.h>
7 #include <linux/vringh.h>
8 #include <linux/virtio_ring.h>
9 #include <linux/kernel.h>
10 #include <linux/ratelimit.h>
11 #include <linux/uaccess.h>
12 #include <linux/slab.h>
13 #include <linux/export.h>
14 #include <uapi/linux/virtio_config.h>
15
16 static __printf(1,2) __cold void vringh_bad(const char *fmt, ...)
17 {
18         static DEFINE_RATELIMIT_STATE(vringh_rs,
19                                       DEFAULT_RATELIMIT_INTERVAL,
20                                       DEFAULT_RATELIMIT_BURST);
21         if (__ratelimit(&vringh_rs)) {
22                 va_list ap;
23                 va_start(ap, fmt);
24                 printk(KERN_NOTICE "vringh:");
25                 vprintk(fmt, ap);
26                 va_end(ap);
27         }
28 }
29
30 /* Returns vring->num if empty, -ve on error. */
31 static inline int __vringh_get_head(const struct vringh *vrh,
32                                     int (*getu16)(const struct vringh *vrh,
33                                                   u16 *val, const __virtio16 *p),
34                                     u16 *last_avail_idx)
35 {
36         u16 avail_idx, i, head;
37         int err;
38
39         err = getu16(vrh, &avail_idx, &vrh->vring.avail->idx);
40         if (err) {
41                 vringh_bad("Failed to access avail idx at %p",
42                            &vrh->vring.avail->idx);
43                 return err;
44         }
45
46         if (*last_avail_idx == avail_idx)
47                 return vrh->vring.num;
48
49         /* Only get avail ring entries after they have been exposed by guest. */
50         virtio_rmb(vrh->weak_barriers);
51
52         i = *last_avail_idx & (vrh->vring.num - 1);
53
54         err = getu16(vrh, &head, &vrh->vring.avail->ring[i]);
55         if (err) {
56                 vringh_bad("Failed to read head: idx %d address %p",
57                            *last_avail_idx, &vrh->vring.avail->ring[i]);
58                 return err;
59         }
60
61         if (head >= vrh->vring.num) {
62                 vringh_bad("Guest says index %u > %u is available",
63                            head, vrh->vring.num);
64                 return -EINVAL;
65         }
66
67         (*last_avail_idx)++;
68         return head;
69 }
70
71 /* Copy some bytes to/from the iovec.  Returns num copied. */
72 static inline ssize_t vringh_iov_xfer(struct vringh_kiov *iov,
73                                       void *ptr, size_t len,
74                                       int (*xfer)(void *addr, void *ptr,
75                                                   size_t len))
76 {
77         int err, done = 0;
78
79         while (len && iov->i < iov->used) {
80                 size_t partlen;
81
82                 partlen = min(iov->iov[iov->i].iov_len, len);
83                 err = xfer(iov->iov[iov->i].iov_base, ptr, partlen);
84                 if (err)
85                         return err;
86                 done += partlen;
87                 len -= partlen;
88                 ptr += partlen;
89                 iov->consumed += partlen;
90                 iov->iov[iov->i].iov_len -= partlen;
91                 iov->iov[iov->i].iov_base += partlen;
92
93                 if (!iov->iov[iov->i].iov_len) {
94                         /* Fix up old iov element then increment. */
95                         iov->iov[iov->i].iov_len = iov->consumed;
96                         iov->iov[iov->i].iov_base -= iov->consumed;
97                         
98                         iov->consumed = 0;
99                         iov->i++;
100                 }
101         }
102         return done;
103 }
104
105 /* May reduce *len if range is shorter. */
106 static inline bool range_check(struct vringh *vrh, u64 addr, size_t *len,
107                                struct vringh_range *range,
108                                bool (*getrange)(struct vringh *,
109                                                 u64, struct vringh_range *))
110 {
111         if (addr < range->start || addr > range->end_incl) {
112                 if (!getrange(vrh, addr, range))
113                         return false;
114         }
115         BUG_ON(addr < range->start || addr > range->end_incl);
116
117         /* To end of memory? */
118         if (unlikely(addr + *len == 0)) {
119                 if (range->end_incl == -1ULL)
120                         return true;
121                 goto truncate;
122         }
123
124         /* Otherwise, don't wrap. */
125         if (addr + *len < addr) {
126                 vringh_bad("Wrapping descriptor %zu@0x%llx",
127                            *len, (unsigned long long)addr);
128                 return false;
129         }
130
131         if (unlikely(addr + *len - 1 > range->end_incl))
132                 goto truncate;
133         return true;
134
135 truncate:
136         *len = range->end_incl + 1 - addr;
137         return true;
138 }
139
140 static inline bool no_range_check(struct vringh *vrh, u64 addr, size_t *len,
141                                   struct vringh_range *range,
142                                   bool (*getrange)(struct vringh *,
143                                                    u64, struct vringh_range *))
144 {
145         return true;
146 }
147
148 /* No reason for this code to be inline. */
149 static int move_to_indirect(const struct vringh *vrh,
150                             int *up_next, u16 *i, void *addr,
151                             const struct vring_desc *desc,
152                             struct vring_desc **descs, int *desc_max)
153 {
154         u32 len;
155
156         /* Indirect tables can't have indirect. */
157         if (*up_next != -1) {
158                 vringh_bad("Multilevel indirect %u->%u", *up_next, *i);
159                 return -EINVAL;
160         }
161
162         len = vringh32_to_cpu(vrh, desc->len);
163         if (unlikely(len % sizeof(struct vring_desc))) {
164                 vringh_bad("Strange indirect len %u", desc->len);
165                 return -EINVAL;
166         }
167
168         /* We will check this when we follow it! */
169         if (desc->flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT))
170                 *up_next = vringh16_to_cpu(vrh, desc->next);
171         else
172                 *up_next = -2;
173         *descs = addr;
174         *desc_max = len / sizeof(struct vring_desc);
175
176         /* Now, start at the first indirect. */
177         *i = 0;
178         return 0;
179 }
180
181 static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp)
182 {
183         struct kvec *new;
184         unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED) * 2;
185
186         if (new_num < 8)
187                 new_num = 8;
188
189         flag = (iov->max_num & VRINGH_IOV_ALLOCATED);
190         if (flag)
191                 new = krealloc(iov->iov, new_num * sizeof(struct iovec), gfp);
192         else {
193                 new = kmalloc(new_num * sizeof(struct iovec), gfp);
194                 if (new) {
195                         memcpy(new, iov->iov,
196                                iov->max_num * sizeof(struct iovec));
197                         flag = VRINGH_IOV_ALLOCATED;
198                 }
199         }
200         if (!new)
201                 return -ENOMEM;
202         iov->iov = new;
203         iov->max_num = (new_num | flag);
204         return 0;
205 }
206
207 static u16 __cold return_from_indirect(const struct vringh *vrh, int *up_next,
208                                        struct vring_desc **descs, int *desc_max)
209 {
210         u16 i = *up_next;
211
212         *up_next = -1;
213         *descs = vrh->vring.desc;
214         *desc_max = vrh->vring.num;
215         return i;
216 }
217
218 static int slow_copy(struct vringh *vrh, void *dst, const void *src,
219                      bool (*rcheck)(struct vringh *vrh, u64 addr, size_t *len,
220                                     struct vringh_range *range,
221                                     bool (*getrange)(struct vringh *vrh,
222                                                      u64,
223                                                      struct vringh_range *)),
224                      bool (*getrange)(struct vringh *vrh,
225                                       u64 addr,
226                                       struct vringh_range *r),
227                      struct vringh_range *range,
228                      int (*copy)(void *dst, const void *src, size_t len))
229 {
230         size_t part, len = sizeof(struct vring_desc);
231
232         do {
233                 u64 addr;
234                 int err;
235
236                 part = len;
237                 addr = (u64)(unsigned long)src - range->offset;
238
239                 if (!rcheck(vrh, addr, &part, range, getrange))
240                         return -EINVAL;
241
242                 err = copy(dst, src, part);
243                 if (err)
244                         return err;
245
246                 dst += part;
247                 src += part;
248                 len -= part;
249         } while (len);
250         return 0;
251 }
252
253 static inline int
254 __vringh_iov(struct vringh *vrh, u16 i,
255              struct vringh_kiov *riov,
256              struct vringh_kiov *wiov,
257              bool (*rcheck)(struct vringh *vrh, u64 addr, size_t *len,
258                             struct vringh_range *range,
259                             bool (*getrange)(struct vringh *, u64,
260                                              struct vringh_range *)),
261              bool (*getrange)(struct vringh *, u64, struct vringh_range *),
262              gfp_t gfp,
263              int (*copy)(void *dst, const void *src, size_t len))
264 {
265         int err, count = 0, indirect_count = 0, up_next, desc_max;
266         struct vring_desc desc, *descs;
267         struct vringh_range range = { -1ULL, 0 }, slowrange;
268         bool slow = false;
269
270         /* We start traversing vring's descriptor table. */
271         descs = vrh->vring.desc;
272         desc_max = vrh->vring.num;
273         up_next = -1;
274
275         /* You must want something! */
276         if (WARN_ON(!riov && !wiov))
277                 return -EINVAL;
278
279         if (riov)
280                 riov->i = riov->used = 0;
281         if (wiov)
282                 wiov->i = wiov->used = 0;
283
284         for (;;) {
285                 void *addr;
286                 struct vringh_kiov *iov;
287                 size_t len;
288
289                 if (unlikely(slow))
290                         err = slow_copy(vrh, &desc, &descs[i], rcheck, getrange,
291                                         &slowrange, copy);
292                 else
293                         err = copy(&desc, &descs[i], sizeof(desc));
294                 if (unlikely(err))
295                         goto fail;
296
297                 if (unlikely(desc.flags &
298                              cpu_to_vringh16(vrh, VRING_DESC_F_INDIRECT))) {
299                         u64 a = vringh64_to_cpu(vrh, desc.addr);
300
301                         /* Make sure it's OK, and get offset. */
302                         len = vringh32_to_cpu(vrh, desc.len);
303                         if (!rcheck(vrh, a, &len, &range, getrange)) {
304                                 err = -EINVAL;
305                                 goto fail;
306                         }
307
308                         if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
309                                 slow = true;
310                                 /* We need to save this range to use offset */
311                                 slowrange = range;
312                         }
313
314                         addr = (void *)(long)(a + range.offset);
315                         err = move_to_indirect(vrh, &up_next, &i, addr, &desc,
316                                                &descs, &desc_max);
317                         if (err)
318                                 goto fail;
319                         continue;
320                 }
321
322                 if (up_next == -1)
323                         count++;
324                 else
325                         indirect_count++;
326
327                 if (count > vrh->vring.num || indirect_count > desc_max) {
328                         vringh_bad("Descriptor loop in %p", descs);
329                         err = -ELOOP;
330                         goto fail;
331                 }
332
333                 if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_WRITE))
334                         iov = wiov;
335                 else {
336                         iov = riov;
337                         if (unlikely(wiov && wiov->used)) {
338                                 vringh_bad("Readable desc %p after writable",
339                                            &descs[i]);
340                                 err = -EINVAL;
341                                 goto fail;
342                         }
343                 }
344
345                 if (!iov) {
346                         vringh_bad("Unexpected %s desc",
347                                    !wiov ? "writable" : "readable");
348                         err = -EPROTO;
349                         goto fail;
350                 }
351
352         again:
353                 /* Make sure it's OK, and get offset. */
354                 len = vringh32_to_cpu(vrh, desc.len);
355                 if (!rcheck(vrh, vringh64_to_cpu(vrh, desc.addr), &len, &range,
356                             getrange)) {
357                         err = -EINVAL;
358                         goto fail;
359                 }
360                 addr = (void *)(unsigned long)(vringh64_to_cpu(vrh, desc.addr) +
361                                                range.offset);
362
363                 if (unlikely(iov->used == (iov->max_num & ~VRINGH_IOV_ALLOCATED))) {
364                         err = resize_iovec(iov, gfp);
365                         if (err)
366                                 goto fail;
367                 }
368
369                 iov->iov[iov->used].iov_base = addr;
370                 iov->iov[iov->used].iov_len = len;
371                 iov->used++;
372
373                 if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
374                         desc.len = cpu_to_vringh32(vrh,
375                                    vringh32_to_cpu(vrh, desc.len) - len);
376                         desc.addr = cpu_to_vringh64(vrh,
377                                     vringh64_to_cpu(vrh, desc.addr) + len);
378                         goto again;
379                 }
380
381                 if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT)) {
382                         i = vringh16_to_cpu(vrh, desc.next);
383                 } else {
384                         /* Just in case we need to finish traversing above. */
385                         if (unlikely(up_next > 0)) {
386                                 i = return_from_indirect(vrh, &up_next,
387                                                          &descs, &desc_max);
388                                 slow = false;
389                                 indirect_count = 0;
390                         } else
391                                 break;
392                 }
393
394                 if (i >= desc_max) {
395                         vringh_bad("Chained index %u > %u", i, desc_max);
396                         err = -EINVAL;
397                         goto fail;
398                 }
399         }
400
401         return 0;
402
403 fail:
404         return err;
405 }
406
407 static inline int __vringh_complete(struct vringh *vrh,
408                                     const struct vring_used_elem *used,
409                                     unsigned int num_used,
410                                     int (*putu16)(const struct vringh *vrh,
411                                                   __virtio16 *p, u16 val),
412                                     int (*putused)(struct vring_used_elem *dst,
413                                                    const struct vring_used_elem
414                                                    *src, unsigned num))
415 {
416         struct vring_used *used_ring;
417         int err;
418         u16 used_idx, off;
419
420         used_ring = vrh->vring.used;
421         used_idx = vrh->last_used_idx + vrh->completed;
422
423         off = used_idx % vrh->vring.num;
424
425         /* Compiler knows num_used == 1 sometimes, hence extra check */
426         if (num_used > 1 && unlikely(off + num_used >= vrh->vring.num)) {
427                 u16 part = vrh->vring.num - off;
428                 err = putused(&used_ring->ring[off], used, part);
429                 if (!err)
430                         err = putused(&used_ring->ring[0], used + part,
431                                       num_used - part);
432         } else
433                 err = putused(&used_ring->ring[off], used, num_used);
434
435         if (err) {
436                 vringh_bad("Failed to write %u used entries %u at %p",
437                            num_used, off, &used_ring->ring[off]);
438                 return err;
439         }
440
441         /* Make sure buffer is written before we update index. */
442         virtio_wmb(vrh->weak_barriers);
443
444         err = putu16(vrh, &vrh->vring.used->idx, used_idx + num_used);
445         if (err) {
446                 vringh_bad("Failed to update used index at %p",
447                            &vrh->vring.used->idx);
448                 return err;
449         }
450
451         vrh->completed += num_used;
452         return 0;
453 }
454
455
456 static inline int __vringh_need_notify(struct vringh *vrh,
457                                        int (*getu16)(const struct vringh *vrh,
458                                                      u16 *val,
459                                                      const __virtio16 *p))
460 {
461         bool notify;
462         u16 used_event;
463         int err;
464
465         /* Flush out used index update. This is paired with the
466          * barrier that the Guest executes when enabling
467          * interrupts. */
468         virtio_mb(vrh->weak_barriers);
469
470         /* Old-style, without event indices. */
471         if (!vrh->event_indices) {
472                 u16 flags;
473                 err = getu16(vrh, &flags, &vrh->vring.avail->flags);
474                 if (err) {
475                         vringh_bad("Failed to get flags at %p",
476                                    &vrh->vring.avail->flags);
477                         return err;
478                 }
479                 return (!(flags & VRING_AVAIL_F_NO_INTERRUPT));
480         }
481
482         /* Modern: we know when other side wants to know. */
483         err = getu16(vrh, &used_event, &vring_used_event(&vrh->vring));
484         if (err) {
485                 vringh_bad("Failed to get used event idx at %p",
486                            &vring_used_event(&vrh->vring));
487                 return err;
488         }
489
490         /* Just in case we added so many that we wrap. */
491         if (unlikely(vrh->completed > 0xffff))
492                 notify = true;
493         else
494                 notify = vring_need_event(used_event,
495                                           vrh->last_used_idx + vrh->completed,
496                                           vrh->last_used_idx);
497
498         vrh->last_used_idx += vrh->completed;
499         vrh->completed = 0;
500         return notify;
501 }
502
503 static inline bool __vringh_notify_enable(struct vringh *vrh,
504                                           int (*getu16)(const struct vringh *vrh,
505                                                         u16 *val, const __virtio16 *p),
506                                           int (*putu16)(const struct vringh *vrh,
507                                                         __virtio16 *p, u16 val))
508 {
509         u16 avail;
510
511         if (!vrh->event_indices) {
512                 /* Old-school; update flags. */
513                 if (putu16(vrh, &vrh->vring.used->flags, 0) != 0) {
514                         vringh_bad("Clearing used flags %p",
515                                    &vrh->vring.used->flags);
516                         return true;
517                 }
518         } else {
519                 if (putu16(vrh, &vring_avail_event(&vrh->vring),
520                            vrh->last_avail_idx) != 0) {
521                         vringh_bad("Updating avail event index %p",
522                                    &vring_avail_event(&vrh->vring));
523                         return true;
524                 }
525         }
526
527         /* They could have slipped one in as we were doing that: make
528          * sure it's written, then check again. */
529         virtio_mb(vrh->weak_barriers);
530
531         if (getu16(vrh, &avail, &vrh->vring.avail->idx) != 0) {
532                 vringh_bad("Failed to check avail idx at %p",
533                            &vrh->vring.avail->idx);
534                 return true;
535         }
536
537         /* This is unlikely, so we just leave notifications enabled
538          * (if we're using event_indices, we'll only get one
539          * notification anyway). */
540         return avail == vrh->last_avail_idx;
541 }
542
543 static inline void __vringh_notify_disable(struct vringh *vrh,
544                                            int (*putu16)(const struct vringh *vrh,
545                                                          __virtio16 *p, u16 val))
546 {
547         if (!vrh->event_indices) {
548                 /* Old-school; update flags. */
549                 if (putu16(vrh, &vrh->vring.used->flags,
550                            VRING_USED_F_NO_NOTIFY)) {
551                         vringh_bad("Setting used flags %p",
552                                    &vrh->vring.used->flags);
553                 }
554         }
555 }
556
557 /* Userspace access helpers: in this case, addresses are really userspace. */
558 static inline int getu16_user(const struct vringh *vrh, u16 *val, const __virtio16 *p)
559 {
560         __virtio16 v = 0;
561         int rc = get_user(v, (__force __virtio16 __user *)p);
562         *val = vringh16_to_cpu(vrh, v);
563         return rc;
564 }
565
566 static inline int putu16_user(const struct vringh *vrh, __virtio16 *p, u16 val)
567 {
568         __virtio16 v = cpu_to_vringh16(vrh, val);
569         return put_user(v, (__force __virtio16 __user *)p);
570 }
571
572 static inline int copydesc_user(void *dst, const void *src, size_t len)
573 {
574         return copy_from_user(dst, (__force void __user *)src, len) ?
575                 -EFAULT : 0;
576 }
577
578 static inline int putused_user(struct vring_used_elem *dst,
579                                const struct vring_used_elem *src,
580                                unsigned int num)
581 {
582         return copy_to_user((__force void __user *)dst, src,
583                             sizeof(*dst) * num) ? -EFAULT : 0;
584 }
585
586 static inline int xfer_from_user(void *src, void *dst, size_t len)
587 {
588         return copy_from_user(dst, (__force void __user *)src, len) ?
589                 -EFAULT : 0;
590 }
591
592 static inline int xfer_to_user(void *dst, void *src, size_t len)
593 {
594         return copy_to_user((__force void __user *)dst, src, len) ?
595                 -EFAULT : 0;
596 }
597
598 /**
599  * vringh_init_user - initialize a vringh for a userspace vring.
600  * @vrh: the vringh to initialize.
601  * @features: the feature bits for this ring.
602  * @num: the number of elements.
603  * @weak_barriers: true if we only need memory barriers, not I/O.
604  * @desc: the userpace descriptor pointer.
605  * @avail: the userpace avail pointer.
606  * @used: the userpace used pointer.
607  *
608  * Returns an error if num is invalid: you should check pointers
609  * yourself!
610  */
611 int vringh_init_user(struct vringh *vrh, u64 features,
612                      unsigned int num, bool weak_barriers,
613                      struct vring_desc __user *desc,
614                      struct vring_avail __user *avail,
615                      struct vring_used __user *used)
616 {
617         /* Sane power of 2 please! */
618         if (!num || num > 0xffff || (num & (num - 1))) {
619                 vringh_bad("Bad ring size %u", num);
620                 return -EINVAL;
621         }
622
623         vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
624         vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
625         vrh->weak_barriers = weak_barriers;
626         vrh->completed = 0;
627         vrh->last_avail_idx = 0;
628         vrh->last_used_idx = 0;
629         vrh->vring.num = num;
630         /* vring expects kernel addresses, but only used via accessors. */
631         vrh->vring.desc = (__force struct vring_desc *)desc;
632         vrh->vring.avail = (__force struct vring_avail *)avail;
633         vrh->vring.used = (__force struct vring_used *)used;
634         return 0;
635 }
636 EXPORT_SYMBOL(vringh_init_user);
637
638 /**
639  * vringh_getdesc_user - get next available descriptor from userspace ring.
640  * @vrh: the userspace vring.
641  * @riov: where to put the readable descriptors (or NULL)
642  * @wiov: where to put the writable descriptors (or NULL)
643  * @getrange: function to call to check ranges.
644  * @head: head index we received, for passing to vringh_complete_user().
645  *
646  * Returns 0 if there was no descriptor, 1 if there was, or -errno.
647  *
648  * Note that on error return, you can tell the difference between an
649  * invalid ring and a single invalid descriptor: in the former case,
650  * *head will be vrh->vring.num.  You may be able to ignore an invalid
651  * descriptor, but there's not much you can do with an invalid ring.
652  *
653  * Note that you may need to clean up riov and wiov, even on error!
654  */
655 int vringh_getdesc_user(struct vringh *vrh,
656                         struct vringh_iov *riov,
657                         struct vringh_iov *wiov,
658                         bool (*getrange)(struct vringh *vrh,
659                                          u64 addr, struct vringh_range *r),
660                         u16 *head)
661 {
662         int err;
663
664         *head = vrh->vring.num;
665         err = __vringh_get_head(vrh, getu16_user, &vrh->last_avail_idx);
666         if (err < 0)
667                 return err;
668
669         /* Empty... */
670         if (err == vrh->vring.num)
671                 return 0;
672
673         /* We need the layouts to be the identical for this to work */
674         BUILD_BUG_ON(sizeof(struct vringh_kiov) != sizeof(struct vringh_iov));
675         BUILD_BUG_ON(offsetof(struct vringh_kiov, iov) !=
676                      offsetof(struct vringh_iov, iov));
677         BUILD_BUG_ON(offsetof(struct vringh_kiov, i) !=
678                      offsetof(struct vringh_iov, i));
679         BUILD_BUG_ON(offsetof(struct vringh_kiov, used) !=
680                      offsetof(struct vringh_iov, used));
681         BUILD_BUG_ON(offsetof(struct vringh_kiov, max_num) !=
682                      offsetof(struct vringh_iov, max_num));
683         BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
684         BUILD_BUG_ON(offsetof(struct iovec, iov_base) !=
685                      offsetof(struct kvec, iov_base));
686         BUILD_BUG_ON(offsetof(struct iovec, iov_len) !=
687                      offsetof(struct kvec, iov_len));
688         BUILD_BUG_ON(sizeof(((struct iovec *)NULL)->iov_base)
689                      != sizeof(((struct kvec *)NULL)->iov_base));
690         BUILD_BUG_ON(sizeof(((struct iovec *)NULL)->iov_len)
691                      != sizeof(((struct kvec *)NULL)->iov_len));
692
693         *head = err;
694         err = __vringh_iov(vrh, *head, (struct vringh_kiov *)riov,
695                            (struct vringh_kiov *)wiov,
696                            range_check, getrange, GFP_KERNEL, copydesc_user);
697         if (err)
698                 return err;
699
700         return 1;
701 }
702 EXPORT_SYMBOL(vringh_getdesc_user);
703
704 /**
705  * vringh_iov_pull_user - copy bytes from vring_iov.
706  * @riov: the riov as passed to vringh_getdesc_user() (updated as we consume)
707  * @dst: the place to copy.
708  * @len: the maximum length to copy.
709  *
710  * Returns the bytes copied <= len or a negative errno.
711  */
712 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len)
713 {
714         return vringh_iov_xfer((struct vringh_kiov *)riov,
715                                dst, len, xfer_from_user);
716 }
717 EXPORT_SYMBOL(vringh_iov_pull_user);
718
719 /**
720  * vringh_iov_push_user - copy bytes into vring_iov.
721  * @wiov: the wiov as passed to vringh_getdesc_user() (updated as we consume)
722  * @dst: the place to copy.
723  * @len: the maximum length to copy.
724  *
725  * Returns the bytes copied <= len or a negative errno.
726  */
727 ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
728                              const void *src, size_t len)
729 {
730         return vringh_iov_xfer((struct vringh_kiov *)wiov,
731                                (void *)src, len, xfer_to_user);
732 }
733 EXPORT_SYMBOL(vringh_iov_push_user);
734
735 /**
736  * vringh_abandon_user - we've decided not to handle the descriptor(s).
737  * @vrh: the vring.
738  * @num: the number of descriptors to put back (ie. num
739  *       vringh_get_user() to undo).
740  *
741  * The next vringh_get_user() will return the old descriptor(s) again.
742  */
743 void vringh_abandon_user(struct vringh *vrh, unsigned int num)
744 {
745         /* We only update vring_avail_event(vr) when we want to be notified,
746          * so we haven't changed that yet. */
747         vrh->last_avail_idx -= num;
748 }
749 EXPORT_SYMBOL(vringh_abandon_user);
750
751 /**
752  * vringh_complete_user - we've finished with descriptor, publish it.
753  * @vrh: the vring.
754  * @head: the head as filled in by vringh_getdesc_user.
755  * @len: the length of data we have written.
756  *
757  * You should check vringh_need_notify_user() after one or more calls
758  * to this function.
759  */
760 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len)
761 {
762         struct vring_used_elem used;
763
764         used.id = cpu_to_vringh32(vrh, head);
765         used.len = cpu_to_vringh32(vrh, len);
766         return __vringh_complete(vrh, &used, 1, putu16_user, putused_user);
767 }
768 EXPORT_SYMBOL(vringh_complete_user);
769
770 /**
771  * vringh_complete_multi_user - we've finished with many descriptors.
772  * @vrh: the vring.
773  * @used: the head, length pairs.
774  * @num_used: the number of used elements.
775  *
776  * You should check vringh_need_notify_user() after one or more calls
777  * to this function.
778  */
779 int vringh_complete_multi_user(struct vringh *vrh,
780                                const struct vring_used_elem used[],
781                                unsigned num_used)
782 {
783         return __vringh_complete(vrh, used, num_used,
784                                  putu16_user, putused_user);
785 }
786 EXPORT_SYMBOL(vringh_complete_multi_user);
787
788 /**
789  * vringh_notify_enable_user - we want to know if something changes.
790  * @vrh: the vring.
791  *
792  * This always enables notifications, but returns false if there are
793  * now more buffers available in the vring.
794  */
795 bool vringh_notify_enable_user(struct vringh *vrh)
796 {
797         return __vringh_notify_enable(vrh, getu16_user, putu16_user);
798 }
799 EXPORT_SYMBOL(vringh_notify_enable_user);
800
801 /**
802  * vringh_notify_disable_user - don't tell us if something changes.
803  * @vrh: the vring.
804  *
805  * This is our normal running state: we disable and then only enable when
806  * we're going to sleep.
807  */
808 void vringh_notify_disable_user(struct vringh *vrh)
809 {
810         __vringh_notify_disable(vrh, putu16_user);
811 }
812 EXPORT_SYMBOL(vringh_notify_disable_user);
813
814 /**
815  * vringh_need_notify_user - must we tell the other side about used buffers?
816  * @vrh: the vring we've called vringh_complete_user() on.
817  *
818  * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
819  */
820 int vringh_need_notify_user(struct vringh *vrh)
821 {
822         return __vringh_need_notify(vrh, getu16_user);
823 }
824 EXPORT_SYMBOL(vringh_need_notify_user);
825
826 /* Kernelspace access helpers. */
827 static inline int getu16_kern(const struct vringh *vrh,
828                               u16 *val, const __virtio16 *p)
829 {
830         *val = vringh16_to_cpu(vrh, ACCESS_ONCE(*p));
831         return 0;
832 }
833
834 static inline int putu16_kern(const struct vringh *vrh, __virtio16 *p, u16 val)
835 {
836         ACCESS_ONCE(*p) = cpu_to_vringh16(vrh, val);
837         return 0;
838 }
839
840 static inline int copydesc_kern(void *dst, const void *src, size_t len)
841 {
842         memcpy(dst, src, len);
843         return 0;
844 }
845
846 static inline int putused_kern(struct vring_used_elem *dst,
847                                const struct vring_used_elem *src,
848                                unsigned int num)
849 {
850         memcpy(dst, src, num * sizeof(*dst));
851         return 0;
852 }
853
854 static inline int xfer_kern(void *src, void *dst, size_t len)
855 {
856         memcpy(dst, src, len);
857         return 0;
858 }
859
860 /**
861  * vringh_init_kern - initialize a vringh for a kernelspace vring.
862  * @vrh: the vringh to initialize.
863  * @features: the feature bits for this ring.
864  * @num: the number of elements.
865  * @weak_barriers: true if we only need memory barriers, not I/O.
866  * @desc: the userpace descriptor pointer.
867  * @avail: the userpace avail pointer.
868  * @used: the userpace used pointer.
869  *
870  * Returns an error if num is invalid.
871  */
872 int vringh_init_kern(struct vringh *vrh, u64 features,
873                      unsigned int num, bool weak_barriers,
874                      struct vring_desc *desc,
875                      struct vring_avail *avail,
876                      struct vring_used *used)
877 {
878         /* Sane power of 2 please! */
879         if (!num || num > 0xffff || (num & (num - 1))) {
880                 vringh_bad("Bad ring size %u", num);
881                 return -EINVAL;
882         }
883
884         vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
885         vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
886         vrh->weak_barriers = weak_barriers;
887         vrh->completed = 0;
888         vrh->last_avail_idx = 0;
889         vrh->last_used_idx = 0;
890         vrh->vring.num = num;
891         vrh->vring.desc = desc;
892         vrh->vring.avail = avail;
893         vrh->vring.used = used;
894         return 0;
895 }
896 EXPORT_SYMBOL(vringh_init_kern);
897
898 /**
899  * vringh_getdesc_kern - get next available descriptor from kernelspace ring.
900  * @vrh: the kernelspace vring.
901  * @riov: where to put the readable descriptors (or NULL)
902  * @wiov: where to put the writable descriptors (or NULL)
903  * @head: head index we received, for passing to vringh_complete_kern().
904  * @gfp: flags for allocating larger riov/wiov.
905  *
906  * Returns 0 if there was no descriptor, 1 if there was, or -errno.
907  *
908  * Note that on error return, you can tell the difference between an
909  * invalid ring and a single invalid descriptor: in the former case,
910  * *head will be vrh->vring.num.  You may be able to ignore an invalid
911  * descriptor, but there's not much you can do with an invalid ring.
912  *
913  * Note that you may need to clean up riov and wiov, even on error!
914  */
915 int vringh_getdesc_kern(struct vringh *vrh,
916                         struct vringh_kiov *riov,
917                         struct vringh_kiov *wiov,
918                         u16 *head,
919                         gfp_t gfp)
920 {
921         int err;
922
923         err = __vringh_get_head(vrh, getu16_kern, &vrh->last_avail_idx);
924         if (err < 0)
925                 return err;
926
927         /* Empty... */
928         if (err == vrh->vring.num)
929                 return 0;
930
931         *head = err;
932         err = __vringh_iov(vrh, *head, riov, wiov, no_range_check, NULL,
933                            gfp, copydesc_kern);
934         if (err)
935                 return err;
936
937         return 1;
938 }
939 EXPORT_SYMBOL(vringh_getdesc_kern);
940
941 /**
942  * vringh_iov_pull_kern - copy bytes from vring_iov.
943  * @riov: the riov as passed to vringh_getdesc_kern() (updated as we consume)
944  * @dst: the place to copy.
945  * @len: the maximum length to copy.
946  *
947  * Returns the bytes copied <= len or a negative errno.
948  */
949 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len)
950 {
951         return vringh_iov_xfer(riov, dst, len, xfer_kern);
952 }
953 EXPORT_SYMBOL(vringh_iov_pull_kern);
954
955 /**
956  * vringh_iov_push_kern - copy bytes into vring_iov.
957  * @wiov: the wiov as passed to vringh_getdesc_kern() (updated as we consume)
958  * @dst: the place to copy.
959  * @len: the maximum length to copy.
960  *
961  * Returns the bytes copied <= len or a negative errno.
962  */
963 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
964                              const void *src, size_t len)
965 {
966         return vringh_iov_xfer(wiov, (void *)src, len, xfer_kern);
967 }
968 EXPORT_SYMBOL(vringh_iov_push_kern);
969
970 /**
971  * vringh_abandon_kern - we've decided not to handle the descriptor(s).
972  * @vrh: the vring.
973  * @num: the number of descriptors to put back (ie. num
974  *       vringh_get_kern() to undo).
975  *
976  * The next vringh_get_kern() will return the old descriptor(s) again.
977  */
978 void vringh_abandon_kern(struct vringh *vrh, unsigned int num)
979 {
980         /* We only update vring_avail_event(vr) when we want to be notified,
981          * so we haven't changed that yet. */
982         vrh->last_avail_idx -= num;
983 }
984 EXPORT_SYMBOL(vringh_abandon_kern);
985
986 /**
987  * vringh_complete_kern - we've finished with descriptor, publish it.
988  * @vrh: the vring.
989  * @head: the head as filled in by vringh_getdesc_kern.
990  * @len: the length of data we have written.
991  *
992  * You should check vringh_need_notify_kern() after one or more calls
993  * to this function.
994  */
995 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len)
996 {
997         struct vring_used_elem used;
998
999         used.id = cpu_to_vringh32(vrh, head);
1000         used.len = cpu_to_vringh32(vrh, len);
1001
1002         return __vringh_complete(vrh, &used, 1, putu16_kern, putused_kern);
1003 }
1004 EXPORT_SYMBOL(vringh_complete_kern);
1005
1006 /**
1007  * vringh_notify_enable_kern - we want to know if something changes.
1008  * @vrh: the vring.
1009  *
1010  * This always enables notifications, but returns false if there are
1011  * now more buffers available in the vring.
1012  */
1013 bool vringh_notify_enable_kern(struct vringh *vrh)
1014 {
1015         return __vringh_notify_enable(vrh, getu16_kern, putu16_kern);
1016 }
1017 EXPORT_SYMBOL(vringh_notify_enable_kern);
1018
1019 /**
1020  * vringh_notify_disable_kern - don't tell us if something changes.
1021  * @vrh: the vring.
1022  *
1023  * This is our normal running state: we disable and then only enable when
1024  * we're going to sleep.
1025  */
1026 void vringh_notify_disable_kern(struct vringh *vrh)
1027 {
1028         __vringh_notify_disable(vrh, putu16_kern);
1029 }
1030 EXPORT_SYMBOL(vringh_notify_disable_kern);
1031
1032 /**
1033  * vringh_need_notify_kern - must we tell the other side about used buffers?
1034  * @vrh: the vring we've called vringh_complete_kern() on.
1035  *
1036  * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
1037  */
1038 int vringh_need_notify_kern(struct vringh *vrh)
1039 {
1040         return __vringh_need_notify(vrh, getu16_kern);
1041 }
1042 EXPORT_SYMBOL(vringh_need_notify_kern);
1043
1044 MODULE_LICENSE("GPL");