mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Maxim Patlasov <mpatlasov@parallels.com>
To: Miklos Szeredi <miklos@szeredi.hu>
Cc: <fuse-devel@lists.sourceforge.net>,
	<linux-kernel@vger.kernel.org>, <devel@openvz.org>,
	<xemul@parallels.com>
Subject: Re: [PATCH] fuse: fix race in fuse_writepages()
Date: Thu, 29 Aug 2013 16:38:22 +0400	[thread overview]
Message-ID: <521F40BE.901@parallels.com> (raw)
In-Reply-To: <20130829114650.GA19636@tucsk.piliscsaba.szeredi.hu>

Hi,

08/29/2013 03:46 PM, Miklos Szeredi пишет:
> On Fri, Aug 16, 2013 at 03:51:41PM +0400, Maxim Patlasov wrote:
>> The patch is for
>>
>>   git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse.git writepages.v2
>>
>> The patch fixes a race between ftruncate(2), mmap-ed write and write(2):
>>
>> 1) An user makes a page dirty via mmap-ed write.
>> 2) The user performs shrinking truncate(2) intended to purge the page.
>> 3) Before fuse_do_setattr calls truncate_pagecache, the page goes to
>>     writeback. fuse_writepages_fill attaches a new page to FUSE_WRITE request,
>>     then releases the original page by end_page_writeback and unlock it.
>> 4) fuse_do_setattr completes and successfully returns. Since now, i_mutex
>>     is free.
>> 5) Ordinary write(2) extends i_size back to cover the page. Note that
>>     fuse_send_write_pages do wait for fuse writeback, but for another
>>     page->index.
>> 6) fuse_writepages_fill attaches more pages to the request (if any), then
>>     fuse_writepages_send is eventually called. It is supposed to crop
>>     inarg->size of the request, but it doesn't because i_size has already been
>>     extended back.
>>
>> Moving end_page_writeback behind fuse_writepages_send guarantees that
>> __fuse_release_nowrite (called from fuse_do_setattr) will crop inarg->size
>> of the request before write(2) gets the chance to extend i_size.
> Thanks for the report.  Your analysis looks correct.
>
> Just one nit, why orig_pages? req->pages is already there, so why duplicate it?

req->pages is there, but it is already occupied by new pages (allocated 
by fuse_writepages_fill). We can't re-use req->pages for original pages 
because as soon as we put the request to bg_queue (in 
fuse_writepages_send) and released fc->lock, req->pages may be accessed 
w/o any delay. So we have two bunches of pointers to "struct page" to be 
stashed somewhere : original and new one. req->pages is for new pages, 
orig_pages[] is for original ones.

> Note: you can do __fuse_get_request()/fuse_put_request() to prevent the req from
> going away after it's been sent.

Yes, I experimented with this technique before adding orig_pages[]. I 
was very reluctant about duplicating that page array and was looking for 
any opportunity to avoid it. Pinning original pages to new ones using 
page->private looked promising, but unfortunately it didn't work because 
__fuse_get_request() protects only request itself from disappearing, not 
from releasing pages that req->pages[] points to. And obviously, as soon 
as a page released, it's not correct to rely on the content of its 
'private' field.

Thanks,
Maxim

  reply	other threads:[~2013-08-29 12:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-16 11:51 Maxim Patlasov
2013-08-29 11:46 ` Miklos Szeredi
2013-08-29 12:38   ` Maxim Patlasov [this message]
2013-08-29 16:21     ` Miklos Szeredi

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=521F40BE.901@parallels.com \
    --to=mpatlasov@parallels.com \
    --cc=devel@openvz.org \
    --cc=fuse-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    --cc=xemul@parallels.com \
    /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

Powered by JetHome