From mboxrd@z Thu Jan 1 00:00:00 1970 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 3C0EBC433B4 for ; Tue, 27 Apr 2021 16:13:55 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D3005613C2 for ; Tue, 27 Apr 2021 16:13:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D3005613C2 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 63CAB6B0081; Tue, 27 Apr 2021 12:13:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 613B76B0082; Tue, 27 Apr 2021 12:13:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B9A36B0083; Tue, 27 Apr 2021 12:13:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0046.hostedemail.com [216.40.44.46]) by kanga.kvack.org (Postfix) with ESMTP id 25EB66B0081 for ; Tue, 27 Apr 2021 12:13:47 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id DA547180AD81D for ; Tue, 27 Apr 2021 16:13:46 +0000 (UTC) X-FDA: 78078642852.01.9EB091F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf17.hostedemail.com (Postfix) with ESMTP id F03EE40001DE for ; Tue, 27 Apr 2021 16:13:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619540026; 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=MQo3igXQ2ZrlT78vmfcbMjgqdCn30eweEx4RKB8geEQ=; b=GXugMs41Lg2WhU6t70DJte53tdotvshl5v3R/VwcSN8gUs60tIovGCn3bCjw75AtsOJ35n BJfDBJIUJRikrILUqefm7zLbCTUWKPNOwBmsqT8vp9SpPqehUSNHfKnG132KEDPvfh7If0 n8f7bgWQLStuvpNe/FQzyUBlWLSKebk= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-333-AaNLft8GOHeUUTGtOC6-oA-1; Tue, 27 Apr 2021 12:13:44 -0400 X-MC-Unique: AaNLft8GOHeUUTGtOC6-oA-1 Received: by mail-qk1-f200.google.com with SMTP id g76-20020a379d4f0000b02902e40532d832so17025115qke.20 for ; Tue, 27 Apr 2021 09:13:44 -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=MQo3igXQ2ZrlT78vmfcbMjgqdCn30eweEx4RKB8geEQ=; b=kIQVKKRBpiQDbP9lFyP29tgEBQhB18BEAzFSd47hym7JIxRUd5vPfTbBpRxsjlqr4a Nl7hhAZqnu3vGSzzFWs6fFIUrHkZOX9o3kCGd04WtwUbiRzUzQEcLbTf+BNxoWADic/p 5BbjUg1A1k8ytuxQDsHqJc9z4aXev1EdVVJTUVPLBsMs1w5XufK/keSjAVKBr3RLTzSR f6hP2f/K0j5137Z8nhHFBy916ODscPo7ATQ/6+p1yJM+0ZAK5PBz0rpjTaXgygXxalRH nLjWL929Dd8atVjqBiaIlr9AoJaXcz3dl9RjHCw79IqwIfMkJ3mWZ4ydGoCgxTvOP5bW DaTQ== X-Gm-Message-State: AOAM53329ElDwfrqcXD7fk39sBw+4La4t55921KtyogmsRF9tIesdI9A 0bG/eoM6VDdZeDMNgmlXBL47wVnwKDdBcHxgP/QhHHnSoEiEFBtVBh9yUvOoRpe1ZlWFn4c6pRd fUkwKd5nsTB8fSwGjGKAt45w1UFSvBLOB7yYCiFVNLXx02k/Rh2SeS9No+wbe X-Received: by 2002:a37:ae85:: with SMTP id x127mr23480375qke.436.1619540023608; Tue, 27 Apr 2021 09:13:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwBvftm878KrfXNKO7yJY30UGBZnCImne2Q/icuY+oWvYS3f5pjudN4L4FNOQSPKT1Zf9Wuiw== X-Received: by 2002:a37:ae85:: with SMTP id x127mr23480325qke.436.1619540023259; Tue, 27 Apr 2021 09:13:43 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-77-184-145-104-227.dsl.bell.ca. [184.145.104.227]) by smtp.gmail.com with ESMTPSA id v66sm3103621qkd.113.2021.04.27.09.13.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 09:13:42 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , Miaohe Lin , Mike Rapoport , Andrea Arcangeli , Hugh Dickins , peterx@redhat.com, Jerome Glisse , Mike Kravetz , Jason Gunthorpe , Matthew Wilcox , Andrew Morton , Axel Rasmussen , "Kirill A . Shutemov" Subject: [PATCH v2 13/24] shmem/userfaultfd: Handle the left-overed special swap ptes Date: Tue, 27 Apr 2021 12:13:06 -0400 Message-Id: <20210427161317.50682-14-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210427161317.50682-1-peterx@redhat.com> References: <20210427161317.50682-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="US-ASCII" X-Rspamd-Queue-Id: F03EE40001DE X-Stat-Signature: em47mcby4cjsmkz4djbwq5tumcntgg7r X-Rspamd-Server: rspam02 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf17; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1619540022-130948 Content-Transfer-Encoding: quoted-printable 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 infor= mation for the special swap ptes because they're not real mappings but just mark= ers. So we need to take care of that when we see a marker but when it's actual= ly meaningless (the page behind it got evicted). We have already taken care of that in e.g. alloc_set_pte() where we'll tr= eat the special swap pte as pte_none() when necessary. However we need to al= so teach userfaultfd itself on either UFFDIO_COPY or handling page faults, s= o that everything will still work as expected. Signed-off-by: Peter Xu --- fs/userfaultfd.c | 15 +++++++++++++++ mm/shmem.c | 13 ++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 5dd78238cc156..b34486a88b5f3 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -329,6 +329,21 @@ static inline bool userfaultfd_must_wait(struct user= faultfd_ctx *ctx, */ if (pte_none(*pte)) ret =3D 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 =3D true; if (!pte_write(*pte) && (reason & VM_UFFD_WP)) ret =3D true; pte_unmap(pte); diff --git a/mm/shmem.c b/mm/shmem.c index 8fbf7680f044c..a1f21736ad68e 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2453,7 +2453,18 @@ static int shmem_mfill_atomic_pte(struct mm_struct= *dst_mm, goto out_release_unlock; =20 ret =3D -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. + */ + if (!pte_none(*dst_pte) && !pte_swp_uffd_wp_special(*dst_pte)) goto out_release_unlock; =20 lru_cache_add(page); --=20 2.26.2