Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3611269ybl; Fri, 20 Dec 2019 12:17:43 -0800 (PST) X-Google-Smtp-Source: APXvYqwHxQw6fvxjAc1P6ztHdDadGBDKFaREBBLFJ26ehVl0bT35INfaoZ5Rc2soc1nnkRmKaHAx X-Received: by 2002:a9d:461b:: with SMTP id y27mr18350481ote.280.1576873063565; Fri, 20 Dec 2019 12:17:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576873063; cv=none; d=google.com; s=arc-20160816; b=OxTK3P2h9ZAuxnjnaX2lbrVsVFrHrAlRExR4o+gWCiZrGnFdfefhIn05L+CExLEL5M /mBsvS3Q+7VRVk41aQZr8XyqLaoG1ZKKYmrnLOjpfVM662PARyMzkIqXumZacTm5oI+e rq/+kMqe5Xrk4rhp154wIWaWWSYFprRHtm5DKGU8dUa7rtLOb4SzIFnDpKC+6I5/wuY5 BjhCaPU5cUJ5J/0rv04s0WKXFJiZOLgeGxxJwnS01nVVcGyAkWPtDIm2yVHtamI9/BJd 4hvb+YpRkFmlgUP4l+2+jv6LuuCnqXrZMV+27Q/pzgTmd7fmbeGUcqD6I9xrkQTqGtb8 9Ysg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=g6bhJ3O/Kb9OIlUOOCruO4caX87OhdhFS7dhn3GDFOg=; b=a4t2T/0uY13zgvNeHXh0JEBQnKf54gVOs8jX2viruEhENTuVEs7OzcYR+gBubgM5ep rI9PqC/cWo6td54lJ5FWBuotqlCXOSy64ljklTgzDVFew1UXfXX7y8+6NyxUMS7RfT9f e5A0+70yqkt+/a6T6TevZ0GdOeANMxpuG2gwuVNhWp0iW9Pguhj9/N1FnMypa0Wti34o iazgAHuOBZRuUAyfoEgneuTZPeusxZuzlZkzi57eJyK7s4mgNo0jU3xqASbKRTNDuy7s ExIq9I42P665NKJZVVbqK2tcKCEfK0SNKLi5aTOBkx8gIv0vJPpa3VtZYGXZ1Z5w6/R8 giyg== ARC-Authentication-Results: i=1; mx.google.com; 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 f25si5773434oti.84.2019.12.20.12.17.32; Fri, 20 Dec 2019 12:17:43 -0800 (PST) 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; 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 S1727474AbfLTUQ0 (ORCPT + 99 others); Fri, 20 Dec 2019 15:16:26 -0500 Received: from Galois.linutronix.de ([193.142.43.55]:34176 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727402AbfLTUQZ (ORCPT ); Fri, 20 Dec 2019 15:16:25 -0500 Received: from bigeasy by Galois.linutronix.de with local (Exim 4.80) (envelope-from ) id 1iiOhB-00087w-Lp; Fri, 20 Dec 2019 21:16:21 +0100 Date: Fri, 20 Dec 2019 21:16:21 +0100 From: Sebastian Andrzej Siewior To: Yu-cheng Yu Cc: linux-kernel@vger.kernel.org, x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , Dave Hansen , Tony Luck , Andy Lutomirski , Borislav Petkov , Rik van Riel , "Ravi V. Shankar" , Fenghua Yu , Peter Zijlstra Subject: Re: [PATCH v2 3/3] x86/fpu/xstate: Invalidate fpregs when __fpu_restore_sig() fails Message-ID: <20191220201621.riyrptl5vwdukztc@linutronix.de> References: <20191212210855.19260-1-yu-cheng.yu@intel.com> <20191212210855.19260-4-yu-cheng.yu@intel.com> <20191218155449.sk4gjabtynh67jqb@linutronix.de> <587463c4e5fa82dff8748e5f753890ac390e993e.camel@intel.com> <20191219142217.axgpqlb7zzluoxnf@linutronix.de> <19a94f88f1bc66bb81dbf5dd72083d03ca5090e9.camel@intel.com> <20191219171635.phdsfkvsyazwaq7s@linutronix.de> <1607597639a6c6255127fef07704ee9193e33166.camel@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1607597639a6c6255127fef07704ee9193e33166.camel@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019-12-19 09:40:06 [-0800], Yu-cheng Yu wrote: > On Thu, 2019-12-19 at 18:16 +0100, Sebastian Andrzej Siewior wrote: > > On 2019-12-19 08:44:08 [-0800], Yu-cheng Yu wrote: > > > Yes, this works. But then everywhere that calls copy_*_to_xregs_*() etc. needs to be checked. > > > Are there other alternatives? > > > > I don't like the big hammer approach of your very much. It might make > > all it "correct" but then it might lead to more "invalids" then needed. > > It also required to export the symbol which I would like to avoid. > > Copying to registers invalids current fpregs context. It might not cause > extra register loading, because registers are in fact already invalidated > and any task owning the context needs to reload anyway. Setting > fpu_fpregs_owner_ctx is only to let the rest of the kernel know the > fact that already happened. > > But, I agree with you the patch does look biggish. Now that I looked at it: All kernel loads don't fail. If they fail we end up in the handler and restore to init-state. So no need to reset `fpu_fpregs_owner_ctx' in this case. The variable is actually set to task's FPU state so resetting is not required. fpu__save() invokes copy_kernel_to_fpregs() (on older boxes) and by resetting `fpu_fpregs_owner_ctx' we would load it twice (in fpu__save() and on return to userland). So far I can tell, the only problematic case is the signal code because here the state restore *may* fail and we *may* do it in two steps. The error happens only if both `may' are true. > > So if this patch works for you and you don't find anything else where it > > falls apart then I will audit tomorrow all callers which got the > > "invalidator" added and check for that angle. > > Yes, that works for me. Also, most of these call sites are under fpregs_lock(), > and we could use __cpu_invalidate_fpregs_state(). > I was also thinking maybe add warnings when any new code re-introduces the issue, > but not sure where to add that. Do you think that is needed? I was thinking about it. So the `read-FPU-state' function must be invoked within the fpregs_lock() section. This could be easily enforced. At fpregs_unlock() time `fpu_fpregs_owner_ctx' must be NULL or pointing to task's FPU. My brain is fried for today so I'm sure if this is a sane approach. But it might be a start. > Thanks, > Yu-cheng Sebastian