linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anton Altaparmakov <aia21@cam.ac.uk>
To: Matthew Wilcox <matthew@wil.cx>
Cc: Andrew Morton <akpm@osdl.org>, Nathan Scott <nathans@sgi.com>,
	Al Viro <viro@parcelfarce.linux.theplanet.co.uk>,
	lkml <linux-kernel@vger.kernel.org>,
	fsdevel <linux-fsdevel@vger.kernel.org>
Subject: Re: RFC: [PATCH-2.6] Add helper function to lock multiple page cache pages.
Date: Wed, 02 Feb 2005 15:56:38 +0000	[thread overview]
Message-ID: <1107359798.18444.42.camel@imp.csi.cam.ac.uk> (raw)
In-Reply-To: <20050202154307.GG10088@parcelfarce.linux.theplanet.co.uk>

Hi Matthew,

On Wed, 2005-02-02 at 15:43 +0000, Matthew Wilcox wrote:
> On Wed, Feb 02, 2005 at 03:12:50PM +0000, Anton Altaparmakov wrote:
> 
> I think the below loop would be clearer as a for loop ...
> 
> 	err = 0;
> 	for (nr = 0; nr < nr_pages; nr++, start++) {
> 		if (start == lp_idx) {
> 			pages[nr] = locked_page;
> 			if (!nr)
> 				continue;
> 			lock_page(locked_page);
> 			if (!wbc)
> 				continue;
> 			if (wbc->for_reclaim) {
> 				up(&inode->i_sem);
> 				up_read(&inode->i_sb->s_umount);
> 			}
> 			/* Was the page truncated under us? */
> 			if (page_mapping(locked_page) != mapping) {
> 				err = -ESTALE;
> 				goto err_out_locked;
> 			}
> 		} else {
> 			pages[nr] = find_lock_page(mapping, start);
> 			if (pages[nr])
> 				continue;
> 			if (!cached_page) {
> 				cached_page = alloc_page(gfp_mask);
> 				if (unlikely(!cached_page))
> 					goto err_out;
> 			}
> 			err = add_to_page_cache_lru(cached_page,
> 					mapping, start, gfp_mask);
> 			if (unlikely(err)) {
> 				if (err == -EEXIST)
> 					continue;
> 				goto err_out;
> 			}
> 			pages[nr] = cached_page;
> 			cached_page = NULL;
> 		}
> 	}
> 
> The above fixes two bugs in the below:
>  - if (!unlikely(cached_page)) should be if (unlikely(!cached_page))

Ah, oops.  Thanks!  Well spotted!  I did say it was only compile
tested...  (-;

>  - The -EEXIST case after add_to_page_cache_lru() would result in
>    an infinite loop in the original as nr wasn't being incremented.

That was exactly what was meant to happen.  It is not a bug.  It is a
feature.  This is why it is a while loop instead of a for loop.  I need
to have @nr and @start incremented only if the code reaches the end of
the loop.

The -EEXIST case needs to repeat for the same @nr and @start.  It
basically means that someone else allocated the page with index @start
and added it to the page cache in between us running find_lock_page()
and add_to_page_cache_lru().  So what we want to do is to run
find_lock_page() again which should then find and lock the page that the
other process created.

Of course what could happen is that between us getting the -EEXIST and
us repeating the find_lock_page() the page is freed again so the
find_lock_page() fails again.  Perhaps this time we will succeed with
add_to_page_cache_lru() and if not we repeat again.  Eventually either
find_lock_page() or add_to_page_cache_lru() will succeed so in practise
it will never be an endless loop.

If the while loop is changed to a for loop, the "continue;" on -EEXIST
would need to be changed to "goto repeat;" and a label "repeat:" would
need to be placed at the beginning of the loop.  I considered this but
decided the while loop looks nicer.  (-:

Thanks for the review!

> > +	err = nr = 0;
> > +	while (nr < nr_pages) {
> > +		if (start == lp_idx) {
> > +			pages[nr] = locked_page;
> > +			if (nr) {
> > +				lock_page(locked_page);
> > +				if (wbc) {
> > +					if (wbc->for_reclaim) {
> > +						up(&inode->i_sem);
> > +						up_read(&inode->i_sb->s_umount);
> > +					}
> > +					/* Was the page truncated under us? */
> > +					if (page_mapping(locked_page) !=
> > +							mapping) {
> > +						err = -ESTALE;
> > +						goto err_out_locked;
> > +					}
> > +				}
> > +			}
> > +		} else {
> > +			pages[nr] = find_lock_page(mapping, start);
> > +			if (!pages[nr]) {
> > +				if (!cached_page) {
> > +					cached_page = alloc_page(gfp_mask);
> > +					if (!unlikely(cached_page))
> > +						goto err_out;
> > +				}
> > +				err = add_to_page_cache_lru(cached_page,
> > +						mapping, start, gfp_mask);
> > +				if (unlikely(err)) {
> > +					if (err == -EEXIST)
> > +						continue;
> > +					goto err_out;
> > +				}
> > +				pages[nr] = cached_page;
> > +				cached_page = NULL;
> > +			}
> > +		}
> > +		nr++;
> > +		start++;
> > +	}

Best regards,

        Anton
-- 
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK
Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net
WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/


  reply	other threads:[~2005-02-02 15:56 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-02 15:12 RFC: [PATCH-2.6] Add helper function to lock multiple page cache pages Anton Altaparmakov
2005-02-02 15:43 ` Matthew Wilcox
2005-02-02 15:56   ` Anton Altaparmakov [this message]
2005-02-02 22:34 ` Andrew Morton
2005-02-03 10:37   ` Anton Altaparmakov
2005-02-03 10:47     ` Andrew Morton
2005-02-03 11:23       ` Anton Altaparmakov
2005-02-03 19:23         ` RFC: [PATCH-2.6] Add helper function to lock multiple page cache pages - nopage alternative Bryan Henderson
2005-02-04 15:36           ` Anton Altaparmakov
2005-02-04 17:17             ` Hugh Dickins
2005-02-04 23:09             ` Bryan Henderson
2005-02-03 19:03       ` RFC: [PATCH-2.6] Add helper function to lock multiple page cache pages - loop device Bryan Henderson
2005-02-06 19:42       ` RFC: [PATCH-2.6] Add helper function to lock multiple page cache pages Anton Altaparmakov
2005-02-06 20:42         ` Andrew Morton
2005-02-16 21:56           ` Anton Altaparmakov

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=1107359798.18444.42.camel@imp.csi.cam.ac.uk \
    --to=aia21@cam.ac.uk \
    --cc=akpm@osdl.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthew@wil.cx \
    --cc=nathans@sgi.com \
    --cc=viro@parcelfarce.linux.theplanet.co.uk \
    /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).