virtio: Introduce vringh wrappers in virtio_config
Subsystems (2)
PATCH 1/1 virtio: Introduce vringh wrappers in virtio_config
From: Sjur Brændeland <[email protected]>
Add wrappers for the host vrings to support loose coupling between the virtio device and driver.
The functions find_vrhs() and del_vrhs() are added to struct virtio_config_ops to manage the host vrings. The function vringh_notify() is added so the guest can be kicked when buffers are added to the used-ring.
This enables the virtio drivers to manage the virtio rings without knowledge of how the host vrings are managed.
--- include/linux/virtio_config.h | 13 +++++++++++++ include/linux/vringh.h | 13 +++++++++++++ 2 files changed, 26 insertions(+), 0 deletions(-)
include/linux/virtio_config.h +13 -0
@@ -51,7 +51,17 @@* This returns a pointer to the bus name a la pci_name from which * the caller can then copy. * @set_vq_affinity: set the affinity for a virtqueue. +* @find_vrhs: find the host vrings and instantiate them +* vdev: the virtio_device +* nhvrs: the number of host vrings to find +* hvrs: on success, includes new host vrings +* callbacks: array of driver callbacks, for each host vring +* include a NULL entry for vqs that do not need a callback +* Returns 0 on success or error status +* @del_vrhs: free the host vrings found by find_vrhs(). */ +struct vringh ; +typedef void vrh_callback_t (struct virtio_device *,struct vringh *);typedef void vq_callback_t (struct virtqueue *);struct virtio_config_ops { void (*get)(struct virtio_device *vdev, unsigned offset,
@@ -70,6 +80,9 @@ struct virtio_config_ops {void (*finalize_features)(struct virtio_device *vdev);const char *(*bus_name)(struct virtio_device *vdev);int (*set_vq_affinity)(struct virtqueue *vq,int cpu); +int (*find_vrhs)(struct virtio_device *vdev,unsigned nhvrs, +struct vringh *vrhs[],vrh_callback_t *callbacks[]); +void (*del_vrhs)(struct virtio_device *vdev); };/* If driver didn't advertise the feature, it will never appear. */
include/linux/vringh.h +13 -0
@@ -50,6 +50,12 @@ struct vringh {/* The vring (note: it may contain user pointers!) */ struct vring vring; + +/* The function to call when buffers are available */ +void (*notify)(struct vringh *); + +/* A pointer for the vringh clients to use. */ +void *priv; };/* The memory the vring can access, and what offset to apply. */
@@ -182,4 +188,11 @@ void vringh_notify_disable_kern(struct vringh *vrh);int vringh_need_notify_kern (struct vringh *vrh); +/* Notify the guest about buffers added to the used ring */ +static inline void vringh_notify (struct vringh *vrh) +{ +if (vrh->notify) + vrh->notify (vrh); +} +#endif /* _LINUX_VRINGH_H */
Signature · 6 lines
1.7.5.4 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 5 lines · [email protected] writes:
From: Sjur Brændeland <[email protected]>
Add wrappers for the host vrings to support loose coupling between the virtio device and driver.
Quoted 7 lines
The functions
find_vrhs()anddel_vrhs()are added to structvirtio_config_opsto manage the host vrings. The functionvringh_notify()is added so the guest can be kicked when buffers are added to the used-ring.This enables the virtio drivers to manage the virtio rings without knowledge of how the host vrings are managed.
Hmm, this is a bit weird.
Quoted 23 lines
include/linux/virtio_config.h +10
-0@@ -51,7 +51,17 @@* This returns a pointer to the bus name a la pci_name from which * the caller can then copy. * @set_vq_affinity: set the affinity for a virtqueue. +* @find_vrhs: find the host vrings and instantiate them +* vdev: the virtio_device +* nhvrs: the number of host vrings to find +* hvrs: on success, includes new host vrings +* callbacks: array of driver callbacks, for each host vring +* include a NULL entry for vqs that do not need a callback +* Returns 0 on success or error status +* @del_vrhs: free the host vrings found by find_vrhs(). */ +struct vringh ; +typedef void vrh_callback_t (struct virtio_device *,struct vringh *);typedef void vq_callback_t (struct virtqueue *);struct virtio_config_ops { void (*get)(struct virtio_device *vdev, unsigned offset,
include/linux/virtio_config.h struct virtio_config_ops { · line 88
Quoted 9 lines
include/linux/virtio_config.h +3
-0@@ -70,6 +80,9 @@ struct virtio_config_ops {void (*finalize_features)(struct virtio_device *vdev);const char *(*bus_name)(struct virtio_device *vdev);int (*set_vq_affinity)(struct virtqueue *vq,int cpu); +int (*find_vrhs)(struct virtio_device *vdev,unsigned nhvrs, +struct vringh *vrhs[],vrh_callback_t *callbacks[]); +void (*del_vrhs)(struct virtio_device *vdev); };/* If driver didn't advertise the feature, it will never appear. */
It's weird that you conflate the host and guest ring sides in rpmsg, but that might make sense if they're really bound together. However, in general they are not: it's normal to be a guest or host, not both.
This implies that you need a struct vringh_config, to put this in.
Quoted 5 lines
include/linux/vringh.h +0
-0
include/linux/vringh.h struct vringh { · line 59
Quoted 10 lines
include/linux/vringh.h +6
-0@@ -50,6 +50,12 @@ struct vringh {/* The vring (note: it may contain user pointers!) */ struct vring vring; + +/* The function to call when buffers are available */ +void (*notify)(struct vringh *); + +/* A pointer for the vringh clients to use. */ +void *priv; };
Since the caller allocates the vringh, can it not use container_of() instead of a priv pointer?
Thanks,
Rusty.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
On Wed, Mar 6, 2013 at 5:42 AM, Rusty Russell <[email protected]> wrote:
...
Quoted 9 lines
Quoted 9 lines
include/linux/virtio_config.h +3
-0@@ -70,6 +80,9 @@ struct virtio_config_ops {void (*finalize_features)(struct virtio_device *vdev);const char *(*bus_name)(struct virtio_device *vdev);int (*set_vq_affinity)(struct virtqueue *vq,int cpu); +int (*find_vrhs)(struct virtio_device *vdev,unsigned nhvrs, +struct vringh *vrhs[],vrh_callback_t *callbacks[]); +void (*del_vrhs)(struct virtio_device *vdev); };
Quoted 4 lines
include/linux/virtio_config.h struct virtio_config_ops { · line 88
Quoted 1 line
include/linux/virtio_config.h +0
-0/* If driver didn't advertise the feature, it will never appear. */ It's weird that you conflate the host and guest ring sides in rpmsg, but that might make sense if they're really bound together.
The caif_virtio driver is using both host and guest ring sides, host side rings in RX direction and guest side rings in TX direction. The reason behind this is to enable zero-copy on the producer sides. (For details see recent discussion with Ohad "Wrappers for vringh" or the initial blurb when first introducing caif virtio:
https://lkml.org/lkml/2012/10/31/677)
Quoted 4 lines
... However, in
general they are not: it's normal to be a guest or host, not both.This implies that you need a struct
vringh_config, to put this in.
OK, should we move the definition of struct vringh_config into vringh.h then, and add a vringh_config field in struct virtio_device?
Quoted 6 lines
Quoted 6 lines
include/linux/vringh.h +0
-0@@ -50,6 +50,12 @@ struct vringh {
Quoted 12 lines
include/linux/vringh.h struct vringh { · line 59
Quoted 9 lines
include/linux/vringh.h +6
-0/* The vring (note: it may contain user pointers!) */ struct vring vring; + +/* The function to call when buffers are available */ +void (*notify)(struct vringh *); + +/* A pointer for the vringh clients to use. */ +void *priv; };Since the caller allocates the vringh, can it not use
container_of()instead of a priv pointer?
Sure, no problem. I just need to add a new struct in remoteproc containing both the vringh and a pointer to the internal ring data.
Regards,
Sjur
Quoted 4 lines · Sjur Brændeland
The
caif_virtiodriver is using both host and guest ring sides, host side rings in RX direction and guest side rings in TX direction. The reason behind this is to enable zero-copy on the producer sides. (For details see recent discussion with Ohad "Wrappers for vringh"
FWIW, I'm not convinced that host side rings are necessary for zero-copy - you could always just pass pointers to your huge data inside buffers posted by guest side rings.
That said, I do believe that mixing guest side and host side rings may simplify the overall solution in some cases, especially in more complex multi core scenarios (where several cores all communicate with each other).
Quoted 9 lines · Ohad Ben-Cohen
Quoted 4 lines · Sjur Brændeland
The
caif_virtiodriver is using both host and guest ring sides, host side rings in RX direction and guest side rings in TX direction. The reason behind this is to enable zero-copy on the producer sides. (For details see recent discussion with Ohad "Wrappers for vringh"FWIW, I'm not convinced that host side rings are necessary for zero-copy - you could always just pass pointers to your huge data inside buffers posted by guest side rings.
Well, to do that for CAIF you would have to integrate virtio buffer handling deep into the lower layer of the modems 3GPP stack so that the 3GPP stack could take buffers from the available ring.
From the outside point of view, perhaps this sounds doable, but in reality you wouldn't get any Network Signalling engineers implementing the 3GPP stack to buy into that.
Another issue is that our modem also support other interface technologies such as USB and HSI. It simply doesn't make sense to enforce virtio rings internally if virtio isn't otherwise used on the modem.
Without host side rings we simply wouldn't be able to implement zero-copy in the RX direction for the STE-modem.
Regards,
Sjur
Quoted 5 lines
That said, I do believe that mixing guest side and host side rings may simplify the overall solution in some cases, especially in more complex multi core scenarios (where several cores all communicate with each other).
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 10 lines · Sjur Brændeland
Quoted 9 lines · Ohad Ben-Cohen
Quoted 4 lines · Sjur Brændeland
The
caif_virtiodriver is using both host and guest ring sides, host side rings in RX direction and guest side rings in TX direction. The reason behind this is to enable zero-copy on the producer sides. (For details see recent discussion with Ohad "Wrappers for vringh"FWIW, I'm not convinced that host side rings are necessary for zero-copy - you could always just pass pointers to your huge data inside buffers posted by guest side rings.
When I read this again I realized that I missed your point regarding passing pointers inside buffers completely. My rambling below argued against using the buffers provided in the available ring as a replacement of the modem internal allocator. Sorry for the confusion.
I haven't really spent time thinking through the implication of the alternative of passing buffer pointers inside buffers posted by guest side rings. But it seems to me that there would be some issues related to returning consumed buffers back if you use the rings in this way.
Thanks,
Sjur
Quoted 12 lines
Well, to do that for CAIF you would have to integrate virtio buffer handling deep into the lower layer of the modems 3GPP stack so that the 3GPP stack could take buffers from the available ring.
From the outside point of view, perhaps this sounds doable, but in reality you wouldn't get any Network Signalling engineers implementing the 3GPP stack to buy into that.
Another issue is that our modem also support other interface technologies such as USB and HSI. It simply doesn't make sense to enforce virtio rings internally if virtio isn't otherwise used on the modem.
Quoted 11 lines
Without host side rings we simply wouldn't be able to implement zero-copy in the RX direction for the STE-modem.
Regards,
SjurQuoted 5 lines
That said, I do believe that mixing guest side and host side rings may simplify the overall solution in some cases, especially in more complex multi core scenarios (where several cores all communicate with each other).
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
[FYI] vringh fixes.
In reply to [email protected]Hi all,
More benchmarking using vringh_test, and I finally figured out that my changes to virtio_ring had no effect because we were actually bottlenecked on the vringh side...
Here are the changes I've ended up with, for your reading pleasure. They are all in my pending-rebases git tree, and I'll fold them together before putting them into virtio-next (I'm waiting for Sjur's notify changes).
Cheers,
Rusty.
PATCH 1/2 [PATCH 1/2]
tools/virtio: separate headers more.
This makes them a bit more like the kernel headers, so we can include more real kernel headers in our tests.
In addition this means that we don't break tools/virtio with the next patch.
(Changes since last version: use ACCESS_ONCE() for "userspace" reads.
This semantic is assumed in kernel userspace access, since otherwise the compiler might decide to re-grab a value after verification, introducing a security hole).
tools/virtio/asm/barrier.h new file +14 -0
@@ -0,0 +1,14 @@ +#if defined(__i386__) || defined(__x86_64__) +#define barrier () asm volatile("" ::: "memory") +#define mb () __sync_synchronize() + +#define smp_mb () mb() +# define smp_rmb () barrier() +# define smp_wmb () barrier() +/* Weak barriers should be used. If not - it's a bug */ +# define rmb () abort() +# define wmb () abort() +#else +#error Please fill in barrier macros +#endif +
tools/virtio/linux/bug.h new file +10 -0
@@ -0,0 +1,10 @@ +#ifndef BUG_H +#define BUG_H + +#define BUG_ON (__BUG_ON_cond) assert(!(__BUG_ON_cond)) + +#define BUILD_BUG_ON (x) + +#define BUG () abort() + +#endif /* BUG_H */
tools/virtio/linux/err.h new file +26 -0
@@ -0,0 +1,26 @@ +#ifndef ERR_H +#define ERR_H +#define MAX_ERRNO 4095 + +#define IS_ERR_VALUE (x) unlikely((x) >= (unsigned long)-MAX_ERRNO) + +static inline void *__must_check ERR_PTR (long error) +{ +return (void *) error; +} + +static inline long __must_check PTR_ERR (const void *ptr) +{ +return (long ) ptr; +} + +static inline long __must_check IS_ERR (const void *ptr) +{ +return IS_ERR_VALUE ((unsigned long )ptr); +} + +static inline long __must_check IS_ERR_OR_NULL (const void *ptr) +{ +return !ptr ||IS_ERR_VALUE ((unsigned long )ptr); +} +#endif /* ERR_H */
tools/virtio/linux/export.h new file +5 -0
@@ -0,0 +1,5 @@ +#define EXPORT_SYMBOL (sym) +#define EXPORT_SYMBOL_GPL (sym) +#define EXPORT_SYMBOL_GPL_FUTURE (sym) +#define EXPORT_UNUSED_SYMBOL (sym) +#define EXPORT_UNUSED_SYMBOL_GPL (sym)
tools/virtio/linux/irqreturn.h new file +1 -0
@@ -0,0 +1 @@ +#include "../../../include/linux/irqreturn.h"
tools/virtio/linux/kernel.h new file +112 -0
112 lines not on this page · raw message
tools/virtio/linux/module.h +1 -0
@@ -0,0 +1 @@ +#include <linux/export.h>
tools/virtio/linux/printk.h new file +4 -0
@@ -0,0 +1,4 @@ +#include "../../../include/linux/kern_levels.h" + +#define printk printf +#define vprintk vprintf
tools/virtio/linux/ratelimit.h new file +4 -0
@@ -0,0 +1,4 @@ +#define DEFINE_RATELIMIT_STATE (name, interval_init, burst_init) int name = 0 + +#define __ratelimit (x) (*(x)) +
tools/virtio/linux/scatterlist.h new file +173 -0
173 lines not on this page · raw message
tools/virtio/linux/types.h new file +28 -0
@@ -0,0 +1,28 @@ +#ifndef TYPES_H +#define TYPES_H +#include <stdint.h> + +#define __force +#define __user +#define __must_check +#define __cold + +typedef uint64_t u64 ; +typedef int64_t s64 ; +typedef uint32_t u32 ; +typedef int32_t s32 ; +typedef uint16_t u16 ; +typedef int16_t s16 ; +typedef uint8_t u8 ; +typedef int8_t s8 ; + +typedef uint64_t __u64 ; +typedef int64_t __s64 ; +typedef uint32_t __u32 ; +typedef int32_t __s32 ; +typedef uint16_t __u16 ; +typedef int16_t __s16 ; +typedef uint8_t __u8 ; +typedef int8_t __s8 ; + +#endif /* TYPES_H */
tools/virtio/linux/uaccess.h new file +50 -0
50 lines not on this page · raw message
tools/virtio/linux/uio.h new file +1 -0
@@ -0,0 +1 @@ +#include "../../../include/linux/uio.h"
tools/virtio/linux/virtio.h +3 -147
176 lines not on this page · raw message
tools/virtio/linux/virtio_config.h new file +6 -0
@@ -0,0 +1,6 @@ +#define VIRTIO_TRANSPORT_F_START 28 +#define VIRTIO_TRANSPORT_F_END 32 + +#define virtio_has_feature (dev, feature) \ + test_bit((feature), (dev)->features) +
tools/virtio/linux/virtio_ring.h new file +1 -0
@@ -0,0 +1 @@ +#include "../../../include/linux/virtio_ring.h"
tools/virtio/linux/vringh.h new file +1 -0
@@ -0,0 +1 @@ +#include "../../../include/linux/vringh.h"
tools/virtio/uapi/linux/uio.h new file +1 -0
@@ -0,0 +1 @@ +#include <sys/uio.h>
tools/virtio/uapi/linux/virtio_config.h new file +1 -0
@@ -0,0 +1 @@ +#include "../../../../include/uapi/linux/virtio_config.h"
tools/virtio/uapi/linux/virtio_ring.h new file +4 -0
@@ -0,0 +1,4 @@ +#ifndef VIRTIO_RING_H +#define VIRTIO_RING_H +#include "../../../../include/uapi/linux/virtio_ring.h" +#endif /* VIRTIO_RING_H */
tools/virtio/virtio_test.c +4 -0
@@ -10,11 +10,15 @@#include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> +#include <stdbool.h> #include <linux/vhost.h> #include <linux/virtio.h> #include <linux/virtio_ring.h> #include "../../drivers/vhost/test.h" +/* Unused */ +void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end; +struct vq_info {int kick;int call;
PATCH 2/2 tools/virtio: make barriers stronger.
In the coming vringh_test, we share an mmap with another userspace process for testing. This requires real barriers.
tools/virtio/asm/barrier.h +2 -2
@@ -3,8 +3,8 @@#define mb () __sync_synchronize()#define smp_mb () mb() -# define smp_rmb () barrier() -# define smp_wmb () barrier() +# define smp_rmb () mb() +# define smp_wmb () mb()/* Weak barriers should be used. If not - it's a bug */ # define rmb () abort()# define wmb () abort()
Michael S. Tsirkin
maintainerRe: [PATCH 2/2] tools/virtio: make barriers stronger.
Quoted 10 lines · Rusty Russell
In the coming
vringh_test, we share an mmap with another userspace process for testing. This requires real barriers.Signed-off-by Rusty Russell <[email protected]>tools/virtio/asm/barrier.h +0
-0
tools/virtio/asm/barrier.h line 10
Quoted 10 lines
tools/virtio/asm/barrier.h +2
-2@@ -3,8 +3,8 @@#define mb () __sync_synchronize()#define smp_mb () mb() -# define smp_rmb () barrier() -# define smp_wmb () barrier() +# define smp_rmb () mb() +# define smp_wmb () mb()/* Weak barriers should be used. If not - it's a bug */ # define rmb () abort()# define wmb () abort()
Hmm this seems wrong on x86 which has strong order in hardware. It should not matter whether the other side is a userspace process or a kernel thread.
Signature · 1 line
MST
Re: [PATCH 2/2] tools/virtio: make barriers stronger.
Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Rusty Russell
In the coming
vringh_test, we share an mmap with another userspace process for testing. This requires real barriers.Signed-off-by Rusty Russell <[email protected]>tools/virtio/asm/barrier.h +0
-0@@ -3,8 +3,8 @@#define mb () __sync_synchronize()
Quoted 12 lines
tools/virtio/asm/barrier.h line 10
Quoted 8 lines
tools/virtio/asm/barrier.h +2
-2#define smp_mb () mb() -# define smp_rmb () barrier() -# define smp_wmb () barrier() +# define smp_rmb () mb() +# define smp_wmb () mb()/* Weak barriers should be used. If not - it's a bug */ # define rmb () abort()# define wmb () abort()Hmm this seems wrong on x86 which has strong order in hardware. It should not matter whether the other side is a userspace process or a kernel thread.
Actually, this code is completely generic now, though overkill for x86 smp_wmb():
Interestingly, when I try defining them, 32-bit x86 slows down (it seems that gcc is using "lock orl $0x0,(%esp)" for __sync_synchronize()).:
On my 32-bit laptop: Intel(R) Core(TM) i5 CPU M 560 @ 2.67GHz
Before:
Wall time:1.660000-1.790000(1.682500) After: Wall time:1.930000-3.620000(1.960625)
64 bit it's a win:
On 2.6.32-358.el6.x86_64, gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3), Intel(R) Xeon(R) CPU E5620 @ 2.40GHz:
Before:
real 0m2.937000-8.339000(3.123979)s user 0m2.811000-8.233000(2.954813)s sys 0m0.052000-0.154000(0.089396)s After: real 0m2.559000-2.936000(2.726729)s user 0m2.397000-2.651000(2.506396)s sys 0m0.055000-0.152000(0.090667)s
Raw performance doesn't really matter, of course, but it's tempting to use these asm barriers for __x86_64__, and use __sync_synchronize() everywhere for everyone else.
Thoughts?
Rusty.
tools/virtio/asm/barrier.h +5 -4
@@ -1,11 +1,12 @@#if defined(__i386__) || defined(__x86_64__)#define barrier () asm volatile("" ::: "memory") -#define mb () __sync_synchronize() -#define smp_mb () mb() -# define smp_rmb () mb() -# define smp_wmb () mb() +#define smp_mb () asm volatile("mfence":::"memory") +#define smp_rmb () asm volatile("lfence":::"memory") +#define smp_wmb () asm volatile("sfence" ::: "memory") +/* Weak barriers should be used. If not - it's a bug */ +# define mb () abort()# define rmb () abort()# define wmb () abort()#else
Michael S. Tsirkin
maintainerRe: [PATCH 2/2] tools/virtio: make barriers stronger.
Quoted 27 lines · Rusty Russell
Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Rusty Russell
In the coming
vringh_test, we share an mmap with another userspace process for testing. This requires real barriers.Signed-off-by Rusty Russell <[email protected]>tools/virtio/asm/barrier.h +0
-0@@ -3,8 +3,8 @@#define mb () __sync_synchronize()Quoted 12 lines
tools/virtio/asm/barrier.h line 10
Quoted 8 lines
tools/virtio/asm/barrier.h +2
-2#define smp_mb () mb() -# define smp_rmb () barrier() -# define smp_wmb () barrier() +# define smp_rmb () mb() +# define smp_wmb () mb()/* Weak barriers should be used. If not - it's a bug */ # define rmb () abort()# define wmb () abort()Hmm this seems wrong on x86 which has strong order in hardware. It should not matter whether the other side is a userspace process or a kernel thread.
Quoted 4 lines
Actually, this code is completely generic now, though overkill for x86
smp_wmb():Interestingly, when I try defining them, 32-bit x86 slows down (it seems that gcc is using "lock orl $0x0,(%esp)" for
__sync_synchronize()).:
Well this depends on which arch you are building for. We saw this in qemu too, see e.g. include/qemu/atomic.h in qemu.
Quoted 19 lines
On my 32-bit laptop: Intel(R) Core(TM) i5 CPU M 560 @ 2.67GHz
Before:
Wall time:1.660000-1.790000(1.682500) After: Wall time:1.930000-3.620000(1.960625)64 bit it's a win:
On 2.6.32-358.el6.x86_64, gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3), Intel(R) Xeon(R) CPU E5620 @ 2.40GHz:Before:
real 0m2.937000-8.339000(3.123979)s user 0m2.811000-8.233000(2.954813)s sys 0m0.052000-0.154000(0.089396)s After: real 0m2.559000-2.936000(2.726729)s user 0m2.397000-2.651000(2.506396)s sys 0m0.055000-0.152000(0.090667)s
Quoted 6 lines
Raw performance doesn't really matter, of course, but it's tempting to use these asm barriers for
__x86_64__, and use__sync_synchronize()everywhere for everyone else.Thoughts?
Rusty.
For smp_mb, I agree.
Quoted 6 lines
tools/virtio/asm/barrier.h +0
-0@@ -1,11 +1,12 @@#if defined(__i386__) || defined(__x86_64__)
tools/virtio/asm/barrier.h line 7
Quoted 10 lines
tools/virtio/asm/barrier.h +4
-4#define barrier () asm volatile("" ::: "memory") -#define mb () __sync_synchronize() -#define smp_mb () mb() -# define smp_rmb () mb() -# define smp_wmb () mb() +#define smp_mb () asm volatile("mfence":::"memory") +#define smp_rmb () asm volatile("lfence":::"memory") +#define smp_wmb () asm volatile("sfence" ::: "memory") +
Confused. On x86_64, as long as you are not synchronizing with a device these are not necessary, a compiler barrier will do, unless there are non-temporal loads/stores, which we don't use.
Quoted 5 lines
tools/virtio/asm/barrier.h +1
-0/* Weak barriers should be used. If not - it's a bug */ +# define mb () abort()# define rmb () abort()# define wmb () abort()#else
Re: [PATCH 2/2] tools/virtio: make barriers stronger.
Quoted 28 lines · Michael S. Tsirkin
Quoted 27 lines · Rusty Russell
Quoted 25 lines · Michael S. Tsirkin
Quoted 10 lines · Rusty Russell
In the coming
vringh_test, we share an mmap with another userspace process for testing. This requires real barriers.Signed-off-by Rusty Russell <[email protected]>tools/virtio/asm/barrier.h +0
-0tools/virtio/asm/barrier.h line 10
Quoted 10 lines
tools/virtio/asm/barrier.h +2
-2@@ -3,8 +3,8 @@#define mb () __sync_synchronize()#define smp_mb () mb() -# define smp_rmb () barrier() -# define smp_wmb () barrier() +# define smp_rmb () mb() +# define smp_wmb () mb()/* Weak barriers should be used. If not - it's a bug */ # define rmb () abort()# define wmb () abort()Hmm this seems wrong on x86 which has strong order in hardware. It should not matter whether the other side is a userspace process or a kernel thread.
Quoted 7 lines
Quoted 4 lines
Actually, this code is completely generic now, though overkill for x86
smp_wmb():Interestingly, when I try defining them, 32-bit x86 slows down (it seems that gcc is using "lock orl $0x0,(%esp)" for
__sync_synchronize()).:Well this depends on which arch you are building for. We saw this in qemu too, see e.g.
include/qemu/atomic.hin qemu.
Hmm, I thought x86 had load reordering, but it seems that was only some older chips, which we can consider obsolete.
I learned something... I've dropped the patch.
Thanks,
Rusty.
PATCH 1/3 vringh: host-side implementation of virtio rings (v2)
In reply to Rusty RussellGetting use of virtio rings correct is tricky, and a recent patch saw an implementation of in-kernel rings (as separate from userspace).
This abstracts the business of dealing with the virtio ring layout from the access (userspace or direct); to do this, we use function pointers, which gcc inlines correctly.
Changed:
Use ACCESS_ONCE in kernel ring accessors, to match user accessor semantics.
drivers/Makefile +1 -1
@@ -123,7 +123,7 @@ obj-$(CONFIG_PPC_PS3) += ps3/ obj-$(CONFIG_OF) += of/ obj-$(CONFIG_SSB) += ssb/ obj-$(CONFIG_BCMA) += bcma/ -obj-$(CONFIG_VHOST_NET) += vhost/ +obj-$(CONFIG_VHOST_RING) += vhost/ obj-$(CONFIG_VLYNQ) += vlynq/ obj-$(CONFIG_STAGING) += staging/ obj-y += platform/
drivers/vhost/Kconfig +8 -0
@@ -1,6 +1,7 @@ config VHOST_NET tristate "Host kernel accelerator for virtio net" depends on NET && EVENTFD && (TUN || !TUN) && (MACVTAP || !MACVTAP) + select VHOST_RING ---help--- This kernel module can be loaded in host kernel to accelerate guest networking with virtio_net. Not to be confused with virtio_net
@@ -12,3 +13,10 @@ config VHOST_NET if STAGING source "drivers/vhost/Kconfig.tcm" endif + +config VHOST_RING + tristate + ---help--- + This option is selected by any driver which needs to access + the host side of a virtio ring. +
drivers/vhost/Kconfig.tcm +1 -0
@@ -1,6 +1,7 @@ config TCM_VHOST tristate "TCM_VHOST fabric module" depends on TARGET_CORE && EVENTFD && m + select VHOST_RING default n ---help--- Say M here to enable the TCM_VHOST fabric module for use with virtio-scsi guests
drivers/vhost/Makefile +2 -0
@@ -2,3 +2,5 @@ obj-$(CONFIG_VHOST_NET) += vhost_net.o vhost_net-y := vhost.o net.o obj-$(CONFIG_TCM_VHOST) += tcm_vhost.o + +obj-$(CONFIG_VHOST_RING) += vringh.o
drivers/vhost/vringh.c new file +1012 -0
1,012 lines not on this page · raw message
include/linux/vringh.h new file +185 -0
185 lines not on this page · raw message
PATCH 2/3 vringh: don't flag already listening.
This doesn't work for eventidx:
1) vringh_notify_enable_user() gets called because the ring is empty (vrh->last_avail_idx == vrh->vring.avail->idx).
2) It puts vrh->last_avail_idx into vring_avail_event().
3) It sets ->listening to true.
4) Meanwhile, the other side has done more work, updating vrh->vring.avail->idx.
5) It notices vrh->vring.avail->idx has changed, so returns true to make us do more work.
But since the value we wrote into vring_avail_event() is past, we won't get notified. This happens rarely: only once I'd optimized add_buf could I trigger this in about 1 in 10 runs.
Since it's so rare, just remove the listening flag: it's fine to do this twice anyway.
drivers/vhost/vringh.c +0 -13
@@ -476,12 +476,6 @@ static inline bool __vringh_notify_enable(struct vringh *vrh, {u16 avail; -/* Already enabled? */ -if (vrh->listening) -return false; - - vrh->listening = true; -if (!vrh->event_indices) {/* Old-school; update flags. */ if (putu16 (&vrh->vring.used->flags,0 ) !=0 ) {
@@ -515,11 +509,6 @@ static inline bool __vringh_notify_enable(struct vringh *vrh,static inline void __vringh_notify_disable (struct vringh *vrh,int (*putu16)(u16 *p,u16 val)) { -/* Already disabled? */ -if (!vrh->listening) -return ; - - vrh->listening = false;if (!vrh->event_indices) {/* Old-school; update flags. */ if (putu16 (&vrh->vring.used->flags,VRING_USED_F_NO_NOTIFY )) {
@@ -593,7 +582,6 @@ int vringh_init_user(struct vringh *vrh, u32 features, vrh->event_indices = (features & (1 <<VIRTIO_RING_F_EVENT_IDX )); vrh->weak_barriers = weak_barriers; - vrh->listening = false; vrh->completed =0 ; vrh->last_avail_idx =0 ; vrh->last_used_idx =0 ;
@@ -853,7 +841,6 @@ int vringh_init_kern(struct vringh *vrh, u32 features, vrh->event_indices = (features & (1 <<VIRTIO_RING_F_EVENT_IDX )); vrh->weak_barriers = weak_barriers; - vrh->listening = false; vrh->completed =0 ; vrh->last_avail_idx =0 ; vrh->last_used_idx =0 ;
include/linux/vringh.h +0 -3
@@ -33,9 +33,6 @@ struct vringh {/* Guest publishes used event idx (note: we always do). */ bool event_indices; -/* Have we told the other end we want to be notified? */ -bool listening; -/* Can we get away with weak barriers? */ bool weak_barriers;
PATCH 3/3 tools/virtio: add vring_test (v2)
In reply to Rusty RussellThis is mainly to test the drivers/vhost/vringh.c code, but it also uses the drivers/virtio/virtio_ring.c code for the guest side.
Usage for testing the basic implementation:
./vringh_test # Test with indirect descriptors ./vringh_test --indirect # Test with indirect descriptors and event indexex ./vringh_test --indirect --eventidx
You can run a parallel stress test by adding --parallel to any of the above options.
eg ./vringh_test --parallel:
Using CPUS 0 and 3 Guest: notified 10107974, pinged 107970 Host: notified 108158, pinged 3172148
./vringh_test --indirect --eventidx --parallel:
Using CPUS 0 and 3 Guest: notified 156357, pinged 156251 Host: notified 156251, pinged 78179
Average of 50 times doing ./vringh_test --indirect --eventidx --parallel:
2.840000-3.040000(2.927292)user
Changes:
- build objects separately, just like kernel does.
- slightly better cmdline handling, arbitrary ordering of args
- --fast-vringh for benchmarking virtio_ring improvements.
- -U_FORTIFY_SOURCE to remove bogus Ubuntu fortify warnings.
tools/virtio/Makefile +6 -4
@@ -1,12 +1,14 @@ all: test mod -test: virtio_test +test: virtio_test vringh_test virtio_test: virtio_ring.o virtio_test.o -CFLAGS += -g -O2 -Wall -I. -I ../../usr/include/ -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -vpath %.c ../../drivers/virtio +vringh_test: vringh_test.o vringh.o virtio_ring.o + +CFLAGS += -g -O2 -Wall -I. -I ../../usr/include/ -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE +vpath %.c ../../drivers/virtio ../../drivers/vhost mod: ${MAKE} -C `pwd`/../.. M=`pwd`/vhost_test .PHONY: all test mod clean clean: - ${RM} *.o vhost_test/*.o vhost_test/.*.cmd \ + ${RM} *.o vringh_test virtio_test vhost_test/*.o vhost_test/.*.cmd \ vhost_test/Module.symvers vhost_test/modules.order *.d -include *.d
tools/virtio/linux/uio.h +2 -0
@@ -1 +1,3 @@ +#include <linux/kernel.h> +#include "../../../include/linux/uio.h"
tools/virtio/vringh_test.c new file +735 -0
735 lines not on this page · raw message