Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp5525204rwi; Mon, 17 Oct 2022 23:41:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM51bAok6Sn67MJ/W1wzll37f17cWRhDK7dQuOu+tlRG564opiszHmn7vaCCzs2D85/gSgie X-Received: by 2002:a17:907:7f91:b0:78e:2e30:9513 with SMTP id qk17-20020a1709077f9100b0078e2e309513mr1114817ejc.759.1666075296325; Mon, 17 Oct 2022 23:41:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666075296; cv=none; d=google.com; s=arc-20160816; b=jM7xjbgKzFrYZXtxWqORiVkUq+eXE4pVLlN4+G8TDx5rM2msRColQ1+Le1DQKXnaWi qgRarLwMO6z4ICX09Vi0mzUTHbgN5WVa9OiNkYeyQCNQRnWyuxf5qS2813bS4Dd0kA0I IoUszxzKpLYIhNua5IlCdZuZLmmzbEH1lhkrFhdj4d9//S2uQo9hHq3p+Y43J35xEdK8 wm6f/5wNMSUqYBkQVBo/34JsNe6ZH2MBoB2YTvRiKtpRDSTSjL6qyqzVmtBKOJYtvmwi 9PMn+vlVMVwM/YiSAEJNvNV2GlfIy7fAuty3dHFRX9+NdN2P7DVWDluzrdVglk91GGjr jT9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=EQO7ffQfQUVFiKB1IMrLc0KgPrGLkgZgerf0h8+vjUU=; b=IVK1AUY/cET6+Tbm9uTmfmalfYUT9IMh//2OuMxDNn11NLWeKGolTEMwxksX96zx4q SMcGtwqyVU8hYLC4nduM1TCQe/kL2KrcB1fROKaymmc6WUiDtZKQdcMDC4rpQVWZDFQN cVJQQTK8qnuqF4WXi1EY1a1bL3BK8fcBLpBtoGZAR18Lt1wlDziIDaHaEMK8IwR66oBb pD7C/hHcjmd9J9bpQbi8BYCDFkC+BLtZhb73evqx8jPCv+uTlerH0QyjgV4PW3L7Vlj2 JGk4Fu3RDySl9huxmFYwr96zoAhgQXl8rza81cyqVQWiQYeTcEstfI3qlQTdO1yBHhTN q44Q== 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:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p11-20020a056402500b00b0045d212cc170si10489617eda.363.2022.10.17.23.41.09; Mon, 17 Oct 2022 23:41:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229755AbiJRGZT (ORCPT + 99 others); Tue, 18 Oct 2022 02:25:19 -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) X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 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