Skip to contentSkip to conversation

scsi: ufs: core: Fix error handler encryption support

PATCH In review Po-Wen Kao 3 participants 8 messages Dec 8, 2025 – Dec 16, 2025

Subsystems (2)
lore
Share & tools
lore

Po-Wen Kao

author
#0 lore raw

PATCH 1/1 scsi: ufs: core: Fix error handler encryption support

From: Brian Kao <[email protected]>

The UFS driver utilizes block layer crypto fields, such as rq->crypt_keyslot and rq->crypt_ctx, to configure hardware for inline encryption. However, the SCSI error handler (EH) reuses the Protocol Data Unit (PDU) from the original failing request when issuing EH commands (e.g., TEST UNIT READY, START STOP UNIT).

This can lead to issues if the original request of reused PDU contains stale cryptographic configurations, which are not applicable for the simple EH commands. These commands should not involve data encryption.

This patch fixes this by checking if the command was submitted by the SCSI error handler. If so, it bypasses the cryptographic setup for the request, ensuring UTRDs are not inadvertently configured with potentially incorrect encryption parameters.

---
 drivers/ufs/core/ufshcd-crypto.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd-crypto.h +6 -1
index c148a5194378..26a0699c8412 100644
@@ -16,7 +16,12 @@
 static inline void ufshcd_prepare_lrbp_crypto(struct request *rq,
 					      struct ufshcd_lrb *lrbp)
 {
-	if (!rq || !rq->crypt_keyslot) {
+	/*
+	 * Do not use the crypto settings if the SCSI error handler has replaced
+	 * the SCSI command
+	 */
+	if (!rq || !rq->crypt_keyslot ||
+	    unlikely(lrbp->cmd->submitter == SUBMITTED_BY_SCSI_ERROR_HANDLER)) {
 		lrbp->crypto_key_slot = -1;
 		return;
 	}
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 31 lines · Po-Wen Kao

From: Brian Kao <[email protected]>

The UFS driver utilizes block layer crypto fields, such as rq->crypt_keyslot and rq->crypt_ctx, to configure hardware for inline encryption. However, the SCSI error handler (EH) reuses the Protocol Data Unit (PDU) from the original failing request when issuing EH commands (e.g., TEST UNIT READY, START STOP UNIT).

This can lead to issues if the original request of reused PDU contains stale cryptographic configurations, which are not applicable for the simple EH commands. These commands should not involve data encryption.

This patch fixes this by checking if the command was submitted by the SCSI error handler. If so, it bypasses the cryptographic setup for the request, ensuring UTRDs are not inadvertently configured with potentially incorrect encryption parameters.

---
  drivers/ufs/core/ufshcd-crypto.h | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd-crypto.h +0 -0
index c148a5194378..26a0699c8412 100644
@@ -16,7 +16,12 @@
  static inline void ufshcd_prepare_lrbp_crypto(struct request *rq,
  					      struct ufshcd_lrb *lrbp)
  {

drivers/ufs/core/ufshcd-crypto.h line 27

Quoted 10 lines
drivers/ufs/core/ufshcd-crypto.h +6 -1
-	if (!rq || !rq->crypt_keyslot) {
+	/*
+	 * Do not use the crypto settings if the SCSI error handler has replaced
+	 * the SCSI command
+	 */
+	if (!rq || !rq->crypt_keyslot ||
+	    unlikely(lrbp->cmd->submitter == SUBMITTED_BY_SCSI_ERROR_HANDLER)) {
  		lrbp->crypto_key_slot = -1;
  		return;
  	}

(+Christoph Hellwig and Eric Biggers)

#2 lore raw
In reply to Po-Wen Kao
Quoted 13 lines · Po-Wen Kao

From: Brian Kao <[email protected]>

The UFS driver utilizes block layer crypto fields, such as rq->crypt_keyslot and rq->crypt_ctx, to configure hardware for inline encryption. However, the SCSI error handler (EH) reuses the Protocol Data Unit (PDU) from the original failing request when issuing EH commands (e.g., TEST UNIT READY, START STOP UNIT).

This can lead to issues if the original request of reused PDU contains stale cryptographic configurations, which are not applicable for the simple EH commands. These commands should not involve data encryption.

Quoted 4 lines

This patch fixes this by checking if the command was submitted by the SCSI error handler. If so, it bypasses the cryptographic setup for the request, ensuring UTRDs are not inadvertently configured with potentially incorrect encryption parameters.

As mentioned last round, why are you even calling into the crypto code here? Calling that for a request without a crypt context, which includes all of them that do not transfer any data makes no sense to start with.

#3 lore raw
Quoted 4 lines · Christoph Hellwig

As mentioned last round, why are you even calling into the crypto code here? Calling that for a request without a crypt context, which includes all of them that do not transfer any data makes no sense to start with.

ufshcd_prepare_lrbp_crypto() only has one caller. Moving the new test from inside ufshcd_prepare_lrbp_crypto() into its caller should be easy.

Thanks,

Bart.

#4 lore raw
Quoted 8 lines · Bart Van Assche
Quoted 4 lines · Christoph Hellwig

As mentioned last round, why are you even calling into the crypto code here? Calling that for a request without a crypt context, which includes all of them that do not transfer any data makes no sense to start with.

ufshcd_prepare_lrbp_crypto() only has one caller. Moving the new test from inside ufshcd_prepare_lrbp_crypto() into its caller should be easy.

I don't think callers vs calle is the important part here. It is to check if any actual data is tranferred instead of special casing EH commands.

#5 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Bart Van Assche
Quoted 4 lines · Christoph Hellwig

As mentioned last round, why are you even calling into the crypto code here? Calling that for a request without a crypt context, which includes all of them that do not transfer any data makes no sense to start with.

Quoted 6 lines
Quoted 2 lines

ufshcd_prepare_lrbp_crypto() only has one caller. Moving the new test from inside ufshcd_prepare_lrbp_crypto() into its caller should be easy.

I don't think callers vs calle is the important part here. It is to check if any actual data is tranferred instead of special casing EH commands.

Hi Christoph,

Do you agree with the following?

(a) There is code in the SCSI error handler that submits SCSI commands

    with a data buffer. Hence, disabling encryption if and only if the
    data buffer length is zero can't fix the reported problem. From
    scsi_eh_prep_cmnd() in drivers/scsi/scsi_error.c:

		scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
					 sense_bytes);
		sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
			    scmd->sdb.length);
		scmd->sdb.table.sgl = &ses->sense_sgl;

(b) In general in the Linux kernel it is strongly preferred to fix the root cause of a bug rather than to implement a workaround. This is preferred because it makes kernel code easier to maintain and reduces the chance that new bugs are introduced.

(c) Disabling encryption in the UFS driver if a command has been submitted by the SCSI error handler is a workaround. Patch [1] fixes the root cause of the problem, namely the SCSI error handler not setting the encryption fields in struct request correctly. We prefer
[1] because UFS devices that support one million IOPS are expected to arrive soon (early 2026). Hence the importance of keeping the hot path in the UFS driver fast.

Thanks,

Bart.

[1] [PATCH v2 1/1] scsi: core: Fix error handler encryption support

(https://lore.kernel.org/linux-scsi/[email protected]/)

#6 lore raw
Quoted 4 lines · Bart Van Assche
Quoted 3 lines

I don't think callers vs calle is the important part here. It is to check if any actual data is tranferred instead of special casing EH commands.

Quoted 8 lines

Hi Christoph,

Do you agree with the following?

(a) There is code in the SCSI error handler that submits SCSI commands with a data buffer. Hence, disabling encryption if and only if the data buffer length is zero can't fix the reported problem. From scsi_eh_prep_cmnd() in drivers/scsi/scsi_error.c:

This still does not actually transfer data to the media, and thus is not affected by inline encryption.

#7 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Bart Van Assche

(a) There is code in the SCSI error handler that submits SCSI commands with a data buffer. Hence, disabling encryption if and only if the data buffer length is zero can't fix the reported problem. From scsi_eh_prep_cmnd() in drivers/scsi/scsi_error.c:

This still does not actually transfer data to the media, and thus is not affected by inline encryption.

Hi Christoph,

Agreed that neither REQUEST SENSE nor any of the other SCSI commands submitted by the SCSI error handler access the storage medium. However, even if Po-Wen's patch would be modified such that it checks the SCSI CDB to verify whether or not the SCSI command accesses the medium, the other concerns that I mentioned still apply. So if nobody objects I propose to repost patch "[PATCH v2 1/1] scsi: core: Fix error handler encryption support" with v2 bumped to v3.

Thanks,

Bart.