Skip to contentSkip to conversation

tpm: Create cleanup class for tpm_buf

PATCH Superseded Jarkko Sakkinen 2 participants 5 messages Jun 25, 2025 – Jun 26, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH 1/1 tpm: Create cleanup class for tpm_buf

From: Jarkko Sakkinen <[email protected]>

Define a cleanup for tpm_buf, which will caused tpm_buf_destroy() automatically called at the end of a function scope. This will significantly decrease the likelihood of memory leaks.

---
 drivers/char/tpm/tpm1-cmd.c               | 47 +++++-----------
 drivers/char/tpm/tpm2-cmd.c               | 67 ++++++++---------------
 drivers/char/tpm/tpm2-sessions.c          | 19 ++-----
 drivers/char/tpm/tpm2-space.c             | 15 +----
 drivers/char/tpm/tpm_vtpm_proxy.c         | 11 +---
 include/linux/tpm.h                       |  2 +
 security/keys/trusted-keys/trusted_tpm1.c |  6 +-
 security/keys/trusted-keys/trusted_tpm2.c | 26 +++------
 8 files changed, 59 insertions(+), 134 deletions(-)
drivers/char/tpm/tpm1-cmd.c +15 -32
index cf64c7385105..c7421f3f4187 100644
@@ -323,7 +323,7 @@ unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  */
 static int tpm1_startup(struct tpm_chip *chip)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	int rc;
 
 	dev_info(&chip->dev, "starting up the TPM manually\n");
@@ -335,7 +335,6 @@ static int tpm1_startup(struct tpm_chip *chip)
 	tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
 
 	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
-	tpm_buf_destroy(&buf);
 	return rc;
 }
 
@@ -463,7 +462,7 @@ int tpm1_get_timeouts(struct tpm_chip *chip)
 int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
 		    const char *log_msg)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	int rc;
 
 	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND);
@@ -474,7 +473,6 @@ int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
 	tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
 
 	rc = tpm_transmit_cmd(chip, &buf, TPM_DIGEST_SIZE, log_msg);
-	tpm_buf_destroy(&buf);
 	return rc;
 }
 
@@ -482,7 +480,7 @@ int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
 		    const char *desc, size_t min_cap_length)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	int rc;
 
 	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
@@ -506,7 +504,6 @@ ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
 	rc = tpm_transmit_cmd(chip, &buf, min_cap_length, desc);
 	if (!rc)
 		*cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
-	tpm_buf_destroy(&buf);
 	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm1_getcap);
@@ -529,9 +526,9 @@ struct tpm1_get_random_out {
  */
 int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 {
+	CLASS(tpm_buf, buf)();
 	struct tpm1_get_random_out *out;
 	u32 num_bytes =  min_t(u32, max, TPM_MAX_RNG_DATA);
-	struct tpm_buf buf;
 	u32 total = 0;
 	int retries = 5;
 	u32 recd;
@@ -549,22 +546,18 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 		if (rc) {
 			if (rc > 0)
 				rc = -EIO;
-			goto out;
+			return rc;
 		}
 
 		out = (struct tpm1_get_random_out *)&buf.data[TPM_HEADER_SIZE];
 
 		recd = be32_to_cpu(out->rng_data_len);
-		if (recd > num_bytes) {
-			rc = -EFAULT;
-			goto out;
-		}
+		if (recd > num_bytes)
+			return -EFAULT;
 
 		if (tpm_buf_length(&buf) < TPM_HEADER_SIZE +
-					   sizeof(out->rng_data_len) + recd) {
-			rc = -EFAULT;
-			goto out;
-		}
+					   sizeof(out->rng_data_len) + recd)
+			return -EFAULT;
 		memcpy(dest, out->rng_data, recd);
 
 		dest += recd;
@@ -575,15 +568,13 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 	} while (retries-- && total < max);
 
 	rc = total ? (int)total : -EIO;
-out:
-	tpm_buf_destroy(&buf);
 	return rc;
 }
 
 #define TPM_ORD_PCRREAD 21
 int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	int rc;
 
 	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCRREAD);
@@ -595,17 +586,12 @@ int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
 	rc = tpm_transmit_cmd(chip, &buf, TPM_DIGEST_SIZE,
 			      "attempting to read a pcr value");
 	if (rc)
-		goto out;
+		return rc;
 
-	if (tpm_buf_length(&buf) < TPM_DIGEST_SIZE) {
-		rc = -EFAULT;
-		goto out;
-	}
+	if (tpm_buf_length(&buf) < TPM_DIGEST_SIZE)
+		return -EFAULT;
 
 	memcpy(res_buf, &buf.data[TPM_HEADER_SIZE], TPM_DIGEST_SIZE);
-
-out:
-	tpm_buf_destroy(&buf);
 	return rc;
 }
 
@@ -619,7 +605,7 @@ int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
  */
 static int tpm1_continue_selftest(struct tpm_chip *chip)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	int rc;
 
 	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_CONTINUE_SELFTEST);
@@ -627,7 +613,6 @@ static int tpm1_continue_selftest(struct tpm_chip *chip)
 		return rc;
 
 	rc = tpm_transmit_cmd(chip, &buf, 0, "continue selftest");
-	tpm_buf_destroy(&buf);
 	return rc;
 }
 
@@ -742,7 +727,7 @@ int tpm1_auto_startup(struct tpm_chip *chip)
 int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)
 {
 	u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	unsigned int try;
 	int rc;
 
@@ -782,8 +767,6 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)
 		dev_warn(&chip->dev, "TPM savestate took %dms\n",
 			 try * TPM_TIMEOUT_RETRY);
 
-	tpm_buf_destroy(&buf);
-
 	return rc;
 }
 
drivers/char/tpm/tpm2-cmd.c +22 -45

245 lines not on this page · raw message

drivers/char/tpm/tpm2-sessions.c +5 -14
index 7b5049b3d476..94af69a66252 100644
@@ -182,7 +182,7 @@ static int tpm2_parse_read_public(char *name, struct tpm_buf *buf)
 
 static int tpm2_read_public(struct tpm_chip *chip, u32 handle, char *name)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	int rc;
 
 	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_READ_PUBLIC);
@@ -194,8 +194,6 @@ static int tpm2_read_public(struct tpm_chip *chip, u32 handle, char *name)
 	if (rc == TPM2_RC_SUCCESS)
 		rc = tpm2_parse_read_public(name, &buf);
 
-	tpm_buf_destroy(&buf);
-
 	return rc;
 }
 #endif /* CONFIG_TCG_TPM2_HMAC */
@@ -968,8 +966,8 @@ static int tpm2_load_null(struct tpm_chip *chip, u32 *null_key)
  */
 int tpm2_start_auth_session(struct tpm_chip *chip)
 {
+	CLASS(tpm_buf, buf)();
 	struct tpm2_auth *auth;
-	struct tpm_buf buf;
 	u32 null_key;
 	int rc;
 
@@ -1022,8 +1020,6 @@ int tpm2_start_auth_session(struct tpm_chip *chip)
 	if (rc == TPM2_RC_SUCCESS)
 		rc = tpm2_parse_start_auth_session(auth, &buf);
 
-	tpm_buf_destroy(&buf);
-
 	if (rc == TPM2_RC_SUCCESS) {
 		chip->auth = auth;
 		return 0;
@@ -1243,19 +1239,17 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy,
 			       u32 *handle, u8 *name)
 {
+	CLASS(tpm_buf, buf)();
+	CLASS(tpm_buf, template)();
 	int rc;
-	struct tpm_buf buf;
-	struct tpm_buf template;
 
 	rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE_PRIMARY);
 	if (rc)
 		return rc;
 
 	rc = tpm_buf_init_sized(&template);
-	if (rc) {
-		tpm_buf_destroy(&buf);
+	if (rc)
 		return rc;
-	}
 
 	/*
 	 * create the template.  Note: in order for userspace to
@@ -1319,7 +1313,6 @@ static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy,
 
 	/* the public template */
 	tpm_buf_append(&buf, template.data, template.length);
-	tpm_buf_destroy(&template);
 
 	/* outside info (empty) */
 	tpm_buf_append_u16(&buf, 0);
@@ -1334,8 +1327,6 @@ static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy,
 		rc = tpm2_parse_create_primary(chip, &buf, handle, hierarchy,
 					       name);
 
-	tpm_buf_destroy(&buf);
-
 	return rc;
 }
 
drivers/char/tpm/tpm2-space.c +2 -13
index 60354cd53b5c..c549111b60e4 100644
@@ -71,7 +71,7 @@ void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space)
 int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
 		      unsigned int *offset, u32 *handle)
 {
-	struct tpm_buf tbuf;
+	CLASS(tpm_buf, tbuf)();
 	struct tpm2_context *ctx;
 	unsigned int body_size;
 	int rc;
@@ -88,7 +88,6 @@ int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
 	if (rc < 0) {
 		dev_warn(&chip->dev, "%s: failed with a system error %d\n",
 			 __func__, rc);
-		tpm_buf_destroy(&tbuf);
 		return -EFAULT;
 	} else if (tpm2_rc_value(rc) == TPM2_RC_HANDLE ||
 		   rc == TPM2_RC_REFERENCE_H0) {
@@ -103,29 +102,24 @@ int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
 		 * flushed outside the space
 		 */
 		*handle = 0;
-		tpm_buf_destroy(&tbuf);
 		return -ENOENT;
 	} else if (tpm2_rc_value(rc) == TPM2_RC_INTEGRITY) {
-		tpm_buf_destroy(&tbuf);
 		return -EINVAL;
 	} else if (rc > 0) {
 		dev_warn(&chip->dev, "%s: failed with a TPM error 0x%04X\n",
 			 __func__, rc);
-		tpm_buf_destroy(&tbuf);
 		return -EFAULT;
 	}
 
 	*handle = be32_to_cpup((__be32 *)&tbuf.data[TPM_HEADER_SIZE]);
 	*offset += body_size;
-
-	tpm_buf_destroy(&tbuf);
 	return 0;
 }
 
 int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
 		      unsigned int buf_size, unsigned int *offset)
 {
-	struct tpm_buf tbuf;
+	CLASS(tpm_buf, tbuf)();
 	unsigned int body_size;
 	int rc;
 
@@ -139,28 +133,23 @@ int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
 	if (rc < 0) {
 		dev_warn(&chip->dev, "%s: failed with a system error %d\n",
 			 __func__, rc);
-		tpm_buf_destroy(&tbuf);
 		return -EFAULT;
 	} else if (tpm2_rc_value(rc) == TPM2_RC_REFERENCE_H0) {
-		tpm_buf_destroy(&tbuf);
 		return -ENOENT;
 	} else if (rc) {
 		dev_warn(&chip->dev, "%s: failed with a TPM error 0x%04X\n",
 			 __func__, rc);
-		tpm_buf_destroy(&tbuf);
 		return -EFAULT;
 	}
 
 	body_size = tpm_buf_length(&tbuf) - TPM_HEADER_SIZE;
 	if ((*offset + body_size) > buf_size) {
 		dev_warn(&chip->dev, "%s: out of backing storage\n", __func__);
-		tpm_buf_destroy(&tbuf);
 		return -ENOMEM;
 	}
 
 	memcpy(&buf[*offset], &tbuf.data[TPM_HEADER_SIZE], body_size);
 	*offset += body_size;
-	tpm_buf_destroy(&tbuf);
 	return 0;
 }
 
drivers/char/tpm/tpm_vtpm_proxy.c +3 -8
index 0818bb517805..e21cf709481e 100644
@@ -395,7 +395,7 @@ static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip  *chip, u8 status)
 
 static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	int rc;
 	const struct tpm_header *header;
 	struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
@@ -416,19 +416,14 @@ static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality)
 
 	proxy_dev->state &= ~STATE_DRIVER_COMMAND;
 
-	if (rc < 0) {
-		locality = rc;
-		goto out;
-	}
+	if (rc < 0)
+		return rc;
 
 	header = (const struct tpm_header *)buf.data;
 	rc = be32_to_cpu(header->return_code);
 	if (rc)
 		locality = -1;
 
-out:
-	tpm_buf_destroy(&buf);
-
 	return locality;
 }
 
include/linux/tpm.h +2 -0
index 804fbbe3873d..b39cd7ca7471 100644
@@ -427,6 +427,8 @@ u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset);
 u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset);
 void tpm_buf_append_handle(struct tpm_chip *chip, struct tpm_buf *buf, u32 handle);
 
+DEFINE_CLASS(tpm_buf, struct tpm_buf, tpm_buf_destroy(&_T), (struct tpm_buf) {}, void)
+
 /*
  * Check if TPM device is in the firmware upgrade mode.
  */
security/keys/trusted-keys/trusted_tpm1.c +2 -4
index 89c9798d1800..fabd8ee12aee 100644
@@ -672,7 +672,7 @@ static int tpm_unseal(struct tpm_buf *tb,
 static int key_seal(struct trusted_key_payload *p,
 		    struct trusted_key_options *o)
 {
-	struct tpm_buf tb;
+	CLASS(tpm_buf, tb)();
 	int ret;
 
 	ret = tpm_buf_init(&tb, 0, 0);
@@ -688,7 +688,6 @@ static int key_seal(struct trusted_key_payload *p,
 	if (ret < 0)
 		pr_info("srkseal failed (%d)\n", ret);
 
-	tpm_buf_destroy(&tb);
 	return ret;
 }
 
@@ -698,7 +697,7 @@ static int key_seal(struct trusted_key_payload *p,
 static int key_unseal(struct trusted_key_payload *p,
 		      struct trusted_key_options *o)
 {
-	struct tpm_buf tb;
+	CLASS(tpm_buf, tb)();
 	int ret;
 
 	ret = tpm_buf_init(&tb, 0, 0);
@@ -713,7 +712,6 @@ static int key_unseal(struct trusted_key_payload *p,
 		/* pull migratable flag out of sealed key */
 		p->migratable = p->key[--p->key_len];
 
-	tpm_buf_destroy(&tb);
 	return ret;
 }
 
security/keys/trusted-keys/trusted_tpm2.c +8 -18
index 024be262702f..6d787e2fdc19 100644
@@ -241,8 +241,9 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 		      struct trusted_key_payload *payload,
 		      struct trusted_key_options *options)
 {
+	CLASS(tpm_buf, buf)();
+	CLASS(tpm_buf, sized)();
 	off_t offset = TPM_HEADER_SIZE;
-	struct tpm_buf buf, sized;
 	int blob_len = 0;
 	u32 hash;
 	u32 flags;
@@ -278,7 +279,6 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 
 	rc = tpm_buf_init_sized(&sized);
 	if (rc) {
-		tpm_buf_destroy(&buf);
 		tpm2_end_auth_session(chip);
 		goto out_put;
 	}
@@ -350,9 +350,6 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	blob_len = tpm2_key_encode(payload, options, &buf.data[offset], blob_len);
 
 out:
-	tpm_buf_destroy(&sized);
-	tpm_buf_destroy(&buf);
-
 	if (rc > 0) {
 		if (tpm2_rc_value(rc) == TPM2_RC_HASH)
 			rc = -EINVAL;
@@ -387,7 +384,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 			 struct trusted_key_options *options,
 			 u32 *blob_handle)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	unsigned int private_len;
 	unsigned int public_len;
 	unsigned int blob_len;
@@ -466,7 +463,6 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 out:
 	if (blob != payload->blob)
 		kfree(blob);
-	tpm_buf_destroy(&buf);
 
 	if (rc > 0)
 		rc = -EPERM;
@@ -491,7 +487,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 			   struct trusted_key_options *options,
 			   u32 blob_handle)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	u16 data_len;
 	u8 *data;
 	int rc;
@@ -540,15 +536,11 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	if (!rc) {
 		data_len = be16_to_cpup(
 			(__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
-		if (data_len < MIN_KEY_SIZE ||  data_len > MAX_KEY_SIZE) {
-			rc = -EFAULT;
-			goto out;
-		}
+		if (data_len < MIN_KEY_SIZE ||  data_len > MAX_KEY_SIZE)
+			return -EFAULT;
 
-		if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {
-			rc = -EFAULT;
-			goto out;
-		}
+		if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len)
+			return -EFAULT;
 		data = &buf.data[TPM_HEADER_SIZE + 6];
 
 		if (payload->old_format) {
@@ -566,8 +558,6 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 		}
 	}
 
-out:
-	tpm_buf_destroy(&buf);
 	return rc;
 }
 
Signature · 1 line
2.39.5
author maintainer
#1 lore raw
Quoted 5 lines · Jarkko Sakkinen

From: Jarkko Sakkinen <[email protected]>

Define a cleanup for tpm_buf, which will caused tpm_buf_destroy() automatically called at the end of a function scope. This will significantly decrease the likelihood of memory leaks.

Oops, this did not go well.

Here's a corrected version.

"Define a cleanup class for struct tpm_buf, which will take
the responsibility of calling tpm_buf_destroy() automatically when tpm_buf instance is going out of scope."

For the code change itself I noticed that I missed tpm-sysfs.c i.e., v2 coming post the feedback for this.

BR, Jarkko

#2 lore raw
In reply to Jarkko Sakkinen
Quoted 10 lines · Jarkko Sakkinen
drivers/char/tpm/tpm1-cmd.c +1 -1
@@ -323,7 +323,7 @@ unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  */
 static int tpm1_startup(struct tpm_chip *chip)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	int rc;
 
 	dev_info(&chip->dev, "starting up the TPM manually\n");

drivers/char/tpm/tpm1-cmd.c static int tpm1_startup(struct tpm_chip *chip) · line 339

Quoted 6 lines
drivers/char/tpm/tpm1-cmd.c +0 -1
@@ -335,7 +335,6 @@ static int tpm1_startup(struct tpm_chip *chip)
 	tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
 
 	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
-	tpm_buf_destroy(&buf);
 	return rc;
 }

So, Linus has spoken negatively about just converting existing code to use cleanup.h, fearful it would introduce more bugs.

I would certainly split this into more patches, and it would be nice if something mechanical like coccinelle could do the change.

At least I would add the class and drop the tpm_buf_destroy() as one patch, and another would be to cleanup any empty gotos.

Also, I think the style guide for cleanup.h is to not use the variable block, so it should be more like:

CLASS(tpm_buf, buf)();
if (!tpm_buf)
return -ENOMEM;

AFAICT, but that seems to be some kind of tribal knowledge.

Jason

author maintainer
#3 lore raw
Quoted 13 lines · Jason Gunthorpe
Quoted 12 lines · Jarkko Sakkinen
drivers/char/tpm/tpm1-cmd.c +1 -1
@@ -323,7 +323,7 @@ unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  */
 static int tpm1_startup(struct tpm_chip *chip)
 {
-	struct tpm_buf buf;
+	CLASS(tpm_buf, buf)();
 	int rc;
 
 	dev_info(&chip->dev, "starting up the TPM manually\n");
@@ -335,7 +335,6 @@ static int tpm1_startup(struct tpm_chip *chip)
 	tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
 
Quoted 7 lines

drivers/char/tpm/tpm1-cmd.c static int tpm1_startup(struct tpm_chip *chip) · line 339

Quoted 4 lines
drivers/char/tpm/tpm1-cmd.c +0 -1
 	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
-	tpm_buf_destroy(&buf);
 	return rc;
 }

So, Linus has spoken negatively about just converting existing code to use cleanup.h, fearful it would introduce more bugs.

I did not do this for the sake of conversion. It's just that tpm_buf is a pretty good fit for such construct, as it is always in function scope and always heap allocated.

Quoted 2 lines

I would certainly split this into more patches, and it would be nice if something mechanical like coccinelle could do the change.

I took this a bit in further:

https://lore.kernel.org/linux-integrity/[email protected]/T/#t

I did that few dozen times while developing this, running always at minimum:

1. https://codeberg.org/jarkko/linux-tpmdd-test/src/branch/main/board/pc_x86_64/test_tpm2_kselftest.exp.in
2. https://codeberg.org/jarkko/linux-tpmdd-test/src/branch/main/board/pc_x86_64/test_tpm2_trusted.exp.in

A few times I run some ad-hoc tests too.

And despite 89% is mechanical work there was at least a dozen code blocks where you need to understand the context too. So actually with this careful manual work was not that bad idea in the end.

Quoted 4 lines

At least I would add the class and drop the tpm_buf_destroy() as one patch, and another would be to cleanup any empty gotos.

Quoted 8 lines

Also, I think the style guide for cleanup.h is to not use the variable block, so it should be more like:

CLASS(tpm_buf, buf)();
if (!tpm_buf)
return -ENOMEM;

AFAICT, but that seems to be some kind of tribal knowledge.

This was improved in v2 :-) If you have some proposal how you'd liked that version to be splitted, please give feedback.

Quoted 2 lines

Jason

BR, Jarkko

author maintainer
#4 lore raw
Quoted 6 lines · Jarkko Sakkinen
Quoted 6 lines

At least I would add the class and drop the tpm_buf_destroy() as one patch, and another would be to cleanup any empty gotos.

Also, I think the style guide for cleanup.h is to not use the variable block, so it should be more like:

Quoted 8 lines
Quoted 5 lines

CLASS(tpm_buf, buf)();
if (!tpm_buf)
return -ENOMEM;

AFAICT, but that seems to be some kind of tribal knowledge.

This was improved in v2 :-) If you have some proposal how you'd liked that version to be splitted, please give feedback.

After a bit of thought, II could split v2 e.g., into to the following list of patches (a draft, along the lines):

1. Prepare internals for API changes.
2. Implement tpm_buf_alloc().
3. Implement CLASS_TPM_BUF() macro.
4. Changes for tpm{1,2}-cmd.c.
6. Changes for tpm2-sessions.c.
7. Changes for tpm2-space.c.
8. Changes for trusted_tpm{1,2}.c
9. Remove stuff left w/o a call site.

It's pretty good exercise for v2 actually as it is already somewhat functional code. By doing this split this update will get already reasonably well verified.

I should also probably emphasize the motivation better in the next version. Especially with multiple tpm_buf instances in the same function scope, things do something are messy to backtrack. In addition, this complexity might cap the motivation for someone to contribute a useful feature.

I don't really have even followed Linus' opinions in this topic per se I personally just think that since I have a measured argument for this. I got with that and talk with Linus if he wants to bring it up :-)

BR, Jarkko