linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: James Bottomley <jejb@linux.vnet.ibm.com>
To: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>,
	tpmdd-devel@lists.sourceforge.net
Cc: open list <linux-kernel@vger.kernel.org>,
	linux-security-module@vger.kernel.org
Subject: Re: [tpmdd-devel] [PATCH RFC v2 3/5] tpm: infrastructure for TPM spaces
Date: Thu, 12 Jan 2017 17:17:23 -0800	[thread overview]
Message-ID: <1484270243.5807.31.camel@linux.vnet.ibm.com> (raw)
In-Reply-To: <20170112174612.9314-4-jarkko.sakkinen@linux.intel.com>

On Thu, 2017-01-12 at 19:46 +0200, Jarkko Sakkinen wrote:
> @@ -189,6 +190,12 @@ struct tpm_chip *tpm_chip_alloc(struct device
> *pdev,
>  	chip->cdev.owner = THIS_MODULE;
>  	chip->cdev.kobj.parent = &chip->dev.kobj;
> 
> +	chip->work_space.context_buf = kzalloc(PAGE_SIZE,
> GFP_KERNEL);
> +	if (!chip->work_space.context_buf) {
> +		rc = -ENOMEM;
> +		goto out;
> +	}
> +

I think the work_buf handling can be greatly simplified by making it a
pointer to the space: it's only usable between tpm2_prepare_space() and
tpm2_commit_space() which are protected by the chip mutex, so there's
no need for it to exist outside of these calls (i.e. it can be NULL).

Doing it this way also saves the allocation and copying overhead of
work_space.

The patch below can be folded to effect this.

James

---

diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index 13cac09..770a8c0 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -131,7 +131,6 @@ static void tpm_dev_release(struct device *dev)
 	mutex_unlock(&idr_lock);
 
 	kfree(chip->log.bios_event_log);
-	kfree(chip->work_space.context_buf);
 	kfree(chip);
 }
 
@@ -206,12 +205,6 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
 	chip->cdev.kobj.parent = &chip->dev.kobj;
 	chip->cdevrm.kobj.parent = &chip->devrm.kobj;
 
-	chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
-	if (!chip->work_space.context_buf) {
-		rc = -ENOMEM;
-		goto out;
-	}
-
 	return chip;
 
 out:
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 8009ed4..adf7810 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -211,7 +211,7 @@ struct tpm_chip {
 	char ppi_version[TPM_PPI_VERSION_LEN + 1];
 #endif /* CONFIG_ACPI */
 
-	struct tpm_space work_space;
+	struct tpm_space *work_space;
 	u32 nr_commands;
 	u32 *cc_attrs_tbl;
 };
diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c
index 44e5501..285361e 100644
--- a/drivers/char/tpm/tpm2-space.c
+++ b/drivers/char/tpm/tpm2-space.c
@@ -27,7 +27,7 @@ enum tpm2_handle_types {
 
 static void tpm2_flush_space(struct tpm_chip *chip)
 {
-	struct tpm_space *space = &chip->work_space;
+	struct tpm_space *space = chip->work_space;
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(space->context_tbl); i++)
@@ -45,7 +45,7 @@ struct tpm2_context {
 
 static int tpm2_load_space(struct tpm_chip *chip)
 {
-	struct tpm_space *space = &chip->work_space;
+	struct tpm_space *space = chip->work_space;
 	struct tpm2_context *ctx;
 	struct tpm_buf buf;
 	int i;
@@ -99,7 +99,7 @@ static int tpm2_load_space(struct tpm_chip *chip)
 
 static int tpm2_map_command(struct tpm_chip *chip, u32 cc, u8 *cmd, size_t len)
 {
-	struct tpm_space *space = &chip->work_space;
+	struct tpm_space *space = chip->work_space;
 	unsigned int nr_handles;
 	u32 vhandle;
 	u32 phandle;
@@ -147,9 +147,7 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space,
 	if (!space)
 		return 0;
 
-	memcpy(&chip->work_space.context_tbl, &space->context_tbl,
-	       sizeof(space->context_tbl));
-	memcpy(chip->work_space.context_buf, space->context_buf, PAGE_SIZE);
+	chip->work_space = space;
 
 	rc = tpm2_load_space(chip);
 	if (rc)
@@ -164,7 +162,7 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space,
 
 static int tpm2_map_response(struct tpm_chip *chip, u32 cc, u8 *rsp, size_t len)
 {
-	struct tpm_space *space = &chip->work_space;
+	struct tpm_space *space = chip->work_space;
 	u32 phandle;
 	u32 vhandle;
 	u32 attrs;
@@ -222,7 +220,7 @@ static int tpm2_map_response(struct tpm_chip *chip, u32 cc, u8 *rsp, size_t len)
 
 static int tpm2_save_space(struct tpm_chip *chip)
 {
-	struct tpm_space *space = &chip->work_space;
+	struct tpm_space *space = chip->work_space;
 	struct tpm_buf buf;
 	int i;
 	int j;
@@ -295,9 +293,7 @@ int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
 	if (rc)
 		return rc;
 
-	memcpy(&space->context_tbl, &chip->work_space.context_tbl,
-	       sizeof(space->context_tbl));
-	memcpy(space->context_buf, chip->work_space.context_buf, PAGE_SIZE);
+	chip->work_space = NULL;
 
 	return 0;
 }

  parent reply	other threads:[~2017-01-13  1:17 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-12 17:46 [PATCH RFC v2 0/5] RFC: in-kernel resource manager Jarkko Sakkinen
2017-01-12 17:46 ` [PATCH RFC v2 1/5] tpm: validate TPM 2.0 commands Jarkko Sakkinen
2017-01-12 20:34   ` Jarkko Sakkinen
2017-01-12 17:46 ` [PATCH RFC v2 2/5] tpm: export tpm2_flush_context_cmd Jarkko Sakkinen
2017-01-12 17:46 ` [PATCH RFC v2 3/5] tpm: infrastructure for TPM spaces Jarkko Sakkinen
2017-01-12 18:38   ` [tpmdd-devel] " James Bottomley
2017-01-12 20:31     ` Jarkko Sakkinen
2017-01-12 20:38   ` James Bottomley
2017-01-13 16:28     ` Jarkko Sakkinen
     [not found]       ` <o5dohv$60l$1@blaine.gmane.org>
2017-01-16  9:52         ` Jarkko Sakkinen
2017-01-12 20:50   ` Jarkko Sakkinen
2017-01-13  1:17   ` James Bottomley [this message]
2017-01-13 16:31     ` [tpmdd-devel] " Jarkko Sakkinen
2017-01-16  9:09     ` Jarkko Sakkinen
2017-01-16 14:24       ` James Bottomley
2017-01-16 14:48         ` Jarkko Sakkinen
2017-01-16 14:58           ` James Bottomley
2017-01-16 16:52             ` Jarkko Sakkinen
2017-01-12 17:46 ` [PATCH RFC v2 4/5] tpm: split out tpm-dev.c into tpm-dev.c and tpm-common-dev.c Jarkko Sakkinen
2017-01-13 19:18   ` [tpmdd-devel] " James Bottomley
2017-01-12 17:46 ` [PATCH RFC v2 5/5] tpm2: expose resource manager via a device link /dev/tpms<n> Jarkko Sakkinen
2017-01-12 18:39   ` Jason Gunthorpe
2017-01-13 19:20     ` [tpmdd-devel] " James Bottomley
2017-01-13 19:47       ` Jason Gunthorpe
2017-01-13 20:02         ` James Bottomley
2017-01-13 21:23           ` Jason Gunthorpe
2017-01-14  1:10             ` James Bottomley
2017-01-16 16:54               ` Jason Gunthorpe
2017-01-12 19:46   ` James Bottomley
2017-01-12 20:56   ` Jarkko Sakkinen
2017-01-13 17:25     ` Jason Gunthorpe
2017-01-13 17:40       ` [tpmdd-devel] " James Bottomley
2017-01-13 18:01         ` Jason Gunthorpe
2017-01-13 18:11           ` James Bottomley
2017-01-16  9:45         ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1484270243.5807.31.camel@linux.vnet.ibm.com \
    --to=jejb@linux.vnet.ibm.com \
    --cc=jarkko.sakkinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=tpmdd-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).