public inbox for linux-fsdevel@vger.kernel.org
 help / color / mirror / Atom feed
From: Luis Henriques <luis@igalia.com>
To: Bernd Schubert <bernd@bsbernd.com>
Cc: Miklos Szeredi <miklos@szeredi.hu>,
	 linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Teng Qin <tqin@jumptrading.com>
Subject: Re: [RFC PATCH] fuse: fix race in fuse_notify_store()
Date: Wed, 29 Jan 2025 21:26:14 +0000	[thread overview]
Message-ID: <87ldut5n2h.fsf@igalia.com> (raw)
In-Reply-To: <efba1076-d14c-488b-954a-856e0427f917@bsbernd.com> (Bernd Schubert's message of "Wed, 29 Jan 2025 20:30:56 +0100")

Hi Bernd,

On Wed, Jan 29 2025, Bernd Schubert wrote:

> Hi Luis,
>
> On 1/29/25 19:44, Luis Henriques wrote:
>> diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c
>> index 27ccae63495d..9a0cd88a9bb9 100644
>> --- a/fs/fuse/dev.c
>> +++ b/fs/fuse/dev.c
>> @@ -1630,6 +1630,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
>>  	unsigned int num;
>>  	loff_t file_size;
>>  	loff_t end;
>> +	int fgp_flags = FGP_LOCK | FGP_ACCESSED | FGP_CREAT;
>>  
>>  	err = -EINVAL;
>>  	if (size < sizeof(outarg))
>> @@ -1645,6 +1646,9 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
>>  
>>  	nodeid = outarg.nodeid;
>>  
>> +	if (outarg.flags & FUSE_NOTIFY_STORE_NOWAIT)
>> +		fgp_flags |= FGP_NOWAIT;
>> +
>>  	down_read(&fc->killsb);
>>  
>>  	err = -ENOENT;
>> @@ -1668,14 +1672,25 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
>>  		struct page *page;
>>  		unsigned int this_num;
>>  
>> -		folio = filemap_grab_folio(mapping, index);
>> -		err = PTR_ERR(folio);
>> -		if (IS_ERR(folio))
>> -			goto out_iput;
>> +		folio = __filemap_get_folio(mapping, index, fgp_flags,
>> +					    mapping_gfp_mask(mapping));
>> +		err = PTR_ERR_OR_ZERO(folio);
>> +		if (err) {
>> +			if (!(outarg.flags & FUSE_NOTIFY_STORE_NOWAIT))
>> +				goto out_iput;
>> +			page = NULL;
>> +			/* XXX */
>
> What is the XXX for? 

Right, I guess I should have added extra info there.  I just wanted to
point out that I'm not sure about the value to use for the min_t():

  - If we have a folio, I believe it's clear that we should use
    (offset will always be '0' except for the first iteration);
  - If we don't have a folio, I'm using PAGE_SIZE

I *think* that's the correct value.  But I may be wrong.

> Also, I think you want to go to "skip" only on -EAGAIN? And if so, need
> to unset err? 

Ah, good point!  Thanks!  So, something like this should fix it:

		if (!(outarg.flags & FUSE_NOTIFY_STORE_NOWAIT) || err != -EAGAIN)
			goto out_iput;

I'll wait until tomorrow before sending v2 because I was hoping to also
have some feedback on the idea of completely dropping the use of the
NOWAIT flag.  Not sure you have some opinion about it, or maybe Miklos, as
this patch was originally from him.

Cheers,
-- 
Luís

>> +			this_num = min_t(unsigned int, num, PAGE_SIZE - offset);
>> +		} else {
>> +			page = &folio->page;
>> +			this_num = min_t(unsigned int, num,
>> +					 folio_size(folio) - offset);
>> +		}
>>  
>> -		page = &folio->page;
>> -		this_num = min_t(unsigned, num, folio_size(folio) - offset);
>>  		err = fuse_copy_page(cs, &page, offset, this_num, 0);
>> +		if (!page)
>> +			goto skip;
>> +
>>  		if (!folio_test_uptodate(folio) && !err && offset == 0 &&
>>  		    (this_num == folio_size(folio) || file_size == end)) {
>>  			folio_zero_segment(folio, this_num, folio_size(folio));
>> @@ -1683,7 +1698,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
>>  		}
>>  		folio_unlock(folio);
>>  		folio_put(folio);
>> -
>> +skip:
>>  		if (err)
>>  			goto out_iput;
>>  
>> diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h
>> index e9e78292d107..59725f89340e 100644
>> --- a/include/uapi/linux/fuse.h
>> +++ b/include/uapi/linux/fuse.h
>> @@ -576,6 +576,12 @@ struct fuse_file_lock {
>>   */
>>  #define FUSE_EXPIRE_ONLY		(1 << 0)
>>  
>> +/**
>> + * notify_store flags
>> + * FUSE_NOTIFY_STORE_NOWAIT: skip locked pages
>> + */
>> +#define FUSE_NOTIFY_STORE_NOWAIT	(1 << 0)
>> +
>>  /**
>>   * extension type
>>   * FUSE_MAX_NR_SECCTX: maximum value of &fuse_secctx_header.nr_secctx
>> @@ -1075,7 +1081,7 @@ struct fuse_notify_store_out {
>>  	uint64_t	nodeid;
>>  	uint64_t	offset;
>>  	uint32_t	size;
>> -	uint32_t	padding;
>> +	uint32_t	flags;
>>  };
>>  
>>  struct fuse_notify_retrieve_out {
>> 
>
> Thanks,
> Bernd
>

      reply	other threads:[~2025-01-29 21:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-29 18:44 [RFC PATCH] fuse: fix race in fuse_notify_store() Luis Henriques
2025-01-29 19:30 ` Bernd Schubert
2025-01-29 21:26   ` Luis Henriques [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=87ldut5n2h.fsf@igalia.com \
    --to=luis@igalia.com \
    --cc=bernd@bsbernd.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    --cc=tqin@jumptrading.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