Skip to contentSkip to conversation

blktests: src/miniublk.c: fix unaligned mmap offset for 64K page size

PATCH Applied Ming Lei 3 participants 3 messages Dec 6, 2024 – Dec 11, 2024 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 blktests: src/miniublk.c: fix unaligned mmap offset for 64K page size

The 'offset' passed to mmap() has to be PAGE_SIZE aligned, which is always true for 4K page size, but not true for 64K page size.

Fix it by adding helper of ublk_queue_max_cmd_buf_sz().

---
 src/miniublk.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
src/miniublk.c +13 -4
index 565aa60..73791fd 100644
@@ -472,14 +472,24 @@ static struct ublk_dev *ublk_ctrl_init()
 	return dev;
 }
 
-static int ublk_queue_cmd_buf_sz(struct ublk_queue *q)
+static int __ublk_queue_cmd_buf_sz(unsigned depth)
 {
-	int size =  q->q_depth * sizeof(struct ublksrv_io_desc);
+	int size =  depth * sizeof(struct ublksrv_io_desc);
 	unsigned int page_sz = getpagesize();
 
 	return round_up(size, page_sz);
 }
 
+static int ublk_queue_max_cmd_buf_sz(void)
+{
+	return __ublk_queue_cmd_buf_sz(UBLK_MAX_QUEUE_DEPTH);
+}
+
+static int ublk_queue_cmd_buf_sz(struct ublk_queue *q)
+{
+	return __ublk_queue_cmd_buf_sz(q->q_depth);
+}
+
 static void ublk_queue_deinit(struct ublk_queue *q)
 {
 	int i;
@@ -516,8 +526,7 @@ static int ublk_queue_init(struct ublk_queue *q)
 	q->tid = gettid();
 
 	cmd_buf_size = ublk_queue_cmd_buf_sz(q);
-	off = UBLKSRV_CMD_BUF_OFFSET +
-		q->q_id * (UBLK_MAX_QUEUE_DEPTH * sizeof(struct ublksrv_io_desc));
+	off = UBLKSRV_CMD_BUF_OFFSET + q->q_id * ublk_queue_max_cmd_buf_sz();
 	q->io_cmd_buf = (char *)mmap(0, cmd_buf_size, PROT_READ,
 			MAP_SHARED | MAP_POPULATE, dev->fds[0], off);
 	if (q->io_cmd_buf == MAP_FAILED) {
Signature · 1 line
2.47.0
Tested-by on v1 1/1
#1 lore raw

Verified with 6.13-rc1 64k kernel.

Quoted 56 lines · Ming Lei

The 'offset' passed to mmap() has to be PAGE_SIZE aligned, which is always true for 4K page size, but not true for 64K page size.

Fix it by adding helper of ublk_queue_max_cmd_buf_sz().

---
 src/miniublk.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
src/miniublk.c +13 -5
index 565aa60..73791fd 100644
@@ -472,14 +472,24 @@ static struct ublk_dev *ublk_ctrl_init()
        return dev;
 }
 
-static int ublk_queue_cmd_buf_sz(struct ublk_queue *q)
+static int __ublk_queue_cmd_buf_sz(unsigned depth)
 {
-       int size =  q->q_depth * sizeof(struct ublksrv_io_desc);
+       int size =  depth * sizeof(struct ublksrv_io_desc);
        unsigned int page_sz = getpagesize();
 
        return round_up(size, page_sz);
 }
 
+static int ublk_queue_max_cmd_buf_sz(void)
+{
+       return __ublk_queue_cmd_buf_sz(UBLK_MAX_QUEUE_DEPTH);
+}
+
+static int ublk_queue_cmd_buf_sz(struct ublk_queue *q)
+{
+       return __ublk_queue_cmd_buf_sz(q->q_depth);
+}
+
 static void ublk_queue_deinit(struct ublk_queue *q)
 {
        int i;
@@ -516,8 +526,7 @@ static int ublk_queue_init(struct ublk_queue *q)
        q->tid = gettid();
 
        cmd_buf_size = ublk_queue_cmd_buf_sz(q);
-       off = UBLKSRV_CMD_BUF_OFFSET +
-               q->q_id * (UBLK_MAX_QUEUE_DEPTH * sizeof(struct ublksrv_io_desc));
+       off = UBLKSRV_CMD_BUF_OFFSET + q->q_id * ublk_queue_max_cmd_buf_sz();
        q->io_cmd_buf = (char *)mmap(0, cmd_buf_size, PROT_READ,
                        MAP_SHARED | MAP_POPULATE, dev->fds[0], off);
        if (q->io_cmd_buf == MAP_FAILED) {
--

2.47.0

Signature · 2 lines
Best Regards,
  Yi Zhang
#2 lore raw
In reply to Ming Lei
Quoted 4 lines · Ming Lei

The 'offset' passed to mmap() has to be PAGE_SIZE aligned, which is always true for 4K page size, but not true for 64K page size.

Fix it by adding helper of ublk_queue_max_cmd_buf_sz().

I applied it. Thanks!