Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1149830rwr; Fri, 5 May 2023 09:46:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7pg0ERuuuRJeAGiVtDbEKZybBocKVb9IJSfhETLL831SDDpb8bJlm2w1lyOtf2D8LtZu6O X-Received: by 2002:a05:6a21:1786:b0:f4:4ff5:11ab with SMTP id nx6-20020a056a21178600b000f44ff511abmr2300195pzb.58.1683305173503; Fri, 05 May 2023 09:46:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683305173; cv=none; d=google.com; s=arc-20160816; b=ph6iaaUpNQUAiO2/dQtjWHyb4EkA4k5T3PMo2HgVkgfVMMV08gQzqOgjSnEpSY+mBP Li9jBhh+YkdoL8wjxTrTPdDnJnOBHGgT55DNNmUZ/t43JLt7RdZnKnRf7dGFZRL+YItk YCsmULP2RpSK9BTQhmlPzvK8U9F7b56ejnoY0ZcHurIILIXGCD6Bov5p4LF13eQIn9ZZ 9wIgV23jhbN5VHUYwdxoyeicom+Xy6e/ymNbIsdtXSKTiDCig/h7rJCly5ANtUjfOVFl To5ZovGWtAcZlVCwPJk9v+bgvW4Dsi/AXDWZP7FZuoJD0jsvqj2tMJb0RJZW2kWVyVfs 5mpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:dkim-signature; bh=tpG4xiqP3mzArRx9Ask1She9PnSIKi9NV0T7HckdUAY=; b=K7HnDbwtzP84Mv/utEsXqKlcFwL0MhVvM4CrsYjrjLJPwp7scci4ncZ2J/ASP+0REn wi2l5f1sIof78j7ufDXQrM7CiBC97fF6o9jyDltf1qJtKINhk4BLHrv0VYR921I5C9Vv 93yDx1NCoNuWOCFtlEfWJmGvV9wn22Hdcvb1WXxptK8spZi+EzzG28Sf/UrPsEAFc6xZ CQT1PkCvLRAdCD4GHCI/2d/P/catRqCP8o3LIT+nCly37prwVFuGKlFn//8QYsjyV88A O5yFDUrbDajMI8fK9V+t7f6z2ftFVnIIZLEKzvlavNRmKR1okgTRzH+7fMNY99jqRBp0 M0Fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=R7ZId5oM; 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 s6-20020a656446000000b0052cb8690805si358001pgv.393.2023.05.05.09.45.58; Fri, 05 May 2023 09:46:13 -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=20221208 header.b=R7ZId5oM; 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 S232900AbjEEQ2R (ORCPT + 99 others); Fri, 5 May 2023 12:28:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231852AbjEEQ2Q (ORCPT ); Fri, 5 May 2023 12:28:16 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA25A160B8 for ; Fri, 5 May 2023 09:28:10 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-559e281c5dfso19662147b3.3 for ; Fri, 05 May 2023 09:28:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683304090; x=1685896090; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=tpG4xiqP3mzArRx9Ask1She9PnSIKi9NV0T7HckdUAY=; b=R7ZId5oM7hLrX+tcfVm/Bnk2u8JbAC1I8QSh2twc/YutVfGMASmtW7tbm2YP3uHqlk RwtPwL861jZAZsMdLif5CXyQc6HVUK4hq+Rsigm7g9H5SwYxxSrPZKroysdphn0wBKMc 5pkuhlqCX/BOp4yTuk4h5T1NCQgcpfPZ9KLIiem4KASWPyrgXBR48rcxQa06YhEaqMbn orfLSFQcdN2vNomHtco8b3soICbHS5a9W6R91ScgWRyqTuI6OJFa+omcbJE3G4QloJsc Zd/UhaIHTp9GBfbJO6BShFyMFzXWMH9S7e+yp/9GzVVu/Y2uZsmKlNFrDjrpY2RYPhtU 7R+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683304090; x=1685896090; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=tpG4xiqP3mzArRx9Ask1She9PnSIKi9NV0T7HckdUAY=; b=UPHLNr7lpoHZ75RCZdhR959FnruSPzftzai1kR0yQUvJ1pn12wV20jAap33vJz8D+C pWGFPloJ+zC2uSJQ1YdKcXpTzraRTuMDolxtRND4Muqxv2+5gRbbnfNhMNVsA9C+ilE9 mvKvdHTRT1dJE3Z6U5CjyKCPXjub2/KwcHkC5/xfmk9ZoQ88WAyhLiq2Uc9JvEynmjsc sU+flOeSsOmo+ZC/Uqvbvqah2wAkI5RvgsIVr5L9TPhzsana+uakFnkiX9bLB2KTkytZ mI4GTkZJ7olC41kutAz8dsH7IiH9Mm3Cp15/5wUSu7MvLJYOkYfr9n2fYn6z8apvpTI/ UJMQ== X-Gm-Message-State: AC+VfDzOrTb65RQlVu+KP+UapRZJektc0Mx+LQE9ZWYVlUrbyLQUxLOP c6wGLUAYzYl06dq6lYZyA2kKGgQLoDQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:ac24:0:b0:559:d859:d746 with SMTP id k36-20020a81ac24000000b00559d859d746mr1208880ywh.10.1683304090087; Fri, 05 May 2023 09:28:10 -0700 (PDT) Date: Fri, 5 May 2023 09:28:08 -0700 In-Reply-To: <20230505152046.6575-3-mic@digikod.net> Mime-Version: 1.0 References: <20230505152046.6575-1-mic@digikod.net> <20230505152046.6575-3-mic@digikod.net> Message-ID: Subject: Re: [PATCH v1 2/9] KVM: x86/mmu: Add support for prewrite page tracking From: Sean Christopherson To: "=?iso-8859-1?Q?Micka=EBl_Sala=FCn?=" Cc: Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Ingo Molnar , Kees Cook , Paolo Bonzini , Thomas Gleixner , Vitaly Kuznetsov , Wanpeng Li , Alexander Graf , Forrest Yuan Yu , James Morris , John Andersen , Liran Alon , "Madhavan T . Venkataraman" , Marian Rotariu , "Mihai =?utf-8?B?RG9uyJt1?=" , "=?utf-8?B?TmljdciZb3IgQ8OuyJt1?=" , Rick Edgecombe , Thara Gopinath , Will Deacon , Zahra Tarkhani , "=?utf-8?Q?=C8=98tefan_=C8=98icleru?=" , dev@lists.cloudhypervisor.org, kvm@vger.kernel.org, linux-hardening@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, qemu-devel@nongnu.org, virtualization@lists.linux-foundation.org, x86@kernel.org, xen-devel@lists.xenproject.org Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_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 Fri, May 05, 2023, Micka=EF=BF=BDl Sala=EF=BF=BDn wrote: > diff --git a/arch/x86/include/asm/kvm_page_track.h b/arch/x86/include/asm= /kvm_page_track.h > index eb186bc57f6a..a7fb4ff888e6 100644 > --- a/arch/x86/include/asm/kvm_page_track.h > +++ b/arch/x86/include/asm/kvm_page_track.h > @@ -3,6 +3,7 @@ > #define _ASM_X86_KVM_PAGE_TRACK_H > =20 > enum kvm_page_track_mode { > + KVM_PAGE_TRACK_PREWRITE, Heh, just when I decide to finally kill off support for multiple modes[1] := -) My assessment from that changelog still holds true for this case: Drop "support" for multiple page-track modes, as there is no evidence that array-based and refcounted metadata is the optimal solution for other modes, nor is there any evidence that other use cases, e.g. for access-tracking, will be a good fit for the page-track machinery in general. =20 E.g. one potential use case of access-tracking would be to prevent guest access to poisoned memory (from the guest's perspective). In that case, the number of poisoned pages is likely to be a very small percentage of the guest memory, and there is no need to reference count the number of access-tracking users, i.e. expanding gfn_track[] for a new mode would be grossly inefficient. And for poisoned memory, host userspace would also likely want to trap accesses, e.g. to inject #MC into the guest, and that isn't currently supported by the page-track framework. =20 A better alternative for that poisoned page use case is likely a variation of the proposed per-gfn attributes overlay (linked), which would allow efficiently tracking the sparse set of poisoned pages, and by default would exit to userspace on access. Of particular relevance: - Using the page-track machinery is inefficient because the guest is like= ly going to write-protect a minority of its memory. And this select KVM_EXTERNAL_WRITE_TRACKING if KVM is particularly nasty because simply enabling HEKI in the Kconfig will = cause KVM to allocate rmaps and gfn tracking. - There's no need to reference count the protection, i.e. 15 of the 16 bi= ts of gfn_track are dead weight. - As proposed, adding a second "mode" would double the cost of gfn tracki= ng. - Tying the protections to the memslots will create an impossible-to-main= tain ABI because the protections will be lost if the owning memslot is delet= ed and recreated. - The page-track framework provides incomplete protection and will lead t= o an ongoing game of whack-a-mole, e.g. this patch catches the obvious cases= by adding calls to kvm_page_track_prewrite(), but misses things like kvm_v= cpu_map(). - The scaling and maintenance issues will only get worse if/when someone = tries to support dropping read and/or execute permissions, e.g. for execute-o= nly. - The code is x86-only, and is likely to stay that way for the foreseeabl= e future. The proposed alternative is to piggyback the memory attributes implementati= on[2] that is being added (if all goes according to plan) for confidential VMs. = This use case (dropping permissions) came up not too long ago[3], which is why I= have a ready-made answer). I have no doubt that we'll need to solve performance and scaling issues wit= h the memory attributes implementation, e.g. to utilize xarray multi-range suppor= t instead of storing information on a per-4KiB-page basis, but AFAICT, the co= re idea is sound. And a very big positive from a maintenance perspective is t= hat any optimizations, fixes, etc. for one use case (CoCo vs. hardening) should= also benefit the other use case. [1] https://lore.kernel.org/all/20230311002258.852397-22-seanjc@google.com [2] https://lore.kernel.org/all/Y2WB48kD0J4VGynX@google.com [3] https://lore.kernel.org/all/Y1a1i9vbJ%2FpVmV9r@google.com