提交 05f3f415 编写于 作者: L Linus Torvalds

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-virtio

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-virtio:
  virtio: Force use of power-of-two for descriptor ring sizes
  lguest: Fix lguest virtio-blk backend size computation
  virtio: Fix used_idx wrap-around
  virtio: more fallout from scatterlist changes.
  virtio: fix vring_init for 64 bits
...@@ -62,8 +62,8 @@ typedef uint8_t u8; ...@@ -62,8 +62,8 @@ typedef uint8_t u8;
#endif #endif
/* We can have up to 256 pages for devices. */ /* We can have up to 256 pages for devices. */
#define DEVICE_PAGES 256 #define DEVICE_PAGES 256
/* This fits nicely in a single 4096-byte page. */ /* This will occupy 2 pages: it must be a power of 2. */
#define VIRTQUEUE_NUM 127 #define VIRTQUEUE_NUM 128
/*L:120 verbose is both a global flag and a macro. The C preprocessor allows /*L:120 verbose is both a global flag and a macro. The C preprocessor allows
* this, and although I wouldn't recommend it, it works quite nicely here. */ * this, and although I wouldn't recommend it, it works quite nicely here. */
...@@ -1036,7 +1036,8 @@ static void add_virtqueue(struct device *dev, unsigned int num_descs, ...@@ -1036,7 +1036,8 @@ static void add_virtqueue(struct device *dev, unsigned int num_descs,
void *p; void *p;
/* First we need some pages for this virtqueue. */ /* First we need some pages for this virtqueue. */
pages = (vring_size(num_descs) + getpagesize() - 1) / getpagesize(); pages = (vring_size(num_descs, getpagesize()) + getpagesize() - 1)
/ getpagesize();
p = get_pages(pages); p = get_pages(pages);
/* Initialize the configuration. */ /* Initialize the configuration. */
...@@ -1045,7 +1046,7 @@ static void add_virtqueue(struct device *dev, unsigned int num_descs, ...@@ -1045,7 +1046,7 @@ static void add_virtqueue(struct device *dev, unsigned int num_descs,
vq->config.pfn = to_guest_phys(p) / getpagesize(); vq->config.pfn = to_guest_phys(p) / getpagesize();
/* Initialize the vring. */ /* Initialize the vring. */
vring_init(&vq->vring, num_descs, p); vring_init(&vq->vring, num_descs, p, getpagesize());
/* Add the configuration information to this device's descriptor. */ /* Add the configuration information to this device's descriptor. */
add_desc_field(dev, VIRTIO_CONFIG_F_VIRTQUEUE, add_desc_field(dev, VIRTIO_CONFIG_F_VIRTQUEUE,
...@@ -1342,7 +1343,7 @@ static bool service_io(struct device *dev) ...@@ -1342,7 +1343,7 @@ static bool service_io(struct device *dev)
if (out->type & VIRTIO_BLK_T_SCSI_CMD) { if (out->type & VIRTIO_BLK_T_SCSI_CMD) {
fprintf(stderr, "Scsi commands unsupported\n"); fprintf(stderr, "Scsi commands unsupported\n");
in->status = VIRTIO_BLK_S_UNSUPP; in->status = VIRTIO_BLK_S_UNSUPP;
wlen = sizeof(in); wlen = sizeof(*in);
} else if (out->type & VIRTIO_BLK_T_OUT) { } else if (out->type & VIRTIO_BLK_T_OUT) {
/* Write */ /* Write */
...@@ -1363,7 +1364,7 @@ static bool service_io(struct device *dev) ...@@ -1363,7 +1364,7 @@ static bool service_io(struct device *dev)
/* Die, bad Guest, die. */ /* Die, bad Guest, die. */
errx(1, "Write past end %llu+%u", off, ret); errx(1, "Write past end %llu+%u", off, ret);
} }
wlen = sizeof(in); wlen = sizeof(*in);
in->status = (ret >= 0 ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR); in->status = (ret >= 0 ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR);
} else { } else {
/* Read */ /* Read */
...@@ -1376,10 +1377,10 @@ static bool service_io(struct device *dev) ...@@ -1376,10 +1377,10 @@ static bool service_io(struct device *dev)
ret = readv(vblk->fd, iov+1, in_num-1); ret = readv(vblk->fd, iov+1, in_num-1);
verbose("READ from sector %llu: %i\n", out->sector, ret); verbose("READ from sector %llu: %i\n", out->sector, ret);
if (ret >= 0) { if (ret >= 0) {
wlen = sizeof(in) + ret; wlen = sizeof(*in) + ret;
in->status = VIRTIO_BLK_S_OK; in->status = VIRTIO_BLK_S_OK;
} else { } else {
wlen = sizeof(in); wlen = sizeof(*in);
in->status = VIRTIO_BLK_S_IOERR; in->status = VIRTIO_BLK_S_IOERR;
} }
} }
......
...@@ -200,7 +200,8 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev, ...@@ -200,7 +200,8 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
/* Figure out how many pages the ring will take, and map that memory */ /* Figure out how many pages the ring will take, and map that memory */
lvq->pages = lguest_map((unsigned long)lvq->config.pfn << PAGE_SHIFT, lvq->pages = lguest_map((unsigned long)lvq->config.pfn << PAGE_SHIFT,
DIV_ROUND_UP(vring_size(lvq->config.num), DIV_ROUND_UP(vring_size(lvq->config.num,
PAGE_SIZE),
PAGE_SIZE)); PAGE_SIZE));
if (!lvq->pages) { if (!lvq->pages) {
err = -ENOMEM; err = -ENOMEM;
......
...@@ -146,6 +146,7 @@ static void try_fill_recv(struct virtnet_info *vi) ...@@ -146,6 +146,7 @@ static void try_fill_recv(struct virtnet_info *vi)
struct scatterlist sg[1+MAX_SKB_FRAGS]; struct scatterlist sg[1+MAX_SKB_FRAGS];
int num, err; int num, err;
sg_init_table(sg, 1+MAX_SKB_FRAGS);
for (;;) { for (;;) {
skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN); skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
if (unlikely(!skb)) if (unlikely(!skb))
...@@ -231,6 +232,8 @@ static int start_xmit(struct sk_buff *skb, struct net_device *dev) ...@@ -231,6 +232,8 @@ static int start_xmit(struct sk_buff *skb, struct net_device *dev)
const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
DECLARE_MAC_BUF(mac); DECLARE_MAC_BUF(mac);
sg_init_table(sg, 1+MAX_SKB_FRAGS);
pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest)); pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest));
free_old_xmit_skbs(vi); free_old_xmit_skbs(vi);
......
...@@ -53,7 +53,7 @@ struct vring_virtqueue ...@@ -53,7 +53,7 @@ struct vring_virtqueue
unsigned int num_added; unsigned int num_added;
/* Last used index we've seen. */ /* Last used index we've seen. */
unsigned int last_used_idx; u16 last_used_idx;
/* How to notify other side. FIXME: commonalize hcalls! */ /* How to notify other side. FIXME: commonalize hcalls! */
void (*notify)(struct virtqueue *vq); void (*notify)(struct virtqueue *vq);
...@@ -277,11 +277,17 @@ struct virtqueue *vring_new_virtqueue(unsigned int num, ...@@ -277,11 +277,17 @@ struct virtqueue *vring_new_virtqueue(unsigned int num,
struct vring_virtqueue *vq; struct vring_virtqueue *vq;
unsigned int i; unsigned int i;
/* We assume num is a power of 2. */
if (num & (num - 1)) {
dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
return NULL;
}
vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL); vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
if (!vq) if (!vq)
return NULL; return NULL;
vring_init(&vq->vring, num, pages); vring_init(&vq->vring, num, pages, PAGE_SIZE);
vq->vq.callback = callback; vq->vq.callback = callback;
vq->vq.vdev = vdev; vq->vq.vdev = vdev;
vq->vq.vq_ops = &vring_vq_ops; vq->vq.vq_ops = &vring_vq_ops;
......
...@@ -67,7 +67,7 @@ struct vring { ...@@ -67,7 +67,7 @@ struct vring {
}; };
/* The standard layout for the ring is a continuous chunk of memory which looks /* The standard layout for the ring is a continuous chunk of memory which looks
* like this. The used fields will be aligned to a "num+1" boundary. * like this. We assume num is a power of 2.
* *
* struct vring * struct vring
* { * {
...@@ -79,8 +79,8 @@ struct vring { ...@@ -79,8 +79,8 @@ struct vring {
* __u16 avail_idx; * __u16 avail_idx;
* __u16 available[num]; * __u16 available[num];
* *
* // Padding so a correctly-chosen num value will cache-align used_idx. * // Padding to the next page boundary.
* char pad[sizeof(struct vring_desc) - sizeof(avail_flags)]; * char pad[];
* *
* // A ring of used descriptor heads with free-running index. * // A ring of used descriptor heads with free-running index.
* __u16 used_flags; * __u16 used_flags;
...@@ -88,18 +88,21 @@ struct vring { ...@@ -88,18 +88,21 @@ struct vring {
* struct vring_used_elem used[num]; * struct vring_used_elem used[num];
* }; * };
*/ */
static inline void vring_init(struct vring *vr, unsigned int num, void *p) static inline void vring_init(struct vring *vr, unsigned int num, void *p,
unsigned int pagesize)
{ {
vr->num = num; vr->num = num;
vr->desc = p; vr->desc = p;
vr->avail = p + num*sizeof(struct vring); vr->avail = p + num*sizeof(struct vring_desc);
vr->used = p + (num+1)*(sizeof(struct vring) + sizeof(__u16)); vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + pagesize-1)
& ~(pagesize - 1));
} }
static inline unsigned vring_size(unsigned int num) static inline unsigned vring_size(unsigned int num, unsigned int pagesize)
{ {
return (num + 1) * (sizeof(struct vring_desc) + sizeof(__u16)) return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (2 + num)
+ sizeof(__u32) + num * sizeof(struct vring_used_elem); + pagesize - 1) & ~(pagesize - 1))
+ sizeof(__u16) * 2 + sizeof(struct vring_used_elem) * num;
} }
#ifdef __KERNEL__ #ifdef __KERNEL__
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册