Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2342055pxb; Fri, 5 Feb 2021 15:45:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJzOjzsZvrzR+0LZR3sExP9xZMnXuQbKNY7cDpx9ihj/qg/nNd5S7vWiZjtJVln72uEztLM9 X-Received: by 2002:a17:906:3e42:: with SMTP id t2mr6392957eji.439.1612568724518; Fri, 05 Feb 2021 15:45:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612568724; cv=none; d=google.com; s=arc-20160816; b=IuJZVhRTcKzEDDcGLXhXgRb4BO/nbJqxKv0ixrIiur2X5mJnCv/Fw8E9oTX1Sf5DBe aVVj1K4hL4ibqIo9LVPyiz1cvlbk8H2lszvQ4V2b4X2f62Spodf65yy0ldZ+mEtBWABV drRc8wxmcV6t0FIrlNsNZIEKkPWzg8ZQiN4IcwcZhD8Opi+Pe9s3hWXVEfZqprU9RCna YmyZ89E/KAyefLmRuBEjvXNin+yEMAM/xWKN9+btBaN9Cz8jAKzYs94suBB01Dd4utjL SAkFvvAIrUOGampPBEpp0Vj7NJUnnZTuJhir4j0eJ0eu0xN6iHHOQVpFJaX2vNXhxF3d ITsQ== 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:from:date:dkim-signature; bh=q5eyzLZq3gCKs8Kqw79XkqaQD8/gdoEJh1yzqQxqHt0=; b=XL9uXZi0qTet77LDwT3B8JOeoaz1qo8v1WGwHArHfVKTBEVzVU8DOXN8LKm6THueLp UcuP2tXT0r8ujP5/y/6T6rPlmAFyh1Gq81pc9/yu99jfc1wLp8jfFrWgox187LksCPtH T7YapJQ3kq3oPzSgSLLGTXBQjQ/PGGvqQz/CvHRPlW1RQ8afZ5LQbaJ1zq8cu4yAAMaU QQAlmyI8mPISwAlAYePhoAvair9yFtbevtpIk/BPTbN6JxXML6PsijPDaZcTvozKPttr cadWwXlj/h0KGcT09slyxl/Tce0Y2ToPcnQPmsWAyLgRPiVp3xitlp3Dl0t1++nKI/N2 ilPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=ZEWUNFja; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s10si3477040edw.18.2021.02.05.15.45.00; Fri, 05 Feb 2021 15:45:24 -0800 (PST) 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=@ziepe.ca header.s=google header.b=ZEWUNFja; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230210AbhBEXlt (ORCPT + 99 others); Fri, 5 Feb 2021 18:41:49 -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-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