2015-11-26 02:52:11

by Chaotian Jing

[permalink] [raw]
Subject: [PATCH] mmc: core: fix __mmc_switch timeout caused by preempt

there is a time window between __mmc_send_status() and time_afer(),
on some eMMC chip, the timeout_ms is only 10ms, if this thread was
scheduled out during this period, then, even card has already changes
to transfer state by the result of CMD13, this part of code also treat
it to timeout error.
So, need calculate timeout first, then call __mmc_send_status(), if
already timeout and card still in programing state, then treat it to
the real timeout error.

Signed-off-by: Chaotian Jing <[email protected]>
---
drivers/mmc/core/mmc_ops.c | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/drivers/mmc/core/mmc_ops.c b/drivers/mmc/core/mmc_ops.c
index 1f44426..0add634 100644
--- a/drivers/mmc/core/mmc_ops.c
+++ b/drivers/mmc/core/mmc_ops.c
@@ -489,6 +489,7 @@ int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
unsigned long timeout;
u32 status = 0;
bool use_r1b_resp = use_busy_signal;
+ bool expired = false;

mmc_retune_hold(host);

@@ -544,10 +545,22 @@ int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
/* Must check status to be sure of no errors. */
timeout = jiffies + msecs_to_jiffies(timeout_ms);
do {
+ /* Timeout if the device never leaves the program state.
+ * must check it firstly to avoid problem caused by preempt.
+ */
+ expired = time_after(jiffies, timeout);
+
if (send_status) {
err = __mmc_send_status(card, &status, ignore_crc);
- if (err)
+ if (err) {
goto out;
+ } else if (expired &&
+ R1_CURRENT_STATE(status) == R1_STATE_PRG) {
+ pr_err("%s: Card stuck in programming state! %s\n",
+ mmc_hostname(host), __func__);
+ err = -ETIMEDOUT;
+ goto out;
+ }
}
if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
break;
@@ -563,14 +576,6 @@ int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
mmc_delay(timeout_ms);
goto out;
}
-
- /* Timeout if the device never leaves the program state. */
- if (time_after(jiffies, timeout)) {
- pr_err("%s: Card stuck in programming state! %s\n",
- mmc_hostname(host), __func__);
- err = -ETIMEDOUT;
- goto out;
- }
} while (R1_CURRENT_STATE(status) == R1_STATE_PRG);

err = mmc_switch_status_error(host, status);
--
1.8.1.1.dirty


2015-11-26 11:56:12

by Ulf Hansson

[permalink] [raw]
Subject: Re: [PATCH] mmc: core: fix __mmc_switch timeout caused by preempt

On 26 November 2015 at 03:51, Chaotian Jing <[email protected]> wrote:
> there is a time window between __mmc_send_status() and time_afer(),
> on some eMMC chip, the timeout_ms is only 10ms, if this thread was
> scheduled out during this period, then, even card has already changes
> to transfer state by the result of CMD13, this part of code also treat
> it to timeout error.
> So, need calculate timeout first, then call __mmc_send_status(), if
> already timeout and card still in programing state, then treat it to
> the real timeout error.
>
> Signed-off-by: Chaotian Jing <[email protected]>
> ---
> drivers/mmc/core/mmc_ops.c | 23 ++++++++++++++---------
> 1 file changed, 14 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/mmc/core/mmc_ops.c b/drivers/mmc/core/mmc_ops.c
> index 1f44426..0add634 100644
> --- a/drivers/mmc/core/mmc_ops.c
> +++ b/drivers/mmc/core/mmc_ops.c
> @@ -489,6 +489,7 @@ int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
> unsigned long timeout;
> u32 status = 0;
> bool use_r1b_resp = use_busy_signal;
> + bool expired = false;

No need to assign an initial value.

>
> mmc_retune_hold(host);
>
> @@ -544,10 +545,22 @@ int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
> /* Must check status to be sure of no errors. */
> timeout = jiffies + msecs_to_jiffies(timeout_ms);
> do {
> + /* Timeout if the device never leaves the program state.

Please start mutli-row comment with an empty line.

> + * must check it firstly to avoid problem caused by preempt.

Please rephrase this to:

Due to the possibility of being preempted after sending the status
command, check the expiration time first.

> + */
> + expired = time_after(jiffies, timeout);

I think we can move this inside the below "if (send_status) {"
.. but of course before sending the status command.

> +
> if (send_status) {
> err = __mmc_send_status(card, &status, ignore_crc);
> - if (err)
> + if (err) {
> goto out;
> + } else if (expired &&

You may use a new "if" statement instead of an "else if", due to the
above "goto out".

> + R1_CURRENT_STATE(status) == R1_STATE_PRG) {
> + pr_err("%s: Card stuck in programming state! %s\n",
> + mmc_hostname(host), __func__);
> + err = -ETIMEDOUT;
> + goto out;
> + }
> }
> if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
> break;
> @@ -563,14 +576,6 @@ int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
> mmc_delay(timeout_ms);
> goto out;
> }
> -
> - /* Timeout if the device never leaves the program state. */
> - if (time_after(jiffies, timeout)) {
> - pr_err("%s: Card stuck in programming state! %s\n",
> - mmc_hostname(host), __func__);
> - err = -ETIMEDOUT;
> - goto out;
> - }
> } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
>
> err = mmc_switch_status_error(host, status);
> --
> 1.8.1.1.dirty
>

Kind regards
Uffe