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 X-Spam-Level: X-Spam-Status: No, score=-3.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D781C433DB for ; Mon, 21 Dec 2020 19:54:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D287722BF3 for ; Mon, 21 Dec 2020 19:54:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D287722BF3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 33E716B005C; Mon, 21 Dec 2020 14:54:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2EED96B005D; Mon, 21 Dec 2020 14:54:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 205D96B0068; Mon, 21 Dec 2020 14:54:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0084.hostedemail.com [216.40.44.84]) by kanga.kvack.org (Postfix) with ESMTP id 0A2A16B005C for ; Mon, 21 Dec 2020 14:54:04 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id BFD8A1F06 for ; Mon, 21 Dec 2020 19:54:03 +0000 (UTC) X-FDA: 77618340366.04.girl53_4c1469e2745a Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin04.hostedemail.com (Postfix) with ESMTP id 9FA6F800BA88 for ; Mon, 21 Dec 2020 19:54:03 +0000 (UTC) X-HE-Tag: girl53_4c1469e2745a X-Filterd-Recvd-Size: 8382 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf12.hostedemail.com (Postfix) with ESMTP for ; Mon, 21 Dec 2020 19:54:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1608580442; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zU38n9umUo1jYRE3cwoN9aCysH5tPAi2O3MLmmeDuyM=; b=bo5YzfeLIECBtbgHTmndefz/d2vuKz7g07YAAOTjG5NYPk3Ry1+2tX7JnOEPrCuRFLVusp bK0Cy4XJbm/4Mfj/gLV7lLbQ6aKHEN28QT04pt6Wk9MW4DcCHzN53GXSEgxhriRug8NrkW 9/5g0alT/xM/8kH13u6a+fRvgQGu+FE= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-511-MQfiLGX7PzOHmpl599IqkQ-1; Mon, 21 Dec 2020 14:54:00 -0500 X-MC-Unique: MQfiLGX7PzOHmpl599IqkQ-1 Received: by mail-qk1-f197.google.com with SMTP id g26so9605579qkk.13 for ; Mon, 21 Dec 2020 11:54:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=zU38n9umUo1jYRE3cwoN9aCysH5tPAi2O3MLmmeDuyM=; b=PTJe+7nfyn8rtOyOQibcoQY0oLtDvhxH/sXlHaMZ2xl4KndRi/hK6dn9MNoxb0O6Yt QER0YS+AHEiwdP5mEy3OWkatZ2uFLEjfbdgJp+m6zHxnrnhntOoXTQ4xZPc3BnqhAxVi J5Kdx0lrSzWXMdd25FDhDE+Flw8j4MFdyW2k1M/IvxHOikBq1I5r8mme3uxMJgGCH3LD /gOLs0fxdtfun/idtNIAzru7GBeJXVOeh/oeUA/EWGwhsYvOAVqiZbO3Xbs6wX1jLBfj O3SrgeOO/JP/dJMthKW0MXHO9HpHvpwoDhQWDgDSeqCA6GWl674oexfN+6Y+laKuf1zm ahlQ== X-Gm-Message-State: AOAM532i++Z9TIRaxbMYpwhc/vMjBrDA1PLTDEQfqozOeT/x/j4xoWgd qxR9VOQBzfnIL0+pWKRlqhA2MYTysVcP7ZdTQX5yJlPUITOfXF84f9d+40zXOnGmLV+8Krt1rqA w0kw5dnOkcS4= X-Received: by 2002:a05:6214:13a3:: with SMTP id h3mr18853333qvz.5.1608580440173; Mon, 21 Dec 2020 11:54:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJxZUFJ0EpSQHCnxqLVRZJf/1yoKx4zvQDrvPQ1qmp6MpiTzd+fKsVYX8djRGDxm9PLeaE8t5g== X-Received: by 2002:a05:6214:13a3:: with SMTP id h3mr18853313qvz.5.1608580439943; Mon, 21 Dec 2020 11:53:59 -0800 (PST) Received: from xz-x1 ([142.126.83.202]) by smtp.gmail.com with ESMTPSA id w33sm10147410qth.34.2020.12.21.11.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Dec 2020 11:53:59 -0800 (PST) Date: Mon, 21 Dec 2020 14:53:57 -0500 From: Peter Xu To: Nadav Amit Cc: Yu Zhao , Andrea Arcangeli , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable@vger.kernel.org, minchan@kernel.org, Andy Lutomirski , Will Deacon , Peter Zijlstra Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect Message-ID: <20201221195357.GI6640@xz-x1> References: <20201219043006.2206347-1-namit@vmware.com> <20201221172711.GE6640@xz-x1> <76B4F49B-ED61-47EA-9BE4-7F17A26B610D@gmail.com> MIME-Version: 1.0 In-Reply-To: <76B4F49B-ED61-47EA-9BE4-7F17A26B610D@gmail.com> Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline 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 Mon, Dec 21, 2020 at 10:31:57AM -0800, Nadav Amit wrote: > > On Dec 21, 2020, at 9:27 AM, Peter Xu wrote: > >=20 > > Hi, Nadav, > >=20 > > On Sun, Dec 20, 2020 at 12:06:38AM -0800, Nadav Amit wrote: > >=20 > > [...] > >=20 > >> So to correct myself, I think that what I really encountered was act= ually > >> during MM_CP_UFFD_WP_RESOLVE (i.e., when the protection is removed).= The > >> problem was that in this case the =E2=80=9Cwrite=E2=80=9D-bit was re= moved during unprotect. > >> Sorry for the strange formatting to fit within 80 columns: > >=20 > > I assume I can ignore the race mentioned in the commit message but on= ly refer > > to this one below. However I'm still confused. Please see below. > >=20 > >> [ Start: PTE is writable ] > >>=20 > >> cpu0 cpu1 cpu2 > >> ---- ---- ---- > >> [ Writable PTE=20 > >> cached in TLB ] > >=20 > > Here cpu2 got writable pte in tlb. But why? > >=20 > > If below is an unprotect, it means it must have been protected once b= y > > userfaultfd, right? If so, the previous change_protection_range() wh= ich did > > the wr-protect should have done a tlb flush already before it returns= (since > > pages>0 - we protected one pte at least). Then I can't see why cpu2 = tlb has > > stall data. >=20 > Thanks, Peter. Just as you can munprotect() a region which was not prot= ected > before, you can ufff-unprotect a region that was not protected before. = It > might be that the user tried to unprotect a large region, which was > partially protected and partially unprotected. >=20 > The selftest obviously blindly unprotect some regions to check for bugs= . >=20 > So to your question - it was not write-protected (think about initial c= opy > without write-protecting). If that's the only case, how about we don't touch the ptes at all? Instea= d of playing with preserve_write, I'm thinking something like this right befor= e ptep_modify_prot_start(), even for uffd_wp=3D=3Dtrue: if (uffd_wp && pte_uffd_wp(old_pte)) { WARN_ON_ONCE(pte_write(old_pte)); continue; } if (uffd_wp_resolve && !pte_uffd_wp(old_pte)) continue; Then we can also avoid the heavy operations on changing ptes back and for= th. >=20 > > If I assume cpu2 doesn't have that cached tlb, then "write to old pag= e" won't > > happen either, because cpu1/cpu2 will all go through the cow path and= pgtable > > lock should serialize them. > >=20 > >> userfaultfd_writeprotect() =09 > >> [ write-*unprotect* ] > >> mwriteprotect_range() > >> mmap_read_lock() > >> change_protection() > >>=20 > >> change_protection_range() > >> ... > >> change_pte_range() > >> [ *clear* =E2=80=9Cwrite=E2=80=9D-bit ] > >> [ defer TLB flushes] > >> [ page-fault ] > >> =E2=80=A6 > >> wp_page_copy() > >> cow_user_page() > >> [ copy page ] > >> [ write to old > >> page ] > >> =E2=80=A6 > >> set_pte_at_notify() > >>=20 > >> [ End: cpu2 write not copied form old to new page. ] > >=20 > > Could you share how to reproduce the problem? I would be glad to giv= e it a > > shot as well. >=20 > You can run the selftests/userfaultfd with my small patch [1]. I ran it= with > the following parameters: =E2=80=9C ./userfaultfd anon 100 100 =E2=80=9C= . I think that it is > more easily reproducible with =E2=80=9Cmitigations=3Doff idle=3Dpoll=E2= =80=9D as kernel > parameters. >=20 > [1] https://lore.kernel.org/patchwork/patch/1346386/ Thanks. >=20 > >=20 > >> [1] https://lore.kernel.org/patchwork/patch/1346386 > >=20 > > PS: Sorry to not have read the other series of yours. It seems to ne= ed some > > chunk of time so I postponed it a bit due to other things; but I'll r= ead at > > least the fixes very soon. >=20 > Thanks again, I will post RFCv2 with some numbers soon. I read the patch 1/3 of the series. Would it be better to post them sepa= rately just in case Andrew would like to pick them earlier? Since you seem to be heavily working on uffd-wp - I do still have a few u= ffd-wp fixes locally even for anonymous. I think they're related to some corner= cases like either thp or migration entry convertions, but anyway I'll see wheth= er I should post them even earlier (I planned to add smap/pagemap support for uffd-wp so maybe I can even write some test case to verify some of them).= Just a FYI... Thanks, --=20 Peter Xu