From patchwork Wed Jul 10 23:42:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729885 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 01C48C3DA41 for ; Wed, 10 Jul 2024 23:47: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=86wUnUhZHhvP2m3KtD2mzNoNsfKZM3y2BVe++0uTyFA=; b=k71sbKq5HB0biR5SpHsQ3PqNo3 6BJFAKqgknHLikVy9CD59NQ4wXSfQuSPmAlmIy55G+7GkGGlMdVjGN1G26qoR0qoDVg3NzQMIipEW 0SkNQoCcekEOpehhxaYrN0DoTtzgnomfWPaoBwceB6zjMRjTA/Lke6UB8kvHf+94dcyDSpKjnXE5S Cgcfs/SdvQt8fWPyCpPAHGaymZUkh84Tha0syj0krsYyMMbW0yZKgefTm/jbv6zZR5DWqB+EMNJI3 LAP5F5L1+JMDQArsa63J0KZIAtTfSSKKK4r/gr8ZhcAZILOWcwLjO9/lyYYqvBVdzHspCVZbUv4av B+vabVfg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh1q-0000000C9gR-2ivW; Wed, 10 Jul 2024 23:47:18 +0000 Received: from mail-ua1-x949.google.com ([2607:f8b0:4864:20::949]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxa-0000000C7Iy-1nMd for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:58 +0000 Received: by mail-ua1-x949.google.com with SMTP id a1e0cc1a2514c-8107b03e1ccso56383241.1 for ; Wed, 10 Jul 2024 16:42:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654973; x=1721259773; 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=86wUnUhZHhvP2m3KtD2mzNoNsfKZM3y2BVe++0uTyFA=; b=KlTvuru78aMLPgtK/IvhEKU2YY0cSv+8M/4zQwBbAUD6FLgpHcxG3zYLQ0pqtz+vIi jTOukui8IfTdvis2eqYCvizecfJqYt3CtaKS1b79HmhAKg1BcWsSoU/Y+bc2miMixAwJ hH66jh0PfxN+afEbBgh/fbmJyVrpOQwB+uwRC2boLl79cRMWd78b+dOvrnwA1+O20GhI NhlN4TfiDhYzu6P70I7XHx5MVfS+D+cBE+kZk6gxAXZ7PYuq4LRZbhKHtIc7YSlHbbc+ eARHjVqd/sG0MYp3noTa0uInoAbMGaods4W8/nCHMU+l7oT1zMlSwvVYWeSYM6klNYCL 7kKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654973; x=1721259773; 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=86wUnUhZHhvP2m3KtD2mzNoNsfKZM3y2BVe++0uTyFA=; b=p0iqtJwNdioItPF3ZcjSjDArjIe4YFUZMUicgCK00Q3h766KdZTxltxP4t6OGIT9YB 8ZyXNw2Tbc25IXXNwO1SsKYpeBPD1BQWnusLnHr3Y9z2boIe/IDK3GLlf2oGGSj6oaCK OOlQo2nmB+DEDPpQ7SR4KbXLarCto51ROIv0z+iDU4u6PMBAlVy6Lz4q0+hjmYvu5Uhh JP13m5kAtRHXsWR3fckCR2tm0qGKloy0ongmTFrfkXXeDKZ9cuJ4QsTIZqUNyLkIVZN9 ncsazyTqTxY+SPnPqdt2KAbOgzU078b1lP2a/snD3oKlpFVqpstpy09RVNNYPts7uZrm +9GA== X-Forwarded-Encrypted: i=1; AJvYcCUkCUsgaE2X7bFgRYCqKvDTUxHhXk1MNyx0imb9iw56bVtC8Vv01gd4xwsdOvHdQJbBqyq4SbsEfXJGy2DePQjaas50ufWQWQzygjvNCEuC4wbn4/s= X-Gm-Message-State: AOJu0YytVvEquLa50PFE144WK+lFAlBdiwfVMz8Wx7Ev0KorCDpg1DQH ZKU8QXIMsPJMkYgahugoxT5zPRr22d2FczE3BkXvbWQCzsOlVWdbHHZyCxUxjFGsf5Td785SiXe VOLPax3bvUl+Ke4wVwA== X-Google-Smtp-Source: AGHT+IFuuD8TX5cMTXqfbBITU1Rk5gbgp8/gjNqtfzjUDzjMtEhdHehLKT0HBMd1mO2mStlwTdwfc0NYjYZ51/Zs X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6130:2c07:b0:80f:e89a:dc21 with SMTP id a1e0cc1a2514c-81076f04ee0mr30519241.2.1720654972976; Wed, 10 Jul 2024 16:42:52 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:19 +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-16-jthoughton@google.com> Subject: [RFC PATCH 15/18] KVM: guest_memfd: Add KVM Userfault support 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_164254_710092_BB847FF6 X-CRM114-Status: GOOD ( 16.52 ) 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 We now have to pass our struct kvm into __kvm_gmem_get_pfn to know if a gfn is userfault-enabled or not. For faults on userfault-enabled gfns, indicate this to the caller by setting *pfn to KVM_PFN_ERR_USERFAULT. Architectures may use this to know to return a userfault to userspace, though they should be careful to set a value for *pfn before calling (e.g. KVM_PFN_ERR_FAULT). While we're at it, set *pfn to KVM_PFN_ERR_HWPOISON for accesses to poisoned gfns. Signed-off-by: James Houghton --- virt/kvm/guest_memfd.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 9148b9679bb1..ba7a981e3396 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -542,8 +542,9 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) fput(file); } -static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, - gfn_t gfn, kvm_pfn_t *pfn, int *max_order, bool prepare) +static int __kvm_gmem_get_pfn(struct kvm *kvm, struct file *file, + struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, + int *max_order, bool prepare) { pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; struct kvm_gmem *gmem = file->private_data; @@ -551,6 +552,11 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, struct page *page; int r; + if (gfn_has_userfault(kvm, gfn)) { + *pfn = KVM_PFN_ERR_USERFAULT; + return -EFAULT; + } + if (file != slot->gmem.file) { WARN_ON_ONCE(slot->gmem.file); return -EFAULT; @@ -567,6 +573,7 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, return PTR_ERR(folio); if (folio_test_hwpoison(folio)) { + *pfn = KVM_PFN_ERR_HWPOISON; folio_unlock(folio); folio_put(folio); return -EHWPOISON; @@ -594,7 +601,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, if (!file) return -EFAULT; - r = __kvm_gmem_get_pfn(file, slot, gfn, pfn, max_order, true); + r = __kvm_gmem_get_pfn(kvm, file, slot, gfn, pfn, max_order, true); fput(file); return r; } @@ -634,7 +641,8 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long break; } - ret = __kvm_gmem_get_pfn(file, slot, gfn, &pfn, &max_order, false); + ret = __kvm_gmem_get_pfn(kvm, file, slot, gfn, &pfn, + &max_order, false); if (ret) break;