Skip to contentSkip to conversation

aio: Support read/write with non-iter file-ops

PATCH In review Bjorn Andersson 3 participants 6 messages Jul 18, 2019 – Jul 19, 2019

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

PATCH 1/1 aio: Support read/write with non-iter file-ops

Implement a wrapper for aio_read()/write() to allow async IO on files not implementing the iter version of read/write, such as sysfs. This mimics how readv/writev uses non-iter ops in do_loop_readv_writev().

---
 fs/aio.c | 52 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 45 insertions(+), 7 deletions(-)
fs/aio.c +45 -7
index f9f441b59966..0137a1a9bef1 100644
@@ -1514,12 +1514,44 @@ static inline void aio_rw_done(struct kiocb *req, ssize_t ret)
 	}
 }
 
+static ssize_t aio_iter_readv_writev(struct file *file, struct kiocb *req,
+				     struct iov_iter *iter, int type)
+{
+	ssize_t ret = 0;
+	ssize_t nr;
+
+	while (iov_iter_count(iter)) {
+		struct iovec iovec = iov_iter_iovec(iter);
+
+		if (type == READ) {
+			nr = file->f_op->read(file, iovec.iov_base,
+					      iovec.iov_len, &req->ki_pos);
+		} else {
+			nr = file->f_op->write(file, iovec.iov_base,
+					       iovec.iov_len, &req->ki_pos);
+		}
+
+		if (nr < 0) {
+			ret = nr;
+			break;
+		}
+
+		ret += nr;
+		if (nr != iovec.iov_len)
+			break;
+		iov_iter_advance(iter, nr);
+	}
+
+	return ret;
+}
+
 static int aio_read(struct kiocb *req, const struct iocb *iocb,
 			bool vectored, bool compat)
 {
 	struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
 	struct iov_iter iter;
 	struct file *file;
+	ssize_t count;
 	int ret;
 
 	ret = aio_prep_rw(req, iocb);
@@ -1529,15 +1561,18 @@ static int aio_read(struct kiocb *req, const struct iocb *iocb,
 	if (unlikely(!(file->f_mode & FMODE_READ)))
 		return -EBADF;
 	ret = -EINVAL;
-	if (unlikely(!file->f_op->read_iter))
-		return -EINVAL;
 
 	ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter);
 	if (ret < 0)
 		return ret;
 	ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter));
-	if (!ret)
-		aio_rw_done(req, call_read_iter(file, req, &iter));
+	if (!ret) {
+		if (likely(file->f_op->read_iter))
+			count = call_read_iter(file, req, &iter);
+		else
+			count = aio_iter_readv_writev(file, req, &iter, READ);
+		aio_rw_done(req, count);
+	}
 	kfree(iovec);
 	return ret;
 }
@@ -1548,6 +1583,7 @@ static int aio_write(struct kiocb *req, const struct iocb *iocb,
 	struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
 	struct iov_iter iter;
 	struct file *file;
+	ssize_t count;
 	int ret;
 
 	ret = aio_prep_rw(req, iocb);
@@ -1557,8 +1593,6 @@ static int aio_write(struct kiocb *req, const struct iocb *iocb,
 
 	if (unlikely(!(file->f_mode & FMODE_WRITE)))
 		return -EBADF;
-	if (unlikely(!file->f_op->write_iter))
-		return -EINVAL;
 
 	ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter);
 	if (ret < 0)
@@ -1577,7 +1611,11 @@ static int aio_write(struct kiocb *req, const struct iocb *iocb,
 			__sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE);
 		}
 		req->ki_flags |= IOCB_WRITE;
-		aio_rw_done(req, call_write_iter(file, req, &iter));
+		if (likely(file->f_op->write_iter))
+			count = call_write_iter(file, req, &iter);
+		else
+			count = aio_iter_readv_writev(file, req, &iter, WRITE);
+		aio_rw_done(req, count);
 	}
 	kfree(iovec);
 	return ret;
Signature · 1 line
2.18.0
maintainer
#1 lore raw
Quoted 3 lines · Bjorn Andersson

Implement a wrapper for aio_read()/write() to allow async IO on files not implementing the iter version of read/write, such as sysfs. This mimics how readv/writev uses non-iter ops in do_loop_readv_writev().

IDGI. How would that IO manage to be async? And what's the point using aio in such situations in the first place?

#2 lore raw
Quoted 7 lines · Al Viro
Quoted 3 lines · Bjorn Andersson

Implement a wrapper for aio_read()/write() to allow async IO on files not implementing the iter version of read/write, such as sysfs. This mimics how readv/writev uses non-iter ops in do_loop_readv_writev().

IDGI. How would that IO manage to be async? And what's the point using aio in such situations in the first place?

The point is that an application using aio to submit io operations on a set of files, can use the same mechanism to read/write files that happens to be implemented by driver only implementing read/write (not read_iter/write_iter) in the registered file_operations struct, such as kernfs.

In this particular case I have a sysfs file that is accessing hardware and hence will block for a while and using this patch I can io_submit() a write and handle the completion of this in my normal event loop.

Each individual io operation will be just as synchronous as the current iter-based mechanism - for the drivers that implement that.

Regards,
Bjorn

maintainer
#3 lore raw
Quoted 7 lines · Bjorn Andersson
Quoted 5 lines · Al Viro
Quoted 3 lines · Bjorn Andersson

Implement a wrapper for aio_read()/write() to allow async IO on files not implementing the iter version of read/write, such as sysfs. This mimics how readv/writev uses non-iter ops in do_loop_readv_writev().

Quoted 5 lines
Quoted 2 lines

IDGI. How would that IO manage to be async? And what's the point using aio in such situations in the first place?

The point is that an application using aio to submit io operations on a set of files,

... for no reason whatsoever, I take it?

Quoted 4 lines

can use the same mechanism to read/write files that
happens to be implemented by driver only implementing read/write (not read_iter/write_iter) in the registered file_operations struct, such as kernfs.

... except that it still has to support the kernels that don't have your patch, so the fallback in userland is *not* going away.

#4 lore raw
In reply to Bjorn Andersson
Quoted 16 lines · Bjorn Andersson
Quoted 7 lines · Al Viro
Quoted 3 lines · Bjorn Andersson

Implement a wrapper for aio_read()/write() to allow async IO on files not implementing the iter version of read/write, such as sysfs. This mimics how readv/writev uses non-iter ops in do_loop_readv_writev().

IDGI. How would that IO manage to be async? And what's the point using aio in such situations in the first place?

The point is that an application using aio to submit io operations on a set of files, can use the same mechanism to read/write files that happens to be implemented by driver only implementing read/write (not read_iter/write_iter) in the registered file_operations struct, such as kernfs.

Quoted 7 lines

In this particular case I have a sysfs file that is accessing hardware and hence will block for a while and using this patch I can io_submit() a write and handle the completion of this in my normal event loop.

Each individual io operation will be just as synchronous as the current iter-based mechanism - for the drivers that implement that.

Just adding the fops is not enough. I have patches floating around at Solace that add thread based fallbacks for files that don't have an aio read / write implementation, but I'm not working on that code any more. The thread based methods were quite useful in applications that had a need for using other kernel infrastructure in their main event loops.

-ben

Quoted 3 lines

Regards,
Bjorn

Signature · 1 line
"Thought is the essence of where you are now."
#5 lore raw
Quoted 22 lines · Benjamin LaHaise
Quoted 21 lines · Bjorn Andersson
Quoted 7 lines · Al Viro
Quoted 3 lines · Bjorn Andersson

Implement a wrapper for aio_read()/write() to allow async IO on files not implementing the iter version of read/write, such as sysfs. This mimics how readv/writev uses non-iter ops in do_loop_readv_writev().

IDGI. How would that IO manage to be async? And what's the point using aio in such situations in the first place?

The point is that an application using aio to submit io operations on a set of files, can use the same mechanism to read/write files that happens to be implemented by driver only implementing read/write (not read_iter/write_iter) in the registered file_operations struct, such as kernfs.

In this particular case I have a sysfs file that is accessing hardware and hence will block for a while and using this patch I can io_submit() a write and handle the completion of this in my normal event loop.

Quoted 6 lines
Quoted 2 lines

Each individual io operation will be just as synchronous as the current iter-based mechanism - for the drivers that implement that.

Just adding the fops is not enough. I have patches floating around at Solace that add thread based fallbacks for files that don't have an aio read / write implementation, but I'm not working on that code any more.

My bad. Took another look and now I see the bigger picture of how this is currently implemented and why just adding the fops would defeat the purpose of the api.

Sorry for the noise.

Quoted 3 lines

The thread based methods were quite useful in applications that had a need for using other kernel infrastructure in their main event loops.

Yes indeed.

Regards,
Bjorn