Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1031288pxj; Thu, 27 May 2021 18:26:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyNlh+tKCPnh7vDPg0sFT6EYGmdwCfwfTWdNpIlG6Z2Awjjp3OqCBHpKiXMg8YYs8g8Bvt8 X-Received: by 2002:a05:6638:37a6:: with SMTP id w38mr6077625jal.106.1622165159927; Thu, 27 May 2021 18:25:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622165159; cv=none; d=google.com; s=arc-20160816; b=Lv+lf0LSy5eyW7c3+hvcPECH+N0Ck/SkE5q8egDQRFBGitZKKgRZF37wEY5GDAlaOu 0ZfFFBC8Fu3cKtl8IewULrHOod+7cjc8ZxRBfM3u8usRrLy+fhy0zPSZPNZ4ERkkPlOd Hzpa3LeKGQv9hmFxlrsaPH3LOJFM9SA43sOOu0T8gVBkD7Y2+zQtosi/CIYC8I0zxwMh RLynvShJh2FofJLFZEOVUye+KJxWVEvP8ECxE/UsbcOXhF6KPw88HBoj1s0HPwRn172J 4qk5UD/qH6GKw3lLkWqE4lzgCME50V5fteioe0Ng93Vcy1/WDhpvRRwjzC8f7EKmxAQG d2zw== 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=8MXCY9EtHu8l00OeGEX9KSWIeSxJZ91wjW9WQpZLBho=; b=NkD88qamwrga4OUEeikWYrelvaGaW0l1oNPRY1DakRkBCaS857SMFttSbwNeOKdn5B x4pG7i17bUGJ9HAiDQqFTIm6bvnXMcssogo18GeXkEVlHGgsNWqpWeOFKmN2U8tJ3yic ZWCF3PNl0I/2KHzjFAIcUFkl2jF5v0NIScoeBy9m6BeeT2DEBhC3QchjkobvwaF7wqOj a4KRix+Ggfg4Z0VZM1srFExXbhM4hJMPRLnwiDe6X4+ZJk4LzuODsuz7SJsY9HbnhQHP OUTLFlHG6uKFyNbcNtdgSSH+DVvPkVDJhWftMQ2XtIxqDTdr5gNddA94a4UJ+yXrlPn/ p25Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZUzY7+vu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y10si2137432iom.14.2021.05.27.18.25.46; Thu, 27 May 2021 18:25:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZUzY7+vu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S235820AbhE0UY3 (ORCPT + 99 others); Thu, 27 May 2021 16:24:29 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:46576 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235646AbhE0UY1 (ORCPT ); Thu, 27 May 2021 16:24:27 -0400 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8MXCY9EtHu8l00OeGEX9KSWIeSxJZ91wjW9WQpZLBho=; b=ZUzY7+vux4VUkS9Z5MtKin513ejl8FoIxgHapzfHsbH5MBVzNFNkFf4SYjtvUYy8R65q3Q 6BOfwl2h3qeb0UwB5t3gmjdrsMea7soEeCrdYm30t3c5KI3JzMQOdUgImBaaHDaoeDg2Fq LLpe0FhuXdw/W4HKTqbIiyf3qD0pN4A= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-374-nNYQVxHiMjGbMDvltnG-FA-1; Thu, 27 May 2021 16:22:52 -0400 X-MC-Unique: nNYQVxHiMjGbMDvltnG-FA-1 Received: by mail-qv1-f70.google.com with SMTP id w14-20020a056214012eb02901f3a4388530so996661qvs.17 for ; Thu, 27 May 2021 13:22:52 -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=II1fu5mRuIkwBjOHxdmKS+2Uj7Cu5dggPlocFO4Ik4ZdXF15uo3KA0yAZgGYfFmlrk 4QIjlso+GwRlCH6zPrEH3bp0LZBZzpnfBzERE4LxDKgIyovF9wpGTUg+KGiu8ZpZnfRJ sUa4+Oc/nVCg/CkuxFtXwBLO1l3OYQci4rdDJQUBTi3wN8HKsDK6H5XjVakGdZdSWuhV t67QrjLbvSBNWeuAEIJELuD2t2+LViNYJuEi+E13qPbOUoc2h3uxxx6cFgmhLRuwqwOa 1914fa19dkgPtqq3+wPj5akgf9shNtPhNSmiOPn7xVw35QROoOcifRIAHiEhrN0Zk+7U JUDA== X-Gm-Message-State: AOAM533YXbZiK8XaiWWGqK5n2RUKXgOmTbIopub0Buphe+dXBDJCSqIu yP50+lIffJe89R6TydWTwy8OfWvKrQe2MncLMQY3H9idqPKqjwhc5oO5LCL8fvMrCCivzb9j+sL 0qn6azeXQ1N9H6ouOmRMI+oN5roTvutt9fSuqpKZ6OgTqgggvH+TLmwO9DPn6OpESa+ABLNV1Tg == X-Received: by 2002:a05:620a:787:: with SMTP id 7mr285039qka.397.1622146970839; Thu, 27 May 2021 13:22:50 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 when 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 server 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 pgtable entry does not exist. Note that this patch only covers the small pages (pte level) but not covering 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 += PAGE_SIZE, addr != 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; } +/* + * File-backed vma allows uffd wr-protect upon none ptes, because even if pte + * is missing, page/swap cache could exist. When that happens, the wr-protect + * 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 not + * exist, it means the whole range covered by the pmd entry (of a pud) does 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 vm_area_struct *vma, next = pmd_addr_end(addr, end); + 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 -- 2.31.1