From nobody Fri Oct 2 12:20:43 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8226843802C for ; Fri, 31 Jul 2026 17:58:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785520722; cv=none; b=KCpbQzaxOt5l0aaDf9YnPUjJi/4xQtJeIVqKRRSsUN6/iF7RezC/I587Ze+wYgQdtu9ifcsrBzXttRr3Bv84zkwoyw/qBllBOfhNufp9mGsujGP14q1ynPBFK8j/5a5NG4F3vL9es92NrkAZJ6BGND2KYlTS5EI/PGLDtPr0ZNA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785520722; c=relaxed/simple; bh=pQ0teTd7XDWNbpvsOw580l+rfVUj4fBL0/VVxc3yCKs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ZuPQm/eNd+HPf/dQI9x8czvrJsAKW9IvP/dRmogBI/TidpRtXAGM2iMi7E8/qdn/F0oS0u3lCDRHl94IlmvPmoe0frmJpmZHspFv178RHaHCE6UqklvCo7gsOJJ1KinsD4qrwPPEq/hEuIh5bvTbfeK0FO60fPZOo8N1fWe3ssc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=c20kOij3; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=icj+N84N; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="c20kOij3"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="icj+N84N" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1785520720; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=2qKhJTG2JAuuEqxKzD1FHAMu/w1RQ6BoELHLT6tOYQg=; b=c20kOij3CtV9TgWZ2QIqaetC4oRR7gid6soIxujLfvrTOPcPmBk6NqNSnc3/pqC6s7xSqo CejnZQhUHKceur0K6QGQmJZ0rSyCIpTgqXUpgiE/+gZzSAt0GXFps/4zhrYpf0gs/sFNoT 92hQt3wNycyRcOA6bUZUIyMfxyuJ6ME= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-340-MozysUrePyuTdZJqbcd9qg-1; Fri, 31 Jul 2026 13:58:38 -0400 X-MC-Unique: MozysUrePyuTdZJqbcd9qg-1 X-Mimecast-MFC-AGG-ID: MozysUrePyuTdZJqbcd9qg_1785520717 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-c15d4224f9dso101405266b.3 for ; Fri, 31 Jul 2026 10:58:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1785520717; x=1786125517; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=2qKhJTG2JAuuEqxKzD1FHAMu/w1RQ6BoELHLT6tOYQg=; b=icj+N84N44o+lVhMiejxyugpInwSpI9bzOA9JrN2EiLJ29MI/5fXGjhhB19LnPAgCG oDgBIAydzmFGp1twxxZh3N2Rvv7ckBl6u5VNgZRj1Xxw1SA1vsODAQ6RDSMqlJKGwlxA jFSI4HPbDL+sq6/GKX051KBAYl1s4/ZXHoDgk/KsAUzRtfxpcDszqhrdrfrSUZK+Q/A2 ixdhNLCBgcqg55j6C0HYdoIlmMXgrToDc0whK81kylTKSjKFM/i7j2S+EUfGEcyREiUw 1YpRgxNfF9y2Rz4R7vf7EgSbekgRir4nNU0zguI7QwjrjcEXx8NEO7oK2pIpF7LTwQdE MgxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785520717; x=1786125517; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=2qKhJTG2JAuuEqxKzD1FHAMu/w1RQ6BoELHLT6tOYQg=; b=QtaWj32zneGtc6Vu2Ll40Ohg1i/Nqj3UFs0qu877AIBT+WfV/be0fMokiLGfYzyhz7 d6p292mrZZmWzpV+xKnXbT6bKOdIj7AkKze0GF5xV9uPkHpgCzo7tWNbAXZJUStTpc+8 81Z8FrOqh6GugvZMzt/qa1ZYxk5OZBRMTMH69k0CT6r+g/N40UvrPaY1/88hfRr116+M wf0pFsYrEywvpHDeC90XgGSU9aG9w1/oET1jImnXUjgK8DP6j+LqBP6jaAXm1psL+D1r FpxQzFoIrDoKgUXXXG2UOof8/ad5EVQidXhDiJKlIa/c02o3sVLG710phEHB5gR3YO4S ashA== X-Gm-Message-State: AOJu0Ywf7vpaVvqWZ+Gdx6xTxXnyQn6dLRzbqS/RYohu8+A/rVw/hpcn cif3hl5YsoYdJT3Hg9/8eNxyhtauMOrADHxpy5NnwH+DnkUIUGRZVC0Fh63jdEwA7cJXgZ0IPaM ZIFa/k1eV3/P9VhVF8EXw6kZJS+n1VyociIeizIOpxLa3Kq6x7WFsEmncNwxciGVLCZnhuM86o3 UaoJh+tCQ2kfAKdLLwe8gUo3agEhz5JJCiOdiEFbZe27a6Mme63Q== X-Gm-Gg: AR+sD10EHcej75++hlh7maTLimAFGd5q+MAwBa2+BI1lDa3DHfbwCQFE7Q9UVrJ9JI8 nxhyyJQ6Af/JJ/tfVMPSCKc4O5Pq3qqK4TOEC7x3Xst8QxOg7kdUcQfsdWvHJm+TSkZuZPdyQH+ OZXwlnoKPyNkke+PKoa9XZrHHy95vI6YKWz3JdgmSyA87sjSFxJAerf4Z5PINl83mTETXCV57wk caHQ9oTb8F0v/Fvp6tGmBy0qUKJkEgvQMLfDrva1qFyO8cqDgE5xmCIwz7In3ed9dR1xabURJAK rhdXqscX+DzlnHq828csdVk/QotCPAuTK+Z2ukiLdUPdNt0UIqlu3uPWN2CusSIiu0qlj1D55fH p3rpiotgoAKA/++oSYIQYR5AIZKHGidjKDHWgsif0b/e5Z1Jzf+YMtqvvSf5ttEovofIDkykjOH 5lnFg= X-Received: by 2002:a17:907:961a:b0:c16:8aed:cec3 with SMTP id a640c23a62f3a-c1fe8315915mr41041066b.58.1785520716782; Fri, 31 Jul 2026 10:58:36 -0700 (PDT) X-Received: by 2002:a17:907:961a:b0:c16:8aed:cec3 with SMTP id a640c23a62f3a-c1fe8315915mr41039966b.58.1785520716265; Fri, 31 Jul 2026 10:58:36 -0700 (PDT) Received: from [192.168.10.48] ([151.95.34.92]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c1fd4454321sm215703566b.31.2026.07.31.10.58.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jul 2026 10:58:35 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson Subject: [PATCH] kvm: apply VM_READ/VM_WRITE checks to all VMA types Date: Fri, 31 Jul 2026 19:58:34 +0200 Message-ID: <20260731175834.1121005-1-pbonzini@redhat.com> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The VM_READ and VM_WRITE flags are checked only at the very end of hva_to_pfn(). For both the hva_to_pfn_remapped() case and for regular mappings, this adds unnecessary cases and inconsistent error behavior. For hva_to_pfn_remapped(), the code is relying on fixup_user_fault() to detect this situation. This is fragile because hva_to_pfn_remapped() returns different error codes for a !VM_WRITE VMA depending on whether the PTE happens to be mapped: * if the PTE is present, follow_pfnmap_start() sets args.writable to false and KVM_PFN_ERR_RO_FAULT is returned; * if no PTE is present, fixup_user_fault(FAULT_FLAG_WRITE) returns -EFAULT after checking vma_permits_fault(), and hva_to_pfn() ends up returning KVM_PFN_ERR_FAULT. With this patch KVM_PFN_ERR_RO_FAULT is returned uniformly. Likewise, a PROT_NONE pfnmap VMA would be mapped into the guest if the PTE was pte_present()[1] when the guest attempted to read it; with the patch instead KVM uniformly returns KVM_PFN_ERR_FAULT. Doing the check early avoids these special cases and also sidesteps the issue pointed out at https://sashiko.dev/#/patchset/20260731160514.1101989-1-pbonzini%40redhat.c= om. For regular mappings a PROT_READ VMA, if placed in a writable memslot, would return KVM_PFN_ERR_FAULT instead of KVM_PFN_ERR_RO_FAULT when the guest writes to it. This would cause a -EFAULT exit to userspace, instead of triggering emulation as the VM_IO|VM_PFNMAP arm would do; however it should be considered part of the KVM API because mmu_stress_test relies on it. Still, even with this snag about the returned pfn error code, pull the vm_flags checks in front so that they are done for all VMAs and the above inconsistency goes away for the VM_IO|VM_PFNMAP case. [1] on x86, for example, such a page would have _PAGE_PRESENT clear but _PAGE_PROTNONE set Cc: Sean Christopherson Signed-off-by: Paolo Bonzini --- virt/kvm/kvm_main.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 45e784462ec6..eaf8ee0725d4 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2925,17 +2925,6 @@ static int hva_to_pfn_slow(struct kvm_follow_pfn *kf= p, kvm_pfn_t *pfn) return npages; } =20 -static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault) -{ - if (unlikely(!(vma->vm_flags & VM_READ))) - return false; - - if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE)))) - return false; - - return true; -} - static int hva_to_pfn_remapped(struct vm_area_struct *vma, struct kvm_follow_pfn *kfp, kvm_pfn_t *p_pfn) { @@ -3008,20 +2997,29 @@ kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp) retry: vma =3D vma_lookup(current->mm, kfp->hva); =20 - if (vma =3D=3D NULL) + /* + * GUP failed. It could be an inaccessible mapping, a pfnmap one, + * or the page might be absent. + */ + + if (vma =3D=3D NULL || unlikely(!(vma->vm_flags & VM_READ))) { pfn =3D KVM_PFN_ERR_FAULT; - else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) { + } else if ((kfp->flags & FOLL_WRITE) && unlikely(!(vma->vm_flags & VM_WRI= TE))) { + /* + * Exit to userspace for PROT_READ mappings in a writable + * memslot, as this is part of the API. + */ + pfn =3D vma->vm_flags & (VM_IO | VM_PFNMAP) + ? KVM_PFN_ERR_RO_FAULT : KVM_PFN_ERR_FAULT; + } else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) { r =3D hva_to_pfn_remapped(vma, kfp, &pfn); if (r =3D=3D -EAGAIN) goto retry; if (r < 0) pfn =3D KVM_PFN_ERR_FAULT; } else { - if ((kfp->flags & FOLL_NOWAIT) && - vma_is_valid(vma, kfp->flags & FOLL_WRITE)) - pfn =3D KVM_PFN_ERR_NEEDS_IO; - else - pfn =3D KVM_PFN_ERR_FAULT; + pfn =3D kfp->flags & FOLL_NOWAIT + ? KVM_PFN_ERR_NEEDS_IO : KVM_PFN_ERR_FAULT; } mmap_read_unlock(current->mm); return pfn; --=20 2.55.0