Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp922136pxp; Wed, 16 Mar 2022 21:24:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzV/uLJ3MNQBJQToS+Ia+55n5HpiZ8eGmJKKpKN+Pu36W1mIgghpAVhd1DJ/5EZCsXB02+0 X-Received: by 2002:a17:90a:5b0d:b0:1bc:7e66:2970 with SMTP id o13-20020a17090a5b0d00b001bc7e662970mr3211725pji.12.1647491082472; Wed, 16 Mar 2022 21:24:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647491082; cv=none; d=google.com; s=arc-20160816; b=C4LKZ+5mCyKZ+uGKbBzxR6XP3ZQkNZRJzp6GImqOCABYUOrR2f1P5JG8LtE58VeU8y fw6zz7LJym8osMcqRmCqOggtJJCFVOa/zQd+jiLBBjCW+vyDoj8JIbDTCj22HK0CXWDr gyxoa0/lIprkA7gxuFYQiNtcPHQ+dqEfKNlb0LAPUFdDZh8gBPae2ndsGqQwnr9MUBFx pJkaJPot5g4AQgCaI/IVX76CCg9Ql1nV9cf5LFKfO4WTmB+Hkjv1Du6Mw8VrFCPUwR4A b2asm/onw2h6qJwto0jJpGiG3lO4g16l+sojDiOrl8rY2J3ggJnOoz2ipPqWkDhXEcyw KWTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=sjMIKNUZa2aOQc5kBuhU6QwzJOYDQtfDwB/hLrjFXTQ=; b=xK3Fy/s7I1YZv54DM2K41OeA4vTDHVWD7I9MkGUEj5JAPxaiWukvWLwnv3LF2B/7PO 1UEZBAuHYxLXrlSq17IeZTelDTqJ4jQaM5D2urF+ni+bylwxlBYxFWBcatRgT34aj6ja eohyYzkAOiYGmzWY3TbZ336smK0syDPbsro+fVFjJrWvY4fE1XpBroyI0mx2sAgnWDei aF9h/XiEZdKUO7zmGj3ydQqiO3ELUxtvtEheMgZJpDVx+2+orBLuO1xrQxsaUnh9i+EB iwRQaZcztLMto091EJVZEEEe8rA+j7x9zu6EMv4MWTZ9r9oUMiQIVkZAP9poJuB1Mgwz P+JA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id e16-20020a635450000000b003816043f0e2si941865pgm.727.2022.03.16.21.24.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 21:24:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0C651EA777; Wed, 16 Mar 2022 20:57:22 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344585AbiCODMr (ORCPT + 99 others); Mon, 14 Mar 2022 23:12:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233712AbiCODMo (ORCPT ); Mon, 14 Mar 2022 23:12:44 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B82DF48324; Mon, 14 Mar 2022 20:11:32 -0700 (PDT) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4KHdgc0z6DzfZ1w; Tue, 15 Mar 2022 11:10:04 +0800 (CST) Received: from [10.67.110.173] (10.67.110.173) by dggpemm500024.china.huawei.com (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 15 Mar 2022 11:11:30 +0800 Message-ID: <489ef3a4-2f9b-f773-f6bf-90199f995e67@huawei.com> Date: Tue, 15 Mar 2022 11:11:30 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.6.0 Subject: Re: [PATCH] tpm: Fix memory leak in tpmm_chip_alloc Content-Language: en-US To: Jarkko Sakkinen CC: , , , , , , References: <20220307104827.40843-1-guozihua@huawei.com> <22e6a4b4b01a219282e4da6a941eaad68897841f.camel@kernel.org> From: "Guozihua (Scott)" In-Reply-To: <22e6a4b4b01a219282e4da6a941eaad68897841f.camel@kernel.org> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.67.110.173] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500024.china.huawei.com (7.185.36.203) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/3/12 0:34, Jarkko Sakkinen wrote: > On Thu, 2022-03-10 at 11:33 +0800, Guozihua (Scott) wrote: >> >> >> On 2022/3/7 21:45, Jarkko Sakkinen wrote: >>> On Mon, Mar 07, 2022 at 06:48:27PM +0800, GUO Zihua wrote: >>>> Fix a memory leak in tpmm_chip_alloc. devm_add_action_or_reset would >>>> call put_device on error, while tpm->devs is left untouched. Call >>>> put_device on tpm->devs as well if devm_add_action_or_reset returns an >>>> error. >>>> >>>> Fixes: fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm") >>>> Signed-off-by: GUO Zihua >>>> --- >>>>   drivers/char/tpm/tpm-chip.c | 13 +++++++++---- >>>>   1 file changed, 9 insertions(+), 4 deletions(-) >>>> >>>> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c >>>> index b009e7479b70..0a92334e8c40 100644 >>>> --- a/drivers/char/tpm/tpm-chip.c >>>> +++ b/drivers/char/tpm/tpm-chip.c >>>> @@ -308,6 +308,12 @@ static int tpm_class_shutdown(struct device *dev) >>>>         return 0; >>>>   } >>>> >>>> +static void tpm_chip_free(struct tpm_chip *chip) >>>> +{ >>>> +       put_device(&chip->devs); >>>> +       put_device(&chip->dev); >>>> +} >>>> + >>>>   /** >>>>    * tpm_chip_alloc() - allocate a new struct tpm_chip instance >>>>    * @pdev: device to which the chip is associated >>>> @@ -396,8 +402,7 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev, >>>>         return chip; >>>> >>>>   out: >>>> -       put_device(&chip->devs); >>>> -       put_device(&chip->dev); >>>> +       tpm_chip_free(chip); >>>>         return ERR_PTR(rc); >>>>   } >>>>   EXPORT_SYMBOL_GPL(tpm_chip_alloc); >>>> @@ -420,8 +425,8 @@ struct tpm_chip *tpmm_chip_alloc(struct device *pdev, >>>>                 return chip; >>>> >>>>         rc = devm_add_action_or_reset(pdev, >>>> -                                     (void (*)(void *)) put_device, >>>> -                                     &chip->dev); >>>> +                                     (void (*)(void *)) tpm_chip_free, >>>> +                                     chip); >>>>         if (rc) >>>>                 return ERR_PTR(rc); >>>> >>>> -- >>>> 2.17.1 >>>> >>> >>> Please test against the latest in >>> >>> git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd.git >>> >>> and share your results. >>> >>> BR, Jarkko >>> . >> >> Hi Jarkko, >> >> I'll do that. Do we have a test set for TPM? Or do we just build and run >> it and see if everything works as expected? >> >> This is an error handling optimization BTW. > > There is kselftest in tools/testing/kselftes/tpm2 that you can use > but do not have to. > > BR, Jarkko > > Hi Jarkko, The code on the master branch on git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd.git seems to be quite different from what I saw on linux upstream. Namely tpm_chip->devs does not exist on linux-tpmdd. Has this member been deleted? -- Best GUO Zihua