From patchwork Thu Apr 1 18:21:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 12179167 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=-31.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 BF629C433B4 for ; Thu, 1 Apr 2021 18:21:38 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6B7016023C for ; Thu, 1 Apr 2021 18:21:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6B7016023C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 090D66B0080; Thu, 1 Apr 2021 14:21:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E966E6B0082; Thu, 1 Apr 2021 14:21:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CE9206B0083; Thu, 1 Apr 2021 14:21:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0168.hostedemail.com [216.40.44.168]) by kanga.kvack.org (Postfix) with ESMTP id A5C0F6B0080 for ; Thu, 1 Apr 2021 14:21:37 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 5D7521839AD83 for ; Thu, 1 Apr 2021 18:21:37 +0000 (UTC) X-FDA: 77984616234.04.19C2649 Received: from mail-qv1-f74.google.com (mail-qv1-f74.google.com [209.85.219.74]) by imf28.hostedemail.com (Postfix) with ESMTP id A1E032000257 for ; Thu, 1 Apr 2021 18:21:36 +0000 (UTC) Received: by mail-qv1-f74.google.com with SMTP id u15so3854048qvo.13 for ; Thu, 01 Apr 2021 11:21:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Oo4/iLObcqLXLV2/XExPkM5pqEReTYs+R+HS3we8lZ4=; b=tuz14c1aRmcVZA08g3WfHkeWoJDzUVhxIpLw1Vp9J743ol12q2ihLiSrd9dSujmDSe rX3+gUYoS8a5Gn27DtJVlZWNp24bjHifABDq97HEH8DfZxheS5k4MFwk3AabRyBlnNtg F6PzQSLgVplocyN8cqAiDKNdSnVNqGInuJ4SJbVxu1WdTJKA+RCLu4MYbi6Lwg1Ey3In P6HrGt/3oe4VyhsDVuxZkLn4P3uTX3qvhyE9mHmeyjDOOH5Qv73pZ8KUi4ep+mCa/WXZ 6t6fW4wYHuppKajlhNQfsjQoB5z6wNTthH7LT59lhzDrYyyWZ1Ub3doo40630Sbi1X8J vrpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Oo4/iLObcqLXLV2/XExPkM5pqEReTYs+R+HS3we8lZ4=; b=TfAMdMYbDq3uhi8mTj9jxaMBhOunsj1CAOFmMBpKs9QI+nlVNDWUPqpAnXShXRvMvi mHNbUHA5PaX5E4CSl0z0nHnYozShdxKX+RaBAJ/WfvqdXBn+h2WsevFZsd2QuosPV55I 9WFJCJv+nRNHSiDxUL2AYhDwvC49uK6D1PBdX6E8eDGIVU/G+M2FjsDhA5hlnOTrtwNn ZiWbb2e2eGzH9ZZAJU1nElYzqZRiu/ecqTqa6xmqIfnOQNYp7K4+sNZGYLcAnSemuWnd joA6/lUrj3RZ7Co1yZk07ErAKO+3XgFQtqCPGyNsv4yRvbYeq7cWjZyxbDeaGRKRRxZR tibg== X-Gm-Message-State: AOAM531S1Zgqe8zPBCbA5691geu+Wzip7mRhNPfo3Jb4D9X/27N7gAIK lJcI11HmGj+qAg0d2hraffq5SuX5Ydg= X-Google-Smtp-Source: ABdhPJwcg8o/8IRXJYzRz4F/NAPO3qCJYllZtztoX0gkXvKl/3JJgpcO7aVC6xjLgs/5XDWGYjNBR4Am+Do= X-Received: from surenb1.mtv.corp.google.com ([2620:15c:211:200:899:1066:21fc:b3c5]) (user=surenb job=sendgmr) by 2002:a05:6214:5af:: with SMTP id by15mr9621777qvb.37.1617301296257; Thu, 01 Apr 2021 11:21:36 -0700 (PDT) Date: Thu, 1 Apr 2021 11:21:24 -0700 In-Reply-To: <20210401182125.171484-1-surenb@google.com> Message-Id: <20210401182125.171484-5-surenb@google.com> Mime-Version: 1.0 References: <20210401182125.171484-1-surenb@google.com> X-Mailer: git-send-email 2.31.0.291.g576ba9dcdaf-goog Subject: [PATCH 4/5] userfaultfd: wp: add helper for writeprotect check From: Suren Baghdasaryan To: stable@vger.kernel.org Cc: gregkh@linuxfoundation.org, jannh@google.com, ktkhai@virtuozzo.com, torvalds@linux-foundation.org, shli@fb.com, namit@vmware.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com, Andrea Arcangeli , Peter Xu , Andrew Morton , Jerome Glisse , Mike Rapoport , Rik van Riel , "Kirill A . Shutemov" , Mel Gorman , Hugh Dickins , Johannes Weiner , Bobby Powers , Brian Geffon , David Hildenbrand , Denis Plotnikov , "Dr . David Alan Gilbert" , Martin Cracauer , Marty McFadden , Maya Gokhale , Mike Kravetz , Pavel Emelyanov X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A1E032000257 X-Stat-Signature: ygccmja9bktxmgqhbqru4box1pakt8af Received-SPF: none (flex--surenb.bounces.google.com>: No applicable sender policy available) receiver=imf28; identity=mailfrom; envelope-from="<3MA9mYAYKCO8jliVeSXffXcV.TfdcZelo-ddbmRTb.fiX@flex--surenb.bounces.google.com>"; helo=mail-qv1-f74.google.com; client-ip=209.85.219.74 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617301296-763504 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: From: Shaohua Li Patch series "userfaultfd: write protection support", v6. Overview ======== The uffd-wp work was initialized by Shaohua Li [1], and later continued by Andrea [2]. This series is based upon Andrea's latest userfaultfd tree, and it is a continuous works from both Shaohua and Andrea. Many of the follow up ideas come from Andrea too. Besides the old MISSING register mode of userfaultfd, the new uffd-wp support provides another alternative register mode called UFFDIO_REGISTER_MODE_WP that can be used to listen to not only missing page faults but also write protection page faults, or even they can be registered together. At the same time, the new feature also provides a new userfaultfd ioctl called UFFDIO_WRITEPROTECT which allows the userspace to write protect a range or memory or fixup write permission of faulted pages. Please refer to the document patch "userfaultfd: wp: UFFDIO_REGISTER_MODE_WP documentation update" for more information on the new interface and what it can do. The major workflow of an uffd-wp program should be: 1. Register a memory region with WP mode using UFFDIO_REGISTER_MODE_WP 2. Write protect part of the whole registered region using UFFDIO_WRITEPROTECT, passing in UFFDIO_WRITEPROTECT_MODE_WP to show that we want to write protect the range. 3. Start a working thread that modifies the protected pages, meanwhile listening to UFFD messages. 4. When a write is detected upon the protected range, page fault happens, a UFFD message will be generated and reported to the page fault handling thread 5. The page fault handler thread resolves the page fault using the new UFFDIO_WRITEPROTECT ioctl, but this time passing in !UFFDIO_WRITEPROTECT_MODE_WP instead showing that we want to recover the write permission. Before this operation, the fault handler thread can do anything it wants, e.g., dumps the page to a persistent storage. 6. The worker thread will continue running with the correctly applied write permission from step 5. Currently there are already two projects that are based on this new userfaultfd feature. QEMU Live Snapshot: The project provides a way to allow the QEMU hypervisor to take snapshot of VMs without stopping the VM [3]. LLNL umap library: The project provides a mmap-like interface and "allow to have an application specific buffer of pages cached from a large file, i.e. out-of-core execution using memory map" [4][5]. Before posting the patchset, this series was smoke tested against QEMU live snapshot and the LLNL umap library (by doing parallel quicksort using 128 sorting threads + 80 uffd servicing threads). My sincere thanks to Marty Mcfadden and Denis Plotnikov for the help along the way. TODO ==== - hugetlbfs/shmem support - performance - more architectures - cooperate with mprotect()-allowed processes (???) - ... References ========== [1] https://lwn.net/Articles/666187/ [2] https://git.kernel.org/pub/scm/linux/kernel/git/andrea/aa.git/log/?h=userfault [3] https://github.com/denis-plotnikov/qemu/commits/background-snapshot-kvm [4] https://github.com/LLNL/umap [5] https://llnl-umap.readthedocs.io/en/develop/ [6] https://git.kernel.org/pub/scm/linux/kernel/git/andrea/aa.git/commit/?h=userfault&id=b245ecf6cf59156966f3da6e6b674f6695a5ffa5 [7] https://lkml.org/lkml/2018/11/21/370 [8] https://lkml.org/lkml/2018/12/30/64 This patch (of 19): Add helper for writeprotect check. Will use it later. Signed-off-by: Shaohua Li Signed-off-by: Andrea Arcangeli Signed-off-by: Peter Xu Signed-off-by: Andrew Morton Reviewed-by: Jerome Glisse Reviewed-by: Mike Rapoport Cc: Rik van Riel Cc: Kirill A. Shutemov Cc: Mel Gorman Cc: Hugh Dickins Cc: Johannes Weiner Cc: Bobby Powers Cc: Brian Geffon Cc: David Hildenbrand Cc: Denis Plotnikov Cc: "Dr . David Alan Gilbert" Cc: Martin Cracauer Cc: Marty McFadden Cc: Maya Gokhale Cc: Mike Kravetz Cc: Pavel Emelyanov Link: http://lkml.kernel.org/r/20200220163112.11409-2-peterx@redhat.com Signed-off-by: Linus Torvalds --- include/linux/userfaultfd_k.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 37c9eba75c98..38f748e7186e 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -50,6 +50,11 @@ static inline bool userfaultfd_missing(struct vm_area_struct *vma) return vma->vm_flags & VM_UFFD_MISSING; } +static inline bool userfaultfd_wp(struct vm_area_struct *vma) +{ + return vma->vm_flags & VM_UFFD_WP; +} + static inline bool userfaultfd_armed(struct vm_area_struct *vma) { return vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP); @@ -94,6 +99,11 @@ static inline bool userfaultfd_missing(struct vm_area_struct *vma) return false; } +static inline bool userfaultfd_wp(struct vm_area_struct *vma) +{ + return false; +} + static inline bool userfaultfd_armed(struct vm_area_struct *vma) { return false;