From mboxrd@z Thu Jan 1 00:00:00 1970 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 F362CC433EF for ; Wed, 27 Oct 2021 20:44:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6D50560C4A for ; Wed, 27 Oct 2021 20:44:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6D50560C4A 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 EE1876B0071; Wed, 27 Oct 2021 16:44:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E90126B0072; Wed, 27 Oct 2021 16:44:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D32746B0073; Wed, 27 Oct 2021 16:44:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0055.hostedemail.com [216.40.44.55]) by kanga.kvack.org (Postfix) with ESMTP id AD9966B0071 for ; Wed, 27 Oct 2021 16:44:51 -0400 (EDT) Received: from smtpin40.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 4FB4A8249980 for ; Wed, 27 Oct 2021 20:44:51 +0000 (UTC) X-FDA: 78743396382.40.52FC7C6 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) by imf01.hostedemail.com (Postfix) with ESMTP id 74C8D50898D8 for ; Wed, 27 Oct 2021 20:44:43 +0000 (UTC) Received: by mail-ed1-f51.google.com with SMTP id h7so16002382ede.8 for ; Wed, 27 Oct 2021 13:44:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=m5TrXx7dXH1SiYJA1++6NEnaXYdCWIdktAUywsE4RX0=; b=akIAcR7MWOhUON05qRVFJjbKRt/PMu3/E/9T+JxeU7Cdm1VuLz4d/LGvKfG3S/a0fH LNEB27OEF7na0VvowXEp5i9bwoRpyEPrma4PibNukFiofQu557Y/4wU8Hng5UwMSoyWW PxBoS8W00M5/W4/6r1sbT7LYMdIBK3zJh7suxGcvEnOryHOP5EWF/k6Le1dD/ugLCvB6 7zMz/0OT2BcEIPGSVm/VGInyDQwhpWBshY4Ht0RIZOdxyI9jFPRjlXgqnLNU98shbfg6 LAkTSeyrZ+C/1/x7s/LevolGNlDjp25qerm2zFuxVeEsY4caEgsYrxIR9ks6i7n+l6Yc lsVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=m5TrXx7dXH1SiYJA1++6NEnaXYdCWIdktAUywsE4RX0=; b=lP+EPwlt9PDFVh7NlaGFrRplkfbi+IEI67gKPWD3EZMinb4AcxK9w2Oe+A96gmrMJj cTIecnpIX4rnaDx/XBUagXQnPF7b8I2C4WUbwP/+weQaeZq75OR2cAIr2kyrHUpuCUgc Yq1L5vqugKrFJDw4HmrFyfnNOAjbMewDi2U9OKpTZeJwFVJX98f1ztAbWDOciI0yaU1X O0gK1QcrOfM0S05XNUTf0Ly4+3vKO1LaQSRSxx2Eq+fGxuB2rv7FvNZ8GkvcPJEzHE/U 53GOVmoRwJ3D1wulOxaUdp0yKZDrPSKa3YkqnMt4N16gcygU92M6Rcqasb2XmULhleSH jmdA== X-Gm-Message-State: AOAM5339Y7oNTJbUO55idpHWdF1BjOT6peo01t5JFT/wMh312iipe9dm Wlz4B8P/5mx0DMthNte+CI3+0RDzSLFtZGXZ0P4= X-Google-Smtp-Source: ABdhPJyrKkbFwY0S008XBn0rgHTRoSBKFT0NjTdmm5C5y13au5Cwmp9tzLsUj6X6JjZVTGAd7to5BEcXMZU5uJb3Huc= X-Received: by 2002:aa7:de83:: with SMTP id j3mr211527edv.312.1635367489683; Wed, 27 Oct 2021 13:44:49 -0700 (PDT) MIME-Version: 1.0 References: <20211027195221.3825-1-shy828301@gmail.com> In-Reply-To: From: Yang Shi Date: Wed, 27 Oct 2021 13:44:37 -0700 Message-ID: Subject: Re: [PATCH] mm: khugepaged: skip huge page collapse for special files To: Song Liu Cc: Hugh Dickins , "sunhao.th@gmail.com" , Matthew Wilcox , "Kirill A. Shutemov" , "andrea.righi@canonical.com" , Andrew Morton , Linux Memory Management List , "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: mfuwc8sjusgwipawxq383ayxr6r5896q Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=akIAcR7M; spf=pass (imf01.hostedemail.com: domain of shy828301@gmail.com designates 209.85.208.51 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 74C8D50898D8 X-HE-Tag: 1635367483-86652 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: On Wed, Oct 27, 2021 at 1:35 PM Song Liu wrote: > > > > > On Oct 27, 2021, at 12:52 PM, Yang Shi wrote: > > > > 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); > > Am I misreading this? If we return here for vma->vm_file, the following > logic (shmem_file(), etc.) would be skipped, no? Oh, yes, you are right. My mistake. Andrew, Could you please apply the below fix? diff --git a/mm/khugepaged.c b/mm/khugepaged.c index e91b7271275e..26f1798c88d2 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -445,9 +445,9 @@ static bool hugepage_vma_check(struct vm_area_struct *vma, if (!transhuge_vma_enabled(vma, vm_flags)) return false; - if (vma->vm_file) - return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff, - HPAGE_PMD_NR); + if (vma->vm_file && !IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - + vma->vm_pgoff, HPAGE_PMD_NR)) + return false; /* Enabled via shmem mount options or sysfs settings. */ if (shmem_file(vma->vm_file)) > > Thanks, > Song > > > - } > > + > > + /* 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) > > -- > > 2.26.2 > > >