Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1826913imm; Tue, 22 May 2018 09:57:28 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo8nrmP1u8MEtI8ONlZoZ2/jK0huWiJ3vO43eSwxMbak2gLhnNSKQnnWEnsB/X2Jd/lmujY X-Received: by 2002:a17:902:268:: with SMTP id 95-v6mr25712060plc.386.1527008248537; Tue, 22 May 2018 09:57:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527008248; cv=none; d=google.com; s=arc-20160816; b=0YfE+CkqTXYHgYdsGBQ3PbED6Fm05+m/eGnGb+ZvjWPIjvmnA+qcflMOU5Wi/LcZrX iAmmgmRKKxLYnrlN6sOtB5t0++agx3UHZzGiaOxDzYsYt0Z7Pcnj2TGDbxNQ0OnVOaN0 u0FTp8zhxWMmPxggxdO3tuukJ8F5Q2e6XrdZQaEStJtHKygzsWqWSGvFVHP8ylKHS4xe Wy3QlZvue0CFclMYhE2kBiJ6RwxKxwFOpWZ2E916U9wGevp0t3PeoZQvReRXUFXXXhi+ LotsUc4uxlIpkhqkC001DQjaVJknDgNZ4Z5oeRiDBFoyym4rui5dfWZnuxyRRsDwog8X 2m4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:arc-authentication-results; bh=yRMnrM3iyd13gmkXmfrfhcAo4e3y8kSnv9wiITu3pnc=; b=yD9pcuX0DExV1Vkox61JbCw1sSU7g1kW4e8wtYfEQZbVYXD23Ca6lMD1oci/sZhIfO 7+J8OuWbNYGfGpmRf+bQ9jOf009b62kIsBJH/JzSGxUZRUYZNpcDmkyg7rB3tzA/Lgt8 GYxCHUumHUClgt3bIDnJOGjvrVcTvlPOQ4R5vSPrA4avJWUK5v+zht2u0uBKnNHLPHGq xtRbp16xSFClFI3ZLobIc6F+E95MBypwLoRceBEpLWwrFDk55Y2FNryXoh6iXmAkH9yk cENjts7rhWziKj1udi2x+cpzKtugeXe/i0D7K6zpqg40t9jBME74lg/XoH8PfjPCa1ur lbUA== ARC-Authentication-Results: i=1; mx.google.com; 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 u81-v6si17010775pfg.289.2018.05.22.09.57.13; Tue, 22 May 2018 09:57:28 -0700 (PDT) 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; 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 S1751527AbeEVQ47 (ORCPT + 99 others); Tue, 22 May 2018 12:56:59 -0400 Received: from ale.deltatee.com ([207.54.116.67]:48280 "EHLO ale.deltatee.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751268AbeEVQ4y (ORCPT ); Tue, 22 May 2018 12:56:54 -0400 Received: from guinness.priv.deltatee.com ([172.16.1.162]) by ale.deltatee.com with esmtp (Exim 4.89) (envelope-from ) id 1fLAai-000131-4S; Tue, 22 May 2018 10:56:53 -0600 To: Jon Mason , Arvind Yadav Cc: dave.jiang@intel.com, allenbh@gmail.com, linux-kernel@vger.kernel.org, linux-ntb@googlegroups.com References: <6717325ea580df9a0294db6f83fd72a5db3daad9.1520591515.git.arvind.yadav.cs@gmail.com> <20180522004846.GC3255@kudzu.us> From: Logan Gunthorpe Message-ID: Date: Tue, 22 May 2018 10:56:51 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20180522004846.GC3255@kudzu.us> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-SA-Exim-Connect-IP: 172.16.1.162 X-SA-Exim-Rcpt-To: linux-ntb@googlegroups.com, linux-kernel@vger.kernel.org, allenbh@gmail.com, dave.jiang@intel.com, arvind.yadav.cs@gmail.com, jdmason@kudzu.us X-SA-Exim-Mail-From: logang@deltatee.com X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on ale.deltatee.com X-Spam-Level: X-Spam-Status: No, score=-7.7 required=5.0 tests=ALL_TRUSTED,BAYES_00, GREYLIST_ISWHITE,LR_URI_NUMERIC_ENDING,MYRULES_FREE autolearn=ham autolearn_force=no version=3.4.1 Subject: Re: [PATCH] ntb_transport: use put_device() instead of kfree() X-SA-Exim-Version: 4.2.1 (built Tue, 02 Aug 2016 21:08:31 +0000) X-SA-Exim-Scanned: Yes (on ale.deltatee.com) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 21/05/18 06:48 PM, Jon Mason wrote: > On Fri, Mar 09, 2018 at 04:03:24PM +0530, Arvind Yadav wrote: >> Never directly free @dev after calling device_register(), even >> if it returned an error! Always use put_device() to give up the >> reference initialized. >> >> Signed-off-by: Arvind Yadav >> --- >> drivers/ntb/ntb_transport.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c >> index 9878c48..8182a3a 100644 >> --- a/drivers/ntb/ntb_transport.c >> +++ b/drivers/ntb/ntb_transport.c >> @@ -393,7 +393,7 @@ int ntb_transport_register_client_dev(char *device_name) >> >> rc = device_register(dev); >> if (rc) { >> - kfree(client_dev); >> + put_device(dev); > > Now we are leaking client_dev, which is bigger than just dev. I think > we are going to need both now. No, when put_device is called, ntb_transport_client_release() will be called which then kfree's the structure there. This is the preferred way of freeing anything that's reference counted (as all struct devices are). See [1]. Though, if I remember correctly, the NTB tree breaks this rule a lot. ie. the fact that ntb_dev_release() doesn't actually free the underlying structure has always irked me a bit because it forces the calling drivers to break this rule. This means the reference counting on the NTB devices is broken so if someone ever uses get_device() on an struct ntb_dev and doesn't put it back before the driver is unbound, there will be a use after free bug. As far as I know though, at this time this isn't done. Logan [1] https://elixir.bootlin.com/linux/v4.17-rc6/source/drivers/base/core.c#L1931