Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1140399imm; Fri, 15 Jun 2018 11:54:30 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIJ82QdBZtxADX7n8fndlmjkoToupArKaNhs8knV/RkPH7l9leBjL8Y7OZUFxc0WAdFkEwY X-Received: by 2002:a63:9c02:: with SMTP id f2-v6mr2665016pge.16.1529088870526; Fri, 15 Jun 2018 11:54:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529088870; cv=none; d=google.com; s=arc-20160816; b=BYhbJj2PPPMs8nUbE05eVYnjd6fm0gYceHNR9KJeriQYig/sdJxuCNpz4WvuTtVV/X KppNuPnPeTEcOr/jsSDHsMVp0zLXK5pa0rXy7nYSn7FGfS1l8Afoh863F67QMo/jLUIF ZoFWf/wBaVov2c6J0H3srHfv14nRRwApklV6IKbG7Kyk+cXFngKGeXz2SF5lbmp7rfV1 koIX0jm5rsoo+3Ovds+X2uPbppt8DVXqvU34aYnmlCwsRGOiMBocGBMThS+PzI+A4v4q ZvzmOh/KC89F2yoeJTgHPkBse+yoxsk9uyV3hx6iZgAYsi7ETTWBrRR8iioyhG0VMc9j /Q/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature :arc-authentication-results; bh=wxabzD4CmbL6Q0yvpuLYxFwwdmPunanzEq+aDxUHRV8=; b=t2cQPr4ksFTIdaEPoQY2RJnPEBZX+D4Km0fTxyxySBdUUWZk3Ab66YdPM0RsqJfoI2 E4ZnYIx5RucWEHO99UukUhCxIzHKdkC/PHryBGRF4iT7TMg4VXDwXUzJ0x7cgQWxaJku xAd3IWvvd4JUjBAghteIDXnw85L3fruBBzUyyOMVXGiEtrvE+tHeCYbl0+KClvQCiEMj pI+8iMPegWB5SYgxUV28K7iOrO/2oXf8vFF41BvFYNGLC4hM8xFJAGMmhtdJMY/PH8Kb cDEihbllIydqUL+KRxZko5Xu/VJHJ1jjMjujretPYp+at215pZq8/9zMqAgw7t/VfkQK 3/jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bORExDWy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k5-v6si3471664plt.178.2018.06.15.11.54.15; Fri, 15 Jun 2018 11:54:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bORExDWy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756409AbeFOSxy (ORCPT + 99 others); Fri, 15 Jun 2018 14:53:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:35656 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756346AbeFOSxx (ORCPT ); Fri, 15 Jun 2018 14:53:53 -0400 Received: from mail-wr0-f171.google.com (mail-wr0-f171.google.com [209.85.128.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 69148208B2 for ; Fri, 15 Jun 2018 18:53:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1529088832; bh=WGR9HA8fQDs7GgeeCC/tRoXoKNMmT8OokD1iIyVHJqU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=bORExDWyaKRQIEIjBJEiWwO+sIyp7gkSCI00Kfr3JrO//6UVOSbVoAHcQ/39NC45q UldzKOH4XtIc6uoPKTvV+tGfuwTCKULuS2nLoxlItP0Cm9b6VM3KCYVkjS+6vz40yX TCKVBNQQVK5sFrffKCT65yjqfw1szY0m8etFrjf0= Received: by mail-wr0-f171.google.com with SMTP id w10-v6so10849838wrk.9 for ; Fri, 15 Jun 2018 11:53:52 -0700 (PDT) X-Gm-Message-State: APt69E1qFBjnFrI0SKPhO73b84rTx2uBDFTcBVXuAN9R/4+WkuimuQ1/ AtBrneHQcDmsy7FInRTiZr019yQvBR3ErkwXcVp/dg== X-Received: by 2002:adf:b445:: with SMTP id v5-v6mr2571414wrd.67.1529088830896; Fri, 15 Jun 2018 11:53:50 -0700 (PDT) MIME-Version: 1.0 References: <6eecf873-9d87-5345-70ba-5c064a31714b@linux.intel.com> In-Reply-To: <6eecf873-9d87-5345-70ba-5c064a31714b@linux.intel.com> From: Andy Lutomirski Date: Fri, 15 Jun 2018 11:53:39 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Lazy FPU restoration / moving kernel_fpu_end() to context switch To: Dave Hansen Cc: Andrew Lutomirski , "Jason A. Donenfeld" , Rik van Riel , LKML , X86 ML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 15, 2018 at 11:50 AM Dave Hansen wrote: > > On 06/15/2018 11:31 AM, Andy Lutomirski wrote: > > for (thing) { > > kernel_fpu_begin(); > > encrypt(thing); > > kernel_fpu_end(); > > } > > Don't forget that the processor has optimizations for this, too. The > "modified optimization" will notice that between: > > kernel_fpu_end(); -> XRSTOR > and > kernel_fpu_start(); -> XSAVE(S|OPT) > > the processor has not modified the states. It'll skip doing any writes > of the state. Doing what Andy is describing is still way better than > letting the processor do it, but you should just know up front that this > may not be as much of a win as you would expect. Even with the modified optimization, kernel_fpu_end() still needs to reload the state that was trashed by the kernel FPU use. If the kernel is using something like AVX512 state, then kernel_fpu_end() will transfer an enormous amount of data no matter how clever the CPU is. And I think I once measured XSAVEOPT taking a hundred cycles or so even when RFBM==0, so it's not exactly super fast.