Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp433034rwr; Wed, 26 Apr 2023 01:05:31 -0700 (PDT) X-Google-Smtp-Source: AKy350ZUrKGeW1R5juyXAgjVPoErPp7AWSCQeQg1j9FaWVoX+eDXuzsXb0MEJeYrhzxrXN4yKRB0 X-Received: by 2002:a05:6a20:3d1d:b0:db:b7:fe3f with SMTP id y29-20020a056a203d1d00b000db00b7fe3fmr26985179pzi.10.1682496331040; Wed, 26 Apr 2023 01:05:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682496331; cv=none; d=google.com; s=arc-20160816; b=cyjixhZx97eXQsOVlro7Wl5XU3l/GnkJVOZk31vBxoyjK82Pf3itfMqbVZw+3kHdGx 2dwQG/4SUSMqYoyNdeHT9LjyadK8b6n6j31BbMOu3RiD1GXf+mR9aEE7kVGG0zgkbQO+ NsPYC8F7QtgZ1fKDyeMBdZWexLZ7+D0O824ghiRzl/5ZSZY2UNkEQ+9gQy4ZFTaaaAhi 2cMn3XpudxUL1xSSnZB3T3d1sVdnd2xPsJySsggZYGmZOjDg/mzg6SnRLMXdJVW87GzN NbhscRw0rBARJSNFcGWSn0na9gagFova64ku6R+qE4sq9aS+0MJVfQpU578oDa8eJZ/I r37w== 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:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=c6VqCfb1kZ/+ni6WW1VeBoVtLPJZGHHpNASSaglTaKY=; b=kKBCgmI5HKd3uzq0MhTHOw2n1z6knUH5rw5xx+OCOHGmKixwPoLQSgmD6A49UtDtt9 Id0X5YxbsLiS9WQAqgXr4ryCroj5xLUKMEZpXnBXF689YESpxC82Ez+uAslR46iTYmbc OCAX3ftzifOFHg1kOXsmCO6Gbfl7QMuyHDV8VVf7Q8gjaNlmOw/B5GnqrWSZzpcIUWoU vXx5HIf+2FvR4K295uqzWeEsP662fM2Zuze90R966ELG8AEnZgFF61Hbx0IS1DkFzAyB tVfuR5gOHfRO9uGe6P+LtzDt6JUbABAgQW4+EMFgN6s5P6AnOV10cYdG+rb7enizhl0X K2wA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=Hp3aE02H; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e14-20020a63d94e000000b00527d158ec6fsi6680596pgj.37.2023.04.26.01.05.19; Wed, 26 Apr 2023 01:05:31 -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=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=Hp3aE02H; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239840AbjDZICz (ORCPT + 99 others); Wed, 26 Apr 2023 04:02:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231467AbjDZICx (ORCPT ); Wed, 26 Apr 2023 04:02:53 -0400 Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C01843593 for ; Wed, 26 Apr 2023 01:02:51 -0700 (PDT) Received: by mail-lj1-x22f.google.com with SMTP id 38308e7fff4ca-2a8aea2a654so65958121fa.1 for ; Wed, 26 Apr 2023 01:02:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1682496170; x=1685088170; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=c6VqCfb1kZ/+ni6WW1VeBoVtLPJZGHHpNASSaglTaKY=; b=Hp3aE02HRO0m3EBKxD02RPwATf5ceTDh/i9USme207oxn1diekd5bTeXtZQSKuJrxc lqxuBO0dtGbO01nQ1KD4vl/RkVBN0QnvLtUu7Y6aZDFdB7OiGGLCXEmGM6h6YHIWwtBU HxyaJiWfbNR6Zuemq5kBjaycfIlDoYxHVaVH4FxsN9o0Est2c0n5wikAMnmtFiXX9PgM X9mbBgGkR1aQGsADNvNuE/7vP4qPCdq71Cq7Eyt/io68aRAjbtbK6b4fMCrAjZwWkK3/ k44hCbUubgZlGOX/Wn/x/Ris46x7S735iq4yyoAI4E97YMPF+plb+C8uS1LYmylluBR2 UcIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682496170; x=1685088170; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c6VqCfb1kZ/+ni6WW1VeBoVtLPJZGHHpNASSaglTaKY=; b=jpWeDUjq2PyEctaAVaCnREsR+o/auPDGtRlnH1FBi8xQuIwEni+4YVJlc3C2zYYhNj Vqe57JYBEzIleAZD/210+KgU0zS3ytDFmAKbkQEHzm0XDO6BR+EpQYRILWpQqPgjyz3P 6CcoWcnud6yfdk64XLQYvNALbmJg8+YFi9fuihjWRIre5XewfA3wNX/nBRhCrkeYk3HJ tMqLWx5sL65DQpioZeBBn7KsKDsHPQbhJgYDzWj36LVNII8Ww1HSCYxF/L68m+gpVaxU zeCwDuvuDCeo8VjDYHWgpwDgxtpfskNRCQ0djgv/pFY+HPgIxqW+GHHTwvhLvyD9Jp1F Y32g== X-Gm-Message-State: AAQBX9c2w5w6E0/LbnnEASrC8yhG5YiJOGOJHUW3rxsnwHBUvndGKbmy MG9BlWfEDeKHuhrVl4u9Qamg7hTx4PlSSwaVglk83w== X-Received: by 2002:a2e:a41a:0:b0:2a1:ab4a:153d with SMTP id p26-20020a2ea41a000000b002a1ab4a153dmr4970025ljn.29.1682496169872; Wed, 26 Apr 2023 01:02:49 -0700 (PDT) MIME-Version: 1.0 References: <20230419221716.3603068-1-atishp@rivosinc.com> <20230419221716.3603068-46-atishp@rivosinc.com> <69ba1760-a079-fd8f-b079-fcb01e3eedec@intel.com> <81c476f4-ef62-e4a6-0033-8a46a15379fd@intel.com> In-Reply-To: From: Atish Kumar Patra Date: Wed, 26 Apr 2023 13:32:38 +0530 Message-ID: Subject: Re: [RFC 45/48] RISC-V: ioremap: Implement for arch specific ioremap hooks To: Dave Hansen Cc: linux-kernel@vger.kernel.org, Rajnesh Kanwal , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Suzuki K Poulose , Will Deacon , Marc Zyngier , Sean Christopherson , linux-coco@lists.linux.dev, Dylan Reid , abrestic@rivosinc.com, Samuel Ortiz , Christoph Hellwig , Conor Dooley , Greg Kroah-Hartman , Guo Ren , Heiko Stuebner , Jiri Slaby , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, Mayuresh Chitale , Palmer Dabbelt , Paolo Bonzini , Paul Walmsley , Uladzislau Rezki Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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 Tue, Apr 25, 2023 at 6:40=E2=80=AFPM Dave Hansen = wrote: > > On 4/25/23 01:00, Atish Kumar Patra wrote: > > On Mon, Apr 24, 2023 at 7:18=E2=80=AFPM Dave Hansen wrote: > >> On 4/21/23 12:24, Atish Kumar Patra wrote: > >> I'm not _quite_ sure what "guest initiated" means. But SEV and TDX > >> don't require an ioremap hook like this. So, even if they *are* "gues= t > >> initiated", the question still remains how they work without this patc= h, > >> or what they are missing without it. > > > > Maybe I misunderstood your question earlier. Are you concerned about gu= ests > > invoking any MMIO region specific calls in the ioremap path or passing > > that information to the host ? > > My concern is that I don't know why this patch is here. There should be > a very simple answer to the question: Why does RISC-V need this patch > but x86 does not? > > > Earlier, I assumed the former but it seems you are also concerned > > about the latter as well. Sorry for the confusion in that case. > > The guest initiation is necessary while the host notification can be > > made optional. > > The "guest initiated" means the guest tells the TSM (equivalent of TDX > > module in RISC-V) the MMIO region details. > > The TSM keeps a track of this and any page faults that happen in that > > region are forwarded > > to the host by the TSM after the instruction decoding. Thus TSM can > > make sure that only ioremapped regions are > > considered MMIO regions. Otherwise, all memory outside the guest > > physical region will be considered as the MMIO region. > > Ahh, OK, that's a familiar problem. I see the connection to device > filtering now. > > Is this functionality in the current set? I went looking for it and all > I found was the host notification side. > The current series doesn't have the guest filtering feature enabled. However, we implemented guest filtering and is maintained in a separate tre= e https://github.com/rivosinc/linux/tree/cove-integration-device-filtering We did not include those in this series because the tdx patches are still undergoing development. We are planning to rebase our changes once the revised patches are available. > Is this the only mechanism by which the guest tells the TSM which parts > of the guest physical address space can be exposed to the host? > This is the current approach defined in CoVE spec. Guest informs about both shared memory & mmio regions via dedicated SBI calls ( e.g sbi_covg_[add/remove]_mmio_region and sbi_covg_[share/unshare]_memory_region) > For TDX and SEV, that information is inferred from a bit in the page > tables. Essentially, there are dedicated guest physical addresses that > tell the hardware how to treat the mappings: should the secure page > tables or the host's EPT/NPT be consulted? > Yes. We don't have such a mechanism defined in CoVE yet. Having said that, there is nothing in ISA to prevent that and it is doable. Some specific bits in the PTE entry can also be used to encode for shared & mmio physical memory addresses. The TSM implementation will probably need to implement a software page walker in that case. Are there any performance advantages between the two approaches ? As per my understanding, we are saving some boot time privilege transitions & less ABIs but adds the cost of software walk at runtime faults. > If that mechanism is different for RISC-V, it would go a long way to > explaining why RISC-V needs this patch. > > > In the current CoVE implementation, that MMIO region information is als= o > > passed to the host to provide additional flexibility. The host may > > choose to do additional > > sanity check and bail if the fault address does not belong to > > requested MMIO regions without > > going to the userspace. This is purely an optimization and may not be m= andatory. > > Makes sense, thanks for the explanation. > > >>> It can be a subset of the region's host provided the layout. The > >>> guest device filtering solution is based on this idea as well [1]. > >>> > >>> [1] https://lore.kernel.org/all/20210930010511.3387967-1-sathyanaraya= nan.kuppuswamy@linux.intel.com/ > >> > >> I don't really see the connection. Even if that series was going > >> forward (I'm not sure it is) there is no ioremap hook there. There's > >> also no guest->host communication in that series. The guest doesn't > >> _tell_ the host where the MMIO is, it just declines to run code for > >> devices that it didn't expect to see. > > > > This is a recent version of the above series from tdx github. This is > > a WIP as well and has not been posted to > > the mailing list. Thus, it may be going under revisions as well. > > As per my understanding the above ioremap changes for TDX mark the > > ioremapped pages as shared. > > The guest->host communication happen in the #VE exception handler > > where the guest converts this to a hypercall by invoking TDG.VP.VMCALL > > with an EPT violation set. The host would emulate an MMIO address if > > it gets an VMCALL with EPT violation. > > Please correct me if I am wrong. > > Yeah, TDX does: > > 1. Guest MMIO access > 2. Guest #VE handler (if the access faults) > 3. Guest hypercall->host > 4. Host fixes the fault > 5. Hypercall returns, guest returns from #VE via IRET > 6. Guest retries MMIO instruction > > From what you said, RISC-V appears to do: > > 1. Guest MMIO access > 2. Host MMIO handler > 3. Host handles the fault, returns > 4. Guest retries MMIO instruction > > In other words, this mechanism does the same thing but short-circuits > the trip through #VE and the hypercall. > Yes. Thanks for summarizing the tdx approach. > What happens if this ioremap() hook is not in place? Does the hardware > (or TSM) generate an exception like TDX gets? If so, it's probably > possible to move this "notify the TSM" code to that exception handler > instead of needing an ioremap() hook. > We don't have a #VE like exception mechanism in RISC-V. > I'm not saying that it's _better_ to do that, but it would allow you to > get rid of this patch for now and get me to shut up. :) > > > As I said above, the objective here is to notify the TSM where the > > MMIO is. Notifying the host is just an optimization that we choose to > > add. In fact, in this series the KVM code doesn't do anything with > > that information. The commit text probably can be improved to clarify > > that. > > Just to close the loop here, please go take a look at > pgprot_decrypted(). That's where the x86 guest page table bit gets to > tell the hardware that the mapping might cause a #VE and is under the > control of the host. That's the extent of what x86 does at ioremap() tim= e. > > So, to summarize, we have: > > x86: > 1. Guest page table bit to mark shared (host) vs. private (guest) > control > 2. #VE if there is a fault on a shared mapping to call into the host > > RISC-V: > 1. Guest->TSM call to mark MMIO vs. private > 2. Faults in the MMIO area are then transparent to the guest > Yup. This discussion raised a very valid design aspect of the CoVE spec. To summarize, we need to investigate whether using PTE bits instead of additional ABI for managing shared/confidential/ioremapped pages makes more sense. Thanks for putting up with my answers and the feedback :). > That design difference would, indeed, help explain why this patch is > here. I'm still not 100% convinced that the patch is *required*, but I > at least understand how we arrived here.