public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Fenghua Yu <fenghua.yu@intel.com>, Borislav Petkov <bp@alien8.de>,
	Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Tony Luck <tony.luck@intel.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	Xiaoyao Li <xiaoyao.li@intel.com>,
	Ravi V Shankar <ravi.v.shankar@intel.com>
Cc: linux-kernel <linux-kernel@vger.kernel.org>, x86 <x86@kernel.org>,
	Fenghua Yu <fenghua.yu@intel.com>
Subject: Re: [PATCH v4 3/4] x86/bus_lock: Set rate limit for bus lock
Date: Wed, 27 Jan 2021 22:57:14 +0100	[thread overview]
Message-ID: <87r1m6dow5.fsf@nanos.tec.linutronix.de> (raw)
In-Reply-To: <20201124205245.4164633-4-fenghua.yu@intel.com>

On Tue, Nov 24 2020 at 20:52, Fenghua Yu wrote:
> To enforce user application throttling or mitigations, extend the
> existing split lock detect kernel parameter:
> 	split_lock_detect=ratelimit:N
>
> It limits bus lock rate to N per second for non-root users.

Yet another changelog which describes what it does without giving some
clue why. It also lacks the information that the ratelimiting is per UID
and not per task and why this was chosen to be per UID...

> Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
> Reviewed-by: Tony Luck <tony.luck@intel.com>
> ---
>  arch/x86/kernel/cpu/intel.c | 35 ++++++++++++++++++++++++++++++-----
>  include/linux/sched/user.h  |  4 +++-
>  kernel/user.c               |  7 +++++++
>  3 files changed, 40 insertions(+), 6 deletions(-)
>
> diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c
> index 77c3e33f41c7..5eb5822a446d 100644
> --- a/arch/x86/kernel/cpu/intel.c
> +++ b/arch/x86/kernel/cpu/intel.c
> @@ -10,6 +10,9 @@
>  #include <linux/thread_info.h>
>  #include <linux/init.h>
>  #include <linux/uaccess.h>
> +#include <linux/cred.h>
> +#include <linux/delay.h>
> +#include <linux/sched/user.h>
>  
>  #include <asm/cpufeature.h>
>  #include <asm/msr.h>
> @@ -40,6 +43,7 @@ enum split_lock_detect_state {
>  	sld_off = 0,
>  	sld_warn,
>  	sld_fatal,
> +	sld_ratelimit,
>  };
>  
>  /*
> @@ -998,13 +1002,25 @@ static const struct {
>  	{ "off",	sld_off   },
>  	{ "warn",	sld_warn  },
>  	{ "fatal",	sld_fatal },
> +	{ "ratelimit:", sld_ratelimit },
>  };
>  
>  static inline bool match_option(const char *arg, int arglen, const char *opt)
>  {
> -	int len = strlen(opt);
>  
> -	return len == arglen && !strncmp(arg, opt, len);
> +	int len = strlen(opt), ratelimit;
> +
> +	if (strncmp(arg, opt, len))
> +		return false;
> +
> +	if (sscanf(arg, "ratelimit:%d", &ratelimit) == 1 && ratelimit > 0 &&
> +	    ratelimit_bl <= HZ / 2) {

That last part condition of the condition is always true because
ratelimit_bl is a global variable in the BSS and nothing else than the
below writes to it. So INT_MAX is a valid argument ....

> +		ratelimit_bl = ratelimit;

Can you please use consistent name spaces? bld_XXX all over the place?

>  static void split_lock_init(void)
>  {
>  	/*
> -	 * If supported, #DB for bus lock will handle warn
> +	 * If supported, #DB for bus lock will handle warn or ratelimit
>  	 * and #AC for split lock is disabled.
>  	 */
> -	if (bld && sld_state == sld_warn) {
> +	if ((bld && sld_state == sld_warn) || sld_state == sld_ratelimit) {

...

> -	if ((regs->flags & X86_EFLAGS_AC) || !sld || sld_state == sld_fatal)
> +	if ((regs->flags & X86_EFLAGS_AC) || !sld || sld_state == sld_fatal ||
> +	    sld_state == sld_ratelimit)

These conditions are uncomprehensible by now

>  		return false;
>  	split_lock_warn(regs->ip);
>  	return true;
> @@ -1168,6 +1185,10 @@ void handle_bus_lock(struct pt_regs *regs)
>  	case sld_fatal:
>  		force_sig_fault(SIGBUS, BUS_ADRALN, NULL);
>  		break;
> +	case sld_ratelimit:
> +		while (!__ratelimit(&get_current_user()->ratelimit_bl))
> +			msleep(1000 / ratelimit_bl);

Yikes. That's really creative abuse of the ratelimit mechanics of course
without any comment how this is supposed to work.

> diff --git a/include/linux/sched/user.h b/include/linux/sched/user.h
> index a8ec3b6093fc..79f95002a123 100644
> --- a/include/linux/sched/user.h
> +++ b/include/linux/sched/user.h
> @@ -40,8 +40,9 @@ struct user_struct {
>  	atomic_t nr_watches;	/* The number of watches this user currently has */
>  #endif
>  
> -	/* Miscellaneous per-user rate limit */
> +	/* Miscellaneous per-user rate limits */
>  	struct ratelimit_state ratelimit;
> +	struct ratelimit_state ratelimit_bl;

Why has every architecture to carry this if only one out of N uses it?

Thanks,

        tglx



  reply	other threads:[~2021-01-27 21:59 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-24 20:52 [PATCH v4 0/4] x86/bus_lock: Enable bus lock detection Fenghua Yu
2020-11-24 20:52 ` [PATCH v4 1/4] x86/cpufeatures: Enumerate #DB for " Fenghua Yu
2021-01-27 21:13   ` Thomas Gleixner
2021-01-27 22:39     ` Yu, Fenghua
2021-01-27 23:23       ` Thomas Gleixner
2020-11-24 20:52 ` [PATCH v4 2/4] x86/bus_lock: Handle warn and fatal in #DB for bus lock Fenghua Yu
2021-01-27 21:16   ` Thomas Gleixner
2020-11-24 20:52 ` [PATCH v4 3/4] x86/bus_lock: Set rate limit " Fenghua Yu
2021-01-27 21:57   ` Thomas Gleixner [this message]
2020-11-24 20:52 ` [PATCH v4 4/4] Documentation/admin-guide: Change doc for split_lock_detect parameter Fenghua Yu
2021-01-27 22:09   ` Thomas Gleixner
2020-12-02 20:36 ` [PATCH v4 0/4] x86/bus_lock: Enable bus lock detection Yu, Fenghua
2021-01-04 19:42 ` Fenghua Yu
2021-01-25 19:27   ` Fenghua Yu

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=87r1m6dow5.fsf@nanos.tec.linutronix.de \
    --to=tglx@linutronix.de \
    --cc=bp@alien8.de \
    --cc=fenghua.yu@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=ravi.v.shankar@intel.com \
    --cc=rdunlap@infradead.org \
    --cc=tony.luck@intel.com \
    --cc=x86@kernel.org \
    --cc=xiaoyao.li@intel.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox