Skip to contentSkip to conversation

fs/aio: Check IOCB_AIO_RW before the struct aio_kiocb conversion

PATCH In review Bart Van Assche 4 participants 4 messages Mar 4, 2024 – Mar 5, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/aio: Check IOCB_AIO_RW before the struct aio_kiocb conversion

The first kiocb_set_cancel_fn() argument may point at a struct kiocb that is not embedded inside struct aio_kiocb. With the current code, depending on the compiler, the req->ki_ctx read happens either before the IOCB_AIO_RW test or after that test. Move the req->ki_ctx read such that it is guaranteed that the IOCB_AIO_RW test happens first.

Cc Benjamin LaHaise <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Avi Kivity <[email protected]>
Cc Sandeep Dhavale <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Kent Overstreet <[email protected]>
Fixes b820de741ae4 ("fs/aio: Restrict kiocb_set_cancel_fn() to I/O submitted via libaio")
---
 fs/aio.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/aio.c +6 -2
index da18dbcfcb22..9cdaa2faa536 100644
@@ -589,8 +589,8 @@ static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
 
 void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
 {
-	struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw);
-	struct kioctx *ctx = req->ki_ctx;
+	struct aio_kiocb *req;
+	struct kioctx *ctx;
 	unsigned long flags;
 
 	/*
@@ -600,9 +600,13 @@ void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
 	if (!(iocb->ki_flags & IOCB_AIO_RW))
 		return;
 
+	req = container_of(iocb, struct aio_kiocb, rw);
+
 	if (WARN_ON_ONCE(!list_empty(&req->ki_list)))
 		return;
 
+	ctx = req->ki_ctx;
+
 	spin_lock_irqsave(&ctx->ctx_lock, flags);
 	list_add_tail(&req->ki_list, &ctx->active_reqs);
 	req->ki_cancel = cancel;
#1 lore raw
Quoted 18 lines · Bart Van Assche

The first kiocb_set_cancel_fn() argument may point at a struct kiocb that is not embedded inside struct aio_kiocb. With the current code, depending on the compiler, the req->ki_ctx read happens either before the IOCB_AIO_RW test or after that test. Move the req->ki_ctx read such that it is guaranteed that the IOCB_AIO_RW test happens first.

Cc Benjamin LaHaise <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Avi Kivity <[email protected]>
Cc Sandeep Dhavale <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Kent Overstreet <[email protected]>
Fixes b820de741ae4 ("fs/aio: Restrict kiocb_set_cancel_fn() to I/O submitted via libaio")
Quoted 1 line

---

Can I please get a review from Eric and/or Benjamin, please?

Quoted 32 lines
 fs/aio.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/aio.c +6 -2
index da18dbcfcb22..9cdaa2faa536 100644
@@ -589,8 +589,8 @@ static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
 
 void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
 {
-	struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw);
-	struct kioctx *ctx = req->ki_ctx;
+	struct aio_kiocb *req;
+	struct kioctx *ctx;
 	unsigned long flags;
 
 	/*
@@ -600,9 +600,13 @@ void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
 	if (!(iocb->ki_flags & IOCB_AIO_RW))
 		return;
 
+	req = container_of(iocb, struct aio_kiocb, rw);
+
 	if (WARN_ON_ONCE(!list_empty(&req->ki_list)))
 		return;
 
+	ctx = req->ki_ctx;
+
 	spin_lock_irqsave(&ctx->ctx_lock, flags);
 	list_add_tail(&req->ki_list, &ctx->active_reqs);
 	req->ki_cancel = cancel;
Reviewed-by on v1 1/1
#2 lore raw
In reply to Bart Van Assche
Quoted 5 lines · Bart Van Assche

The first kiocb_set_cancel_fn() argument may point at a struct kiocb that is not embedded inside struct aio_kiocb. With the current code, depending on the compiler, the req->ki_ctx read happens either before the IOCB_AIO_RW test or after that test. Move the req->ki_ctx read such that it is guaranteed that the IOCB_AIO_RW test happens first.

Signature · 1 line
Jens Axboe
Reviewed-by on v1 1/1
#3 lore raw
In reply to Bart Van Assche
Quoted 41 lines · Bart Van Assche

The first kiocb_set_cancel_fn() argument may point at a struct kiocb that is not embedded inside struct aio_kiocb. With the current code, depending on the compiler, the req->ki_ctx read happens either before the IOCB_AIO_RW test or after that test. Move the req->ki_ctx read such that it is guaranteed that the IOCB_AIO_RW test happens first.

Cc Benjamin LaHaise <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Avi Kivity <[email protected]>
Cc Sandeep Dhavale <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Kent Overstreet <[email protected]>
Fixes b820de741ae4 ("fs/aio: Restrict kiocb_set_cancel_fn() to I/O submitted via libaio")
---
 fs/aio.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/aio.c +2 -2
index da18dbcfcb22..9cdaa2faa536 100644
@@ -589,8 +589,8 @@ static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
 
 void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
 {
-	struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw);
-	struct kioctx *ctx = req->ki_ctx;
+	struct aio_kiocb *req;
+	struct kioctx *ctx;
 	unsigned long flags;
 
 	/*
@@ -600,9 +600,13 @@ void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
 	if (!(iocb->ki_flags & IOCB_AIO_RW))
 		return;
 

fs/aio.c void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel) · line 612

Quoted 10 lines
fs/aio.c +4 -0
+	req = container_of(iocb, struct aio_kiocb, rw);
+
 	if (WARN_ON_ONCE(!list_empty(&req->ki_list)))
 		return;
 
+	ctx = req->ki_ctx;
+
 	spin_lock_irqsave(&ctx->ctx_lock, flags);
 	list_add_tail(&req->ki_list, &ctx->active_reqs);
 	req->ki_cancel = cancel;

- Eric