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 37987C4707F for ; Thu, 27 May 2021 20:22:55 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CD338613CC for ; Thu, 27 May 2021 20:22:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CD338613CC 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 682C16B006C; Thu, 27 May 2021 16:22:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 658ED6B006E; Thu, 27 May 2021 16:22:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4FA0E6B0070; Thu, 27 May 2021 16:22:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0030.hostedemail.com [216.40.44.30]) by kanga.kvack.org (Postfix) with ESMTP id 1D8646B006C for ; Thu, 27 May 2021 16:22:54 -0400 (EDT) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id B2DD7181AF5C7 for ; Thu, 27 May 2021 20:22:53 +0000 (UTC) X-FDA: 78188134626.18.17031DC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 85CF6600027A for ; Thu, 27 May 2021 20:22:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622146973; 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=8MXCY9EtHu8l00OeGEX9KSWIeSxJZ91wjW9WQpZLBho=; b=JNJb190BzckPJojNXouHff0883NwMCeoZ4IdxWt9SGSuKZt2Tkal5CXDUMTFKAPs4uGI4Y bkZn7M/gFfn5gEsmWq4rELB4X+6EBzn/6LxObwodL2IKynjiVcazIdKSvFGxRuqvS/GvvX FIH4/DlbmNHPGAyieMrnphjMJpzF19M= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-308-7wOOhiiVPQe_3e8eKCz2sQ-1; Thu, 27 May 2021 16:22:51 -0400 X-MC-Unique: 7wOOhiiVPQe_3e8eKCz2sQ-1 Received: by mail-qv1-f72.google.com with SMTP id j14-20020a0cf30e0000b02902142ba51ef2so1021360qvl.10 for ; Thu, 27 May 2021 13:22:51 -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=8MXCY9EtHu8l00OeGEX9KSWIeSxJZ91wjW9WQpZLBho=; b=nrRaxrb17UOBViHDTGTfmUz3AOTIgn+rNMb5nfmsOgndgU2yln/VtcIuG264axBh+o zfyApDK6M3RAPsCoR2LBYan7HF/ccpjUDpKm+OcGkx4dgE9bObGBthcR1n2XaehzbzOG RX/c3p1zzzVhtmVhlvPBQraOR0QDu4KV5Vtd2NP0uA09gQptIro/mKe769V8SGVO4glb KeEbs7OFfDMNLEitEQB7FXxdW5ogcL+s28tfIagHs89YIt5QXcVWdwtioBxmrGlBBMqc i5QxJc6czF9E9SlNsWxU6pgHff+HPPj+pF8KVyy/oonsgWzLeAeFJy+mx2bhH+a6RYnP xo+Q== X-Gm-Message-State: AOAM533WaxoWwIqb9nrDk6tECcd7f7wNV1g/IBazxycSZaSfY87KhbID Y+5Qzq/LajSGxjXEc47fXDTO313+TjrkXTjfZmK/voptlkOlSSGtspS9moTrYtX5CBLXp25B8pt +kLcvKZS07qc= X-Received: by 2002:a05:620a:787:: with SMTP id 7mr285034qka.397.1622146970779; Thu, 27 May 2021 13:22:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxRUikWEOLkY4CRYqDWC3dJNWhf0mol6ag7uMDBXVUXW2OhGQF4dTOGhSEyiU91Ub93XNgb1A== X-Received: by 2002:a05:620a:787:: with SMTP id 7mr284996qka.397.1622146970458; Thu, 27 May 2021 13:22:50 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id n19sm1977380qtp.65.2021.05.27.13.22.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 May 2021 13:22:49 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Rapoport , peterx@redhat.com, Andrew Morton , Mike Kravetz , Jerome Glisse , Miaohe Lin , Nadav Amit , Hugh Dickins , Matthew Wilcox , Jason Gunthorpe , "Kirill A . Shutemov" , Andrea Arcangeli , Axel Rasmussen Subject: [PATCH v3 12/27] shmem/userfaultfd: Allow wr-protect none pte for file-backed mem Date: Thu, 27 May 2021 16:22:47 -0400 Message-Id: <20210527202247.31613-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210527201927.29586-1-peterx@redhat.com> References: <20210527201927.29586-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="US-ASCII" Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JNJb190B; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf09.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 85CF6600027A X-Stat-Signature: iuxg3jghpsneaf9e9tbejic3os1thnnf X-HE-Tag: 1622146968-17280 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: File-backed memory differs from anonymous memory in that even if the pte = is missing, the data could still resides either in the file or in page/swap = cache. So when wr-protect a pte, we need to consider none ptes too. We do that by installing the uffd-wp special swap pte as a marker. So wh= en there's a future write to the pte, the fault handler will go the special = path to first fault-in the page as read-only, then report to userfaultfd serve= r with the wr-protect message. On the other hand, when unprotecting a page, it's also possible that the = pte got unmapped but replaced by the special uffd-wp marker. Then we'll need= to be able to recover from a uffd-wp special swap pte into a none pte, so that = the next access to the page will fault in correctly as usual when trigger the= fault handler next time, rather than sending a uffd-wp message. Special care needs to be taken throughout the change_protection_range() process. Since now we allow user to wr-protect a none pte, we need to be= able to pre-populate the page table entries if we see !anonymous && MM_CP_UFFD= _WP requests, otherwise change_protection_range() will always skip when the p= gtable entry does not exist. Note that this patch only covers the small pages (pte level) but not cove= ring any of the transparent huge pages yet. But this will be a base for thps = too. Signed-off-by: Peter Xu --- mm/mprotect.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/mm/mprotect.c b/mm/mprotect.c index 4b743394afbe..8ec85b276975 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -186,6 +187,32 @@ static unsigned long change_pte_range(struct vm_area= _struct *vma, pmd_t *pmd, set_pte_at(vma->vm_mm, addr, pte, newpte); pages++; } + } else if (unlikely(is_swap_special_pte(oldpte))) { + if (uffd_wp_resolve && !vma_is_anonymous(vma) && + pte_swp_uffd_wp_special(oldpte)) { + /* + * This is uffd-wp special pte and we'd like to + * unprotect it. What we need to do is simply + * recover the pte into a none pte; the next + * page fault will fault in the page. + */ + pte_clear(vma->vm_mm, addr, pte); + pages++; + } + } else { + /* It must be an none page, or what else?.. */ + WARN_ON_ONCE(!pte_none(oldpte)); + if (unlikely(uffd_wp && !vma_is_anonymous(vma))) { + /* + * For file-backed mem, we need to be able to + * wr-protect even for a none pte! Because + * even if the pte is null, the page/swap cache + * could exist. + */ + set_pte_at(vma->vm_mm, addr, pte, + pte_swp_mkuffd_wp_special(vma)); + pages++; + } } } while (pte++, addr +=3D PAGE_SIZE, addr !=3D end); arch_leave_lazy_mmu_mode(); @@ -219,6 +246,25 @@ static inline int pmd_none_or_clear_bad_unless_trans= _huge(pmd_t *pmd) return 0; } =20 +/* + * File-backed vma allows uffd wr-protect upon none ptes, because even i= f pte + * is missing, page/swap cache could exist. When that happens, the wr-p= rotect + * information will be stored in the page table entries with the marker = (e.g., + * PTE_SWP_UFFD_WP_SPECIAL). Prepare for that by always populating the = page + * tables to pte level, so that we'll install the markers in change_pte_= range() + * where necessary. + * + * Note that we only need to do this in pmd level, because if pmd does n= ot + * exist, it means the whole range covered by the pmd entry (of a pud) d= oes not + * contain any valid data but all zeros. Then nothing to wr-protect. + */ +#define change_protection_prepare(vma, pmd, addr, cp_flags) \ + do { \ + if (unlikely((cp_flags & MM_CP_UFFD_WP) && pmd_none(*pmd) && \ + !vma_is_anonymous(vma))) \ + WARN_ON_ONCE(pte_alloc(vma->vm_mm, pmd)); \ + } while (0) + static inline unsigned long change_pmd_range(struct vm_area_struct *vma, pud_t *pud, unsigned long addr, unsigned long end, pgprot_t newprot, unsigned long cp_flags) @@ -237,6 +283,8 @@ static inline unsigned long change_pmd_range(struct v= m_area_struct *vma, =20 next =3D pmd_addr_end(addr, end); =20 + change_protection_prepare(vma, pmd, addr, cp_flags); + /* * Automatic NUMA balancing walks the tables with mmap_lock * held for read. It's possible a parallel update to occur --=20 2.31.1