Skip to content

Commit

Permalink
virtio: net refill on out-of-memory
Browse files Browse the repository at this point in the history
If we run out of memory, use keventd to fill the buffer.  There's a
report of this happening: "Page allocation failures in guest",
Message-ID: <20090713115158.0a4892b0@mjolnir.ossman.eu>

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Rusty Russell authored and David S. Miller committed Aug 26, 2009
1 parent 0b4f292 commit 3161e45
Showing 1 changed file with 46 additions and 15 deletions.
61 changes: 46 additions & 15 deletions drivers/net/virtio_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ struct virtnet_info
struct sk_buff_head recv;
struct sk_buff_head send;

/* Work struct for refilling if we run low on memory. */
struct delayed_work refill;

/* Chain pages by the private ptr. */
struct page *pages;
};
Expand Down Expand Up @@ -273,19 +276,22 @@ static void receive_skb(struct net_device *dev, struct sk_buff *skb,
dev_kfree_skb(skb);
}

static void try_fill_recv_maxbufs(struct virtnet_info *vi)
static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp)
{
struct sk_buff *skb;
struct scatterlist sg[2+MAX_SKB_FRAGS];
int num, err, i;
bool oom = false;

sg_init_table(sg, 2+MAX_SKB_FRAGS);
for (;;) {
struct virtio_net_hdr *hdr;

skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN);
if (unlikely(!skb))
if (unlikely(!skb)) {
oom = true;
break;
}

skb_reserve(skb, NET_IP_ALIGN);
skb_put(skb, MAX_PACKET_LEN);
Expand All @@ -296,7 +302,7 @@ static void try_fill_recv_maxbufs(struct virtnet_info *vi)
if (vi->big_packets) {
for (i = 0; i < MAX_SKB_FRAGS; i++) {
skb_frag_t *f = &skb_shinfo(skb)->frags[i];
f->page = get_a_page(vi, GFP_ATOMIC);
f->page = get_a_page(vi, gfp);
if (!f->page)
break;

Expand Down Expand Up @@ -325,31 +331,35 @@ static void try_fill_recv_maxbufs(struct virtnet_info *vi)
if (unlikely(vi->num > vi->max))
vi->max = vi->num;
vi->rvq->vq_ops->kick(vi->rvq);
return !oom;
}

static void try_fill_recv(struct virtnet_info *vi)
/* Returns false if we couldn't fill entirely (OOM). */
static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
{
struct sk_buff *skb;
struct scatterlist sg[1];
int err;
bool oom = false;

if (!vi->mergeable_rx_bufs) {
try_fill_recv_maxbufs(vi);
return;
}
if (!vi->mergeable_rx_bufs)
return try_fill_recv_maxbufs(vi, gfp);

for (;;) {
skb_frag_t *f;

skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
if (unlikely(!skb))
if (unlikely(!skb)) {
oom = true;
break;
}

skb_reserve(skb, NET_IP_ALIGN);

f = &skb_shinfo(skb)->frags[0];
f->page = get_a_page(vi, GFP_ATOMIC);
f->page = get_a_page(vi, gfp);
if (!f->page) {
oom = true;
kfree_skb(skb);
break;
}
Expand All @@ -373,6 +383,7 @@ static void try_fill_recv(struct virtnet_info *vi)
if (unlikely(vi->num > vi->max))
vi->max = vi->num;
vi->rvq->vq_ops->kick(vi->rvq);
return !oom;
}

static void skb_recv_done(struct virtqueue *rvq)
Expand All @@ -385,6 +396,23 @@ static void skb_recv_done(struct virtqueue *rvq)
}
}

static void refill_work(struct work_struct *work)
{
struct virtnet_info *vi;
bool still_empty;

vi = container_of(work, struct virtnet_info, refill.work);
napi_disable(&vi->napi);
try_fill_recv(vi, GFP_KERNEL);
still_empty = (vi->num == 0);
napi_enable(&vi->napi);

/* In theory, this can happen: if we don't get any buffers in
* we will *never* try to fill again. */
if (still_empty)
schedule_delayed_work(&vi->refill, HZ/2);
}

static int virtnet_poll(struct napi_struct *napi, int budget)
{
struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
Expand All @@ -400,10 +428,10 @@ static int virtnet_poll(struct napi_struct *napi, int budget)
received++;
}

/* FIXME: If we oom and completely run out of inbufs, we need
* to start a timer trying to fill more. */
if (vi->num < vi->max / 2)
try_fill_recv(vi);
if (vi->num < vi->max / 2) {
if (!try_fill_recv(vi, GFP_ATOMIC))
schedule_delayed_work(&vi->refill, 0);
}

/* Out of packets? */
if (received < budget) {
Expand Down Expand Up @@ -893,6 +921,7 @@ static int virtnet_probe(struct virtio_device *vdev)
vi->vdev = vdev;
vdev->priv = vi;
vi->pages = NULL;
INIT_DELAYED_WORK(&vi->refill, refill_work);

/* If they give us a callback when all buffers are done, we don't need
* the timer. */
Expand Down Expand Up @@ -941,7 +970,7 @@ static int virtnet_probe(struct virtio_device *vdev)
}

/* Last of all, set up some receive buffers. */
try_fill_recv(vi);
try_fill_recv(vi, GFP_KERNEL);

/* If we didn't even get one input buffer, we're useless. */
if (vi->num == 0) {
Expand All @@ -958,6 +987,7 @@ static int virtnet_probe(struct virtio_device *vdev)

unregister:
unregister_netdev(dev);
cancel_delayed_work_sync(&vi->refill);
free_vqs:
vdev->config->del_vqs(vdev);
free:
Expand Down Expand Up @@ -986,6 +1016,7 @@ static void virtnet_remove(struct virtio_device *vdev)
BUG_ON(vi->num != 0);

unregister_netdev(vi->dev);
cancel_delayed_work_sync(&vi->refill);

vdev->config->del_vqs(vi->vdev);

Expand Down

0 comments on commit 3161e45

Please sign in to comment.