mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] scsi: ufs: core: Avoid possible memory reclaim deadlock in TX EQTR context
@ 2026-06-18 14:09 Can Guo
  2026-07-02  7:27 ` Ziqi Chen
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Can Guo @ 2026-06-18 14:09 UTC (permalink / raw)
  To: bvanassche, beanhuo, peter.wang, martin.petersen, mani
  Cc: linux-scsi, Can Guo, Alim Akhtar, Avri Altman,
	James E.J. Bottomley, open list

TX EQTR may run while devfreq gear scaling has quiesced the UFS tagset. In
that context, functions ufshcd_tx_eqtr(), __ufshcd_tx_eqtr() and
ufs_qcom_get_rx_fom() allocate memory with GFP_KERNEL. If direct reclaim
is triggered, reclaim/writeback can depend on I/O to UFS device. Because
the queue is quiesced, this can cause deadlock.

Use memalloc_noio_save/restore() in ufshcd_tx_eqtr() to cover all
allocations in the TX EQTR call tree, including:
- params->eqtr_record in ufshcd_tx_eqtr()
- eqtr_data in __ufshcd_tx_eqtr()
- params in ufs_qcom_get_rx_fom()

This is preferred over tagging individual call sites with GFP_NOIO, as it
automatically covers any future allocations added anywhere in the call
tree without requiring each caller to be aware of this constraint.

Fixes: 03e5d38e2f98 ("scsi: ufs: core: Add support for TX Equalization")
Closes: https://sashiko.dev/#/patchset/20260615132834.2985346-1-can.guo@oss.qualcomm.com?part=2
Signed-off-by: Can Guo <can.guo@oss.qualcomm.com>
---
v1 -> v2:
- Replaced per-allocation GFP_NOIO usage with memalloc_noio_save/restore()
  around ufshcd_tx_eqtr() call tree.

 drivers/ufs/core/ufs-txeq.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/drivers/ufs/core/ufs-txeq.c b/drivers/ufs/core/ufs-txeq.c
index 4b264adfdf49..9dca0cd344b8 100644
--- a/drivers/ufs/core/ufs-txeq.c
+++ b/drivers/ufs/core/ufs-txeq.c
@@ -10,6 +10,7 @@
 #include <linux/delay.h>
 #include <linux/errno.h>
 #include <linux/kernel.h>
+#include <linux/sched/mm.h>
 #include <ufs/ufshcd.h>
 #include <ufs/unipro.h>
 #include "ufshcd-priv.h"
@@ -1212,14 +1213,25 @@ static int ufshcd_tx_eqtr(struct ufs_hba *hba,
 			  struct ufs_pa_layer_attr *pwr_mode)
 {
 	struct ufs_pa_layer_attr old_pwr_info;
+	unsigned int noio_flag;
 	int ret;
 
+	/*
+	 * ufshcd_tx_eqtr() is called from a power-mode-change context where
+	 * I/O is suspended. Use memalloc_noio_save() to propagate GFP_NOIO
+	 * to all allocations in the call tree instead of tagging each call
+	 * site individually.
+	 */
+	noio_flag = memalloc_noio_save();
+
 	if (!params->eqtr_record) {
 		params->eqtr_record = devm_kzalloc(hba->dev,
 						   sizeof(*params->eqtr_record),
 						   GFP_KERNEL);
-		if (!params->eqtr_record)
-			return -ENOMEM;
+		if (!params->eqtr_record) {
+			ret = -ENOMEM;
+			goto out_noio_restore;
+		}
 	}
 
 	memcpy(&old_pwr_info, &hba->pwr_info, sizeof(struct ufs_pa_layer_attr));
@@ -1244,6 +1256,9 @@ static int ufshcd_tx_eqtr(struct ufs_hba *hba,
 	if (ret)
 		ufshcd_tx_eqtr_unprepare(hba, &old_pwr_info);
 
+out_noio_restore:
+	memalloc_noio_restore(noio_flag);
+
 	return ret;
 }
 
-- 
2.34.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] scsi: ufs: core: Avoid possible memory reclaim deadlock in TX EQTR context
  2026-06-18 14:09 [PATCH v2] scsi: ufs: core: Avoid possible memory reclaim deadlock in TX EQTR context Can Guo
@ 2026-07-02  7:27 ` Ziqi Chen
  2026-07-06 14:09 ` Bart Van Assche
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ziqi Chen @ 2026-07-02  7:27 UTC (permalink / raw)
  To: Can Guo, bvanassche, beanhuo, peter.wang, martin.petersen, mani
  Cc: linux-scsi, Alim Akhtar, Avri Altman, James E.J. Bottomley, open list


On 6/18/2026 10:09 PM, Can Guo wrote:
> TX EQTR may run while devfreq gear scaling has quiesced the UFS tagset. In
> that context, functions ufshcd_tx_eqtr(), __ufshcd_tx_eqtr() and
> ufs_qcom_get_rx_fom() allocate memory with GFP_KERNEL. If direct reclaim
> is triggered, reclaim/writeback can depend on I/O to UFS device. Because
> the queue is quiesced, this can cause deadlock.
>
> Use memalloc_noio_save/restore() in ufshcd_tx_eqtr() to cover all
> allocations in the TX EQTR call tree, including:
> - params->eqtr_record in ufshcd_tx_eqtr()
> - eqtr_data in __ufshcd_tx_eqtr()
> - params in ufs_qcom_get_rx_fom()
>
> This is preferred over tagging individual call sites with GFP_NOIO, as it
> automatically covers any future allocations added anywhere in the call
> tree without requiring each caller to be aware of this constraint.
>
> Fixes: 03e5d38e2f98 ("scsi: ufs: core: Add support for TX Equalization")
> Closes: https://sashiko.dev/#/patchset/20260615132834.2985346-1-can.guo@oss.qualcomm.com?part=2
> Signed-off-by: Can Guo <can.guo@oss.qualcomm.com>
Reviewed-by: Ziqi Chen <ziqi.chen@oss.qualcomm.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] scsi: ufs: core: Avoid possible memory reclaim deadlock in TX EQTR context
  2026-06-18 14:09 [PATCH v2] scsi: ufs: core: Avoid possible memory reclaim deadlock in TX EQTR context Can Guo
  2026-07-02  7:27 ` Ziqi Chen
@ 2026-07-06 14:09 ` Bart Van Assche
  2026-07-06 15:44 ` Manivannan Sadhasivam
  2026-07-12 18:02 ` Martin K. Petersen
  3 siblings, 0 replies; 5+ messages in thread
From: Bart Van Assche @ 2026-07-06 14:09 UTC (permalink / raw)
  To: Can Guo, beanhuo, peter.wang, martin.petersen, mani
  Cc: linux-scsi, Alim Akhtar, Avri Altman, James E.J. Bottomley, open list

On 6/18/26 7:09 AM, Can Guo wrote:
> @@ -1244,6 +1256,9 @@ static int ufshcd_tx_eqtr(struct ufs_hba *hba,
>   	if (ret)
>   		ufshcd_tx_eqtr_unprepare(hba, &old_pwr_info);
>   
> +out_noio_restore:
> +	memalloc_noio_restore(noio_flag);
> +
>   	return ret;
>   }
>   

This patch adds a new label "out_noio_restore" next to the existing
label "out". Shouldn't the existing "out" label be renamed? Otherwise
this patch looks good to me.

Thanks,

Bart.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] scsi: ufs: core: Avoid possible memory reclaim deadlock in TX EQTR context
  2026-06-18 14:09 [PATCH v2] scsi: ufs: core: Avoid possible memory reclaim deadlock in TX EQTR context Can Guo
  2026-07-02  7:27 ` Ziqi Chen
  2026-07-06 14:09 ` Bart Van Assche
@ 2026-07-06 15:44 ` Manivannan Sadhasivam
  2026-07-12 18:02 ` Martin K. Petersen
  3 siblings, 0 replies; 5+ messages in thread
From: Manivannan Sadhasivam @ 2026-07-06 15:44 UTC (permalink / raw)
  To: Can Guo
  Cc: bvanassche, beanhuo, peter.wang, martin.petersen, linux-scsi,
	Alim Akhtar, Avri Altman, James E.J. Bottomley, open list

On Thu, Jun 18, 2026 at 07:09:29AM -0700, Can Guo wrote:
> TX EQTR may run while devfreq gear scaling has quiesced the UFS tagset. In
> that context, functions ufshcd_tx_eqtr(), __ufshcd_tx_eqtr() and
> ufs_qcom_get_rx_fom() allocate memory with GFP_KERNEL. If direct reclaim
> is triggered, reclaim/writeback can depend on I/O to UFS device. Because
> the queue is quiesced, this can cause deadlock.
> 
> Use memalloc_noio_save/restore() in ufshcd_tx_eqtr() to cover all
> allocations in the TX EQTR call tree, including:
> - params->eqtr_record in ufshcd_tx_eqtr()
> - eqtr_data in __ufshcd_tx_eqtr()
> - params in ufs_qcom_get_rx_fom()
> 
> This is preferred over tagging individual call sites with GFP_NOIO, as it
> automatically covers any future allocations added anywhere in the call
> tree without requiring each caller to be aware of this constraint.
> 
> Fixes: 03e5d38e2f98 ("scsi: ufs: core: Add support for TX Equalization")
> Closes: https://sashiko.dev/#/patchset/20260615132834.2985346-1-can.guo@oss.qualcomm.com?part=2
> Signed-off-by: Can Guo <can.guo@oss.qualcomm.com>

Reviewed-by: Manivannan Sadhasivam <mani@kernel.org>

- Mani

> ---
> v1 -> v2:
> - Replaced per-allocation GFP_NOIO usage with memalloc_noio_save/restore()
>   around ufshcd_tx_eqtr() call tree.
> 
>  drivers/ufs/core/ufs-txeq.c | 19 +++++++++++++++++--
>  1 file changed, 17 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/ufs/core/ufs-txeq.c b/drivers/ufs/core/ufs-txeq.c
> index 4b264adfdf49..9dca0cd344b8 100644
> --- a/drivers/ufs/core/ufs-txeq.c
> +++ b/drivers/ufs/core/ufs-txeq.c
> @@ -10,6 +10,7 @@
>  #include <linux/delay.h>
>  #include <linux/errno.h>
>  #include <linux/kernel.h>
> +#include <linux/sched/mm.h>
>  #include <ufs/ufshcd.h>
>  #include <ufs/unipro.h>
>  #include "ufshcd-priv.h"
> @@ -1212,14 +1213,25 @@ static int ufshcd_tx_eqtr(struct ufs_hba *hba,
>  			  struct ufs_pa_layer_attr *pwr_mode)
>  {
>  	struct ufs_pa_layer_attr old_pwr_info;
> +	unsigned int noio_flag;
>  	int ret;
>  
> +	/*
> +	 * ufshcd_tx_eqtr() is called from a power-mode-change context where
> +	 * I/O is suspended. Use memalloc_noio_save() to propagate GFP_NOIO
> +	 * to all allocations in the call tree instead of tagging each call
> +	 * site individually.
> +	 */
> +	noio_flag = memalloc_noio_save();
> +
>  	if (!params->eqtr_record) {
>  		params->eqtr_record = devm_kzalloc(hba->dev,
>  						   sizeof(*params->eqtr_record),
>  						   GFP_KERNEL);
> -		if (!params->eqtr_record)
> -			return -ENOMEM;
> +		if (!params->eqtr_record) {
> +			ret = -ENOMEM;
> +			goto out_noio_restore;
> +		}
>  	}
>  
>  	memcpy(&old_pwr_info, &hba->pwr_info, sizeof(struct ufs_pa_layer_attr));
> @@ -1244,6 +1256,9 @@ static int ufshcd_tx_eqtr(struct ufs_hba *hba,
>  	if (ret)
>  		ufshcd_tx_eqtr_unprepare(hba, &old_pwr_info);
>  
> +out_noio_restore:
> +	memalloc_noio_restore(noio_flag);
> +
>  	return ret;
>  }
>  
> -- 
> 2.34.1

-- 
மணிவண்ணன் சதாசிவம்

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] scsi: ufs: core: Avoid possible memory reclaim deadlock in TX EQTR context
  2026-06-18 14:09 [PATCH v2] scsi: ufs: core: Avoid possible memory reclaim deadlock in TX EQTR context Can Guo
                   ` (2 preceding siblings ...)
  2026-07-06 15:44 ` Manivannan Sadhasivam
@ 2026-07-12 18:02 ` Martin K. Petersen
  3 siblings, 0 replies; 5+ messages in thread
From: Martin K. Petersen @ 2026-07-12 18:02 UTC (permalink / raw)
  To: Can Guo
  Cc: bvanassche, beanhuo, peter.wang, martin.petersen, mani,
	linux-scsi, Alim Akhtar, Avri Altman, James E.J. Bottomley,
	open list


Can,

> TX EQTR may run while devfreq gear scaling has quiesced the UFS
> tagset. In that context, functions ufshcd_tx_eqtr(),
> __ufshcd_tx_eqtr() and ufs_qcom_get_rx_fom() allocate memory with
> GFP_KERNEL. If direct reclaim is triggered, reclaim/writeback can
> depend on I/O to UFS device. Because the queue is quiesced, this can
> cause deadlock.

Applied to 7.3/scsi-staging, thanks!

-- 
Martin K. Petersen

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-07-12 18:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-18 14:09 [PATCH v2] scsi: ufs: core: Avoid possible memory reclaim deadlock in TX EQTR context Can Guo
2026-07-02  7:27 ` Ziqi Chen
2026-07-06 14:09 ` Bart Van Assche
2026-07-06 15:44 ` Manivannan Sadhasivam
2026-07-12 18:02 ` Martin K. Petersen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox