From: David Hildenbrand <david@redhat.com>
To: Ryan Roberts <ryan.roberts@arm.com>,
Andrew Morton <akpm@linux-foundation.org>,
"Huang, Ying" <ying.huang@intel.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v1] mm: swap: Fix race between free_swap_and_cache() and swapoff()
Date: Tue, 5 Mar 2024 23:05:11 +0100 [thread overview]
Message-ID: <017414bc-78cd-4aa1-9edf-6ce947b9e4e4@redhat.com> (raw)
In-Reply-To: <8989df79-84f5-488c-bd74-c11d2241eff1@arm.com>
On 05.03.24 17:33, Ryan Roberts wrote:
> On 05/03/2024 15:50, David Hildenbrand wrote:
>> On 05.03.24 16:13, Ryan Roberts wrote:
>>> There was previously a theoretical window where swapoff() could run and
>>> teardown a swap_info_struct while a call to free_swap_and_cache() was
>>> running in another thread. This could cause, amongst other bad
>>> possibilities, swap_page_trans_huge_swapped() (called by
>>> free_swap_and_cache()) to access the freed memory for swap_map.
>>>
>>> This is a theoretical problem and I haven't been able to provoke it from
>>> a test case. But there has been agreement based on code review that this
>>> is possible (see link below).
>>>
>>> Fix it by using get_swap_device()/put_swap_device(), which will stall
>>> swapoff(). There was an extra check in _swap_info_get() to confirm that
>>> the swap entry was valid. This wasn't present in get_swap_device() so
>>> I've added it. I couldn't find any existing get_swap_device() call sites
>>> where this extra check would cause any false alarms.
>>>
>>> Details of how to provoke one possible issue (thanks to David Hilenbrand
>>> for deriving this):
>>
>> Almost
>>
>> "s/Hilenbrand/Hildenbrand/" :)
>
> Ahh sorry... I even explicitly checked it against your name on emails... fat
> fingers...
No need to be sorry. Even your average German person would get it wrong,
because there are other (more common) variants :)
[...]
>>>
>>
>> LGTM
>>
>> Are you planning on sending a doc extension for get_swap_device()?
>
> I saw your comment:
>
> We should likely update the documentation of get_swap_device(), that after
> decrementing the refcount, the SI might become stale and should not be touched
> without a prior get_swap_device().
>
> But when I went to make the changes, I saw the documentation already said:
>
> ...we need to enclose all swap related functions with get_swap_device() and
> put_swap_device()... Notice that swapoff ... can still happen before the
> percpu_ref_tryget_live() in get_swap_device() or after the percpu_ref_put() in
> put_swap_device()... The caller must be prepared for that.
>
> I thought that already covered it? I'm sure as usual, I've misunderstood your
> point. Happy to respin if you have something in mind?
No need to respin, we could clarify on top, if we decide it makes sense.
I was thinking about something like this, making it clearer that the PTL
discussion above does not express the corner case we discovered:
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 2b3a2d85e350b..646a436581eee 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1232,6 +1232,11 @@ static unsigned char __swap_entry_free_locked(struct swap_info_struct *p,
* with get_swap_device() and put_swap_device(), unless the swap
* functions call get/put_swap_device() by themselves.
*
+ * Note that when only holding the PTL, swapoff might succeed immediately
+ * after freeing a swap entry. Therefore, immediately after
+ * __swap_entry_free(), the swap info might become stale and should not
+ * be touched without a prior get_swap_device().
+ *
* Check whether swap entry is valid in the swap device. If so,
* return pointer to swap_info_struct, and keep the swap entry valid
* via preventing the swap device from being swapoff, until
--
Cheers,
David / dhildenb
next prev parent reply other threads:[~2024-03-05 22:05 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-05 15:13 Ryan Roberts
2024-03-05 15:50 ` David Hildenbrand
2024-03-05 16:33 ` Ryan Roberts
2024-03-05 22:05 ` David Hildenbrand [this message]
2024-03-06 2:39 ` Huang, Ying
2024-03-06 8:10 ` Ryan Roberts
2024-03-06 2:19 ` Johannes Weiner
2024-03-06 8:23 ` Ryan Roberts
2024-03-06 2:52 ` Huang, Ying
2024-03-06 8:51 ` Miaohe Lin
2024-03-06 9:31 ` Ryan Roberts
2024-03-07 2:38 ` Miaohe Lin
2024-03-07 5:56 ` Huang, Ying
2024-03-07 6:50 ` Miaohe Lin
2024-03-07 7:34 ` Huang, Ying
2024-03-07 7:48 ` Ryan Roberts
2024-03-07 8:54 ` Huang, Ying
2024-03-07 9:19 ` Ryan Roberts
2024-03-08 0:55 ` Huang, Ying
2024-03-07 8:50 ` Miaohe Lin
2024-03-07 4:37 ` Huang, Ying
2024-03-07 4:39 ` Huang, Ying
2024-03-06 9:03 ` Ryan Roberts
2024-03-07 5:48 ` Huang, Ying
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=017414bc-78cd-4aa1-9edf-6ce947b9e4e4@redhat.com \
--to=david@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ryan.roberts@arm.com \
--cc=stable@vger.kernel.org \
--cc=ying.huang@intel.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