From patchwork Tue Mar 23 00:50:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12156519 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 BBF0BC433E0 for ; Tue, 23 Mar 2021 00:51:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 65BDF6196C for ; Tue, 23 Mar 2021 00:51:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 65BDF6196C 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 F420C6B0140; Mon, 22 Mar 2021 20:51:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC9716B0142; Mon, 22 Mar 2021 20:51:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD1E56B0143; Mon, 22 Mar 2021 20:51:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0203.hostedemail.com [216.40.44.203]) by kanga.kvack.org (Postfix) with ESMTP id A4D856B0140 for ; Mon, 22 Mar 2021 20:51:03 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 6F1B81803F9A7 for ; Tue, 23 Mar 2021 00:51:03 +0000 (UTC) X-FDA: 77949309606.12.43F9AF0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf27.hostedemail.com (Postfix) with ESMTP id 69A5180192C0 for ; Tue, 23 Mar 2021 00:51:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616460662; 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=gMz4zykUsRqLQsQnpjugwWFonlMak/AsLri3yN76IU8=; b=QTxs42UBsYO+oy1OAP26VABNFhFommfYiJCSiw1W1wCrcMhP3v9crffMPHwv2QK4JoPGbk dgzCM6DiGOVyM0A9d6P3vhE0BF2jDG9IOOU1zeMbwp8qwGjjFCNK7ToXyhwFEUhtaQTmvn zwhAeTV9GKl67wWPFqDBIi/WBwGmlhw= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-215-1PImHJzUNRCHDkrDPtkYNQ-1; Mon, 22 Mar 2021 20:51:00 -0400 X-MC-Unique: 1PImHJzUNRCHDkrDPtkYNQ-1 Received: by mail-qv1-f69.google.com with SMTP id k92so542369qva.20 for ; Mon, 22 Mar 2021 17:51:00 -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=gMz4zykUsRqLQsQnpjugwWFonlMak/AsLri3yN76IU8=; b=kwEOoCVC7EqoSlcLwwuRPBnXsXXirfvam06YNKlHnhKkKZeL3Q/Zo87w0NZ6REW3X+ eLt/0ZqJUJwhVkvv4BShMYvKV/qizXjTFDpobCHKaA8ptLMm1Tr2FlGaCd2Q1402144p CMlPyug+YRtWMBWnf96p0xBba/KW7LJheQtgzO/Y7jxf3JDbk6OTONI32dThra91YAB2 C7SCKQFldmk38zDvEkILEXnFt83vBxZq+6QSH5pKWkN30GdU8jG2bWS0kSS7xdsJg5BI 9giSkQOQUbFHm4xXPRwiAGrk37bD1PV6+D/rL+TebOO1NNEETepvcDYBe61VhNLRVIFy Swug== X-Gm-Message-State: AOAM531ji7Qpss+ElLBxb9/52xejPQxdV6/4TIYxzuHUu3NQysXWHolu QipsaWRNM5Cc2aQ6PXIalklNE2JYFwHTSBWbPqSe+rDQw3V7WBBgNDmwjqMYh89dk1AiUeZzghb bPm5inJiqxetcXjpzpwyfP6zUXASJmeT+AankIn3x+Dp7hte1Z082MzyyPVrR X-Received: by 2002:a37:6848:: with SMTP id d69mr2970021qkc.159.1616460659760; Mon, 22 Mar 2021 17:50:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3wt9SmWeNMBwi1yvJ4wgntypuHoNSgIQv+r28OKmzDrhpIEGPoRQZMlpRSyZw5Ld0JfAMuA== X-Received: by 2002:a37:6848:: with SMTP id d69mr2969993qkc.159.1616460659449; Mon, 22 Mar 2021 17:50:59 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id c27sm12337483qko.71.2021.03.22.17.50.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 17:50:58 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Mike Rapoport , Nadav Amit , Jerome Glisse , Hugh Dickins , Andrea Arcangeli , Andrew Morton , "Kirill A . Shutemov" , Axel Rasmussen , peterx@redhat.com, Mike Kravetz , Matthew Wilcox Subject: [PATCH 22/23] mm/userfaultfd: Enable write protection for shmem & hugetlbfs Date: Mon, 22 Mar 2021 20:50:57 -0400 Message-Id: <20210323005057.36027-1-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210323004912.35132-1-peterx@redhat.com> References: <20210323004912.35132-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 X-Stat-Signature: husq4fgup57jay8d4et3xyezw3dah1mz X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 69A5180192C0 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf27; 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: 1616460662-273169 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: We've had all the necessary changes ready for both shmem and hugetlbfs. Turn on all the shmem/hugetlbfs switches for userfaultfd-wp. Now we can remove the flags parameter for vma_can_userfault() since not used any more. Meanwhile, we can expand UFFD_API_RANGE_IOCTLS_BASIC with _UFFDIO_WRITEPROTECT too because all existing types now support write protection mode. Since vma_can_userfault() will be used elsewhere, move into userfaultfd_k.h. Signed-off-by: Peter Xu --- fs/userfaultfd.c | 18 ------------------ include/linux/userfaultfd_k.h | 14 ++++++++++++++ include/uapi/linux/userfaultfd.h | 7 +++++-- mm/userfaultfd.c | 10 +++------- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index f6fa34f58c37..b4f30fe84aa3 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1275,24 +1275,6 @@ static __always_inline int validate_range(struct mm_struct *mm, return 0; } -static inline bool vma_can_userfault(struct vm_area_struct *vma, - unsigned long vm_flags) -{ - /* FIXME: add WP support to hugetlbfs and shmem */ - if (vm_flags & VM_UFFD_WP) { - if (is_vm_hugetlb_page(vma) || vma_is_shmem(vma)) - return false; - } - - if (vm_flags & VM_UFFD_MINOR) { - if (!(is_vm_hugetlb_page(vma) || vma_is_shmem(vma))) - return false; - } - - return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) || - vma_is_shmem(vma); -} - static int userfaultfd_register(struct userfaultfd_ctx *ctx, unsigned long arg) { diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index fefebe6e9656..413323fc81ca 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -16,6 +16,7 @@ #include #include #include +#include /* The set of all possible UFFD-related VM flags. */ #define __VM_UFFD_FLAGS (VM_UFFD_MISSING | VM_UFFD_WP | VM_UFFD_MINOR) @@ -132,6 +133,19 @@ static inline bool userfaultfd_armed(struct vm_area_struct *vma) return vma->vm_flags & __VM_UFFD_FLAGS; } +static inline bool vma_can_userfault(struct vm_area_struct *vma, + unsigned long vm_flags) +{ + if (vm_flags & VM_UFFD_MINOR) { + if (!(is_vm_hugetlb_page(vma) || vma_is_shmem(vma))) + return false; + } + + return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) || + vma_is_shmem(vma); +} + + extern int dup_userfaultfd(struct vm_area_struct *, struct list_head *); extern void dup_userfaultfd_complete(struct list_head *); diff --git a/include/uapi/linux/userfaultfd.h b/include/uapi/linux/userfaultfd.h index 47d9790d863d..000cc4cfc787 100644 --- a/include/uapi/linux/userfaultfd.h +++ b/include/uapi/linux/userfaultfd.h @@ -32,7 +32,8 @@ UFFD_FEATURE_SIGBUS | \ UFFD_FEATURE_THREAD_ID | \ UFFD_FEATURE_MINOR_HUGETLBFS | \ - UFFD_FEATURE_MINOR_SHMEM) + UFFD_FEATURE_MINOR_SHMEM | \ + UFFD_FEATURE_WP_HUGETLBFS_SHMEM) #define UFFD_API_IOCTLS \ ((__u64)1 << _UFFDIO_REGISTER | \ (__u64)1 << _UFFDIO_UNREGISTER | \ @@ -46,7 +47,8 @@ #define UFFD_API_RANGE_IOCTLS_BASIC \ ((__u64)1 << _UFFDIO_WAKE | \ (__u64)1 << _UFFDIO_COPY | \ - (__u64)1 << _UFFDIO_CONTINUE) + (__u64)1 << _UFFDIO_CONTINUE | \ + (__u64)1 << _UFFDIO_WRITEPROTECT) /* * Valid ioctl command number range with this API is from 0x00 to @@ -198,6 +200,7 @@ struct uffdio_api { #define UFFD_FEATURE_THREAD_ID (1<<8) #define UFFD_FEATURE_MINOR_HUGETLBFS (1<<9) #define UFFD_FEATURE_MINOR_SHMEM (1<<10) +#define UFFD_FEATURE_WP_HUGETLBFS_SHMEM (1<<11) __u64 features; __u64 ioctls; diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index a2b0dcc80a19..e6c6095878bb 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -452,7 +452,6 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, break; } } else { - VM_WARN_ON_ONCE(wp_copy); err = shmem_mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, src_addr, mode, page, wp_copy); } @@ -683,15 +682,12 @@ int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, err = -ENOENT; dst_vma = find_dst_vma(dst_mm, start, len); - /* - * Make sure the vma is not shared, that the dst range is - * both valid and fully within a single existing vma. - */ - if (!dst_vma || (dst_vma->vm_flags & VM_SHARED)) + + if (!dst_vma) goto out_unlock; if (!userfaultfd_wp(dst_vma)) goto out_unlock; - if (!vma_is_anonymous(dst_vma)) + if (!vma_can_userfault(dst_vma, dst_vma->vm_flags)) goto out_unlock; if (is_vm_hugetlb_page(dst_vma)) {