Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp310915rwb; Wed, 28 Sep 2022 03:06:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5t1NqbydyZ8D1MH9p1RLB/16uj7mseGKx95vvTZ+0sjUp2PsxFLFGBctAHOzbb0CjBA1QT X-Received: by 2002:a17:90b:388b:b0:202:f92c:700f with SMTP id mu11-20020a17090b388b00b00202f92c700fmr9282258pjb.225.1664359586082; Wed, 28 Sep 2022 03:06:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664359586; cv=none; d=google.com; s=arc-20160816; b=ha8V+3Lf2UHHc/jGO6B6qmQ8VZ1CKr7eKDovUfFFIbDB3FhLP2p7/giZkXWVd++jb7 mTt/Kc0u5LfsJGdVS1OEmV4LrLWzrW8IW7gAWNNLxJmZ1rpQKErdiC5WolD+Hz+E/96a PD6FSev+tZJD499IzE9ocMzcoRjDTjd77u/Xo+GBqjZ09Bc7dxcba3PpEUjmnNjC1uor ns0dt6DVe+tdTrA0tRFMF24zLNVpfRTwr441oI6RiYYiKR5LmNkHcv3d6wsN5kbtzBqq ETYopSt4HDNeLn0SgrJNvVza0PYkcmwinHhYIIffInbzZfNBE6S6wrxPytT6H/yZIwV8 hwQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=ZiALUVy5CqizSDow31VVi7wSQoS4s7EZbdQ2Rk/2um8=; b=tr9HzJDihnUjx+7yXqmrbnqhwr6A043qbEDV95ZlE5A9SQInMYiIh6674i6ea30GrO 4n4YhBj4u33QC1cKBQP3MNelO8dlv3YzBZxpuBvU8YZRxVtk3yG/0UCrOHva94wQ+Ouv PoaP5953tpYi1lh9CwFOKrtkHH9dzXw4f7tY96yWGYNIF5vuI4ER0WH/+H9PaxSAUr9d lw1yIEozzWT1Z0zvUnTuqzyrFdNQ7eLdiAojGhqEw069UCKM8BZGGuZuFS9pZbm/A7X9 SHwq6HQU6Rv/nkBShr6XblypP23v905zq4aPnVPbwKps2y5pMZD9V9QkwQwbHa80O9op IGHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CL0wURgX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v1-20020a632f01000000b0042a75aeeb93si4504684pgv.521.2022.09.28.03.06.14; Wed, 28 Sep 2022 03:06:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CL0wURgX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232380AbiI1Jv1 (ORCPT + 99 others); Wed, 28 Sep 2022 05:51:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229885AbiI1JvZ (ORCPT ); Wed, 28 Sep 2022 05:51:25 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE065A6C06; Wed, 28 Sep 2022 02:51:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1664358684; x=1695894684; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=Br2i/PQsVuorfzONFXOsgP+GRMRyazWmVkbxSLZDOVE=; b=CL0wURgX/CMbJlPPBHWlyY5Z37NsJ44L9JqCsV2D3dNG1uh4Y8BptL1a sY03SqAjEBPKMCALB8oXH5JGKnjyWEgSQzWdFu7bF1pOvkCD4OzkWlHNm gRgH+Nr0U4K/1/mECtvZr/zigO/VXGoFmJi3+qKr4L0yY+EigGxFK0hFN K1FFxZDaLwNJuJasFDR5weJBbg1T/i8WbOz+qtfEVOR92n7PVIRyv7kFX 0kT/nH51zbldYQnRT8w80YpPmrk+K199JhubcUM9Bfgwt+vkY5OnFa0Uj kUduteBl2BL4mZb5WWuTiTYYFF8i0rrKGdqIFRzJFoJTjs93/wcudb6P2 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10483"; a="303041247" X-IronPort-AV: E=Sophos;i="5.93,351,1654585200"; d="scan'208";a="303041247" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Sep 2022 02:51:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10483"; a="652617338" X-IronPort-AV: E=Sophos;i="5.93,351,1654585200"; d="scan'208";a="652617338" Received: from liuzhao-optiplex-7080.sh.intel.com ([10.239.160.132]) by orsmga008.jf.intel.com with ESMTP; 28 Sep 2022 02:51:19 -0700 From: Zhao Liu To: "K . Y . Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ira Weiny , "Fabio M . De Francesco" , Zhenyu Wang , Zhao Liu , Dave Hansen Subject: [PATCH] x86/hyperv: Replace kmap() with kmap_local_page() Date: Wed, 28 Sep 2022 17:56:40 +0800 Message-Id: <20220928095640.626350-1-zhao1.liu@linux.intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhao Liu kmap() is being deprecated in favor of kmap_local_page()[1]. There are two main problems with kmap(): (1) It comes with an overhead as mapping space is restricted and protected by a global lock for synchronization and (2) it also requires global TLB invalidation when the kmap's pool wraps and it might block when the mapping space is fully utilized until a slot becomes available. With kmap_local_page() the mappings are per thread, CPU local, can take page faults, and can be called from any context (including interrupts). It is faster than kmap() in kernels with HIGHMEM enabled. Furthermore, the tasks can be preempted and, when they are scheduled to run again, the kernel virtual addresses are restored and are still valid. In the fuction hyperv_init() of hyperv/hv_init.c, the mapping is used in a single thread and is short live. So, in this case, it's safe to simply use kmap_local_page() to create mapping, and this avoids the wasted cost of kmap() for global synchronization. In addtion, the fuction hyperv_init() checks if kmap() fails by BUG_ON(). From the original discussion[2], the BUG_ON() here is just used to explicitly panic NULL pointer. So still keep the BUG_ON() in place to check if kmap_local_page() fails. Based on this consideration, memcpy_to_page() is not selected here but only kmap_local_page() is used. Therefore, replace kmap() with kmap_local_page() in hyperv/hv_init.c. [1]: https://lore.kernel.org/all/20220813220034.806698-1-ira.weiny@intel.com [2]: https://lore.kernel.org/lkml/20200915103710.cqmdvzh5lys4wsqo@liuwe-devbox-debian-v2/ Suggested-by: Dave Hansen Suggested-by: Ira Weiny Suggested-by: Fabio M. De Francesco Signed-off-by: Zhao Liu --- Suggested by credits. Dave: Referred to his comments about whether kmap() can fail and the suggestion to keep BUG_ON() in place. Ira: Referred to his task documentation and review comments about keeping BUG_ON() for kmap_local_page(). Fabio: Stole some of his boiler plate commit message. --- arch/x86/hyperv/hv_init.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index 3de6d8b53367..72fe46eb183f 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -459,13 +459,13 @@ void __init hyperv_init(void) wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); pg = vmalloc_to_page(hv_hypercall_pg); - dst = kmap(pg); + dst = kmap_local_page(pg); src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE, MEMREMAP_WB); BUG_ON(!(src && dst)); memcpy(dst, src, HV_HYP_PAGE_SIZE); memunmap(src); - kunmap(pg); + kunmap_local(dst); } else { hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg); wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); -- 2.34.1