linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Matthew Wilcox <willy@infradead.org>
Cc: Christoph Hellwig <hch@lst.de>,
	Andrew Morton <akpm@linux-foundation.org>,
	Kent Overstreet <kent.overstreet@gmail.com>,
	linux-mm@kvack.org, linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH 05/13] mm: simplify generic_file_buffered_read_no_cached_page
Date: Sun, 1 Nov 2020 11:29:08 +0100	[thread overview]
Message-ID: <20201101102908.GB26447@lst.de> (raw)
In-Reply-To: <20201031162813.GS27442@casper.infradead.org>

On Sat, Oct 31, 2020 at 04:28:13PM +0000, Matthew Wilcox wrote:
> On Sat, Oct 31, 2020 at 09:59:56AM +0100, Christoph Hellwig wrote:
> > +static int filemap_new_page(struct kiocb *iocb, struct iov_iter *iter,
> > +		struct page **page)
> 
> I don't like this calling convention.  It's too easy to get it wrong,
> as you demonstrated.  I preferred the way Kent had it with returning
> an ERR_PTR.

I guess for this function we can do that, here is the untested patch
on top of my series to show what we'd end up with:

diff --git a/mm/filemap.c b/mm/filemap.c
index b45f0bafdbaebf..1ac1fcd0067bf1 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2281,38 +2281,40 @@ static int filemap_make_page_uptodate(struct kiocb *iocb, struct iov_iter *iter,
 	return 0;
 }
 
-static int filemap_new_page(struct kiocb *iocb, struct iov_iter *iter,
-		struct page **page)
+static struct page *filemap_new_page(struct kiocb *iocb, struct iov_iter *iter)
 {
 	struct address_space *mapping = iocb->ki_filp->f_mapping;
 	gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
 	pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
+	struct page *page;
 	int error;
 
 	if (iocb->ki_flags & IOCB_NOIO)
-		return -EAGAIN;
+		return ERR_PTR(-EAGAIN);
 
-	*page = page_cache_alloc(mapping);
+	page = page_cache_alloc(mapping);
 	if (!page)
-		return -ENOMEM;
-	error = add_to_page_cache_lru(*page, mapping, index, gfp);
+		return ERR_PTR(-ENOMEM);
+	error = add_to_page_cache_lru(page, mapping, index, gfp);
 	if (error)
 		goto put_page;
-	error = filemap_readpage(iocb, *page);
+	error = filemap_readpage(iocb, page);
 	if (error)
 		goto put_page;
-	if (PageReadahead(*page)) {
+	if (PageReadahead(page)) {
 		error = -EAGAIN;
 		if (iocb->ki_flags & IOCB_NOIO)
 			goto put_page;
 		page_cache_async_readahead(mapping, &iocb->ki_filp->f_ra,
-				iocb->ki_filp, *page, index,
+				iocb->ki_filp, page, index,
 				(iter->count + PAGE_SIZE - 1) >> PAGE_SHIFT);
 	}
 	return 0;
 put_page:
-	put_page(*page);
-	return error;
+	put_page(page);
+	if (error == -EEXIST || error == AOP_TRUNCATED_PAGE)
+		return NULL;
+	return ERR_PTR(error);
 }
 
 static int filemap_find_get_pages(struct kiocb *iocb, struct iov_iter *iter,
@@ -2347,28 +2349,30 @@ static int filemap_read_pages(struct kiocb *iocb, struct iov_iter *iter,
 		return -EINTR;
 
 	nr_pages = filemap_find_get_pages(iocb, iter, pages, nr);
-	if (nr_pages) {
-		for (i = 0; i < nr_pages; i++) {
-			err = filemap_make_page_uptodate(iocb, iter, pages[i],
-					index + i, i == 0);
-			if (err) {
-				for (j = i; j < nr_pages; j++)
-					put_page(pages[j]);
-				nr_pages = i;
-				break;
-			}
+	if (!nr_pages) {
+		pages[0] = filemap_new_page(iocb, iter);
+		if (pages[0] == NULL)
+			goto retry;
+		if (IS_ERR(pages[0]))
+			return PTR_ERR(pages[0]);
+		return 1;
+	}
+
+	for (i = 0; i < nr_pages; i++) {
+		err = filemap_make_page_uptodate(iocb, iter, pages[i],
+				index + i, i == 0);
+		if (err) {
+			for (j = i; j < nr_pages; j++)
+				put_page(pages[j]);
+			if (likely(i > 0))
+				return i;
+			if (err == AOP_TRUNCATED_PAGE)
+				goto retry;
+			return err;
 		}
-	} else {
-		err = filemap_new_page(iocb, iter, &pages[0]);
-		if (!err)
-			nr_pages = 1;
 	}
 
-	if (likely(nr_pages))
-		return nr_pages;
-	if (err == -EEXIST || err == AOP_TRUNCATED_PAGE)
-		goto retry;
-	return err;
+	return nr_pages;
 }
 
 /**

  reply	other threads:[~2020-11-01 10:29 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-31  8:59 clean up the generic pagecache read helpers Christoph Hellwig
2020-10-31  8:59 ` [PATCH 01/13] mm: simplify generic_file_buffered_read_readpage Christoph Hellwig
2020-10-31  8:59 ` [PATCH 02/13] mm: simplify generic_file_buffered_read_pagenotuptodate Christoph Hellwig
2020-10-31  8:59 ` [PATCH 03/13] mm: lift the nowait checks into generic_file_buffered_read_pagenotuptodate Christoph Hellwig
2020-10-31  8:59 ` [PATCH 04/13] mm: handle readahead in generic_file_buffered_read_pagenotuptodate Christoph Hellwig
2020-10-31 17:06   ` Matthew Wilcox
2020-11-01 10:31     ` Christoph Hellwig
2020-11-01 10:49       ` Matthew Wilcox
2020-11-01 10:51         ` Christoph Hellwig
2020-11-01 10:51           ` Christoph Hellwig
2020-11-01 11:04             ` Matthew Wilcox
2020-11-01 11:52               ` Christoph Hellwig
2020-11-01 14:55                 ` Matthew Wilcox
2020-11-02  8:18                   ` Christoph Hellwig
2020-10-31  8:59 ` [PATCH 05/13] mm: simplify generic_file_buffered_read_no_cached_page Christoph Hellwig
2020-10-31 16:20   ` Matthew Wilcox
2020-10-31 16:28   ` Matthew Wilcox
2020-11-01 10:29     ` Christoph Hellwig [this message]
2020-10-31  8:59 ` [PATCH 06/13] mm: factor out a filemap_find_get_pages helper Christoph Hellwig
2020-10-31  8:59 ` [PATCH 07/13] mm: refactor generic_file_buffered_read_get_pages Christoph Hellwig
2020-11-01 11:18   ` Matthew Wilcox
2020-10-31  8:59 ` [PATCH 08/13] mm: move putting the page on error out of filemap_readpage Christoph Hellwig
2020-10-31  9:00 ` [PATCH 09/13] mm: move putting the page on error out of filemap_make_page_uptodate Christoph Hellwig
2020-10-31  9:00 ` [PATCH 10/13] mm: open code readahead in filemap_new_page Christoph Hellwig
2020-11-01 11:20   ` Matthew Wilcox
2020-10-31  9:00 ` [PATCH 11/13] mm: streamline the partially uptodate checks in filemap_make_page_uptodate Christoph Hellwig
2020-11-01 11:23   ` Matthew Wilcox
2020-10-31  9:00 ` [PATCH 12/13] mm: rename generic_file_buffered_read to filemap_read Christoph Hellwig
2020-10-31  9:00 ` [PATCH 13/13] mm: simplify generic_file_read_iter Christoph Hellwig
2020-10-31 15:42 ` clean up the generic pagecache read helpers Matthew Wilcox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201101102908.GB26447@lst.de \
    --to=hch@lst.de \
    --cc=akpm@linux-foundation.org \
    --cc=kent.overstreet@gmail.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).