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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3C7FC433FE for ; Tue, 18 Oct 2022 06:25:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230153AbiJRGZU (ORCPT ); Tue, 18 Oct 2022 02:25:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230166AbiJRGZP (ORCPT ); Tue, 18 Oct 2022 02:25:15 -0400 Received: from bmailout1.hostsharing.net (bmailout1.hostsharing.net [IPv6:2a01:37:1000::53df:5f64:0]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 530305F7F; Mon, 17 Oct 2022 23:25:10 -0700 (PDT) Received: from h08.hostsharing.net (h08.hostsharing.net [83.223.95.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "*.hostsharing.net", Issuer "RapidSSL TLS DV RSA Mixed SHA256 2020 CA-1" (verified OK)) by bmailout1.hostsharing.net (Postfix) with ESMTPS id 8748530024DBD; Tue, 18 Oct 2022 08:25:08 +0200 (CEST) Received: by h08.hostsharing.net (Postfix, from userid 100393) id 6AB8A61F9A; Tue, 18 Oct 2022 08:25:08 +0200 (CEST) Date: Tue, 18 Oct 2022 08:25:08 +0200 From: Lukas Wunner To: Lino Sanfilippo Cc: peterhuewe@gmx.de, jarkko@kernel.org, jgg@ziepe.ca, stefanb@linux.vnet.ibm.com, linux@mniewoehner.de, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, jandryuk@gmail.com, pmenzel@molgen.mpg.de, l.sanfilippo@kunbus.com, p.rosenberger@kunbus.com Subject: Re: [PATCH v8 08/11] tpm, tpm: Implement usage counter for locality Message-ID: <20221018062508.GB25237@wunner.de> References: <20221017235732.10145-1-LinoSanfilippo@gmx.de> <20221017235732.10145-9-LinoSanfilippo@gmx.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221017235732.10145-9-LinoSanfilippo@gmx.de> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 18, 2022 at 01:57:29AM +0200, Lino Sanfilippo wrote: > Implement a usage counter for the (default) locality used by the TPM TIS > driver: > Request the locality from the TPM if it has not been claimed yet, otherwise > only increment the counter. Also release the locality if the counter is 0 > otherwise only decrement the counter. Ensure thread-safety by protecting > the counter with a mutex. > > This allows to request and release the locality from a thread and the > interrupt handler at the same time without the danger to interfere with > each other. [...] > +static int tpm_tis_release_locality(struct tpm_chip *chip, int l) > { > struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); > > - tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY); > + mutex_lock(&priv->locality_count_mutex); > + priv->locality_count--; > + if (priv->locality_count == 0) > + tpm_tis_release_locality_locked(priv, l); > + mutex_unlock(&priv->locality_count_mutex); > > return 0; > } Hm, any reason not to use struct kref for the locality counter? Provides correct memory ordering (no mutex needed) and allows for calling a release function too upon reaching 0. Thanks, Lukas