This is an alternative to
vhost: force spec specified alignment on types
which is a bit safer as it does not change UAPI.
I still think it's best to change the UAPI header as well,
we can do that as a follow-up cleanup.
changes from v5:
ack for mellanox patch
fixup to remoteproc
changes from v4:
fixup to issues reported by kbuild
changes from v3:
tools/virtio fixes
a bunch more cleanups that now become possible
Changes from v2:
don't change struct name, instead add ifndef
so kernel does not see the legacy UAPI version.
Jason, can you pls ack one of the approaches?
Matej Genci (1):
virtio: add VIRTIO_RING_NO_LEGACY
Michael S. Tsirkin (11):
tools/virtio: define aligned attribute
tools/virtio: make asm/barrier.h self contained
tools/virtio: define __KERNEL__
virtio: stop using legacy struct vring in kernel
vhost: force spec specified alignment on types
virtio: add legacy init/size APIs
virtio_ring: switch to virtio_legacy_init/size
tools/virtio: switch to virtio_legacy_init/size
vop: switch to virtio_legacy_init/size
remoteproc: switch to virtio_legacy_init/size
mellanox: switch to virtio_legacy_init/size
drivers/block/virtio_blk.c | 1 +
drivers/misc/mic/vop/vop_main.c | 5 +--
drivers/misc/mic/vop/vop_vringh.c | 8 +++--
drivers/platform/mellanox/mlxbf-tmfifo.c | 6 ++--
drivers/remoteproc/remoteproc_core.c | 2 +-
drivers/remoteproc/remoteproc_virtio.c | 2 +-
drivers/vhost/vhost.h | 6 ++--
drivers/virtio/virtio_pci_modern.c | 1 +
drivers/virtio/virtio_ring.c | 15 ++++----
include/linux/virtio.h | 1 -
include/linux/virtio_ring.h | 46 ++++++++++++++++++++++++
include/linux/vringh.h | 1 +
include/uapi/linux/virtio_ring.h | 30 ++++++++++------
tools/virtio/Makefile | 2 +-
tools/virtio/asm/barrier.h | 1 +
tools/virtio/linux/compiler.h | 1 +
tools/virtio/ringtest/virtio_ring_0_9.c | 6 ++--
tools/virtio/virtio_test.c | 6 ++--
tools/virtio/vringh_test.c | 18 +++++-----
19 files changed, 112 insertions(+), 46 deletions(-)
--
MST
We are using abort() so let's include stdlib.h
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
tools/virtio/asm/barrier.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/tools/virtio/asm/barrier.h b/tools/virtio/asm/barrier.h
index d0351f83aebe..04d563fc9b95 100644
--- a/tools/virtio/asm/barrier.h
+++ b/tools/virtio/asm/barrier.h
@@ -1,4 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0 */
+#include <stdlib.h>
#if defined(__i386__) || defined(__x86_64__)
#define barrier() asm volatile("" ::: "memory")
#define virt_mb() __sync_synchronize()
--
MST
Even though we are building in userspace, we are emulating kernel bits.
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
tools/virtio/Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/virtio/Makefile b/tools/virtio/Makefile
index b587b9a7a124..ea6674307507 100644
--- a/tools/virtio/Makefile
+++ b/tools/virtio/Makefile
@@ -4,7 +4,7 @@ test: virtio_test vringh_test
virtio_test: virtio_ring.o virtio_test.o
vringh_test: vringh_test.o vringh.o virtio_ring.o
-CFLAGS += -g -O2 -Werror -Wall -I. -I../include/ -I ../../usr/include/ -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE -include ../../include/linux/kconfig.h
+CFLAGS += -g -O2 -Werror -Wall -I. -I../include/ -I ../../usr/include/ -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE -include ../../include/linux/kconfig.h -D__KERNEL__
vpath %.c ../../drivers/virtio ../../drivers/vhost
mod:
${MAKE} -C `pwd`/../.. M=`pwd`/vhost_test V=${V}
--
MST
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
tools/virtio/linux/compiler.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/tools/virtio/linux/compiler.h b/tools/virtio/linux/compiler.h
index 903dc9c4bd11..2c51bccb97bb 100644
--- a/tools/virtio/linux/compiler.h
+++ b/tools/virtio/linux/compiler.h
@@ -7,4 +7,5 @@
#define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
+#define __aligned(x) __attribute((__aligned__(x)))
#endif
--
MST
struct vring (in the uapi directory) and supporting APIs are kept
around to solely avoid breaking old userspace builds.
It's not actually part of the UAPI - it was kept in the UAPI
header by mistake, and using it in kernel isn't necessary
and prevents us from making changes safely.
In particular, the APIs actually assume the legacy layout.
Add an internal kernel-only struct vring and
switch everyone to use that.
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
drivers/block/virtio_blk.c | 1 +
include/linux/virtio.h | 1 -
include/linux/virtio_ring.h | 10 ++++++++++
include/linux/vringh.h | 1 +
include/uapi/linux/virtio_ring.h | 26 ++++++++++++++++----------
5 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 0736248999b0..dd5732dc4b07 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -15,6 +15,7 @@
#include <linux/blk-mq.h>
#include <linux/blk-mq-virtio.h>
#include <linux/numa.h>
+#include <uapi/linux/virtio_ring.h>
#define PART_BITS 4
#define VQ_NAME_LEN 16
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index 15f906e4a748..a493eac08393 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -9,7 +9,6 @@
#include <linux/device.h>
#include <linux/mod_devicetable.h>
#include <linux/gfp.h>
-#include <linux/vringh.h>
/**
* virtqueue - a queue to register buffers for sending or receiving.
diff --git a/include/linux/virtio_ring.h b/include/linux/virtio_ring.h
index 3dc70adfe5f5..11680e74761a 100644
--- a/include/linux/virtio_ring.h
+++ b/include/linux/virtio_ring.h
@@ -60,6 +60,16 @@ static inline void virtio_store_mb(bool weak_barriers,
struct virtio_device;
struct virtqueue;
+struct vring {
+ unsigned int num;
+
+ struct vring_desc *desc;
+
+ struct vring_avail *avail;
+
+ struct vring_used *used;
+};
+
/*
* Creates a virtqueue and allocates the descriptor ring. If
* may_reduce_num is set, then this may allocate a smaller ring than
diff --git a/include/linux/vringh.h b/include/linux/vringh.h
index 9e2763d7c159..d71b3710f58e 100644
--- a/include/linux/vringh.h
+++ b/include/linux/vringh.h
@@ -11,6 +11,7 @@
#ifndef _LINUX_VRINGH_H
#define _LINUX_VRINGH_H
#include <uapi/linux/virtio_ring.h>
+#include <linux/virtio_ring.h>
#include <linux/virtio_byteorder.h>
#include <linux/uio.h>
#include <linux/slab.h>
diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h
index 9223c3a5c46a..8961a4adda5c 100644
--- a/include/uapi/linux/virtio_ring.h
+++ b/include/uapi/linux/virtio_ring.h
@@ -118,16 +118,6 @@ struct vring_used {
struct vring_used_elem ring[];
};
-struct vring {
- unsigned int num;
-
- struct vring_desc *desc;
-
- struct vring_avail *avail;
-
- struct vring_used *used;
-};
-
/* Alignment requirements for vring elements.
* When using pre-virtio 1.0 layout, these fall out naturally.
*/
@@ -166,6 +156,21 @@ struct vring {
#define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
#define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num])
+#ifndef __KERNEL__
+/*
+ * The following definitions have been put in the UAPI header by mistake. We
+ * keep them around to avoid breaking old userspace builds.
+ */
+struct vring {
+ unsigned int num;
+
+ struct vring_desc *desc;
+
+ struct vring_avail *avail;
+
+ struct vring_used *used;
+};
+
static inline void vring_init(struct vring *vr, unsigned int num, void *p,
unsigned long align)
{
@@ -182,6 +187,7 @@ static inline unsigned vring_size(unsigned int num, unsigned long align)
+ align - 1) & ~(align - 1))
+ sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num;
}
+#endif
#endif /* VIRTIO_RING_NO_LEGACY */
--
MST
From: Matej Genci <[email protected]>
Add macro to disable legacy vring functions.
Signed-off-by: Matej Genci <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
drivers/virtio/virtio_pci_modern.c | 1 +
include/uapi/linux/virtio_ring.h | 4 ++++
2 files changed, 5 insertions(+)
diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
index 7abcc50838b8..db93cedd262f 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -16,6 +16,7 @@
#include <linux/delay.h>
#define VIRTIO_PCI_NO_LEGACY
+#define VIRTIO_RING_NO_LEGACY
#include "virtio_pci_common.h"
/*
diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h
index 559f42e73315..9223c3a5c46a 100644
--- a/include/uapi/linux/virtio_ring.h
+++ b/include/uapi/linux/virtio_ring.h
@@ -135,6 +135,8 @@ struct vring {
#define VRING_USED_ALIGN_SIZE 4
#define VRING_DESC_ALIGN_SIZE 16
+#ifndef VIRTIO_RING_NO_LEGACY
+
/* The standard layout for the ring is a continuous chunk of memory which looks
* like this. We assume num is a power of 2.
*
@@ -181,6 +183,8 @@ static inline unsigned vring_size(unsigned int num, unsigned long align)
+ sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num;
}
+#endif /* VIRTIO_RING_NO_LEGACY */
+
/* The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX */
/* Assuming a given event_idx value from the other side, if
* we have just incremented index from old to new_idx,
--
MST
These are used for legacy ring format, switch to APIs
that make this explicit.
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
drivers/virtio/virtio_ring.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 58b96baa8d48..2a2b12449bac 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -867,8 +867,10 @@ static struct virtqueue *vring_create_virtqueue_split(
}
/* TODO: allocate each queue chunk individually */
- for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
- queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
+ for (; num && vring_legacy_size(num, vring_align) > PAGE_SIZE;
+ num /= 2) {
+ queue = vring_alloc_queue(vdev,
+ vring_legacy_size(num, vring_align),
&dma_addr,
GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
if (queue)
@@ -882,14 +884,15 @@ static struct virtqueue *vring_create_virtqueue_split(
if (!queue) {
/* Try to get a single page. You are my only hope! */
- queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
+ queue = vring_alloc_queue(vdev,
+ vring_legacy_size(num, vring_align),
&dma_addr, GFP_KERNEL|__GFP_ZERO);
}
if (!queue)
return NULL;
- queue_size_in_bytes = vring_size(num, vring_align);
- vring_init(&vring, num, queue, vring_align);
+ queue_size_in_bytes = vring_legacy_size(num, vring_align);
+ vring_legacy_init(&vring, num, queue, vring_align);
vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
notify, callback, name);
@@ -2169,7 +2172,7 @@ struct virtqueue *vring_new_virtqueue(unsigned int index,
if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
return NULL;
- vring_init(&vring, num, pages, vring_align);
+ vring_legacy_init(&vring, num, pages, vring_align);
return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
notify, callback, name);
}
--
MST
The ring element addresses are passed between components with different
alignments assumptions. Thus, if guest/userspace selects a pointer and
host then gets and dereferences it, we might need to decrease the
compiler-selected alignment to prevent compiler on the host from
assuming pointer is aligned.
This actually triggers on ARM with -mabi=apcs-gnu - which is a
deprecated configuration, but it seems safer to handle this
generally.
I verified that the produced binary is exactly identical on x86.
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
drivers/vhost/vhost.h | 6 +++---
include/linux/virtio_ring.h | 24 +++++++++++++++++++++---
2 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index f8403bd46b85..60cab4c78229 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h
@@ -67,9 +67,9 @@ struct vhost_virtqueue {
/* The actual ring of buffers. */
struct mutex mutex;
unsigned int num;
- struct vring_desc __user *desc;
- struct vring_avail __user *avail;
- struct vring_used __user *used;
+ vring_desc_t __user *desc;
+ vring_avail_t __user *avail;
+ vring_used_t __user *used;
const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS];
struct file *kick;
struct eventfd_ctx *call_ctx;
diff --git a/include/linux/virtio_ring.h b/include/linux/virtio_ring.h
index 11680e74761a..c3f9ca054250 100644
--- a/include/linux/virtio_ring.h
+++ b/include/linux/virtio_ring.h
@@ -60,14 +60,32 @@ static inline void virtio_store_mb(bool weak_barriers,
struct virtio_device;
struct virtqueue;
+/*
+ * The ring element addresses are passed between components with different
+ * alignments assumptions. Thus, we might need to decrease the compiler-selected
+ * alignment, and so must use a typedef to make sure the __aligned attribute
+ * actually takes hold:
+ *
+ * https://gcc.gnu.org/onlinedocs//gcc/Common-Type-Attributes.html#Common-Type-Attributes
+ *
+ * When used on a struct, or struct member, the aligned attribute can only
+ * increase the alignment; in order to decrease it, the packed attribute must
+ * be specified as well. When used as part of a typedef, the aligned attribute
+ * can both increase and decrease alignment, and specifying the packed
+ * attribute generates a warning.
+ */
+typedef struct vring_desc __aligned(VRING_DESC_ALIGN_SIZE) vring_desc_t;
+typedef struct vring_avail __aligned(VRING_AVAIL_ALIGN_SIZE) vring_avail_t;
+typedef struct vring_used __aligned(VRING_USED_ALIGN_SIZE) vring_used_t;
+
struct vring {
unsigned int num;
- struct vring_desc *desc;
+ vring_desc_t *desc;
- struct vring_avail *avail;
+ vring_avail_t *avail;
- struct vring_used *used;
+ vring_used_t *used;
};
/*
--
MST
These are used for legacy ring format, switch to APIs that make this
explicit.
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
tools/virtio/ringtest/virtio_ring_0_9.c | 6 +++---
tools/virtio/virtio_test.c | 6 +++---
tools/virtio/vringh_test.c | 18 +++++++++---------
3 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/tools/virtio/ringtest/virtio_ring_0_9.c b/tools/virtio/ringtest/virtio_ring_0_9.c
index 13a035a390e9..e2ab6ac53966 100644
--- a/tools/virtio/ringtest/virtio_ring_0_9.c
+++ b/tools/virtio/ringtest/virtio_ring_0_9.c
@@ -67,13 +67,13 @@ void alloc_ring(void)
int i;
void *p;
- ret = posix_memalign(&p, 0x1000, vring_size(ring_size, 0x1000));
+ ret = posix_memalign(&p, 0x1000, vring_legacy_size(ring_size, 0x1000));
if (ret) {
perror("Unable to allocate ring buffer.\n");
exit(3);
}
- memset(p, 0, vring_size(ring_size, 0x1000));
- vring_init(&ring, ring_size, p, 0x1000);
+ memset(p, 0, vring_legacy_size(ring_size, 0x1000));
+ vring_legacy_init(&ring, ring_size, p, 0x1000);
guest.avail_idx = 0;
guest.kicked_avail_idx = -1;
diff --git a/tools/virtio/virtio_test.c b/tools/virtio/virtio_test.c
index 93d81cd64ba0..25be607d8711 100644
--- a/tools/virtio/virtio_test.c
+++ b/tools/virtio/virtio_test.c
@@ -102,10 +102,10 @@ static void vq_info_add(struct vdev_info *dev, int num)
info->idx = dev->nvqs;
info->kick = eventfd(0, EFD_NONBLOCK);
info->call = eventfd(0, EFD_NONBLOCK);
- r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
+ r = posix_memalign(&info->ring, 4096, vring_legacy_size(num, 4096));
assert(r >= 0);
- memset(info->ring, 0, vring_size(num, 4096));
- vring_init(&info->vring, num, info->ring, 4096);
+ memset(info->ring, 0, vring_legacy_size(num, 4096));
+ vring_legacy_init(&info->vring, num, info->ring, 4096);
info->vq = vring_new_virtqueue(info->idx,
info->vring.num, 4096, &dev->vdev,
true, false, info->ring,
diff --git a/tools/virtio/vringh_test.c b/tools/virtio/vringh_test.c
index 293653463303..8ee2c9a6ad46 100644
--- a/tools/virtio/vringh_test.c
+++ b/tools/virtio/vringh_test.c
@@ -151,7 +151,7 @@ static int parallel_test(u64 features,
err(1, "Opening /tmp/vringh_test-file");
/* Extra room at the end for some data, and indirects */
- mapsize = vring_size(RINGSIZE, ALIGN)
+ mapsize = vring_legacy_size(RINGSIZE, ALIGN)
+ RINGSIZE * 2 * sizeof(int)
+ RINGSIZE * 6 * sizeof(struct vring_desc);
mapsize = (mapsize + getpagesize() - 1) & ~(getpagesize() - 1);
@@ -185,7 +185,7 @@ static int parallel_test(u64 features,
close(to_guest[0]);
close(to_host[1]);
- vring_init(&vrh.vring, RINGSIZE, host_map, ALIGN);
+ vring_legacy_init(&vrh.vring, RINGSIZE, host_map, ALIGN);
vringh_init_user(&vrh, features, RINGSIZE, true,
vrh.vring.desc, vrh.vring.avail, vrh.vring.used);
CPU_SET(first_cpu, &cpu_set);
@@ -297,7 +297,7 @@ static int parallel_test(u64 features,
unsigned int finished = 0;
/* We pass sg[]s pointing into here, but we need RINGSIZE+1 */
- data = guest_map + vring_size(RINGSIZE, ALIGN);
+ data = guest_map + vring_legacy_size(RINGSIZE, ALIGN);
indirects = (void *)data + (RINGSIZE + 1) * 2 * sizeof(int);
/* We are the guest. */
@@ -478,7 +478,7 @@ int main(int argc, char *argv[])
if (posix_memalign(&__user_addr_min, PAGE_SIZE, USER_MEM) != 0)
abort();
__user_addr_max = __user_addr_min + USER_MEM;
- memset(__user_addr_min, 0, vring_size(RINGSIZE, ALIGN));
+ memset(__user_addr_min, 0, vring_legacy_size(RINGSIZE, ALIGN));
/* Set up guest side. */
vq = vring_new_virtqueue(0, RINGSIZE, ALIGN, &vdev, true, false,
@@ -487,7 +487,7 @@ int main(int argc, char *argv[])
"guest vq");
/* Set up host side. */
- vring_init(&vrh.vring, RINGSIZE, __user_addr_min, ALIGN);
+ vring_legacy_init(&vrh.vring, RINGSIZE, __user_addr_min, ALIGN);
vringh_init_user(&vrh, vdev.features, RINGSIZE, true,
vrh.vring.desc, vrh.vring.avail, vrh.vring.used);
@@ -506,7 +506,7 @@ int main(int argc, char *argv[])
sgs[1] = &guest_sg[1];
/* May allocate an indirect, so force it to allocate user addr */
- __kmalloc_fake = __user_addr_min + vring_size(RINGSIZE, ALIGN);
+ __kmalloc_fake = __user_addr_min + vring_legacy_size(RINGSIZE, ALIGN);
err = virtqueue_add_sgs(vq, sgs, 1, 1, &err, GFP_KERNEL);
if (err)
errx(1, "virtqueue_add_sgs: %i", err);
@@ -556,7 +556,7 @@ int main(int argc, char *argv[])
errx(1, "vringh_complete_user: %i", err);
/* Guest should see used token now. */
- __kfree_ignore_start = __user_addr_min + vring_size(RINGSIZE, ALIGN);
+ __kfree_ignore_start = __user_addr_min + vring_legacy_size(RINGSIZE, ALIGN);
__kfree_ignore_end = __kfree_ignore_start + 1;
ret = virtqueue_get_buf(vq, &i);
if (ret != &err)
@@ -575,7 +575,7 @@ int main(int argc, char *argv[])
((char *)__user_addr_max - USER_MEM/4)[i] = i;
/* This will allocate an indirect, so force it to allocate user addr */
- __kmalloc_fake = __user_addr_min + vring_size(RINGSIZE, ALIGN);
+ __kmalloc_fake = __user_addr_min + vring_legacy_size(RINGSIZE, ALIGN);
err = virtqueue_add_outbuf(vq, guest_sg, RINGSIZE, &err, GFP_KERNEL);
if (err)
errx(1, "virtqueue_add_outbuf (large): %i", err);
@@ -680,7 +680,7 @@ int main(int argc, char *argv[])
if (err)
errx(1, "virtqueue_add_outbuf (indirect): %i", err);
- vring_init(&vring, RINGSIZE, __user_addr_min, ALIGN);
+ vring_legacy_init(&vring, RINGSIZE, __user_addr_min, ALIGN);
/* They're used in order, but double-check... */
assert(vring.desc[0].addr == (unsigned long)d);
--
MST
These are used for legacy ring format, switch to APIs that make this
explicit.
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
maintainers, pls ack merging this through virtio tree due to dependency
on previous patches in the patchset.
drivers/misc/mic/vop/vop_main.c | 5 +++--
drivers/misc/mic/vop/vop_vringh.c | 8 +++++---
2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/drivers/misc/mic/vop/vop_main.c b/drivers/misc/mic/vop/vop_main.c
index 85942f6717c5..829b3b14b1d7 100644
--- a/drivers/misc/mic/vop/vop_main.c
+++ b/drivers/misc/mic/vop/vop_main.c
@@ -283,7 +283,7 @@ static struct virtqueue *vop_new_virtqueue(unsigned int index,
bool weak_barriers = false;
struct vring vring;
- vring_init(&vring, num, pages, MIC_VIRTIO_RING_ALIGN);
+ vring_legacy_init(&vring, num, pages, MIC_VIRTIO_RING_ALIGN);
vring.used = used;
return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
@@ -320,7 +320,8 @@ static struct virtqueue *vop_find_vq(struct virtio_device *dev,
/* First assign the vring's allocated in host memory */
vqconfig = _vop_vq_config(vdev->desc) + index;
memcpy_fromio(&config, vqconfig, sizeof(config));
- _vr_size = vring_size(le16_to_cpu(config.num), MIC_VIRTIO_RING_ALIGN);
+ _vr_size = vring_legacy_size(le16_to_cpu(config.num),
+ MIC_VIRTIO_RING_ALIGN);
vr_size = PAGE_ALIGN(_vr_size + sizeof(struct _mic_vring_info));
va = vpdev->hw_ops->remap(vpdev, le64_to_cpu(config.address), vr_size);
if (!va)
diff --git a/drivers/misc/mic/vop/vop_vringh.c b/drivers/misc/mic/vop/vop_vringh.c
index 30eac172f017..0535c02d637d 100644
--- a/drivers/misc/mic/vop/vop_vringh.c
+++ b/drivers/misc/mic/vop/vop_vringh.c
@@ -296,7 +296,8 @@ static int vop_virtio_add_device(struct vop_vdev *vdev,
num = le16_to_cpu(vqconfig[i].num);
mutex_init(&vvr->vr_mutex);
- vr_size = PAGE_ALIGN(vring_size(num, MIC_VIRTIO_RING_ALIGN) +
+ vr_size = PAGE_ALIGN(vring_legacy_size(num,
+ MIC_VIRTIO_RING_ALIGN) +
sizeof(struct _mic_vring_info));
vr->va = (void *)
__get_free_pages(GFP_KERNEL | __GFP_ZERO,
@@ -308,7 +309,8 @@ static int vop_virtio_add_device(struct vop_vdev *vdev,
goto err;
}
vr->len = vr_size;
- vr->info = vr->va + vring_size(num, MIC_VIRTIO_RING_ALIGN);
+ vr->info = vr->va + vring_legacy_size(num,
+ MIC_VIRTIO_RING_ALIGN);
vr->info->magic = cpu_to_le32(MIC_MAGIC + vdev->virtio_id + i);
vr_addr = dma_map_single(&vpdev->dev, vr->va, vr_size,
DMA_BIDIRECTIONAL);
@@ -321,7 +323,7 @@ static int vop_virtio_add_device(struct vop_vdev *vdev,
}
vqconfig[i].address = cpu_to_le64(vr_addr);
- vring_init(&vr->vr, num, vr->va, MIC_VIRTIO_RING_ALIGN);
+ vring_legacy_init(&vr->vr, num, vr->va, MIC_VIRTIO_RING_ALIGN);
ret = vringh_init_kern(&vvr->vrh,
*(u32 *)mic_vq_features(vdev->dd),
num, false, vr->vr.desc, vr->vr.avail,
--
MST
These are used for legacy ring format, switch to APIs that make this
explicit.
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
maintainers, pls ack merging this through virtio tree due to dependency
on previous patches in the patchset.
drivers/remoteproc/remoteproc_core.c | 2 +-
drivers/remoteproc/remoteproc_virtio.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/remoteproc/remoteproc_core.c b/drivers/remoteproc/remoteproc_core.c
index 097f33e4f1f3..c350a01e6c4e 100644
--- a/drivers/remoteproc/remoteproc_core.c
+++ b/drivers/remoteproc/remoteproc_core.c
@@ -322,7 +322,7 @@ int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
struct rproc_mem_entry *mem;
/* actual size of vring (in bytes) */
- size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
+ size = PAGE_ALIGN(vring_legacy_size(rvring->len, rvring->align));
rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
diff --git a/drivers/remoteproc/remoteproc_virtio.c b/drivers/remoteproc/remoteproc_virtio.c
index 8c07cb2ca8ba..35b02468197a 100644
--- a/drivers/remoteproc/remoteproc_virtio.c
+++ b/drivers/remoteproc/remoteproc_virtio.c
@@ -95,7 +95,7 @@ static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
len = rvring->len;
/* zero vring */
- size = vring_size(len, rvring->align);
+ size = vring_legacy_size(len, rvring->align);
memset(addr, 0, size);
dev_dbg(dev, "vring%d: va %pK qsz %d notifyid %d\n",
--
MST
These are used for legacy ring format, switch to APIs that make this
explicit.
Acked-by: Andy Shevchenko <[email protected]>
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
maintainers, pls ack merging this through virtio tree due to dependency
on previous patches in the patchset.
drivers/platform/mellanox/mlxbf-tmfifo.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/platform/mellanox/mlxbf-tmfifo.c b/drivers/platform/mellanox/mlxbf-tmfifo.c
index 5739a9669b29..f42b1fb713f1 100644
--- a/drivers/platform/mellanox/mlxbf-tmfifo.c
+++ b/drivers/platform/mellanox/mlxbf-tmfifo.c
@@ -214,7 +214,7 @@ static void mlxbf_tmfifo_free_vrings(struct mlxbf_tmfifo *fifo,
for (i = 0; i < ARRAY_SIZE(tm_vdev->vrings); i++) {
vring = &tm_vdev->vrings[i];
if (vring->va) {
- size = vring_size(vring->num, vring->align);
+ size = vring_legacy_size(vring->num, vring->align);
dma_free_coherent(tm_vdev->vdev.dev.parent, size,
vring->va, vring->dma);
vring->va = NULL;
@@ -245,7 +245,7 @@ static int mlxbf_tmfifo_alloc_vrings(struct mlxbf_tmfifo *fifo,
vring->vdev_id = tm_vdev->vdev.id.device;
dev = &tm_vdev->vdev.dev;
- size = vring_size(vring->num, vring->align);
+ size = vring_legacy_size(vring->num, vring->align);
va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
if (!va) {
mlxbf_tmfifo_free_vrings(fifo, tm_vdev);
@@ -935,7 +935,7 @@ static int mlxbf_tmfifo_virtio_find_vqs(struct virtio_device *vdev,
vring = &tm_vdev->vrings[i];
/* zero vring */
- size = vring_size(vring->num, vring->align);
+ size = vring_legacy_size(vring->num, vring->align);
memset(vring->va, 0, size);
vq = vring_new_virtqueue(i, vring->num, vring->align, vdev,
false, false, vring->va,
--
MST
vring_init/vring_size (in the uapi directory) are kept
around to solely avoid breaking old userspace builds.
It's not actually part of the UAPI - it was kept in the UAPI
header by mistake, and using it in kernel isn't necessary
and prevents us from making changes safely.
In particular, the APIs actually assume the legacy layout.
Add an internal kernel-only vring_legacy_init/vring_legacy_size.
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
include/linux/virtio_ring.h | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/include/linux/virtio_ring.h b/include/linux/virtio_ring.h
index c3f9ca054250..766d70bca492 100644
--- a/include/linux/virtio_ring.h
+++ b/include/linux/virtio_ring.h
@@ -140,4 +140,22 @@ void vring_del_virtqueue(struct virtqueue *vq);
void vring_transport_features(struct virtio_device *vdev);
irqreturn_t vring_interrupt(int irq, void *_vq);
+
+static inline void vring_legacy_init(struct vring *vr, unsigned int num, void *p,
+ unsigned long align)
+{
+ vr->num = num;
+ vr->desc = p;
+ vr->avail = (struct vring_avail *)((char *)p + num * sizeof(struct vring_desc));
+ vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16)
+ + align-1) & ~(align - 1));
+}
+
+static inline unsigned vring_legacy_size(unsigned int num, unsigned long align)
+{
+ return ((sizeof(struct vring_desc) * num + sizeof(__virtio16) * (3 + num)
+ + align - 1) & ~(align - 1))
+ + sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num;
+}
+
#endif /* _LINUX_VIRTIO_RING_H */
--
MST
On Mon, 06 Apr 2020 15:26:48 -0700, Michael S. Tsirkin wrote:
>
> These are used for legacy ring format, switch to APIs that make this
> explicit.
>
> Signed-off-by: Michael S. Tsirkin <[email protected]>
> ---
>
> maintainers, pls ack merging this through virtio tree due to dependency
> on previous patches in the patchset.
Acked-by: Ashutosh Dixit <[email protected]>
>
> drivers/misc/mic/vop/vop_main.c | 5 +++--
> drivers/misc/mic/vop/vop_vringh.c | 8 +++++---
> 2 files changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/misc/mic/vop/vop_main.c b/drivers/misc/mic/vop/vop_main.c
> index 85942f6717c5..829b3b14b1d7 100644
> --- a/drivers/misc/mic/vop/vop_main.c
> +++ b/drivers/misc/mic/vop/vop_main.c
> @@ -283,7 +283,7 @@ static struct virtqueue *vop_new_virtqueue(unsigned int index,
> bool weak_barriers = false;
> struct vring vring;
>
> - vring_init(&vring, num, pages, MIC_VIRTIO_RING_ALIGN);
> + vring_legacy_init(&vring, num, pages, MIC_VIRTIO_RING_ALIGN);
> vring.used = used;
>
> return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
> @@ -320,7 +320,8 @@ static struct virtqueue *vop_find_vq(struct virtio_device *dev,
> /* First assign the vring's allocated in host memory */
> vqconfig = _vop_vq_config(vdev->desc) + index;
> memcpy_fromio(&config, vqconfig, sizeof(config));
> - _vr_size = vring_size(le16_to_cpu(config.num), MIC_VIRTIO_RING_ALIGN);
> + _vr_size = vring_legacy_size(le16_to_cpu(config.num),
> + MIC_VIRTIO_RING_ALIGN);
> vr_size = PAGE_ALIGN(_vr_size + sizeof(struct _mic_vring_info));
> va = vpdev->hw_ops->remap(vpdev, le64_to_cpu(config.address), vr_size);
> if (!va)
> diff --git a/drivers/misc/mic/vop/vop_vringh.c b/drivers/misc/mic/vop/vop_vringh.c
> index 30eac172f017..0535c02d637d 100644
> --- a/drivers/misc/mic/vop/vop_vringh.c
> +++ b/drivers/misc/mic/vop/vop_vringh.c
> @@ -296,7 +296,8 @@ static int vop_virtio_add_device(struct vop_vdev *vdev,
>
> num = le16_to_cpu(vqconfig[i].num);
> mutex_init(&vvr->vr_mutex);
> - vr_size = PAGE_ALIGN(vring_size(num, MIC_VIRTIO_RING_ALIGN) +
> + vr_size = PAGE_ALIGN(vring_legacy_size(num,
> + MIC_VIRTIO_RING_ALIGN) +
> sizeof(struct _mic_vring_info));
> vr->va = (void *)
> __get_free_pages(GFP_KERNEL | __GFP_ZERO,
> @@ -308,7 +309,8 @@ static int vop_virtio_add_device(struct vop_vdev *vdev,
> goto err;
> }
> vr->len = vr_size;
> - vr->info = vr->va + vring_size(num, MIC_VIRTIO_RING_ALIGN);
> + vr->info = vr->va + vring_legacy_size(num,
> + MIC_VIRTIO_RING_ALIGN);
> vr->info->magic = cpu_to_le32(MIC_MAGIC + vdev->virtio_id + i);
> vr_addr = dma_map_single(&vpdev->dev, vr->va, vr_size,
> DMA_BIDIRECTIONAL);
> @@ -321,7 +323,7 @@ static int vop_virtio_add_device(struct vop_vdev *vdev,
> }
> vqconfig[i].address = cpu_to_le64(vr_addr);
>
> - vring_init(&vr->vr, num, vr->va, MIC_VIRTIO_RING_ALIGN);
> + vring_legacy_init(&vr->vr, num, vr->va, MIC_VIRTIO_RING_ALIGN);
> ret = vringh_init_kern(&vvr->vrh,
> *(u32 *)mic_vq_features(vdev->dd),
> num, false, vr->vr.desc, vr->vr.avail,
> --
> MST
>