From patchwork Tue Mar 28 10:12:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonas Jensen X-Patchwork-Id: 9648749 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3B237601D7 for ; Tue, 28 Mar 2017 10:13:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2727B26419 for ; Tue, 28 Mar 2017 10:13:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1C10A26D05; Tue, 28 Mar 2017 10:13:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.4 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,RCVD_IN_SORBS_WEB autolearn=no version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8371226419 for ; Tue, 28 Mar 2017 10:13:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=z4MDF8eTYxJC/1jO9hrUmySDmlnesuG1w+yZNj2b+1E=; b=tre OMbgAOg4/OCCqmD0Iv3qASwZY7cww2/ASPvvQLmsnQ9VHDDkSnCMG7sGnAEWROqHhDuWy4N5z/giD TwaBZ4tR/stCF2kliHXvO4p8N8ez9ze7GMGVE+je38CwrNpTG4GNUqEsIT9KthZCoEc9Xx3SRoNXs 5IVnApr5fmXbXRSMvRoc2dXSguPe24x4cS0lFAcQNU037wqVWPvUMtOjXOCTr/cT0h9VK9anMeSYd RGc4U1RfyrOBZLEVqTn9dWaHkzrifSt4JXQbJIRacwj3zZTzjmQ4oaA2BXBfHuLhFUc6+PPyT/w1B sUjLDhZ3dHUv/ev0wr1xbfk4ejTYHQQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cso7o-0000wc-3e; Tue, 28 Mar 2017 10:13:16 +0000 Received: from mail-lf0-x243.google.com ([2a00:1450:4010:c07::243]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cso7j-0000u7-Ik for linux-arm-kernel@lists.infradead.org; Tue, 28 Mar 2017 10:13:13 +0000 Received: by mail-lf0-x243.google.com with SMTP id n78so9966328lfi.3 for ; Tue, 28 Mar 2017 03:12:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=UoRAA/pIzmGoiuwZZsDAL3ME5ZY1UPlXISpnnykbe98=; b=VYVFRsdM6t5/QRnS7kugIOvPkgYRlZU5WR0iKKcx6Y2C2kmLLPrQ0EuCl91M4c6bew LfCyTG6gfLYEayfV2xYRO/fXflFspqSbqaeA8v73xc6+5OlqT6vaQrQia7GI1g4Zmg3m tBCdCxFz89vxeLTcoAvKCo2WHrLLXGx9KZZnUqTm2VYWFELH5KQ+MsUm3/bXZfxQbHuD Rh+cbyoN91aJV0plVkn+XdtBWfZUcmF4jy6qCS0j4xSv9ednKiuuPVzVPWqa54CRBO/L dp3cbhRORS5iqP0SAYgFgcYrErGD+B2sVTwAyL9KAbrz/sKdO3p/ElS0NvAO4ggL0AOo nhwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=UoRAA/pIzmGoiuwZZsDAL3ME5ZY1UPlXISpnnykbe98=; b=YN8Gxucsi2N7MwtkBdB6MMGN6qnkRzbpefDqu9bWR0ro8F5X98AXlELHoz4hCkRqU7 9DR69wZpYn6qBqJ7yh8ckzM5fwQ/jmpwuN3kssj8ne/4/iV6DTC82NArAyylpSuLM3H1 v727CMgRhxWKCi82cH7LrV7L8WUi0jCzA3UwWohAtodM5ag1iNn2n+73CFjYOM5YzuqD 4n1J4UviOzwYduFO1ppx08Rn1DaSZ46sNjBJrC5V2gm3aQ+c+H7U6pN++9D0PsXXLZSK 6KpzjL2uBbhNXzvzOQzxLPj3+vjBXElluIzs/qIeWcK0y1n6OpolkX8I6v1gIOvuNLL5 gVwg== X-Gm-Message-State: AFeK/H3+MyCyyO3mo+20UFmLh+P74hbpdSEzlm65aNaXOjGUkDiRf/qsttnmJvhc90V+3Q== X-Received: by 10.25.92.212 with SMTP id u81mr10793549lfi.160.1490695969462; Tue, 28 Mar 2017 03:12:49 -0700 (PDT) Received: from ildjarn.test.com (static-213-115-41-10.sme.bredbandsbolaget.se. [213.115.41.10]) by smtp.gmail.com with ESMTPSA id 2sm605619ljh.57.2017.03.28.03.12.48 (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 28 Mar 2017 03:12:48 -0700 (PDT) From: Jonas Jensen To: netdev@vger.kernel.org Subject: [PATCH v3] net: moxa: fix TX overrun memory leak Date: Tue, 28 Mar 2017 12:12:38 +0200 Message-Id: <1490695958-23222-1-git-send-email-jonas.jensen@gmail.com> X-Mailer: git-send-email 1.8.2.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170328_031311_861879_219D616D X-CRM114-Status: GOOD ( 13.59 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Jonas Jensen MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP moxart_mac_start_xmit() doesn't care where tx_tail is, tx_head can catch and pass tx_tail, which is bad because moxart_tx_finished() isn't guaranteed to catch up on freeing resources from tx_tail. Add a check in moxart_mac_start_xmit() stopping the queue at the end of the circular buffer. Also add a check in moxart_tx_finished() waking the queue if the buffer has TX_WAKE_THRESHOLD or more free descriptors. While we're at it, move spin_lock_irq() to happen before our descriptor pointer is assigned in moxart_mac_start_xmit(). Addresses https://bugzilla.kernel.org/show_bug.cgi?id=99451 Signed-off-by: Jonas Jensen --- Notes: ChangeLog v2->v3: - add wakeup threshold ChangeLog v1->v2: - stop queue instead of dropping frames Applies to next-20170310 drivers/net/ethernet/moxa/moxart_ether.c | 20 ++++++++++++++++++-- drivers/net/ethernet/moxa/moxart_ether.h | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/moxa/moxart_ether.c b/drivers/net/ethernet/moxa/moxart_ether.c index 06c9f41..6ad44be 100644 --- a/drivers/net/ethernet/moxa/moxart_ether.c +++ b/drivers/net/ethernet/moxa/moxart_ether.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "moxart_ether.h" @@ -278,6 +279,13 @@ static int moxart_rx_poll(struct napi_struct *napi, int budget) return rx; } +static int moxart_tx_queue_space(struct net_device *ndev) +{ + struct moxart_mac_priv_t *priv = netdev_priv(ndev); + + return CIRC_SPACE(priv->tx_head, priv->tx_tail, TX_DESC_NUM); +} + static void moxart_tx_finished(struct net_device *ndev) { struct moxart_mac_priv_t *priv = netdev_priv(ndev); @@ -297,6 +305,9 @@ static void moxart_tx_finished(struct net_device *ndev) tx_tail = TX_NEXT(tx_tail); } priv->tx_tail = tx_tail; + if (netif_queue_stopped(ndev) && + moxart_tx_queue_space(ndev) >= TX_WAKE_THRESHOLD) + netif_wake_queue(ndev); } static irqreturn_t moxart_mac_interrupt(int irq, void *dev_id) @@ -324,13 +335,18 @@ static int moxart_mac_start_xmit(struct sk_buff *skb, struct net_device *ndev) struct moxart_mac_priv_t *priv = netdev_priv(ndev); void *desc; unsigned int len; - unsigned int tx_head = priv->tx_head; + unsigned int tx_head; u32 txdes1; int ret = NETDEV_TX_BUSY; + spin_lock_irq(&priv->txlock); + + tx_head = priv->tx_head; desc = priv->tx_desc_base + (TX_REG_DESC_SIZE * tx_head); - spin_lock_irq(&priv->txlock); + if (moxart_tx_queue_space(ndev) == 1) + netif_stop_queue(ndev); + if (moxart_desc_read(desc + TX_REG_OFFSET_DESC0) & TX_DESC0_DMA_OWN) { net_dbg_ratelimited("no TX space for packet\n"); priv->stats.tx_dropped++; diff --git a/drivers/net/ethernet/moxa/moxart_ether.h b/drivers/net/ethernet/moxa/moxart_ether.h index 93a9563..afc32ec 100644 --- a/drivers/net/ethernet/moxa/moxart_ether.h +++ b/drivers/net/ethernet/moxa/moxart_ether.h @@ -59,6 +59,7 @@ #define TX_NEXT(N) (((N) + 1) & (TX_DESC_NUM_MASK)) #define TX_BUF_SIZE 1600 #define TX_BUF_SIZE_MAX (TX_DESC1_BUF_SIZE_MASK+1) +#define TX_WAKE_THRESHOLD 16 #define RX_DESC_NUM 64 #define RX_DESC_NUM_MASK (RX_DESC_NUM-1)