From: Nikolay Borisov <nborisov@suse.com>
To: Matthew Wilcox <willy@infradead.org>
Cc: Filipe Manana <fdmanana@kernel.org>,
dai.ngo@oracle.com, linux-btrfs <linux-btrfs@vger.kernel.org>,
gniebler@suse.com,
"linux-nfs@vger.kernel.org" <linux-nfs@vger.kernel.org>
Subject: Re: bug in btrfs during low memory testing.
Date: Wed, 6 Jul 2022 15:13:31 +0300 [thread overview]
Message-ID: <f188d693-03db-6edd-8961-ed54a190fb84@suse.com> (raw)
In-Reply-To: <YsV7kfglS4EEQTJU@casper.infradead.org>
On 6.07.22 г. 15:09 ч., Matthew Wilcox wrote:
> On Wed, Jul 06, 2022 at 09:36:42AM +0300, Nikolay Borisov wrote:
>> On 5.07.22 г. 23:29 ч., Matthew Wilcox wrote:
>>> On Tue, Jul 05, 2022 at 09:26:47PM +0100, Filipe Manana wrote:
>>>> In this case we can actually call xa_insert() without holding that
>>>> spinlock, it's safe against other concurrent calls to
>>>> btrfs_get_or_create_delayed_node(), btrfs_get_delayed_node(),
>>>> btrfs_kill_delayed_inode_items(), etc.
>>>>
>>>> However, looking at xa_insert() we have:
>>>>
>>>> xa_lock(xa);
>>>> err = __xa_insert(xa, index, entry, gfp);
>>>> xa_unlock(xa);
>>>>
>>>> And xa_lock() is defined as:
>>>>
>>>> #define xa_lock(xa) spin_lock(&(xa)->xa_lock)
>>>>
>>>> So we'll always be under a spinlock even if we change btrfs to not
>>>> take the root->inode_lock spinlock.
>>>>
>>>> This seems more like a general problem outside btrfs' control.
>>>> So CC'ing Willy to double check.
>>>
>>> No, the XArray knows about its own spinlock. It'll drop it if it needs
>>> to allocate memory and the GFP flags indicate that the caller can sleep.
>>> It doesn't know about your spinlock, so it can't do the same thing for
>>> you ;-)
>>
>>
>> In order to catch (and prevent) further offensive can we perhaps have
>> something like that in xa_insert:
>>
>>
>> diff --git a/include/linux/xarray.h b/include/linux/xarray.h
>> index c29e11b2c073..63c00b2945a2 100644
>> --- a/include/linux/xarray.h
>> +++ b/include/linux/xarray.h
>> @@ -770,6 +770,9 @@ static inline int __must_check xa_insert(struct xarray
>> *xa,
>> {
>> int err;
>>
>> + if (gfpflags_allow_blocking(gfp))
>> + might_sleep()
>> +
>> xa_lock(xa);
>> err = __xa_insert(xa, index, entry, gfp);
>> xa_unlock(xa);
>
> I think you mean:
>
> might_alloc(gfp);
>
> And yes, I think that makes a lot of sense. Quite a few similar places
> to do ... I'll take care of it.
Actually I had in mind more along the lines of:
might_sleep_if(gfpflags_allow_blocking(gfp_mask));
but this is essentially what might_alloc does + lockdep annotations, so
yeah, that would work. What I'd like to achieve with such a modification
is for new users of xa array to instantly get a splat when xa_insert is
executed irrespective of whether the allocation happens or not so that
they have a chance to fix their code sooner rather than later.
prev parent reply other threads:[~2022-07-06 12:14 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-05 19:37 bug in btrfs during low memory testing dai.ngo
2022-07-05 20:26 ` Filipe Manana
2022-07-05 20:29 ` Matthew Wilcox
2022-07-05 20:33 ` Filipe Manana
2022-07-06 6:36 ` Nikolay Borisov
2022-07-06 12:09 ` Matthew Wilcox
2022-07-06 12:13 ` Nikolay Borisov [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=f188d693-03db-6edd-8961-ed54a190fb84@suse.com \
--to=nborisov@suse.com \
--cc=dai.ngo@oracle.com \
--cc=fdmanana@kernel.org \
--cc=gniebler@suse.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-nfs@vger.kernel.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