Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3780745pxf; Mon, 22 Mar 2021 15:10:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwg3RHHmrLW7ItzCWnQpGLECc54hv5BYnGSHivsqwDoEhqLW6qHJ/g7D6aB0GRZuDO93clF X-Received: by 2002:a17:906:3849:: with SMTP id w9mr1856401ejc.7.1616451028534; Mon, 22 Mar 2021 15:10:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616451028; cv=none; d=google.com; s=arc-20160816; b=pvWehYeX44gYQv2HjjVvNAhyF3wUntyL+T2GMJb93fAZnJMPkKvHJHAjv2nBQ3/BwR FjsC0iCgsdLHGe5SxwBZ1Fc3GNRz44Y5/cOPTE/LkTBb0N0q9YPJANoC4tdRvMu2GFTD sytPuUCJlCa3WVVawS7eJb0X9LSJ0Us6CWUYtEvMUnP2MQw8d6CGjgji+iaIzH3Y9GJ4 LCzCx4PRcF51NqHK7ndFksetxvOyEk5xeqke1E2YrSAzSwaccRMxjSwdjPN+DVcGhntl wOEcIY942gp15MIhp1WHoPDMF0qriS7my2VRfbiD5zmtImvR+vlHmSkuGMAykR6TN4SG Tv+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OsBykH7rJZDyHmiXDUnOaeMRhfkxz6sA4fBXUYBd6g0=; b=RECB8gKpayu4vgMGLB263OvE1Nup7elYzRnxSxX8VqElN6djpDYjBJeOc8YRRLp2+E 0SC05zvLjFx774QCAJf7V8Xz2TctvgpAJa9sPVrAX646ET5+tWBSskaEbSX4kJoA5YrH pdZo28RC6ZNcBkiyPoegdhLeObU1zxMm24SZlKNWUlHkOvSyY+JNKc82aX1Q6VrqInAM 7jGa6S5CUfMaASgP7mqzdu20exigMCknejLHHrTpeTBP5jr52OEscLYnTqWtDQgOWbXB ONdizYVrGBuZNFT9jyNMChKJUIdM3+e9xQ9KuqA7gK6tQdD9NA9UY1cDOWquWseF7CfR tbjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LmAPQLRD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h7si11386432ede.526.2021.03.22.15.10.06; Mon, 22 Mar 2021 15:10:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LmAPQLRD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230226AbhCVWJM (ORCPT + 99 others); Mon, 22 Mar 2021 18:09:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:24417 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230247AbhCVWI5 (ORCPT ); Mon, 22 Mar 2021 18:08:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616450936; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OsBykH7rJZDyHmiXDUnOaeMRhfkxz6sA4fBXUYBd6g0=; b=LmAPQLRDi7LGaeyLpkqKSMFmveC0E4r8UnHUCCZ2/0SGFUPbSS1l+bH4MGUCx8kf1bWW0u 1h+xq5xJg4k1TV9slcz2Lh84lGT4fmaOBWG2D/Xeja9Op5xV6SlGfqyWRDhpB4Hzp1QCVr tcNd+IQDXbL8mvHQWEouc0LvWtXZCKg= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-588-lh_AQ3yFMtKTaK4DbWEY4g-1; Mon, 22 Mar 2021 18:08:54 -0400 X-MC-Unique: lh_AQ3yFMtKTaK4DbWEY4g-1 Received: by mail-qk1-f198.google.com with SMTP id h134so580358qke.1 for ; Mon, 22 Mar 2021 15:08:54 -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=OsBykH7rJZDyHmiXDUnOaeMRhfkxz6sA4fBXUYBd6g0=; b=YJhEtIcL5AIFre9kl0wkHGR4PoDmUZu054MVo3cnncMfCPjcvqbMjCkkxOxWrlRl2F uV3AGslV7wN4dqYapK+4uUm5Ji2jU1RXXAicmJX7aDETlSlLV6QNHWavyXRk5dYwbjo+ 4Z7rHCqNs77zhqimSvZTaPjL6naIT6wyfJnig4PS24hbq6IhOSLwYbEqlcwGo/ujcSY9 3Y1HqylN9qZRf1f+U2N/kzJneQI2xMZU2TS970TpsxU5IoPEmeSa1H0J+R0HJyUPZfv1 Waz7Y7RhE4fkPTc0i8fAkoLCceyA3yOGB5pMtlleY8qvS1y1TtJvM8fLkqZ6GShAFFw7 /BRA== X-Gm-Message-State: AOAM531XYisLf5QHLi45zpEkrQz3hSZWgAOyg75yrKK7zxHKsV7s7tmY YvzO3sxSM8MVVcBKOVeWXWQCFTPlEuszqi2h/BUSX18rr7OYuuPtOnRsZA1Cj+F4dHlmYRlV2xc xdEfs1AusU9B1cLkT+X9l5HgE X-Received: by 2002:a37:ef14:: with SMTP id j20mr2354870qkk.471.1616450934361; Mon, 22 Mar 2021 15:08:54 -0700 (PDT) X-Received: by 2002:a37:ef14:: with SMTP id j20mr2354842qkk.471.1616450934058; Mon, 22 Mar 2021 15:08:54 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id x1sm9627850qtr.97.2021.03.22.15.08.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 15:08:53 -0700 (PDT) From: Peter Xu To: linux-man@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Andrea Arcangeli , Mike Rapoport , Axel Rasmussen , Alejandro Colomar , peterx@redhat.com, Michael Kerrisk , Nadav Amit , Andrew Morton Subject: [PATCH v4 2/4] userfaultfd.2: Add write-protect mode Date: Mon, 22 Mar 2021 18:08:46 -0400 Message-Id: <20210322220848.52162-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210322220848.52162-1-peterx@redhat.com> References: <20210322220848.52162-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Write-protect mode is supported starting from Linux 5.7. Signed-off-by: Peter Xu --- man2/userfaultfd.2 | 104 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/man2/userfaultfd.2 b/man2/userfaultfd.2 index 555e37409..8ad4a71b5 100644 --- a/man2/userfaultfd.2 +++ b/man2/userfaultfd.2 @@ -78,6 +78,32 @@ all memory ranges that were registered with the object are unregistered and unread events are flushed. .\" .PP +Userfaultfd supports two modes of registration: +.TP +.BR UFFDIO_REGISTER_MODE_MISSING " (since 4.10)" +When registered with +.B 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 +.B UFFDIO_COPY +or an +.B UFFDIO_ZEROPAGE +ioctl. +.TP +.BR UFFDIO_REGISTER_MODE_WP " (since 5.7)" +When registered with +.B 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 +.B UFFDIO_WRITEPROTECT +ioctl. +.PP +Multiple modes can be enabled at the same time for the same memory range. +.PP Since Linux 4.14, userfaultfd page fault message can selectively embed faulting thread ID information into the fault message. One needs to enable this feature explicitly using the @@ -144,6 +170,17 @@ 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 refer to "Userfaultfd write-protect mode" section. .SS Userfaultfd operation After the userfaultfd object is created with .BR userfaultfd (), @@ -219,6 +256,65 @@ 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 5.7) +Since Linux 5.7, userfaultfd supports write-protect mode. +The user needs to first check availability of this feature using +.B UFFDIO_API +ioctl against the feature bit +.B UFFD_FEATURE_PAGEFAULT_FLAG_WP +before using this feature. +.PP +To register with userfaultfd write-protect mode, the user needs to initiate the +.B UFFDIO_REGISTER +ioctl with mode +.B 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 +.B UFFDIO_REGISTER +with the mode set to +.BR "UFFDIO_REGISTER_MODE_MISSING | UFFDIO_REGISTER_MODE_WP" . +When there is only +.B 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 +.B UFFDIO_REGISTER +ioctl completed with +.B UFFDIO_REGISTER_MODE_WP +mode set, +the user can write-protect any existing memory within the range using the ioctl +.B 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 +.B UFFD_PAGEFAULT_FLAG_WP +flag set. +Note: since only writes can trigger such kind of fault, +write-protect messages will always be with +.B UFFD_PAGEFAULT_FLAG_WRITE +bit set too along with bit +.BR UFFD_PAGEFAULT_FLAG_WP . +.PP +To resolve a write-protection page fault, the user should initiate another +.B UFFDIO_WRITEPROTECT +ioctl, whose +.I uffd_msg.pagefault.flags +should have the flag +.B UFFDIO_WRITEPROTECT_MODE_WP +cleared upon the faulted page or range. +.PP +Write-protect mode only supports private anonymous memory. .SS Reading from the userfaultfd structure Each .BR read (2) @@ -364,8 +460,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 -- 2.26.2