From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D90D9C433F5 for ; Tue, 15 Mar 2022 03:32:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 221F08D0002; Mon, 14 Mar 2022 23:32:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D0F88D0001; Mon, 14 Mar 2022 23:32:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 099C58D0002; Mon, 14 Mar 2022 23:32:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0240.hostedemail.com [216.40.44.240]) by kanga.kvack.org (Postfix) with ESMTP id EFBD28D0001 for ; Mon, 14 Mar 2022 23:32:41 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 919D218213818 for ; Tue, 15 Mar 2022 03:32:41 +0000 (UTC) X-FDA: 79245198522.16.22927CF Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf20.hostedemail.com (Postfix) with ESMTP id 6685B1C0006 for ; Tue, 15 Mar 2022 03:32:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=LX0E2les13YpXYBTKiKwQRB41rW+42lYHTDGzHdEAYU=; b=AJtxFRchW7D27kGcK6oYZmF5sA XonPIJLddVIcOM02rq5l+CPZAvswzV8R0vLGibH1n7vDtNBi+gm0lpOHdTI8nd6bcE/Ek6ASTfm24 yiJ0EYKcNa7cX75sXm10TtJOKSou5cdiBjj4gA5LMqyPZyDJ1ZRU7fhIIyqyAjT5EWxdPJCil+RKd W5ZQnQj1V5z0lzKNjpi2q6oZ2+UCkhn6CX2vevQTuW3ooFUnhiwRikZDoYpLgqwr+CaELRGcUl7dj DuRyLHWcQU2iD+7P2DJilra4pXNGTl+F+1IqVcgdzEeKgkte+s1UX9rzFVrKVR4pRAlvNMyY4m+r+ RirbJIMg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nTxuy-004e5z-Dd; Tue, 15 Mar 2022 03:32:16 +0000 Date: Tue, 15 Mar 2022 03:32:16 +0000 From: Matthew Wilcox To: Andrew Yang Cc: Andrew Morton , Matthias Brugger , Vlastimil Babka , David Howells , William Kucharski , David Hildenbrand , Yang Shi , Marc Zyngier , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, wsd_upstream@mediatek.com, Nicholas Tang , Kuan-Ying Lee Subject: Re: [PATCH] mm/migrate: fix race between lock page and clear PG_Isolated Message-ID: References: <20220315030515.20263-1-andrew.yang@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220315030515.20263-1-andrew.yang@mediatek.com> X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 6685B1C0006 X-Stat-Signature: pibn57kq9uf5iqnytczjc9aakmcn7rgn Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=AJtxFRch; dmarc=none; spf=none (imf20.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Rspam-User: X-HE-Tag: 1647315160-532908 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Mar 15, 2022 at 11:05:15AM +0800, Andrew Yang 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. > > Signed-off-by: andrew.yang > --- > include/linux/page-flags.h | 2 +- > mm/migrate.c | 12 ++++++------ > 2 files changed, 7 insertions(+), 7 deletions(-) > > diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h > index 1c3b6e5c8bfd..64a84a9835cb 100644 > --- a/include/linux/page-flags.h > +++ b/include/linux/page-flags.h > @@ -918,7 +918,7 @@ PAGE_TYPE_OPS(Guard, guard) > > extern bool is_free_buddy_page(struct page *page); > > -__PAGEFLAG(Isolated, isolated, PF_ANY); > +PAGEFLAG(Isolated, isolated, PF_ANY); Agreed. Further, page cannot be a tail page (this is implied by the get_page_unless_zero() as tailpages have a zero refcount, and it is assumed by __PageMovable() as page->mapping is undefined for tail pages). So this can actually be: +PAGEFLAG(Isolated, isolated, PF_NO_TAIL); I considered PF_ONLY_HEAD, but there are a lot more places that _check_ PageIsolated() and I don't want to prove that they're all definitely working on head pages.