aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/virtio_ring.h
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2007-11-11 21:39:18 -0500
committerRusty Russell <rusty@rustcorp.com.au>2007-11-11 21:59:40 -0500
commit42b36cc0ce717deeb10030141a43dede763a3ebe (patch)
treeb2dc48b4f16c5dc59461ad24b027d631edda1da4 /include/linux/virtio_ring.h
parent1200e646ae238afc536be70257290eb33fb6e364 (diff)
virtio: Force use of power-of-two for descriptor ring sizes
The virtio descriptor rings of size N-1 were nicely set up to be aligned to an N-byte boundary. But as Anthony Liguori points out, the free-running indices used by virtio require that the sizes be a power of 2, otherwise we get problems on wrap (demonstrated with lguest). So we replace the clever "2^n-1" scheme with a simple "align to page boundary" scheme: this means that all virtio rings take at least two pages, but it's safer than guessing cache alignment. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'include/linux/virtio_ring.h')
-rw-r--r--include/linux/virtio_ring.h19
1 files changed, 11 insertions, 8 deletions
diff --git a/include/linux/virtio_ring.h b/include/linux/virtio_ring.h
index 5b88d215af50..1a4ed49f6478 100644
--- a/include/linux/virtio_ring.h
+++ b/include/linux/virtio_ring.h
@@ -67,7 +67,7 @@ struct vring {
67}; 67};
68 68
69/* The standard layout for the ring is a continuous chunk of memory which looks 69/* The standard layout for the ring is a continuous chunk of memory which looks
70 * like this. The used fields will be aligned to a "num+1" boundary. 70 * like this. We assume num is a power of 2.
71 * 71 *
72 * struct vring 72 * struct vring
73 * { 73 * {
@@ -79,8 +79,8 @@ struct vring {
79 * __u16 avail_idx; 79 * __u16 avail_idx;
80 * __u16 available[num]; 80 * __u16 available[num];
81 * 81 *
82 * // Padding so a correctly-chosen num value will cache-align used_idx. 82 * // Padding to the next page boundary.
83 * char pad[sizeof(struct vring_desc) - sizeof(avail_flags)]; 83 * char pad[];
84 * 84 *
85 * // A ring of used descriptor heads with free-running index. 85 * // A ring of used descriptor heads with free-running index.
86 * __u16 used_flags; 86 * __u16 used_flags;
@@ -88,18 +88,21 @@ struct vring {
88 * struct vring_used_elem used[num]; 88 * struct vring_used_elem used[num];
89 * }; 89 * };
90 */ 90 */
91static inline void vring_init(struct vring *vr, unsigned int num, void *p) 91static inline void vring_init(struct vring *vr, unsigned int num, void *p,
92 unsigned int pagesize)
92{ 93{
93 vr->num = num; 94 vr->num = num;
94 vr->desc = p; 95 vr->desc = p;
95 vr->avail = p + num*sizeof(struct vring_desc); 96 vr->avail = p + num*sizeof(struct vring_desc);
96 vr->used = p + (num+1)*(sizeof(struct vring_desc) + sizeof(__u16)); 97 vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + pagesize-1)
98 & ~(pagesize - 1));
97} 99}
98 100
99static inline unsigned vring_size(unsigned int num) 101static inline unsigned vring_size(unsigned int num, unsigned int pagesize)
100{ 102{
101 return (num + 1) * (sizeof(struct vring_desc) + sizeof(__u16)) 103 return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (2 + num)
102 + sizeof(__u32) + num * sizeof(struct vring_used_elem); 104 + pagesize - 1) & ~(pagesize - 1))
105 + sizeof(__u16) * 2 + sizeof(struct vring_used_elem) * num;
103} 106}
104 107
105#ifdef __KERNEL__ 108#ifdef __KERNEL__