From patchwork Fri Jan 17 16:29:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943550 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 2A75DC02183 for ; Fri, 17 Jan 2025 16:30:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2C8206B0083; Fri, 17 Jan 2025 11:30:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 24E576B0092; Fri, 17 Jan 2025 11:30:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C9566B0093; Fri, 17 Jan 2025 11:30:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D92F06B0092 for ; Fri, 17 Jan 2025 11:30:13 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 78FF4140592 for ; Fri, 17 Jan 2025 16:30:13 +0000 (UTC) X-FDA: 83017481106.11.7B0EB39 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf08.hostedemail.com (Postfix) with ESMTP id 910C0160005 for ; Fri, 17 Jan 2025 16:30:11 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2c3g3IgY; spf=pass (imf08.hostedemail.com: domain of 3koWKZwUKCAs4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3koWKZwUKCAs4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@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=1737131411; 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=i68yYctGbzvQYlSdS0p+4WdM6JfkNAVslQef6pXRQ/Y=; b=HlmkPPvD4hi9XS6anStbD86ehmOBPenaRUEYOGBaxgf7UQrucw6yTapYyF/PrFPWeZc/ix 00pKg8qi+aN0lKls8h9bw6qVKZEVOi2i+zvJnBh4mf4jf8beGUfQfRlOZBBRQT40m2ru3H eohDXaG7KMmD3zF9FG7WijWKLz6LNQE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131411; a=rsa-sha256; cv=none; b=dRE3t23abnmghnPXeUH744fcmFSzM8q1oxduBW3ZCqOs1iNfm4VYxn5Ah/BhJUA72M0uGi VuA38xy078zpxSkBKqJoar2/oavxePzZXY4MFc1/jPQiKH4sqm3G3i3RM5XLNHOrPdiAdn S1+PSnrotSkFIb5RP7dQK5Ozcm5C1S4= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2c3g3IgY; spf=pass (imf08.hostedemail.com: domain of 3koWKZwUKCAs4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3koWKZwUKCAs4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361eb83f46so16494435e9.3 for ; Fri, 17 Jan 2025 08:30:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131410; x=1737736210; 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=i68yYctGbzvQYlSdS0p+4WdM6JfkNAVslQef6pXRQ/Y=; b=2c3g3IgYiwkkBklIom/8dGFtkHTfixXOsxZ4NN9qsS7KtaAE+l5jc2AeWJzbPLrrtH RNDhXsWiLrh+BL+VxHoE15QNdsMc3WigEw++5LZUQbCn0o2bM5pQKMz8fFn2YmglkzjM PT047Z63OQemTxWN5+MN/xKI+SQ+VPmU+beO0N/ged3A+7idRe0HTfzSSkdoLqs/IJgd Ny/XeSGpFCF5izq9f7DTG04p/GA/EhsTOD5Rf6qxU7ntV2b/zW40flsmPgjPeMaBtCiA DsNPL172Y65rYa3w908WtgBYlLRClZH9yqil1a7xG+F4s3/fRc8JkJ9MKceGjohDySF4 8Snw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131410; x=1737736210; 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=i68yYctGbzvQYlSdS0p+4WdM6JfkNAVslQef6pXRQ/Y=; b=C8Bu1X22z7PMfh8v87iBPjstA3O7BuUiwKriQn2jnMg12w6P6GOkb1PW5VRFOt6LSe T4h6XMxueNjGRrzZrHaHtZgWpWHuZOFXX7lHX6kPw8x9KiGw5cz9sytFjNI8kccFmYo2 JiT61FWizGuMfuBLPD5bRWooDr/GJJ1BnqVSwcN/ChbN3f4mc/N3uxHvsZt5BKwna9M6 ucz1QU2o9kP0BPXB5affbhNxC5/CJuymNFyQiK/jPOcBxZIZ/ve9/uVUYnvchCbXeOoT cIRIvvWoQqTC9xy8axSrhPRLwENvecE9UsBWnH8yQtS2yG1PZzLZTuRMt3Az/8xwDGb5 YkiQ== X-Forwarded-Encrypted: i=1; AJvYcCWakPXW1NZSSGCyZpOc0rn/3A0r5OnC8Gzs5Gbdy82ADL3pcTZKjrSxyJoVClg2zj4HI7N2eySIVQ==@kvack.org X-Gm-Message-State: AOJu0YyGeUA+m5Zgw3MPpuHfZsBd2QEqkkGN1flTRPHS2u31MM7Rlxkf zRbyJwM9S/l2DL0HoIW3PcvtS3FtarOuo4mSJkC18IgPssLaAtB82SA6/wkoF2L4jVjqcA2RZA= = X-Google-Smtp-Source: AGHT+IGdZ+elMcO0tC2o/blUEAO2sesjnET5jVfx30pYB41ogtDO4dGi3tBo1ONh1MJc7o8U8tLatcsfwg== X-Received: from wmbhc13.prod.google.com ([2002:a05:600c:870d:b0:42c:bfc2:aa72]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3b88:b0:436:488f:4d8 with SMTP id 5b1f17b1804b1-438913cdba3mr42250775e9.11.1737131410248; Fri, 17 Jan 2025 08:30:10 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:49 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-4-tabba@google.com> Subject: [RFC PATCH v5 03/15] 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: et1eg5ptm4esoirb6qxetmt4z7ixr6ir X-Rspamd-Queue-Id: 910C0160005 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1737131411-713387 X-HE-Meta: U2FsdGVkX1/LXPBk4bXilolyO/I2IuoSnIMrArtW5EjlWeXIQ5Tuk7yg9PhCmvqQIlI8mMJVCupWKD00ZWxW4Ug3lfqVDqyF8bl91Enpa49L8glZfivbmUlOXXuME6+GQvcUW2mLZtNvPJYRFycVSMIqELplmKyiDcamo+nVlK7P/TBrsGJexJUAyI6QZMakqIRmXXA81tn86gRdZ72NLHshneOuwx7ak7N46CPic/kPgOV1AkcAxEbfgLpFgFGZAVcnDUYK1QXLWy4wBh0FURHJMJFTOQ/OyqR42u3Vrqi5nnwHsqSW9wyDHRkZy2VOjAyTNlrhh5pXfEO5yQ0nnSg4fwT/rAQDXKRkxA4N7thJykRG4k7X5YuhpP3akERMKp1OT2IUI7MuZJJjVGQQ+TFf9oMrAgfeb0zvhwomNJrhw+NkBnfSxnGN6RpGgRtbCF7JtaqoI9pHofv/LLmr7z1aum+umDxxLYe81nJvad5as0H3fthBVj6c3VfzK0rjeLJLxhU+yc1lCu20imUwUEp433kHC54KmVpmAt+okOWAaAwVmEQLiJs0q7ll9txxoO18dliym5v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ham, tests=bogofilter, spamicity=0.000000, 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 | 27 ++++++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 401439bb21e3..cda3ed4c3c27 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2500,6 +2500,9 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, struct page **page, int *max_order); +int kvm_gmem_get_pfn_locked(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, struct page **page, + int *max_order); #else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, @@ -2509,6 +2512,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, + struct page **page, 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 198554b1f0b5..6453658d2650 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -672,9 +672,9 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file, return folio; } -int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, - gfn_t gfn, kvm_pfn_t *pfn, struct page **page, - int *max_order) +int kvm_gmem_get_pfn_locked(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, struct page **page, + int *max_order) { pgoff_t index = kvm_gmem_get_index(slot, gfn); struct file *file = kvm_gmem_get_file(slot); @@ -694,17 +694,30 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, if (!is_prepared) r = kvm_gmem_prepare_folio(kvm, slot, gfn, folio); - folio_unlock(folio); - - if (!r) + if (!r) { *page = folio_file_page(folio, index); - else + } else { + folio_unlock(folio); folio_put(folio); + } out: 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, struct page **page, + int *max_order) +{ + int r = kvm_gmem_get_pfn_locked(kvm, slot, gfn, pfn, page, max_order); + + if (!r) + unlock_page(*page); + + return r; +} EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); #ifdef CONFIG_KVM_GENERIC_PRIVATE_MEM