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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B5BCFC433FE for ; Mon, 14 Nov 2022 16:09:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3673E6B0073; Mon, 14 Nov 2022 11:09:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 317076B0075; Mon, 14 Nov 2022 11:09:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E1E08E0001; Mon, 14 Nov 2022 11:09:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0EBEA6B0073 for ; Mon, 14 Nov 2022 11:09:41 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D9146AAC57 for ; Mon, 14 Nov 2022 16:09:40 +0000 (UTC) X-FDA: 80132533320.18.ADFB45F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id E6E601A0005 for ; Mon, 14 Nov 2022 16:09:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668442177; 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=FRDOlAhtq+4FPxDn7xJvAO+rfsHWJZyuf4JMnt1m0vE=; b=ZZMMMlfR1nbzUAYOjb+QTByN2hxS7quKdWS32eLRYNy0NMnlkzN+Rn0NJiFRBhOnEI9Jjt VH9yj0Nj2ALce9tqua52M/FEKe9sL2KU9fI4bRNiC57vs1bqIGEvGciPdkFNHrcdyp6J5a ShZb9LcH/YKY6Af1dtHD8OpUpYjQOsA= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-410-gdeXarKHP8y6VjJ-C85i_A-1; Mon, 14 Nov 2022 11:09:36 -0500 X-MC-Unique: gdeXarKHP8y6VjJ-C85i_A-1 Received: by mail-wm1-f70.google.com with SMTP id c5-20020a1c3505000000b003c56da8e894so9145958wma.0 for ; Mon, 14 Nov 2022 08:09:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FRDOlAhtq+4FPxDn7xJvAO+rfsHWJZyuf4JMnt1m0vE=; b=r8sr7m7LKZtGio6/gpAXHSNt19qkKZFm9H+lYgonjrcBFoMmbf1RvLYpfhPkaTdQ8T 187uS16vo0Bstjfny16MDnE3rJPuU5lJ8vkkxeFpYL2PyCCPiNoPPNn33BzpyKdmJTDs 4dgA96HjRfDW3iTOpzWxfzMS8CXPHipgMmVuvvEtNEqH/DUXBVfi3gvQdnxmf1hVo+iS 3co99AMFCDAHH5XkgKsuYwqL5hGyKbD69PHkNP/VOVE1VLf84p+OufBI1eIh+rbm8+f3 LsASN+FO1A3U6O2fhlh6yBItpUe6lz9V4fax/kwAvHDeceb/xFGVwxdc1JJzK5CEfU98 WXbw== X-Gm-Message-State: ANoB5pkMl5WOwd/mDUJyctzX00LEfRDb8IGPFx/5un6JWMLbF0RNRYXx /TBa/9odrhG6ZB1S3JeBfp1RYNmaMClLfjgSxWhyPFmeaC9uiyoCTO4KfiRN+L6Nkoz8s1VAtdR SXhqx9fMqmKY= X-Received: by 2002:a05:600c:4143:b0:3c6:bc31:20ed with SMTP id h3-20020a05600c414300b003c6bc3120edmr8592690wmm.41.1668442174565; Mon, 14 Nov 2022 08:09:34 -0800 (PST) X-Google-Smtp-Source: AA0mqf6jaWURvxgfsRAJxd6JqJAJZSFvhIsCymgU3NpsSC69NAaRQ9FIrIJbC8tZozb1WChbCSr4JQ== X-Received: by 2002:a05:600c:4143:b0:3c6:bc31:20ed with SMTP id h3-20020a05600c414300b003c6bc3120edmr8592646wmm.41.1668442174142; Mon, 14 Nov 2022 08:09:34 -0800 (PST) Received: from ?IPV6:2003:cb:c703:d300:d881:1fd5:bc95:5e2d? (p200300cbc703d300d8811fd5bc955e2d.dip0.t-ipconnect.de. [2003:cb:c703:d300:d881:1fd5:bc95:5e2d]) by smtp.gmail.com with ESMTPSA id l22-20020a05600c16d600b003cf4eac8e80sm14761753wmn.23.2022.11.14.08.09.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Nov 2022 08:09:33 -0800 (PST) Message-ID: <9af36be3-313b-e39c-85bb-bf30011bccb8@redhat.com> Date: Mon, 14 Nov 2022 17:09:32 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 To: Peter Xu , linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Andrea Arcangeli , Axel Rasmussen , Ives van Hoorne , Nadav Amit , Andrew Morton , Mike Rapoport , stable@vger.kernel.org References: <20221110203132.1498183-1-peterx@redhat.com> <20221110203132.1498183-2-peterx@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v2 1/2] mm/migrate: Fix read-only page got writable when recover pte In-Reply-To: <20221110203132.1498183-2-peterx@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668442180; h=from:from:sender: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:dkim-signature; bh=FRDOlAhtq+4FPxDn7xJvAO+rfsHWJZyuf4JMnt1m0vE=; b=4sAk2HoEUjEfjOBt98jFAYdeRkL7hEuX4BaTkZ8A2NElKfOBkCY3z3z7MvjwD8DNYFJouo /jNUgki8nZqfxWzTe1cqKqnfL2syDQlAY9BM21Ltgl3CUW3W0u7ihvpP/d4vlggO04qWaR ItyjQxSHxTBQOGoM1R8CuHMB868EDGo= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZZMMMlfR; spf=pass (imf19.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668442180; a=rsa-sha256; cv=none; b=n9ZQbLBMwEgnb38lCOWQgs7/DRtWMFzIuaJtyu92DXBmPdSv5r3k2vxb7gKMX8jHudyhqM qHyMAdU/US/mRKk2NLkH50//iiKaSiYw0L9DUDdu+UJ5SFjNV50xnr03voG3CXa0dmHpaY zkBOY5nh5903vpDcjlC/9fudZRX8s1U= X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: E6E601A0005 Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZZMMMlfR; spf=pass (imf19.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: burhp8p4kdtpg6fniburozwbkubu54qn X-HE-Tag: 1668442179-377723 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 10.11.22 21:31, Peter Xu wrote: > Ives van Hoorne from codesandbox.io reported an issue regarding possible > data loss of uffd-wp when applied to memfds on heavily loaded systems. The > sympton is some read page got data mismatch from the snapshot child VMs. > > Here I can also reproduce with a Rust reproducer that was provided by Ives > that keeps taking snapshot of a 256MB VM, on a 32G system when I initiate > 80 instances I can trigger the issues in ten minutes. > > It turns out that we got some pages write-through even if uffd-wp is > applied to the pte. > > The problem is, when removing migration entries, we didn't really worry > about write bit as long as we know it's not a write migration entry. That > may not be true, for some memory types (e.g. writable shmem) mk_pte can > return a pte with write bit set, then to recover the migration entry to its > original state we need to explicit wr-protect the pte or it'll has the > write bit set if it's a read migration entry. > > For uffd it can cause write-through. I didn't verify, but I think it'll be > the same for mprotect()ed pages and after migration we can miss the sigbus > instead. I don't think so. mprotect() handling relies on vma->vm_page_prot, which is supposed to do the right thing. E.g., map the pte protnone without VM_READ/VM_WRITE/.... > > The relevant code on uffd was introduced in the anon support, which is > commit f45ec5ff16a7 ("userfaultfd: wp: support swap and page migration", > 2020-04-07). However anon shouldn't suffer from this problem because anon > should already have the write bit cleared always, so that may not be a > proper Fixes target. To satisfy the need on the backport, I'm attaching > the Fixes tag to the uffd-wp shmem support. Since no one had issue with > mprotect, so I assume that's also the kernel version we should start to > backport for stable, and we shouldn't need to worry before that. > > Cc: Andrea Arcangeli > Cc: stable@vger.kernel.org > Fixes: b1f9e876862d ("mm/uffd: enable write protection for shmem & hugetlbfs") > Reported-by: Ives van Hoorne > Signed-off-by: Peter Xu > --- > mm/migrate.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff --git a/mm/migrate.c b/mm/migrate.c > index dff333593a8a..8b6351c08c78 100644 > --- a/mm/migrate.c > +++ b/mm/migrate.c > @@ -213,8 +213,14 @@ static bool remove_migration_pte(struct folio *folio, > pte = pte_mkdirty(pte); > if (is_writable_migration_entry(entry)) > pte = maybe_mkwrite(pte, vma); > - else if (pte_swp_uffd_wp(*pvmw.pte)) > + else > + /* NOTE: mk_pte can have write bit set */ > + pte = pte_wrprotect(pte); Any particular reason why not to simply glue this to pte_swp_uffd_wp(), because only that needs special care: if (pte_swp_uffd_wp(*pvmw.pte)) { pte = pte_wrprotect(pte); pte = pte_mkuffd_wp(pte); } And that would match what actually should have been done in commit f45ec5ff16a7 -- only special-case uffd-wp. Note that I think there are cases where we have a PTE that was !writable, but after migration we can map it writable. BTW, does unuse_pte() need similar care? new_pte = pte_mkold(mk_pte(page, vma->vm_page_prot)); if (pte_swp_uffd_wp(*pte)) new_pte = pte_mkuffd_wp(new_pte); set_pte_at(vma->vm_mm, addr, pte, new_pte); -- Thanks, David / dhildenb