diff mbox series

[07/19] mm/filemap: Use head pages in generic_file_buffered_read

Message ID 20201029193405.29125-8-willy@infradead.org (mailing list archive)
State New, archived
Headers show
Series Transparent Hugepages for non-tmpfs filesystems | expand

Commit Message

Matthew Wilcox Oct. 29, 2020, 7:33 p.m. UTC
Add mapping_get_read_heads() which returns the head pages which
represent a contiguous array of bytes in the file.  It also stops
when encountering a page marked as Readahead or !Uptodate (but
does return that page) so it can be handled appropriately by
gfbr_get_pages().

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 mm/filemap.c | 78 ++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 61 insertions(+), 17 deletions(-)

Comments

Kent Overstreet Oct. 30, 2020, 12:19 a.m. UTC | #1
On Thu, Oct 29, 2020 at 07:33:53PM +0000, Matthew Wilcox (Oracle) wrote:
> Add mapping_get_read_heads() which returns the head pages which
> represent a contiguous array of bytes in the file.  It also stops
> when encountering a page marked as Readahead or !Uptodate (but
> does return that page) so it can be handled appropriately by
> gfbr_get_pages().

I don't like the _heads naming - this is like find_get_pages_contig() except it
returns compound pages instead of single pages, and the naming should reflect
that. And, working with compound pages should be the default in the future -
code working with individual pages should be considered a code smell in the
future, i.e. find_get_pages() et. all. should be considered deprecated.

We have the same issue in the block layer with multi page bvecs where the naming
really should be better; the more complicated name should be for the unusual
case, which is not what we have now.

Also - you're implementing new core functionality with a pecularity of the read
path, the two probably be split out. Perhaps find_get_cpages (compound_pages)
that can also be passed a stop condition.

Not all of this is necessarily for this patch, but perhaps a comment indicating
where we want to go in the future would be helpful.

> -			if (writably_mapped)
> -				flush_dcache_page(pages[i]);
> +			if (writably_mapped) {
> +				int j;
> +
> +				for (j = 0; j < thp_nr_pages(page); j++)
> +					flush_dcache_page(page + j);
> +			}

this should be a new helper
Matthew Wilcox Oct. 30, 2020, 1:03 a.m. UTC | #2
On Thu, Oct 29, 2020 at 08:19:09PM -0400, Kent Overstreet wrote:
> On Thu, Oct 29, 2020 at 07:33:53PM +0000, Matthew Wilcox (Oracle) wrote:
> > Add mapping_get_read_heads() which returns the head pages which
> > represent a contiguous array of bytes in the file.  It also stops
> > when encountering a page marked as Readahead or !Uptodate (but
> > does return that page) so it can be handled appropriately by
> > gfbr_get_pages().
> 
> I don't like the _heads naming - this is like find_get_pages_contig() except it
> returns compound pages instead of single pages, and the naming should reflect
> that. And, working with compound pages should be the default in the future -
> code working with individual pages should be considered a code smell in the
> future, i.e. find_get_pages() et. all. should be considered deprecated.

You're right.  It should just be called mapping_get_(something)_thps.

> Also - you're implementing new core functionality with a pecularity of the read
> path, the two probably be split out. Perhaps find_get_cpages (compound_pages)
> that can also be passed a stop condition.

It isn't clear to me _how_ to do that, so in the absence of that obvious,
I decided to implement what we need right now, and we can figure out how
to abstract it later.

So until we have a better idea, I'm going with "read" for the (something)
above.

> > +			if (writably_mapped) {
> > +				int j;
> > +
> > +				for (j = 0; j < thp_nr_pages(page); j++)
> > +					flush_dcache_page(page + j);
> > +			}
> 
> this should be a new helper

Good point.  One flush_dcache_thp() coming right up!
diff mbox series

Patch

diff --git a/mm/filemap.c b/mm/filemap.c
index 1bfd87d85bfd..c0161f42f37d 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2166,6 +2166,48 @@  static void shrink_readahead_size_eio(struct file_ra_state *ra)
 	ra->ra_pages /= 4;
 }
 
+static unsigned mapping_get_read_heads(struct address_space *mapping,
+		pgoff_t index, unsigned int nr_pages, struct page **pages)
+{
+	XA_STATE(xas, &mapping->i_pages, index);
+	struct page *head;
+	unsigned int ret = 0;
+
+	if (unlikely(!nr_pages))
+		return 0;
+
+	rcu_read_lock();
+	for (head = xas_load(&xas); head; head = xas_next(&xas)) {
+		if (xas_retry(&xas, head))
+			continue;
+		if (xa_is_value(head))
+			break;
+		if (!page_cache_get_speculative(head))
+			goto retry;
+
+		/* Has the page moved or been split? */
+		if (unlikely(head != xas_reload(&xas)))
+			goto put_page;
+
+		pages[ret++] = head;
+		if (ret == nr_pages)
+			break;
+		if (!PageUptodate(head))
+			break;
+		if (PageReadahead(head))
+			break;
+		xas.xa_index = head->index + thp_nr_pages(head) - 1;
+		xas.xa_offset = (xas.xa_index >> xas.xa_shift) & XA_CHUNK_MASK;
+		continue;
+put_page:
+		put_page(head);
+retry:
+		xas_reset(&xas);
+	}
+	rcu_read_unlock();
+	return ret;
+}
+
 static int lock_page_for_iocb(struct kiocb *iocb, struct page *page)
 {
 	if (iocb->ki_flags & IOCB_WAITQ)
@@ -2328,14 +2370,14 @@  static int gfbr_get_pages(struct kiocb *iocb, struct iov_iter *iter,
 	struct file_ra_state *ra = &filp->f_ra;
 	pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
 	pgoff_t last_index = (iocb->ki_pos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
-	int i, j, nr_got, err = 0;
+	int i, nr_got, err = 0;
 
 	nr = min_t(unsigned long, last_index - index, nr);
 find_page:
 	if (fatal_signal_pending(current))
 		return -EINTR;
 
-	nr_got = find_get_pages_contig(mapping, index, nr, pages);
+	nr_got = mapping_get_read_heads(mapping, index, nr, pages);
 	if (nr_got)
 		goto got_pages;
 
@@ -2344,7 +2386,7 @@  static int gfbr_get_pages(struct kiocb *iocb, struct iov_iter *iter,
 
 	page_cache_sync_readahead(mapping, ra, filp, index, last_index - index);
 
-	nr_got = find_get_pages_contig(mapping, index, nr, pages);
+	nr_got = mapping_get_read_heads(mapping, index, nr, pages);
 	if (nr_got)
 		goto got_pages;
 
@@ -2355,15 +2397,14 @@  static int gfbr_get_pages(struct kiocb *iocb, struct iov_iter *iter,
 got_pages:
 	for (i = 0; i < nr_got; i++) {
 		struct page *page = pages[i];
-		pgoff_t pg_index = index + i;
+		pgoff_t pg_index = page->index;
 		loff_t pg_pos = max(iocb->ki_pos,
 				    (loff_t) pg_index << PAGE_SHIFT);
 		loff_t pg_count = iocb->ki_pos + iter->count - pg_pos;
 
 		if (PageReadahead(page)) {
 			if (iocb->ki_flags & IOCB_NOIO) {
-				for (j = i; j < nr_got; j++)
-					put_page(pages[j]);
+				put_page(page);
 				nr_got = i;
 				err = -EAGAIN;
 				break;
@@ -2375,8 +2416,7 @@  static int gfbr_get_pages(struct kiocb *iocb, struct iov_iter *iter,
 		if (!PageUptodate(page)) {
 			if ((iocb->ki_flags & IOCB_NOWAIT) ||
 			    ((iocb->ki_flags & IOCB_WAITQ) && i)) {
-				for (j = i; j < nr_got; j++)
-					put_page(pages[j]);
+				put_page(page);
 				nr_got = i;
 				err = -EAGAIN;
 				break;
@@ -2385,8 +2425,6 @@  static int gfbr_get_pages(struct kiocb *iocb, struct iov_iter *iter,
 			page = gfbr_update_page(iocb, mapping, iter, page,
 					pg_pos, pg_count);
 			if (IS_ERR_OR_NULL(page)) {
-				for (j = i + 1; j < nr_got; j++)
-					put_page(pages[j]);
 				nr_got = i;
 				err = PTR_ERR_OR_ZERO(page);
 				break;
@@ -2500,20 +2538,26 @@  ssize_t generic_file_buffered_read(struct kiocb *iocb,
 			mark_page_accessed(pages[i]);
 
 		for (i = 0; i < pg_nr; i++) {
-			unsigned int offset = iocb->ki_pos & ~PAGE_MASK;
-			unsigned int bytes = min_t(loff_t, end_offset - iocb->ki_pos,
-						   PAGE_SIZE - offset);
-			unsigned int copied;
+			struct page *page = pages[i];
+			size_t page_size = thp_size(page);
+			size_t offset = iocb->ki_pos & (page_size - 1);
+			size_t bytes = min_t(loff_t, end_offset - iocb->ki_pos,
+					     page_size - offset);
+			size_t copied;
 
 			/*
 			 * If users can be writing to this page using arbitrary
 			 * virtual addresses, take care about potential aliasing
 			 * before reading the page on the kernel side.
 			 */
-			if (writably_mapped)
-				flush_dcache_page(pages[i]);
+			if (writably_mapped) {
+				int j;
+
+				for (j = 0; j < thp_nr_pages(page); j++)
+					flush_dcache_page(page + j);
+			}
 
-			copied = copy_page_to_iter(pages[i], offset, bytes, iter);
+			copied = copy_page_to_iter(page, offset, bytes, iter);
 
 			written += copied;
 			iocb->ki_pos += copied;