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