All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arun Menon <armenon@redhat.com>
To: marcandre.lureau@redhat.com
Cc: qemu-devel@nongnu.org, "Ani Sinha" <anisinha@redhat.com>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Laurent Vivier" <lvivier@redhat.com>,
	"Zhao Liu" <zhao1.liu@intel.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Stefan Berger" <stefanb@linux.vnet.ibm.com>,
	"Fabiano Rosas" <farosas@suse.de>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Yanan Wang" <wangyanan55@huawei.com>,
	"Stefan Berger" <stefanb@linux.ibm.com>
Subject: Re: [RFC v2 4/7] hw/tpm: Implement TPM CRB chunking logic
Date: Tue, 31 Mar 2026 22:37:06 +0530	[thread overview]
Message-ID: <acv_OgPqHED_rQbq@fedora> (raw)
In-Reply-To: <177452442285.34609.12928739139262714987.b4-review@b4>

Hi Marc-André,

Thank you for the review.

On Thu, Mar 26, 2026 at 03:27:02PM +0400, marcandre.lureau@redhat.com wrote:
> On Thu, 19 Mar 2026 19:23:13 +0530, Arun Menon <armenon@redhat.com> wrote:
> 
> Hi,
> 
> >
> > diff --git a/hw/tpm/tpm_crb.c b/hw/tpm/tpm_crb.c
> > index 5ea1a4a9706..e61c04aee0b 100644
> > --- a/hw/tpm/tpm_crb.c
> > +++ b/hw/tpm/tpm_crb.c
> > @@ -80,6 +82,8 @@ enum crb_ctrl_req {
> >  
> >  enum crb_start {
> >      CRB_START_INVOKE = BIT(0),
> > +    CRB_START_RESP_RETRY = BIT(1),
> 
> Hmm, maybe we should rename that field, since it is "Start" in the spec (it changed?).

The Start has not changed from before.
Yes, I shall make changes to the other 2.

> 
> > +    CRB_START_NEXT_CHUNK = BIT(2),
> >  };
> 
> And follow the spec naming here "RspRetry" -> RSP_RETRY
> 
> > @@ -122,6 +126,68 @@ static uint8_t tpm_crb_get_active_locty(CRBState *s)
> > [ ... skip 23 lines ... ]
> > +            ARRAY_FIELD_DP32(s->regs, CRB_CTRL_STS, tpmSts, 1);
> > +            ARRAY_FIELD_DP32(s->regs, CRB_CTRL_START, invoke, 0);
> > +            ARRAY_FIELD_DP32(s->regs, CRB_CTRL_START, nextChunk, 0);
> > +            tpm_crb_clear_internal_buffers(s);
> > +            error_report("Command size '%d' less than TPM header size '%d'",
> > +                         total_request_size, TPM_HEADER_SIZE);
> 
> Use PRIu32 to avoid sign sign-mismatch

Yes. Will do.

> 
> > [ ... skip 28 lines ... ]
> > +    if (to_copy < CRB_CTRL_CMD_SIZE) {
> > +        memset((guint8 *)mem + to_copy, 0, CRB_CTRL_CMD_SIZE - to_copy);
> > +    }
> > +
> > +    s->response_offset += to_copy;
> > +    memory_region_set_dirty(&s->cmdmem, 0, CRB_CTRL_CMD_SIZE);
> 
> Those lines are repeated below and could be factorized.

Yes, I will move it into a separate function

> 
> > @@ -152,20 +218,48 @@ static void tpm_crb_mmio_write(void *opaque, hwaddr addr,
> > [ ... skip 21 lines ... ]
> > +            if (!(s->regs[R_CRB_CTRL_START] & CRB_START_INVOKE)) {
> > +                if (!tpm_crb_append_command_request(s)) {
> > +                    break;
> > +                }
> > +                ARRAY_FIELD_DP32(s->regs, CRB_CTRL_START, invoke, 1);
> > +                g_byte_array_set_size(s->response_buffer, s->be_buffer_size);
> 
> This pre-allocates the response buffer before the backend has written
> anything. Since response_buffer->len > 0 now, a subsequent nextChunk
> from the guest would enter tpm_crb_fill_command_response() and serve
> uninitialized data.
> 
> Before this patch we would serve guest input data, which wasn't great either.
> 
> 
> We could add a separate flag to track response readiness instead. This
> will need to be migrated too.

Good catch. I did not think of what happens when the guest issues a
subsequent nextChunk while the tpm backend has not yet processed the
command.

I am wondering, checking if the start invoke bit is set will alone
prevent such a situation in the first place.

For example adding the following at the beginning,

case A_CRB_CTRL_START:
    if (s->regs[R_CRB_CTRL_START] & CRB_START_INVOKE) {
        break;
    }

That way, a subsequent nextChunk or rspRetry will not be processed in
the first place. Do you see any problem with this approach? This will
also allow us to keep the migration logic as is.

> 
> > +                s->cmd = (TPMBackendCmd) {
> > +                    .in = s->command_buffer->data,
> > +                    .in_len = s->command_buffer->len,
> > +                    .out = s->response_buffer->data,
> > +                    .out_len = s->response_buffer->len,
> > +                };
> > +                tpm_backend_deliver_request(s->tpmbe, &s->cmd);
> > +            }
> > +        } else if (val & CRB_START_NEXT_CHUNK) {
> > +            /*
> > +             * nextChunk is used both while sending and receiving data.
> > +             * To distinguish between the two, response_buffer is checked
> 
> Missing .

Will correct.

> 
> > +             * If it does not have data, then that means we have not yet
> > +             * sent the command to the tpm backend, and therefore call
> > +             * tpm_crb_append_command_request()
> > +             */
> > +            if (s->response_buffer->len > 0 &&
> > +                s->response_offset < s->response_buffer->len) {
> > +                    tpm_crb_fill_command_response(s);
> 
> Indentation <4

Yes.

> 
> > +            } else {
> > +                if (!tpm_crb_append_command_request(s)) {
> > +                    break;
> > +                }
> > +            }
> > +            ARRAY_FIELD_DP32(s->regs, CRB_CTRL_START, nextChunk, 0);
> > +        } else if (val & CRB_START_RESP_RETRY) {
> > +            if (s->response_buffer->len > 0) {
> 
> I'd suggest adding a trace here.

Sure, will add.

> 
> > @@ -205,13 +299,36 @@ static const MemoryRegionOps tpm_crb_memory_ops = {
> > [ ... skip 15 lines ... ]
> > +
> > +        /*
> > +         * Send the first chunk. Subsequent chunks will be sent using
> > +         * tpm_crb_fill_command_response()
> > +         */
> > +        uint32_t to_copy = MIN(CRB_CTRL_CMD_SIZE, s->response_buffer->len);
> 
> QEMU coding style: declaration not mixed with statements.

I will add this to the top of the block.

> 
> > +        memcpy(mem, s->response_buffer->data, to_copy);
> > +
> > +        if (to_copy < CRB_CTRL_CMD_SIZE) {
> > +            memset((guint8 *)mem + to_copy, 0, CRB_CTRL_CMD_SIZE - to_copy);
> > +        }
> > +        s->response_offset += to_copy;
> >      }
> >      memory_region_set_dirty(&s->cmdmem, 0, CRB_CTRL_CMD_SIZE);
> 
> Consider factorizing
> 
> > +    ARRAY_FIELD_DP32(s->regs, CRB_CTRL_START, invoke, 0);
> 
> redundant clear

yes, shall remove.

> 
> -- 
> Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> 

Regards,
Arun Menon



  reply	other threads:[~2026-03-31 17:08 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-19 13:53 [RFC v2 0/7] hw/tpm: CRB chunking capability to handle PQC Arun Menon
2026-03-19 13:53 ` [RFC v2 1/7] hw/tpm: Add TPM CRB chunking fields Arun Menon
2026-03-19 13:53 ` [RFC v2 2/7] hw/tpm: Refactor CRB_CTRL_START register access Arun Menon
2026-03-19 13:53 ` [RFC v2 3/7] hw/tpm: Add internal buffer state for chunking Arun Menon
2026-03-26 11:27   ` marcandre.lureau
2026-03-19 13:53 ` [RFC v2 4/7] hw/tpm: Implement TPM CRB chunking logic Arun Menon
2026-03-26 11:27   ` marcandre.lureau
2026-03-31 17:07     ` Arun Menon [this message]
2026-03-19 13:53 ` [RFC v2 5/7] test/qtest: Add test for tpm crb chunking Arun Menon
2026-03-26 11:27   ` marcandre.lureau
2026-03-26 11:32     ` Marc-André Lureau
2026-03-27 22:10   ` Stefan Berger
2026-03-19 13:53 ` [RFC v2 6/7] hw/tpm: Add support for VM migration with TPM CRB chunking Arun Menon
2026-03-26 11:27   ` marcandre.lureau
2026-04-02 15:22     ` Arun Menon
2026-03-19 13:53 ` [RFC v2 7/7] hw/tpm: Increase TPM TIS max buffer size to 8192 Arun Menon
2026-03-20 18:57   ` Stefan Berger
2026-03-31 19:31     ` Stefan Berger
2026-04-01  5:44       ` Arun Menon
2026-04-01 13:43         ` Stefan Berger
2026-04-01 14:05           ` Stefan Berger

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=acv_OgPqHED_rQbq@fedora \
    --to=armenon@redhat.com \
    --cc=anisinha@redhat.com \
    --cc=farosas@suse.de \
    --cc=imammedo@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanb@linux.ibm.com \
    --cc=stefanb@linux.vnet.ibm.com \
    --cc=wangyanan55@huawei.com \
    --cc=zhao1.liu@intel.com \
    /path/to/YOUR_REPLY

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

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.