Skip to content

Commit 64ec45b

Browse files
committed
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
Pull virtio fixes from Michael S Tsirkin: "virtio 1.0 related fixes Most importantly, this fixes using virtio_pci as a module. Further, the big virtio 1.0 conversion missed a couple of places. This fixes them up. This isn't 100% sparse-clean yet because on many architectures get_user triggers sparse warnings when used with __bitwise tag (when same tag is on both pointer and value read). I posted a patchset to fix it up by adding __force on all arches that don't already have it (many do), when that's merged these warnings will go away" * tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost: virtio_pci: restore module attributes mic/host: fix up virtio 1.0 APIs vringh: update for virtio 1.0 APIs vringh: 64 bit features tools/virtio: add virtio 1.0 in vringh_test tools/virtio: add virtio 1.0 in virtio_test tools/virtio: enable -Werror tools/virtio: 64 bit features tools/virtio: fix vringh test tools/virtio: more stubs virtio: core support for config generation virtio_pci: add VIRTIO_PCI_NO_LEGACY virtio_pci: move probe to common file virtio_pci_common.h: drop VIRTIO_PCI_NO_LEGACY virtio_config: fix virtio_cread_bytes virtio: set VIRTIO_CONFIG_S_FEATURES_OK on restore
2 parents c0f486f + 5ff1611 commit 64ec45b

16 files changed

Lines changed: 324 additions & 109 deletions

File tree

‎drivers/misc/mic/host/mic_debugfs.c‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,21 +326,27 @@ static int mic_vdev_info_show(struct seq_file *s, void *unused)
326326
}
327327
avail = vrh->vring.avail;
328328
seq_printf(s, "avail flags 0x%x idx %d\n",
329-
avail->flags, avail->idx & (num - 1));
329+
vringh16_to_cpu(vrh, avail->flags),
330+
vringh16_to_cpu(vrh, avail->idx) & (num - 1));
330331
seq_printf(s, "avail flags 0x%x idx %d\n",
331-
avail->flags, avail->idx);
332+
vringh16_to_cpu(vrh, avail->flags),
333+
vringh16_to_cpu(vrh, avail->idx));
332334
for (j = 0; j < num; j++)
333335
seq_printf(s, "avail ring[%d] %d\n",
334336
j, avail->ring[j]);
335337
used = vrh->vring.used;
336338
seq_printf(s, "used flags 0x%x idx %d\n",
337-
used->flags, used->idx & (num - 1));
339+
vringh16_to_cpu(vrh, used->flags),
340+
vringh16_to_cpu(vrh, used->idx) & (num - 1));
338341
seq_printf(s, "used flags 0x%x idx %d\n",
339-
used->flags, used->idx);
342+
vringh16_to_cpu(vrh, used->flags),
343+
vringh16_to_cpu(vrh, used->idx));
340344
for (j = 0; j < num; j++)
341345
seq_printf(s, "used ring[%d] id %d len %d\n",
342-
j, used->ring[j].id,
343-
used->ring[j].len);
346+
j, vringh32_to_cpu(vrh,
347+
used->ring[j].id),
348+
vringh32_to_cpu(vrh,
349+
used->ring[j].len));
344350
}
345351
}
346352
mutex_unlock(&mdev->mic_mutex);

‎drivers/vhost/vringh.c‎

Lines changed: 76 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/uaccess.h>
1212
#include <linux/slab.h>
1313
#include <linux/export.h>
14+
#include <uapi/linux/virtio_config.h>
1415

1516
static __printf(1,2) __cold void vringh_bad(const char *fmt, ...)
1617
{
@@ -28,13 +29,14 @@ static __printf(1,2) __cold void vringh_bad(const char *fmt, ...)
2829

2930
/* Returns vring->num if empty, -ve on error. */
3031
static inline int __vringh_get_head(const struct vringh *vrh,
31-
int (*getu16)(u16 *val, const u16 *p),
32+
int (*getu16)(const struct vringh *vrh,
33+
u16 *val, const __virtio16 *p),
3234
u16 *last_avail_idx)
3335
{
3436
u16 avail_idx, i, head;
3537
int err;
3638

37-
err = getu16(&avail_idx, &vrh->vring.avail->idx);
39+
err = getu16(vrh, &avail_idx, &vrh->vring.avail->idx);
3840
if (err) {
3941
vringh_bad("Failed to access avail idx at %p",
4042
&vrh->vring.avail->idx);
@@ -49,7 +51,7 @@ static inline int __vringh_get_head(const struct vringh *vrh,
4951

5052
i = *last_avail_idx & (vrh->vring.num - 1);
5153

52-
err = getu16(&head, &vrh->vring.avail->ring[i]);
54+
err = getu16(vrh, &head, &vrh->vring.avail->ring[i]);
5355
if (err) {
5456
vringh_bad("Failed to read head: idx %d address %p",
5557
*last_avail_idx, &vrh->vring.avail->ring[i]);
@@ -144,28 +146,32 @@ static inline bool no_range_check(struct vringh *vrh, u64 addr, size_t *len,
144146
}
145147

146148
/* No reason for this code to be inline. */
147-
static int move_to_indirect(int *up_next, u16 *i, void *addr,
149+
static int move_to_indirect(const struct vringh *vrh,
150+
int *up_next, u16 *i, void *addr,
148151
const struct vring_desc *desc,
149152
struct vring_desc **descs, int *desc_max)
150153
{
154+
u32 len;
155+
151156
/* Indirect tables can't have indirect. */
152157
if (*up_next != -1) {
153158
vringh_bad("Multilevel indirect %u->%u", *up_next, *i);
154159
return -EINVAL;
155160
}
156161

157-
if (unlikely(desc->len % sizeof(struct vring_desc))) {
162+
len = vringh32_to_cpu(vrh, desc->len);
163+
if (unlikely(len % sizeof(struct vring_desc))) {
158164
vringh_bad("Strange indirect len %u", desc->len);
159165
return -EINVAL;
160166
}
161167

162168
/* We will check this when we follow it! */
163-
if (desc->flags & VRING_DESC_F_NEXT)
164-
*up_next = desc->next;
169+
if (desc->flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT))
170+
*up_next = vringh16_to_cpu(vrh, desc->next);
165171
else
166172
*up_next = -2;
167173
*descs = addr;
168-
*desc_max = desc->len / sizeof(struct vring_desc);
174+
*desc_max = len / sizeof(struct vring_desc);
169175

170176
/* Now, start at the first indirect. */
171177
*i = 0;
@@ -287,22 +293,25 @@ __vringh_iov(struct vringh *vrh, u16 i,
287293
if (unlikely(err))
288294
goto fail;
289295

290-
if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
296+
if (unlikely(desc.flags &
297+
cpu_to_vringh16(vrh, VRING_DESC_F_INDIRECT))) {
298+
u64 a = vringh64_to_cpu(vrh, desc.addr);
299+
291300
/* Make sure it's OK, and get offset. */
292-
len = desc.len;
293-
if (!rcheck(vrh, desc.addr, &len, &range, getrange)) {
301+
len = vringh32_to_cpu(vrh, desc.len);
302+
if (!rcheck(vrh, a, &len, &range, getrange)) {
294303
err = -EINVAL;
295304
goto fail;
296305
}
297306

298-
if (unlikely(len != desc.len)) {
307+
if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
299308
slow = true;
300309
/* We need to save this range to use offset */
301310
slowrange = range;
302311
}
303312

304-
addr = (void *)(long)(desc.addr + range.offset);
305-
err = move_to_indirect(&up_next, &i, addr, &desc,
313+
addr = (void *)(long)(a + range.offset);
314+
err = move_to_indirect(vrh, &up_next, &i, addr, &desc,
306315
&descs, &desc_max);
307316
if (err)
308317
goto fail;
@@ -315,7 +324,7 @@ __vringh_iov(struct vringh *vrh, u16 i,
315324
goto fail;
316325
}
317326

318-
if (desc.flags & VRING_DESC_F_WRITE)
327+
if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_WRITE))
319328
iov = wiov;
320329
else {
321330
iov = riov;
@@ -336,12 +345,14 @@ __vringh_iov(struct vringh *vrh, u16 i,
336345

337346
again:
338347
/* Make sure it's OK, and get offset. */
339-
len = desc.len;
340-
if (!rcheck(vrh, desc.addr, &len, &range, getrange)) {
348+
len = vringh32_to_cpu(vrh, desc.len);
349+
if (!rcheck(vrh, vringh64_to_cpu(vrh, desc.addr), &len, &range,
350+
getrange)) {
341351
err = -EINVAL;
342352
goto fail;
343353
}
344-
addr = (void *)(unsigned long)(desc.addr + range.offset);
354+
addr = (void *)(unsigned long)(vringh64_to_cpu(vrh, desc.addr) +
355+
range.offset);
345356

346357
if (unlikely(iov->used == (iov->max_num & ~VRINGH_IOV_ALLOCATED))) {
347358
err = resize_iovec(iov, gfp);
@@ -353,14 +364,16 @@ __vringh_iov(struct vringh *vrh, u16 i,
353364
iov->iov[iov->used].iov_len = len;
354365
iov->used++;
355366

356-
if (unlikely(len != desc.len)) {
357-
desc.len -= len;
358-
desc.addr += len;
367+
if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
368+
desc.len = cpu_to_vringh32(vrh,
369+
vringh32_to_cpu(vrh, desc.len) - len);
370+
desc.addr = cpu_to_vringh64(vrh,
371+
vringh64_to_cpu(vrh, desc.addr) + len);
359372
goto again;
360373
}
361374

362-
if (desc.flags & VRING_DESC_F_NEXT) {
363-
i = desc.next;
375+
if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT)) {
376+
i = vringh16_to_cpu(vrh, desc.next);
364377
} else {
365378
/* Just in case we need to finish traversing above. */
366379
if (unlikely(up_next > 0)) {
@@ -387,7 +400,8 @@ __vringh_iov(struct vringh *vrh, u16 i,
387400
static inline int __vringh_complete(struct vringh *vrh,
388401
const struct vring_used_elem *used,
389402
unsigned int num_used,
390-
int (*putu16)(u16 *p, u16 val),
403+
int (*putu16)(const struct vringh *vrh,
404+
__virtio16 *p, u16 val),
391405
int (*putused)(struct vring_used_elem *dst,
392406
const struct vring_used_elem
393407
*src, unsigned num))
@@ -420,7 +434,7 @@ static inline int __vringh_complete(struct vringh *vrh,
420434
/* Make sure buffer is written before we update index. */
421435
virtio_wmb(vrh->weak_barriers);
422436

423-
err = putu16(&vrh->vring.used->idx, used_idx + num_used);
437+
err = putu16(vrh, &vrh->vring.used->idx, used_idx + num_used);
424438
if (err) {
425439
vringh_bad("Failed to update used index at %p",
426440
&vrh->vring.used->idx);
@@ -433,7 +447,9 @@ static inline int __vringh_complete(struct vringh *vrh,
433447

434448

435449
static inline int __vringh_need_notify(struct vringh *vrh,
436-
int (*getu16)(u16 *val, const u16 *p))
450+
int (*getu16)(const struct vringh *vrh,
451+
u16 *val,
452+
const __virtio16 *p))
437453
{
438454
bool notify;
439455
u16 used_event;
@@ -447,7 +463,7 @@ static inline int __vringh_need_notify(struct vringh *vrh,
447463
/* Old-style, without event indices. */
448464
if (!vrh->event_indices) {
449465
u16 flags;
450-
err = getu16(&flags, &vrh->vring.avail->flags);
466+
err = getu16(vrh, &flags, &vrh->vring.avail->flags);
451467
if (err) {
452468
vringh_bad("Failed to get flags at %p",
453469
&vrh->vring.avail->flags);
@@ -457,7 +473,7 @@ static inline int __vringh_need_notify(struct vringh *vrh,
457473
}
458474

459475
/* Modern: we know when other side wants to know. */
460-
err = getu16(&used_event, &vring_used_event(&vrh->vring));
476+
err = getu16(vrh, &used_event, &vring_used_event(&vrh->vring));
461477
if (err) {
462478
vringh_bad("Failed to get used event idx at %p",
463479
&vring_used_event(&vrh->vring));
@@ -478,20 +494,22 @@ static inline int __vringh_need_notify(struct vringh *vrh,
478494
}
479495

480496
static inline bool __vringh_notify_enable(struct vringh *vrh,
481-
int (*getu16)(u16 *val, const u16 *p),
482-
int (*putu16)(u16 *p, u16 val))
497+
int (*getu16)(const struct vringh *vrh,
498+
u16 *val, const __virtio16 *p),
499+
int (*putu16)(const struct vringh *vrh,
500+
__virtio16 *p, u16 val))
483501
{
484502
u16 avail;
485503

486504
if (!vrh->event_indices) {
487505
/* Old-school; update flags. */
488-
if (putu16(&vrh->vring.used->flags, 0) != 0) {
506+
if (putu16(vrh, &vrh->vring.used->flags, 0) != 0) {
489507
vringh_bad("Clearing used flags %p",
490508
&vrh->vring.used->flags);
491509
return true;
492510
}
493511
} else {
494-
if (putu16(&vring_avail_event(&vrh->vring),
512+
if (putu16(vrh, &vring_avail_event(&vrh->vring),
495513
vrh->last_avail_idx) != 0) {
496514
vringh_bad("Updating avail event index %p",
497515
&vring_avail_event(&vrh->vring));
@@ -503,7 +521,7 @@ static inline bool __vringh_notify_enable(struct vringh *vrh,
503521
* sure it's written, then check again. */
504522
virtio_mb(vrh->weak_barriers);
505523

506-
if (getu16(&avail, &vrh->vring.avail->idx) != 0) {
524+
if (getu16(vrh, &avail, &vrh->vring.avail->idx) != 0) {
507525
vringh_bad("Failed to check avail idx at %p",
508526
&vrh->vring.avail->idx);
509527
return true;
@@ -516,26 +534,32 @@ static inline bool __vringh_notify_enable(struct vringh *vrh,
516534
}
517535

518536
static inline void __vringh_notify_disable(struct vringh *vrh,
519-
int (*putu16)(u16 *p, u16 val))
537+
int (*putu16)(const struct vringh *vrh,
538+
__virtio16 *p, u16 val))
520539
{
521540
if (!vrh->event_indices) {
522541
/* Old-school; update flags. */
523-
if (putu16(&vrh->vring.used->flags, VRING_USED_F_NO_NOTIFY)) {
542+
if (putu16(vrh, &vrh->vring.used->flags,
543+
VRING_USED_F_NO_NOTIFY)) {
524544
vringh_bad("Setting used flags %p",
525545
&vrh->vring.used->flags);
526546
}
527547
}
528548
}
529549

530550
/* Userspace access helpers: in this case, addresses are really userspace. */
531-
static inline int getu16_user(u16 *val, const u16 *p)
551+
static inline int getu16_user(const struct vringh *vrh, u16 *val, const __virtio16 *p)
532552
{
533-
return get_user(*val, (__force u16 __user *)p);
553+
__virtio16 v = 0;
554+
int rc = get_user(v, (__force __virtio16 __user *)p);
555+
*val = vringh16_to_cpu(vrh, v);
556+
return rc;
534557
}
535558

536-
static inline int putu16_user(u16 *p, u16 val)
559+
static inline int putu16_user(const struct vringh *vrh, __virtio16 *p, u16 val)
537560
{
538-
return put_user(val, (__force u16 __user *)p);
561+
__virtio16 v = cpu_to_vringh16(vrh, val);
562+
return put_user(v, (__force __virtio16 __user *)p);
539563
}
540564

541565
static inline int copydesc_user(void *dst, const void *src, size_t len)
@@ -577,7 +601,7 @@ static inline int xfer_to_user(void *dst, void *src, size_t len)
577601
* Returns an error if num is invalid: you should check pointers
578602
* yourself!
579603
*/
580-
int vringh_init_user(struct vringh *vrh, u32 features,
604+
int vringh_init_user(struct vringh *vrh, u64 features,
581605
unsigned int num, bool weak_barriers,
582606
struct vring_desc __user *desc,
583607
struct vring_avail __user *avail,
@@ -589,6 +613,7 @@ int vringh_init_user(struct vringh *vrh, u32 features,
589613
return -EINVAL;
590614
}
591615

616+
vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
592617
vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
593618
vrh->weak_barriers = weak_barriers;
594619
vrh->completed = 0;
@@ -729,8 +754,8 @@ int vringh_complete_user(struct vringh *vrh, u16 head, u32 len)
729754
{
730755
struct vring_used_elem used;
731756

732-
used.id = head;
733-
used.len = len;
757+
used.id = cpu_to_vringh32(vrh, head);
758+
used.len = cpu_to_vringh32(vrh, len);
734759
return __vringh_complete(vrh, &used, 1, putu16_user, putused_user);
735760
}
736761
EXPORT_SYMBOL(vringh_complete_user);
@@ -792,15 +817,16 @@ int vringh_need_notify_user(struct vringh *vrh)
792817
EXPORT_SYMBOL(vringh_need_notify_user);
793818

794819
/* Kernelspace access helpers. */
795-
static inline int getu16_kern(u16 *val, const u16 *p)
820+
static inline int getu16_kern(const struct vringh *vrh,
821+
u16 *val, const __virtio16 *p)
796822
{
797-
*val = ACCESS_ONCE(*p);
823+
*val = vringh16_to_cpu(vrh, ACCESS_ONCE(*p));
798824
return 0;
799825
}
800826

801-
static inline int putu16_kern(u16 *p, u16 val)
827+
static inline int putu16_kern(const struct vringh *vrh, __virtio16 *p, u16 val)
802828
{
803-
ACCESS_ONCE(*p) = val;
829+
ACCESS_ONCE(*p) = cpu_to_vringh16(vrh, val);
804830
return 0;
805831
}
806832

@@ -836,7 +862,7 @@ static inline int xfer_kern(void *src, void *dst, size_t len)
836862
*
837863
* Returns an error if num is invalid.
838864
*/
839-
int vringh_init_kern(struct vringh *vrh, u32 features,
865+
int vringh_init_kern(struct vringh *vrh, u64 features,
840866
unsigned int num, bool weak_barriers,
841867
struct vring_desc *desc,
842868
struct vring_avail *avail,
@@ -848,6 +874,7 @@ int vringh_init_kern(struct vringh *vrh, u32 features,
848874
return -EINVAL;
849875
}
850876

877+
vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
851878
vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
852879
vrh->weak_barriers = weak_barriers;
853880
vrh->completed = 0;
@@ -962,8 +989,8 @@ int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len)
962989
{
963990
struct vring_used_elem used;
964991

965-
used.id = head;
966-
used.len = len;
992+
used.id = cpu_to_vringh32(vrh, head);
993+
used.len = cpu_to_vringh32(vrh, len);
967994

968995
return __vringh_complete(vrh, &used, 1, putu16_kern, putused_kern);
969996
}

0 commit comments

Comments
 (0)