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: 18162 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp207424wru; Thu, 10 Nov 2022 07:19:44 -0800 (PST) X-Google-Smtp-Source: AMsMyM6AzNcB6v+mmMAdoERpQCGylgD0X6/oNwNgXaQvmPKsHuDM3gRDI35oyiIkUKhuhQXRNezt X-Received: by 2002:a17:907:1626:b0:78d:b654:8af9 with SMTP id hb38-20020a170907162600b0078db6548af9mr3157464ejc.660.1668093584463; Thu, 10 Nov 2022 07:19:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668093584; cv=none; d=google.com; s=arc-20160816; b=i1FtuD/SvzYhlkKVn3yZnR0q4yJSEKBeiK46RqSgYe2mUHFbQp6SPUPfNtIx4nyfS+ Ibs6Pkju0cgGd1iPmeVzZsVXJO8fIMiVWdhijw2+Omn6SO83cJjT0WUfibwaNOD0qLMQ wfizevBx2+Y3rwzh3p5VdpMFpXOW8SNCgfpbtiC7pJJ0ed6KcWeO+mJsi4dWic0Jpb9z Md3+3IkogTafL7R//8jIIMjM2j97KMuoz2Jw+1Pte1aukp1MqvlWVWDE4UsP5eavCPCP jcqG6pKcd5qfIDxUJnh8vo1NODBwhN7TMCVfFHdOCfDJOuBo0X1vysCcHSsWXCj/qOuK Me6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ff4uIKDwEIX1/MsTGENOmYM+Xv3Uc0BmHEC53Fnq3vY=; b=QwaswQCBfhpOA32rzwUi3/RBu/VRLg/ud3rGW36vBaSDYA9lS3qYnc3V+a97ufZ9cy Gondt98Jpz9preQO/jq0NYiAWt83G1H6fCPbss6CQfSp8/MNEXmaKOcdWjbfN+0J2voS 8kGTxZA7a3+4zjppIgulpDOB/Mjm3aduzreNUG0tf4Mg4LyJP7bcDZZ0aYl50jnk9o4y MxalU6wncV5tispy4eYeT2TWsLOwST5kyM2bARtopV/HnbupnFODV3cIdQcex4IoVCbE ootLbQBwIJJJNUB0WeAtaJmBsRkqUnb/Izz26g4SuuZ7O7pT67NHOGVntVXSrJuwZXWx uF+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cOuv3RLA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sg10-20020a170907a40a00b007ae86742c37si6241137ejc.60.2022.11.10.07.19.19; Thu, 10 Nov 2022 07:19:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cOuv3RLA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231465AbiKJPSj (ORCPT + 99 others); Thu, 10 Nov 2022 10:18:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231222AbiKJPSJ (ORCPT ); Thu, 10 Nov 2022 10:18:09 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1628B1F2D9 for ; Thu, 10 Nov 2022 07:17:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668093428; 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=cOuv3RLAxaFsMmREF/kybouKR8goDi+OUvGXNAJvH/nzfaK8kTTZ74xPedRNWnm5I+teuR 8+LAzb6wBAjyvEzhos6pyEoryWqiwPmMB5clAH249cQH3LLPtywesuquoS5YXY05SUQL6I YHU17qBj15/lDEHRHzxNSnIbfkjeccQ= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-73-g_wJ1O3SPMWm9ZRVZBR7xA-1; Thu, 10 Nov 2022 10:17:07 -0500 X-MC-Unique: g_wJ1O3SPMWm9ZRVZBR7xA-1 Received: by mail-qt1-f197.google.com with SMTP id n12-20020ac85a0c000000b003a5849497f9so1560119qta.20 for ; Thu, 10 Nov 2022 07:17:06 -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=tRqKjxBgo5FJsy94vKnQbGcxlew0wzyYEpjcR4Fvz5nnbYIto4d+JsCJ9xblGuqIRM 4bAccc+57OEpB9SYGRq3EUAi1LtFe9ceFN6EPH71ygh3QDUwVlH7+YZ9aPvbxR7YRlPN 2ZMhJHsK0lQ9YqZeqMD75o8g47qJ2FAODWajj6lKpe/7dav+ToCbqLOgpETBF1XUIlvB r1p+PlcUwApZZg/huXTj5htrtzDsarK/SJWGlDDaLZwfTbVyqWy6qZpUiUYHiC1hiG4U eLJH2zuemp8Dy6W0QP/L/i4wMmizaTJ0Zca160kNFXl7ZI7HiVv9z8DjA6/vgxLMSF0V FlWg== X-Gm-Message-State: ACrzQf2AhuPwB5E+/OEeiTf00WMh8qWCDu+QZm03Io7BnE85SlDu//qd H7Bi/e9SAtddy5mwvfKs57F7aQxtu+GUCmJit+OPzCIDnfaHr8HSMyaqtJ7wZfnTz/EFp0sT8ww oB61cRQWFlWzXnkREI6Mg8o81 X-Received: by 2002:ac8:688b:0:b0:3a5:4032:84 with SMTP id m11-20020ac8688b000000b003a540320084mr38891962qtq.594.1668093426511; Thu, 10 Nov 2022 07:17:06 -0800 (PST) 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 Content-type: text/plain X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749122898435574140?= X-GMAIL-MSGID: =?utf-8?q?1749122898435574140?= 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: 18161 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp207351wru; Thu, 10 Nov 2022 07:19:35 -0800 (PST) X-Google-Smtp-Source: AMsMyM6kaOXk6LfObHIwi7kfBTR9Uw1mnPrZTcfGjh6bKco7U0xgHEInUqgNRMdBJmswaXTL1pNO X-Received: by 2002:a17:906:ad9:b0:78d:b6ea:174e with SMTP id z25-20020a1709060ad900b0078db6ea174emr2944640ejf.95.1668093575372; Thu, 10 Nov 2022 07:19:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668093575; cv=none; d=google.com; s=arc-20160816; b=SF73UVZVydD2WQ9h0upasxYylfMxq4bUkOQoF9VGGZ3FEAhWS21eGhbuHImjIzYUw+ vJfkJ/oyU7M9d9v5ORYSEMPTADy2YHoL9wavILTbLaW4oFdPWeUoMzJUsIv4ypcxh/8B Mzwqnrz8ZEAvMbQFBYZSwRCskIDyE1EUG37zTWtlelc5/8cjzmkdlec/aiD1vMgv/0Nh aElNZWViLX/EO3vGMOIwdHBRYPTZIjAt8qqzfiaeLfPwMQ38GOCQxvdJ5h6auDTZAcy4 N4qJob0SeCS4IjMfj7DvKgPHuVhOkMBgGv94uKNY5MZz2nX2nghdgqqCe6JSdPzBBTWl aD3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=6CDOnJYMZah4WeGmtiqmew/mMyquYuFWazZX9Q40Hac=; b=VdYFco6Iwv5Fv0855tPvL3EqbBC96U40jMEah3bSD+G+iDWX9Xkn0uw8peQSIPiSw6 pysYEF6Iw+4eUUXmAPA2D8RPZw3l8G7w75uSqQpxRrFSZATU47XAyx6F8DLc70Le6RWF O0Aj2ds/+KeX9AJLkJjqxKl5SGJKd5UUMQkGj0JSoquJ+8arGk07ljRHOpGfeLpa5LCw VVNI0TXH22djlvOnlcvZpBxe1RrOjpRDp9A4DE6bFN29dgRvUhUjC+MVCnKJ5ncZFP8+ w67x5Gl4R1shvPd50Z5JmPLHbPbHJCrCLtmwdy4VN/dOKKGHDWCVWFJCLfVhP+iltvrR XYsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=B7GCCQbm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m15-20020a1709060d8f00b007ae24f7774bsi14643816eji.168.2022.11.10.07.19.11; Thu, 10 Nov 2022 07:19:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=B7GCCQbm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231451AbiKJPSa (ORCPT + 99 others); Thu, 10 Nov 2022 10:18:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231394AbiKJPSJ (ORCPT ); Thu, 10 Nov 2022 10:18:09 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFD6F12615 for ; Thu, 10 Nov 2022 07:17:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668093433; 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=B7GCCQbmfuiRx2auHOnSL2kx6j7HIxHBNq/lKMblO7KLiwm19yilMjnJJM5t7nrYEI+NF+ zCcC9n/kdj5k01Creb0rFpI8LtgpcyTzG+3SJBHE6ZPQmzDTTvhc7PCM/mpHNyiir4tRbO IeNUZhx9m5gtTV391vPRf1Hea4ZW6Nk= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-553-9_rZmloSNcWu0PtaFgKW6g-1; Thu, 10 Nov 2022 10:17:10 -0500 X-MC-Unique: 9_rZmloSNcWu0PtaFgKW6g-1 Received: by mail-qv1-f69.google.com with SMTP id b2-20020a0cfe62000000b004bbfb15297dso1651173qvv.19 for ; Thu, 10 Nov 2022 07:17:10 -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=WWjwY9B6hgwdt1PXiLSdaoLbMqjmSHjGoDw0HwoNrz85ykKq6toNfRu/ECRN9As9Y9 FZmzRt98VsRiDz3WBIzCdV+Vbpczu8krCPjfaqj3Tdf/lzJJ8kmRfVRVPK8zT4Z6avcu rs54MdWYI4FIEX/BGd6Lgdd1Pj5t4p7oNWJtHynsn1HhVQL0psHuWoHQQnruQGhpd76k wLPw92ldxorRD/rNlGg+KU8Ag/qpe+5+StmyQe/RIpcJ9Ebr/prXBPRTtxJJBlX9TFza yVlLSO3iltsBcn7SP0/emBXa/p9GKsV+hT5YWMgITnLmbuJ7HkXIst78Br/mKOJoKV4N lwLQ== X-Gm-Message-State: ACrzQf2vynabYt7Xr3WCz9diXoX+wRPoi9b6z1pIQvPgebOy8oP4HQE5 nJ7Itasmh/i8AH6LuLp/TDTdQNGj+xj79OBwYhzWUoIU5cQI93swlyr7vZX7+mP7Ir94i3CmhIQ ceA3S/NH5H7BNodQYOKFo3PyQ X-Received: by 2002:a05:620a:cc2:b0:6fa:2175:5fa7 with SMTP id b2-20020a05620a0cc200b006fa21755fa7mr44267847qkj.0.1668093427831; Thu, 10 Nov 2022 07:17:07 -0800 (PST) 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 Content-type: text/plain X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749122888843261734?= X-GMAIL-MSGID: =?utf-8?q?1749122888843261734?= 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)