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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 239C0C636CD for ; Tue, 7 Feb 2023 10:40:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ViVR+KAlh1jGXEMkynreyVQ7MN5HX9LpjF/rCXgJRwk=; b=C8a34F8xBYqW0Z D8rY1PuqN8thl1M0x80gGj5gQrqCUuCYf1NnbCTOva62Hbw/8/jGurpsa8XAScaDlQPKlU+bR3SOX f+/EOtLn+rrc1c4Ns49W+MG3TnaIypOpkLFwP5oh+st3tB9hShoZvj0IPwegiRPj7CHFdT2MrV8Fc gGmzuD5sej2CgxWq6PacTTscmVe3+8FwI0VLxDPZdKdmdfJXy3aSD4sZy4NeNRaTq2SnC0ko1SyXF Qa71aPYR7yPWMToWmeMy7eGpBSe58FkFv6Q/t2XMwnoXha3CWox8O8+7i0b2OVq9+HAmWKuEquisK B8+PWOEPLtobZTKCXdEA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pPLOH-00BlBs-B6; Tue, 07 Feb 2023 10:39:57 +0000 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pPLOD-00BlAj-Pn for linux-arm-kernel@lists.infradead.org; Tue, 07 Feb 2023 10:39:55 +0000 Received: by mail-wr1-x434.google.com with SMTP id o18so13090436wrj.3 for ; Tue, 07 Feb 2023 02:39:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=Ezuf8zIGtjTbwprIBH3RkeiO7eWB8R8vQxUpLhGnoGw=; b=xUgt49eDesXWBFfzjD7n4TFdFOPbhMqmAkmaDcQH1O33LN7B2lcB7F1wc4hYi3wmv1 r2hEgVKt/oPCqgxpok1UG2a0ILNWdzHuypas15hOUaPavVxoc6ea7c757XOj7HTgh72S SmzjrczVxPKuzed5ze9dxd6V65pu9+h88/y/cb558ytu5qjDB3sp8PWJ3f0xJ9ha5PoJ vRED/uIoGf3O3sYnGYRWy0aXnDYNOfJzjQKTK/Tj9AwqXAfmokk6dcuWJeJGFgpneV/y gxrskFJsHWVoQwdjj0VD0pYzcmP+cVivQO5tZnPknSFZbcsDxbpR9drG7X5LP2UPL2K/ 3h3g== 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=Ezuf8zIGtjTbwprIBH3RkeiO7eWB8R8vQxUpLhGnoGw=; b=I9/DYD6io0YGq6pLn/qVon4p1LG9Bfartwd1o7kJ6Yr/aEKs2SZG/dfIfZoGCbMUiL GDdW0b70jZpG9HTPZ6sg7XZx8EBYuflm7RTV0JvPR8lSNvJ99DghjfUdWKcVeXFBJ+Zw pNaknOa+d/6Ca2WFRTx/LcfZ8k4gdPLlDSBdeY1g9J+XgEd67fcCAtem81mrYuuOyfyl WW6Twaz3ypoOHOroF8NvFh882aFS+RrPuBL6Zz3OnzQhYSMDsLLSFOQrVykpg1FJT1L0 MWlb21uB7RR3SmA9bRj40CrBKUNlZv62tdB4x3zxVvPKDfdWAIG+PBLzpjaJY5yQzAaT H0Fw== X-Gm-Message-State: AO0yUKUBnZCr22gIhTmtfRtx6hNTdm2IQCMcFiSmEoWdOjlmljmyUEFM sRSCeUm5yj4l4y88aG0QmS1jQQ== X-Google-Smtp-Source: AK7set8URL8FZwsSiW+sDnFANc5QHMcYb6CW6VXqlAe361UtKx/EnW5TcnLZYj/p4W5+PO6ACStkBw== X-Received: by 2002:adf:f712:0:b0:2bf:b281:1bbb with SMTP id r18-20020adff712000000b002bfb2811bbbmr2244453wrp.36.1675766391394; Tue, 07 Feb 2023 02:39:51 -0800 (PST) Received: from myrica (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id l5-20020adfe585000000b002c3e3ee7d1asm6290379wrm.79.2023.02.07.02.39.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Feb 2023 02:39:51 -0800 (PST) Date: Tue, 7 Feb 2023 10:39:46 +0000 From: Jean-Philippe Brucker To: "tina.zhang" Cc: maz@kernel.org, catalin.marinas@arm.com, will@kernel.org, joro@8bytes.org, robin.murphy@arm.com, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, smostafa@google.com, dbrazdil@google.com, ryan.roberts@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, iommu@lists.linux.dev Subject: Re: [RFC PATCH 15/45] KVM: arm64: pkvm: Add __pkvm_host_share/unshare_dma() Message-ID: References: <20230201125328.2186498-1-jean-philippe@linaro.org> <20230201125328.2186498-16-jean-philippe@linaro.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230207_023953_863767_36ADE08A X-CRM114-Status: GOOD ( 30.97 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, Feb 07, 2023 at 10:37:51AM +0800, tina.zhang wrote: > Hi Jean, > > On 2/6/23 20:13, Jean-Philippe Brucker wrote: > > Hi Tina, > > > > On Sat, Feb 04, 2023 at 08:51:38PM +0800, tina.zhang wrote: > > > > +int __pkvm_host_share_dma(phys_addr_t phys_addr, size_t size, bool is_ram) > > > > +{ > > > > + int i; > > > > + int ret; > > > > + size_t nr_pages = size >> PAGE_SHIFT; > > > > + > > > > + if (WARN_ON(!PAGE_ALIGNED(phys_addr | size))) > > > > + return -EINVAL; > > > > + > > > > + host_lock_component(); > > > > + hyp_lock_component(); > > > > + > > > > + for (i = 0; i < nr_pages; i++) { > > > > + ret = __pkvm_host_share_dma_page(phys_addr + i * PAGE_SIZE, > > > > + is_ram); > > > Hi Jean, > > > > > > I'm not familiar with ARM arch. Just out of curiosity. If pKVM-ARM populates > > > the host stage-2 page table lazily, would there be a case that device driver > > > in host triggers DMA with pages which have not been mapped to the host > > > stage-2 page table yet? How do we handle this situation? > > > > It's possible that the host asks the hypervisor to map on the IOMMU side > > a page that is not yet mapped on the CPU side. In general before calling > > map_pages() the host zero-initializes the page, triggering a page fault > > which creates the mapping, so this case is rare. But if it happens, > > __pkvm_host_share_dma() will create the CPU stage-2 mapping: > > > > __pkvm_host_share_dma() > > do_share() > > host_initiate_share() > > host_stage2_idmap_locked() > > > > which creates a valid identity mapping, along with the ownership > > information PVKM_PAGE_SHARED_OWNED. That ownership info is really all we > > need here, to prevent future donations to guests or hyp. Since the SMMU > > side uses separate stage-2 page tables, we don't actually need to create a > > valid mapping on the CPU side yet, that's just how pKVM's mem_protect > > currently works. But I don't think it hurts to create the mapping right > > away instead of waiting for the CPU page fault, because the host will > > likely access the page soon to read what the device wrote there. > Right. I was thinking if stage-2 lazy mapping method is being adopted, > whether there would be a case that the check_share() couldn't pass, as the > pte might not be valid at that time. After checking the logic, I see we > check kvm_pte_valid(pte) as well as the value of pte. So I guess the > check_share() can return successfully even w/o having triggered host CPU > page fault. Yes, during check_share(), host_get_page_state() allows disabled PTE unless the PTE already contains ownership information (in particular, if the page has been donated to a guest or to the hypervisor, the PTE is invalid and contains the non-zero owner ID.) Thanks, Jean _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel