[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <883877a-30b0-96e0-48a6-7cfc3c59de93@google.com>
Date: Tue, 15 Mar 2022 13:34:02 -0700 (PDT)
From: Hugh Dickins <hughd@...gle.com>
To: David Hildenbrand <david@...hat.com>
cc: Andrew Morton <akpm@...ux-foundation.org>,
Andrew Yang <andrew.yang@...iatek.com>,
Matthias Brugger <matthias.bgg@...il.com>,
Matthew Wilcox <willy@...radead.org>,
Vlastimil Babka <vbabka@...e.cz>,
David Howells <dhowells@...hat.com>,
William Kucharski <william.kucharski@...cle.com>,
Yang Shi <shy828301@...il.com>, Marc Zyngier <maz@...nel.org>,
linux-kernel@...r.kernel.org, linux-mm@...ck.org,
linux-arm-kernel@...ts.infradead.org,
linux-mediatek@...ts.infradead.org, wsd_upstream@...iatek.com,
Nicholas Tang <nicholas.tang@...iatek.com>,
Kuan-Ying Lee <Kuan-Ying.Lee@...iatek.com>
Subject: Re: [PATCH] mm/migrate: fix race between lock page and clear
PG_Isolated
On Tue, 15 Mar 2022, David Hildenbrand wrote:
> On 15.03.22 05:21, Andrew Morton wrote:
> > On Tue, 15 Mar 2022 11:05:15 +0800 Andrew Yang <andrew.yang@...iatek.com> wrote:
> >
> >> When memory is tight, system may start to compact memory for large
> >> continuous memory demands. If one process tries to lock a memory page
> >> that is being locked and isolated for compaction, it may wait a long time
> >> or even forever. This is because compaction will perform non-atomic
> >> PG_Isolated clear while holding page lock, this may overwrite PG_waiters
> >> set by the process that can't obtain the page lock and add itself to the
> >> waiting queue to wait for the lock to be unlocked.
> >>
> >> CPU1 CPU2
> >> lock_page(page); (successful)
> >> lock_page(); (failed)
> >> __ClearPageIsolated(page); SetPageWaiters(page) (may be overwritten)
> >> unlock_page(page);
> >>
> >> The solution is to not perform non-atomic operation on page flags while
> >> holding page lock.
> >
> > Sure, the non-atomic bitop optimization is really risky and I suspect
> > we reach for it too often. Or at least without really clearly
> > demonstrating that it is safe, and documenting our assumptions.
>
> I agree. IIRC, non-atomic variants are mostly only safe while the
> refcount is 0. Everything else is just absolutely fragile.
It is normal and correct to use __SetPageFlag(page) on a page just allocated
from the buddy, and not yet logically visible to others: that has refcount 1.
Of course, it might have refcount 2 or more, through being speculatively
visible to get_page_unless_zero() users: perhaps through earlier usage of
the same struct page, or by physical scan of memmap.
Those few such others - compaction's isolate_migratepages_block() is the
one I know best - must be very careful in their sequence of operations.
Preliminary read-only checks are usually okay (but some VM_BUG_ON_PGFLAGS
are increasingly problematic: I've had to turn off that CONFIG), then
get_page_unless_zero(), then read-only check that the page is of the
manageable kind (PageLRU in my world), and only then can it be safe to
lock the page - which of course touches page flags, and so would be
problematic for a racing user's __SetPageFlag(page).
But PageMovable and PageIsolated are beyond my ken: I can't say there.
Hugh
Powered by blists - more mailing lists