Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1264031pxb; Wed, 6 Apr 2022 13:03:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzHF5fP6c/ILlBrWyRudCO7bkh3/7mNRtSf3xN3dUIhC7V7bIeXVgkIvRSh69xYTT/PcHVL X-Received: by 2002:a17:907:1b10:b0:6e4:dae9:b8e5 with SMTP id mp16-20020a1709071b1000b006e4dae9b8e5mr10042333ejc.470.1649275380337; Wed, 06 Apr 2022 13:03:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649275380; cv=none; d=google.com; s=arc-20160816; b=Bh/Q8Q9tdTMYBLYgftqNmu8w+iK0KVO+qW70H2V0okyV7W7C6MgJvTtcJavxD73jG+ 4WSfPatPkK/SjJMH790I2xb6k+qy4MChnXXPGYMQEl4oO+b12CzUT7yFRfAsWnH5NOS8 UjhdgD/WG8xSlzeVHwVkLb0yRBJZv8WskBoCedAk9kclm5XRm9Sq4Luh2pffVEEzCiSu d9UBf8eWYf26/3B0XVPEaZowkcwCXD7vuoEmgl7eL5Ph/OmlX1x8XfusD7X/MIw56HbR rdJNZVQUucg89ipiFPHbomo3JxAr8maKrr40xQBfaCffmH8Un6V6cViti0kdSXahrxAP Cr7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=pwVDMj/ZX1WG0fUD7lJ3zryiamzy9jGNovg+lKD/C/M=; b=c5C/KFUPeh8Rja4ZFVFGz4qxRMzCrhTiBJ/ZE3GfLyMjVElUfYw5NMVyNFLPWJw90E 2LqlGYp27M3z3uCl5j5GWNOUDAop8kPUS56miWe10OzlvGTdZ7px7Cwn13HwTFZciaCE 5qf4l6cBld/SY1n2p7p9yGFQAVzH40Rafl6yMFTxyIc7yyhfE54D/ZOC3BUsv8ByHFsy 03qgA4hsXqe0e6FpHMj7mfT0+GHIEOF0qUr667/9KxJent+7WyqXDQHDu1ju7Jiw4zfI kLkfiMS822MW1mR+iNcF1PAHHnA5fCpwTRMOCv26/ou19uChsyBcPMIituwrnstKxT1x Kyyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=C6YrU9mY; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h15-20020a1709062dcf00b006df76385cdesi9560544eji.382.2022.04.06.13.02.28; Wed, 06 Apr 2022 13:03:00 -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=@google.com header.s=20210112 header.b=C6YrU9mY; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232640AbiDFTrY (ORCPT + 99 others); Wed, 6 Apr 2022 15:47:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232445AbiDFTqb (ORCPT ); Wed, 6 Apr 2022 15:46:31 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FB16255140 for ; Wed, 6 Apr 2022 11:26:23 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id h23-20020a17090a051700b001c9c1dd3acbso3602138pjh.3 for ; Wed, 06 Apr 2022 11:26:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=pwVDMj/ZX1WG0fUD7lJ3zryiamzy9jGNovg+lKD/C/M=; b=C6YrU9mYfOcQyhqLWy4lavHHbqdo8FGR9wJFW/4SxXtrorfCltRsee/diqEnAVoiau 1/SxeNIcwKvrIfKmpMg8v/7wUedVq85YT0hwjJdbSa75IN0xvkND/ZHoqA+M5t6Q8JiK gUPduAK9F0l4n2O0LC6vbisG61VXkCy7p6hB41NPARLJNEfm4wRol2riIjodg4mSUFjy HB33lIVlkuYgVkFRQWtP5o+fcGVtG0aYtKkxDE1btGSm+QJ6eRAxhrjBV4NNHS5exfGu Xxt4sDDP84MQfr2BhCEYIJ2kYzIF/QjyLw7QWlsmUyTlrdXvqESHBab2zmk8jSI6VTFO 2QAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=pwVDMj/ZX1WG0fUD7lJ3zryiamzy9jGNovg+lKD/C/M=; b=VYoLAU5TIRZR7LiqP+xrlcfJmFeiI2rTN5E9djNvJJPK/2vKLA2z+xPotZFvyFAWLA 0VprC+RlH9pExHW6oy8/aJ9BMhV7ZGz4IbRPvuk4vRIoKoyxiS2YJ0swUYlA9aptnXyV n/d+YVrJ5rodEv7VPt4DZ5vhN2QlOAPGwZMxiBJ/N2iyGT3hRqQDS8+ZcP/yQp38rFM6 /EVdHuyss+XEtxhSqwRVNzlb65CVdbR1Ca5nSGUH3qA1PzZ7Q751NinJV9KKV9a0kOL2 CuDf9xb9fouusi3utLbh7/uprRtuuQZdd23DA9gJHk1iSQWkZxtaPbBesDD1K8OLwDZH 3TdQ== X-Gm-Message-State: AOAM532JLxyhU0Ehar904gjduq9Hy3aDv1VT5ri+4qoFlAimtlgQdY3y fPX5gqlkWK99EXBUYTesLMRgog== X-Received: by 2002:a17:90b:1803:b0:1c7:24c4:ab52 with SMTP id lw3-20020a17090b180300b001c724c4ab52mr11333797pjb.240.1649269582585; Wed, 06 Apr 2022 11:26:22 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id v15-20020a637a0f000000b003994e32c368sm6573684pgc.1.2022.04.06.11.26.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 11:26:21 -0700 (PDT) Date: Wed, 6 Apr 2022 18:26:18 +0000 From: Sean Christopherson To: Mingwei Zhang Cc: Peter Gonda , kvm , LKML Subject: Re: [PATCH] KVM: SEV: Add cond_resched() to loop in sev_clflush_pages() Message-ID: References: <20220330164306.2376085-1-pgonda@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 On Wed, Apr 06, 2022, Mingwei Zhang wrote: > Hi Sean, > > > > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > > > > index 75fa6dd268f0..c2fe89ecdb2d 100644 > > > > --- a/arch/x86/kvm/svm/sev.c > > > > +++ b/arch/x86/kvm/svm/sev.c > > > > @@ -465,6 +465,7 @@ static void sev_clflush_pages(struct page *pages[], unsigned long npages) > > > > page_virtual = kmap_atomic(pages[i]); > > > > clflush_cache_range(page_virtual, PAGE_SIZE); > > > > kunmap_atomic(page_virtual); > > > > + cond_resched(); > > > > > > If you add cond_resched() here, the frequency (once per 4K) might be > > > too high. You may want to do it once per X pages, where X could be > > > something like 1G/4K? > > > > No, every iteration is perfectly ok. The "cond"itional part means that this will > > reschedule if and only if it actually needs to be rescheduled, e.g. if the task's > > timeslice as expired. The check for a needed reschedule is cheap, using > > cond_resched() in tight-ish loops is ok and intended, e.g. KVM does a reched > > check prior to enterring the guest. > > Double check on the code again. I think the point is not about flag > checking. Obviously branch prediction could really help. The point I > think is the 'call' to cond_resched(). Depending on the kernel > configuration, cond_resched() may not always be inlined, at least this > is my understanding so far? So if that is true, then it still might > not always be the best to call cond_resched() that often. Eh, compared to the cost of 64 back-to-back CLFLUSHOPTs, the cost of __cond_resched() is peanuts. Even accounting for the rcu_all_qs() work, it's still dwarfed by the cost of flushing data from the cache. E.g. based on Agner Fog's wonderful uop latencies[*], the actual flush time for a single page is going to be upwards of 10k cycles, whereas __cond_resched() is going to well under 100 cycles in the happy case of no work. Even if those throughput numbers are off by an order of magnitude, e.g. CLFLUSHOPT can complete in 15 cycles, that's still ~1k cycles. Peter, don't we also theoretically need cond_resched() in the loops in sev_launch_update_data()? AFAICT, there's no articifical restriction on the size of the payload, i.e. the kernel is effectively relying on userspace to not update large swaths of memory. [*] https://www.agner.org/optimize/instruction_tables.pdf