Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp10794225imu; Thu, 6 Dec 2018 06:56:35 -0800 (PST) X-Google-Smtp-Source: AFSGD/XIQjEbsww1sdliRt8eqm7M2A00HUWgRl8lEdxOY+ga+0wZI555Kd8cjrs2gquD7/Bx2t5Y X-Received: by 2002:a62:6303:: with SMTP id x3mr29710230pfb.110.1544108195890; Thu, 06 Dec 2018 06:56:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544108195; cv=none; d=google.com; s=arc-20160816; b=Uis5RECPNpY+AfNq/kkW+Ra1k4g98MqAvimOFNhMOL890wYOsm7FyWQhlI3+1bCVjV hYg9KnLAitSoPKu5vxIbNEuUVKiuroI/+c8mnbTfqBHNxMbpLKW4nGRtBDGAMLIe1NOx ED2QzOSucHzJpR+PsUV7312ayDzqNT7iRBe7DDMa8irM3n1vlgz1MW83QKY/lW4BBB7G fX1RgnWCwOFdL2vheCMTMchur5P519lW9Ab5f1WW5XCKY9taXiXOePo7co8xix9a5A0k wfwvX4TUoIhPRwsJ9SqPZEGyfahC5DYJk6V7xomVDcFyqqgI79ZXc39pY2IYwKgR4L9L 3hfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=KrM5zea7xvpowowVHDgCsaFO/ro3Oxmk/dIq0X4AQGI=; b=f0iqYBxzHbM5JEu4fs1K3dSsIxnfZ//kpaRCn7RajnBKTh7FbipakfSMNMJJRVRJKy Rlt/csATJIePx3iTsGqCcinTI5ROnV4rWROHqhAb/MkLHXslTRnY7YePBtAbbG5EZ0+o Xsqum3GlhDpS2B33ZK5jJh9AbBwXnpMMPqKF2fhHPU7BNz+kI6iwnnhtmZC4fROXfGsP YIVhF5dIrIG7jQu31tZjMyPbyIYL896TucFb0qYmMvReZcYgWE03nrz9WHrbJ14PWgA8 2oQDberlq0Lgq5s8DyPhrGqmS2Tm6lQjvFAb+uN7WCMGW/QIH3jczQnlzvQwMtbbG4DC zMlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=w6C921TX; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n187si474394pfn.83.2018.12.06.06.56.17; Thu, 06 Dec 2018 06:56:35 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=w6C921TX; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730755AbeLFOnv (ORCPT + 99 others); Thu, 6 Dec 2018 09:43:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:48302 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730241AbeLFOnr (ORCPT ); Thu, 6 Dec 2018 09:43:47 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C6EAA20892; Thu, 6 Dec 2018 14:43:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544107426; bh=hJAY9MHq6YJiDw9xDHCSHyvOWbT8lTxHWzqBQ+8LupY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=w6C921TXjZY2h0qu1HrbDq7qDFpBHl1WhkiDX0kEgiJ3WA6EyBgCZfpaRKqFtC5AW QvlwQM4ftNUwADBca3kkG/C/0MRUD2mBbeI84A4b/e1UILDJ5e06Ea3dkXxYxg9oai yBm2hGOTUViQb7SZAxlQ5fbjomDju35f3VwGmnfY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrea Arcangeli , Mike Rapoport , Hugh Dickins , "Dr. David Alan Gilbert" , Jann Horn , Mike Kravetz , Peter Xu , Andrew Morton , Linus Torvalds Subject: [PATCH 4.14 23/55] userfaultfd: shmem: allocate anonymous memory for MAP_PRIVATE shmem Date: Thu, 6 Dec 2018 15:38:57 +0100 Message-Id: <20181206143003.002397136@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181206143001.749982936@linuxfoundation.org> References: <20181206143001.749982936@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Andrea Arcangeli commit 5b51072e97d587186c2f5390c8c9c1fb7e179505 upstream. Userfaultfd did not create private memory when UFFDIO_COPY was invoked on a MAP_PRIVATE shmem mapping. Instead it wrote to the shmem file, even when that had not been opened for writing. Though, fortunately, that could only happen where there was a hole in the file. Fix the shmem-backed implementation of UFFDIO_COPY to create private memory for MAP_PRIVATE mappings. The hugetlbfs-backed implementation was already correct. This change is visible to userland, if userfaultfd has been used in unintended ways: so it introduces a small risk of incompatibility, but is necessary in order to respect file permissions. An app that uses UFFDIO_COPY for anything like postcopy live migration won't notice the difference, and in fact it'll run faster because there will be no copy-on-write and memory waste in the tmpfs pagecache anymore. Userfaults on MAP_PRIVATE shmem keep triggering only on file holes like before. The real zeropage can also be built on a MAP_PRIVATE shmem mapping through UFFDIO_ZEROPAGE and that's safe because the zeropage pte is never dirty, in turn even an mprotect upgrading the vma permission from PROT_READ to PROT_READ|PROT_WRITE won't make the zeropage pte writable. Link: http://lkml.kernel.org/r/20181126173452.26955-3-aarcange@redhat.com Fixes: 4c27fe4c4c84 ("userfaultfd: shmem: add shmem_mcopy_atomic_pte for userfaultfd support") Signed-off-by: Andrea Arcangeli Reported-by: Mike Rapoport Reviewed-by: Hugh Dickins Cc: Cc: "Dr. David Alan Gilbert" Cc: Jann Horn Cc: Mike Kravetz Cc: Peter Xu Cc: stable@vger.kernel.org Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/userfaultfd.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -381,7 +381,17 @@ static __always_inline ssize_t mfill_ato { ssize_t err; - if (vma_is_anonymous(dst_vma)) { + /* + * The normal page fault path for a shmem will invoke the + * fault, fill the hole in the file and COW it right away. The + * result generates plain anonymous memory. So when we are + * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll + * generate anonymous memory directly without actually filling + * the hole. For the MAP_PRIVATE case the robustness check + * only happens in the pagetable (to verify it's still none) + * and not in the radix tree. + */ + if (!(dst_vma->vm_flags & VM_SHARED)) { if (!zeropage) err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, src_addr, page); @@ -480,7 +490,8 @@ retry: * dst_vma. */ err = -ENOMEM; - if (vma_is_anonymous(dst_vma) && unlikely(anon_vma_prepare(dst_vma))) + if (!(dst_vma->vm_flags & VM_SHARED) && + unlikely(anon_vma_prepare(dst_vma))) goto out_unlock; while (src_addr < src_start + len) {