Skip to contentSkip to conversation

Do not flush cache when it is disabled

PATCH Superseded Avri Altman 2 participants 6 messages Apr 18, 2021 – Apr 19, 2021 MMC, SD and SDIO

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover Do not flush cache when it is disabled

v1 -> v2:
- Attend Adrian's comments

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   | 19 +++++++++++++++++++
 drivers/mmc/core/mmc_ops.c |  4 +---
 drivers/mmc/core/mmc_ops.h |  5 +++++
 3 files changed, 25 insertions(+), 3 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 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)
---
 drivers/mmc/core/block.c   | 7 +++++++
 drivers/mmc/core/mmc_ops.c | 4 +---
 drivers/mmc/core/mmc_ops.h | 5 +++++
 3 files changed, 13 insertions(+), 3 deletions(-)
drivers/mmc/core/block.c +7 -0
index fe5892d30778..6800feb70c92 100644
@@ -1476,6 +1476,11 @@ 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)) {
+		blk_mq_end_request(req, BLK_STS_OK);
+		return -EPERM;
+	}
+
 	mrq->cmd->opcode = MMC_SWITCH;
 	mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
 			(EXT_CSD_FLUSH_CACHE << 16) |
@@ -2226,6 +2231,8 @@ enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
 		switch (req_op(req)) {
 		case REQ_OP_FLUSH:
 			ret = mmc_blk_cqe_issue_flush(mq, req);
+			if (ret == -EPERM)
+				return MMC_REQ_FINISHED;
 			break;
 		case REQ_OP_READ:
 		case REQ_OP_WRITE:
drivers/mmc/core/mmc_ops.c +1 -3
index f413474f0f80..9c2a665be034 100644
@@ -967,9 +967,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);
drivers/mmc/core/mmc_ops.h +5 -0
index 632009260e51..bf2b315addd7 100644
@@ -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);
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 1/2] mmc: block: Issue flush only if allowed

Quoted 6 lines · Avri Altman

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.

Avri, thanks for your patch. Overall this looks good to me.

However things are becoming more and more messy in these layers of the mmc core. In particular, I would like us to take advantage of the bus_ops callbacks, when eMMC and/or SD specific features need different implementations.

I have posted a patch [1], that moves the eMMC cache flushing into a bus_ops callback. Would you mind rebasing this series on top of that?

Kind regards
Uffe

[1]
https://patchwork.kernel.org/project/linux-mmc/patch/[email protected]/

Quoted 71 lines
Fixes 1e8e55b67030 (mmc: block: Add CQE support)
---
 drivers/mmc/core/block.c   | 7 +++++++
 drivers/mmc/core/mmc_ops.c | 4 +---
 drivers/mmc/core/mmc_ops.h | 5 +++++
 3 files changed, 13 insertions(+), 3 deletions(-)
drivers/mmc/core/block.c +7 -0
index fe5892d30778..6800feb70c92 100644
@@ -1476,6 +1476,11 @@ 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)) {
+               blk_mq_end_request(req, BLK_STS_OK);
+               return -EPERM;
+       }
+
        mrq->cmd->opcode = MMC_SWITCH;
        mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
                        (EXT_CSD_FLUSH_CACHE << 16) |
@@ -2226,6 +2231,8 @@ enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
                switch (req_op(req)) {
                case REQ_OP_FLUSH:
                        ret = mmc_blk_cqe_issue_flush(mq, req);
+                       if (ret == -EPERM)
+                               return MMC_REQ_FINISHED;
                        break;
                case REQ_OP_READ:
                case REQ_OP_WRITE:
drivers/mmc/core/mmc_ops.c +1 -3
index f413474f0f80..9c2a665be034 100644
@@ -967,9 +967,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);
drivers/mmc/core/mmc_ops.h +5 -1
index 632009260e51..bf2b315addd7 100644
@@ -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

#3 lore raw

RE: [PATCH v2 1/2] mmc: block: Issue flush only if allowed

Quoted 12 lines · Avri Altman
Quoted 3 lines

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

Quoted 3 lines

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.

Avri, thanks for your patch. Overall this looks good to me.

Quoted 7 lines

However things are becoming more and more messy in these layers of the mmc core. In particular, I would like us to take advantage of the bus_ops callbacks, when eMMC and/or SD specific features need different implementations.

I have posted a patch [1], that moves the eMMC cache flushing into a bus_ops callback. Would you mind rebasing this series on top of that?

Sure.

Thanks,
Avri

Quoted 7 lines

Kind regards
Uffe

[1]
https://patchwork.kernel.org/project/linux-
mmc/patch/[email protected]/

#4 lore raw

RE: [PATCH v2 1/2] mmc: block: Issue flush only if allowed

In reply to Ulf Hansson
Quoted 12 lines · Avri Altman
Quoted 3 lines

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

Quoted 3 lines

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.

Avri, thanks for your patch. Overall this looks good to me.

Quoted 7 lines

However things are becoming more and more messy in these layers of the mmc core. In particular, I would like us to take advantage of the bus_ops callbacks, when eMMC and/or SD specific features need different implementations.

I have posted a patch [1], that moves the eMMC cache flushing into a bus_ops callback. Would you mind rebasing this series on top of that?

Sure.

Thanks,
Avri

Quoted 7 lines

Kind regards
Uffe

[1]
https://patchwork.kernel.org/project/linux-
mmc/patch/[email protected]/

#5 lore raw

PATCH v2 2/2 mmc: block: Update ext_csd.cache_ctrl if it was written

In reply to Avri Altman

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.

---
 drivers/mmc/core/block.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/mmc/core/block.c +12 -0
index 6800feb70c92..0372702231ce 100644
@@ -572,6 +572,18 @@ 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. The cache
+	 * will get turned back on if the card is re-initialized, e.g.
+	 * 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.
Signature · 1 line
2.25.1