From patchwork Thu Aug 1 09:01:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13749992 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 92C85C3DA64 for ; Thu, 1 Aug 2024 09:01:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E65F6B008A; Thu, 1 Aug 2024 05:01:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BDAF6B0092; Thu, 1 Aug 2024 05:01:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC7A46B0093; Thu, 1 Aug 2024 05:01:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C82466B008A for ; Thu, 1 Aug 2024 05:01:25 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 847F0A697A for ; Thu, 1 Aug 2024 09:01:25 +0000 (UTC) X-FDA: 82403082930.15.1C4AE7F Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf28.hostedemail.com (Postfix) with ESMTP id B1927C002A for ; Thu, 1 Aug 2024 09:01:23 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KZtL2XlU; spf=pass (imf28.hostedemail.com: domain of 34k6rZgUKCGkaHIIHNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--tabba.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=34k6rZgUKCGkaHIIHNVVNSL.JVTSPUbe-TTRcHJR.VYN@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=1722502878; 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=55lqFFECQwDLNrYt4aytrJso1NybdDvPLt3jS/2DBHA=; b=fk34soNMVc06wcqMlprFiXWK6kdGFLOEBvYdowU9cA+FfeYmPKpDQNYIqP6ac0ERPgmy06 4xc7g/LDsUMHKcG1veilsj1l4k7jxSYcqrlgNGU4dAqYLRpmC1KMxct2ax8Eyl5AF2u/Jv ZYgdGnqqdXeuTdfGNqQOBV3hhHXAy5E= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KZtL2XlU; spf=pass (imf28.hostedemail.com: domain of 34k6rZgUKCGkaHIIHNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--tabba.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=34k6rZgUKCGkaHIIHNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722502878; a=rsa-sha256; cv=none; b=YIn/pMdFs4z6kxHHHCc776KHjmWdw2wU0otNk8oU5OGmHFOAWrvjk5K2yXX2K5qK65MgVY CezdWwu2vm0CV+Xt2V9sogAjG4PuI1cg+Xak4dsWoczieJyDu6UvLEvegZoiTnCRz4VxnD RHPSwX3OntvMbIGiGbx+gk9uMyltW3Q= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-650b621f4cdso124035567b3.1 for ; Thu, 01 Aug 2024 02:01:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722502883; x=1723107683; 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=55lqFFECQwDLNrYt4aytrJso1NybdDvPLt3jS/2DBHA=; b=KZtL2XlUCT+512u4tHl239eruPbd1Q9YdclR3KDnH9+KDeHxUFaW4zkU8tHcf/jVFl nXMHr6027iElzIhqqQMzJFSfInaQlVRIIAcQ9371U33GzRLoYPQ/hBozO1Yl1kQmcCJh IAs0g5PzWne/tCERsjiNW599nTAr5yFC4gsYOF9xzjKQqI+bndx9GYVYcdrDvBWCh0ek rtWWp9wFtklvi5S/keQmeAEhdf2gBV+d3p/2x1mmp12/NTMVnE2RYbKUAIsMnaFCTSRv 5zNHlU1y5lfzWOfaLkq6ymWobslukckcb0XmeobZvVbBCvbmXiCfFTFv0zcozx7ZaEYm fQCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722502883; x=1723107683; 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=55lqFFECQwDLNrYt4aytrJso1NybdDvPLt3jS/2DBHA=; b=vBA1toRMyL8yYwAGZV2OZOqjZWjt19L+iCgVs9ttIMyfWLHKwfwxkv0PVqlycG+wSx O8eflHVMO+NyHC1dIUHJ0NfLZIhE58Z0bccmgP7Vdu/ik4zO9e/eqUHme9rAYY4gRIhy mlOn38Bnq3Hz7IXfI7hFwbrU6oE01MqlWECIVr/0niAkwrd/ErjjNAA6AMB4XGzrTNM6 cHty6Hfz+JWpSXY9rMjCZao39W0a/dvx35mU3OUSJSoChNwhFOjRX4nK0YdGBtHrKxtk UQipdORXsWbrD7mq6KpKvRFI6wrX8BEmIyQqpyqAUIzDM1NrHT4bBOsmeLEUkAq5u6sf 2a+A== X-Forwarded-Encrypted: i=1; AJvYcCUqNfujtphgPiWG/uR2/cG3g/pT4IIgEiERvn8zXrWSVHuaj640USNNkdaRwWwEVO6GAancCPW+pqpiVA3XAZU+fC4= X-Gm-Message-State: AOJu0YxqIZrDL4r6Ihj8DbbTdGMY5TkIgsup1r9xcAfoEhG2tc0/5qWw +C7TuZaqxLuVeVdpi7VBDoi8fY88dghV96wxXvXXnSR6BsxNAuWvPbSFkmk0sjDqI3DEA8/l3w= = X-Google-Smtp-Source: AGHT+IFXjhPCx5Vb6+Hy1aNGDM/3GXCZNNlq0AHfSY4+hcUlvoRXu5eLes4iJzd35wbNh934dQJX6BHp3A== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:690c:f05:b0:648:3f93:68e0 with SMTP id 00721157ae682-6875028afcamr1291237b3.6.1722502882722; Thu, 01 Aug 2024 02:01:22 -0700 (PDT) Date: Thu, 1 Aug 2024 10:01:08 +0100 In-Reply-To: <20240801090117.3841080-1-tabba@google.com> Mime-Version: 1.0 References: <20240801090117.3841080-1-tabba@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240801090117.3841080-2-tabba@google.com> Subject: [RFC PATCH v2 01/10] KVM: 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, tabba@google.com X-Rspam-User: X-Stat-Signature: s3m4c79ydek8szsgmaiqsyo3f1efdmix X-Rspamd-Queue-Id: B1927C002A X-Rspamd-Server: rspam11 X-HE-Tag: 1722502883-321158 X-HE-Meta: U2FsdGVkX1+feeKhJPc3KJ1vsQ7xnkzx0GcGZG1o7NNpQtbxe3tWMoyvxxKgc+yTTXQwvZnBC3rYlGwvh90CyV0g/jKQqrxF7R4UjZf2WQpOjgu+EoFftrEnRCpayvxxtKFLIShPV0Y9hukYsvH1ZX8M8lMjzLkdooy7VAZqSDXyWZ2VgSJCBGlCTJk9OQIfvqOO5D0cD7VSe0zDYI8hsu1htP+62Ivha0yhU4AYOmyYNqg2jiC8fjqeRorB+GJudw44SfVrptzbCbgxmoUxbSp3gWtcFN7pK7K4PYu36TYW9qSwP+a2AWyJVk5O+QY+Bip8W1YKqiwxYrKSlHat9ZqkELDZYgEohAHGOPWt+ZmpJfb0NV1286AnVvE4r9qRK5zSL35WuiD6UE92aWeKrcf2kGNpbBFnwoVHpS6uLzY72upNNvTaiNcVPU+zLXBFwY7CJt1521oQGiJt/WHysuZ44eZwr/+IOJe7ODqmJ6YZeiZZ5WCHd9TQfHB13T5kddfyWLpQPh81da6s/jcPP0Otw23uTDCJc/XDT5sFtH/WbrwKCo3uIwt06Am0fRLCZoWwf4uVuHqiQZQ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408, 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. Signed-off-by: Fuad Tabba --- include/linux/kvm_host.h | 11 +++++++++++ virt/kvm/guest_memfd.c | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 692c01e41a18..43a157f8171a 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2431,6 +2431,8 @@ 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, @@ -2439,6 +2441,15 @@ 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 */ #endif diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 747fe251e445..f3f4334a9ccb 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -482,8 +482,8 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) fput(file); } -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) { pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; struct kvm_gmem *gmem; @@ -524,10 +524,23 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, r = 0; - folio_unlock(folio); out_fput: fput(file); return r; } +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) +{ + int r; + + r = kvm_gmem_get_pfn_locked(kvm, slot, gfn, pfn, max_order); + if (r) + return r; + + unlock_page(pfn_to_page(*pfn)); + return 0; +} EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn);