From patchwork Thu Mar 4 01:59:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12115325 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 5A946C433E6 for ; Thu, 4 Mar 2021 01:59:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 04428650DE for ; Thu, 4 Mar 2021 01:59:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 04428650DE 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 6EA376B0007; Wed, 3 Mar 2021 20:59:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C0A16B0008; Wed, 3 Mar 2021 20:59:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53CB36B000A; Wed, 3 Mar 2021 20:59:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0021.hostedemail.com [216.40.44.21]) by kanga.kvack.org (Postfix) with ESMTP id 351FF6B0007 for ; Wed, 3 Mar 2021 20:59:57 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E6B7A9893 for ; Thu, 4 Mar 2021 01:59:56 +0000 (UTC) X-FDA: 77880535992.29.DFEACB6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf06.hostedemail.com (Postfix) with ESMTP id C53C3C0001EA for ; Thu, 4 Mar 2021 01:59:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614823195; 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=niUIy0c/0oGI63bArq9uLm/RRa5ZjXXO2YChidlOiJo=; b=eYWI1a0ANzDmd/mDwGsSmzT2FujX7qG3/48KM+iVAXEs9XMnwawgUJAogSIuacol0FL7O/ Y2vHzh+3Qek2YYl+1FGWSMUpErW8capqNC4yKtC0fFcLMF49TUlwAo4TMLfYxIPLU3ZeLl CgTykEiTlWvLlGuF6de0vW5kGf2vzjk= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-480-CtAqK-iPPjOJrEVPG9_mLQ-1; Wed, 03 Mar 2021 20:59:54 -0500 X-MC-Unique: CtAqK-iPPjOJrEVPG9_mLQ-1 Received: by mail-qk1-f200.google.com with SMTP id m68so6491603qkd.5 for ; Wed, 03 Mar 2021 17:59:54 -0800 (PST) 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=niUIy0c/0oGI63bArq9uLm/RRa5ZjXXO2YChidlOiJo=; b=rxjLZMBLQ3PXc4n0CImy/i0373lw23yb9fs2/xjUMMEWdSGtI8YRJXUSMei3icZ+rg u0iYzyS4KNzL+0VwCFi1jKPkBLzDlM+aFN6D0qHStsRl6lH5DaGzVj04fiT/CXJr5M1j 1Eyka9voj0i+TiDGEofvldjO3QQeKlOO/R8woWKmldv+BbV9auK1+pwHiyoGwnEkVq/e Ut4MXH+bgvomINx3qapKUUS51iwQUMyldzREuBWINOYjN71OlI2hkTq1gZH5GlTtlzSt 4Jzey9pMHHPgb5EId2Q0bvlwkvmeNMg9v5pJWDOAMsjFOWnBb9kH/vYW73SSsQbB3TkV JryA== X-Gm-Message-State: AOAM5309ZW2ke+W86BOH8tw5CMvvn8If/tD6uFJeXcvbMRqJ4fuvT031 Gp8UI4tZONuIwcLCYvlVpKnNPiSDgRbUe3Rn8JWV4emH/7/y3N2DDmElSMn5PuMz7zfT5WQwuzQ IVDFN3N97tBw= X-Received: by 2002:a05:620a:1442:: with SMTP id i2mr1991184qkl.469.1614823193482; Wed, 03 Mar 2021 17:59:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJzFz8/KVOJPa1tGdZSbCyCB8BIU+7VVcA3wOtpUEiqLGkOezSBFeOaKOITweH1HIu7biYofAg== X-Received: by 2002:a05:620a:1442:: with SMTP id i2mr1991167qkl.469.1614823193151; Wed, 03 Mar 2021 17:59:53 -0800 (PST) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-25-174-95-95-253.dsl.bell.ca. [174.95.95.253]) by smtp.gmail.com with ESMTPSA id b7sm18610766qkj.115.2021.03.03.17.59.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Mar 2021 17:59:52 -0800 (PST) From: Peter Xu To: linux-man@vger.kernel.org Cc: Mike Rapoport , Nadav Amit , linux-kernel@vger.kernel.org, Linux MM Mailing List , peterx@redhat.com, Axel Rasmussen , Alejandro Colomar , Andrea Arcangeli , Andrew Morton , Michael Kerrisk Subject: [PATCH 2/4] userfaultfd.2: Add write-protect mode Date: Wed, 3 Mar 2021 20:59:45 -0500 Message-Id: <20210304015947.517713-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210304015947.517713-1-peterx@redhat.com> References: <20210304015947.517713-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: gety31go6i5a9jk8qepyows8t3jiusbj X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: C53C3C0001EA Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf06; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=63.128.21.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1614823195-799370 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: Write-protect mode is supported starting from Linux 5.7. Signed-off-by: Peter Xu --- man2/userfaultfd.2 | 88 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/man2/userfaultfd.2 b/man2/userfaultfd.2 index 2d14effc6..8e1602d62 100644 --- a/man2/userfaultfd.2 +++ b/man2/userfaultfd.2 @@ -78,6 +78,28 @@ all memory ranges that were registered with the object are unregistered and unread events are flushed. .\" .PP +Currently, userfaultfd supports two modes of registration: +.TP +.BR UFFDIO_REGISTER_MODE_MISSING +When registered with +.BR UFFDIO_REGISTER_MODE_MISSING +mode, the userspace will receive a page fault message when a missing page is +accessed. The faulted thread will be stopped from execution until the page +fault is resolved from the userspace by either an +.BR UFFDIO_COPY +or an +.BR UFFDIO_ZEROPAGE +ioctl. +.TP +.BR UFFDIO_REGISTER_MODE_WP +When registered with +.BR UFFDIO_REGISTER_MODE_WP +mode, the userspace will receive a page fault message when a write-protected +page is written. The faulted thread will be stopped from execution until the +userspace un-write-protect the page using an +.BR UFFDIO_WRITEPROTECT +ioctl. +.PP Since Linux 4.14, userfaultfd page fault message can selectively embed fault thread ID information into the fault message. One needs to enable this feature explicitly using the @@ -143,6 +165,16 @@ single threaded non-cooperative userfaultfd manager implementations. .\" and limitations remaining in 4.11 .\" Maybe it's worth adding a dedicated sub-section... .\" +.PP +Starting from Linux 5.7, userfaultfd is able to do synchronous page dirty +tracking using the new write-protection register mode. One should check +against the feature bit +.B UFFD_FEATURE_PAGEFAULT_FLAG_WP +before using this feature. Similar to the original userfaultfd missing mode, +the write-protect mode will generate an userfaultfd message when the protected +page is written. The user needs to resolve the page fault by unprotecting the +faulted page and kick the faulted thread to continue. For more information, +please read the "Userfaultfd write-protect mode" section below. .SS Userfaultfd operation After the userfaultfd object is created with .BR userfaultfd (), @@ -218,6 +250,54 @@ userfaultfd can be used only with anonymous private memory mappings. Since Linux 4.11, userfaultfd can be also used with hugetlbfs and shared memory mappings. .\" +.SS Userfaultfd write-protect mode +Since Linux 5.7, userfaultfd started to support write-protect mode. The user +needs to first check availability of this feature using +.BR UFFDIO_API +ioctl against the feature bit +.BR UFFD_FEATURE_PAGEFAULT_FLAG_WP . +.PP +To register with userfaultfd write-protect mode, the user needs to send the +.BR UFFDIO_REGISTER +ioctl with mode +.BR UFFDIO_REGISTER_MODE_WP +set. Note that it's legal to monitor the same memory range with multiple +modes. For example, the user can do +.BR UFFDIO_REGISTER +with the mode set to +.BR UFFDIO_REGISTER_MODE_MISSING\ |\ UFFDIO_REGISTER_MODE_WP. +When there is only +.BR UFFDIO_REGISTER_MODE_WP +registered, the userspace will +.I not +receive any message when a missing page is written. Instead, the userspace +will only receive a write-protect page fault message when an existing but +write-protected page got written. +.PP +After the +.BR UFFDIO_REGISTER +ioctl completed with +.BR UFFDIO_REGISTER_MODE_WP +mode set, one can write-protect any existing memory within the range using the +ioctl +.BR UFFDIO_WRITEPROTECT +where +.I uffdio_writeprotect.mode +should be set to +.BR UFFDIO_WRITEPROTECT_MODE_WP . +.PP +When a write-protect event happens, the userspace will receive a page fault +message whose +.I uffd_msg.pagefault.flags +will be with +.BR UFFD_PAGEFAULT_FLAG_WP +flag set. Note: since only writes can trigger such kind of fault, +write-protect messages will always be with +.BR UFFD_PAGEFAULT_FLAG_WRITE +bit set too along with +.BR UFFD_PAGEFAULT_FLAG_WP . +.PP +Currently, write-protect mode only supports private anonymous memory. .SS Reading from the userfaultfd structure Each .BR read (2) @@ -363,8 +443,12 @@ flag (see .BR ioctl_userfaultfd (2)) and this flag is set, this a write fault; otherwise it is a read fault. -.\" -.\" UFFD_PAGEFAULT_FLAG_WP is not yet supported. +.TP +.B UFFD_PAGEFAULT_FLAG_WP +If the address is in a range that was registered with the +.B UFFDIO_REGISTER_MODE_WP +flag, when this bit is set it means it's a write-protect fault. Otherwise it's +a page missing fault. .RE .TP .I pagefault.feat.pid