Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp29737imm; Fri, 21 Sep 2018 17:25:54 -0700 (PDT) X-Google-Smtp-Source: ACcGV61769MAV2CCwdEE8b4xsWphHGsAwTFefoxiEy3F/Q7MXsU8tOnvf/k77RRL0HlBMUnKbGpR X-Received: by 2002:a17:902:9893:: with SMTP id s19-v6mr134762plp.130.1537575954023; Fri, 21 Sep 2018 17:25:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537575953; cv=none; d=google.com; s=arc-20160816; b=zkn45+zgGTniu4MWs+9Ifhckw8c07D/3HFOsedvOntdyKVwSYESXJ0ZOTH3gZ3xbsf Ut2WlLlf02N22w76rpCeJS8LoNrlD9zRe0aYWxFfYiZpIscRuBuM+fEPg8WgTK2RfF0z iMqnCF+6T7Wu37faMS6+/HhRq5I23kWe7l5pTcFqMwKG67f8r/mOQES/pp9BUrtsBvm5 sr9/Gt7uDqoM8TQIlZRKiNQgTGm/LLeXGKBBk4FUIkNSauwXs2WteqwobfniadxizXH8 iiG4CRYnHuF2x9mU/zVqCMq97IiXhbtPQZpjiXZvmslsGFi2rT/2A+6DnKiEbthwZfIO 5KwQ== 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:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=2LgJWLMs8yo5FowLlvkTfcdTp6XQC9dr0FQAc/W0KlA=; b=C34YZpH0gmQN6m0ZSe8QYygwOYiubPIDEGXkB0jgKHC+UTdQuxfBsd7vU0213DaWLX h18wo5kh2oeIzfA3FlGPPfYFWXzue6hYDzAVqkDTa9d5+ZlYdxT4BdvtyGpyc2/w3iVV YnmIuSMueuk8rEDQjWlGWSFQAUyTxMwCGa778kiFJc6b1G+dMcyeQswX5Ys0UwLvGTFh VziNlVO+LAtocB4OcvIBktPJ7mcgE6gv3rjOT6oQwLaTjxK5MR5MYJ4qDNQoDsGNJtkJ RCzztBjNWdlQm1akASrnUrW+mzUXa8Li02dXF84QCYqfK3O4Pte29dAzzxKSGRTZ2jjo 9j3A== 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 i126-v6si28849712pgd.332.2018.09.21.17.25.38; Fri, 21 Sep 2018 17:25:53 -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; 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 S2391982AbeIVGKr (ORCPT + 99 others); Sat, 22 Sep 2018 02:10:47 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:44142 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391931AbeIVGKp (ORCPT ); Sat, 22 Sep 2018 02:10:45 -0400 Received: from [2a02:8011:400e:2:cbab:f00:c93f:614] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1g3Vdv-0008BP-4a; Sat, 22 Sep 2018 01:19:27 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1g3Vdp-0000uM-5q; Sat, 22 Sep 2018 01:19:21 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Kyle Huey" , "Andy Lutomirski" , "Kyle Huey" , "Peter Zijlstra" , "Thomas Gleixner" Date: Sat, 22 Sep 2018 01:15:42 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 58/63] x86/process: Optimize TIF checks in __switch_to_xtra() In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:cbab:f00:c93f:614 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.58-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Kyle Huey commit af8b3cd3934ec60f4c2a420d19a9d416554f140b upstream. Help the compiler to avoid reevaluating the thread flags for each checked bit by reordering the bit checks and providing an explicit xor for evaluation. With default defconfigs for each arch, x86_64: arch/x86/kernel/process.o text data bss dec hex 3056 8577 16 11649 2d81 Before 3024 8577 16 11617 2d61 After i386: arch/x86/kernel/process.o text data bss dec hex 2957 8673 8 11638 2d76 Before 2925 8673 8 11606 2d56 After Originally-by: Thomas Gleixner Signed-off-by: Kyle Huey Cc: Peter Zijlstra Cc: Andy Lutomirski Link: http://lkml.kernel.org/r/20170214081104.9244-2-khuey@kylehuey.com Signed-off-by: Thomas Gleixner [bwh: Backported to 3.16: - We don't do refresh_tr_limit() here - Use ACCESS_ONCE() instead of READ_ONCE()] Signed-off-by: Ben Hutchings --- arch/x86/kernel/process.c | 54 ++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 23 deletions(-) --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -196,48 +196,56 @@ int set_tsc_mode(unsigned int val) return 0; } +static inline void switch_to_bitmap(struct tss_struct *tss, + struct thread_struct *prev, + struct thread_struct *next, + unsigned long tifp, unsigned long tifn) +{ + if (tifn & _TIF_IO_BITMAP) { + /* + * Copy the relevant range of the IO bitmap. + * Normally this is 128 bytes or less: + */ + memcpy(tss->io_bitmap, next->io_bitmap_ptr, + max(prev->io_bitmap_max, next->io_bitmap_max)); + } else if (tifp & _TIF_IO_BITMAP) { + /* + * Clear any possible leftover bits: + */ + memset(tss->io_bitmap, 0xff, prev->io_bitmap_max); + } +} + void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p, struct tss_struct *tss) { struct thread_struct *prev, *next; + unsigned long tifp, tifn; prev = &prev_p->thread; next = &next_p->thread; - if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^ - test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) { + tifn = ACCESS_ONCE(task_thread_info(next_p)->flags); + tifp = ACCESS_ONCE(task_thread_info(prev_p)->flags); + switch_to_bitmap(tss, prev, next, tifp, tifn); + + propagate_user_return_notify(prev_p, next_p); + + if ((tifp ^ tifn) & _TIF_BLOCKSTEP) { unsigned long debugctl = get_debugctlmsr(); debugctl &= ~DEBUGCTLMSR_BTF; - if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) + if (tifn & _TIF_BLOCKSTEP) debugctl |= DEBUGCTLMSR_BTF; - update_debugctlmsr(debugctl); } - if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^ - test_tsk_thread_flag(next_p, TIF_NOTSC)) { - /* prev and next are different */ - if (test_tsk_thread_flag(next_p, TIF_NOTSC)) + if ((tifp ^ tifn) & _TIF_NOTSC) { + if (tifn & _TIF_NOTSC) hard_disable_TSC(); else hard_enable_TSC(); } - - if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) { - /* - * Copy the relevant range of the IO bitmap. - * Normally this is 128 bytes or less: - */ - memcpy(tss->io_bitmap, next->io_bitmap_ptr, - max(prev->io_bitmap_max, next->io_bitmap_max)); - } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) { - /* - * Clear any possible leftover bits: - */ - memset(tss->io_bitmap, 0xff, prev->io_bitmap_max); - } - propagate_user_return_notify(prev_p, next_p); } /*