Skip to contentSkip to conversation

erofs: file-backed mount supports direct io

PATCH New Hongbo Li 1 participant 5 messages Feb 11, 2025 EROFS file system

lore
Share & tools
lore

Hongbo Li

author
#0 lore raw

v2 cover erofs: file-backed mount supports direct io

erofs has add file-backed mount support. In this scenario, only buffer io is allowed. So we enhance the io mode by implementing the direct io. Also, this can make the iov_iter (user buffer) interact with the backed file's page cache directly.

To be mentioned, the direct io is atomic, if the part of the iov_iter of direct io failed, the whole direct io also fails.

We conduct the basic test on direct io and normal io, the fio is used in the test, the results show it can decrease the memory overhead. It slower than normal io in seq read due to erofs page cache and readahead, uut in rand read direct io is similar than buffer io. The results are reasonable.

v2:
- Reuse the original erofs_fileio_scan_folio logic and do some construction.

v1:
- https://lore.kernel.org/all/[email protected]/T/

Comments and questions are, as always, welcome.

Thanks,
Hongbo.

Hongbo Li (4):
erofs: decouple the iterator on folio
erofs: decouple callback action for fileio bio
erofs: add erofs_fileio_direct_io helper to handle direct io
erofs: file-backed mount supports direct io

 fs/erofs/data.c   |  10 ++-
 fs/erofs/fileio.c | 170 ++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 148 insertions(+), 32 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/4 erofs: decouple the iterator on folio

When reading data in file-backed mount case, we need to iterate the each mapping item to read the real data into memory. Currently, the iterator is based on the folio structure. To make the code more compatibable, we move the folio related logic out of iteration so that it only depends on the iov_iter structure. This allows the reading process (such as direct io) to reuse this without interacting with the folio structure.

We conducted the base performance test with fio (iosize is 4k), and the modifications did not affect performance.

[Before]
- first round

    seq read: IOPS=96.6k
    rand read: IOPS=4101

- multi-round

    seq read: IOPS=188k
    rand read: IOPS=35.2k

[After]
- first round

    seq read: IOPS=96.3k
    rand read: IOPS=4245

- multi-round

    seq read: IOPS=184k
    rand read: IOPS=34.3k
---
 fs/erofs/fileio.c | 72 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 51 insertions(+), 21 deletions(-)
fs/erofs/fileio.c +51 -21
index 0ffd1c63beeb..616dc93c0dc5 100644
@@ -3,6 +3,7 @@
  * Copyright (C) 2024, Alibaba Cloud
  */
 #include "internal.h"
+#include <linux/folio_queue.h>
 #include <trace/events/erofs.h>
 
 struct erofs_fileio_rq {
@@ -12,10 +13,15 @@ struct erofs_fileio_rq {
 	struct super_block *sb;
 };
 
+typedef void (fileio_rq_split_t)(void *data);
+
 struct erofs_fileio {
 	struct erofs_map_blocks map;
 	struct erofs_map_dev dev;
 	struct erofs_fileio_rq *rq;
+	struct inode *inode;
+	fileio_rq_split_t *split;
+	void *private;
 };
 
 static void erofs_fileio_ki_complete(struct kiocb *iocb, long ret)
@@ -43,6 +49,11 @@ static void erofs_fileio_ki_complete(struct kiocb *iocb, long ret)
 	kfree(rq);
 }
 
+static void erofs_folio_split(void *data)
+{
+	erofs_onlinefolio_split((struct folio *)data);
+}
+
 static void erofs_fileio_rq_submit(struct erofs_fileio_rq *rq)
 {
 	struct iov_iter iter;
@@ -85,17 +96,15 @@ void erofs_fileio_submit_bio(struct bio *bio)
 						   bio));
 }
 
-static int erofs_fileio_scan_folio(struct erofs_fileio *io, struct folio *folio)
+static int erofs_fileio_scan(struct erofs_fileio *io,
+			     loff_t pos, struct iov_iter *iter)
 {
-	struct inode *inode = folio_inode(folio);
+	struct inode *inode = io->inode;
 	struct erofs_map_blocks *map = &io->map;
-	unsigned int cur = 0, end = folio_size(folio), len, attached = 0;
-	loff_t pos = folio_pos(folio), ofs;
-	struct iov_iter iter;
-	struct bio_vec bv;
+	unsigned int cur = 0, end = iov_iter_count(iter), len, attached = 0;
+	loff_t ofs;
 	int err = 0;
 
-	erofs_onlinefolio_init(folio);
 	while (cur < end) {
 		if (!in_range(pos + cur, map->m_la, map->m_llen)) {
 			map->m_la = pos + cur;
@@ -105,7 +114,7 @@ static int erofs_fileio_scan_folio(struct erofs_fileio *io, struct folio *folio)
 				break;
 		}
 
-		ofs = folio_pos(folio) + cur - map->m_la;
+		ofs = pos + cur - map->m_la;
 		len = min_t(loff_t, map->m_llen - ofs, end - cur);
 		if (map->m_flags & EROFS_MAP_META) {
 			struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
@@ -117,21 +126,17 @@ static int erofs_fileio_scan_folio(struct erofs_fileio *io, struct folio *folio)
 				err = PTR_ERR(src);
 				break;
 			}
-			bvec_set_folio(&bv, folio, len, cur);
-			iov_iter_bvec(&iter, ITER_DEST, &bv, 1, len);
-			if (copy_to_iter(src, len, &iter) != len) {
+			if (copy_to_iter(src, len, iter) != len) {
 				erofs_put_metabuf(&buf);
 				err = -EIO;
 				break;
 			}
 			erofs_put_metabuf(&buf);
 		} else if (!(map->m_flags & EROFS_MAP_MAPPED)) {
-			folio_zero_segment(folio, cur, cur + len);
-			attached = 0;
+			iov_iter_zero(len, iter);
 		} else {
 			if (io->rq && (map->m_pa + ofs != io->dev.m_pa ||
 				       map->m_deviceid != io->dev.m_deviceid)) {
-io_retry:
 				erofs_fileio_rq_submit(io->rq);
 				io->rq = NULL;
 			}
@@ -148,26 +153,39 @@ static int erofs_fileio_scan_folio(struct erofs_fileio *io, struct folio *folio)
 				io->rq->bio.bi_iter.bi_sector = io->dev.m_pa >> 9;
 				attached = 0;
 			}
-			if (!attached++)
-				erofs_onlinefolio_split(folio);
-			if (!bio_add_folio(&io->rq->bio, folio, len, cur))
-				goto io_retry;
+			if (bio_iov_iter_get_pages(&io->rq->bio, iter)) {
+				err = -EIO;
+				break;
+			}
+			if (io->split && !attached++)
+				io->split(io->private);
 			io->dev.m_pa += len;
 		}
 		cur += len;
 	}
-	erofs_onlinefolio_end(folio, err);
 	return err;
 }
 
 static int erofs_fileio_read_folio(struct file *file, struct folio *folio)
 {
 	struct erofs_fileio io = {};
+	struct folio_queue folioq;
+	struct iov_iter iter;
 	int err;
 
+	folioq_init(&folioq, 0);
+	folioq_append(&folioq, folio);
+	iov_iter_folio_queue(&iter, ITER_DEST, &folioq, 0, 0, folio_size(folio));
+	io.inode = folio_inode(folio);
+	io.split = erofs_folio_split;
+	io.private = folio;
+
 	trace_erofs_read_folio(folio, true);
-	err = erofs_fileio_scan_folio(&io, folio);
+	erofs_onlinefolio_init(folio);
+	err = erofs_fileio_scan(&io, folio_pos(folio), &iter);
+	erofs_onlinefolio_end(folio, err);
 	erofs_fileio_rq_submit(io.rq);
+
 	return err;
 }
 
@@ -175,13 +193,25 @@ static void erofs_fileio_readahead(struct readahead_control *rac)
 {
 	struct inode *inode = rac->mapping->host;
 	struct erofs_fileio io = {};
+	struct folio_queue folioq;
+	struct iov_iter iter;
 	struct folio *folio;
 	int err;
 
+	io.inode = inode;
+	io.split = erofs_folio_split;
 	trace_erofs_readpages(inode, readahead_index(rac),
 			      readahead_count(rac), true);
 	while ((folio = readahead_folio(rac))) {
-		err = erofs_fileio_scan_folio(&io, folio);
+		folioq_init(&folioq, 0);
+		folioq_append(&folioq, folio);
+		iov_iter_folio_queue(&iter, ITER_DEST, &folioq, 0, 0, folio_size(folio));
+
+		io.private = folio;
+		erofs_onlinefolio_init(folio);
+		err = erofs_fileio_scan(&io, folio_pos(folio), &iter);
+		erofs_onlinefolio_end(folio, err);
+
 		if (err && err != -EINTR)
 			erofs_err(inode->i_sb, "readahead error at folio %lu @ nid %llu",
 				  folio->index, EROFS_I(inode)->nid);
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/4 erofs: decouple callback action for fileio bio

In reply to Hongbo Li

Introduce erofs_fileio_end_folio as the .bi_end_io callback for fileio bio.

---
 fs/erofs/fileio.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
fs/erofs/fileio.c +19 -8
index 616dc93c0dc5..cdd432ec266c 100644
@@ -11,6 +11,7 @@ struct erofs_fileio_rq {
 	struct bio bio;
 	struct kiocb iocb;
 	struct super_block *sb;
+	ssize_t ret;
 };
 
 typedef void (fileio_rq_split_t)(void *data);
@@ -22,14 +23,15 @@ struct erofs_fileio {
 	struct inode *inode;
 	fileio_rq_split_t *split;
 	void *private;
+	bio_end_io_t *end;
 };
 
 static void erofs_fileio_ki_complete(struct kiocb *iocb, long ret)
 {
 	struct erofs_fileio_rq *rq =
 			container_of(iocb, struct erofs_fileio_rq, iocb);
-	struct folio_iter fi;
 
+	rq->ret = ret;
 	if (ret > 0) {
 		if (ret != rq->bio.bi_iter.bi_size) {
 			bio_advance(&rq->bio, ret);
@@ -37,14 +39,8 @@ static void erofs_fileio_ki_complete(struct kiocb *iocb, long ret)
 		}
 		ret = 0;
 	}
-	if (rq->bio.bi_end_io) {
+	if (rq->bio.bi_end_io)
 		rq->bio.bi_end_io(&rq->bio);
-	} else {
-		bio_for_each_folio_all(fi, &rq->bio) {
-			DBG_BUGON(folio_test_uptodate(fi.folio));
-			erofs_onlinefolio_end(fi.folio, ret);
-		}
-	}
 	bio_uninit(&rq->bio);
 	kfree(rq);
 }
@@ -54,6 +50,18 @@ static void erofs_folio_split(void *data)
 	erofs_onlinefolio_split((struct folio *)data);
 }
 
+static void erofs_fileio_end_folio(struct bio *bio)
+{
+	struct erofs_fileio_rq *rq =
+			container_of(bio, struct erofs_fileio_rq, bio);
+	struct folio_iter fi;
+
+	bio_for_each_folio_all(fi, &rq->bio) {
+		DBG_BUGON(folio_test_uptodate(fi.folio));
+		erofs_onlinefolio_end(fi.folio, rq->ret >= 0 ? 0 : rq->ret);
+	}
+}
+
 static void erofs_fileio_rq_submit(struct erofs_fileio_rq *rq)
 {
 	struct iov_iter iter;
@@ -151,6 +159,7 @@ static int erofs_fileio_scan(struct erofs_fileio *io,
 					break;
 				io->rq = erofs_fileio_rq_alloc(&io->dev);
 				io->rq->bio.bi_iter.bi_sector = io->dev.m_pa >> 9;
+				io->rq->bio.bi_end_io = io->end;
 				attached = 0;
 			}
 			if (bio_iov_iter_get_pages(&io->rq->bio, iter)) {
@@ -177,6 +186,7 @@ static int erofs_fileio_read_folio(struct file *file, struct folio *folio)
 	folioq_append(&folioq, folio);
 	iov_iter_folio_queue(&iter, ITER_DEST, &folioq, 0, 0, folio_size(folio));
 	io.inode = folio_inode(folio);
+	io.end = erofs_fileio_end_folio;
 	io.split = erofs_folio_split;
 	io.private = folio;
 
@@ -199,6 +209,7 @@ static void erofs_fileio_readahead(struct readahead_control *rac)
 	int err;
 
 	io.inode = inode;
+	io.end = erofs_fileio_end_folio;
 	io.split = erofs_folio_split;
 	trace_erofs_readpages(inode, readahead_index(rac),
 			      readahead_count(rac), true);
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 3/4 erofs: add erofs_fileio_direct_io helper to handle direct io

In reply to Hongbo Li

erofs has add file-backed mount support. In this scenario, only buffer io is allowed. So we enhance the io mode by implementing the direct io. Also, this can make the iov_iter (user buffer) interact with the backed file's page cache directly.

To be mentioned, the direct io is atomic, if the part of the iov_iter of direct io failed, the whole direct io also fails.

---
 fs/erofs/fileio.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)
fs/erofs/fileio.c +71 -0
index cdd432ec266c..b652e3df050c 100644
@@ -12,6 +12,7 @@ struct erofs_fileio_rq {
 	struct kiocb iocb;
 	struct super_block *sb;
 	ssize_t ret;
+	void *private;
 };
 
 typedef void (fileio_rq_split_t)(void *data);
@@ -24,6 +25,11 @@ struct erofs_fileio {
 	fileio_rq_split_t *split;
 	void *private;
 	bio_end_io_t *end;
+	/* the following members control the sync call */
+	struct completion ctr;
+	refcount_t ref;
+	size_t total;
+	size_t done;
 };
 
 static void erofs_fileio_ki_complete(struct kiocb *iocb, long ret)
@@ -50,6 +56,13 @@ static void erofs_folio_split(void *data)
 	erofs_onlinefolio_split((struct folio *)data);
 }
 
+static void erofs_iter_split(void *data)
+{
+	struct erofs_fileio *io = (struct erofs_fileio *)data;
+
+	refcount_inc(&io->ref);
+}
+
 static void erofs_fileio_end_folio(struct bio *bio)
 {
 	struct erofs_fileio_rq *rq =
@@ -62,6 +75,25 @@ static void erofs_fileio_end_folio(struct bio *bio)
 	}
 }
 
+static void erofs_fileio_iter_complete(struct erofs_fileio *io)
+{
+	if (!refcount_dec_and_test(&io->ref))
+		return;
+	complete(&io->ctr);
+}
+
+static void erofs_fileio_end_iter(struct bio *bio)
+{
+	struct erofs_fileio_rq *rq =
+			container_of(bio, struct erofs_fileio_rq, bio);
+	struct erofs_fileio *io = (struct erofs_fileio *)rq->private;
+
+	if (rq->ret > 0)
+		io->done += rq->ret;
+
+	erofs_fileio_iter_complete(io);
+}
+
 static void erofs_fileio_rq_submit(struct erofs_fileio_rq *rq)
 {
 	struct iov_iter iter;
@@ -158,6 +190,7 @@ static int erofs_fileio_scan(struct erofs_fileio *io,
 				if (err)
 					break;
 				io->rq = erofs_fileio_rq_alloc(&io->dev);
+				io->rq->private = io;
 				io->rq->bio.bi_iter.bi_sector = io->dev.m_pa >> 9;
 				io->rq->bio.bi_end_io = io->end;
 				attached = 0;
@@ -230,7 +263,45 @@ static void erofs_fileio_readahead(struct readahead_control *rac)
 	erofs_fileio_rq_submit(io.rq);
 }
 
+static ssize_t erofs_fileio_direct_io(struct kiocb *iocb, struct iov_iter *iter)
+{
+	struct file *file = iocb->ki_filp;
+	struct inode *inode = file_inode(file);
+	size_t i_size = i_size_read(inode);
+	struct erofs_fileio io = {};
+	int err;
+
+	if (unlikely(iocb->ki_pos >= i_size))
+		return 0;
+
+	iter->count = min_t(size_t, iter->count,
+			    max_t(size_t, 0, i_size - iocb->ki_pos));
+	io.total = iter->count;
+	if (!io.total)
+		return 0;
+
+	io.inode = inode;
+	io.done = 0;
+	io.split = erofs_iter_split;
+	io.private = &io;
+	io.end = erofs_fileio_end_iter;
+	init_completion(&io.ctr);
+	refcount_set(&io.ref, 1);
+	err = erofs_fileio_scan(&io, iocb->ki_pos, iter);
+	erofs_fileio_rq_submit(io.rq);
+
+	erofs_fileio_iter_complete(&io);
+	wait_for_completion(&io.ctr);
+	if (io.total != io.done) {
+		iov_iter_revert(iter, io.done);
+		return err ?: -EIO;
+	}
+
+	return io.done;
+}
+
 const struct address_space_operations erofs_fileio_aops = {
 	.read_folio = erofs_fileio_read_folio,
 	.readahead = erofs_fileio_readahead,
+	.direct_IO = erofs_fileio_direct_io,
 };
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v2 4/4 erofs: file-backed mount supports direct io

In reply to Hongbo Li

After .direct_IO is hooked, so it is easy to handle direct io in fileio mount case.

We conduct the basic test on direct io and normal io, the fio is used in the test, the results show it can decrease the memory overhead. It slower than normal io in seq read due to erofs page cache and readahead, uut in rand read direct io is similar than buffer io. The results are reasonable.

```
- buffer io
           total      used        free      shared  buff/cache   available
Mem:        54Gi     2.4Gi        52Gi        11Mi       254Mi        51Gi
Swap:      4.0Gi        0B       4.0Gi

after read

             total    used        free      shared  buff/cache   available
Mem:        54Gi     2.5Gi        50Gi        11Mi       2.3Gi        51Gi
Swap:      4.0Gi        0B       4.0Gi

cost 2GB memory (the test file is 1GB)

- direct io

           total      used        free      shared  buff/cache   available
Mem:        54Gi     2.4Gi        52Gi        11Mi       280Mi        51Gi
Swap:      4.0Gi        0B       4.0Gi

after read

           total      used        free      shared  buff/cache   available
Mem:        54Gi     2.6Gi        51Gi        11Mi       1.2Gi        51Gi
Swap:      4.0Gi        0B       4.0Gi

only cost 1GB memory (the test file is 1GB)

buffer io: 96.6k (seq read), 4245 (rand read)
direct io: 21.6k (seq read), 4187 (rand read)
```
---
 fs/erofs/data.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
fs/erofs/data.c +7 -3
index 0cd6b5c4df98..d58496225381 100644
@@ -395,9 +395,13 @@ static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 	if (IS_DAX(inode))
 		return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
 #endif
-	if ((iocb->ki_flags & IOCB_DIRECT) && inode->i_sb->s_bdev)
-		return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
-				    NULL, 0, NULL, 0);
+	if (iocb->ki_flags & IOCB_DIRECT) {
+		if (inode->i_sb->s_bdev)
+			return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
+					    NULL, 0, NULL, 0);
+		if (erofs_is_fileio_mode(EROFS_SB(inode->i_sb)))
+			return generic_file_read_iter(iocb, to);
+	}
 	return filemap_read(iocb, to, 0);
 }
 
Signature · 1 line
2.34.1