From patchwork Fri Aug 9 20:51:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anish Moorthy X-Patchwork-Id: 13759264 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2556B166315 for ; Fri, 9 Aug 2024 20:52:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723236727; cv=none; b=fEXx7XwWdsZ81nzcgN0yHEaR3G/EWxVhmuzY7n4nh+ht8Qn63zusIaaSSJMblGbM2DlF+6fBwdF82zRwgGPia17de6bKHZC80kfuj0n+mYMPgNHTLGZuuSYO1T1/pxjs9hJGA0OdtRYSJKanGw6QWRbnf3/T3MakKy10kPOnYsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723236727; c=relaxed/simple; bh=F/i+lgt9dfMODC8lpmtv7OIwLyDsx7Z0jTeBvQyEnTQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oeIAj6kMt4YqlmT1V8ebZlYlL49gCxFvfGgdfT4ikyfh2zgveFY8VurJ5MPbblG5Pt8FkVUI/rqrcEExtc4xqaEc5P8y4e8noH1rkFCQbMLQs/tncCIYDom1+17P1rRuK3uTHk7V/2XK8pXZzsIlS3Pn7aS78XCsDNxyQBdeBSY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--amoorthy.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jub9747I; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--amoorthy.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jub9747I" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6902dc6d3ffso61135827b3.2 for ; Fri, 09 Aug 2024 13:52:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723236725; x=1723841525; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=54OZeUBWyPXA0S87QTioBQQ7Q2CkC6lMvkpXhup6C7s=; b=jub9747IQgCw0kZHtpkL8hMF+7U/w23VBZBH474tfcxbiMyRawADGZwfe9t3O82zjz A1Ah2K8waVHedjgDAowst9Ro9d3dUa89KITohY6ZcSAPYqsDgUC8i/ywaD2TlHknwu7N HSD/iK/86QCxmezTxE0ds+CokjU4JBlubIoPsyY2Ux7yHpGdYBlMhc1rQ7r+HPeNPw5V yu2uj+gMD070OpZjALXZ9JzugCPFpQMLQhoGMQEOoN+eGquz1xm6Zu73Jb2iTKnCbSgP +M28q7Mwh2NjmNGHhf2a2m/nvWTYyfT8jhPusPXq8LqZc3ENg2I8q7aK1tJZiLhSf1SV a3aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723236725; x=1723841525; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=54OZeUBWyPXA0S87QTioBQQ7Q2CkC6lMvkpXhup6C7s=; b=kgIU7ckhE5wTtkhXS4oAqPHv2rPzjN0sBiV15CDFmmYb7EFHLcGT6luPkMKnp7baVR eOh4qFcsDMD89AH8O5v2Pb2fmxp5Mb1scgPdrZZYkCZ/5L+/cR/0KtAXpnExkef0ll5r zETz0ySVQkP/M/tXRCHJ/4GGZrQt341V58Hw5tRv6wkceTtJJaULWOl4nwm9AyvZYVDV TTq4vLB82W4kmOcYr09VjTSqa2Faoi0kSvyTNXc2f6gLx9H3tOeRqhwpVxbbK7QljHg/ m79M51DIfGBVExkxGSGmRuzRECS3vWZX/Q1bDmTh53z2YYr5gV0IDrXjspMLF9YOT8LR qX3w== X-Forwarded-Encrypted: i=1; AJvYcCVzByb0y7VxP5b3HuvYOwOvySq/9O/91hgLyHy0yUJgSVVQStVlgWJniUsPI2u/LTbSkNTYWETjsBIbjMG9Isl02Dg5 X-Gm-Message-State: AOJu0YxHnc6S4MbsAWdlMMB75LioYSg7jo7GPgWzEPAFqr6iZk7+my1F hrXJm57IIjIN9KEqaWXwQx6fHV8aw3aVLW3cAgGj11O3hOm4qpI1T2ntEHddADhIwtb7PZpemIQ WMNgSRpO0kQ== X-Google-Smtp-Source: AGHT+IEd1rLj8FyLEliwY0HHp1LBZ6RRlFIxvN6TqVFlyWLq4RucNIuVDKVa8kZXEO3o2onu1BiztI0aOW/YaQ== X-Received: from laogai.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2c9]) (user=amoorthy job=sendgmr) by 2002:a5b:d44:0:b0:e0e:426b:bf6f with SMTP id 3f1490d57ef6-e0eb99f2ea1mr5705276.7.1723236725130; Fri, 09 Aug 2024 13:52:05 -0700 (PDT) Date: Fri, 9 Aug 2024 20:51:56 +0000 In-Reply-To: <20240809205158.1340255-1-amoorthy@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240809205158.1340255-1-amoorthy@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240809205158.1340255-2-amoorthy@google.com> Subject: [PATCH v2 1/3] KVM: Documentation: Clarify docs for KVM_CAP_MEMORY_FAULT_INFO From: Anish Moorthy To: seanjc@google.com, oliver.upton@linux.dev, kvm@vger.kernel.org, kvmarm@lists.linux.dev Cc: jthoughton@google.com, amoorthy@google.com, rananta@google.com The initial paragraph of the documentation here makes it sound like a KVM_EXIT_MEMORY_FAULT will always accompany an EFAULT from KVM_RUN, but that's not a guarantee. Also, define zero to be a special value for the "size" field. This allows memory faults exits to be set up in spots where KVM_RUN must EFAULT, but is not able to supply an accurate size. Signed-off-by: Anish Moorthy --- Documentation/virt/kvm/api.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 8e5dad80b337..c5ce7944005c 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7073,7 +7073,8 @@ spec refer, https://github.com/riscv/riscv-sbi-doc. KVM_EXIT_MEMORY_FAULT indicates the vCPU has encountered a memory fault that could not be resolved by KVM. The 'gpa' and 'size' (in bytes) describe the -guest physical address range [gpa, gpa + size) of the fault. The 'flags' field +guest physical address range [gpa, gpa + size) of the fault: when zero, it +indicates that the size of the fault could not be determined. The 'flags' field describes properties of the faulting access that are likely pertinent: - KVM_MEMORY_EXIT_FLAG_PRIVATE - When set, indicates the memory fault occurred @@ -8131,7 +8132,7 @@ unavailable to host or other VMs. :Architectures: x86 :Returns: Informational only, -EINVAL on direct KVM_ENABLE_CAP. -The presence of this capability indicates that KVM_RUN will fill +The presence of this capability indicates that KVM_RUN *may* fill kvm_run.memory_fault if KVM cannot resolve a guest page fault VM-Exit, e.g. if there is a valid memslot but no backing VMA for the corresponding host virtual address. From patchwork Fri Aug 9 20:51:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anish Moorthy X-Patchwork-Id: 13759265 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4E7191684AB for ; Fri, 9 Aug 2024 20:52:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723236728; cv=none; b=MyTOgd5e7DN9pM3m0TfePSIx3EIMeYmSBi25/t49Y6XEV02cgtnyHcSA10SMLpmuyFa2TFcGR8vO7TXy51kRa4/Ka+TU6d83+J4jPtlPLV9i2NmKuQtPhckSs9QcwpRmnGzGO6BjJfSEAmDiuNHbkzyJIBQeZ4OnyZPFl1Zw8II= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723236728; c=relaxed/simple; bh=UylfwD3oilt2Ap00B9QfJgsS5JbmMx/LZPo5qb2iVKI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HJ5vXLO1A5SEx/djcnqxNaUXtVp7YsB+aPh6hlJLiPziEjNItsKuiu22tE5jFC4v2g2PivaPAprfvx7sez+f4nRdjJcgZDItaF3i7mi+deWcVbQWsgOQDaTOy9bcIyCM5QDgW99xgABENnBevVHv0iS0QnJ4j3UGosoMUfCcfZ8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--amoorthy.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vX5O7m+d; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--amoorthy.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vX5O7m+d" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e08bc29c584so4111789276.0 for ; Fri, 09 Aug 2024 13:52:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723236726; x=1723841526; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=br3FwjgDzPooOiG0lkeIxLVZHvjVaW5LdrLy5p4r3+8=; b=vX5O7m+dtJOzrxJTTTYn1W3D9d0czGw2tTlKPe9u+q4z3hqf8VcWOAurd3/SvztwxM 9NpuMGnkDQMLk4Tz2/BatT3e4KOLEtEPzpW6VHfIJKBGq9+WHmfqHxVeGFGgi7ehY00w GB0OoqU/FlpYbU4x7I/+OtSbx0ft4EXNOCN463DWmvmtUYqL7UQ58TMWd9Fo1NLmzNEM mOthIFDUrT4TOLvsbnCvcshN6EAeNJ2FEhX9VIBY2p1bDznwAhKl5tpl1Li6JhETHsLR yQnvENeZtBRH9/yvx4mRMElwbXyP0pItdDcMzqoCuo0lQAQSU98v+DgOjEfBLB4elPUq 6Low== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723236726; x=1723841526; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=br3FwjgDzPooOiG0lkeIxLVZHvjVaW5LdrLy5p4r3+8=; b=d+MYgiWY9lBkVDtoDJYxweF1V5onGcGhkNCn1mUFziNgeTy/K8TEazZP7/zNK3z1C5 Ul921omaDVhevlB+0q2KAv306lZVsjvtttsnlu2H8k59osDCOA4Kb0MuCFSgh27mxX0P MyaOk3zicchkAnLF+OZys5izvu8sGbwADGXNSsdyCtS6FLtLJOI/nRxYSq43dDrYTuFG jKmkejRkj0wtatnudjTmmtSdCrVQ5pZHzfwEM0S2Szz3xn5wxYncl8D65nMziUuaX0hO ZD0ZN+Rpoetlw083BV5EEe5ot9tQyUUSEg+yRyIhH9M3AXw/ltebeGbu1FTP359LRuo+ tQYQ== X-Forwarded-Encrypted: i=1; AJvYcCVJEeMy2x8GgmhF3f/mNM1pCMLRz7UYWy5qqG4zXoszXPSudjxivjsu1C7VnShxSTXcI+GS1WJ1Yp4MYTyKHAqNO3yF X-Gm-Message-State: AOJu0YwN/Gni8iy0HDaERxbyulC5oCm528ke20E3JA7ch5FFcd4AbKlh Bzb7G7kOGT5sSc5H6jc1Tnv0LmiWh+4cJaJ8RFKqrTvRcuCbk+1IH4MoP61lHngCzOxi+TzxAGm DKKzlQnDIvg== X-Google-Smtp-Source: AGHT+IEDwdW8UPrl6O8L/dLUe+yB4KqU+io0AFuuQD9m2vE0SJljtoLO2dozJZ1XveNUHCZDlwUWfzVuIHv+5w== X-Received: from laogai.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2c9]) (user=amoorthy job=sendgmr) by 2002:a25:ac5d:0:b0:e03:3cfa:1aa7 with SMTP id 3f1490d57ef6-e0eb98c93e5mr4591276.1.1723236726226; Fri, 09 Aug 2024 13:52:06 -0700 (PDT) Date: Fri, 9 Aug 2024 20:51:57 +0000 In-Reply-To: <20240809205158.1340255-1-amoorthy@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240809205158.1340255-1-amoorthy@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240809205158.1340255-3-amoorthy@google.com> Subject: [PATCH v2 2/3] KVM: x86: Do a KVM_MEMORY_FAULT EXIT when stage-2 fault handler EFAULTs From: Anish Moorthy To: seanjc@google.com, oliver.upton@linux.dev, kvm@vger.kernel.org, kvmarm@lists.linux.dev Cc: jthoughton@google.com, amoorthy@google.com, rananta@google.com Right now userspace just gets a bare EFAULT when the stage-2 fault handler fails to fault in the relevant page. Set up a memory fault exit when this happens, which at the very least eases debugging and might also let userspace decide on/take some specific action other than crashing the VM. Signed-off-by: Anish Moorthy --- arch/x86/kvm/mmu/mmu.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 901be9e420a4..c22c807696ae 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3264,6 +3264,7 @@ static int kvm_handle_error_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fa return RET_PF_RETRY; } + kvm_mmu_prepare_memory_fault_exit(vcpu, fault); return -EFAULT; } From patchwork Fri Aug 9 20:51:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anish Moorthy X-Patchwork-Id: 13759266 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35A6416A923 for ; Fri, 9 Aug 2024 20:52:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723236729; cv=none; b=YJ6s3VFawZ42ofYpe0L2u33egP5X+hPNvSywD+lynyEiUxpmjs5B+WF8vIwkHjECg7Nr7JRD7GFx3Cmf209F0lDfy8+gTj4IgjMq+zYOwUlZipZxRn04Qjs9K+bFZc4T3hYL/j/TzJKfdxx/q8Qf+SCKeOQu6opZ3EXfNC78pNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723236729; c=relaxed/simple; bh=aRlZUJEaiHRJGRJLYanOLgphxj1dBoKyLzhPuRD+1jE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NumJ9WGLU/FsonyUqjy2/i/NQ3yfBxMRRohkPByDxms2GQ0u8iy19Z8aMw13jm6Oa1P8XM6/nImHUNvR3KBkmRogfcTpB3e2tXY9pUTRCTgydN0BvITOc38uFbfrMhSA0I7nGKlmTzAoAHGznFu8W4FiUX9FEbXwgQ0N24S8rNU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--amoorthy.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fNsD4QNE; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--amoorthy.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fNsD4QNE" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-665a6dd38c8so57710297b3.1 for ; Fri, 09 Aug 2024 13:52:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723236727; x=1723841527; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dIbtHxMOA9YLjvqp+dS6Qdcb7Qd+13Wo6Sg7tuMzI5Y=; b=fNsD4QNE31StJ1qFHKH8vfbE9M+OprI5rN4w/IcQvUHTZtpu/n3O8JFOcDxHmkTmHM nCEavGEOsyXRuYkaEixAv6AdSU1f8zCZMgoBxBtN+Ls/AgN8gbArbRqR5/k3hMVFNRCY 4Cp4JWhPCpQfhxMQ0oFGox86kscOEJUdPVFNmOt0u0PJwch9+CuLH4qUrHcfWaGbCI/d GG42RuFdkzuad7ThfRSL2roOrucg2gowTw0xUOe0jjPQn72ZP4OOpisWZT20rt4LSI4m m+nWuzFWED0ElEg4cAI/PbxN9WxK8+NLPw4+qOwJeE3c1/6k4HLYHEOwuL9Fih2uNs/n KMkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723236727; x=1723841527; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dIbtHxMOA9YLjvqp+dS6Qdcb7Qd+13Wo6Sg7tuMzI5Y=; b=U8RvjYKj/z3Hb4wQ4kSWmgFGrvNaCmONrH+/OHs7gaKc4MyTyvDWoetBryRiJpLb0L K3+Rmun3m150TzDbyF4tsc9GwXNzqjg6FbrB4+0QQ3VyJQKIhmpAJm5PEhpxVY+84HUt +BRdMAii1UoxP6frfEXewt3A7ThvSk2vDuEwRRThvSQFkXDaWAG3yiF5ILsC6zW7SPxx B0b+iEzabPTfV5qsfg8BJhSFbRggVWhm53TV2heKKIcnN2Ni+d9JJ/TBGD5CN59U+hU5 ldOAolwxqU9e5UKMn/YylUAucFhwfxGbkqX5VCmxwwbeCn3GoAQUC3sYYmOiA8T7M7Tf DA6A== X-Forwarded-Encrypted: i=1; AJvYcCUaCj38barWn7YTBkAYYmOhlGbd1lv+A8NalykMqpOtZs9hEmZqjtLB/6QSkJRsCP3JD3quC6eEzEU1xzyu9vbPnYjD X-Gm-Message-State: AOJu0YzjuK8wJirSOTe4F1rqFY2x973XyDEKrPo0ZM3pIbcFCYrKzBXh U1I8SiiGHAo9Ov10dZH62Wcq2I0yM8pKbLJ86PLO1AolcyGiQ6fei5a0zYnFHiTl9IsE3vKTJUY umh41gcQOlg== X-Google-Smtp-Source: AGHT+IFEaybp/gQAI2dPTCR/v0E5CX7dxbSJimk27amgMiDWfuzdCjImbiciR7wYsAeR11wIqKwlf/27qs26HA== X-Received: from laogai.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2c9]) (user=amoorthy job=sendgmr) by 2002:a81:8786:0:b0:68d:14aa:894a with SMTP id 00721157ae682-69ec4a193b8mr1144237b3.2.1723236727230; Fri, 09 Aug 2024 13:52:07 -0700 (PDT) Date: Fri, 9 Aug 2024 20:51:58 +0000 In-Reply-To: <20240809205158.1340255-1-amoorthy@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240809205158.1340255-1-amoorthy@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240809205158.1340255-4-amoorthy@google.com> Subject: [PATCH v2 3/3] KVM: arm64: Perform memory fault exits when stage-2 handler EFAULTs From: Anish Moorthy To: seanjc@google.com, oliver.upton@linux.dev, kvm@vger.kernel.org, kvmarm@lists.linux.dev Cc: jthoughton@google.com, amoorthy@google.com, rananta@google.com Right now userspace just gets a bare EFAULT when the stage-2 fault handler fails to fault in the relevant page. Set up a KVM_EXIT_MEMORY_FAULT whenever this happens, which at the very least eases debugging and might also let userspace decide on/take some specific action other than crashing the VM. In some cases, user_mem_abort() EFAULTs before the size of the fault is calculated: return 0 in these cases to indicate that the fault is of unknown size. Signed-off-by: Anish Moorthy --- Documentation/virt/kvm/api.rst | 2 +- arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/mmu.c | 11 ++++++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index c5ce7944005c..7b321fefcb3e 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -8129,7 +8129,7 @@ unavailable to host or other VMs. 7.34 KVM_CAP_MEMORY_FAULT_INFO ------------------------------ -:Architectures: x86 +:Architectures: arm64, x86 :Returns: Informational only, -EINVAL on direct KVM_ENABLE_CAP. The presence of this capability indicates that KVM_RUN *may* fill diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index a7ca776b51ec..4121b5a43b9c 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -335,6 +335,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_ARM_SYSTEM_SUSPEND: case KVM_CAP_IRQFD_RESAMPLE: case KVM_CAP_COUNTER_OFFSET: + case KVM_CAP_MEMORY_FAULT_INFO: r = 1; break; case KVM_CAP_SET_GUEST_DEBUG2: diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 6981b1bc0946..c97199d1feac 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1448,6 +1448,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, if (fault_is_perm && !write_fault && !exec_fault) { kvm_err("Unexpected L2 read permission error\n"); + kvm_prepare_memory_fault_exit(vcpu, fault_ipa, 0, + write_fault, exec_fault, false); return -EFAULT; } @@ -1473,6 +1475,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, if (unlikely(!vma)) { kvm_err("Failed to find VMA for hva 0x%lx\n", hva); mmap_read_unlock(current->mm); + kvm_prepare_memory_fault_exit(vcpu, fault_ipa, 0, + write_fault, exec_fault, false); return -EFAULT; } @@ -1568,8 +1572,11 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, kvm_send_hwpoison_signal(hva, vma_shift); return 0; } - if (is_error_noslot_pfn(pfn)) + if (is_error_noslot_pfn(pfn)) { + kvm_prepare_memory_fault_exit(vcpu, fault_ipa, vma_pagesize, + write_fault, exec_fault, false); return -EFAULT; + } if (kvm_is_device_pfn(pfn)) { /* @@ -1643,6 +1650,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, if (mte_allowed) { sanitise_mte_tags(kvm, pfn, vma_pagesize); } else { + kvm_prepare_memory_fault_exit(vcpu, fault_ipa, vma_pagesize, + write_fault, exec_fault, false); ret = -EFAULT; goto out_unlock; }