From patchwork Fri Dec 13 16:47:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907461 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 79169E7717F for ; Fri, 13 Dec 2024 16:48:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 047506B009E; Fri, 13 Dec 2024 11:48:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F370D6B009F; Fri, 13 Dec 2024 11:48:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB3F86B00A0; Fri, 13 Dec 2024 11:48:23 -0500 (EST) 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 B414D6B009E for ; Fri, 13 Dec 2024 11:48:23 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6AD7142D25 for ; Fri, 13 Dec 2024 16:48:23 +0000 (UTC) X-FDA: 82890517878.06.D566373 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf21.hostedemail.com (Postfix) with ESMTP id 580B21C0002 for ; Fri, 13 Dec 2024 16:47:32 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=XCuNnPg8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3U2VcZwUKCNAFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3U2VcZwUKCNAFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108477; a=rsa-sha256; cv=none; b=CU88uAzjwhk76c5wyXziYHl5yh0H1eTxkCrRTFNP5Zk0kYrQMCH0eDBJfT/9y5mKSDZgbj qJg6A3p2Be8BrVMI1q/OoA4mwG/aXO4vp7ypsY7qeC1kKHqPR7PVTroUg5mXkOx5i0lDKP D26DqG+I8jfjP/HhbDC8bUkPllz+OxQ= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=XCuNnPg8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3U2VcZwUKCNAFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3U2VcZwUKCNAFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108477; 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=Nn98e+WujoBnk4EYt4M9w4x2TZk0zk8clmlBtUUAHUE=; b=xdoU4SGFLG97jF78yL79tBByUnOeaj2PX8sFC+QP2iitp2B/SCcdHXsFroF6X/C3yOA2BM KWO1M7iV6GlgXBuB8/Cl56d9EttK+m7J+3vCt5jL8usyNFeManAcJjTCdLATU5d2a1STO9 bK4vFqyVuJh84yXxQjEKgTL77oSMBzA= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4359eb032c9so15928765e9.2 for ; Fri, 13 Dec 2024 08:48:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108500; x=1734713300; 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=Nn98e+WujoBnk4EYt4M9w4x2TZk0zk8clmlBtUUAHUE=; b=XCuNnPg82Y2Y14g1idQRjfWBPmyffspGwo1XlYnsIGRZkykp3Lx0Vw9Nhw1pPJHQMU UHheJExMDuc064yGbryUR9zsa0cKRyj/VfRKBym+I2tn9AfSzzW7V3xTTVd+yfkqlEby Vgh1qi6XoVscBt2g91Z8tqMIOMmc5xiwDSPwOshjx6m8yYL9/gTuA5XprlRjkjU33q94 kmMf0WoBZ+VZ12It395OOJR/ukr5KuMbCwk/oeC5quKRImtCkjcl6lQVJVrq3CBvQgmY WLnSrsuzxcaaxw8W9Wim6lsTXYYzk6RPbr9ikf0TJmIJTSmq2driEWJ5tPzxwMrme2Hp 5e/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108500; x=1734713300; 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=Nn98e+WujoBnk4EYt4M9w4x2TZk0zk8clmlBtUUAHUE=; b=vots7j25OvwsF/EW0hSGbShiz8ODr/uxPOVfER+kYuKMnFSV03lbW0oUd4nsGrNvcN YnOf7f3O5NSnu6dV+AuxywgwLM9W9VEKSyhiFaLLArNfEGq8btlsXkIhNuVHq1O09xgE chw4L7lc+GVNG4kl79JlxNhqgiJGdh1qWqxCm7qeCzqJbqBqXKBic7Ph+kBCrTDaexxA Mn3A3e4lyqdm8y4UlkBX2n1EqgZ09X+w4IyLXIAxGDkZzBU4+xyZZzVl2stn0ggFBR80 JYDRYYQCvKbs0URI9a0ryM5js5PoQO7QLY947onA2nWIUWub99Lq7Pf+xmfZOSjI3FKe eyCQ== X-Forwarded-Encrypted: i=1; AJvYcCUUa80ekGWDP/8IxP8AuHkjhA+0OrIYlJeXc9L4AqRVBDuo28zCFMoJVlXbC+OM+scewBcye9zGzw==@kvack.org X-Gm-Message-State: AOJu0YywTHK9lXCtJEYiUcpQmn0NrJypP1tNk0UGJEvhnxCR4z24qeEF +dnaKnnANF9pLPmsTLcG30p3h0jUSp+KDm8Q006IlztcV6VSd9bbThiaYzlIT7mc1km4AcbjUQ= = X-Google-Smtp-Source: AGHT+IGbUUdOrO+LdNxgwAJN5RVrV2s69jyBzXe2ihtQ1qCa2+mzNDnn/emZBrvQ9OXRTxBEpqDlu0D2/g== X-Received: from wmma26.prod.google.com ([2002:a05:600c:225a:b0:434:f299:5633]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1e8a:b0:434:f5d1:f10f with SMTP id 5b1f17b1804b1-4362aa52fa7mr31352765e9.17.1734108499591; Fri, 13 Dec 2024 08:48:19 -0800 (PST) Date: Fri, 13 Dec 2024 16:47:59 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-4-tabba@google.com> Subject: [RFC PATCH v4 03/14] 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: h598m6tfnzmwpf7qd5bdwymwze8oxpn5 X-Rspam-User: X-Rspamd-Queue-Id: 580B21C0002 X-Rspamd-Server: rspam08 X-HE-Tag: 1734108452-291279 X-HE-Meta: U2FsdGVkX1+TdQ78aI0uVO9X1PipAOceqN9EC4WPlvnI51C1oVpB2pnrAMd4CfhKNnTHkW3rZXU7+HD8jzBGYjrXTdL3w4wioxwI4f+rk2R9wzGVp62xmEhagBbFb1m8CyRqRfRl0Xtex7qNSfmTPegmYdHm/KbXkmqrd+BO4/xZayE9xJsDhA+S6PJ75idhFHh3zABbBG3LhUQmZ1M9FLF8txEJOIN2TBoQnGWs0IMDHSITXgToP5TZY4GHTIEKcRj25BcD0EMkysHrc/hwqF0CxC78+Vn1zmmJnxXlFXx29kW3YpXsSSsFDeSPzTT2P15wk/2NpNE+1vxdzcSNtx/zJsXtHnSkez3UgGXHcQw65sE1zkzFhtjtL9ryPBA5LzkuKjXoqPjKjSREt1cK8Eno18FBBhBneF4JOiSVJSeZuG1jzGHtcXIYoFOLwb3XLPW16cR6DvySMqGiNEd/T7v7I9R/vJH4lVEjU57wlJWFLNgCLyYrgAOXF+Sap60CcQ/n+6ZGAbvjuNNr5X9OE0LAIcBfBzZTi66JtBdMMY0fROOKw/SueTSNuKnYft8JgDk8P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M0s= X-Bogosity: Ham, tests=bogofilter, spamicity=0.001012, 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