From patchwork Sat Sep 16 00:30:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13388052 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 A62E1C46CA1 for ; Sat, 16 Sep 2023 01:46:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Reply-To:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID :References:Mime-Version:In-Reply-To:Date:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=s5BpNcF2Dn3LJO+42PRvixaJo5GN1Ig4gjiXjgmaoFA=; b=KRlKN7kkEnMjbt erqfLNm6Ar8ij12JfELIMUjVniwXazAw4dTMBuctDJI4s72xm5oUTR6wa8ydvOvsdpAmipZPhftbr WruNkmUMZeGPyX7xP3qFhRVDjfmEowy38kl3WWadb8T1XuInYNw5lTsNPH8qnWlc9EZVBwPU5HNIt gxX1jGY9gTUVDXdkeUt4KvZ2VzPDQQ0VeMqFvnXobHbnjyJNSPfWLzIv8eD2Wlbtd61XhM3TBnwyF GP0MeZMf5SFEAc/YF7kbqXeZVB0m3eGn2wPTnIBXd6PnBruIR+WsB11JlLveoG8+aQIgg6kcXcXVB kUzQVeu78RGBo2sAgTAA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qhKNp-00BkJL-2B; Sat, 16 Sep 2023 01:46:05 +0000 Received: from mail-oa1-x49.google.com ([2001:4860:4864:20::49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qhJDb-00BcN7-0W for linux-arm-kernel@lists.infradead.org; Sat, 16 Sep 2023 00:31:30 +0000 Received: by mail-oa1-x49.google.com with SMTP id 586e51a60fabf-1b728bfb372so4416337fac.2 for ; Fri, 15 Sep 2023 17:31:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694824284; x=1695429084; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=NdOAPx70WjWtTXOKtIxgwEtN+XJRELer/ruyZoFwPzQ=; b=LSquUboBDoQ7KX4y/WPTLIPUsaCl72dhIc0X/rKkpVaRLs7PKmbtHjA4s7bh+zaMCV eqYI2syT9HFV5iKs76v6cXfEDFW01sNZWGOuhis1vIXp+5Bg6AtKNDBEgAwLvKxeU1Pp xIo6aBZ7VwKqOPO2qeTk1EW9e5InPNGysEmmS67t5Woque/ViGY1loxGQ+zFXEm9suzK DuVgEzB9puF7k819xSf4xlm5TVVl4xwAd6iyyVaCvz3K6CHxmXCnDV6XSuRUskS2Eoli 8elzgoJLg1LZilp32721lIEm9G1gU8Ygmd+UYm5tvp0GyMzoooBvdOSBmyAoQ4gzToss 1lHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694824284; x=1695429084; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NdOAPx70WjWtTXOKtIxgwEtN+XJRELer/ruyZoFwPzQ=; b=Lz7fm2OVeRHJ4L11do9Uv8mTSlly3BS0qKslfH9yWHST3kNAafCSkRb4nsax+PdpZb sWL0JsY3kMcg/hToHvfJt350qSvFABpajmO8jYKDvaicxw6q+zhe8+9LPyvazYx69ha0 SFpnu/FfayMxla9oEQs8tyRgxKml1lXlADb8XrW9RuwwMs9ycJPvmdnkMRyM6CTISYjM LLY16hl0+No8YZdtlxJpMbMbnfb+Fal6PFTOLpuK/OYeJChGxGc1Hgjg6+DzaKAe7uBA AE91MhA4JhOOB+p4rbuzAeow18PRfe2bk/jZe0emreAl0ipWcmV0ERAWtqyjw3MotYOg lscg== X-Gm-Message-State: AOJu0Yya/y+qkuBV8Fe8pcmULBKDYGQ79XEOILCQZs2zOYeEV5muTuGE tceYqCd7lsX3/d/0Fi5G6sYxuEUDurU= X-Google-Smtp-Source: AGHT+IFjfayrLbVmI/uUcITF7riix4t2p7LQSznFwYgaB2xPw0hEc6MrZUjOPPPKeYuL02E3L+TxPOZWHW4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6870:b7ad:b0:1d6:4da3:ae2d with SMTP id ed45-20020a056870b7ad00b001d64da3ae2dmr1068876oab.7.1694824284654; Fri, 15 Sep 2023 17:31:24 -0700 (PDT) Date: Fri, 15 Sep 2023 17:30:54 -0700 In-Reply-To: <20230916003118.2540661-1-seanjc@google.com> Mime-Version: 1.0 References: <20230916003118.2540661-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.459.ge4e396fd5e-goog Message-ID: <20230916003118.2540661-3-seanjc@google.com> Subject: [PATCH 02/26] vfio: Move KVM get/put helpers to colocate it with other KVM related code From: Sean Christopherson To: Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Peter Zijlstra , Arnaldo Carvalho de Melo , Sean Christopherson , Paolo Bonzini , Tony Krowiak , Halil Pasic , Jason Herne , Harald Freudenberger , Alex Williamson , Andy Lutomirski Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, kvm@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Anish Ghulati , Venkatesh Srinivas , Andrew Thornton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230915_173127_218284_B0B64DB2 X-CRM114-Status: GOOD ( 14.24 ) 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: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Move the definitions of vfio_device_get_kvm_safe() and vfio_device_put_kvm() down in vfio_main.c to colocate them with other KVM-specific functions, e.g. to allow wrapping them all with a single CONFIG_KVM check. Signed-off-by: Sean Christopherson Reviewed-by: Jason Gunthorpe Reviewed-by: Alex Williamson --- drivers/vfio/vfio_main.c | 104 +++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 80e39f7a6d8f..6368eed7b7b2 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -381,58 +381,6 @@ void vfio_unregister_group_dev(struct vfio_device *device) } EXPORT_SYMBOL_GPL(vfio_unregister_group_dev); -#if IS_ENABLED(CONFIG_KVM) -void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm) -{ - void (*pfn)(struct kvm *kvm); - bool (*fn)(struct kvm *kvm); - bool ret; - - lockdep_assert_held(&device->dev_set->lock); - - if (!kvm) - return; - - pfn = symbol_get(kvm_put_kvm); - if (WARN_ON(!pfn)) - return; - - fn = symbol_get(kvm_get_kvm_safe); - if (WARN_ON(!fn)) { - symbol_put(kvm_put_kvm); - return; - } - - ret = fn(kvm); - symbol_put(kvm_get_kvm_safe); - if (!ret) { - symbol_put(kvm_put_kvm); - return; - } - - device->put_kvm = pfn; - device->kvm = kvm; -} - -void vfio_device_put_kvm(struct vfio_device *device) -{ - lockdep_assert_held(&device->dev_set->lock); - - if (!device->kvm) - return; - - if (WARN_ON(!device->put_kvm)) - goto clear; - - device->put_kvm(device->kvm); - device->put_kvm = NULL; - symbol_put(kvm_put_kvm); - -clear: - device->kvm = NULL; -} -#endif - /* true if the vfio_device has open_device() called but not close_device() */ static bool vfio_assert_device_open(struct vfio_device *device) { @@ -1354,6 +1302,58 @@ bool vfio_file_enforced_coherent(struct file *file) } EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent); +#if IS_ENABLED(CONFIG_KVM) +void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm) +{ + void (*pfn)(struct kvm *kvm); + bool (*fn)(struct kvm *kvm); + bool ret; + + lockdep_assert_held(&device->dev_set->lock); + + if (!kvm) + return; + + pfn = symbol_get(kvm_put_kvm); + if (WARN_ON(!pfn)) + return; + + fn = symbol_get(kvm_get_kvm_safe); + if (WARN_ON(!fn)) { + symbol_put(kvm_put_kvm); + return; + } + + ret = fn(kvm); + symbol_put(kvm_get_kvm_safe); + if (!ret) { + symbol_put(kvm_put_kvm); + return; + } + + device->put_kvm = pfn; + device->kvm = kvm; +} + +void vfio_device_put_kvm(struct vfio_device *device) +{ + lockdep_assert_held(&device->dev_set->lock); + + if (!device->kvm) + return; + + if (WARN_ON(!device->put_kvm)) + goto clear; + + device->put_kvm(device->kvm); + device->put_kvm = NULL; + symbol_put(kvm_put_kvm); + +clear: + device->kvm = NULL; +} +#endif + static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm) { struct vfio_device_file *df = file->private_data;