Skip to contentSkip to conversation

mmc: core: RPMB code improvements

PATCH In review Bean Huo 3 participants 6 messages Sep 11, 2025 – Sep 12, 2025 MMC, SD and SDIO

lore
Share & tools
lore

Bean Huo

author
#0 lore raw

v3 cover mmc: core: RPMB code improvements

This patch series improves the RPMB frame handling code in the MMC block driver with several code quality enhancements.

v2--v3:
1. Previous version was mistakenly mixed with other changes, dropped them from v3.

v1--v2:
1. Add fix tag
2. Incorporate Avri's suggestions

Bean Huo (2):
mmc: core: Fix variable shadowing in mmc_route_rpmb_frames()
mmc: core: Improve RPMB frame handling code

 drivers/mmc/core/block.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/2 mmc: core: Fix variable shadowing in mmc_route_rpmb_frames()

From: Bean Huo <[email protected]>

Rename the inner 'frm' variable to 'resp_frm' in the write path of mmc_route_rpmb_frames() to avoid shadowing the outer 'frm' variable.

The function declares 'frm' at function scope pointing to the request frame, but then redeclares another 'frm' variable inside the write block pointing to the response frame. This shadowing makes the code confusing and error-prone.

Using 'resp_frm' for the response frame makes the distinction clear and improves code readability.

Fixes 7852028a35f0 ("mmc: block: register RPMB partition with the RPMB subsystem")
---
 drivers/mmc/core/block.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mmc/core/block.c +3 -3
index 9cc47bf94804..dd6cffc0df72 100644
@@ -2936,15 +2936,15 @@ static int mmc_route_rpmb_frames(struct device *dev, u8 *req,
 		return -ENOMEM;
 
 	if (write) {
-		struct rpmb_frame *frm = (struct rpmb_frame *)resp;
+		struct rpmb_frame *resp_frm = (struct rpmb_frame *)resp;
 
 		/* Send write request frame(s) */
 		set_idata(idata[0], MMC_WRITE_MULTIPLE_BLOCK,
 			  1 | MMC_CMD23_ARG_REL_WR, req, req_len);
 
 		/* Send result request frame */
-		memset(frm, 0, sizeof(*frm));
-		frm->req_resp = cpu_to_be16(RPMB_RESULT_READ);
+		memset(resp_frm, 0, sizeof(*resp_frm));
+		resp_frm->req_resp = cpu_to_be16(RPMB_RESULT_READ);
 		set_idata(idata[1], MMC_WRITE_MULTIPLE_BLOCK, 1, resp,
 			  resp_len);
 
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v3 2/2 mmc: core: Improve RPMB frame handling code

In reply to Bean Huo

From: Bean Huo <[email protected]>

Introduce RPMB_FRAME_SIZE, CHECK_SIZE_NEQ(), and CHECK_SIZE_ALIGNED() macros to replace repetitive sizeof(struct rpmb_frame) checks in mmc_route_rpmb_frames().

---
 drivers/mmc/core/block.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
drivers/mmc/core/block.c +14 -16
index dd6cffc0df72..b32eefcca4b7 100644
@@ -121,6 +121,10 @@ struct rpmb_frame {
 #define RPMB_READ_DATA         0x4    /* Read data from RPMB partition */
 #define RPMB_RESULT_READ       0x5    /* Read result request  (Internal) */
 
+#define RPMB_FRAME_SIZE        sizeof(struct rpmb_frame)
+#define CHECK_SIZE_NEQ(val) ((val) != sizeof(struct rpmb_frame))
+#define CHECK_SIZE_ALIGNED(val) IS_ALIGNED((val), sizeof(struct rpmb_frame))
+
 static DEFINE_MUTEX(block_mutex);
 
 /*
@@ -2864,12 +2868,12 @@ static void set_idata(struct mmc_blk_ioc_data *idata, u32 opcode,
 	 * The size of an RPMB frame must match what's expected by the
 	 * hardware.
 	 */
-	BUILD_BUG_ON(sizeof(struct rpmb_frame) != 512);
+	static_assert(!CHECK_SIZE_NEQ(512), "RPMB frame size must be 512 bytes");
 
 	idata->ic.opcode = opcode;
 	idata->ic.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
 	idata->ic.write_flag = write_flag;
-	idata->ic.blksz = sizeof(struct rpmb_frame);
+	idata->ic.blksz = RPMB_FRAME_SIZE;
 	idata->ic.blocks = buf_bytes /  idata->ic.blksz;
 	idata->buf = buf;
 	idata->buf_bytes = buf_bytes;
@@ -2893,32 +2897,28 @@ static int mmc_route_rpmb_frames(struct device *dev, u8 *req,
 	if (IS_ERR(md->queue.card))
 		return PTR_ERR(md->queue.card);
 
-	if (req_len < sizeof(*frm))
+	if (req_len < RPMB_FRAME_SIZE)
 		return -EINVAL;
 
 	req_type = be16_to_cpu(frm->req_resp);
 	switch (req_type) {
 	case RPMB_PROGRAM_KEY:
-		if (req_len != sizeof(struct rpmb_frame) ||
-		    resp_len != sizeof(struct rpmb_frame))
+		if (CHECK_SIZE_NEQ(req_len) || CHECK_SIZE_NEQ(resp_len))
 			return -EINVAL;
 		write = true;
 		break;
 	case RPMB_GET_WRITE_COUNTER:
-		if (req_len != sizeof(struct rpmb_frame) ||
-		    resp_len != sizeof(struct rpmb_frame))
+		if (CHECK_SIZE_NEQ(req_len) || CHECK_SIZE_NEQ(resp_len))
 			return -EINVAL;
 		write = false;
 		break;
 	case RPMB_WRITE_DATA:
-		if (req_len % sizeof(struct rpmb_frame) ||
-		    resp_len != sizeof(struct rpmb_frame))
+		if (!CHECK_SIZE_ALIGNED(req_len) || CHECK_SIZE_NEQ(resp_len))
 			return -EINVAL;
 		write = true;
 		break;
 	case RPMB_READ_DATA:
-		if (req_len != sizeof(struct rpmb_frame) ||
-		    resp_len % sizeof(struct rpmb_frame))
+		if (CHECK_SIZE_NEQ(req_len) || !CHECK_SIZE_ALIGNED(resp_len))
 			return -EINVAL;
 		write = false;
 		break;
@@ -2926,10 +2926,8 @@ static int mmc_route_rpmb_frames(struct device *dev, u8 *req,
 		return -EINVAL;
 	}
 
-	if (write)
-		cmd_count = 3;
-	else
-		cmd_count = 2;
+	/* Write operations require 3 commands, read operations require 2 */
+	cmd_count = write ? 3 : 2;
 
 	idata = alloc_idata(rpmb, cmd_count);
 	if (!idata)
@@ -2943,7 +2941,7 @@ static int mmc_route_rpmb_frames(struct device *dev, u8 *req,
 			  1 | MMC_CMD23_ARG_REL_WR, req, req_len);
 
 		/* Send result request frame */
-		memset(resp_frm, 0, sizeof(*resp_frm));
+		memset(resp_frm, 0, RPMB_FRAME_SIZE);
 		resp_frm->req_resp = cpu_to_be16(RPMB_RESULT_READ);
 		set_idata(idata[1], MMC_WRITE_MULTIPLE_BLOCK, 1, resp,
 			  resp_len);
Signature · 1 line
2.34.1
#4 lore raw
In reply to Bean Huo
Quoted 16 lines · Bean Huo

This patch series improves the RPMB frame handling code in the MMC block driver with several code quality enhancements.

v2--v3:
1. Previous version was mistakenly mixed with other changes, dropped them from v3.

v1--v2:
1. Add fix tag
2. Incorporate Avri's suggestions

Bean Huo (2):
mmc: core: Fix variable shadowing in mmc_route_rpmb_frames()
mmc: core: Improve RPMB frame handling code

Quoted 6 lines
 drivers/mmc/core/block.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)

--
2.34.1

Applied for next, but dropping the Fixes-tag from patch 1 (it's just a nice improvement of the code, not really fixing an error, right?), thanks!

Kind regards
Uffe

author
#5 lore raw
Quoted 8 lines · Ulf Hansson
Quoted 7 lines

  drivers/mmc/core/block.c | 34 ++++++++++++++++------------------

  1 file changed, 16 insertions(+), 18 deletions(-)

--
2.34.1

Quoted 6 lines

Applied for next, but dropping the Fixes-tag from patch 1 (it's just a nice improvement of the code, not really fixing an error, right?), thanks!

Kind regards
Uffe

thanks, Uffe.

kind regards,
Bean