[rh7,20/30] new helper: iov_iter_npages()

Submitted by Kirill Tkhai on May 20, 2020, 4:05 p.m.

Details

Message ID 158999071631.2234365.15492998361361219412.stgit@localhost.localdomain
State New
Series "fs, direct_IO: Switch to iov_iter and allow bio_vec for ext4"
Headers show

Commit Message

Kirill Tkhai May 20, 2020, 4:05 p.m.
From: Al Viro <viro@zeniv.linux.org.uk>

ms commit f67da30c1d5f

counts the pages covered by iov_iter, up to given limit.
do_block_direct_io() and fuse_iter_npages() switched to
it.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 fs/direct-io.c     |   12 +-----------
 include/linux/fs.h |    1 +
 mm/iov-iter.c      |   27 +++++++++++++++++++++++++++
 3 files changed, 29 insertions(+), 11 deletions(-)

Patch hide | download patch | download mbox

diff --git a/fs/direct-io.c b/fs/direct-io.c
index 55c40be5a014..f7e464d8bcdb 100644
--- a/fs/direct-io.c
+++ b/fs/direct-io.c
@@ -1185,7 +1185,6 @@  do_blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
 	get_block_t get_block, dio_iodone_t end_io,
 	dio_submit_t submit_io,	int flags)
 {
-	int seg;
 	unsigned i_blkbits = ACCESS_ONCE(inode->i_blkbits);
 	unsigned blkbits = i_blkbits;
 	unsigned blocksize_mask = (1 << blkbits) - 1;
@@ -1194,14 +1193,10 @@  do_blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
 	loff_t end = offset + count;
 	struct dio *dio;
 	struct dio_submit sdio = { 0, };
-	unsigned long user_addr;
 	struct buffer_head map_bh = { 0, };
 	struct blk_plug plug;
 	unsigned long align = offset | iov_iter_alignment(iter);
 
-	const struct iovec *iov = iov_iter_iovec(iter);
-	unsigned long nr_segs = iter->nr_segs;
-
 	if (rw & WRITE)
 		rw = WRITE_ODIRECT;
 
@@ -1340,12 +1335,7 @@  do_blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
 	if (unlikely(sdio.blkfactor))
 		sdio.pages_in_io = 2;
 
-	for (seg = 0; seg < nr_segs; seg++) {
-		user_addr = (unsigned long)iov[seg].iov_base;
-		sdio.pages_in_io +=
-			((user_addr + iov[seg].iov_len + PAGE_SIZE-1) /
-				PAGE_SIZE - user_addr / PAGE_SIZE);
-	}
+	sdio.pages_in_io += iov_iter_npages(iter, INT_MAX);
 
 	blk_start_plug(&plug);
 
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 70d57756b83f..ece93a278ab7 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3654,6 +3654,7 @@  static inline iop_dentry_open_t get_dentry_open_iop(struct inode *inode)
 extern unsigned long iov_iter_alignment(struct iov_iter *i);
 ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
 			size_t maxsize, size_t *start, int rw);
+int iov_iter_npages(struct iov_iter *i, int maxpages);
 extern bool path_noexec(const struct path *path);
 
 #endif /* _LINUX_FS_H */
diff --git a/mm/iov-iter.c b/mm/iov-iter.c
index bf627cf009a6..3a3a51cf3b59 100644
--- a/mm/iov-iter.c
+++ b/mm/iov-iter.c
@@ -938,3 +938,30 @@  ssize_t iov_iter_get_pages(struct iov_iter *i,
 	return (res == n ? len : res * PAGE_SIZE) - *start;
 }
 EXPORT_SYMBOL(iov_iter_get_pages);
+
+int iov_iter_npages(const struct iov_iter *i, int maxpages)
+{
+	size_t offset = i->iov_offset;
+	size_t size = i->count;
+	const struct iovec *iov = iov_iter_iovec(i);
+	int npages = 0;
+	int n;
+
+	for (n = 0; size && n < i->nr_segs; n++, iov++) {
+		unsigned long addr = (unsigned long)iov->iov_base + offset;
+		size_t len = iov->iov_len - offset;
+		offset = 0;
+		if (unlikely(!len))	/* empty segment */
+			continue;
+		if (len > size)
+			len = size;
+		npages += (addr + len + PAGE_SIZE - 1) / PAGE_SIZE
+			  - addr / PAGE_SIZE;
+		if (npages >= maxpages)	/* don't bother going further */
+			return maxpages;
+		size -= len;
+		offset = 0;
+	}
+	return min(npages, maxpages);
+}
+EXPORT_SYMBOL(iov_iter_npages);