2021-03-22 13:39:18

by Avri Altman

[permalink] [raw]
Subject: [PATCH 0/2] Do not flush cache when it is disabled

Cache is a temporary storage space in an eMMC device. Volatile by
nature, the cache should in typical case reduce the access time compared
to an access to the main nonvolatile storage.

The cache function can be turned ON and OFF. Once OFF, the host is not
expected to issue a flush-cache command to the device.


Avri Altman (2):
mmc: block: Issue flush only if allowed
mmc: block: Update ext_csd.cache_ctrl if it was written

drivers/mmc/core/block.c | 11 +++++++++++
drivers/mmc/core/mmc_ops.c | 4 +---
drivers/mmc/core/mmc_ops.h | 5 +++++
3 files changed, 17 insertions(+), 3 deletions(-)

--
2.25.1


2021-03-22 13:42:07

by Avri Altman

[permalink] [raw]
Subject: [PATCH 1/2] mmc: block: Issue flush only if allowed

The cache may be flushed to the nonvolatile storage by writing to
FLUSH_CACHE byte (EXT_CSD byte [32]). When in command queueing mode, the
cache may be flushed by issuing a CMDQ_TASK_ DEV_MGMT (CMD48) with a
FLUSH_CACHE op-code. Either way, verify that The cache function is
turned ON before doing so.

fixes: 1e8e55b67030 (mmc: block: Add CQE support)

Reported-by: Brendan Peter <[email protected]>
Tested-by: Brendan Peter <[email protected]>
Signed-off-by: Avri Altman <[email protected]>
---
drivers/mmc/core/block.c | 3 +++
drivers/mmc/core/mmc_ops.c | 4 +---
drivers/mmc/core/mmc_ops.h | 5 +++++
3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c
index 42e27a298218..a93c66aa91db 100644
--- a/drivers/mmc/core/block.c
+++ b/drivers/mmc/core/block.c
@@ -1473,6 +1473,9 @@ static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);

+ if (mmc_card_mmc(mq->card) && !mmc_flush_allowed(mq->card))
+ return 0;
+
mrq->cmd->opcode = MMC_SWITCH;
mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
(EXT_CSD_FLUSH_CACHE << 16) |
diff --git a/drivers/mmc/core/mmc_ops.c b/drivers/mmc/core/mmc_ops.c
index baa6314f69b4..b8a0c9ac8a20 100644
--- a/drivers/mmc/core/mmc_ops.c
+++ b/drivers/mmc/core/mmc_ops.c
@@ -988,9 +988,7 @@ int mmc_flush_cache(struct mmc_card *card)
{
int err = 0;

- if (mmc_card_mmc(card) &&
- (card->ext_csd.cache_size > 0) &&
- (card->ext_csd.cache_ctrl & 1)) {
+ if (mmc_card_mmc(card) && mmc_flush_allowed(card)) {
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
EXT_CSD_FLUSH_CACHE, 1,
MMC_CACHE_FLUSH_TIMEOUT_MS);
diff --git a/drivers/mmc/core/mmc_ops.h b/drivers/mmc/core/mmc_ops.h
index 632009260e51..bf2b315addd7 100644
--- a/drivers/mmc/core/mmc_ops.h
+++ b/drivers/mmc/core/mmc_ops.h
@@ -19,6 +19,11 @@ enum mmc_busy_cmd {
struct mmc_host;
struct mmc_card;

+static inline bool mmc_flush_allowed(struct mmc_card *card)
+{
+ return card->ext_csd.cache_size > 0 && (card->ext_csd.cache_ctrl & 1);
+}
+
int mmc_select_card(struct mmc_card *card);
int mmc_deselect_cards(struct mmc_host *host);
int mmc_set_dsr(struct mmc_host *host);
--
2.25.1

2021-03-22 13:42:08

by Avri Altman

[permalink] [raw]
Subject: [PATCH 2/2] mmc: block: Update ext_csd.cache_ctrl if it was written

The cache function can be turned ON and OFF by writing to the CACHE_CTRL
byte (EXT_CSD byte [33]). However, card->ext_csd.cache_ctrl is only
set on init if cache size > 0.

Fix that by explicitly setting ext_csd.cache_ctrl on ext-csd write.

Signed-off-by: Avri Altman <[email protected]>
---
drivers/mmc/core/block.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c
index a93c66aa91db..4c816d001ac1 100644
--- a/drivers/mmc/core/block.c
+++ b/drivers/mmc/core/block.c
@@ -571,6 +571,14 @@ static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
}

+ /* Make sure to update CACHE_CTRL in case it was changed */
+ if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
+ (cmd.opcode == MMC_SWITCH)) {
+ u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
+
+ card->ext_csd.cache_ctrl = value;
+ }
+
/*
* According to the SD specs, some commands require a delay after
* issuing the command.
--
2.25.1

2021-03-22 18:02:38

by Adrian Hunter

[permalink] [raw]
Subject: Re: [PATCH 1/2] mmc: block: Issue flush only if allowed

On 22/03/21 3:36 pm, Avri Altman wrote:
> The cache may be flushed to the nonvolatile storage by writing to
> FLUSH_CACHE byte (EXT_CSD byte [32]). When in command queueing mode, the
> cache may be flushed by issuing a CMDQ_TASK_ DEV_MGMT (CMD48) with a
> FLUSH_CACHE op-code. Either way, verify that The cache function is
> turned ON before doing so.
>
> fixes: 1e8e55b67030 (mmc: block: Add CQE support)
>
> Reported-by: Brendan Peter <[email protected]>
> Tested-by: Brendan Peter <[email protected]>
> Signed-off-by: Avri Altman <[email protected]>
> ---
> drivers/mmc/core/block.c | 3 +++
> drivers/mmc/core/mmc_ops.c | 4 +---
> drivers/mmc/core/mmc_ops.h | 5 +++++
> 3 files changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c
> index 42e27a298218..a93c66aa91db 100644
> --- a/drivers/mmc/core/block.c
> +++ b/drivers/mmc/core/block.c
> @@ -1473,6 +1473,9 @@ static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
> struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
> struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
>
> + if (mmc_card_mmc(mq->card) && !mmc_flush_allowed(mq->card))
> + return 0;

Returning 0 means the flush was issued successfully. i.e. it is in
flight.

Instead, a call to blk_mq_end_request(req, BLK_STS_OK) is needed,
and mmc_blk_mq_issue_rq() must be amended so that it will return
MMC_REQ_DONE for this case.

> +
> mrq->cmd->opcode = MMC_SWITCH;
> mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
> (EXT_CSD_FLUSH_CACHE << 16) |
> diff --git a/drivers/mmc/core/mmc_ops.c b/drivers/mmc/core/mmc_ops.c
> index baa6314f69b4..b8a0c9ac8a20 100644
> --- a/drivers/mmc/core/mmc_ops.c
> +++ b/drivers/mmc/core/mmc_ops.c
> @@ -988,9 +988,7 @@ int mmc_flush_cache(struct mmc_card *card)
> {
> int err = 0;
>
> - if (mmc_card_mmc(card) &&
> - (card->ext_csd.cache_size > 0) &&
> - (card->ext_csd.cache_ctrl & 1)) {
> + if (mmc_card_mmc(card) && mmc_flush_allowed(card)) {
> err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
> EXT_CSD_FLUSH_CACHE, 1,
> MMC_CACHE_FLUSH_TIMEOUT_MS);
> diff --git a/drivers/mmc/core/mmc_ops.h b/drivers/mmc/core/mmc_ops.h
> index 632009260e51..bf2b315addd7 100644
> --- a/drivers/mmc/core/mmc_ops.h
> +++ b/drivers/mmc/core/mmc_ops.h
> @@ -19,6 +19,11 @@ enum mmc_busy_cmd {
> struct mmc_host;
> struct mmc_card;
>
> +static inline bool mmc_flush_allowed(struct mmc_card *card)
> +{
> + return card->ext_csd.cache_size > 0 && (card->ext_csd.cache_ctrl & 1);
> +}
> +
> int mmc_select_card(struct mmc_card *card);
> int mmc_deselect_cards(struct mmc_host *host);
> int mmc_set_dsr(struct mmc_host *host);
>

2021-03-22 19:01:37

by Adrian Hunter

[permalink] [raw]
Subject: Re: [PATCH 2/2] mmc: block: Update ext_csd.cache_ctrl if it was written

On 22/03/21 3:36 pm, Avri Altman wrote:
> The cache function can be turned ON and OFF by writing to the CACHE_CTRL
> byte (EXT_CSD byte [33]). However, card->ext_csd.cache_ctrl is only
> set on init if cache size > 0.
>
> Fix that by explicitly setting ext_csd.cache_ctrl on ext-csd write.
>
> Signed-off-by: Avri Altman <[email protected]>
> ---
> drivers/mmc/core/block.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c
> index a93c66aa91db..4c816d001ac1 100644
> --- a/drivers/mmc/core/block.c
> +++ b/drivers/mmc/core/block.c
> @@ -571,6 +571,14 @@ static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
> main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
> }
>
> + /* Make sure to update CACHE_CTRL in case it was changed */

It might be worth noting that the cache will get turned back
on if the card is re-initialized, for example suspend/resume or
hw reset in recovery.

> + if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
> + (cmd.opcode == MMC_SWITCH)) {
> + u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
> +
> + card->ext_csd.cache_ctrl = value;
> + }
> +
> /*
> * According to the SD specs, some commands require a delay after
> * issuing the command.
>

2021-04-02 07:48:51

by Avri Altman

[permalink] [raw]
Subject: Re: [PATCH 1/2] mmc: block: Issue flush only if allowed

>> @@ -1473,6 +1473,9 @@ static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
>> struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
>> struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
>>
>> + if (mmc_card_mmc(mq->card) && !mmc_flush_allowed(mq->card))
>> + return 0;

> Returning 0 means the flush was issued successfully. i.e. it is in
> flight.
>
> Instead, a call to blk_mq_end_request(req, BLK_STS_OK) is needed,
> and mmc_blk_mq_issue_rq() must be amended so that it will return
> MMC_REQ_DONE for this case.
Done.

Thanks,
Avri

2021-04-02 08:00:35

by Avri Altman

[permalink] [raw]
Subject: Re: [PATCH 2/2] mmc: block: Update ext_csd.cache_ctrl if it was written

>> @@ -571,6 +571,14 @@ static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
>> main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
>> }
>>
>> + /* Make sure to update CACHE_CTRL in case it was changed */

> It might be worth noting that the cache will get turned back
> on if the card is re-initialized, for example suspend/resume or
> hw reset in recovery.
Done.

Thanks,
Avri