From patchwork Thu Feb 22 23:16:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13568309 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 A79B9C47DD9 for ; Thu, 22 Feb 2024 23:24:27 +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:CC:To:In-Reply-To:References:Message-ID :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=L0x+vDEQR1MJ9JFwr2gDuyXhx3ylLfiLFTcsagcoR/4=; b=TfMiIhtyulKojw xcmuJA+EhZj8Q4o8c6tAc5CxpCCyA9MWjhzHURPqnWhRpw6mmS4dGSK4vvwg5Cskm6PZpn7H+KoEl 6bJxp/Hca5EtcIXdvhR5SlvsQUZL6qwupvn3ffz23LIuPAx+6Nine4UoAdwzqEKzpvnRCGUZpU2x/ KHheglsDmN7O98e9FbQXqRjKtIVnbXWyOlT5QmjfOBCmiTnJOd2nr7z99/Q8tatqH+g3FtWYctaHb AEIfocgIoJa4uY8dW/+1zbrv3TCy4ethAYvemp08wIFOXOP4WlbCCRxvA4liYpnPdtnnc11aJau30 EkUpqXBq8rO7M4s6xQvg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rdIQF-00000006yQx-1K8O; Thu, 22 Feb 2024 23:24:11 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rdIJP-00000006vuf-3z6k for linux-arm-kernel@bombadil.infradead.org; Thu, 22 Feb 2024 23:17:17 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=CC:To:In-Reply-To:References:Message-ID :Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From:Sender :Reply-To:Content-ID:Content-Description; bh=vo+GeAkZrIZtCLsV/xLZNLxXgjO/d+8dOj+GwHIQAtA=; b=s5FLZIAfsfAOP77EFKsuRriJY6 0FoiJlLY4LKF9/x6bbdXMoigurwdVfXH5CKUd9Bs2PkXTeqGK2dluhKTbinmFb4Vt/BdZkbCfR1T3 Cr1qNEPFUjgrFgWubdxK78NYaowVRgKmo3O7DSsJKj+65K8nCNDnQRKK0pmfEYYxhUJz0BXn1rrx1 BUDvs4iN1SrUSKTmU+eCHAyUxKDjcy2uCGg7DpHOXfXYJcGwj94ATqwNAMBmXQcVkyKm5n1s21HSU H/IhVmzJ3kpqSTjamJwOfUsop3IMl7EfTpVZJrI+auZtX+x1nT0v29S/Aac0ai1mzRsJJ9c9lysKH 0+6a/CnQ==; Received: from mx0a-0031df01.pphosted.com ([205.220.168.131]) by casper.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rdIJK-00000004zdF-3SZi for linux-arm-kernel@lists.infradead.org; Thu, 22 Feb 2024 23:17:06 +0000 Received: from pps.filterd (m0279862.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 41MMgV2w013376; Thu, 22 Feb 2024 23:16:44 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:date:subject:mime-version:content-type :content-transfer-encoding:message-id:references:in-reply-to:to :cc; s=qcppdkim1; bh=vo+GeAkZrIZtCLsV/xLZNLxXgjO/d+8dOj+GwHIQAtA =; b=A7W7wqejQwx3z1l/oM1GMtburTD6EFddQKuBnKoGYLGt2+b9GKt7z5tgI+G tQHISS7AKjdH4P9r6NEvYcAS1YEnqMEl4l7LBJGz+lBPWzDkqhGp7J00N/ASyFv7 giXzs6CtafrawcvfKE6jMLd5ioTl+Yzaz/N9ZHiNFn7ZA9JkxjWn4vXULo5frsSc 7Vy3gId/qgro/Z87uWkyIb9/gmkAT2Q+Fvi3uvaZvGzBlFUaA/T25boT/q52goHP ub10yfU0ASdLHaWMCuQGHCwxf1FeVtHZ9WkKVYXBGi8CnWC/MwZ0Voeqme+oX/is 6YtjBUgtQxGqEuVVP3Dwrb9DtKA== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3wea7csc2y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 22 Feb 2024 23:16:43 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA02.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 41MNGhwg027798 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 22 Feb 2024 23:16:43 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Thu, 22 Feb 2024 15:16:42 -0800 From: Elliot Berman Date: Thu, 22 Feb 2024 15:16:51 -0800 Subject: [PATCH v17 28/35] virt: gunyah: Enable demand paging MIME-Version: 1.0 Message-ID: <20240222-gunyah-v17-28-1e9da6763d38@quicinc.com> References: <20240222-gunyah-v17-0-1e9da6763d38@quicinc.com> In-Reply-To: <20240222-gunyah-v17-0-1e9da6763d38@quicinc.com> To: Alex Elder , Srinivas Kandagatla , Murali Nalajal , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Philip Derrin , Prakruthi Deepak Heragu , Jonathan Corbet , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Catalin Marinas , Will Deacon , Konrad Dybcio , Bjorn Andersson , Dmitry Baryshkov , "Fuad Tabba" , Sean Christopherson , "Andrew Morton" CC: , , , , , , Elliot Berman X-Mailer: b4 0.12.4 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01c.na.qualcomm.com (10.47.97.35) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: LcPB2YrX8aG6g6aI739ImLk5MugK1jGr X-Proofpoint-GUID: LcPB2YrX8aG6g6aI739ImLk5MugK1jGr X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-22_15,2024-02-22_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 priorityscore=1501 mlxscore=0 suspectscore=0 adultscore=0 impostorscore=0 malwarescore=0 phishscore=0 spamscore=0 bulkscore=0 lowpriorityscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2402120000 definitions=main-2402220179 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240222_231703_239974_D9E0C22A X-CRM114-Status: GOOD ( 22.64 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Tell resource manager to enable demand paging and wire vCPU faults to provide the backing folio when a guestmemfd is bound to the faulting access. Signed-off-by: Elliot Berman --- drivers/virt/gunyah/guest_memfd.c | 94 +++++++++++++++++++++++++++++++++++++++ drivers/virt/gunyah/gunyah_vcpu.c | 41 +++++++++++++---- drivers/virt/gunyah/vm_mgr.c | 29 ++++++++++++ drivers/virt/gunyah/vm_mgr.h | 3 ++ 4 files changed, 159 insertions(+), 8 deletions(-) diff --git a/drivers/virt/gunyah/guest_memfd.c b/drivers/virt/gunyah/guest_memfd.c index 0c35ffa616d9c..797bc29a520bc 100644 --- a/drivers/virt/gunyah/guest_memfd.c +++ b/drivers/virt/gunyah/guest_memfd.c @@ -891,3 +891,97 @@ int gunyah_gmem_reclaim_parcel(struct gunyah_vm *ghvm, return 0; } + +int gunyah_gmem_setup_demand_paging(struct gunyah_vm *ghvm) +{ + struct gunyah_rm_mem_entry *entries; + struct gunyah_gmem_binding *b; + unsigned long index = 0; + u32 count = 0, i; + int ret = 0; + + down_read(&ghvm->bindings_lock); + mt_for_each(&ghvm->bindings, b, index, ULONG_MAX) + if (gunyah_guest_mem_is_lend(ghvm, b->flags)) + count++; + + if (!count) + goto out; + + entries = kcalloc(count, sizeof(*entries), GFP_KERNEL); + if (!entries) { + ret = -ENOMEM; + goto out; + } + + index = i = 0; + mt_for_each(&ghvm->bindings, b, index, ULONG_MAX) { + if (!gunyah_guest_mem_is_lend(ghvm, b->flags)) + continue; + entries[i].phys_addr = cpu_to_le64(gunyah_gfn_to_gpa(b->gfn)); + entries[i].size = cpu_to_le64(b->nr << PAGE_SHIFT); + if (++i == count) + break; + } + + ret = gunyah_rm_vm_set_demand_paging(ghvm->rm, ghvm->vmid, i, entries); + kfree(entries); +out: + up_read(&ghvm->bindings_lock); + return ret; +} + +int gunyah_gmem_demand_page(struct gunyah_vm *ghvm, u64 gpa, bool write) +{ + unsigned long gfn = gunyah_gpa_to_gfn(gpa); + struct gunyah_gmem_binding *b; + struct folio *folio; + int ret; + + down_read(&ghvm->bindings_lock); + b = mtree_load(&ghvm->bindings, gfn); + if (!b) { + ret = -ENOENT; + goto unlock; + } + + if (write && !(b->flags & GUNYAH_MEM_ALLOW_WRITE)) { + ret = -EPERM; + goto unlock; + } + + folio = gunyah_gmem_get_folio(file_inode(b->file), + gunyah_gfn_to_off(b, gfn)); + if (!folio) { + ret = -ENOMEM; + pr_err_ratelimited("Failed to obtain memory for guest addr %016llx\n", gpa); + goto unlock; + } + + /** + * the folio covers the requested guest address, but the folio may not + * start at the requested guest address. recompute the gfn based on the + * folio itself. + */ + gfn = gunyah_off_to_gfn(b, folio_index(folio)); + + filemap_invalidate_lock_shared(b->file->f_mapping); + ret = gunyah_vm_provide_folio(ghvm, folio, gfn, + !gunyah_guest_mem_is_lend(ghvm, b->flags), + !!(b->flags & GUNYAH_MEM_ALLOW_WRITE)); + filemap_invalidate_unlock_shared(b->file->f_mapping); + if (ret) { + if (ret != -EAGAIN) + pr_err_ratelimited( + "Failed to provide folio for guest addr: %016llx: %d\n", + gpa, ret); + goto out; + } +out: + folio_unlock(folio); + folio_put(folio); +unlock: + up_read(&ghvm->bindings_lock); + return ret; +} +EXPORT_SYMBOL_GPL(gunyah_gmem_demand_page); diff --git a/drivers/virt/gunyah/gunyah_vcpu.c b/drivers/virt/gunyah/gunyah_vcpu.c index 8192350f180b0..f8306620b1dd6 100644 --- a/drivers/virt/gunyah/gunyah_vcpu.c +++ b/drivers/virt/gunyah/gunyah_vcpu.c @@ -89,29 +89,44 @@ static irqreturn_t gunyah_vcpu_irq_handler(int irq, void *data) return IRQ_HANDLED; } -static void gunyah_handle_page_fault( +static bool gunyah_handle_page_fault( struct gunyah_vcpu *vcpu, const struct gunyah_hypercall_vcpu_run_resp *vcpu_run_resp) { u64 addr = vcpu_run_resp->state_data[0]; + bool write = !!vcpu_run_resp->state_data[1]; + int ret = 0; + + ret = gunyah_gmem_demand_page(vcpu->ghvm, addr, write); + if (!ret || ret == -EAGAIN) + return true; vcpu->vcpu_run->page_fault.resume_action = GUNYAH_VCPU_RESUME_FAULT; - vcpu->vcpu_run->page_fault.attempt = 0; + vcpu->vcpu_run->page_fault.attempt = ret; vcpu->vcpu_run->page_fault.phys_addr = addr; vcpu->vcpu_run->exit_reason = GUNYAH_VCPU_EXIT_PAGE_FAULT; + return false; } -static void -gunyah_handle_mmio(struct gunyah_vcpu *vcpu, +static bool +gunyah_handle_mmio(struct gunyah_vcpu *vcpu, unsigned long resume_data[3], const struct gunyah_hypercall_vcpu_run_resp *vcpu_run_resp) { u64 addr = vcpu_run_resp->state_data[0], len = vcpu_run_resp->state_data[1], data = vcpu_run_resp->state_data[2]; + int ret; if (WARN_ON(len > sizeof(u64))) len = sizeof(u64); + ret = gunyah_gmem_demand_page(vcpu->ghvm, addr, + vcpu->vcpu_run->mmio.is_write); + if (!ret || ret == -EAGAIN) { + resume_data[1] = GUNYAH_ADDRSPACE_VMMIO_ACTION_RETRY; + return true; + } + if (vcpu_run_resp->state == GUNYAH_VCPU_ADDRSPACE_VMMIO_READ) { vcpu->vcpu_run->mmio.is_write = 0; /* Record that we need to give vCPU user's supplied value next gunyah_vcpu_run() */ @@ -128,11 +143,15 @@ gunyah_handle_mmio(struct gunyah_vcpu *vcpu, vcpu->mmio_addr = vcpu->vcpu_run->mmio.phys_addr = addr; vcpu->vcpu_run->mmio.len = len; vcpu->vcpu_run->exit_reason = GUNYAH_VCPU_EXIT_MMIO; + + return false; } static int gunyah_handle_mmio_resume(struct gunyah_vcpu *vcpu, unsigned long resume_data[3]) { + bool write = vcpu->state == GUNYAH_VCPU_RUN_STATE_MMIO_WRITE; + switch (vcpu->vcpu_run->mmio.resume_action) { case GUNYAH_VCPU_RESUME_HANDLED: if (vcpu->state == GUNYAH_VCPU_RUN_STATE_MMIO_READ) { @@ -148,6 +167,8 @@ static int gunyah_handle_mmio_resume(struct gunyah_vcpu *vcpu, resume_data[1] = GUNYAH_ADDRSPACE_VMMIO_ACTION_FAULT; break; case GUNYAH_VCPU_RESUME_RETRY: + /* userspace probably added a memory binding */ + gunyah_gmem_demand_page(vcpu->ghvm, vcpu->mmio_addr, write); resume_data[1] = GUNYAH_ADDRSPACE_VMMIO_ACTION_RETRY; break; default: @@ -310,11 +331,15 @@ static int gunyah_vcpu_run(struct gunyah_vcpu *vcpu) break; case GUNYAH_VCPU_ADDRSPACE_VMMIO_READ: case GUNYAH_VCPU_ADDRSPACE_VMMIO_WRITE: - gunyah_handle_mmio(vcpu, &vcpu_run_resp); - goto out; + if (!gunyah_handle_mmio(vcpu, resume_data, + &vcpu_run_resp)) + goto out; + break; case GUNYAH_VCPU_ADDRSPACE_PAGE_FAULT: - gunyah_handle_page_fault(vcpu, &vcpu_run_resp); - goto out; + if (!gunyah_handle_page_fault(vcpu, + &vcpu_run_resp)) + goto out; + break; default: pr_warn_ratelimited( "Unknown vCPU state: %llx\n", diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c index 198a99d78c44c..473740a1f97d0 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -474,6 +474,23 @@ static int gunyah_vm_start(struct gunyah_vm *ghvm) goto err; } + ret = gunyah_gmem_setup_demand_paging(ghvm); + if (ret) { + dev_warn(ghvm->parent, + "Failed to set up gmem demand paging: %d\n", ret); + goto err; + } + + ret = gunyah_rm_vm_set_address_layout( + ghvm->rm, ghvm->vmid, GUNYAH_RM_RANGE_ID_IMAGE, + ghvm->dtb.parcel_start << PAGE_SHIFT, + ghvm->dtb.parcel_pages << PAGE_SHIFT); + if (ret) { + dev_warn(ghvm->parent, + "Failed to set location of DTB mem parcel: %d\n", ret); + goto err; + } + ret = gunyah_rm_vm_init(ghvm->rm, ghvm->vmid); if (ret) { ghvm->vm_status = GUNYAH_RM_VM_STATUS_INIT_FAILED; @@ -501,8 +518,20 @@ static int gunyah_vm_start(struct gunyah_vm *ghvm) gunyah_vm_add_resource(ghvm, ghrsc); } + ret = gunyah_vm_parcel_to_paged(ghvm, &ghvm->dtb.parcel, + ghvm->dtb.parcel_start, + ghvm->dtb.parcel_pages); + if (ret) + goto err; + ret = gunyah_rm_vm_start(ghvm->rm, ghvm->vmid); if (ret) { + /** + * need to rollback parcel_to_paged because RM is still + * tracking the parcel + */ + gunyah_vm_mm_erase_range(ghvm, ghvm->dtb.parcel_start, + ghvm->dtb.parcel_pages); dev_warn(ghvm->parent, "Failed to start VM: %d\n", ret); goto err; } diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h index d8c299e5cf7cc..5eb14f30ba965 100644 --- a/drivers/virt/gunyah/vm_mgr.h +++ b/drivers/virt/gunyah/vm_mgr.h @@ -199,4 +199,7 @@ int gunyah_gmem_reclaim_parcel(struct gunyah_vm *ghvm, struct gunyah_rm_mem_parcel *parcel, u64 gfn, u64 nr); +int gunyah_gmem_setup_demand_paging(struct gunyah_vm *ghvm); +int gunyah_gmem_demand_page(struct gunyah_vm *ghvm, u64 gpa, bool write); + #endif