Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp110664pxu; Wed, 14 Oct 2020 22:07:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJztqjhOt1x6CzGXc08e9jOYomOmuI0PVoMf4wA3dsg5SKHhgG6IYRRWZ4as+h24gvPV3S7B X-Received: by 2002:a17:906:1150:: with SMTP id i16mr2591249eja.82.1602738441967; Wed, 14 Oct 2020 22:07:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602738441; cv=none; d=google.com; s=arc-20160816; b=Nk2+eGVbrRbXk8HNsEeD7siRHO0KP7NsQoT6IzuFt83PS7g3dAFt6zneI+2rwraBAv Cey19nvwzDdasHmTaCYsdN1OpNPWleX2z1G/HzPH82yhjo0uhO4+dyPfaa2PQMQ9ZEuE agWM7PxvdQ7OB6XorUVqUz1W2k5ki2TrkRVtToNEsONilPQvJzKxAcDblIpme8YiqXba NPsBqbmD7sgXL0EI4tDyaMcaF7VkOztE2ihIeYtB21ZNaU08AvnuFtI8o+OZ0wwu3gUU TtX/kTjECzVnZZtghrUY7owi+T4FzBrGrKRstjfCsIJqyJ14LBANiq1emUEbHXOo7AWB 2FUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:date:from:dkim-signature; bh=KmLWiTcOmmjLTrrn22jVJ7/COvI0q5pT3DXtKHVvFIE=; b=ul/pIFtb5Zvpg2xQ4ph0LyrtbCCBR35MmQ/GzBQf6MivPUHYhR4yE6ltzGKYm7izDT Hm4L5U+QC1v6LWRTbZb0eLdXdFDjmgtVjHo8IiPq4CrOVMt5v48ZUBDzJsidh+AqbHyA 7NyrDKu8j1KTkOQTK22JKStf4yRdSHhfZueYGITuxaoop3f2qsY197UU8D+K26oCQhiu DezFwcMC8hFwG4FNf0In/Sgn9MVSpEBDE4HYX4S09uy6VF6LCJRJhJy3aabQgkx7ejz5 cGvw3k48AH8Ey+/TFLuhl9rQ/TYpuP/JZrn6wIPWzTWIJZ1iQPVxjr+IQ58NWU3AmFtq B6dg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ijgJr+7F; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r17si1097329edo.383.2020.10.14.22.06.59; Wed, 14 Oct 2020 22:07:21 -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=@gmail.com header.s=20161025 header.b=ijgJr+7F; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728420AbgJOE0t (ORCPT + 99 others); Thu, 15 Oct 2020 00:26:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725208AbgJOE0t (ORCPT ); Thu, 15 Oct 2020 00:26:49 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60824C061755; Wed, 14 Oct 2020 21:26:49 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id k8so1216625pfk.2; Wed, 14 Oct 2020 21:26:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:date:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=KmLWiTcOmmjLTrrn22jVJ7/COvI0q5pT3DXtKHVvFIE=; b=ijgJr+7FId4/CSIH0DhfVqvliBZfLgMpxavEJhtmQyKe9XhvGbVbvKBvUQYqiJMn7m qm+7+49Dxk4/H93QEbq8xiavqCI+D69P58TdgaO5nTnymMNpfJLXpADSOtLDHNEyP6yo XW86eNoxcWAyIKXjxAkKXuoElVXnLZw0bHTWLtU8N4el9qiyzZipm5DJH6+HNG8nuYYL Tl/46pz7XAdDDIeyDvVCa5XHrClmKjCuZ2GtrB8wvqiHaOh64Lx+mbVq03v2EJzxLeuG 5f2okgLxmC7/FfB4KQyg0jceESNdLFqg2GE8BmsGC5GX97UdYE2ce5iFso207Tl8BU5N zaNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=KmLWiTcOmmjLTrrn22jVJ7/COvI0q5pT3DXtKHVvFIE=; b=F9RZll/Vy1LME86mOWKVuRHWzmzM3kshBmaX1bDpH+7ZtOe/RVhqgeueCT2y4dAywj gIfbtxEXWhI1BTBEd9w4C7OOXuxhcFO04lvjoEidb9MX7ICWyvzuIIcTr/SPsakFjHHF Zh18TkBA8QNBadBe9Spm949V5H0RokvquDw8UPuQ8PMtc+5t54bU9I8gkoPM5FIfyl2T qonhVUxspkLHYx2utrJxWhL34DHfu9bPEMpq7jUfaQauDCSgqcgNu2X/y3ryRDOaq7X+ uUEcVDc15dJTuNVYMBZ+d6HEqO6jcVuY2RCEzZeJCtUq0LBYHnP6tcL08RKsnkY3c3ia EiYA== X-Gm-Message-State: AOAM531WjU7HQrskBckzWFbrTbOzcdT63JUxjGZWZowm+x5RqUrSnB4N kdOu5TPQyhv9I1q29N0ixfw= X-Received: by 2002:a63:df42:: with SMTP id h2mr1927869pgj.239.1602736008980; Wed, 14 Oct 2020 21:26:48 -0700 (PDT) Received: from localhost ([2001:e42:102:1532:160:16:113:140]) by smtp.gmail.com with ESMTPSA id g14sm1346060pfo.17.2020.10.14.21.26.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Oct 2020 21:26:48 -0700 (PDT) From: Coiby Xu X-Google-Original-From: Coiby Xu Date: Thu, 15 Oct 2020 12:22:08 +0800 To: Dan Carpenter 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: <20201015042208.otne7sizy2bj2on6@Rk> References: <20201014104306.63756-1-coiby.xu@gmail.com> <20201014104306.63756-3-coiby.xu@gmail.com> <20201014130846.GU1042@kadam> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20201014130846.GU1042@kadam> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Oct 14, 2020 at 04:08:46PM +0300, Dan Carpenter wrote: >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. Thank you for spotting this issue! > >> >> - 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; > Thank you for teaching me this pattern! >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(). > I'll see how I can improve ql_init_device. Thank you for the suggestion! >Anyway, let's not introduce new leaks at least. > >regards, >dan carpenter -- Best regards, Coiby