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 8B0C8C433FE for ; Wed, 29 Sep 2021 07:50:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 23265613D0 for ; Wed, 29 Sep 2021 07:50:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 23265613D0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 822E4940012; Wed, 29 Sep 2021 03:50:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D1FE940011; Wed, 29 Sep 2021 03:50:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C0FB940012; Wed, 29 Sep 2021 03:50:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0161.hostedemail.com [216.40.44.161]) by kanga.kvack.org (Postfix) with ESMTP id 5C45A940011 for ; Wed, 29 Sep 2021 03:50:46 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 0C4BB181F2296 for ; Wed, 29 Sep 2021 07:50:46 +0000 (UTC) X-FDA: 78639839292.20.818BD02 Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) by imf15.hostedemail.com (Postfix) with ESMTP id 7E77DD000097 for ; Wed, 29 Sep 2021 07:50:44 +0000 (UTC) X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R831e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04394;MF=rongwei.wang@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0Uq01iuM_1632901840; Received: from 30.240.96.199(mailfrom:rongwei.wang@linux.alibaba.com fp:SMTPD_---0Uq01iuM_1632901840) by smtp.aliyun-inc.com(127.0.0.1); Wed, 29 Sep 2021 15:50:41 +0800 Message-ID: Date: Wed, 29 Sep 2021 15:50:39 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:93.0) Gecko/20100101 Thunderbird/93.0 Subject: Re: [PATCH v2 1/2] mm, thp: check page mapping when truncating page cache Content-Language: en-US To: Song Liu Cc: Andrew Morton , Matthew Wilcox , Linux MM , Linux Kernel Mailing List , William Kucharski , Hugh Dickins 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> <68737431-01d2-e6e3-5131-7d7c731e49ae@linux.alibaba.com> From: Rongwei Wang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 7E77DD000097 X-Stat-Signature: igyinyjj3gsww853ir16uq7wn6whjrdx Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf15.hostedemail.com: domain of rongwei.wang@linux.alibaba.com designates 115.124.30.130 as permitted sender) smtp.mailfrom=rongwei.wang@linux.alibaba.com X-HE-Tag: 1632901844-807498 Content-Transfer-Encoding: quoted-printable 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 9/29/21 3:14 PM, Song Liu wrote: > On Tue, Sep 28, 2021 at 9:20 AM Rongwei Wang > wrote: >> >> >> >> On 9/28/21 6:24 AM, Song Liu wrote: >>> 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 = wrote: >>>>>>> >>>>>>> On Wed, Sep 22, 2021 at 03:06:44PM +0800, Rongwei Wang wrote: >>>>>>>> Transparent huge page has supported read-only non-shmem files. T= he file- >>>>>>>> backed THP is collapsed by khugepaged and truncated when written= (for >>>>>>>> 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 writ= able 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_DENYWRITE 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.infradea= d.org/ >>>>>> All in all, what you mean is that we should solve this race at the= source? >>>>> >>>>> 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_FS: >>>> >>>> $ 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. >>> >> Hi >> I have tried to check my test case again. Can you make sure the DSO th= at >> you test have THP mapping? >> >> If you are willing to try again, I can send my libtest.c which is used >> to test by myself (actually, it shouldn't be target DSO problem). >> >> Thanks very much! >>> 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_THISNOD= E; >>> >>> + if (deny_write_access(file)) { >>> + result =3D SCAN_BUSY_WRITE; >>> + return; >>> + } >>> + >> This can indeed avoid some possible races from source. >> >> But, I am thinking about whether this will lead to DDoS attack? >> I remember the reason of DSO has ignored MAP_DENYWRITE in kernel >> is that DDoS attack. In addition, 'deny_write_access' will change >> the behavior, such as user will get 'Text file busy' during >> collapse_file. I am not sure whether the behavior changing is acceptab= le >> in user space. >> >> If it is acceptable, I am very willing to fix the races like your way. >=20 > I guess we should not let the write get ETXTBUSY for khugepaged work. >=20 > I am getting some segfault on stress_madvise_dso. And it doesn't really > generate the bug stack in my vm (qemu-system-x86_64). Is there an newer Hi, I can sure I am not update the stress_madvise_dso.c. My test environment is vm (qemu-system-aarch64, 32 cores). And I can=20 think of the following possibilities: (1) in thread_read() printf("read %s\n", dso_path); d =3D open(dso_path, O_RDONLY); /* The start addr must be alignment with 2M */ void *p =3D mmap((void *)0x40000dc00000UL, 0x800000, PROT_READ |=20 PROT_EXEC,MAP_PRIVATE, fd, 0); if (p =3D=3D MAP_FAILED) { perror("mmap"); goto out; } 0x40000dc00000 is random setting by myself. I am not sure this address=20 is available in your vm. (2) in thread_write() int fd =3D open(dso_path, O_RDWR); p =3D mmap(NULL, 0x800000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (p =3D=3D MAP_FAILED) { perror("mmap"); goto out; /* fail */ } because of I am sure the DSO is bigger than 0x800000, so directly map=20 the DSO using 0x800000. Maybe I had use '-z max-page-size=3D0x200000' to=20 compile the DSO? likes: $ gcc -z max-page-size=3D0x200000 -o libtest.so -shared libtest.o If you don't mind, you can send the segment fault log to me. And I will find x86 environment to test. Thanks! > version of it? >=20 > Thanks, > Song >=20