From: Alistair Popple <apopple@nvidia.com>
To: Jason Gunthorpe <jgg@ziepe.ca>
Cc: akpm@linux-foundation.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, christian.koenig@amd.com,
jhubbard@nvidia.com, rcampbell@nvidia.com
Subject: Re: [PATCH] mm/mmu_notifier.c: Fix race in mmu_interval_notifier_remove()
Date: Tue, 19 Apr 2022 11:51:58 +1000 [thread overview]
Message-ID: <87v8v5kfem.fsf@nvdebian.thelocal> (raw)
In-Reply-To: <20220414135005.GJ64706@ziepe.ca>
[-- Attachment #1: Type: text/plain, Size: 2830 bytes --]
Jason Gunthorpe <jgg@ziepe.ca> writes:
> On Thu, Apr 14, 2022 at 01:18:10PM +1000, Alistair Popple wrote:
>> In some cases it is possible for mmu_interval_notifier_remove() to race
>> with mn_tree_inv_end() allowing it to return while the notifier data
>> structure is still in use. Consider the following sequence:
>>
>> CPU0 - mn_tree_inv_end() CPU1 - mmu_interval_notifier_remove()
>> spin_lock(subscriptions->lock);
>> seq = subscriptions->invalidate_seq;
>> spin_lock(subscriptions->lock); spin_unlock(subscriptions->lock);
>> subscriptions->invalidate_seq++;
>> wait_event(invalidate_seq != seq);
>> return;
>> interval_tree_remove(interval_sub); kfree(interval_sub);
>> spin_unlock(subscriptions->lock);
>> wake_up_all();
>>
>> As the wait_event() condition is true it will return immediately. This
>> can lead to use-after-free type errors if the caller frees the data
>> structure containing the interval notifier subscription while it is
>> still on a deferred list. Fix this by changing invalidate_seq to an
>> atomic type as it is read outside of the lock and moving the increment
>> until after deferred lists have been updated.
>
> Oh, yes, that is a mistake.
>
> I would not solve it with more unlocked atomics though, this is just a
> simple case of a missing lock - can you look at this and if you like
> it post it as a patch please?
Yep, that looks good and is easier to understand. For some reason I had assumed
the lack of locking was intentional. Will post the below fix as v2.
> diff --git a/mm/mmu_notifier.c b/mm/mmu_notifier.c
> index 459d195d2ff64b..f45ff1b7626a62 100644
> --- a/mm/mmu_notifier.c
> +++ b/mm/mmu_notifier.c
> @@ -1036,6 +1036,18 @@ int mmu_interval_notifier_insert_locked(
> }
> EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert_locked);
>
> +static bool
> +mmu_interval_seq_released(struct mmu_notifier_subscriptions *subscriptions,
> + unsigned long seq)
> +{
> + bool ret;
> +
> + spin_lock(&subscriptions->lock);
> + ret = subscriptions->invalidate_seq != seq;
> + spin_unlock(&subscriptions->lock);
> + return ret;
> +}
> +
> /**
> * mmu_interval_notifier_remove - Remove a interval notifier
> * @interval_sub: Interval subscription to unregister
> @@ -1083,7 +1095,7 @@ void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub)
> lock_map_release(&__mmu_notifier_invalidate_range_start_map);
> if (seq)
> wait_event(subscriptions->wq,
> - READ_ONCE(subscriptions->invalidate_seq) != seq);
> + mmu_interval_seq_released(subscriptions, seq));
>
> /* pairs with mmgrab in mmu_interval_notifier_insert() */
> mmdrop(mm);
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
prev parent reply other threads:[~2022-04-19 1:53 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-14 3:18 [PATCH] mm/mmu_notifier.c: Fix race in mmu_interval_notifier_remove() Alistair Popple
2022-04-14 13:50 ` Jason Gunthorpe
2022-04-19 1:51 ` Alistair Popple [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=87v8v5kfem.fsf@nvdebian.thelocal \
--to=apopple@nvidia.com \
--cc=akpm@linux-foundation.org \
--cc=christian.koenig@amd.com \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rcampbell@nvidia.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;
as well as URLs for NNTP newsgroup(s).