* [PATCH] ublk: read any SQE values upfront
@ 2023-04-06 2:03 Jens Axboe
2023-04-06 2:53 ` Ming Lei
0 siblings, 1 reply; 2+ messages in thread
From: Jens Axboe @ 2023-04-06 2:03 UTC (permalink / raw)
To: io-uring, [email protected]; +Cc: Ming Lei
Since SQE memory is shared with userspace, we should only be reading it
once. We cannot read it multiple times, particularly when it's read once
for validation and then read again for the actual use.
ublk_ch_uring_cmd() is safe when called as a retry operation, as the
memory backing is stable at that point. But for normal issue, we want
to ensure that we only read ublksrv_io_cmd once. Wrap the function in
a helper that reads the value into an on-stack copy of the struct.
Cc: [email protected] # 6.0+
Signed-off-by: Jens Axboe <[email protected]>
---
drivers/block/ublk_drv.c | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c
index d1d1c8d606c8..b7a28f5d9297 100644
--- a/drivers/block/ublk_drv.c
+++ b/drivers/block/ublk_drv.c
@@ -1256,9 +1256,10 @@ static void ublk_handle_need_get_data(struct ublk_device *ub, int q_id,
ublk_queue_cmd(ubq, req);
}
-static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
+static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
+ unsigned int issue_flags,
+ struct ublksrv_io_cmd *ub_cmd)
{
- struct ublksrv_io_cmd *ub_cmd = (struct ublksrv_io_cmd *)cmd->cmd;
struct ublk_device *ub = cmd->file->private_data;
struct ublk_queue *ubq;
struct ublk_io *io;
@@ -1357,6 +1358,23 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
return -EIOCBQUEUED;
}
+static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
+{
+ struct ublksrv_io_cmd *ub_src = (struct ublksrv_io_cmd *) cmd->cmd;
+ struct ublksrv_io_cmd ub_cmd;
+
+ /*
+ * Not necessary for async retry, but let's keep it simple and always
+ * copy the values to avoid any potential reuse.
+ */
+ ub_cmd.q_id = READ_ONCE(ub_src->q_id);
+ ub_cmd.tag = READ_ONCE(ub_src->tag);
+ ub_cmd.result = READ_ONCE(ub_src->result);
+ ub_cmd.addr = READ_ONCE(ub_src->addr);
+
+ return __ublk_ch_uring_cmd(cmd, issue_flags, &ub_cmd);
+}
+
static const struct file_operations ublk_ch_fops = {
.owner = THIS_MODULE,
.open = ublk_ch_open,
--
Jens Axboe
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] ublk: read any SQE values upfront
2023-04-06 2:03 [PATCH] ublk: read any SQE values upfront Jens Axboe
@ 2023-04-06 2:53 ` Ming Lei
0 siblings, 0 replies; 2+ messages in thread
From: Ming Lei @ 2023-04-06 2:53 UTC (permalink / raw)
To: Jens Axboe; +Cc: io-uring, [email protected]
On Wed, Apr 05, 2023 at 08:03:57PM -0600, Jens Axboe wrote:
> Since SQE memory is shared with userspace, we should only be reading it
> once. We cannot read it multiple times, particularly when it's read once
> for validation and then read again for the actual use.
>
> ublk_ch_uring_cmd() is safe when called as a retry operation, as the
> memory backing is stable at that point. But for normal issue, we want
> to ensure that we only read ublksrv_io_cmd once. Wrap the function in
> a helper that reads the value into an on-stack copy of the struct.
>
> Cc: [email protected] # 6.0+
> Signed-off-by: Jens Axboe <[email protected]>
Reviewed-by: Ming Lei <[email protected]>
Thanks,
Ming
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-04-06 2:55 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-06 2:03 [PATCH] ublk: read any SQE values upfront Jens Axboe
2023-04-06 2:53 ` Ming Lei
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox