Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1368696pxb; Thu, 28 Jan 2021 14:55:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJwavycbC7cihPfWjTMQ7Hl225Ulb+ojZ/JLGFeybZkkLqmVQnuvrKi+pVJ2hRC/OZQ+AAGg X-Received: by 2002:aa7:cb8f:: with SMTP id r15mr2147104edt.130.1611874530415; Thu, 28 Jan 2021 14:55:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611874530; cv=none; d=google.com; s=arc-20160816; b=YvqmLFToMG9My8kT9m/LeogJXrh80/NpX7De4m5fcr5PfgYRhDVHdDvlTAnp9KWLEE JX8au4v3UpHmxOoH9U9QsMMvxfT8f3oAi/Rf8LphyWJx7pZSfLo2VDVAJCCayrDlbV1R JS7AQtEbNZPrG8c80BClcV4PnoxFXZgjlsAeFPND143b5RR3/ENUY3MNBXPP0L/z0B8E GYQx1qvnbiKzXCl6xdO/ugvbcrgxECxDVF8JRgRxSz5f3xC6USPDoGc4bIn8XigLUL4U dKRP0cjBOMqj8uAg8i0Aifey9dl0Y5sS1INq2nBWPWpIhyRKbpQAJupdlmzBntLuSw8G 1Y7Q== 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=9d4h1nc7cXRM0oclaURotgVZ2pAa3xoO69oOTeFmJb8=; b=A5WG1t/uft6W37V6+rOnnrQbx9bGfLeAFSms+EcBs4tthMPQBEw8mfBQuN2ifTizpG LJlH6YDkltMKTyRPB+b618XZZ2hL5xq79uHoCOLgfxaSQrhHbd3ikiUsXuWJtdwVDe2J XL9Ejtw6nwrAFkE3Xhzg0esJezFg0Wx8HbNsJF8t6mfd6nBfcaAw62Ml0qea37h8BINn R8LmYUZtylSNk5gueZQapT2/9ugyZ0WWOyd2+DEx5A3nR3a7Umg3stSYD4Gn1HtO9ixk FNNwFbTvnDw/rdkyVcfKqabksFVazD+iRdkHhDTSiOboRS7Xr0HTFGxHOQUtcn+MRd6S GRyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Z7B678u+; 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 c15si4169419ede.341.2021.01.28.14.55.06; Thu, 28 Jan 2021 14:55:30 -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=Z7B678u+; 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 S231174AbhA1Wxq (ORCPT + 99 others); Thu, 28 Jan 2021 17:53:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231727AbhA1WwB (ORCPT ); Thu, 28 Jan 2021 17:52:01 -0500 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 460A1C061351 for ; Thu, 28 Jan 2021 14:48:42 -0800 (PST) Received: by mail-qt1-x84a.google.com with SMTP id w3so4589241qti.17 for ; Thu, 28 Jan 2021 14:48:42 -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=9d4h1nc7cXRM0oclaURotgVZ2pAa3xoO69oOTeFmJb8=; b=Z7B678u+6f6pTLNsvIl9IUdSVip/5XXUFW052UEMvgabZhSN+1esIL7SA/i1MrduID iwAbaYduhC0CxiavFM2dOUFxo7+MHrBagTGlP4EKatw+ZTk4J/s2iMe1858Jc0QRpeq4 QO9BE3KM67jDWCUQ0vFsNHKJ4Qx0ZI7AcBnRNYP9tPvh+SIjhRwaUq0MzV7zrtulT7np D77EmXMVWlJ0xrRxggdjNPf82FDCgSESYStVMW1Hr8Cjis1v/T+iZDd10q9+0NHe/URR vygj9wcQCc9LDSTNmgJ71PYqGSKeQOvhSk0UHeifNv1HSUwWE2f1SCvnOu69uelKOx61 i1sg== 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=9d4h1nc7cXRM0oclaURotgVZ2pAa3xoO69oOTeFmJb8=; b=PxjFKjidSa3ZyDBrg7ooN0isbRnM/dGhT+nvPLOwRGcce3pWDzuN17WrMVQMMu3pHH uaZPl+veCbzhtiXO/NAz983kyivgrt1CwEIoN2lbG/jh/c0whLk1qKFnP0rfqmCfyO+N Y0ZR+wMMYDOVeP+mBKvLasbkpwlZgB0p0XZMUl1Mta7JtsSUXdcdVG2gBnS6zCt8oK0c norIFVMfQnLFx1RgorDrUXVQIiJ8h98cI7XFfZTAg8O1iZx733XDZPKYh9ElKFo0FUsY j76SqySnEIVlI17QCN1u18mBodk8SGz+Mz4cd+F/jPI3IYxnH2RQ/A+wwA3KGrbLteO7 Gg0g== X-Gm-Message-State: AOAM533m27KxB6wDoOWztWPx3GG2BBjipcRd+hrem3/w79a/0R8kPfTW T2MwuI/ggB1uuBPNmG21Eg2CFHGJhtSptWvfI9xf Sender: "axelrasmussen via sendgmr" X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:f693:9fff:feef:c8f8]) (user=axelrasmussen job=sendgmr) by 2002:ad4:4dc9:: with SMTP id cw9mr1601769qvb.8.1611874121378; Thu, 28 Jan 2021 14:48:41 -0800 (PST) Date: Thu, 28 Jan 2021 14:48:18 -0800 In-Reply-To: <20210128224819.2651899-1-axelrasmussen@google.com> Message-Id: <20210128224819.2651899-9-axelrasmussen@google.com> Mime-Version: 1.0 References: <20210128224819.2651899-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH v3 8/9] 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 , 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 | 105 +++++++++++-------- 1 file changed, 64 insertions(+), 41 deletions(-) diff --git a/Documentation/admin-guide/mm/userfaultfd.rst b/Documentation/admin-guide/mm/userfaultfd.rst index 65eefa66c0ba..10c69458c794 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 envoking 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,44 @@ 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 of these are used depends on the kind of fault: + +- For ``UFFDIO_REGISTER_MODE_MISSING`` faults, a new page has to be + provided. This can be done with either ``UFFDIO_COPY`` or + ``UFFDIO_ZEROPAGE``. The default (non-userfaultfd) behavior would be to + provide a zero page, but in userfaultfd this is left up to userspace. + +- For ``UFFDIO_REGISTER_MODE_MINOR`` faults, an existing page already + exists. Userspace needs to ensure its contents are correct (if it needs + to be modified, by writing directly to the non-userfaultfd-registered + side of shared memory), and then issue ``UFFDIO_CONTINUE`` to resolve + the fault. 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 +145,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.365.g02bc693789-goog