Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp806893pxu; Wed, 14 Oct 2020 14:28:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTSUl86umARR+I0tfg+PPOTyAZ0udLddBVeKOdoM3nlqdA4cUDgcEDotLY3p+iari1L4ia X-Received: by 2002:a17:906:2e0e:: with SMTP id n14mr1104915eji.120.1602710880019; Wed, 14 Oct 2020 14:28:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602710880; cv=none; d=google.com; s=arc-20160816; b=W+8QUvIyj+nXxwoc9zbazr6SzQgCg6ibybyjbn1V/eo3ZcK226RQcRTXtN0xrFhGGC pEbhQdT/3xu3q6dMft4DQLKKCcR5Mas4xwjqiCZLpBDR/F0j+fISnurn1tWV+J9EUyTl pX358yD22GXtgs6ODf0KkG6b2tcZBjItEWeyZQjVal1Mn0IkXShUT+BvVyABRyCejl7s kHsTbIduUcSBFN0agVSk3HmXI4ADvSZmIaBnA81PsjiQ/3xXDqy3uS8068AmAK2JYsVQ Z8ddyiGh4UDorY9bvKN9RmpAQ5cebuE95wpCg3I2MT+c2jae4eEBJnf4GWz7wAHEE9zZ Hkpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=q6thQZBRRq+1PTM392w9Mekv2vxKvbBynstiWkLboYI=; b=ERA8PyicVWVYQdo/DNOR5G2duyyJ/rKs9aUYSlkQlgz/qoJus7x0b9av0FrZ4pxgOZ kDk0FvMUk3lwPxH2aPzXv/bK4zFF4RdT26UinolFPceElwSTjP7Q5qgwThV+rxy5IOEE BMVsRoBegiCGFh/39c7r40z8yrjts9aSAhzsiICgJUhBLGnPIhb+Z4e2IMUhL3i2PnnD nahhVETxSfZExv7XYUD4JQxK5FuC5NSbCa57iLJ3ZS4Xphe3C6cTi3aHNzUCDWDJh/Kf vrWteCwhaaYOLAzBQKTBbP3ok+ar1JuwrNLaGvLulKzJ7i6fE7hsAK9NdS7FtJlVyXRF Zotg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=vl2LLDEY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v7si648498edj.392.2020.10.14.14.27.36; Wed, 14 Oct 2020 14:28:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=vl2LLDEY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731056AbgJNNJG (ORCPT + 99 others); Wed, 14 Oct 2020 09:09:06 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:52996 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727141AbgJNNJG (ORCPT ); Wed, 14 Oct 2020 09:09:06 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 09ECsJ0p044174; Wed, 14 Oct 2020 13:08:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=corp-2020-01-29; bh=q6thQZBRRq+1PTM392w9Mekv2vxKvbBynstiWkLboYI=; b=vl2LLDEYIu/tkteUj4gO/wWNsUoP4B3DDaePMhuxHydGPsJhz8HL1y3aayN+oRTMxkRf OB/OR51Yc9Q71vOOwnC0gr0z1ixvg1Mi11AYpVpcnVtB1GLeKUrwS67DzWpg1Lxee+2T n8YRyZdo//k7F3DSnzNG0G8UEF5/9X4mNQwtMgUOAlo+swpJ7NkqkzeHsh30Po91y+Mt 0rRZH0w5rFpi2EKWY8S7qHH2/38ta9tj3us4aS+JOYIdmPX3cpNVW4thyH3BeA1IRqQM GuDpxmYHngnUXzLQ6l0YrWzty+2ZRq2aDe3txvzcbX41q8716wWTmxp0fFr5umFyE8Ke Mg== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by aserp2120.oracle.com with ESMTP id 3434wkqbde-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 14 Oct 2020 13:08:57 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 09ECpZNm179520; Wed, 14 Oct 2020 13:08:56 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userp3020.oracle.com with ESMTP id 344by3knj4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 14 Oct 2020 13:08:56 +0000 Received: from abhmp0015.oracle.com (abhmp0015.oracle.com [141.146.116.21]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 09ED8stV025276; Wed, 14 Oct 2020 13:08:54 GMT Received: from kadam (/41.57.98.10) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 14 Oct 2020 06:08:54 -0700 Date: Wed, 14 Oct 2020 16:08:46 +0300 From: Dan Carpenter To: Coiby Xu Cc: devel@driverdev.osuosl.org, Willem de Bruijn , "supporter:QLOGIC QLGE 10Gb ETHERNET DRIVER" , Manish Chopra , Greg Kroah-Hartman , Shung-Hsi Yu , open list , Benjamin Poirier , "open list:QLOGIC QLGE 10Gb ETHERNET DRIVER" Subject: Re: [PATCH v2 2/7] staging: qlge: Initialize devlink health dump framework Message-ID: <20201014130846.GU1042@kadam> References: <20201014104306.63756-1-coiby.xu@gmail.com> <20201014104306.63756-3-coiby.xu@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201014104306.63756-3-coiby.xu@gmail.com> User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9773 signatures=668681 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 spamscore=0 suspectscore=2 mlxscore=0 malwarescore=0 adultscore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2010140093 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9773 signatures=668681 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 lowpriorityscore=0 mlxscore=0 malwarescore=0 phishscore=0 suspectscore=2 impostorscore=0 clxscore=1011 spamscore=0 priorityscore=1501 bulkscore=0 adultscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2010140093 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Oct 14, 2020 at 06:43:01PM +0800, Coiby Xu wrote: > static int qlge_probe(struct pci_dev *pdev, > const struct pci_device_id *pci_entry) > { > struct net_device *ndev = NULL; > struct qlge_adapter *qdev = NULL; > + struct devlink *devlink; > static int cards_found; > int err = 0; > > - ndev = alloc_etherdev_mq(sizeof(struct qlge_adapter), > + devlink = devlink_alloc(&qlge_devlink_ops, sizeof(struct qlge_adapter)); > + if (!devlink) > + return -ENOMEM; > + > + qdev = devlink_priv(devlink); > + > + ndev = alloc_etherdev_mq(sizeof(struct qlge_netdev_priv), > min(MAX_CPUS, > netif_get_num_default_rss_queues())); > if (!ndev) > - return -ENOMEM; > + goto devlink_free; > > - err = qlge_init_device(pdev, ndev, cards_found); > - if (err < 0) { > - free_netdev(ndev); > - return err; In the old code, if qlge_init_device() fails then it frees "ndev". > - } > + qdev->ndev = ndev; > + err = qlge_init_device(pdev, qdev, cards_found); > + if (err < 0) > + goto devlink_free; But the patch introduces a new resource leak. > > - qdev = netdev_priv(ndev); > SET_NETDEV_DEV(ndev, &pdev->dev); > ndev->hw_features = NETIF_F_SG | > NETIF_F_IP_CSUM | > @@ -4611,8 +4619,14 @@ static int qlge_probe(struct pci_dev *pdev, > qlge_release_all(pdev); > pci_disable_device(pdev); > free_netdev(ndev); > - return err; > + goto devlink_free; > } > + > + err = devlink_register(devlink, &pdev->dev); > + if (err) > + goto devlink_free; > + > + qlge_health_create_reporters(qdev); > /* Start up the timer to trigger EEH if > * the bus goes dead > */ > @@ -4623,6 +4637,10 @@ static int qlge_probe(struct pci_dev *pdev, > atomic_set(&qdev->lb_count, 0); > cards_found++; > return 0; > + > +devlink_free: > + devlink_free(devlink); > + return err; > } The best way to write error handling code is keep tracke of the most recent allocation which was allocated successfully. one = alloc(); if (!one) return -ENOMEM; // <-- nothing allocated successfully two = alloc(); if (!two) { ret = -ENOMEM; goto free_one; // <-- one was allocated successfully // Notice that the label name says what // the goto does. } three = alloc(); if (!three) { ret = -ENOMEM; goto free_two; // <-- two allocated, two freed. } ... return 0; free_two: free(two); free_one: free(one); return ret; In the old code qlge_probe() freed things before returning, and that's fine if there is only two allocations in the function but when there are three or more allocations, then use gotos to unwind. Ideally there would be a ql_deinit_device() function to mirror the ql_init_device() function. The ql_init_device() is staging quality code with leaks and bad label names. It should be re-written to free things one step at a time instead of calling ql_release_all(). Anyway, let's not introduce new leaks at least. regards, dan carpenter