Skip to contentSkip to conversation

tpm, tpm_crb: fix unaligned read of the command buffer address

PATCH Superseded Jarkko Sakkinen 3 participants 5 messages Sep 14, 2015 – Sep 15, 2015 TPM device driver

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm, tpm_crb: fix unaligned read of the command buffer address

The command buffer address is necessarily not naturally aligned. The hardware drops the entire read on some platforms and fills the address with 1's. This patch fixes the issue by splitting the read into two 32 bit reads.

---
 drivers/char/tpm/tpm_crb.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm_crb.c +5 -2
index b4564b6..12bb41c 100644
@@ -68,7 +68,8 @@ struct crb_control_area {
 	u32 int_enable;
 	u32 int_sts;
 	u32 cmd_size;
-	u64 cmd_pa;
+	u32 cmd_pa_low;
+	u32 cmd_pa_high;
 	u32 rsp_size;
 	u64 rsp_pa;
 } __packed;
@@ -264,7 +265,9 @@ static int crb_acpi_add(struct acpi_device *device)
 	}
 
 	memcpy_fromio(&pa, &priv->cca->cmd_pa, 8);
-	pa = le64_to_cpu(pa);
+
+	pa = ((u64) le32_to_cpu(ioread32(&priv->cca->cmd_pa_high)) << 32) +
+		(u64) le32_to_cpu(ioread32(&priv->cca->cmd_pa_low));
 	priv->cmd = devm_ioremap_nocache(dev, pa,
 					 ioread32(&priv->cca->cmd_size));
 	if (!priv->cmd) {
Signature · 1 line
2.5.0
#1 lore raw
Quoted 4 lines · Jarkko Sakkinen

The command buffer address is necessarily not naturally aligned. The hardware drops the entire read on some platforms and fills the address with 1's. This patch fixes the issue by splitting the read into two 32 bit reads.

Is this necessary? The packed attribution means that unaligned members are allowed and the compiler deals with it where necessary.

Jason

#2 lore raw
Quoted 8 lines · Jason Gunthorpe
Quoted 4 lines · Jarkko Sakkinen

The command buffer address is necessarily not naturally aligned. The hardware drops the entire read on some platforms and fills the address with 1's. This patch fixes the issue by splitting the read into two 32 bit reads.

Is this necessary? The packed attribution means that unaligned members are allowed and the compiler deals with it where necessary.

For regular memory memory controller splits the read into two 32 bit reads.

However, for MMIO address the hardware might abort the entire request when trying to do a 64-bit read, which causes the CPU to fill the result with 1's.

This is not hypothetical bug. We are experiencing this on some platforms and the proposed fix resolves the issue.

Quoted 1 line

Jason

/Jarkko

#3 lore raw
Quoted 3 lines · Jarkko Sakkinen

However, for MMIO address the hardware might abort the entire request when trying to do a 64-bit read, which causes the CPU to fill the result with 1's.

Okay, yes, for iomem you can't rely on packed.

packed actually can mess up iomem loads on some arches as it also tells the compiler things are unaligned. I'd drop the __packed since the new structure is naturally packed in this case. (for other cases be careful to add __aligned(2) to avoid unaligned accesses)

However, I'm still confused, the original code did:
memcpy_fromio(&pa, &priv->cca->cmd_pa, 8);

Which might do byte reads from the iomem cmd_pa, but there should be no problem with an unaligned access.

Is the real issue that you can't do memcpy_fromio to tpm control memory? That would not suprise me one bit. In which case the commit message should be revised.

Quoted 2 lines

This is not hypothetical bug. We are experiencing this on some platforms and the proposed fix resolves the issue.

I am confused because of the memcpy_fromio:

memcpy_fromio(&pa, &priv->cca->cmd_pa, 8);
- pa = le64_to_cpu(pa);
+
+ pa = ((u64) le32_to_cpu(ioread32(&priv->cca->cmd_pa_high)) << 32) +
+ (u64) le32_to_cpu(ioread32(&priv->cca->cmd_pa_low));
priv->cmd = devm_ioremap_nocache(dev, pa,
ioread32(&priv->cca->cmd_size));

The code wasn't doing a direct load from cmd_pa, so the type doesn't matter.

BTW. Does the above even compile with that memcpy_fromio left in?

Jason

#4 lore raw
Quoted 15 lines · Jason Gunthorpe
Quoted 3 lines · Jarkko Sakkinen

However, for MMIO address the hardware might abort the entire request when trying to do a 64-bit read, which causes the CPU to fill the result with 1's.

Okay, yes, for iomem you can't rely on packed.

packed actually can mess up iomem loads on some arches as it also tells the compiler things are unaligned. I'd drop the __packed since the new structure is naturally packed in this case. (for other cases be careful to add __aligned(2) to avoid unaligned accesses)

However, I'm still confused, the original code did:
memcpy_fromio(&pa, &priv->cca->cmd_pa, 8);

Quoted 6 lines

Which might do byte reads from the iomem cmd_pa, but there should be no problem with an unaligned access.

Is the real issue that you can't do memcpy_fromio to tpm control memory? That would not suprise me one bit. In which case the commit message should be revised.

Good question and point. Emprically it seems to be so. I guess you have to do exactly 32-bit read for the field. I'll revise the commit message.

Quoted 13 lines
Quoted 2 lines

This is not hypothetical bug. We are experiencing this on some platforms and the proposed fix resolves the issue.

I am confused because of the memcpy_fromio:

drivers/char/tpm/tpm_crb.c +3 -1
 	memcpy_fromio(&pa, &priv->cca->cmd_pa, 8);
-	pa = le64_to_cpu(pa);
+
+	pa = ((u64) le32_to_cpu(ioread32(&priv->cca->cmd_pa_high)) << 32) +
+		(u64) le32_to_cpu(ioread32(&priv->cca->cmd_pa_low));
 	priv->cmd = devm_ioremap_nocache(dev, pa,
 					 ioread32(&priv->cca->cmd_size));
Quoted 4 lines

The code wasn't doing a direct load from cmd_pa, so the type doesn't matter.

BTW. Does the above even compile with that memcpy_fromio left in?

Nope :) See my own reply to the original message.

Quoted 1 line

Jason

/Jarkko