From patchwork Mon Nov 14 00:04:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 19485 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1883034wru; Sun, 13 Nov 2022 16:08:58 -0800 (PST) X-Google-Smtp-Source: AA0mqf4q6sNIblWeppK9W7TLwlR0KZKbK9VUi+Xa4vtu3la6fL9p7gt8Uy6MNGRNElR2vx4Peb2a X-Received: by 2002:a63:5a60:0:b0:470:4320:93d6 with SMTP id k32-20020a635a60000000b00470432093d6mr9427059pgm.504.1668384538726; Sun, 13 Nov 2022 16:08:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668384538; cv=none; d=google.com; s=arc-20160816; b=CcZjIKeh85/5hn5GBvb0KLFn/7qn2i6Rgy23DzvCskrTShvI0pJ9w2MFDwhkzM2G8A Xhwxu+b24Rwc5FQHtMNh88XC+fNMmF0SMf4xjt4FUG0YvPK8gj8x34iwEl1HbwaDCSEU RkeFyEHWJPlIqjbMtwhPP1iDseFglXzJswViElLWaWpOvi/oFtjnkuOP6BX+3Tjte9Lc Bpv7P2ir/GkzUPeSozpzZawVuHCk6wZ98YbyzAbfGMLAo11qpX2yS5fDh7bEcyUQ2dj3 SvyNsVEWf9zHeRQbCO+khqKiohokVavxCkivqkWvygO7NwE9rToTkLCwd+Yuz+rc8UXn +atw== 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=vuiiwLAN7I1THL57m13Ion7pO7eyRCignS0WdOlLecg=; b=hp7a9dYJXWbNRVGkC+PNA5RoBukCwu33UeXTwTjf0AbZU+LPeZ4lxH5DZz5f97XZru oCIwG/i6kjkMxhRPT+M+MG6sb5qKncHxmQ0OHBpMGi8AU/GplazjawF6mycs63yyfMyT hF8S4Zm+0VcRA+He0T8U4MxLttMi4iV21E6bcZcR8+cIxWgennU7ArsnKNcHCo1XH03F xG8oK3UQO6XhgPxybYNRFYFJj16bp90OMtcUkS6td2LrKuI3awMiDt3slwf95gARB5SZ ZeXUhILH7gWXx+KTYH8PVNKRTCrerObpvuassoKQXfgK4yEee1EhqAO/hpp3BngLiuLd XE+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IPXcjSQY; 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 s20-20020a63f054000000b00439f012ca7asi8041852pgj.743.2022.11.13.16.08.45; Sun, 13 Nov 2022 16:08:58 -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=IPXcjSQY; 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 S229692AbiKNAF7 (ORCPT + 99 others); Sun, 13 Nov 2022 19:05:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235415AbiKNAFt (ORCPT ); Sun, 13 Nov 2022 19:05:49 -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 BC190DE86 for ; Sun, 13 Nov 2022 16:04:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668384295; 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=vuiiwLAN7I1THL57m13Ion7pO7eyRCignS0WdOlLecg=; b=IPXcjSQYbS+MXcWY1MD4DN+6sgEkg6BCqxpeBaVMeL9sDu/uIUcuktc8NwMnHRow+CW1Mk ivyM40Ohz9HZsWmuT4JktuB5vuMe969auy3VIvqbGowb2XzRsr5uShpsWcWJ9rsXCbTrtN ArJObzZ32htRj5OKERlDPXLQvzvFzeo= 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-307-Ce3JMMc4Ph2Xyi0NjQBi4w-1; Sun, 13 Nov 2022 19:04:54 -0500 X-MC-Unique: Ce3JMMc4Ph2Xyi0NjQBi4w-1 Received: by mail-qv1-f72.google.com with SMTP id 71-20020a0c804d000000b004b2fb260447so7572669qva.10 for ; Sun, 13 Nov 2022 16:04:53 -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=vuiiwLAN7I1THL57m13Ion7pO7eyRCignS0WdOlLecg=; b=lWBVSMuGjPve9Kuxd0OcuuJD7UTd8iS7B37RgpVQ1irDyZTNrSAA7nIZg5KXqc/ijW cAQYcALvOUNl+ObPJ31TEn/7QEcUAkGclVhRx7uQwqylRTdFqW+RvILZ8aWMK77mGj7D gHJIRB6/NJReH5oKvqrvQTSAerKtUDht8r2xO6CFf9Fx9la+En58/q+b5yoyValhVzOT mlt8BsCwQLFLAJ2Vke+NVB9PiYjh3KzSNbAQ5APIhei+tFDbALEuYXXhrho2TJGo16bL 1MrFcec/IAHpUR9EETi5zKDP8O0oawKr9FpwnfDLvaoehuia3KO+uDhdiHgZo/rsY8tZ cbAw== X-Gm-Message-State: ANoB5pmONyNiOlP5ADFsUz4VDPr3sct0t+gYw9Nj7PVBTfMpf+TidsVE 1VGffgI+qJd8RqIwDeq/lULNWAv4sNO+CGsgcctAUIi18QhGgmtmgH5zboJTRI4C0py9CsWVg9V hR+n7AWVviZFwUOMOE3o6+/yHSe/FyWA+VZef3FM4rqzg7QDZkpnlR1wVw7eLUDxLLBpuX5go9A == X-Received: by 2002:ae9:e302:0:b0:6fa:6cb1:8965 with SMTP id v2-20020ae9e302000000b006fa6cb18965mr9424185qkf.541.1668384293245; Sun, 13 Nov 2022 16:04:53 -0800 (PST) X-Received: by 2002:ae9:e302:0:b0:6fa:6cb1:8965 with SMTP id v2-20020ae9e302000000b006fa6cb18965mr9424139qkf.541.1668384292663; Sun, 13 Nov 2022 16:04:52 -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 cb5-20020a05622a1f8500b0039cc0fbdb61sm4870380qtb.53.2022.11.13.16.04.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 16:04:52 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Rapoport , Nadav Amit , Andrew Morton , peterx@redhat.com, Andrea Arcangeli , Ives van Hoorne , Axel Rasmussen , Alistair Popple Subject: [PATCH v3 2/2] mm/uffd: Sanity check write bit for uffd-wp protected ptes Date: Sun, 13 Nov 2022 19:04:47 -0500 Message-Id: <20221114000447.1681003-3-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221114000447.1681003-1-peterx@redhat.com> References: <20221114000447.1681003-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?1749427986091052066?= X-GMAIL-MSGID: =?utf-8?q?1749427986091052066?= 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 | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 5059799bebe3..63bdbb0f989e 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -291,7 +291,23 @@ 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)