Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp558183iog; Wed, 15 Jun 2022 07:43:24 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uIIw00O5GD1wZmimHQ5X3McKF40dVZwnv/iAAHS4cqQD17Xht5dcXaaaV9LAYzgGdG/viD X-Received: by 2002:a17:907:3e84:b0:6fe:8c5f:d552 with SMTP id hs4-20020a1709073e8400b006fe8c5fd552mr144021ejc.710.1655304204176; Wed, 15 Jun 2022 07:43:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655304204; cv=none; d=google.com; s=arc-20160816; b=DaPG8bjRgTN+7CGEBaLnIFRGCvpuFRaw7xCkrqvV4jZ0QepOCg2ZrY/jtu7lUfJWjh uZGdGEnTqAsKx1LNrXUzOglS/3j2Kz5q3P5gY3Ar05lcdYKo2uaP+RQzuANdVT77oAE8 tCNZQtgX6proREZ6zjRgYniT21dxpRwylpVrb8QxXG/33NrdFlH5hj2tMsmR6bJk/QCm 42mVuOm6m3pBmuiigNnoOnvukixO5eFeV0LMc1uWulLh4bXZsfQaHvm2WOhMLDuN6xg2 qKo1ER+RgIZMkcvkCPxCw2QY7WYkKgzUtT85QbuLWIDuCwX+6vNXfcBjS+ZonbV/rvj3 p9Cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=urXLCEo0HdMiwrT9/lHPLDvk+tFr+S8kFKoTasWYgTM=; b=G76+20HgeznsZ9pcaGDEyuvajy2mkg49AUhWWis9FloaLHcKitKA9C3+LRJNMB6XcS ePMsjS+JGDbkJpPrTlQyQQPYDIKCD3a0jtjQAoQrIEr/aXnHjJoCU2l+VA/RygOX5G8v iSYq8gxGeXAjrv/MQKFygXavhf1nNIXt3iGlW29qGrjR6So35GADUcSUKrVHda10kGaw 0S/yHWNokqFiyRTbw2xtSFNSwM/KLNz6L1iXzOQdsJralJRCKxEmjaQDGeEYsQcTnvPj plIYzCgupXpysI7U87baaCHO6bu4agN+k+wwR+3pCWDyDoKtIRHBYTQfIigdaO+ozmNb gCcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jE8+1Eoa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r9-20020a05640251c900b0042dd179c25bsi17335532edd.623.2022.06.15.07.42.58; Wed, 15 Jun 2022 07:43:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jE8+1Eoa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S1353771AbiFOO3x (ORCPT + 99 others); Wed, 15 Jun 2022 10:29:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242339AbiFOO3v (ORCPT ); Wed, 15 Jun 2022 10:29:51 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8DF735ABC for ; Wed, 15 Jun 2022 07:29:50 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id 187so11596518pfu.9 for ; Wed, 15 Jun 2022 07:29:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=urXLCEo0HdMiwrT9/lHPLDvk+tFr+S8kFKoTasWYgTM=; b=jE8+1EoaVPLQAQgJuF6uSHsVQUNEYiULaZEf4qWJ+FDCtBcy5wU+1PuRU/qTw6mXuJ rhkDSC1GoM3r6gP1XqDzyFKdRxVJyQscQceF46fTmrMh4tpJ7/3OD4E0UUUL4FXaE4E4 HLsY4nWxsZ1bLbc4mtfRzI75UcR7ige6egZvCSqqgKpyeTsqtkTZjDQiW2mEf6TP1C0u 784mpZz2YhCQYjeTdJsTJu7oJeXZ7GHllZjsJApUFHo2QiJHGuFpB3Xi7l8+I0pC+m8X WUI+8LcLsPMDaqunWg8z0EiW4EPIpYE086RJe+7JyYOWlUAXkMPBTz0xEA+GhbwuK97s EpUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=urXLCEo0HdMiwrT9/lHPLDvk+tFr+S8kFKoTasWYgTM=; b=vdKEQi02jGLHM2mO7KwsuDfoRj4O1XVuHuNscynfhGAT9GhVpmk+ywDxDpjcMPiIgL fIxal5AWurSIFHanFZG3cdC6WzhRf9+6Y162HxYO+JI3c/mcPEhhjSZuv1Pjkrlz7FdZ N8TUFj+Sc6vt45vBdOmkjTY9oBSM8YzuEAThZfCSX6uSaJAmHoepuy0guxtcT7JKyBIc 1rcDJPsWFRTBnbYKSC5eGBpsOP20tKCO1UukJ3pc3bza6KLlI6LQyU5TPOJ0/TUNWClq V6jDOUC5avf4uwBXtKWZDBCBfBa1BL6VxWiB/pyszh/bs3k8MjFhvhoe7Q7aVVZMXvdw AKiw== X-Gm-Message-State: AJIora+Vmxv6Ftce4VEfd2vbWqxKVM4wsWbYVIVGcpPzXWwKJ/iEqq3H IyGvk8LUD7Qpoqryqf0F5Szipg== X-Received: by 2002:a63:e1c:0:b0:3fd:f319:df7b with SMTP id d28-20020a630e1c000000b003fdf319df7bmr75509pgl.135.1655303389770; Wed, 15 Jun 2022 07:29:49 -0700 (PDT) Received: from google.com (123.65.230.35.bc.googleusercontent.com. [35.230.65.123]) by smtp.gmail.com with ESMTPSA id z15-20020a170903018f00b0015eb200cc00sm9366660plg.138.2022.06.15.07.29.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 07:29:49 -0700 (PDT) Date: Wed, 15 Jun 2022 14:29:45 +0000 From: Sean Christopherson To: Chao Peng Cc: Andy Lutomirski , Vishal Annapurve , Marc Orr , kvm list , LKML , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, qemu-devel@nongnu.org, Paolo Bonzini , Jonathan Corbet , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86 , "H . Peter Anvin" , Hugh Dickins , Jeff Layton , "J . Bruce Fields" , Andrew Morton , Mike Rapoport , Steven Price , "Maciej S . Szmigiero" , Vlastimil Babka , Yu Zhang , "Kirill A . Shutemov" , Jun Nakajima , Dave Hansen , Andi Kleen , David Hildenbrand , aarcange@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, Quentin Perret , Michael Roth , mhocko@suse.com Subject: Re: [PATCH v6 0/8] KVM: mm: fd-based approach for supporting KVM guest private memory Message-ID: References: <20220607065749.GA1513445@chaop.bj.intel.com> <20220608021820.GA1548172@chaop.bj.intel.com> <20220614072800.GB1783435@chaop.bj.intel.com> <20220615091759.GB1823790@chaop.bj.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220615091759.GB1823790@chaop.bj.intel.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 15, 2022, Chao Peng wrote: > On Tue, Jun 14, 2022 at 01:59:41PM -0700, Andy Lutomirski wrote: > > On Tue, Jun 14, 2022 at 12:09 PM Sean Christopherson wrote: > > > > > > On Tue, Jun 14, 2022, Andy Lutomirski wrote: > > > > This patch series is fairly close to implementing a rather more > > > > efficient solution. I'm not familiar enough with hypervisor userspace > > > > to really know if this would work, but: > > > > > > > > What if shared guest memory could also be file-backed, either in the > > > > same fd or with a second fd covering the shared portion of a memslot? > > > > This would allow changes to the backing store (punching holes, etc) to > > > > be some without mmap_lock or host-userspace TLB flushes? Depending on > > > > what the guest is doing with its shared memory, userspace might need > > > > the memory mapped or it might not. > > > > > > That's what I'm angling for with the F_SEAL_FAULT_ALLOCATIONS idea. The issue, > > > unless I'm misreading code, is that punching a hole in the shared memory backing > > > store doesn't prevent reallocating that hole on fault, i.e. a helper process that > > > keeps a valid mapping of guest shared memory can silently fill the hole. > > > > > > What we're hoping to achieve is a way to prevent allocating memory without a very > > > explicit action from userspace, e.g. fallocate(). > > > > Ah, I misunderstood. I thought your goal was to mmap it and prevent > > page faults from allocating. I don't think you misunderstood, that's also one of the goals. The use case is that multiple processes in the host mmap() guest memory, and we'd like to be able to punch a hole without having to rendezvous with all processes and also to prevent an unintentional re-allocation. > I think we still need the mmap, but want to prevent allocating when > userspace touches previously mmaped area that has never filled the page. Yes, or if a chunk was filled at some point but then was removed via PUNCH_HOLE. > I don't have clear answer if other operations like read/write should be > also prevented (probably yes). And only after an explicit fallocate() to > allocate the page these operations would act normally. I always forget about read/write. I believe reads should be ok, the semantics of holes are that they return zeros, i.e. can use ZERO_PAGE() and not allocate a new backing page. Not sure what to do about writes though. Allocating on direct writes might be ok for our use case, but that could also result in a rather wierd API. > > It is indeed the case (and has been since before quite a few of us > > were born) that a hole in a sparse file is logically just a bunch of > > zeros. A way to make a file for which a hole is an actual hole seems > > like it would solve this problem nicely. It could also be solved more > > specifically for KVM by making sure that the private/shared mode that > > userspace programs is strict enough to prevent accidental allocations > > -- if a GPA is definitively private, shared, neither, or (potentially, > > on TDX only) both, then a page that *isn't* shared will never be > > accidentally allocated by KVM. > > KVM is clever enough to not allocate since it knows a GPA is shared or > not. This case it's the host userspace that can cause the allocating and > is too complex to check on every access from guest. Yes, KVM is not in the picture at all. KVM won't trigger allocation, but KVM also is not in a position to prevent userspace from touching memory. > > If the shared backing is not mmapped, > > it also won't be accidentally allocated by host userspace on a stray > > or careless write. > > As said above, mmap is still prefered, otherwise too many changes are > needed for usespace VMM. Forcing userspace to change doesn't bother me too much, the biggest concern is having to take mmap_lock for write in a per-host process.