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: 13568301 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 1326AC54798 for ; Thu, 22 Feb 2024 23:23:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 95DC26B00BF; Thu, 22 Feb 2024 18:23:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 90C986B00CA; Thu, 22 Feb 2024 18:23:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 75EB76B00CB; Thu, 22 Feb 2024 18:23:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 63A566B00BF for ; Thu, 22 Feb 2024 18:23:24 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 343B4A1786 for ; Thu, 22 Feb 2024 23:23:24 +0000 (UTC) X-FDA: 81821018328.14.5A892D0 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by imf09.hostedemail.com (Postfix) with ESMTP id 0CCB414002E for ; Thu, 22 Feb 2024 23:23:21 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=A7W7wqej; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf09.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.168.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=1708644202; 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=vo+GeAkZrIZtCLsV/xLZNLxXgjO/d+8dOj+GwHIQAtA=; b=wL+gUQ9R0OOtEe/22DGGspP9skbdbfaroUKXkWyqrrgR5MaWGpiCjMD+f7+lBLHj8MjWXZ fqVrQVQ4VfO1HW97IOSk7TJWnyU7c0+c9dpVNh3W0fOofpsjelvsXrOW3/DVaz95khU4cV 5Yl4HTbo+tNaQ/1Zbw14TJb1TBEg1Y8= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=A7W7wqej; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf09.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.168.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708644202; a=rsa-sha256; cv=none; b=k6NN4Xd+UpGFskC2g8CJYAxi7IGg+fzkDtX7qZXoBYRIPWVYi8ZwLspyom6KIS3zEb9aLe NF8yLfFet8tHINhedcvXhhhO6FWTs0vJ9hthhMwq9TMOWHrCD+uPDy09RLKlVGIssoUQrJ WuLGBw4KbWB/D5EC7l6azDAyg1hqrdw= 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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 0CCB414002E X-Stat-Signature: jwg4ffz9wj17e1dweg1f8wmstum5cats X-HE-Tag: 1708644201-641231 X-HE-Meta: U2FsdGVkX19Y1+SabYGQFvZ5qph19yr1TCkXybexiSrflzn8lwD3QSctj5landzmSVxyO8wamug1y72vNiI9WeX9v4yPx+mQlfKxbYQcR2W3S05IyFa5kPXNJFPz789AjNLoFe6Uh8pxhpXkh2whS63o2CU+lBI5/wlc/jcFY4XaFpJApDZjXaLSn5l9PNqz/2yNnXXoFCHTLuL4Z97PwXFaGOBweLkWS0C4nOwHCHiAb8F62yzzhZQfOuS0yESeqnCvDTTJ/8C4Pvz3h64Smlq93hpU1w/P/AOtTwooeFIgcqnOCjDNUnBWnNZa8Z4INdl6ljIh7MaqJHrbRTfHRGGvjSAKbH0fQk6UWyRswNEwwTBVsg3w/1rPoQFmhctK++wTi533bx4wdDgu9OsBJLM5FoXdh6XCf09nbXUpBneY2D+1RXd4xbJZPc1twIwSXxtJaHwQBPKPPTUxd3IpsB5zWAxNPjeJALiIeYlsg1H1ptBswib7K/4Td0CBF2B2597nuoFQMF5vOINJbG2vjY79uXi/FFa7VVcUfQvW215/MkAWgt31hGpzpPlfxvk07sD5DjBS3sSzMdCzW0twLao4UgdPC6hvXqs/RiTJ1CFbUA4YdBEyipCf/EmxNFMRNJk2O2WHYB53+wjeaEgj97b0HeTRPmIU3fsuWzrfB/5XxxzmxAsz8KP2fmcvLhYZES324y+irwsdZgfA5IsvCmyhotnO2Nl0wHQqqpyDkNQFWVUL/MtK8lAxcXkpGFBkLl8EFjykVBQVTvnGTBerb9byoYZlAYqaSWh00Xd50s6AbkruvOB6JGcBxM2EnApS+mhwBsNRtO/esGRn5C0q0lrbnwcMPVpt8lTIgqmGHpwHCY0esh0EPovTzY3VuBDm/aQwMesRrXdGlBCIx3oOdKcJUPaDoZ/cBFjLLIbffNEAo8RwrTicjipBKz994lvbTETfVv+nJkU= 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 | 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