From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: Dave Chinner <david@fromorbit.com>
Cc: viro@zeniv.linux.org.uk, xfs <linux-xfs@vger.kernel.org>,
linux-fsdevel <linux-fsdevel@vger.kernel.org>
Subject: Re: [PATCH] vfs: fix page locking deadlocks when deduping files
Date: Wed, 7 Aug 2019 19:10:46 -0700 [thread overview]
Message-ID: <20190808021046.GD7157@magnolia> (raw)
In-Reply-To: <20190807223850.GQ7777@dread.disaster.area>
On Thu, Aug 08, 2019 at 08:38:50AM +1000, Dave Chinner wrote:
> On Wed, Aug 07, 2019 at 07:51:14AM -0700, Darrick J. Wong wrote:
> > From: Darrick J. Wong <darrick.wong@oracle.com>
> > +/*
> > + * Lock two pages, ensuring that we lock in offset order if the pages are from
> > + * the same file.
> > + */
> > +static void vfs_lock_two_pages(struct page *page1, struct page *page2)
> > +{
> > + if (page1 == page2) {
> > + lock_page(page1);
> > + return;
> > + }
> > +
> > + if (page1->mapping == page2->mapping && page1->index > page2->index)
> > + swap(page1, page2);
>
> I would do this even if the pages are on different mappings. That
> way we don't expose a landmine if some other code locks two pages
> from the same mappings in a different order...
Sure.
> > + lock_page(page1);
> > + lock_page(page2);
> > +}
> > +
> > /*
> > * Compare extents of two files to see if they are the same.
> > * Caller must have locked both inodes to prevent write races.
> > @@ -1867,10 +1881,12 @@ static int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
> > dest_page = vfs_dedupe_get_page(dest, destoff);
> > if (IS_ERR(dest_page)) {
> > error = PTR_ERR(dest_page);
> > - unlock_page(src_page);
> > put_page(src_page);
> > goto out_error;
> > }
> > +
> > + vfs_lock_two_pages(src_page, dest_page);
> > +
> > src_addr = kmap_atomic(src_page);
> > dest_addr = kmap_atomic(dest_page);
> >
> > @@ -1882,7 +1898,8 @@ static int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
> >
> > kunmap_atomic(dest_addr);
> > kunmap_atomic(src_addr);
> > - unlock_page(dest_page);
> > + if (dest_page != src_page)
> > + unlock_page(dest_page);
> > unlock_page(src_page);
>
> Would it make sense for symmetry to wrap these in
> vfs_unlock_two_pages()?
Sure.
--D
> -Dave.
> --
> Dave Chinner
> david@fromorbit.com
prev parent reply other threads:[~2019-08-08 2:10 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-07 14:51 [PATCH] vfs: fix page locking deadlocks when deduping files Darrick J. Wong
2019-08-07 22:38 ` Dave Chinner
2019-08-08 2:10 ` Darrick J. Wong [this message]
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=20190808021046.GD7157@magnolia \
--to=darrick.wong@oracle.com \
--cc=david@fromorbit.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=viro@zeniv.linux.org.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).