Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp4357519pxb; Mon, 27 Sep 2021 15:26:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVYb5ixedTulaUw9AO54qOBC70SAOwzcFtbZ7+4Z/2SrpKkBbrL99nnMA144dPbvBueoRg X-Received: by 2002:a65:664f:: with SMTP id z15mr1696980pgv.252.1632781560906; Mon, 27 Sep 2021 15:26:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632781560; cv=none; d=google.com; s=arc-20160816; b=MlcV35AXSRIxbLY+G/0uiJ/rrBQiDmwGPTn59u/dA8KlqogkKVNihli9vmYOfRp8fe PLwkWltO/gKISNoUD4koXc5Arde0XgHTgtb2sSkr+dGykkT3/hqajj162xe03u5ycrUn /bw6Err8x5EBEnNSDo4Is1DAiDueR7JaMRbQckxeDgouOvOhvjbThrJ6rT9Y15BavPsW wYZo7A/tnMOzf7RRzwTY16Hdr+BEIX3ANi8zlwe1hq/PfPbNm8qst92/xXl7udn2MPSh ZSc7lTro1g2mMaF3vJv7JbTbznP0Vtic1yXDNbque8ioJWYM00S03INuqTQDjajnh9+I pB5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=0FTR5ajT2Fzsl3SrTT1IooSz56gne+B8P4FmdbVNapM=; b=Ej9MqR4ke1QVJtFCQWz7VnircDeBWybC959kFU56/CMa1nYkquIYznbS3+CRJwUjg3 pDkPvnWqMc6ZtcH98ql7gBVujac64NoyHXOlEmm3srPcid2LY+jLVUsV3xVvFtcWizhT UhyxuTYPxMdmIJrNC2/GzgPyK3LoYkhgyqa5e3NdiGH9njJEm0HatLapJB1RGpD/StOQ ZTQaz6Jqegj1EgMvUlVkFBtOv4V3JxnHL+A7vnekWoXdCTzvgCCZ38/UMEWP2GULnTAz BL/ToKqDF01h4eALzWXn14LDSBALGsR6Ip8s/ZO5kyDEunzw256oB0LjR06qwY6nQpxk R1GQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Bf+EOvNd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lr2si1225787pjb.12.2021.09.27.15.25.48; Mon, 27 Sep 2021 15:26:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Bf+EOvNd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237545AbhI0W0k (ORCPT + 99 others); Mon, 27 Sep 2021 18:26:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:49078 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237444AbhI0W0i (ORCPT ); Mon, 27 Sep 2021 18:26:38 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D79B661058 for ; Mon, 27 Sep 2021 22:24:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1632781499; bh=TyKEhPGij+CGTbxdLG5vYotUYB4WHx9ib8bzmK1W8v4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Bf+EOvNdmkZ32sSz5ALoF7qIusFlbGALBf8935PjknFf/SHPszFTX0YkCyxt38X1A pbG+dpt+FGsS0Y7aMTSz5hC/PuIYikOSOUj7UKtvcAVnji+UiC5BHC9gqFg5SHUF4s SECd94rStr7A1XuWeVlrjpiL+D+kEAGPM7iJdcyDXigExE8Z56bo6KcTM/UXYLQB5L QgFTQOF49SyKxSXl8mVSX1fYyM3ilqigdGosIRixRjnKgiG8luh7gnVQAwGudFi4rE 6cnQJjmlDSHiO2TKleEB5WphAeR1M6MsVGR3HTX6WDhNpaYPBhn6XDywCmh2434oHb e6oL7b15x1Ufg== Received: by mail-lf1-f49.google.com with SMTP id x27so84072194lfu.5 for ; Mon, 27 Sep 2021 15:24:59 -0700 (PDT) X-Gm-Message-State: AOAM531PjYTt9edssNIUMZWkDDaiVOrKHZOvdyjeSIJvQshAHUBqLaE0 oT1xOj1n4RKqmabU2Pg5A/kt8TiUlCawtjECPfI= X-Received: by 2002:a05:6512:39c4:: with SMTP id k4mr2231945lfu.14.1632781498176; Mon, 27 Sep 2021 15:24:58 -0700 (PDT) MIME-Version: 1.0 References: <20210906121200.57905-1-rongwei.wang@linux.alibaba.com> <20210922070645.47345-2-rongwei.wang@linux.alibaba.com> <20210923194343.ca0f29e1c4d361170343a6f2@linux-foundation.org> <9e41661d-9919-d556-8c49-610dae157553@linux.alibaba.com> In-Reply-To: <9e41661d-9919-d556-8c49-610dae157553@linux.alibaba.com> From: Song Liu Date: Mon, 27 Sep 2021 15:24:47 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2 1/2] mm, thp: check page mapping when truncating page cache To: Rongwei Wang Cc: Andrew Morton , Matthew Wilcox , Linux MM , Linux Kernel Mailing List , William Kucharski , Hugh Dickins Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 24, 2021 at 12:12 AM Rongwei Wang wrote: > > > > On 9/24/21 10:43 AM, Andrew Morton wrote: > > On Thu, 23 Sep 2021 01:04:54 +0800 Rongwei Wang wrote: > > > >> > >> > >>> On Sep 22, 2021, at 7:37 PM, Matthew Wilcox wro= te: > >>> > >>> On Wed, Sep 22, 2021 at 03:06:44PM +0800, Rongwei Wang wrote: > >>>> Transparent huge page has supported read-only non-shmem files. The f= ile- > >>>> backed THP is collapsed by khugepaged and truncated when written (fo= r > >>>> shared libraries). > >>>> > >>>> However, there is race in two possible places. > >>>> > >>>> 1) multiple writers truncate the same page cache concurrently; > >>>> 2) collapse_file rolls back when writer truncates the page cache; > >>> > >>> As I've said before, the bug here is that somehow there is a writable= fd > >>> to a file with THPs. That's what we need to track down and fix. > >> Hi, Matthew > >> I am not sure get your means. We know =E2=80=9Cmm, thp: relax the VM_D= ENYWRITE constraint on file-backed THPs" > >> Introduced file-backed THPs for DSO. It is possible {very rarely} for = DSO to be opened in writeable way. > >> > >> ... > >> > >>> https://lore.kernel.org/linux-mm/YUdL3lFLFHzC80Wt@casper.infradead.or= g/ > >> All in all, what you mean is that we should solve this race at the sou= rce? > > > > Matthew is being pretty clear here: we shouldn't be permitting > > userspace to get a writeable fd for a thp-backed file. > > > > Why are we permitting the DSO to be opened writeably? If there's a > > legitimate case for doing this then presumably "mm, thp: relax the > There is a use case to stress file-backed THP within attachment. > I test this case in a system which has enabled CONFIG_READ_ONLY_THP_FOR_F= S: > > $ gcc -Wall -g -o stress_madvise_dso stress_madvise_dso.c > $ ulimit -s unlimited > $ ./stress_madvise_dso 10000 > > the meaning of above parameters: > 10000: the max test time; > : the DSO that will been mapped into file-backed THP by > madvise. It recommended that the text segment of DSO to be tested is > greater than 2M. > > The crash will been triggered at once in the latest kernel. And this > case also can used to trigger the bug that mentioned in our another patch= . Hmm.. I am not able to use the repro program to crash the system. Not sure what I did wrong. OTOH, does it make sense to block writes within khugepaged, like: diff --git i/mm/khugepaged.c w/mm/khugepaged.c index 045cc579f724e..ad7c41ec15027 100644 --- i/mm/khugepaged.c +++ w/mm/khugepaged.c @@ -51,6 +51,7 @@ enum scan_result { SCAN_CGROUP_CHARGE_FAIL, SCAN_TRUNCATED, SCAN_PAGE_HAS_PRIVATE, + SCAN_BUSY_WRITE, }; #define CREATE_TRACE_POINTS @@ -1652,6 +1653,11 @@ static void collapse_file(struct mm_struct *mm, /* Only allocate from the target node */ gfp =3D alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE; + if (deny_write_access(file)) { + result =3D SCAN_BUSY_WRITE; + return; + } + new_page =3D khugepaged_alloc_page(hpage, gfp, node); if (!new_page) { result =3D SCAN_ALLOC_HUGE_PAGE_FAIL; @@ -1863,19 +1869,6 @@ static void collapse_file(struct mm_struct *mm, else { __mod_lruvec_page_state(new_page, NR_FILE_THPS, nr); filemap_nr_thps_inc(mapping); - /* - * Paired with smp_mb() in do_dentry_open() to ensure - * i_writecount is up to date and the update to nr_thps is - * visible. Ensures the page cache will be truncated if the - * file is opened writable. - */ - smp_mb(); - if (inode_is_open_for_write(mapping->host)) { - result =3D SCAN_FAIL; - __mod_lruvec_page_state(new_page, NR_FILE_THPS, -nr= ); - filemap_nr_thps_dec(mapping); - goto xa_locked; - } } if (nr_none) { @@ -1976,6 +1969,7 @@ static void collapse_file(struct mm_struct *mm, VM_BUG_ON(!list_empty(&pagelist)); if (!IS_ERR_OR_NULL(*hpage)) mem_cgroup_uncharge(*hpage); + allow_write_access(file); /* TODO: tracepoints */ }