From patchwork Thu Nov 10 20:31:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 18312 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp364683wru; Thu, 10 Nov 2022 12:39:18 -0800 (PST) X-Google-Smtp-Source: AMsMyM4z019klDDGJS4NwX8EIgikVg5IBeUFnqbQrxnuZUE2aYE7GseYWXscTPHlGAFMnHD0eTUz X-Received: by 2002:a50:eb88:0:b0:462:317a:e02f with SMTP id y8-20020a50eb88000000b00462317ae02fmr3374699edr.125.1668112758043; Thu, 10 Nov 2022 12:39:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668112758; cv=none; d=google.com; s=arc-20160816; b=L4rggxpDBh9so3+mI5UhVCdVniypFsAmXdI9KurPPVNZ1VCiD1Fvw8AyQkMH74hIdy oh3PDkY66iQG/VxJpWbeQfezjjHzFBPGWMXaIG6rJmSE0xNRW2LCtfwcCfsQwcnpMzJF tRhH64bOqY4OS8Q63DJp3+wfClF3gh+b3C4ZAbClXDokSx4RGVi7G1EZWXmqn8Kvbdi8 R9dGXwHbDPPX4jrigRkbRMRWJGVACqVLgxA0XmBLYX8oVJAnxjFHDoGqeHqMnq+M0tmL x60ZXVzQwNNORGkwfwvYCmW2Q6aTG96XBH2oNoBoAjdN9ZcgHf1jgFqrrFDimwnbz6kC UJaQ== 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=SCGnQ9/TD+SnjFjIZGjCub3r/P36ZnZbj+dnACujd4fBmZijU7s3n3RPo/KUFmo2hD okDeUzNLlhp6KZgNfRxWZReNlAoluho+9i43yWUmB+2C5nMBFOyJktis4vJ+0MhCu0DN Ik9bLaOF3GNDYOdGELqVCajlmmdjwa5FqbG6tJG8SPlDgD1Ul1YPWB2qXImkO1yTOtef 6eoUBeGINC5NpbglXwhZbU6EjARIM5CU4nYglRhsavtGZnMUNwmyircCkny7iQvFTIp4 +mACef/QA1SOYqtePEAQEm1q61xgEBu1/K92KgneoLRQzCfboFYg5qPZZwF5cp5dj5wV Ylnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JLggzwm0; 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 xj10-20020a170906db0a00b00791910ecd0fsi253578ejb.540.2022.11.10.12.38.54; Thu, 10 Nov 2022 12:39:18 -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=JLggzwm0; 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 S230424AbiKJUce (ORCPT + 99 others); Thu, 10 Nov 2022 15:32:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229499AbiKJUcd (ORCPT ); Thu, 10 Nov 2022 15:32:33 -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 121E52C101 for ; Thu, 10 Nov 2022 12:31:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668112298; 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=JLggzwm0ouJzT4gFMOEdeonTCpxls6CBRmM5WibRXZ+EE3uNOhleb47D9AN882oDPAjA44 yON9f9jjSBebUlINNUtkGasruJBsFFKFJRLyybkcjr5SQHn77bTEpUCjbxq1xWjh8GnXsy Rg7gnen+vFl1KRwIG+EUDa0SUBY0Ob8= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-49-OFgK-zluPViWqEPnxECd5w-1; Thu, 10 Nov 2022 15:31:37 -0500 X-MC-Unique: OFgK-zluPViWqEPnxECd5w-1 Received: by mail-qt1-f200.google.com with SMTP id cd6-20020a05622a418600b003a54cb17ad9so2275162qtb.0 for ; Thu, 10 Nov 2022 12:31:37 -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=ivSa2/XzbtZ6u5s8nUrYDeVl3aBSwqMzqN8NIYjMibUzDXHgNwyzQkPLgzpKbQQXNm P1X0f8gIhKMyLiuHL8KwGSSIRoJzjU6oC8fYMEu2JJVACe8d2D45eMG7StXEi3M1rn5b IMWBcaiOD4RirWxGLRKGuG4ULCgAfkZgVxOPva1OOKuI577DTyIuaBquL50W4tss5mAx Af8j2olQZfeLQKoHyM96aAeUHCe/N2kkEfkKcBbCYt2vg046UV0Nx7pnlAW9XnRZd9zy Gbq9C0Ahkyyv2HrMqhFRi41MB4XK3j1McCr/ULO+PUv/XmapZTQrGzeTDBB1GUT9vpuL ItGA== X-Gm-Message-State: ACrzQf24WGGYtllVU1pHgCd7C6cHp67lJaQG3jk/5+fcyGL5h6vrf5H1 Peeu8wxxI61BsHjn3+ByxLgpbRmFJ+d4B0kmcL52UE9GYvG7EjgaKAtRTGvFYxPKShtmsqbv8pF Kwde5LqjVJ/4OksUrU128qWUT3CFIgMQ8FRAe0emYIaWpDF11MlCnY04ATpRO4gTIkGtL7gVsyg == X-Received: by 2002:a05:620a:2455:b0:6fa:3fbf:6b51 with SMTP id h21-20020a05620a245500b006fa3fbf6b51mr1747398qkn.519.1668112296278; Thu, 10 Nov 2022 12:31:36 -0800 (PST) X-Received: by 2002:a05:620a:2455:b0:6fa:3fbf:6b51 with SMTP id h21-20020a05620a245500b006fa3fbf6b51mr1747373qkn.519.1668112295954; Thu, 10 Nov 2022 12:31:35 -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 i1-20020ac860c1000000b00399edda03dfsm123588qtm.67.2022.11.10.12.31.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 12:31:35 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: peterx@redhat.com, Andrea Arcangeli , Axel Rasmussen , Ives van Hoorne , Nadav Amit , Andrew Morton , Mike Rapoport , stable@vger.kernel.org Subject: [PATCH v2 1/2] mm/migrate: Fix read-only page got writable when recover pte Date: Thu, 10 Nov 2022 15:31:31 -0500 Message-Id: <20221110203132.1498183-2-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221110203132.1498183-1-peterx@redhat.com> References: <20221110203132.1498183-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=unavailable 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?1749143003296324049?= X-GMAIL-MSGID: =?utf-8?q?1749143003296324049?= 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 Reviewed-by: Alistair Popple --- 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 20:31:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 18313 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp364700wru; Thu, 10 Nov 2022 12:39:20 -0800 (PST) X-Google-Smtp-Source: AMsMyM73ehcFmoQ17Ho0riLQ+Xxa9gW9taNQNIP9yZlwhySC7edy7bhPgm1gI+awdlJoDus2IAX3 X-Received: by 2002:a05:6402:c07:b0:459:f897:7940 with SMTP id co7-20020a0564020c0700b00459f8977940mr3247067edb.168.1668112760106; Thu, 10 Nov 2022 12:39:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668112760; cv=none; d=google.com; s=arc-20160816; b=UXuVCyrGhCUENRIKNsTp5S1Pzl+x25lUz7opYOD/iaU6yUNbkCbIyIkLn/RzHxzHXJ 2wrTyfLvdKpkUIQCvehcEaCxMt8k876IU+hJEaLcLuEWZcU8LSR2BH8m/UrayC1AR/xN b3QAF0MAruNpRrSaVwWp/bK4SyCLAeVKUKZV0Oo7U879Zn01QpGfT3WN8ZpVrEzgl7su ixIhqJ+OjMVZ767YCgh1883juN+v2tq669IdB+5y5kR96Y6QqG3HQSeq8oEljzdEairY UNm2LFaCEpJxyJSKu+J2/kkQ2jrnkLMSUMrzLGqr3X9Jy54pclgvdKU8aSGmHnCk8h2w H2wA== 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=WQpC1KJZxfuI3Xd3ROAIVllQOpnyDtGxdSkS8bhzCN0=; b=Eo+D6kkLEjytx5aJXa4a+yrwFA2Tb8m/BpixOjVYp345kVofJzwJsCkzqBzmNLSERW f/PcvVHXX459yvf6vXArE4JQo7mQ/E1lQVNqNQKuRe+oSyIlky8cgdfBch/TBkGh1MEu aGwlWd4Gj7g6OyuQfnp3rzUvLFhRQxk2D4Nu0tWxuieo9TKE4fNOTGIHsczKPU6Z8Xlu taGTjY5kCZkyUKHeMKw5efS5trDlCp4kNSCyW5IEafVvaILSnyyQnbm/tx0hInuXBgmS veCnPZwT+xlQWplLulnK+0aGgRJUUz/eczWt6o7u87YOUM5v+q04+Z1/1KBPNlkohdw/ hJPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VrC1zLew; 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 wt13-20020a170906ee8d00b007807e613e5bsi222220ejb.933.2022.11.10.12.38.56; Thu, 10 Nov 2022 12:39:20 -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=VrC1zLew; 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 S231802AbiKJUcj (ORCPT + 99 others); Thu, 10 Nov 2022 15:32:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231587AbiKJUcg (ORCPT ); Thu, 10 Nov 2022 15:32:36 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 864021E701 for ; Thu, 10 Nov 2022 12:31:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668112300; 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=WQpC1KJZxfuI3Xd3ROAIVllQOpnyDtGxdSkS8bhzCN0=; b=VrC1zLewUg/xJCLnssyU+XO5/3ytsetbqE9mKtFNQsd2Ax1rdZNl3xZyPxQCTzA/s+55zf aOixlonX9Qw1/Mj4AES5TtpGtO0+Gf+dIwq0x3R65Tu+9MAYcjq9l3l0L/e86yvg31iO4r k4Wh+k96gAbYKycmlcMTOBhGoDVp/P0= 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-318-PH6j4e1qN1uRaPbYNWd1Og-1; Thu, 10 Nov 2022 15:31:39 -0500 X-MC-Unique: PH6j4e1qN1uRaPbYNWd1Og-1 Received: by mail-qt1-f199.google.com with SMTP id u31-20020a05622a199f00b003a51fa90654so2228453qtc.19 for ; Thu, 10 Nov 2022 12:31:39 -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=WQpC1KJZxfuI3Xd3ROAIVllQOpnyDtGxdSkS8bhzCN0=; b=aZs2E5TrDJUEls8dZyMJCn3RjhpaDhYra/Ml6QopASQwZA0PgvRko2Ky1U6tWob0ZZ f+uH9U0r5ZJ9CUQe8ONTZRbUGEU1wHIp9SC0VsA8LfRKTMvEj7tHl8DPPByOZab72ClR 2w869TzAihMSK/We4btw9cGbJAe8/rvk0WGie1T+wmQ8g13RmCDZFf4rnoauPev/XY3z K3b89hS5D35EKcbOekHh4xcW28XNYMKUQDynM8OQetIRSMhHtuGpJfxGrLRLHCMuiWGT W0B/R0vD7C0JSx3+Wr4pB/tnGH8jql1dAFO307u6aYttnqPq7fiqlGojooZqFWxnGbXO wBAw== X-Gm-Message-State: ACrzQf35iIpovYgn3ZTl5+jZ83QDdArFjaOykOXg/jVQmI0s4neyhUbp rKKYPntUq7dlGdFIbFL2LVq0rfBqEZIQHey1fTIPmubddBqeTuoLNZggbbSgswnzji2LQW+EYI/ ZCbVJ5rBmhtPo+OLvkivuqJKP5yc867DaxV521cPekqf0y6DvLgOAEQesW64jEvDx1GcTOQoE5g == X-Received: by 2002:a05:6214:c47:b0:4bb:85b4:fd8e with SMTP id r7-20020a0562140c4700b004bb85b4fd8emr60020951qvj.25.1668112298131; Thu, 10 Nov 2022 12:31:38 -0800 (PST) X-Received: by 2002:a05:6214:c47:b0:4bb:85b4:fd8e with SMTP id r7-20020a0562140c4700b004bb85b4fd8emr60020935qvj.25.1668112297792; Thu, 10 Nov 2022 12:31:37 -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 i1-20020ac860c1000000b00399edda03dfsm123588qtm.67.2022.11.10.12.31.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 12:31:37 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: peterx@redhat.com, Andrea Arcangeli , Axel Rasmussen , Ives van Hoorne , Nadav Amit , Andrew Morton , Mike Rapoport Subject: [PATCH v2 2/2] mm/uffd: Sanity check write bit for uffd-wp protected ptes Date: Thu, 10 Nov 2022 15:31:32 -0500 Message-Id: <20221110203132.1498183-3-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221110203132.1498183-1-peterx@redhat.com> References: <20221110203132.1498183-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?1749143005333767845?= X-GMAIL-MSGID: =?utf-8?q?1749143005333767845?= 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. 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..64141acf70c8 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; + + /* + * 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. + */ + VM_WARN_ON_ONCE(wp && pte_write(pte)); + + return wp; } static inline pte_t pte_mkuffd_wp(pte_t pte)