From patchwork Thu Nov 10 15:25:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 18167 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp212091wru; Thu, 10 Nov 2022 07:29:02 -0800 (PST) X-Google-Smtp-Source: AMsMyM5DGgCMePi5WYhZ6sVl2SHuBCiIHEBW3LsjoV96mHN1xGBq2ibOdddb0QU3bdYb1uPdrQBP X-Received: by 2002:a05:6402:1344:b0:461:8fbf:f607 with SMTP id y4-20020a056402134400b004618fbff607mr2418547edw.235.1668094142398; Thu, 10 Nov 2022 07:29:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094142; cv=none; d=google.com; s=arc-20160816; b=Q8cORmh+/f/xLSW4U4kTKE8RU6ZYr8KZ++wcN2ZDFLoeg1ApMGenqi1xaGLniTFhuK 0ycI6BeiPduQzduYoQnF3BIo8DA/BlxraaVxUKsn6173K1DqX/9ReMFC8DIqR3pK9Tg0 Pm8ytYB27A+L2FqpXvYlxpHUtyNrTV4xWd+vkcw+tfcxZLqbeCoucf4vnp+VfCynE+I5 Pu/EETcKZ8Hq4+LLSCjQzp6MCxPaH7r+16KPsE5hFLkPB+3bI1KFJZZ/4IgNn6XnnVGO DMa+4bQTa3RyOXXHS9+/205+2jo1Pj893N94uum0HpramX6+sHxbiq55Zz17dgl2Eo/4 ULzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:ui-outboundreport:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3KlLEQb7IWTC+S751hHd5VTkqMQyRRQZBkiQMbL6Rjc=; b=VO2t4mkGoX8ODgMVDQM3q9AXiwc0p7o2MXvg2iLZ7omEuyRLBRnjnumo1Y8YDCibFV Lw/jz9P8UG4KaAa0VV+cTQNgew4afgqU4SjN3Bmn2I3uRknSPsIYZqiakgJEyG3AOJ6T JwnZrUl9H/dzZ61JIYcWtzGSx6/9IvNhhY/5HA2TrVYc+ZU1YfLkmMe0M26JvfI1OhFe 83LERb6KatxptWV9pmHuRmYFVdT2yZZ40+/rNWGS2vavxOJuK8tvOdwB+8vEjrlF9pnv Y/NV0eDqw6nvLLQgQQ54Al5I8P1/0UT2nxMH4dvtRsTlgKZ7OZDcgY9OFpWAJAURoZ1f 1nVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=gmyFgNjD; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dz21-20020a0564021d5500b00458d43beac7si19781748edb.41.2022.11.10.07.28.37; Thu, 10 Nov 2022 07:29:02 -0800 (PST) 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; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=gmyFgNjD; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230252AbiKJP0o (ORCPT + 99 others); Thu, 10 Nov 2022 10:26:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231614AbiKJP0U (ORCPT ); Thu, 10 Nov 2022 10:26:20 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65A9F3E080; Thu, 10 Nov 2022 07:26:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093945; bh=3KlLEQb7IWTC+S751hHd5VTkqMQyRRQZBkiQMbL6Rjc=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=gmyFgNjDriHqVPT4KLgTD+F/ig5VEqBJHAgVJx+Tq5aHtNYj9ri/wetSH3DiAXkiX goKHhPTpTiSO1ymAVJbdSVarM2lCEn21lXU48RJz7Tgg31F3XxeQAAoiAaQsVx/4l4 w6uoaf46KknggE2HDkPWuWKcpRCb4I1pExKlod+Nbmg8dvOKGu1Vyor9M2SC0yNqzO hhXl+rAUCU/+Cj0oERKEIG7ALloN56Uh2olLew/eUyaFdf//xaDKfs9ZQjNEeT6Uuk fkJvhgKE3XmPxTuYU0NbJFwP75RyzAKJpIsDLlEdsT3ZywPGjuS53axwvukxofk4Kv ORLhooYdY2fIA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MeU4y-1pRNj31YKi-00aZJR; Thu, 10 Nov 2022 16:25:45 +0100 From: Lino Sanfilippo To: peterhuewe@gmx.de, jarkko@kernel.org, jgg@ziepe.ca Cc: 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, LinoSanfilippo@gmx.de, lukas@wunner.de, p.rosenberger@kunbus.com Subject: [PATCH v9 08/12] tpm, tpm: Implement usage counter for locality Date: Thu, 10 Nov 2022 16:25:29 +0100 Message-Id: <20221110152533.24243-9-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:TVTk0VgrwhN591otFJ9HqLORi9Da94m3kLRXwxfV8hnXxT8NjwS hrptZSUJRDHt3ftVOCF5mnodhgHY///HjyuTruamk44wCEHaH5yAK4gSoOZqN4mCylcZzJz iuhNJjbm2bDeOiGfEZOZzlH+HBsjEXQE0eg4JQmxoyAkxf0WUtP/mfAsHz3b2UzZwse2nJp ayc9YAuuNpotKEJZTLzcQ== UI-OutboundReport: notjunk:1;M01:P0:6Xy56185HPY=;Nnn/ICEXtr9XICdfhGGsCmBWmAJ zrYU52YGvBjihIVMRkXNwTB5CsQdbnja8ywNxCrRIFeyUshAkPxnqO49RAjqx+44cozD3dVKz ka4tBbSXSaBeRQp3mFYbNzxwCA+vEhrA4MZ51JlzkJlDiUogHI9R8DYRrO5+Jx/An8UnC1NWB r3ncEGyJYDA6GyVss3aFXqrYpH/QdwCatamBL27b2Z9llKlolgVBi34rBcaikn5RIdoODpaQK 233t+nI8Ewb9S882oD7Sov8DP7TNyfGdlqVMVP+9h7u/JmD3MOnBscBctAC2qVPohgwOC8pRK /ItyeEfxOpbDgfOIdJY75rAxh/hTWen14Dlw7PPehCGKvKhrAwPh/SbbXQGWZsv3oxGarEN3C jfkzdzccf1HP5aZP3Z7ZBC3sr7l044JP36FLRqvWgm5241koArQNcHHlxyrXHwu42ilGg5dmG BJWbrz617t37TCj/jAoKClWHQ59RMCCk+drB0CAEFMO8DYwPK2kmU3ltdcBqa3TZDFS+h2JUT WcAnTyHEHhSulkJ34cIw+dsKnfW8syQNN3EJ4i/zrzOHkfn5HhqiEpbfdRTX/si/EQDFXlV3R dVvna6qfN/ZYLAVcRl105rbSLDAlRC3yMQqkb2OH3JXJyQavPj4R3iXzs0LYtiBb+nmicoirt grQLy0Ik+PxE2rumWTGoUsDHPIazswPaxp/cepTIdIFrIgN63HrIlVVEUKC1oQscwbM4CMAlH hcu3HM7XeIWksqRh+X4E+3KmE40JSQfyTddPJs/Vw/+MoflFaKCsMyaQNx890zSa+oHgOYUvj PcIKIHUUNAganRixL6iA2k99cC8GxiTZinvLhBgVeDdXILG5pXgXUA4hzOzqJUO0w8lKaH1cf JGNaH5vGE8OA66ovG9YNXISt4AOwKS6pmwkNwTtCaBE6K53AcYqa1OHK+c0ZGXa4tdf2J2VRw uBW0038w2T/PRBl9HFp3N+96h9c= X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123483514580184?= X-GMAIL-MSGID: =?utf-8?q?1749123483514580184?= From: Lino Sanfilippo 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. Since in case of SPI the register accesses are locked by means of the SPI bus mutex use a sleepable lock (i.e. also a mutex) to ensure thread-safety of the counter which may be accessed by both a userspace thread and the interrupt handler. By doing this refactor the names of the amended functions to use a more appropriate prefix. Signed-off-by: Lino Sanfilippo Tested-by: Michael Niewöhner --- drivers/char/tpm/tpm_tis_core.c | 75 ++++++++++++++++++++++----------- drivers/char/tpm/tpm_tis_core.h | 2 + 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index 4336f7ea8c2b..ecde684a8f80 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -165,16 +165,27 @@ static bool check_locality(struct tpm_chip *chip, int l) return false; } -static int release_locality(struct tpm_chip *chip, int l) +static int __tpm_tis_relinquish_locality(struct tpm_tis_data *priv, int l) +{ + tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY); + + return 0; +} + +static int tpm_tis_relinquish_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_relinquish_locality(priv, l); + mutex_unlock(&priv->locality_count_mutex); return 0; } -static int request_locality(struct tpm_chip *chip, int l) +static int __tpm_tis_request_locality(struct tpm_chip *chip, int l) { struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); unsigned long stop, timeout; @@ -215,6 +226,20 @@ static int request_locality(struct tpm_chip *chip, int l) return -1; } +static int tpm_tis_request_locality(struct tpm_chip *chip, int l) +{ + struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); + int ret = 0; + + mutex_lock(&priv->locality_count_mutex); + if (priv->locality_count == 0) + ret = __tpm_tis_request_locality(chip, l); + if (!ret) + priv->locality_count++; + mutex_unlock(&priv->locality_count_mutex); + return ret; +} + static u8 tpm_tis_status(struct tpm_chip *chip) { struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); @@ -682,7 +707,7 @@ static int probe_itpm(struct tpm_chip *chip) if (vendor != TPM_VID_INTEL) return 0; - if (request_locality(chip, 0) != 0) + if (tpm_tis_request_locality(chip, 0) != 0) return -EBUSY; rc = tpm_tis_send_data(chip, cmd_getticks, len); @@ -703,7 +728,7 @@ static int probe_itpm(struct tpm_chip *chip) out: tpm_tis_ready(chip); - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } @@ -762,7 +787,7 @@ static int tpm_tis_gen_interrupt(struct tpm_chip *chip) cap_t cap; int ret; - ret = request_locality(chip, 0); + ret = tpm_tis_request_locality(chip, 0); if (ret < 0) return ret; @@ -771,7 +796,7 @@ static int tpm_tis_gen_interrupt(struct tpm_chip *chip) else ret = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc, 0); - release_locality(chip, 0); + tpm_tis_relinquish_locality(chip, 0); return ret; } @@ -796,33 +821,33 @@ static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask, } priv->irq = irq; - rc = request_locality(chip, 0); + rc = tpm_tis_request_locality(chip, 0); if (rc < 0) return rc; rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality), &original_int_vec); if (rc < 0) { - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq); if (rc < 0) { - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status); if (rc < 0) { - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } /* Clear all existing */ rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status); if (rc < 0) { - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } @@ -830,11 +855,11 @@ static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask, rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask | TPM_GLOBAL_INT_ENABLE); if (rc < 0) { - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); clear_bit(TPM_TIS_IRQ_TESTED, &priv->flags); /* Generate an interrupt by having the core call through to @@ -970,8 +995,8 @@ static const struct tpm_class_ops tpm_tis = { .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, .req_canceled = tpm_tis_req_canceled, - .request_locality = request_locality, - .relinquish_locality = release_locality, + .request_locality = tpm_tis_request_locality, + .relinquish_locality = tpm_tis_relinquish_locality, .clk_enable = tpm_tis_clkrun_enable, }; @@ -1005,6 +1030,8 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, priv->timeout_min = TPM_TIMEOUT_USECS_MIN; priv->timeout_max = TPM_TIMEOUT_USECS_MAX; priv->phy_ops = phy_ops; + priv->locality_count = 0; + mutex_init(&priv->locality_count_mutex); dev_set_drvdata(&chip->dev, priv); @@ -1083,14 +1110,14 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, intmask &= ~TPM_GLOBAL_INT_ENABLE; - rc = request_locality(chip, 0); + rc = tpm_tis_request_locality(chip, 0); if (rc < 0) { rc = -ENODEV; goto out_err; } tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); - release_locality(chip, 0); + tpm_tis_relinquish_locality(chip, 0); rc = tpm_chip_start(chip); if (rc) @@ -1124,13 +1151,13 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, * proper timeouts for the driver. */ - rc = request_locality(chip, 0); + rc = tpm_tis_request_locality(chip, 0); if (rc < 0) goto out_err; rc = tpm_get_timeouts(chip); - release_locality(chip, 0); + tpm_tis_relinquish_locality(chip, 0); if (rc) { dev_err(dev, "Could not get TPM timeouts and durations\n"); @@ -1150,11 +1177,11 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, dev_err(&chip->dev, FW_BUG "TPM interrupt not working, polling instead\n"); - rc = request_locality(chip, 0); + rc = tpm_tis_request_locality(chip, 0); if (rc < 0) goto out_err; disable_interrupts(chip); - release_locality(chip, 0); + tpm_tis_relinquish_locality(chip, 0); } } @@ -1221,13 +1248,13 @@ int tpm_tis_resume(struct device *dev) * an error code but for unknown reason it isn't handled. */ if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) { - ret = request_locality(chip, 0); + ret = tpm_tis_request_locality(chip, 0); if (ret < 0) return ret; tpm1_do_selftest(chip); - release_locality(chip, 0); + tpm_tis_relinquish_locality(chip, 0); } return 0; diff --git a/drivers/char/tpm/tpm_tis_core.h b/drivers/char/tpm/tpm_tis_core.h index 2deef11c88db..13bdcf38e56f 100644 --- a/drivers/char/tpm/tpm_tis_core.h +++ b/drivers/char/tpm/tpm_tis_core.h @@ -91,6 +91,8 @@ enum tpm_tis_flags { struct tpm_tis_data { u16 manufacturer_id; + struct mutex locality_count_mutex; + unsigned int locality_count; int locality; int irq; unsigned int int_mask;