Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp159110imm; Thu, 20 Sep 2018 20:46:34 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdb1tJ/YO3a8hwEdeL75VbzXl4z2O/A2NyMBkiq0J+BZF6PJULKIx0v/83VsIt9xDAuvMCR/ X-Received: by 2002:a62:be03:: with SMTP id l3-v6mr44505316pff.138.1537501594460; Thu, 20 Sep 2018 20:46:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537501594; cv=none; d=google.com; s=arc-20160816; b=x5lZROmQIY2fyIH9RtyriSQdgXoZOgWjWnni21rqUaoBMKr3nBj5bCVOLd6RF9F0eE 0p7+c8+3l4SMLK1U/bOk5GPSqfonYRvYL9O2zd/+TabZeMWI7BLjGWUwFn/2EgAPCUpi VVprWA5upj1qMc5m7jEpAJ1HN8CwG2ZvSyzYpwHE7loONZ8J25MHz2g0xQXR++/rIP6C ftXDwdtBeP5/3iruaq3ALWx0JlEWR3y8/JqkwyZcHXFEmmjgxBRHGJ744L3H4w7TOyG+ I9G9E+/wsTw3y1bs2yILNoSr1q7+tR+ALfF6QEJOeSz2K/7pxNkTLViX5EwaapEFO9Gx Y9gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=OkR0EEmHjd/g+8mpgc80h8botRsNUF/CTCsPMtxMXPY=; b=rg+wgM4b6/xxARKMpV1MyOmXXQccCdlLUDl8BCAmbHCCQ9Ci/u0rOZBOYyQ0FMx1q2 A92/8qUJAHV6f8jri6z64vXV+go9v359rV9AV2hI695M4C9577Wskcfqgs5mnLUl4R9f 0QE3ALHcR/PenthvmXkltwo85aOawjxJWMSifgWBLjie6pfp3YT98k4aHzPpA5BkiEIG Cp+lYDm+u+HvoMZUSTmGB2UqqA6uqXLqYHzQCyLUO8RqVDd8qU937Yezz9uKOJU0jPw3 Jst+k5zfo5nUFovZTEOUJYjwenOy9Lt+jfYqgHcewKfSRdt+W93DrbkeVq+Nw9I/e98B aEyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=t6TGjcvq; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 34-v6si25544031plz.492.2018.09.20.20.46.18; Thu, 20 Sep 2018 20:46:34 -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=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=t6TGjcvq; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389125AbeIUJcv (ORCPT + 99 others); Fri, 21 Sep 2018 05:32:51 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:36802 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388944AbeIUJcv (ORCPT ); Fri, 21 Sep 2018 05:32:51 -0400 Received: by mail-pf1-f195.google.com with SMTP id b11-v6so5344491pfo.3 for ; Thu, 20 Sep 2018 20:45:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amacapital-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=OkR0EEmHjd/g+8mpgc80h8botRsNUF/CTCsPMtxMXPY=; b=t6TGjcvqBVZL8z9FEEXU787pqQTzgSyo1/KICHdPItJpoqwt7+toeMfgxKe4UW9VjJ O4ZPfNktdgWk6oOBp35vNhsYPYE0QH6T05EkajTHcxPTsLxwatKRKBjeMwLfxUAPmjk2 Q3OH7NIen2PMG6SPKVYUTa2X4s2bBpoX/rpFjnzqRVF2wZ3xWAnSIN91QfiBDRiDXLB5 jOcvXBrlahkN3EONobp1gQvgoEvkr8Ux9lI4jlssgIYxwmYalWYkulEieg/3HIhkd69n KRnZbrqTkwY/sf++fzfgu3umutC9sabuHlGkN9dcDgOvKwYFKhyDln2Gsgi3LkTX3nmj /OFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=OkR0EEmHjd/g+8mpgc80h8botRsNUF/CTCsPMtxMXPY=; b=sD39Yb3qNJ2QI6xrgJhTXIX8s7VQoWBz+3Cqs2gv93m7bwpCPAVvQJqepz/ZsSY0bk uDnzgRh7ozI1mnXd8rJDQKyR4pi9Y+kp4ehnCBthQD2HlbbVMnHEYuqZqUION98VIrwD 7H/TBpqEXUYioi99QQLNXr2QJJVgBcdrhYhzg+DHkTwHFAdIT09sPUkhs/bMEu0GkNRX Qws+xql+52IQ3PFC1iCixJglXPR7BPT+Wk3pzxhx1cWfzqfwIOemdiTSFtYZ7U9fb8Jr 4nGf1cY7H7JEv6YAxetquLOuIiwdiFCdchQwEFmHzCRTSlkHVPQTQR58sqZigtSmcUFX XOlQ== X-Gm-Message-State: APzg51DMzaTn47bFyyIHUbSfTmnEFEv65RZXMqr2oZNm+st1iSFWsuIQ ndy+pORnSmXpDMyu928OcyOBrQ== X-Received: by 2002:a63:2fc6:: with SMTP id v189-v6mr39506877pgv.61.1537501558980; Thu, 20 Sep 2018 20:45:58 -0700 (PDT) Received: from ?IPv6:2601:646:c200:7429:413c:2b8f:d2ea:80b3? ([2601:646:c200:7429:413c:2b8f:d2ea:80b3]) by smtp.gmail.com with ESMTPSA id m10-v6sm1133911pgp.94.2018.09.20.20.45.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 20:45:57 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (1.0) Subject: Re: [RFC PATCH 10/10] x86/fpu: defer FPU state load until return to userspace From: Andy Lutomirski X-Mailer: iPhone Mail (15G77) In-Reply-To: <20180919170515.ptqmmpsxrdjsi64j@linutronix.de> Date: Thu, 20 Sep 2018 20:45:56 -0700 Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Andy Lutomirski , Paolo Bonzini , =?utf-8?Q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= , kvm@vger.kernel.org, "Jason A. Donenfeld" , Rik van Riel Content-Transfer-Encoding: quoted-printable Message-Id: References: <20180912133353.20595-1-bigeasy@linutronix.de> <20180912133353.20595-11-bigeasy@linutronix.de> <650FC457-7E4C-473A-9E5F-EAFC74F6444B@amacapital.net> <20180919170515.ptqmmpsxrdjsi64j@linutronix.de> To: Sebastian Andrzej Siewior Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Sep 19, 2018, at 10:05 AM, Sebastian Andrzej Siewior wrote: >=20 > On 2018-09-12 08:47:19 [-0700], Andy Lutomirski wrote: >>> --- a/arch/x86/kernel/fpu/core.c >>> +++ b/arch/x86/kernel/fpu/core.c >>> @@ -101,14 +101,14 @@ void __kernel_fpu_begin(void) >>>=20 >>> kernel_fpu_disable(); >>>=20 >>> - if (fpu->initialized) { >>> + __cpu_invalidate_fpregs_state(); >>> + >>> + if (!test_and_set_thread_flag(TIF_LOAD_FPU)) { >>=20 >> Since the already-TIF_LOAD_FPU path is supposed to be fast here, use test= _thread_flag() instead. test_and_set operations do unconditional RMW operati= ons and are always full barriers, so they=E2=80=99re slow. > okay. >=20 >> Also, on top of this patch, there should be lots of cleanups available. I= n particular, all the fpu state accessors could probably be reworked to take= TIF_LOAD_FPU into account, which would simplify the callers and maybe even t= he mess of variables tracking whether the state is in regs. >=20 > Do you refer to the fpu.initilized check or something else? >=20 I mean the fpu.initialized variable entirely. AFAIK, its only use is for ker= nel threads =E2=80=94 setting it to false lets us switch to a kernel thread a= nd back without saving and restoring. But TIF_LOAD_FPU should be able to rep= lace it: when we have FPU regs loaded and we switch to *any* thread, kernel o= r otherwise, we can set TIF_LOAD_FPU and leave the old regs loaded. So we d= on=E2=80=99t need the special case for kernel threads. Which reminds me: if you haven=E2=80=99t already done so, can you add a help= er to sanity check the current context? It should check that the combinatio= n of owner_ctx, last_cpu, and TIF_LOAD_FPU is sane. For example, if owner_ct= x or last_cpu is says the cpu regs are invalid for current but TIF_LOAD_FPU i= s clear, it should warn. I think that at least switch_fpu_finish should cal= l it. Arguably switch_fpu_prepare should too, at the beginning.=