From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37E57C433E0 for ; Fri, 5 Feb 2021 23:42:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CD7B864FF8 for ; Fri, 5 Feb 2021 23:42:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230469AbhBEXmT (ORCPT ); Fri, 5 Feb 2021 18:42:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232537AbhBEOUK (ORCPT ); Fri, 5 Feb 2021 09:20:10 -0500 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72A48C06121F for ; Fri, 5 Feb 2021 07:58:10 -0800 (PST) Received: by mail-qk1-x735.google.com with SMTP id x81so7357411qkb.0 for ; Fri, 05 Feb 2021 07:58:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=q5eyzLZq3gCKs8Kqw79XkqaQD8/gdoEJh1yzqQxqHt0=; b=ZEWUNFjaacXrMffIa+tWBci8iY9Dj/FxUUXeBdqAu2KvjWWVuxjTwyNuZihnDEFXPF rHLdpEmbtxPLlxdTercBuvHfpTHV+RyH3DwxMpk/eBXg8TCVwevSt60qABU9a++u1QWw ixshWfeoK5Is5BrPf+qqDVyp5mTATbijtMgYZuCmKsNG9wOFnAn7Rn9GXSI1uhfOsfBr H646r6BwHCa9FS0WxX6Xf85kzWHo4lIGiygp2Fpv1PX5EnL9CqDu5pFUxF6V4Dvs85e5 4fBmkGaVaKvnHn6uqelFKNvVi+Cybsi64bcoX7tdF7DaKX4hoAQbowhSbHc7+U6tvW3b 7vNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=q5eyzLZq3gCKs8Kqw79XkqaQD8/gdoEJh1yzqQxqHt0=; b=IhZb/yobdz0VYE9FOsBUfiZMVUS/b/wJGrbf8pC7l4NMXcIHxNq8E6MYpkPYruDMbb IS0E/HmLugC3sHSO4a2K/DGR4fEnr7J1qVRwC9K3CaRhfgUPk9LFB6eCcq3xICedUKsZ uObzBoN5iewaY1tGr0KFfq9IwAdWHFmoKZg4fbzJa/CQBANyZByIBhd2mZhGLnVJnCKG rQm6HDvqNLcUJhUvUVut9KRBpSFboE9Zd4nvXWJ6k4EY8CfaiForbxDoCX3nZy6PzEvP kU/88wp+WrhI6QW1rWshQMIiV39T2DPmXVeMTUWEnA9iuaOTxRQOtlom66ZsySr3B2JL LkeQ== X-Gm-Message-State: AOAM530GIHkZS2Dvz+UrGcTSIXpwGU/1EDM284BrguTQR5Gi/eGTr+Xe +dj8vtANzg0EmYwAzOQGFnAH5gbdbKasaJu/ X-Google-Smtp-Source: ABdhPJyA7K4oUWNeCskRU6Pypei9zC3tndf7O8W2PUr0OOndWQlgAUmypXMY9EdbNg2wmLUYeRpk9Q== X-Received: by 2002:a37:a053:: with SMTP id j80mr5100519qke.198.1612540689687; Fri, 05 Feb 2021 07:58:09 -0800 (PST) Received: from ziepe.ca (hlfxns017vw-142-162-115-133.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.162.115.133]) by smtp.gmail.com with ESMTPSA id b16sm8170352qtx.85.2021.02.05.07.58.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Feb 2021 07:58:09 -0800 (PST) Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1l83Um-003tDI-IH; Fri, 05 Feb 2021 11:58:08 -0400 Date: Fri, 5 Feb 2021 11:58:08 -0400 From: Jason Gunthorpe To: Lino Sanfilippo Cc: Lino Sanfilippo , peterhuewe@gmx.de, jarkko@kernel.org, stefanb@linux.vnet.ibm.com, James.Bottomley@hansenpartnership.com, stable@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 1/2] tpm: fix reference counting for struct tpm_chip Message-ID: <20210205155808.GO4718@ziepe.ca> References: <1612482643-11796-1-git-send-email-LinoSanfilippo@gmx.de> <1612482643-11796-2-git-send-email-LinoSanfilippo@gmx.de> <20210205130511.GI4718@ziepe.ca> <3b821bf9-0f54-3473-d934-61c0c29f8957@kunbus.com> <20210205151511.GM4718@ziepe.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 05, 2021 at 04:50:13PM +0100, Lino Sanfilippo wrote: > > On 05.02.21 16:15, Jason Gunthorpe wrote: > > > > No, the cdev layer holds the refcount on the device while open is > > being called. > > > Yes, but the reference that is responsible for the chip deallocation is chip->dev > which is linked to chip->cdev and represents /dev/tpm, not /dev/tpmrm. > You are right, we dont have the issue with /dev/tpm for the reason you mentioned. > But /dev/tpmrm is represented by chip->cdevs and keeping this ref held by the cdev > layer wont protect us from the chip being freed (which is the reason why we need > the chip->dev reference in the first place). No, they are all chained together because they are all in the same struct: struct tpm_chip { struct device dev; struct device devs; struct cdev cdev; struct cdev cdevs; dev holds the refcount on memory, when it goes 0 the whole thing is kfreed. The rule is dev's refcount can't go to zero while any other refcount is != 0. For instance devs holds a get on dev that is put back only when devs goes to 0: static void tpm_devs_release(struct device *dev) { struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs); /* release the master device reference */ put_device(&chip->dev); } Both cdev elements do something similar inside the cdev layer. The net result is during any open() the tpm_chip is guarenteed to have a positive refcount. Jason