From patchwork Fri Dec 30 18:46:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Bottomley X-Patchwork-Id: 709857 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tqwSD1xBfz9sR9 for ; Sat, 31 Dec 2016 05:47:08 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=sfs-ml-3.v29.ch3.sourceforge.com) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1cN2Cn-0002wV-1s; Fri, 30 Dec 2016 18:47:05 +0000 Received: from sog-mx-3.v43.ch3.sourceforge.com ([172.29.43.193] helo=mx.sourceforge.net) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1cN2Cl-0002wO-8L for tpmdd-devel@lists.sourceforge.net; Fri, 30 Dec 2016 18:47:03 +0000 X-ACL-Warn: Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by sog-mx-3.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1cN2Cj-000642-6j for tpmdd-devel@lists.sourceforge.net; Fri, 30 Dec 2016 18:47:03 +0000 Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id uBUIhnog121897 for ; Fri, 30 Dec 2016 13:46:55 -0500 Received: from e34.co.us.ibm.com (e34.co.us.ibm.com [32.97.110.152]) by mx0a-001b2d01.pphosted.com with ESMTP id 27nkr3x7q9-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Fri, 30 Dec 2016 13:46:55 -0500 Received: from localhost by e34.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 30 Dec 2016 11:46:54 -0700 Received: from d03dlp03.boulder.ibm.com (9.17.202.179) by e34.co.us.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 30 Dec 2016 11:46:52 -0700 Received: from b01cxnp22035.gho.pok.ibm.com (b01cxnp22035.gho.pok.ibm.com [9.57.198.25]) by d03dlp03.boulder.ibm.com (Postfix) with ESMTP id 5CC5619D8026 for ; Fri, 30 Dec 2016 11:46:09 -0700 (MST) Received: from b01ledav006.gho.pok.ibm.com (b01ledav006.gho.pok.ibm.com [9.57.199.111]) by b01cxnp22035.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id uBUIkpfJ40501316; Fri, 30 Dec 2016 18:46:51 GMT Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7569DAC043; Fri, 30 Dec 2016 13:46:51 -0500 (EST) Received: from jarvis.ext.hansenpartnership.com (unknown [9.80.199.178]) by b01ledav006.gho.pok.ibm.com (Postfix) with ESMTPS id E6CB0AC046; Fri, 30 Dec 2016 13:46:50 -0500 (EST) From: James Bottomley To: Ken Goldman , tpmdd-devel@lists.sourceforge.net Date: Fri, 30 Dec 2016 10:46:49 -0800 In-Reply-To: <1483114928.2442.28.camel@linux.vnet.ibm.com> References: <1483114928.2442.28.camel@linux.vnet.ibm.com> X-Mailer: Evolution 3.16.5 Mime-Version: 1.0 X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16123018-0016-0000-0000-0000058710F5 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00006345; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000199; SDB=6.00801288; UDB=6.00389494; IPR=6.00579110; BA=6.00005019; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00013770; XFM=3.00000011; UTC=2016-12-30 18:46:53 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16123018-0017-0000-0000-000035E0A15A Message-Id: <1483123609.2712.1.camel@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-12-30_13:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1612300293 X-Spam-Score: 0.0 (/) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. X-Headers-End: 1cN2Cj-000642-6j Subject: Re: [tpmdd-devel] TPM 2.0 device driver blocking open X-BeenThere: tpmdd-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Tpm Device Driver maintainance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: tpmdd-devel-bounces@lists.sourceforge.net On Fri, 2016-12-30 at 08:22 -0800, James Bottomley wrote: > On Fri, 2016-12-30 at 10:53 -0500, Ken Goldman wrote: > > Is there any way to change it. I didn't set O_NOBLOCK. Is there > > perhaps an ioctl()? > > Is this something that should be added? > > I think for the 2.0 model of every application getting direct access, > we should make it so that every open gets a separate read/write > stream to the tpm which we send in via the locked version of > tpm_transmit() and just let the chip->tpm_mutex sort out the > accesses. > > I can code up a patch if no-one's already done it. Just FYI this is the temporary hack I'm using to fix my multiple access problem while Jarkko is working on the RM as the final solution. All it does is block a second and subsequent open until the first one completes. James --- ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot diff --git a/drivers/char/tpm/tpm-dev.c b/drivers/char/tpm/tpm-dev.c index 912ad30..cce67d5 100644 --- a/drivers/char/tpm/tpm-dev.c +++ b/drivers/char/tpm/tpm-dev.c @@ -51,23 +51,44 @@ static void timeout_work(struct work_struct *work) mutex_unlock(&priv->buffer_mutex); } +static int tpm_open_lock(struct tpm_chip *chip, int lock) +{ + static DECLARE_COMPLETION(wait); + + if (lock) { + while (test_and_set_bit(0, &chip->is_open)) { + int ret; + + dev_dbg(&chip->dev, "Another process owns this TPM\n"); + ret = wait_for_completion_interruptible(&wait); + if (ret) + return ret; + } + } else { + clear_bit(0, &chip->is_open); + complete(&wait); + } + + return 0; +} + static int tpm_open(struct inode *inode, struct file *file) { struct tpm_chip *chip = container_of(inode->i_cdev, struct tpm_chip, cdev); struct file_priv *priv; + int ret; /* It's assured that the chip will be opened just once, * by the check of is_open variable, which is protected * by driver_lock. */ - if (test_and_set_bit(0, &chip->is_open)) { - dev_dbg(&chip->dev, "Another process owns this TPM\n"); - return -EBUSY; - } + ret = tpm_open_lock(chip, 1); + if (ret) + return ret; priv = kzalloc(sizeof(*priv), GFP_KERNEL); if (priv == NULL) { - clear_bit(0, &chip->is_open); + tpm_open_lock(chip, 0); return -ENOMEM; } @@ -173,7 +194,7 @@ static int tpm_release(struct inode *inode, struct file *file) flush_work(&priv->work); file->private_data = NULL; atomic_set(&priv->data_pending, 0); - clear_bit(0, &priv->chip->is_open); + tpm_open_lock(priv->chip, 0); kfree(priv); return 0; }