diff mbox series

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

Message ID 20241206110427.976391-1-ming.lei@redhat.com (mailing list archive)
State New
Headers show
Series blktests: src/miniublk.c: fix unaligned mmap offset for 64K page size | expand

Commit Message

Ming Lei Dec. 6, 2024, 11:04 a.m. UTC
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().

Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 src/miniublk.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

Comments

Yi Zhang Dec. 9, 2024, 3:18 a.m. UTC | #1
Verified with 6.13-rc1 64k kernel.

Tested-by: Yi Zhang <yi.zhang@redhat.com>

On Fri, Dec 6, 2024 at 7:04 PM Ming Lei <ming.lei@redhat.com> wrote:
>
> 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().
>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
> ---
>  src/miniublk.c | 17 +++++++++++++----
>  1 file changed, 13 insertions(+), 4 deletions(-)
>
> diff --git a/src/miniublk.c b/src/miniublk.c
> index 565aa60..73791fd 100644
> --- a/src/miniublk.c
> +++ b/src/miniublk.c
> @@ -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
>
>
Shinichiro Kawasaki Dec. 11, 2024, 10:30 a.m. UTC | #2
On Dec 06, 2024 / 19:04, Ming Lei wrote:
> 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!
diff mbox series

Patch

diff --git a/src/miniublk.c b/src/miniublk.c
index 565aa60..73791fd 100644
--- a/src/miniublk.c
+++ b/src/miniublk.c
@@ -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) {