Skip to contentSkip to conversation

cxl/hdm: Account for locked decoders regardless of commit state

PATCH In review Davidlohr Bueso 3 participants 4 messages Oct 29 – Nov 20 CXL

lore
Share & tools
lore

Davidlohr Bueso

author maintainer
#0 lore raw

PATCH 1/1 cxl/hdm: Account for locked decoders regardless of commit state

Firmware may arm a locked decoder it leaves uncommitted, a corner case albeit a valid one. Enumeration only records the bit for committed decoders, yet setup_hw_decoder() carries it through and the driver's own commit triggers it read-only.

A later region teardown then writes a reset the hardware ignores and clears CXL_DECODER_F_ENABLE anyway, so the decoder looks free while it still decodes and releases its DPA.

Record the bit regardless of the commit state, such that the decoder then reads locked from probe, and a region built on it is locked at attach.

Tested on qemu modified to enforce locked, arming the bit on an uncommitted endpoint decoder before the endpoint probes, then committing and deleting a 256M region on it and creating a 512M one. Before, the decoder and region read unlocked, the DPA is freed and the second region reports committed while the hardware still decodes the first; after, both read locked from attach, decommit fails with -EPERM, the DPA release with -EBUSY, and the second region cannot allocate DPA.

Fixes 2230c4bdc412 ("cxl: Add handling of locked CXL decoder")
---
 drivers/cxl/core/hdm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/cxl/core/hdm.c +3 -2
index fdcef20e01ba..a34f3509ab1f 100644
@@ -1064,11 +1064,12 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
 		.end = base + size - 1,
 	};
 
+	if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
+		cxld->flags |= CXL_DECODER_F_LOCK;
+
 	/* decoders are enabled if committed */
 	if (committed) {
 		cxld->flags |= CXL_DECODER_F_ENABLE;
-		if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
-			cxld->flags |= CXL_DECODER_F_LOCK;
 		if (FIELD_GET(CXL_HDM_DECODER0_CTRL_HOSTONLY, ctrl))
 			cxld->target_type = CXL_DECODER_HOSTONLYMEM;
 		else
Signature · 1 line
2.39.5
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · Davidlohr Bueso

Firmware may arm a locked decoder it leaves uncommitted, a corner case albeit a valid one. Enumeration only records the bit for committed decoders, yet setup_hw_decoder() carries it through and the driver's own commit triggers it read-only.

A later region teardown then writes a reset the hardware ignores and clears CXL_DECODER_F_ENABLE anyway, so the decoder looks free while it still decodes and releases its DPA.

Record the bit regardless of the commit state, such that the decoder then reads locked from probe, and a region built on it is locked at attach.

Tested on qemu modified to enforce locked, arming the bit on an uncommitted endpoint decoder before the endpoint probes, then committing and deleting a 256M region on it and creating a 512M one. Before, the decoder and region read unlocked, the DPA is freed and the second region reports committed while the hardware still decodes the first; after, both read locked from attach, decommit fails with -EPERM, the DPA release with -EBUSY, and the second region cannot allocate DPA.

Quoted 2 lines
Fixes 2230c4bdc412 ("cxl: Add handling of locked CXL decoder")

Can you commit the decoder in locked state? Or are you just stuck in this weird state?

Quoted 23 lines
---
 drivers/cxl/core/hdm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/cxl/core/hdm.c +3 -2
index fdcef20e01ba..a34f3509ab1f 100644
@@ -1064,11 +1064,12 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
 		.end = base + size - 1,
 	};
 
+	if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
+		cxld->flags |= CXL_DECODER_F_LOCK;
+
 	/* decoders are enabled if committed */
 	if (committed) {
 		cxld->flags |= CXL_DECODER_F_ENABLE;
-		if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
-			cxld->flags |= CXL_DECODER_F_LOCK;
 		if (FIELD_GET(CXL_HDM_DECODER0_CTRL_HOSTONLY, ctrl))
 			cxld->target_type = CXL_DECODER_HOSTONLYMEM;
 		else
author maintainer
#2 lore raw
Quoted 25 lines · Dave Jiang
Quoted 22 lines · Davidlohr Bueso

Firmware may arm a locked decoder it leaves uncommitted, a corner case albeit a valid one. Enumeration only records the bit for committed decoders, yet setup_hw_decoder() carries it through and the driver's own commit triggers it read-only.

A later region teardown then writes a reset the hardware ignores and clears CXL_DECODER_F_ENABLE anyway, so the decoder looks free while it still decodes and releases its DPA.

Record the bit regardless of the commit state, such that the decoder then reads locked from probe, and a region built on it is locked at attach.

Tested on qemu modified to enforce locked, arming the bit on an uncommitted endpoint decoder before the endpoint probes, then committing and deleting a 256M region on it and creating a 512M one. Before, the decoder and region read unlocked, the DPA is freed and the second region reports committed while the hardware still decodes the first; after, both read locked from attach, decommit fails with -EPERM, the DPA release with -EBUSY, and the second region cannot allocate DPA.

Quoted 4 lines
Quoted 2 lines
Fixes 2230c4bdc412 ("cxl: Add handling of locked CXL decoder")

Can you commit the decoder in locked state? Or are you just stuck in this weird state?

I would think so, the bit is locked *on* commit, so the decoder is not locked yet.

Quoted 2 lines

Thanks,
Davidlohr

maintainer
#3 lore raw
In reply to Davidlohr Bueso
Quoted 13 lines · Davidlohr Bueso

Firmware may arm a locked decoder it leaves uncommitted, a corner case albeit a valid one. Enumeration only records the bit for committed decoders, yet setup_hw_decoder() carries it through and the driver's own commit triggers it read-only.

A later region teardown then writes a reset the hardware ignores and clears CXL_DECODER_F_ENABLE anyway, so the decoder looks free while it still decodes and releases its DPA.

Record the bit regardless of the commit state, such that the decoder then reads locked from probe, and a region built on it is locked at attach.

Quoted 8 lines

Tested on qemu modified to enforce locked, arming the bit on an uncommitted endpoint decoder before the endpoint probes, then committing and deleting a 256M region on it and creating a 512M one. Before, the decoder and region read unlocked, the DPA is freed and the second region reports committed while the hardware still decodes the first; after, both read locked from attach, decommit fails with -EPERM, the DPA release with -EBUSY, and the second region cannot allocate DPA.

Hi Davidlohr,

This does fix the stated problem. When firmware sets Lock On Commit on a decoder but leaves it uncommitted (armed, below), the driver's own commit locks it. Region teardown then marks the decoder free while the hardware still decodes, and its DPA can be handed to a second region while the first still decodes to it. Refusing that teardown is the safer way to fail.

It calls attention to a wider cleanup of the lock tracking. Lock On Commit doesn't lock anything until the decoder commits but the flag is now set at probe, the region picks it up at attach, and is never cleared afterwards.

The problem with that is the driver treats a region as locked before anything in the hardware is, and keeps it locked even if the armed decoder never commits. A locked region skips all decoder teardown, so -

- If the armed decoder is detached before commit, the region stays locked. A region then committed with only unlocked decoders can't be decommitted even though the hardware would allow it.
- If a region commit fails partway, before the armed decoder commits, the unwind is skipped. The decoders that did commit stay committed after the region is deleted, and their DPA can't be freed, though none of them is locked.
- An SBR clears the hardware bit, but if nothing was committed the decoder keeps the lock flag. The driver then commits it unlocked, yet the region on it can't be decommitted.

Could we record the decoder lock when the driver's commit actually locks it? The region handling would then need to cover both a successful region commit and a decoder becoming locked before a later commit fails, preserving the locked decoder's resources while unwinding decoders that can still be reset. Locking the region only after the entire commit succeeds would miss that partial failure.

Your modified QEMU is the only way I know to test it, since cxl_test doesn't go through the real enumeration or commit paths.

I didn't prototype anything of this at all, so nothing to concrete to share other than my words.

-- Alison

Quoted 29 lines
Fixes 2230c4bdc412 ("cxl: Add handling of locked CXL decoder")
---
 drivers/cxl/core/hdm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/cxl/core/hdm.c +3 -2
index fdcef20e01ba..a34f3509ab1f 100644
@@ -1064,11 +1064,12 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
 		.end = base + size - 1,
 	};
 
+	if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
+		cxld->flags |= CXL_DECODER_F_LOCK;
+
 	/* decoders are enabled if committed */
 	if (committed) {
 		cxld->flags |= CXL_DECODER_F_ENABLE;
-		if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
-			cxld->flags |= CXL_DECODER_F_LOCK;
 		if (FIELD_GET(CXL_HDM_DECODER0_CTRL_HOSTONLY, ctrl))
 			cxld->target_type = CXL_DECODER_HOSTONLYMEM;
 		else
Signature · 1 line
2.39.5