From patchwork Thu Jul 15 20:16:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12380989 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB237C636C9 for ; Thu, 15 Jul 2021 20:20:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4D229613D0 for ; Thu, 15 Jul 2021 20:20:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4D229613D0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9624E8D00FD; Thu, 15 Jul 2021 16:20:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 938FF8D00FC; Thu, 15 Jul 2021 16:20:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78C408D00FD; Thu, 15 Jul 2021 16:20:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0252.hostedemail.com [216.40.44.252]) by kanga.kvack.org (Postfix) with ESMTP id 568078D00FC for ; Thu, 15 Jul 2021 16:20:00 -0400 (EDT) Received: from smtpin40.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 3D9E61859AA1E for ; Thu, 15 Jul 2021 20:19:59 +0000 (UTC) X-FDA: 78365938518.40.1D3044A 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 E2097E0106E6 for ; Thu, 15 Jul 2021 20:16:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626380171; 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=LFcYHlyDKbLlgbIUnPtt3LhncHUQTe/w3qvUe3g6dgM=; b=YLfelCNW6ZIOfNgMxNQhQcoqDKz1YcPZ7Xfk6b5Q0FT908yKym756VmXP4AcxNYIl/XdrD N0cDWL+75JEhDD7e2OfcHSFZIxK8ezwgyFuP0TIJnbyhfbdxDr2SH3Eh5eNWunGIzlGUG4 CdZpaMipFhbRESBvphqWKbL7SMC2fkA= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-382-XVFOiUvYOk6tAxR9_BFXjg-1; Thu, 15 Jul 2021 16:16:10 -0400 X-MC-Unique: XVFOiUvYOk6tAxR9_BFXjg-1 Received: by mail-qk1-f198.google.com with SMTP id e13-20020a37e50d0000b02903ad5730c883so4634443qkg.22 for ; Thu, 15 Jul 2021 13:16:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LFcYHlyDKbLlgbIUnPtt3LhncHUQTe/w3qvUe3g6dgM=; b=t2xTUprCXsSA6AVFXdjmCRSI7eKd0wqFKmi2rKwoMDpVysZHsd7lHFm8kRzFbhiEBt ROCRCzdTokfztpZbS//ILCPG++ZWc3tRV8pNRqFOtFlc0wfOD28LYw1qS+HygavTvk/y I6bTu0UqWkP8DrIZCWNcY6zTQgsvi3t/Sxf/3mhCmUowtXLK3bqZ9sD524/R7MTWg62s fE+jjUNx1Axj5rq7LpVbDjdSOcJpYu1quPS/QiVvF5fiU7rnztTIgVpYTEkrv+JGJFQ5 pC/gFihUJDy4hBOIGzVkGSvBTC/5xH/7rb/kI0zHrIMq2VxGraPfARmMULyarhjH62cy MDgA== X-Gm-Message-State: AOAM53284tDXyOn1zpJqV9sVFpzHMgHy8Pp3yEnTfrTJ/9DuwW+06HLg O3jiFroB+kJ/R947k67sf27JVLRTkx1h+24oZJxqi6moL6JrfyWx5BlMtFEpbqEU1iDu9L97lrW uYauEqNtQZyU= X-Received: by 2002:ac8:110c:: with SMTP id c12mr5780287qtj.201.1626380169772; Thu, 15 Jul 2021 13:16:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxDAI/RGFVMXseRIsiXI0+GYwkXgmr6GBcXlMnsNgdtorEkJR8QBSr81kv2GUt4rMDqMXJibQ== X-Received: by 2002:ac8:110c:: with SMTP id c12mr5780264qtj.201.1626380169556; Thu, 15 Jul 2021 13:16:09 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-65-184-144-111-238.dsl.bell.ca. [184.144.111.238]) by smtp.gmail.com with ESMTPSA id j3sm2894807qka.96.2021.07.15.13.16.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jul 2021 13:16:09 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Nadav Amit , Jerome Glisse , "Kirill A . Shutemov" , Jason Gunthorpe , Alistair Popple , Andrew Morton , David Hildenbrand , peterx@redhat.com, Andrea Arcangeli , Matthew Wilcox , Mike Kravetz , Tiberiu Georgescu , Hugh Dickins , Miaohe Lin , Mike Rapoport Subject: [PATCH v5 13/26] shmem/userfaultfd: Handle the left-overed special swap ptes Date: Thu, 15 Jul 2021 16:16:06 -0400 Message-Id: <20210715201606.211559-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210715201422.211004-1-peterx@redhat.com> References: <20210715201422.211004-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YLfelCNW; spf=none (imf30.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: co9sgdt3rwqbdt7amn1833uwumb1jupg X-Rspamd-Queue-Id: E2097E0106E6 X-Rspamd-Server: rspam01 X-HE-Tag: 1626380171-662636 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: Note that the special uffd-wp swap pte can be left over even if the page under the pte got evicted. Normally when evict a page, we will unmap the ptes by walking through the reverse mapping. However we never tracked such information for the special swap ptes because they're not real mappings but just markers. So we need to take care of that when we see a marker but when it's actually meaningless (the page behind it got evicted). We have already taken care of that in e.g. alloc_set_pte() where we'll treat the special swap pte as pte_none() when necessary. However we need to also teach userfaultfd itself on either UFFDIO_COPY or handling page faults, so that everything will still work as expected. Signed-off-by: Peter Xu --- fs/userfaultfd.c | 15 +++++++++++++++ mm/userfaultfd.c | 13 ++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index f6e0f0c0d0e5..e1c1cbc7bcc8 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -329,6 +329,21 @@ static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx, */ if (pte_none(*pte)) ret = true; + /* + * We also treat the swap special uffd-wp pte as the pte_none() here. + * This should in most cases be a missing event, as we never handle + * wr-protect upon a special uffd-wp swap pte - it should first be + * converted into a normal read request before handling wp. It just + * means the page/swap cache that backing this pte is gone, so this + * special pte is leftover. + * + * We can't simply replace it with a none pte because we're not with + * the pgtable lock here. Instead of taking it and clearing the pte, + * the easy way is to let UFFDIO_COPY understand this pte too when + * trying to install a new page onto it. + */ + if (pte_swp_uffd_wp_special(*pte)) + ret = true; if (!pte_write(*pte) && (reason & VM_UFFD_WP)) ret = true; pte_unmap(pte); diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 2a9c9e6eb876..0c7212dfb95d 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -100,7 +100,18 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, } ret = -EEXIST; - if (!pte_none(*dst_pte)) + /* + * Besides the none pte, we also allow UFFDIO_COPY to install a pte + * onto the uffd-wp swap special pte, because that pte should be the + * same as a pte_none() just in that it contains wr-protect information + * (which could only be dropped when unmap the memory). + * + * It's safe to drop that marker because we know this is part of a + * MISSING fault, and the caller is very clear about this page missing + * rather than wr-protected. Then we're sure the wr-protect bit is + * just a leftover so it's useless already and is the same as none pte. + */ + if (!pte_none(*dst_pte) && !pte_swp_uffd_wp_special(*dst_pte)) goto out_unlock; if (page_in_cache)