All of lore.kernel.org
 help / color / mirror / Atom feed
From: Baoquan He <bhe@redhat.com>
To: Kairui Song <ryncsn@gmail.com>
Cc: linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
	Chris Li <chrisl@kernel.org>, Barry Song <v-songbaohua@oppo.com>,
	Ryan Roberts <ryan.roberts@arm.com>,
	Hugh Dickins <hughd@google.com>,
	Yosry Ahmed <yosryahmed@google.com>,
	"Huang, Ying" <ying.huang@linux.alibaba.com>,
	Nhat Pham <nphamcs@gmail.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Kalesh Singh <kaleshsingh@google.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 07/13] mm, swap: hold a reference during scan and cleanup flag usage
Date: Wed, 5 Feb 2025 17:18:22 +0800	[thread overview]
Message-ID: <Z6Ms3qRDXat31sKc@fedora> (raw)
In-Reply-To: <CAMgjq7CiOFPRuxfEShkemJB9+tOJEw4sYFSndBE0pvNTEQH+YQ@mail.gmail.com>

On 01/27/25 at 05:19pm, Kairui Song wrote:
> On Mon, Jan 20, 2025 at 10:39 AM Baoquan He <bhe@redhat.com> wrote:
> >
> > On 01/13/25 at 01:34pm, Kairui Song wrote:
> > > On Sat, Jan 4, 2025 at 1:46 PM Baoquan He <bhe@redhat.com> wrote:
> > > >
> > > > On 12/31/24 at 01:46am, Kairui Song wrote:
> > > > > From: Kairui Song <kasong@tencent.com>
> > > > >
> > > > > The flag SWP_SCANNING was used as an indicator of whether a device
> > > > > is being scanned for allocation, and prevents swapoff. Combined with
> > > > > SWP_WRITEOK, they work as a set of barriers for a clean swapoff:
> > > > >
> > > > > 1. Swapoff clears SWP_WRITEOK, allocation requests will see
> > > > >    ~SWP_WRITEOK and abort as it's serialized by si->lock.
> > > > > 2. Swapoff unuses all allocated entries.
> > > > > 3. Swapoff waits for SWP_SCANNING flag to be cleared, so ongoing
> > > > >    allocations will stop, preventing UAF.
> > > > > 4. Now swapoff can free everything safely.
> > > > >
> > > > > This will make the allocation path have a hard dependency on
> > > > > si->lock. Allocation always have to acquire si->lock first for
> > > > > setting SWP_SCANNING and checking SWP_WRITEOK.
> > > > >
> > > > > This commit removes this flag, and just uses the existing per-CPU
> > > > > refcount instead to prevent UAF in step 3, which serves well for
> > > > > such usage without dependency on si->lock, and scales very well too.
> > > > > Just hold a reference during the whole scan and allocation process.
> > > > > Swapoff will kill and wait for the counter.
> > > > >
> > > > > And for preventing any allocation from happening after step 1 so the
> > > > > unuse in step 2 can ensure all slots are free, swapoff will acquire
> > > > > the ci->lock of each cluster one by one to ensure all allocations
> > > > > see ~SWP_WRITEOK and abort.
> > > >
> > > > Changing to use si->users is great, while wondering why we need acquire =
> > > > each ci->lock now. After setup 1, we have cleared SWP_WRITEOK, and take
> > > > the si off swap_avail_heads list. No matter what, we just need wait for
> > > > p->comm's completion and continue, why bothering to loop for the
> > > > ci->lock acquiring?
> > > >
> > >
> > > Hi Baoquan,
> > >
> > > Waiting for p->comm's completion must be done after unuse is called
> > > (unuse will need to take the si->users refcound, so it can't be dead
> > > yet), but unuse must be called after no one will allocate any new
> > > entry. That is guaranteed by the loop ci->lock acquiring.
> >
> > Sorry for late response, Kairui. I went trought the code flow of swap
> > allocation several times, however haven't made clear how loop ci->lock
> > acquiring is needed here.  Once si->flags &= ~SWP_WRITEOK is executed in
> > del_from_avail_list() when swaping off, even though the allocation
> > action is still on going, it will be failed in cluster_alloc_range()
> > by the 'if (!(si->flags & SWP_WRITEOK))' checking. Then that allocation
> 
> Hi Baoquan,
> 
> Thanks for the careful review.
> 
> > requirement will be failed and returned, means no new swap entry|slot
> > allcation will be done. Then unuse won't be impacted at all. In this
> > case, why do we care about it?
> >
> > Please forgive my stupidity, could you elaborate in which case this kind
> > of still ongoging swap allocation will happen during its swap device's
> > off? Could you give an example of the concurrent execution flows?
> 
> There is no barrier or lock between clear the flag and try_to_unuse,
> so nothing guarantees the "if (!(si->flags & SWP_WRITEOK))" in
> cluster_alloc_range will see the updated flag. The loop ci->lock acts
> like a full memory barrier, ensuring any allocation after the loop
> lock will definitely see the updated flags, and try_to_unuse will only
> go on after all allocation have either stopped or will see the updated
> flags. In practice this problem is almost impossible to happen, but in
> theory possible.

Got it now. swap_avail_lock is not taken during allocation, and we don't
take it when accessing si->flags in cluster_alloc_range() becasue that
could bring in new lock contention.

Thanks a lot for patient explanation.



  reply	other threads:[~2025-02-05  9:18 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-30 17:46 [PATCH v3 00/13] mm, swap: rework of swap allocator locks Kairui Song
2024-12-30 17:46 ` [PATCH v3 01/13] mm, swap: minor clean up for swap entry allocation Kairui Song
2025-01-09  4:04   ` Baoquan He
2024-12-30 17:46 ` [PATCH v3 02/13] mm, swap: fold swap_info_get_cont in the only caller Kairui Song
2025-01-09  4:05   ` Baoquan He
2024-12-30 17:46 ` [PATCH v3 03/13] mm, swap: remove old allocation path for HDD Kairui Song
2025-01-09  4:06   ` Baoquan He
2024-12-30 17:46 ` [PATCH v3 04/13] mm, swap: use cluster lock " Kairui Song
2025-01-09  4:07   ` Baoquan He
2024-12-30 17:46 ` [PATCH v3 05/13] mm, swap: clean up device availability check Kairui Song
2025-01-09  4:08   ` Baoquan He
2024-12-30 17:46 ` [PATCH v3 06/13] mm, swap: clean up plist removal and adding Kairui Song
2025-01-02  8:59   ` Baoquan He
2025-01-03  8:07     ` Kairui Song
2024-12-30 17:46 ` [PATCH v3 07/13] mm, swap: hold a reference during scan and cleanup flag usage Kairui Song
2025-01-04  5:46   ` Baoquan He
2025-01-13  5:34     ` Kairui Song
2025-01-20  2:39       ` Baoquan He
2025-01-27  9:19         ` Kairui Song
2025-02-05  9:18           ` Baoquan He [this message]
2024-12-30 17:46 ` [PATCH v3 08/13] mm, swap: use an enum to define all cluster flags and wrap flags changes Kairui Song
2025-01-06  8:43   ` Baoquan He
2025-01-13  5:49     ` Kairui Song
2024-12-30 17:46 ` [PATCH v3 09/13] mm, swap: reduce contention on device lock Kairui Song
2025-01-06 10:12   ` Baoquan He
2025-01-08 11:09   ` Baoquan He
2025-01-09  2:15     ` Kairui Song
2025-01-10 11:23       ` Baoquan He
2025-01-13  6:33         ` Kairui Song
2025-01-13  8:07           ` Kairui Song
2024-12-30 17:46 ` [PATCH v3 10/13] mm, swap: simplify percpu cluster updating Kairui Song
2025-01-09  2:07   ` Baoquan He
2024-12-30 17:46 ` [PATCH v3 11/13] mm, swap: introduce a helper for retrieving cluster from offset Kairui Song
2024-12-30 17:46 ` [PATCH v3 12/13] mm, swap: use a global swap cluster for non-rotation devices Kairui Song
2024-12-30 17:46 ` [PATCH v3 13/13] mm, swap_slots: remove slot cache for freeing path Kairui Song

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=Z6Ms3qRDXat31sKc@fedora \
    --to=bhe@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=chrisl@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=kaleshsingh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nphamcs@gmail.com \
    --cc=ryan.roberts@arm.com \
    --cc=ryncsn@gmail.com \
    --cc=v-songbaohua@oppo.com \
    --cc=ying.huang@linux.alibaba.com \
    --cc=yosryahmed@google.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.