From patchwork Wed Nov 6 06:09:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 11229359 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 5A63015AB for ; Wed, 6 Nov 2019 06:09:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EF8E8214D8 for ; Wed, 6 Nov 2019 06:09:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="aWd2ZJxE" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EF8E8214D8 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 406616B0006; Wed, 6 Nov 2019 01:09:57 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3B6976B000A; Wed, 6 Nov 2019 01:09:57 -0500 (EST) 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 2CCB36B000E; Wed, 6 Nov 2019 01:09:57 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0234.hostedemail.com [216.40.44.234]) by kanga.kvack.org (Postfix) with ESMTP id 176AB6B0006 for ; Wed, 6 Nov 2019 01:09:57 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with SMTP id B95C5180AD802 for ; Wed, 6 Nov 2019 06:09:56 +0000 (UTC) X-FDA: 76124826792.24.hook39_56b9babaef907 X-Spam-Summary: 2,0,0,035ec78cbd630242,d41d8cd98f00b204,prvs=6213cc43f4=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:hannes@cmpxchg.org:hughd@google.com,RULES_HIT:41:69:355:379:421:541:800:960:967:973:988:989:1260:1261:1263:1277:1313:1314:1345:1359:1431:1437:1516:1518:1535:1543:1711:1730:1747:1777:1792:2393:2525:2553:2559:2566:2682:2685:2693:2859:2902:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3355:3865:3866:3867:3868:3870:3871:3872:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4117:4250:4321:5007:6119:6261:6653:7903:8599:8957:9025:10004:10400:10913:11026:11232:11473:11658:11914:12043:12296:12297:12438:12555:12679:12783:12895:12986:13161:13221:13229:14096:14097:14181:14394:14721:14849:21080:21324:21433:21451:21627:21740:21749:21811:21939:30054:30064:30070:30090,0,RBL:error,CacheIP:none, Bayesian X-HE-Tag: hook39_56b9babaef907 X-Filterd-Recvd-Size: 6578 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Wed, 6 Nov 2019 06:09:55 +0000 (UTC) Received: from pps.filterd (m0109334.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id xA6648Kc009778 for ; Tue, 5 Nov 2019 22:09:54 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=ZNEK/2HxCUlHQJ5GI8dDxUQNL+Ty25u3PZmn8QkENtk=; b=aWd2ZJxEnjwy9mWP0mLdjqoDoSC7tBju9kJkVFyv3qvQvMZjsR88H8yginXnZ7BXVcCV WMElqehGQJuHsd0eri/rkc0B5sV6c0rNvhUzLg27TdaQozq6uwNwsdLJiR/PxCiSgZ/8 0uwlkvTDYVlkPfr3i1jUxnWsVvWtjf34y4A= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 2w3fcy2n19-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 05 Nov 2019 22:09:54 -0800 Received: from 2401:db00:30:6012:face:0:17:0 (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1713.5; Tue, 5 Nov 2019 22:09:53 -0800 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id DAB0162E36EB; Tue, 5 Nov 2019 22:09:51 -0800 (PST) Smtp-Origin-Hostprefix: devbig From: Song Liu Smtp-Origin-Hostname: devbig006.ftw2.facebook.com To: , , CC: , , , , Song Liu , Johannes Weiner , Hugh Dickins Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH v5 1/2] mm,thp: recheck each page before collapsing file THP Date: Tue, 5 Nov 2019 22:09:29 -0800 Message-ID: <20191106060930.2571389-2-songliubraving@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191106060930.2571389-1-songliubraving@fb.com> References: <20191106060930.2571389-1-songliubraving@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.95,18.0.572 definitions=2019-11-06_01:2019-11-05,2019-11-06 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 bulkscore=0 lowpriorityscore=0 suspectscore=0 mlxlogscore=999 phishscore=0 clxscore=1015 mlxscore=0 impostorscore=0 malwarescore=0 spamscore=0 adultscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1908290000 definitions=main-1911060063 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 should not try to collapse dirty file THP, because it is limited to read-only .text. The only case we hit a dirty page here is when the page hasn't been written since write. Bail out and retry when this happens. syzbot reported bug on previous version of this patch. Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS") Reported-by: syzbot+efb9e48b9fbdc49bb34a@syzkaller.appspotmail.com Cc: Johannes Weiner Cc: Kirill A. Shutemov Cc: Hugh Dickins Cc: William Kucharski Cc: Andrew Morton Signed-off-by: Song Liu --- [songliubraving@fb.com: v4] Link: http://lkml.kernel.org/r/20191022191006.411277-1-songliubraving@fb.com [songliubraving@fb.com: fix deadlock in collapse_file()] Link: http://lkml.kernel.org/r/20191028221414.3685035-1-songliubraving@fb.com Link: http://lkml.kernel.org/r/20191018180345.4188310-1-songliubraving@fb.com [songliubraving@fb.com: flush file for !is_shmem PageDirty() case in collapse_file()] https://lkml.kernel.org/linux-mm/20191030200736.3455046-1-songliubraving@fb.com/ --- mm/khugepaged.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) -- 2.17.1 diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 0a1b4b484ac5..40215795d641 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); + + /* make sure 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,16 @@ 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. + */ + result = SCAN_FAIL; + goto out_unlock; + } + if (isolate_lru_page(page)) { result = SCAN_DEL_PAGE_LRU; goto out_unlock; From patchwork Wed Nov 6 06:09:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 11229363 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 A6CE71515 for ; Wed, 6 Nov 2019 06:12:08 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5F9942178F for ; Wed, 6 Nov 2019 06:12:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="nbZZHlW7" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5F9942178F 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 B7ED06B0006; Wed, 6 Nov 2019 01:12:07 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B2F516B000A; Wed, 6 Nov 2019 01:12:07 -0500 (EST) 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 A1D136B000E; Wed, 6 Nov 2019 01:12:07 -0500 (EST) 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 8E7686B0006 for ; Wed, 6 Nov 2019 01:12:07 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with SMTP id 3A250180AD802 for ; Wed, 6 Nov 2019 06:12:07 +0000 (UTC) X-FDA: 76124832294.07.beast10_69c3e74165527 X-Spam-Summary: 2,0,0,3063f81b826621f7,d41d8cd98f00b204,prvs=6213cc43f4=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:hannes@cmpxchg.org:willy@infradead.org,RULES_HIT:41:355:379:541:800:960:967:973:988:989:1260:1261:1277:1313:1314:1345:1359:1431:1437:1516:1518:1534:1541:1711:1730:1747:1777:1792:2393:2525:2559:2563:2682:2685:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3353:3865:3867:3868:3870:3871:3872:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4321:5007:6261:6653:8660:8957:9025:10004:10400:10450:10455:11026:11658:11914:12043:12296:12297:12555:12895:12986:13069:13148:13184:13229:13230:13255:13311:13357:14096:14097:14181:14394:14721:19904:19999:21080:21433:21451:21627:21740:21749:21811:30054:30064,0,RBL:error,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCac he:0,MSF X-HE-Tag: beast10_69c3e74165527 X-Filterd-Recvd-Size: 4773 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by imf07.hostedemail.com (Postfix) with ESMTP for ; Wed, 6 Nov 2019 06:12:06 +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 xA6681PS027038 for ; Tue, 5 Nov 2019 22:12:03 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=XlCuU5N/+fFIuKLh8fAov4sp+sZHEolANP8THw5ikpA=; b=nbZZHlW7kHx3YCrWxC5lC2zbb7cYvB8sYsza7H5x59KgC1HKe/W3SHipEwyHl3KgjXgi CkUUKxaI5AxDmUzrCxKz/gh00OpxLQomiTLv9MPI7lvl+Tg5LAWO/zoaH70MxJRuCNtp lVNcpJ+Zrke0MJ+cbPkRSH8eQ8JsJ4iEEDo= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 2w3fhxjhph-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 05 Nov 2019 22:12:03 -0800 Received: from 2401:db00:2120:81ca:face:0:31:0 (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::c) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1713.5; Tue, 5 Nov 2019 22:12:02 -0800 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 3348962E36EB; Tue, 5 Nov 2019 22:09:55 -0800 (PST) Smtp-Origin-Hostprefix: devbig From: Song Liu Smtp-Origin-Hostname: devbig006.ftw2.facebook.com To: , , CC: , , , , Song Liu , Hugh Dickins , Johannes Weiner , Matthew Wilcox Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH v5 2/2] mm/thp: flush file for !is_shmem PageDirty() case in collapse_file() Date: Tue, 5 Nov 2019 22:09:30 -0800 Message-ID: <20191106060930.2571389-3-songliubraving@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191106060930.2571389-1-songliubraving@fb.com> References: <20191106060930.2571389-1-songliubraving@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.95,18.0.572 definitions=2019-11-06_01:2019-11-05,2019-11-06 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 mlxscore=0 mlxlogscore=934 clxscore=1015 phishscore=0 malwarescore=0 spamscore=0 lowpriorityscore=0 priorityscore=1501 adultscore=0 suspectscore=0 impostorscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1908290000 definitions=main-1911060064 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: For non-shmem file THPs, khugepaged only collapses read only .text mapping (VM_DENYWRITE). These pages should not be dirty except the case where the file hasn't been flushed since first write. Call filemap_flush() in collapse_file() to accelerate the write back in such cases. Cc: Kirill A. Shutemov Cc: Hugh Dickins Cc: William Kucharski Cc: Johannes Weiner Cc: Andrew Morton Cc: Matthew Wilcox Signed-off-by: Song Liu --- [songliubraving@fb.com: flush file for !is_shmem PageDirty() case in collapse_file()] https://lkml.kernel.org/linux-mm/20191030200736.3455046-1-songliubraving@fb.com/ --- mm/khugepaged.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) -- 2.17.1 diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 40215795d641..513cce897794 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1601,6 +1601,24 @@ static void collapse_file(struct mm_struct *mm, result = SCAN_FAIL; goto xa_unlocked; } + } else if (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. + */ + xas_unlock_irq(&xas); + filemap_flush(mapping); + result = SCAN_FAIL; + goto xa_unlocked; } else if (trylock_page(page)) { get_page(page); xas_unlock_irq(&xas);