From patchwork Thu Jan 7 19:04:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12004689 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=-11.6 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, 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 3DB37C433E0 for ; Thu, 7 Jan 2021 19:05:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AF29F23436 for ; Thu, 7 Jan 2021 19:05:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AF29F23436 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 AD5C28D0142; Thu, 7 Jan 2021 14:05:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A85E08D013A; Thu, 7 Jan 2021 14:05:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94C598D0142; Thu, 7 Jan 2021 14:05:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0019.hostedemail.com [216.40.44.19]) by kanga.kvack.org (Postfix) with ESMTP id 7F4DF8D013A for ; Thu, 7 Jan 2021 14:05:07 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 42C178245571 for ; Thu, 7 Jan 2021 19:05:07 +0000 (UTC) X-FDA: 77679906654.22.soap94_0a06d6f274ec Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id 1C82418038E60 for ; Thu, 7 Jan 2021 19:05:07 +0000 (UTC) X-HE-Tag: soap94_0a06d6f274ec X-Filterd-Recvd-Size: 9450 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf44.hostedemail.com (Postfix) with ESMTP for ; Thu, 7 Jan 2021 19:05:06 +0000 (UTC) Received: by mail-pg1-f201.google.com with SMTP id y34so5466276pgk.21 for ; Thu, 07 Jan 2021 11:05:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=1Ks8/3nZJCYS90R8JKk2puoyafhaeYHGuGPtcF6mxek=; b=s33gwiE6wEj4kJwKpZ+JBEnAPq2Iul6GLks8POMY9MgZc9LBZEzjlEFhyqZYiwx4id dMUAawf6ZO1RuNGq6qWn7XiCwJZaExLSJ/x0qIdCrZcZ5lvZbgLpOXACTZgO7pmlu87x Zv/9mH/Oxu+THRCrCKk23deOoYVx+kIKAa5hSJt7UI49J1EJO2RC9KiHnXTjT/vb1SAb yhuOQHXkHuckcPE1Q+qkGIReZWdZZ1KhdiTyGY799YowVmEqWHJf722bg2qLRozEV0Kd vZm4JkDx7ZwLeiNeB2q5xb7tZVq7cjBJEKYOlzBvaCQRi8AAGDzniY+am3PWed00CrYr UxpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=1Ks8/3nZJCYS90R8JKk2puoyafhaeYHGuGPtcF6mxek=; b=ervya6QY6sp46pcy/cepYfHkgPQ25hm2Tpi80tuCy1edAZ7yRa2dSok2z1CxKb6hYv JslfaOBSKRNZnE2LVvM2yxqYe8Tld9pbwtq6EdHOp+isjuTiIQtSK3sOJ6/uviA3L+fZ trXn6DDvph9plV59bF/nloML92/YVOqz3MT0D38f4eYHpMmjp/ncfEslbj8KScc1rS3x hESuva1wGgF2rZt8zAvS2ZgwySM1Cl2A3NIdHFBFR8nRsLpWTcQCaazBXdMbFGrMlX2d e7vtoW8j0Gw2ouLO6CvjooGA5DZOCvn3YO1S58KtsDzUfQ1DCgi/vpMIyP8NZJU+bBTX 7nAQ== X-Gm-Message-State: AOAM532a6xpq+LVFl5n6/qVW0c3+08ws9tZ276EGjx0VIgL9+oyaI2ni eWI80yyCNWfhXQg7G/7nqqf4hgdbmQ1UOnOuGCf9 X-Google-Smtp-Source: ABdhPJxFND2XY3Pg2WyGKBNLXJEiCRrx4CwvkyILubCDSqYj/vz535k983RPVy3w45oJ8bmaDS3h0zHA99/3XyZBAkZh X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:f693:9fff:feef:c8f8]) (user=axelrasmussen job=sendgmr) by 2002:a17:90a:c085:: with SMTP id o5mr10737346pjs.210.1610046305274; Thu, 07 Jan 2021 11:05:05 -0800 (PST) Date: Thu, 7 Jan 2021 11:04:51 -0800 Message-Id: <20210107190453.3051110-1-axelrasmussen@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.29.2.729.g45daf8777d-goog Subject: [RFC PATCH 0/2] userfaultfd: handle minor faults, add UFFDIO_CONTINUE 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_Koutn=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 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: Overview ======== This series adds a new userfaultfd registration mode, UFFDIO_REGISTER_MODE_MINOR. This allows userspace to intercept "minor" faults. By "minor" fault, I mean the following situation: Let there exist two mappings (i.e., VMAs) to the same page(s) (shared memory). One of the mappings is registered with userfaultfd (in minor mode), and the other is not. Via the non-UFFD mapping, the underlying pages have already been allocated & filled with some contents. The UFFD mapping has not yet been faulted in; when it is touched for the first time, this results in what I'm calling a "minor" fault. As a concrete example, when working with hugetlbfs, we have huge_pte_none(), but find_lock_page() finds an existing page. We also add a new ioctl to resolve such faults: UFFDIO_CONTINUE. The idea is, userspace resolves the fault by either a) doing nothing if the contents are already correct, or b) updating the underlying contents using the second, non-UFFD mapping (via memcpy/memset or similar, or something fancier like RDMA, or etc...). In either case, userspace issues UFFDIO_CONTINUE to tell the kernel "I have ensured the page contents are correct, carry on setting up the mapping". Use Case ======== Consider the use case of VM live migration (e.g. under QEMU/KVM): 1. While a VM is still running, we copy the contents of its memory to a target machine. The pages are populated on the target by writing to the non-UFFD mapping, using the setup described above. The VM is still running (and therefore its memory is likely changing), so this may be repeated several times, until we decide the target is "up to date enough". 2. We pause the VM on the source, and start executing on the target machine. During this gap, the VM's user(s) will *see* a pause, so it is desirable to minimize this window. 3. Between the last time any page was copied from the source to the target, and when the VM was paused, the contents of that page may have changed - and therefore the copy we have on the target machine is out of date. Although we can keep track of which pages are out of date, for VMs with large amounts of memory, it is "slow" to transfer this information to the target machine. We want to resume execution before such a transfer would complete. 4. So, the guest begins executing on the target machine. The first time it touches its memory (via the UFFD-registered mapping), userspace wants to intercept this fault. Userspace checks whether or not the page is up to date, and if not, copies the updated page from the source machine, via the non-UFFD mapping. Finally, whether a copy was performed or not, userspace issues a UFFDIO_CONTINUE ioctl to tell the kernel "I have ensured the page contents are correct, carry on setting up the mapping". We don't have to do all of the final updates on-demand. The userfaultfd manager can, in the background, also copy over updated pages once it receives the map of which pages are up-to-date or not. Interaction with Existing APIs ============================== Because it's possible to combine registration modes (e.g. a single VMA can be userfaultfd-registered MINOR | MISSING), and because it's up to userspace how to resolve faults once they are received, I spent some time thinking through how the existing API interacts with the new feature. UFFDIO_CONTINUE cannot be used to resolve non-minor faults, as it does not allocate a new page. If UFFDIO_CONTINUE is used on a non-minor fault: - For non-shared memory or shmem, -EINVAL is returned. - For hugetlb, -EFAULT is returned. UFFDIO_COPY and UFFDIO_ZEROPAGE cannot be used to resolve minor faults. Without modifications, the existing codepath assumes a new page needs to be allocated. This is okay, since userspace must have a second non-UFFD-registered mapping anyway, thus there isn't much reason to want to use these in any case (just memcpy or memset or similar). - If UFFDIO_COPY is used on a minor fault, -EEXIST is returned. - If UFFDIO_ZEROPAGE is used on a minor fault, -EEXIST is returned (or -EINVAL in the case of hugetlb, as UFFDIO_ZEROPAGE is unsupported in any case). - UFFDIO_WRITEPROTECT simply doesn't work with shared memory, and returns -ENOENT in that case (regardless of the kind of fault). Remaining Work ============== This patchset doesn't include updates to userfaultfd's documentation or selftests. This will be added before I send a non-RFC version of this series (I want to find out if there are strong objections to the API surface before spending the time to document it.) Currently the patchset only supports hugetlbfs. There is no reason it can't work with shmem, but I expect hugetlbfs to be much more commonly used since we're talking about backing guest memory for VMs. I plan to implement shmem support in a follow-up patch series. Axel Rasmussen (2): userfaultfd: add minor fault registration mode userfaultfd: add UFFDIO_CONTINUE ioctl fs/proc/task_mmu.c | 1 + fs/userfaultfd.c | 143 ++++++++++++++++++++++++------- include/linux/mm.h | 1 + include/linux/userfaultfd_k.h | 14 ++- include/trace/events/mmflags.h | 1 + include/uapi/linux/userfaultfd.h | 36 +++++++- mm/hugetlb.c | 42 +++++++-- mm/userfaultfd.c | 86 ++++++++++++++----- 8 files changed, 261 insertions(+), 63 deletions(-) --- 2.29.2.729.g45daf8777d-goog