From patchwork Mon Nov 15 07:55:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12618851 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F18DEC433EF for ; Mon, 15 Nov 2021 07:56:20 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A69D76321A for ; Mon, 15 Nov 2021 07:56:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org A69D76321A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 46AEE6B0083; Mon, 15 Nov 2021 02:56:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F45C6B0085; Mon, 15 Nov 2021 02:56:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 296296B0087; Mon, 15 Nov 2021 02:56:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0236.hostedemail.com [216.40.44.236]) by kanga.kvack.org (Postfix) with ESMTP id 11BA16B0083 for ; Mon, 15 Nov 2021 02:56:20 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id C3E838249980 for ; Mon, 15 Nov 2021 07:56:19 +0000 (UTC) X-FDA: 78810406878.22.226AE73 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 5364EB0000B1 for ; Mon, 15 Nov 2021 07:56:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1636962978; 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=ubfx2uHai2Y0k1b/BYqRnZVepmNNtt1cqqHxspLPNNE=; b=LScwSVagYHBxtVtmrNKsle6JXYyd6OlEyV2yjuZTsEs09ti9fR9ozo7rauToZ046AZNNZs kEn6/6ueoPX7q4snclOdVulhPP2gncgj67BOs3XkzSAphUf4qVV0y+Pcb+v1ka8Wa8YklZ 1I5+rSwATj3r6YXRf3KvXaNtjNe/VGE= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-251-J9iDmPwRPkGj7hYL99oZYw-1; Mon, 15 Nov 2021 02:56:17 -0500 X-MC-Unique: J9iDmPwRPkGj7hYL99oZYw-1 Received: by mail-pj1-f72.google.com with SMTP id mv1-20020a17090b198100b001a67d5901d2so8210355pjb.7 for ; Sun, 14 Nov 2021 23:56:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ubfx2uHai2Y0k1b/BYqRnZVepmNNtt1cqqHxspLPNNE=; b=WC2eTBDu/OucFx+W5ihiPm141dhHzY1Pz7CKBFvuo8cpE5FnQZ7j8Vyi0Lnvkee/RN vSQQTolSuD7KW+l4VcApEMHf7Rw3s973dJDSt7WnkrO856wfbYJ2viiCPc2VWiTdMCiA +5PXphasuh1Z4MJgsA5QFwFYdM4w3WsCPXMvOrUX5hBAl9MmvzwxfV4x6Ox/3SaPNV6e l7MjZOcQSgF5cfh2XBpSuVgcC3hbATAcStypyS7bXTvJhIwzMNLmVNP19V9RuXbwu82e qq0IwZ2o2rhTaUN2CboyEdUJsI/GOOCRbQ/D0XyNRfLirZmvOsdfeGaxhzZxWLG3QYk6 23lg== X-Gm-Message-State: AOAM530E78GtxjTSjgFn8kfsdm+CQ+G/jwgXMUAmVDIMlEHStCg6Vngm kGIj+A+UmhpUHSsXbMnkvdXLqEm2MTd+bCtWtRNl0iSZOxnFiAQQSOJvYpyv7dmI1xShlKnj7jU Si2AyIaICXg+vsERkIKOvMW+thBhrdY2PXGNHlHPtCMf6cefXebbZPD9QwqN9 X-Received: by 2002:a17:902:b615:b0:143:bbf0:aad0 with SMTP id b21-20020a170902b61500b00143bbf0aad0mr16671635pls.12.1636962976302; Sun, 14 Nov 2021 23:56:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJwavIcr/CimjYOEqw/YJdq6Mqfcy+56LKYHxJhlNMkBwmXshfu7Q/OZQlTY3k8NhCo5kJhghQ== X-Received: by 2002:a17:902:b615:b0:143:bbf0:aad0 with SMTP id b21-20020a170902b61500b00143bbf0aad0mr16671588pls.12.1636962975927; Sun, 14 Nov 2021 23:56:15 -0800 (PST) Received: from localhost.localdomain ([191.101.132.223]) by smtp.gmail.com with ESMTPSA id e10sm15792796pfv.140.2021.11.14.23.56.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 14 Nov 2021 23:56:15 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Axel Rasmussen , Nadav Amit , Mike Rapoport , Hugh Dickins , Mike Kravetz , "Kirill A . Shutemov" , Alistair Popple , Jerome Glisse , Matthew Wilcox , Andrew Morton , peterx@redhat.com, David Hildenbrand , Andrea Arcangeli Subject: [PATCH v6 05/23] mm/shmem: Take care of UFFDIO_COPY_MODE_WP Date: Mon, 15 Nov 2021 15:55:04 +0800 Message-Id: <20211115075522.73795-6-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211115075522.73795-1-peterx@redhat.com> References: <20211115075522.73795-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 5364EB0000B1 X-Stat-Signature: gqi5j6xgk35an65uwctqiqcagqhofsad Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=LScwSVag; spf=none (imf24.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1636962979-769798 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: Pass wp_copy into shmem_mfill_atomic_pte() through the stack, then apply the UFFD_WP bit properly when the UFFDIO_COPY on shmem is with UFFDIO_COPY_MODE_WP. wp_copy lands mfill_atomic_install_pte() finally. Note: we must do pte_wrprotect() if !writable in mfill_atomic_install_pte(), as mk_pte() could return a writable pte (e.g., when VM_SHARED on a shmem file). Signed-off-by: Peter Xu --- include/linux/shmem_fs.h | 4 ++-- mm/shmem.c | 4 ++-- mm/userfaultfd.c | 30 +++++++++++++++++++++--------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 166158b6e917..0ee0f437b14f 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -145,11 +145,11 @@ extern int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - bool zeropage, + bool zeropage, bool wp_copy, struct page **pagep); #else /* !CONFIG_SHMEM */ #define shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, \ - src_addr, zeropage, pagep) ({ BUG(); 0; }) + src_addr, zeropage, wp_copy, pagep) ({ BUG(); 0; }) #endif /* CONFIG_SHMEM */ #endif /* CONFIG_USERFAULTFD */ diff --git a/mm/shmem.c b/mm/shmem.c index dc038ce78700..167a46e6a1ff 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2344,7 +2344,7 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - bool zeropage, + bool zeropage, bool wp_copy, struct page **pagep) { struct inode *inode = file_inode(dst_vma->vm_file); @@ -2415,7 +2415,7 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, goto out_release; ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, - page, true, false); + page, true, wp_copy); if (ret) goto out_delete_from_cache; diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index ac6f036298cd..95e5a9ba3196 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -70,14 +70,22 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, _dst_pte = mk_pte(page, dst_vma->vm_page_prot); _dst_pte = pte_mkdirty(_dst_pte); - if (page_in_cache && !vm_shared) + /* Don't write if uffd-wp wr-protected */ + if (wp_copy) { + _dst_pte = pte_mkuffd_wp(_dst_pte); writable = false; - if (writable) { - if (wp_copy) - _dst_pte = pte_mkuffd_wp(_dst_pte); - else - _dst_pte = pte_mkwrite(_dst_pte); } + /* Don't write if page cache privately mapped */ + if (page_in_cache && !vm_shared) + writable = false; + if (writable) + _dst_pte = pte_mkwrite(_dst_pte); + else + /* + * We need this to make sure write bit removed; as mk_pte() + * could return a pte with write bit set. + */ + _dst_pte = pte_wrprotect(_dst_pte); dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); @@ -92,7 +100,12 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, } ret = -EEXIST; - if (!pte_none(*dst_pte)) + /* + * We allow to overwrite a pte marker: consider when both MISSING|WP + * registered, we firstly wr-protect a none pte which has no page cache + * page backing it, then access the page. + */ + if (!pte_none_mostly(*dst_pte)) goto out_unlock; if (page_in_cache) @@ -467,11 +480,10 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, err = mfill_zeropage_pte(dst_mm, dst_pmd, dst_vma, dst_addr); } else { - VM_WARN_ON_ONCE(wp_copy); err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, src_addr, mode != MCOPY_ATOMIC_NORMAL, - page); + wp_copy, page); } return err;