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