Skip to content

Commit

Permalink
Merge pull request torvalds#151 from liuyuan10/barrier
Browse files Browse the repository at this point in the history
lkl: Add proper memory barrier to virtio.c
  • Loading branch information
Octavian Purdila committed Apr 19, 2016
2 parents 931f4f3 + f52b4c2 commit 987ed9f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tools/lkl/lib/virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ static inline void virtio_set_avail_event(struct virtio_queue *q, uint16_t val)
static inline void virtio_deliver_irq(struct virtio_dev *dev)
{
dev->int_status |= VIRTIO_MMIO_INT_VRING;
/* Make sure all memory writes before are visible to the driver. */
__sync_synchronize();
lkl_trigger_irq(dev->irq);
}

Expand All @@ -72,6 +74,13 @@ void virtio_req_complete(struct virtio_req *req, uint32_t len)

q->used->ring[idx].id = htole16(req->idx);
q->used->ring[idx].len = htole16(len);
/* Make sure all memory writes before are visible to the driver before
* updating the idx.
* We need it here even we already have one in virtio_deliver_irq()
* because there might already be an driver thread reading the idx and
* dequeuing used buffers.
*/
__sync_synchronize();
q->used->idx = htole16(new);

/* There are two rings: q->avail and q->used for each of the rx and tx
Expand Down Expand Up @@ -193,6 +202,9 @@ void virtio_process_queue(struct virtio_dev *dev, uint32_t qidx)
virtio_set_avail_event(q, q->avail->idx);

while (q->last_avail_idx != le16toh(q->avail->idx)) {
/* Make sure following loads happens after loading q->avail->idx.
*/
__sync_synchronize();
if (virtio_process_one(dev, q, q->last_avail_idx) < 0)
break;
}
Expand Down

0 comments on commit 987ed9f

Please sign in to comment.