Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp811295pxb; Fri, 22 Apr 2022 11:39:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxMuEIO5x/tEkBWFixHU/U+mKtddK9bCCD1KcqI67if+1TeGBFEfwGwrNpA6EBgC2XZJpCx X-Received: by 2002:a05:6a00:2349:b0:4fa:934f:f6db with SMTP id j9-20020a056a00234900b004fa934ff6dbmr6387872pfj.44.1650652793171; Fri, 22 Apr 2022 11:39:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650652793; cv=none; d=google.com; s=arc-20160816; b=klMgBSz6N9bgOFVUcOaOzFqaoD/1qvJY0U2Otedd0uu5zrRmYNTbGaCwcu2vyBRszG DXdIuy0ClvkbaXfeRTASq9daWmWkLjUq5wpXtenRwsrZm5Smy05SFGASXJVXMkNrf/Ds jTuoMfs2z4QROL+9ZgcC/bVtxntQpR7sRbBk/cNZVdNwDlrfRgkgF4osJXpkLQkqYqLF Bhqb1+KBADs0f0gjAP8cXjvCopVHYTKzS9Rjlxwi6tAO5qCrp44qUUqy7BT1x9SLDpP/ kmI81SG80bc9TGE3V1o0dkM6G+vzTvn0Nu5iOrnu8xYjgupVjndfsbnI1bI3vQpg3pPC XNaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=zQXdcZYLTgL+7c/dGrHyarz4xtqMzDlgUfL99vE41bw=; b=fsDBhOzIiHGOZE2IWvujBE7xeK2s78fVkhV7QtflgrG5ynlt4Qszws/WqVHTCLt6Nh JH6MXn2DyPZyKVhRJ0eAVpKDR+7bKSbJxhiN9KOMMbu8FjMxzUA3u12T5dYkMG12ALOB QxJ2ta1nGWN4kEgFuSL9QSt0NG1yPVKHkvy23VlpOcvZ78hHu1hE3vkuZ18XdAsVj9v5 rlnYG8/FzEL+Jb886D8A80DRnyM8Zdo99unfU0SMMG75+E0zgtq1K+9SO/XG6OFG1UJM BhSayLoYoykDKyH3xvx3XHl0lpBI5hdWUxWP0V7yVMxBtzC5ZGinz0y5NJaB2w+GSHU2 QKYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=CsuOgFLY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id h10-20020a65518a000000b0039da21013b0si9197724pgq.250.2022.04.22.11.39.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 11:39:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=CsuOgFLY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 66578EF3E4; Fri, 22 Apr 2022 11:09:51 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1389494AbiDUOi2 (ORCPT + 99 others); Thu, 21 Apr 2022 10:38:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1389448AbiDUOiO (ORCPT ); Thu, 21 Apr 2022 10:38:14 -0400 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA4B813D34 for ; Thu, 21 Apr 2022 07:35:24 -0700 (PDT) Received: by mail-lf1-x12b.google.com with SMTP id y32so9068355lfa.6 for ; Thu, 21 Apr 2022 07:35:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=zQXdcZYLTgL+7c/dGrHyarz4xtqMzDlgUfL99vE41bw=; b=CsuOgFLYG0kT9MEviWVke1X8+c5ACXlhwdM9tDmBQpbSbsD37e/e4qon8Z8O/dowBo f5wTnk3tt2CsqrBryUFjvwRjxyGejeUqrte7wcrDselQYWxV+SQdGSGOEwmaw3XX1rVA 9m+0UqM9CWGCNZUQrdqD9PaiJK7pXtsnPU/XxzjtfDn0bY7kXduy+B6EkqQEnbjLb12v G/izCo80AETHSWzU4FEfzk+hR1xkcak0rleedpiExdSCUPHOkBZ0yNYKmX558k/73rgj KdrGBIjHDDOj1Jx4GsPzY58EUouSLyIhB5XHwoFryjfOELTGS5zL+42nijLRUhh/DiyY UouQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=zQXdcZYLTgL+7c/dGrHyarz4xtqMzDlgUfL99vE41bw=; b=Lg3gYHbUj6howWxytPBC3FONycZ9PncI7G5rXk7qi9PRBYNIsYP4UvzBtNP+nla2Ro MXnXOUd4IdV/x+5wgXRXFsku+dqIIGPxsruK6EOLk6H29QOGekDuZCBL8ESXz60ZjvAr s/v/my+Fx0JqhKKKHBADjf3vyWE0e4aZtXcUCj1Ps17rJDZRUmA8a7NJO/uBFKIMmVfg 9Z6yHBWQpRRIVOtm4fbfApiKTTVuT+w6eYu5U+oi3SCtJYqM9hhdj9VGJ0gqEe5R+HzE QZE9Gk2bLQ/tSt0A5Fu+70NOrulCEXb6oUBCrwqhh1EH+8djhybc6lF2z9/3uE64oGMD 4hVA== X-Gm-Message-State: AOAM530NqHLFgC40Yay7tLk476ttm0Jyu54EA9UrTN4f1BQ2WtIx8D/B KRxOhkKLbudrgHHfVBws48Yng6JSf71Tnz8/CLO4WQ== X-Received: by 2002:a05:6512:c01:b0:448:6aec:65c5 with SMTP id z1-20020a0565120c0100b004486aec65c5mr18960186lfu.193.1650551722806; Thu, 21 Apr 2022 07:35:22 -0700 (PDT) MIME-Version: 1.0 References: <20220330164306.2376085-1-pgonda@google.com> In-Reply-To: From: Peter Gonda Date: Thu, 21 Apr 2022 08:35:11 -0600 Message-ID: Subject: Re: [PATCH] KVM: SEV: Add cond_resched() to loop in sev_clflush_pages() To: Sean Christopherson Cc: Mingwei Zhang , kvm , LKML Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,USER_IN_DEF_DKIM_WL autolearn=no 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 Mon, Apr 18, 2022 at 9:48 AM Sean Christopherson wrote: > > On Wed, Apr 06, 2022, Peter Gonda wrote: > > On Wed, Apr 6, 2022 at 12:26 PM Sean Christopherson wrote: > > > > > > 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. > > > > Yea we probably do want to cond_resched() in the for loop inside of > > sev_launch_update_data(). Ithink in sev_dbg_crypt() userspace could > > request a large number of pages to be decrypted/encrypted for > > debugging but se have a call to sev_pin_memory() in the loop so that > > will have a cond_resded() inside of __get_users_pages(). Or should we > > have a cond_resded() inside of the loop in sev_dbg_crypt() too? > > I believe sev_dbg_crypt() needs a cond_resched() of its own, sev_pin_memory() > isn't guaranteed to get into the slow path of internal_get_user_pages_fast(). Ah, understood thanks. I'll send out patches for those two paths. I personally haven't seen any warning logs from them though.