Skip to contentSkip to conversation

tpm: add send_recv() op and use it in tpm_ftpm_tee driver

PATCH In review Stefano Garzarella 4 participants 15 messages Mar 20, 2025 – Mar 27, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover tpm: add send_recv() op and use it in tpm_ftpm_tee driver

This series is a follow-up to the discussion we had about whether or not to add send_recv() op in tpm_class_ops[1].

Some devices do not support interrupts and provide a single operation to send the command and receive the response on the same buffer. In order to simplify these drivers and avoid temporary buffers to be used between the .send() and .recv() callbacks, introduce a new callback send_recv(). This was suggested by Jason Gunthorpe while reviewing the new SVSM vTPM driver, but the same callback can be used also for the fTPM driver to simplify it a bit (second patch of this series).

The RFC version of this series also contained changes to the SVSM vTPM driver (still under review). Those changes will be integrated directly into that series if we decide to merge this.

Changelog
- RFC -> v1
RFC: https://lore.kernel.org/linux-integrity/[email protected]/
- added Jens' T-b on ftpm driver
- removed last patch since [2] is not yet merged and will require a new version

[1] https://lore.kernel.org/linux-integrity/[email protected]/
[2] https://lore.kernel.org/linux-integrity/[email protected]/

Stefano Garzarella (2):
tpm: add send_recv() op in tpm_class_ops
tpm/tpm_ftpm_tee: use send_recv() op

 drivers/char/tpm/tpm_ftpm_tee.h  |  4 --
 include/linux/tpm.h              |  2 +
 drivers/char/tpm/tpm-interface.c |  7 +++
 drivers/char/tpm/tpm_ftpm_tee.c  | 86 ++++++++------------------------
 4 files changed, 30 insertions(+), 69 deletions(-)

11,841 lines not shown · raw message

Part of this message is not shown here. View the raw message.

#1 lore raw

PATCH 1/2 tpm: add send_recv() op in tpm_class_ops

From: Stefano Garzarella <[email protected]>

Some devices do not support interrupts and provide a single operation to send the command and receive the response on the same buffer.

To support this scenario, a driver could set TPM_CHIP_FLAG_IRQ in the chip's flags to get recv() to be called immediately after send() in tpm_try_transmit(), or it needs to implement .status() to return 0, and set both .req_complete_mask and .req_complete_val to 0.

In order to simplify these drivers and avoid temporary buffers to be used between the .send() and .recv() callbacks, introduce a new callback send_recv(). If that callback is defined, it is called in tpm_try_transmit() to send the command and receive the response on the same buffer in a single call.

---
 include/linux/tpm.h              | 2 ++
 drivers/char/tpm/tpm-interface.c | 7 +++++++
 2 files changed, 9 insertions(+)
include/linux/tpm.h +2 -0
index 6c3125300c00..4e796b8726b5 100644
@@ -88,6 +88,8 @@ struct tpm_class_ops {
 	bool (*req_canceled)(struct tpm_chip *chip, u8 status);
 	int (*recv) (struct tpm_chip *chip, u8 *buf, size_t len);
 	int (*send) (struct tpm_chip *chip, u8 *buf, size_t len);
+	int (*send_recv)(struct tpm_chip *chip, u8 *buf, size_t buf_len,
+			 size_t cmd_len);
 	void (*cancel) (struct tpm_chip *chip);
 	u8 (*status) (struct tpm_chip *chip);
 	void (*update_timeouts)(struct tpm_chip *chip,
drivers/char/tpm/tpm-interface.c +7 -0
index f62f7871edbd..7f4e01790352 100644
@@ -82,6 +82,12 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
 		return -E2BIG;
 	}
 
+	if (chip->ops->send_recv) {
+		rc = 0;
+		len = chip->ops->send_recv(chip, buf, bufsiz, count);
+		goto out_send_recv;
+	}
+
 	rc = chip->ops->send(chip, buf, count);
 	if (rc < 0) {
 		if (rc != -EPIPE)
@@ -124,6 +130,7 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
 
 out_recv:
 	len = chip->ops->recv(chip, buf, bufsiz);
+out_send_recv:
 	if (len < 0) {
 		rc = len;
 		dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc);
Signature · 1 line
2.48.1
maintainer
#2 lore raw

Re: [PATCH 1/2] tpm: add send_recv() op in tpm_class_ops

Quoted 11 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

Some devices do not support interrupts and provide a single operation to send the command and receive the response on the same buffer.

To support this scenario, a driver could set TPM_CHIP_FLAG_IRQ in the chip's flags to get recv() to be called immediately after send() in tpm_try_transmit(), or it needs to implement .status() to return 0, and set both .req_complete_mask and .req_complete_val to 0.

In order to simplify these drivers and avoid temporary buffers to be

Simplification can be addressed with no callback changes:

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

I also noticed that tpm_ftpm_tee initalized req_complete_mask and req_complete_val explictly while they would be already implicitly zero.

So it reduces this just a matter of getting rid off the extra buffer.

Quoted 4 lines

used between the .send() and .recv() callbacks, introduce a new callback send_recv(). If that callback is defined, it is called in tpm_try_transmit() to send the command and receive the response on the same buffer in a single call.

I don't find anything in the commit message addressing buf_len an cmd_len (vs "just len"). Why two lengths are required?

Not completely rejecting but this explanation is incomplete.

BR, Jarkko

#3 lore raw

Re: [PATCH 1/2] tpm: add send_recv() op in tpm_class_ops

Quoted 15 lines · Jarkko Sakkinen
Quoted 11 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

Some devices do not support interrupts and provide a single operation to send the command and receive the response on the same buffer.

To support this scenario, a driver could set TPM_CHIP_FLAG_IRQ in the chip's flags to get recv() to be called immediately after send() in tpm_try_transmit(), or it needs to implement .status() to return 0, and set both .req_complete_mask and .req_complete_val to 0.

In order to simplify these drivers and avoid temporary buffers to be

Simplification can be addressed with no callback changes:

Quoted 8 lines

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

I also noticed that tpm_ftpm_tee initalized req_complete_mask and req_complete_val explictly while they would be already implicitly zero.

So it reduces this just a matter of getting rid off the extra buffer.

Yep, as mentioned I think your patch should go either way. So here I can rephrase and put the emphasis on the temporary buffer and the driver simplification.

Quoted 10 lines
Quoted 5 lines

used between the .send() and .recv() callbacks, introduce a new callback send_recv(). If that callback is defined, it is called in tpm_try_transmit() to send the command and receive the response on the same buffer in a single call.

I don't find anything in the commit message addressing buf_len an cmd_len (vs "just len"). Why two lengths are required?

Not completely rejecting but this explanation is incomplete.

Right.

The same buffer is used as input and output.
For input, the buffer contains the command (cmd_len) but the driver can use the entire buffer for output (buf_len).
It's basically the same as in tpm_try_transmit(), but we avoid having to parse the header in each driver since we already do that in tpm_try_transmit().

In summary cmd_len = count = be32_to_cpu(header->length).

I admit I'm not good with names, would you prefer a different name or is it okay to explain it better in the commit?

My idea is to add this:

buf is used as input and output. It contains the command (cmd_len bytes) as input. The driver will be able to use the entire buffer (buf_len bytes) for the response as output. Passing cmd_len is an optimization to avoid having to access the command header again in each driver and check it.

WDYT?

Thanks,
Stefano

maintainer
#4 lore raw

Re: [PATCH 1/2] tpm: add send_recv() op in tpm_class_ops

Quoted 22 lines · Stefano Garzarella
Quoted 21 lines · Jarkko Sakkinen
Quoted 11 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

Some devices do not support interrupts and provide a single operation to send the command and receive the response on the same buffer.

To support this scenario, a driver could set TPM_CHIP_FLAG_IRQ in the chip's flags to get recv() to be called immediately after send() in tpm_try_transmit(), or it needs to implement .status() to return 0, and set both .req_complete_mask and .req_complete_val to 0.

In order to simplify these drivers and avoid temporary buffers to be

Simplification can be addressed with no callback changes:

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

I also noticed that tpm_ftpm_tee initalized req_complete_mask and req_complete_val explictly while they would be already implicitly zero.

Quoted 6 lines
Quoted 2 lines

So it reduces this just a matter of getting rid off the extra buffer.

Yep, as mentioned I think your patch should go either way. So here I can rephrase and put the emphasis on the temporary buffer and the driver simplification.

Yes. Removing extra copy is a goal that can only make sense!

Quoted 26 lines
Quoted 11 lines
Quoted 6 lines

used between the .send() and .recv() callbacks, introduce a new callback send_recv(). If that callback is defined, it is called in tpm_try_transmit() to send the command and receive the response on the same buffer in a single call.

I don't find anything in the commit message addressing buf_len an cmd_len (vs "just len"). Why two lengths are required?

Not completely rejecting but this explanation is incomplete.

Right.

The same buffer is used as input and output.
For input, the buffer contains the command (cmd_len) but the driver can use the entire buffer for output (buf_len).
It's basically the same as in tpm_try_transmit(), but we avoid having to parse the header in each driver since we already do that in tpm_try_transmit().

In summary cmd_len = count = be32_to_cpu(header->length).

I admit I'm not good with names, would you prefer a different name or is it okay to explain it better in the commit?

Quoted 7 lines

My idea is to add this:

buf is used as input and output. It contains the command (cmd_len bytes) as input. The driver will be able to use the entire buffer (buf_len bytes) for the response as output. Passing cmd_len is an optimization to avoid having to access the command header again in each driver and check it.

This makes more sense. Maybe we could name them as buf_size and cmd_len to further make dead obvious the use and purpose.

Quoted 2 lines

WDYT?

I just want to get this done right if it is done at all, so here's one more suggestion:

1. Add TPM_CHIP_FLAG_SYNC
2. Update send() parameters.

You don't have to do anything smart with the new parameter other than add it to leaf drivers. It makes the first patch bit more involved but this way we end up keeping the callback interface as simple as it was.

I'm also thinking that for async case do we actually need all those complicated masks etc. or could we simplify that side but it is definitely out-of-scope for this patch set (no need to worry about this).

Quoted 5 lines

Thanks,
Stefano

BR, Jarkko

#5 lore raw

Re: [PATCH 1/2] tpm: add send_recv() op in tpm_class_ops

Quoted 60 lines · Jarkko Sakkinen
Quoted 22 lines · Stefano Garzarella
Quoted 21 lines · Jarkko Sakkinen
Quoted 11 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

Some devices do not support interrupts and provide a single operation to send the command and receive the response on the same buffer.

To support this scenario, a driver could set TPM_CHIP_FLAG_IRQ in the chip's flags to get recv() to be called immediately after send() in tpm_try_transmit(), or it needs to implement .status() to return 0, and set both .req_complete_mask and .req_complete_val to 0.

In order to simplify these drivers and avoid temporary buffers to be

Simplification can be addressed with no callback changes:

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

I also noticed that tpm_ftpm_tee initalized req_complete_mask and req_complete_val explictly while they would be already implicitly zero.

Quoted 6 lines
Quoted 2 lines

So it reduces this just a matter of getting rid off the extra buffer.

Yep, as mentioned I think your patch should go either way. So here I can rephrase and put the emphasis on the temporary buffer and the driver simplification.

Yes. Removing extra copy is a goal that can only make sense!

Quoted 28 lines
Quoted 11 lines
Quoted 6 lines

used between the .send() and .recv() callbacks, introduce a new callback send_recv(). If that callback is defined, it is called in tpm_try_transmit() to send the command and receive the response on the same buffer in a single call.

I don't find anything in the commit message addressing buf_len an cmd_len (vs "just len"). Why two lengths are required?

Not completely rejecting but this explanation is incomplete.

Right.

The same buffer is used as input and output.
For input, the buffer contains the command (cmd_len) but the driver can use the entire buffer for output (buf_len).
It's basically the same as in tpm_try_transmit(), but we avoid having to parse the header in each driver since we already do that in tpm_try_transmit().

In summary cmd_len = count = be32_to_cpu(header->length).

I admit I'm not good with names, would you prefer a different name or is it okay to explain it better in the commit?

My idea is to add this:

Quoted 8 lines
Quoted 5 lines

buf is used as input and output. It contains the command (cmd_len bytes) as input. The driver will be able to use the entire buffer (buf_len bytes) for the response as output. Passing cmd_len is an optimization to avoid having to access the command header again in each driver and check it.

This makes more sense. Maybe we could name them as buf_size and cmd_len to further make dead obvious the use and purpose.

Yeah, I see. I'll do!

Quoted 9 lines
Quoted 3 lines

WDYT?

I just want to get this done right if it is done at all, so here's one more suggestion:

1. Add TPM_CHIP_FLAG_SYNC
2. Update send() parameters.

So, IIUC something like this:

    int (*send) (struct tpm_chip *chip, u8 *buf, size_t cmd_len, size_t buf_size);

Where buf_size is ignored if the driver doesn't set TPM_CHIP_FLAG_SYNC.

Right?

Quoted 3 lines

You don't have to do anything smart with the new parameter other than add it to leaf drivers.

Okay, this should answer my question :-) (I leave it just to be sure).

Quoted 2 lines

It makes the first patch bit more involved but
this way we end up keeping the callback interface as simple as it was.

Yep, I see.
And maybe I need to change something in tpm_try_transmit() because now send() returns 0 in case of success, but after the change it might return > 0 in case TPM_CHIP_FLAG_SYNC is set. But I will see how to handle this.

Quoted 5 lines

I'm also thinking that for async case do we actually need all those complicated masks etc. or could we simplify that side but it is definitely out-of-scope for this patch set (no need to worry about this).

I see, I can take a look later in another series.

Thanks,
Stefano

#6 lore raw

PATCH 2/2 tpm/tpm_ftpm_tee: use send_recv() op

In reply to Stefano Garzarella

From: Stefano Garzarella <[email protected]>

This driver does not support interrupts, and receiving the response is synchronous with sending the command.

It used an internal buffer to cache the response when .send() is called, and then return it when .recv() is called.

Let's simplify the driver by implementing the new send_recv() op, so that we can also remove the 4KB internal buffer used to cache the response.

---
v1:
- added Jens' T-b

---
 drivers/char/tpm/tpm_ftpm_tee.h |  4 --
 drivers/char/tpm/tpm_ftpm_tee.c | 86 ++++++++-------------------------
 2 files changed, 21 insertions(+), 69 deletions(-)
drivers/char/tpm/tpm_ftpm_tee.h +0 -4
index e39903b7ea07..8d5c3f0d2879 100644
@@ -22,16 +22,12 @@
  * struct ftpm_tee_private - fTPM's private data
  * @chip:     struct tpm_chip instance registered with tpm framework.
  * @session:  fTPM TA session identifier.
- * @resp_len: cached response buffer length.
- * @resp_buf: cached response buffer.
  * @ctx:      TEE context handler.
  * @shm:      Memory pool shared with fTPM TA in TEE.
  */
 struct ftpm_tee_private {
 	struct tpm_chip *chip;
 	u32 session;
-	size_t resp_len;
-	u8 resp_buf[MAX_RESPONSE_SIZE];
 	struct tee_context *ctx;
 	struct tee_shm *shm;
 };
drivers/char/tpm/tpm_ftpm_tee.c +21 -65

134 lines not on this page · raw message

Signature · 1 line
2.48.1
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] tpm/tpm_ftpm_tee: use send_recv() op

Quoted 13 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

This driver does not support interrupts, and receiving the response is synchronous with sending the command.

It used an internal buffer to cache the response when .send() is called, and then return it when .recv() is called.

Let's simplify the driver by implementing the new send_recv() op, so that we can also remove the 4KB internal buffer used to cache the response.

Quoted 8 lines

---
v1:
- added Jens' T-b

---
 drivers/char/tpm/tpm_ftpm_tee.h |  4 --
 drivers/char/tpm/tpm_ftpm_tee.c | 86 ++++++++-------------------------
 2 files changed, 21 insertions(+), 69 deletions(-)

-Sumit

Quoted 167 lines
drivers/char/tpm/tpm_ftpm_tee.h +0 -4
index e39903b7ea07..8d5c3f0d2879 100644
@@ -22,16 +22,12 @@
  * struct ftpm_tee_private - fTPM's private data
  * @chip:     struct tpm_chip instance registered with tpm framework.
  * @session:  fTPM TA session identifier.
- * @resp_len: cached response buffer length.
- * @resp_buf: cached response buffer.
  * @ctx:      TEE context handler.
  * @shm:      Memory pool shared with fTPM TA in TEE.
  */
 struct ftpm_tee_private {
 	struct tpm_chip *chip;
 	u32 session;
-	size_t resp_len;
-	u8 resp_buf[MAX_RESPONSE_SIZE];
 	struct tee_context *ctx;
 	struct tee_shm *shm;
 };
drivers/char/tpm/tpm_ftpm_tee.c +21 -65
index 8d9209dfc384..d472199c0a7b 100644
@@ -31,45 +31,19 @@ static const uuid_t ftpm_ta_uuid =
 		  0x82, 0xCB, 0x34, 0x3F, 0xB7, 0xF3, 0x78, 0x96);
 
 /**
- * ftpm_tee_tpm_op_recv() - retrieve fTPM response.
- * @chip:	the tpm_chip description as specified in driver/char/tpm/tpm.h.
- * @buf:	the buffer to store data.
- * @count:	the number of bytes to read.
- *
- * Return:
- *	In case of success the number of bytes received.
- *	On failure, -errno.
- */
-static int ftpm_tee_tpm_op_recv(struct tpm_chip *chip, u8 *buf, size_t count)
-{
-	struct ftpm_tee_private *pvt_data = dev_get_drvdata(chip->dev.parent);
-	size_t len;
-
-	len = pvt_data->resp_len;
-	if (count < len) {
-		dev_err(&chip->dev,
-			"%s: Invalid size in recv: count=%zd, resp_len=%zd\n",
-			__func__, count, len);
-		return -EIO;
-	}
-
-	memcpy(buf, pvt_data->resp_buf, len);
-	pvt_data->resp_len = 0;
-
-	return len;
-}
-
-/**
- * ftpm_tee_tpm_op_send() - send TPM commands through the TEE shared memory.
+ * ftpm_tee_tpm_op_send_recv() - send TPM commands through the TEE shared memory
+ * and retrieve the response.
  * @chip:	the tpm_chip description as specified in driver/char/tpm/tpm.h
- * @buf:	the buffer to send.
- * @len:	the number of bytes to send.
+ * @buf:	the buffer to send and to store the response.
+ * @buf_len:	the size of the buffer.
+ * @cmd_len:	the number of bytes to send.
  *
  * Return:
- *	In case of success, returns 0.
+ *	In case of success, returns the number of bytes received.
  *	On failure, -errno
  */
-static int ftpm_tee_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t len)
+static int ftpm_tee_tpm_op_send_recv(struct tpm_chip *chip, u8 *buf,
+				     size_t buf_len, size_t cmd_len)
 {
 	struct ftpm_tee_private *pvt_data = dev_get_drvdata(chip->dev.parent);
 	size_t resp_len;
@@ -80,16 +54,15 @@ static int ftpm_tee_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t len)
 	struct tee_param command_params[4];
 	struct tee_shm *shm = pvt_data->shm;
 
-	if (len > MAX_COMMAND_SIZE) {
+	if (cmd_len > MAX_COMMAND_SIZE) {
 		dev_err(&chip->dev,
 			"%s: len=%zd exceeds MAX_COMMAND_SIZE supported by fTPM TA\n",
-			__func__, len);
+			__func__, cmd_len);
 		return -EIO;
 	}
 
 	memset(&transceive_args, 0, sizeof(transceive_args));
 	memset(command_params, 0, sizeof(command_params));
-	pvt_data->resp_len = 0;
 
 	/* Invoke FTPM_OPTEE_TA_SUBMIT_COMMAND function of fTPM TA */
 	transceive_args = (struct tee_ioctl_invoke_arg) {
@@ -103,7 +76,7 @@ static int ftpm_tee_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t len)
 		.attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT,
 		.u.memref = {
 			.shm = shm,
-			.size = len,
+			.size = cmd_len,
 			.shm_offs = 0,
 		},
 	};
@@ -115,7 +88,7 @@ static int ftpm_tee_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t len)
 		return PTR_ERR(temp_buf);
 	}
 	memset(temp_buf, 0, (MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE));
-	memcpy(temp_buf, buf, len);
+	memcpy(temp_buf, buf, cmd_len);
 
 	command_params[1] = (struct tee_param) {
 		.attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT,
@@ -156,38 +129,21 @@ static int ftpm_tee_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t len)
 			__func__, resp_len);
 		return -EIO;
 	}
+	if (resp_len > buf_len) {
+		dev_err(&chip->dev,
+			"%s: Invalid size in recv: buf_len=%zd, resp_len=%zd\n",
+			__func__, buf_len, resp_len);
+		return -EIO;
+	}
 
-	/* sanity checks look good, cache the response */
-	memcpy(pvt_data->resp_buf, temp_buf, resp_len);
-	pvt_data->resp_len = resp_len;
-
-	return 0;
-}
-
-static void ftpm_tee_tpm_op_cancel(struct tpm_chip *chip)
-{
-	/* not supported */
-}
-
-static u8 ftpm_tee_tpm_op_status(struct tpm_chip *chip)
-{
-	return 0;
-}
+	memcpy(buf, temp_buf, resp_len);
 
-static bool ftpm_tee_tpm_req_canceled(struct tpm_chip *chip, u8 status)
-{
-	return false;
+	return resp_len;
 }
 
 static const struct tpm_class_ops ftpm_tee_tpm_ops = {
 	.flags = TPM_OPS_AUTO_STARTUP,
-	.recv = ftpm_tee_tpm_op_recv,
-	.send = ftpm_tee_tpm_op_send,
-	.cancel = ftpm_tee_tpm_op_cancel,
-	.status = ftpm_tee_tpm_op_status,
-	.req_complete_mask = 0,
-	.req_complete_val = 0,
-	.req_canceled = ftpm_tee_tpm_req_canceled,
+	.send_recv = ftpm_tee_tpm_op_send_recv,
 };
 
 /*
Signature · 1 line
2.48.1
maintainer
#8 lore raw

Re: [PATCH 2/2] tpm/tpm_ftpm_tee: use send_recv() op

Quoted 23 lines · Sumit Garg
Quoted 13 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

This driver does not support interrupts, and receiving the response is synchronous with sending the command.

It used an internal buffer to cache the response when .send() is called, and then return it when .recv() is called.

Let's simplify the driver by implementing the new send_recv() op, so that we can also remove the 4KB internal buffer used to cache the response.

Quoted 8 lines

---
v1:
- added Jens' T-b

---
 drivers/char/tpm/tpm_ftpm_tee.h |  4 --
 drivers/char/tpm/tpm_ftpm_tee.c | 86 ++++++++-------------------------
 2 files changed, 21 insertions(+), 69 deletions(-)
Quoted 1 line

I've knowingly even peeked at these patches because of stuff in-flight.

Generally speaking I don't see enough value in complicating callback interface. It's better to handle complications in the leaves (i.e. dictatorship of majority ;-) ).

BR, Jarkko

#9 lore raw

Re: [PATCH 2/2] tpm/tpm_ftpm_tee: use send_recv() op

Quoted 3 lines · Jarkko Sakkinen

Generally speaking I don't see enough value in complicating callback interface. It's better to handle complications in the leaves (i.e. dictatorship of majority ;-) ).

That is very much not the way most driver subsystems view the world. We want to pull logical things into the core code and remove them from drivers to make the drivers simpler and more robust.

The amount of really dumb driver boiler plate that this series obviously removes is exactly the sort of stuff we should be fixing by improving the core code.

The callback interface was never really sanely designed, it was just built around the idea of pulling the timout processing into the core code for TIS hardware. It should be revised to properly match these new HW types that don't have this kind of timeout mechanism.

Jason

maintainer
#10 lore raw

Re: [PATCH 2/2] tpm/tpm_ftpm_tee: use send_recv() op

Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Jarkko Sakkinen

Generally speaking I don't see enough value in complicating callback interface. It's better to handle complications in the leaves (i.e. dictatorship of majority ;-) ).

That is very much not the way most driver subsystems view the world. We want to pull logical things into the core code and remove them from drivers to make the drivers simpler and more robust.

Quoted 8 lines

The amount of really dumb driver boiler plate that this series obviously removes is exactly the sort of stuff we should be fixing by improving the core code.

The callback interface was never really sanely designed, it was just built around the idea of pulling the timout processing into the core code for TIS hardware. It should be revised to properly match these new HW types that don't have this kind of timeout mechanism.

Both TIS and CRB, which are TCG standards and they span to many different types of drivers and busses. I don't have the figures but probably they cover vast majority of the hardware.

We are talking about 39 lines of reduced complexity at the cost of complicating branching at the top level. I doubt that there is either any throughput or latency issues.

What is measurable benefit? The rationale is way way too abstract for me to cope, sorry.

Quoted 1 line

Jason

BR, Jarkko

maintainer
#11 lore raw

Re: [PATCH 2/2] tpm/tpm_ftpm_tee: use send_recv() op

Quoted 24 lines · Jarkko Sakkinen
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Jarkko Sakkinen

Generally speaking I don't see enough value in complicating callback interface. It's better to handle complications in the leaves (i.e. dictatorship of majority ;-) ).

That is very much not the way most driver subsystems view the world. We want to pull logical things into the core code and remove them from drivers to make the drivers simpler and more robust.

Quoted 8 lines

The amount of really dumb driver boiler plate that this series obviously removes is exactly the sort of stuff we should be fixing by improving the core code.

The callback interface was never really sanely designed, it was just built around the idea of pulling the timout processing into the core code for TIS hardware. It should be revised to properly match these new HW types that don't have this kind of timeout mechanism.

Both TIS and CRB, which are TCG standards and they span to many different types of drivers and busses. I don't have the figures but probably they cover vast majority of the hardware.

Quoted 6 lines

We are talking about 39 lines of reduced complexity at the cost of complicating branching at the top level. I doubt that there is either any throughput or latency issues.

What is measurable benefit? The rationale is way way too abstract for me to cope, sorry.

E.g., here's how you can get rid of extra cruft in tpm_ftpm_tee w/o any new callbacks.

BR, Jarkko

From 1125e80ea274a5ec5d5dba32bfce716ce62c5e4a Mon Sep 17 00:00:00 2001
From: Jarkko Sakkinen <[email protected]>
Date: Wed, 26 Mar 2025 17:55:49 +0200
Subject: [PATCH] tpm: Make chip->{status,cancel,req_canceled} opt

tpm_ftpm_tee does not require chip->status, chip->cancel and chip->req_canceled. Make them optional.

---
 drivers/char/tpm/tpm-interface.c | 31 ++++++++++++++++++++++++++++---
 drivers/char/tpm/tpm_ftpm_tee.c  | 18 ------------------
 2 files changed, 28 insertions(+), 21 deletions(-)
drivers/char/tpm/tpm-interface.c +28 -3
index f62f7871edbd..10ba47a882d8 100644
@@ -58,6 +58,30 @@ unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
 }
 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
 
+static void tpm_chip_cancel(struct tpm_chip *chip)
+{
+	if (!chip->ops->cancel)
+		return;
+
+	chip->ops->cancel(chip);
+}
+
+static u8 tpm_chip_status(struct tpm_chip *chip)
+{
+	if (!chip->ops->status)
+		return 0;
+
+	return chip->ops->status(chip);
+}
+
+static bool tpm_chip_req_canceled(struct tpm_chip *chip, u8 status)
+{
+	if (!chip->ops->req_canceled)
+		return false;
+
+	return chip->ops->req_canceled(chip, status);
+}
+
 static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
 {
 	struct tpm_header *header = buf;
@@ -65,6 +89,7 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
 	ssize_t len = 0;
 	u32 count, ordinal;
 	unsigned long stop;
+	u8 status;
 
 	if (bufsiz < TPM_HEADER_SIZE)
 		return -EINVAL;
@@ -104,12 +129,12 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
 
 	stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
 	do {
-		u8 status = chip->ops->status(chip);
+		status = tpm_chip_status(chip);
 		if ((status & chip->ops->req_complete_mask) ==
 		    chip->ops->req_complete_val)
 			goto out_recv;
 
-		if (chip->ops->req_canceled(chip, status)) {
+		if (tpm_chip_req_canceled(chip, status)) {
 			dev_err(&chip->dev, "Operation Canceled\n");
 			return -ECANCELED;
 		}
@@ -118,7 +143,7 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
 		rmb();
 	} while (time_before(jiffies, stop));
 
-	chip->ops->cancel(chip);
+	tpm_chip_cancel(chip);
 	dev_err(&chip->dev, "Operation Timed out\n");
 	return -ETIME;
 
drivers/char/tpm/tpm_ftpm_tee.c +0 -18
index 8d9209dfc384..3732f3623537 100644
@@ -164,30 +164,12 @@ static int ftpm_tee_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t len)
 	return 0;
 }
 
-static void ftpm_tee_tpm_op_cancel(struct tpm_chip *chip)
-{
-	/* not supported */
-}
-
-static u8 ftpm_tee_tpm_op_status(struct tpm_chip *chip)
-{
-	return 0;
-}
-
-static bool ftpm_tee_tpm_req_canceled(struct tpm_chip *chip, u8 status)
-{
-	return false;
-}
-
 static const struct tpm_class_ops ftpm_tee_tpm_ops = {
 	.flags = TPM_OPS_AUTO_STARTUP,
 	.recv = ftpm_tee_tpm_op_recv,
 	.send = ftpm_tee_tpm_op_send,
-	.cancel = ftpm_tee_tpm_op_cancel,
-	.status = ftpm_tee_tpm_op_status,
 	.req_complete_mask = 0,
 	.req_complete_val = 0,
-	.req_canceled = ftpm_tee_tpm_req_canceled,
 };
 
 /*
Signature · 1 line
2.39.5
maintainer
#12 lore raw

Re: [PATCH 2/2] tpm/tpm_ftpm_tee: use send_recv() op

Quoted 29 lines · Jarkko Sakkinen
Quoted 28 lines · Jarkko Sakkinen
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Jarkko Sakkinen

Generally speaking I don't see enough value in complicating callback interface. It's better to handle complications in the leaves (i.e. dictatorship of majority ;-) ).

That is very much not the way most driver subsystems view the world. We want to pull logical things into the core code and remove them from drivers to make the drivers simpler and more robust.

Quoted 8 lines

The amount of really dumb driver boiler plate that this series obviously removes is exactly the sort of stuff we should be fixing by improving the core code.

The callback interface was never really sanely designed, it was just built around the idea of pulling the timout processing into the core code for TIS hardware. It should be revised to properly match these new HW types that don't have this kind of timeout mechanism.

Both TIS and CRB, which are TCG standards and they span to many different types of drivers and busses. I don't have the figures but probably they cover vast majority of the hardware.

We are talking about 39 lines of reduced complexity at the cost of complicating branching at the top level. I doubt that there is either any throughput or latency issues.

Quoted 5 lines
Quoted 2 lines

What is measurable benefit? The rationale is way way too abstract for me to cope, sorry.

E.g., here's how you can get rid of extra cruft in tpm_ftpm_tee w/o any new callbacks.

Measurable benefit: no need to allocate memory buffer.

Let's take that as a starting point ;-)

On that basis I can consider this (i.e. something to measure).

BR, Jarkko

#13 lore raw

Re: [PATCH 2/2] tpm/tpm_ftpm_tee: use send_recv() op

Quoted 30 lines · Jarkko Sakkinen
Quoted 29 lines · Jarkko Sakkinen
Quoted 28 lines · Jarkko Sakkinen
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Jarkko Sakkinen

Generally speaking I don't see enough value in complicating callback interface. It's better to handle complications in the leaves (i.e. dictatorship of majority ;-) ).

That is very much not the way most driver subsystems view the world. We want to pull logical things into the core code and remove them from drivers to make the drivers simpler and more robust.

Quoted 8 lines

The amount of really dumb driver boiler plate that this series obviously removes is exactly the sort of stuff we should be fixing by improving the core code.

The callback interface was never really sanely designed, it was just built around the idea of pulling the timout processing into the core code for TIS hardware. It should be revised to properly match these new HW types that don't have this kind of timeout mechanism.

Both TIS and CRB, which are TCG standards and they span to many different types of drivers and busses. I don't have the figures but probably they cover vast majority of the hardware.

We are talking about 39 lines of reduced complexity at the cost of complicating branching at the top level. I doubt that there is either any throughput or latency issues.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

What is measurable benefit? The rationale is way way too abstract for me to cope, sorry.

E.g., here's how you can get rid of extra cruft in tpm_ftpm_tee w/o any new callbacks.

Yeah, I agree that your patch should go in any case, with send_recv() or not. It's a good cleanup.

Quoted 2 lines

Measurable benefit: no need to allocate memory buffer.

That's right, I read the whole thread before responding, but that's exactly what I wanted to highlight. Implementing send_recv() we could completely remove the buffer for the cache here in tpm_ftpm_tee, simplifying it quite a bit.

In tpm_svsm instead we allocate it while probing anyway to avoid having to allocate it every time, but we could potentially do the same (I don't know if it makes sense honestly). We do this because for SVSM any buffer is fine, as it can access all guest kernel memory, whereas IIUC for ftpm it has to be taken from shared memory.

Quoted 2 lines

Let's take that as a starting point ;-)

Yeah!

Quoted 2 lines

On that basis I can consider this (i.e. something to measure).

Okay, I explain this better in the commit description for the next version!

Thanks,
Stefano

maintainer
#14 lore raw

Re: [PATCH 2/2] tpm/tpm_ftpm_tee: use send_recv() op

Quoted 55 lines · Stefano Garzarella
Quoted 30 lines · Jarkko Sakkinen
Quoted 29 lines · Jarkko Sakkinen
Quoted 28 lines · Jarkko Sakkinen
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Jarkko Sakkinen

Generally speaking I don't see enough value in complicating callback interface. It's better to handle complications in the leaves (i.e. dictatorship of majority ;-) ).

That is very much not the way most driver subsystems view the world. We want to pull logical things into the core code and remove them from drivers to make the drivers simpler and more robust.

Quoted 8 lines

The amount of really dumb driver boiler plate that this series obviously removes is exactly the sort of stuff we should be fixing by improving the core code.

The callback interface was never really sanely designed, it was just built around the idea of pulling the timout processing into the core code for TIS hardware. It should be revised to properly match these new HW types that don't have this kind of timeout mechanism.

Both TIS and CRB, which are TCG standards and they span to many different types of drivers and busses. I don't have the figures but probably they cover vast majority of the hardware.

We are talking about 39 lines of reduced complexity at the cost of complicating branching at the top level. I doubt that there is either any throughput or latency issues.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

What is measurable benefit? The rationale is way way too abstract for me to cope, sorry.

E.g., here's how you can get rid of extra cruft in tpm_ftpm_tee w/o any new callbacks.

Yeah, I agree that your patch should go in any case, with send_recv() or not. It's a good cleanup.

Quoted 2 lines

Measurable benefit: no need to allocate memory buffer.

That's right, I read the whole thread before responding, but that's exactly what I wanted to highlight. Implementing send_recv() we could completely remove the buffer for the cache here in tpm_ftpm_tee, simplifying it quite a bit.

In tpm_svsm instead we allocate it while probing anyway to avoid having to allocate it every time, but we could potentially do the same (I don't know if it makes sense honestly). We do this because for SVSM any buffer is fine, as it can access all guest kernel memory, whereas IIUC for ftpm it has to be taken from shared memory.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

Let's take that as a starting point ;-)

Yeah!

Quoted 2 lines

On that basis I can consider this (i.e. something to measure).

Okay, I explain this better in the commit description for the next version!

Awesome :-) Thanks for the patience with this. I rather precautionaly throw sticks on the road than go through fixes post upstreaming, which is factors more nasty and time-consuming...

Quoted 4 lines

Thanks,
Stefano

BR, Jarkko