Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1657441pxb; Wed, 10 Feb 2021 13:30:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJz3RPiz6BgVvaRdqya8sRjEfkNxz+aPUMi5Bj/QZZtx3FgiJl54qzObvnfWfv1i7sdtKVTt X-Received: by 2002:a05:6402:19:: with SMTP id d25mr5042944edu.71.1612992652878; Wed, 10 Feb 2021 13:30:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612992652; cv=none; d=google.com; s=arc-20160816; b=vyhSNtt7P4M/NuJvUpOfVcy9+iDbjlFD4TQz00EzlvnYx9X7PdmwkGFMY+sKMCkwFt S9swIlVhO0EdKVmW3LpNQ+S3jb4BNf53vr+9P6WjxIhMOyFm8RfkmHXJIX4B82mqqjOf FqJk3OElRODVgPACc9dmU/bsQ87mCUfpAoAWmAJ6XpYvR5U9Mr+x9DP604vEz6RZ9F6V D2712wP/cLDBXB9QKZYkcry3gm+RdDxoI5FYDtPoU4te9L8yKCrGbIw1gVxgFNRIJuLj 4Xv36VIExmla6+Dpo/qXfGVcYiT3dLCVbgxGlRpETrKxfbtjor4T9pNmdBrTz8XzZPRU rIrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=4EM0wnbYoFqLzr2o9eJpbKi30ljVgmfRwB6Uz2fcmAs=; b=dXu9CKarE50LFHrJsXQUOslU2K9nyVWkgBrhXR5dBBIxHWccpmEcWtxjEheFlZIagW hVPV+DD/KHWsaxBivzKmnYA0Hdq2INTk3YNAn7U6UyKRbBS+N4dkttbLoe6DawVrb2V+ gsxbaMJZCh7zzZmDDip6+DOBbtCsRqiKccss+b2erbbMaXMVcS2q2ocA/OwE1LmnHOg1 s9p0q7QdE6rjK57QPb04BnOdTejhwRL7A+L1/JbvsDrf8GbSB5BX2lqGW9J3mHB5LzWz dAcXsE4VKn9IhhK1ZsBZxr5Bn+Un6yWsYPnTDcxxgIsmIX5+kTpHIvNloyz18Z6+DfUi 1Kng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=m28r71if; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n13si2114909edy.457.2021.02.10.13.30.28; Wed, 10 Feb 2021 13:30:52 -0800 (PST) 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=@google.com header.s=20161025 header.b=m28r71if; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233369AbhBJV1F (ORCPT + 99 others); Wed, 10 Feb 2021 16:27:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233493AbhBJVXD (ORCPT ); Wed, 10 Feb 2021 16:23:03 -0500 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14CB2C06121F for ; Wed, 10 Feb 2021 13:22:25 -0800 (PST) Received: by mail-qk1-x74a.google.com with SMTP id 124so2748911qkg.6 for ; Wed, 10 Feb 2021 13:22:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=4EM0wnbYoFqLzr2o9eJpbKi30ljVgmfRwB6Uz2fcmAs=; b=m28r71ifLpz190NqPtEgBBzDN34NWJyKfSHg6UBlbNRwURhazG+IB5FAxBxY1t1aYL Wz63F072+5C32bPetLo7ZnCnucNeFZbQM2ubqc2Cm+tGZ4pr/tGi2vvLKkZH/ldl7JPC CCHXlXiuNPVj+4gz1nonsWe0likBhOhSWcrzGvUwRbuCMFAoDaGckzAH9bcpeau7IKUT JiGLU4IMh9FP/xSEHkjyMf4zKjJL7NbsqVKO099spKCfY5N3pdo0MbchNwADv+uLltL+ 3xXvLX6uH5MUnKj6BcjexCw8M5cZbO3wNfTAkV09uKdOUG9cNYlRS+pTFpNpkmiH61pV NgPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=4EM0wnbYoFqLzr2o9eJpbKi30ljVgmfRwB6Uz2fcmAs=; b=f3MdG5YCEpmc1hbPtBdfTsaTqaTG1mri14ptXM5fz4f1Q2AbesNfsdVG8sgBWsryfC 2ibRyvZwj8JbyHhUEQvq15aL/KdBeL8Yo1RdjHFCgGwov8YMiGVapfdXaAslZP/PR9Bb vQXXS93cbpri+RSUEeRa9/YGiInpmr/oqqevWqQxKd6QkWsfV313wvIDfyOiMA8YQGeS JYKyYPF7RegMmmju7otZRx534BjEQPPAuMZlOEPdlp2Wv0zczn9UyboDrrQBf9UP92A2 kecxOmCUJzj/07xMJqpaw4KaX/TO5d9WppS6G9rcfQrMyjPf479a3ubHUYgFaI30RKpc O8pg== X-Gm-Message-State: AOAM533Cnx/J6CtA+CZ30SQOQ7x0rQOcpshc7OdHLg/m2cpqwMNOqY40 xG7WheASXMTFB1WAfmCFAb133YvQHa0OF+6UcdfF Sender: "axelrasmussen via sendgmr" X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:94ee:de01:168:9f20]) (user=axelrasmussen job=sendgmr) by 2002:a0c:e641:: with SMTP id c1mr4178501qvn.47.1612992144161; Wed, 10 Feb 2021 13:22:24 -0800 (PST) Date: Wed, 10 Feb 2021 13:21:59 -0800 In-Reply-To: <20210210212200.1097784-1-axelrasmussen@google.com> Message-Id: <20210210212200.1097784-10-axelrasmussen@google.com> Mime-Version: 1.0 References: <20210210212200.1097784-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH v5 09/10] userfaultfd: update documentation to describe minor fault handling From: Axel Rasmussen To: Alexander Viro , Alexey Dobriyan , Andrea Arcangeli , Andrew Morton , Anshuman Khandual , Catalin Marinas , Chinwen Chang , Huang Ying , Ingo Molnar , Jann Horn , Jerome Glisse , Lokesh Gidra , "Matthew Wilcox (Oracle)" , Michael Ellerman , "=?UTF-8?q?Michal=20Koutn=C3=BD?=" , Michel Lespinasse , Mike Kravetz , Mike Rapoport , Nicholas Piggin , Peter Xu , Shaohua Li , Shawn Anastasio , Steven Rostedt , Steven Price , Vlastimil Babka Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Adam Ruprecht , Axel Rasmussen , Cannon Matthews , "Dr . David Alan Gilbert" , David Rientjes , Mina Almasry , Oliver Upton Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Reword / reorganize things a little bit into "lists", so new features / modes / ioctls can sort of just be appended. Describe how UFFDIO_REGISTER_MODE_MINOR and UFFDIO_CONTINUE can be used to intercept and resolve minor faults. Make it clear that COPY and ZEROPAGE are used for MISSING faults, whereas CONTINUE is used for MINOR faults. Signed-off-by: Axel Rasmussen --- Documentation/admin-guide/mm/userfaultfd.rst | 107 ++++++++++++------- 1 file changed, 66 insertions(+), 41 deletions(-) diff --git a/Documentation/admin-guide/mm/userfaultfd.rst b/Documentation/admin-guide/mm/userfaultfd.rst index 65eefa66c0ba..3aa38e8b8361 100644 --- a/Documentation/admin-guide/mm/userfaultfd.rst +++ b/Documentation/admin-guide/mm/userfaultfd.rst @@ -63,36 +63,36 @@ the generic ioctl available. The ``uffdio_api.features`` bitmask returned by the ``UFFDIO_API`` ioctl defines what memory types are supported by the ``userfaultfd`` and what -events, except page fault notifications, may be generated. - -If the kernel supports registering ``userfaultfd`` ranges on hugetlbfs -virtual memory areas, ``UFFD_FEATURE_MISSING_HUGETLBFS`` will be set in -``uffdio_api.features``. Similarly, ``UFFD_FEATURE_MISSING_SHMEM`` will be -set if the kernel supports registering ``userfaultfd`` ranges on shared -memory (covering all shmem APIs, i.e. tmpfs, ``IPCSHM``, ``/dev/zero``, -``MAP_SHARED``, ``memfd_create``, etc). - -The userland application that wants to use ``userfaultfd`` with hugetlbfs -or shared memory need to set the corresponding flag in -``uffdio_api.features`` to enable those features. - -If the userland desires to receive notifications for events other than -page faults, it has to verify that ``uffdio_api.features`` has appropriate -``UFFD_FEATURE_EVENT_*`` bits set. These events are described in more -detail below in `Non-cooperative userfaultfd`_ section. - -Once the ``userfaultfd`` has been enabled the ``UFFDIO_REGISTER`` ioctl should -be invoked (if present in the returned ``uffdio_api.ioctls`` bitmask) to -register a memory range in the ``userfaultfd`` by setting the +events, except page fault notifications, may be generated: + +- The ``UFFD_FEATURE_EVENT_*`` flags indicate that various other events + other than page faults are supported. These events are described in more + detail below in the `Non-cooperative userfaultfd`_ section. + +- ``UFFD_FEATURE_MISSING_HUGETLBFS`` and ``UFFD_FEATURE_MISSING_SHMEM`` + indicate that the kernel supports ``UFFDIO_REGISTER_MODE_MISSING`` + registrations for hugetlbfs and shared memory (covering all shmem APIs, + i.e. tmpfs, ``IPCSHM``, ``/dev/zero``, ``MAP_SHARED``, ``memfd_create``, + etc) virtual memory areas, respectively. + +- ``UFFD_FEATURE_MINOR_HUGETLBFS`` indicates that the kernel supports + ``UFFDIO_REGISTER_MODE_MINOR`` registration for hugetlbfs virtual memory + areas. + +The userland application should set the feature flags it intends to use +when invoking the ``UFFDIO_API`` ioctl, to request that those features be +enabled if supported. + +Once the ``userfaultfd`` API has been enabled the ``UFFDIO_REGISTER`` +ioctl should be invoked (if present in the returned ``uffdio_api.ioctls`` +bitmask) to register a memory range in the ``userfaultfd`` by setting the uffdio_register structure accordingly. The ``uffdio_register.mode`` bitmask will specify to the kernel which kind of faults to track for -the range (``UFFDIO_REGISTER_MODE_MISSING`` would track missing -pages). The ``UFFDIO_REGISTER`` ioctl will return the +the range. The ``UFFDIO_REGISTER`` ioctl will return the ``uffdio_register.ioctls`` bitmask of ioctls that are suitable to resolve userfaults on the range registered. Not all ioctls will necessarily be -supported for all memory types depending on the underlying virtual -memory backend (anonymous memory vs tmpfs vs real filebacked -mappings). +supported for all memory types (e.g. anonymous memory vs. shmem vs. +hugetlbfs), or all types of intercepted faults. Userland can use the ``uffdio_register.ioctls`` to manage the virtual address space in the background (to add or potentially also remove @@ -100,21 +100,46 @@ memory from the ``userfaultfd`` registered range). This means a userfault could be triggering just before userland maps in the background the user-faulted page. -The primary ioctl to resolve userfaults is ``UFFDIO_COPY``. That -atomically copies a page into the userfault registered range and wakes -up the blocked userfaults -(unless ``uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE`` is set). -Other ioctl works similarly to ``UFFDIO_COPY``. They're atomic as in -guaranteeing that nothing can see an half copied page since it'll -keep userfaulting until the copy has finished. +Resolving Userfaults +-------------------- + +There are three basic ways to resolve userfaults: + +- ``UFFDIO_COPY`` atomically copies some existing page contents from + userspace. + +- ``UFFDIO_ZEROPAGE`` atomically zeros the new page. + +- ``UFFDIO_CONTINUE`` maps an existing, previously-populated page. + +These operations are atomic in the sense that they guarantee nothing can +see a half-populated page, since readers will keep userfaulting until the +operation has finished. + +By default, these wake up userfaults blocked on the range in question. +They support a ``UFFDIO_*_MODE_DONTWAKE`` ``mode`` flag, which indicates +that waking will be done separately at some later time. + +Which ioctl to choose depends on the kind of page fault, and what we'd +like to do to resolve it: + +- For ``UFFDIO_REGISTER_MODE_MISSING`` faults, the fault needs to be + resolved by either providing a new page (``UFFDIO_COPY``), or mapping + the zero page (``UFFDIO_ZEROPAGE``). By default, the kernel would map + the zero page for a missing fault. With userfaultfd, userspace can + decide what content to provide before the faulting thread continues. + +- For ``UFFDIO_REGISTER_MODE_MINOR`` faults, there is an existing page (in + the page cache). Userspace has the option of modifying the page's + contents before resolving the fault. Once the contents are correct + (modified or not), userspace asks the kernel to map the page and let the + faulting thread continue with ``UFFDIO_CONTINUE``. Notes: -- If you requested ``UFFDIO_REGISTER_MODE_MISSING`` when registering then - you must provide some kind of page in your thread after reading from - the uffd. You must provide either ``UFFDIO_COPY`` or ``UFFDIO_ZEROPAGE``. - The normal behavior of the OS automatically providing a zero page on - an anonymous mmaping is not in place. +- You can tell which kind of fault occurred by examining + ``pagefault.flags`` within the ``uffd_msg``, checking for the + ``UFFD_PAGEFAULT_FLAG_*`` flags. - None of the page-delivering ioctls default to the range that you registered with. You must fill in all fields for the appropriate @@ -122,9 +147,9 @@ Notes: - You get the address of the access that triggered the missing page event out of a struct uffd_msg that you read in the thread from the - uffd. You can supply as many pages as you want with ``UFFDIO_COPY`` or - ``UFFDIO_ZEROPAGE``. Keep in mind that unless you used DONTWAKE then - the first of any of those IOCTLs wakes up the faulting thread. + uffd. You can supply as many pages as you want with these IOCTLs. + Keep in mind that unless you used DONTWAKE then the first of any of + those IOCTLs wakes up the faulting thread. - Be sure to test for all errors including (``pollfd[0].revents & POLLERR``). This can happen, e.g. when ranges -- 2.30.0.478.g8a0d178c01-goog