net: sun4i-emac: fix memory leak on bad packet

Message ID 1407253479-30076-1-git-send-email-marc.zyngier@arm.com
State New
Headers show

Commit Message

Marc Zyngier Aug. 5, 2014, 3:44 p.m.
Upon reception of a new frame, the emac driver checks for a number
of error conditions, and flag the packet as "bad" if any of these
are present. It then allocates a skb unconditionally, but only uses
it if the packet is "good". On the error path, the skb is just forgotten,
and the system leaks memory.

The piece of junk I have on my desk seems to encounter such error
frequently enough so that the box goes OOM after a couple of days,
which makes me grumpy.

Fix this by moving the allocation on the "good_packet" path (and
convert it to netdev_alloc_skb while we're at it).

Tested on a random Allwinner A20 board.

Cc: Stefan Roese <sr@denx.de>
Cc: Maxime Ripard <maxime.ripard@free-electrons.com>
Cc: <stable@vger.kernel.org> # 3.11+
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
---
 drivers/net/ethernet/allwinner/sun4i-emac.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Maxime Ripard Aug. 5, 2014, 9:07 p.m. | #1
On Tue, Aug 05, 2014 at 04:44:39PM +0100, Marc Zyngier wrote:
> Upon reception of a new frame, the emac driver checks for a number
> of error conditions, and flag the packet as "bad" if any of these
> are present. It then allocates a skb unconditionally, but only uses
> it if the packet is "good". On the error path, the skb is just forgotten,
> and the system leaks memory.
> 
> The piece of junk I have on my desk seems to encounter such error
> frequently enough so that the box goes OOM after a couple of days,
> which makes me grumpy.
> 
> Fix this by moving the allocation on the "good_packet" path (and
> convert it to netdev_alloc_skb while we're at it).
> 
> Tested on a random Allwinner A20 board.
> 
> Cc: Stefan Roese <sr@denx.de>
> Cc: Maxime Ripard <maxime.ripard@free-electrons.com>
> Cc: <stable@vger.kernel.org> # 3.11+
> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>

Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Thanks!
Maxime

Patch

diff --git a/drivers/net/ethernet/allwinner/sun4i-emac.c b/drivers/net/ethernet/allwinner/sun4i-emac.c
index d81e716..29b9f08 100644
--- a/drivers/net/ethernet/allwinner/sun4i-emac.c
+++ b/drivers/net/ethernet/allwinner/sun4i-emac.c
@@ -633,8 +633,10 @@  static void emac_rx(struct net_device *dev)
 		}
 
 		/* Move data from EMAC */
-		skb = dev_alloc_skb(rxlen + 4);
-		if (good_packet && skb) {
+		if (good_packet) {
+			skb = netdev_alloc_skb(dev, rxlen + 4);
+			if (!skb)
+				continue;
 			skb_reserve(skb, 2);
 			rdptr = (u8 *) skb_put(skb, rxlen - 4);