From patchwork Wed Oct 27 19:52:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 12587985 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3247DC433FE for ; Wed, 27 Oct 2021 19:52:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BCE2660C51 for ; Wed, 27 Oct 2021 19:52:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BCE2660C51 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 46F74940007; Wed, 27 Oct 2021 15:52:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 41F8B6B0072; Wed, 27 Oct 2021 15:52:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BF7D940007; Wed, 27 Oct 2021 15:52:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0237.hostedemail.com [216.40.44.237]) by kanga.kvack.org (Postfix) with ESMTP id 0635A6B0071 for ; Wed, 27 Oct 2021 15:52:25 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 95D3A39F26 for ; Wed, 27 Oct 2021 19:52:25 +0000 (UTC) X-FDA: 78743264250.02.7064CB3 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf25.hostedemail.com (Postfix) with ESMTP id BDA76B00018A for ; Wed, 27 Oct 2021 19:52:18 +0000 (UTC) Received: by mail-pj1-f52.google.com with SMTP id gn3so2933254pjb.0 for ; Wed, 27 Oct 2021 12:52:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=5sUNlB/oeM7v7Lajc1viu0P4O/wTbw1CkB2X041cqCg=; b=kN+rFpi6Xvrq4I1a0elUch1TmP+0VipuO4agksFf6bPJaJ44eZ7R8sLkFRm1/jEuht EB/9egA/hDHzDWAP9vIdLzBgs/TwmiPpnhSO9DgIStJ8eupi2EGvHzIu5MDpYTcJz4/D jIOVplfvUDIZFXG7UhrrIhFo5lCHm8YJF35wKGev+i+ldz6sY3qOPtufj9Pf53IRNIyV OFNFY9VcHXYBJoMZh9Z0J9IRP1zl5LULq6GpkjFajonSfmIN4gg59HqEVT6yOCoq3b0u m9jadSPrK+pYgArBn4D0PrBCDoUDJL3zt+U6YZgJqtv/EQ3d+P7pSHa1uN4RAVxSy4IM gOuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=5sUNlB/oeM7v7Lajc1viu0P4O/wTbw1CkB2X041cqCg=; b=V+aAoRw34gowG//gFpUQ+bDEj63TLRSxpaqVeABHo1WGi8Kn+GyZYYQRnvIJO4AyGA S8Xcjxli/8Ssa4ETcNDS1jgPCDnuc7QhvOWGHoC52fizotafKJI3fjt8sI8Bayk7es25 zDtoAhWa351BEi35NAFZ4JBxVff+WVGvP8+7Yt7oL12UZs6ENDPbJXhe8Kj0FDIZqpT5 lPQBQnk7bNTz2/tTXhhWMAdrQcrSrePp69OY1noHCiRMrId7h3U0MHD737A1LMVK3EPS mzQ2zD4iGaGH78XJ74E7iCG1DSTPHCEniB/CkiBBC/g8yKz2JeMnd8lw8JYJS7eOefkm 9aSQ== X-Gm-Message-State: AOAM530cb9bu0bxyq2vqa75TyLThaBf2jo8okh+lsK0p/97F6CcmvnaU 24z3HIE13rqcWuUvCkFq9w4= X-Google-Smtp-Source: ABdhPJxnhGGhyOcAl898TGV06elPp3eS65A6ugFkxNlpR9hmUWsA3aFLPh0FJumtdnL263vo43m+6Q== X-Received: by 2002:a17:903:1cd:b0:140:43f0:353c with SMTP id e13-20020a17090301cd00b0014043f0353cmr23652935plh.81.1635364344086; Wed, 27 Oct 2021 12:52:24 -0700 (PDT) Received: from localhost.localdomain (c-73-93-239-127.hsd1.ca.comcast.net. [73.93.239.127]) by smtp.gmail.com with ESMTPSA id nr14sm509433pjb.24.2021.10.27.12.52.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Oct 2021 12:52:23 -0700 (PDT) From: Yang Shi To: hughd@google.com, sunhao.th@gmail.com, willy@infradead.org, kirill.shutemov@linux.intel.com, songliubraving@fb.com, andrea.righi@canonical.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH] mm: khugepaged: skip huge page collapse for special files Date: Wed, 27 Oct 2021 12:52:21 -0700 Message-Id: <20211027195221.3825-1-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-Stat-Signature: oiifj5kxbg7kjacu8t6usd5iy1tbkkmo Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=kN+rFpi6; spf=pass (imf25.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.52 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: BDA76B00018A X-HE-Tag: 1635364338-576282 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: The read-only THP for filesystems would collapse THP for file opened readonly and mapped with VM_EXEC, the intended usecase is to avoid TLB miss for large text segment. But it doesn't restrict the file types so THP could be collapsed for non-regular file, for example, block device, if it is opened readonly and mapped with EXEC permission. This may cause bugs, like [1] and [2]. This is definitely not intended usecase, so just collapsing THP for regular file in order to close the attack surface. [1] https://lore.kernel.org/lkml/CACkBjsYwLYLRmX8GpsDpMthagWOjWWrNxqY6ZLNQVr6yx+f5vA@mail.gmail.com/ [2] https://lore.kernel.org/linux-mm/000000000000c6a82505ce284e4c@google.com/ Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS") Reported-by: Hao Sun Reported-by: syzbot+aae069be1de40fb11825@syzkaller.appspotmail.com Cc: Hao Sun Cc: Matthew Wilcox Cc: Kirill A. Shutemov Cc: Song Liu Cc: Andrea Righi Cc: Signed-off-by: Hugh Dickins Signed-off-by: Yang Shi --- The patch is basically based off the proposal from Hugh (https://lore.kernel.org/linux-mm/a07564a3-b2fc-9ffe-3ace-3f276075ea5c@google.com/). It seems Hugh is too busy to prepare the patch for formal submission (I didn't hear from him by pinging him a couple of times on mailing list), so I prepared the patch and added his SOB. mm/khugepaged.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 045cc579f724..e91b7271275e 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -445,22 +445,25 @@ static bool hugepage_vma_check(struct vm_area_struct *vma, if (!transhuge_vma_enabled(vma, vm_flags)) return false; - /* Enabled via shmem mount options or sysfs settings. */ - if (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) { + if (vma->vm_file) return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff, HPAGE_PMD_NR); - } + + /* Enabled via shmem mount options or sysfs settings. */ + if (shmem_file(vma->vm_file)) + return shmem_huge_enabled(vma); /* THP settings require madvise. */ if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) return false; - /* Read-only file mappings need to be aligned for THP to work. */ + /* Only regular file is valid */ if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && vma->vm_file && - !inode_is_open_for_write(vma->vm_file->f_inode) && (vm_flags & VM_EXEC)) { - return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff, - HPAGE_PMD_NR); + struct inode *inode = vma->vm_file->f_inode; + + return !inode_is_open_for_write(inode) && + S_ISREG(inode->i_mode); } if (!vma->anon_vma || vma->vm_ops)