From patchwork Tue May 12 21:03:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 11543993 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 41D0790 for ; Tue, 12 May 2020 21:03:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E6C4220769 for ; Tue, 12 May 2020 21:03:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E6C4220769 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E18589000F2; Tue, 12 May 2020 17:03:15 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id DA2C1900036; Tue, 12 May 2020 17:03:15 -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 C69E49000F2; Tue, 12 May 2020 17:03:15 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0169.hostedemail.com [216.40.44.169]) by kanga.kvack.org (Postfix) with ESMTP id ABBE6900036 for ; Tue, 12 May 2020 17:03:15 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 651788248D51 for ; Tue, 12 May 2020 21:03:15 +0000 (UTC) X-FDA: 76809292350.03.jelly37_3962bbbba6206 X-Spam-Summary: 2,0,0,6296b15a7dd9c991,d41d8cd98f00b204,yang.shi@linux.alibaba.com,,RULES_HIT:41:355:379:541:960:968:973:988:989:1260:1261:1345:1437:1535:1544:1605:1711:1730:1747:1777:1792:1801:2393:2553:2559:2562:2693:3138:3139:3140:3141:3142:3608:3865:3867:3868:3870:3871:3872:3874:4250:4321:4605:5007:6119:6120:6261:7903:8957:9121:9592:10004:11026:11233:11473:11658:11914:12043:12295:12296:12297:12438:12555:12679:12895:12986:13095:13161:13229:13255:13870:14096:14181:14721:21060:21080:21212:21433:21451:21611:21627:21660:21990:30054:30070:30090,0,RBL:115.124.30.130:@linux.alibaba.com:.lbl8.mailshell.net-64.201.201.201 62.20.2.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:25,LUA_SUMMARY:none X-HE-Tag: jelly37_3962bbbba6206 X-Filterd-Recvd-Size: 5996 Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) by imf39.hostedemail.com (Postfix) with ESMTP for ; Tue, 12 May 2020 21:03:13 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e01358;MF=yang.shi@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0TyNQql1_1589317383; Received: from localhost(mailfrom:yang.shi@linux.alibaba.com fp:SMTPD_---0TyNQql1_1589317383) by smtp.aliyun-inc.com(127.0.0.1); Wed, 13 May 2020 05:03:11 +0800 From: Yang Shi To: kirill.shutemov@linux.intel.com, akpm@linux-foundation.org Cc: yang.shi@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC linux-next PATCH] mm: khugepaged: remove error message when checking external pins Date: Wed, 13 May 2020 05:03:03 +0800 Message-Id: <1589317383-9595-1-git-send-email-yang.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 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: When running khugepaged with higher frequency (for example, set scan_sleep_millisecs to 0), the below error message was reported: khugepaged: expected_refcount (1024) > refcount (512) page:ffffd75784258000 count:511 mapcount:1 mapping:ffff968de06c7421 index:0x7fa288600 compound_mapcount: 0 flags: 0x17fffc00009003c(uptodate|dirty|lru|active|head|swapbacked) raw: 017fffc00009003c ffffd7578ba70788 ffffd7578bdb5148 ffff968de06c7421 raw: 00000007fa288600 0000000000000000 000001ff00000000 ffff968e5e7d6000 page dumped because: Unexpected refcount page->mem_cgroup:ffff968e5e7d6000 This is introduced by allowing collapsing fork shared and PTE-mapped THPs. The check may run into the below race: Assuming parent process forked child process, then they do CPU A CPU B CPU C ----- ----- ----- Parent Child khugepaged MADV_DONTNEED split huge pmd Double mapped MADV_DONTNEED zap_huge_pmd remove_page_rmap Clear double map khugepaged_scan_pmd(parent) check mapcount and refcount --> total_mapcount > refcount dec mapcount The issue can be reproduced by the below test program. ---8<--- void main() { void *addr; int ret; pid_t pid; addr = memalign(ALIGN, 2 * 1024 * 1024); if (!addr) { printf("malloc failed\n"); return; } ret = madvise(addr, 2 * 1024 * 1024, MADV_HUGEPAGE); if (ret < 0) { printf("madvise failed\n"); return; } memset(addr, 0xdeadbeef, 2 * 1024 * 1024); pid = fork(); if (pid == 0) { /* Child process */ ret = madvise(addr + (2 * 1024 * 1024) - 4096, 4096, MADV_DONTNEED); if (ret < 0) { printf("madvise failed in child\n"); return; } sleep(120); } else if (pid > 0) { sleep(5); /* Parent process */ ret = madvise(addr, 2 * 1024 * 1024, MADV_DONTNEED); if (ret < 0) { printf("madvise failed in parent\n"); return; } } else { printf("fork failed\n"); return; } sleep(120); } ---8<--- So, total_mapcount > refcount seems not unexpected due to the inherent race. Removed the error message even though it is protected by CONFIG_VM_DEBUG since we have to live with the race and AFAIK some distros may have CONFIG_VM_DEBUG enabled dy default. Since such case is ephemeral we could always try collapse the area again later, so it sounds not harmful. But, it might report false positive if the page has excessive GUP pins (i.e. 512), however it might be not that bad since the same check will be done later. I didn't figure out a simple way to prevent the false positive. Added some notes to elaborate the race and the consequence as well. Cc: Kirill A. Shutemov Signed-off-by: Yang Shi Acked-by: Kirill A. Shutemov --- mm/khugepaged.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 1fdd677..048f5d4 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -602,12 +602,6 @@ static bool is_refcount_suitable(struct page *page) if (PageSwapCache(page)) expected_refcount += compound_nr(page); - if (IS_ENABLED(CONFIG_DEBUG_VM) && expected_refcount > refcount) { - pr_err("expected_refcount (%d) > refcount (%d)\n", - expected_refcount, refcount); - dump_page(page, "Unexpected refcount"); - } - return page_count(page) == expected_refcount; } @@ -1341,7 +1335,23 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, goto out_unmap; } - /* Check if the page has any GUP (or other external) pins */ + /* + * Check if the page has any GUP (or other external) pins. + * + * Here the check is racy it may see totmal_mapcount > refcount + * in some cases. + * For example, one process with one forked child process. + * The parent has the PMD split due to MADV_DONTNEED, then + * the child is trying unmap the whole PMD, but khugepaged + * may be scanning the parent between the child has + * PageDoubleMap flag cleared and dec the mapcount. So + * khugepaged may see total_mapcount > refcount. + * + * But such case is ephemeral we could always retry collapse + * later. However it may report false positive if the page + * has excessive GUP pins (i.e. 512). Anyway the same check + * will be done again later the risk seems low. + */ if (!is_refcount_suitable(page)) { result = SCAN_PAGE_COUNT; goto out_unmap;