linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 1/1] mmc: mmc_spi: Simplify busy loop in mmc_spi_skip()
@ 2021-06-23 10:17 Andy Shevchenko
  2021-07-08 12:31 ` Ulf Hansson
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2021-06-23 10:17 UTC (permalink / raw)
  To: Andy Shevchenko, linux-mmc, linux-kernel; +Cc: Ulf Hansson

Infinite loops are hard to read and understand because of
hidden main loop condition. Simplify such one in mmc_spi_skip().

Using schedule() to schedule (and be friendly to others)
is discouraged and cond_resched() should be used instead.
Hence, replace schedule() with cond_resched() at the same
time.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/mmc/host/mmc_spi.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
index 65c65bb5737f..a1bcde3395a6 100644
--- a/drivers/mmc/host/mmc_spi.c
+++ b/drivers/mmc/host/mmc_spi.c
@@ -180,7 +180,7 @@ static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout,
 	u8 *cp = host->data->status;
 	unsigned long start = jiffies;
 
-	while (1) {
+	do {
 		int		status;
 		unsigned	i;
 
@@ -193,16 +193,9 @@ static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout,
 				return cp[i];
 		}
 
-		if (time_is_before_jiffies(start + timeout))
-			break;
-
-		/* If we need long timeouts, we may release the CPU.
-		 * We use jiffies here because we want to have a relation
-		 * between elapsed time and the blocking of the scheduler.
-		 */
-		if (time_is_before_jiffies(start + 1))
-			schedule();
-	}
+		/* If we need long timeouts, we may release the CPU */
+		cond_resched();
+	} while (time_is_after_jiffies(start + timeout));
 	return -ETIMEDOUT;
 }
 
-- 
2.30.2


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

* Re: [PATCH v1 1/1] mmc: mmc_spi: Simplify busy loop in mmc_spi_skip()
  2021-06-23 10:17 [PATCH v1 1/1] mmc: mmc_spi: Simplify busy loop in mmc_spi_skip() Andy Shevchenko
@ 2021-07-08 12:31 ` Ulf Hansson
  2021-07-08 12:49   ` Andy Shevchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Ulf Hansson @ 2021-07-08 12:31 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-mmc, Linux Kernel Mailing List

On Wed, 23 Jun 2021 at 12:17, Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> Infinite loops are hard to read and understand because of
> hidden main loop condition. Simplify such one in mmc_spi_skip().
>
> Using schedule() to schedule (and be friendly to others)
> is discouraged and cond_resched() should be used instead.
> Hence, replace schedule() with cond_resched() at the same
> time.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/mmc/host/mmc_spi.c | 15 ++++-----------
>  1 file changed, 4 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
> index 65c65bb5737f..a1bcde3395a6 100644
> --- a/drivers/mmc/host/mmc_spi.c
> +++ b/drivers/mmc/host/mmc_spi.c
> @@ -180,7 +180,7 @@ static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout,
>         u8 *cp = host->data->status;
>         unsigned long start = jiffies;
>
> -       while (1) {
> +       do {
>                 int             status;
>                 unsigned        i;
>
> @@ -193,16 +193,9 @@ static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout,
>                                 return cp[i];
>                 }
>
> -               if (time_is_before_jiffies(start + timeout))
> -                       break;
> -
> -               /* If we need long timeouts, we may release the CPU.
> -                * We use jiffies here because we want to have a relation
> -                * between elapsed time and the blocking of the scheduler.
> -                */
> -               if (time_is_before_jiffies(start + 1))
> -                       schedule();
> -       }
> +               /* If we need long timeouts, we may release the CPU */
> +               cond_resched();
> +       } while (time_is_after_jiffies(start + timeout));

This certainly is an improvement.

Although, what do you think of moving to readx_poll_timeout(), that
should allow even a better cleanup, don't you think?

>         return -ETIMEDOUT;
>  }
>

Kind regards
Uffe

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

* Re: [PATCH v1 1/1] mmc: mmc_spi: Simplify busy loop in mmc_spi_skip()
  2021-07-08 12:31 ` Ulf Hansson
@ 2021-07-08 12:49   ` Andy Shevchenko
  2021-07-08 13:03     ` Ulf Hansson
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2021-07-08 12:49 UTC (permalink / raw)
  To: Ulf Hansson; +Cc: Andy Shevchenko, linux-mmc, Linux Kernel Mailing List

On Thu, Jul 8, 2021 at 3:33 PM Ulf Hansson <ulf.hansson@linaro.org> wrote:
> On Wed, 23 Jun 2021 at 12:17, Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:

...

> This certainly is an improvement.
>
> Although, what do you think of moving to readx_poll_timeout(), that
> should allow even a better cleanup, don't you think?

I believe you meant rather read_poll_timeout(). Either way I don't see
the benefit of using that macro when you have to customize its body a
lot. Besides that the macro doesn't use cond_sched() or even
schedule() and I'm not sure it will be an equivalent change.

That said, I prefer going this patch as is for the time being. We may
adjust it later on.

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH v1 1/1] mmc: mmc_spi: Simplify busy loop in mmc_spi_skip()
  2021-07-08 12:49   ` Andy Shevchenko
@ 2021-07-08 13:03     ` Ulf Hansson
  0 siblings, 0 replies; 4+ messages in thread
From: Ulf Hansson @ 2021-07-08 13:03 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Andy Shevchenko, linux-mmc, Linux Kernel Mailing List

On Thu, 8 Jul 2021 at 14:50, Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
>
> On Thu, Jul 8, 2021 at 3:33 PM Ulf Hansson <ulf.hansson@linaro.org> wrote:
> > On Wed, 23 Jun 2021 at 12:17, Andy Shevchenko
> > <andriy.shevchenko@linux.intel.com> wrote:
>
> ...
>
> > This certainly is an improvement.
> >
> > Although, what do you think of moving to readx_poll_timeout(), that
> > should allow even a better cleanup, don't you think?
>
> I believe you meant rather read_poll_timeout(). Either way I don't see
> the benefit of using that macro when you have to customize its body a
> lot. Besides that the macro doesn't use cond_sched() or even
> schedule() and I'm not sure it will be an equivalent change.
>
> That said, I prefer going this patch as is for the time being. We may
> adjust it later on.

Okay, no strong opinion from my side. Queued for v5.15 on my devel
branch, thanks!

Kind regards
Uffe

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

end of thread, other threads:[~2021-07-08 13:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-23 10:17 [PATCH v1 1/1] mmc: mmc_spi: Simplify busy loop in mmc_spi_skip() Andy Shevchenko
2021-07-08 12:31 ` Ulf Hansson
2021-07-08 12:49   ` Andy Shevchenko
2021-07-08 13:03     ` Ulf Hansson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).