From patchwork Thu Nov 10 15:17:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13038900 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 6437FC433FE for ; Thu, 10 Nov 2022 15:17:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 17CED6B0072; Thu, 10 Nov 2022 10:17:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0321A6B007B; Thu, 10 Nov 2022 10:17:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D34F36B0072; Thu, 10 Nov 2022 10:17:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BD86A6B0074 for ; Thu, 10 Nov 2022 10:17:11 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7D2B8C057A for ; Thu, 10 Nov 2022 15:17:11 +0000 (UTC) X-FDA: 80117885862.11.3C0F48A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 09AC214000D for ; Thu, 10 Nov 2022 15:17:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668093429; 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=Ff4uIKDwEIX1/MsTGENOmYM+Xv3Uc0BmHEC53Fnq3vY=; b=C44tQlQqHOxqU0k8sktJhDozCbDCF5RcDYb0A62BNGNZ4TXXBP0PCWX+QoWbg/6RAXaL4R Zk1SDXXq+cTt06l1Vi2XOuD3Ird4FW1VmZyTkfaXrGEC0rXJOtNfxvJ6g17VDmVawKO+I9 9IyZWN86FEoI4fvFULT3blVl2o9e4/E= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-408-U3gjD2ksPWSHgGrK0UhmkQ-1; Thu, 10 Nov 2022 10:17:08 -0500 X-MC-Unique: U3gjD2ksPWSHgGrK0UhmkQ-1 Received: by mail-qt1-f199.google.com with SMTP id s14-20020a05622a1a8e00b00397eacd9c1aso1558265qtc.21 for ; Thu, 10 Nov 2022 07:17:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ff4uIKDwEIX1/MsTGENOmYM+Xv3Uc0BmHEC53Fnq3vY=; b=CjIF6xdxyx7uEgB6/HTtkvDQg6/7AowTnfZ6o08ngJMwY2LQGHtCrxBrflRgLI2Q33 w2r8C2evby5uYC5gUrPdMtFOSMK8tliYKqeimwOYqkrVos51iHlwR9WcOj+IpcsenEtB BxjGlTxSydncShBHL7nSHl3Zi5rXMFACmN3cpSwWrCCApTPEVOLSyrYiRSY9OPtiF45i /PxFD+UnSiLr/8zRPG4y+UJony+bDx8Zo7p5wjRxeNxvtGUjPMgaoG9O8N8SRRhmEa/h dtgDM+9ZzIFMVHnFrFwkHNhskHWDN5hxSXprAhlP3/mVgF+0cqi70kyqINtqfipp1VEc Tlyw== X-Gm-Message-State: ACrzQf0yfx26ckob+YQD+jGl++eYIZ7YoXO3yYavOSEo4rYWsx9ZMXTT hO0nzCf7idn7DoD2IGeOrj5BEHevK4OfPKFOC2ftJXtcIWLukdn3cvR5BV1L3+dPs4I1s/ELq9L KKOjymKwWBOTb1CNpOKXD9ppYL3bWh5M9kf794Qwo0m2JMRbo4EVoAjN9OGyI X-Received: by 2002:ac8:688b:0:b0:3a5:4032:84 with SMTP id m11-20020ac8688b000000b003a540320084mr38891973qtq.594.1668093426576; Thu, 10 Nov 2022 07:17:06 -0800 (PST) X-Google-Smtp-Source: AMsMyM6tpasQ30d/7Z7xCvuxHK3YLAF5URmUDDJQLgiiIm9IreU3H5TqEwrf6XMfeWsh8URcfVsH1A== X-Received: by 2002:ac8:688b:0:b0:3a5:4032:84 with SMTP id m11-20020ac8688b000000b003a540320084mr38891929qtq.594.1668093426204; Thu, 10 Nov 2022 07:17:06 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id l19-20020a05620a28d300b006ec771d8f89sm13621596qkp.112.2022.11.10.07.17.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:17:05 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Mike Rapoport , peterx@redhat.com, Nadav Amit , Andrew Morton , Axel Rasmussen , Andrea Arcangeli , Ives van Hoorne , stable@vger.kernel.org Subject: [PATCH 1/2] mm/migrate: Fix read-only page got writable when recover pte Date: Thu, 10 Nov 2022 10:17:01 -0500 Message-Id: <20221110151702.1478763-2-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221110151702.1478763-1-peterx@redhat.com> References: <20221110151702.1478763-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668093430; a=rsa-sha256; cv=none; b=Ud9mQDLROULGAoPEfmU70pnPeyWWpOZWIxTXVPG6muG15LSXrXYq/x/u/mhyTSwKbihsFE w8bo74qO+buNJzSF/P9YSgLISjRV8B/LdDOR+SW/H3URTbE88S+N5lXY5cU4H34xJ94Wmw 3S7mMF+DHE1dWmmwrV/3CpjmSfECJ2k= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=C44tQlQq; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668093430; 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=Ff4uIKDwEIX1/MsTGENOmYM+Xv3Uc0BmHEC53Fnq3vY=; b=CL6Cou6yi6ulpErpio4zHbIbsBL6P36osUQjPbPnR1umHVvvPM1rV+lF+2yA/sILUqgLBY E9otcJ2rHFL9zHYJtmzhv5lJvX9zbSxO8brMkUnvbw55bXg1S03822AM2Kc5BLlMUdEpbf 8DG9d/8hWpETt0w0SMTpcKikynMJxCk= X-Rspamd-Queue-Id: 09AC214000D Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=C44tQlQq; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com X-Rspam-User: X-Rspamd-Server: rspam01 X-Stat-Signature: p6xg3qf46enqhkhdakmfrouuqabe5rzr X-HE-Tag: 1668093429-935152 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: 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. 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); + + if (pte_swp_uffd_wp(*pvmw.pte)) { + WARN_ON_ONCE(pte_write(pte)); pte = pte_mkuffd_wp(pte); + } if (folio_test_anon(folio) && !is_readable_migration_entry(entry)) rmap_flags |= RMAP_EXCLUSIVE; From patchwork Thu Nov 10 15:17:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13038899 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 7EA63C4332F for ; Thu, 10 Nov 2022 15:17:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED72A6B0074; Thu, 10 Nov 2022 10:17:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E3AF26B0075; Thu, 10 Nov 2022 10:17:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C8DEC8E0001; Thu, 10 Nov 2022 10:17:11 -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 B99716B0072 for ; Thu, 10 Nov 2022 10:17:11 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8638C8146C for ; Thu, 10 Nov 2022 15:17:11 +0000 (UTC) X-FDA: 80117885862.09.0FD1B3D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 1271C80012 for ; Thu, 10 Nov 2022 15:17:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668093430; 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=6CDOnJYMZah4WeGmtiqmew/mMyquYuFWazZX9Q40Hac=; b=UtZIhFFkEgkxC5nCClVaMEa3jEoJhXDv09Nt0InlDP4+20j4PfjjtIt5u2FEZgE3frF6OD Ua5iYWZ1OwuAsK9UyRgEEcuGADzHbfPQDwuzo6eBH3dNb8AbjkpnNG1OKQ38PN/2byWHrg VVTFoAGwS1Zgwt4hp2AWQr5JfFQbTuE= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-32-GbtVpv2mOu265TwJOwEIWw-1; Thu, 10 Nov 2022 10:17:08 -0500 X-MC-Unique: GbtVpv2mOu265TwJOwEIWw-1 Received: by mail-qv1-f72.google.com with SMTP id 71-20020a0c804d000000b004b2fb260447so1675855qva.10 for ; Thu, 10 Nov 2022 07:17:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6CDOnJYMZah4WeGmtiqmew/mMyquYuFWazZX9Q40Hac=; b=MSiZz1Tl+r699EEtZjCCaNuax/jSg048KrMfEQstcGURS/TxvuyC1xXj7WXg6d3Yii NbUdNe502YWp+YCrpUBxxeYRgbyWFBzeiEdYpABraa17XoE/lC3kX3iF7H+rgiBQKlY3 sv5KLvDA52wcjg62V6NhUPK2I7lHL5fa2/32gBHNeMjYcKB29ylGDXnAiVBEmNv68Oz0 TnA9qdlZfDP3wVajvog2ZQbWobdajezbA6N7B8VU0NrS8JAaKduN91uQi/dEam96SR1m srqBN6HpSBmD3AoySyvxqKFwdpzNkeauu20y/eQ9nx0V+nh0u649lENC5LEoZXl+5eq/ 49CQ== X-Gm-Message-State: ACrzQf3KiifljQhutjU0NB+LYT8mP5QA9prPLU9zFWBWjOD/g5bTMhO5 W+uqSC7yDtle/a+4xSjZOl3HePxAB00RGryVI7BRl1I9WA4j/Nrrf3GJhMAspiJkBNGcmdV69oV 1Ne1c/iFSbcK1q+EsR5xsR4ecGyNl4BetfImqp59QNv3XlhpH1pXNxN6+vz4R X-Received: by 2002:a05:620a:cc2:b0:6fa:2175:5fa7 with SMTP id b2-20020a05620a0cc200b006fa21755fa7mr44267859qkj.0.1668093427888; Thu, 10 Nov 2022 07:17:07 -0800 (PST) X-Google-Smtp-Source: AMsMyM5sZc0MPXl3k0/8piyAMxvk9MkH6fZqGYMI8ObhnZ5YYr0EEA2u8MpiOMTdbYs26mM5B7/hMg== X-Received: by 2002:a05:620a:cc2:b0:6fa:2175:5fa7 with SMTP id b2-20020a05620a0cc200b006fa21755fa7mr44267821qkj.0.1668093427556; Thu, 10 Nov 2022 07:17:07 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id l19-20020a05620a28d300b006ec771d8f89sm13621596qkp.112.2022.11.10.07.17.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:17:07 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Mike Rapoport , peterx@redhat.com, Nadav Amit , Andrew Morton , Axel Rasmussen , Andrea Arcangeli , Ives van Hoorne Subject: [PATCH 2/2] mm/uffd: Sanity check write bit for uffd-wp protected ptes Date: Thu, 10 Nov 2022 10:17:02 -0500 Message-Id: <20221110151702.1478763-3-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221110151702.1478763-1-peterx@redhat.com> References: <20221110151702.1478763-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668093431; a=rsa-sha256; cv=none; b=KjXh76nzyha23css6QyQuADhKaRy68LG9D0gdSK5Dbpc0RFkhxxelPXoBKfSg52WGCN3Zp TB8jsZLEJKd9Evsg9OOMtpNKSolYO5UPWnZyjTgeq9TfvfhYEzEFiPp8lZ8HEBM0qpjj2b cPw0tiiB3WIGRlz1/2kdguS7C1flI04= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UtZIhFFk; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668093431; 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=6CDOnJYMZah4WeGmtiqmew/mMyquYuFWazZX9Q40Hac=; b=Noc7IbYGHnRlkgeW83gpeXNtZ14A+ylr5SrMfePdsKbeJR5q9bunuDe+aCoYK04DXCbS4q /wLVjzf/zYvXTFIY4Kuu8/00oh4E8KvVyncm325iatN5gUSRQIe6mDl/JPPAwrMZhztQhB HWMLr5fVClk7dkQ/EPa/ybSzXmpati0= X-Rspam-User: X-Stat-Signature: yt5wum4mxf1gqehc9pn5sdr1dys5d1ik X-Rspamd-Queue-Id: 1271C80012 Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UtZIhFFk; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com X-Rspamd-Server: rspam03 X-HE-Tag: 1668093430-394282 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: Let's add one sanity check for CONFIG_DEBUG_VM on the write bit in whatever chance we have when walking through the pgtables. It can bring the error earlier even before the app notices the data was corrupted on the snapshot. Also it helps us to identify this is a wrong pgtable setup, so hopefully a great information to have for debugging too. Wrapping with CONFIG_DEBUG_VM is not that useful considering we have that in many distros already, but still do that just in case some custom build doesn't want anything like it. Cc: Andrea Arcangeli Signed-off-by: Peter Xu --- arch/x86/include/asm/pgtable.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 5059799bebe3..27fff6b14929 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -291,7 +291,21 @@ static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear) #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP static inline int pte_uffd_wp(pte_t pte) { - return pte_flags(pte) & _PAGE_UFFD_WP; + bool wp = pte_flags(pte) & _PAGE_UFFD_WP; +#ifdef CONFIG_DEBUG_VM + /* + * Having write bit for wr-protect-marked present ptes is fatal, + * because it means the uffd-wp bit will be ignored and write will + * just go through. + * + * Use any chance of pgtable walking to verify this (e.g., when + * page swapped out or being migrated for all purposes). It means + * something is already wrong. Tell the admin even before the + * process crashes. We also nail it with wrong pgtable setup. + */ + WARN_ON_ONCE(wp && pte_write(pte)); +#endif + return wp; } static inline pte_t pte_mkuffd_wp(pte_t pte)