From patchwork Thu Jul 15 20:13:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12380927 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 2B5F1C636CA for ; Thu, 15 Jul 2021 20:14:35 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D0009613C4 for ; Thu, 15 Jul 2021 20:14:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D0009613C4 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 80A138D00FF; Thu, 15 Jul 2021 16:14:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E4318D00EC; Thu, 15 Jul 2021 16:14:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 60D558D00FF; Thu, 15 Jul 2021 16:14:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0116.hostedemail.com [216.40.44.116]) by kanga.kvack.org (Postfix) with ESMTP id 40F928D00EC for ; Thu, 15 Jul 2021 16:14:34 -0400 (EDT) Received: from smtpin38.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 275CA2570B for ; Thu, 15 Jul 2021 20:14:33 +0000 (UTC) X-FDA: 78365924826.38.513CFC6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf22.hostedemail.com (Postfix) with ESMTP id C742B19AE for ; Thu, 15 Jul 2021 20:14:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626380072; 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=OlGOfsM9rm2m+cZAonb5FrW/IEnwBmumPPW7EcnV+EI=; b=J7KLmAYTiaXMPbhaaFHeluqFTD8SRhN2CZgKyRWDcnH/mpIpQPenmG6fPe60kq9HSftaYU +QQxpZ3uzAuQ3s8uZ1rHl2rGI1SpOhN7fR/w1imEuZOAQM9P9FahdnOyLqZPSx+iGxh7Rq sCJsWXg9cKsu3SQrZxg/7sja8cjdeUY= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-317-V0pGbWNWOQWV_qYen-2yhw-1; Thu, 15 Jul 2021 16:14:31 -0400 X-MC-Unique: V0pGbWNWOQWV_qYen-2yhw-1 Received: by mail-qk1-f197.google.com with SMTP id k63-20020a37a1420000b02903b4fb67f606so4676507qke.10 for ; Thu, 15 Jul 2021 13:14:31 -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=OlGOfsM9rm2m+cZAonb5FrW/IEnwBmumPPW7EcnV+EI=; b=obLUvfg6uaOmuxiUSr4GrZH8e6zpd9WarEcVP6MY8TYBO7It1VSazhk9xfnYOfAzIC 44JagnZtvrqvVGdgxbjxUTclxRtB0YgobIRLIDIBzvnCnJ3HkCW9fqb/kckWZiUPmXF0 kgNShBearA76U84H2Vd/tJQiQe7MNC+Ad9+V9o+0hl08FJkP6ZLYxB0j+6Lu1vCnRkDF PtG4uQXc8ObfyiSSwPy7NzOdbDSPQV7Vm/ESnUbc66e7g4zIEBUB7p5TxghPgXPhYDZk F1XSog0HmUegPSfwv50XP9kLnP5FTVOh0p/d0FqTZR5cDFYJ6HqB+rvltAe3e7S4ss5e 4V8A== X-Gm-Message-State: AOAM531hSMZSs17mRrrbEzWxO02+poabNmktEwHAMZNJAnHwNZ2G+NZi 3iYU/KsbiBjSIUaQx9g81oRlunSb72at5Y7CeyckD12IQXQHfXu71A+Hi0ucmcYtSefwl4nT82g C26IITUIx3xjetpS/1gXuVuUXXcIClL3ASzHUEWuGlIGLGviaRqkiVHTbMx7e X-Received: by 2002:ad4:56e8:: with SMTP id cr8mr6203155qvb.53.1626380070375; Thu, 15 Jul 2021 13:14:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx0uhkf6wYkI04VL2sZsW5m+cA6f4n1NaOa24iDjGZ7k4OHdI1+cuzgsVl+sEeChu1fm59chA== X-Received: by 2002:ad4:56e8:: with SMTP id cr8mr6203119qvb.53.1626380070078; Thu, 15 Jul 2021 13:14:30 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-65-184-144-111-238.dsl.bell.ca. [184.144.111.238]) by smtp.gmail.com with ESMTPSA id p64sm2915206qka.114.2021.07.15.13.14.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jul 2021 13:14:29 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Jason Gunthorpe , Mike Kravetz , David Hildenbrand , Alistair Popple , Matthew Wilcox , "Kirill A . Shutemov" , Hugh Dickins , Tiberiu Georgescu , Andrea Arcangeli , Axel Rasmussen , Nadav Amit , Mike Rapoport , Jerome Glisse , Andrew Morton , Miaohe Lin , peterx@redhat.com Subject: [PATCH v5 02/26] shmem/userfaultfd: Take care of UFFDIO_COPY_MODE_WP Date: Thu, 15 Jul 2021 16:13:58 -0400 Message-Id: <20210715201422.211004-3-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210715201422.211004-1-peterx@redhat.com> References: <20210715201422.211004-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: C742B19AE X-Stat-Signature: qyzgkixaab9yqn18ukbdcbngshrpp5wg Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=J7KLmAYT; spf=none (imf22.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1626380072-427653 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, then wp_copy lands mfill_atomic_install_pte() which is newly introduced very recently. We need to make sure shmem_mfill_atomic_pte() will always set the dirty bit in pte even if UFFDIO_COPY_MODE_WP is set. After the rework of minor fault series on shmem we need to slightly touch up the logic there, since uffd-wp needs to be applied even if writable==false previously (e.g., for shmem private mapping). 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 | 23 +++++++++++++++-------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 8e775ce517bb..ba6ea8c4d534 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -128,11 +128,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 dc9f95b5fb34..9608200f8a64 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2368,7 +2368,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); @@ -2439,7 +2439,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 b30a3724c701..2a9c9e6eb876 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); @@ -467,11 +475,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;