From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72280C4332F for ; Mon, 5 Dec 2022 18:20:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F0C478E0003; Mon, 5 Dec 2022 13:20:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E952D8E0001; Mon, 5 Dec 2022 13:20:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0E698E0003; Mon, 5 Dec 2022 13:20:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BA3678E0001 for ; Mon, 5 Dec 2022 13:20:42 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 80DF280977 for ; Mon, 5 Dec 2022 18:20:42 +0000 (UTC) X-FDA: 80209068324.14.A7C214F Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf27.hostedemail.com (Postfix) with ESMTP id 0DBDB40014 for ; Mon, 5 Dec 2022 18:20:41 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="A/ZBFF+0"; spf=pass (imf27.hostedemail.com: domain of seanjc@google.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=seanjc@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670264442; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+bOJKTiPkfMHk6uxaZoppVRYQMYBhmTscSqARviGH6I=; b=eOY/LQWfxCLcGWoIwQb7qod5JbBvOSWpwvglU5fmrJ34CgHanuZ/pnEV3pA13CEIwKWYuh rpjinDkcJ1hr4CKs7OY6VVo6szFWb0++uRDwuaW/p3OLHP5i00drYkluU1amMBE+D6Jb3o xGhyyle8CqjGMUmi6wOnLf9sISXxonc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="A/ZBFF+0"; spf=pass (imf27.hostedemail.com: domain of seanjc@google.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=seanjc@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670264442; a=rsa-sha256; cv=none; b=KAeCfBSAZeIgJd1RgwCQb38QLMm7ox0jrXwiowkeZ9p+Ka+WQZDXjul7qatOS3W1GiAZgy qo0x+1am4ZfHYSYB4ct21/Z88sV2stFF8UFlHuTUO1M5UARKXsnIumosmyEknMqrNHUEbU 9ysyCeOA+1sPI5i1DmKJtBYKC4WQl4w= Received: by mail-pl1-f175.google.com with SMTP id a9so11602881pld.7 for ; Mon, 05 Dec 2022 10:20:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=+bOJKTiPkfMHk6uxaZoppVRYQMYBhmTscSqARviGH6I=; b=A/ZBFF+0chYWUBxVma+AtamAbUokaN1/mdtekkaBq/N8uat0FmmtuLcI+4/P4xBIhy ca6LDTqcxdFr+zgqX8cQ2pfYf8jXX6BbgqNQ9Bh/JC0XhIz6k3Z16npbUyR5peIen7M6 Mkw44kH/qNIfAQXzRNuyamx9C0DtULYt8vXQjeycVq+DE8rRD39XEWd2TqFYGiQWxA9s uSiD7dTPPTHBJ+biIkD0vilQlKIbod57uyo+EfLmGiTlvVd+3J7g9MxId2cYMhDYQC5F obfdpMs6gjynBC8omQG+Yxc78nBU/cOZJKpWU3ZAebxq1nxD2Xen/22XAqfdheeqnN/k pxbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=+bOJKTiPkfMHk6uxaZoppVRYQMYBhmTscSqARviGH6I=; b=0hmJhefuWtEYaT6V93kpEkn/iT9zSVGIczKlqPI3dFj7qM1JBTrnJL3u5uk6k+me1M eRW6lddde7XAPxU1FITjDKQJ2wvnkpYTAuNKZan1LhGd09qPwMWccviLB/T2p4YGYVp1 xhw4fhrFOT3JIajRG+9CE2Ceu2lQ3DNEjPp3GvATs98tKKCb6VGUTHkOK7g5GSfRxdTo KaYszSQES127I2oVUklciFZ/vhS8UxLAUHStIybx6PfMn0Q5ubNeyW1T5q2pWqGyeY7S gcu+HhhR/AHi90IyC+utJuIVlOogeWhpIPNhRbhzGDy58uSxL6GQ71lQA1VHkkGn/ukW sLVA== X-Gm-Message-State: ANoB5pmqTQumEv8fcNSWga5JIyctXBWnpzwbHXQ+lc2vtaRzowlUbt11 iSU2v0aNLpN4TqDSytjnypCEQnHSn/9OH4Bg X-Google-Smtp-Source: AA0mqf7bma3A3dXb4cBUkvq1rm3yfGH83Fz6ysb4agHDeVOLMg8FIrSV/KqaeJaAPoDK0We6iKdKzQ== X-Received: by 2002:a17:90a:a611:b0:219:3a95:5b0b with SMTP id c17-20020a17090aa61100b002193a955b0bmr40930132pjq.190.1670264440830; Mon, 05 Dec 2022 10:20:40 -0800 (PST) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id p4-20020a170902780400b0016d9b101413sm10865780pll.200.2022.12.05.10.20.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 10:20:40 -0800 (PST) Date: Mon, 5 Dec 2022 18:20:36 +0000 From: Sean Christopherson To: David Matlack Cc: Peter Xu , James Houghton , Andrea Arcangeli , Paolo Bonzini , Axel Rasmussen , Linux MM , kvm , chao.p.peng@linux.intel.com Subject: Re: [RFC] Improving userfaultfd scalability for live migration Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 0DBDB40014 X-Stat-Signature: obju1haqozpuz8h6rzu1uzxd3bdmtisy X-Spamd-Result: default: False [-2.90 / 9.00]; BAYES_HAM(-6.00)[100.00%]; SORBS_IRL_BL(3.00)[209.85.214.175:from]; MIME_GOOD(-0.10)[text/plain]; RCVD_NO_TLS_LAST(0.10)[]; BAD_REP_POLICIES(0.10)[]; TO_DN_SOME(0.00)[]; RCPT_COUNT_SEVEN(0.00)[9]; DMARC_POLICY_ALLOW(0.00)[google.com,reject]; DKIM_TRACE(0.00)[google.com:+]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; R_SPF_ALLOW(0.00)[+ip4:209.85.128.0/17]; RCVD_COUNT_THREE(0.00)[3]; R_DKIM_ALLOW(0.00)[google.com:s=20210112]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspam-User: X-HE-Tag: 1670264441-479983 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Dec 05, 2022, David Matlack wrote: > On Mon, Dec 5, 2022 at 7:30 AM Peter Xu wrote: > > > > == Getting the faulting GPA to userspace == > > > > KVM_EXIT_MEMORY_FAULT was introduced recently [1] (not yet merged), > > > > and it provides the main functionality we need. We can extend it > > > > easily to support our use case here, and I think we have at least two > > > > options: > > > > - Introduce something like KVM_CAP_MEM_FAULT_REPORTING, which causes > > > > KVM_RUN to exit with exit reason KVM_EXIT_MEMORY_FAULT when it would > > > > otherwise just return -EFAULT (i.e., when kvm_handle_bad_page returns > > > > -EFAULT). > > > > - We're already introducing a new CAP, so just tie the above behavior > > > > to whether or not one of the CAPs (below) is being used. > > > > > > We might even be able to get away with a third option: unconditionally return > > > KVM_EXIT_MEMORY_FAULT instead of -EFAULT when the error occurs when accessing > > > guest memory. > > > > > > > == Problems == > > > > The major problem here is that this only solves the scalability > > > > problem for the KVM demand paging case. Other userfaultfd users, if > > > > they have scalability problems, will need to find another approach. > > > > > > It may not fully solve KVM's problem either. E.g. if the VM is running nested > > > VMs, many (most?) of the user faults could be triggered by FNAME(walk_addr_generic) > > > via __get_user() when walking L1's EPT tables. > > We could always modify FNAME(walk_addr_generic) to return out to user > space in the same way if that is indeed another bottleneck. Yes, but given that there's a decent chance that solving this problem will add new ABI, I want to make sure that we are confident that we won't end up with gaps in the ABI. I.e. I don't want to punt the nested case to the future. > > > Disclaimer: I know _very_ little about UFFD. > > > > > > Rather than add yet another flag to gup(), what about flag to say the task doesn't > > > want to wait for UFFD faults? If desired/necessary, KVM could even toggle the flag > > > in KVM_RUN so that faults that occur outside of KVM ultimately don't send an actual > > > SIGBUGS. > > There are some copy_to/from_user() calls in KVM that cannot easily > exit out to KVM_RUN (for example, in the guts of the emulator IIRC). > But we could use your approach just to wrap the specific call sites > that can return from KVM_RUN. Yeah, it would definitely need to be opt-in. > > > diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c > > > index 07c81ab3fd4d..7f66b56dd6e7 100644 > > > --- a/fs/userfaultfd.c > > > +++ b/fs/userfaultfd.c > > > @@ -394,7 +394,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) > > > * shmem_vm_ops->fault method is invoked even during > > > * coredumping without mmap_lock and it ends up here. > > > */ > > > - if (current->flags & (PF_EXITING|PF_DUMPCORE)) > > > + if (current->flags & (PF_EXITING|PF_DUMPCORE|PF_NO_UFFD_WAIT)) > > > goto out; > > > > I'll have a closer read on the nested part, but note that this path already > > has the mmap lock then it invalidates the goal if we want to avoid taking > > it from the first place, or maybe we don't care? > > > > If we want to avoid taking the mmap lock at all (hence the fast-gup > > approach), I'd also suggest we don't make it related to uffd at all but > > instead an interface to say "let's check whether the page tables are there > > (walk pgtable by fast-gup only), if not return to userspace". Ooh, good point. If KVM provided a way for userspace to toggle a "fast-only" flag, then hva_to_pfn() could bail if hva_to_pfn_fast() failed, and I think KVM could just do pagefault_disable/enable() around compatible KVM uaccesses? > > Because IIUC fast-gup has nothing to do with uffd, so it can also be a more > > generic interface. It's just that if the userspace knows what it's doing > > (postcopy-ing), it knows then the faults can potentially be resolved by > > userfaultfd at this stage. > > Are there any cases where fast-gup can fail while uffd is enabled but > it's not due to uffd? e.g. if a page is swapped out? Undoubtedly. COW, NUMA balancing, KSM?, etc. Nit, I don't think "due to uffd" is the right terminology, I think the right phrasing is something like "but can't be resolved by userspace", or maybe "but weren't induced by userspace". UFFD itself never causes faults. > I don't know what userspace would do in those situations to make forward progress. Access the page from userspace? E.g. a "LOCK AND -1" would resolve read and write faults without modifying guest memory. That won't work for guests backed by "restricted mem", a.k.a. UPM guests, but restricted mem really should be able to prevent those types of faults in the first place. SEV guests are the one case I can think of where that approach won't work, since writes will corrupt the guest. SEV guests can likely be special cased though.