Skip to contentSkip to conversation

mmc: avoid livelock in mmc_sd_detect

PATCH In review Rokudo Yan 2 participants 3 messages Apr 13, 2022 MMC, SD and SDIO

lore
Share & tools
lore

Rokudo Yan

author
#0 lore raw

PATCH 1/1 mmc: avoid livelock in mmc_sd_detect

There is a potential livelock when mmc_sd_detect is invoked by pm_suspend, as below:

[thread do suspend]
state_store
-pm_suspend
-__pm_notifier_call_chain
-mmc_pm_notify

    -cancel_delayed_work_sync(&host->detect)
     -__cancel_work_timer
      -__flush_work
       -wait_for_completion
       wait for detect work current running to complete

[kworker run detect work]
process_one_work
-mmc_rescan
-mmc_sd_detect
-mmc_get_card
-__mmc_claim_host
-schedule
wait for current host claimer release

[kworker dispatch io (current host claimer)] process_one_work
-blk_mq_run_work_fn
-__blk_mq_run_hw_queue
-blk_mq_sched_dispatch_requests
-blk_mq_do_dispatch_sched
-blk_mq_dispatch_rq_list
-mmc_mq_queue_rq
-mmc_get_card -- claim mmc host if inflight=1 (the first io)
and only release when all ios complete
-mmc_blk_mq_issue_rq

if there are continous io requests in the system to keep queue busy (inflight !=0 all the time), the supsend process will blocked and hang the system.

This issue can reproduced by steps below:
1. prepare 1 SD card
2. run fio to keep the card io busy

  fio --size=1m --bs=32k --ioengine=libaio --iodepth=64 \
    --direct=1 --rw=read --time_based --runtime=60000 \
    --name=test --filename=<path under sd card>

3. suspend the system (echo mem > /sys/power/state)
4. resume the system
5. suspend the system again & device hang

---
 drivers/mmc/core/queue.c | 8 ++++++++
 drivers/mmc/core/sd.c    | 2 ++
 include/linux/mmc/host.h | 3 +++
 3 files changed, 13 insertions(+)
drivers/mmc/core/queue.c +8 -0
index c69b2d9df6f1..f00fc45a4fd5 100644
@@ -241,6 +241,14 @@ static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
 		return BLK_STS_IOERR;
 	}
 
+	/*
+	 * requeue the io if sd detect is on-going to avoid livelock
+	 * when mmc_sd_detect is invoked by pm_suspend and there are
+	 * continous io requests simultaneously.
+	 */
+	if (host->doing_sd_detect)
+		return BLK_STS_RESOURCE;
+
 	issue_type = mmc_issue_type(mq, req);
 
 	spin_lock_irq(&mq->lock);
drivers/mmc/core/sd.c +2 -0
index 68df6b2f49cc..460ea6ab9aba 100644
@@ -1594,6 +1594,7 @@ static void mmc_sd_detect(struct mmc_host *host)
 {
 	int err;
 
+	host->doing_sd_detect = true;
 	mmc_get_card(host->card, NULL);
 
 	/*
@@ -1611,6 +1612,7 @@ static void mmc_sd_detect(struct mmc_host *host)
 		mmc_power_off(host);
 		mmc_release_host(host);
 	}
+	host->doing_sd_detect = false;
 }
 
 static int sd_can_poweroff_notify(struct mmc_card *card)
include/linux/mmc/host.h +3 -0
index 7afb57cab00b..4abfc5d7ef20 100644
@@ -501,6 +501,9 @@ struct mmc_host {
 	/* Host Software Queue support */
 	bool			hsq_enabled;
 
+	/* indicate SD detect on-going */
+	bool			doing_sd_detect;
+
 	unsigned long		private[] ____cacheline_aligned;
 };
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 43 lines · Rokudo Yan

There is a potential livelock when mmc_sd_detect is invoked by pm_suspend, as below:

[thread do suspend]
state_store
-pm_suspend
-__pm_notifier_call_chain
-mmc_pm_notify

    -cancel_delayed_work_sync(&host->detect)
     -__cancel_work_timer
      -__flush_work
       -wait_for_completion
       wait for detect work current running to complete

[kworker run detect work]
process_one_work
-mmc_rescan
-mmc_sd_detect
-mmc_get_card
-__mmc_claim_host
-schedule
wait for current host claimer release

[kworker dispatch io (current host claimer)] process_one_work
-blk_mq_run_work_fn
-__blk_mq_run_hw_queue
-blk_mq_sched_dispatch_requests
-blk_mq_do_dispatch_sched
-blk_mq_dispatch_rq_list
-mmc_mq_queue_rq
-mmc_get_card -- claim mmc host if inflight=1 (the first io)
and only release when all ios complete
-mmc_blk_mq_issue_rq

if there are continous io requests in the system to keep queue busy (inflight !=0 all the time), the supsend process will blocked and hang the system.

This issue can reproduced by steps below:
1. prepare 1 SD card
2. run fio to keep the card io busy

Quoted 8 lines
  fio --size=1m --bs=32k --ioengine=libaio --iodepth=64 \
    --direct=1 --rw=read --time_based --runtime=60000 \
    --name=test --filename=<path under sd card>

3. suspend the system (echo mem > /sys/power/state)
4. resume the system
5. suspend the system again & device hang

I believe you are running some vendor/old kernel, because I think this problem has been fixed.

You may have a look at commit 17a17bf50612 ("mmc: core: Fix hanging on I/O during system suspend for removable cards")

Kind regards
Uffe

Quoted 62 lines
---
 drivers/mmc/core/queue.c | 8 ++++++++
 drivers/mmc/core/sd.c    | 2 ++
 include/linux/mmc/host.h | 3 +++
 3 files changed, 13 insertions(+)
drivers/mmc/core/queue.c +8 -0
index c69b2d9df6f1..f00fc45a4fd5 100644
@@ -241,6 +241,14 @@ static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
                return BLK_STS_IOERR;
        }
 
+       /*
+        * requeue the io if sd detect is on-going to avoid livelock
+        * when mmc_sd_detect is invoked by pm_suspend and there are
+        * continous io requests simultaneously.
+        */
+       if (host->doing_sd_detect)
+               return BLK_STS_RESOURCE;
+
        issue_type = mmc_issue_type(mq, req);
 
        spin_lock_irq(&mq->lock);
drivers/mmc/core/sd.c +2 -0
index 68df6b2f49cc..460ea6ab9aba 100644
@@ -1594,6 +1594,7 @@ static void mmc_sd_detect(struct mmc_host *host)
 {
        int err;
 
+       host->doing_sd_detect = true;
        mmc_get_card(host->card, NULL);
 
        /*
@@ -1611,6 +1612,7 @@ static void mmc_sd_detect(struct mmc_host *host)
                mmc_power_off(host);
                mmc_release_host(host);
        }
+       host->doing_sd_detect = false;
 }
 
 static int sd_can_poweroff_notify(struct mmc_card *card)
include/linux/mmc/host.h +3 -1
index 7afb57cab00b..4abfc5d7ef20 100644
@@ -501,6 +501,9 @@ struct mmc_host {
        /* Host Software Queue support */
        bool                    hsq_enabled;
 
+       /* indicate SD detect on-going */
+       bool                    doing_sd_detect;
+
        unsigned long           private[] ____cacheline_aligned;
 };
 
--

2.25.1

author
#2 lore raw
Quoted 121 lines · Ulf Hansson
Quoted 43 lines · Rokudo Yan

There is a potential livelock when mmc_sd_detect is invoked by pm_suspend, as below:

[thread do suspend]
state_store
-pm_suspend
-__pm_notifier_call_chain

    -mmc_pm_notify
     -cancel_delayed_work_sync(&host->detect)
      -__cancel_work_timer
       -__flush_work
        -wait_for_completion
        wait for detect work current running to complete

[kworker run detect work]
process_one_work
-mmc_rescan
-mmc_sd_detect
-mmc_get_card
-__mmc_claim_host
-schedule
wait for current host claimer release

[kworker dispatch io (current host claimer)] process_one_work
-blk_mq_run_work_fn
-__blk_mq_run_hw_queue
-blk_mq_sched_dispatch_requests
-blk_mq_do_dispatch_sched
-blk_mq_dispatch_rq_list
-mmc_mq_queue_rq
-mmc_get_card -- claim mmc host if inflight=1 (the first io)
and only release when all ios complete
-mmc_blk_mq_issue_rq

if there are continous io requests in the system to keep queue busy (inflight !=0 all the time), the supsend process will blocked and hang the system.

This issue can reproduced by steps below:
1. prepare 1 SD card
2. run fio to keep the card io busy

Quoted 8 lines
   fio --size=1m --bs=32k --ioengine=libaio --iodepth=64 \
     --direct=1 --rw=read --time_based --runtime=60000 \
     --name=test --filename=<path under sd card>

3. suspend the system (echo mem > /sys/power/state)
4. resume the system
5. suspend the system again & device hang

I believe you are running some vendor/old kernel, because I think this problem has been fixed.

You may have a look at commit 17a17bf50612 ("mmc: core: Fix hanging on I/O during system suspend for removable cards")

Kind regards
Uffe

Quoted 59 lines
---
  drivers/mmc/core/queue.c | 8 ++++++++
  drivers/mmc/core/sd.c    | 2 ++
  include/linux/mmc/host.h | 3 +++
  3 files changed, 13 insertions(+)
drivers/mmc/core/queue.c +8 -0
index c69b2d9df6f1..f00fc45a4fd5 100644
@@ -241,6 +241,14 @@ static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
                 return BLK_STS_IOERR;
         }
 
+       /*
+        * requeue the io if sd detect is on-going to avoid livelock
+        * when mmc_sd_detect is invoked by pm_suspend and there are
+        * continous io requests simultaneously.
+        */
+       if (host->doing_sd_detect)
+               return BLK_STS_RESOURCE;
+
         issue_type = mmc_issue_type(mq, req);
 
         spin_lock_irq(&mq->lock);
drivers/mmc/core/sd.c +2 -0
index 68df6b2f49cc..460ea6ab9aba 100644
@@ -1594,6 +1594,7 @@ static void mmc_sd_detect(struct mmc_host *host)
  {
         int err;
 
+       host->doing_sd_detect = true;
         mmc_get_card(host->card, NULL);
 
         /*
@@ -1611,6 +1612,7 @@ static void mmc_sd_detect(struct mmc_host *host)
                 mmc_power_off(host);
                 mmc_release_host(host);
         }
+       host->doing_sd_detect = false;
  }
 
  static int sd_can_poweroff_notify(struct mmc_card *card)
include/linux/mmc/host.h +3 -0
index 7afb57cab00b..4abfc5d7ef20 100644
@@ -501,6 +501,9 @@ struct mmc_host {
         /* Host Software Queue support */
         bool                    hsq_enabled;
 
+       /* indicate SD detect on-going */
+       bool                    doing_sd_detect;
+
         unsigned long           private[] ____cacheline_aligned;
  };
 
Quoted 3 lines
Quoted 3 lines

--
2.25.1

Hi, Uffe

Thanks for reply, the kernel version used in our device is kernel-4.19 And the patch you mentioned is not applied, I will try to apply the patch.

Thanks
yanwu