From patchwork Wed Jul 10 23:42:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729874 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 AC56BC3DA42 for ; Wed, 10 Jul 2024 23:44: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:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=6BWcXKAsCbgqy4w2nNCeUW6tFbXjuDYwJZkZfwSoSpo=; b=A2biPlDsPvnMXGDv7mKlUNqza+ UPldhxBSnV1Syc9Bc70GWqILHHR9w2M4ILBKvqZOI/ux2LUmslANzHcCUdLO4ier0TLHwOdXunZRS Vty6M31nig2t1TpjdCf9eHkl/XBawwQc/3r67ARqQrlYYs2cpCI2QraAaV0czaJjhj8xsBFtLS5gu WzJ0I5OeOXggUWBQvgm45f1kmMfs67LCf06i+iN2O23QRwpSrNLko3llms3BnU8DUDQnagj5QNnIn 2f7LG1PrOn9xAAQg3qtC3dyiH7/dD7Ed2jhWP8OrqLgeSYv5ErwhmxmobNuzhes1yUx6ezrdvzFbu 4HBf53DA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgyv-0000000C80t-0hNk; Wed, 10 Jul 2024 23:44:17 +0000 Received: from mail-ua1-x94a.google.com ([2607:f8b0:4864:20::94a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxP-0000000C79s-3OHW for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:45 +0000 Received: by mail-ua1-x94a.google.com with SMTP id a1e0cc1a2514c-80fe896d0e4so84797241.3 for ; Wed, 10 Jul 2024 16:42:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654962; x=1721259762; darn=lists.infradead.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=6BWcXKAsCbgqy4w2nNCeUW6tFbXjuDYwJZkZfwSoSpo=; b=CXvB2QlcbuKSWY9U4vccsKq/hZQ9y9TDTDVqE3hzD6eqLwH7jGmMSIVEZND9fym/IE tYLrjr+wjF5suJxAp8cXJfwmGwVaKYQMn5EnPWE8U57W3z1+ym50YI3GBygcd8jZhvNd WugF0WqQ4fois7sbY31wE/tT5Cp5oBWufYY9NEbq4dNDjKLevio6oqM6RmUHo/y0/wV7 nbEfD1cp05Y97qoug5jL7kngD6Pk2PTfR+FtQgJySGiU3NJpwbhyqIePm2Aehq+pYz0B HFhKe63ZZC/xZhx0NNu4fR+JvlyLxx8AL01ar1VIho2qptiFApV//1qpL+5l2eyHLNFT pXag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654962; x=1721259762; 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=6BWcXKAsCbgqy4w2nNCeUW6tFbXjuDYwJZkZfwSoSpo=; b=Hzk+/H64aOh8L5B4r3DV4tU+N4ZhS26VyqkuDo5CIkv2nxn3jD3lj76xfKzjhH7b2z TmCBeuUvka2Hz6XbsVWDIqqh5n1WhxC1VP2Q73Dj6H7G4LW35qno96LHHUqkrzkqcVr5 WXOy0WwZmNBx1YVsFKihnw2eDUaxQD7IiKrImQ6Sz4MV4jxpg4JnGvyE71XxULKb+afx 9TyZEOox5pQb4p8s7Moh7X43bBhy8tdKjBV1wGPgWk0u4zOwA8mkQp4eMzgCSSqo5Fsx sGSaT8WZZLcQn81teE87ldUthIuFPxDFUAAtUqvVmKIhC6eizxeXVXzQlg/C8/A5rBkT IVqQ== X-Forwarded-Encrypted: i=1; AJvYcCUPDzxvO/a4Ag+z6OLtxKkSeRRJxepROLhpLaH6vMw37q/k2H0MZAvmeHEsOOcYEzq7xvijIVNZ+5CX0IGEQgFcQhmFGAwj3zbFlhE65LPbDAbg48A= X-Gm-Message-State: AOJu0YxVHfpLTGjDXDbQm2Dc9lCwbEmztUDHGYbiioQcWMpLzYDbNC6M Vmra0/IXkRQ7y9+iUjoIpdlpYEy97lTjkGt4DveQRm0amzyjruKobu6f0j9hRphvoTfGTt38FPr lHRpHMJ4SxH+sVsh+OQ== X-Google-Smtp-Source: AGHT+IEs5vUQHQ0lGxrPeXnI8U3zg0scUB3VU7/qVip/3trOIT/81ZfcXpykTilnpI2xQFZASFVWimG+3mGsVHMv X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6130:2b07:b0:810:2714:60fd with SMTP id a1e0cc1a2514c-81076dc9388mr47870241.1.1720654962027; Wed, 10 Jul 2024 16:42:42 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:08 +0000 In-Reply-To: <20240710234222.2333120-1-jthoughton@google.com> Mime-Version: 1.0 References: <20240710234222.2333120-1-jthoughton@google.com> X-Mailer: git-send-email 2.45.2.993.g49e7a77208-goog Message-ID: <20240710234222.2333120-5-jthoughton@google.com> Subject: [RFC PATCH 04/18] KVM: Fail __gfn_to_hva_many for userfault gfns. From: James Houghton To: Paolo Bonzini Cc: Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Sean Christopherson , Shuah Khan , Peter Xu , Axel Rasmussen , David Matlack , James Houghton , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240710_164243_929920_1644AEE4 X-CRM114-Status: GOOD ( 11.42 ) 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 Add gfn_has_userfault() that (1) checks that KVM Userfault is enabled, and (2) that our particular gfn is a userfault gfn. Check gfn_has_userfault() as part of __gfn_to_hva_many to prevent gfn->hva translations for userfault gfns. Signed-off-by: James Houghton --- include/linux/kvm_host.h | 12 ++++++++++++ virt/kvm/kvm_main.c | 3 +++ 2 files changed, 15 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index c1eb59a3141b..4cca896fb44a 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -140,6 +140,7 @@ static inline bool is_noslot_pfn(kvm_pfn_t pfn) #define KVM_HVA_ERR_BAD (PAGE_OFFSET) #define KVM_HVA_ERR_RO_BAD (PAGE_OFFSET + PAGE_SIZE) +#define KVM_HVA_ERR_USERFAULT (PAGE_OFFSET + 2 * PAGE_SIZE) static inline bool kvm_is_error_hva(unsigned long addr) { @@ -2493,4 +2494,15 @@ static inline bool kvm_userfault_enabled(struct kvm *kvm) #endif } +static inline bool gfn_has_userfault(struct kvm *kvm, gfn_t gfn) +{ +#ifdef CONFIG_KVM_USERFAULT + return kvm_userfault_enabled(kvm) && + (kvm_get_memory_attributes(kvm, gfn) & + KVM_MEMORY_ATTRIBUTE_USERFAULT); +#else + return false; +#endif +} + #endif diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ffa452a13672..758deb90a050 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2686,6 +2686,9 @@ static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t if (memslot_is_readonly(slot) && write) return KVM_HVA_ERR_RO_BAD; + if (gfn_has_userfault(slot->kvm, gfn)) + return KVM_HVA_ERR_USERFAULT; + if (nr_pages) *nr_pages = slot->npages - (gfn - slot->base_gfn);