From patchwork Sat Dec 2 09:18:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 172765 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1673671vqy; Sat, 2 Dec 2023 01:47:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IFl+ps0MmwF8r2PdEYWWWa0zImwhRT0GVaPZJwjxxQ+kT9lNg0nPCq15BwIWDi7J+6cjkxF X-Received: by 2002:a92:db10:0:b0:35d:49e2:3090 with SMTP id b16-20020a92db10000000b0035d49e23090mr1201285iln.10.1701510439708; Sat, 02 Dec 2023 01:47:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701510439; cv=none; d=google.com; s=arc-20160816; b=lOHNDUFyVdRBu6iFY26aebA9UF3ilMKsqg6ijiObE7YrMWzkxYM6Q2G+wSY0AkHhlx gbWSsMWa5YxnS5HOU0QyEsF38ImQr2BWKYsOrtSMt6p9TGkINsvq7N6LkJydtoia7crU CYdcPDMXCWhV7MGf1Obt3AxKM4bTRx1uzC3tAzxyaffMgLgFf2E1iLchoDGbRQg+0Eoy YCoH5dBWSk64CaSvdvblMIFMdi61M3CisJLDaL4rhWFPqnzEZeSM8AA2Fm3FpyN5PC2j ABp/QAnUevvsmSHHF8oTpJYsPcLMgmrYuJYQaVSDU5pRpWz+lErZTRoilRH+LXrKKeT1 SGFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=jZmIwMQk8hX7YqY9lgDIlTUkUIM87gi8OpSTiSa4fHw=; fh=+WI4m5k3dRLR+dR3neThuZkNBTzIm/a8HgtddERL9fA=; b=ylKgeAkJ1RH6WQGPrYEVIARWWAakQ83+KigjnV6u1FwzKxaPltNAys8aiYM3Jg/bEY UQuzdVjvO9TNAWP9SkvN9aNYF6rq3zo2NiaL2ktdawMjOM/gwjxj9zZSZykIfebm7kbC vaacvHLuILgYv8mcJ4L3HTLEg7EtTwGc/BUwb6BW3K3ykMyhjri4rvhqGmsduebbWhHU 35dS3VEoCnuiHOKni1Ud2BEqwvCPfM5zLUIkyG8UlununqDP0rWtb2BDx1N/hcotmWAC 5SSNYeU2CUpvy1pHxwWe98K60gsvTQI8TweHfmmS/eFKVGbgjCKaOXquyJrzc7xYGlCZ BN5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kTCuUk0j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id k18-20020a6568d2000000b00584e731e7fbsi4584486pgt.280.2023.12.02.01.47.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 01:47:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kTCuUk0j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 3583F827D0D5; Sat, 2 Dec 2023 01:47:16 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232330AbjLBJrH (ORCPT + 99 others); Sat, 2 Dec 2023 04:47:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232310AbjLBJrF (ORCPT ); Sat, 2 Dec 2023 04:47:05 -0500 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5BC5181; Sat, 2 Dec 2023 01:47:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701510432; x=1733046432; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=zgOH3vLMDlIUcyJqzB7ENWd/3UPYCaFUNvwD3NOa7Mg=; b=kTCuUk0jF3Cet0kEc1nT9o6WBuMhdOP176iSHOBk3UCAA0fNXah5apdy MqtYogUNLf78NWVGbQ2frnCPgpngNy17SrYzTh8lkwU6nKKZC+EVP/Ime 6r+ZsgqT3zOZ2idF3yzkhjjiwCabX+fXcqC6iXQrayh3Dp/XhO2l3zjN9 E2bMkb3VIEg0p/j/bKgZBIx56dCAhdhLvNxJDQynsBB5rnKHN+j34R5Us 2z6ZAKjpXTDEpzSwiFG0Xnt/IojXPengQylXDhoeJDd8MZ8cr5HVDB4nQ QOkIdRa13YL80aNUV00PTkVx5XyjWaagoNJdaXr0qH8melomxngcGRSZu g==; X-IronPort-AV: E=McAfee;i="6600,9927,10911"; a="474342" X-IronPort-AV: E=Sophos;i="6.04,245,1695711600"; d="scan'208";a="474342" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2023 01:47:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,245,1695711600"; d="scan'208";a="18037347" Received: from yzhao56-desk.sh.intel.com ([10.239.159.62]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2023 01:47:08 -0800 From: Yan Zhao To: iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: alex.williamson@redhat.com, jgg@nvidia.com, pbonzini@redhat.com, seanjc@google.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, kevin.tian@intel.com, baolu.lu@linux.intel.com, dwmw2@infradead.org, yi.l.liu@intel.com, Yan Zhao Subject: [RFC PATCH 07/42] KVM: Forward page fault requests to arch specific code for exported TDP Date: Sat, 2 Dec 2023 17:18:12 +0800 Message-Id: <20231202091812.13830-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231202091211.13376-1-yan.y.zhao@intel.com> References: <20231202091211.13376-1-yan.y.zhao@intel.com> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Sat, 02 Dec 2023 01:47:16 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784163010986435992 X-GMAIL-MSGID: 1784163010986435992 Implement .fault op of KVM TDP FD object and pass page fault requests from importers of KVM TDP FD to KVM arch specific code. Since the thread for importers to call .fault op is not vCPU thread and could be a kernel thread, thread "mm" is checked and kthread_use_mm() are called when necessary. Signed-off-by: Yan Zhao --- include/linux/kvm_host.h | 9 +++++++++ virt/kvm/tdp_fd.c | 28 +++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f73d32eef8833..b76919eec9b72 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2346,6 +2346,8 @@ struct kvm_exported_tdp { #ifdef __KVM_HAVE_ARCH_EXPORTED_TDP int kvm_arch_exported_tdp_init(struct kvm *kvm, struct kvm_exported_tdp *tdp); void kvm_arch_exported_tdp_destroy(struct kvm_exported_tdp *tdp); +int kvm_arch_fault_exported_tdp(struct kvm_exported_tdp *tdp, unsigned long gfn, + struct kvm_tdp_fault_type type); #else static inline int kvm_arch_exported_tdp_init(struct kvm *kvm, struct kvm_exported_tdp *tdp) @@ -2355,6 +2357,13 @@ static inline int kvm_arch_exported_tdp_init(struct kvm *kvm, static inline void kvm_arch_exported_tdp_destroy(struct kvm_exported_tdp *tdp) { } + +static inline int kvm_arch_fault_exported_tdp(struct kvm_exported_tdp *tdp, + unsigned long gfn, + struct kvm_tdp_fault_type type) +{ + return -EOPNOTSUPP; +} #endif /* __KVM_HAVE_ARCH_EXPORTED_TDP */ #endif /* CONFIG_HAVE_KVM_EXPORTED_TDP */ diff --git a/virt/kvm/tdp_fd.c b/virt/kvm/tdp_fd.c index 3271da1a4b2c1..02c9066391ebe 100644 --- a/virt/kvm/tdp_fd.c +++ b/virt/kvm/tdp_fd.c @@ -223,7 +223,33 @@ static void *kvm_tdp_get_metadata(struct kvm_tdp_fd *tdp_fd) static int kvm_tdp_fault(struct kvm_tdp_fd *tdp_fd, struct mm_struct *mm, unsigned long gfn, struct kvm_tdp_fault_type type) { - return -EOPNOTSUPP; + bool kthread = current->mm == NULL; + int ret = -EINVAL; + + if (!tdp_fd || !tdp_fd->priv || !tdp_fd->priv->kvm) + return -EINVAL; + + if (!type.read && !type.write && !type.exec) + return -EINVAL; + + if (!mm || tdp_fd->priv->kvm->mm != mm) + return -EINVAL; + + if (!mmget_not_zero(mm)) + return -EPERM; + + if (kthread) + kthread_use_mm(mm); + else if (current->mm != mm) + goto out; + + ret = kvm_arch_fault_exported_tdp(tdp_fd->priv, gfn, type); + + if (kthread) + kthread_unuse_mm(mm); +out: + mmput(mm); + return ret; } static const struct kvm_exported_tdp_ops exported_tdp_ops = {