Received: by 2002:a17:90a:9103:0:0:0:0 with SMTP id k3csp11802366pjo; Thu, 2 Jan 2020 14:32:21 -0800 (PST) X-Google-Smtp-Source: APXvYqwovaidOj30+4Loq1odcrA/DdWqZ9rGOouFYw6SRNZefjoaD+DnUm6tOr9JjU7BIBgctnrn X-Received: by 2002:a9d:3e43:: with SMTP id h3mr91248483otg.84.1578004341246; Thu, 02 Jan 2020 14:32:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578004341; cv=none; d=google.com; s=arc-20160816; b=HA22gV/Nj1j5Pn2lkuz89uJl63XU7ojARptI1yyUCnvo4YSANU7na7RMtfDY+vH1Rd 60UE9FxM9UIXEih8iPSw8/0OARQDQlel/mENiluI7XCSwkJn9cgkjsBC9PzkPPJ+judp LNAqb0f5MqsRMg/DeKM//Wd309L9LxfjBGIFw8BCln3R2YRPPvHXGsYzYjquVw2A7PkB 7bSzTq+jpaIKFWiHgf9BPrivpN0nXYQsssDf2AObov5TXX3duXU4aZYiDaOUhf4bYyUm 73Xw/pbXS+45sjXuR99K6kiDgeALx0UQwagoJqxAlIHoBeanQIVSayAtRT79CkdxfHor WLhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pACDpn5SwA1TRCpMX0coCLX0pP1owm1QkGqewEIdm5c=; b=dgqGQEyi6Gqa09cTnkEaocjePkWkAc2NtJQCvNZ0XWLif1eqGW3J1XqopqTNv4Dz2p Mgry5Nfn9XBV+gm4dvvvKc8v/eOdjVsLxtqkxF5V9L1hou0jXq9DNmzb7ahi7xc1zIT2 aatEXiVpgoeYz9iTOd2r0NdJoMLioLazfaiMGT6UpJewOaP+R52Eyl7AUt8j9ztSX7bs d1KuLeuP6oAflA3PZ4quwhCpwyD8mtZi8XzmkqcVtijYNqlyJLBBzYI8mPQhCwYPzgUC Hv7tYDWV2DB78MYp385QS3cLjbjGHLhnlK5uadxpnHGvvnrxCthgDB45Z8yaLFl44W32 cm8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mRP2sa5g; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v5si30593218otn.155.2020.01.02.14.32.10; Thu, 02 Jan 2020 14:32:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mRP2sa5g; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730295AbgABWba (ORCPT + 99 others); Thu, 2 Jan 2020 17:31:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:36618 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730143AbgABWb1 (ORCPT ); Thu, 2 Jan 2020 17:31:27 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AD1EF2253D; Thu, 2 Jan 2020 22:31:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578004287; bh=vApgtmOsQVMPYg5aH4YmE1Ihj8QEHEKdTfmokdJklfs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mRP2sa5gnkLYwozfqeKqgFXFKkDlPcft7byUm0p31SF6XV6v7qNhtoYSc5QcyLIjP Nphz0B4lE8vpQPA4+xjSIqEHyZh9XBhUP8731uEvUfSRZV61TK+TtoLW/+vDXtFJce 3GT97G2bLTJMVkSfaCfxstZ2yqiyaR1Zyda163GE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ben Hutchings , "David S. Miller" Subject: [PATCH 4.9 098/171] net: qlogic: Fix error paths in ql_alloc_large_buffers() Date: Thu, 2 Jan 2020 23:07:09 +0100 Message-Id: <20200102220600.852789397@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200102220546.960200039@linuxfoundation.org> References: <20200102220546.960200039@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ben Hutchings [ Upstream commit cad46039e4c99812db067c8ac22a864960e7acc4 ] ql_alloc_large_buffers() has the usual RX buffer allocation loop where it allocates skbs and maps them for DMA. It also treats failure as a fatal error. There are (at least) three bugs in the error paths: 1. ql_free_large_buffers() assumes that the lrg_buf[] entry for the first buffer that couldn't be allocated will have .skb == NULL. But the qla_buf[] array is not zero-initialised. 2. ql_free_large_buffers() DMA-unmaps all skbs in lrg_buf[]. This is incorrect for the last allocated skb, if DMA mapping failed. 3. Commit 1acb8f2a7a9f ("net: qlogic: Fix memory leak in ql_alloc_large_buffers") added a direct call to dev_kfree_skb_any() after the skb is recorded in lrg_buf[], so ql_free_large_buffers() will double-free it. The bugs are somewhat inter-twined, so fix them all at once: * Clear each entry in qla_buf[] before attempting to allocate an skb for it. This goes half-way to fixing bug 1. * Set the .skb field only after the skb is DMA-mapped. This fixes the rest. Fixes: 1357bfcf7106 ("qla3xxx: Dynamically size the rx buffer queue ...") Fixes: 0f8ab89e825f ("qla3xxx: Check return code from pci_map_single() ...") Fixes: 1acb8f2a7a9f ("net: qlogic: Fix memory leak in ql_alloc_large_buffers") Signed-off-by: Ben Hutchings Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- drivers/net/ethernet/qlogic/qla3xxx.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) --- a/drivers/net/ethernet/qlogic/qla3xxx.c +++ b/drivers/net/ethernet/qlogic/qla3xxx.c @@ -2752,6 +2752,9 @@ static int ql_alloc_large_buffers(struct int err; for (i = 0; i < qdev->num_large_buffers; i++) { + lrg_buf_cb = &qdev->lrg_buf[i]; + memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb)); + skb = netdev_alloc_skb(qdev->ndev, qdev->lrg_buffer_len); if (unlikely(!skb)) { @@ -2762,11 +2765,7 @@ static int ql_alloc_large_buffers(struct ql_free_large_buffers(qdev); return -ENOMEM; } else { - - lrg_buf_cb = &qdev->lrg_buf[i]; - memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb)); lrg_buf_cb->index = i; - lrg_buf_cb->skb = skb; /* * We save some space to copy the ethhdr from first * buffer @@ -2788,6 +2787,7 @@ static int ql_alloc_large_buffers(struct return -ENOMEM; } + lrg_buf_cb->skb = skb; dma_unmap_addr_set(lrg_buf_cb, mapaddr, map); dma_unmap_len_set(lrg_buf_cb, maplen, qdev->lrg_buffer_len -