From patchwork Tue Oct 22 19:10:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 11205213 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 77CA5139A for ; Tue, 22 Oct 2019 19:10:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 443FC21872 for ; Tue, 22 Oct 2019 19:10:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="VJJXGG+6" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 443FC21872 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=fb.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 66E556B0007; Tue, 22 Oct 2019 15:10:23 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 61EAD6B0008; Tue, 22 Oct 2019 15:10:23 -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 534416B000A; Tue, 22 Oct 2019 15:10:23 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0097.hostedemail.com [216.40.44.97]) by kanga.kvack.org (Postfix) with ESMTP id 31FF46B0007 for ; Tue, 22 Oct 2019 15:10:23 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with SMTP id CE1C9180AD804 for ; Tue, 22 Oct 2019 19:10:22 +0000 (UTC) X-FDA: 76072361484.14.trip79_3d73acca10c20 X-Spam-Summary: 2,0,0,84023d828da440a2,d41d8cd98f00b204,prvs=5198c5fe81=songliubraving@fb.com,:linux-kernel@vger.kernel.org::akpm@linux-foundation.org:matthew.wilcox@oracle.com:kernel-team@fb.com:william.kucharski@oracle.com:kirill.shutemov@linux.intel.com:songliubraving@fb.com:hughd@google.com,RULES_HIT:41:69:355:379:421:541:800:960:968:973:988:989:1260:1261:1277:1313:1314:1345:1431:1437:1516:1518:1535:1543:1711:1730:1747:1777:1792:2393:2553:2559:2562:2693:3138:3139:3140:3141:3142:3355:3865:3867:3868:3870:3871:3872:4117:4250:4321:4419:5007:6119:6261:6653:7903:8957:10004:10400:10450:10455:11026:11232:11473:11658:11914:12043:12291:12296:12297:12438:12555:12679:12683:12895:13161:13184:13229:14096:14097:14181:14394:14721:19904:19999:21080:21324:21433:21451:21627:21740:30054:30064:30070:30090,0,RBL:67.231.153.30:@fb.com:.lbl8.mailshell.net-62.2.0.100 64.100.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0 :0,LFtim X-HE-Tag: trip79_3d73acca10c20 X-Filterd-Recvd-Size: 6348 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by imf28.hostedemail.com (Postfix) with ESMTP for ; Tue, 22 Oct 2019 19:10:22 +0000 (UTC) Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id x9MJ86ED030247 for ; Tue, 22 Oct 2019 12:10:21 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=K7W6Cq68GUV+oF3NKZ7ALy9xAKZZYUngRToXuNpGikY=; b=VJJXGG+6FUX+J0ijs1vIb56b2s8RSj2WQbPQlx7XgGFUHcqq8DARUFJLdXjo/5LWkztS hU5vqwmgvgW4+Ga3Q6wQj2ennWK26+FXBrgBvoPAGTWCfeMzS6uiNhv3m2FW0Mwf26DT iXwaw/tli2P6MRQPuaqQ4g2z6zkUQc5eAs8= Received: from mail.thefacebook.com (mailout.thefacebook.com [199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 2vt4ysrvwu-16 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Tue, 22 Oct 2019 12:10:21 -0700 Received: from 2401:db00:30:600c:face:0:39:0 (2620:10d:c081:10::13) by mail.thefacebook.com (2620:10d:c081:35::125) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1713.5; Tue, 22 Oct 2019 12:10:17 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 27F5A62E1F04; Tue, 22 Oct 2019 12:10:17 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Song Liu Smtp-Origin-Hostname: devbig006.ftw2.facebook.com To: , , CC: , , , , Song Liu , Hugh Dickins Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH v4] mm,thp: recheck each page before collapsing file THP Date: Tue, 22 Oct 2019 12:10:06 -0700 Message-ID: <20191022191006.411277-1-songliubraving@fb.com> X-Mailer: git-send-email 2.17.1 X-FB-Internal: Safe MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.95,1.0.8 definitions=2019-10-22_03:2019-10-22,2019-10-22 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 adultscore=0 spamscore=0 mlxscore=0 clxscore=1015 bulkscore=0 impostorscore=0 suspectscore=0 lowpriorityscore=0 phishscore=0 mlxlogscore=999 malwarescore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1908290000 definitions=main-1910220160 X-FB-Internal: deliver 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: In collapse_file(), for !is_shmem case, current check cannot guarantee the locked page is up-to-date. Specifically, xas_unlock_irq() should not be called before lock_page() and get_page(); and it is necessary to recheck PageUptodate() after locking the page. With this bug and CONFIG_READ_ONLY_THP_FOR_FS=y, madvise(HUGE)'ed .text may contain corrupted data. This is because khugepaged mistakenly collapses some not up-to-date sub pages into a huge page, and assumes the huge page is up-to-date. This will NOT corrupt data in the disk, because the page is read-only and never written back. Fix this by properly checking PageUptodate() after locking the page. This check replaces "VM_BUG_ON_PAGE(!PageUptodate(page), page);". Also, move PageDirty() check after locking the page. Current khugepaged only collapse read-only .text. Therefore, the page could only be dirty if it hasn't been flushed since first write. In such case, calls filemap_flush() and defer the collapse. Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS") Cc: Kirill A. Shutemov Cc: Hugh Dickins Cc: William Kucharski Cc: Andrew Morton Acked-by: Johannes Weiner Signed-off-by: Song Liu --- Changes v1 => v2: Rearrange the checks per feedback from Johannes, Rik, and Kirill. Changes v2 => v3: Remove redudant checks before trylock_page(). Changes v3 => v4: Rewrite commit log. Trigger filemap_flush() for PageDirty() case. This covers one-off situation, where the file hasn't been flushed since first write. --- mm/khugepaged.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 0a1b4b484ac5..cd480dce92c6 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1601,17 +1601,6 @@ static void collapse_file(struct mm_struct *mm, result = SCAN_FAIL; goto xa_unlocked; } - } else if (!PageUptodate(page)) { - xas_unlock_irq(&xas); - wait_on_page_locked(page); - if (!trylock_page(page)) { - result = SCAN_PAGE_LOCK; - goto xa_unlocked; - } - get_page(page); - } else if (PageDirty(page)) { - result = SCAN_FAIL; - goto xa_locked; } else if (trylock_page(page)) { get_page(page); xas_unlock_irq(&xas); @@ -1626,7 +1615,12 @@ static void collapse_file(struct mm_struct *mm, * without racing with truncate. */ VM_BUG_ON_PAGE(!PageLocked(page), page); - VM_BUG_ON_PAGE(!PageUptodate(page), page); + + /* double check the page is up to date */ + if (unlikely(!PageUptodate(page))) { + result = SCAN_FAIL; + goto out_unlock; + } /* * If file was truncated then extended, or hole-punched, before @@ -1642,6 +1636,24 @@ static void collapse_file(struct mm_struct *mm, goto out_unlock; } + if (!is_shmem && PageDirty(page)) { + /* + * khugepaged only works on read-only fd, so this + * page is dirty because it hasn't been flushed + * since first write. There won't be new dirty + * pages. + * + * Trigger async flush here and hope the writeback + * is done when khugepaged revisits this page. + * + * This is a one-off situation. We are not forcing + * writeback in loop. + */ + filemap_flush(mapping); + result = SCAN_FAIL; + goto out_unlock; + } + if (isolate_lru_page(page)) { result = SCAN_DEL_PAGE_LRU; goto out_unlock;