From patchwork Tue Apr 27 16:12:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12226867 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 0E226C433ED for ; Tue, 27 Apr 2021 16:13:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 773BA61151 for ; Tue, 27 Apr 2021 16:13:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 773BA61151 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 EC7796B006E; Tue, 27 Apr 2021 12:13:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E9E248D0001; Tue, 27 Apr 2021 12:13:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D409F6B0071; Tue, 27 Apr 2021 12:13:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0061.hostedemail.com [216.40.44.61]) by kanga.kvack.org (Postfix) with ESMTP id B30DC6B006E for ; Tue, 27 Apr 2021 12:13:28 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 614D68249980 for ; Tue, 27 Apr 2021 16:13:28 +0000 (UTC) X-FDA: 78078642096.01.594B318 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 73C6EC0007E0 for ; Tue, 27 Apr 2021 16:13:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619540007; 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=sHnDMU05VPiYnBKDqKuhxjdmmK4rVbQVkQv2fE0zGZ4=; b=ZYVRnh9XnxYJT5/w2eBCbS/Jp2pLfp6DQIMOZPO6T6YW0YXhS6KZ0SDowbHFYB9U06ts0e IofcpF6V/iRX9wYy4UxWDTWTidXSYoh6FdGfLVa+EsN1DnGyO2p57ohYguLUjySDn+huE9 EnaxsLLazg8tAVG0aSkSqEQ510aLBws= 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-546-eLXLLL91PRKoYwELHyrsDQ-1; Tue, 27 Apr 2021 12:13:23 -0400 X-MC-Unique: eLXLLL91PRKoYwELHyrsDQ-1 Received: by mail-qv1-f70.google.com with SMTP id c5-20020a0ca9c50000b02901aede9b5061so14109358qvb.14 for ; Tue, 27 Apr 2021 09:13:23 -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=sHnDMU05VPiYnBKDqKuhxjdmmK4rVbQVkQv2fE0zGZ4=; b=IPbdz90jwUnXtrNiIzq7fwNB1vGSsrbldKG/fa3ZSg/WxcSw0U4Bxw/zWZENjNrmXZ gClELLS5yqhMesz5DGU8o3F5oUWwyWOH7KU/4BMjACAntTjRww0HA0CoDl+U4mJVZ4tt Gcok8PVCJb6WKJnLF8dIYeuofn5+ev48CTmulzUoJgoC8QSjEKvb4IU4lEXhcL3iWhAp iy9EEEhJSjPsm84sZYVccAz7U6VDhNLa3rCTtl/h+iOSTzEISxOB6a4w9AfLXfi+AHJ6 gov3GQBMe51PAO+yMtQuk7qW/U26rEZHDvSrADsA3staZyc5b9NoO3Ks7JZo9s8o4MZl Zsbw== X-Gm-Message-State: AOAM533sdfqdiIXHnOgYdN06N0Gd0r83QEE5h8Ea9kE7BT8ShXxfx0mW 0oFW7fvi9jyXi7aBVSf+FGKJDjnSzPaethzi0q7odgmcKMQzAQReSzBnSYd4r5/nn45dKqDHqtu RExCWZWWtKonlUvRpOJyUlo34vqFtEVTDdLBqup8lPcQqzTeoNARGEu7+BZ9e X-Received: by 2002:a05:620a:1026:: with SMTP id a6mr16061611qkk.357.1619540002206; Tue, 27 Apr 2021 09:13:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzK7lRS/kPuU3kySXb7Ky2ewLEdvChHwX5FGOdMQ2X/Vpu9E4UvJ1aP8a2K5CItcnmpbfQx2g== X-Received: by 2002:a05:620a:1026:: with SMTP id a6mr16061570qkk.357.1619540001874; Tue, 27 Apr 2021 09:13:21 -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.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 09:13:20 -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 01/24] shmem/userfaultfd: Take care of UFFDIO_COPY_MODE_WP Date: Tue, 27 Apr 2021 12:12:54 -0400 Message-Id: <20210427161317.50682-2-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 X-Stat-Signature: u4am4njq47ak6rqfxeu1hidwr73ethb1 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 73C6EC0007E0 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf03; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=170.10.133.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1619540002-775484 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: Firstly, 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. One thing to mention is that shmem_mfill_atomic_pte() needs to set the dirty bit in pte even if UFFDIO_COPY_MODE_WP is set. The reason is similar to dcf7fe9d8976 ("userfaultfd: shmem: UFFDIO_COPY: set the page dirty if VM_WRITE is not set") where we need to set page as dirty even if VM_WRITE is no there. It's just that shmem can drop the pte any time later, and if it's not dirty the data will be dropped. For uffd-wp, that could lead to data loss if without the dirty bit set. Note that shmem_mfill_zeropage_pte() will always call shmem_mfill_atomic_pte() with wp_copy==false because UFFDIO_ZEROCOPY does not support UFFDIO_COPY_MODE_WP. Signed-off-by: Peter Xu --- include/linux/shmem_fs.h | 5 +++-- mm/shmem.c | 27 ++++++++++++++++++++------- mm/userfaultfd.c | 2 +- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index d82b6f3965885..a21eb25183d02 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -127,14 +127,15 @@ extern int shmem_mcopy_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, - struct page **pagep); + struct page **pagep, + bool wp_copy); extern int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr); #else #define shmem_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma, dst_addr, \ - src_addr, pagep) ({ BUG(); 0; }) + src_addr, pagep, wp_copy) ({ BUG(); 0; }) #define shmem_mfill_zeropage_pte(dst_mm, dst_pmd, dst_vma, \ dst_addr) ({ BUG(); 0; }) #endif diff --git a/mm/shmem.c b/mm/shmem.c index 26c76b13ad233..8fbf7680f044c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2360,7 +2360,8 @@ static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, unsigned long dst_addr, unsigned long src_addr, bool zeropage, - struct page **pagep) + struct page **pagep, + bool wp_copy) { struct inode *inode = file_inode(dst_vma->vm_file); struct shmem_inode_info *info = SHMEM_I(inode); @@ -2422,9 +2423,18 @@ static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, goto out_release; _dst_pte = mk_pte(page, dst_vma->vm_page_prot); - if (dst_vma->vm_flags & VM_WRITE) - _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte)); - else { + if (dst_vma->vm_flags & VM_WRITE) { + if (wp_copy) + _dst_pte = pte_mkuffd_wp(pte_wrprotect(_dst_pte)); + else + _dst_pte = pte_mkwrite(_dst_pte); + /* + * Similar reason to set_page_dirty(), that we need to mark the + * pte dirty even if wp_copy==true here, otherwise the pte and + * its page could be dropped at anytime when e.g. swapped out. + */ + _dst_pte = pte_mkdirty(_dst_pte); + } else { /* * We don't set the pte dirty if the vma has no * VM_WRITE permission, so mark the page dirty or it @@ -2482,10 +2492,12 @@ int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - struct page **pagep) + struct page **pagep, + bool wp_copy) { return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, - dst_addr, src_addr, false, pagep); + dst_addr, src_addr, false, pagep, + wp_copy); } int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm, @@ -2496,7 +2508,8 @@ int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm, struct page *page = NULL; return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, - dst_addr, 0, true, &page); + dst_addr, 0, true, &page, + false); } #ifdef CONFIG_TMPFS diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index e14b3820c6a81..7adaebe222b8e 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -443,7 +443,7 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, if (!zeropage) err = shmem_mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, - src_addr, page); + src_addr, page, wp_copy); else err = shmem_mfill_zeropage_pte(dst_mm, dst_pmd, dst_vma, dst_addr);