All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: "Luck\, Tony" <tony.luck@intel.com>,
	Arvind Sankar <nivedita@alum.mit.edu>
Cc: "Christopherson\, Sean J" <sean.j.christopherson@intel.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@kernel.org>, "Yu\,
	Fenghua" <fenghua.yu@intel.com>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>, H Peter Anvin <hpa@zytor.com>,
	"Raj\, Ashok" <ashok.raj@intel.com>, "Shankar\,
	Ravi V" <ravi.v.shankar@intel.com>,
	linux-kernel <linux-kernel@vger.kernel.org>, x86 <x86@kernel.org>
Subject: Re: [PATCH v14] x86/split_lock: Enable split lock detection by kernel
Date: Fri, 24 Jan 2020 22:36:03 +0100	[thread overview]
Message-ID: <87h80kmta4.fsf@nanos.tec.linutronix.de> (raw)
In-Reply-To: <20200123231652.GA4457@agluck-desk2.amr.corp.intel.com>

Tony,

"Luck, Tony" <tony.luck@intel.com> writes:
> +	split_lock_detect=
> +			[X86] Enable split lock detection
> +
> +			When enabled (and if hardware support is present), atomic
> +			instructions that access data across cache line
> +			boundaries will result in an alignment check exception.
> +
> +			off	- not enabled
> +
> +			warn	- the kernel will pr_alert about applications

pr_alert is not a verb. And the implementation uses
pr_warn_ratelimited(). So this should be something like:

                       The kernel will emit rate limited warnings about
                       applications ...

> +				  triggering the #AC exception
> @@ -40,4 +40,21 @@ int mwait_usable(const struct cpuinfo_x86 *);
>  unsigned int x86_family(unsigned int sig);
>  unsigned int x86_model(unsigned int sig);
>  unsigned int x86_stepping(unsigned int sig);
> +#ifdef CONFIG_CPU_SUP_INTEL
> +extern void __init cpu_set_core_cap_bits(struct cpuinfo_x86 *c);
> +extern bool split_lock_detect_enabled(void);

That function is unused.

> +extern bool handle_user_split_lock(struct pt_regs *regs, long error_code);
> +extern void switch_sld(struct task_struct *);
> +#else
> +static inline void __init cpu_set_core_cap_bits(struct cpuinfo_x86 *c) {}
> +static inline bool split_lock_detect_enabled(void)
> +{
> +	return false;
> +}
> +static inline bool handle_user_split_lock(struct pt_regs *regs, long error_code)
> +{
> +	return false;
> +}
> +static inline void switch_sld(struct task_struct *prev) {}
> +#endif
  
> +enum split_lock_detect_state {
> +	sld_off = 0,
> +	sld_warn,
> +	sld_fatal,
> +};
> +
> +/*
> + * Default to sld_off because most systems do not support
> + * split lock detection. split_lock_setup() will switch this

Can you please add: If supported, then ...

> + * to sld_warn, and then check to see if there is a command
> + * line override.

I had to read this 3 times and then stare at the code.

> + */
> +static enum split_lock_detect_state sld_state = sld_off;
> +
> +static void __init split_lock_setup(void)
> +{
> +	enum split_lock_detect_state sld;
> +	char arg[20];
> +	int i, ret;
> +
> +	sld_state = sld = sld_warn;

This intermediate variable is pointless.

> +	setup_force_cpu_cap(X86_FEATURE_SPLIT_LOCK_DETECT);
> +
> +	ret = cmdline_find_option(boot_command_line, "split_lock_detect",
> +				  arg, sizeof(arg));
> +	if (ret < 0)
> +		goto print;
> +
> +	for (i = 0; i < ARRAY_SIZE(sld_options); i++) {
> +		if (match_option(arg, ret, sld_options[i].option)) {
> +			sld = sld_options[i].state;
> +			break;
> +		}
> +	}
> +
> +	if (sld != sld_state)
> +		sld_state = sld;
> +
> +print:

> +/*
> + * The TEST_CTRL MSR is per core. So multiple threads can
> + * read/write the MSR in parallel. But it's possible to
> + * simplify the read/write without locking and without
> + * worry about overwriting the MSR because only bit 29
> + * is implemented in the MSR and the bit is set as 1 by all
> + * threads. Locking may be needed in the future if situation
> + * is changed e.g. other bits are implemented.

This sentence doesn't parse. Something like this perhaps:

     Locking is not required at the moment because only bit 29 of this
     MSR is implemented and locking would not prevent that the operation
     of one thread is immediately undone by the sibling thread.

This implies that locking might become necessary when new bits are added.

> + */
> +
> +static bool __sld_msr_set(bool on)
> +{
> +	u64 test_ctrl_val;
> +
> +	if (rdmsrl_safe(MSR_TEST_CTRL, &test_ctrl_val))
> +		return false;
> +
> +	if (on)
> +		test_ctrl_val |= MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
> +	else
> +		test_ctrl_val &= ~MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
> +
> +	if (wrmsrl_safe(MSR_TEST_CTRL, test_ctrl_val))
> +		return false;
> +
> +	return true;

	return !wrmsrl_safe(MSR_TEST_CTRL, test_ctrl_val);

> +}
> +
> +static void split_lock_init(void)
> +{
> +	if (sld_state == sld_off)
> +		return;
> +
> +	if (__sld_msr_set(true))
> +		return;
> +
> +	/*
> +	 * If this is anything other than the boot-cpu, you've done
> +	 * funny things and you get to keep whatever pieces.
> +	 */
> +	pr_warn("MSR fail -- disabled\n");
> +	__sld_msr_set(sld_off);

That should do:

        sld_state = sld_off;

for consistency sake.

> +}
> +
> +bool split_lock_detect_enabled(void)
> +{
> +	return sld_state != sld_off;
> +}
> +
> +bool handle_user_split_lock(struct pt_regs *regs, long error_code)
> +{
> +	if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
> +		return false;
> +
> +	pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
> +		 current->comm, current->pid, regs->ip);

So with 10 prints per 5 seconds an intentional offender can still fill dmesg
pretty good. A standard dmesg buffer should be full of this in
~15min. Not a big issue, but it might be annoying. Let's start with this
and deal with it when people complain.

The magic below really lacks a comment. Something like:

	/*
         * Disable the split lock detection for this task so it can make
         * progress and set TIF_SLD so the detection is reenabled via
         * switch_to_sld() when the task is scheduled out.
         */

> +	__sld_msr_set(false);
> +	set_tsk_thread_flag(current, TIF_SLD);
> +	return true;
> +}
> +
> +void switch_sld(struct task_struct *prev)

switch_to_sld() perhaps?

> +{
> +	__sld_msr_set(true);
> +	clear_tsk_thread_flag(prev, TIF_SLD);
> +}
>  
> +dotraplinkage void do_alignment_check(struct pt_regs *regs, long error_code)
> +{
> +	const char str[] = "alignment check";
> +
> +	RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
> +
> +	if (notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_AC, SIGBUS) == NOTIFY_STOP)
> +		return;
> +
> +	if (!user_mode(regs))
> +		die("Split lock detected\n", regs, error_code);
> +
> +	cond_local_irq_enable(regs);

This cond is pointless. We recently removed the ability for user space
to disable interrupts and even if that would still be allowed then
keeping interrupts disabled here does not make sense.

Other than those details, I really like this approach.

Thanks,

        tglx

  reply	other threads:[~2020-01-24 21:36 UTC|newest]

Thread overview: 145+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-21  0:53 [PATCH v10 0/6] Enable split lock detection for real time and debug Fenghua Yu
2019-11-21  0:53 ` [PATCH v10 1/6] x86/msr-index: Add two new MSRs Fenghua Yu
2019-11-21  0:53 ` [PATCH v10 2/6] x86/cpufeatures: Enumerate the IA32_CORE_CAPABILITIES MSR Fenghua Yu
2019-11-21  0:53 ` [PATCH v10 3/6] x86/split_lock: Enumerate split lock detection by " Fenghua Yu
2019-11-21  0:53 ` [PATCH v10 4/6] x86/split_lock: Enumerate split lock detection if the IA32_CORE_CAPABILITIES MSR is not supported Fenghua Yu
2019-11-21 22:07   ` Andy Lutomirski
2019-11-22  0:37     ` Fenghua Yu
2019-11-22  2:13       ` Andy Lutomirski
2019-11-22  9:46         ` Peter Zijlstra
2019-11-21  0:53 ` [PATCH v10 5/6] x86/split_lock: Handle #AC exception for split lock Fenghua Yu
2019-11-21 22:10   ` Andy Lutomirski
2019-11-21 23:14     ` Fenghua Yu
2019-11-21 23:12       ` Andy Lutomirski
2019-11-21  0:53 ` [PATCH v10 6/6] x86/split_lock: Enable split lock detection by kernel parameter Fenghua Yu
2019-11-21  6:04   ` Ingo Molnar
2019-11-21 13:01     ` Peter Zijlstra
2019-11-21 13:15       ` Peter Zijlstra
2019-11-21 21:51         ` Luck, Tony
2019-11-21 22:24           ` Andy Lutomirski
2019-11-21 22:29             ` Luck, Tony
2019-11-21 23:18               ` Andy Lutomirski
2019-11-21 23:53                 ` Fenghua Yu
2019-11-22  1:52                   ` Sean Christopherson
2019-11-22  2:21                     ` Andy Lutomirski
2019-11-22  2:39                       ` Xiaoyao Li
2019-11-22  2:57                         ` Andy Lutomirski
2019-11-21 23:55                 ` Luck, Tony
2019-11-22  0:55             ` Luck, Tony
2019-11-22 10:08           ` Peter Zijlstra
2019-11-21 16:14       ` Fenghua Yu
2019-11-21 17:14         ` Ingo Molnar
2019-11-21 17:35         ` Peter Zijlstra
2019-11-21 17:12       ` Ingo Molnar
2019-11-21 17:34         ` Luck, Tony
2019-11-22 10:51           ` Peter Zijlstra
2019-11-22 15:27             ` Peter Zijlstra
2019-11-22 17:22               ` Luck, Tony
2019-11-22 20:23                 ` Peter Zijlstra
2019-11-22 18:02               ` Luck, Tony
2019-11-22 20:23                 ` Peter Zijlstra
2019-11-22 20:42                   ` Fenghua Yu
2019-11-22 21:25                     ` Andy Lutomirski
2019-12-12  8:57                       ` Peter Zijlstra
2019-12-12 18:52                         ` Luck, Tony
2019-12-12 19:46                           ` Luck, Tony
2019-12-12 20:01                             ` Andy Lutomirski
2019-12-16 16:21                               ` David Laight
2019-11-22 18:44               ` Sean Christopherson
2019-11-22 20:30                 ` Peter Zijlstra
2019-11-23  0:30               ` Luck, Tony
2019-11-25 16:13                 ` Sean Christopherson
2019-12-02 18:20                   ` Luck, Tony
2019-12-12  8:59                   ` Peter Zijlstra
2020-01-10 19:24                     ` [PATCH v11] x86/split_lock: Enable split lock detection by kernel Luck, Tony
2020-01-14  5:55                       ` Sean Christopherson
2020-01-15 22:27                         ` Luck, Tony
2020-01-15 22:57                           ` Sean Christopherson
2020-01-15 23:48                             ` Luck, Tony
2020-01-22 18:55                             ` [PATCH v12] " Luck, Tony
2020-01-22 19:04                               ` Borislav Petkov
2020-01-22 20:03                                 ` Luck, Tony
2020-01-22 20:55                                   ` Borislav Petkov
2020-01-22 22:42                               ` Arvind Sankar
2020-01-22 22:52                                 ` Arvind Sankar
2020-01-22 23:24                                 ` Luck, Tony
2020-01-23  0:45                                   ` Arvind Sankar
2020-01-23  1:23                                     ` Luck, Tony
2020-01-23  4:21                                       ` Arvind Sankar
2020-01-23 17:15                                         ` Luck, Tony
2020-01-23  3:53                                     ` [PATCH v13] " Luck, Tony
2020-01-23  4:45                                       ` Arvind Sankar
2020-01-23 23:16                                         ` [PATCH v14] " Luck, Tony
2020-01-24 21:36                                           ` Thomas Gleixner [this message]
2020-01-25  2:47                                             ` [PATCH v15] " Luck, Tony
2020-01-25 10:44                                               ` Borislav Petkov
2020-01-25 19:55                                                 ` Luck, Tony
2020-01-25 20:12                                                   ` Peter Zijlstra
2020-01-25 20:33                                                     ` Borislav Petkov
2020-01-25 21:42                                                       ` Luck, Tony
2020-01-25 22:17                                                         ` Borislav Petkov
2020-01-25 20:29                                                   ` Borislav Petkov
2020-01-25 13:41                                               ` Thomas Gleixner
2020-01-25 22:07                                                 ` [PATCH v16] " Luck, Tony
2020-01-25 22:43                                                   ` Mark D Rustad
2020-01-25 23:10                                                     ` Luck, Tony
2020-01-26 17:27                                                       ` Mark D Rustad
2020-01-26 20:05                                                         ` [PATCH v17] " Luck, Tony
2020-01-29 12:31                                                           ` Thomas Gleixner
2020-01-29 15:24                                                           ` [tip: x86/cpu] " tip-bot2 for Peter Zijlstra (Intel)
2020-02-03 20:41                                                           ` [PATCH v17] " Sean Christopherson
2020-02-06  0:49                                                             ` [PATCH] x86/split_lock: Avoid runtime reads of the TEST_CTRL MSR Luck, Tony
2020-02-06  1:18                                                               ` Andy Lutomirski
2020-02-06 16:46                                                                 ` Luck, Tony
2020-02-06 19:37                                                                   ` Andy Lutomirski
2020-03-03 19:22                                                                     ` Sean Christopherson
2020-02-04  0:04                                                           ` [PATCH v17] x86/split_lock: Enable split lock detection by kernel Sean Christopherson
2020-02-04 12:52                                                             ` Thomas Gleixner
2020-01-26  0:34                                                   ` [PATCH v16] " Andy Lutomirski
2020-01-26 20:01                                                     ` Luck, Tony
2020-01-25 21:25                                               ` [PATCH v15] " Arvind Sankar
2020-01-25 21:50                                                 ` Luck, Tony
2020-01-25 23:51                                                   ` Arvind Sankar
2020-01-26  2:52                                                     ` Luck, Tony
2020-01-27  2:05                                                       ` Tony Luck
2020-01-27  8:04                                                   ` Peter Zijlstra
2020-01-27  8:36                                                     ` Peter Zijlstra
2020-01-27 17:35                                                     ` Luck, Tony
2020-01-27  8:02                                                 ` Peter Zijlstra
2019-12-13  0:09               ` [PATCH v11] x86/split_lock: Enable split lock detection by kernel parameter Tony Luck
2019-12-13  0:16                 ` Luck, Tony
2019-11-21 17:43         ` [PATCH v10 6/6] " David Laight
2019-11-21 17:51           ` Andy Lutomirski
2019-11-21 18:53             ` Fenghua Yu
2019-11-21 19:01               ` Andy Lutomirski
2019-11-21 20:25                 ` Fenghua Yu
2019-11-21 20:19                   ` Peter Zijlstra
2019-11-21 19:46               ` Peter Zijlstra
2019-11-21 20:25               ` Peter Zijlstra
2019-11-21 21:22                 ` Andy Lutomirski
2019-11-22  9:25                   ` Peter Zijlstra
2019-11-22 17:48                     ` Luck, Tony
2019-11-22 20:31                       ` Peter Zijlstra
2019-11-22 21:23                         ` Andy Lutomirski
2019-12-11 17:52                           ` Peter Zijlstra
2019-12-11 18:12                             ` Andy Lutomirski
2019-12-11 22:34                               ` Peter Zijlstra
2019-12-12 19:40                                 ` Andy Lutomirski
2019-12-16  9:59                                   ` David Laight
2019-12-16 17:22                                     ` Andy Lutomirski
2019-12-16 17:45                                       ` David Laight
2019-12-16 18:06                                         ` Andy Lutomirski
2019-12-17 10:03                                           ` David Laight
2019-12-11 18:44                             ` Luck, Tony
2019-12-11 22:39                               ` Peter Zijlstra
2019-12-12 10:36                                 ` David Laight
2019-12-12 13:04                                   ` Peter Zijlstra
2019-12-12 16:02                                     ` Andy Lutomirski
2019-12-12 16:23                                       ` David Laight
2019-12-12 16:29                                     ` David Laight
2019-11-21 19:56             ` Peter Zijlstra
2019-11-21 21:01               ` Andy Lutomirski
2019-11-22  9:36                 ` Peter Zijlstra
2019-11-22  9:46             ` David Laight
2019-11-22 20:32               ` Peter Zijlstra
2019-11-21  8:00   ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h80kmta4.fsf@nanos.tec.linutronix.de \
    --to=tglx@linutronix.de \
    --cc=ashok.raj@intel.com \
    --cc=bp@alien8.de \
    --cc=fenghua.yu@intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.com \
    --cc=nivedita@alum.mit.edu \
    --cc=peterz@infradead.org \
    --cc=ravi.v.shankar@intel.com \
    --cc=sean.j.christopherson@intel.com \
    --cc=tony.luck@intel.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.