Skip to contentSkip to conversation

tpm: Cleanup class for tpm_buf

PATCH In review Jarkko Sakkinen 4 participants 8 messages Jun 26, 2025 – Jun 29, 2025

Subsystems (4)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH v2 1/1 tpm: Cleanup class for tpm_buf

From: Jarkko Sakkinen <[email protected]>

Create a cleanup class for struct tpm_buf using DEFINE_CLASS(), which will guarantee that the heap allocated memory will be freed automatically for the transient instances of this structure, when they go out of scope.

Wrap this all into help macro CLASS_TPM_BUF().

A TPM buffer can now be declared trivially:

    CLASS_TPM_BUF(buf, buf_size);

Before use caller needs to check that buf is not NULL, but other that there are no other memory management responsibilities.

---
v2:
- Implement also memory allocation using the cleanup class.
- Rewrote the commit message.
- Implemented CLASS_TPM_BUF() helper macro.

---
 drivers/char/tpm/tpm-buf.c                |  63 +++---
 drivers/char/tpm/tpm-sysfs.c              |  19 +-
 drivers/char/tpm/tpm1-cmd.c               | 144 ++++++------
 drivers/char/tpm/tpm2-cmd.c               | 261 ++++++++++------------
 drivers/char/tpm/tpm2-sessions.c          | 116 +++++-----
 drivers/char/tpm/tpm2-space.c             |  42 ++--
 drivers/char/tpm/tpm_vtpm_proxy.c         |  29 +--
 include/linux/tpm.h                       |  20 +-
 security/keys/trusted-keys/trusted_tpm1.c |  35 +--
 security/keys/trusted-keys/trusted_tpm2.c | 150 ++++++-------
 10 files changed, 394 insertions(+), 485 deletions(-)
drivers/char/tpm/tpm-buf.c +27 -36
index dc882fc9fa9e..2bc6e6e9c4e8 100644
@@ -7,24 +7,38 @@
 #include <linux/module.h>
 #include <linux/tpm.h>
 
+#undef pr_fmt
+#define pr_fmt(fmt) "tpm_buf: " fmt
+
 /**
- * tpm_buf_init() - Allocate and initialize a TPM command
- * @buf:	A &tpm_buf
- * @tag:	TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
- * @ordinal:	A command ordinal
+ * tpm_buf_alloc() - Allocate a &tpm_buf instance
+ * @size:	the buffer size in bytes
+ *
+ * As &tpm_buf header is placed in the beginning of the buffer before contents,
+ * the total capacity will be a eight bytes less than the requested size. E.g,
+ * if 4096 bytes is requested, the capacity for data is 4088 bytes.
  *
- * Return: 0 or -ENOMEM
+ * Return:
+ * * &tpm_buf:	success
+ * * NULL:	out of memory
  */
-int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
+struct tpm_buf *tpm_buf_alloc(u16 size)
 {
-	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
-	if (!buf->data)
-		return -ENOMEM;
+	struct tpm_buf *buf;
 
-	tpm_buf_reset(buf, tag, ordinal);
-	return 0;
+	if (size < sizeof(*buf)) {
+		pr_warn("too small buffer size: %lu\n", size);
+		return NULL;
+	}
+
+	buf = kzalloc(size, GFP_KERNEL);
+	if (!buf)
+		return NULL;
+
+	buf->capacity = size - sizeof(*buf);
+	return buf;
 }
-EXPORT_SYMBOL_GPL(tpm_buf_init);
+EXPORT_SYMBOL_GPL(tpm_buf_alloc);
 
 /**
  * tpm_buf_reset() - Initialize a TPM command
@@ -48,23 +62,6 @@ void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
 }
 EXPORT_SYMBOL_GPL(tpm_buf_reset);
 
-/**
- * tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer
- * @buf:	A @tpm_buf
- *
- * Return: 0 or -ENOMEM
- */
-int tpm_buf_init_sized(struct tpm_buf *buf)
-{
-	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
-	if (!buf->data)
-		return -ENOMEM;
-
-	tpm_buf_reset_sized(buf);
-	return 0;
-}
-EXPORT_SYMBOL_GPL(tpm_buf_init_sized);
-
 /**
  * tpm_buf_reset_sized() - Initialize a sized buffer
  * @buf:	A &tpm_buf
@@ -78,12 +75,6 @@ void tpm_buf_reset_sized(struct tpm_buf *buf)
 }
 EXPORT_SYMBOL_GPL(tpm_buf_reset_sized);
 
-void tpm_buf_destroy(struct tpm_buf *buf)
-{
-	free_page((unsigned long)buf->data);
-}
-EXPORT_SYMBOL_GPL(tpm_buf_destroy);
-
 /**
  * tpm_buf_length() - Return the number of bytes consumed by the data
  * @buf:	A &tpm_buf
@@ -108,7 +99,7 @@ void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length)
 	if (buf->flags & TPM_BUF_OVERFLOW)
 		return;
 
-	if ((buf->length + new_length) > PAGE_SIZE) {
+	if ((buf->length + new_length) > buf->capacity) {
 		WARN(1, "tpm_buf: write overflow\n");
 		buf->flags |= TPM_BUF_OVERFLOW;
 		return;
drivers/char/tpm/tpm-sysfs.c +9 -10
index 94231f052ea7..415065ff5267 100644
@@ -32,28 +32,29 @@ struct tpm_readpubek_out {
 static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
 			  char *buf)
 {
-	struct tpm_buf tpm_buf;
+	CLASS_TPM_BUF(tpm_buf, PAGE_SIZE);
 	struct tpm_readpubek_out *out;
 	int i;
 	char *str = buf;
 	struct tpm_chip *chip = to_tpm_chip(dev);
 	char anti_replay[20];
 
+	if (!tpm_buf)
+		return -ENOMEM;
+
 	memset(&anti_replay, 0, sizeof(anti_replay));
 
 	if (tpm_try_get_ops(chip))
 		return 0;
 
-	if (tpm_buf_init(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK))
-		goto out_ops;
-
-	tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay));
+	tpm_buf_reset(tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK);
+	tpm_buf_append(tpm_buf, anti_replay, sizeof(anti_replay));
 
-	if (tpm_transmit_cmd(chip, &tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE,
+	if (tpm_transmit_cmd(chip, tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE,
 			     "attempting to read the PUBEK"))
-		goto out_buf;
+		goto out_ops;
 
-	out = (struct tpm_readpubek_out *)&tpm_buf.data[10];
+	out = (struct tpm_readpubek_out *)&tpm_buf->data[10];
 	str +=
 	    sprintf(str,
 		    "Algorithm: %4ph\n"
@@ -71,8 +72,6 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
 	for (i = 0; i < 256; i += 16)
 		str += sprintf(str, "%16ph\n", &out->modulus[i]);
 
-out_buf:
-	tpm_buf_destroy(&tpm_buf);
 out_ops:
 	tpm_put_ops(chip);
 	return str - buf;
drivers/char/tpm/tpm1-cmd.c +66 -78

270 lines not on this page · raw message

drivers/char/tpm/tpm2-cmd.c +117 -144

510 lines not on this page · raw message

drivers/char/tpm/tpm2-sessions.c +52 -64

220 lines not on this page · raw message

drivers/char/tpm/tpm2-space.c +16 -26

104 lines not on this page · raw message

drivers/char/tpm/tpm_vtpm_proxy.c +12 -17
index 0818bb517805..87152a4b68ce 100644
@@ -395,40 +395,35 @@ 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, PAGE_SIZE);
 	int rc;
 	const struct tpm_header *header;
 	struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
 
+	if (!buf)
+		return -ENOMEM;
+
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
-		rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS,
-				  TPM2_CC_SET_LOCALITY);
+		tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_SET_LOCALITY);
 	else
-		rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND,
-				  TPM_ORD_SET_LOCALITY);
-	if (rc)
-		return rc;
-	tpm_buf_append_u8(&buf, locality);
+		tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SET_LOCALITY);
+
+	tpm_buf_append_u8(buf, locality);
 
 	proxy_dev->state |= STATE_DRIVER_COMMAND;
 
-	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to set locality");
+	rc = tpm_transmit_cmd(chip, buf, 0, "attempting to set 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;
+	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 +12 -8
index 804fbbe3873d..05dbb47dd1ef 100644
@@ -375,13 +375,15 @@ enum tpm_buf_flags {
 };
 
 /*
- * A string buffer type for constructing TPM commands.
+ * A buffer for constructing and parsing TPM commands, responses and sized
+ * (TPM2B) buffers.
  */
 struct tpm_buf {
-	u32 flags;
-	u32 length;
-	u8 *data;
-	u8 handles;
+	u16 flags;
+	u16 capacity;
+	u16 length;
+	u16 handles;
+	u8 data[];
 };
 
 enum tpm2_object_attributes {
@@ -412,11 +414,9 @@ struct tpm2_hash {
 	unsigned int tpm_id;
 };
 
-int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal);
+struct tpm_buf *tpm_buf_alloc(u16 size);
 void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal);
-int tpm_buf_init_sized(struct tpm_buf *buf);
 void tpm_buf_reset_sized(struct tpm_buf *buf);
-void tpm_buf_destroy(struct tpm_buf *buf);
 u32 tpm_buf_length(struct tpm_buf *buf);
 void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length);
 void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value);
@@ -427,6 +427,10 @@ 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 *, kfree(_T), tpm_buf_alloc(size), u16 size);
+
+#define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
+
 /*
  * Check if TPM device is in the firmware upgrade mode.
  */
security/keys/trusted-keys/trusted_tpm1.c +18 -17
index 89c9798d1800..059c9872b34f 100644
@@ -356,21 +356,26 @@ static int TSS_checkhmac2(unsigned char *buffer,
  */
 int trusted_tpm_send(unsigned char *cmd, size_t buflen)
 {
-	struct tpm_buf buf;
+	CLASS_TPM_BUF(buf, PAGE_SIZE);
 	int rc;
 
 	if (!chip)
 		return -ENODEV;
 
+	if (!buf)
+		return -ENOMEM;
+
+	if (buflen > buf->capacity)
+		return -EINVAL;
+
 	rc = tpm_try_get_ops(chip);
 	if (rc)
 		return rc;
 
-	buf.flags = 0;
-	buf.length = buflen;
-	buf.data = cmd;
+	tpm_buf_append(buf, cmd, buflen);
+
 	dump_tpm_buf(cmd);
-	rc = tpm_transmit_cmd(chip, &buf, 4, "sending data");
+	rc = tpm_transmit_cmd(chip, buf, 4, "sending command");
 	dump_tpm_buf(cmd);
 
 	if (rc > 0)
@@ -672,23 +677,21 @@ 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, PAGE_SIZE);
 	int ret;
 
-	ret = tpm_buf_init(&tb, 0, 0);
-	if (ret)
-		return ret;
+	if (!tb)
+		return -ENOMEM;
 
 	/* include migratable flag at end of sealed key */
 	p->key[p->key_len] = p->migratable;
 
-	ret = tpm_seal(&tb, o->keytype, o->keyhandle, o->keyauth,
+	ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
 		       p->key, p->key_len + 1, p->blob, &p->blob_len,
 		       o->blobauth, o->pcrinfo, o->pcrinfo_len);
 	if (ret < 0)
 		pr_info("srkseal failed (%d)\n", ret);
 
-	tpm_buf_destroy(&tb);
 	return ret;
 }
 
@@ -698,14 +701,13 @@ 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, PAGE_SIZE);
 	int ret;
 
-	ret = tpm_buf_init(&tb, 0, 0);
-	if (ret)
-		return ret;
+	if (!tb)
+		return -ENOMEM;
 
-	ret = tpm_unseal(&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
+	ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
 			 o->blobauth, p->key, &p->key_len);
 	if (ret < 0)
 		pr_info("srkunseal failed (%d)\n", ret);
@@ -713,7 +715,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 +65 -85

281 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer
#1 lore raw
Quoted 7 lines · Jarkko Sakkinen

From: Jarkko Sakkinen <[email protected]>

Create a cleanup class for struct tpm_buf using DEFINE_CLASS(), which will guarantee that the heap allocated memory will be freed automatically for the transient instances of this structure, when they go out of scope.

Quoted 5 lines

Wrap this all into help macro CLASS_TPM_BUF().

A TPM buffer can now be declared trivially:

    CLASS_TPM_BUF(buf, buf_size);

Well, that's not all ... you're also adding a size to the API that we didn't have before, which should at least be documented in the commit message and probably be a separate patch.

What is the reason for this, though? The reason we currently use a page is that it's easy for the OS to manage (no slab fragmentation issues). The TCG reference platform defines this to be just under 4k (actually 4096-0x80) precisely because TPM implementations don't do scatter gather, so they don't want it going over an ARM page, so there's no danger of us ever needing more than a page.

Regards,

James

author maintainer
#2 lore raw
Quoted 18 lines · James Bottomley
Quoted 7 lines · Jarkko Sakkinen

From: Jarkko Sakkinen <[email protected]>

Create a cleanup class for struct tpm_buf using DEFINE_CLASS(), which will guarantee that the heap allocated memory will be freed automatically for the transient instances of this structure, when they go out of scope.

Quoted 5 lines

Wrap this all into help macro CLASS_TPM_BUF().

A TPM buffer can now be declared trivially:

    CLASS_TPM_BUF(buf, buf_size);

Well, that's not all ... you're also adding a size to the API that we didn't have before, which should at least be documented in the commit message and probably be a separate patch.

Quoted 6 lines

What is the reason for this, though? The reason we currently use a page is that it's easy for the OS to manage (no slab fragmentation issues). The TCG reference platform defines this to be just under 4k (actually 4096-0x80) precisely because TPM implementations don't do scatter gather, so they don't want it going over an ARM page, so there's no danger of us ever needing more than a page.

Thanks for the valuable feedback.

I can drop "buf_size" parameter. It is not a priority, and I also agree with your comments.

Quoted 4 lines

Regards,

James

BR, Jarkko

author maintainer
#3 lore raw
Quoted 26 lines · Jarkko Sakkinen
Quoted 18 lines · James Bottomley
Quoted 7 lines · Jarkko Sakkinen

From: Jarkko Sakkinen <[email protected]>

Create a cleanup class for struct tpm_buf using DEFINE_CLASS(), which will guarantee that the heap allocated memory will be freed automatically for the transient instances of this structure, when they go out of scope.

Quoted 5 lines

Wrap this all into help macro CLASS_TPM_BUF().

A TPM buffer can now be declared trivially:

    CLASS_TPM_BUF(buf, buf_size);

Well, that's not all ... you're also adding a size to the API that we didn't have before, which should at least be documented in the commit message and probably be a separate patch.

Quoted 6 lines

What is the reason for this, though? The reason we currently use a page is that it's easy for the OS to manage (no slab fragmentation issues). The TCG reference platform defines this to be just under 4k (actually 4096-0x80) precisely because TPM implementations don't do scatter gather, so they don't want it going over an ARM page, so there's no danger of us ever needing more than a page.

Quoted 4 lines

Thanks for the valuable feedback.

I can drop "buf_size" parameter. It is not a priority, and I also agree with your comments.

I also noticed that I had changed one log message in tpm2-sessions.c. It was unintended i.e. a spurious change. I'll revert that one too.

I'll split this into more reasonable portions for next version so these should be easier to review then.

BR, Jarkko

#4 lore raw
In reply to Jarkko Sakkinen
kernel test robot noticed the following build warnings:

Hi Jarkko,

kernel test robot noticed the following build warnings:

[auto build test WARNING on char-misc/char-misc-testing]
[also build test WARNING on char-misc/char-misc-next char-misc/char-misc-linus linus/master v6.16-rc3 next-20250626] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Jarkko-Sakkinen/tpm-Cleanup-class-for-tpm_buf/20250626-182148
base:   char-misc/char-misc-testing
patch link:    https://lore.kernel.org/r/20250626101935.1007898-1-jarkko%40kernel.org
patch subject: [PATCH v2] tpm: Cleanup class for tpm_buf
config: arm64-randconfig-001-20250627 (https://download.01.org/0day-ci/archive/20250627/[email protected]/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250627/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from drivers/char/tpm/tpm1-cmd.c:21:
include/linux/tpm_eventlog.h:167:6: warning: variable 'mapping_size' set but not used [-Wunused-but-set-variable]

     167 |         int mapping_size;
         |             ^
>> drivers/char/tpm/tpm1-cmd.c:326:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     326 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm1-cmd.c:465:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     465 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm1-cmd.c:483:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     483 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm1-cmd.c:530:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     530 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm1-cmd.c:581:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     581 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm1-cmd.c:612:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     612 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm1-cmd.c:733:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     733 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^

include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
| ~~~~~~~~~~~~~~~~~~~ ^~~~
8 warnings generated.
--
In file included from drivers/char/tpm/tpm2-space.c:16:
In file included from drivers/char/tpm/tpm.h:28:
include/linux/tpm_eventlog.h:167:6: warning: variable 'mapping_size' set but not used [-Wunused-but-set-variable]

     167 |         int mapping_size;
         |             ^
>> drivers/char/tpm/tpm2-space.c:74:22: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
      74 |         CLASS_TPM_BUF(tbuf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-space.c:123:22: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     123 |         CLASS_TPM_BUF(tbuf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^

include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
| ~~~~~~~~~~~~~~~~~~~ ^~~~
3 warnings generated.
--
In file included from drivers/char/tpm/tpm2-cmd.c:14:
In file included from drivers/char/tpm/tpm.h:28:
include/linux/tpm_eventlog.h:167:6: warning: variable 'mapping_size' set but not used [-Wunused-but-set-variable]

     167 |         int mapping_size;
         |             ^
>> drivers/char/tpm/tpm2-cmd.c:168:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     168 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-cmd.c:235:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     235 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-cmd.c:293:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     293 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-cmd.c:368:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     368 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-cmd.c:405:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     405 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-cmd.c:447:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     447 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-cmd.c:472:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     472 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-cmd.c:508:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     508 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-cmd.c:564:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     564 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-cmd.c:637:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     637 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^
   include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
     430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
         |                                  ~~~~~~~~~~~~~~~~~~~ ^~~~
   drivers/char/tpm/tpm2-cmd.c:712:21: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
     712 |         CLASS_TPM_BUF(buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^

include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
| ~~~~~~~~~~~~~~~~~~~ ^~~~
12 warnings generated.
--
In file included from drivers/char/tpm/tpm-sysfs.c:16:
In file included from drivers/char/tpm/tpm.h:28:
include/linux/tpm_eventlog.h:167:6: warning: variable 'mapping_size' set but not used [-Wunused-but-set-variable]

     167 |         int mapping_size;
         |             ^
>> drivers/char/tpm/tpm-sysfs.c:35:25: warning: implicit conversion from 'unsigned long' to 'u16' (aka 'unsigned short') changes value from 65536 to 0 [-Wconstant-conversion]
      35 |         CLASS_TPM_BUF(tpm_buf, PAGE_SIZE);
         |         ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
   include/vdso/page.h:15:30: note: expanded from macro 'PAGE_SIZE'
      15 | #define PAGE_SIZE       (_AC(1,UL) << CONFIG_PAGE_SHIFT)
         |                                    ^

include/linux/tpm.h:430:54: note: expanded from macro 'CLASS_TPM_BUF'
430 | #define CLASS_TPM_BUF(buf, size) CLASS(tpm_buf, buf)(size)
| ~~~~~~~~~~~~~~~~~~~ ^~~~
2 warnings generated.

vim +326 drivers/char/tpm/tpm1-cmd.c

313
314 /**
315 * tpm1_startup() - turn on the TPM
316 * @chip: TPM chip to use
317 *
318 * Normally the firmware should start the TPM. This function is provided as a
319 * workaround if this does not happen. A legal case for this could be for
320 * example when a TPM emulator is used.
321 *
322 * Return: same as tpm_transmit_cmd()
323 */
324 static int tpm1_startup(struct tpm_chip *chip)
325 {

 > 326		CLASS_TPM_BUF(buf, PAGE_SIZE);

327 int rc;
328
329 if (!buf)
330 return -ENOMEM;
331
332 dev_info(&chip->dev, "starting up the TPM manually\n");
333
334 tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP);
335 tpm_buf_append_u16(buf, TPM_ST_CLEAR);
336
337 rc = tpm_transmit_cmd(chip, buf, 0, "attempting to start the TPM");
338 return rc;
339 }
340

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw
In reply to Jarkko Sakkinen

Hi Jarkko,

kernel test robot noticed the following build warnings:

https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Jarkko-Sakkinen/tpm-Cleanup-class-for-tpm_buf/20250626-182148
base:   char-misc/char-misc-testing
patch link:    https://lore.kernel.org/r/20250626101935.1007898-1-jarkko%40kernel.org
patch subject: [PATCH v2] tpm: Cleanup class for tpm_buf
config: x86_64-randconfig-161-20250627 (https://download.01.org/0day-ci/archive/20250628/[email protected]/config)
compiler: clang version 20.1.7 (https://github.com/llvm/llvm-project 6146a88f60492b520a36f8f8f3231e15f3cc6082)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
drivers/char/tpm/tpm2-cmd.c:575 tpm2_get_pcr_allocation() warn: iterator 'i' not incremented

vim +/i +575 drivers/char/tpm/tpm2-cmd.c

fa4f99c05320eb2 Nayna Jain      2019-07-11  562  ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
1db15344f874f65 Nayna Jain      2017-01-30  563  {
ce255545c9add8c Jarkko Sakkinen 2025-06-26  564  	CLASS_TPM_BUF(buf, PAGE_SIZE);
1db15344f874f65 Nayna Jain      2017-01-30  565  	struct tpm2_pcr_selection pcr_selection;
1db15344f874f65 Nayna Jain      2017-01-30  566  	void *marker;
1db15344f874f65 Nayna Jain      2017-01-30  567  	void *end;
1db15344f874f65 Nayna Jain      2017-01-30  568  	void *pcr_select_offset;
1db15344f874f65 Nayna Jain      2017-01-30  569  	u32 sizeof_pcr_selection;
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  570  	u32 nr_possible_banks;
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  571  	u32 nr_alloc_banks = 0;
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  572  	u16 hash_alg;
1db15344f874f65 Nayna Jain      2017-01-30  573  	u32 rsp_len;
1db15344f874f65 Nayna Jain      2017-01-30  574  	int rc;
1db15344f874f65 Nayna Jain      2017-01-30 @575  	int i = 0;
1db15344f874f65 Nayna Jain      2017-01-30  576  
ce255545c9add8c Jarkko Sakkinen 2025-06-26  577  	if (!buf)
ce255545c9add8c Jarkko Sakkinen 2025-06-26  578  		return -ENOMEM;
1db15344f874f65 Nayna Jain      2017-01-30  579  
ce255545c9add8c Jarkko Sakkinen 2025-06-26  580  	tpm_buf_reset(buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
ce255545c9add8c Jarkko Sakkinen 2025-06-26  581  	tpm_buf_append_u32(buf, TPM2_CAP_PCRS);
ce255545c9add8c Jarkko Sakkinen 2025-06-26  582  	tpm_buf_append_u32(buf, 0);
ce255545c9add8c Jarkko Sakkinen 2025-06-26  583  	tpm_buf_append_u32(buf, 1);
1db15344f874f65 Nayna Jain      2017-01-30  584  
ce255545c9add8c Jarkko Sakkinen 2025-06-26  585  	rc = tpm_transmit_cmd(chip, buf, 9, "get tpm pcr allocation");
1db15344f874f65 Nayna Jain      2017-01-30  586  	if (rc)
ce255545c9add8c Jarkko Sakkinen 2025-06-26  587  		return rc;
1db15344f874f65 Nayna Jain      2017-01-30  588  
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  589  	nr_possible_banks = be32_to_cpup(
ce255545c9add8c Jarkko Sakkinen 2025-06-26  590  		(__be32 *)&buf->data[TPM_HEADER_SIZE + 5]);
1db15344f874f65 Nayna Jain      2017-01-30  591  
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  592  	chip->allocated_banks = kcalloc(nr_possible_banks,
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  593  					sizeof(*chip->allocated_banks),
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  594  					GFP_KERNEL);
ce255545c9add8c Jarkko Sakkinen 2025-06-26  595  	if (!chip->allocated_banks)
ce255545c9add8c Jarkko Sakkinen 2025-06-26  596  		return -ENOMEM;
1db15344f874f65 Nayna Jain      2017-01-30  597  
ce255545c9add8c Jarkko Sakkinen 2025-06-26  598  	marker = &buf->data[TPM_HEADER_SIZE + 9];
1db15344f874f65 Nayna Jain      2017-01-30  599  
ce255545c9add8c Jarkko Sakkinen 2025-06-26  600  	rsp_len = be32_to_cpup((__be32 *)&buf->data[2]);
ce255545c9add8c Jarkko Sakkinen 2025-06-26  601  	end = &buf->data[rsp_len];
1db15344f874f65 Nayna Jain      2017-01-30  602  
ce255545c9add8c Jarkko Sakkinen 2025-06-26  603  	return rc;
                                                        ^^^^^^^^^^

Presumably this was not intentional?

bcfff8384f6c4e6 Roberto Sassu   2019-02-06  604  	for (i = 0; i < nr_possible_banks; i++) {
1db15344f874f65 Nayna Jain      2017-01-30  605  		pcr_select_offset = marker +
1db15344f874f65 Nayna Jain      2017-01-30  606  			offsetof(struct tpm2_pcr_selection, size_of_select);
ce255545c9add8c Jarkko Sakkinen 2025-06-26  607  		if (pcr_select_offset >= end)
ce255545c9add8c Jarkko Sakkinen 2025-06-26  608  			return -EFAULT;
1db15344f874f65 Nayna Jain      2017-01-30  609  
1db15344f874f65 Nayna Jain      2017-01-30  610  		memcpy(&pcr_selection, marker, sizeof(pcr_selection));
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  611  		hash_alg = be16_to_cpu(pcr_selection.hash_alg);
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  612  
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  613  		pcr_select_offset = memchr_inv(pcr_selection.pcr_select, 0,
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  614  					       pcr_selection.size_of_select);
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  615  		if (pcr_select_offset) {
879b589210a9a0c Roberto Sassu   2019-02-06  616  			chip->allocated_banks[nr_alloc_banks].alg_id = hash_alg;
879b589210a9a0c Roberto Sassu   2019-02-06  617  
879b589210a9a0c Roberto Sassu   2019-02-06  618  			rc = tpm2_init_bank_info(chip, nr_alloc_banks);
879b589210a9a0c Roberto Sassu   2019-02-06  619  			if (rc < 0)
ce255545c9add8c Jarkko Sakkinen 2025-06-26  620  				return rc;
879b589210a9a0c Roberto Sassu   2019-02-06  621  
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  622  			nr_alloc_banks++;
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  623  		}
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  624  
1db15344f874f65 Nayna Jain      2017-01-30  625  		sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
1db15344f874f65 Nayna Jain      2017-01-30  626  			sizeof(pcr_selection.size_of_select) +
1db15344f874f65 Nayna Jain      2017-01-30  627  			pcr_selection.size_of_select;
1db15344f874f65 Nayna Jain      2017-01-30  628  		marker = marker + sizeof_pcr_selection;
1db15344f874f65 Nayna Jain      2017-01-30  629  	}
1db15344f874f65 Nayna Jain      2017-01-30  630  
bcfff8384f6c4e6 Roberto Sassu   2019-02-06  631  	chip->nr_allocated_banks = nr_alloc_banks;
ce255545c9add8c Jarkko Sakkinen 2025-06-26  632  	return 0;
1db15344f874f65 Nayna Jain      2017-01-30  633  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#6 lore raw
In reply to Jarkko Sakkinen
Quoted 11 lines · Jarkko Sakkinen

From: Jarkko Sakkinen <[email protected]>

Create a cleanup class for struct tpm_buf using DEFINE_CLASS(), which will guarantee that the heap allocated memory will be freed automatically for the transient instances of this structure, when they go out of scope.

Wrap this all into help macro CLASS_TPM_BUF().

A TPM buffer can now be declared trivially:

    CLASS_TPM_BUF(buf, buf_size);

Right, so learning this while doing and probably DEFINE_CLASS() would be a bad idea :-) There's a better fit in cleanup.h: DEFINE_FREE() and __free().

Given thatintroduction of tpm_buf_alloc() wipes out tpm_buf_destroy(), we don't need to create any new wrappers with DEFINE_FREE() as linux/slab.h has kfree() covered already.

This leads up to "one step backwards" solution i.e., explicitly call tpm_buf_alloc() and implictly destroy (null checks are left out from examples):

	struct tpm_buf *buf __free(kfree) = tpm_buf_alloc();
	/* 
	 * I dropped buf_size as it will be gone in v3 as requested by
	 * James earlier.
	 */

	 /* Or: */
	struct tpm_buf *buf2 __free(kfree) = NULL;

	/* ... */
	buf2 = tpm_buf_alloc();

OFF-TOPIC: while doing this patch I noticed maybe 3-5 location where we have do this:

1. Init something that is more complex to rollback than rolling back tpm_buf (which is kfree).
2. Init tpm_buf.
3. After this guaranteed success.

Only reason for doing the rollback for the "more complex to rollback thing" is that stupid placement of tpm_buf_init(). There's no additional conditionally failing steps after it.

I need to relocate these code blocks and do a reorders as split patches and place them to the head of the patch set.

This was mostly reminder for myself :-)

BR, Jarkko

#7 lore raw
In reply to Jarkko Sakkinen
kernel test robot noticed the following build warnings:

Hi Jarkko,

kernel test robot noticed the following build warnings:

[auto build test WARNING on char-misc/char-misc-testing]
[also build test WARNING on char-misc/char-misc-next char-misc/char-misc-linus linus/master v6.16-rc3 next-20250627] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Jarkko-Sakkinen/tpm-Cleanup-class-for-tpm_buf/20250626-182148
base:   char-misc/char-misc-testing
patch link:    https://lore.kernel.org/r/20250626101935.1007898-1-jarkko%40kernel.org
patch subject: [PATCH v2] tpm: Cleanup class for tpm_buf
config: sh-randconfig-r111-20250629 (https://download.01.org/0day-ci/archive/20250630/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 12.4.0
reproduce: (https://download.01.org/0day-ci/archive/20250630/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> security/keys/trusted-keys/trusted_tpm2.c:244:9: sparse: sparse: cast truncates bits from constant value (10000 becomes 0)

security/keys/trusted-keys/trusted_tpm2.c:245:9: sparse: sparse: cast truncates bits from constant value (10000 becomes 0) security/keys/trusted-keys/trusted_tpm2.c:381:9: sparse: sparse: cast truncates bits from constant value (10000 becomes 0) security/keys/trusted-keys/trusted_tpm2.c:482:9: sparse: sparse: cast truncates bits from constant value (10000 becomes 0)
--
security/keys/trusted-keys/trusted_tpm1.c:194:19: sparse: sparse: cast to restricted __be32 security/keys/trusted-keys/trusted_tpm1.c:195:15: sparse: sparse: cast to restricted __be16 security/keys/trusted-keys/trusted_tpm1.c:282:19: sparse: sparse: cast to restricted __be32 security/keys/trusted-keys/trusted_tpm1.c:283:15: sparse: sparse: cast to restricted __be16 security/keys/trusted-keys/trusted_tpm1.c:430:21: sparse: sparse: cast to restricted __be32 security/keys/trusted-keys/trusted_tpm1.c:454:19: sparse: sparse: cast to restricted __be32
security/keys/trusted-keys/trusted_tpm1.c:519:17: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned int [usertype] ordinal @@ got restricted __be32 [usertype] @@
security/keys/trusted-keys/trusted_tpm1.c:519:17: sparse: expected unsigned int [usertype] ordinal
security/keys/trusted-keys/trusted_tpm1.c:519:17: sparse: got restricted __be32 [usertype]
security/keys/trusted-keys/trusted_tpm1.c:520:17: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned int [usertype] datsize @@ got restricted __be32 [usertype] @@
security/keys/trusted-keys/trusted_tpm1.c:520:17: sparse: expected unsigned int [usertype] datsize
security/keys/trusted-keys/trusted_tpm1.c:520:17: sparse: got restricted __be32 [usertype]
security/keys/trusted-keys/trusted_tpm1.c:521:17: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned int [usertype] pcrsize @@ got restricted __be32 [usertype] @@
security/keys/trusted-keys/trusted_tpm1.c:521:17: sparse: expected unsigned int [usertype] pcrsize
security/keys/trusted-keys/trusted_tpm1.c:521:17: sparse: got restricted __be32 [usertype]
security/keys/trusted-keys/trusted_tpm1.c:567:24: sparse: sparse: cast to restricted __be32 security/keys/trusted-keys/trusted_tpm1.c:568:23: sparse: sparse: cast to restricted __be32
security/keys/trusted-keys/trusted_tpm1.c:620:17: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned int [usertype] ordinal @@ got restricted __be32 [usertype] @@
security/keys/trusted-keys/trusted_tpm1.c:620:17: sparse: expected unsigned int [usertype] ordinal
security/keys/trusted-keys/trusted_tpm1.c:620:17: sparse: got restricted __be32 [usertype]
security/keys/trusted-keys/trusted_tpm1.c:659:20: sparse: sparse: cast to restricted __be32

>> security/keys/trusted-keys/trusted_tpm1.c:359:9: sparse: sparse: cast truncates bits from constant value (10000 becomes 0)

security/keys/trusted-keys/trusted_tpm1.c:680:9: sparse: sparse: cast truncates bits from constant value (10000 becomes 0) security/keys/trusted-keys/trusted_tpm1.c:704:9: sparse: sparse: cast truncates bits from constant value (10000 becomes 0)

vim +244 security/keys/trusted-keys/trusted_tpm2.c

230
231 /**
232 * tpm2_seal_trusted() - seal the payload of a trusted key
233 *
234 * @chip: TPM chip to use
235 * @payload: the key data in clear and encrypted form
236 * @options: authentication values and other options
237 *
238 * Return: < 0 on error and 0 on success.
239 */
240 int tpm2_seal_trusted(struct tpm_chip *chip,
241 struct trusted_key_payload *payload,
242 struct trusted_key_options *options)
243 {

 > 244		CLASS_TPM_BUF(buf, PAGE_SIZE);

245 CLASS_TPM_BUF(sized, PAGE_SIZE);
246 off_t offset = TPM_HEADER_SIZE;
247 int blob_len = 0;
248 u32 hash;
249 u32 flags;
250 int i;
251 int rc;
252
253 if (!buf || !sized)
254 return -ENOMEM;
255
256 for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
257 if (options->hash == tpm2_hash_map[i].crypto_id) {
258 hash = tpm2_hash_map[i].tpm_id;
259 break;
260 }
261 }
262
263 if (i == ARRAY_SIZE(tpm2_hash_map))
264 return -EINVAL;
265
266 if (!options->keyhandle)
267 return -EINVAL;
268
269 rc = tpm_try_get_ops(chip);
270 if (rc)
271 return rc;
272
273 rc = tpm2_start_auth_session(chip);
274 if (rc)
275 goto out_put;
276
277 tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
278 tpm_buf_reset_sized(sized);
279 tpm_buf_append_name(chip, buf, options->keyhandle, NULL);
280 tpm_buf_append_hmac_session(chip, buf, TPM2_SA_DECRYPT,
281 options->keyauth, TPM_DIGEST_SIZE);
282
283 /* sensitive */
284 tpm_buf_append_u16(sized, options->blobauth_len);
285
286 if (options->blobauth_len)
287 tpm_buf_append(sized, options->blobauth, options->blobauth_len);
288
289 tpm_buf_append_u16(sized, payload->key_len);
290 tpm_buf_append(sized, payload->key, payload->key_len);
291 tpm_buf_append(buf, sized->data, sized->length);
292
293 /* public */
294 tpm_buf_reset_sized(sized);
295 tpm_buf_append_u16(sized, TPM_ALG_KEYEDHASH);
296 tpm_buf_append_u16(sized, hash);
297
298 /* key properties */
299 flags = 0;
300 flags |= options->policydigest_len ? 0 : TPM2_OA_USER_WITH_AUTH;
301 flags |= payload->migratable ? 0 : (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT);
302 tpm_buf_append_u32(sized, flags);
303
304 /* policy */
305 tpm_buf_append_u16(sized, options->policydigest_len);
306 if (options->policydigest_len)
307 tpm_buf_append(sized, options->policydigest, options->policydigest_len);
308
309 /* public parameters */
310 tpm_buf_append_u16(sized, TPM_ALG_NULL);
311 tpm_buf_append_u16(sized, 0);
312
313 tpm_buf_append(buf, sized->data, sized->length);
314
315 /* outside info */
316 tpm_buf_append_u16(buf, 0);
317
318 /* creation PCR */
319 tpm_buf_append_u32(buf, 0);
320
321 if (buf->flags & TPM_BUF_OVERFLOW) {
322 rc = -E2BIG;
323 tpm2_end_auth_session(chip);
324 goto out;
325 }
326
327 tpm_buf_fill_hmac_session(chip, buf);
328 rc = tpm_transmit_cmd(chip, buf, 4, "sealing data");
329 rc = tpm_buf_check_hmac_response(chip, buf, rc);
330 if (rc)
331 goto out;
332
333 blob_len = tpm_buf_read_u32(buf, &offset);
334 if (blob_len > MAX_BLOB_SIZE || buf->flags & TPM_BUF_BOUNDARY_ERROR) {
335 rc = -E2BIG;
336 goto out;
337 }
338 if (buf->length - offset < blob_len) {
339 rc = -EFAULT;
340 goto out;
341 }
342
343 blob_len = tpm2_key_encode(payload, options, &buf->data[offset],
344 blob_len);
345
346 out:
347 if (rc > 0) {
348 if (tpm2_rc_value(rc) == TPM2_RC_HASH)
349 rc = -EINVAL;
350 else
351 rc = -EPERM;
352 }
353 if (blob_len < 0)
354 rc = blob_len;
355 else
356 payload->blob_len = blob_len;
357
358 out_put:
359 tpm_put_ops(chip);
360 return rc;
361 }
362

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki