From patchwork Thu Jun 11 22:28:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 11600961 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 572B590 for ; Thu, 11 Jun 2020 22:29:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 240FE2075F for ; Thu, 11 Jun 2020 22:29:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="C9efrmYg" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 240FE2075F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3EB838D00A9; Thu, 11 Jun 2020 18:28:59 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 39B0B8D00A0; Thu, 11 Jun 2020 18:28:59 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D81F8D00A9; Thu, 11 Jun 2020 18:28:59 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0213.hostedemail.com [216.40.44.213]) by kanga.kvack.org (Postfix) with ESMTP id 16D2C8D00A0 for ; Thu, 11 Jun 2020 18:28:59 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id C235F180AD82F for ; Thu, 11 Jun 2020 22:28:58 +0000 (UTC) X-FDA: 76918372356.15.crate64_4b0521c26dd7 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id 94EAA1814B0C1 for ; Thu, 11 Jun 2020 22:28:58 +0000 (UTC) X-Spam-Summary: 2,0,0,9ca55b1361c31eef,d41d8cd98f00b204,hughd@google.com,,RULES_HIT:41:355:379:800:960:966:973:988:989:1260:1277:1313:1314:1345:1437:1516:1518:1535:1542:1593:1594:1711:1730:1747:1777:1792:2194:2196:2199:2200:2393:2553:2559:2562:2693:2895:2898:3138:3139:3140:3141:3142:3152:3353:3865:3866:3867:3868:3870:3872:4321:4385:5007:6119:6261:6653:6742:7875:7903:8957:10004:10400:11026:11232:11658:11914:12043:12114:12296:12297:12438:12517:12519:12555:12740:12895:12986:13153:13161:13228:13229:13439:14096:14097:14181:14394:14659:14721:21080:21444:21451:21627:30054:30070:30090,0,RBL:209.85.210.42:@google.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: crate64_4b0521c26dd7 X-Filterd-Recvd-Size: 5512 Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.42]) by imf42.hostedemail.com (Postfix) with ESMTP for ; Thu, 11 Jun 2020 22:28:58 +0000 (UTC) Received: by mail-ot1-f42.google.com with SMTP id k15so5850037otp.8 for ; Thu, 11 Jun 2020 15:28:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:user-agent:mime-version; bh=yQuXdwOztfCCh1OM0Y/znnoWVtA42w245nppjLu++SM=; b=C9efrmYgO5lfHiPabO9leNQkZRsdhlwEQGAWMA52SmmHSpafgnK+/l847DiwWOyBpj BAeGvslbj7wElzHda4DwvXS0ku8R79Ov2+bHCOh/Wc5QGdmv0su6wFBq2OfvGk2eaTGq u9MFt68ahkxY0WjRh3nuhhBOlrOW1MnYy8G6VghffVY4IdRBNay/7mj7FCjMHKTJv2ht GYwLGmvsv2JG78AtNqL4aAn4TX+q6VYmWxN1EECEZxgW8vMb3YpE6BvTsBs1SmWoAXbN k7edAcL63vK2Ld9noBiS6oSmq4WzbzSYWD0olZCdndrD21tLhFkspBj4YJEgjnw+feF7 tXHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:user-agent :mime-version; bh=yQuXdwOztfCCh1OM0Y/znnoWVtA42w245nppjLu++SM=; b=U15PrqrzboxTaSFqGzDFnBq9LK76trahoI4483a/skob3d4pqz5bD6dGPBNfoLrV+p eAUTWLpMrPgLs8wDTnqbs+XtBmm0DUwKnw8kP7PY+aRWvYEqIpyerM8QQKaMIY40o7X7 sbpY+C6axA9GT7selFf21em3mnccvQmvs4NrK86DDCmIHiiIbl15QziydEUECUlbaPu/ EK86Y2NRnBMXTMIOGc5uV+S0dsN3YF3Yxt+dW3TPLVPkqT7SSI7EfzgFJ2qjVVR68J1q ocrdTM0hMdQ2cYkNis23TnA+nqjWNz+VQ7Y3FoXuQmYNwKoVAGcDpO+kaxLj6BhVZ39U 2KLQ== X-Gm-Message-State: AOAM530oM/ZroSlYa8gK9sqHSdbISn09JH7peaKZZEiUp4nhKzpeTC72 Hvm7wOnoLtgsHQ4g7SSSTlCeRQ== X-Google-Smtp-Source: ABdhPJwC0cSrxAVd/oScf78OXudE/J7qH0OBFpxhpmCCenQEqZL2QUwwdccMsG0/Q/aHQLe+aN7Mmg== X-Received: by 2002:a9d:1c9b:: with SMTP id l27mr9063837ota.22.1591914537191; Thu, 11 Jun 2020 15:28:57 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id 126sm974999oii.39.2020.06.11.15.28.54 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Thu, 11 Jun 2020 15:28:56 -0700 (PDT) Date: Thu, 11 Jun 2020 15:28:54 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Alex Shi cc: akpm@linux-foundation.org, mgorman@techsingularity.net, tj@kernel.org, khlebnikov@yandex-team.ru, daniel.m.jordan@oracle.com, yang.shi@linux.alibaba.com, willy@infradead.org, hannes@cmpxchg.org, lkp@intel.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, shakeelb@google.com, iamjoonsoo.kim@lge.com, richard.weiyang@gmail.com Subject: [PATCH alexv12 1/2] mm/compaction: fix isolate_migratepages_block() fails Message-ID: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 X-Rspamd-Queue-Id: 94EAA1814B0C1 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: Fix lots of crashes under compaction load: isolate_migratepages_block() must clean up appropriately when rejecting a page, setting PageLRU again if it had been cleared; and a put_page() after get_page_unless_zero() cannot safely be done while holding locked_lruvec - it may turn out to be the final put_page(), which will take an lruvec lock when PageLRU. Signed-off-by: Hugh Dickins --- These fixes should be folded into 10/16 and 12/16, I have not tried to figure out what belongs in which. mm/compaction.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) --- alexv12/mm/compaction.c 2020-06-11 13:24:52.037154793 -0700 +++ hughd/mm/compaction.c 2020-06-11 13:48:10.437046025 -0700 @@ -879,6 +879,7 @@ isolate_migratepages_block(struct compac if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) { if (!cc->ignore_skip_hint && get_pageblock_skip(page)) { low_pfn = end_pfn; + page = NULL; goto isolate_abort; } valid_page = page; @@ -971,10 +972,8 @@ isolate_migratepages_block(struct compac goto isolate_fail; /* Try isolate the page */ - if (!TestClearPageLRU(page)) { - put_page(page); - goto isolate_fail; - } + if (!TestClearPageLRU(page)) + goto isolate_fail_put; rcu_read_lock(); lruvec = mem_cgroup_page_lruvec(page, pgdat); @@ -1005,7 +1004,8 @@ isolate_migratepages_block(struct compac */ if (unlikely(PageCompound(page) && !cc->alloc_contig)) { low_pfn += compound_nr(page) - 1; - goto isolate_fail; + SetPageLRU(page); + goto isolate_fail_put; } } else rcu_read_unlock(); @@ -1038,6 +1038,15 @@ isolate_success: } continue; + +isolate_fail_put: + /* Avoid potential deadlock in freeing page under lru_lock */ + if (locked_lruvec) { + unlock_page_lruvec_irqrestore(locked_lruvec, flags); + locked_lruvec = NULL; + } + put_page(page); + isolate_fail: if (!skip_on_failure) continue; @@ -1074,10 +1083,15 @@ isolate_fail: */ if (unlikely(low_pfn > end_pfn)) low_pfn = end_pfn; + page = NULL; isolate_abort: if (locked_lruvec) unlock_page_lruvec_irqrestore(locked_lruvec, flags); + if (page) { + SetPageLRU(page); + put_page(page); + } /* * Updated the cached scanner pfn once the pageblock has been scanned From patchwork Thu Jun 11 22:30:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 11600991 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 35F5F913 for ; Thu, 11 Jun 2020 22:30:51 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 017672074B for ; Thu, 11 Jun 2020 22:30:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="W26cncJy" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 017672074B Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 34E768D00AA; Thu, 11 Jun 2020 18:30:50 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 2D7B58D00A0; Thu, 11 Jun 2020 18:30:50 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A0098D00AA; Thu, 11 Jun 2020 18:30:50 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0080.hostedemail.com [216.40.44.80]) by kanga.kvack.org (Postfix) with ESMTP id F2B458D00A0 for ; Thu, 11 Jun 2020 18:30:49 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id AFE4F181ABE8B for ; Thu, 11 Jun 2020 22:30:49 +0000 (UTC) X-FDA: 76918377018.08.stew49_5900b9426dd7 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id 77DFA181ADB5F for ; Thu, 11 Jun 2020 22:30:49 +0000 (UTC) X-Spam-Summary: 2,0,0,8c6826af27535535,d41d8cd98f00b204,hughd@google.com,,RULES_HIT:41:355:379:800:960:966:973:988:989:1260:1277:1313:1314:1345:1437:1516:1518:1535:1542:1593:1594:1711:1730:1747:1777:1792:2194:2196:2199:2200:2393:2553:2559:2562:3138:3139:3140:3141:3142:3152:3354:3743:3865:3866:3867:3868:3870:3871:3872:3874:4321:4385:5007:6119:6261:6653:6742:7808:7875:7903:8660:8957:9108:9592:10004:10400:11026:11232:11658:11914:12043:12114:12296:12297:12438:12517:12519:12555:12740:12895:12986:13148:13230:13439:14096:14097:14181:14394:14659:14721:21080:21212:21444:21451:21627:21939:30054:30070:30090,0,RBL:209.85.210.65:@google.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: stew49_5900b9426dd7 X-Filterd-Recvd-Size: 5731 Received: from mail-ot1-f65.google.com (mail-ot1-f65.google.com [209.85.210.65]) by imf39.hostedemail.com (Postfix) with ESMTP for ; Thu, 11 Jun 2020 22:30:49 +0000 (UTC) Received: by mail-ot1-f65.google.com with SMTP id 97so5876552otg.3 for ; Thu, 11 Jun 2020 15:30:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:user-agent:mime-version; bh=+EahI0YlQUT1/wBvgRJFQZcHP3obsCOagtKe7ySyhrM=; b=W26cncJyLH/i2zSIOMrO837pRs3uf2BxQU7IzrLdCgttozbazIeNDlQgCyQFiYpUyY Ms4olROFTxfvmQAdemlh52vDy0Ftr89GQMQrDXOir38oNwY8+8gPiDMnQChh/qPvfhj1 iUsIdILczYmCpO1xqBDBaFflxA4qJYGotgpCt1+gBdruUxe14ckzTnM3+9z+NR3Qkckp 8bXFzj6IhT3p8E6ZtAmnlH8HwiB/X34paleeTrwi7sO8xUPY1QHO1EqXGNShfQxJUmTK oNomeUpR4hUAyI5seihocIFNBhP5T0Ojn22FOBFdo6AgdYqo+CShth6zOvtf4oEc8vZn S8fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:user-agent :mime-version; bh=+EahI0YlQUT1/wBvgRJFQZcHP3obsCOagtKe7ySyhrM=; b=eNIOTck/QpT+zfJF4aVTP4GCOh7lZUvUbLeTWxa4GQ7ic/h5C/KB55N8sDYSMbw95T rjfSin58jgvPlZE0Hlqqg7+ZJYx9Y0tdnzWrI/rhJjyRhhDSpVA3UOYsZVAvTri9D5nC 2yDvRuRU8iLb72tCfD0CCmmw7ugNJX6+AuzanD7AiKCXVdE69S8d2f4peMqg0gZmLq2s dccJVI/6Sf4Jp3Boe8LN4EZ7vJ8QVJ6AE1ubS9SO1OR72V0ysludg34q56OJ9Pk3vS8i yczXQQfr8leGx04z0w0uY+MXKLcmwDXIKp5YkfE55uSm2V2zCkBudBdESBCyriOeOfWX QowA== X-Gm-Message-State: AOAM533e6qekjU2pO22ZiycPKP93kJ7cIN3kU7JeuiIJz6J8fAWurx3h 1DhjA5XySlETxsRs6Wr0pQj3jw== X-Google-Smtp-Source: ABdhPJzEon9DZKGa7Vpn9RDUNP7chh++P+QM4RhU5y/VWUIMVZUMKcU4pCYumWLy9ZXhRooe4mHeuA== X-Received: by 2002:a05:6830:1dba:: with SMTP id z26mr8682022oti.180.1591914648150; Thu, 11 Jun 2020 15:30:48 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id r15sm978948oot.5.2020.06.11.15.30.45 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Thu, 11 Jun 2020 15:30:47 -0700 (PDT) Date: Thu, 11 Jun 2020 15:30:45 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Alex Shi cc: akpm@linux-foundation.org, mgorman@techsingularity.net, tj@kernel.org, khlebnikov@yandex-team.ru, daniel.m.jordan@oracle.com, yang.shi@linux.alibaba.com, willy@infradead.org, hannes@cmpxchg.org, lkp@intel.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, shakeelb@google.com, iamjoonsoo.kim@lge.com, richard.weiyang@gmail.com Subject: [PATCH alexv12 2/2] mm/compaction: fix call to __isolate_lru_page_prepare() Message-ID: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 X-Rspamd-Queue-Id: 77DFA181ADB5F X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 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: isolate_migratepages_block() is calling __isolate_lru_page_prepare() at a point when it has not yet acquired a reference to the page, and may not yet hold the right lruvec lock: it has no hold on the page. trylock_page() is not safe to use at this time: its setting PG_locked can race with the page being freed or allocated ("Bad page"), and can also erase flags being set by one of those "sole owners" of a freshly allocated page who use non-atomic __SetPageFlag(). Though I have tried rcu_read_lock() instead of trylock_page() there (like in page_evictable()), 054f1d1faaed ("mm/swap_state.c: simplify total_swapcache_pages() with get_swap_device()") stopped the freeing of swapper_spaces by RCU; and races against setting PageSwapCache, and the dereference of mapping->a_ops, and the lack of any page reference: all make that a more dubious approach. Just move the call to __isolate_lru_page_prepare() after the call to get_page_unless_zero(), when using trylock_page() has become safe (safe given __isolate's check for PageLRU - unsafe without that). Signed-off-by: Hugh Dickins --- I had envisaged this as a separate patch; but once it came down to just moving the call inside isolate_migratepages_block(), it's probably best folded into 10/16 or 12/16 (needs isolate_fail_put). I shall probably want to come along later, to clean up or eliminate __isolate_lru_page_prepare(): which I found before to have almost nothing in common between its use by isolate_migratepages_block() and its use by isolate_lru_pages(). We can then do its safer checks before the get_page_unless_zero(). But trying that cleanup right now would just get in the way of this series. mm/compaction.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) --- alexv12/mm/compaction.c 2020-06-11 13:48:10.437046025 -0700 +++ hughd/mm/compaction.c 2020-06-11 13:49:05.570579095 -0700 @@ -960,9 +960,6 @@ isolate_migratepages_block(struct compac if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page)) goto isolate_fail; - if (__isolate_lru_page_prepare(page, isolate_mode) != 0) - goto isolate_fail; - /* * Be careful not to clear PageLRU until after we're * sure the page is not being freed elsewhere -- the @@ -971,6 +968,9 @@ isolate_migratepages_block(struct compac if (unlikely(!get_page_unless_zero(page))) goto isolate_fail; + if (__isolate_lru_page_prepare(page, isolate_mode) != 0) + goto isolate_fail_put; + /* Try isolate the page */ if (!TestClearPageLRU(page)) goto isolate_fail_put;