From patchwork Mon Jan 21 07:57:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 10772795 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 19E011390 for ; Mon, 21 Jan 2019 07:58:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0B46129D39 for ; Mon, 21 Jan 2019 07:58:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F2CC129D3D; Mon, 21 Jan 2019 07:58:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7C17F29D39 for ; Mon, 21 Jan 2019 07:58:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA2298E0009; Mon, 21 Jan 2019 02:58:23 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A51958E0001; Mon, 21 Jan 2019 02:58:23 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 968BD8E0009; Mon, 21 Jan 2019 02:58:23 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by kanga.kvack.org (Postfix) with ESMTP id 6A1FE8E0001 for ; Mon, 21 Jan 2019 02:58:23 -0500 (EST) Received: by mail-qk1-f199.google.com with SMTP id c71so18252685qke.18 for ; Sun, 20 Jan 2019 23:58:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=E5pUTQPw9eOs30SK4xNMDv9HtqOmCvKjdxalnhHddoE=; b=LOr7IuDQ/3Oz20iZN+TAYkm+03vhQe/CcK2porXWxlW3UXfmtKDM4RYltcYmlcJMEw gKefbqDJuhUBrx6+520i8wtwvlLjvELHjg+d68hCNoUJiPK6Jvppipu7F4z1+hz1/QmK fq+pUoZx/zUkS9+hfKoSTqx6XeIBijXY56OAblRudeyo7cc2AGrG5awRtxPKXuj806yJ wIiUD9cun4Dl/1EAljuBpa2x4kWvocnyesydvsFCBdTXCgDv/Rv5AcslnYO6UzFAkD52 BAGDIsaUJIfnmfEyAxRMAsNOM7kX9t2xowLes/grM0AzfkxKKvz61ChRp1iFznE8HTQN Yp7Q== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: AJcUukfAlhiKoEJb038dm9Sp7oriU2bW28HyRETnqOgRw06YbUsqMKNJ CX1UdMQHJ5t7PCmIa/cQvz2u7jZ35nxwsYKwmz31INzB4QHalK5HrZ2zIv//l9G+MYbOXuQBaXq THD1fFJ1/LBugXvqsOLhzAApUZSMRkQcpaT6nv2s07w0MioR/P08rJHcDkmOjuYJD9w== X-Received: by 2002:a37:62d3:: with SMTP id w202mr16506892qkb.357.1548057503197; Sun, 20 Jan 2019 23:58:23 -0800 (PST) X-Google-Smtp-Source: ALg8bN57dpT4IvQDfGLIdfz5zEH7Fs86sEiu+4tfSiihiF1jiGpmFthoBH1QiOSZOfwcyLHwJGiH X-Received: by 2002:a37:62d3:: with SMTP id w202mr16506871qkb.357.1548057502394; Sun, 20 Jan 2019 23:58:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548057502; cv=none; d=google.com; s=arc-20160816; b=ALIwalLMDi2rJptsfFeGMoU4MAYGl9+XrE09SgxYCR9aFOnsTXBaNH024/2Jcg+BXm wu9P9pJoplEkpOMsLpS9r+KKJvap3kolgyrtOKbK/S26tMP68fvIFF28Q3wtd881Imda qgQgdNZPeJGP0qDihBkWCZcqVv1YCIhRsyAaU0q/O+Rk+yl/VCWvGPZbw4+dXpPekS1y QszNfD+NLCtEoSfTzfgsVgCwFTo9FGiawK5WMDIXC6eabtAQPHkET004J3BE0WfIhx3Z TUcQfccPbA1f+sVVaanxWRHS+ypkIAEyQaDP5r3TSXrJrJENjJ+zDGmGaxJF3sW4LnMR Bsdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=E5pUTQPw9eOs30SK4xNMDv9HtqOmCvKjdxalnhHddoE=; b=envVmpxDsVDl1ZijKalMi3hfFvK7Sib3ZwhTavkRZ/hfRM7qg+oyL8PTPd/pX4nItr 9bpEKhuzrZ0pSiQ1AzD8XXYg7Y4UTChwXQUC/McTmYNxK8wyyfMCIzaF3fSrLi8qwHKO UWYoQch/bVlGiR+aP/ZItVCQww0sLTp8yXl2AWjr7dkO1uoo0lGRViF1yxEjC8hF5boO yJGsoYThIq9sY8wTG6Hz+LF4b9uaLybXi5BEjXPHnaAKudK6ZojM9I++Rs9kgydShOYo 5ABT/9XfyPFlq+pR0ceJwgCgw4W/KTgBbVA09VeX+9wLKd9PFAct3gcoAFjsypYIiV2+ 5sVQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id w39si806948qtc.168.2019.01.20.23.58.22 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 20 Jan 2019 23:58:22 -0800 (PST) Received-SPF: pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 64D2DCAA8E; Mon, 21 Jan 2019 07:58:21 +0000 (UTC) Received: from xz-x1.nay.redhat.com (dhcp-14-116.nay.redhat.com [10.66.14.116]) by smtp.corp.redhat.com (Postfix) with ESMTP id A405A608F3; Mon, 21 Jan 2019 07:58:12 +0000 (UTC) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hugh Dickins , Maya Gokhale , Jerome Glisse , Johannes Weiner , peterx@redhat.com, Martin Cracauer , Denis Plotnikov , Shaohua Li , Andrea Arcangeli , Pavel Emelyanov , Mike Kravetz , Marty McFadden , Mike Rapoport , Mel Gorman , "Kirill A . Shutemov" , "Dr . David Alan Gilbert" , Rik van Riel Subject: [PATCH RFC 06/24] userfaultfd: wp: support write protection for userfault vma range Date: Mon, 21 Jan 2019 15:57:04 +0800 Message-Id: <20190121075722.7945-7-peterx@redhat.com> In-Reply-To: <20190121075722.7945-1-peterx@redhat.com> References: <20190121075722.7945-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Mon, 21 Jan 2019 07:58:21 +0000 (UTC) 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Shaohua Li Add API to enable/disable writeprotect a vma range. Unlike mprotect, this doesn't split/merge vmas. Cc: Andrea Arcangeli Cc: Pavel Emelyanov Cc: Rik van Riel Cc: Kirill A. Shutemov Cc: Mel Gorman Cc: Hugh Dickins Cc: Johannes Weiner Signed-off-by: Shaohua Li Signed-off-by: Andrea Arcangeli Signed-off-by: Peter Xu --- include/linux/userfaultfd_k.h | 2 ++ mm/userfaultfd.c | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 38f748e7186e..e82f3156f4e9 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -37,6 +37,8 @@ extern ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long dst_start, unsigned long len, bool *mmap_changing); +extern int mwriteprotect_range(struct mm_struct *dst_mm, + unsigned long start, unsigned long len, bool enable_wp); /* mm helpers */ static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma, diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 458acda96f20..c38903f501c7 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -615,3 +615,55 @@ ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start, { return __mcopy_atomic(dst_mm, start, 0, len, true, mmap_changing); } + +int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, + unsigned long len, bool enable_wp) +{ + struct vm_area_struct *dst_vma; + pgprot_t newprot; + int err; + + /* + * Sanitize the command parameters: + */ + BUG_ON(start & ~PAGE_MASK); + BUG_ON(len & ~PAGE_MASK); + + /* Does the address range wrap, or is the span zero-sized? */ + BUG_ON(start + len <= start); + + down_read(&dst_mm->mmap_sem); + + /* + * Make sure the vma is not shared, that the dst range is + * both valid and fully within a single existing vma. + */ + err = -EINVAL; + dst_vma = find_vma(dst_mm, start); + if (!dst_vma || (dst_vma->vm_flags & VM_SHARED)) + goto out_unlock; + if (start < dst_vma->vm_start || + start + len > dst_vma->vm_end) + goto out_unlock; + + if (!dst_vma->vm_userfaultfd_ctx.ctx) + goto out_unlock; + if (!userfaultfd_wp(dst_vma)) + goto out_unlock; + + if (!vma_is_anonymous(dst_vma)) + goto out_unlock; + + if (enable_wp) + newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE)); + else + newprot = vm_get_page_prot(dst_vma->vm_flags); + + change_protection(dst_vma, start, start + len, newprot, + !enable_wp, 0); + + err = 0; +out_unlock: + up_read(&dst_mm->mmap_sem); + return err; +}