From patchwork Tue Jan 9 19:38:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13515294 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A097FC4706C for ; Tue, 9 Jan 2024 19:39:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 568728D0016; Tue, 9 Jan 2024 14:38:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 516048D0017; Tue, 9 Jan 2024 14:38:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34E148D0016; Tue, 9 Jan 2024 14:38:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 17B8E8D0017 for ; Tue, 9 Jan 2024 14:38:18 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DF0E1120A05 for ; Tue, 9 Jan 2024 19:38:17 +0000 (UTC) X-FDA: 81660783834.25.097DB0A Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by imf02.hostedemail.com (Postfix) with ESMTP id B60FF80016 for ; Tue, 9 Jan 2024 19:38:15 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=N9WJqtBu; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf02.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704829095; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xmkTmjNsdPCs10aYcSn7XO86TvYDKbjp9vja8YDUKN0=; b=h0fRMwnFRF3o4pot1Q3sjA3akr1x6C3H9x43Z0eNrbSkY3QOHnNVhUCffgWsEE1/Eyc9to wfaeksFeXXVsVMfANDHZlJu3eo80mlHLlUEELnKAr9uetwx90nuOp6YkIL3SAHvmQPHut+ eJO4c6XLXrrpnSoiSWZ417VNOiEVuaA= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=N9WJqtBu; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf02.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704829095; a=rsa-sha256; cv=none; b=e4JJ7/PNUOH2XCBI8Ksi8gZO85rUF8zGuEzZXkjSRofwbaMMS0kiA//llV96ADXluW4rpn NUOKrmVAi72FY86mZVVTWtwx/eMvxQH6pvYo79tyWcugarUFC9IDspViriTnJEZTTzvzOZ EngOjw8/I/acmXv9shgQMxel16R4Op8= Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 409GTKRk006618; Tue, 9 Jan 2024 19:38:08 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=xmkTmjNsdPCs10aYcSn7XO86TvYDKbjp9vja8YDUKN0 =; b=N9WJqtBujwwUhLG/HBE5Z/UFBG1ofvojUfp3iKfVzqKaxm/kS8nZQuIPWFi vOwR3KVMHLY4s0bMG/nyHrPGegjtSCEkq4Rsc2S1fNFNUNfvOwM4FeeWyrYVEfD5 CmmDFLUggrFTzFcdp1pjtuijkItvKcbfhSWXQ6eI2aC5csVZvr0vyPhsqW5/6R9u GItgPaoqr6Vb55R960tUsBD3LQ0E08l5WdYZ80Ztks6ewnjQPECy7t0lKLj/nzMx CbsMkO8iGaux+Hw/lr0oEhe1eCnI0D+qS/sVcHAZY69KbIMZ4+b4Juw7IXJrbO80 oVy9SkDPMumDF8zffBpw3D+Q10w== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3vh9q70egv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 09 Jan 2024 19:38:08 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 409Jc7lf011554 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 9 Jan 2024 19:38:07 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; Tue, 9 Jan 2024 11:38:06 -0800 From: Elliot Berman Date: Tue, 9 Jan 2024 11:38:05 -0800 Subject: [PATCH v16 27/34] virt: gunyah: Enable demand paging MIME-Version: 1.0 Message-ID: <20240109-gunyah-v16-27-634904bf4ce9@quicinc.com> References: <20240109-gunyah-v16-0-634904bf4ce9@quicinc.com> In-Reply-To: <20240109-gunyah-v16-0-634904bf4ce9@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.13-dev 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: WPtW2STFLzqb5RdGaoN_fa6pORGkIgFJ X-Proofpoint-GUID: WPtW2STFLzqb5RdGaoN_fa6pORGkIgFJ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-12-09_01,2023-12-07_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 adultscore=0 clxscore=1015 impostorscore=0 suspectscore=0 bulkscore=0 mlxlogscore=766 malwarescore=0 lowpriorityscore=0 priorityscore=1501 mlxscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2311290000 definitions=main-2401090158 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: B60FF80016 X-Stat-Signature: 1aydb685bq5ago9zmgm7y9xf4zdyfurz X-HE-Tag: 1704829095-665509 X-HE-Meta: U2FsdGVkX1+MlL7M/xxg+qTRfCgCkZ8MEtLf0ryTV2doeNjzHHJJeUblhfVga7x6yuLr4jPahQhosvUOxgzlHIKcgT5rbiXKRfEbXeMRbZyni79fUJizSVrGBv9g6iS4aGsUiVIw5sWZ9mLy7fDWAo8+ZYa/OVEiHZ/pAH4zP5Mp9sgY1vXj96sD/9wg+5yiZeSeMzmRxKZbKVI1feFksD4lkAjRGGu0lZxg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CPiNnGv33GDowXHhIT+Go06O/itTV/9Uwe1sPRnLd7bURWFCzQd8odRsodMp8dxWOJ6n+dfpvNkj X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 | 115 ++++++++++++++++++++++++++++++++++++++ drivers/virt/gunyah/gunyah_vcpu.c | 39 ++++++++++--- drivers/virt/gunyah/vm_mgr.c | 17 ++++++ drivers/virt/gunyah/vm_mgr.h | 3 + 4 files changed, 166 insertions(+), 8 deletions(-) diff --git a/drivers/virt/gunyah/guest_memfd.c b/drivers/virt/gunyah/guest_memfd.c index 5eeac6ac451e..4696ff4c7c22 100644 --- a/drivers/virt/gunyah/guest_memfd.c +++ b/drivers/virt/gunyah/guest_memfd.c @@ -843,3 +843,118 @@ 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; +} + +static bool folio_mmapped(struct folio *folio) +{ + struct address_space *mapping = folio->mapping; + struct vm_area_struct *vma; + bool ret = false; + + i_mmap_lock_read(mapping); + vma_interval_tree_foreach(vma, &mapping->i_mmap, folio_index(folio), + folio_index(folio) + folio_nr_pages(folio)) { + ret = true; + break; + } + i_mmap_unlock_read(mapping); + 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 (IS_ERR(folio)) { + ret = PTR_ERR(folio); + pr_err_ratelimited( + "Failed to obtain memory for guest addr %016llx: %d\n", + gpa, ret); + goto unlock; + } + + if (gunyah_guest_mem_is_lend(ghvm, b->flags) && + (folio_mapped(folio) || folio_mmapped(folio))) { + ret = -EPERM; + goto out; + } + + /** + * 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)); + + ret = gunyah_vm_provide_folio(ghvm, folio, gfn, + !gunyah_guest_mem_is_lend(ghvm, b->flags), + !!(b->flags & GUNYAH_MEM_ALLOW_WRITE)); + 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 b636b54dc9a1..f01e6d6163ba 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() */ @@ -127,6 +142,8 @@ 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, @@ -147,6 +164,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: + gunyah_gmem_demand_page(vcpu->ghvm, vcpu->mmio_addr, + vcpu->state == GUNYAH_VCPU_RUN_STATE_MMIO_WRITE); resume_data[1] = GUNYAH_ADDRSPACE_VMMIO_ACTION_RETRY; break; default: @@ -309,11 +328,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 4379b5ba151e..3b767eeeb7c2 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -471,6 +471,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; diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h index b2ab2f1bda3a..474ac866d237 100644 --- a/drivers/virt/gunyah/vm_mgr.h +++ b/drivers/virt/gunyah/vm_mgr.h @@ -137,4 +137,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