From patchwork Thu Oct 10 08:59:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829774 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 91C41CF07D9 for ; Thu, 10 Oct 2024 08:59:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 27EFA6B0092; Thu, 10 Oct 2024 04:59:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 231146B0093; Thu, 10 Oct 2024 04:59:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 081D26B0095; Thu, 10 Oct 2024 04:59:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id DD0A76B0092 for ; Thu, 10 Oct 2024 04:59:44 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 82F95141BEC for ; Thu, 10 Oct 2024 08:59:41 +0000 (UTC) X-FDA: 82657094688.13.511F67B Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf21.hostedemail.com (Postfix) with ESMTP id 9EE3D1C0011 for ; Thu, 10 Oct 2024 08:59:40 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=35EOJcuG; spf=pass (imf21.hostedemail.com: domain of 3fJcHZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3fJcHZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550712; a=rsa-sha256; cv=none; b=Z+C+TKIknV2BkBZuV9kgLl16+9xeepz9eqjG0F5AnBf5SSZ42T5w2HcfotVy/lFzx8q2mN bOj2mKJgDBRfUImTOXb0ukI9ZKIGtb3bhIYVy1gx7VtJhkf7/St6D9vcTu8ToRsLii9g5m qmDvhLJuxEjdkg1Zzg3YH9c68a7AtEw= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=35EOJcuG; spf=pass (imf21.hostedemail.com: domain of 3fJcHZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3fJcHZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550712; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zi/pjPr4ta3drldcSk6p6o77eOYFs/2AcZb+H+YLVPU=; b=7bDGT7GmTC9xMCy+Mw9iaR5soMEMfm7UP+1YPzv1mWh8j0DaUHNdyBchAXLBIQYKuezsK8 cLqoeRLX3BM/16QY4jzvpRkeIJTaPMcYyw0PhOIXcUR5mSKkVAHhPY+B6rlV/lBSiXmIou xajyCU4pp+OlRtAUhm8fUGxw+XDw9D4= Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-a996c29edaaso50268266b.0 for ; Thu, 10 Oct 2024 01:59:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550781; x=1729155581; darn=kvack.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=zi/pjPr4ta3drldcSk6p6o77eOYFs/2AcZb+H+YLVPU=; b=35EOJcuGccCKSy/FD/LbmqKNWAbqM7V0Df1vhK1iUNTwalOy/tB26olOqZLStf5jwj 6+OKiMDDbpwE5RMSoqlFvIOKdKYnAY7B0CRQrBvP4sLyxpGGucpmvCOHkUIDlycgFSf0 UXi1e7Y/XES/IskYeLLA8XbHBb2Y+/nNrltnhtO+g0WSU8J+yDzACGkTyHeBqksNI9Fu JA4lJ0L8HgQQ6Pj/9T7VxmEYLD+Is5zdAvDtnh7ffLXGmFSpPiuwcPL8WZBAxUtU0sFV VMp4AXHGi69KwmdG7iFDpIO18/YjqKbGc1XpOGjH5lnRgbtM0JW3o7xFNl3fLD0hjtWJ CG8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550781; x=1729155581; 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=zi/pjPr4ta3drldcSk6p6o77eOYFs/2AcZb+H+YLVPU=; b=Ll0KE7pai36dvo9NfYkPsif7V+TRHTuqbF0/BGmCFawPk4Zt/TIiMQEmj7xByYpc4Z Xzgch4dIW7HBAgmxHM2g8AB0IKiZUldj80qL0mycetkX+T+bH5D1Yb1do70qrPeHlOs7 GbzkkYeorMjzr81F7w53yJy+2WJVWd39msmRtWPJAzBvJb4ruEAr7JD5i09fxBYiCU2m k4m8talbODXDa9zrUHyQVT5LnBk24z+bflZyG2d/sVldsKPG769s3HKlvHWolIuSOgYu RV41UQ6fDf+Y2K396rrSHB6iJA1T5QQ+VhHpV4t22Bmv+bjhdGZIyxQlNgOyHzPytgYs LEhQ== X-Forwarded-Encrypted: i=1; AJvYcCVPly8+sUvUU/fdZxWMKWFB+JAugDSvDxj1MXnBWV64Ll5bd2t8Ad8nsI+mXMzzlhB5i6yxxU6Mcg==@kvack.org X-Gm-Message-State: AOJu0YykgjliGL3dc+0C0DjaBnQOJTlPFerN4Ge/RjURZm08KUNLloZe HkCVJJMUCA4NT3sRbbb9iOuHenbTvb9Tat0r8iIOS6q+r7ntq1r4TgCb/Th3oKgEL+pRFMk8Tw= = X-Google-Smtp-Source: AGHT+IFJwsaDfsra9si5hSwL5TQTTo4RhmMq+iKHIyBjxFdz3SDsa44kizohADHJBxPg0EWk39rrAlUrlw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a17:906:e207:b0:a89:ee2b:e2dd with SMTP id a640c23a62f3a-a998d31c253mr128966b.12.1728550780668; Thu, 10 Oct 2024 01:59:40 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:22 +0100 In-Reply-To: <20241010085930.1546800-1-tabba@google.com> Mime-Version: 1.0 References: <20241010085930.1546800-1-tabba@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241010085930.1546800-4-tabba@google.com> Subject: [PATCH v3 03/11] KVM: guest_memfd: Introduce kvm_gmem_get_pfn_locked(), which retains the folio lock From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Stat-Signature: h5oojk9s17d41684newtdyj1aepezh5u X-Rspamd-Queue-Id: 9EE3D1C0011 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1728550780-628750 X-HE-Meta: U2FsdGVkX18DDHprxg6zVtIWccw8d51bGry0V8QudzHmVr35gxtqocqsI0oJ+zbwVLNy5nWcfmyAjljI7sQPdPE1Vt3MdV3qgJ2tYRoeQpe6TSOSzXBEnPCQfkK7hbPaWvgE846d4QEiRYSQP5oDe/JdqUb619sWAX7ZSff9a302VHP+osl4fLdaWFqzkY8GTlFzkfqWaAIDPnt/pFtCqhXWTB6jwoNqU7goqDciB2oxJMZ0sFe19N8+M4EF5jNradIUoAUxtTndhYEQBuaoBEIBoEySssEPjltkR/96tIqCs3tSxiES4GtCxtrdms/wDeh4PF2o4GdyOUk0BxLs5BR47sFJ/FWSJ/u7oGmRMOLWAfmwMfLYBJSY8tnncwhiBRy+zydZuGf+kaB5MujTIpaPkvYVHaINf90VvfBApGvNc+PI43yoZt1aSOxiNLakq8NCQvGQLiRXm+366usqSFi815aJOTd7DDbJj3zyU8bITGcXo3WfKa+SRI8P521P/OEyb8h5A+ZT7qzcc1d79R4kytX1sU0I4VIWsYMUMRpuOqoow4/Ha+HMWcUG4BvUl1vWiWnSxUwEZ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ests=bogofilter, spamicity=0.000020, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Create a new variant of kvm_gmem_get_pfn(), which retains the folio lock if it returns successfully. This is needed in subsequent patches in order to protect against races when checking whether a folio can be mapped by the host. Signed-off-by: Fuad Tabba --- include/linux/kvm_host.h | 11 ++++++++++ virt/kvm/guest_memfd.c | 45 +++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index db567d26f7b9..acf85995b582 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2464,6 +2464,9 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) #ifdef CONFIG_KVM_PRIVATE_MEM int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, int *max_order); +int kvm_gmem_get_pfn_locked(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order); + #else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, @@ -2472,6 +2475,14 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm, KVM_BUG_ON(1, kvm); return -EIO; } +static inline int kvm_gmem_get_pfn_locked(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, + kvm_pfn_t *pfn, int *max_order) +{ + KVM_BUG_ON(1, kvm); + return -EIO; +} #endif /* CONFIG_KVM_PRIVATE_MEM */ #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 4d3ba346c415..f414646c475b 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -714,34 +714,59 @@ __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, return folio; } -int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, - gfn_t gfn, kvm_pfn_t *pfn, int *max_order) +static int +kvm_gmem_get_pfn_folio_locked(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order, + struct folio **folio) { struct file *file = kvm_gmem_get_file(slot); - struct folio *folio; bool is_prepared = false; int r = 0; if (!file) return -EFAULT; - folio = __kvm_gmem_get_pfn(file, slot, gfn, pfn, &is_prepared, max_order); - if (IS_ERR(folio)) { - r = PTR_ERR(folio); + *folio = __kvm_gmem_get_pfn(file, slot, gfn, pfn, &is_prepared, max_order); + if (IS_ERR(*folio)) { + r = PTR_ERR(*folio); goto out; } if (!is_prepared) - r = kvm_gmem_prepare_folio(kvm, slot, gfn, folio); + r = kvm_gmem_prepare_folio(kvm, slot, gfn, *folio); - folio_unlock(folio); - if (r < 0) - folio_put(folio); + if (r) { + folio_unlock(*folio); + folio_put(*folio); + } out: fput(file); return r; } + +int kvm_gmem_get_pfn_locked(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order) +{ + struct folio *folio; + + return kvm_gmem_get_pfn_folio_locked(kvm, slot, gfn, pfn, max_order, &folio); + +} +EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn_locked); + +int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order) +{ + struct folio *folio; + int r; + + r = kvm_gmem_get_pfn_folio_locked(kvm, slot, gfn, pfn, max_order, &folio); + if (!r) + folio_unlock(folio); + + return r; +} EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); #ifdef CONFIG_KVM_GENERIC_PRIVATE_MEM