Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1267184pxb; Wed, 6 Apr 2022 13:08:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZKuYl5cAxJDp2FiWoPwXYs+MCuKaMi/+rm1L+4NsG/hmfrVuSAQD53OqD/h0kGRT+eXkL X-Received: by 2002:a05:6402:1941:b0:413:2822:9c8 with SMTP id f1-20020a056402194100b00413282209c8mr10624867edz.13.1649275702207; Wed, 06 Apr 2022 13:08:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649275702; cv=none; d=google.com; s=arc-20160816; b=SJVClvt4iUHiKRFLk1vlowLE0YzfmrXYAnehORYOEOFj8is0mIeruQLZUs78W0PBNH YipD0gxLPN+Hsj6aNBKE2mEB5I+mHDpP9Q7DAlZS6PF3fHa3E5+vtdQs1OcLpI/CBXC+ uHlY9SiLyzlQ+TFIwgf6cjg9vRELdGoOz04vqR0ZFYavMZXEyNDDIT+YxjSdZ0u2NzNU pX9VaNWxW1gp2S0FQuIqE+KXQwz96DxMIUKR6wWP/av/dKWb77DQDvAHvoi1BbBONS+c 507TPS6dGwmjjer3Ca/CfVNBOgxkFOOlX5CGdWRK2poDQftwQ3HW0kOsEJMioZ+mGMfr tYVA== 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=HSwp1BDG0k7X/+K+RwJOW/sVCMMxMSEnJISgNBGi2bg=; b=Pyg7Fxk015sG3H0FCjpp8zLSrDtRQjoa24TIh3PtSaxo1MhAbq7Myzs2uYXLmJXrnR 4u/B+mcp1kpLTJRMes0o4ofJaxuM39b+Y+hNByDPI6aji+ItMMkUX1TafzitssOVjPFZ xR25PTC893gLay/ZiSBD9IrHwwYGRlFzi+iwoBYH8n5Dwh4ULXCoMU6eP7pXksGTGfvz fga/ReCM6cyQpTk0AFRJqzz8NQkER60q/lzqfMtCG8DB4gGd6Olg8ncXmjcnuMYM+1j6 j35zxswMD+7oEaZe0r0O+PstTPaPgZmF5NASLl/vsqhxJgyvCW4Pqo9/PMbUjhvkzghh EFSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="Ys/1WSnG"; 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 m4-20020a17090607c400b006dfd7c152fbsi11930401ejc.103.2022.04.06.13.07.57; Wed, 06 Apr 2022 13:08:22 -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="Ys/1WSnG"; 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 S232788AbiDFTvV (ORCPT + 99 others); Wed, 6 Apr 2022 15:51:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232774AbiDFTup (ORCPT ); Wed, 6 Apr 2022 15:50:45 -0400 Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAD1C18649C for ; Wed, 6 Apr 2022 11:34:32 -0700 (PDT) Received: by mail-lj1-x22f.google.com with SMTP id h11so4445540ljb.2 for ; Wed, 06 Apr 2022 11:34:32 -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=HSwp1BDG0k7X/+K+RwJOW/sVCMMxMSEnJISgNBGi2bg=; b=Ys/1WSnGhYSt2OK9wL+F2ZyDI0KN3bD3nkQ7cSEAfHt1sAJ+3HACDKvwFqmgSO0McZ QFScanM5uUJaQB9Dg7NYmvPo2C65E+TWz5N6SYM8r9Roi/wG86Rh7LzBhbgvInT42DFQ 7Qn1qPwmokjVsWFL7xBCD7dR5M2pNWfz9LutvoFMBLrdy9yPU0pbuHgbnhyGep2+PxlF B+WXQTlzC15jmupuOPCyOclnMpoFIGqpDpOFln7aOg1ado+Q4UuO8F1YYm+el0+4BD9G 8Afv9mb1mWYGKWv/eaktdDgf0tWjnHCB0QK0RS14sBPVGz+3igXdnZum3nr2wGmSxHnv Op4w== 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=HSwp1BDG0k7X/+K+RwJOW/sVCMMxMSEnJISgNBGi2bg=; b=N7PzB23zjc33DdUsb9uHBosKwona8noknznFzxjbSp+6ZodjTCKgrRA7jPNSmm81lC tkBcfNffmh3T4MSUuC/53QxngKpulVsOuad3WZm4FzGNRPezEq2iFwy22BdzaSalfy4x Dlu8rQr7IOcg6wstTYdel5jYf3OWJDFG7xFwnLJoGvf52umhQAsN2Rf1+XTML00vyL5J mJeb2aEUXVMLFXNzJP4NHtF1hq4GzYteJ5CSPxVViEHeLSUm0MsLufb3X/GxysD0VNhJ KIgRAGhmAEXnp+K2917v7NdyvXterBZFQCWIaOxFQPOGKgpeiG3jPpQbS8jnA7sd9xZO 8GHw== X-Gm-Message-State: AOAM532paex0uAPA6IdGxisHF5+lfI7v0eEg7X+unEuSonFQ6VjXjIr9 Q5jL9rfVTkj4VbLn3axJpTu35d16ke4B3XfXhB8wKw== X-Received: by 2002:a05:651c:b1e:b0:249:95d3:7832 with SMTP id b30-20020a05651c0b1e00b0024995d37832mr6253661ljr.426.1649270070676; Wed, 06 Apr 2022 11:34:30 -0700 (PDT) MIME-Version: 1.0 References: <20220330164306.2376085-1-pgonda@google.com> In-Reply-To: From: Peter Gonda Date: Wed, 6 Apr 2022 12:34:19 -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=-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 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? > > [*] https://www.agner.org/optimize/instruction_tables.pdf