From patchwork Thu Feb 15 15:28:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 201577 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp486328dyb; Thu, 15 Feb 2024 07:44:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWyTYG7tuuaUAOG7UkaxdH4r2/bjV3DahfxTPu0H2yzH94H02Wgrk3n2U6y6mxsIJ5P67dET7MgzVPVO0n2EZZkNR9mGQ== X-Google-Smtp-Source: AGHT+IGbpUjpIjHRoeq978mB7xAZpwrq20vI+I/OiK9D7a97/7/0A7Z1pobrz5w656dYBn5bsIX+ X-Received: by 2002:a17:906:3d41:b0:a3d:b7e1:2670 with SMTP id q1-20020a1709063d4100b00a3db7e12670mr362266ejf.14.1708011844295; Thu, 15 Feb 2024 07:44:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708011844; cv=pass; d=google.com; s=arc-20160816; b=dc5tpxVt8YijaGhoY+/k4qL2DBU+Oa/1sxidb6DmwEsSnF0O1grli1lnNrC3bn8QFu +W2SdaEEoVs5RZHEcRVlIWDumti8i/sKaD4uUb0jNxWwWEETHq5i2g+F6eQF8//Y0MQr i0euLVNivCc0EP+uWSmvzwLvvR9FCZ3psHsTVVPCDOnCV/m1Uvp/GzMEN8jBxNjM5c7y Xb36flrFv0iwUXcxIzdXdViVuMCD17bshYgz8RakW5xSJVfkkBZ8kBMUW30IRb7Zur5T iMT+6/Ddt4szz6fdhv3rXiYWdDmsNEyQpDYzHCTpZtk75gl1AuCpbLuGEOmXfTNtMbAK /I9w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:to:from:dkim-signature; bh=DVmkuHvAJAn35Oz9caMUxenePj8iuuNulYJyd17+sk4=; fh=NrOggL3VZvuSFh5W9Wj2+kLgS4v0nogfnAWvBJoouXQ=; b=gKq6NR7oCqvl+bkCtoME+mFV/E5qSBWarwONpVMBAI+NFVnA4hwUqLLX1ZCBYWBnKS ktTnvEQy5pga0DykPAnHv3Shf4Abb1tpEM5v7tv8ASl2eBG+XHeSmQAMxVV6wMQ4VEQy VStj9nL+kKDBThhsgs8gXsppWjksMRO6AZH1VzslvOjhaGoaTwoP9KsMlgnheYdItiDu EW5NUqcVXNeJXQDnKiQEXSsuDoyiaafVu5h3nJ+cX8QQEVI6+mxnGa6vZ3Rz5y+ZUtps U2+ec4KNJob91GEqL3evzpwqyqdg6El5OcecTdTsTDdvvne+TAuo3FuteRfg0sM7INq1 l2hQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=uN7n7u9Q; arc=pass (i=1 spf=pass spfdomain=xen.org dkim=pass dkdomain=xen.org); spf=pass (google.com: domain of linux-kernel+bounces-67186-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67186-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id gg26-20020a170906e29a00b00a3d9a169dfcsi601774ejb.370.2024.02.15.07.44.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 07:44:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67186-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=uN7n7u9Q; arc=pass (i=1 spf=pass spfdomain=xen.org dkim=pass dkdomain=xen.org); spf=pass (google.com: domain of linux-kernel+bounces-67186-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67186-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 430711F258EE for ; Thu, 15 Feb 2024 15:32:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 18A081353E8; Thu, 15 Feb 2024 15:30:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xen.org header.i=@xen.org header.b="uN7n7u9Q" Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1F8B1350C1; Thu, 15 Feb 2024 15:30:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.215.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708011040; cv=none; b=LjgyUqcgIEq5oPiaXEgz/pl0zrQoO5fnoX2ibChTVr4gVLtQYL8MUOG1fuUv2hK8kcSyqxEDDl3iuOp0Vz3jyFB+UsRPXkvg5GuUYyBNKq+HWkPrvjjehgUSc7luflChnfSmOORRxf4bl/N71xJgvmejumOqb2K52/OT+sDhBws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708011040; c=relaxed/simple; bh=R8mjm6bTMdSdKqKArLsYNQPe2oXTn04HDiw3MW+30do=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VgyNRQnmDeUX2cAY8PnjMYHlDgVgryiSQXPuxQVvXwordZPOMEVKhyUMVvoSHBG/VaFt5pSNUYt8ud+xCCuPJl+9e4l47oyMxmAZX8naLmTUDcWLR/FNZ1jXHtl4P5XkCecbYKd5fYoEaJIcBe5sWlF+Ot3ruNCixFbsS9C2pbU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org; spf=pass smtp.mailfrom=xen.org; dkim=pass (1024-bit key) header.d=xen.org header.i=@xen.org header.b=uN7n7u9Q; arc=none smtp.client-ip=104.130.215.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xen.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:To:From; bh=DVmkuHvAJAn35Oz9caMUxenePj8iuuNulYJyd17+sk4=; b=uN7n7u9QS1Sm1pOOOlhRLCrfcV g/qevuY49oxJ5QtFWbF4Wnu8gLqzuDqXrdUCqK6TOjJXAuT46wWIG5zTi+Pfc2UQKl4DZzftLPpvt /igCmUawwEcNjKWix9bVW5A68/uYt+ZiBXnqPbeuHUpExhAotK3N35oSeEXgKGDNeuLU=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1radgd-0001Dx-R7; Thu, 15 Feb 2024 15:30:07 +0000 Received: from 54-240-197-226.amazon.com ([54.240.197.226] helo=REM-PW02S00X.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1radgd-00089r-Fl; Thu, 15 Feb 2024 15:30:07 +0000 From: Paul Durrant To: Paolo Bonzini , Jonathan Corbet , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , David Hildenbrand , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Sven Schnelle , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , David Woodhouse , Paul Durrant , Shuah Khan , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v13 01/21] KVM: pfncache: Add a map helper function Date: Thu, 15 Feb 2024 15:28:56 +0000 Message-Id: <20240215152916.1158-2-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240215152916.1158-1-paul@xen.org> References: <20240215152916.1158-1-paul@xen.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790980227444264448 X-GMAIL-MSGID: 1790980227444264448 From: Paul Durrant There is a pfncache unmap helper but mapping is open-coded. Arguably this is fine because mapping is done in only one place, hva_to_pfn_retry(), but adding the helper does make that function more readable. No functional change intended. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Sean Christopherson Cc: David Woodhouse Cc: Paolo Bonzini v8: - Re-work commit comment. - Fix CONFIG_HAS_IOMEM=n build. --- virt/kvm/pfncache.c | 47 ++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 2d6aba677830..10842f1eeeae 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -96,17 +96,32 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) } EXPORT_SYMBOL_GPL(kvm_gpc_check); -static void gpc_unmap_khva(kvm_pfn_t pfn, void *khva) +static void *gpc_map(kvm_pfn_t pfn) { - /* Unmap the old pfn/page if it was mapped before. */ - if (!is_error_noslot_pfn(pfn) && khva) { - if (pfn_valid(pfn)) - kunmap(pfn_to_page(pfn)); + if (pfn_valid(pfn)) + return kmap(pfn_to_page(pfn)); + #ifdef CONFIG_HAS_IOMEM - else - memunmap(khva); + return memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB); +#else + return NULL; #endif +} + +static void gpc_unmap(kvm_pfn_t pfn, void *khva) +{ + /* Unmap the old pfn/page if it was mapped before. */ + if (is_error_noslot_pfn(pfn) || !khva) + return; + + if (pfn_valid(pfn)) { + kunmap(pfn_to_page(pfn)); + return; } + +#ifdef CONFIG_HAS_IOMEM + memunmap(khva); +#endif } static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_seq) @@ -175,7 +190,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) * the existing mapping and didn't create a new one. */ if (new_khva != old_khva) - gpc_unmap_khva(new_pfn, new_khva); + gpc_unmap(new_pfn, new_khva); kvm_release_pfn_clean(new_pfn); @@ -193,15 +208,11 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) * too must be done outside of gpc->lock! */ if (gpc->usage & KVM_HOST_USES_PFN) { - if (new_pfn == gpc->pfn) { + if (new_pfn == gpc->pfn) new_khva = old_khva; - } else if (pfn_valid(new_pfn)) { - new_khva = kmap(pfn_to_page(new_pfn)); -#ifdef CONFIG_HAS_IOMEM - } else { - new_khva = memremap(pfn_to_hpa(new_pfn), PAGE_SIZE, MEMREMAP_WB); -#endif - } + else + new_khva = gpc_map(new_pfn); + if (!new_khva) { kvm_release_pfn_clean(new_pfn); goto out_error; @@ -326,7 +337,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, mutex_unlock(&gpc->refresh_lock); if (unmap_old) - gpc_unmap_khva(old_pfn, old_khva); + gpc_unmap(old_pfn, old_khva); return ret; } @@ -412,7 +423,7 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc) list_del(&gpc->list); spin_unlock(&kvm->gpc_lock); - gpc_unmap_khva(old_pfn, old_khva); + gpc_unmap(old_pfn, old_khva); } } EXPORT_SYMBOL_GPL(kvm_gpc_deactivate);